This source file includes following definitions.
- cdu535_check_media_change
- enable_interrupts
- disable_interrupts
- cdu535_interrupt
- sony_sleep
- select_unit
- read_result_reg
- read_exec_status
- check_drive_status
- do_sony_cmd
- set_drive_mode
- seek_and_read_N_blocks
- request_toc_data
- spin_up_drive
- int_to_bcd
- bcd_to_int
- log_to_msf
- msf_to_log
- size_to_buf
- do_cdu535_request
- sony_get_toc
- find_track
- read_subcode
- sony_get_subchnl_info
- cdu_ioctl
- cdu_open
- cdu_release
- sony535_init
- sonycd535_setup
- cleanup_module
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 #include <linux/config.h>
111
112 #ifdef MODULE
113 # include <linux/module.h>
114 # include <linux/version.h>
115 # ifndef CONFIG_MODVERSIONS
116 char kernel_version[]= UTS_RELEASE;
117 # endif
118 #define sony535_init init_module
119 #else
120 # define MOD_INC_USE_COUNT
121 # define MOD_DEC_USE_COUNT
122 #endif
123
124 #include <linux/errno.h>
125 #include <linux/signal.h>
126 #include <linux/sched.h>
127 #include <linux/timer.h>
128 #include <linux/fs.h>
129 #include <linux/kernel.h>
130 #include <linux/ioport.h>
131 #include <linux/hdreg.h>
132 #include <linux/genhd.h>
133 #include <linux/mm.h>
134 #include <linux/malloc.h>
135
136 #define REALLY_SLOW_IO
137 #include <asm/system.h>
138 #include <asm/io.h>
139 #include <asm/segment.h>
140
141 #include <linux/cdrom.h>
142
143 #define MAJOR_NR CDU535_CDROM_MAJOR
144 # include <linux/blk.h>
145 #define sony535_cd_base_io sonycd535
146 #include <linux/sonycd535.h>
147
148
149
150
151
152
153
154 #ifndef CDU535_ADDRESS
155 # define CDU535_ADDRESS 0x340
156 #endif
157 #ifndef CDU535_INTERRUPT
158 # define CDU535_INTERRUPT 0
159 #endif
160 #ifndef CDU535_HANDLE
161 # define CDU535_HANDLE "cdu535"
162 #endif
163 #ifndef CDU535_MESSAGE_NAME
164 # define CDU535_MESSAGE_NAME "Sony CDU-535"
165 #endif
166
167 #ifndef MAX_SPINUP_RETRY
168 # define MAX_SPINUP_RETRY 3
169 #endif
170 #ifndef RETRY_FOR_BAD_STATUS
171 # define RETRY_FOR_BAD_STATUS 100
172 #endif
173
174 #ifndef DEBUG
175 # define DEBUG 1
176 #endif
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192 #define SONY535_BUFFER_SIZE (64*1024)
193
194
195
196
197
198 #ifndef NO_LOCK_DOORS
199 # define LOCK_DOORS
200 #endif
201
202 static int read_subcode(void);
203 static void sony_get_toc(void);
204 static int cdu_open(struct inode *inode, struct file *filp);
205 static inline unsigned int int_to_bcd(unsigned int val);
206 static unsigned int bcd_to_int(unsigned int bcd);
207 static int do_sony_cmd(Byte * cmd, int nCmd, Byte status[2],
208 Byte * response, int n_response, int ignoreStatusBit7);
209
210
211
212 #ifndef MODULE
213 static
214 #endif
215 unsigned short sony535_cd_base_io = CDU535_ADDRESS;
216
217
218
219
220
221 static unsigned short select_unit_reg;
222 static unsigned short result_reg;
223 static unsigned short command_reg;
224 static unsigned short read_status_reg;
225 static unsigned short data_reg;
226
227 static int initialized = 0;
228 static int sony_disc_changed = 1;
229
230 static int sony_toc_read = 0;
231
232 static unsigned int sony_buffer_size;
233
234 static unsigned int sony_buffer_sectors;
235
236 static unsigned int sony_usage = 0;
237
238
239 static int sony_first_block = -1;
240
241 static int sony_last_block = -1;
242
243
244 static struct s535_sony_toc *sony_toc;
245
246
247 static struct s535_sony_subcode *last_sony_subcode;
248
249 static Byte **sony_buffer;
250
251
252 static int sony_inuse = 0;
253
254
255
256
257
258
259 static int sony_audio_status = CDROM_AUDIO_NO_STATUS;
260
261
262
263
264
265
266
267
268
269
270 static Byte cur_pos_msf[3] = {0, 0, 0};
271 static Byte final_pos_msf[3] = {0, 0, 0};
272
273
274 #ifndef MODULE
275 static
276 #endif
277 int sony535_irq_used = CDU535_INTERRUPT;
278
279
280 static struct wait_queue *cdu535_irq_wait = NULL;
281
282
283
284
285
286
287 static int
288 cdu535_check_media_change(kdev_t full_dev)
289 {
290 int retval;
291
292 if (MINOR(full_dev) != 0) {
293 printk(CDU535_MESSAGE_NAME " request error: invalid device.\n");
294 return 0;
295 }
296
297
298 retval = initialized ? sony_disc_changed : 0;
299 sony_disc_changed = 0;
300 return retval;
301 }
302
303 static inline void
304 enable_interrupts(void)
305 {
306 #ifdef USE_IRQ
307
308
309
310 curr_control_reg |= ( SONY_ATTN_INT_EN_BIT
311 | SONY_RES_RDY_INT_EN_BIT
312 | SONY_DATA_RDY_INT_EN_BIT);
313 outb(curr_control_reg, sony_cd_control_reg);
314 #endif
315 }
316
317 static inline void
318 disable_interrupts(void)
319 {
320 #ifdef USE_IRQ
321
322
323
324 curr_control_reg &= ~(SONY_ATTN_INT_EN_BIT
325 | SONY_RES_RDY_INT_EN_BIT
326 | SONY_DATA_RDY_INT_EN_BIT);
327 outb(curr_control_reg, sony_cd_control_reg);
328 #endif
329 }
330
331 static void
332 cdu535_interrupt(int irq, struct pt_regs *regs)
333 {
334 disable_interrupts();
335 if (cdu535_irq_wait != NULL)
336 wake_up(&cdu535_irq_wait);
337 else
338 printk(CDU535_MESSAGE_NAME
339 ": Got an interrupt but nothing was waiting\n");
340 }
341
342
343
344
345
346
347
348 static inline void
349 sony_sleep(void)
350 {
351 if (sony535_irq_used <= 0) {
352 current->state = TASK_INTERRUPTIBLE;
353 current->timeout = jiffies;
354 schedule();
355 } else {
356 cli();
357 enable_interrupts();
358 interruptible_sleep_on(&cdu535_irq_wait);
359 sti();
360 }
361 }
362
363
364
365
366
367
368
369
370 static void
371 select_unit(int unit_no)
372 {
373 unsigned int select_mask = ~(1 << unit_no);
374 outb(select_mask, select_unit_reg);
375 }
376
377
378
379
380
381
382
383
384 static int
385 read_result_reg(Byte *data_ptr)
386 {
387 int retry_count;
388 int read_status;
389
390 retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
391 while (jiffies < retry_count) {
392 if (((read_status = inb(read_status_reg)) & SONY535_RESULT_NOT_READY_BIT) == 0) {
393 #if DEBUG > 1
394 printk(CDU535_MESSAGE_NAME
395 ": read_result_reg(): readStatReg = 0x%x\n", read_status);
396 #endif
397 *data_ptr = inb(result_reg);
398 return 0;
399 } else {
400 sony_sleep();
401 }
402 }
403 printk(CDU535_MESSAGE_NAME " read_result_reg: TIME OUT!\n");
404 return TIME_OUT;
405 }
406
407
408
409
410
411
412
413
414 static int
415 read_exec_status(Byte status[2])
416 {
417 status[1] = 0;
418 if (read_result_reg(&(status[0])) != 0)
419 return TIME_OUT;
420 if ((status[0] & 0x80) != 0) {
421 if (read_result_reg(&(status[1])) != 0)
422 return TIME_OUT;
423 }
424 #if DEBUG > 1
425 printk(CDU535_MESSAGE_NAME ": read_exec_status: read 0x%x 0x%x\n",
426 status[0], status[1]);
427 #endif
428 return 0;
429 }
430
431
432
433
434
435
436
437
438 static int
439 check_drive_status(void)
440 {
441 Byte status, e_status[2];
442 int CDD, ATN;
443 Byte cmd;
444
445 select_unit(0);
446 if (sony_audio_status == CDROM_AUDIO_PLAY) {
447 outb(SONY535_REQUEST_AUDIO_STATUS, command_reg);
448 if (read_result_reg(&status) == 0) {
449 switch (status) {
450 case 0x0:
451 break;
452 case 0x1:
453 break;
454 case 0x3:
455 case 0x5:
456 sony_audio_status = CDROM_AUDIO_COMPLETED;
457 read_subcode();
458 break;
459 case 0x4:
460 sony_audio_status = CDROM_AUDIO_ERROR;
461 break;
462 }
463 }
464 }
465
466 outb(SONY535_REQUEST_DRIVE_STATUS_2, command_reg);
467 if (read_result_reg(&status) != 0)
468 return TIME_OUT;
469
470 #if DEBUG > 1
471 printk(CDU535_MESSAGE_NAME ": check_drive_status() got 0x%x\n", status);
472 #endif
473
474 if (status == 0)
475 return 0;
476
477 ATN = status & 0xf;
478 CDD = (status >> 4) & 0xf;
479
480 switch (ATN) {
481 case 0x0:
482 break;
483 case SONY535_ATN_BUSY:
484 if (initialized)
485 printk(CDU535_MESSAGE_NAME " error: drive busy\n");
486 return CD_BUSY;
487 case SONY535_ATN_EJECT_IN_PROGRESS:
488 printk(CDU535_MESSAGE_NAME " error: eject in progress\n");
489 sony_audio_status = CDROM_AUDIO_INVALID;
490 return CD_BUSY;
491 case SONY535_ATN_RESET_OCCURRED:
492 case SONY535_ATN_DISC_CHANGED:
493 case SONY535_ATN_RESET_AND_DISC_CHANGED:
494 #if DEBUG > 0
495 printk(CDU535_MESSAGE_NAME " notice: reset occurred or disc changed\n");
496 #endif
497 sony_disc_changed = 1;
498 sony_toc_read = 0;
499 sony_audio_status = CDROM_AUDIO_NO_STATUS;
500 sony_first_block = -1;
501 sony_last_block = -1;
502 if (initialized) {
503 cmd = SONY535_SPIN_UP;
504 do_sony_cmd(&cmd, 1, e_status, NULL, 0, 0);
505 sony_get_toc();
506 }
507 return 0;
508 default:
509 printk(CDU535_MESSAGE_NAME " error: drive busy (ATN=0x%x)\n", ATN);
510 return CD_BUSY;
511 }
512 switch (CDD) {
513 case 0x0:
514 case 0x2:
515 case 0xa:
516 break;
517 case 0xc:
518 printk(CDU535_MESSAGE_NAME
519 ": check_drive_status(): CDD = 0xc! Not properly handled!\n");
520 return CD_BUSY;
521 default:
522 return CD_BUSY;
523 }
524 return 0;
525 }
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543 static int
544 do_sony_cmd(Byte * cmd, int n_cmd, Byte status[2],
545 Byte * response, int n_response, int ignore_status_bit7)
546 {
547 int i;
548
549
550 for (i = 0; i < n_cmd; i++)
551 outb(cmd[i], command_reg);
552
553
554 if (read_result_reg(status) != 0)
555 return TIME_OUT;
556 if (!ignore_status_bit7 && ((status[0] & 0x80) != 0)) {
557
558 if (read_result_reg(status + 1) != 0)
559 return TIME_OUT;
560 } else {
561 status[1] = 0;
562 }
563 #if DEBUG > 2
564 printk(CDU535_MESSAGE_NAME ": do_sony_cmd %x: %x %x\n",
565 *cmd, status[0], status[1]);
566 #endif
567
568
569 if ((status[0] & ((ignore_status_bit7 ? 0x7f : 0xff) & 0x8f)) != 0)
570 return 0;
571
572
573 for (i = 0; 0 < n_response; n_response--, i++)
574 if (read_result_reg(response + i) != 0)
575 return TIME_OUT;
576 return 0;
577 }
578
579
580
581
582
583
584
585 static int
586 set_drive_mode(int mode, Byte status[2])
587 {
588 Byte cmd_buff[2];
589 Byte ret_buff[1];
590
591 cmd_buff[0] = SONY535_SET_DRIVE_MODE;
592 cmd_buff[1] = mode;
593 return do_sony_cmd(cmd_buff, 2, status, ret_buff, 1, 1);
594 }
595
596
597
598
599
600
601
602
603
604
605
606
607
608 static int
609 seek_and_read_N_blocks(Byte params[], int n_blocks, Byte status[2],
610 Byte **buff, int buf_size)
611 {
612 const int block_size = 2048;
613 Byte cmd_buff[7];
614 int i;
615 int read_status;
616 int retry_count;
617 Byte *data_buff;
618 int sector_count = 0;
619
620 if (buf_size < ((long)block_size) * n_blocks)
621 return NO_ROOM;
622
623 set_drive_mode(SONY535_CDROM_DRIVE_MODE, status);
624
625
626 cmd_buff[0] = SONY535_SEEK_AND_READ_N_BLOCKS_1;
627 for (i = 0; i < 6; i++)
628 cmd_buff[i + 1] = params[i];
629 for (i = 0; i < 7; i++)
630 outb(cmd_buff[i], command_reg);
631
632
633 while (0 < n_blocks--) {
634
635 retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
636 while (jiffies < retry_count) {
637 read_status = inb(read_status_reg);
638 if ((read_status & SONY535_RESULT_NOT_READY_BIT) == 0) {
639 read_exec_status(status);
640 return BAD_STATUS;
641 }
642 if ((read_status & SONY535_DATA_NOT_READY_BIT) == 0) {
643
644 data_buff = buff[sector_count++];
645 for (i = 0; i < block_size; i++)
646 *data_buff++ = inb(data_reg);
647 break;
648 }
649 sony_sleep();
650 }
651 if (retry_count <= jiffies)
652 return TIME_OUT;
653 }
654
655
656 if ((i = read_exec_status(status)) != 0)
657 return i;
658 return block_size * sector_count;
659 }
660
661
662
663
664
665
666
667 static int
668 request_toc_data(Byte status[2], struct s535_sony_toc *toc)
669 {
670 int to_status;
671 int i, j, n_tracks, track_no;
672 int first_track_num, last_track_num;
673 Byte cmd_no = 0xb2;
674 Byte track_address_buffer[5];
675
676
677 if ((to_status = do_sony_cmd(&cmd_no, 1, status, (Byte *) toc, 15, 1)) != 0)
678 return to_status;
679
680
681 first_track_num = bcd_to_int(toc->first_track_num);
682 last_track_num = bcd_to_int(toc->last_track_num);
683 n_tracks = last_track_num - first_track_num + 1;
684
685
686 for (i = 0; i < n_tracks; i++) {
687
688 for (j = 0; j < 5; j++) {
689 if (read_result_reg(track_address_buffer + j) != 0)
690 return TIME_OUT;
691 if (j == 1)
692 track_no = bcd_to_int(track_address_buffer[j]);
693 }
694
695 memcpy(toc->tracks + i, track_address_buffer, 5);
696 }
697 return 0;
698 }
699
700
701
702
703
704
705 static int
706 spin_up_drive(Byte status[2])
707 {
708 Byte cmd;
709
710
711 cmd = SONY535_REQUEST_DRIVE_STATUS_1;
712 if (do_sony_cmd(&cmd, 1, status, NULL, 0, 0) != 0)
713 return TIME_OUT;
714 if ((status[0] & SONY535_STATUS1_NOT_SPINNING) == 0)
715 return 0;
716
717
718 cmd = SONY535_SPIN_UP;
719 return do_sony_cmd(&cmd, 1, status, NULL, 0, 0);
720 }
721
722
723
724
725 static inline unsigned int
726 int_to_bcd(unsigned int val)
727 {
728 int retval;
729
730 retval = (val / 10) << 4;
731 retval = retval | val % 10;
732 return retval;
733 }
734
735
736
737 static unsigned int
738 bcd_to_int(unsigned int bcd)
739 {
740 return (((bcd >> 4) & 0x0f) * 10) + (bcd & 0x0f);
741 }
742
743
744
745
746
747
748 static void
749 log_to_msf(unsigned int log, Byte *msf)
750 {
751 log = log + LOG_START_OFFSET;
752 msf[0] = int_to_bcd(log / 4500);
753 log = log % 4500;
754 msf[1] = int_to_bcd(log / 75);
755 msf[2] = int_to_bcd(log % 75);
756 }
757
758
759
760
761
762 static unsigned int
763 msf_to_log(Byte *msf)
764 {
765 unsigned int log;
766
767
768 log = bcd_to_int(msf[2]);
769 log += bcd_to_int(msf[1]) * 75;
770 log += bcd_to_int(msf[0]) * 4500;
771 log = log - LOG_START_OFFSET;
772
773 return log;
774 }
775
776
777
778
779
780
781 static void
782 size_to_buf(unsigned int size, Byte *buf)
783 {
784 buf[0] = size / 65536;
785 size = size % 65536;
786 buf[1] = size / 256;
787 buf[2] = size % 256;
788 }
789
790
791
792
793
794
795
796
797
798 static void
799 do_cdu535_request(void)
800 {
801 unsigned int dev;
802 unsigned int read_size;
803 int block;
804 int nsect;
805 int copyoff;
806 int spin_up_retry;
807 Byte params[10];
808 Byte status[2];
809 Byte cmd[2];
810
811 if (!sony_inuse) {
812 cdu_open(NULL, NULL);
813 }
814 while (1) {
815
816
817
818
819 if (!(CURRENT) || CURRENT->rq_status == RQ_INACTIVE) {
820 return;
821 }
822 INIT_REQUEST;
823 dev = MINOR(CURRENT->rq_dev);
824 block = CURRENT->sector;
825 nsect = CURRENT->nr_sectors;
826 if (dev != 0) {
827 end_request(0);
828 continue;
829 }
830 switch (CURRENT->cmd) {
831 case READ:
832
833
834
835
836
837 if (sony_toc->lead_out_start_lba <= (block / 4)) {
838 end_request(0);
839 return;
840 }
841 if (sony_toc->lead_out_start_lba <= ((block + nsect) / 4)) {
842 end_request(0);
843 return;
844 }
845 while (0 < nsect) {
846
847
848
849
850 if ((block < sony_first_block) || (sony_last_block < block)) {
851 sony_first_block = (block / 4) * 4;
852 log_to_msf(block / 4, params);
853
854
855
856
857
858 if (sony_toc->lead_out_start_lba <= ((block / 4) + sony_buffer_sectors)) {
859 sony_last_block = (sony_toc->lead_out_start_lba * 4) - 1;
860 read_size = sony_toc->lead_out_start_lba - (block / 4);
861 } else {
862 sony_last_block = sony_first_block + (sony_buffer_sectors * 4) - 1;
863 read_size = sony_buffer_sectors;
864 }
865 size_to_buf(read_size, ¶ms[3]);
866
867
868
869
870
871 for (spin_up_retry=0 ;; ++spin_up_retry) {
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891 int readStatus = seek_and_read_N_blocks(params, read_size,
892 status, sony_buffer, (read_size * 2048));
893 if (0 <= readStatus)
894 break;
895 if (readStatus == NO_ROOM || spin_up_retry == MAX_SPINUP_RETRY) {
896
897 if (readStatus == NO_ROOM)
898 printk(CDU535_MESSAGE_NAME " No room to read from CD\n");
899 else
900 printk(CDU535_MESSAGE_NAME " Read error: 0x%.2x\n",
901 status[0]);
902 sony_first_block = -1;
903 sony_last_block = -1;
904 end_request(0);
905 return;
906 }
907 if (readStatus == BAD_STATUS) {
908
909 current->state = TASK_INTERRUPTIBLE;
910 current->timeout = jiffies + RETRY_FOR_BAD_STATUS;
911 schedule();
912 }
913 #if DEBUG > 0
914 printk(CDU535_MESSAGE_NAME
915 " debug: calling spin up when reading data!\n");
916 #endif
917 cmd[0] = SONY535_SPIN_UP;
918 do_sony_cmd(cmd, 1, status, NULL, 0, 0);
919 }
920 }
921
922
923
924
925 copyoff = block - sony_first_block;
926 memcpy(CURRENT->buffer,
927 sony_buffer[copyoff / 4] + 512 * (copyoff % 4), 512);
928
929 block += 1;
930 nsect -= 1;
931 CURRENT->buffer += 512;
932 }
933
934 end_request(1);
935 break;
936
937 case WRITE:
938 end_request(0);
939 break;
940
941 default:
942 panic("Unknown SONY CD cmd");
943 }
944 }
945 }
946
947
948
949
950
951
952 static void
953 sony_get_toc(void)
954 {
955 Byte status[2];
956 if (!sony_toc_read) {
957
958 if (request_toc_data(status, sony_toc) < 0)
959 return;
960 sony_toc->lead_out_start_lba = msf_to_log(sony_toc->lead_out_start_msf);
961 sony_toc_read = 1;
962 }
963 }
964
965
966
967
968
969
970 static int
971 find_track(int track)
972 {
973 int i;
974 int num_tracks;
975
976
977 num_tracks = bcd_to_int(sony_toc->last_track_num) -
978 bcd_to_int(sony_toc->first_track_num) + 1;
979 for (i = 0; i < num_tracks; i++) {
980 if (sony_toc->tracks[i].track == track) {
981 return i;
982 }
983 }
984
985 return -1;
986 }
987
988
989
990
991 static int
992 read_subcode(void)
993 {
994 Byte cmd = SONY535_REQUEST_SUB_Q_DATA;
995 Byte status[2];
996 int dsc_status;
997
998 if (check_drive_status() != 0)
999 return -EIO;
1000
1001 if ((dsc_status = do_sony_cmd(&cmd, 1, status, (Byte *) last_sony_subcode,
1002 sizeof(struct s535_sony_subcode), 1)) != 0) {
1003 printk(CDU535_MESSAGE_NAME " error 0x%.2x, %d (read_subcode)\n",
1004 status[0], dsc_status);
1005 return -EIO;
1006 }
1007 return 0;
1008 }
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018 static int
1019 sony_get_subchnl_info(long arg)
1020 {
1021 struct cdrom_subchnl schi;
1022 int err;
1023
1024
1025 if (check_drive_status() != 0)
1026 return -EIO;
1027
1028 sony_get_toc();
1029 if (!sony_toc_read) {
1030 return -EIO;
1031 }
1032 err = verify_area(VERIFY_WRITE , (char *)arg, sizeof schi);
1033 if (err)
1034 return err;
1035
1036 memcpy_fromfs(&schi, (char *)arg, sizeof schi);
1037
1038 switch (sony_audio_status) {
1039 case CDROM_AUDIO_PLAY:
1040 if (read_subcode() < 0) {
1041 return -EIO;
1042 }
1043 break;
1044
1045 case CDROM_AUDIO_PAUSED:
1046 case CDROM_AUDIO_COMPLETED:
1047 break;
1048
1049 case CDROM_AUDIO_NO_STATUS:
1050 schi.cdsc_audiostatus = sony_audio_status;
1051 memcpy_tofs((char *)arg, &schi, sizeof schi);
1052 return 0;
1053 break;
1054
1055 case CDROM_AUDIO_INVALID:
1056 case CDROM_AUDIO_ERROR:
1057 default:
1058 return -EIO;
1059 }
1060
1061 schi.cdsc_audiostatus = sony_audio_status;
1062 schi.cdsc_adr = last_sony_subcode->address;
1063 schi.cdsc_ctrl = last_sony_subcode->control;
1064 schi.cdsc_trk = bcd_to_int(last_sony_subcode->track_num);
1065 schi.cdsc_ind = bcd_to_int(last_sony_subcode->index_num);
1066 if (schi.cdsc_format == CDROM_MSF) {
1067 schi.cdsc_absaddr.msf.minute = bcd_to_int(last_sony_subcode->abs_msf[0]);
1068 schi.cdsc_absaddr.msf.second = bcd_to_int(last_sony_subcode->abs_msf[1]);
1069 schi.cdsc_absaddr.msf.frame = bcd_to_int(last_sony_subcode->abs_msf[2]);
1070
1071 schi.cdsc_reladdr.msf.minute = bcd_to_int(last_sony_subcode->rel_msf[0]);
1072 schi.cdsc_reladdr.msf.second = bcd_to_int(last_sony_subcode->rel_msf[1]);
1073 schi.cdsc_reladdr.msf.frame = bcd_to_int(last_sony_subcode->rel_msf[2]);
1074 } else if (schi.cdsc_format == CDROM_LBA) {
1075 schi.cdsc_absaddr.lba = msf_to_log(last_sony_subcode->abs_msf);
1076 schi.cdsc_reladdr.lba = msf_to_log(last_sony_subcode->rel_msf);
1077 }
1078 memcpy_tofs((char *)arg, &schi, sizeof schi);
1079 return 0;
1080 }
1081
1082
1083
1084
1085
1086 static int
1087 cdu_ioctl(struct inode *inode,
1088 struct file *file,
1089 unsigned int cmd,
1090 unsigned long arg)
1091 {
1092 unsigned int dev;
1093 Byte status[2];
1094 Byte cmd_buff[10], params[10];
1095 int i;
1096 int dsc_status;
1097 int err;
1098
1099 if (!inode) {
1100 return -EINVAL;
1101 }
1102 dev = MINOR(inode->i_rdev) >> 6;
1103 if (dev != 0) {
1104 return -EINVAL;
1105 }
1106 if (check_drive_status() != 0)
1107 return -EIO;
1108
1109 switch (cmd) {
1110 case CDROMSTART:
1111 if (spin_up_drive(status) < 0) {
1112 printk(CDU535_MESSAGE_NAME " error 0x%.2x (CDROMSTART)\n",
1113 status[0]);
1114 return -EIO;
1115 }
1116 return 0;
1117 break;
1118
1119 case CDROMSTOP:
1120 cmd_buff[0] = SONY535_HOLD;
1121 do_sony_cmd(cmd_buff, 1, status, NULL, 0, 0);
1122
1123
1124
1125
1126
1127 sony_audio_status = CDROM_AUDIO_NO_STATUS;
1128 cmd_buff[0] = SONY535_SPIN_DOWN;
1129 dsc_status = do_sony_cmd(cmd_buff, 1, status, NULL, 0, 0);
1130 if (((dsc_status < 0) && (dsc_status != BAD_STATUS)) ||
1131 ((status[0] & ~(SONY535_STATUS1_NOT_SPINNING)) != 0)) {
1132 printk(CDU535_MESSAGE_NAME " error 0x%.2x (CDROMSTOP)\n",
1133 status[0]);
1134 return -EIO;
1135 }
1136 return 0;
1137 break;
1138
1139 case CDROMPAUSE:
1140 cmd_buff[0] = SONY535_HOLD;
1141 if (do_sony_cmd(cmd_buff, 1, status, NULL, 0, 0) != 0) {
1142 printk(CDU535_MESSAGE_NAME " error 0x%.2x (CDROMPAUSE)\n",
1143 status[0]);
1144 return -EIO;
1145 }
1146
1147 if (read_subcode() < 0) {
1148 return -EIO;
1149 }
1150 cur_pos_msf[0] = last_sony_subcode->abs_msf[0];
1151 cur_pos_msf[1] = last_sony_subcode->abs_msf[1];
1152 cur_pos_msf[2] = last_sony_subcode->abs_msf[2];
1153 sony_audio_status = CDROM_AUDIO_PAUSED;
1154 return 0;
1155 break;
1156
1157 case CDROMRESUME:
1158 set_drive_mode(SONY535_AUDIO_DRIVE_MODE, status);
1159
1160 if (sony_audio_status != CDROM_AUDIO_PAUSED) {
1161 return -EINVAL;
1162 }
1163 spin_up_drive(status);
1164
1165
1166 cmd_buff[0] = SONY535_PLAY_AUDIO;
1167 cmd_buff[1] = 0;
1168 cmd_buff[2] = cur_pos_msf[0];
1169 cmd_buff[3] = cur_pos_msf[1];
1170 cmd_buff[4] = cur_pos_msf[2];
1171 cmd_buff[5] = SONY535_PLAY_AUDIO;
1172 cmd_buff[6] = 2;
1173 cmd_buff[7] = final_pos_msf[0];
1174 cmd_buff[8] = final_pos_msf[1];
1175 cmd_buff[9] = final_pos_msf[2];
1176 if ((do_sony_cmd(cmd_buff, 5, status, NULL, 0, 0) != 0) ||
1177 (do_sony_cmd(cmd_buff + 5, 5, status, NULL, 0, 0) != 0)) {
1178 printk(CDU535_MESSAGE_NAME " error 0x%.2x (CDROMRESUME)\n",
1179 status[0]);
1180 return -EIO;
1181 }
1182 sony_audio_status = CDROM_AUDIO_PLAY;
1183 return 0;
1184 break;
1185
1186 case CDROMPLAYMSF:
1187 err = verify_area(VERIFY_READ, (char *)arg, 6);
1188 if (err)
1189 return err;
1190 spin_up_drive(status);
1191 set_drive_mode(SONY535_AUDIO_DRIVE_MODE, status);
1192 memcpy_fromfs(params, (void *)arg, 6);
1193
1194
1195 for (i = 0; i < 3; i++) {
1196 cmd_buff[2 + i] = int_to_bcd(params[i]);
1197 cmd_buff[7 + i] = int_to_bcd(params[i + 3]);
1198 }
1199 cmd_buff[0] = SONY535_PLAY_AUDIO;
1200 cmd_buff[1] = 0;
1201
1202 cmd_buff[5] = SONY535_PLAY_AUDIO;
1203 cmd_buff[6] = 2;
1204
1205 if ((do_sony_cmd(cmd_buff, 5, status, NULL, 0, 0) != 0) ||
1206 (do_sony_cmd(cmd_buff + 5, 5, status, NULL, 0, 0) != 0)) {
1207 printk(CDU535_MESSAGE_NAME " error 0x%.2x (CDROMPLAYMSF)\n",
1208 status[0]);
1209 return -EIO;
1210 }
1211
1212 final_pos_msf[0] = cmd_buff[7];
1213 final_pos_msf[1] = cmd_buff[8];
1214 final_pos_msf[2] = cmd_buff[9];
1215 sony_audio_status = CDROM_AUDIO_PLAY;
1216 return 0;
1217 break;
1218
1219 case CDROMREADTOCHDR:
1220 {
1221 struct cdrom_tochdr *hdr;
1222 struct cdrom_tochdr loc_hdr;
1223
1224 sony_get_toc();
1225 if (!sony_toc_read)
1226 return -EIO;
1227 hdr = (struct cdrom_tochdr *)arg;
1228 err = verify_area(VERIFY_WRITE, hdr, sizeof *hdr);
1229 if (err)
1230 return err;
1231 loc_hdr.cdth_trk0 = bcd_to_int(sony_toc->first_track_num);
1232 loc_hdr.cdth_trk1 = bcd_to_int(sony_toc->last_track_num);
1233 memcpy_tofs(hdr, &loc_hdr, sizeof *hdr);
1234 }
1235 return 0;
1236 break;
1237
1238 case CDROMREADTOCENTRY:
1239 {
1240 struct cdrom_tocentry *entry;
1241 struct cdrom_tocentry loc_entry;
1242 int track_idx;
1243 Byte *msf_val = NULL;
1244
1245 sony_get_toc();
1246 if (!sony_toc_read) {
1247 return -EIO;
1248 }
1249 entry = (struct cdrom_tocentry *)arg;
1250 err = verify_area(VERIFY_WRITE , entry, sizeof *entry);
1251 if (err)
1252 return err;
1253
1254 memcpy_fromfs(&loc_entry, entry, sizeof loc_entry);
1255
1256
1257 if (loc_entry.cdte_track == CDROM_LEADOUT) {
1258 loc_entry.cdte_adr = 0 ;
1259 loc_entry.cdte_ctrl = sony_toc->control2;
1260 msf_val = sony_toc->lead_out_start_msf;
1261 } else {
1262 track_idx = find_track(int_to_bcd(loc_entry.cdte_track));
1263 if (track_idx < 0)
1264 return -EINVAL;
1265 loc_entry.cdte_adr = 0 ;
1266 loc_entry.cdte_ctrl = sony_toc->tracks[track_idx].control;
1267 msf_val = sony_toc->tracks[track_idx].track_start_msf;
1268 }
1269
1270
1271 if (loc_entry.cdte_format == CDROM_LBA) {
1272 loc_entry.cdte_addr.lba = msf_to_log(msf_val);
1273 } else if (loc_entry.cdte_format == CDROM_MSF) {
1274 loc_entry.cdte_addr.msf.minute = bcd_to_int(*msf_val);
1275 loc_entry.cdte_addr.msf.second = bcd_to_int(*(msf_val + 1));
1276 loc_entry.cdte_addr.msf.frame = bcd_to_int(*(msf_val + 2));
1277 }
1278 memcpy_tofs(entry, &loc_entry, sizeof *entry);
1279 }
1280 return 0;
1281 break;
1282
1283 case CDROMPLAYTRKIND:
1284 {
1285 struct cdrom_ti ti;
1286 int track_idx;
1287
1288 sony_get_toc();
1289 if (!sony_toc_read)
1290 return -EIO;
1291 err = verify_area(VERIFY_READ, (char *)arg, sizeof ti);
1292 if (err)
1293 return err;
1294
1295 memcpy_fromfs(&ti, (char *)arg, sizeof ti);
1296 if ((ti.cdti_trk0 < sony_toc->first_track_num)
1297 || (sony_toc->last_track_num < ti.cdti_trk0)
1298 || (ti.cdti_trk1 < ti.cdti_trk0)) {
1299 return -EINVAL;
1300 }
1301 track_idx = find_track(int_to_bcd(ti.cdti_trk0));
1302 if (track_idx < 0)
1303 return -EINVAL;
1304 params[1] = sony_toc->tracks[track_idx].track_start_msf[0];
1305 params[2] = sony_toc->tracks[track_idx].track_start_msf[1];
1306 params[3] = sony_toc->tracks[track_idx].track_start_msf[2];
1307
1308
1309
1310
1311 if (bcd_to_int(sony_toc->last_track_num) <= ti.cdti_trk1) {
1312 log_to_msf(msf_to_log(sony_toc->lead_out_start_msf) - 1,
1313 &(params[4]));
1314 } else {
1315 track_idx = find_track(int_to_bcd(ti.cdti_trk1 + 1));
1316 if (track_idx < 0)
1317 return -EINVAL;
1318 log_to_msf(msf_to_log(sony_toc->tracks[track_idx].track_start_msf) - 1,
1319 &(params[4]));
1320 }
1321 params[0] = 0x03;
1322
1323 spin_up_drive(status);
1324
1325 set_drive_mode(SONY535_AUDIO_DRIVE_MODE, status);
1326
1327
1328 cmd_buff[0] = SONY535_PLAY_AUDIO;
1329 cmd_buff[1] = 0;
1330 cmd_buff[2] = params[1];
1331 cmd_buff[3] = params[2];
1332 cmd_buff[4] = params[3];
1333 cmd_buff[5] = SONY535_PLAY_AUDIO;
1334 cmd_buff[6] = 2;
1335 cmd_buff[7] = params[4];
1336 cmd_buff[8] = params[5];
1337 cmd_buff[9] = params[6];
1338 if ((do_sony_cmd(cmd_buff, 5, status, NULL, 0, 0) != 0) ||
1339 (do_sony_cmd(cmd_buff + 5, 5, status, NULL, 0, 0) != 0)) {
1340 printk(CDU535_MESSAGE_NAME " error 0x%.2x (CDROMPLAYTRKIND)\n",
1341 status[0]);
1342 printk("... Params: %x %x %x %x %x %x %x\n",
1343 params[0], params[1], params[2],
1344 params[3], params[4], params[5], params[6]);
1345 return -EIO;
1346 }
1347
1348 final_pos_msf[0] = params[4];
1349 final_pos_msf[1] = params[5];
1350 final_pos_msf[2] = params[6];
1351 sony_audio_status = CDROM_AUDIO_PLAY;
1352 return 0;
1353 }
1354
1355 case CDROMSUBCHNL:
1356 return sony_get_subchnl_info(arg);
1357
1358 case CDROMVOLCTRL:
1359 {
1360 struct cdrom_volctrl volctrl;
1361
1362 err = verify_area(VERIFY_READ, (char *)arg, sizeof volctrl);
1363 if (err)
1364 return err;
1365
1366 memcpy_fromfs(&volctrl, (char *)arg, sizeof volctrl);
1367 cmd_buff[0] = SONY535_SET_VOLUME;
1368 cmd_buff[1] = volctrl.channel0;
1369 cmd_buff[2] = volctrl.channel1;
1370 if (do_sony_cmd(cmd_buff, 3, status, NULL, 0, 0) != 0) {
1371 printk(CDU535_MESSAGE_NAME " error 0x%.2x (CDROMVOLCTRL)\n",
1372 status[0]);
1373 return -EIO;
1374 }
1375 }
1376 return 0;
1377
1378 case CDROMEJECT:
1379 cmd_buff[0] = SONY535_STOP;
1380 do_sony_cmd(cmd_buff, 1, status, NULL, 0, 0);
1381 cmd_buff[0] = SONY535_SPIN_DOWN;
1382 do_sony_cmd(cmd_buff, 1, status, NULL, 0, 0);
1383
1384 sony_audio_status = CDROM_AUDIO_INVALID;
1385 cmd_buff[0] = SONY535_EJECT_CADDY;
1386 if (do_sony_cmd(cmd_buff, 1, status, NULL, 0, 0) != 0) {
1387 printk(CDU535_MESSAGE_NAME " error 0x%.2x (CDROMEJECT)\n",
1388 status[0]);
1389 return -EIO;
1390 }
1391 return 0;
1392 break;
1393
1394 default:
1395 return -EINVAL;
1396 }
1397 }
1398
1399
1400
1401
1402
1403
1404 static int
1405 cdu_open(struct inode *inode,
1406 struct file *filp)
1407 {
1408 Byte status[2], cmd_buff[2];
1409
1410
1411 if (sony_inuse)
1412 return -EBUSY;
1413 if (check_drive_status() != 0)
1414 return -EIO;
1415 sony_inuse = 1;
1416 MOD_INC_USE_COUNT;
1417
1418 if (spin_up_drive(status) != 0) {
1419 printk(CDU535_MESSAGE_NAME " error 0x%.2x (cdu_open, spin up)\n",
1420 status[0]);
1421 sony_inuse = 0;
1422 MOD_DEC_USE_COUNT;
1423 return -EIO;
1424 }
1425 sony_get_toc();
1426 if (!sony_toc_read) {
1427 cmd_buff[0] = SONY535_SPIN_DOWN;
1428 do_sony_cmd(cmd_buff, 1, status, NULL, 0, 0);
1429 sony_inuse = 0;
1430 MOD_DEC_USE_COUNT;
1431 return -EIO;
1432 }
1433 if (inode) {
1434 check_disk_change(inode->i_rdev);
1435 }
1436 sony_usage++;
1437
1438 #ifdef LOCK_DOORS
1439
1440 cmd_buff[0] = SONY535_DISABLE_EJECT_BUTTON;
1441 do_sony_cmd(cmd_buff, 1, status, NULL, 0, 0);
1442 #endif
1443
1444 return 0;
1445 }
1446
1447
1448
1449
1450
1451
1452 static void
1453 cdu_release(struct inode *inode,
1454 struct file *filp)
1455 {
1456 Byte status[2], cmd_no;
1457
1458 sony_inuse = 0;
1459 MOD_DEC_USE_COUNT;
1460
1461 if (0 < sony_usage) {
1462 sony_usage--;
1463 }
1464 if (sony_usage == 0) {
1465 sync_dev(inode->i_rdev);
1466 check_drive_status();
1467
1468 if (sony_audio_status != CDROM_AUDIO_PLAY) {
1469 cmd_no = SONY535_SPIN_DOWN;
1470 do_sony_cmd(&cmd_no, 1, status, NULL, 0, 0);
1471 }
1472 #ifdef LOCK_DOORS
1473
1474 cmd_no = SONY535_ENABLE_EJECT_BUTTON;
1475 do_sony_cmd(&cmd_no, 1, status, NULL, 0, 0);
1476 #endif
1477 }
1478 }
1479
1480
1481 static struct file_operations cdu_fops =
1482 {
1483 NULL,
1484 block_read,
1485 block_write,
1486 NULL,
1487 NULL,
1488 cdu_ioctl,
1489 NULL,
1490 cdu_open,
1491 cdu_release,
1492 NULL,
1493 NULL,
1494 cdu535_check_media_change,
1495 NULL
1496 };
1497
1498
1499
1500
1501 int
1502 sony535_init(void)
1503 {
1504 struct s535_sony_drive_config drive_config;
1505 Byte cmd_buff[3];
1506 Byte ret_buff[2];
1507 Byte status[2];
1508 int retry_count;
1509 int tmp_irq;
1510 int i;
1511
1512
1513 if ((sony535_cd_base_io == 0xffff)||(sony535_cd_base_io == 0))
1514 return 0;
1515
1516
1517 result_reg = sony535_cd_base_io;
1518 command_reg = sony535_cd_base_io;
1519 data_reg = sony535_cd_base_io + 1;
1520 read_status_reg = sony535_cd_base_io + 2;
1521 select_unit_reg = sony535_cd_base_io + 3;
1522
1523 #ifndef USE_IRQ
1524 sony535_irq_used = 0;
1525 #endif
1526
1527 tmp_irq = sony535_irq_used;
1528 sony535_irq_used = 0;
1529
1530 #if DEBUG > 0
1531 printk(CDU535_MESSAGE_NAME ": probing base address %03X\n",
1532 sony535_cd_base_io);
1533 #endif
1534 if (check_region(sony535_cd_base_io,4)) {
1535 printk(CDU535_MESSAGE_NAME ": my base address is not free!\n");
1536 return -EIO;
1537 }
1538
1539 inb(select_unit_reg);
1540 retry_count = jiffies + 2 * HZ;
1541 while (jiffies < retry_count)
1542 sony_sleep();
1543 inb(result_reg);
1544
1545 outb(0, read_status_reg);
1546 retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
1547 while (jiffies < retry_count) {
1548 select_unit(0);
1549 if (inb(result_reg) != 0xff)
1550 break;
1551 sony_sleep();
1552 }
1553
1554 if ((jiffies < retry_count) && (check_drive_status() != TIME_OUT)) {
1555
1556 cmd_buff[0] = SONY535_INQUIRY;
1557 if (do_sony_cmd(cmd_buff, 1, status,
1558 (Byte *)&drive_config, 28, 1) == 0) {
1559
1560
1561
1562 #if DEBUG > 0
1563
1564 if ( (status[0] & 0x7f) != 0 && (status[0] & 0x7f) != 0x50 )
1565 printk(CDU535_MESSAGE_NAME
1566 "Inquiry command returned status = 0x%x\n", status[0]);
1567 #endif
1568
1569 sony535_irq_used = tmp_irq;
1570 #ifndef MODULE
1571
1572
1573
1574
1575 if (sony535_irq_used < 0) {
1576 autoirq_setup(0);
1577 enable_interrupts();
1578 outb(0, read_status_reg);
1579 sony535_irq_used = autoirq_report(10);
1580 disable_interrupts();
1581 }
1582 #endif
1583 if (sony535_irq_used > 0) {
1584 if (request_irq(sony535_irq_used, cdu535_interrupt,
1585 SA_INTERRUPT, CDU535_HANDLE)) {
1586 printk("Unable to grab IRQ%d for the " CDU535_MESSAGE_NAME
1587 " driver; polling instead.\n", sony535_irq_used);
1588 sony535_irq_used = 0;
1589 }
1590 }
1591 cmd_buff[0] = SONY535_SET_DRIVE_MODE;
1592 cmd_buff[1] = 0x0;
1593 if (do_sony_cmd(cmd_buff, 2, status, ret_buff, 1, 1) == 0) {
1594
1595 sony_buffer_size = SONY535_BUFFER_SIZE;
1596 sony_buffer_sectors = sony_buffer_size / 2048;
1597
1598 printk(CDU535_MESSAGE_NAME " I/F CDROM : %8.8s %16.16s %4.4s",
1599 drive_config.vendor_id,
1600 drive_config.product_id,
1601 drive_config.product_rev_level);
1602 printk(" base address %03X, ", sony535_cd_base_io);
1603 if (tmp_irq > 0)
1604 printk("IRQ%d, ", tmp_irq);
1605 printk("using %d byte buffer\n", sony_buffer_size);
1606
1607 if (register_blkdev(MAJOR_NR, CDU535_HANDLE, &cdu_fops)) {
1608 printk("Unable to get major %d for %s\n",
1609 MAJOR_NR, CDU535_MESSAGE_NAME);
1610 return -EIO;
1611 }
1612 blk_dev[MAJOR_NR].request_fn = DEVICE_REQUEST;
1613 read_ahead[MAJOR_NR] = 8;
1614
1615 sony_toc = (struct s535_sony_toc *)
1616 kmalloc(sizeof *sony_toc, GFP_KERNEL);
1617 if (sony_toc == NULL)
1618 return -ENOMEM;
1619 last_sony_subcode = (struct s535_sony_subcode *)
1620 kmalloc(sizeof *last_sony_subcode, GFP_KERNEL);
1621 if (last_sony_subcode == NULL) {
1622 kfree(sony_toc);
1623 return -ENOMEM;
1624 }
1625 sony_buffer = (Byte **)
1626 kmalloc(4 * sony_buffer_sectors, GFP_KERNEL);
1627 if (sony_buffer == NULL) {
1628 kfree(sony_toc);
1629 kfree(last_sony_subcode);
1630 return -ENOMEM;
1631 }
1632 for (i = 0; i < sony_buffer_sectors; i++) {
1633 sony_buffer[i] = (Byte *)kmalloc(2048, GFP_KERNEL);
1634 if (sony_buffer[i] == NULL) {
1635 while (--i>=0)
1636 kfree(sony_buffer[i]);
1637 kfree(sony_buffer);
1638 kfree(sony_toc);
1639 kfree(last_sony_subcode);
1640 return -ENOMEM;
1641 }
1642 }
1643 initialized = 1;
1644 }
1645 }
1646 }
1647
1648 if (!initialized) {
1649 printk("Did not find a " CDU535_MESSAGE_NAME " drive\n");
1650 return -EIO;
1651 }
1652 request_region(sony535_cd_base_io, 4, CDU535_HANDLE);
1653 return 0;
1654 }
1655
1656 #ifndef MODULE
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666 void
1667 sonycd535_setup(char *strings, int *ints)
1668 {
1669
1670
1671
1672 if (ints[0] > 0)
1673 if (ints[0] != 0)
1674 sony535_cd_base_io = ints[1];
1675 if (ints[0] > 1)
1676 sony535_irq_used = ints[2];
1677 if ((strings != NULL) && (*strings != '\0'))
1678 printk(CDU535_MESSAGE_NAME
1679 ": Warning: Unknown interface type: %s\n", strings);
1680 }
1681
1682 #else
1683
1684 void
1685 cleanup_module(void)
1686 {
1687 int i;
1688 if (MOD_IN_USE) {
1689 printk(CDU535_HANDLE " module in use, cannot remove\n");
1690 return;
1691 }
1692 release_region(sony535_cd_base_io, 4);
1693 for (i = 0; i < sony_buffer_sectors; i++)
1694 kfree_s(sony_buffer[i], 2048);
1695 kfree_s(sony_buffer, 4 * sony_buffer_sectors);
1696 kfree_s(last_sony_subcode, sizeof *last_sony_subcode);
1697 kfree_s(sony_toc, sizeof *sony_toc);
1698 if (unregister_blkdev(MAJOR_NR, CDU535_HANDLE) == -EINVAL)
1699 printk("Uh oh, couldn't unregister " CDU535_HANDLE "\n");
1700 else
1701 printk(CDU535_HANDLE " module released\n");
1702 }
1703 #endif