This source file includes following definitions.
- scd_disk_change
- enable_interrupts
- disable_interrupts
- sony_sleep
- is_attention
- is_busy
- is_data_ready
- is_data_requested
- is_result_ready
- is_param_write_rdy
- is_result_reg_not_empty
- reset_drive
- clear_attention
- clear_result_ready
- clear_data_ready
- clear_param_reg
- read_status_register
- read_result_register
- read_data_register
- write_param
- write_cmd
- cdu31a_interrupt
- set_drive_params
- restart_on_error
- write_params
- get_result
- do_sony_cd_cmd
- handle_sony_cd_attention
- int_to_bcd
- bcd_to_int
- log_to_msf
- msf_to_log
- size_to_buf
- start_request
- abort_read
- handle_abort_timeout
- input_data
- read_data_block
- do_cdu31a_request
- mcovlp
- sony_get_toc
- find_track
- read_subcode
- sony_get_subchnl_info
- read_audio_data
- read_audio
- do_sony_cd_cmd_chk
- scd_ioctl
- scd_open
- scd_release
- get_drive_configuration
- cdu31a_setup
- cdu31a_init
- 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172 #include <linux/major.h>
173
174 #include <linux/module.h>
175
176 #include <linux/errno.h>
177 #include <linux/signal.h>
178 #include <linux/sched.h>
179 #include <linux/timer.h>
180 #include <linux/fs.h>
181 #include <linux/kernel.h>
182 #include <linux/hdreg.h>
183 #include <linux/genhd.h>
184 #include <linux/ioport.h>
185 #include <linux/string.h>
186 #include <linux/malloc.h>
187
188 #include <asm/system.h>
189 #include <asm/io.h>
190 #include <asm/segment.h>
191 #include <asm/dma.h>
192
193 #include <linux/cdrom.h>
194 #include <linux/cdu31a.h>
195
196 #define MAJOR_NR CDU31A_CDROM_MAJOR
197 #include <linux/blk.h>
198
199 #define DEBUG 0
200
201 #define CDU31A_READAHEAD 128
202 #define CDU31A_MAX_CONSECUTIVE_ATTENTIONS 10
203
204
205 #undef SONY_POLL_EACH_BYTE
206
207
208
209
210
211
212 static struct
213 {
214 unsigned short base;
215 short int_num;
216
217 } cdu31a_addresses[] =
218 {
219 #if 0
220
221 { 0x340, 0 },
222 { 0x1f88, 0 },
223 { 0x230, 0 },
224 { 0x360, 0 },
225 { 0x320, 0 },
226 { 0x330, 0 },
227 { 0x634, 0 },
228 { 0x654, 0 },
229 #endif
230 { 0 }
231 };
232
233 static int handle_sony_cd_attention(void);
234 static int read_subcode(void);
235 static void sony_get_toc(void);
236 static int scd_open(struct inode *inode, struct file *filp);
237 static void do_sony_cd_cmd(unsigned char cmd,
238 unsigned char *params,
239 unsigned int num_params,
240 unsigned char *result_buffer,
241 unsigned int *result_size);
242 static void size_to_buf(unsigned int size,
243 unsigned char *buf);
244
245
246 static unsigned int sony_next_block;
247 static unsigned int sony_blocks_left = 0;
248
249
250
251
252
253 static unsigned int cdu31a_port = 0;
254
255
256
257
258
259 static volatile unsigned short sony_cd_cmd_reg;
260 static volatile unsigned short sony_cd_param_reg;
261 static volatile unsigned short sony_cd_write_reg;
262 static volatile unsigned short sony_cd_control_reg;
263 static volatile unsigned short sony_cd_status_reg;
264 static volatile unsigned short sony_cd_result_reg;
265 static volatile unsigned short sony_cd_read_reg;
266 static volatile unsigned short sony_cd_fifost_reg;
267
268
269 static int sony_spun_up = 0;
270
271 static int sony_xa_mode = 0;
272
273
274 static int sony_raw_data_mode = 1;
275
276
277 static unsigned int sony_usage = 0;
278
279
280 static int sony_pas_init = 0;
281
282
283 static struct s_sony_session_toc sony_toc;
284
285
286
287 static int sony_toc_read = 0;
288
289
290 static struct s_sony_subcode last_sony_subcode;
291
292
293 static volatile int sony_inuse = 0;
294
295
296 static struct wait_queue * sony_wait = NULL;
297
298 static struct task_struct *has_cd_task = NULL;
299
300
301
302 static int is_double_speed = 0;
303
304 static int is_auto_eject = 1;
305
306
307
308
309
310 static volatile int sony_audio_status = CDROM_AUDIO_NO_STATUS;
311
312
313
314
315
316
317
318
319 static unsigned volatile char cur_pos_msf[3] = { 0, 0, 0 };
320 static unsigned volatile char final_pos_msf[3] = { 0, 0, 0 };
321
322
323 static int cdu31a_irq = 0;
324
325
326
327 static struct wait_queue *cdu31a_irq_wait = NULL;
328
329 static int curr_control_reg = 0;
330
331
332
333
334 static char disk_changed;
335
336
337
338
339 static char readahead_buffer[CD_FRAMESIZE_RAW];
340 static int readahead_dataleft = 0;
341 static int readahead_bad = 0;
342
343
344
345
346 static struct timer_list cdu31a_abort_timer;
347
348
349
350
351 static int abort_read_started = 0;
352
353
354
355
356
357
358 static int
359 scd_disk_change(kdev_t full_dev)
360 {
361 int retval;
362
363 retval = disk_changed;
364 disk_changed = 0;
365
366 return retval;
367 }
368
369 static inline void
370 enable_interrupts(void)
371 {
372 curr_control_reg |= ( SONY_ATTN_INT_EN_BIT
373 | SONY_RES_RDY_INT_EN_BIT
374 | SONY_DATA_RDY_INT_EN_BIT);
375 outb(curr_control_reg, sony_cd_control_reg);
376 }
377
378 static inline void
379 disable_interrupts(void)
380 {
381 curr_control_reg &= ~( SONY_ATTN_INT_EN_BIT
382 | SONY_RES_RDY_INT_EN_BIT
383 | SONY_DATA_RDY_INT_EN_BIT);
384 outb(curr_control_reg, sony_cd_control_reg);
385 }
386
387
388
389
390
391 static inline void
392 sony_sleep(void)
393 {
394 unsigned long flags;
395
396 if (cdu31a_irq <= 0)
397 {
398 current->state = TASK_INTERRUPTIBLE;
399 current->timeout = jiffies;
400 schedule();
401 }
402 else
403 {
404 save_flags(flags);
405 cli();
406 enable_interrupts();
407 interruptible_sleep_on(&cdu31a_irq_wait);
408 restore_flags(flags);
409 }
410 }
411
412
413
414
415
416
417 static inline int
418 is_attention(void)
419 {
420 return((inb(sony_cd_status_reg) & SONY_ATTN_BIT) != 0);
421 }
422
423 static inline int
424 is_busy(void)
425 {
426 return((inb(sony_cd_status_reg) & SONY_BUSY_BIT) != 0);
427 }
428
429 static inline int
430 is_data_ready(void)
431 {
432 return((inb(sony_cd_status_reg) & SONY_DATA_RDY_BIT) != 0);
433 }
434
435 static inline int
436 is_data_requested(void)
437 {
438 return((inb(sony_cd_status_reg) & SONY_DATA_REQUEST_BIT) != 0);
439 }
440
441 static inline int
442 is_result_ready(void)
443 {
444 return((inb(sony_cd_status_reg) & SONY_RES_RDY_BIT) != 0);
445 }
446
447 static inline int
448 is_param_write_rdy(void)
449 {
450 return((inb(sony_cd_fifost_reg) & SONY_PARAM_WRITE_RDY_BIT) != 0);
451 }
452
453 static inline int
454 is_result_reg_not_empty(void)
455 {
456 return((inb(sony_cd_fifost_reg) & SONY_RES_REG_NOT_EMP_BIT) != 0);
457 }
458
459 static inline void
460 reset_drive(void)
461 {
462 curr_control_reg = 0;
463 readahead_dataleft = 0;
464 sony_toc_read = 0;
465 outb(SONY_DRIVE_RESET_BIT, sony_cd_control_reg);
466 }
467
468 static inline void
469 clear_attention(void)
470 {
471 outb(curr_control_reg | SONY_ATTN_CLR_BIT, sony_cd_control_reg);
472 }
473
474 static inline void
475 clear_result_ready(void)
476 {
477 outb(curr_control_reg | SONY_RES_RDY_CLR_BIT, sony_cd_control_reg);
478 }
479
480 static inline void
481 clear_data_ready(void)
482 {
483 outb(curr_control_reg | SONY_DATA_RDY_CLR_BIT, sony_cd_control_reg);
484 }
485
486 static inline void
487 clear_param_reg(void)
488 {
489 outb(curr_control_reg | SONY_PARAM_CLR_BIT, sony_cd_control_reg);
490 }
491
492 static inline unsigned char
493 read_status_register(void)
494 {
495 return(inb(sony_cd_status_reg));
496 }
497
498 static inline unsigned char
499 read_result_register(void)
500 {
501 return(inb(sony_cd_result_reg));
502 }
503
504 static inline unsigned char
505 read_data_register(void)
506 {
507 return(inb(sony_cd_read_reg));
508 }
509
510 static inline void
511 write_param(unsigned char param)
512 {
513 outb(param, sony_cd_param_reg);
514 }
515
516 static inline void
517 write_cmd(unsigned char cmd)
518 {
519 outb(curr_control_reg | SONY_RES_RDY_INT_EN_BIT, sony_cd_control_reg);
520 outb(cmd, sony_cd_cmd_reg);
521 }
522
523 static void
524 cdu31a_interrupt(int irq, void *dev_id, struct pt_regs *regs)
525 {
526 unsigned char val;
527
528 if (abort_read_started)
529 {
530
531
532
533
534 while (is_result_reg_not_empty())
535 {
536 val = read_result_register();
537 }
538 clear_data_ready();
539 clear_result_ready();
540
541
542 while (is_data_requested())
543 {
544 val = read_data_register();
545 }
546 abort_read_started = 0;
547
548
549 if (cdu31a_irq_wait != NULL)
550 {
551 disable_interrupts();
552 wake_up(&cdu31a_irq_wait);
553 }
554 }
555 else if (cdu31a_irq_wait != NULL)
556 {
557 disable_interrupts();
558 wake_up(&cdu31a_irq_wait);
559 }
560 else
561 {
562 disable_interrupts();
563 printk("CDU31A: Got an interrupt but nothing was waiting\n");
564 }
565 }
566
567
568
569
570
571 static void
572 set_drive_params(void)
573 {
574 unsigned char res_reg[12];
575 unsigned int res_size;
576 unsigned char params[3];
577
578
579 params[0] = SONY_SD_AUTO_SPIN_DOWN_TIME;
580 params[1] = 0x00;
581 do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
582 params,
583 2,
584 res_reg,
585 &res_size);
586 if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20))
587 {
588 printk(" Unable to set spin-down time: 0x%2.2x\n", res_reg[1]);
589 }
590
591 params[0] = SONY_SD_MECH_CONTROL;
592 params[1] = SONY_AUTO_SPIN_UP_BIT;
593
594 if (is_auto_eject) params[1] |= SONY_AUTO_EJECT_BIT;
595
596 if (is_double_speed)
597 {
598 params[1] |= SONY_DOUBLE_SPEED_BIT;
599
600 }
601 do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
602 params,
603 2,
604 res_reg,
605 &res_size);
606 if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20))
607 {
608 printk(" Unable to set mechanical parameters: 0x%2.2x\n", res_reg[1]);
609 }
610 }
611
612
613
614
615 static void
616 restart_on_error(void)
617 {
618 unsigned char res_reg[12];
619 unsigned int res_size;
620 unsigned int retry_count;
621
622
623 printk("cdu31a: Resetting drive on error\n");
624 reset_drive();
625 retry_count = jiffies + SONY_RESET_TIMEOUT;
626 while ((retry_count > jiffies) && (!is_attention()))
627 {
628 sony_sleep();
629 }
630 set_drive_params();
631 do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg, &res_size);
632 if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20))
633 {
634 printk("cdu31a: Unable to spin up drive: 0x%2.2x\n", res_reg[1]);
635 }
636
637 current->state = TASK_INTERRUPTIBLE;
638 current->timeout = jiffies + 2*HZ;
639 schedule();
640
641 sony_get_toc();
642 }
643
644
645
646
647
648 static int
649 write_params(unsigned char *params,
650 int num_params)
651 {
652 unsigned int retry_count;
653
654
655 retry_count = SONY_READY_RETRIES;
656 while ((retry_count > 0) && (!is_param_write_rdy()))
657 {
658 retry_count--;
659 }
660 if (!is_param_write_rdy())
661 {
662 return -EIO;
663 }
664
665 while (num_params > 0)
666 {
667 write_param(*params);
668 params++;
669 num_params--;
670 }
671
672 return 0;
673 }
674
675
676
677
678
679
680
681
682 static void
683 get_result(unsigned char *result_buffer,
684 unsigned int *result_size)
685 {
686 unsigned char a, b;
687 int i;
688 unsigned int retry_count;
689
690
691 while (handle_sony_cd_attention())
692 ;
693
694 retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
695 while ((retry_count > jiffies) && (is_busy() || (!(is_result_ready()))))
696 {
697 sony_sleep();
698
699 while (handle_sony_cd_attention())
700 ;
701 }
702 if (is_busy() || (!(is_result_ready())))
703 {
704 #if DEBUG
705 printk("CDU31A timeout out %d\n", __LINE__);
706 #endif
707 result_buffer[0] = 0x20;
708 result_buffer[1] = SONY_TIMEOUT_OP_ERR;
709 *result_size = 2;
710 return;
711 }
712
713
714
715
716
717 clear_result_ready();
718 a = read_result_register();
719 *result_buffer = a;
720 result_buffer++;
721
722
723 if ((a & 0xf0) == 0x50)
724 {
725 *result_size = 1;
726 return;
727 }
728
729 b = read_result_register();
730 *result_buffer = b;
731 result_buffer++;
732 *result_size = 2;
733
734
735
736
737
738
739
740
741
742 if ((a & 0xf0) != 0x20)
743 {
744 if (b > 8)
745 {
746 for (i=0; i<8; i++)
747 {
748 *result_buffer = read_result_register();
749 result_buffer++;
750 (*result_size)++;
751 }
752 b = b - 8;
753
754 while (b > 10)
755 {
756 retry_count = SONY_READY_RETRIES;
757 while ((retry_count > 0) && (!is_result_ready()))
758 {
759 retry_count--;
760 }
761 if (!is_result_ready())
762 {
763 #if DEBUG
764 printk("CDU31A timeout out %d\n", __LINE__);
765 #endif
766 result_buffer[0] = 0x20;
767 result_buffer[1] = SONY_TIMEOUT_OP_ERR;
768 *result_size = 2;
769 return;
770 }
771
772 clear_result_ready();
773
774 for (i=0; i<10; i++)
775 {
776 *result_buffer = read_result_register();
777 result_buffer++;
778 (*result_size)++;
779 }
780 b = b - 10;
781 }
782
783 if (b > 0)
784 {
785 retry_count = SONY_READY_RETRIES;
786 while ((retry_count > 0) && (!is_result_ready()))
787 {
788 retry_count--;
789 }
790 if (!is_result_ready())
791 {
792 #if DEBUG
793 printk("CDU31A timeout out %d\n", __LINE__);
794 #endif
795 result_buffer[0] = 0x20;
796 result_buffer[1] = SONY_TIMEOUT_OP_ERR;
797 *result_size = 2;
798 return;
799 }
800 }
801 }
802
803 while (b > 0)
804 {
805 *result_buffer = read_result_register();
806 result_buffer++;
807 (*result_size)++;
808 b--;
809 }
810 }
811 }
812
813
814
815
816
817
818 static void
819 do_sony_cd_cmd(unsigned char cmd,
820 unsigned char *params,
821 unsigned int num_params,
822 unsigned char *result_buffer,
823 unsigned int *result_size)
824 {
825 unsigned int retry_count;
826 int num_retries;
827 int recursive_call;
828 unsigned long flags;
829
830
831 save_flags(flags);
832 cli();
833 if (current != has_cd_task)
834 {
835 while (sony_inuse)
836 {
837 interruptible_sleep_on(&sony_wait);
838 if (current->signal & ~current->blocked)
839 {
840 result_buffer[0] = 0x20;
841 result_buffer[1] = SONY_SIGNAL_OP_ERR;
842 *result_size = 2;
843 restore_flags(flags);
844 return;
845 }
846 }
847 sony_inuse = 1;
848 has_cd_task = current;
849 recursive_call = 0;
850 }
851 else
852 {
853 recursive_call = 1;
854 }
855
856 num_retries = 0;
857 retry_cd_operation:
858
859 while (handle_sony_cd_attention())
860 ;
861
862 sti();
863
864 retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
865 while ((retry_count > jiffies) && (is_busy()))
866 {
867 sony_sleep();
868
869 while (handle_sony_cd_attention())
870 ;
871 }
872 if (is_busy())
873 {
874 #if DEBUG
875 printk("CDU31A timeout out %d\n", __LINE__);
876 #endif
877 result_buffer[0] = 0x20;
878 result_buffer[1] = SONY_TIMEOUT_OP_ERR;
879 *result_size = 2;
880 }
881 else
882 {
883 clear_result_ready();
884 clear_param_reg();
885
886 write_params(params, num_params);
887 write_cmd(cmd);
888
889 get_result(result_buffer, result_size);
890 }
891
892 if ( ((result_buffer[0] & 0xf0) == 0x20)
893 && (num_retries < MAX_CDU31A_RETRIES))
894 {
895 num_retries++;
896 current->state = TASK_INTERRUPTIBLE;
897 current->timeout = jiffies + HZ/10;
898 schedule();
899 goto retry_cd_operation;
900 }
901
902 if (!recursive_call)
903 {
904 has_cd_task = NULL;
905 sony_inuse = 0;
906 wake_up_interruptible(&sony_wait);
907 }
908
909 restore_flags(flags);
910 }
911
912
913
914
915
916
917
918
919
920
921
922 static int
923 handle_sony_cd_attention(void)
924 {
925 unsigned char atten_code;
926 static int num_consecutive_attentions = 0;
927 volatile int val;
928
929
930 if (is_attention())
931 {
932 if (num_consecutive_attentions > CDU31A_MAX_CONSECUTIVE_ATTENTIONS)
933 {
934 printk("cdu31a: Too many consecutive attentions: %d\n",
935 num_consecutive_attentions);
936 num_consecutive_attentions = 0;
937 return(0);
938 }
939
940 clear_attention();
941 atten_code = read_result_register();
942
943 switch (atten_code)
944 {
945
946 case SONY_MECH_LOADED_ATTN:
947 disk_changed = 1;
948 sony_toc_read = 0;
949 sony_audio_status = CDROM_AUDIO_NO_STATUS;
950 sony_blocks_left = 0;
951 break;
952
953 case SONY_SPIN_DOWN_COMPLETE_ATTN:
954
955 sony_spun_up = 0;
956 break;
957
958 case SONY_AUDIO_PLAY_DONE_ATTN:
959 sony_audio_status = CDROM_AUDIO_COMPLETED;
960 read_subcode();
961 break;
962
963 case SONY_EJECT_PUSHED_ATTN:
964 if (is_auto_eject)
965 {
966 sony_audio_status = CDROM_AUDIO_INVALID;
967 }
968 break;
969
970 case SONY_LEAD_IN_ERR_ATTN:
971 case SONY_LEAD_OUT_ERR_ATTN:
972 case SONY_DATA_TRACK_ERR_ATTN:
973 case SONY_AUDIO_PLAYBACK_ERR_ATTN:
974 sony_audio_status = CDROM_AUDIO_ERROR;
975 break;
976 }
977
978 num_consecutive_attentions++;
979 return(1);
980 }
981 else if (abort_read_started)
982 {
983 while (is_result_reg_not_empty())
984 {
985 val = read_result_register();
986 }
987 clear_data_ready();
988 clear_result_ready();
989
990 while (is_data_requested())
991 {
992 val = read_data_register();
993 }
994 abort_read_started = 0;
995 return(1);
996 }
997
998 num_consecutive_attentions = 0;
999 return(0);
1000 }
1001
1002
1003
1004 static inline unsigned int
1005 int_to_bcd(unsigned int val)
1006 {
1007 int retval;
1008
1009
1010 retval = (val / 10) << 4;
1011 retval = retval | val % 10;
1012 return(retval);
1013 }
1014
1015
1016
1017 static unsigned int
1018 bcd_to_int(unsigned int bcd)
1019 {
1020 return((((bcd >> 4) & 0x0f) * 10) + (bcd & 0x0f));
1021 }
1022
1023
1024
1025
1026
1027
1028 static void
1029 log_to_msf(unsigned int log, unsigned char *msf)
1030 {
1031 log = log + LOG_START_OFFSET;
1032 msf[0] = int_to_bcd(log / 4500);
1033 log = log % 4500;
1034 msf[1] = int_to_bcd(log / 75);
1035 msf[2] = int_to_bcd(log % 75);
1036 }
1037
1038
1039
1040
1041
1042 static unsigned int
1043 msf_to_log(unsigned char *msf)
1044 {
1045 unsigned int log;
1046
1047
1048 log = bcd_to_int(msf[2]);
1049 log += bcd_to_int(msf[1]) * 75;
1050 log += bcd_to_int(msf[0]) * 4500;
1051 log = log - LOG_START_OFFSET;
1052
1053 return log;
1054 }
1055
1056
1057
1058
1059
1060
1061 static void
1062 size_to_buf(unsigned int size,
1063 unsigned char *buf)
1064 {
1065 buf[0] = size / 65536;
1066 size = size % 65536;
1067 buf[1] = size / 256;
1068 buf[2] = size % 256;
1069 }
1070
1071
1072
1073
1074
1075
1076
1077 static int
1078 start_request(unsigned int sector,
1079 unsigned int nsect,
1080 int read_nsect_only)
1081 {
1082 unsigned char params[6];
1083 unsigned int read_size;
1084 unsigned int retry_count;
1085
1086
1087 log_to_msf(sector, params);
1088
1089 if (read_nsect_only)
1090 {
1091 read_size = nsect;
1092 }
1093
1094
1095
1096
1097 else if ((sector + nsect) >= sony_toc.lead_out_start_lba)
1098 {
1099 read_size = sony_toc.lead_out_start_lba - sector;
1100 }
1101
1102 else
1103 {
1104 read_size = CDU31A_READAHEAD;
1105 }
1106 size_to_buf(read_size, ¶ms[3]);
1107
1108
1109
1110
1111
1112 while (handle_sony_cd_attention())
1113 ;
1114
1115 retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
1116 while ((retry_count > jiffies) && (is_busy()))
1117 {
1118 sony_sleep();
1119
1120 while (handle_sony_cd_attention())
1121 ;
1122 }
1123
1124 if (is_busy())
1125 {
1126 printk("CDU31A: Timeout while waiting to issue command\n");
1127 return(1);
1128 }
1129 else
1130 {
1131
1132 clear_result_ready();
1133 clear_param_reg();
1134
1135 write_params(params, 6);
1136 write_cmd(SONY_READ_BLKERR_STAT_CMD);
1137
1138 sony_blocks_left = read_size * 4;
1139 sony_next_block = sector * 4;
1140 readahead_dataleft = 0;
1141 readahead_bad = 0;
1142 return(0);
1143 }
1144 }
1145
1146
1147
1148 static void
1149 abort_read(void)
1150 {
1151 unsigned char result_reg[2];
1152 int result_size;
1153 volatile int val;
1154
1155
1156 do_sony_cd_cmd(SONY_ABORT_CMD, NULL, 0, result_reg, &result_size);
1157 if ((result_reg[0] & 0xf0) == 0x20)
1158 {
1159 printk("CDU31A: Error aborting read, error = 0x%2.2x\n",
1160 result_reg[1]);
1161 }
1162
1163 while (is_result_reg_not_empty())
1164 {
1165 val = read_result_register();
1166 }
1167 clear_data_ready();
1168 clear_result_ready();
1169
1170 while (is_data_requested())
1171 {
1172 val = read_data_register();
1173 }
1174
1175 sony_blocks_left = 0;
1176 readahead_dataleft = 0;
1177 readahead_bad = 0;
1178 }
1179
1180
1181
1182 static void
1183 handle_abort_timeout(unsigned long data)
1184 {
1185
1186 if (!sony_inuse)
1187 {
1188
1189
1190
1191
1192 clear_result_ready();
1193 clear_param_reg();
1194 write_cmd(SONY_ABORT_CMD);
1195
1196 sony_blocks_left = 0;
1197 readahead_dataleft = 0;
1198 readahead_bad = 0;
1199 abort_read_started = 1;
1200 }
1201 }
1202
1203
1204 static void
1205 input_data(char *buffer,
1206 unsigned int bytesleft,
1207 unsigned int nblocks,
1208 unsigned int offset,
1209 unsigned int skip)
1210 {
1211 int i;
1212 volatile unsigned char val;
1213
1214
1215
1216
1217 if (sony_xa_mode)
1218 {
1219 for(i=0; i<CD_XA_HEAD; i++)
1220 {
1221 val = read_data_register();
1222 }
1223 }
1224
1225 clear_data_ready();
1226
1227 if (bytesleft == 2048)
1228 {
1229 insb(sony_cd_read_reg, buffer, 2048);
1230 readahead_dataleft = 0;
1231 }
1232 else
1233 {
1234
1235
1236 if (skip != 0)
1237 {
1238 insb(sony_cd_read_reg, readahead_buffer, skip);
1239 }
1240
1241
1242 insb(sony_cd_read_reg, &buffer[offset], bytesleft);
1243
1244
1245
1246 readahead_dataleft = (2048 - skip) - bytesleft;
1247 insb(sony_cd_read_reg,
1248 readahead_buffer + bytesleft,
1249 readahead_dataleft);
1250 }
1251 sony_blocks_left -= nblocks;
1252 sony_next_block += nblocks;
1253
1254
1255
1256 if (sony_xa_mode)
1257 {
1258 for(i=0; i<CD_XA_TAIL; i++)
1259 {
1260 val = read_data_register();
1261 }
1262 }
1263 }
1264
1265
1266 static void
1267 read_data_block(char *buffer,
1268 unsigned int block,
1269 unsigned int nblocks,
1270 unsigned char res_reg[],
1271 int *res_size)
1272 {
1273 unsigned int retry_count;
1274 unsigned int bytesleft;
1275 unsigned int offset;
1276 unsigned int skip;
1277
1278
1279 res_reg[0] = 0;
1280 res_reg[1] = 0;
1281 *res_size = 0;
1282 bytesleft = nblocks * 512;
1283 offset = 0;
1284
1285
1286
1287 if (((block % 4) * 512) != ((2048 - readahead_dataleft) % 2048))
1288 {
1289 sony_next_block += block % 4;
1290 sony_blocks_left -= block % 4;
1291 skip = (block % 4) * 512;
1292 }
1293 else
1294 {
1295 skip = 0;
1296 }
1297
1298
1299
1300 if (readahead_dataleft != 0)
1301 {
1302 if (bytesleft > readahead_dataleft)
1303 {
1304
1305
1306 memcpy(buffer,
1307 readahead_buffer + (2048 - readahead_dataleft),
1308 readahead_dataleft);
1309 readahead_dataleft = 0;
1310 bytesleft -= readahead_dataleft;
1311 offset += readahead_dataleft;
1312 }
1313 else
1314 {
1315
1316
1317 memcpy(buffer,
1318 readahead_buffer + (2048 - readahead_dataleft),
1319 bytesleft);
1320 readahead_dataleft -= bytesleft;
1321 bytesleft = 0;
1322 sony_blocks_left -= nblocks;
1323 sony_next_block += nblocks;
1324
1325
1326
1327 if (readahead_bad)
1328 {
1329 res_reg[0] = 0x20;
1330 res_reg[1] = SONY_BAD_DATA_ERR;
1331 *res_size = 2;
1332 }
1333
1334 if (readahead_dataleft == 0)
1335 {
1336 readahead_bad = 0;
1337 }
1338
1339
1340 if (sony_blocks_left == 0)
1341 {
1342 get_result(res_reg, res_size);
1343 }
1344 return;
1345 }
1346 }
1347
1348
1349 retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
1350 while ((retry_count > jiffies) && !(is_data_ready()))
1351 {
1352 while (handle_sony_cd_attention())
1353 ;
1354
1355 sony_sleep();
1356 }
1357 if (!(is_data_ready()))
1358 {
1359 if (is_result_ready())
1360 {
1361 get_result(res_reg, res_size);
1362 if ((res_reg[0] & 0xf0) != 0x20)
1363 {
1364 printk("CDU31A: Got result that should have been error: %d\n",
1365 res_reg[0]);
1366 res_reg[0] = 0x20;
1367 res_reg[1] = SONY_BAD_DATA_ERR;
1368 *res_size = 2;
1369 }
1370 abort_read();
1371 }
1372 else
1373 {
1374 #if DEBUG
1375 printk("CDU31A timeout out %d\n", __LINE__);
1376 #endif
1377 res_reg[0] = 0x20;
1378 res_reg[1] = SONY_TIMEOUT_OP_ERR;
1379 *res_size = 2;
1380 abort_read();
1381 }
1382 }
1383 else
1384 {
1385 input_data(buffer, bytesleft, nblocks, offset, skip);
1386
1387
1388 retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
1389 while ((retry_count > jiffies) && !(is_result_ready()))
1390 {
1391 while (handle_sony_cd_attention())
1392 ;
1393
1394 sony_sleep();
1395 }
1396
1397 if (!is_result_ready())
1398 {
1399 #if DEBUG
1400 printk("CDU31A timeout out %d\n", __LINE__);
1401 #endif
1402 res_reg[0] = 0x20;
1403 res_reg[1] = SONY_TIMEOUT_OP_ERR;
1404 *res_size = 2;
1405 abort_read();
1406 }
1407 else
1408 {
1409 get_result(res_reg, res_size);
1410
1411
1412 if ((res_reg[0] & 0xf0) == 0x50)
1413 {
1414
1415 if ( (res_reg[0] == SONY_NO_CIRC_ERR_BLK_STAT)
1416 || (res_reg[0] == SONY_NO_LECC_ERR_BLK_STAT)
1417 || (res_reg[0] == SONY_RECOV_LECC_ERR_BLK_STAT))
1418 {
1419
1420
1421
1422 if (readahead_bad)
1423 {
1424 readahead_bad = 0;
1425 res_reg[0] = 0x20;
1426 res_reg[1] = SONY_BAD_DATA_ERR;
1427 *res_size = 2;
1428 }
1429 }
1430 else
1431 {
1432 printk("CDU31A: Data block error: 0x%x\n", res_reg[0]);
1433 res_reg[0] = 0x20;
1434 res_reg[1] = SONY_BAD_DATA_ERR;
1435 *res_size = 2;
1436
1437
1438
1439 if (bytesleft != 2048)
1440 {
1441 readahead_bad = 1;
1442 }
1443 }
1444
1445
1446 if (sony_blocks_left == 0)
1447 {
1448 get_result(res_reg, res_size);
1449 }
1450 }
1451 else if ((res_reg[0] & 0xf0) != 0x20)
1452 {
1453
1454
1455 printk("CDU31A: Invalid block status: 0x%x\n", res_reg[0]);
1456 restart_on_error();
1457 res_reg[0] = 0x20;
1458 res_reg[1] = SONY_BAD_DATA_ERR;
1459 *res_size = 2;
1460 }
1461 }
1462 }
1463 }
1464
1465
1466
1467
1468
1469
1470
1471
1472 static void
1473 do_cdu31a_request(void)
1474 {
1475 int block;
1476 int nblock;
1477 unsigned char res_reg[12];
1478 unsigned int res_size;
1479 int num_retries;
1480 unsigned long flags;
1481
1482
1483
1484
1485
1486
1487 save_flags(flags);
1488 cli();
1489 while (sony_inuse)
1490 {
1491 interruptible_sleep_on(&sony_wait);
1492 if (current->signal & ~current->blocked)
1493 {
1494 restore_flags(flags);
1495 if (CURRENT && CURRENT->rq_status != RQ_INACTIVE)
1496 {
1497 end_request(0);
1498 }
1499 restore_flags(flags);
1500 return;
1501 }
1502 }
1503 sony_inuse = 1;
1504 has_cd_task = current;
1505
1506
1507 while (handle_sony_cd_attention())
1508 ;
1509
1510
1511 sony_get_toc();
1512
1513 sti();
1514
1515
1516 if (cdu31a_abort_timer.next != NULL)
1517 {
1518 del_timer(&cdu31a_abort_timer);
1519 }
1520
1521 while (1)
1522 {
1523 cdu31a_request_startover:
1524
1525
1526
1527
1528 if (!(CURRENT) || CURRENT->rq_status == RQ_INACTIVE)
1529 {
1530 goto end_do_cdu31a_request;
1531 }
1532
1533 if (!sony_spun_up)
1534 {
1535 struct inode in;
1536
1537
1538
1539
1540 in.i_rdev = CURRENT->rq_dev;
1541 scd_open(&in,NULL);
1542 }
1543
1544
1545
1546
1547 if (MAJOR(CURRENT->rq_dev) != MAJOR_NR)
1548 {
1549 panic(DEVICE_NAME ": request list destroyed");
1550 }
1551 if (CURRENT->bh)
1552 {
1553 if (!buffer_locked(CURRENT->bh))
1554 {
1555 panic(DEVICE_NAME ": block not locked");
1556 }
1557 }
1558
1559 block = CURRENT->sector;
1560 nblock = CURRENT->nr_sectors;
1561
1562 if (!sony_toc_read)
1563 {
1564 printk("CDU31A: TOC not read\n");
1565 end_request(0);
1566 goto cdu31a_request_startover;
1567 }
1568
1569
1570
1571
1572
1573 if (block < 80)
1574 {
1575
1576 block += (sony_toc.start_track_lba * 4);
1577 }
1578
1579 switch(CURRENT->cmd)
1580 {
1581 case READ:
1582
1583
1584
1585
1586 #if 0
1587 if ((block / 4) < sony_toc.start_track_lba)
1588 {
1589 printk("CDU31A: Request before beginning of media\n");
1590 end_request(0);
1591 goto cdu31a_request_startover;
1592 }
1593 #endif
1594 if ((block / 4) >= sony_toc.lead_out_start_lba)
1595 {
1596 printk("CDU31A: Request past end of media\n");
1597 end_request(0);
1598 goto cdu31a_request_startover;
1599 }
1600 if (((block + nblock) / 4) >= sony_toc.lead_out_start_lba)
1601 {
1602 printk("CDU31A: Request past end of media\n");
1603 end_request(0);
1604 goto cdu31a_request_startover;
1605 }
1606
1607 num_retries = 0;
1608
1609 try_read_again:
1610 while (handle_sony_cd_attention())
1611 ;
1612
1613 if (!sony_toc_read)
1614 {
1615 printk("CDU31A: TOC not read\n");
1616 end_request(0);
1617 goto cdu31a_request_startover;
1618 }
1619
1620
1621
1622 if (sony_blocks_left == 0)
1623 {
1624 if (start_request(block / 4, CDU31A_READAHEAD / 4, 0))
1625 {
1626 end_request(0);
1627 goto cdu31a_request_startover;
1628 }
1629 }
1630
1631
1632
1633 else if (block != sony_next_block)
1634 {
1635 #if DEBUG
1636 printk("CDU31A Warning: Read for block %d, expected %d\n",
1637 block,
1638 sony_next_block);
1639 #endif
1640 abort_read();
1641 if (!sony_toc_read)
1642 {
1643 printk("CDU31A: TOC not read\n");
1644 end_request(0);
1645 goto cdu31a_request_startover;
1646 }
1647 if (start_request(block / 4, CDU31A_READAHEAD / 4, 0))
1648 {
1649 printk("CDU31a: start request failed\n");
1650 end_request(0);
1651 goto cdu31a_request_startover;
1652 }
1653 }
1654
1655 read_data_block(CURRENT->buffer, block, nblock, res_reg, &res_size);
1656 if (res_reg[0] == 0x20)
1657 {
1658 if (num_retries > MAX_CDU31A_RETRIES)
1659 {
1660 end_request(0);
1661 goto cdu31a_request_startover;
1662 }
1663
1664 num_retries++;
1665 if (res_reg[1] == SONY_NOT_SPIN_ERR)
1666 {
1667 do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg, &res_size);
1668 }
1669 else
1670 {
1671 printk("CDU31A: Read error: 0x%2.2x\n", res_reg[1]);
1672 }
1673 goto try_read_again;
1674 }
1675 else
1676 {
1677 end_request(1);
1678 }
1679 break;
1680
1681 case WRITE:
1682 end_request(0);
1683 break;
1684
1685 default:
1686 panic("CDU31A: Unknown cmd");
1687 }
1688 }
1689
1690 end_do_cdu31a_request:
1691 #if 0
1692
1693 abort_read();
1694 #else
1695
1696
1697 cdu31a_abort_timer.expires = jiffies + 2*HZ;
1698 add_timer(&cdu31a_abort_timer);
1699 #endif
1700
1701 has_cd_task = NULL;
1702 sony_inuse = 0;
1703 wake_up_interruptible(&sony_wait);
1704 restore_flags(flags);
1705 }
1706
1707
1708 static void
1709 mcovlp(char *dst,
1710 char *src,
1711 int size)
1712 {
1713 src += (size - 1);
1714 dst += (size - 1);
1715 while (size > 0)
1716 {
1717 *dst = *src;
1718 size--;
1719 dst--;
1720 src--;
1721 }
1722 }
1723
1724
1725
1726
1727
1728
1729 static void
1730 sony_get_toc(void)
1731 {
1732 unsigned char res_reg[2];
1733 unsigned int res_size;
1734 unsigned char parms[1];
1735 int session;
1736 int num_spin_ups;
1737
1738
1739 #if DEBUG
1740 printk("Entering sony_get_toc\n");
1741 #endif
1742
1743 num_spin_ups = 0;
1744 if (!sony_toc_read)
1745 {
1746 respinup_on_gettoc:
1747
1748 do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg, &res_size);
1749
1750 do_sony_cd_cmd(SONY_READ_TOC_CMD, NULL, 0, res_reg, &res_size);
1751
1752
1753
1754 if ((res_size < 2) || ((res_reg[0] != 0) && (res_reg[1] != 0)))
1755 {
1756
1757 if ((res_reg[1] == SONY_AUDIO_PLAYING_ERR) || (res_reg[1] == 0))
1758 {
1759 goto gettoc_drive_spinning;
1760 }
1761
1762
1763
1764 if ( (res_reg[1] == SONY_NOT_SPIN_ERR)
1765 && (num_spin_ups < MAX_CDU31A_RETRIES))
1766 {
1767 num_spin_ups++;
1768 goto respinup_on_gettoc;
1769 }
1770
1771 printk("cdu31a: Error reading TOC: %x %x\n",
1772 sony_toc.exec_status[0],
1773 sony_toc.exec_status[1]);
1774 return;
1775 }
1776
1777 gettoc_drive_spinning:
1778
1779
1780
1781
1782
1783
1784
1785 session = 2;
1786 while (1)
1787 {
1788 #if DEBUG
1789 printk("Trying session %d\n", session);
1790 #endif
1791 parms[0] = session;
1792 do_sony_cd_cmd(SONY_READ_TOC_SPEC_CMD,
1793 parms,
1794 1,
1795 res_reg,
1796 &res_size);
1797
1798 #if DEBUG
1799 printk("%2.2x %2.2x\n", res_reg[0], res_reg[1]);
1800 #endif
1801
1802 if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20))
1803 {
1804
1805 break;
1806 }
1807
1808 session++;
1809
1810
1811 if (session > 20)
1812 {
1813 printk("cdu31a: too many sessions: %d\n", session);
1814 return;
1815 }
1816 }
1817
1818 session--;
1819
1820 #if DEBUG
1821 printk("Reading session %d\n", session);
1822 #endif
1823
1824 parms[0] = session;
1825 do_sony_cd_cmd(SONY_REQ_TOC_DATA_SPEC_CMD,
1826 parms,
1827 1,
1828 (unsigned char *) &sony_toc,
1829 &res_size);
1830 if ((res_size < 2) || ((sony_toc.exec_status[0] & 0xf0) == 0x20))
1831 {
1832 printk("cdu31a: Error reading session %d: %x %x\n",
1833 session,
1834 sony_toc.exec_status[0],
1835 sony_toc.exec_status[1]);
1836
1837
1838 return;
1839 }
1840
1841 sony_toc_read = 1;
1842
1843
1844
1845 if (sony_toc.pointb0 != 0xb0)
1846 {
1847 mcovlp(((char *) &sony_toc) + 27,
1848 ((char *) &sony_toc) + 18,
1849 res_size - 18);
1850 res_size += 9;
1851 }
1852 if (sony_toc.pointb1 != 0xb1)
1853 {
1854 mcovlp(((char *) &sony_toc) + 36,
1855 ((char *) &sony_toc) + 27,
1856 res_size - 27);
1857 res_size += 9;
1858 }
1859 if (sony_toc.pointb2 != 0xb2)
1860 {
1861 mcovlp(((char *) &sony_toc) + 45,
1862 ((char *) &sony_toc) + 36,
1863 res_size - 36);
1864 res_size += 9;
1865 }
1866 if (sony_toc.pointb3 != 0xb3)
1867 {
1868 mcovlp(((char *) &sony_toc) + 54,
1869 ((char *) &sony_toc) + 45,
1870 res_size - 45);
1871 res_size += 9;
1872 }
1873 if (sony_toc.pointb4 != 0xb4)
1874 {
1875 mcovlp(((char *) &sony_toc) + 63,
1876 ((char *) &sony_toc) + 54,
1877 res_size - 54);
1878 res_size += 9;
1879 }
1880 if (sony_toc.pointc0 != 0xc0)
1881 {
1882 mcovlp(((char *) &sony_toc) + 72,
1883 ((char *) &sony_toc) + 63,
1884 res_size - 63);
1885 res_size += 9;
1886 }
1887
1888 sony_toc.start_track_lba = msf_to_log(sony_toc.tracks[0].track_start_msf);
1889 sony_toc.lead_out_start_lba = msf_to_log(sony_toc.lead_out_start_msf);
1890
1891 #if DEBUG
1892 printk("Disk session %d, start track: %d, stop track: %d\n",
1893 session,
1894 sony_toc.start_track_lba,
1895 sony_toc.lead_out_start_lba);
1896 #endif
1897 }
1898 #if DEBUG
1899 printk("Leaving sony_get_toc\n");
1900 #endif
1901 }
1902
1903
1904
1905
1906
1907 static int
1908 find_track(int track)
1909 {
1910 int i;
1911 int num_tracks;
1912
1913
1914 num_tracks = sony_toc.last_track_num - sony_toc.first_track_num + 1;
1915 for (i = 0; i < num_tracks; i++)
1916 {
1917 if (sony_toc.tracks[i].track == track)
1918 {
1919 return i;
1920 }
1921 }
1922
1923 return -1;
1924 }
1925
1926
1927
1928
1929
1930 static int
1931 read_subcode(void)
1932 {
1933 unsigned int res_size;
1934
1935
1936 do_sony_cd_cmd(SONY_REQ_SUBCODE_ADDRESS_CMD,
1937 NULL,
1938 0,
1939 (unsigned char *) &last_sony_subcode,
1940 &res_size);
1941 if ((res_size < 2) || ((last_sony_subcode.exec_status[0] & 0xf0) == 0x20))
1942 {
1943 printk("Sony CDROM error 0x%2.2x (read_subcode)\n",
1944 last_sony_subcode.exec_status[1]);
1945 return -EIO;
1946 }
1947
1948 return 0;
1949 }
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959 static int
1960 sony_get_subchnl_info(long arg)
1961 {
1962 int err;
1963 struct cdrom_subchnl schi;
1964
1965
1966
1967 while (handle_sony_cd_attention())
1968 ;
1969
1970 sony_get_toc();
1971 if (!sony_toc_read)
1972 {
1973 return -EIO;
1974 }
1975
1976 err = verify_area(VERIFY_READ, (char *) arg, sizeof(schi)) ||
1977 verify_area(VERIFY_WRITE, (char *) arg, sizeof(schi));
1978 if (err) return err;
1979
1980 memcpy_fromfs(&schi, (char *) arg, sizeof(schi));
1981
1982 switch (sony_audio_status)
1983 {
1984 case CDROM_AUDIO_PLAY:
1985 if (read_subcode() < 0)
1986 {
1987 return -EIO;
1988 }
1989 break;
1990
1991 case CDROM_AUDIO_PAUSED:
1992 case CDROM_AUDIO_COMPLETED:
1993 break;
1994
1995 case CDROM_AUDIO_NO_STATUS:
1996 schi.cdsc_audiostatus = sony_audio_status;
1997 memcpy_tofs((char *) arg, &schi, sizeof(schi));
1998 return 0;
1999 break;
2000
2001 case CDROM_AUDIO_INVALID:
2002 case CDROM_AUDIO_ERROR:
2003 default:
2004 return -EIO;
2005 }
2006
2007 schi.cdsc_audiostatus = sony_audio_status;
2008 schi.cdsc_adr = last_sony_subcode.address;
2009 schi.cdsc_ctrl = last_sony_subcode.control;
2010 schi.cdsc_trk = bcd_to_int(last_sony_subcode.track_num);
2011 schi.cdsc_ind = bcd_to_int(last_sony_subcode.index_num);
2012 if (schi.cdsc_format == CDROM_MSF)
2013 {
2014 schi.cdsc_absaddr.msf.minute = bcd_to_int(last_sony_subcode.abs_msf[0]);
2015 schi.cdsc_absaddr.msf.second = bcd_to_int(last_sony_subcode.abs_msf[1]);
2016 schi.cdsc_absaddr.msf.frame = bcd_to_int(last_sony_subcode.abs_msf[2]);
2017
2018 schi.cdsc_reladdr.msf.minute = bcd_to_int(last_sony_subcode.rel_msf[0]);
2019 schi.cdsc_reladdr.msf.second = bcd_to_int(last_sony_subcode.rel_msf[1]);
2020 schi.cdsc_reladdr.msf.frame = bcd_to_int(last_sony_subcode.rel_msf[2]);
2021 }
2022 else if (schi.cdsc_format == CDROM_LBA)
2023 {
2024 schi.cdsc_absaddr.lba = msf_to_log(last_sony_subcode.abs_msf);
2025 schi.cdsc_reladdr.lba = msf_to_log(last_sony_subcode.rel_msf);
2026 }
2027
2028 memcpy_tofs((char *) arg, &schi, sizeof(schi));
2029 return 0;
2030 }
2031
2032
2033
2034
2035
2036 static void
2037 read_audio_data(char *buffer,
2038 unsigned char res_reg[],
2039 int *res_size)
2040 {
2041 unsigned int retry_count;
2042 int result_read;
2043
2044
2045 res_reg[0] = 0;
2046 res_reg[1] = 0;
2047 *res_size = 0;
2048 result_read = 0;
2049
2050
2051 retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
2052 continue_read_audio_wait:
2053 while ( (retry_count > jiffies)
2054 && !(is_data_ready())
2055 && !(is_result_ready() || result_read))
2056 {
2057 while (handle_sony_cd_attention())
2058 ;
2059
2060 sony_sleep();
2061 }
2062 if (!(is_data_ready()))
2063 {
2064 if (is_result_ready() && !result_read)
2065 {
2066 get_result(res_reg, res_size);
2067
2068
2069 if ((res_reg[0] & 0xf0) == 0x50)
2070 {
2071 result_read = 1;
2072 goto continue_read_audio_wait;
2073 }
2074
2075 else if ((res_reg[0] & 0xf0) != 0x20)
2076 {
2077 printk("CDU31A: Got result that should have been error: %d\n",
2078 res_reg[0]);
2079 res_reg[0] = 0x20;
2080 res_reg[1] = SONY_BAD_DATA_ERR;
2081 *res_size = 2;
2082 }
2083 abort_read();
2084 }
2085 else
2086 {
2087 #if DEBUG
2088 printk("CDU31A timeout out %d\n", __LINE__);
2089 #endif
2090 res_reg[0] = 0x20;
2091 res_reg[1] = SONY_TIMEOUT_OP_ERR;
2092 *res_size = 2;
2093 abort_read();
2094 }
2095 }
2096 else
2097 {
2098 clear_data_ready();
2099
2100
2101 if (sony_raw_data_mode)
2102 {
2103 insb(sony_cd_read_reg, buffer + CD_XA_HEAD, CD_FRAMESIZE_XA);
2104 }
2105 else
2106 {
2107
2108 insb(sony_cd_read_reg, buffer, CD_FRAMESIZE_RAW);
2109 }
2110
2111
2112 if (!result_read)
2113 {
2114
2115 retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
2116 while ((retry_count > jiffies) && !(is_result_ready()))
2117 {
2118 while (handle_sony_cd_attention())
2119 ;
2120
2121 sony_sleep();
2122 }
2123
2124 if (!is_result_ready())
2125 {
2126 #if DEBUG
2127 printk("CDU31A timeout out %d\n", __LINE__);
2128 #endif
2129 res_reg[0] = 0x20;
2130 res_reg[1] = SONY_TIMEOUT_OP_ERR;
2131 *res_size = 2;
2132 abort_read();
2133 return;
2134 }
2135 else
2136 {
2137 get_result(res_reg, res_size);
2138 }
2139 }
2140
2141 if ((res_reg[0] & 0xf0) == 0x50)
2142 {
2143 if ( (res_reg[0] == SONY_NO_CIRC_ERR_BLK_STAT)
2144 || (res_reg[0] == SONY_NO_LECC_ERR_BLK_STAT)
2145 || (res_reg[0] == SONY_RECOV_LECC_ERR_BLK_STAT)
2146 || (res_reg[0] == SONY_NO_ERR_DETECTION_STAT))
2147 {
2148
2149 }
2150 else
2151 {
2152 printk("CDU31A: Data block error: 0x%x\n", res_reg[0]);
2153 res_reg[0] = 0x20;
2154 res_reg[1] = SONY_BAD_DATA_ERR;
2155 *res_size = 2;
2156 }
2157 }
2158 else if ((res_reg[0] & 0xf0) != 0x20)
2159 {
2160
2161
2162 printk("CDU31A: Invalid block status: 0x%x\n", res_reg[0]);
2163 restart_on_error();
2164 res_reg[0] = 0x20;
2165 res_reg[1] = SONY_BAD_DATA_ERR;
2166 *res_size = 2;
2167 }
2168 }
2169 }
2170
2171
2172
2173 static int
2174 read_audio(struct cdrom_read_audio *ra,
2175 struct inode *inode)
2176 {
2177 int retval;
2178 unsigned char params[2];
2179 unsigned char res_reg[12];
2180 unsigned int res_size;
2181 unsigned int cframe;
2182 unsigned long flags;
2183
2184
2185
2186
2187
2188 save_flags(flags);
2189 cli();
2190 while (sony_inuse)
2191 {
2192 interruptible_sleep_on(&sony_wait);
2193 if (current->signal & ~current->blocked)
2194 {
2195 restore_flags(flags);
2196 return -EAGAIN;
2197 }
2198 }
2199 sony_inuse = 1;
2200 has_cd_task = current;
2201 restore_flags(flags);
2202
2203 if (!sony_spun_up)
2204 {
2205 scd_open (inode, NULL);
2206 }
2207
2208
2209 params[0] = SONY_SD_DECODE_PARAM;
2210 params[1] = 0x06 | sony_raw_data_mode;
2211 do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
2212 params,
2213 2,
2214 res_reg,
2215 &res_size);
2216 if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20))
2217 {
2218 printk("CDU31A: Unable to set decode params: 0x%2.2x\n", res_reg[1]);
2219 return -EIO;
2220 }
2221
2222
2223
2224
2225
2226 retval = 0;
2227
2228 if (start_request(ra->addr.lba, ra->nframes, 1))
2229 {
2230 retval = -EIO;
2231 goto exit_read_audio;
2232 }
2233
2234
2235 cframe = 0;
2236 while (cframe < ra->nframes)
2237 {
2238 read_audio_data(readahead_buffer, res_reg, &res_size);
2239 if ((res_reg[0] & 0xf0) == 0x20)
2240 {
2241 if (res_reg[1] == SONY_BAD_DATA_ERR)
2242 {
2243 printk("CDU31A: Data error on audio sector %d\n",
2244 ra->addr.lba + cframe);
2245 }
2246 else if (res_reg[1] == SONY_ILL_TRACK_R_ERR)
2247 {
2248
2249 sony_raw_data_mode = (sony_raw_data_mode) ? 0 : 1;
2250
2251
2252 params[0] = SONY_SD_DECODE_PARAM;
2253 params[1] = 0x06 | sony_raw_data_mode;
2254 do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
2255 params,
2256 2,
2257 res_reg,
2258 &res_size);
2259 if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20))
2260 {
2261 printk("CDU31A: Unable to set decode params: 0x%2.2x\n", res_reg[1]);
2262 retval = -EIO;
2263 goto exit_read_audio;
2264 }
2265
2266
2267 if (start_request(ra->addr.lba + cframe, ra->nframes - cframe, 1))
2268 {
2269 retval = -EIO;
2270 goto exit_read_audio;
2271 }
2272
2273
2274
2275
2276 read_audio_data(readahead_buffer, res_reg, &res_size);
2277 if ((res_reg[0] & 0xf0) == 0x20)
2278 {
2279 if (res_reg[1] == SONY_BAD_DATA_ERR)
2280 {
2281 printk("CDU31A: Data error on audio sector %d\n",
2282 ra->addr.lba + cframe);
2283 }
2284 else
2285 {
2286 printk("CDU31A: Error reading audio data on sector %d: 0x%x\n",
2287 ra->addr.lba + cframe,
2288 res_reg[1]);
2289 retval = -EIO;
2290 goto exit_read_audio;
2291 }
2292 }
2293 else
2294 {
2295 memcpy_tofs((char *) (ra->buf + (CD_FRAMESIZE_RAW * cframe)),
2296 (char *) readahead_buffer,
2297 CD_FRAMESIZE_RAW);
2298 }
2299 }
2300 else
2301 {
2302 printk("CDU31A: Error reading audio data on sector %d: 0x%x\n",
2303 ra->addr.lba + cframe,
2304 res_reg[1]);
2305 retval = -EIO;
2306 goto exit_read_audio;
2307 }
2308 }
2309 else
2310 {
2311 memcpy_tofs((char *) (ra->buf + (CD_FRAMESIZE_RAW * cframe)),
2312 (char *) readahead_buffer,
2313 CD_FRAMESIZE_RAW);
2314 }
2315
2316 cframe++;
2317 }
2318
2319 get_result(res_reg, &res_size);
2320 if ((res_reg[0] & 0xf0) == 0x20)
2321 {
2322 printk("CDU31A: Error return from audio read: 0x%x\n",
2323 res_reg[1]);
2324 retval = -EIO;
2325 goto exit_read_audio;
2326 }
2327
2328 exit_read_audio:
2329
2330
2331 params[0] = SONY_SD_DECODE_PARAM;
2332 if (!sony_xa_mode)
2333 {
2334 params[1] = 0x0f;
2335 }
2336 else
2337 {
2338 params[1] = 0x07;
2339 }
2340 do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
2341 params,
2342 2,
2343 res_reg,
2344 &res_size);
2345 if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20))
2346 {
2347 printk("CDU31A: Unable to reset decode params: 0x%2.2x\n", res_reg[1]);
2348 return -EIO;
2349 }
2350
2351 has_cd_task = NULL;
2352 sony_inuse = 0;
2353 wake_up_interruptible(&sony_wait);
2354
2355 return(retval);
2356 }
2357
2358 static int
2359 do_sony_cd_cmd_chk(const char *name,
2360 unsigned char cmd,
2361 unsigned char *params,
2362 unsigned int num_params,
2363 unsigned char *result_buffer,
2364 unsigned int *result_size)
2365 {
2366 do_sony_cd_cmd(cmd, params, num_params, result_buffer, result_size);
2367 if ((*result_size < 2) || ((result_buffer[0] & 0xf0) == 0x20))
2368 {
2369 printk("Sony CDROM error 0x%2.2x (CDROM%s)\n", result_buffer[1], name);
2370 return -EIO;
2371 }
2372 return 0;
2373 }
2374
2375
2376
2377
2378 static int scd_ioctl(struct inode *inode,
2379 struct file *file,
2380 unsigned int cmd,
2381 unsigned long arg)
2382 {
2383 unsigned char res_reg[12];
2384 unsigned int res_size;
2385 unsigned char params[7];
2386 int i;
2387
2388
2389 if (!inode)
2390 {
2391 return -EINVAL;
2392 }
2393
2394 switch (cmd)
2395 {
2396 case CDROMSTART:
2397 return do_sony_cd_cmd_chk("START",SONY_SPIN_UP_CMD, NULL, 0, res_reg, &res_size);
2398 return 0;
2399 break;
2400
2401 case CDROMSTOP:
2402 do_sony_cd_cmd(SONY_AUDIO_STOP_CMD, NULL, 0, res_reg, &res_size);
2403
2404
2405
2406
2407
2408 sony_audio_status = CDROM_AUDIO_NO_STATUS;
2409 return do_sony_cd_cmd_chk("STOP",SONY_SPIN_DOWN_CMD, NULL, 0, res_reg, &res_size);
2410
2411 case CDROMPAUSE:
2412 if(do_sony_cd_cmd_chk("PAUSE", SONY_AUDIO_STOP_CMD, NULL, 0, res_reg, &res_size))
2413 return -EIO;
2414
2415 if (read_subcode() < 0)
2416 {
2417 return -EIO;
2418 }
2419 cur_pos_msf[0] = last_sony_subcode.abs_msf[0];
2420 cur_pos_msf[1] = last_sony_subcode.abs_msf[1];
2421 cur_pos_msf[2] = last_sony_subcode.abs_msf[2];
2422 sony_audio_status = CDROM_AUDIO_PAUSED;
2423 return 0;
2424 break;
2425
2426 case CDROMRESUME:
2427 if (sony_audio_status != CDROM_AUDIO_PAUSED)
2428 {
2429 return -EINVAL;
2430 }
2431
2432 do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg, &res_size);
2433
2434
2435 params[1] = cur_pos_msf[0];
2436 params[2] = cur_pos_msf[1];
2437 params[3] = cur_pos_msf[2];
2438 params[4] = final_pos_msf[0];
2439 params[5] = final_pos_msf[1];
2440 params[6] = final_pos_msf[2];
2441 params[0] = 0x03;
2442 if(do_sony_cd_cmd_chk("RESUME",SONY_AUDIO_PLAYBACK_CMD, params, 7, res_reg, &res_size)<0)
2443 return -EIO;
2444 sony_audio_status = CDROM_AUDIO_PLAY;
2445 return 0;
2446
2447 case CDROMPLAYMSF:
2448 i=verify_area(VERIFY_READ, (char *) arg, 6);
2449 if(i)
2450 return i;
2451 do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg, &res_size);
2452 memcpy_fromfs(&(params[1]), (void *) arg, 6);
2453
2454
2455 for (i=1; i<7; i++)
2456 {
2457 params[i] = int_to_bcd(params[i]);
2458 }
2459 params[0] = 0x03;
2460 if(do_sony_cd_cmd_chk("PLAYMSF",SONY_AUDIO_PLAYBACK_CMD, params, 7, res_reg, &res_size)<0)
2461 return -EIO;
2462
2463
2464 final_pos_msf[0] = params[4];
2465 final_pos_msf[1] = params[5];
2466 final_pos_msf[2] = params[6];
2467 sony_audio_status = CDROM_AUDIO_PLAY;
2468 return 0;
2469
2470 case CDROMREADTOCHDR:
2471 {
2472 struct cdrom_tochdr *hdr;
2473 struct cdrom_tochdr loc_hdr;
2474
2475 sony_get_toc();
2476 if (!sony_toc_read)
2477 {
2478 return -EIO;
2479 }
2480
2481 hdr = (struct cdrom_tochdr *) arg;
2482 i=verify_area(VERIFY_WRITE, hdr, sizeof(*hdr));
2483 if(i<0)
2484 return i;
2485 loc_hdr.cdth_trk0 = bcd_to_int(sony_toc.first_track_num);
2486 loc_hdr.cdth_trk1 = bcd_to_int(sony_toc.last_track_num);
2487 memcpy_tofs(hdr, &loc_hdr, sizeof(*hdr));
2488 }
2489 return 0;
2490
2491 case CDROMREADTOCENTRY:
2492 {
2493 struct cdrom_tocentry *entry;
2494 struct cdrom_tocentry loc_entry;
2495 int track_idx;
2496 unsigned char *msf_val = NULL;
2497
2498 sony_get_toc();
2499 if (!sony_toc_read)
2500 {
2501 return -EIO;
2502 }
2503
2504 entry = (struct cdrom_tocentry *) arg;
2505 i=verify_area(VERIFY_READ, entry, sizeof(*entry));
2506 if(i<0)
2507 return i;
2508 i=verify_area(VERIFY_WRITE, entry, sizeof(*entry));
2509 if(i<0)
2510 return i;
2511
2512 memcpy_fromfs(&loc_entry, entry, sizeof(loc_entry));
2513
2514
2515 if (loc_entry.cdte_track == CDROM_LEADOUT)
2516 {
2517 loc_entry.cdte_adr = sony_toc.address2;
2518 loc_entry.cdte_ctrl = sony_toc.control2;
2519 msf_val = sony_toc.lead_out_start_msf;
2520 }
2521 else
2522 {
2523 track_idx = find_track(int_to_bcd(loc_entry.cdte_track));
2524 if (track_idx < 0)
2525 {
2526 return -EINVAL;
2527 }
2528
2529 loc_entry.cdte_adr = sony_toc.tracks[track_idx].address;
2530 loc_entry.cdte_ctrl = sony_toc.tracks[track_idx].control;
2531 msf_val = sony_toc.tracks[track_idx].track_start_msf;
2532 }
2533
2534
2535 if (loc_entry.cdte_format == CDROM_LBA)
2536 {
2537 loc_entry.cdte_addr.lba = msf_to_log(msf_val);
2538 }
2539 else if (loc_entry.cdte_format == CDROM_MSF)
2540 {
2541 loc_entry.cdte_addr.msf.minute = bcd_to_int(*msf_val);
2542 loc_entry.cdte_addr.msf.second = bcd_to_int(*(msf_val+1));
2543 loc_entry.cdte_addr.msf.frame = bcd_to_int(*(msf_val+2));
2544 }
2545 memcpy_tofs(entry, &loc_entry, sizeof(*entry));
2546 }
2547 return 0;
2548 break;
2549
2550 case CDROMPLAYTRKIND:
2551 {
2552 struct cdrom_ti ti;
2553 int track_idx;
2554
2555 sony_get_toc();
2556 if (!sony_toc_read)
2557 {
2558 return -EIO;
2559 }
2560
2561 i=verify_area(VERIFY_READ, (char *) arg, sizeof(ti));
2562 if(i<0)
2563 return i;
2564
2565 memcpy_fromfs(&ti, (char *) arg, sizeof(ti));
2566 if ( (ti.cdti_trk0 < sony_toc.first_track_num)
2567 || (ti.cdti_trk0 > sony_toc.last_track_num)
2568 || (ti.cdti_trk1 < ti.cdti_trk0))
2569 {
2570 return -EINVAL;
2571 }
2572
2573 track_idx = find_track(int_to_bcd(ti.cdti_trk0));
2574 if (track_idx < 0)
2575 {
2576 return -EINVAL;
2577 }
2578 params[1] = sony_toc.tracks[track_idx].track_start_msf[0];
2579 params[2] = sony_toc.tracks[track_idx].track_start_msf[1];
2580 params[3] = sony_toc.tracks[track_idx].track_start_msf[2];
2581
2582
2583
2584
2585
2586 if (ti.cdti_trk1 >= bcd_to_int(sony_toc.last_track_num))
2587 {
2588 log_to_msf(msf_to_log(sony_toc.lead_out_start_msf)-1,
2589 &(params[4]));
2590 }
2591 else
2592 {
2593 track_idx = find_track(int_to_bcd(ti.cdti_trk1+1));
2594 if (track_idx < 0)
2595 {
2596 return -EINVAL;
2597 }
2598 log_to_msf(msf_to_log(sony_toc.tracks[track_idx].track_start_msf)-1,
2599 &(params[4]));
2600 }
2601 params[0] = 0x03;
2602
2603 do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg, &res_size);
2604
2605 do_sony_cd_cmd(SONY_AUDIO_PLAYBACK_CMD, params, 7, res_reg, &res_size);
2606
2607 if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20))
2608 {
2609 printk("Params: %x %x %x %x %x %x %x\n", params[0], params[1],
2610 params[2], params[3], params[4], params[5], params[6]);
2611 printk("Sony CDROM error 0x%2.2x (CDROMPLAYTRKIND\n", res_reg[1]);
2612 return -EIO;
2613 }
2614
2615
2616 final_pos_msf[0] = params[4];
2617 final_pos_msf[1] = params[5];
2618 final_pos_msf[2] = params[6];
2619 sony_audio_status = CDROM_AUDIO_PLAY;
2620 return 0;
2621 }
2622
2623 case CDROMSUBCHNL:
2624 return sony_get_subchnl_info(arg);
2625
2626 case CDROMVOLCTRL:
2627 {
2628 struct cdrom_volctrl volctrl;
2629
2630 i=verify_area(VERIFY_READ, (char *) arg, sizeof(volctrl));
2631 if(i<0)
2632 return i;
2633
2634 memcpy_fromfs(&volctrl, (char *) arg, sizeof(volctrl));
2635 params[0] = SONY_SD_AUDIO_VOLUME;
2636 params[1] = volctrl.channel0;
2637 params[2] = volctrl.channel1;
2638 return do_sony_cd_cmd_chk("VOLCTRL",SONY_SET_DRIVE_PARAM_CMD, params, 3, res_reg, &res_size);
2639 }
2640 case CDROMEJECT:
2641 do_sony_cd_cmd(SONY_AUDIO_STOP_CMD, NULL, 0, res_reg, &res_size);
2642 do_sony_cd_cmd(SONY_SPIN_DOWN_CMD, NULL, 0, res_reg, &res_size);
2643
2644 sony_audio_status = CDROM_AUDIO_INVALID;
2645 return do_sony_cd_cmd_chk("EJECT",SONY_EJECT_CMD, NULL, 0, res_reg, &res_size);
2646
2647 case CDROMREADAUDIO:
2648
2649 {
2650 struct cdrom_read_audio ra;
2651
2652
2653 sony_get_toc();
2654 if (!sony_toc_read)
2655 {
2656 return -EIO;
2657 }
2658
2659 i=verify_area(VERIFY_READ, (char *) arg, sizeof(ra));
2660 if(i<0)
2661 return i;
2662 memcpy_fromfs(&ra, (char *) arg, sizeof(ra));
2663
2664 i=verify_area(VERIFY_WRITE, ra.buf, CD_FRAMESIZE_RAW * ra.nframes);
2665 if(i<0)
2666 return i;
2667
2668 if (ra.addr_format == CDROM_LBA)
2669 {
2670 if ( (ra.addr.lba >= sony_toc.lead_out_start_lba)
2671 || (ra.addr.lba + ra.nframes >= sony_toc.lead_out_start_lba))
2672 {
2673 return -EINVAL;
2674 }
2675 }
2676 else if (ra.addr_format == CDROM_MSF)
2677 {
2678 if ( (ra.addr.msf.minute >= 75)
2679 || (ra.addr.msf.second >= 60)
2680 || (ra.addr.msf.frame >= 75))
2681 {
2682 return -EINVAL;
2683 }
2684
2685 ra.addr.lba = ( (ra.addr.msf.minute * 4500)
2686 + (ra.addr.msf.second * 75)
2687 + ra.addr.msf.frame);
2688 if ( (ra.addr.lba >= sony_toc.lead_out_start_lba)
2689 || (ra.addr.lba + ra.nframes >= sony_toc.lead_out_start_lba))
2690 {
2691 return -EINVAL;
2692 }
2693
2694
2695
2696
2697 ra.addr.lba -= LOG_START_OFFSET;
2698 }
2699 else
2700 {
2701 return -EINVAL;
2702 }
2703
2704 return(read_audio(&ra, inode));
2705 }
2706 return 0;
2707 break;
2708
2709 case CDROMEJECT_SW:
2710 is_auto_eject = arg;
2711 set_drive_params();
2712 return 0;
2713 break;
2714
2715 default:
2716 return -EINVAL;
2717 }
2718 }
2719
2720
2721
2722
2723
2724
2725 static int
2726 scd_open(struct inode *inode,
2727 struct file *filp)
2728 {
2729 unsigned char res_reg[12];
2730 unsigned int res_size;
2731 int num_spin_ups;
2732 unsigned char params[2];
2733
2734
2735 if ((filp) && filp->f_mode & 2)
2736 return -EROFS;
2737
2738 if (!sony_spun_up)
2739 {
2740 num_spin_ups = 0;
2741
2742 respinup_on_open:
2743 do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg, &res_size);
2744
2745
2746
2747 if ((res_size < 2) || ((res_reg[0] != 0) && (res_reg[1] != 0)))
2748 {
2749 printk("Sony CDROM error 0x%2.2x (scd_open, spin up)\n", res_reg[1]);
2750 return -EIO;
2751 }
2752
2753 do_sony_cd_cmd(SONY_READ_TOC_CMD, NULL, 0, res_reg, &res_size);
2754
2755
2756
2757 if ((res_size < 2) || ((res_reg[0] != 0) && (res_reg[1] != 0)))
2758 {
2759
2760 if ((res_reg[1] == SONY_AUDIO_PLAYING_ERR) || (res_reg[1] == 0))
2761 {
2762 goto drive_spinning;
2763 }
2764
2765
2766
2767 if ( (res_reg[1] == SONY_NOT_SPIN_ERR)
2768 && (num_spin_ups < MAX_CDU31A_RETRIES))
2769 {
2770 num_spin_ups++;
2771 goto respinup_on_open;
2772 }
2773
2774 printk("Sony CDROM error 0x%2.2x (scd_open, read toc)\n", res_reg[1]);
2775 do_sony_cd_cmd(SONY_SPIN_DOWN_CMD, NULL, 0, res_reg, &res_size);
2776
2777 return -EIO;
2778 }
2779
2780 sony_get_toc();
2781 if (!sony_toc_read)
2782 {
2783 do_sony_cd_cmd(SONY_SPIN_DOWN_CMD, NULL, 0, res_reg, &res_size);
2784 return -EIO;
2785 }
2786
2787
2788
2789 if ( (sony_toc.disk_type == SONY_XA_DISK_TYPE)
2790 && (!is_double_speed))
2791 {
2792 params[0] = SONY_SD_DECODE_PARAM;
2793 params[1] = 0x07;
2794 do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
2795 params,
2796 2,
2797 res_reg,
2798 &res_size);
2799 if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20))
2800 {
2801 printk("CDU31A: Unable to set XA params: 0x%2.2x\n", res_reg[1]);
2802 }
2803 sony_xa_mode = 1;
2804 }
2805
2806 else if (sony_xa_mode)
2807 {
2808 params[0] = SONY_SD_DECODE_PARAM;
2809 params[1] = 0x0f;
2810 do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
2811 params,
2812 2,
2813 res_reg,
2814 &res_size);
2815 if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20))
2816 {
2817 printk("CDU31A: Unable to reset XA params: 0x%2.2x\n", res_reg[1]);
2818 }
2819 sony_xa_mode = 0;
2820 }
2821
2822 sony_spun_up = 1;
2823 }
2824
2825 drive_spinning:
2826
2827
2828 if (filp)
2829 {
2830 check_disk_change(inode->i_rdev);
2831 }
2832
2833 sony_usage++;
2834 MOD_INC_USE_COUNT;
2835
2836
2837 is_auto_eject = 0;
2838 set_drive_params();
2839
2840 return 0;
2841 }
2842
2843
2844
2845
2846
2847
2848 static void
2849 scd_release(struct inode *inode,
2850 struct file *filp)
2851 {
2852 unsigned char res_reg[12];
2853 unsigned int res_size;
2854
2855
2856 if (sony_usage > 0)
2857 {
2858 sony_usage--;
2859 MOD_DEC_USE_COUNT;
2860 }
2861 if (sony_usage == 0)
2862 {
2863 sync_dev(inode->i_rdev);
2864
2865
2866 is_auto_eject = 1;
2867 set_drive_params();
2868
2869 do_sony_cd_cmd(SONY_SPIN_DOWN_CMD, NULL, 0, res_reg, &res_size);
2870
2871 sony_spun_up = 0;
2872 }
2873 }
2874
2875
2876 static struct file_operations scd_fops = {
2877 NULL,
2878 block_read,
2879 block_write,
2880 NULL,
2881 NULL,
2882 scd_ioctl,
2883 NULL,
2884 scd_open,
2885 scd_release,
2886 NULL,
2887 NULL,
2888 scd_disk_change,
2889 NULL
2890 };
2891
2892
2893
2894 static const char *load_mech[] = { "caddy", "tray", "pop-up", "unknown" };
2895
2896 static void
2897 get_drive_configuration(unsigned short base_io,
2898 unsigned char res_reg[],
2899 unsigned int *res_size)
2900 {
2901 int retry_count;
2902
2903
2904
2905 cdu31a_port = base_io;
2906
2907
2908 sony_cd_cmd_reg = cdu31a_port + SONY_CMD_REG_OFFSET;
2909 sony_cd_param_reg = cdu31a_port + SONY_PARAM_REG_OFFSET;
2910 sony_cd_write_reg = cdu31a_port + SONY_WRITE_REG_OFFSET;
2911 sony_cd_control_reg = cdu31a_port + SONY_CONTROL_REG_OFFSET;
2912 sony_cd_status_reg = cdu31a_port + SONY_STATUS_REG_OFFSET;
2913 sony_cd_result_reg = cdu31a_port + SONY_RESULT_REG_OFFSET;
2914 sony_cd_read_reg = cdu31a_port + SONY_READ_REG_OFFSET;
2915 sony_cd_fifost_reg = cdu31a_port + SONY_FIFOST_REG_OFFSET;
2916
2917
2918
2919
2920
2921
2922 if (read_status_register() != 0xff)
2923 {
2924
2925
2926
2927
2928 reset_drive();
2929 retry_count = jiffies + SONY_RESET_TIMEOUT;
2930 while ((retry_count > jiffies) && (!is_attention()))
2931 {
2932 sony_sleep();
2933 }
2934
2935 #if 0
2936
2937 if (!is_attention())
2938 {
2939 res_reg[0] = 0x20;
2940 return;
2941 }
2942 #endif
2943
2944
2945
2946
2947 do_sony_cd_cmd(SONY_REQ_DRIVE_CONFIG_CMD,
2948 NULL,
2949 0,
2950 (unsigned char *) res_reg,
2951 res_size);
2952 return;
2953 }
2954
2955
2956 res_reg[0] = 0x20;
2957 }
2958
2959 #ifndef MODULE
2960
2961
2962
2963 void
2964 cdu31a_setup(char *strings,
2965 int *ints)
2966 {
2967 if (ints[0] > 0)
2968 {
2969 cdu31a_port = ints[1];
2970 }
2971 if (ints[0] > 1)
2972 {
2973 cdu31a_irq = ints[2];
2974 }
2975 if ((strings != NULL) && (*strings != '\0'))
2976 {
2977 if (strcmp(strings, "PAS") == 0)
2978 {
2979 sony_pas_init = 1;
2980 }
2981 else
2982 {
2983 printk("CDU31A: Unknown interface type: %s\n", strings);
2984 }
2985 }
2986 }
2987 #endif
2988
2989 static int cdu31a_block_size;
2990
2991
2992
2993
2994 int
2995 cdu31a_init(void)
2996 {
2997 struct s_sony_drive_config drive_config;
2998 unsigned int res_size;
2999 int i;
3000 int drive_found;
3001 int tmp_irq;
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011 if (sony_pas_init)
3012 {
3013 outb(0xbc, 0x9a01);
3014 outb(0xe2, 0x9a01);
3015 }
3016
3017 drive_found = 0;
3018
3019
3020 if (cdu31a_port == 0xffff)
3021 {
3022 }
3023 else if (cdu31a_port != 0)
3024 {
3025 tmp_irq = cdu31a_irq;
3026 cdu31a_irq = 0;
3027
3028 get_drive_configuration(cdu31a_port,
3029 drive_config.exec_status,
3030 &res_size);
3031 if ((res_size > 2) && ((drive_config.exec_status[0] & 0xf0) == 0x00))
3032 {
3033 drive_found = 1;
3034 }
3035
3036 cdu31a_irq = tmp_irq;
3037 }
3038 else
3039 {
3040 cdu31a_irq = 0;
3041 i = 0;
3042 while ( (cdu31a_addresses[i].base != 0)
3043 && (!drive_found))
3044 {
3045 if (check_region(cdu31a_addresses[i].base, 4)) {
3046 i++;
3047 continue;
3048 }
3049 get_drive_configuration(cdu31a_addresses[i].base,
3050 drive_config.exec_status,
3051 &res_size);
3052 if ((res_size > 2) && ((drive_config.exec_status[0] & 0xf0) == 0x00))
3053 {
3054 drive_found = 1;
3055 cdu31a_irq = cdu31a_addresses[i].int_num;
3056 }
3057 else
3058 {
3059 i++;
3060 }
3061 }
3062 }
3063
3064 if (drive_found)
3065 {
3066 request_region(cdu31a_port, 4,"cdu31a");
3067
3068 if (register_blkdev(MAJOR_NR,"cdu31a",&scd_fops))
3069 {
3070 printk("Unable to get major %d for CDU-31a\n", MAJOR_NR);
3071 return -EIO;
3072 }
3073
3074 if (SONY_HWC_DOUBLE_SPEED(drive_config))
3075 {
3076 is_double_speed = 1;
3077 }
3078
3079 tmp_irq = cdu31a_irq;
3080 cdu31a_irq = 0;
3081
3082 set_drive_params();
3083
3084 cdu31a_irq = tmp_irq;
3085
3086 if (cdu31a_irq > 0)
3087 {
3088 if (request_irq(cdu31a_irq, cdu31a_interrupt, SA_INTERRUPT, "cdu31a", NULL))
3089 {
3090 printk("Unable to grab IRQ%d for the CDU31A driver\n", cdu31a_irq);
3091 cdu31a_irq = 0;
3092 }
3093 }
3094
3095 printk("Sony I/F CDROM : %8.8s %16.16s %8.8s\n",
3096 drive_config.vendor_id,
3097 drive_config.product_id,
3098 drive_config.product_rev_level);
3099 printk(" Capabilities: %s",
3100 load_mech[SONY_HWC_GET_LOAD_MECH(drive_config)]);
3101 if (SONY_HWC_AUDIO_PLAYBACK(drive_config))
3102 {
3103 printk(", audio");
3104 }
3105 if (SONY_HWC_EJECT(drive_config))
3106 {
3107 printk(", eject");
3108 }
3109 if (SONY_HWC_LED_SUPPORT(drive_config))
3110 {
3111 printk(", LED");
3112 }
3113 if (SONY_HWC_ELECTRIC_VOLUME(drive_config))
3114 {
3115 printk(", elec. Vol");
3116 }
3117 if (SONY_HWC_ELECTRIC_VOLUME_CTL(drive_config))
3118 {
3119 printk(", sep. Vol");
3120 }
3121 if (is_double_speed)
3122 {
3123 printk(", double speed");
3124 }
3125 if (cdu31a_irq > 0)
3126 {
3127 printk(", irq %d", cdu31a_irq);
3128 }
3129 printk("\n");
3130
3131 blk_dev[MAJOR_NR].request_fn = DEVICE_REQUEST;
3132 read_ahead[MAJOR_NR] = CDU31A_READAHEAD;
3133 cdu31a_block_size = 1024;
3134
3135 blksize_size[MAJOR_NR] = &cdu31a_block_size;
3136
3137 cdu31a_abort_timer.next = NULL;
3138 cdu31a_abort_timer.prev = NULL;
3139 cdu31a_abort_timer.function = handle_abort_timeout;
3140 }
3141
3142
3143 disk_changed = 1;
3144
3145 if (drive_found)
3146 {
3147 return(0);
3148 }
3149 else
3150 {
3151 return -EIO;
3152 }
3153 }
3154
3155 #ifdef MODULE
3156
3157 int
3158 init_module(void)
3159 {
3160 return cdu31a_init();
3161 }
3162
3163 void
3164 cleanup_module(void)
3165 {
3166 if ((unregister_blkdev(MAJOR_NR, "cdu31a") == -EINVAL))
3167 {
3168 printk("Can't unregister cdu31a\n");
3169 return;
3170 }
3171 release_region(cdu31a_port,4);
3172 printk("cdu31a module released.\n");
3173 }
3174 #endif MODULE