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