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, 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 struct cdrom_subchnl schi;
1963
1964
1965
1966 while (handle_sony_cd_attention())
1967 ;
1968
1969 sony_get_toc();
1970 if (!sony_toc_read)
1971 {
1972 return -EIO;
1973 }
1974
1975 verify_area(VERIFY_READ, (char *) arg, sizeof(schi));
1976 verify_area(VERIFY_WRITE, (char *) arg, sizeof(schi));
1977
1978 memcpy_fromfs(&schi, (char *) arg, sizeof(schi));
1979
1980 switch (sony_audio_status)
1981 {
1982 case CDROM_AUDIO_PLAY:
1983 if (read_subcode() < 0)
1984 {
1985 return -EIO;
1986 }
1987 break;
1988
1989 case CDROM_AUDIO_PAUSED:
1990 case CDROM_AUDIO_COMPLETED:
1991 break;
1992
1993 case CDROM_AUDIO_NO_STATUS:
1994 schi.cdsc_audiostatus = sony_audio_status;
1995 memcpy_tofs((char *) arg, &schi, sizeof(schi));
1996 return 0;
1997 break;
1998
1999 case CDROM_AUDIO_INVALID:
2000 case CDROM_AUDIO_ERROR:
2001 default:
2002 return -EIO;
2003 }
2004
2005 schi.cdsc_audiostatus = sony_audio_status;
2006 schi.cdsc_adr = last_sony_subcode.address;
2007 schi.cdsc_ctrl = last_sony_subcode.control;
2008 schi.cdsc_trk = bcd_to_int(last_sony_subcode.track_num);
2009 schi.cdsc_ind = bcd_to_int(last_sony_subcode.index_num);
2010 if (schi.cdsc_format == CDROM_MSF)
2011 {
2012 schi.cdsc_absaddr.msf.minute = bcd_to_int(last_sony_subcode.abs_msf[0]);
2013 schi.cdsc_absaddr.msf.second = bcd_to_int(last_sony_subcode.abs_msf[1]);
2014 schi.cdsc_absaddr.msf.frame = bcd_to_int(last_sony_subcode.abs_msf[2]);
2015
2016 schi.cdsc_reladdr.msf.minute = bcd_to_int(last_sony_subcode.rel_msf[0]);
2017 schi.cdsc_reladdr.msf.second = bcd_to_int(last_sony_subcode.rel_msf[1]);
2018 schi.cdsc_reladdr.msf.frame = bcd_to_int(last_sony_subcode.rel_msf[2]);
2019 }
2020 else if (schi.cdsc_format == CDROM_LBA)
2021 {
2022 schi.cdsc_absaddr.lba = msf_to_log(last_sony_subcode.abs_msf);
2023 schi.cdsc_reladdr.lba = msf_to_log(last_sony_subcode.rel_msf);
2024 }
2025
2026 memcpy_tofs((char *) arg, &schi, sizeof(schi));
2027 return 0;
2028 }
2029
2030
2031
2032
2033
2034 static void
2035 read_audio_data(char *buffer,
2036 unsigned char res_reg[],
2037 int *res_size)
2038 {
2039 unsigned int retry_count;
2040 int result_read;
2041
2042
2043 res_reg[0] = 0;
2044 res_reg[1] = 0;
2045 *res_size = 0;
2046 result_read = 0;
2047
2048
2049 retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
2050 continue_read_audio_wait:
2051 while ( (retry_count > jiffies)
2052 && !(is_data_ready())
2053 && !(is_result_ready() || result_read))
2054 {
2055 while (handle_sony_cd_attention())
2056 ;
2057
2058 sony_sleep();
2059 }
2060 if (!(is_data_ready()))
2061 {
2062 if (is_result_ready() && !result_read)
2063 {
2064 get_result(res_reg, res_size);
2065
2066
2067 if ((res_reg[0] & 0xf0) == 0x50)
2068 {
2069 result_read = 1;
2070 goto continue_read_audio_wait;
2071 }
2072
2073 else if ((res_reg[0] & 0xf0) != 0x20)
2074 {
2075 printk("CDU31A: Got result that should have been error: %d\n",
2076 res_reg[0]);
2077 res_reg[0] = 0x20;
2078 res_reg[1] = SONY_BAD_DATA_ERR;
2079 *res_size = 2;
2080 }
2081 abort_read();
2082 }
2083 else
2084 {
2085 #if DEBUG
2086 printk("CDU31A timeout out %d\n", __LINE__);
2087 #endif
2088 res_reg[0] = 0x20;
2089 res_reg[1] = SONY_TIMEOUT_OP_ERR;
2090 *res_size = 2;
2091 abort_read();
2092 }
2093 }
2094 else
2095 {
2096 clear_data_ready();
2097
2098
2099 if (sony_raw_data_mode)
2100 {
2101 insb(sony_cd_read_reg, buffer + CD_XA_HEAD, CD_FRAMESIZE_XA);
2102 }
2103 else
2104 {
2105
2106 insb(sony_cd_read_reg, buffer, CD_FRAMESIZE_RAW);
2107 }
2108
2109
2110 if (!result_read)
2111 {
2112
2113 retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
2114 while ((retry_count > jiffies) && !(is_result_ready()))
2115 {
2116 while (handle_sony_cd_attention())
2117 ;
2118
2119 sony_sleep();
2120 }
2121
2122 if (!is_result_ready())
2123 {
2124 #if DEBUG
2125 printk("CDU31A timeout out %d\n", __LINE__);
2126 #endif
2127 res_reg[0] = 0x20;
2128 res_reg[1] = SONY_TIMEOUT_OP_ERR;
2129 *res_size = 2;
2130 abort_read();
2131 return;
2132 }
2133 else
2134 {
2135 get_result(res_reg, res_size);
2136 }
2137 }
2138
2139 if ((res_reg[0] & 0xf0) == 0x50)
2140 {
2141 if ( (res_reg[0] == SONY_NO_CIRC_ERR_BLK_STAT)
2142 || (res_reg[0] == SONY_NO_LECC_ERR_BLK_STAT)
2143 || (res_reg[0] == SONY_RECOV_LECC_ERR_BLK_STAT)
2144 || (res_reg[0] == SONY_NO_ERR_DETECTION_STAT))
2145 {
2146
2147 }
2148 else
2149 {
2150 printk("CDU31A: Data block error: 0x%x\n", res_reg[0]);
2151 res_reg[0] = 0x20;
2152 res_reg[1] = SONY_BAD_DATA_ERR;
2153 *res_size = 2;
2154 }
2155 }
2156 else if ((res_reg[0] & 0xf0) != 0x20)
2157 {
2158
2159
2160 printk("CDU31A: Invalid block status: 0x%x\n", res_reg[0]);
2161 restart_on_error();
2162 res_reg[0] = 0x20;
2163 res_reg[1] = SONY_BAD_DATA_ERR;
2164 *res_size = 2;
2165 }
2166 }
2167 }
2168
2169
2170
2171 static int
2172 read_audio(struct cdrom_read_audio *ra,
2173 struct inode *inode)
2174 {
2175 int retval;
2176 unsigned char params[2];
2177 unsigned char res_reg[12];
2178 unsigned int res_size;
2179 unsigned int cframe;
2180 unsigned long flags;
2181
2182
2183
2184
2185
2186 save_flags(flags);
2187 cli();
2188 while (sony_inuse)
2189 {
2190 interruptible_sleep_on(&sony_wait);
2191 if (current->signal & ~current->blocked)
2192 {
2193 restore_flags(flags);
2194 return -EAGAIN;
2195 }
2196 }
2197 sony_inuse = 1;
2198 has_cd_task = current;
2199 restore_flags(flags);
2200
2201 if (!sony_spun_up)
2202 {
2203 scd_open (inode, NULL);
2204 }
2205
2206
2207 params[0] = SONY_SD_DECODE_PARAM;
2208 params[1] = 0x06 | sony_raw_data_mode;
2209 do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
2210 params,
2211 2,
2212 res_reg,
2213 &res_size);
2214 if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20))
2215 {
2216 printk("CDU31A: Unable to set decode params: 0x%2.2x\n", res_reg[1]);
2217 return -EIO;
2218 }
2219
2220
2221
2222
2223
2224 retval = 0;
2225
2226 if (start_request(ra->addr.lba, ra->nframes, 1))
2227 {
2228 retval = -EIO;
2229 goto exit_read_audio;
2230 }
2231
2232
2233 cframe = 0;
2234 while (cframe < ra->nframes)
2235 {
2236 read_audio_data(readahead_buffer, res_reg, &res_size);
2237 if ((res_reg[0] & 0xf0) == 0x20)
2238 {
2239 if (res_reg[1] == SONY_BAD_DATA_ERR)
2240 {
2241 printk("CDU31A: Data error on audio sector %d\n",
2242 ra->addr.lba + cframe);
2243 }
2244 else if (res_reg[1] == SONY_ILL_TRACK_R_ERR)
2245 {
2246
2247 sony_raw_data_mode = (sony_raw_data_mode) ? 0 : 1;
2248
2249
2250 params[0] = SONY_SD_DECODE_PARAM;
2251 params[1] = 0x06 | sony_raw_data_mode;
2252 do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
2253 params,
2254 2,
2255 res_reg,
2256 &res_size);
2257 if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20))
2258 {
2259 printk("CDU31A: Unable to set decode params: 0x%2.2x\n", res_reg[1]);
2260 retval = -EIO;
2261 goto exit_read_audio;
2262 }
2263
2264
2265 if (start_request(ra->addr.lba + cframe, ra->nframes - cframe, 1))
2266 {
2267 retval = -EIO;
2268 goto exit_read_audio;
2269 }
2270
2271
2272
2273
2274 read_audio_data(readahead_buffer, res_reg, &res_size);
2275 if ((res_reg[0] & 0xf0) == 0x20)
2276 {
2277 if (res_reg[1] == SONY_BAD_DATA_ERR)
2278 {
2279 printk("CDU31A: Data error on audio sector %d\n",
2280 ra->addr.lba + cframe);
2281 }
2282 else
2283 {
2284 printk("CDU31A: Error reading audio data on sector %d: 0x%x\n",
2285 ra->addr.lba + cframe,
2286 res_reg[1]);
2287 retval = -EIO;
2288 goto exit_read_audio;
2289 }
2290 }
2291 else
2292 {
2293 memcpy_tofs((char *) (ra->buf + (CD_FRAMESIZE_RAW * cframe)),
2294 (char *) readahead_buffer,
2295 CD_FRAMESIZE_RAW);
2296 }
2297 }
2298 else
2299 {
2300 printk("CDU31A: Error reading audio data on sector %d: 0x%x\n",
2301 ra->addr.lba + cframe,
2302 res_reg[1]);
2303 retval = -EIO;
2304 goto exit_read_audio;
2305 }
2306 }
2307 else
2308 {
2309 memcpy_tofs((char *) (ra->buf + (CD_FRAMESIZE_RAW * cframe)),
2310 (char *) readahead_buffer,
2311 CD_FRAMESIZE_RAW);
2312 }
2313
2314 cframe++;
2315 }
2316
2317 get_result(res_reg, &res_size);
2318 if ((res_reg[0] & 0xf0) == 0x20)
2319 {
2320 printk("CDU31A: Error return from audio read: 0x%x\n",
2321 res_reg[1]);
2322 retval = -EIO;
2323 goto exit_read_audio;
2324 }
2325
2326 exit_read_audio:
2327
2328
2329 params[0] = SONY_SD_DECODE_PARAM;
2330 if (!sony_xa_mode)
2331 {
2332 params[1] = 0x0f;
2333 }
2334 else
2335 {
2336 params[1] = 0x07;
2337 }
2338 do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
2339 params,
2340 2,
2341 res_reg,
2342 &res_size);
2343 if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20))
2344 {
2345 printk("CDU31A: Unable to reset decode params: 0x%2.2x\n", res_reg[1]);
2346 return -EIO;
2347 }
2348
2349 has_cd_task = NULL;
2350 sony_inuse = 0;
2351 wake_up_interruptible(&sony_wait);
2352
2353 return(retval);
2354 }
2355
2356 static int
2357 do_sony_cd_cmd_chk(const char *name,
2358 unsigned char cmd,
2359 unsigned char *params,
2360 unsigned int num_params,
2361 unsigned char *result_buffer,
2362 unsigned int *result_size)
2363 {
2364 do_sony_cd_cmd(cmd, params, num_params, result_buffer, result_size);
2365 if ((*result_size < 2) || ((result_buffer[0] & 0xf0) == 0x20))
2366 {
2367 printk("Sony CDROM error 0x%2.2x (CDROM%s)\n", result_buffer[1], name);
2368 return -EIO;
2369 }
2370 return 0;
2371 }
2372
2373
2374
2375
2376 static int scd_ioctl(struct inode *inode,
2377 struct file *file,
2378 unsigned int cmd,
2379 unsigned long arg)
2380 {
2381 unsigned char res_reg[12];
2382 unsigned int res_size;
2383 unsigned char params[7];
2384 int i;
2385
2386
2387 if (!inode)
2388 {
2389 return -EINVAL;
2390 }
2391
2392 switch (cmd)
2393 {
2394 case CDROMSTART:
2395 return do_sony_cd_cmd_chk("START",SONY_SPIN_UP_CMD, NULL, 0, res_reg, &res_size);
2396 return 0;
2397 break;
2398
2399 case CDROMSTOP:
2400 do_sony_cd_cmd(SONY_AUDIO_STOP_CMD, NULL, 0, res_reg, &res_size);
2401
2402
2403
2404
2405
2406 sony_audio_status = CDROM_AUDIO_NO_STATUS;
2407 return do_sony_cd_cmd_chk("STOP",SONY_SPIN_DOWN_CMD, NULL, 0, res_reg, &res_size);
2408
2409 case CDROMPAUSE:
2410 if(do_sony_cd_cmd_chk("PAUSE", SONY_AUDIO_STOP_CMD, NULL, 0, res_reg, &res_size))
2411 return -EIO;
2412
2413 if (read_subcode() < 0)
2414 {
2415 return -EIO;
2416 }
2417 cur_pos_msf[0] = last_sony_subcode.abs_msf[0];
2418 cur_pos_msf[1] = last_sony_subcode.abs_msf[1];
2419 cur_pos_msf[2] = last_sony_subcode.abs_msf[2];
2420 sony_audio_status = CDROM_AUDIO_PAUSED;
2421 return 0;
2422 break;
2423
2424 case CDROMRESUME:
2425 if (sony_audio_status != CDROM_AUDIO_PAUSED)
2426 {
2427 return -EINVAL;
2428 }
2429
2430 do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg, &res_size);
2431
2432
2433 params[1] = cur_pos_msf[0];
2434 params[2] = cur_pos_msf[1];
2435 params[3] = cur_pos_msf[2];
2436 params[4] = final_pos_msf[0];
2437 params[5] = final_pos_msf[1];
2438 params[6] = final_pos_msf[2];
2439 params[0] = 0x03;
2440 if(do_sony_cd_cmd_chk("RESUME",SONY_AUDIO_PLAYBACK_CMD, params, 7, res_reg, &res_size)<0)
2441 return -EIO;
2442 sony_audio_status = CDROM_AUDIO_PLAY;
2443 return 0;
2444
2445 case CDROMPLAYMSF:
2446 i=verify_area(VERIFY_READ, (char *) arg, 6);
2447 if(i)
2448 return i;
2449 do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg, &res_size);
2450 memcpy_fromfs(&(params[1]), (void *) arg, 6);
2451
2452
2453 for (i=1; i<7; i++)
2454 {
2455 params[i] = int_to_bcd(params[i]);
2456 }
2457 params[0] = 0x03;
2458 if(do_sony_cd_cmd_chk("PLAYMSF",SONY_AUDIO_PLAYBACK_CMD, params, 7, res_reg, &res_size)<0)
2459 return -EIO;
2460
2461
2462 final_pos_msf[0] = params[4];
2463 final_pos_msf[1] = params[5];
2464 final_pos_msf[2] = params[6];
2465 sony_audio_status = CDROM_AUDIO_PLAY;
2466 return 0;
2467
2468 case CDROMREADTOCHDR:
2469 {
2470 struct cdrom_tochdr *hdr;
2471 struct cdrom_tochdr loc_hdr;
2472
2473 sony_get_toc();
2474 if (!sony_toc_read)
2475 {
2476 return -EIO;
2477 }
2478
2479 hdr = (struct cdrom_tochdr *) arg;
2480 i=verify_area(VERIFY_WRITE, hdr, sizeof(*hdr));
2481 if(i<0)
2482 return i;
2483 loc_hdr.cdth_trk0 = bcd_to_int(sony_toc.first_track_num);
2484 loc_hdr.cdth_trk1 = bcd_to_int(sony_toc.last_track_num);
2485 memcpy_tofs(hdr, &loc_hdr, sizeof(*hdr));
2486 }
2487 return 0;
2488
2489 case CDROMREADTOCENTRY:
2490 {
2491 struct cdrom_tocentry *entry;
2492 struct cdrom_tocentry loc_entry;
2493 int track_idx;
2494 unsigned char *msf_val = NULL;
2495
2496 sony_get_toc();
2497 if (!sony_toc_read)
2498 {
2499 return -EIO;
2500 }
2501
2502 entry = (struct cdrom_tocentry *) arg;
2503 i=verify_area(VERIFY_READ, entry, sizeof(*entry));
2504 if(i<0)
2505 return i;
2506 i=verify_area(VERIFY_WRITE, entry, sizeof(*entry));
2507 if(i<0)
2508 return i;
2509
2510 memcpy_fromfs(&loc_entry, entry, sizeof(loc_entry));
2511
2512
2513 if (loc_entry.cdte_track == CDROM_LEADOUT)
2514 {
2515 loc_entry.cdte_adr = sony_toc.address2;
2516 loc_entry.cdte_ctrl = sony_toc.control2;
2517 msf_val = sony_toc.lead_out_start_msf;
2518 }
2519 else
2520 {
2521 track_idx = find_track(int_to_bcd(loc_entry.cdte_track));
2522 if (track_idx < 0)
2523 {
2524 return -EINVAL;
2525 }
2526
2527 loc_entry.cdte_adr = sony_toc.tracks[track_idx].address;
2528 loc_entry.cdte_ctrl = sony_toc.tracks[track_idx].control;
2529 msf_val = sony_toc.tracks[track_idx].track_start_msf;
2530 }
2531
2532
2533 if (loc_entry.cdte_format == CDROM_LBA)
2534 {
2535 loc_entry.cdte_addr.lba = msf_to_log(msf_val);
2536 }
2537 else if (loc_entry.cdte_format == CDROM_MSF)
2538 {
2539 loc_entry.cdte_addr.msf.minute = bcd_to_int(*msf_val);
2540 loc_entry.cdte_addr.msf.second = bcd_to_int(*(msf_val+1));
2541 loc_entry.cdte_addr.msf.frame = bcd_to_int(*(msf_val+2));
2542 }
2543 memcpy_tofs(entry, &loc_entry, sizeof(*entry));
2544 }
2545 return 0;
2546 break;
2547
2548 case CDROMPLAYTRKIND:
2549 {
2550 struct cdrom_ti ti;
2551 int track_idx;
2552
2553 sony_get_toc();
2554 if (!sony_toc_read)
2555 {
2556 return -EIO;
2557 }
2558
2559 i=verify_area(VERIFY_READ, (char *) arg, sizeof(ti));
2560 if(i<0)
2561 return i;
2562
2563 memcpy_fromfs(&ti, (char *) arg, sizeof(ti));
2564 if ( (ti.cdti_trk0 < sony_toc.first_track_num)
2565 || (ti.cdti_trk0 > sony_toc.last_track_num)
2566 || (ti.cdti_trk1 < ti.cdti_trk0))
2567 {
2568 return -EINVAL;
2569 }
2570
2571 track_idx = find_track(int_to_bcd(ti.cdti_trk0));
2572 if (track_idx < 0)
2573 {
2574 return -EINVAL;
2575 }
2576 params[1] = sony_toc.tracks[track_idx].track_start_msf[0];
2577 params[2] = sony_toc.tracks[track_idx].track_start_msf[1];
2578 params[3] = sony_toc.tracks[track_idx].track_start_msf[2];
2579
2580
2581
2582
2583
2584 if (ti.cdti_trk1 >= bcd_to_int(sony_toc.last_track_num))
2585 {
2586 log_to_msf(msf_to_log(sony_toc.lead_out_start_msf)-1,
2587 &(params[4]));
2588 }
2589 else
2590 {
2591 track_idx = find_track(int_to_bcd(ti.cdti_trk1+1));
2592 if (track_idx < 0)
2593 {
2594 return -EINVAL;
2595 }
2596 log_to_msf(msf_to_log(sony_toc.tracks[track_idx].track_start_msf)-1,
2597 &(params[4]));
2598 }
2599 params[0] = 0x03;
2600
2601 do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg, &res_size);
2602
2603 do_sony_cd_cmd(SONY_AUDIO_PLAYBACK_CMD, params, 7, res_reg, &res_size);
2604
2605 if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20))
2606 {
2607 printk("Params: %x %x %x %x %x %x %x\n", params[0], params[1],
2608 params[2], params[3], params[4], params[5], params[6]);
2609 printk("Sony CDROM error 0x%2.2x (CDROMPLAYTRKIND\n", res_reg[1]);
2610 return -EIO;
2611 }
2612
2613
2614 final_pos_msf[0] = params[4];
2615 final_pos_msf[1] = params[5];
2616 final_pos_msf[2] = params[6];
2617 sony_audio_status = CDROM_AUDIO_PLAY;
2618 return 0;
2619 }
2620
2621 case CDROMSUBCHNL:
2622 return sony_get_subchnl_info(arg);
2623
2624 case CDROMVOLCTRL:
2625 {
2626 struct cdrom_volctrl volctrl;
2627
2628 i=verify_area(VERIFY_READ, (char *) arg, sizeof(volctrl));
2629 if(i<0)
2630 return i;
2631
2632 memcpy_fromfs(&volctrl, (char *) arg, sizeof(volctrl));
2633 params[0] = SONY_SD_AUDIO_VOLUME;
2634 params[1] = volctrl.channel0;
2635 params[2] = volctrl.channel1;
2636 return do_sony_cd_cmd_chk("VOLCTRL",SONY_SET_DRIVE_PARAM_CMD, params, 3, res_reg, &res_size);
2637 }
2638 case CDROMEJECT:
2639 do_sony_cd_cmd(SONY_AUDIO_STOP_CMD, NULL, 0, res_reg, &res_size);
2640 do_sony_cd_cmd(SONY_SPIN_DOWN_CMD, NULL, 0, res_reg, &res_size);
2641
2642 sony_audio_status = CDROM_AUDIO_INVALID;
2643 return do_sony_cd_cmd_chk("EJECT",SONY_EJECT_CMD, NULL, 0, res_reg, &res_size);
2644
2645 case CDROMREADAUDIO:
2646
2647 {
2648 struct cdrom_read_audio ra;
2649
2650
2651 sony_get_toc();
2652 if (!sony_toc_read)
2653 {
2654 return -EIO;
2655 }
2656
2657 i=verify_area(VERIFY_READ, (char *) arg, sizeof(ra));
2658 if(i<0)
2659 return i;
2660 memcpy_fromfs(&ra, (char *) arg, sizeof(ra));
2661
2662 i=verify_area(VERIFY_WRITE, ra.buf, CD_FRAMESIZE_RAW * ra.nframes);
2663 if(i<0)
2664 return i;
2665
2666 if (ra.addr_format == CDROM_LBA)
2667 {
2668 if ( (ra.addr.lba >= sony_toc.lead_out_start_lba)
2669 || (ra.addr.lba + ra.nframes >= sony_toc.lead_out_start_lba))
2670 {
2671 return -EINVAL;
2672 }
2673 }
2674 else if (ra.addr_format == CDROM_MSF)
2675 {
2676 if ( (ra.addr.msf.minute >= 75)
2677 || (ra.addr.msf.second >= 60)
2678 || (ra.addr.msf.frame >= 75))
2679 {
2680 return -EINVAL;
2681 }
2682
2683 ra.addr.lba = ( (ra.addr.msf.minute * 4500)
2684 + (ra.addr.msf.second * 75)
2685 + ra.addr.msf.frame);
2686 if ( (ra.addr.lba >= sony_toc.lead_out_start_lba)
2687 || (ra.addr.lba + ra.nframes >= sony_toc.lead_out_start_lba))
2688 {
2689 return -EINVAL;
2690 }
2691
2692
2693
2694
2695 ra.addr.lba -= LOG_START_OFFSET;
2696 }
2697 else
2698 {
2699 return -EINVAL;
2700 }
2701
2702 return(read_audio(&ra, inode));
2703 }
2704 return 0;
2705 break;
2706
2707 case CDROMEJECT_SW:
2708 is_auto_eject = arg;
2709 set_drive_params();
2710 return 0;
2711 break;
2712
2713 default:
2714 return -EINVAL;
2715 }
2716 }
2717
2718
2719
2720
2721
2722
2723 static int
2724 scd_open(struct inode *inode,
2725 struct file *filp)
2726 {
2727 unsigned char res_reg[12];
2728 unsigned int res_size;
2729 int num_spin_ups;
2730 unsigned char params[2];
2731
2732
2733 if ((filp) && filp->f_mode & 2)
2734 return -EROFS;
2735
2736 if (!sony_spun_up)
2737 {
2738 num_spin_ups = 0;
2739
2740 respinup_on_open:
2741 do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg, &res_size);
2742
2743
2744
2745 if ((res_size < 2) || ((res_reg[0] != 0) && (res_reg[1] != 0)))
2746 {
2747 printk("Sony CDROM error 0x%2.2x (scd_open, spin up)\n", res_reg[1]);
2748 return -EIO;
2749 }
2750
2751 do_sony_cd_cmd(SONY_READ_TOC_CMD, NULL, 0, res_reg, &res_size);
2752
2753
2754
2755 if ((res_size < 2) || ((res_reg[0] != 0) && (res_reg[1] != 0)))
2756 {
2757
2758 if ((res_reg[1] == SONY_AUDIO_PLAYING_ERR) || (res_reg[1] == 0))
2759 {
2760 goto drive_spinning;
2761 }
2762
2763
2764
2765 if ( (res_reg[1] == SONY_NOT_SPIN_ERR)
2766 && (num_spin_ups < MAX_CDU31A_RETRIES))
2767 {
2768 num_spin_ups++;
2769 goto respinup_on_open;
2770 }
2771
2772 printk("Sony CDROM error 0x%2.2x (scd_open, read toc)\n", res_reg[1]);
2773 do_sony_cd_cmd(SONY_SPIN_DOWN_CMD, NULL, 0, res_reg, &res_size);
2774
2775 return -EIO;
2776 }
2777
2778 sony_get_toc();
2779 if (!sony_toc_read)
2780 {
2781 do_sony_cd_cmd(SONY_SPIN_DOWN_CMD, NULL, 0, res_reg, &res_size);
2782 return -EIO;
2783 }
2784
2785
2786
2787 if ( (sony_toc.disk_type == SONY_XA_DISK_TYPE)
2788 && (!is_double_speed))
2789 {
2790 params[0] = SONY_SD_DECODE_PARAM;
2791 params[1] = 0x07;
2792 do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
2793 params,
2794 2,
2795 res_reg,
2796 &res_size);
2797 if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20))
2798 {
2799 printk("CDU31A: Unable to set XA params: 0x%2.2x\n", res_reg[1]);
2800 }
2801 sony_xa_mode = 1;
2802 }
2803
2804 else if (sony_xa_mode)
2805 {
2806 params[0] = SONY_SD_DECODE_PARAM;
2807 params[1] = 0x0f;
2808 do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
2809 params,
2810 2,
2811 res_reg,
2812 &res_size);
2813 if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20))
2814 {
2815 printk("CDU31A: Unable to reset XA params: 0x%2.2x\n", res_reg[1]);
2816 }
2817 sony_xa_mode = 0;
2818 }
2819
2820 sony_spun_up = 1;
2821 }
2822
2823 drive_spinning:
2824
2825
2826 if (filp)
2827 {
2828 check_disk_change(inode->i_rdev);
2829 }
2830
2831 sony_usage++;
2832 MOD_INC_USE_COUNT;
2833
2834
2835 is_auto_eject = 0;
2836 set_drive_params();
2837
2838 return 0;
2839 }
2840
2841
2842
2843
2844
2845
2846 static void
2847 scd_release(struct inode *inode,
2848 struct file *filp)
2849 {
2850 unsigned char res_reg[12];
2851 unsigned int res_size;
2852
2853
2854 if (sony_usage > 0)
2855 {
2856 sony_usage--;
2857 MOD_DEC_USE_COUNT;
2858 }
2859 if (sony_usage == 0)
2860 {
2861 sync_dev(inode->i_rdev);
2862
2863
2864 is_auto_eject = 1;
2865 set_drive_params();
2866
2867 do_sony_cd_cmd(SONY_SPIN_DOWN_CMD, NULL, 0, res_reg, &res_size);
2868
2869 sony_spun_up = 0;
2870 }
2871 }
2872
2873
2874 static struct file_operations scd_fops = {
2875 NULL,
2876 block_read,
2877 block_write,
2878 NULL,
2879 NULL,
2880 scd_ioctl,
2881 NULL,
2882 scd_open,
2883 scd_release,
2884 NULL,
2885 NULL,
2886 scd_disk_change,
2887 NULL
2888 };
2889
2890
2891
2892 static const char *load_mech[] = { "caddy", "tray", "pop-up", "unknown" };
2893
2894 static void
2895 get_drive_configuration(unsigned short base_io,
2896 unsigned char res_reg[],
2897 unsigned int *res_size)
2898 {
2899 int retry_count;
2900
2901
2902
2903 cdu31a_port = base_io;
2904
2905
2906 sony_cd_cmd_reg = cdu31a_port + SONY_CMD_REG_OFFSET;
2907 sony_cd_param_reg = cdu31a_port + SONY_PARAM_REG_OFFSET;
2908 sony_cd_write_reg = cdu31a_port + SONY_WRITE_REG_OFFSET;
2909 sony_cd_control_reg = cdu31a_port + SONY_CONTROL_REG_OFFSET;
2910 sony_cd_status_reg = cdu31a_port + SONY_STATUS_REG_OFFSET;
2911 sony_cd_result_reg = cdu31a_port + SONY_RESULT_REG_OFFSET;
2912 sony_cd_read_reg = cdu31a_port + SONY_READ_REG_OFFSET;
2913 sony_cd_fifost_reg = cdu31a_port + SONY_FIFOST_REG_OFFSET;
2914
2915
2916
2917
2918
2919
2920 if (read_status_register() != 0xff)
2921 {
2922
2923
2924
2925
2926 reset_drive();
2927 retry_count = jiffies + SONY_RESET_TIMEOUT;
2928 while ((retry_count > jiffies) && (!is_attention()))
2929 {
2930 sony_sleep();
2931 }
2932
2933 #if 0
2934
2935 if (!is_attention())
2936 {
2937 res_reg[0] = 0x20;
2938 return;
2939 }
2940 #endif
2941
2942
2943
2944
2945 do_sony_cd_cmd(SONY_REQ_DRIVE_CONFIG_CMD,
2946 NULL,
2947 0,
2948 (unsigned char *) res_reg,
2949 res_size);
2950 return;
2951 }
2952
2953
2954 res_reg[0] = 0x20;
2955 }
2956
2957 #ifndef MODULE
2958
2959
2960
2961 void
2962 cdu31a_setup(char *strings,
2963 int *ints)
2964 {
2965 if (ints[0] > 0)
2966 {
2967 cdu31a_port = ints[1];
2968 }
2969 if (ints[0] > 1)
2970 {
2971 cdu31a_irq = ints[2];
2972 }
2973 if ((strings != NULL) && (*strings != '\0'))
2974 {
2975 if (strcmp(strings, "PAS") == 0)
2976 {
2977 sony_pas_init = 1;
2978 }
2979 else
2980 {
2981 printk("CDU31A: Unknown interface type: %s\n", strings);
2982 }
2983 }
2984 }
2985 #endif
2986
2987 static int cdu31a_block_size;
2988
2989
2990
2991
2992 int
2993 cdu31a_init(void)
2994 {
2995 struct s_sony_drive_config drive_config;
2996 unsigned int res_size;
2997 int i;
2998 int drive_found;
2999 int tmp_irq;
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009 if (sony_pas_init)
3010 {
3011 outb(0xbc, 0x9a01);
3012 outb(0xe2, 0x9a01);
3013 }
3014
3015 drive_found = 0;
3016
3017
3018 if (cdu31a_port == 0xffff)
3019 {
3020 }
3021 else if (cdu31a_port != 0)
3022 {
3023 tmp_irq = cdu31a_irq;
3024 cdu31a_irq = 0;
3025
3026 get_drive_configuration(cdu31a_port,
3027 drive_config.exec_status,
3028 &res_size);
3029 if ((res_size > 2) && ((drive_config.exec_status[0] & 0xf0) == 0x00))
3030 {
3031 drive_found = 1;
3032 }
3033
3034 cdu31a_irq = tmp_irq;
3035 }
3036 else
3037 {
3038 cdu31a_irq = 0;
3039 i = 0;
3040 while ( (cdu31a_addresses[i].base != 0)
3041 && (!drive_found))
3042 {
3043 if (check_region(cdu31a_addresses[i].base, 4)) {
3044 i++;
3045 continue;
3046 }
3047 get_drive_configuration(cdu31a_addresses[i].base,
3048 drive_config.exec_status,
3049 &res_size);
3050 if ((res_size > 2) && ((drive_config.exec_status[0] & 0xf0) == 0x00))
3051 {
3052 drive_found = 1;
3053 cdu31a_irq = cdu31a_addresses[i].int_num;
3054 }
3055 else
3056 {
3057 i++;
3058 }
3059 }
3060 }
3061
3062 if (drive_found)
3063 {
3064 request_region(cdu31a_port, 4,"cdu31a");
3065
3066 if (register_blkdev(MAJOR_NR,"cdu31a",&scd_fops))
3067 {
3068 printk("Unable to get major %d for CDU-31a\n", MAJOR_NR);
3069 return -EIO;
3070 }
3071
3072 if (SONY_HWC_DOUBLE_SPEED(drive_config))
3073 {
3074 is_double_speed = 1;
3075 }
3076
3077 tmp_irq = cdu31a_irq;
3078 cdu31a_irq = 0;
3079
3080 set_drive_params();
3081
3082 cdu31a_irq = tmp_irq;
3083
3084 if (cdu31a_irq > 0)
3085 {
3086 if (request_irq(cdu31a_irq, cdu31a_interrupt, SA_INTERRUPT, "cdu31a"))
3087 {
3088 printk("Unable to grab IRQ%d for the CDU31A driver\n", cdu31a_irq);
3089 cdu31a_irq = 0;
3090 }
3091 }
3092
3093 printk("Sony I/F CDROM : %8.8s %16.16s %8.8s\n",
3094 drive_config.vendor_id,
3095 drive_config.product_id,
3096 drive_config.product_rev_level);
3097 printk(" Capabilities: %s",
3098 load_mech[SONY_HWC_GET_LOAD_MECH(drive_config)]);
3099 if (SONY_HWC_AUDIO_PLAYBACK(drive_config))
3100 {
3101 printk(", audio");
3102 }
3103 if (SONY_HWC_EJECT(drive_config))
3104 {
3105 printk(", eject");
3106 }
3107 if (SONY_HWC_LED_SUPPORT(drive_config))
3108 {
3109 printk(", LED");
3110 }
3111 if (SONY_HWC_ELECTRIC_VOLUME(drive_config))
3112 {
3113 printk(", elec. Vol");
3114 }
3115 if (SONY_HWC_ELECTRIC_VOLUME_CTL(drive_config))
3116 {
3117 printk(", sep. Vol");
3118 }
3119 if (is_double_speed)
3120 {
3121 printk(", double speed");
3122 }
3123 if (cdu31a_irq > 0)
3124 {
3125 printk(", irq %d", cdu31a_irq);
3126 }
3127 printk("\n");
3128
3129 blk_dev[MAJOR_NR].request_fn = DEVICE_REQUEST;
3130 read_ahead[MAJOR_NR] = CDU31A_READAHEAD;
3131 cdu31a_block_size = 1024;
3132
3133 blksize_size[MAJOR_NR] = &cdu31a_block_size;
3134
3135 cdu31a_abort_timer.next = NULL;
3136 cdu31a_abort_timer.prev = NULL;
3137 cdu31a_abort_timer.function = handle_abort_timeout;
3138 }
3139
3140
3141 disk_changed = 1;
3142
3143 if (drive_found)
3144 {
3145 return(0);
3146 }
3147 else
3148 {
3149 return -EIO;
3150 }
3151 }
3152
3153 #ifdef MODULE
3154
3155 int
3156 init_module(void)
3157 {
3158 return cdu31a_init();
3159 }
3160
3161 void
3162 cleanup_module(void)
3163 {
3164 if ((unregister_blkdev(MAJOR_NR, "cdu31a") == -EINVAL))
3165 {
3166 printk("Can't unregister cdu31a\n");
3167 return;
3168 }
3169 release_region(cdu31a_port,4);
3170 printk("cdu31a module released.\n");
3171 }
3172 #endif MODULE