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