forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcdu31a.c
3248 lines (2856 loc) · 84.7 KB
/
cdu31a.c
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Sony CDU-31A CDROM interface device driver.
*
* Corey Minyard ([email protected])
*
* Colossians 3:17
*
* See Documentation/cdrom/cdu31a for additional details about this driver.
*
* The Sony interface device driver handles Sony interface CDROM
* drives and provides a complete block-level interface as well as an
* ioctl() interface compatible with the Sun (as specified in
* include/linux/cdrom.h). With this interface, CDROMs can be
* accessed and standard audio CDs can be played back normally.
*
* WARNING - All autoprobes have been removed from the driver.
* You MUST configure the CDU31A via a LILO config
* at boot time or in lilo.conf. I have the
* following in my lilo.conf:
*
* append="cdu31a=0x1f88,0,PAS"
*
* The first number is the I/O base address of the
* card. The second is the interrupt (0 means none).
* The third should be "PAS" if on a Pro-Audio
* spectrum, or nothing if on something else.
*
* This interface is (unfortunately) a polled interface. This is
* because most Sony interfaces are set up with DMA and interrupts
* disables. Some (like mine) do not even have the capability to
* handle interrupts or DMA. For this reason you will see a lot of
* the following:
*
* retry_count = jiffies+ SONY_JIFFIES_TIMEOUT;
* while (time_before(jiffies, retry_count) && (! <some condition to wait for))
* {
* while (handle_sony_cd_attention())
* ;
*
* sony_sleep();
* }
* if (the condition not met)
* {
* return an error;
* }
*
* This ugly hack waits for something to happen, sleeping a little
* between every try. it also handles attentions, which are
* asynchronous events from the drive informing the driver that a disk
* has been inserted, removed, etc.
*
* NEWS FLASH - The driver now supports interrupts but they are
* turned off by default. Use of interrupts is highly encouraged, it
* cuts CPU usage down to a reasonable level. I had DMA in for a while
* but PC DMA is just too slow. Better to just insb() it.
*
* One thing about these drives: They talk in MSF (Minute Second Frame) format.
* There are 75 frames a second, 60 seconds a minute, and up to 75 minutes on a
* disk. The funny thing is that these are sent to the drive in BCD, but the
* interface wants to see them in decimal. A lot of conversion goes on.
*
* DRIVER SPECIAL FEATURES
* -----------------------
*
* This section describes features beyond the normal audio and CD-ROM
* functions of the drive.
*
* XA compatibility
*
* The driver should support XA disks for both the CDU31A and CDU33A.
* It does this transparently, the using program doesn't need to set it.
*
* Multi-Session
*
* A multi-session disk looks just like a normal disk to the user.
* Just mount one normally, and all the data should be there.
* A special thanks to Koen for help with this!
*
* Raw sector I/O
*
* Using the CDROMREADAUDIO it is possible to read raw audio and data
* tracks. Both operations return 2352 bytes per sector. On the data
* tracks, the first 12 bytes is not returned by the drive and the value
* of that data is indeterminate.
*
*
* Copyright (C) 1993 Corey Minyard
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* TODO:
* CDs with form1 and form2 sectors cause problems
* with current read-ahead strategy.
*
* Credits:
* Heiko Eissfeldt <[email protected]>
* For finding abug in the return of the track numbers.
* TOC processing redone for proper multisession support.
*
*
* It probably a little late to be adding a history, but I guess I
* will start.
*
* 10/24/95 - Added support for disabling the eject button when the
* drive is open. Note that there is a small problem
* still here, if the eject button is pushed while the
* drive light is flashing, the drive will return a bad
* status and be reset. It recovers, though.
*
* 03/07/97 - Fixed a problem with timers.
*
*
* 18 Spetember 1997 -- Ported to Uniform CD-ROM driver by
* Heiko Eissfeldt <[email protected]> with additional
* changes by Erik Andersen <[email protected]>
*
* 24 January 1998 -- Removed the scd_disc_status() function, which was now
* just dead code left over from the port.
* Erik Andersen <[email protected]>
*
* 16 July 1998 -- Drive donated to Erik Andersen by John Kodis
* <[email protected]>. Work begun on fixing driver to
* work under 2.1.X. Added temporary extra printks
* which seem to slow it down enough to work.
*
* 9 November 1999 -- Make kernel-parameter implementation work with 2.3.x
* Removed init_module & cleanup_module in favor of
* module_init & module_exit.
* Torben Mathiasen <[email protected]>
*
* 22 October 2004 -- Make the driver work in 2.6.X
* Added workaround to fix hard lockups on eject
* Fixed door locking problem after mounting empty drive
* Set double-speed drives to double speed by default
* Removed all readahead things - not needed anymore
* Ondrej Zary <[email protected]>
*/
#define DEBUG 1
#include <linux/major.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/signal.h>
#include <linux/sched.h>
#include <linux/timer.h>
#include <linux/fs.h>
#include <linux/kernel.h>
#include <linux/hdreg.h>
#include <linux/genhd.h>
#include <linux/ioport.h>
#include <linux/devfs_fs_kernel.h>
#include <linux/string.h>
#include <linux/slab.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/cdrom.h>
#include <asm/system.h>
#include <asm/io.h>
#include <asm/uaccess.h>
#include <asm/dma.h>
#include "cdu31a.h"
#define MAJOR_NR CDU31A_CDROM_MAJOR
#include <linux/blkdev.h>
#define CDU31A_MAX_CONSECUTIVE_ATTENTIONS 10
#define PFX "CDU31A: "
/*
** Edit the following data to change interrupts, DMA channels, etc.
** Default is polled and no DMA. DMA is not recommended for double-speed
** drives.
*/
static struct {
unsigned short base; /* I/O Base Address */
short int_num; /* Interrupt Number (-1 means scan for it,
0 means don't use) */
} cdu31a_addresses[] __initdata = {
{0}
};
static int handle_sony_cd_attention(void);
static int read_subcode(void);
static void sony_get_toc(void);
static int scd_spinup(void);
/*static int scd_open(struct inode *inode, struct file *filp);*/
static int scd_open(struct cdrom_device_info *, int);
static void do_sony_cd_cmd(unsigned char cmd,
unsigned char *params,
unsigned int num_params,
unsigned char *result_buffer,
unsigned int *result_size);
static void size_to_buf(unsigned int size, unsigned char *buf);
/* Parameters for the read-ahead. */
static unsigned int sony_next_block; /* Next 512 byte block offset */
static unsigned int sony_blocks_left = 0; /* Number of 512 byte blocks left
in the current read command. */
/* The base I/O address of the Sony Interface. This is a variable (not a
#define) so it can be easily changed via some future ioctl() */
static unsigned int cdu31a_port = 0;
module_param(cdu31a_port, uint, 0);
/*
* The following are I/O addresses of the various registers for the drive. The
* comment for the base address also applies here.
*/
static volatile unsigned short sony_cd_cmd_reg;
static volatile unsigned short sony_cd_param_reg;
static volatile unsigned short sony_cd_write_reg;
static volatile unsigned short sony_cd_control_reg;
static volatile unsigned short sony_cd_status_reg;
static volatile unsigned short sony_cd_result_reg;
static volatile unsigned short sony_cd_read_reg;
static volatile unsigned short sony_cd_fifost_reg;
static struct request_queue *cdu31a_queue;
static DEFINE_SPINLOCK(cdu31a_lock); /* queue lock */
static int sony_spun_up = 0; /* Has the drive been spun up? */
static int sony_speed = 0; /* Last wanted speed */
static int sony_xa_mode = 0; /* Is an XA disk in the drive
and the drive a CDU31A? */
static int sony_raw_data_mode = 1; /* 1 if data tracks, 0 if audio.
For raw data reads. */
static unsigned int sony_usage = 0; /* How many processes have the
drive open. */
static int sony_pas_init = 0; /* Initialize the Pro-Audio
Spectrum card? */
static struct s_sony_session_toc single_toc; /* Holds the
table of
contents. */
static struct s_all_sessions_toc sony_toc; /* entries gathered from all
sessions */
static int sony_toc_read = 0; /* Has the TOC been read for
the drive? */
static struct s_sony_subcode last_sony_subcode; /* Points to the last
subcode address read */
static DECLARE_MUTEX(sony_sem); /* Semaphore for drive hardware access */
static int is_double_speed = 0; /* does the drive support double speed ? */
static int is_auto_eject = 1; /* Door has been locked? 1=No/0=Yes */
/*
* The audio status uses the values from read subchannel data as specified
* in include/linux/cdrom.h.
*/
static volatile int sony_audio_status = CDROM_AUDIO_NO_STATUS;
/*
* The following are a hack for pausing and resuming audio play. The drive
* does not work as I would expect it, if you stop it then start it again,
* the drive seeks back to the beginning and starts over. This holds the
* position during a pause so a resume can restart it. It uses the
* audio status variable above to tell if it is paused.
*/
static unsigned volatile char cur_pos_msf[3] = { 0, 0, 0 };
static unsigned volatile char final_pos_msf[3] = { 0, 0, 0 };
/* What IRQ is the drive using? 0 if none. */
static int cdu31a_irq = 0;
module_param(cdu31a_irq, int, 0);
/* The interrupt handler will wake this queue up when it gets an
interrupts. */
static DECLARE_WAIT_QUEUE_HEAD(cdu31a_irq_wait);
static int irq_flag = 0;
static int curr_control_reg = 0; /* Current value of the control register */
/* A disk changed variable. When a disk change is detected, it will
all be set to TRUE. As the upper layers ask for disk_changed status
it will be cleared. */
static char disk_changed;
/* This was readahead_buffer once... Now it's used only for audio reads */
static char audio_buffer[CD_FRAMESIZE_RAW];
/* Used to time a short period to abort an operation after the
drive has been idle for a while. This keeps the light on
the drive from flashing for very long. */
static struct timer_list cdu31a_abort_timer;
/* Marks if the timeout has started an abort read. This is used
on entry to the drive to tell the code to read out the status
from the abort read. */
static int abort_read_started = 0;
/*
* Uniform cdrom interface function
* report back, if disc has changed from time of last request.
*/
static int scd_media_changed(struct cdrom_device_info *cdi, int disc_nr)
{
int retval;
retval = disk_changed;
disk_changed = 0;
return retval;
}
/*
* Uniform cdrom interface function
* report back, if drive is ready
*/
static int scd_drive_status(struct cdrom_device_info *cdi, int slot_nr)
{
if (CDSL_CURRENT != slot_nr)
/* we have no changer support */
return -EINVAL;
if (sony_spun_up)
return CDS_DISC_OK;
if (down_interruptible(&sony_sem))
return -ERESTARTSYS;
if (scd_spinup() == 0)
sony_spun_up = 1;
up(&sony_sem);
return sony_spun_up ? CDS_DISC_OK : CDS_DRIVE_NOT_READY;
}
static inline void enable_interrupts(void)
{
curr_control_reg |= (SONY_ATTN_INT_EN_BIT
| SONY_RES_RDY_INT_EN_BIT
| SONY_DATA_RDY_INT_EN_BIT);
outb(curr_control_reg, sony_cd_control_reg);
}
static inline void disable_interrupts(void)
{
curr_control_reg &= ~(SONY_ATTN_INT_EN_BIT
| SONY_RES_RDY_INT_EN_BIT
| SONY_DATA_RDY_INT_EN_BIT);
outb(curr_control_reg, sony_cd_control_reg);
}
/*
* Wait a little while (used for polling the drive). If in initialization,
* setting a timeout doesn't work, so just loop for a while.
*/
static inline void sony_sleep(void)
{
if (cdu31a_irq <= 0) {
yield();
} else { /* Interrupt driven */
DEFINE_WAIT(w);
int first = 1;
while (1) {
prepare_to_wait(&cdu31a_irq_wait, &w,
TASK_INTERRUPTIBLE);
if (first) {
enable_interrupts();
first = 0;
}
if (irq_flag != 0)
break;
if (!signal_pending(current)) {
schedule();
continue;
} else
disable_interrupts();
break;
}
finish_wait(&cdu31a_irq_wait, &w);
irq_flag = 0;
}
}
/*
* The following are convenience routine to read various status and set
* various conditions in the drive.
*/
static inline int is_attention(void)
{
return (inb(sony_cd_status_reg) & SONY_ATTN_BIT) != 0;
}
static inline int is_busy(void)
{
return (inb(sony_cd_status_reg) & SONY_BUSY_BIT) != 0;
}
static inline int is_data_ready(void)
{
return (inb(sony_cd_status_reg) & SONY_DATA_RDY_BIT) != 0;
}
static inline int is_data_requested(void)
{
return (inb(sony_cd_status_reg) & SONY_DATA_REQUEST_BIT) != 0;
}
static inline int is_result_ready(void)
{
return (inb(sony_cd_status_reg) & SONY_RES_RDY_BIT) != 0;
}
static inline int is_param_write_rdy(void)
{
return (inb(sony_cd_fifost_reg) & SONY_PARAM_WRITE_RDY_BIT) != 0;
}
static inline int is_result_reg_not_empty(void)
{
return (inb(sony_cd_fifost_reg) & SONY_RES_REG_NOT_EMP_BIT) != 0;
}
static inline void reset_drive(void)
{
curr_control_reg = 0;
sony_toc_read = 0;
outb(SONY_DRIVE_RESET_BIT, sony_cd_control_reg);
}
/*
* Uniform cdrom interface function
* reset drive and return when it is ready
*/
static int scd_reset(struct cdrom_device_info *cdi)
{
unsigned long retry_count;
if (down_interruptible(&sony_sem))
return -ERESTARTSYS;
reset_drive();
retry_count = jiffies + SONY_RESET_TIMEOUT;
while (time_before(jiffies, retry_count) && (!is_attention())) {
sony_sleep();
}
up(&sony_sem);
return 0;
}
static inline void clear_attention(void)
{
outb(curr_control_reg | SONY_ATTN_CLR_BIT, sony_cd_control_reg);
}
static inline void clear_result_ready(void)
{
outb(curr_control_reg | SONY_RES_RDY_CLR_BIT, sony_cd_control_reg);
}
static inline void clear_data_ready(void)
{
outb(curr_control_reg | SONY_DATA_RDY_CLR_BIT,
sony_cd_control_reg);
}
static inline void clear_param_reg(void)
{
outb(curr_control_reg | SONY_PARAM_CLR_BIT, sony_cd_control_reg);
}
static inline unsigned char read_status_register(void)
{
return inb(sony_cd_status_reg);
}
static inline unsigned char read_result_register(void)
{
return inb(sony_cd_result_reg);
}
static inline unsigned char read_data_register(void)
{
return inb(sony_cd_read_reg);
}
static inline void write_param(unsigned char param)
{
outb(param, sony_cd_param_reg);
}
static inline void write_cmd(unsigned char cmd)
{
outb(curr_control_reg | SONY_RES_RDY_INT_EN_BIT,
sony_cd_control_reg);
outb(cmd, sony_cd_cmd_reg);
}
static irqreturn_t cdu31a_interrupt(int irq, void *dev_id, struct pt_regs *regs)
{
unsigned char val;
if (abort_read_started) {
/* We might be waiting for an abort to finish. Don't
disable interrupts yet, though, because we handle
this one here. */
/* Clear out the result registers. */
while (is_result_reg_not_empty()) {
val = read_result_register();
}
clear_data_ready();
clear_result_ready();
/* Clear out the data */
while (is_data_requested()) {
val = read_data_register();
}
abort_read_started = 0;
/* If something was waiting, wake it up now. */
if (waitqueue_active(&cdu31a_irq_wait)) {
disable_interrupts();
irq_flag = 1;
wake_up_interruptible(&cdu31a_irq_wait);
}
} else if (waitqueue_active(&cdu31a_irq_wait)) {
disable_interrupts();
irq_flag = 1;
wake_up_interruptible(&cdu31a_irq_wait);
} else {
disable_interrupts();
printk(KERN_NOTICE PFX
"Got an interrupt but nothing was waiting\n");
}
return IRQ_HANDLED;
}
/*
* give more verbose error messages
*/
static unsigned char *translate_error(unsigned char err_code)
{
static unsigned char errbuf[80];
switch (err_code) {
case 0x10: return "illegal command ";
case 0x11: return "illegal parameter ";
case 0x20: return "not loaded ";
case 0x21: return "no disc ";
case 0x22: return "not spinning ";
case 0x23: return "spinning ";
case 0x25: return "spindle servo ";
case 0x26: return "focus servo ";
case 0x29: return "eject mechanism ";
case 0x2a: return "audio playing ";
case 0x2c: return "emergency eject ";
case 0x30: return "focus ";
case 0x31: return "frame sync ";
case 0x32: return "subcode address ";
case 0x33: return "block sync ";
case 0x34: return "header address ";
case 0x40: return "illegal track read ";
case 0x41: return "mode 0 read ";
case 0x42: return "illegal mode read ";
case 0x43: return "illegal block size read ";
case 0x44: return "mode read ";
case 0x45: return "form read ";
case 0x46: return "leadout read ";
case 0x47: return "buffer overrun ";
case 0x53: return "unrecoverable CIRC ";
case 0x57: return "unrecoverable LECC ";
case 0x60: return "no TOC ";
case 0x61: return "invalid subcode data ";
case 0x63: return "focus on TOC read ";
case 0x64: return "frame sync on TOC read ";
case 0x65: return "TOC data ";
case 0x70: return "hardware failure ";
case 0x91: return "leadin ";
case 0x92: return "leadout ";
case 0x93: return "data track ";
}
sprintf(errbuf, "unknown 0x%02x ", err_code);
return errbuf;
}
/*
* Set the drive parameters so the drive will auto-spin-up when a
* disk is inserted.
*/
static void set_drive_params(int want_doublespeed)
{
unsigned char res_reg[12];
unsigned int res_size;
unsigned char params[3];
params[0] = SONY_SD_AUTO_SPIN_DOWN_TIME;
params[1] = 0x00; /* Never spin down the drive. */
do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
params, 2, res_reg, &res_size);
if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20)) {
printk(KERN_NOTICE PFX
"Unable to set spin-down time: 0x%2.2x\n", res_reg[1]);
}
params[0] = SONY_SD_MECH_CONTROL;
params[1] = SONY_AUTO_SPIN_UP_BIT; /* Set auto spin up */
if (is_auto_eject)
params[1] |= SONY_AUTO_EJECT_BIT;
if (is_double_speed && want_doublespeed) {
params[1] |= SONY_DOUBLE_SPEED_BIT; /* Set the drive to double speed if
possible */
}
do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
params, 2, res_reg, &res_size);
if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20)) {
printk(KERN_NOTICE PFX "Unable to set mechanical "
"parameters: 0x%2.2x\n", res_reg[1]);
}
}
/*
* Uniform cdrom interface function
* select reading speed for data access
*/
static int scd_select_speed(struct cdrom_device_info *cdi, int speed)
{
if (speed == 0)
sony_speed = 1;
else
sony_speed = speed - 1;
if (down_interruptible(&sony_sem))
return -ERESTARTSYS;
set_drive_params(sony_speed);
up(&sony_sem);
return 0;
}
/*
* Uniform cdrom interface function
* lock or unlock eject button
*/
static int scd_lock_door(struct cdrom_device_info *cdi, int lock)
{
if (lock == 0) {
is_auto_eject = 1;
} else {
is_auto_eject = 0;
}
if (down_interruptible(&sony_sem))
return -ERESTARTSYS;
set_drive_params(sony_speed);
up(&sony_sem);
return 0;
}
/*
* This code will reset the drive and attempt to restore sane parameters.
*/
static void restart_on_error(void)
{
unsigned char res_reg[12];
unsigned int res_size;
unsigned long retry_count;
printk(KERN_NOTICE PFX "Resetting drive on error\n");
reset_drive();
retry_count = jiffies + SONY_RESET_TIMEOUT;
while (time_before(jiffies, retry_count) && (!is_attention())) {
sony_sleep();
}
set_drive_params(sony_speed);
do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg, &res_size);
if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20)) {
printk(KERN_NOTICE PFX "Unable to spin up drive: 0x%2.2x\n",
res_reg[1]);
}
msleep(2000);
sony_get_toc();
}
/*
* This routine writes data to the parameter register. Since this should
* happen fairly fast, it is polled with no OS waits between.
*/
static int write_params(unsigned char *params, int num_params)
{
unsigned int retry_count;
retry_count = SONY_READY_RETRIES;
while ((retry_count > 0) && (!is_param_write_rdy())) {
retry_count--;
}
if (!is_param_write_rdy()) {
return -EIO;
}
while (num_params > 0) {
write_param(*params);
params++;
num_params--;
}
return 0;
}
/*
* The following reads data from the command result register. It is a
* fairly complex routine, all status info flows back through this
* interface. The algorithm is stolen directly from the flowcharts in
* the drive manual.
*/
static void
get_result(unsigned char *result_buffer, unsigned int *result_size)
{
unsigned char a, b;
int i;
unsigned long retry_count;
while (handle_sony_cd_attention());
/* Wait for the result data to be ready */
retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
while (time_before(jiffies, retry_count)
&& (is_busy() || (!(is_result_ready())))) {
sony_sleep();
while (handle_sony_cd_attention());
}
if (is_busy() || (!(is_result_ready()))) {
pr_debug(PFX "timeout out %d\n", __LINE__);
result_buffer[0] = 0x20;
result_buffer[1] = SONY_TIMEOUT_OP_ERR;
*result_size = 2;
return;
}
/*
* Get the first two bytes. This determines what else needs
* to be done.
*/
clear_result_ready();
a = read_result_register();
*result_buffer = a;
result_buffer++;
/* Check for block error status result. */
if ((a & 0xf0) == 0x50) {
*result_size = 1;
return;
}
b = read_result_register();
*result_buffer = b;
result_buffer++;
*result_size = 2;
/*
* 0x20 means an error occurred. Byte 2 will have the error code.
* Otherwise, the command succeeded, byte 2 will have the count of
* how many more status bytes are coming.
*
* The result register can be read 10 bytes at a time, a wait for
* result ready to be asserted must be done between every 10 bytes.
*/
if ((a & 0xf0) != 0x20) {
if (b > 8) {
for (i = 0; i < 8; i++) {
*result_buffer = read_result_register();
result_buffer++;
(*result_size)++;
}
b = b - 8;
while (b > 10) {
retry_count = SONY_READY_RETRIES;
while ((retry_count > 0)
&& (!is_result_ready())) {
retry_count--;
}
if (!is_result_ready()) {
pr_debug(PFX "timeout out %d\n",
__LINE__);
result_buffer[0] = 0x20;
result_buffer[1] =
SONY_TIMEOUT_OP_ERR;
*result_size = 2;
return;
}
clear_result_ready();
for (i = 0; i < 10; i++) {
*result_buffer =
read_result_register();
result_buffer++;
(*result_size)++;
}
b = b - 10;
}
if (b > 0) {
retry_count = SONY_READY_RETRIES;
while ((retry_count > 0)
&& (!is_result_ready())) {
retry_count--;
}
if (!is_result_ready()) {
pr_debug(PFX "timeout out %d\n",
__LINE__);
result_buffer[0] = 0x20;
result_buffer[1] =
SONY_TIMEOUT_OP_ERR;
*result_size = 2;
return;
}
}
}
while (b > 0) {
*result_buffer = read_result_register();
result_buffer++;
(*result_size)++;
b--;
}
}
}
/*
* Do a command that does not involve data transfer. This routine must
* be re-entrant from the same task to support being called from the
* data operation code when an error occurs.
*/
static void
do_sony_cd_cmd(unsigned char cmd,
unsigned char *params,
unsigned int num_params,
unsigned char *result_buffer, unsigned int *result_size)
{
unsigned long retry_count;
int num_retries = 0;
retry_cd_operation:
while (handle_sony_cd_attention());
retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
while (time_before(jiffies, retry_count) && (is_busy())) {
sony_sleep();
while (handle_sony_cd_attention());
}
if (is_busy()) {
pr_debug(PFX "timeout out %d\n", __LINE__);
result_buffer[0] = 0x20;
result_buffer[1] = SONY_TIMEOUT_OP_ERR;
*result_size = 2;
} else {
clear_result_ready();
clear_param_reg();
write_params(params, num_params);
write_cmd(cmd);
get_result(result_buffer, result_size);
}
if (((result_buffer[0] & 0xf0) == 0x20)
&& (num_retries < MAX_CDU31A_RETRIES)) {
num_retries++;
msleep(100);
goto retry_cd_operation;
}
}
/*
* Handle an attention from the drive. This will return 1 if it found one
* or 0 if not (if one is found, the caller might want to call again).
*
* This routine counts the number of consecutive times it is called
* (since this is always called from a while loop until it returns
* a 0), and returns a 0 if it happens too many times. This will help
* prevent a lockup.
*/
static int handle_sony_cd_attention(void)
{
unsigned char atten_code;
static int num_consecutive_attentions = 0;
volatile int val;
#if 0
pr_debug(PFX "Entering %s\n", __FUNCTION__);
#endif
if (is_attention()) {
if (num_consecutive_attentions >
CDU31A_MAX_CONSECUTIVE_ATTENTIONS) {
printk(KERN_NOTICE PFX "Too many consecutive "
"attentions: %d\n", num_consecutive_attentions);
num_consecutive_attentions = 0;
pr_debug(PFX "Leaving %s at %d\n", __FUNCTION__,
__LINE__);
return 0;
}
clear_attention();
atten_code = read_result_register();
switch (atten_code) {
/* Someone changed the CD. Mark it as changed */
case SONY_MECH_LOADED_ATTN:
disk_changed = 1;
sony_toc_read = 0;
sony_audio_status = CDROM_AUDIO_NO_STATUS;
sony_blocks_left = 0;
break;
case SONY_SPIN_DOWN_COMPLETE_ATTN:
/* Mark the disk as spun down. */
sony_spun_up = 0;
break;
case SONY_AUDIO_PLAY_DONE_ATTN:
sony_audio_status = CDROM_AUDIO_COMPLETED;
read_subcode();
break;
case SONY_EJECT_PUSHED_ATTN:
if (is_auto_eject) {
sony_audio_status = CDROM_AUDIO_INVALID;
}
break;
case SONY_LEAD_IN_ERR_ATTN:
case SONY_LEAD_OUT_ERR_ATTN:
case SONY_DATA_TRACK_ERR_ATTN:
case SONY_AUDIO_PLAYBACK_ERR_ATTN:
sony_audio_status = CDROM_AUDIO_ERROR;
break;
}
num_consecutive_attentions++;
pr_debug(PFX "Leaving %s at %d\n", __FUNCTION__, __LINE__);
return 1;
} else if (abort_read_started) {
while (is_result_reg_not_empty()) {
val = read_result_register();
}
clear_data_ready();
clear_result_ready();
/* Clear out the data */
while (is_data_requested()) {
val = read_data_register();
}
abort_read_started = 0;
pr_debug(PFX "Leaving %s at %d\n", __FUNCTION__, __LINE__);
return 1;
}
num_consecutive_attentions = 0;
#if 0
pr_debug(PFX "Leaving %s at %d\n", __FUNCTION__, __LINE__);
#endif
return 0;
}