1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
246
247#define REVISION "Revision: 3.20"
248#define VERSION "Id: cdrom.c 3.20 2003/12/17"
249
250
251
252#define CD_NOTHING 0x0
253#define CD_WARNING 0x1
254#define CD_REG_UNREG 0x2
255#define CD_DO_IOCTL 0x4
256#define CD_OPEN 0x8
257#define CD_CLOSE 0x10
258#define CD_COUNT_TRACKS 0x20
259#define CD_CHANGER 0x40
260#define CD_DVD 0x80
261
262
263
264#define ERRLOGMASK CD_WARNING
265
266
267
268#include <linux/atomic.h>
269#include <linux/module.h>
270#include <linux/fs.h>
271#include <linux/major.h>
272#include <linux/types.h>
273#include <linux/errno.h>
274#include <linux/kernel.h>
275#include <linux/mm.h>
276#include <linux/slab.h>
277#include <linux/cdrom.h>
278#include <linux/sysctl.h>
279#include <linux/proc_fs.h>
280#include <linux/blkpg.h>
281#include <linux/init.h>
282#include <linux/fcntl.h>
283#include <linux/blkdev.h>
284#include <linux/times.h>
285#include <linux/uaccess.h>
286#include <scsi/scsi_common.h>
287#include <scsi/scsi_request.h>
288
289
290static bool debug;
291
292static bool autoclose=1;
293static bool autoeject;
294static bool lockdoor = 1;
295
296static bool check_media_type;
297
298static bool mrw_format_restart = 1;
299module_param(debug, bool, 0);
300module_param(autoclose, bool, 0);
301module_param(autoeject, bool, 0);
302module_param(lockdoor, bool, 0);
303module_param(check_media_type, bool, 0);
304module_param(mrw_format_restart, bool, 0);
305
306static DEFINE_MUTEX(cdrom_mutex);
307
308static const char *mrw_format_status[] = {
309 "not mrw",
310 "bgformat inactive",
311 "bgformat active",
312 "mrw complete",
313};
314
315static const char *mrw_address_space[] = { "DMA", "GAA" };
316
317#if (ERRLOGMASK != CD_NOTHING)
318#define cd_dbg(type, fmt, ...) \
319do { \
320 if ((ERRLOGMASK & type) || debug == 1) \
321 pr_debug(fmt, ##__VA_ARGS__); \
322} while (0)
323#else
324#define cd_dbg(type, fmt, ...) \
325do { \
326 if (0 && (ERRLOGMASK & type) || debug == 1) \
327 pr_debug(fmt, ##__VA_ARGS__); \
328} while (0)
329#endif
330
331
332
333#define CDROM_CAN(type) (cdi->ops->capability & ~cdi->mask & (type))
334
335
336
337
338
339#define CDROM_DEF_TIMEOUT (7 * HZ)
340
341
342
343static void cdrom_sysctl_register(void);
344
345static LIST_HEAD(cdrom_list);
346
347int cdrom_dummy_generic_packet(struct cdrom_device_info *cdi,
348 struct packet_command *cgc)
349{
350 if (cgc->sshdr) {
351 cgc->sshdr->sense_key = 0x05;
352 cgc->sshdr->asc = 0x20;
353 cgc->sshdr->ascq = 0x00;
354 }
355
356 cgc->stat = -EIO;
357 return -EIO;
358}
359EXPORT_SYMBOL(cdrom_dummy_generic_packet);
360
361static int cdrom_flush_cache(struct cdrom_device_info *cdi)
362{
363 struct packet_command cgc;
364
365 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
366 cgc.cmd[0] = GPCMD_FLUSH_CACHE;
367
368 cgc.timeout = 5 * 60 * HZ;
369
370 return cdi->ops->generic_packet(cdi, &cgc);
371}
372
373
374static int cdrom_get_disc_info(struct cdrom_device_info *cdi,
375 disc_information *di)
376{
377 const struct cdrom_device_ops *cdo = cdi->ops;
378 struct packet_command cgc;
379 int ret, buflen;
380
381
382 init_cdrom_command(&cgc, di, sizeof(*di), CGC_DATA_READ);
383 cgc.cmd[0] = GPCMD_READ_DISC_INFO;
384 cgc.cmd[8] = cgc.buflen = 2;
385 cgc.quiet = 1;
386
387 ret = cdo->generic_packet(cdi, &cgc);
388 if (ret)
389 return ret;
390
391
392
393
394 buflen = be16_to_cpu(di->disc_information_length) +
395 sizeof(di->disc_information_length);
396
397 if (buflen > sizeof(disc_information))
398 buflen = sizeof(disc_information);
399
400 cgc.cmd[8] = cgc.buflen = buflen;
401 ret = cdo->generic_packet(cdi, &cgc);
402 if (ret)
403 return ret;
404
405
406 return buflen;
407}
408
409
410
411
412
413
414#define ENSURE(cdo, call, bits) \
415do { \
416 if (cdo->call == NULL) \
417 WARN_ON_ONCE((cdo)->capability & (bits)); \
418} while (0)
419
420
421
422
423
424static int cdrom_mrw_probe_pc(struct cdrom_device_info *cdi)
425{
426 struct packet_command cgc;
427 char buffer[16];
428
429 init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_READ);
430
431 cgc.timeout = HZ;
432 cgc.quiet = 1;
433
434 if (!cdrom_mode_sense(cdi, &cgc, MRW_MODE_PC, 0)) {
435 cdi->mrw_mode_page = MRW_MODE_PC;
436 return 0;
437 } else if (!cdrom_mode_sense(cdi, &cgc, MRW_MODE_PC_PRE1, 0)) {
438 cdi->mrw_mode_page = MRW_MODE_PC_PRE1;
439 return 0;
440 }
441
442 return 1;
443}
444
445static int cdrom_is_mrw(struct cdrom_device_info *cdi, int *write)
446{
447 struct packet_command cgc;
448 struct mrw_feature_desc *mfd;
449 unsigned char buffer[16];
450 int ret;
451
452 *write = 0;
453
454 init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_READ);
455
456 cgc.cmd[0] = GPCMD_GET_CONFIGURATION;
457 cgc.cmd[3] = CDF_MRW;
458 cgc.cmd[8] = sizeof(buffer);
459 cgc.quiet = 1;
460
461 if ((ret = cdi->ops->generic_packet(cdi, &cgc)))
462 return ret;
463
464 mfd = (struct mrw_feature_desc *)&buffer[sizeof(struct feature_header)];
465 if (be16_to_cpu(mfd->feature_code) != CDF_MRW)
466 return 1;
467 *write = mfd->write;
468
469 if ((ret = cdrom_mrw_probe_pc(cdi))) {
470 *write = 0;
471 return ret;
472 }
473
474 return 0;
475}
476
477static int cdrom_mrw_bgformat(struct cdrom_device_info *cdi, int cont)
478{
479 struct packet_command cgc;
480 unsigned char buffer[12];
481 int ret;
482
483 pr_info("%sstarting format\n", cont ? "Re" : "");
484
485
486
487
488 init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_WRITE);
489 cgc.cmd[0] = GPCMD_FORMAT_UNIT;
490 cgc.cmd[1] = (1 << 4) | 1;
491
492 cgc.timeout = 5 * 60 * HZ;
493
494
495
496
497 buffer[1] = 1 << 1;
498 buffer[3] = 8;
499
500
501
502
503 buffer[4] = 0xff;
504 buffer[5] = 0xff;
505 buffer[6] = 0xff;
506 buffer[7] = 0xff;
507
508 buffer[8] = 0x24 << 2;
509 buffer[11] = cont;
510
511 ret = cdi->ops->generic_packet(cdi, &cgc);
512 if (ret)
513 pr_info("bgformat failed\n");
514
515 return ret;
516}
517
518static int cdrom_mrw_bgformat_susp(struct cdrom_device_info *cdi, int immed)
519{
520 struct packet_command cgc;
521
522 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
523 cgc.cmd[0] = GPCMD_CLOSE_TRACK;
524
525
526
527
528 cgc.cmd[1] = !!immed;
529 cgc.cmd[2] = 1 << 1;
530
531 cgc.timeout = 5 * 60 * HZ;
532
533 return cdi->ops->generic_packet(cdi, &cgc);
534}
535
536static int cdrom_mrw_exit(struct cdrom_device_info *cdi)
537{
538 disc_information di;
539 int ret;
540
541 ret = cdrom_get_disc_info(cdi, &di);
542 if (ret < 0 || ret < (int)offsetof(typeof(di),disc_type))
543 return 1;
544
545 ret = 0;
546 if (di.mrw_status == CDM_MRW_BGFORMAT_ACTIVE) {
547 pr_info("issuing MRW background format suspend\n");
548 ret = cdrom_mrw_bgformat_susp(cdi, 0);
549 }
550
551 if (!ret && cdi->media_written)
552 ret = cdrom_flush_cache(cdi);
553
554 return ret;
555}
556
557static int cdrom_mrw_set_lba_space(struct cdrom_device_info *cdi, int space)
558{
559 struct packet_command cgc;
560 struct mode_page_header *mph;
561 char buffer[16];
562 int ret, offset, size;
563
564 init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_READ);
565
566 cgc.buffer = buffer;
567 cgc.buflen = sizeof(buffer);
568
569 ret = cdrom_mode_sense(cdi, &cgc, cdi->mrw_mode_page, 0);
570 if (ret)
571 return ret;
572
573 mph = (struct mode_page_header *)buffer;
574 offset = be16_to_cpu(mph->desc_length);
575 size = be16_to_cpu(mph->mode_data_length) + 2;
576
577 buffer[offset + 3] = space;
578 cgc.buflen = size;
579
580 ret = cdrom_mode_select(cdi, &cgc);
581 if (ret)
582 return ret;
583
584 pr_info("%s: mrw address space %s selected\n",
585 cdi->name, mrw_address_space[space]);
586 return 0;
587}
588
589int register_cdrom(struct cdrom_device_info *cdi)
590{
591 static char banner_printed;
592 const struct cdrom_device_ops *cdo = cdi->ops;
593
594 cd_dbg(CD_OPEN, "entering register_cdrom\n");
595
596 if (cdo->open == NULL || cdo->release == NULL)
597 return -EINVAL;
598 if (!banner_printed) {
599 pr_info("Uniform CD-ROM driver " REVISION "\n");
600 banner_printed = 1;
601 cdrom_sysctl_register();
602 }
603
604 ENSURE(cdo, drive_status, CDC_DRIVE_STATUS);
605 if (cdo->check_events == NULL && cdo->media_changed == NULL)
606 WARN_ON_ONCE(cdo->capability & (CDC_MEDIA_CHANGED | CDC_SELECT_DISC));
607 ENSURE(cdo, tray_move, CDC_CLOSE_TRAY | CDC_OPEN_TRAY);
608 ENSURE(cdo, lock_door, CDC_LOCK);
609 ENSURE(cdo, select_speed, CDC_SELECT_SPEED);
610 ENSURE(cdo, get_last_session, CDC_MULTI_SESSION);
611 ENSURE(cdo, get_mcn, CDC_MCN);
612 ENSURE(cdo, reset, CDC_RESET);
613 ENSURE(cdo, generic_packet, CDC_GENERIC_PACKET);
614 cdi->mc_flags = 0;
615 cdi->options = CDO_USE_FFLAGS;
616
617 if (autoclose == 1 && CDROM_CAN(CDC_CLOSE_TRAY))
618 cdi->options |= (int) CDO_AUTO_CLOSE;
619 if (autoeject == 1 && CDROM_CAN(CDC_OPEN_TRAY))
620 cdi->options |= (int) CDO_AUTO_EJECT;
621 if (lockdoor == 1)
622 cdi->options |= (int) CDO_LOCK;
623 if (check_media_type == 1)
624 cdi->options |= (int) CDO_CHECK_TYPE;
625
626 if (CDROM_CAN(CDC_MRW_W))
627 cdi->exit = cdrom_mrw_exit;
628
629 if (cdi->disk)
630 cdi->cdda_method = CDDA_BPC_FULL;
631 else
632 cdi->cdda_method = CDDA_OLD;
633
634 WARN_ON(!cdo->generic_packet);
635
636 cd_dbg(CD_REG_UNREG, "drive \"/dev/%s\" registered\n", cdi->name);
637 mutex_lock(&cdrom_mutex);
638 list_add(&cdi->list, &cdrom_list);
639 mutex_unlock(&cdrom_mutex);
640 return 0;
641}
642#undef ENSURE
643
644void unregister_cdrom(struct cdrom_device_info *cdi)
645{
646 cd_dbg(CD_OPEN, "entering unregister_cdrom\n");
647
648 mutex_lock(&cdrom_mutex);
649 list_del(&cdi->list);
650 mutex_unlock(&cdrom_mutex);
651
652 if (cdi->exit)
653 cdi->exit(cdi);
654
655 cd_dbg(CD_REG_UNREG, "drive \"/dev/%s\" unregistered\n", cdi->name);
656}
657
658int cdrom_get_media_event(struct cdrom_device_info *cdi,
659 struct media_event_desc *med)
660{
661 struct packet_command cgc;
662 unsigned char buffer[8];
663 struct event_header *eh = (struct event_header *)buffer;
664
665 init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_READ);
666 cgc.cmd[0] = GPCMD_GET_EVENT_STATUS_NOTIFICATION;
667 cgc.cmd[1] = 1;
668 cgc.cmd[4] = 1 << 4;
669 cgc.cmd[8] = sizeof(buffer);
670 cgc.quiet = 1;
671
672 if (cdi->ops->generic_packet(cdi, &cgc))
673 return 1;
674
675 if (be16_to_cpu(eh->data_len) < sizeof(*med))
676 return 1;
677
678 if (eh->nea || eh->notification_class != 0x4)
679 return 1;
680
681 memcpy(med, &buffer[sizeof(*eh)], sizeof(*med));
682 return 0;
683}
684
685static int cdrom_get_random_writable(struct cdrom_device_info *cdi,
686 struct rwrt_feature_desc *rfd)
687{
688 struct packet_command cgc;
689 char buffer[24];
690 int ret;
691
692 init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_READ);
693
694 cgc.cmd[0] = GPCMD_GET_CONFIGURATION;
695 cgc.cmd[3] = CDF_RWRT;
696 cgc.cmd[8] = sizeof(buffer);
697 cgc.quiet = 1;
698
699 if ((ret = cdi->ops->generic_packet(cdi, &cgc)))
700 return ret;
701
702 memcpy(rfd, &buffer[sizeof(struct feature_header)], sizeof (*rfd));
703 return 0;
704}
705
706static int cdrom_has_defect_mgt(struct cdrom_device_info *cdi)
707{
708 struct packet_command cgc;
709 char buffer[16];
710 __be16 *feature_code;
711 int ret;
712
713 init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_READ);
714
715 cgc.cmd[0] = GPCMD_GET_CONFIGURATION;
716 cgc.cmd[3] = CDF_HWDM;
717 cgc.cmd[8] = sizeof(buffer);
718 cgc.quiet = 1;
719
720 if ((ret = cdi->ops->generic_packet(cdi, &cgc)))
721 return ret;
722
723 feature_code = (__be16 *) &buffer[sizeof(struct feature_header)];
724 if (be16_to_cpu(*feature_code) == CDF_HWDM)
725 return 0;
726
727 return 1;
728}
729
730
731static int cdrom_is_random_writable(struct cdrom_device_info *cdi, int *write)
732{
733 struct rwrt_feature_desc rfd;
734 int ret;
735
736 *write = 0;
737
738 if ((ret = cdrom_get_random_writable(cdi, &rfd)))
739 return ret;
740
741 if (CDF_RWRT == be16_to_cpu(rfd.feature_code))
742 *write = 1;
743
744 return 0;
745}
746
747static int cdrom_media_erasable(struct cdrom_device_info *cdi)
748{
749 disc_information di;
750 int ret;
751
752 ret = cdrom_get_disc_info(cdi, &di);
753 if (ret < 0 || ret < offsetof(typeof(di), n_first_track))
754 return -1;
755
756 return di.erasable;
757}
758
759
760
761
762static int cdrom_dvdram_open_write(struct cdrom_device_info *cdi)
763{
764 int ret = cdrom_media_erasable(cdi);
765
766
767
768
769
770 if (!ret)
771 return 1;
772
773 return 0;
774}
775
776static int cdrom_mrw_open_write(struct cdrom_device_info *cdi)
777{
778 disc_information di;
779 int ret;
780
781
782
783
784 if (cdrom_mrw_set_lba_space(cdi, MRW_LBA_DMA)) {
785 pr_err("failed setting lba address space\n");
786 return 1;
787 }
788
789 ret = cdrom_get_disc_info(cdi, &di);
790 if (ret < 0 || ret < offsetof(typeof(di),disc_type))
791 return 1;
792
793 if (!di.erasable)
794 return 1;
795
796
797
798
799
800
801
802
803 ret = 0;
804 pr_info("open: mrw_status '%s'\n", mrw_format_status[di.mrw_status]);
805 if (!di.mrw_status)
806 ret = 1;
807 else if (di.mrw_status == CDM_MRW_BGFORMAT_INACTIVE &&
808 mrw_format_restart)
809 ret = cdrom_mrw_bgformat(cdi, 1);
810
811 return ret;
812}
813
814static int mo_open_write(struct cdrom_device_info *cdi)
815{
816 struct packet_command cgc;
817 char buffer[255];
818 int ret;
819
820 init_cdrom_command(&cgc, &buffer, 4, CGC_DATA_READ);
821 cgc.quiet = 1;
822
823
824
825
826
827
828 ret = cdrom_mode_sense(cdi, &cgc, GPMODE_ALL_PAGES, 0);
829 if (ret)
830 ret = cdrom_mode_sense(cdi, &cgc, GPMODE_VENDOR_PAGE, 0);
831 if (ret) {
832 cgc.buflen = 255;
833 ret = cdrom_mode_sense(cdi, &cgc, GPMODE_ALL_PAGES, 0);
834 }
835
836
837 if (ret)
838 return 0;
839
840 return buffer[3] & 0x80;
841}
842
843static int cdrom_ram_open_write(struct cdrom_device_info *cdi)
844{
845 struct rwrt_feature_desc rfd;
846 int ret;
847
848 if ((ret = cdrom_has_defect_mgt(cdi)))
849 return ret;
850
851 if ((ret = cdrom_get_random_writable(cdi, &rfd)))
852 return ret;
853 else if (CDF_RWRT == be16_to_cpu(rfd.feature_code))
854 ret = !rfd.curr;
855
856 cd_dbg(CD_OPEN, "can open for random write\n");
857 return ret;
858}
859
860static void cdrom_mmc3_profile(struct cdrom_device_info *cdi)
861{
862 struct packet_command cgc;
863 char buffer[32];
864 int ret, mmc3_profile;
865
866 init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_READ);
867
868 cgc.cmd[0] = GPCMD_GET_CONFIGURATION;
869 cgc.cmd[1] = 0;
870 cgc.cmd[2] = cgc.cmd[3] = 0;
871 cgc.cmd[8] = sizeof(buffer);
872 cgc.quiet = 1;
873
874 if ((ret = cdi->ops->generic_packet(cdi, &cgc)))
875 mmc3_profile = 0xffff;
876 else
877 mmc3_profile = (buffer[6] << 8) | buffer[7];
878
879 cdi->mmc3_profile = mmc3_profile;
880}
881
882static int cdrom_is_dvd_rw(struct cdrom_device_info *cdi)
883{
884 switch (cdi->mmc3_profile) {
885 case 0x12:
886 case 0x1A:
887 case 0x43:
888 return 0;
889 default:
890 return 1;
891 }
892}
893
894
895
896
897static int cdrom_open_write(struct cdrom_device_info *cdi)
898{
899 int mrw, mrw_write, ram_write;
900 int ret = 1;
901
902 mrw = 0;
903 if (!cdrom_is_mrw(cdi, &mrw_write))
904 mrw = 1;
905
906 if (CDROM_CAN(CDC_MO_DRIVE))
907 ram_write = 1;
908 else
909 (void) cdrom_is_random_writable(cdi, &ram_write);
910
911 if (mrw)
912 cdi->mask &= ~CDC_MRW;
913 else
914 cdi->mask |= CDC_MRW;
915
916 if (mrw_write)
917 cdi->mask &= ~CDC_MRW_W;
918 else
919 cdi->mask |= CDC_MRW_W;
920
921 if (ram_write)
922 cdi->mask &= ~CDC_RAM;
923 else
924 cdi->mask |= CDC_RAM;
925
926 if (CDROM_CAN(CDC_MRW_W))
927 ret = cdrom_mrw_open_write(cdi);
928 else if (CDROM_CAN(CDC_DVD_RAM))
929 ret = cdrom_dvdram_open_write(cdi);
930 else if (CDROM_CAN(CDC_RAM) &&
931 !CDROM_CAN(CDC_CD_R|CDC_CD_RW|CDC_DVD|CDC_DVD_R|CDC_MRW|CDC_MO_DRIVE))
932 ret = cdrom_ram_open_write(cdi);
933 else if (CDROM_CAN(CDC_MO_DRIVE))
934 ret = mo_open_write(cdi);
935 else if (!cdrom_is_dvd_rw(cdi))
936 ret = 0;
937
938 return ret;
939}
940
941static void cdrom_dvd_rw_close_write(struct cdrom_device_info *cdi)
942{
943 struct packet_command cgc;
944
945 if (cdi->mmc3_profile != 0x1a) {
946 cd_dbg(CD_CLOSE, "%s: No DVD+RW\n", cdi->name);
947 return;
948 }
949
950 if (!cdi->media_written) {
951 cd_dbg(CD_CLOSE, "%s: DVD+RW media clean\n", cdi->name);
952 return;
953 }
954
955 pr_info("%s: dirty DVD+RW media, \"finalizing\"\n", cdi->name);
956
957 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
958 cgc.cmd[0] = GPCMD_FLUSH_CACHE;
959 cgc.timeout = 30*HZ;
960 cdi->ops->generic_packet(cdi, &cgc);
961
962 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
963 cgc.cmd[0] = GPCMD_CLOSE_TRACK;
964 cgc.timeout = 3000*HZ;
965 cgc.quiet = 1;
966 cdi->ops->generic_packet(cdi, &cgc);
967
968 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
969 cgc.cmd[0] = GPCMD_CLOSE_TRACK;
970 cgc.cmd[2] = 2;
971 cgc.quiet = 1;
972 cgc.timeout = 3000*HZ;
973 cdi->ops->generic_packet(cdi, &cgc);
974
975 cdi->media_written = 0;
976}
977
978static int cdrom_close_write(struct cdrom_device_info *cdi)
979{
980#if 0
981 return cdrom_flush_cache(cdi);
982#else
983 return 0;
984#endif
985}
986
987
988static void cdrom_count_tracks(struct cdrom_device_info *cdi, tracktype *tracks)
989{
990 struct cdrom_tochdr header;
991 struct cdrom_tocentry entry;
992 int ret, i;
993 tracks->data = 0;
994 tracks->audio = 0;
995 tracks->cdi = 0;
996 tracks->xa = 0;
997 tracks->error = 0;
998 cd_dbg(CD_COUNT_TRACKS, "entering cdrom_count_tracks\n");
999
1000 if (!CDROM_CAN(CDC_PLAY_AUDIO)) {
1001 tracks->error = CDS_NO_INFO;
1002 return;
1003 }
1004
1005
1006 ret = cdi->ops->audio_ioctl(cdi, CDROMREADTOCHDR, &header);
1007 if (ret) {
1008 if (ret == -ENOMEDIUM)
1009 tracks->error = CDS_NO_DISC;
1010 else
1011 tracks->error = CDS_NO_INFO;
1012 return;
1013 }
1014
1015 entry.cdte_format = CDROM_MSF;
1016 for (i = header.cdth_trk0; i <= header.cdth_trk1; i++) {
1017 entry.cdte_track = i;
1018 if (cdi->ops->audio_ioctl(cdi, CDROMREADTOCENTRY, &entry)) {
1019 tracks->error = CDS_NO_INFO;
1020 return;
1021 }
1022 if (entry.cdte_ctrl & CDROM_DATA_TRACK) {
1023 if (entry.cdte_format == 0x10)
1024 tracks->cdi++;
1025 else if (entry.cdte_format == 0x20)
1026 tracks->xa++;
1027 else
1028 tracks->data++;
1029 } else {
1030 tracks->audio++;
1031 }
1032 cd_dbg(CD_COUNT_TRACKS, "track %d: format=%d, ctrl=%d\n",
1033 i, entry.cdte_format, entry.cdte_ctrl);
1034 }
1035 cd_dbg(CD_COUNT_TRACKS, "disc has %d tracks: %d=audio %d=data %d=Cd-I %d=XA\n",
1036 header.cdth_trk1, tracks->audio, tracks->data,
1037 tracks->cdi, tracks->xa);
1038}
1039
1040static
1041int open_for_data(struct cdrom_device_info *cdi)
1042{
1043 int ret;
1044 const struct cdrom_device_ops *cdo = cdi->ops;
1045 tracktype tracks;
1046 cd_dbg(CD_OPEN, "entering open_for_data\n");
1047
1048
1049 if (cdo->drive_status != NULL) {
1050 ret = cdo->drive_status(cdi, CDSL_CURRENT);
1051 cd_dbg(CD_OPEN, "drive_status=%d\n", ret);
1052 if (ret == CDS_TRAY_OPEN) {
1053 cd_dbg(CD_OPEN, "the tray is open...\n");
1054
1055 if (CDROM_CAN(CDC_CLOSE_TRAY) &&
1056 cdi->options & CDO_AUTO_CLOSE) {
1057 cd_dbg(CD_OPEN, "trying to close the tray\n");
1058 ret=cdo->tray_move(cdi,0);
1059 if (ret) {
1060 cd_dbg(CD_OPEN, "bummer. tried to close the tray but failed.\n");
1061
1062
1063
1064
1065
1066 ret=-ENOMEDIUM;
1067 goto clean_up_and_return;
1068 }
1069 } else {
1070 cd_dbg(CD_OPEN, "bummer. this drive can't close the tray.\n");
1071 ret=-ENOMEDIUM;
1072 goto clean_up_and_return;
1073 }
1074
1075 ret = cdo->drive_status(cdi, CDSL_CURRENT);
1076 if ((ret == CDS_NO_DISC) || (ret==CDS_TRAY_OPEN)) {
1077 cd_dbg(CD_OPEN, "bummer. the tray is still not closed.\n");
1078 cd_dbg(CD_OPEN, "tray might not contain a medium\n");
1079 ret=-ENOMEDIUM;
1080 goto clean_up_and_return;
1081 }
1082 cd_dbg(CD_OPEN, "the tray is now closed\n");
1083 }
1084
1085 ret = cdo->drive_status(cdi, CDSL_CURRENT);
1086 if (ret!=CDS_DISC_OK) {
1087 ret = -ENOMEDIUM;
1088 goto clean_up_and_return;
1089 }
1090 }
1091 cdrom_count_tracks(cdi, &tracks);
1092 if (tracks.error == CDS_NO_DISC) {
1093 cd_dbg(CD_OPEN, "bummer. no disc.\n");
1094 ret=-ENOMEDIUM;
1095 goto clean_up_and_return;
1096 }
1097
1098
1099 if (tracks.data==0) {
1100 if (cdi->options & CDO_CHECK_TYPE) {
1101
1102
1103 cd_dbg(CD_OPEN, "bummer. wrong media type.\n");
1104 cd_dbg(CD_WARNING, "pid %d must open device O_NONBLOCK!\n",
1105 (unsigned int)task_pid_nr(current));
1106 ret=-EMEDIUMTYPE;
1107 goto clean_up_and_return;
1108 }
1109 else {
1110 cd_dbg(CD_OPEN, "wrong media type, but CDO_CHECK_TYPE not set\n");
1111 }
1112 }
1113
1114 cd_dbg(CD_OPEN, "all seems well, opening the devicen");
1115
1116
1117 ret = cdo->open(cdi, 0);
1118 cd_dbg(CD_OPEN, "opening the device gave me %d\n", ret);
1119
1120
1121
1122 if (ret) {
1123 cd_dbg(CD_OPEN, "open device failed\n");
1124 goto clean_up_and_return;
1125 }
1126 if (CDROM_CAN(CDC_LOCK) && (cdi->options & CDO_LOCK)) {
1127 cdo->lock_door(cdi, 1);
1128 cd_dbg(CD_OPEN, "door locked\n");
1129 }
1130 cd_dbg(CD_OPEN, "device opened successfully\n");
1131 return ret;
1132
1133
1134
1135
1136
1137
1138clean_up_and_return:
1139 cd_dbg(CD_OPEN, "open failed\n");
1140 if (CDROM_CAN(CDC_LOCK) && cdi->options & CDO_LOCK) {
1141 cdo->lock_door(cdi, 0);
1142 cd_dbg(CD_OPEN, "door unlocked\n");
1143 }
1144 return ret;
1145}
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155int cdrom_open(struct cdrom_device_info *cdi, struct block_device *bdev,
1156 fmode_t mode)
1157{
1158 int ret;
1159
1160 cd_dbg(CD_OPEN, "entering cdrom_open\n");
1161
1162
1163
1164 cdi->use_count++;
1165 if ((mode & FMODE_NDELAY) && (cdi->options & CDO_USE_FFLAGS)) {
1166 ret = cdi->ops->open(cdi, 1);
1167 } else {
1168 ret = open_for_data(cdi);
1169 if (ret)
1170 goto err;
1171 if (CDROM_CAN(CDC_GENERIC_PACKET))
1172 cdrom_mmc3_profile(cdi);
1173 if (mode & FMODE_WRITE) {
1174 ret = -EROFS;
1175 if (cdrom_open_write(cdi))
1176 goto err_release;
1177 if (!CDROM_CAN(CDC_RAM))
1178 goto err_release;
1179 ret = 0;
1180 cdi->media_written = 0;
1181 }
1182 }
1183
1184 if (ret)
1185 goto err;
1186
1187 cd_dbg(CD_OPEN, "Use count for \"/dev/%s\" now %d\n",
1188 cdi->name, cdi->use_count);
1189 return 0;
1190err_release:
1191 if (CDROM_CAN(CDC_LOCK) && cdi->options & CDO_LOCK) {
1192 cdi->ops->lock_door(cdi, 0);
1193 cd_dbg(CD_OPEN, "door unlocked\n");
1194 }
1195 cdi->ops->release(cdi);
1196err:
1197 cdi->use_count--;
1198 return ret;
1199}
1200
1201
1202
1203
1204static int check_for_audio_disc(struct cdrom_device_info *cdi,
1205 const struct cdrom_device_ops *cdo)
1206{
1207 int ret;
1208 tracktype tracks;
1209 cd_dbg(CD_OPEN, "entering check_for_audio_disc\n");
1210 if (!(cdi->options & CDO_CHECK_TYPE))
1211 return 0;
1212 if (cdo->drive_status != NULL) {
1213 ret = cdo->drive_status(cdi, CDSL_CURRENT);
1214 cd_dbg(CD_OPEN, "drive_status=%d\n", ret);
1215 if (ret == CDS_TRAY_OPEN) {
1216 cd_dbg(CD_OPEN, "the tray is open...\n");
1217
1218 if (CDROM_CAN(CDC_CLOSE_TRAY) &&
1219 cdi->options & CDO_AUTO_CLOSE) {
1220 cd_dbg(CD_OPEN, "trying to close the tray\n");
1221 ret=cdo->tray_move(cdi,0);
1222 if (ret) {
1223 cd_dbg(CD_OPEN, "bummer. tried to close tray but failed.\n");
1224
1225
1226
1227
1228
1229 return -ENOMEDIUM;
1230 }
1231 } else {
1232 cd_dbg(CD_OPEN, "bummer. this driver can't close the tray.\n");
1233 return -ENOMEDIUM;
1234 }
1235
1236 ret = cdo->drive_status(cdi, CDSL_CURRENT);
1237 if ((ret == CDS_NO_DISC) || (ret==CDS_TRAY_OPEN)) {
1238 cd_dbg(CD_OPEN, "bummer. the tray is still not closed.\n");
1239 return -ENOMEDIUM;
1240 }
1241 if (ret!=CDS_DISC_OK) {
1242 cd_dbg(CD_OPEN, "bummer. disc isn't ready.\n");
1243 return -EIO;
1244 }
1245 cd_dbg(CD_OPEN, "the tray is now closed\n");
1246 }
1247 }
1248 cdrom_count_tracks(cdi, &tracks);
1249 if (tracks.error)
1250 return(tracks.error);
1251
1252 if (tracks.audio==0)
1253 return -EMEDIUMTYPE;
1254
1255 return 0;
1256}
1257
1258void cdrom_release(struct cdrom_device_info *cdi, fmode_t mode)
1259{
1260 const struct cdrom_device_ops *cdo = cdi->ops;
1261 int opened_for_data;
1262
1263 cd_dbg(CD_CLOSE, "entering cdrom_release\n");
1264
1265 if (cdi->use_count > 0)
1266 cdi->use_count--;
1267
1268 if (cdi->use_count == 0) {
1269 cd_dbg(CD_CLOSE, "Use count for \"/dev/%s\" now zero\n",
1270 cdi->name);
1271 cdrom_dvd_rw_close_write(cdi);
1272
1273 if ((cdo->capability & CDC_LOCK) && !cdi->keeplocked) {
1274 cd_dbg(CD_CLOSE, "Unlocking door!\n");
1275 cdo->lock_door(cdi, 0);
1276 }
1277 }
1278
1279 opened_for_data = !(cdi->options & CDO_USE_FFLAGS) ||
1280 !(mode & FMODE_NDELAY);
1281
1282
1283
1284
1285 if (CDROM_CAN(CDC_RAM) && !cdi->use_count && cdi->for_data)
1286 cdrom_close_write(cdi);
1287
1288 cdo->release(cdi);
1289 if (cdi->use_count == 0) {
1290 if (opened_for_data &&
1291 cdi->options & CDO_AUTO_EJECT && CDROM_CAN(CDC_OPEN_TRAY))
1292 cdo->tray_move(cdi, 1);
1293 }
1294}
1295
1296static int cdrom_read_mech_status(struct cdrom_device_info *cdi,
1297 struct cdrom_changer_info *buf)
1298{
1299 struct packet_command cgc;
1300 const struct cdrom_device_ops *cdo = cdi->ops;
1301 int length;
1302
1303
1304
1305
1306
1307
1308 if (cdi->sanyo_slot) {
1309 buf->hdr.nslots = 3;
1310 buf->hdr.curslot = cdi->sanyo_slot == 3 ? 0 : cdi->sanyo_slot;
1311 for (length = 0; length < 3; length++) {
1312 buf->slots[length].disc_present = 1;
1313 buf->slots[length].change = 0;
1314 }
1315 return 0;
1316 }
1317
1318 length = sizeof(struct cdrom_mechstat_header) +
1319 cdi->capacity * sizeof(struct cdrom_slot);
1320
1321 init_cdrom_command(&cgc, buf, length, CGC_DATA_READ);
1322 cgc.cmd[0] = GPCMD_MECHANISM_STATUS;
1323 cgc.cmd[8] = (length >> 8) & 0xff;
1324 cgc.cmd[9] = length & 0xff;
1325 return cdo->generic_packet(cdi, &cgc);
1326}
1327
1328static int cdrom_slot_status(struct cdrom_device_info *cdi, int slot)
1329{
1330 struct cdrom_changer_info *info;
1331 int ret;
1332
1333 cd_dbg(CD_CHANGER, "entering cdrom_slot_status()\n");
1334 if (cdi->sanyo_slot)
1335 return CDS_NO_INFO;
1336
1337 info = kmalloc(sizeof(*info), GFP_KERNEL);
1338 if (!info)
1339 return -ENOMEM;
1340
1341 if ((ret = cdrom_read_mech_status(cdi, info)))
1342 goto out_free;
1343
1344 if (info->slots[slot].disc_present)
1345 ret = CDS_DISC_OK;
1346 else
1347 ret = CDS_NO_DISC;
1348
1349out_free:
1350 kfree(info);
1351 return ret;
1352}
1353
1354
1355
1356
1357int cdrom_number_of_slots(struct cdrom_device_info *cdi)
1358{
1359 int status;
1360 int nslots = 1;
1361 struct cdrom_changer_info *info;
1362
1363 cd_dbg(CD_CHANGER, "entering cdrom_number_of_slots()\n");
1364
1365 cdi->capacity = 0;
1366
1367 info = kmalloc(sizeof(*info), GFP_KERNEL);
1368 if (!info)
1369 return -ENOMEM;
1370
1371 if ((status = cdrom_read_mech_status(cdi, info)) == 0)
1372 nslots = info->hdr.nslots;
1373
1374 kfree(info);
1375 return nslots;
1376}
1377
1378
1379
1380static int cdrom_load_unload(struct cdrom_device_info *cdi, int slot)
1381{
1382 struct packet_command cgc;
1383
1384 cd_dbg(CD_CHANGER, "entering cdrom_load_unload()\n");
1385 if (cdi->sanyo_slot && slot < 0)
1386 return 0;
1387
1388 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
1389 cgc.cmd[0] = GPCMD_LOAD_UNLOAD;
1390 cgc.cmd[4] = 2 + (slot >= 0);
1391 cgc.cmd[8] = slot;
1392 cgc.timeout = 60 * HZ;
1393
1394
1395
1396
1397 if (cdi->sanyo_slot && -1 < slot) {
1398 cgc.cmd[0] = GPCMD_TEST_UNIT_READY;
1399 cgc.cmd[7] = slot;
1400 cgc.cmd[4] = cgc.cmd[8] = 0;
1401 cdi->sanyo_slot = slot ? slot : 3;
1402 }
1403
1404 return cdi->ops->generic_packet(cdi, &cgc);
1405}
1406
1407static int cdrom_select_disc(struct cdrom_device_info *cdi, int slot)
1408{
1409 struct cdrom_changer_info *info;
1410 int curslot;
1411 int ret;
1412
1413 cd_dbg(CD_CHANGER, "entering cdrom_select_disc()\n");
1414 if (!CDROM_CAN(CDC_SELECT_DISC))
1415 return -EDRIVE_CANT_DO_THIS;
1416
1417 if (cdi->ops->check_events)
1418 cdi->ops->check_events(cdi, 0, slot);
1419 else
1420 cdi->ops->media_changed(cdi, slot);
1421
1422 if (slot == CDSL_NONE) {
1423
1424 cdi->mc_flags = 0x3;
1425 return cdrom_load_unload(cdi, -1);
1426 }
1427
1428 info = kmalloc(sizeof(*info), GFP_KERNEL);
1429 if (!info)
1430 return -ENOMEM;
1431
1432 if ((ret = cdrom_read_mech_status(cdi, info))) {
1433 kfree(info);
1434 return ret;
1435 }
1436
1437 curslot = info->hdr.curslot;
1438 kfree(info);
1439
1440 if (cdi->use_count > 1 || cdi->keeplocked) {
1441 if (slot == CDSL_CURRENT) {
1442 return curslot;
1443 } else {
1444 return -EBUSY;
1445 }
1446 }
1447
1448
1449
1450
1451
1452
1453 if (slot == CDSL_CURRENT)
1454 slot = curslot;
1455
1456
1457 cdi->mc_flags = 0x3;
1458 if ((ret = cdrom_load_unload(cdi, slot)))
1459 return ret;
1460
1461 return slot;
1462}
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478static void cdrom_update_events(struct cdrom_device_info *cdi,
1479 unsigned int clearing)
1480{
1481 unsigned int events;
1482
1483 events = cdi->ops->check_events(cdi, clearing, CDSL_CURRENT);
1484 cdi->vfs_events |= events;
1485 cdi->ioctl_events |= events;
1486}
1487
1488unsigned int cdrom_check_events(struct cdrom_device_info *cdi,
1489 unsigned int clearing)
1490{
1491 unsigned int events;
1492
1493 cdrom_update_events(cdi, clearing);
1494 events = cdi->vfs_events;
1495 cdi->vfs_events = 0;
1496 return events;
1497}
1498EXPORT_SYMBOL(cdrom_check_events);
1499
1500
1501
1502
1503
1504
1505
1506static
1507int media_changed(struct cdrom_device_info *cdi, int queue)
1508{
1509 unsigned int mask = (1 << (queue & 1));
1510 int ret = !!(cdi->mc_flags & mask);
1511 bool changed;
1512
1513 if (!CDROM_CAN(CDC_MEDIA_CHANGED))
1514 return ret;
1515
1516
1517 if (cdi->ops->check_events) {
1518 BUG_ON(!queue);
1519 cdrom_update_events(cdi, DISK_EVENT_MEDIA_CHANGE);
1520 changed = cdi->ioctl_events & DISK_EVENT_MEDIA_CHANGE;
1521 cdi->ioctl_events = 0;
1522 } else
1523 changed = cdi->ops->media_changed(cdi, CDSL_CURRENT);
1524
1525 if (changed) {
1526 cdi->mc_flags = 0x3;
1527 ret |= 1;
1528 cdi->media_written = 0;
1529 }
1530
1531 cdi->mc_flags &= ~mask;
1532 return ret;
1533}
1534
1535int cdrom_media_changed(struct cdrom_device_info *cdi)
1536{
1537
1538
1539
1540 if (cdi == NULL || cdi->ops->media_changed == NULL)
1541 return 0;
1542 if (!CDROM_CAN(CDC_MEDIA_CHANGED))
1543 return 0;
1544 return media_changed(cdi, 0);
1545}
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563static
1564void sanitize_format(union cdrom_addr *addr,
1565 u_char * curr, u_char requested)
1566{
1567 if (*curr == requested)
1568 return;
1569 if (requested == CDROM_LBA) {
1570 addr->lba = (int) addr->msf.frame +
1571 75 * (addr->msf.second - 2 + 60 * addr->msf.minute);
1572 } else {
1573 int lba = addr->lba;
1574 addr->msf.frame = lba % 75;
1575 lba /= 75;
1576 lba += 2;
1577 addr->msf.second = lba % 60;
1578 addr->msf.minute = lba / 60;
1579 }
1580 *curr = requested;
1581}
1582
1583void init_cdrom_command(struct packet_command *cgc, void *buf, int len,
1584 int type)
1585{
1586 memset(cgc, 0, sizeof(struct packet_command));
1587 if (buf)
1588 memset(buf, 0, len);
1589 cgc->buffer = (char *) buf;
1590 cgc->buflen = len;
1591 cgc->data_direction = type;
1592 cgc->timeout = CDROM_DEF_TIMEOUT;
1593}
1594
1595
1596
1597#define copy_key(dest,src) memcpy((dest), (src), sizeof(dvd_key))
1598#define copy_chal(dest,src) memcpy((dest), (src), sizeof(dvd_challenge))
1599
1600static void setup_report_key(struct packet_command *cgc, unsigned agid, unsigned type)
1601{
1602 cgc->cmd[0] = GPCMD_REPORT_KEY;
1603 cgc->cmd[10] = type | (agid << 6);
1604 switch (type) {
1605 case 0: case 8: case 5: {
1606 cgc->buflen = 8;
1607 break;
1608 }
1609 case 1: {
1610 cgc->buflen = 16;
1611 break;
1612 }
1613 case 2: case 4: {
1614 cgc->buflen = 12;
1615 break;
1616 }
1617 }
1618 cgc->cmd[9] = cgc->buflen;
1619 cgc->data_direction = CGC_DATA_READ;
1620}
1621
1622static void setup_send_key(struct packet_command *cgc, unsigned agid, unsigned type)
1623{
1624 cgc->cmd[0] = GPCMD_SEND_KEY;
1625 cgc->cmd[10] = type | (agid << 6);
1626 switch (type) {
1627 case 1: {
1628 cgc->buflen = 16;
1629 break;
1630 }
1631 case 3: {
1632 cgc->buflen = 12;
1633 break;
1634 }
1635 case 6: {
1636 cgc->buflen = 8;
1637 break;
1638 }
1639 }
1640 cgc->cmd[9] = cgc->buflen;
1641 cgc->data_direction = CGC_DATA_WRITE;
1642}
1643
1644static int dvd_do_auth(struct cdrom_device_info *cdi, dvd_authinfo *ai)
1645{
1646 int ret;
1647 u_char buf[20];
1648 struct packet_command cgc;
1649 const struct cdrom_device_ops *cdo = cdi->ops;
1650 rpc_state_t rpc_state;
1651
1652 memset(buf, 0, sizeof(buf));
1653 init_cdrom_command(&cgc, buf, 0, CGC_DATA_READ);
1654
1655 switch (ai->type) {
1656
1657 case DVD_LU_SEND_AGID:
1658 cd_dbg(CD_DVD, "entering DVD_LU_SEND_AGID\n");
1659 cgc.quiet = 1;
1660 setup_report_key(&cgc, ai->lsa.agid, 0);
1661
1662 if ((ret = cdo->generic_packet(cdi, &cgc)))
1663 return ret;
1664
1665 ai->lsa.agid = buf[7] >> 6;
1666
1667 break;
1668
1669 case DVD_LU_SEND_KEY1:
1670 cd_dbg(CD_DVD, "entering DVD_LU_SEND_KEY1\n");
1671 setup_report_key(&cgc, ai->lsk.agid, 2);
1672
1673 if ((ret = cdo->generic_packet(cdi, &cgc)))
1674 return ret;
1675
1676 copy_key(ai->lsk.key, &buf[4]);
1677
1678 break;
1679
1680 case DVD_LU_SEND_CHALLENGE:
1681 cd_dbg(CD_DVD, "entering DVD_LU_SEND_CHALLENGE\n");
1682 setup_report_key(&cgc, ai->lsc.agid, 1);
1683
1684 if ((ret = cdo->generic_packet(cdi, &cgc)))
1685 return ret;
1686
1687 copy_chal(ai->lsc.chal, &buf[4]);
1688
1689 break;
1690
1691
1692 case DVD_LU_SEND_TITLE_KEY:
1693 cd_dbg(CD_DVD, "entering DVD_LU_SEND_TITLE_KEY\n");
1694 cgc.quiet = 1;
1695 setup_report_key(&cgc, ai->lstk.agid, 4);
1696 cgc.cmd[5] = ai->lstk.lba;
1697 cgc.cmd[4] = ai->lstk.lba >> 8;
1698 cgc.cmd[3] = ai->lstk.lba >> 16;
1699 cgc.cmd[2] = ai->lstk.lba >> 24;
1700
1701 if ((ret = cdo->generic_packet(cdi, &cgc)))
1702 return ret;
1703
1704 ai->lstk.cpm = (buf[4] >> 7) & 1;
1705 ai->lstk.cp_sec = (buf[4] >> 6) & 1;
1706 ai->lstk.cgms = (buf[4] >> 4) & 3;
1707 copy_key(ai->lstk.title_key, &buf[5]);
1708
1709 break;
1710
1711 case DVD_LU_SEND_ASF:
1712 cd_dbg(CD_DVD, "entering DVD_LU_SEND_ASF\n");
1713 setup_report_key(&cgc, ai->lsasf.agid, 5);
1714
1715 if ((ret = cdo->generic_packet(cdi, &cgc)))
1716 return ret;
1717
1718 ai->lsasf.asf = buf[7] & 1;
1719 break;
1720
1721
1722 case DVD_HOST_SEND_CHALLENGE:
1723 cd_dbg(CD_DVD, "entering DVD_HOST_SEND_CHALLENGE\n");
1724 setup_send_key(&cgc, ai->hsc.agid, 1);
1725 buf[1] = 0xe;
1726 copy_chal(&buf[4], ai->hsc.chal);
1727
1728 if ((ret = cdo->generic_packet(cdi, &cgc)))
1729 return ret;
1730
1731 ai->type = DVD_LU_SEND_KEY1;
1732 break;
1733
1734 case DVD_HOST_SEND_KEY2:
1735 cd_dbg(CD_DVD, "entering DVD_HOST_SEND_KEY2\n");
1736 setup_send_key(&cgc, ai->hsk.agid, 3);
1737 buf[1] = 0xa;
1738 copy_key(&buf[4], ai->hsk.key);
1739
1740 if ((ret = cdo->generic_packet(cdi, &cgc))) {
1741 ai->type = DVD_AUTH_FAILURE;
1742 return ret;
1743 }
1744 ai->type = DVD_AUTH_ESTABLISHED;
1745 break;
1746
1747
1748 case DVD_INVALIDATE_AGID:
1749 cgc.quiet = 1;
1750 cd_dbg(CD_DVD, "entering DVD_INVALIDATE_AGID\n");
1751 setup_report_key(&cgc, ai->lsa.agid, 0x3f);
1752 if ((ret = cdo->generic_packet(cdi, &cgc)))
1753 return ret;
1754 break;
1755
1756
1757 case DVD_LU_SEND_RPC_STATE:
1758 cd_dbg(CD_DVD, "entering DVD_LU_SEND_RPC_STATE\n");
1759 setup_report_key(&cgc, 0, 8);
1760 memset(&rpc_state, 0, sizeof(rpc_state_t));
1761 cgc.buffer = (char *) &rpc_state;
1762
1763 if ((ret = cdo->generic_packet(cdi, &cgc)))
1764 return ret;
1765
1766 ai->lrpcs.type = rpc_state.type_code;
1767 ai->lrpcs.vra = rpc_state.vra;
1768 ai->lrpcs.ucca = rpc_state.ucca;
1769 ai->lrpcs.region_mask = rpc_state.region_mask;
1770 ai->lrpcs.rpc_scheme = rpc_state.rpc_scheme;
1771 break;
1772
1773
1774 case DVD_HOST_SEND_RPC_STATE:
1775 cd_dbg(CD_DVD, "entering DVD_HOST_SEND_RPC_STATE\n");
1776 setup_send_key(&cgc, 0, 6);
1777 buf[1] = 6;
1778 buf[4] = ai->hrpcs.pdrc;
1779
1780 if ((ret = cdo->generic_packet(cdi, &cgc)))
1781 return ret;
1782 break;
1783
1784 default:
1785 cd_dbg(CD_WARNING, "Invalid DVD key ioctl (%d)\n", ai->type);
1786 return -ENOTTY;
1787 }
1788
1789 return 0;
1790}
1791
1792static int dvd_read_physical(struct cdrom_device_info *cdi, dvd_struct *s,
1793 struct packet_command *cgc)
1794{
1795 unsigned char buf[21], *base;
1796 struct dvd_layer *layer;
1797 const struct cdrom_device_ops *cdo = cdi->ops;
1798 int ret, layer_num = s->physical.layer_num;
1799
1800 if (layer_num >= DVD_LAYERS)
1801 return -EINVAL;
1802
1803 init_cdrom_command(cgc, buf, sizeof(buf), CGC_DATA_READ);
1804 cgc->cmd[0] = GPCMD_READ_DVD_STRUCTURE;
1805 cgc->cmd[6] = layer_num;
1806 cgc->cmd[7] = s->type;
1807 cgc->cmd[9] = cgc->buflen & 0xff;
1808
1809
1810
1811
1812 cgc->quiet = 1;
1813
1814 ret = cdo->generic_packet(cdi, cgc);
1815 if (ret)
1816 return ret;
1817
1818 base = &buf[4];
1819 layer = &s->physical.layer[layer_num];
1820
1821
1822
1823
1824
1825 memset(layer, 0, sizeof(*layer));
1826 layer->book_version = base[0] & 0xf;
1827 layer->book_type = base[0] >> 4;
1828 layer->min_rate = base[1] & 0xf;
1829 layer->disc_size = base[1] >> 4;
1830 layer->layer_type = base[2] & 0xf;
1831 layer->track_path = (base[2] >> 4) & 1;
1832 layer->nlayers = (base[2] >> 5) & 3;
1833 layer->track_density = base[3] & 0xf;
1834 layer->linear_density = base[3] >> 4;
1835 layer->start_sector = base[5] << 16 | base[6] << 8 | base[7];
1836 layer->end_sector = base[9] << 16 | base[10] << 8 | base[11];
1837 layer->end_sector_l0 = base[13] << 16 | base[14] << 8 | base[15];
1838 layer->bca = base[16] >> 7;
1839
1840 return 0;
1841}
1842
1843static int dvd_read_copyright(struct cdrom_device_info *cdi, dvd_struct *s,
1844 struct packet_command *cgc)
1845{
1846 int ret;
1847 u_char buf[8];
1848 const struct cdrom_device_ops *cdo = cdi->ops;
1849
1850 init_cdrom_command(cgc, buf, sizeof(buf), CGC_DATA_READ);
1851 cgc->cmd[0] = GPCMD_READ_DVD_STRUCTURE;
1852 cgc->cmd[6] = s->copyright.layer_num;
1853 cgc->cmd[7] = s->type;
1854 cgc->cmd[8] = cgc->buflen >> 8;
1855 cgc->cmd[9] = cgc->buflen & 0xff;
1856
1857 ret = cdo->generic_packet(cdi, cgc);
1858 if (ret)
1859 return ret;
1860
1861 s->copyright.cpst = buf[4];
1862 s->copyright.rmi = buf[5];
1863
1864 return 0;
1865}
1866
1867static int dvd_read_disckey(struct cdrom_device_info *cdi, dvd_struct *s,
1868 struct packet_command *cgc)
1869{
1870 int ret, size;
1871 u_char *buf;
1872 const struct cdrom_device_ops *cdo = cdi->ops;
1873
1874 size = sizeof(s->disckey.value) + 4;
1875
1876 buf = kmalloc(size, GFP_KERNEL);
1877 if (!buf)
1878 return -ENOMEM;
1879
1880 init_cdrom_command(cgc, buf, size, CGC_DATA_READ);
1881 cgc->cmd[0] = GPCMD_READ_DVD_STRUCTURE;
1882 cgc->cmd[7] = s->type;
1883 cgc->cmd[8] = size >> 8;
1884 cgc->cmd[9] = size & 0xff;
1885 cgc->cmd[10] = s->disckey.agid << 6;
1886
1887 ret = cdo->generic_packet(cdi, cgc);
1888 if (!ret)
1889 memcpy(s->disckey.value, &buf[4], sizeof(s->disckey.value));
1890
1891 kfree(buf);
1892 return ret;
1893}
1894
1895static int dvd_read_bca(struct cdrom_device_info *cdi, dvd_struct *s,
1896 struct packet_command *cgc)
1897{
1898 int ret, size = 4 + 188;
1899 u_char *buf;
1900 const struct cdrom_device_ops *cdo = cdi->ops;
1901
1902 buf = kmalloc(size, GFP_KERNEL);
1903 if (!buf)
1904 return -ENOMEM;
1905
1906 init_cdrom_command(cgc, buf, size, CGC_DATA_READ);
1907 cgc->cmd[0] = GPCMD_READ_DVD_STRUCTURE;
1908 cgc->cmd[7] = s->type;
1909 cgc->cmd[9] = cgc->buflen & 0xff;
1910
1911 ret = cdo->generic_packet(cdi, cgc);
1912 if (ret)
1913 goto out;
1914
1915 s->bca.len = buf[0] << 8 | buf[1];
1916 if (s->bca.len < 12 || s->bca.len > 188) {
1917 cd_dbg(CD_WARNING, "Received invalid BCA length (%d)\n",
1918 s->bca.len);
1919 ret = -EIO;
1920 goto out;
1921 }
1922 memcpy(s->bca.value, &buf[4], s->bca.len);
1923 ret = 0;
1924out:
1925 kfree(buf);
1926 return ret;
1927}
1928
1929static int dvd_read_manufact(struct cdrom_device_info *cdi, dvd_struct *s,
1930 struct packet_command *cgc)
1931{
1932 int ret = 0, size;
1933 u_char *buf;
1934 const struct cdrom_device_ops *cdo = cdi->ops;
1935
1936 size = sizeof(s->manufact.value) + 4;
1937
1938 buf = kmalloc(size, GFP_KERNEL);
1939 if (!buf)
1940 return -ENOMEM;
1941
1942 init_cdrom_command(cgc, buf, size, CGC_DATA_READ);
1943 cgc->cmd[0] = GPCMD_READ_DVD_STRUCTURE;
1944 cgc->cmd[7] = s->type;
1945 cgc->cmd[8] = size >> 8;
1946 cgc->cmd[9] = size & 0xff;
1947
1948 ret = cdo->generic_packet(cdi, cgc);
1949 if (ret)
1950 goto out;
1951
1952 s->manufact.len = buf[0] << 8 | buf[1];
1953 if (s->manufact.len < 0) {
1954 cd_dbg(CD_WARNING, "Received invalid manufacture info length (%d)\n",
1955 s->manufact.len);
1956 ret = -EIO;
1957 } else {
1958 if (s->manufact.len > 2048) {
1959 cd_dbg(CD_WARNING, "Received invalid manufacture info length (%d): truncating to 2048\n",
1960 s->manufact.len);
1961 s->manufact.len = 2048;
1962 }
1963 memcpy(s->manufact.value, &buf[4], s->manufact.len);
1964 }
1965
1966out:
1967 kfree(buf);
1968 return ret;
1969}
1970
1971static int dvd_read_struct(struct cdrom_device_info *cdi, dvd_struct *s,
1972 struct packet_command *cgc)
1973{
1974 switch (s->type) {
1975 case DVD_STRUCT_PHYSICAL:
1976 return dvd_read_physical(cdi, s, cgc);
1977
1978 case DVD_STRUCT_COPYRIGHT:
1979 return dvd_read_copyright(cdi, s, cgc);
1980
1981 case DVD_STRUCT_DISCKEY:
1982 return dvd_read_disckey(cdi, s, cgc);
1983
1984 case DVD_STRUCT_BCA:
1985 return dvd_read_bca(cdi, s, cgc);
1986
1987 case DVD_STRUCT_MANUFACT:
1988 return dvd_read_manufact(cdi, s, cgc);
1989
1990 default:
1991 cd_dbg(CD_WARNING, ": Invalid DVD structure read requested (%d)\n",
1992 s->type);
1993 return -EINVAL;
1994 }
1995}
1996
1997int cdrom_mode_sense(struct cdrom_device_info *cdi,
1998 struct packet_command *cgc,
1999 int page_code, int page_control)
2000{
2001 const struct cdrom_device_ops *cdo = cdi->ops;
2002
2003 memset(cgc->cmd, 0, sizeof(cgc->cmd));
2004
2005 cgc->cmd[0] = GPCMD_MODE_SENSE_10;
2006 cgc->cmd[2] = page_code | (page_control << 6);
2007 cgc->cmd[7] = cgc->buflen >> 8;
2008 cgc->cmd[8] = cgc->buflen & 0xff;
2009 cgc->data_direction = CGC_DATA_READ;
2010 return cdo->generic_packet(cdi, cgc);
2011}
2012
2013int cdrom_mode_select(struct cdrom_device_info *cdi,
2014 struct packet_command *cgc)
2015{
2016 const struct cdrom_device_ops *cdo = cdi->ops;
2017
2018 memset(cgc->cmd, 0, sizeof(cgc->cmd));
2019 memset(cgc->buffer, 0, 2);
2020 cgc->cmd[0] = GPCMD_MODE_SELECT_10;
2021 cgc->cmd[1] = 0x10;
2022 cgc->cmd[7] = cgc->buflen >> 8;
2023 cgc->cmd[8] = cgc->buflen & 0xff;
2024 cgc->data_direction = CGC_DATA_WRITE;
2025 return cdo->generic_packet(cdi, cgc);
2026}
2027
2028static int cdrom_read_subchannel(struct cdrom_device_info *cdi,
2029 struct cdrom_subchnl *subchnl, int mcn)
2030{
2031 const struct cdrom_device_ops *cdo = cdi->ops;
2032 struct packet_command cgc;
2033 char buffer[32];
2034 int ret;
2035
2036 init_cdrom_command(&cgc, buffer, 16, CGC_DATA_READ);
2037 cgc.cmd[0] = GPCMD_READ_SUBCHANNEL;
2038 cgc.cmd[1] = subchnl->cdsc_format;
2039 cgc.cmd[2] = 0x40;
2040 cgc.cmd[3] = mcn ? 2 : 1;
2041 cgc.cmd[8] = 16;
2042
2043 if ((ret = cdo->generic_packet(cdi, &cgc)))
2044 return ret;
2045
2046 subchnl->cdsc_audiostatus = cgc.buffer[1];
2047 subchnl->cdsc_ctrl = cgc.buffer[5] & 0xf;
2048 subchnl->cdsc_trk = cgc.buffer[6];
2049 subchnl->cdsc_ind = cgc.buffer[7];
2050
2051 if (subchnl->cdsc_format == CDROM_LBA) {
2052 subchnl->cdsc_absaddr.lba = ((cgc.buffer[8] << 24) |
2053 (cgc.buffer[9] << 16) |
2054 (cgc.buffer[10] << 8) |
2055 (cgc.buffer[11]));
2056 subchnl->cdsc_reladdr.lba = ((cgc.buffer[12] << 24) |
2057 (cgc.buffer[13] << 16) |
2058 (cgc.buffer[14] << 8) |
2059 (cgc.buffer[15]));
2060 } else {
2061 subchnl->cdsc_reladdr.msf.minute = cgc.buffer[13];
2062 subchnl->cdsc_reladdr.msf.second = cgc.buffer[14];
2063 subchnl->cdsc_reladdr.msf.frame = cgc.buffer[15];
2064 subchnl->cdsc_absaddr.msf.minute = cgc.buffer[9];
2065 subchnl->cdsc_absaddr.msf.second = cgc.buffer[10];
2066 subchnl->cdsc_absaddr.msf.frame = cgc.buffer[11];
2067 }
2068
2069 return 0;
2070}
2071
2072
2073
2074
2075static int cdrom_read_cd(struct cdrom_device_info *cdi,
2076 struct packet_command *cgc, int lba,
2077 int blocksize, int nblocks)
2078{
2079 const struct cdrom_device_ops *cdo = cdi->ops;
2080
2081 memset(&cgc->cmd, 0, sizeof(cgc->cmd));
2082 cgc->cmd[0] = GPCMD_READ_10;
2083 cgc->cmd[2] = (lba >> 24) & 0xff;
2084 cgc->cmd[3] = (lba >> 16) & 0xff;
2085 cgc->cmd[4] = (lba >> 8) & 0xff;
2086 cgc->cmd[5] = lba & 0xff;
2087 cgc->cmd[6] = (nblocks >> 16) & 0xff;
2088 cgc->cmd[7] = (nblocks >> 8) & 0xff;
2089 cgc->cmd[8] = nblocks & 0xff;
2090 cgc->buflen = blocksize * nblocks;
2091 return cdo->generic_packet(cdi, cgc);
2092}
2093
2094
2095static int cdrom_read_block(struct cdrom_device_info *cdi,
2096 struct packet_command *cgc,
2097 int lba, int nblocks, int format, int blksize)
2098{
2099 const struct cdrom_device_ops *cdo = cdi->ops;
2100
2101 memset(&cgc->cmd, 0, sizeof(cgc->cmd));
2102 cgc->cmd[0] = GPCMD_READ_CD;
2103
2104 cgc->cmd[1] = format << 2;
2105
2106 cgc->cmd[2] = (lba >> 24) & 0xff;
2107 cgc->cmd[3] = (lba >> 16) & 0xff;
2108 cgc->cmd[4] = (lba >> 8) & 0xff;
2109 cgc->cmd[5] = lba & 0xff;
2110
2111 cgc->cmd[6] = (nblocks >> 16) & 0xff;
2112 cgc->cmd[7] = (nblocks >> 8) & 0xff;
2113 cgc->cmd[8] = nblocks & 0xff;
2114 cgc->buflen = blksize * nblocks;
2115
2116
2117 switch (blksize) {
2118 case CD_FRAMESIZE_RAW0 : cgc->cmd[9] = 0x58; break;
2119 case CD_FRAMESIZE_RAW1 : cgc->cmd[9] = 0x78; break;
2120 case CD_FRAMESIZE_RAW : cgc->cmd[9] = 0xf8; break;
2121 default : cgc->cmd[9] = 0x10;
2122 }
2123
2124 return cdo->generic_packet(cdi, cgc);
2125}
2126
2127static int cdrom_read_cdda_old(struct cdrom_device_info *cdi, __u8 __user *ubuf,
2128 int lba, int nframes)
2129{
2130 struct packet_command cgc;
2131 int ret = 0;
2132 int nr;
2133
2134 cdi->last_sense = 0;
2135
2136 memset(&cgc, 0, sizeof(cgc));
2137
2138
2139
2140
2141 nr = nframes;
2142 do {
2143 cgc.buffer = kmalloc_array(nr, CD_FRAMESIZE_RAW, GFP_KERNEL);
2144 if (cgc.buffer)
2145 break;
2146
2147 nr >>= 1;
2148 } while (nr);
2149
2150 if (!nr)
2151 return -ENOMEM;
2152
2153 cgc.data_direction = CGC_DATA_READ;
2154 while (nframes > 0) {
2155 if (nr > nframes)
2156 nr = nframes;
2157
2158 ret = cdrom_read_block(cdi, &cgc, lba, nr, 1, CD_FRAMESIZE_RAW);
2159 if (ret)
2160 break;
2161 if (copy_to_user(ubuf, cgc.buffer, CD_FRAMESIZE_RAW * nr)) {
2162 ret = -EFAULT;
2163 break;
2164 }
2165 ubuf += CD_FRAMESIZE_RAW * nr;
2166 nframes -= nr;
2167 lba += nr;
2168 }
2169 kfree(cgc.buffer);
2170 return ret;
2171}
2172
2173static int cdrom_read_cdda_bpc(struct cdrom_device_info *cdi, __u8 __user *ubuf,
2174 int lba, int nframes)
2175{
2176 struct request_queue *q = cdi->disk->queue;
2177 struct request *rq;
2178 struct scsi_request *req;
2179 struct bio *bio;
2180 unsigned int len;
2181 int nr, ret = 0;
2182
2183 if (!q)
2184 return -ENXIO;
2185
2186 if (!blk_queue_scsi_passthrough(q)) {
2187 WARN_ONCE(true,
2188 "Attempt read CDDA info through a non-SCSI queue\n");
2189 return -EINVAL;
2190 }
2191
2192 cdi->last_sense = 0;
2193
2194 while (nframes) {
2195 nr = nframes;
2196 if (cdi->cdda_method == CDDA_BPC_SINGLE)
2197 nr = 1;
2198 if (nr * CD_FRAMESIZE_RAW > (queue_max_sectors(q) << 9))
2199 nr = (queue_max_sectors(q) << 9) / CD_FRAMESIZE_RAW;
2200
2201 len = nr * CD_FRAMESIZE_RAW;
2202
2203 rq = blk_get_request(q, REQ_OP_SCSI_IN, 0);
2204 if (IS_ERR(rq)) {
2205 ret = PTR_ERR(rq);
2206 break;
2207 }
2208 req = scsi_req(rq);
2209
2210 ret = blk_rq_map_user(q, rq, NULL, ubuf, len, GFP_KERNEL);
2211 if (ret) {
2212 blk_put_request(rq);
2213 break;
2214 }
2215
2216 req->cmd[0] = GPCMD_READ_CD;
2217 req->cmd[1] = 1 << 2;
2218 req->cmd[2] = (lba >> 24) & 0xff;
2219 req->cmd[3] = (lba >> 16) & 0xff;
2220 req->cmd[4] = (lba >> 8) & 0xff;
2221 req->cmd[5] = lba & 0xff;
2222 req->cmd[6] = (nr >> 16) & 0xff;
2223 req->cmd[7] = (nr >> 8) & 0xff;
2224 req->cmd[8] = nr & 0xff;
2225 req->cmd[9] = 0xf8;
2226
2227 req->cmd_len = 12;
2228 rq->timeout = 60 * HZ;
2229 bio = rq->bio;
2230
2231 blk_execute_rq(q, cdi->disk, rq, 0);
2232 if (scsi_req(rq)->result) {
2233 struct scsi_sense_hdr sshdr;
2234
2235 ret = -EIO;
2236 scsi_normalize_sense(req->sense, req->sense_len,
2237 &sshdr);
2238 cdi->last_sense = sshdr.sense_key;
2239 }
2240
2241 if (blk_rq_unmap_user(bio))
2242 ret = -EFAULT;
2243 blk_put_request(rq);
2244
2245 if (ret)
2246 break;
2247
2248 nframes -= nr;
2249 lba += nr;
2250 ubuf += len;
2251 }
2252
2253 return ret;
2254}
2255
2256static int cdrom_read_cdda(struct cdrom_device_info *cdi, __u8 __user *ubuf,
2257 int lba, int nframes)
2258{
2259 int ret;
2260
2261 if (cdi->cdda_method == CDDA_OLD)
2262 return cdrom_read_cdda_old(cdi, ubuf, lba, nframes);
2263
2264retry:
2265
2266
2267
2268 ret = cdrom_read_cdda_bpc(cdi, ubuf, lba, nframes);
2269 if (!ret || ret != -EIO)
2270 return ret;
2271
2272
2273
2274
2275
2276 if (cdi->cdda_method == CDDA_BPC_FULL && nframes > 1) {
2277 pr_info("dropping to single frame dma\n");
2278 cdi->cdda_method = CDDA_BPC_SINGLE;
2279 goto retry;
2280 }
2281
2282
2283
2284
2285
2286
2287 if (cdi->last_sense != 0x04 && cdi->last_sense != 0x0b)
2288 return ret;
2289
2290 pr_info("dropping to old style cdda (sense=%x)\n", cdi->last_sense);
2291 cdi->cdda_method = CDDA_OLD;
2292 return cdrom_read_cdda_old(cdi, ubuf, lba, nframes);
2293}
2294
2295static int cdrom_ioctl_multisession(struct cdrom_device_info *cdi,
2296 void __user *argp)
2297{
2298 struct cdrom_multisession ms_info;
2299 u8 requested_format;
2300 int ret;
2301
2302 cd_dbg(CD_DO_IOCTL, "entering CDROMMULTISESSION\n");
2303
2304 if (!(cdi->ops->capability & CDC_MULTI_SESSION))
2305 return -ENOSYS;
2306
2307 if (copy_from_user(&ms_info, argp, sizeof(ms_info)))
2308 return -EFAULT;
2309
2310 requested_format = ms_info.addr_format;
2311 if (requested_format != CDROM_MSF && requested_format != CDROM_LBA)
2312 return -EINVAL;
2313 ms_info.addr_format = CDROM_LBA;
2314
2315 ret = cdi->ops->get_last_session(cdi, &ms_info);
2316 if (ret)
2317 return ret;
2318
2319 sanitize_format(&ms_info.addr, &ms_info.addr_format, requested_format);
2320
2321 if (copy_to_user(argp, &ms_info, sizeof(ms_info)))
2322 return -EFAULT;
2323
2324 cd_dbg(CD_DO_IOCTL, "CDROMMULTISESSION successful\n");
2325 return 0;
2326}
2327
2328static int cdrom_ioctl_eject(struct cdrom_device_info *cdi)
2329{
2330 cd_dbg(CD_DO_IOCTL, "entering CDROMEJECT\n");
2331
2332 if (!CDROM_CAN(CDC_OPEN_TRAY))
2333 return -ENOSYS;
2334 if (cdi->use_count != 1 || cdi->keeplocked)
2335 return -EBUSY;
2336 if (CDROM_CAN(CDC_LOCK)) {
2337 int ret = cdi->ops->lock_door(cdi, 0);
2338 if (ret)
2339 return ret;
2340 }
2341
2342 return cdi->ops->tray_move(cdi, 1);
2343}
2344
2345static int cdrom_ioctl_closetray(struct cdrom_device_info *cdi)
2346{
2347 cd_dbg(CD_DO_IOCTL, "entering CDROMCLOSETRAY\n");
2348
2349 if (!CDROM_CAN(CDC_CLOSE_TRAY))
2350 return -ENOSYS;
2351 return cdi->ops->tray_move(cdi, 0);
2352}
2353
2354static int cdrom_ioctl_eject_sw(struct cdrom_device_info *cdi,
2355 unsigned long arg)
2356{
2357 cd_dbg(CD_DO_IOCTL, "entering CDROMEJECT_SW\n");
2358
2359 if (!CDROM_CAN(CDC_OPEN_TRAY))
2360 return -ENOSYS;
2361 if (cdi->keeplocked)
2362 return -EBUSY;
2363
2364 cdi->options &= ~(CDO_AUTO_CLOSE | CDO_AUTO_EJECT);
2365 if (arg)
2366 cdi->options |= CDO_AUTO_CLOSE | CDO_AUTO_EJECT;
2367 return 0;
2368}
2369
2370static int cdrom_ioctl_media_changed(struct cdrom_device_info *cdi,
2371 unsigned long arg)
2372{
2373 struct cdrom_changer_info *info;
2374 int ret;
2375
2376 cd_dbg(CD_DO_IOCTL, "entering CDROM_MEDIA_CHANGED\n");
2377
2378 if (!CDROM_CAN(CDC_MEDIA_CHANGED))
2379 return -ENOSYS;
2380
2381
2382 if (!CDROM_CAN(CDC_SELECT_DISC) || arg == CDSL_CURRENT)
2383 return media_changed(cdi, 1);
2384
2385 if (arg >= cdi->capacity)
2386 return -EINVAL;
2387
2388 info = kmalloc(sizeof(*info), GFP_KERNEL);
2389 if (!info)
2390 return -ENOMEM;
2391
2392 ret = cdrom_read_mech_status(cdi, info);
2393 if (!ret)
2394 ret = info->slots[arg].change;
2395 kfree(info);
2396 return ret;
2397}
2398
2399static int cdrom_ioctl_set_options(struct cdrom_device_info *cdi,
2400 unsigned long arg)
2401{
2402 cd_dbg(CD_DO_IOCTL, "entering CDROM_SET_OPTIONS\n");
2403
2404
2405
2406
2407
2408 switch (arg) {
2409 case CDO_USE_FFLAGS:
2410 case CDO_CHECK_TYPE:
2411 break;
2412 case CDO_LOCK:
2413 if (!CDROM_CAN(CDC_LOCK))
2414 return -ENOSYS;
2415 break;
2416 case 0:
2417 return cdi->options;
2418
2419 default:
2420 if (!CDROM_CAN(arg))
2421 return -ENOSYS;
2422 }
2423 cdi->options |= (int) arg;
2424 return cdi->options;
2425}
2426
2427static int cdrom_ioctl_clear_options(struct cdrom_device_info *cdi,
2428 unsigned long arg)
2429{
2430 cd_dbg(CD_DO_IOCTL, "entering CDROM_CLEAR_OPTIONS\n");
2431
2432 cdi->options &= ~(int) arg;
2433 return cdi->options;
2434}
2435
2436static int cdrom_ioctl_select_speed(struct cdrom_device_info *cdi,
2437 unsigned long arg)
2438{
2439 cd_dbg(CD_DO_IOCTL, "entering CDROM_SELECT_SPEED\n");
2440
2441 if (!CDROM_CAN(CDC_SELECT_SPEED))
2442 return -ENOSYS;
2443 return cdi->ops->select_speed(cdi, arg);
2444}
2445
2446static int cdrom_ioctl_select_disc(struct cdrom_device_info *cdi,
2447 unsigned long arg)
2448{
2449 cd_dbg(CD_DO_IOCTL, "entering CDROM_SELECT_DISC\n");
2450
2451 if (!CDROM_CAN(CDC_SELECT_DISC))
2452 return -ENOSYS;
2453
2454 if (arg != CDSL_CURRENT && arg != CDSL_NONE) {
2455 if (arg >= cdi->capacity)
2456 return -EINVAL;
2457 }
2458
2459
2460
2461
2462
2463
2464 if (cdi->ops->select_disc)
2465 return cdi->ops->select_disc(cdi, arg);
2466
2467 cd_dbg(CD_CHANGER, "Using generic cdrom_select_disc()\n");
2468 return cdrom_select_disc(cdi, arg);
2469}
2470
2471static int cdrom_ioctl_reset(struct cdrom_device_info *cdi,
2472 struct block_device *bdev)
2473{
2474 cd_dbg(CD_DO_IOCTL, "entering CDROM_RESET\n");
2475
2476 if (!capable(CAP_SYS_ADMIN))
2477 return -EACCES;
2478 if (!CDROM_CAN(CDC_RESET))
2479 return -ENOSYS;
2480 invalidate_bdev(bdev);
2481 return cdi->ops->reset(cdi);
2482}
2483
2484static int cdrom_ioctl_lock_door(struct cdrom_device_info *cdi,
2485 unsigned long arg)
2486{
2487 cd_dbg(CD_DO_IOCTL, "%socking door\n", arg ? "L" : "Unl");
2488
2489 if (!CDROM_CAN(CDC_LOCK))
2490 return -EDRIVE_CANT_DO_THIS;
2491
2492 cdi->keeplocked = arg ? 1 : 0;
2493
2494
2495
2496
2497
2498 if (cdi->use_count != 1 && !arg && !capable(CAP_SYS_ADMIN))
2499 return -EBUSY;
2500 return cdi->ops->lock_door(cdi, arg);
2501}
2502
2503static int cdrom_ioctl_debug(struct cdrom_device_info *cdi,
2504 unsigned long arg)
2505{
2506 cd_dbg(CD_DO_IOCTL, "%sabling debug\n", arg ? "En" : "Dis");
2507
2508 if (!capable(CAP_SYS_ADMIN))
2509 return -EACCES;
2510 debug = arg ? 1 : 0;
2511 return debug;
2512}
2513
2514static int cdrom_ioctl_get_capability(struct cdrom_device_info *cdi)
2515{
2516 cd_dbg(CD_DO_IOCTL, "entering CDROM_GET_CAPABILITY\n");
2517 return (cdi->ops->capability & ~cdi->mask);
2518}
2519
2520
2521
2522
2523
2524
2525
2526static int cdrom_ioctl_get_mcn(struct cdrom_device_info *cdi,
2527 void __user *argp)
2528{
2529 struct cdrom_mcn mcn;
2530 int ret;
2531
2532 cd_dbg(CD_DO_IOCTL, "entering CDROM_GET_MCN\n");
2533
2534 if (!(cdi->ops->capability & CDC_MCN))
2535 return -ENOSYS;
2536 ret = cdi->ops->get_mcn(cdi, &mcn);
2537 if (ret)
2538 return ret;
2539
2540 if (copy_to_user(argp, &mcn, sizeof(mcn)))
2541 return -EFAULT;
2542 cd_dbg(CD_DO_IOCTL, "CDROM_GET_MCN successful\n");
2543 return 0;
2544}
2545
2546static int cdrom_ioctl_drive_status(struct cdrom_device_info *cdi,
2547 unsigned long arg)
2548{
2549 cd_dbg(CD_DO_IOCTL, "entering CDROM_DRIVE_STATUS\n");
2550
2551 if (!(cdi->ops->capability & CDC_DRIVE_STATUS))
2552 return -ENOSYS;
2553 if (!CDROM_CAN(CDC_SELECT_DISC) ||
2554 (arg == CDSL_CURRENT || arg == CDSL_NONE))
2555 return cdi->ops->drive_status(cdi, CDSL_CURRENT);
2556 if (arg >= cdi->capacity)
2557 return -EINVAL;
2558 return cdrom_slot_status(cdi, arg);
2559}
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578static int cdrom_ioctl_disc_status(struct cdrom_device_info *cdi)
2579{
2580 tracktype tracks;
2581
2582 cd_dbg(CD_DO_IOCTL, "entering CDROM_DISC_STATUS\n");
2583
2584 cdrom_count_tracks(cdi, &tracks);
2585 if (tracks.error)
2586 return tracks.error;
2587
2588
2589 if (tracks.audio > 0) {
2590 if (!tracks.data && !tracks.cdi && !tracks.xa)
2591 return CDS_AUDIO;
2592 else
2593 return CDS_MIXED;
2594 }
2595
2596 if (tracks.cdi > 0)
2597 return CDS_XA_2_2;
2598 if (tracks.xa > 0)
2599 return CDS_XA_2_1;
2600 if (tracks.data > 0)
2601 return CDS_DATA_1;
2602
2603
2604 cd_dbg(CD_WARNING, "This disc doesn't have any tracks I recognize!\n");
2605 return CDS_NO_INFO;
2606}
2607
2608static int cdrom_ioctl_changer_nslots(struct cdrom_device_info *cdi)
2609{
2610 cd_dbg(CD_DO_IOCTL, "entering CDROM_CHANGER_NSLOTS\n");
2611 return cdi->capacity;
2612}
2613
2614static int cdrom_ioctl_get_subchnl(struct cdrom_device_info *cdi,
2615 void __user *argp)
2616{
2617 struct cdrom_subchnl q;
2618 u8 requested, back;
2619 int ret;
2620
2621
2622
2623 if (copy_from_user(&q, argp, sizeof(q)))
2624 return -EFAULT;
2625
2626 requested = q.cdsc_format;
2627 if (requested != CDROM_MSF && requested != CDROM_LBA)
2628 return -EINVAL;
2629 q.cdsc_format = CDROM_MSF;
2630
2631 ret = cdi->ops->audio_ioctl(cdi, CDROMSUBCHNL, &q);
2632 if (ret)
2633 return ret;
2634
2635 back = q.cdsc_format;
2636 sanitize_format(&q.cdsc_absaddr, &back, requested);
2637 sanitize_format(&q.cdsc_reladdr, &q.cdsc_format, requested);
2638
2639 if (copy_to_user(argp, &q, sizeof(q)))
2640 return -EFAULT;
2641
2642 return 0;
2643}
2644
2645static int cdrom_ioctl_read_tochdr(struct cdrom_device_info *cdi,
2646 void __user *argp)
2647{
2648 struct cdrom_tochdr header;
2649 int ret;
2650
2651
2652
2653 if (copy_from_user(&header, argp, sizeof(header)))
2654 return -EFAULT;
2655
2656 ret = cdi->ops->audio_ioctl(cdi, CDROMREADTOCHDR, &header);
2657 if (ret)
2658 return ret;
2659
2660 if (copy_to_user(argp, &header, sizeof(header)))
2661 return -EFAULT;
2662
2663 return 0;
2664}
2665
2666static int cdrom_ioctl_read_tocentry(struct cdrom_device_info *cdi,
2667 void __user *argp)
2668{
2669 struct cdrom_tocentry entry;
2670 u8 requested_format;
2671 int ret;
2672
2673
2674
2675 if (copy_from_user(&entry, argp, sizeof(entry)))
2676 return -EFAULT;
2677
2678 requested_format = entry.cdte_format;
2679 if (requested_format != CDROM_MSF && requested_format != CDROM_LBA)
2680 return -EINVAL;
2681
2682 entry.cdte_format = CDROM_MSF;
2683 ret = cdi->ops->audio_ioctl(cdi, CDROMREADTOCENTRY, &entry);
2684 if (ret)
2685 return ret;
2686 sanitize_format(&entry.cdte_addr, &entry.cdte_format, requested_format);
2687
2688 if (copy_to_user(argp, &entry, sizeof(entry)))
2689 return -EFAULT;
2690
2691 return 0;
2692}
2693
2694static int cdrom_ioctl_play_msf(struct cdrom_device_info *cdi,
2695 void __user *argp)
2696{
2697 struct cdrom_msf msf;
2698
2699 cd_dbg(CD_DO_IOCTL, "entering CDROMPLAYMSF\n");
2700
2701 if (!CDROM_CAN(CDC_PLAY_AUDIO))
2702 return -ENOSYS;
2703 if (copy_from_user(&msf, argp, sizeof(msf)))
2704 return -EFAULT;
2705 return cdi->ops->audio_ioctl(cdi, CDROMPLAYMSF, &msf);
2706}
2707
2708static int cdrom_ioctl_play_trkind(struct cdrom_device_info *cdi,
2709 void __user *argp)
2710{
2711 struct cdrom_ti ti;
2712 int ret;
2713
2714 cd_dbg(CD_DO_IOCTL, "entering CDROMPLAYTRKIND\n");
2715
2716 if (!CDROM_CAN(CDC_PLAY_AUDIO))
2717 return -ENOSYS;
2718 if (copy_from_user(&ti, argp, sizeof(ti)))
2719 return -EFAULT;
2720
2721 ret = check_for_audio_disc(cdi, cdi->ops);
2722 if (ret)
2723 return ret;
2724 return cdi->ops->audio_ioctl(cdi, CDROMPLAYTRKIND, &ti);
2725}
2726static int cdrom_ioctl_volctrl(struct cdrom_device_info *cdi,
2727 void __user *argp)
2728{
2729 struct cdrom_volctrl volume;
2730
2731 cd_dbg(CD_DO_IOCTL, "entering CDROMVOLCTRL\n");
2732
2733 if (!CDROM_CAN(CDC_PLAY_AUDIO))
2734 return -ENOSYS;
2735 if (copy_from_user(&volume, argp, sizeof(volume)))
2736 return -EFAULT;
2737 return cdi->ops->audio_ioctl(cdi, CDROMVOLCTRL, &volume);
2738}
2739
2740static int cdrom_ioctl_volread(struct cdrom_device_info *cdi,
2741 void __user *argp)
2742{
2743 struct cdrom_volctrl volume;
2744 int ret;
2745
2746 cd_dbg(CD_DO_IOCTL, "entering CDROMVOLREAD\n");
2747
2748 if (!CDROM_CAN(CDC_PLAY_AUDIO))
2749 return -ENOSYS;
2750
2751 ret = cdi->ops->audio_ioctl(cdi, CDROMVOLREAD, &volume);
2752 if (ret)
2753 return ret;
2754
2755 if (copy_to_user(argp, &volume, sizeof(volume)))
2756 return -EFAULT;
2757 return 0;
2758}
2759
2760static int cdrom_ioctl_audioctl(struct cdrom_device_info *cdi,
2761 unsigned int cmd)
2762{
2763 int ret;
2764
2765 cd_dbg(CD_DO_IOCTL, "doing audio ioctl (start/stop/pause/resume)\n");
2766
2767 if (!CDROM_CAN(CDC_PLAY_AUDIO))
2768 return -ENOSYS;
2769 ret = check_for_audio_disc(cdi, cdi->ops);
2770 if (ret)
2771 return ret;
2772 return cdi->ops->audio_ioctl(cdi, cmd, NULL);
2773}
2774
2775
2776
2777
2778
2779static int cdrom_switch_blocksize(struct cdrom_device_info *cdi, int size)
2780{
2781 const struct cdrom_device_ops *cdo = cdi->ops;
2782 struct packet_command cgc;
2783 struct modesel_head mh;
2784
2785 memset(&mh, 0, sizeof(mh));
2786 mh.block_desc_length = 0x08;
2787 mh.block_length_med = (size >> 8) & 0xff;
2788 mh.block_length_lo = size & 0xff;
2789
2790 memset(&cgc, 0, sizeof(cgc));
2791 cgc.cmd[0] = 0x15;
2792 cgc.cmd[1] = 1 << 4;
2793 cgc.cmd[4] = 12;
2794 cgc.buflen = sizeof(mh);
2795 cgc.buffer = (char *) &mh;
2796 cgc.data_direction = CGC_DATA_WRITE;
2797 mh.block_desc_length = 0x08;
2798 mh.block_length_med = (size >> 8) & 0xff;
2799 mh.block_length_lo = size & 0xff;
2800
2801 return cdo->generic_packet(cdi, &cgc);
2802}
2803
2804static int cdrom_get_track_info(struct cdrom_device_info *cdi,
2805 __u16 track, __u8 type, track_information *ti)
2806{
2807 const struct cdrom_device_ops *cdo = cdi->ops;
2808 struct packet_command cgc;
2809 int ret, buflen;
2810
2811 init_cdrom_command(&cgc, ti, 8, CGC_DATA_READ);
2812 cgc.cmd[0] = GPCMD_READ_TRACK_RZONE_INFO;
2813 cgc.cmd[1] = type & 3;
2814 cgc.cmd[4] = (track & 0xff00) >> 8;
2815 cgc.cmd[5] = track & 0xff;
2816 cgc.cmd[8] = 8;
2817 cgc.quiet = 1;
2818
2819 ret = cdo->generic_packet(cdi, &cgc);
2820 if (ret)
2821 return ret;
2822
2823 buflen = be16_to_cpu(ti->track_information_length) +
2824 sizeof(ti->track_information_length);
2825
2826 if (buflen > sizeof(track_information))
2827 buflen = sizeof(track_information);
2828
2829 cgc.cmd[8] = cgc.buflen = buflen;
2830 ret = cdo->generic_packet(cdi, &cgc);
2831 if (ret)
2832 return ret;
2833
2834
2835 return buflen;
2836}
2837
2838
2839
2840int cdrom_get_last_written(struct cdrom_device_info *cdi, long *last_written)
2841{
2842 struct cdrom_tocentry toc;
2843 disc_information di;
2844 track_information ti;
2845 __u32 last_track;
2846 int ret = -1, ti_size;
2847
2848 if (!CDROM_CAN(CDC_GENERIC_PACKET))
2849 goto use_toc;
2850
2851 ret = cdrom_get_disc_info(cdi, &di);
2852 if (ret < (int)(offsetof(typeof(di), last_track_lsb)
2853 + sizeof(di.last_track_lsb)))
2854 goto use_toc;
2855
2856
2857 last_track = (di.last_track_msb << 8) | di.last_track_lsb;
2858 ti_size = cdrom_get_track_info(cdi, last_track, 1, &ti);
2859 if (ti_size < (int)offsetof(typeof(ti), track_start))
2860 goto use_toc;
2861
2862
2863 if (ti.blank) {
2864 if (last_track == 1)
2865 goto use_toc;
2866 last_track--;
2867 ti_size = cdrom_get_track_info(cdi, last_track, 1, &ti);
2868 }
2869
2870 if (ti_size < (int)(offsetof(typeof(ti), track_size)
2871 + sizeof(ti.track_size)))
2872 goto use_toc;
2873
2874
2875 if (ti.lra_v && ti_size >= (int)(offsetof(typeof(ti), last_rec_address)
2876 + sizeof(ti.last_rec_address))) {
2877 *last_written = be32_to_cpu(ti.last_rec_address);
2878 } else {
2879
2880 *last_written = be32_to_cpu(ti.track_start) +
2881 be32_to_cpu(ti.track_size);
2882 if (ti.free_blocks)
2883 *last_written -= (be32_to_cpu(ti.free_blocks) + 7);
2884 }
2885 return 0;
2886
2887
2888
2889
2890
2891use_toc:
2892 if (!CDROM_CAN(CDC_PLAY_AUDIO))
2893 return -ENOSYS;
2894
2895 toc.cdte_format = CDROM_MSF;
2896 toc.cdte_track = CDROM_LEADOUT;
2897 if ((ret = cdi->ops->audio_ioctl(cdi, CDROMREADTOCENTRY, &toc)))
2898 return ret;
2899 sanitize_format(&toc.cdte_addr, &toc.cdte_format, CDROM_LBA);
2900 *last_written = toc.cdte_addr.lba;
2901 return 0;
2902}
2903
2904
2905static int cdrom_get_next_writable(struct cdrom_device_info *cdi,
2906 long *next_writable)
2907{
2908 disc_information di;
2909 track_information ti;
2910 __u16 last_track;
2911 int ret, ti_size;
2912
2913 if (!CDROM_CAN(CDC_GENERIC_PACKET))
2914 goto use_last_written;
2915
2916 ret = cdrom_get_disc_info(cdi, &di);
2917 if (ret < 0 || ret < offsetof(typeof(di), last_track_lsb)
2918 + sizeof(di.last_track_lsb))
2919 goto use_last_written;
2920
2921
2922 last_track = (di.last_track_msb << 8) | di.last_track_lsb;
2923 ti_size = cdrom_get_track_info(cdi, last_track, 1, &ti);
2924 if (ti_size < 0 || ti_size < offsetof(typeof(ti), track_start))
2925 goto use_last_written;
2926
2927
2928 if (ti.blank) {
2929 if (last_track == 1)
2930 goto use_last_written;
2931 last_track--;
2932 ti_size = cdrom_get_track_info(cdi, last_track, 1, &ti);
2933 if (ti_size < 0)
2934 goto use_last_written;
2935 }
2936
2937
2938 if (ti.nwa_v && ti_size >= offsetof(typeof(ti), next_writable)
2939 + sizeof(ti.next_writable)) {
2940 *next_writable = be32_to_cpu(ti.next_writable);
2941 return 0;
2942 }
2943
2944use_last_written:
2945 ret = cdrom_get_last_written(cdi, next_writable);
2946 if (ret) {
2947 *next_writable = 0;
2948 return ret;
2949 } else {
2950 *next_writable += 7;
2951 return 0;
2952 }
2953}
2954
2955static noinline int mmc_ioctl_cdrom_read_data(struct cdrom_device_info *cdi,
2956 void __user *arg,
2957 struct packet_command *cgc,
2958 int cmd)
2959{
2960 struct scsi_sense_hdr sshdr;
2961 struct cdrom_msf msf;
2962 int blocksize = 0, format = 0, lba;
2963 int ret;
2964
2965 switch (cmd) {
2966 case CDROMREADRAW:
2967 blocksize = CD_FRAMESIZE_RAW;
2968 break;
2969 case CDROMREADMODE1:
2970 blocksize = CD_FRAMESIZE;
2971 format = 2;
2972 break;
2973 case CDROMREADMODE2:
2974 blocksize = CD_FRAMESIZE_RAW0;
2975 break;
2976 }
2977 if (copy_from_user(&msf, (struct cdrom_msf __user *)arg, sizeof(msf)))
2978 return -EFAULT;
2979 lba = msf_to_lba(msf.cdmsf_min0, msf.cdmsf_sec0, msf.cdmsf_frame0);
2980
2981 if (lba < 0)
2982 return -EINVAL;
2983
2984 cgc->buffer = kzalloc(blocksize, GFP_KERNEL);
2985 if (cgc->buffer == NULL)
2986 return -ENOMEM;
2987
2988 memset(&sshdr, 0, sizeof(sshdr));
2989 cgc->sshdr = &sshdr;
2990 cgc->data_direction = CGC_DATA_READ;
2991 ret = cdrom_read_block(cdi, cgc, lba, 1, format, blocksize);
2992 if (ret && sshdr.sense_key == 0x05 &&
2993 sshdr.asc == 0x20 &&
2994 sshdr.ascq == 0x00) {
2995
2996
2997
2998
2999
3000 ret = cdrom_switch_blocksize(cdi, blocksize);
3001 if (ret)
3002 goto out;
3003 cgc->sshdr = NULL;
3004 ret = cdrom_read_cd(cdi, cgc, lba, blocksize, 1);
3005 ret |= cdrom_switch_blocksize(cdi, blocksize);
3006 }
3007 if (!ret && copy_to_user(arg, cgc->buffer, blocksize))
3008 ret = -EFAULT;
3009out:
3010 kfree(cgc->buffer);
3011 return ret;
3012}
3013
3014static noinline int mmc_ioctl_cdrom_read_audio(struct cdrom_device_info *cdi,
3015 void __user *arg)
3016{
3017 struct cdrom_read_audio ra;
3018 int lba;
3019
3020#ifdef CONFIG_COMPAT
3021 if (in_compat_syscall()) {
3022 struct compat_cdrom_read_audio {
3023 union cdrom_addr addr;
3024 u8 addr_format;
3025 compat_int_t nframes;
3026 compat_caddr_t buf;
3027 } ra32;
3028
3029 if (copy_from_user(&ra32, arg, sizeof(ra32)))
3030 return -EFAULT;
3031
3032 ra = (struct cdrom_read_audio) {
3033 .addr = ra32.addr,
3034 .addr_format = ra32.addr_format,
3035 .nframes = ra32.nframes,
3036 .buf = compat_ptr(ra32.buf),
3037 };
3038 } else
3039#endif
3040 {
3041 if (copy_from_user(&ra, (struct cdrom_read_audio __user *)arg,
3042 sizeof(ra)))
3043 return -EFAULT;
3044 }
3045
3046 if (ra.addr_format == CDROM_MSF)
3047 lba = msf_to_lba(ra.addr.msf.minute,
3048 ra.addr.msf.second,
3049 ra.addr.msf.frame);
3050 else if (ra.addr_format == CDROM_LBA)
3051 lba = ra.addr.lba;
3052 else
3053 return -EINVAL;
3054
3055
3056 if (lba < 0 || ra.nframes <= 0 || ra.nframes > CD_FRAMES)
3057 return -EINVAL;
3058
3059 return cdrom_read_cdda(cdi, ra.buf, lba, ra.nframes);
3060}
3061
3062static noinline int mmc_ioctl_cdrom_subchannel(struct cdrom_device_info *cdi,
3063 void __user *arg)
3064{
3065 int ret;
3066 struct cdrom_subchnl q;
3067 u_char requested, back;
3068 if (copy_from_user(&q, (struct cdrom_subchnl __user *)arg, sizeof(q)))
3069 return -EFAULT;
3070 requested = q.cdsc_format;
3071 if (!((requested == CDROM_MSF) ||
3072 (requested == CDROM_LBA)))
3073 return -EINVAL;
3074
3075 ret = cdrom_read_subchannel(cdi, &q, 0);
3076 if (ret)
3077 return ret;
3078 back = q.cdsc_format;
3079 sanitize_format(&q.cdsc_absaddr, &back, requested);
3080 sanitize_format(&q.cdsc_reladdr, &q.cdsc_format, requested);
3081 if (copy_to_user((struct cdrom_subchnl __user *)arg, &q, sizeof(q)))
3082 return -EFAULT;
3083
3084 return 0;
3085}
3086
3087static noinline int mmc_ioctl_cdrom_play_msf(struct cdrom_device_info *cdi,
3088 void __user *arg,
3089 struct packet_command *cgc)
3090{
3091 const struct cdrom_device_ops *cdo = cdi->ops;
3092 struct cdrom_msf msf;
3093 cd_dbg(CD_DO_IOCTL, "entering CDROMPLAYMSF\n");
3094 if (copy_from_user(&msf, (struct cdrom_msf __user *)arg, sizeof(msf)))
3095 return -EFAULT;
3096 cgc->cmd[0] = GPCMD_PLAY_AUDIO_MSF;
3097 cgc->cmd[3] = msf.cdmsf_min0;
3098 cgc->cmd[4] = msf.cdmsf_sec0;
3099 cgc->cmd[5] = msf.cdmsf_frame0;
3100 cgc->cmd[6] = msf.cdmsf_min1;
3101 cgc->cmd[7] = msf.cdmsf_sec1;
3102 cgc->cmd[8] = msf.cdmsf_frame1;
3103 cgc->data_direction = CGC_DATA_NONE;
3104 return cdo->generic_packet(cdi, cgc);
3105}
3106
3107static noinline int mmc_ioctl_cdrom_play_blk(struct cdrom_device_info *cdi,
3108 void __user *arg,
3109 struct packet_command *cgc)
3110{
3111 const struct cdrom_device_ops *cdo = cdi->ops;
3112 struct cdrom_blk blk;
3113 cd_dbg(CD_DO_IOCTL, "entering CDROMPLAYBLK\n");
3114 if (copy_from_user(&blk, (struct cdrom_blk __user *)arg, sizeof(blk)))
3115 return -EFAULT;
3116 cgc->cmd[0] = GPCMD_PLAY_AUDIO_10;
3117 cgc->cmd[2] = (blk.from >> 24) & 0xff;
3118 cgc->cmd[3] = (blk.from >> 16) & 0xff;
3119 cgc->cmd[4] = (blk.from >> 8) & 0xff;
3120 cgc->cmd[5] = blk.from & 0xff;
3121 cgc->cmd[7] = (blk.len >> 8) & 0xff;
3122 cgc->cmd[8] = blk.len & 0xff;
3123 cgc->data_direction = CGC_DATA_NONE;
3124 return cdo->generic_packet(cdi, cgc);
3125}
3126
3127static noinline int mmc_ioctl_cdrom_volume(struct cdrom_device_info *cdi,
3128 void __user *arg,
3129 struct packet_command *cgc,
3130 unsigned int cmd)
3131{
3132 struct cdrom_volctrl volctrl;
3133 unsigned char buffer[32];
3134 char mask[sizeof(buffer)];
3135 unsigned short offset;
3136 int ret;
3137
3138 cd_dbg(CD_DO_IOCTL, "entering CDROMVOLUME\n");
3139
3140 if (copy_from_user(&volctrl, (struct cdrom_volctrl __user *)arg,
3141 sizeof(volctrl)))
3142 return -EFAULT;
3143
3144 cgc->buffer = buffer;
3145 cgc->buflen = 24;
3146 ret = cdrom_mode_sense(cdi, cgc, GPMODE_AUDIO_CTL_PAGE, 0);
3147 if (ret)
3148 return ret;
3149
3150
3151
3152
3153
3154 offset = 8 + be16_to_cpu(*(__be16 *)(buffer + 6));
3155
3156 if (offset + 16 > sizeof(buffer))
3157 return -E2BIG;
3158
3159 if (offset + 16 > cgc->buflen) {
3160 cgc->buflen = offset + 16;
3161 ret = cdrom_mode_sense(cdi, cgc,
3162 GPMODE_AUDIO_CTL_PAGE, 0);
3163 if (ret)
3164 return ret;
3165 }
3166
3167
3168 if ((buffer[offset] & 0x3f) != GPMODE_AUDIO_CTL_PAGE ||
3169 buffer[offset + 1] < 14)
3170 return -EINVAL;
3171
3172
3173
3174 if (cmd == CDROMVOLREAD) {
3175 volctrl.channel0 = buffer[offset+9];
3176 volctrl.channel1 = buffer[offset+11];
3177 volctrl.channel2 = buffer[offset+13];
3178 volctrl.channel3 = buffer[offset+15];
3179 if (copy_to_user((struct cdrom_volctrl __user *)arg, &volctrl,
3180 sizeof(volctrl)))
3181 return -EFAULT;
3182 return 0;
3183 }
3184
3185
3186 cgc->buffer = mask;
3187 ret = cdrom_mode_sense(cdi, cgc, GPMODE_AUDIO_CTL_PAGE, 1);
3188 if (ret)
3189 return ret;
3190
3191 buffer[offset + 9] = volctrl.channel0 & mask[offset + 9];
3192 buffer[offset + 11] = volctrl.channel1 & mask[offset + 11];
3193 buffer[offset + 13] = volctrl.channel2 & mask[offset + 13];
3194 buffer[offset + 15] = volctrl.channel3 & mask[offset + 15];
3195
3196
3197 cgc->buffer = buffer + offset - 8;
3198 memset(cgc->buffer, 0, 8);
3199 return cdrom_mode_select(cdi, cgc);
3200}
3201
3202static noinline int mmc_ioctl_cdrom_start_stop(struct cdrom_device_info *cdi,
3203 struct packet_command *cgc,
3204 int cmd)
3205{
3206 const struct cdrom_device_ops *cdo = cdi->ops;
3207 cd_dbg(CD_DO_IOCTL, "entering CDROMSTART/CDROMSTOP\n");
3208 cgc->cmd[0] = GPCMD_START_STOP_UNIT;
3209 cgc->cmd[1] = 1;
3210 cgc->cmd[4] = (cmd == CDROMSTART) ? 1 : 0;
3211 cgc->data_direction = CGC_DATA_NONE;
3212 return cdo->generic_packet(cdi, cgc);
3213}
3214
3215static noinline int mmc_ioctl_cdrom_pause_resume(struct cdrom_device_info *cdi,
3216 struct packet_command *cgc,
3217 int cmd)
3218{
3219 const struct cdrom_device_ops *cdo = cdi->ops;
3220 cd_dbg(CD_DO_IOCTL, "entering CDROMPAUSE/CDROMRESUME\n");
3221 cgc->cmd[0] = GPCMD_PAUSE_RESUME;
3222 cgc->cmd[8] = (cmd == CDROMRESUME) ? 1 : 0;
3223 cgc->data_direction = CGC_DATA_NONE;
3224 return cdo->generic_packet(cdi, cgc);
3225}
3226
3227static noinline int mmc_ioctl_dvd_read_struct(struct cdrom_device_info *cdi,
3228 void __user *arg,
3229 struct packet_command *cgc)
3230{
3231 int ret;
3232 dvd_struct *s;
3233 int size = sizeof(dvd_struct);
3234
3235 if (!CDROM_CAN(CDC_DVD))
3236 return -ENOSYS;
3237
3238 s = memdup_user(arg, size);
3239 if (IS_ERR(s))
3240 return PTR_ERR(s);
3241
3242 cd_dbg(CD_DO_IOCTL, "entering DVD_READ_STRUCT\n");
3243
3244 ret = dvd_read_struct(cdi, s, cgc);
3245 if (ret)
3246 goto out;
3247
3248 if (copy_to_user(arg, s, size))
3249 ret = -EFAULT;
3250out:
3251 kfree(s);
3252 return ret;
3253}
3254
3255static noinline int mmc_ioctl_dvd_auth(struct cdrom_device_info *cdi,
3256 void __user *arg)
3257{
3258 int ret;
3259 dvd_authinfo ai;
3260 if (!CDROM_CAN(CDC_DVD))
3261 return -ENOSYS;
3262 cd_dbg(CD_DO_IOCTL, "entering DVD_AUTH\n");
3263 if (copy_from_user(&ai, (dvd_authinfo __user *)arg, sizeof(ai)))
3264 return -EFAULT;
3265 ret = dvd_do_auth(cdi, &ai);
3266 if (ret)
3267 return ret;
3268 if (copy_to_user((dvd_authinfo __user *)arg, &ai, sizeof(ai)))
3269 return -EFAULT;
3270 return 0;
3271}
3272
3273static noinline int mmc_ioctl_cdrom_next_writable(struct cdrom_device_info *cdi,
3274 void __user *arg)
3275{
3276 int ret;
3277 long next = 0;
3278 cd_dbg(CD_DO_IOCTL, "entering CDROM_NEXT_WRITABLE\n");
3279 ret = cdrom_get_next_writable(cdi, &next);
3280 if (ret)
3281 return ret;
3282 if (copy_to_user((long __user *)arg, &next, sizeof(next)))
3283 return -EFAULT;
3284 return 0;
3285}
3286
3287static noinline int mmc_ioctl_cdrom_last_written(struct cdrom_device_info *cdi,
3288 void __user *arg)
3289{
3290 int ret;
3291 long last = 0;
3292 cd_dbg(CD_DO_IOCTL, "entering CDROM_LAST_WRITTEN\n");
3293 ret = cdrom_get_last_written(cdi, &last);
3294 if (ret)
3295 return ret;
3296 if (in_compat_syscall())
3297 return put_user(last, (__s32 __user *)arg);
3298
3299 return put_user(last, (long __user *)arg);
3300}
3301
3302static int mmc_ioctl(struct cdrom_device_info *cdi, unsigned int cmd,
3303 unsigned long arg)
3304{
3305 struct packet_command cgc;
3306 void __user *userptr = (void __user *)arg;
3307
3308 memset(&cgc, 0, sizeof(cgc));
3309
3310
3311
3312 switch (cmd) {
3313 case CDROMREADRAW:
3314 case CDROMREADMODE1:
3315 case CDROMREADMODE2:
3316 return mmc_ioctl_cdrom_read_data(cdi, userptr, &cgc, cmd);
3317 case CDROMREADAUDIO:
3318 return mmc_ioctl_cdrom_read_audio(cdi, userptr);
3319 case CDROMSUBCHNL:
3320 return mmc_ioctl_cdrom_subchannel(cdi, userptr);
3321 case CDROMPLAYMSF:
3322 return mmc_ioctl_cdrom_play_msf(cdi, userptr, &cgc);
3323 case CDROMPLAYBLK:
3324 return mmc_ioctl_cdrom_play_blk(cdi, userptr, &cgc);
3325 case CDROMVOLCTRL:
3326 case CDROMVOLREAD:
3327 return mmc_ioctl_cdrom_volume(cdi, userptr, &cgc, cmd);
3328 case CDROMSTART:
3329 case CDROMSTOP:
3330 return mmc_ioctl_cdrom_start_stop(cdi, &cgc, cmd);
3331 case CDROMPAUSE:
3332 case CDROMRESUME:
3333 return mmc_ioctl_cdrom_pause_resume(cdi, &cgc, cmd);
3334 case DVD_READ_STRUCT:
3335 return mmc_ioctl_dvd_read_struct(cdi, userptr, &cgc);
3336 case DVD_AUTH:
3337 return mmc_ioctl_dvd_auth(cdi, userptr);
3338 case CDROM_NEXT_WRITABLE:
3339 return mmc_ioctl_cdrom_next_writable(cdi, userptr);
3340 case CDROM_LAST_WRITTEN:
3341 return mmc_ioctl_cdrom_last_written(cdi, userptr);
3342 }
3343
3344 return -ENOTTY;
3345}
3346
3347
3348
3349
3350
3351
3352int cdrom_ioctl(struct cdrom_device_info *cdi, struct block_device *bdev,
3353 fmode_t mode, unsigned int cmd, unsigned long arg)
3354{
3355 void __user *argp = (void __user *)arg;
3356 int ret;
3357
3358
3359
3360
3361 ret = scsi_cmd_blk_ioctl(bdev, mode, cmd, argp);
3362 if (ret != -ENOTTY)
3363 return ret;
3364
3365 switch (cmd) {
3366 case CDROMMULTISESSION:
3367 return cdrom_ioctl_multisession(cdi, argp);
3368 case CDROMEJECT:
3369 return cdrom_ioctl_eject(cdi);
3370 case CDROMCLOSETRAY:
3371 return cdrom_ioctl_closetray(cdi);
3372 case CDROMEJECT_SW:
3373 return cdrom_ioctl_eject_sw(cdi, arg);
3374 case CDROM_MEDIA_CHANGED:
3375 return cdrom_ioctl_media_changed(cdi, arg);
3376 case CDROM_SET_OPTIONS:
3377 return cdrom_ioctl_set_options(cdi, arg);
3378 case CDROM_CLEAR_OPTIONS:
3379 return cdrom_ioctl_clear_options(cdi, arg);
3380 case CDROM_SELECT_SPEED:
3381 return cdrom_ioctl_select_speed(cdi, arg);
3382 case CDROM_SELECT_DISC:
3383 return cdrom_ioctl_select_disc(cdi, arg);
3384 case CDROMRESET:
3385 return cdrom_ioctl_reset(cdi, bdev);
3386 case CDROM_LOCKDOOR:
3387 return cdrom_ioctl_lock_door(cdi, arg);
3388 case CDROM_DEBUG:
3389 return cdrom_ioctl_debug(cdi, arg);
3390 case CDROM_GET_CAPABILITY:
3391 return cdrom_ioctl_get_capability(cdi);
3392 case CDROM_GET_MCN:
3393 return cdrom_ioctl_get_mcn(cdi, argp);
3394 case CDROM_DRIVE_STATUS:
3395 return cdrom_ioctl_drive_status(cdi, arg);
3396 case CDROM_DISC_STATUS:
3397 return cdrom_ioctl_disc_status(cdi);
3398 case CDROM_CHANGER_NSLOTS:
3399 return cdrom_ioctl_changer_nslots(cdi);
3400 }
3401
3402
3403
3404
3405
3406
3407
3408 if (CDROM_CAN(CDC_GENERIC_PACKET)) {
3409 ret = mmc_ioctl(cdi, cmd, arg);
3410 if (ret != -ENOTTY)
3411 return ret;
3412 }
3413
3414
3415
3416
3417
3418
3419 switch (cmd) {
3420 case CDROMSUBCHNL:
3421 return cdrom_ioctl_get_subchnl(cdi, argp);
3422 case CDROMREADTOCHDR:
3423 return cdrom_ioctl_read_tochdr(cdi, argp);
3424 case CDROMREADTOCENTRY:
3425 return cdrom_ioctl_read_tocentry(cdi, argp);
3426 case CDROMPLAYMSF:
3427 return cdrom_ioctl_play_msf(cdi, argp);
3428 case CDROMPLAYTRKIND:
3429 return cdrom_ioctl_play_trkind(cdi, argp);
3430 case CDROMVOLCTRL:
3431 return cdrom_ioctl_volctrl(cdi, argp);
3432 case CDROMVOLREAD:
3433 return cdrom_ioctl_volread(cdi, argp);
3434 case CDROMSTART:
3435 case CDROMSTOP:
3436 case CDROMPAUSE:
3437 case CDROMRESUME:
3438 return cdrom_ioctl_audioctl(cdi, cmd);
3439 }
3440
3441 return -ENOSYS;
3442}
3443
3444EXPORT_SYMBOL(cdrom_get_last_written);
3445EXPORT_SYMBOL(register_cdrom);
3446EXPORT_SYMBOL(unregister_cdrom);
3447EXPORT_SYMBOL(cdrom_open);
3448EXPORT_SYMBOL(cdrom_release);
3449EXPORT_SYMBOL(cdrom_ioctl);
3450EXPORT_SYMBOL(cdrom_media_changed);
3451EXPORT_SYMBOL(cdrom_number_of_slots);
3452EXPORT_SYMBOL(cdrom_mode_select);
3453EXPORT_SYMBOL(cdrom_mode_sense);
3454EXPORT_SYMBOL(init_cdrom_command);
3455EXPORT_SYMBOL(cdrom_get_media_event);
3456
3457#ifdef CONFIG_SYSCTL
3458
3459#define CDROM_STR_SIZE 1000
3460
3461static struct cdrom_sysctl_settings {
3462 char info[CDROM_STR_SIZE];
3463 int autoclose;
3464 int autoeject;
3465 int debug;
3466 int lock;
3467 int check;
3468} cdrom_sysctl_settings;
3469
3470enum cdrom_print_option {
3471 CTL_NAME,
3472 CTL_SPEED,
3473 CTL_SLOTS,
3474 CTL_CAPABILITY
3475};
3476
3477static int cdrom_print_info(const char *header, int val, char *info,
3478 int *pos, enum cdrom_print_option option)
3479{
3480 const int max_size = sizeof(cdrom_sysctl_settings.info);
3481 struct cdrom_device_info *cdi;
3482 int ret;
3483
3484 ret = scnprintf(info + *pos, max_size - *pos, header);
3485 if (!ret)
3486 return 1;
3487
3488 *pos += ret;
3489
3490 list_for_each_entry(cdi, &cdrom_list, list) {
3491 switch (option) {
3492 case CTL_NAME:
3493 ret = scnprintf(info + *pos, max_size - *pos,
3494 "\t%s", cdi->name);
3495 break;
3496 case CTL_SPEED:
3497 ret = scnprintf(info + *pos, max_size - *pos,
3498 "\t%d", cdi->speed);
3499 break;
3500 case CTL_SLOTS:
3501 ret = scnprintf(info + *pos, max_size - *pos,
3502 "\t%d", cdi->capacity);
3503 break;
3504 case CTL_CAPABILITY:
3505 ret = scnprintf(info + *pos, max_size - *pos,
3506 "\t%d", CDROM_CAN(val) != 0);
3507 break;
3508 default:
3509 pr_info("invalid option%d\n", option);
3510 return 1;
3511 }
3512 if (!ret)
3513 return 1;
3514 *pos += ret;
3515 }
3516
3517 return 0;
3518}
3519
3520static int cdrom_sysctl_info(struct ctl_table *ctl, int write,
3521 void __user *buffer, size_t *lenp, loff_t *ppos)
3522{
3523 int pos;
3524 char *info = cdrom_sysctl_settings.info;
3525 const int max_size = sizeof(cdrom_sysctl_settings.info);
3526
3527 if (!*lenp || (*ppos && !write)) {
3528 *lenp = 0;
3529 return 0;
3530 }
3531
3532 mutex_lock(&cdrom_mutex);
3533
3534 pos = sprintf(info, "CD-ROM information, " VERSION "\n");
3535
3536 if (cdrom_print_info("\ndrive name:\t", 0, info, &pos, CTL_NAME))
3537 goto done;
3538 if (cdrom_print_info("\ndrive speed:\t", 0, info, &pos, CTL_SPEED))
3539 goto done;
3540 if (cdrom_print_info("\ndrive # of slots:", 0, info, &pos, CTL_SLOTS))
3541 goto done;
3542 if (cdrom_print_info("\nCan close tray:\t",
3543 CDC_CLOSE_TRAY, info, &pos, CTL_CAPABILITY))
3544 goto done;
3545 if (cdrom_print_info("\nCan open tray:\t",
3546 CDC_OPEN_TRAY, info, &pos, CTL_CAPABILITY))
3547 goto done;
3548 if (cdrom_print_info("\nCan lock tray:\t",
3549 CDC_LOCK, info, &pos, CTL_CAPABILITY))
3550 goto done;
3551 if (cdrom_print_info("\nCan change speed:",
3552 CDC_SELECT_SPEED, info, &pos, CTL_CAPABILITY))
3553 goto done;
3554 if (cdrom_print_info("\nCan select disk:",
3555 CDC_SELECT_DISC, info, &pos, CTL_CAPABILITY))
3556 goto done;
3557 if (cdrom_print_info("\nCan read multisession:",
3558 CDC_MULTI_SESSION, info, &pos, CTL_CAPABILITY))
3559 goto done;
3560 if (cdrom_print_info("\nCan read MCN:\t",
3561 CDC_MCN, info, &pos, CTL_CAPABILITY))
3562 goto done;
3563 if (cdrom_print_info("\nReports media changed:",
3564 CDC_MEDIA_CHANGED, info, &pos, CTL_CAPABILITY))
3565 goto done;
3566 if (cdrom_print_info("\nCan play audio:\t",
3567 CDC_PLAY_AUDIO, info, &pos, CTL_CAPABILITY))
3568 goto done;
3569 if (cdrom_print_info("\nCan write CD-R:\t",
3570 CDC_CD_R, info, &pos, CTL_CAPABILITY))
3571 goto done;
3572 if (cdrom_print_info("\nCan write CD-RW:",
3573 CDC_CD_RW, info, &pos, CTL_CAPABILITY))
3574 goto done;
3575 if (cdrom_print_info("\nCan read DVD:\t",
3576 CDC_DVD, info, &pos, CTL_CAPABILITY))
3577 goto done;
3578 if (cdrom_print_info("\nCan write DVD-R:",
3579 CDC_DVD_R, info, &pos, CTL_CAPABILITY))
3580 goto done;
3581 if (cdrom_print_info("\nCan write DVD-RAM:",
3582 CDC_DVD_RAM, info, &pos, CTL_CAPABILITY))
3583 goto done;
3584 if (cdrom_print_info("\nCan read MRW:\t",
3585 CDC_MRW, info, &pos, CTL_CAPABILITY))
3586 goto done;
3587 if (cdrom_print_info("\nCan write MRW:\t",
3588 CDC_MRW_W, info, &pos, CTL_CAPABILITY))
3589 goto done;
3590 if (cdrom_print_info("\nCan write RAM:\t",
3591 CDC_RAM, info, &pos, CTL_CAPABILITY))
3592 goto done;
3593 if (!scnprintf(info + pos, max_size - pos, "\n\n"))
3594 goto done;
3595doit:
3596 mutex_unlock(&cdrom_mutex);
3597 return proc_dostring(ctl, write, buffer, lenp, ppos);
3598done:
3599 pr_info("info buffer too small\n");
3600 goto doit;
3601}
3602
3603
3604
3605
3606
3607static void cdrom_update_settings(void)
3608{
3609 struct cdrom_device_info *cdi;
3610
3611 mutex_lock(&cdrom_mutex);
3612 list_for_each_entry(cdi, &cdrom_list, list) {
3613 if (autoclose && CDROM_CAN(CDC_CLOSE_TRAY))
3614 cdi->options |= CDO_AUTO_CLOSE;
3615 else if (!autoclose)
3616 cdi->options &= ~CDO_AUTO_CLOSE;
3617 if (autoeject && CDROM_CAN(CDC_OPEN_TRAY))
3618 cdi->options |= CDO_AUTO_EJECT;
3619 else if (!autoeject)
3620 cdi->options &= ~CDO_AUTO_EJECT;
3621 if (lockdoor && CDROM_CAN(CDC_LOCK))
3622 cdi->options |= CDO_LOCK;
3623 else if (!lockdoor)
3624 cdi->options &= ~CDO_LOCK;
3625 if (check_media_type)
3626 cdi->options |= CDO_CHECK_TYPE;
3627 else
3628 cdi->options &= ~CDO_CHECK_TYPE;
3629 }
3630 mutex_unlock(&cdrom_mutex);
3631}
3632
3633static int cdrom_sysctl_handler(struct ctl_table *ctl, int write,
3634 void __user *buffer, size_t *lenp, loff_t *ppos)
3635{
3636 int ret;
3637
3638 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
3639
3640 if (write) {
3641
3642
3643 autoclose = !!cdrom_sysctl_settings.autoclose;
3644 autoeject = !!cdrom_sysctl_settings.autoeject;
3645 debug = !!cdrom_sysctl_settings.debug;
3646 lockdoor = !!cdrom_sysctl_settings.lock;
3647 check_media_type = !!cdrom_sysctl_settings.check;
3648
3649
3650
3651
3652 cdrom_update_settings();
3653 }
3654
3655 return ret;
3656}
3657
3658
3659static struct ctl_table cdrom_table[] = {
3660 {
3661 .procname = "info",
3662 .data = &cdrom_sysctl_settings.info,
3663 .maxlen = CDROM_STR_SIZE,
3664 .mode = 0444,
3665 .proc_handler = cdrom_sysctl_info,
3666 },
3667 {
3668 .procname = "autoclose",
3669 .data = &cdrom_sysctl_settings.autoclose,
3670 .maxlen = sizeof(int),
3671 .mode = 0644,
3672 .proc_handler = cdrom_sysctl_handler,
3673 },
3674 {
3675 .procname = "autoeject",
3676 .data = &cdrom_sysctl_settings.autoeject,
3677 .maxlen = sizeof(int),
3678 .mode = 0644,
3679 .proc_handler = cdrom_sysctl_handler,
3680 },
3681 {
3682 .procname = "debug",
3683 .data = &cdrom_sysctl_settings.debug,
3684 .maxlen = sizeof(int),
3685 .mode = 0644,
3686 .proc_handler = cdrom_sysctl_handler,
3687 },
3688 {
3689 .procname = "lock",
3690 .data = &cdrom_sysctl_settings.lock,
3691 .maxlen = sizeof(int),
3692 .mode = 0644,
3693 .proc_handler = cdrom_sysctl_handler,
3694 },
3695 {
3696 .procname = "check_media",
3697 .data = &cdrom_sysctl_settings.check,
3698 .maxlen = sizeof(int),
3699 .mode = 0644,
3700 .proc_handler = cdrom_sysctl_handler
3701 },
3702 { }
3703};
3704
3705static struct ctl_table cdrom_cdrom_table[] = {
3706 {
3707 .procname = "cdrom",
3708 .maxlen = 0,
3709 .mode = 0555,
3710 .child = cdrom_table,
3711 },
3712 { }
3713};
3714
3715
3716static struct ctl_table cdrom_root_table[] = {
3717 {
3718 .procname = "dev",
3719 .maxlen = 0,
3720 .mode = 0555,
3721 .child = cdrom_cdrom_table,
3722 },
3723 { }
3724};
3725static struct ctl_table_header *cdrom_sysctl_header;
3726
3727static void cdrom_sysctl_register(void)
3728{
3729 static atomic_t initialized = ATOMIC_INIT(0);
3730
3731 if (!atomic_add_unless(&initialized, 1, 1))
3732 return;
3733
3734 cdrom_sysctl_header = register_sysctl_table(cdrom_root_table);
3735
3736
3737 cdrom_sysctl_settings.autoclose = autoclose;
3738 cdrom_sysctl_settings.autoeject = autoeject;
3739 cdrom_sysctl_settings.debug = debug;
3740 cdrom_sysctl_settings.lock = lockdoor;
3741 cdrom_sysctl_settings.check = check_media_type;
3742}
3743
3744static void cdrom_sysctl_unregister(void)
3745{
3746 if (cdrom_sysctl_header)
3747 unregister_sysctl_table(cdrom_sysctl_header);
3748}
3749
3750#else
3751
3752static void cdrom_sysctl_register(void)
3753{
3754}
3755
3756static void cdrom_sysctl_unregister(void)
3757{
3758}
3759
3760#endif
3761
3762static int __init cdrom_init(void)
3763{
3764 cdrom_sysctl_register();
3765
3766 return 0;
3767}
3768
3769static void __exit cdrom_exit(void)
3770{
3771 pr_info("Uniform CD-ROM driver unloaded\n");
3772 cdrom_sysctl_unregister();
3773}
3774
3775module_init(cdrom_init);
3776module_exit(cdrom_exit);
3777MODULE_LICENSE("GPL");
3778