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