vserver 1.9.3
[linux-2.6.git] / drivers / cdrom / cdu31a.c
1 /*
2 * Sony CDU-31A CDROM interface device driver.
3 *
4 * Corey Minyard (minyard@wf-rch.cirr.com)
5 *
6 * Colossians 3:17
7 *
8 *  See Documentation/cdrom/cdu31a for additional details about this driver.
9
10 * The Sony interface device driver handles Sony interface CDROM
11 * drives and provides a complete block-level interface as well as an
12 * ioctl() interface compatible with the Sun (as specified in
13 * include/linux/cdrom.h).  With this interface, CDROMs can be
14 * accessed and standard audio CDs can be played back normally.
15 *
16 * WARNING -     All autoprobes have been removed from the driver.
17 *               You MUST configure the CDU31A via a LILO config
18 *               at boot time or in lilo.conf.  I have the
19 *               following in my lilo.conf:
20 *
21 *                append="cdu31a=0x1f88,0,PAS"
22 *
23 *               The first number is the I/O base address of the
24 *               card.  The second is the interrupt (0 means none).
25  *              The third should be "PAS" if on a Pro-Audio
26  *              spectrum, or nothing if on something else.
27  *
28  * This interface is (unfortunately) a polled interface.  This is
29  * because most Sony interfaces are set up with DMA and interrupts
30  * disables.  Some (like mine) do not even have the capability to
31  * handle interrupts or DMA.  For this reason you will see a lot of
32  * the following:
33  *
34  *   retry_count = jiffies+ SONY_JIFFIES_TIMEOUT;
35  *   while (time_before(jiffies, retry_count) && (! <some condition to wait for))
36  *   {
37  *      while (handle_sony_cd_attention())
38  *         ;
39  *
40  *      sony_sleep();
41  *   }
42  *   if (the condition not met)
43  *   {
44  *      return an error;
45  *   }
46  *
47  * This ugly hack waits for something to happen, sleeping a little
48  * between every try.  it also handles attentions, which are
49  * asynchronous events from the drive informing the driver that a disk
50  * has been inserted, removed, etc.
51  *
52  * NEWS FLASH - The driver now supports interrupts but they are
53  * turned off by default.  Use of interrupts is highly encouraged, it
54  * cuts CPU usage down to a reasonable level.  I had DMA in for a while
55  * but PC DMA is just too slow.  Better to just insb() it.
56  *
57  * One thing about these drives: They talk in MSF (Minute Second Frame) format.
58  * There are 75 frames a second, 60 seconds a minute, and up to 75 minutes on a
59  * disk.  The funny thing is that these are sent to the drive in BCD, but the
60  * interface wants to see them in decimal.  A lot of conversion goes on.
61  *
62  * DRIVER SPECIAL FEATURES
63  * -----------------------
64  *
65  * This section describes features beyond the normal audio and CD-ROM
66  * functions of the drive.
67  *
68  * 2048 byte buffer mode
69  *
70  * If a disk is mounted with -o block=2048, data is copied straight
71  * from the drive data port to the buffer.  Otherwise, the readahead
72  * buffer must be involved to hold the other 1K of data when a 1K
73  * block operation is done.  Note that with 2048 byte blocks you
74  * cannot execute files from the CD.
75  *
76  * XA compatibility
77  *
78  * The driver should support XA disks for both the CDU31A and CDU33A.
79  * It does this transparently, the using program doesn't need to set it.
80  *
81  * Multi-Session
82  *
83  * A multi-session disk looks just like a normal disk to the user.
84  * Just mount one normally, and all the data should be there.
85  * A special thanks to Koen for help with this!
86  * 
87  * Raw sector I/O
88  *
89  * Using the CDROMREADAUDIO it is possible to read raw audio and data
90  * tracks.  Both operations return 2352 bytes per sector.  On the data
91  * tracks, the first 12 bytes is not returned by the drive and the value
92  * of that data is indeterminate.
93  *
94  *
95  *  Copyright (C) 1993  Corey Minyard
96  *
97  *  This program is free software; you can redistribute it and/or modify
98  *  it under the terms of the GNU General Public License as published by
99  *  the Free Software Foundation; either version 2 of the License, or
100  *  (at your option) any later version.
101  *
102  *  This program is distributed in the hope that it will be useful,
103  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
104  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
105  *  GNU General Public License for more details.
106  *
107  *  You should have received a copy of the GNU General Public License
108  *  along with this program; if not, write to the Free Software
109  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
110  *
111  * TODO: 
112  *       CDs with form1 and form2 sectors cause problems
113  *       with current read-ahead strategy.
114  *
115  * Credits:
116  *    Heiko Eissfeldt <heiko@colossus.escape.de>
117  *         For finding abug in the return of the track numbers.
118  *         TOC processing redone for proper multisession support.
119  *
120  *
121  *  It probably a little late to be adding a history, but I guess I
122  *  will start.
123  *
124  *  10/24/95 - Added support for disabling the eject button when the
125  *             drive is open.  Note that there is a small problem
126  *             still here, if the eject button is pushed while the
127  *             drive light is flashing, the drive will return a bad
128  *             status and be reset.  It recovers, though.
129  *
130  *  03/07/97 - Fixed a problem with timers.
131  *
132  *
133  *  18 Spetember 1997 -- Ported to Uniform CD-ROM driver by 
134  *                 Heiko Eissfeldt <heiko@colossus.escape.de> with additional
135  *                 changes by Erik Andersen <andersee@debian.org>
136  *
137  *  24 January 1998 -- Removed the scd_disc_status() function, which was now
138  *                     just dead code left over from the port.
139  *                          Erik Andersen <andersee@debian.org>
140  *
141  *  16 July 1998 -- Drive donated to Erik Andersen by John Kodis
142  *                   <kodis@jagunet.com>.  Work begun on fixing driver to
143  *                   work under 2.1.X.  Added temporary extra printks
144  *                   which seem to slow it down enough to work.
145  *
146  *  9 November 1999 -- Make kernel-parameter implementation work with 2.3.x 
147  *                     Removed init_module & cleanup_module in favor of 
148  *                     module_init & module_exit.
149  *                     Torben Mathiasen <tmm@image.dk>
150 */
151
152 #include <linux/major.h>
153
154 #include <linux/module.h>
155
156 #include <linux/errno.h>
157 #include <linux/signal.h>
158 #include <linux/sched.h>
159 #include <linux/timer.h>
160 #include <linux/fs.h>
161 #include <linux/kernel.h>
162 #include <linux/hdreg.h>
163 #include <linux/genhd.h>
164 #include <linux/ioport.h>
165 #include <linux/devfs_fs_kernel.h>
166 #include <linux/string.h>
167 #include <linux/slab.h>
168 #include <linux/init.h>
169 #include <linux/interrupt.h>
170
171 #include <asm/system.h>
172 #include <asm/io.h>
173 #include <asm/uaccess.h>
174 #include <asm/dma.h>
175
176 #include <linux/cdrom.h>
177 #include "cdu31a.h"
178
179 #define MAJOR_NR CDU31A_CDROM_MAJOR
180 #include <linux/blkdev.h>
181
182 #define CDU31A_READAHEAD 4      /* 128 sector, 64kB, 32 reads read-ahead */
183 #define CDU31A_MAX_CONSECUTIVE_ATTENTIONS 10
184
185 #define DEBUG 0
186
187 /* Define the following if you have data corruption problems. */
188 #undef SONY_POLL_EACH_BYTE
189
190 /*
191 ** Edit the following data to change interrupts, DMA channels, etc.
192 ** Default is polled and no DMA.  DMA is not recommended for double-speed
193 ** drives.
194 */
195 static struct {
196         unsigned short base;    /* I/O Base Address */
197         short int_num;          /* Interrupt Number (-1 means scan for it,
198                                    0 means don't use) */
199 } cdu31a_addresses[] __initdata = {
200         {0}
201 };
202
203 static int handle_sony_cd_attention(void);
204 static int read_subcode(void);
205 static void sony_get_toc(void);
206 static int scd_spinup(void);
207 /*static int scd_open(struct inode *inode, struct file *filp);*/
208 static int scd_open(struct cdrom_device_info *, int);
209 static void do_sony_cd_cmd(unsigned char cmd,
210                            unsigned char *params,
211                            unsigned int num_params,
212                            unsigned char *result_buffer,
213                            unsigned int *result_size);
214 static void size_to_buf(unsigned int size, unsigned char *buf);
215
216 /* Parameters for the read-ahead. */
217 static unsigned int sony_next_block;    /* Next 512 byte block offset */
218 static unsigned int sony_blocks_left = 0;       /* Number of 512 byte blocks left
219                                                    in the current read command. */
220
221
222 /* The base I/O address of the Sony Interface.  This is a variable (not a
223    #define) so it can be easily changed via some future ioctl() */
224 static unsigned int cdu31a_port = 0;
225 MODULE_PARM(cdu31a_port, "i");
226
227 /*
228  * The following are I/O addresses of the various registers for the drive.  The
229  * comment for the base address also applies here.
230  */
231 static volatile unsigned short sony_cd_cmd_reg;
232 static volatile unsigned short sony_cd_param_reg;
233 static volatile unsigned short sony_cd_write_reg;
234 static volatile unsigned short sony_cd_control_reg;
235 static volatile unsigned short sony_cd_status_reg;
236 static volatile unsigned short sony_cd_result_reg;
237 static volatile unsigned short sony_cd_read_reg;
238 static volatile unsigned short sony_cd_fifost_reg;
239
240 static struct request_queue *cdu31a_queue;
241 static spinlock_t cdu31a_lock = SPIN_LOCK_UNLOCKED; /* queue lock */
242
243 static int sony_spun_up = 0;    /* Has the drive been spun up? */
244
245 static int sony_speed = 0;      /* Last wanted speed */
246
247 static int sony_xa_mode = 0;    /* Is an XA disk in the drive
248                                    and the drive a CDU31A? */
249
250 static int sony_raw_data_mode = 1;      /* 1 if data tracks, 0 if audio.
251                                            For raw data reads. */
252
253 static unsigned int sony_usage = 0;     /* How many processes have the
254                                            drive open. */
255
256 static int sony_pas_init = 0;   /* Initialize the Pro-Audio
257                                    Spectrum card? */
258
259 static struct s_sony_session_toc single_toc;    /* Holds the
260                                                    table of
261                                                    contents. */
262
263 static struct s_all_sessions_toc sony_toc;      /* entries gathered from all
264                                                    sessions */
265
266 static int sony_toc_read = 0;   /* Has the TOC been read for
267                                    the drive? */
268
269 static struct s_sony_subcode last_sony_subcode; /* Points to the last
270                                                    subcode address read */
271
272 static volatile int sony_inuse = 0;     /* Is the drive in use?  Only one operation
273                                            at a time allowed */
274
275 static DECLARE_WAIT_QUEUE_HEAD(sony_wait);      /* Things waiting for the drive */
276
277 static struct task_struct *has_cd_task = NULL;  /* The task that is currently
278                                                    using the CDROM drive, or
279                                                    NULL if none. */
280
281 static int is_double_speed = 0; /* does the drive support double speed ? */
282 static int is_a_cdu31a = 1;     /* Is the drive a CDU31A? */
283
284 static int is_auto_eject = 1;   /* Door has been locked? 1=No/0=Yes */
285
286 /*
287  * The audio status uses the values from read subchannel data as specified
288  * in include/linux/cdrom.h.
289  */
290 static volatile int sony_audio_status = CDROM_AUDIO_NO_STATUS;
291
292 /*
293  * The following are a hack for pausing and resuming audio play.  The drive
294  * does not work as I would expect it, if you stop it then start it again,
295  * the drive seeks back to the beginning and starts over.  This holds the
296  * position during a pause so a resume can restart it.  It uses the
297  * audio status variable above to tell if it is paused.
298  */
299 static unsigned volatile char cur_pos_msf[3] = { 0, 0, 0 };
300 static unsigned volatile char final_pos_msf[3] = { 0, 0, 0 };
301
302 /* What IRQ is the drive using?  0 if none. */
303 static int cdu31a_irq = 0;
304 MODULE_PARM(cdu31a_irq, "i");
305
306 /* The interrupt handler will wake this queue up when it gets an
307    interrupts. */
308 DECLARE_WAIT_QUEUE_HEAD(cdu31a_irq_wait);
309
310 static int curr_control_reg = 0;        /* Current value of the control register */
311
312 /* A disk changed variable.  When a disk change is detected, it will
313    all be set to TRUE.  As the upper layers ask for disk_changed status
314    it will be cleared. */
315 static char disk_changed;
316
317 /* Variable for using the readahead buffer.  The readahead buffer
318    is used for raw sector reads and for blocksizes that are smaller
319    than 2048 bytes. */
320 static char readahead_buffer[CD_FRAMESIZE_RAW];
321 static int readahead_dataleft = 0;
322 static int readahead_bad = 0;
323
324 /* Used to time a short period to abort an operation after the
325    drive has been idle for a while.  This keeps the light on
326    the drive from flashing for very long. */
327 static struct timer_list cdu31a_abort_timer;
328
329 /* Marks if the timeout has started an abort read.  This is used
330    on entry to the drive to tell the code to read out the status
331    from the abort read. */
332 static int abort_read_started = 0;
333
334 /*
335  * Uniform cdrom interface function
336  * report back, if disc has changed from time of last request.
337  */
338 static int scd_media_changed(struct cdrom_device_info *cdi, int disc_nr)
339 {
340         int retval;
341
342         retval = disk_changed;
343         disk_changed = 0;
344
345         return retval;
346 }
347
348 /*
349  * Uniform cdrom interface function
350  * report back, if drive is ready
351  */
352 static int scd_drive_status(struct cdrom_device_info *cdi, int slot_nr)
353 {
354         if (CDSL_CURRENT != slot_nr) {
355                 /* we have no changer support */
356                 return -EINVAL;
357         }
358         if (scd_spinup() == 0) {
359                 sony_spun_up = 1;
360         }
361         return sony_spun_up ? CDS_DISC_OK : CDS_DRIVE_NOT_READY;
362 }
363
364 static inline void enable_interrupts(void)
365 {
366         curr_control_reg |= (SONY_ATTN_INT_EN_BIT
367                              | SONY_RES_RDY_INT_EN_BIT
368                              | SONY_DATA_RDY_INT_EN_BIT);
369         outb(curr_control_reg, sony_cd_control_reg);
370 }
371
372 static inline void disable_interrupts(void)
373 {
374         curr_control_reg &= ~(SONY_ATTN_INT_EN_BIT
375                               | SONY_RES_RDY_INT_EN_BIT
376                               | SONY_DATA_RDY_INT_EN_BIT);
377         outb(curr_control_reg, sony_cd_control_reg);
378 }
379
380 /*
381  * Wait a little while (used for polling the drive).  If in initialization,
382  * setting a timeout doesn't work, so just loop for a while.
383  */
384 static inline void sony_sleep(void)
385 {
386         unsigned long flags;
387
388         if (cdu31a_irq <= 0) {
389                 yield();
390         } else {                /* Interrupt driven */
391
392                 save_flags(flags);
393                 cli();
394                 enable_interrupts();
395                 interruptible_sleep_on(&cdu31a_irq_wait);
396                 restore_flags(flags);
397         }
398 }
399
400
401 /*
402  * The following are convenience routine to read various status and set
403  * various conditions in the drive.
404  */
405 static inline int is_attention(void)
406 {
407         return ((inb(sony_cd_status_reg) & SONY_ATTN_BIT) != 0);
408 }
409
410 static inline int is_busy(void)
411 {
412         return ((inb(sony_cd_status_reg) & SONY_BUSY_BIT) != 0);
413 }
414
415 static inline int is_data_ready(void)
416 {
417         return ((inb(sony_cd_status_reg) & SONY_DATA_RDY_BIT) != 0);
418 }
419
420 static inline int is_data_requested(void)
421 {
422         return ((inb(sony_cd_status_reg) & SONY_DATA_REQUEST_BIT) != 0);
423 }
424
425 static inline int is_result_ready(void)
426 {
427         return ((inb(sony_cd_status_reg) & SONY_RES_RDY_BIT) != 0);
428 }
429
430 static inline int is_param_write_rdy(void)
431 {
432         return ((inb(sony_cd_fifost_reg) & SONY_PARAM_WRITE_RDY_BIT) != 0);
433 }
434
435 static inline int is_result_reg_not_empty(void)
436 {
437         return ((inb(sony_cd_fifost_reg) & SONY_RES_REG_NOT_EMP_BIT) != 0);
438 }
439
440 static inline void reset_drive(void)
441 {
442         curr_control_reg = 0;
443         readahead_dataleft = 0;
444         sony_toc_read = 0;
445         outb(SONY_DRIVE_RESET_BIT, sony_cd_control_reg);
446 }
447
448 /*
449  * Uniform cdrom interface function
450  * reset drive and return when it is ready
451  */
452 static int scd_reset(struct cdrom_device_info *cdi)
453 {
454         unsigned long retry_count;
455
456         reset_drive();
457
458         retry_count = jiffies + SONY_RESET_TIMEOUT;
459         while (time_before(jiffies, retry_count) && (!is_attention())) {
460                 sony_sleep();
461         }
462
463         return 0;
464 }
465
466 static inline void clear_attention(void)
467 {
468         outb(curr_control_reg | SONY_ATTN_CLR_BIT, sony_cd_control_reg);
469 }
470
471 static inline void clear_result_ready(void)
472 {
473         outb(curr_control_reg | SONY_RES_RDY_CLR_BIT, sony_cd_control_reg);
474 }
475
476 static inline void clear_data_ready(void)
477 {
478         outb(curr_control_reg | SONY_DATA_RDY_CLR_BIT,
479              sony_cd_control_reg);
480 }
481
482 static inline void clear_param_reg(void)
483 {
484         outb(curr_control_reg | SONY_PARAM_CLR_BIT, sony_cd_control_reg);
485 }
486
487 static inline unsigned char read_status_register(void)
488 {
489         return (inb(sony_cd_status_reg));
490 }
491
492 static inline unsigned char read_result_register(void)
493 {
494         return (inb(sony_cd_result_reg));
495 }
496
497 static inline unsigned char read_data_register(void)
498 {
499         return (inb(sony_cd_read_reg));
500 }
501
502 static inline void write_param(unsigned char param)
503 {
504         outb(param, sony_cd_param_reg);
505 }
506
507 static inline void write_cmd(unsigned char cmd)
508 {
509         outb(curr_control_reg | SONY_RES_RDY_INT_EN_BIT,
510              sony_cd_control_reg);
511         outb(cmd, sony_cd_cmd_reg);
512 }
513
514 static irqreturn_t cdu31a_interrupt(int irq, void *dev_id, struct pt_regs *regs)
515 {
516         unsigned char val;
517
518         if (abort_read_started) {
519                 /* We might be waiting for an abort to finish.  Don't
520                    disable interrupts yet, though, because we handle
521                    this one here. */
522                 /* Clear out the result registers. */
523                 while (is_result_reg_not_empty()) {
524                         val = read_result_register();
525                 }
526                 clear_data_ready();
527                 clear_result_ready();
528
529                 /* Clear out the data */
530                 while (is_data_requested()) {
531                         val = read_data_register();
532                 }
533                 abort_read_started = 0;
534
535                 /* If something was waiting, wake it up now. */
536                 if (waitqueue_active(&cdu31a_irq_wait)) {
537                         disable_interrupts();
538                         wake_up(&cdu31a_irq_wait);
539                 }
540         } else if (waitqueue_active(&cdu31a_irq_wait)) {
541                 disable_interrupts();
542                 wake_up(&cdu31a_irq_wait);
543         } else {
544                 disable_interrupts();
545                 printk
546                     ("CDU31A: Got an interrupt but nothing was waiting\n");
547         }
548         return IRQ_HANDLED;
549 }
550
551 /*
552  * give more verbose error messages
553  */
554 static unsigned char *translate_error(unsigned char err_code)
555 {
556         static unsigned char errbuf[80];
557
558         switch (err_code) {
559         case 0x10:
560                 return "illegal command ";
561         case 0x11:
562                 return "illegal parameter ";
563
564         case 0x20:
565                 return "not loaded ";
566         case 0x21:
567                 return "no disc ";
568         case 0x22:
569                 return "not spinning ";
570         case 0x23:
571                 return "spinning ";
572         case 0x25:
573                 return "spindle servo ";
574         case 0x26:
575                 return "focus servo ";
576         case 0x29:
577                 return "eject mechanism ";
578         case 0x2a:
579                 return "audio playing ";
580         case 0x2c:
581                 return "emergency eject ";
582
583         case 0x30:
584                 return "focus ";
585         case 0x31:
586                 return "frame sync ";
587         case 0x32:
588                 return "subcode address ";
589         case 0x33:
590                 return "block sync ";
591         case 0x34:
592                 return "header address ";
593
594         case 0x40:
595                 return "illegal track read ";
596         case 0x41:
597                 return "mode 0 read ";
598         case 0x42:
599                 return "illegal mode read ";
600         case 0x43:
601                 return "illegal block size read ";
602         case 0x44:
603                 return "mode read ";
604         case 0x45:
605                 return "form read ";
606         case 0x46:
607                 return "leadout read ";
608         case 0x47:
609                 return "buffer overrun ";
610
611         case 0x53:
612                 return "unrecoverable CIRC ";
613         case 0x57:
614                 return "unrecoverable LECC ";
615
616         case 0x60:
617                 return "no TOC ";
618         case 0x61:
619                 return "invalid subcode data ";
620         case 0x63:
621                 return "focus on TOC read ";
622         case 0x64:
623                 return "frame sync on TOC read ";
624         case 0x65:
625                 return "TOC data ";
626
627         case 0x70:
628                 return "hardware failure ";
629         case 0x91:
630                 return "leadin ";
631         case 0x92:
632                 return "leadout ";
633         case 0x93:
634                 return "data track ";
635         }
636         sprintf(errbuf, "unknown 0x%02x ", err_code);
637         return errbuf;
638 }
639
640 /*
641  * Set the drive parameters so the drive will auto-spin-up when a
642  * disk is inserted.
643  */
644 static void set_drive_params(int want_doublespeed)
645 {
646         unsigned char res_reg[12];
647         unsigned int res_size;
648         unsigned char params[3];
649
650
651         params[0] = SONY_SD_AUTO_SPIN_DOWN_TIME;
652         params[1] = 0x00;       /* Never spin down the drive. */
653         do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
654                        params, 2, res_reg, &res_size);
655         if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20)) {
656                 printk("  Unable to set spin-down time: 0x%2.2x\n",
657                        res_reg[1]);
658         }
659
660         params[0] = SONY_SD_MECH_CONTROL;
661         params[1] = SONY_AUTO_SPIN_UP_BIT;      /* Set auto spin up */
662
663         if (is_auto_eject)
664                 params[1] |= SONY_AUTO_EJECT_BIT;
665
666         if (is_double_speed && want_doublespeed) {
667                 params[1] |= SONY_DOUBLE_SPEED_BIT;     /* Set the drive to double speed if 
668                                                            possible */
669         }
670         do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
671                        params, 2, res_reg, &res_size);
672         if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20)) {
673                 printk("  Unable to set mechanical parameters: 0x%2.2x\n",
674                        res_reg[1]);
675         }
676 }
677
678 /*
679  * Uniform cdrom interface function
680  * select reading speed for data access
681  */
682 static int scd_select_speed(struct cdrom_device_info *cdi, int speed)
683 {
684         if (speed == 0)
685                 sony_speed = 1;
686         else
687                 sony_speed = speed - 1;
688
689         set_drive_params(sony_speed);
690         return 0;
691 }
692
693 /*
694  * Uniform cdrom interface function
695  * lock or unlock eject button
696  */
697 static int scd_lock_door(struct cdrom_device_info *cdi, int lock)
698 {
699         if (lock == 0 && sony_usage == 1) {
700                 /* Unlock the door, only if nobody is using the drive */
701                 is_auto_eject = 1;
702         } else {
703                 is_auto_eject = 0;
704         }
705         set_drive_params(sony_speed);
706         return 0;
707 }
708
709 /*
710  * This code will reset the drive and attempt to restore sane parameters.
711  */
712 static void restart_on_error(void)
713 {
714         unsigned char res_reg[12];
715         unsigned int res_size;
716         unsigned long retry_count;
717
718
719         printk("cdu31a: Resetting drive on error\n");
720         reset_drive();
721         retry_count = jiffies + SONY_RESET_TIMEOUT;
722         while (time_before(jiffies, retry_count) && (!is_attention())) {
723                 sony_sleep();
724         }
725         set_drive_params(sony_speed);
726         do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg, &res_size);
727         if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20)) {
728                 printk("cdu31a: Unable to spin up drive: 0x%2.2x\n",
729                        res_reg[1]);
730         }
731
732         msleep(2000);
733
734         sony_get_toc();
735 }
736
737 /*
738  * This routine writes data to the parameter register.  Since this should
739  * happen fairly fast, it is polled with no OS waits between.
740  */
741 static int write_params(unsigned char *params, int num_params)
742 {
743         unsigned int retry_count;
744
745
746         retry_count = SONY_READY_RETRIES;
747         while ((retry_count > 0) && (!is_param_write_rdy())) {
748                 retry_count--;
749         }
750         if (!is_param_write_rdy()) {
751                 return -EIO;
752         }
753
754         while (num_params > 0) {
755                 write_param(*params);
756                 params++;
757                 num_params--;
758         }
759
760         return 0;
761 }
762
763
764 /*
765  * The following reads data from the command result register.  It is a
766  * fairly complex routine, all status info flows back through this
767  * interface.  The algorithm is stolen directly from the flowcharts in
768  * the drive manual.
769  */
770 static void
771 get_result(unsigned char *result_buffer, unsigned int *result_size)
772 {
773         unsigned char a, b;
774         int i;
775         unsigned long retry_count;
776
777
778         while (handle_sony_cd_attention());
779         /* Wait for the result data to be ready */
780         retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
781         while (time_before(jiffies, retry_count)
782                && (is_busy() || (!(is_result_ready())))) {
783                 sony_sleep();
784
785                 while (handle_sony_cd_attention());
786         }
787         if (is_busy() || (!(is_result_ready()))) {
788 #if DEBUG
789                 printk("CDU31A timeout out %d\n", __LINE__);
790 #endif
791                 result_buffer[0] = 0x20;
792                 result_buffer[1] = SONY_TIMEOUT_OP_ERR;
793                 *result_size = 2;
794                 return;
795         }
796
797         /*
798          * Get the first two bytes.  This determines what else needs
799          * to be done.
800          */
801         clear_result_ready();
802         a = read_result_register();
803         *result_buffer = a;
804         result_buffer++;
805
806         /* Check for block error status result. */
807         if ((a & 0xf0) == 0x50) {
808                 *result_size = 1;
809                 return;
810         }
811
812         b = read_result_register();
813         *result_buffer = b;
814         result_buffer++;
815         *result_size = 2;
816
817         /*
818          * 0x20 means an error occurred.  Byte 2 will have the error code.
819          * Otherwise, the command succeeded, byte 2 will have the count of
820          * how many more status bytes are coming.
821          *
822          * The result register can be read 10 bytes at a time, a wait for
823          * result ready to be asserted must be done between every 10 bytes.
824          */
825         if ((a & 0xf0) != 0x20) {
826                 if (b > 8) {
827                         for (i = 0; i < 8; i++) {
828                                 *result_buffer = read_result_register();
829                                 result_buffer++;
830                                 (*result_size)++;
831                         }
832                         b = b - 8;
833
834                         while (b > 10) {
835                                 retry_count = SONY_READY_RETRIES;
836                                 while ((retry_count > 0)
837                                        && (!is_result_ready())) {
838                                         retry_count--;
839                                 }
840                                 if (!is_result_ready()) {
841 #if DEBUG
842                                         printk("CDU31A timeout out %d\n",
843                                                __LINE__);
844 #endif
845                                         result_buffer[0] = 0x20;
846                                         result_buffer[1] =
847                                             SONY_TIMEOUT_OP_ERR;
848                                         *result_size = 2;
849                                         return;
850                                 }
851
852                                 clear_result_ready();
853
854                                 for (i = 0; i < 10; i++) {
855                                         *result_buffer =
856                                             read_result_register();
857                                         result_buffer++;
858                                         (*result_size)++;
859                                 }
860                                 b = b - 10;
861                         }
862
863                         if (b > 0) {
864                                 retry_count = SONY_READY_RETRIES;
865                                 while ((retry_count > 0)
866                                        && (!is_result_ready())) {
867                                         retry_count--;
868                                 }
869                                 if (!is_result_ready()) {
870 #if DEBUG
871                                         printk("CDU31A timeout out %d\n",
872                                                __LINE__);
873 #endif
874                                         result_buffer[0] = 0x20;
875                                         result_buffer[1] =
876                                             SONY_TIMEOUT_OP_ERR;
877                                         *result_size = 2;
878                                         return;
879                                 }
880                         }
881                 }
882
883                 while (b > 0) {
884                         *result_buffer = read_result_register();
885                         result_buffer++;
886                         (*result_size)++;
887                         b--;
888                 }
889         }
890 }
891
892 /*
893  * Do a command that does not involve data transfer.  This routine must
894  * be re-entrant from the same task to support being called from the
895  * data operation code when an error occurs.
896  */
897 static void
898 do_sony_cd_cmd(unsigned char cmd,
899                unsigned char *params,
900                unsigned int num_params,
901                unsigned char *result_buffer, unsigned int *result_size)
902 {
903         unsigned long retry_count;
904         int num_retries;
905         int recursive_call;
906         unsigned long flags;
907
908
909         save_flags(flags);
910         cli();
911         if (current != has_cd_task) {   /* Allow recursive calls to this routine */
912                 while (sony_inuse) {
913                         interruptible_sleep_on(&sony_wait);
914                         if (signal_pending(current)) {
915                                 result_buffer[0] = 0x20;
916                                 result_buffer[1] = SONY_SIGNAL_OP_ERR;
917                                 *result_size = 2;
918                                 restore_flags(flags);
919                                 return;
920                         }
921                 }
922                 sony_inuse = 1;
923                 has_cd_task = current;
924                 recursive_call = 0;
925         } else {
926                 recursive_call = 1;
927         }
928
929         num_retries = 0;
930 retry_cd_operation:
931
932         while (handle_sony_cd_attention());
933
934         sti();
935
936         retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
937         while (time_before(jiffies, retry_count) && (is_busy())) {
938                 sony_sleep();
939
940                 while (handle_sony_cd_attention());
941         }
942         if (is_busy()) {
943 #if DEBUG
944                 printk("CDU31A timeout out %d\n", __LINE__);
945 #endif
946                 result_buffer[0] = 0x20;
947                 result_buffer[1] = SONY_TIMEOUT_OP_ERR;
948                 *result_size = 2;
949         } else {
950                 clear_result_ready();
951                 clear_param_reg();
952
953                 write_params(params, num_params);
954                 write_cmd(cmd);
955
956                 get_result(result_buffer, result_size);
957         }
958
959         if (((result_buffer[0] & 0xf0) == 0x20)
960             && (num_retries < MAX_CDU31A_RETRIES)) {
961                 num_retries++;
962                 msleep(100);
963                 goto retry_cd_operation;
964         }
965
966         if (!recursive_call) {
967                 has_cd_task = NULL;
968                 sony_inuse = 0;
969                 wake_up_interruptible(&sony_wait);
970         }
971
972         restore_flags(flags);
973 }
974
975
976 /*
977  * Handle an attention from the drive.  This will return 1 if it found one
978  * or 0 if not (if one is found, the caller might want to call again).
979  *
980  * This routine counts the number of consecutive times it is called
981  * (since this is always called from a while loop until it returns
982  * a 0), and returns a 0 if it happens too many times.  This will help
983  * prevent a lockup.
984  */
985 static int handle_sony_cd_attention(void)
986 {
987         unsigned char atten_code;
988         static int num_consecutive_attentions = 0;
989         volatile int val;
990
991
992 #if 0*DEBUG
993         printk("Entering handle_sony_cd_attention\n");
994 #endif
995         if (is_attention()) {
996                 if (num_consecutive_attentions >
997                     CDU31A_MAX_CONSECUTIVE_ATTENTIONS) {
998                         printk
999                             ("cdu31a: Too many consecutive attentions: %d\n",
1000                              num_consecutive_attentions);
1001                         num_consecutive_attentions = 0;
1002 #if DEBUG
1003                         printk("Leaving handle_sony_cd_attention at %d\n",
1004                                __LINE__);
1005 #endif
1006                         return (0);
1007                 }
1008
1009                 clear_attention();
1010                 atten_code = read_result_register();
1011
1012                 switch (atten_code) {
1013                         /* Someone changed the CD.  Mark it as changed */
1014                 case SONY_MECH_LOADED_ATTN:
1015                         disk_changed = 1;
1016                         sony_toc_read = 0;
1017                         sony_audio_status = CDROM_AUDIO_NO_STATUS;
1018                         sony_blocks_left = 0;
1019                         break;
1020
1021                 case SONY_SPIN_DOWN_COMPLETE_ATTN:
1022                         /* Mark the disk as spun down. */
1023                         sony_spun_up = 0;
1024                         break;
1025
1026                 case SONY_AUDIO_PLAY_DONE_ATTN:
1027                         sony_audio_status = CDROM_AUDIO_COMPLETED;
1028                         read_subcode();
1029                         break;
1030
1031                 case SONY_EJECT_PUSHED_ATTN:
1032                         if (is_auto_eject) {
1033                                 sony_audio_status = CDROM_AUDIO_INVALID;
1034                         }
1035                         break;
1036
1037                 case SONY_LEAD_IN_ERR_ATTN:
1038                 case SONY_LEAD_OUT_ERR_ATTN:
1039                 case SONY_DATA_TRACK_ERR_ATTN:
1040                 case SONY_AUDIO_PLAYBACK_ERR_ATTN:
1041                         sony_audio_status = CDROM_AUDIO_ERROR;
1042                         break;
1043                 }
1044
1045                 num_consecutive_attentions++;
1046 #if DEBUG
1047                 printk("Leaving handle_sony_cd_attention at %d\n",
1048                        __LINE__);
1049 #endif
1050                 return (1);
1051         } else if (abort_read_started) {
1052                 while (is_result_reg_not_empty()) {
1053                         val = read_result_register();
1054                 }
1055                 clear_data_ready();
1056                 clear_result_ready();
1057                 /* Clear out the data */
1058                 while (is_data_requested()) {
1059                         val = read_data_register();
1060                 }
1061                 abort_read_started = 0;
1062 #if DEBUG
1063                 printk("Leaving handle_sony_cd_attention at %d\n",
1064                        __LINE__);
1065 #endif
1066                 return (1);
1067         }
1068
1069         num_consecutive_attentions = 0;
1070 #if 0*DEBUG
1071         printk("Leaving handle_sony_cd_attention at %d\n", __LINE__);
1072 #endif
1073         return (0);
1074 }
1075
1076
1077 /* Convert from an integer 0-99 to BCD */
1078 static inline unsigned int int_to_bcd(unsigned int val)
1079 {
1080         int retval;
1081
1082
1083         retval = (val / 10) << 4;
1084         retval = retval | val % 10;
1085         return (retval);
1086 }
1087
1088
1089 /* Convert from BCD to an integer from 0-99 */
1090 static unsigned int bcd_to_int(unsigned int bcd)
1091 {
1092         return ((((bcd >> 4) & 0x0f) * 10) + (bcd & 0x0f));
1093 }
1094
1095
1096 /*
1097  * Convert a logical sector value (like the OS would want to use for
1098  * a block device) to an MSF format.
1099  */
1100 static void log_to_msf(unsigned int log, unsigned char *msf)
1101 {
1102         log = log + LOG_START_OFFSET;
1103         msf[0] = int_to_bcd(log / 4500);
1104         log = log % 4500;
1105         msf[1] = int_to_bcd(log / 75);
1106         msf[2] = int_to_bcd(log % 75);
1107 }
1108
1109
1110 /*
1111  * Convert an MSF format to a logical sector.
1112  */
1113 static unsigned int msf_to_log(unsigned char *msf)
1114 {
1115         unsigned int log;
1116
1117
1118         log = msf[2];
1119         log += msf[1] * 75;
1120         log += msf[0] * 4500;
1121         log = log - LOG_START_OFFSET;
1122
1123         return log;
1124 }
1125
1126
1127 /*
1128  * Take in integer size value and put it into a buffer like
1129  * the drive would want to see a number-of-sector value.
1130  */
1131 static void size_to_buf(unsigned int size, unsigned char *buf)
1132 {
1133         buf[0] = size / 65536;
1134         size = size % 65536;
1135         buf[1] = size / 256;
1136         buf[2] = size % 256;
1137 }
1138
1139 /* Starts a read operation. Returns 0 on success and 1 on failure. 
1140    The read operation used here allows multiple sequential sectors 
1141    to be read and status returned for each sector.  The driver will
1142    read the output one at a time as the requests come and abort the
1143    operation if the requested sector is not the next one from the
1144    drive. */
1145 static int
1146 start_request(unsigned int sector, unsigned int nsect, int read_nsect_only)
1147 {
1148         unsigned char params[6];
1149         unsigned int read_size;
1150         unsigned long retry_count;
1151
1152
1153 #if DEBUG
1154         printk("Entering start_request\n");
1155 #endif
1156         log_to_msf(sector, params);
1157         /* If requested, read exactly what was asked. */
1158         if (read_nsect_only) {
1159                 read_size = nsect;
1160         }
1161         /*
1162          * If the full read-ahead would go beyond the end of the media, trim
1163          * it back to read just till the end of the media.
1164          */
1165         else if ((sector + nsect) >= sony_toc.lead_out_start_lba) {
1166                 read_size = sony_toc.lead_out_start_lba - sector;
1167         }
1168         /* Read the full readahead amount. */
1169         else {
1170                 read_size = CDU31A_READAHEAD / 4;
1171         }
1172         size_to_buf(read_size, &params[3]);
1173
1174         /*
1175          * Clear any outstanding attentions and wait for the drive to
1176          * complete any pending operations.
1177          */
1178         while (handle_sony_cd_attention());
1179
1180         retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
1181         while (time_before(jiffies, retry_count) && (is_busy())) {
1182                 sony_sleep();
1183
1184                 while (handle_sony_cd_attention());
1185         }
1186
1187         if (is_busy()) {
1188                 printk("CDU31A: Timeout while waiting to issue command\n");
1189 #if DEBUG
1190                 printk("Leaving start_request at %d\n", __LINE__);
1191 #endif
1192                 return (1);
1193         } else {
1194                 /* Issue the command */
1195                 clear_result_ready();
1196                 clear_param_reg();
1197
1198                 write_params(params, 6);
1199                 write_cmd(SONY_READ_BLKERR_STAT_CMD);
1200
1201                 sony_blocks_left = read_size * 4;
1202                 sony_next_block = sector * 4;
1203                 readahead_dataleft = 0;
1204                 readahead_bad = 0;
1205 #if DEBUG
1206                 printk("Leaving start_request at %d\n", __LINE__);
1207 #endif
1208                 return (0);
1209         }
1210 #if DEBUG
1211         printk("Leaving start_request at %d\n", __LINE__);
1212 #endif
1213 }
1214
1215 /* Abort a pending read operation.  Clear all the drive status and
1216    readahead variables. */
1217 static void abort_read(void)
1218 {
1219         unsigned char result_reg[2];
1220         int result_size;
1221         volatile int val;
1222
1223
1224         do_sony_cd_cmd(SONY_ABORT_CMD, NULL, 0, result_reg, &result_size);
1225         if ((result_reg[0] & 0xf0) == 0x20) {
1226                 printk("CDU31A: Error aborting read, %s error\n",
1227                        translate_error(result_reg[1]));
1228         }
1229
1230         while (is_result_reg_not_empty()) {
1231                 val = read_result_register();
1232         }
1233         clear_data_ready();
1234         clear_result_ready();
1235         /* Clear out the data */
1236         while (is_data_requested()) {
1237                 val = read_data_register();
1238         }
1239
1240         sony_blocks_left = 0;
1241         readahead_dataleft = 0;
1242         readahead_bad = 0;
1243 }
1244
1245 /* Called when the timer times out.  This will abort the
1246    pending read operation. */
1247 static void handle_abort_timeout(unsigned long data)
1248 {
1249         unsigned long flags;
1250
1251 #if DEBUG
1252         printk("Entering handle_abort_timeout\n");
1253 #endif
1254         save_flags(flags);
1255         cli();
1256         /* If it is in use, ignore it. */
1257         if (!sony_inuse) {
1258                 /* We can't use abort_read(), because it will sleep
1259                    or schedule in the timer interrupt.  Just start
1260                    the operation, finish it on the next access to
1261                    the drive. */
1262                 clear_result_ready();
1263                 clear_param_reg();
1264                 write_cmd(SONY_ABORT_CMD);
1265
1266                 sony_blocks_left = 0;
1267                 readahead_dataleft = 0;
1268                 readahead_bad = 0;
1269                 abort_read_started = 1;
1270         }
1271         restore_flags(flags);
1272 #if DEBUG
1273         printk("Leaving handle_abort_timeout\n");
1274 #endif
1275 }
1276
1277 /* Actually get data and status from the drive. */
1278 static void
1279 input_data(char *buffer,
1280            unsigned int bytesleft,
1281            unsigned int nblocks, unsigned int offset, unsigned int skip)
1282 {
1283         int i;
1284         volatile unsigned char val;
1285
1286
1287 #if DEBUG
1288         printk("Entering input_data\n");
1289 #endif
1290         /* If an XA disk on a CDU31A, skip the first 12 bytes of data from
1291            the disk.  The real data is after that. */
1292         if (sony_xa_mode) {
1293                 for (i = 0; i < CD_XA_HEAD; i++) {
1294                         val = read_data_register();
1295                 }
1296         }
1297
1298         clear_data_ready();
1299
1300         if (bytesleft == 2048) {        /* 2048 byte direct buffer transfer */
1301                 insb(sony_cd_read_reg, buffer, 2048);
1302                 readahead_dataleft = 0;
1303         } else {
1304                 /* If the input read did not align with the beginning of the block,
1305                    skip the necessary bytes. */
1306                 if (skip != 0) {
1307                         insb(sony_cd_read_reg, readahead_buffer, skip);
1308                 }
1309
1310                 /* Get the data into the buffer. */
1311                 insb(sony_cd_read_reg, &buffer[offset], bytesleft);
1312
1313                 /* Get the rest of the data into the readahead buffer at the
1314                    proper location. */
1315                 readahead_dataleft = (2048 - skip) - bytesleft;
1316                 insb(sony_cd_read_reg,
1317                      readahead_buffer + bytesleft, readahead_dataleft);
1318         }
1319         sony_blocks_left -= nblocks;
1320         sony_next_block += nblocks;
1321
1322         /* If an XA disk, we have to clear out the rest of the unused
1323            error correction data. */
1324         if (sony_xa_mode) {
1325                 for (i = 0; i < CD_XA_TAIL; i++) {
1326                         val = read_data_register();
1327                 }
1328         }
1329 #if DEBUG
1330         printk("Leaving input_data at %d\n", __LINE__);
1331 #endif
1332 }
1333
1334 /* read data from the drive.  Note the nsect must be <= 4. */
1335 static void
1336 read_data_block(char *buffer,
1337                 unsigned int block,
1338                 unsigned int nblocks,
1339                 unsigned char res_reg[], int *res_size)
1340 {
1341         unsigned long retry_count;
1342         unsigned int bytesleft;
1343         unsigned int offset;
1344         unsigned int skip;
1345
1346
1347 #if DEBUG
1348         printk("Entering read_data_block\n");
1349 #endif
1350
1351         res_reg[0] = 0;
1352         res_reg[1] = 0;
1353         *res_size = 0;
1354         bytesleft = nblocks * 512;
1355         offset = 0;
1356
1357         /* If the data in the read-ahead does not match the block offset,
1358            then fix things up. */
1359         if (((block % 4) * 512) != ((2048 - readahead_dataleft) % 2048)) {
1360                 sony_next_block += block % 4;
1361                 sony_blocks_left -= block % 4;
1362                 skip = (block % 4) * 512;
1363         } else {
1364                 skip = 0;
1365         }
1366
1367         /* We have readahead data in the buffer, get that first before we
1368            decide if a read is necessary. */
1369         if (readahead_dataleft != 0) {
1370                 if (bytesleft > readahead_dataleft) {
1371                         /* The readahead will not fill the requested buffer, but
1372                            get the data out of the readahead into the buffer. */
1373                         memcpy(buffer,
1374                                readahead_buffer + (2048 -
1375                                                    readahead_dataleft),
1376                                readahead_dataleft);
1377                         bytesleft -= readahead_dataleft;
1378                         offset += readahead_dataleft;
1379                         readahead_dataleft = 0;
1380                 } else {
1381                         /* The readahead will fill the whole buffer, get the data
1382                            and return. */
1383                         memcpy(buffer,
1384                                readahead_buffer + (2048 -
1385                                                    readahead_dataleft),
1386                                bytesleft);
1387                         readahead_dataleft -= bytesleft;
1388                         bytesleft = 0;
1389                         sony_blocks_left -= nblocks;
1390                         sony_next_block += nblocks;
1391
1392                         /* If the data in the readahead is bad, return an error so the
1393                            driver will abort the buffer. */
1394                         if (readahead_bad) {
1395                                 res_reg[0] = 0x20;
1396                                 res_reg[1] = SONY_BAD_DATA_ERR;
1397                                 *res_size = 2;
1398                         }
1399
1400                         if (readahead_dataleft == 0) {
1401                                 readahead_bad = 0;
1402                         }
1403
1404                         /* Final transfer is done for read command, get final result. */
1405                         if (sony_blocks_left == 0) {
1406                                 get_result(res_reg, res_size);
1407                         }
1408 #if DEBUG
1409                         printk("Leaving read_data_block at %d\n",
1410                                __LINE__);
1411 #endif
1412                         return;
1413                 }
1414         }
1415
1416         /* Wait for the drive to tell us we have something */
1417         retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
1418         while (time_before(jiffies, retry_count) && !(is_data_ready())) {
1419                 while (handle_sony_cd_attention());
1420
1421                 sony_sleep();
1422         }
1423         if (!(is_data_ready())) {
1424                 if (is_result_ready()) {
1425                         get_result(res_reg, res_size);
1426                         if ((res_reg[0] & 0xf0) != 0x20) {
1427                                 printk
1428                                     ("CDU31A: Got result that should have been error: %d\n",
1429                                      res_reg[0]);
1430                                 res_reg[0] = 0x20;
1431                                 res_reg[1] = SONY_BAD_DATA_ERR;
1432                                 *res_size = 2;
1433                         }
1434                         abort_read();
1435                 } else {
1436 #if DEBUG
1437                         printk("CDU31A timeout out %d\n", __LINE__);
1438 #endif
1439                         res_reg[0] = 0x20;
1440                         res_reg[1] = SONY_TIMEOUT_OP_ERR;
1441                         *res_size = 2;
1442                         abort_read();
1443                 }
1444         } else {
1445                 input_data(buffer, bytesleft, nblocks, offset, skip);
1446
1447                 /* Wait for the status from the drive. */
1448                 retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
1449                 while (time_before(jiffies, retry_count)
1450                        && !(is_result_ready())) {
1451                         while (handle_sony_cd_attention());
1452
1453                         sony_sleep();
1454                 }
1455
1456                 if (!is_result_ready()) {
1457 #if DEBUG
1458                         printk("CDU31A timeout out %d\n", __LINE__);
1459 #endif
1460                         res_reg[0] = 0x20;
1461                         res_reg[1] = SONY_TIMEOUT_OP_ERR;
1462                         *res_size = 2;
1463                         abort_read();
1464                 } else {
1465                         get_result(res_reg, res_size);
1466
1467                         /* If we got a buffer status, handle that. */
1468                         if ((res_reg[0] & 0xf0) == 0x50) {
1469
1470                                 if ((res_reg[0] ==
1471                                      SONY_NO_CIRC_ERR_BLK_STAT)
1472                                     || (res_reg[0] ==
1473                                         SONY_NO_LECC_ERR_BLK_STAT)
1474                                     || (res_reg[0] ==
1475                                         SONY_RECOV_LECC_ERR_BLK_STAT)) {
1476                                         /* The data was successful, but if data was read from
1477                                            the readahead  and it was bad, set the whole
1478                                            buffer as bad. */
1479                                         if (readahead_bad) {
1480                                                 readahead_bad = 0;
1481                                                 res_reg[0] = 0x20;
1482                                                 res_reg[1] =
1483                                                     SONY_BAD_DATA_ERR;
1484                                                 *res_size = 2;
1485                                         }
1486                                 } else {
1487                                         printk
1488                                             ("CDU31A: Data block error: 0x%x\n",
1489                                              res_reg[0]);
1490                                         res_reg[0] = 0x20;
1491                                         res_reg[1] = SONY_BAD_DATA_ERR;
1492                                         *res_size = 2;
1493
1494                                         /* Data is in the readahead buffer but an error was returned.
1495                                            Make sure future requests don't use the data. */
1496                                         if (bytesleft != 2048) {
1497                                                 readahead_bad = 1;
1498                                         }
1499                                 }
1500
1501                                 /* Final transfer is done for read command, get final result. */
1502                                 if (sony_blocks_left == 0) {
1503                                         get_result(res_reg, res_size);
1504                                 }
1505                         } else if ((res_reg[0] & 0xf0) != 0x20) {
1506                                 /* The drive gave me bad status, I don't know what to do.
1507                                    Reset the driver and return an error. */
1508                                 printk
1509                                     ("CDU31A: Invalid block status: 0x%x\n",
1510                                      res_reg[0]);
1511                                 restart_on_error();
1512                                 res_reg[0] = 0x20;
1513                                 res_reg[1] = SONY_BAD_DATA_ERR;
1514                                 *res_size = 2;
1515                         }
1516                 }
1517         }
1518 #if DEBUG
1519         printk("Leaving read_data_block at %d\n", __LINE__);
1520 #endif
1521 }
1522
1523
1524 /*
1525  * The OS calls this to perform a read or write operation to the drive.
1526  * Write obviously fail.  Reads to a read ahead of sony_buffer_size
1527  * bytes to help speed operations.  This especially helps since the OS
1528  * uses 1024 byte blocks and the drive uses 2048 byte blocks.  Since most
1529  * data access on a CD is done sequentially, this saves a lot of operations.
1530  */
1531 static void do_cdu31a_request(request_queue_t * q)
1532 {
1533         struct request *req;
1534         int block;
1535         int nblock;
1536         unsigned char res_reg[12];
1537         unsigned int res_size;
1538         int num_retries;
1539         unsigned long flags;
1540
1541
1542 #if DEBUG
1543         printk("Entering do_cdu31a_request\n");
1544 #endif
1545
1546         /* 
1547          * Make sure no one else is using the driver; wait for them
1548          * to finish if it is so.
1549          */
1550         save_flags(flags);
1551         cli();
1552         while (sony_inuse) {
1553                 interruptible_sleep_on(&sony_wait);
1554                 if (signal_pending(current)) {
1555                         restore_flags(flags);
1556 #if DEBUG
1557                         printk("Leaving do_cdu31a_request at %d\n",
1558                                __LINE__);
1559 #endif
1560                         return;
1561                 }
1562         }
1563         sony_inuse = 1;
1564         has_cd_task = current;
1565
1566         /* Get drive status before doing anything. */
1567         while (handle_sony_cd_attention());
1568
1569         /* Make sure we have a valid TOC. */
1570         sony_get_toc();
1571
1572         /*
1573          * jens: driver has lots of races
1574          */
1575         spin_unlock_irq(q->queue_lock);
1576
1577         /* Make sure the timer is cancelled. */
1578         del_timer(&cdu31a_abort_timer);
1579
1580         while (1) {
1581                 /*
1582                  * The beginning here is stolen from the hard disk driver.  I hope
1583                  * it's right.
1584                  */
1585                 req = elv_next_request(q);
1586                 if (!req)
1587                         goto end_do_cdu31a_request;
1588
1589                 if (!sony_spun_up)
1590                         scd_spinup();
1591
1592                 block = req->sector;
1593                 nblock = req->nr_sectors;
1594
1595                 if (!sony_toc_read) {
1596                         printk("CDU31A: TOC not read\n");
1597                         end_request(req, 0);
1598                         continue;
1599                 }
1600
1601                 /* WTF??? */
1602                 if (!(req->flags & REQ_CMD))
1603                         continue;
1604                 if (rq_data_dir(req) == WRITE) {
1605                         end_request(req, 0);
1606                         continue;
1607                 }
1608                 if (rq_data_dir(req) != READ)
1609                         panic("CDU31A: Unknown cmd");
1610                 /*
1611                  * If the block address is invalid or the request goes beyond the end of
1612                  * the media, return an error.
1613                  */
1614                 if ((block / 4) >= sony_toc.lead_out_start_lba) {
1615                         printk("CDU31A: Request past end of media\n");
1616                         end_request(req, 0);
1617                         continue;
1618                 }
1619                 if (((block + nblock) / 4) >= sony_toc.lead_out_start_lba) {
1620                         printk("CDU31A: Request past end of media\n");
1621                         end_request(req, 0);
1622                         continue;
1623                 }
1624
1625                 num_retries = 0;
1626
1627         try_read_again:
1628                 while (handle_sony_cd_attention());
1629
1630                 if (!sony_toc_read) {
1631                         printk("CDU31A: TOC not read\n");
1632                         end_request(req, 0);
1633                         continue;
1634                 }
1635
1636                 /* If no data is left to be read from the drive, start the
1637                    next request. */
1638                 if (sony_blocks_left == 0) {
1639                         if (start_request(block / 4, CDU31A_READAHEAD / 4, 0)) {
1640                                 end_request(req, 0);
1641                                 continue;
1642                         }
1643                 }
1644                 /* If the requested block is not the next one waiting in
1645                    the driver, abort the current operation and start a
1646                    new one. */
1647                 else if (block != sony_next_block) {
1648 #if DEBUG
1649                         printk("CDU31A Warning: Read for block %d, expected %d\n",
1650                                  block, sony_next_block);
1651 #endif
1652                         abort_read();
1653                         if (!sony_toc_read) {
1654                                 printk("CDU31A: TOC not read\n");
1655                                 end_request(req, 0);
1656                                 continue;
1657                         }
1658                         if (start_request(block / 4, CDU31A_READAHEAD / 4, 0)) {
1659                                 printk("CDU31a: start request failed\n");
1660                                 end_request(req, 0);
1661                                 continue;
1662                         }
1663                 }
1664
1665                 read_data_block(req->buffer, block, nblock, res_reg, &res_size);
1666
1667                 if (res_reg[0] != 0x20) {
1668                         end_request(req, 1);
1669                         continue;
1670                 }
1671
1672                 if (num_retries > MAX_CDU31A_RETRIES) {
1673                         end_request(req, 0);
1674                         continue;
1675                 }
1676
1677                 num_retries++;
1678                 if (res_reg[1] == SONY_NOT_SPIN_ERR) {
1679                         do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg,
1680                                         &res_size);
1681                 } else {
1682                         printk("CDU31A: %s error for block %d, nblock %d\n",
1683                                  translate_error(res_reg[1]), block, nblock);
1684                 }
1685                 goto try_read_again;
1686         }
1687       end_do_cdu31a_request:
1688         spin_lock_irq(q->queue_lock);
1689 #if 0
1690         /* After finished, cancel any pending operations. */
1691         abort_read();
1692 #else
1693         /* Start a timer to time out after a while to disable
1694            the read. */
1695         cdu31a_abort_timer.expires = jiffies + 2 * HZ;  /* Wait 2 seconds */
1696         add_timer(&cdu31a_abort_timer);
1697 #endif
1698
1699         has_cd_task = NULL;
1700         sony_inuse = 0;
1701         wake_up_interruptible(&sony_wait);
1702         restore_flags(flags);
1703 #if DEBUG
1704         printk("Leaving do_cdu31a_request at %d\n", __LINE__);
1705 #endif
1706 }
1707
1708
1709 /*
1710  * Read the table of contents from the drive and set up TOC if
1711  * successful.
1712  */
1713 static void sony_get_toc(void)
1714 {
1715         unsigned char res_reg[2];
1716         unsigned int res_size;
1717         unsigned char parms[1];
1718         int session;
1719         int num_spin_ups;
1720         int totaltracks = 0;
1721         int mint = 99;
1722         int maxt = 0;
1723
1724 #if DEBUG
1725         printk("Entering sony_get_toc\n");
1726 #endif
1727
1728         num_spin_ups = 0;
1729         if (!sony_toc_read) {
1730               respinup_on_gettoc:
1731                 /* Ignore the result, since it might error if spinning already. */
1732                 do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg,
1733                                &res_size);
1734
1735                 do_sony_cd_cmd(SONY_READ_TOC_CMD, NULL, 0, res_reg,
1736                                &res_size);
1737
1738                 /* The drive sometimes returns error 0.  I don't know why, but ignore
1739                    it.  It seems to mean the drive has already done the operation. */
1740                 if ((res_size < 2)
1741                     || ((res_reg[0] != 0) && (res_reg[1] != 0))) {
1742                         /* If the drive is already playing, it's ok.  */
1743                         if ((res_reg[1] == SONY_AUDIO_PLAYING_ERR)
1744                             || (res_reg[1] == 0)) {
1745                                 goto gettoc_drive_spinning;
1746                         }
1747
1748                         /* If the drive says it is not spun up (even though we just did it!)
1749                            then retry the operation at least a few times. */
1750                         if ((res_reg[1] == SONY_NOT_SPIN_ERR)
1751                             && (num_spin_ups < MAX_CDU31A_RETRIES)) {
1752                                 num_spin_ups++;
1753                                 goto respinup_on_gettoc;
1754                         }
1755
1756                         printk("cdu31a: Error reading TOC: %x %s\n",
1757                                res_reg[0], translate_error(res_reg[1]));
1758                         return;
1759                 }
1760
1761               gettoc_drive_spinning:
1762
1763                 /* The idea here is we keep asking for sessions until the command
1764                    fails.  Then we know what the last valid session on the disk is.
1765                    No need to check session 0, since session 0 is the same as session
1766                    1; the command returns different information if you give it 0. 
1767                  */
1768 #if DEBUG
1769                 memset(&sony_toc, 0x0e, sizeof(sony_toc));
1770                 memset(&single_toc, 0x0f, sizeof(single_toc));
1771 #endif
1772                 session = 1;
1773                 while (1) {
1774 /* This seems to slow things down enough to make it work.  This
1775  * appears to be a problem in do_sony_cd_cmd.  This printk seems 
1776  * to address the symptoms...  -Erik */
1777 #if 1
1778                         printk("cdu31a: Trying session %d\n", session);
1779 #endif
1780                         parms[0] = session;
1781                         do_sony_cd_cmd(SONY_READ_TOC_SPEC_CMD,
1782                                        parms, 1, res_reg, &res_size);
1783
1784 #if DEBUG
1785                         printk("%2.2x %2.2x\n", res_reg[0], res_reg[1]);
1786 #endif
1787
1788                         if ((res_size < 2)
1789                             || ((res_reg[0] & 0xf0) == 0x20)) {
1790                                 /* An error reading the TOC, this must be past the last session. */
1791                                 if (session == 1)
1792                                         printk
1793                                             ("Yikes! Couldn't read any sessions!");
1794                                 break;
1795                         }
1796 #if DEBUG
1797                         printk("Reading session %d\n", session);
1798 #endif
1799
1800                         parms[0] = session;
1801                         do_sony_cd_cmd(SONY_REQ_TOC_DATA_SPEC_CMD,
1802                                        parms,
1803                                        1,
1804                                        (unsigned char *) &single_toc,
1805                                        &res_size);
1806                         if ((res_size < 2)
1807                             || ((single_toc.exec_status[0] & 0xf0) ==
1808                                 0x20)) {
1809                                 printk
1810                                     ("cdu31a: Error reading session %d: %x %s\n",
1811                                      session, single_toc.exec_status[0],
1812                                      translate_error(single_toc.
1813                                                      exec_status[1]));
1814                                 /* An error reading the TOC.  Return without sony_toc_read
1815                                    set. */
1816                                 return;
1817                         }
1818 #if DEBUG
1819                         printk
1820                             ("add0 %01x, con0 %01x, poi0 %02x, 1st trk %d, dsktyp %x, dum0 %x\n",
1821                              single_toc.address0, single_toc.control0,
1822                              single_toc.point0,
1823                              bcd_to_int(single_toc.first_track_num),
1824                              single_toc.disk_type, single_toc.dummy0);
1825                         printk
1826                             ("add1 %01x, con1 %01x, poi1 %02x, lst trk %d, dummy1 %x, dum2 %x\n",
1827                              single_toc.address1, single_toc.control1,
1828                              single_toc.point1,
1829                              bcd_to_int(single_toc.last_track_num),
1830                              single_toc.dummy1, single_toc.dummy2);
1831                         printk
1832                             ("add2 %01x, con2 %01x, poi2 %02x leadout start min %d, sec %d, frame %d\n",
1833                              single_toc.address2, single_toc.control2,
1834                              single_toc.point2,
1835                              bcd_to_int(single_toc.lead_out_start_msf[0]),
1836                              bcd_to_int(single_toc.lead_out_start_msf[1]),
1837                              bcd_to_int(single_toc.lead_out_start_msf[2]));
1838                         if (res_size > 18 && single_toc.pointb0 > 0xaf)
1839                                 printk
1840                                     ("addb0 %01x, conb0 %01x, poib0 %02x, nextsession min %d, sec %d, frame %d\n"
1841                                      "#mode5_ptrs %02d, max_start_outer_leadout_msf min %d, sec %d, frame %d\n",
1842                                      single_toc.addressb0,
1843                                      single_toc.controlb0,
1844                                      single_toc.pointb0,
1845                                      bcd_to_int(single_toc.
1846                                                 next_poss_prog_area_msf
1847                                                 [0]),
1848                                      bcd_to_int(single_toc.
1849                                                 next_poss_prog_area_msf
1850                                                 [1]),
1851                                      bcd_to_int(single_toc.
1852                                                 next_poss_prog_area_msf
1853                                                 [2]),
1854                                      single_toc.num_mode_5_pointers,
1855                                      bcd_to_int(single_toc.
1856                                                 max_start_outer_leadout_msf
1857                                                 [0]),
1858                                      bcd_to_int(single_toc.
1859                                                 max_start_outer_leadout_msf
1860                                                 [1]),
1861                                      bcd_to_int(single_toc.
1862                                                 max_start_outer_leadout_msf
1863                                                 [2]));
1864                         if (res_size > 27 && single_toc.pointb1 > 0xaf)
1865                                 printk
1866                                     ("addb1 %01x, conb1 %01x, poib1 %02x, %x %x %x %x #skipint_ptrs %d, #skiptrkassign %d %x\n",
1867                                      single_toc.addressb1,
1868                                      single_toc.controlb1,
1869                                      single_toc.pointb1,
1870                                      single_toc.dummyb0_1[0],
1871                                      single_toc.dummyb0_1[1],
1872                                      single_toc.dummyb0_1[2],
1873                                      single_toc.dummyb0_1[3],
1874                                      single_toc.num_skip_interval_pointers,
1875                                      single_toc.num_skip_track_assignments,
1876                                      single_toc.dummyb0_2);
1877                         if (res_size > 36 && single_toc.pointb2 > 0xaf)
1878                                 printk
1879                                     ("addb2 %01x, conb2 %01x, poib2 %02x, %02x %02x %02x %02x %02x %02x %02x\n",
1880                                      single_toc.addressb2,
1881                                      single_toc.controlb2,
1882                                      single_toc.pointb2,
1883                                      single_toc.tracksb2[0],
1884                                      single_toc.tracksb2[1],
1885                                      single_toc.tracksb2[2],
1886                                      single_toc.tracksb2[3],
1887                                      single_toc.tracksb2[4],
1888                                      single_toc.tracksb2[5],
1889                                      single_toc.tracksb2[6]);
1890                         if (res_size > 45 && single_toc.pointb3 > 0xaf)
1891                                 printk
1892                                     ("addb3 %01x, conb3 %01x, poib3 %02x, %02x %02x %02x %02x %02x %02x %02x\n",
1893                                      single_toc.addressb3,
1894                                      single_toc.controlb3,
1895                                      single_toc.pointb3,
1896                                      single_toc.tracksb3[0],
1897                                      single_toc.tracksb3[1],
1898                                      single_toc.tracksb3[2],
1899                                      single_toc.tracksb3[3],
1900                                      single_toc.tracksb3[4],
1901                                      single_toc.tracksb3[5],
1902                                      single_toc.tracksb3[6]);
1903                         if (res_size > 54 && single_toc.pointb4 > 0xaf)
1904                                 printk
1905                                     ("addb4 %01x, conb4 %01x, poib4 %02x, %02x %02x %02x %02x %02x %02x %02x\n",
1906                                      single_toc.addressb4,
1907                                      single_toc.controlb4,
1908                                      single_toc.pointb4,
1909                                      single_toc.tracksb4[0],
1910                                      single_toc.tracksb4[1],
1911                                      single_toc.tracksb4[2],
1912                                      single_toc.tracksb4[3],
1913                                      single_toc.tracksb4[4],
1914                                      single_toc.tracksb4[5],
1915                                      single_toc.tracksb4[6]);
1916                         if (res_size > 63 && single_toc.pointc0 > 0xaf)
1917                                 printk
1918                                     ("addc0 %01x, conc0 %01x, poic0 %02x, %02x %02x %02x %02x %02x %02x %02x\n",
1919                                      single_toc.addressc0,
1920                                      single_toc.controlc0,
1921                                      single_toc.pointc0,
1922                                      single_toc.dummyc0[0],
1923                                      single_toc.dummyc0[1],
1924                                      single_toc.dummyc0[2],
1925                                      single_toc.dummyc0[3],
1926                                      single_toc.dummyc0[4],
1927                                      single_toc.dummyc0[5],
1928                                      single_toc.dummyc0[6]);
1929 #endif
1930 #undef DEBUG
1931 #define DEBUG 0
1932
1933                         sony_toc.lead_out_start_msf[0] =
1934                             bcd_to_int(single_toc.lead_out_start_msf[0]);
1935                         sony_toc.lead_out_start_msf[1] =
1936                             bcd_to_int(single_toc.lead_out_start_msf[1]);
1937                         sony_toc.lead_out_start_msf[2] =
1938                             bcd_to_int(single_toc.lead_out_start_msf[2]);
1939                         sony_toc.lead_out_start_lba =
1940                             single_toc.lead_out_start_lba =
1941                             msf_to_log(sony_toc.lead_out_start_msf);
1942
1943                         /* For points that do not exist, move the data over them
1944                            to the right location. */
1945                         if (single_toc.pointb0 != 0xb0) {
1946                                 memmove(((char *) &single_toc) + 27,
1947                                         ((char *) &single_toc) + 18,
1948                                         res_size - 18);
1949                                 res_size += 9;
1950                         } else if (res_size > 18) {
1951                                 sony_toc.lead_out_start_msf[0] =
1952                                     bcd_to_int(single_toc.
1953                                                max_start_outer_leadout_msf
1954                                                [0]);
1955                                 sony_toc.lead_out_start_msf[1] =
1956                                     bcd_to_int(single_toc.
1957                                                max_start_outer_leadout_msf
1958                                                [1]);
1959                                 sony_toc.lead_out_start_msf[2] =
1960                                     bcd_to_int(single_toc.
1961                                                max_start_outer_leadout_msf
1962                                                [2]);
1963                                 sony_toc.lead_out_start_lba =
1964                                     msf_to_log(sony_toc.
1965                                                lead_out_start_msf);
1966                         }
1967                         if (single_toc.pointb1 != 0xb1) {
1968                                 memmove(((char *) &single_toc) + 36,
1969                                         ((char *) &single_toc) + 27,
1970                                         res_size - 27);
1971                                 res_size += 9;
1972                         }
1973                         if (single_toc.pointb2 != 0xb2) {
1974                                 memmove(((char *) &single_toc) + 45,
1975                                         ((char *) &single_toc) + 36,
1976                                         res_size - 36);
1977                                 res_size += 9;
1978                         }
1979                         if (single_toc.pointb3 != 0xb3) {
1980                                 memmove(((char *) &single_toc) + 54,
1981                                         ((char *) &single_toc) + 45,
1982                                         res_size - 45);
1983                                 res_size += 9;
1984                         }
1985                         if (single_toc.pointb4 != 0xb4) {
1986                                 memmove(((char *) &single_toc) + 63,
1987                                         ((char *) &single_toc) + 54,
1988                                         res_size - 54);
1989                                 res_size += 9;
1990                         }
1991                         if (single_toc.pointc0 != 0xc0) {
1992                                 memmove(((char *) &single_toc) + 72,
1993                                         ((char *) &single_toc) + 63,
1994                                         res_size - 63);
1995                                 res_size += 9;
1996                         }
1997 #if DEBUG
1998                         printk
1999                             ("start track lba %u,  leadout start lba %u\n",
2000                              single_toc.start_track_lba,
2001                              single_toc.lead_out_start_lba);
2002                         {
2003                                 int i;
2004                                 for (i = 0;
2005                                      i <
2006                                      1 +
2007                                      bcd_to_int(single_toc.last_track_num)
2008                                      -
2009                                      bcd_to_int(single_toc.
2010                                                 first_track_num); i++) {
2011                                         printk
2012                                             ("trk %02d: add 0x%01x, con 0x%01x,  track %02d, start min %02d, sec %02d, frame %02d\n",
2013                                              i,
2014                                              single_toc.tracks[i].address,
2015                                              single_toc.tracks[i].control,
2016                                              bcd_to_int(single_toc.
2017                                                         tracks[i].track),
2018                                              bcd_to_int(single_toc.
2019                                                         tracks[i].
2020                                                         track_start_msf
2021                                                         [0]),
2022                                              bcd_to_int(single_toc.
2023                                                         tracks[i].
2024                                                         track_start_msf
2025                                                         [1]),
2026                                              bcd_to_int(single_toc.
2027                                                         tracks[i].
2028                                                         track_start_msf
2029                                                         [2]));
2030                                         if (mint >
2031                                             bcd_to_int(single_toc.
2032                                                        tracks[i].track))
2033                                                 mint =
2034                                                     bcd_to_int(single_toc.
2035                                                                tracks[i].
2036                                                                track);
2037                                         if (maxt <
2038                                             bcd_to_int(single_toc.
2039                                                        tracks[i].track))
2040                                                 maxt =
2041                                                     bcd_to_int(single_toc.
2042                                                                tracks[i].
2043                                                                track);
2044                                 }
2045                                 printk
2046                                     ("min track number %d,   max track number %d\n",
2047                                      mint, maxt);
2048                         }
2049 #endif
2050
2051                         /* prepare a special table of contents for a CD-I disc. They don't have one. */
2052                         if (single_toc.disk_type == 0x10 &&
2053                             single_toc.first_track_num == 2 &&
2054                             single_toc.last_track_num == 2 /* CD-I */ ) {
2055                                 sony_toc.tracks[totaltracks].address = 1;
2056                                 sony_toc.tracks[totaltracks].control = 4;       /* force data tracks */
2057                                 sony_toc.tracks[totaltracks].track = 1;
2058                                 sony_toc.tracks[totaltracks].
2059                                     track_start_msf[0] = 0;
2060                                 sony_toc.tracks[totaltracks].
2061                                     track_start_msf[1] = 2;
2062                                 sony_toc.tracks[totaltracks].
2063                                     track_start_msf[2] = 0;
2064                                 mint = maxt = 1;
2065                                 totaltracks++;
2066                         } else
2067                                 /* gather track entries from this session */
2068                         {
2069                                 int i;
2070                                 for (i = 0;
2071                                      i <
2072                                      1 +
2073                                      bcd_to_int(single_toc.last_track_num)
2074                                      -
2075                                      bcd_to_int(single_toc.
2076                                                 first_track_num);
2077                                      i++, totaltracks++) {
2078                                         sony_toc.tracks[totaltracks].
2079                                             address =
2080                                             single_toc.tracks[i].address;
2081                                         sony_toc.tracks[totaltracks].
2082                                             control =
2083                                             single_toc.tracks[i].control;
2084                                         sony_toc.tracks[totaltracks].
2085                                             track =
2086                                             bcd_to_int(single_toc.
2087                                                        tracks[i].track);
2088                                         sony_toc.tracks[totaltracks].
2089                                             track_start_msf[0] =
2090                                             bcd_to_int(single_toc.
2091                                                        tracks[i].
2092                                                        track_start_msf[0]);
2093                                         sony_toc.tracks[totaltracks].
2094                                             track_start_msf[1] =
2095                                             bcd_to_int(single_toc.
2096                                                        tracks[i].
2097                                                        track_start_msf[1]);
2098                                         sony_toc.tracks[totaltracks].
2099                                             track_start_msf[2] =
2100                                             bcd_to_int(single_toc.
2101                                                        tracks[i].
2102                                                        track_start_msf[2]);
2103                                         if (i == 0)
2104                                                 single_toc.
2105                                                     start_track_lba =
2106                                                     msf_to_log(sony_toc.
2107                                                                tracks
2108                                                                [totaltracks].
2109                                                                track_start_msf);
2110                                         if (mint >
2111                                             sony_toc.tracks[totaltracks].
2112                                             track)
2113                                                 mint =
2114                                                     sony_toc.
2115                                                     tracks[totaltracks].
2116                                                     track;
2117                                         if (maxt <
2118                                             sony_toc.tracks[totaltracks].
2119                                             track)
2120                                                 maxt =
2121                                                     sony_toc.
2122                                                     tracks[totaltracks].
2123                                                     track;
2124                                 }
2125                         }
2126                         sony_toc.first_track_num = mint;
2127                         sony_toc.last_track_num = maxt;
2128                         /* Disk type of last session wins. For example:
2129                            CD-Extra has disk type 0 for the first session, so
2130                            a dumb HiFi CD player thinks it is a plain audio CD.
2131                            We are interested in the disk type of the last session,
2132                            which is 0x20 (XA) for CD-Extra, so we can access the
2133                            data track ... */
2134                         sony_toc.disk_type = single_toc.disk_type;
2135                         sony_toc.sessions = session;
2136
2137                         /* don't believe everything :-) */
2138                         if (session == 1)
2139                                 single_toc.start_track_lba = 0;
2140                         sony_toc.start_track_lba =
2141                             single_toc.start_track_lba;
2142
2143                         if (session > 1 && single_toc.pointb0 == 0xb0 &&
2144                             sony_toc.lead_out_start_lba ==
2145                             single_toc.lead_out_start_lba) {
2146                                 break;
2147                         }
2148
2149                         /* Let's not get carried away... */
2150                         if (session > 40) {
2151                                 printk("cdu31a: too many sessions: %d\n",
2152                                        session);
2153                                 break;
2154                         }
2155                         session++;
2156                 }
2157                 sony_toc.track_entries = totaltracks;
2158                 /* add one entry for the LAST track with track number CDROM_LEADOUT */
2159                 sony_toc.tracks[totaltracks].address = single_toc.address2;
2160                 sony_toc.tracks[totaltracks].control = single_toc.control2;
2161                 sony_toc.tracks[totaltracks].track = CDROM_LEADOUT;
2162                 sony_toc.tracks[totaltracks].track_start_msf[0] =
2163                     sony_toc.lead_out_start_msf[0];
2164                 sony_toc.tracks[totaltracks].track_start_msf[1] =
2165                     sony_toc.lead_out_start_msf[1];
2166                 sony_toc.tracks[totaltracks].track_start_msf[2] =
2167                     sony_toc.lead_out_start_msf[2];
2168
2169                 sony_toc_read = 1;
2170 #undef DEBUG
2171 #if DEBUG
2172                 printk
2173                     ("Disk session %d, start track: %d, stop track: %d\n",
2174                      session, single_toc.start_track_lba,
2175                      single_toc.lead_out_start_lba);
2176 #endif
2177         }
2178 #if DEBUG
2179         printk("Leaving sony_get_toc\n");
2180 #endif
2181 }
2182
2183
2184 /*
2185  * Uniform cdrom interface function
2186  * return multisession offset and sector information
2187  */
2188 static int scd_get_last_session(struct cdrom_device_info *cdi,
2189                                 struct cdrom_multisession *ms_info)
2190 {
2191         if (ms_info == NULL)
2192                 return 1;
2193
2194         if (!sony_toc_read)
2195                 sony_get_toc();
2196
2197         ms_info->addr_format = CDROM_LBA;
2198         ms_info->addr.lba = sony_toc.start_track_lba;
2199         ms_info->xa_flag = sony_toc.disk_type == SONY_XA_DISK_TYPE ||
2200             sony_toc.disk_type == 0x10 /* CDI */ ;
2201
2202         return 0;
2203 }
2204
2205 /*
2206  * Search for a specific track in the table of contents.
2207  */
2208 static int find_track(int track)
2209 {
2210         int i;
2211
2212         for (i = 0; i <= sony_toc.track_entries; i++) {
2213                 if (sony_toc.tracks[i].track == track) {
2214                         return i;
2215                 }
2216         }
2217
2218         return -1;
2219 }
2220
2221
2222 /*
2223  * Read the subcode and put it in last_sony_subcode for future use.
2224  */
2225 static int read_subcode(void)
2226 {
2227         unsigned int res_size;
2228
2229
2230         do_sony_cd_cmd(SONY_REQ_SUBCODE_ADDRESS_CMD,
2231                        NULL,
2232                        0, (unsigned char *) &last_sony_subcode, &res_size);
2233         if ((res_size < 2)
2234             || ((last_sony_subcode.exec_status[0] & 0xf0) == 0x20)) {
2235                 printk("Sony CDROM error %s (read_subcode)\n",
2236                        translate_error(last_sony_subcode.exec_status[1]));
2237                 return -EIO;
2238         }
2239
2240         last_sony_subcode.track_num =
2241             bcd_to_int(last_sony_subcode.track_num);
2242         last_sony_subcode.index_num =
2243             bcd_to_int(last_sony_subcode.index_num);
2244         last_sony_subcode.abs_msf[0] =
2245             bcd_to_int(last_sony_subcode.abs_msf[0]);
2246         last_sony_subcode.abs_msf[1] =
2247             bcd_to_int(last_sony_subcode.abs_msf[1]);
2248         last_sony_subcode.abs_msf[2] =
2249             bcd_to_int(last_sony_subcode.abs_msf[2]);
2250
2251         last_sony_subcode.rel_msf[0] =
2252             bcd_to_int(last_sony_subcode.rel_msf[0]);
2253         last_sony_subcode.rel_msf[1] =
2254             bcd_to_int(last_sony_subcode.rel_msf[1]);
2255         last_sony_subcode.rel_msf[2] =
2256             bcd_to_int(last_sony_subcode.rel_msf[2]);
2257         return 0;
2258 }
2259
2260 /*
2261  * Uniform cdrom interface function
2262  * return the media catalog number found on some older audio cds
2263  */
2264 static int
2265 scd_get_mcn(struct cdrom_device_info *cdi, struct cdrom_mcn *mcn)
2266 {
2267         unsigned char resbuffer[2 + 14];
2268         unsigned char *mcnp = mcn->medium_catalog_number;
2269         unsigned char *resp = resbuffer + 3;
2270         unsigned int res_size;
2271
2272         memset(mcn->medium_catalog_number, 0, 14);
2273         do_sony_cd_cmd(SONY_REQ_UPC_EAN_CMD,
2274                        NULL, 0, resbuffer, &res_size);
2275         if ((res_size < 2) || ((resbuffer[0] & 0xf0) == 0x20));
2276         else {
2277                 /* packed bcd to single ASCII digits */
2278                 *mcnp++ = (*resp >> 4) + '0';
2279                 *mcnp++ = (*resp++ & 0x0f) + '0';
2280                 *mcnp++ = (*resp >> 4) + '0';
2281                 *mcnp++ = (*resp++ & 0x0f) + '0';
2282                 *mcnp++ = (*resp >> 4) + '0';
2283                 *mcnp++ = (*resp++ & 0x0f) + '0';
2284                 *mcnp++ = (*resp >> 4) + '0';
2285                 *mcnp++ = (*resp++ & 0x0f) + '0';
2286                 *mcnp++ = (*resp >> 4) + '0';
2287                 *mcnp++ = (*resp++ & 0x0f) + '0';
2288                 *mcnp++ = (*resp >> 4) + '0';
2289                 *mcnp++ = (*resp++ & 0x0f) + '0';
2290                 *mcnp++ = (*resp >> 4) + '0';
2291         }
2292         *mcnp = '\0';
2293         return 0;
2294 }
2295
2296
2297 /*
2298  * Get the subchannel info like the CDROMSUBCHNL command wants to see it.  If
2299  * the drive is playing, the subchannel needs to be read (since it would be
2300  * changing).  If the drive is paused or completed, the subcode information has
2301  * already been stored, just use that.  The ioctl call wants things in decimal
2302  * (not BCD), so all the conversions are done.
2303  */
2304 static int sony_get_subchnl_info(struct cdrom_subchnl *schi)
2305 {
2306         /* Get attention stuff */
2307         while (handle_sony_cd_attention());
2308
2309         sony_get_toc();
2310         if (!sony_toc_read) {
2311                 return -EIO;
2312         }
2313
2314         switch (sony_audio_status) {
2315         case CDROM_AUDIO_NO_STATUS:
2316         case CDROM_AUDIO_PLAY:
2317                 if (read_subcode() < 0) {
2318                         return -EIO;
2319                 }
2320                 break;
2321
2322         case CDROM_AUDIO_PAUSED:
2323         case CDROM_AUDIO_COMPLETED:
2324                 break;
2325
2326 #if 0
2327         case CDROM_AUDIO_NO_STATUS:
2328                 schi->cdsc_audiostatus = sony_audio_status;
2329                 return 0;
2330                 break;
2331 #endif
2332         case CDROM_AUDIO_INVALID:
2333         case CDROM_AUDIO_ERROR:
2334         default:
2335                 return -EIO;
2336         }
2337
2338         schi->cdsc_audiostatus = sony_audio_status;
2339         schi->cdsc_adr = last_sony_subcode.address;
2340         schi->cdsc_ctrl = last_sony_subcode.control;
2341         schi->cdsc_trk = last_sony_subcode.track_num;
2342         schi->cdsc_ind = last_sony_subcode.index_num;
2343         if (schi->cdsc_format == CDROM_MSF) {
2344                 schi->cdsc_absaddr.msf.minute =
2345                     last_sony_subcode.abs_msf[0];
2346                 schi->cdsc_absaddr.msf.second =
2347                     last_sony_subcode.abs_msf[1];
2348                 schi->cdsc_absaddr.msf.frame =
2349                     last_sony_subcode.abs_msf[2];
2350
2351                 schi->cdsc_reladdr.msf.minute =
2352                     last_sony_subcode.rel_msf[0];
2353                 schi->cdsc_reladdr.msf.second =
2354                     last_sony_subcode.rel_msf[1];
2355                 schi->cdsc_reladdr.msf.frame =
2356                     last_sony_subcode.rel_msf[2];
2357         } else if (schi->cdsc_format == CDROM_LBA) {
2358                 schi->cdsc_absaddr.lba =
2359                     msf_to_log(last_sony_subcode.abs_msf);
2360                 schi->cdsc_reladdr.lba =
2361                     msf_to_log(last_sony_subcode.rel_msf);
2362         }
2363
2364         return 0;
2365 }
2366
2367 /* Get audio data from the drive.  This is fairly complex because I
2368    am looking for status and data at the same time, but if I get status
2369    then I just look for data.  I need to get the status immediately so
2370    the switch from audio to data tracks will happen quickly. */
2371 static void
2372 read_audio_data(char *buffer, unsigned char res_reg[], int *res_size)
2373 {
2374         unsigned long retry_count;
2375         int result_read;
2376
2377
2378         res_reg[0] = 0;
2379         res_reg[1] = 0;
2380         *res_size = 0;
2381         result_read = 0;
2382
2383         /* Wait for the drive to tell us we have something */
2384         retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
2385       continue_read_audio_wait:
2386         while (time_before(jiffies, retry_count) && !(is_data_ready())
2387                && !(is_result_ready() || result_read)) {
2388                 while (handle_sony_cd_attention());
2389
2390                 sony_sleep();
2391         }
2392         if (!(is_data_ready())) {
2393                 if (is_result_ready() && !result_read) {
2394                         get_result(res_reg, res_size);
2395
2396                         /* Read block status and continue waiting for data. */
2397                         if ((res_reg[0] & 0xf0) == 0x50) {
2398                                 result_read = 1;
2399                                 goto continue_read_audio_wait;
2400                         }
2401                         /* Invalid data from the drive.  Shut down the operation. */
2402                         else if ((res_reg[0] & 0xf0) != 0x20) {
2403                                 printk
2404                                     ("CDU31A: Got result that should have been error: %d\n",
2405                                      res_reg[0]);
2406                                 res_reg[0] = 0x20;
2407                                 res_reg[1] = SONY_BAD_DATA_ERR;
2408                                 *res_size = 2;
2409                         }
2410                         abort_read();
2411                 } else {
2412 #if DEBUG
2413                         printk("CDU31A timeout out %d\n", __LINE__);
2414 #endif
2415                         res_reg[0] = 0x20;
2416                         res_reg[1] = SONY_TIMEOUT_OP_ERR;
2417                         *res_size = 2;
2418                         abort_read();
2419                 }
2420         } else {
2421                 clear_data_ready();
2422
2423                 /* If data block, then get 2340 bytes offset by 12. */
2424                 if (sony_raw_data_mode) {
2425                         insb(sony_cd_read_reg, buffer + CD_XA_HEAD,
2426                              CD_FRAMESIZE_RAW1);
2427                 } else {
2428                         /* Audio gets the whole 2352 bytes. */
2429                         insb(sony_cd_read_reg, buffer, CD_FRAMESIZE_RAW);
2430                 }
2431
2432                 /* If I haven't already gotten the result, get it now. */
2433                 if (!result_read) {
2434                         /* Wait for the drive to tell us we have something */
2435                         retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
2436                         while (time_before(jiffies, retry_count)
2437                                && !(is_result_ready())) {
2438                                 while (handle_sony_cd_attention());
2439
2440                                 sony_sleep();
2441                         }
2442
2443                         if (!is_result_ready()) {
2444 #if DEBUG
2445                                 printk("CDU31A timeout out %d\n",
2446                                        __LINE__);
2447 #endif
2448                                 res_reg[0] = 0x20;
2449                                 res_reg[1] = SONY_TIMEOUT_OP_ERR;
2450                                 *res_size = 2;
2451                                 abort_read();
2452                                 return;
2453                         } else {
2454                                 get_result(res_reg, res_size);
2455                         }
2456                 }
2457
2458                 if ((res_reg[0] & 0xf0) == 0x50) {
2459                         if ((res_reg[0] == SONY_NO_CIRC_ERR_BLK_STAT)
2460                             || (res_reg[0] == SONY_NO_LECC_ERR_BLK_STAT)
2461                             || (res_reg[0] == SONY_RECOV_LECC_ERR_BLK_STAT)
2462                             || (res_reg[0] == SONY_NO_ERR_DETECTION_STAT)) {
2463                                 /* Ok, nothing to do. */
2464                         } else {
2465                                 printk("CDU31A: Data block error: 0x%x\n",
2466                                        res_reg[0]);
2467                                 res_reg[0] = 0x20;
2468                                 res_reg[1] = SONY_BAD_DATA_ERR;
2469                                 *res_size = 2;
2470                         }
2471                 } else if ((res_reg[0] & 0xf0) != 0x20) {
2472                         /* The drive gave me bad status, I don't know what to do.
2473                            Reset the driver and return an error. */
2474                         printk("CDU31A: Invalid block status: 0x%x\n",
2475                                res_reg[0]);
2476                         restart_on_error();
2477                         res_reg[0] = 0x20;
2478                         res_reg[1] = SONY_BAD_DATA_ERR;
2479                         *res_size = 2;
2480                 }
2481         }
2482 }
2483
2484 /* Perform a raw data read.  This will automatically detect the
2485    track type and read the proper data (audio or data). */
2486 static int read_audio(struct cdrom_read_audio *ra)
2487 {
2488         int retval;
2489         unsigned char params[2];
2490         unsigned char res_reg[12];
2491         unsigned int res_size;
2492         unsigned int cframe;
2493         unsigned long flags;
2494
2495         /* 
2496          * Make sure no one else is using the driver; wait for them
2497          * to finish if it is so.
2498          */
2499         save_flags(flags);
2500         cli();
2501         while (sony_inuse) {
2502                 interruptible_sleep_on(&sony_wait);
2503                 if (signal_pending(current)) {
2504                         restore_flags(flags);
2505                         return -EAGAIN;
2506                 }
2507         }
2508         sony_inuse = 1;
2509         has_cd_task = current;
2510         restore_flags(flags);
2511
2512         if (!sony_spun_up) {
2513                 scd_spinup();
2514         }
2515
2516         /* Set the drive to do raw operations. */
2517         params[0] = SONY_SD_DECODE_PARAM;
2518         params[1] = 0x06 | sony_raw_data_mode;
2519         do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
2520                        params, 2, res_reg, &res_size);
2521         if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20)) {
2522                 printk("CDU31A: Unable to set decode params: 0x%2.2x\n",
2523                        res_reg[1]);
2524                 return -EIO;
2525         }
2526
2527         /* From here down, we have to goto exit_read_audio instead of returning
2528            because the drive parameters have to be set back to data before
2529            return. */
2530
2531         retval = 0;
2532         /* start_request clears out any readahead data, so it should be safe. */
2533         if (start_request(ra->addr.lba, ra->nframes, 1)) {
2534                 retval = -EIO;
2535                 goto exit_read_audio;
2536         }
2537
2538         /* For every requested frame. */
2539         cframe = 0;
2540         while (cframe < ra->nframes) {
2541                 read_audio_data(readahead_buffer, res_reg, &res_size);
2542                 if ((res_reg[0] & 0xf0) == 0x20) {
2543                         if (res_reg[1] == SONY_BAD_DATA_ERR) {
2544                                 printk
2545                                     ("CDU31A: Data error on audio sector %d\n",
2546                                      ra->addr.lba + cframe);
2547                         } else if (res_reg[1] == SONY_ILL_TRACK_R_ERR) {
2548                                 /* Illegal track type, change track types and start over. */
2549                                 sony_raw_data_mode =
2550                                     (sony_raw_data_mode) ? 0 : 1;
2551
2552                                 /* Set the drive mode. */
2553                                 params[0] = SONY_SD_DECODE_PARAM;
2554                                 params[1] = 0x06 | sony_raw_data_mode;
2555                                 do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
2556                                                params,
2557                                                2, res_reg, &res_size);
2558                                 if ((res_size < 2)
2559                                     || ((res_reg[0] & 0xf0) == 0x20)) {
2560                                         printk
2561                                             ("CDU31A: Unable to set decode params: 0x%2.2x\n",
2562                                              res_reg[1]);
2563                                         retval = -EIO;
2564                                         goto exit_read_audio;
2565                                 }
2566
2567                                 /* Restart the request on the current frame. */
2568                                 if (start_request
2569                                     (ra->addr.lba + cframe,
2570                                      ra->nframes - cframe, 1)) {
2571                                         retval = -EIO;
2572                                         goto exit_read_audio;
2573                                 }
2574
2575                                 /* Don't go back to the top because don't want to get into
2576                                    and infinite loop.  A lot of code gets duplicated, but
2577                                    that's no big deal, I don't guess. */
2578                                 read_audio_data(readahead_buffer, res_reg,
2579                                                 &res_size);
2580                                 if ((res_reg[0] & 0xf0) == 0x20) {
2581                                         if (res_reg[1] ==
2582                                             SONY_BAD_DATA_ERR) {
2583                                                 printk
2584                                                     ("CDU31A: Data error on audio sector %d\n",
2585                                                      ra->addr.lba +
2586                                                      cframe);
2587                                         } else {
2588                                                 printk
2589                                                     ("CDU31A: Error reading audio data on sector %d: %s\n",
2590                                                      ra->addr.lba + cframe,
2591                                                      translate_error
2592                                                      (res_reg[1]));
2593                                                 retval = -EIO;
2594                                                 goto exit_read_audio;
2595                                         }
2596                                 } else if (copy_to_user(ra->buf +
2597                                                                (CD_FRAMESIZE_RAW
2598                                                                 * cframe),
2599                                                         readahead_buffer,
2600                                                         CD_FRAMESIZE_RAW)) {
2601                                         retval = -EFAULT;
2602                                         goto exit_read_audio;
2603                                 }
2604                         } else {
2605                                 printk
2606                                     ("CDU31A: Error reading audio data on sector %d: %s\n",
2607                                      ra->addr.lba + cframe,
2608                                      translate_error(res_reg[1]));
2609                                 retval = -EIO;
2610                                 goto exit_read_audio;
2611                         }
2612                 } else if (copy_to_user(ra->buf + (CD_FRAMESIZE_RAW * cframe),
2613                                         (char *)readahead_buffer,
2614                                         CD_FRAMESIZE_RAW)) {
2615                         retval = -EFAULT;
2616                         goto exit_read_audio;
2617                 }
2618
2619                 cframe++;
2620         }
2621
2622         get_result(res_reg, &res_size);
2623         if ((res_reg[0] & 0xf0) == 0x20) {
2624                 printk("CDU31A: Error return from audio read: %s\n",
2625                        translate_error(res_reg[1]));
2626                 retval = -EIO;
2627                 goto exit_read_audio;
2628         }
2629
2630       exit_read_audio:
2631
2632         /* Set the drive mode back to the proper one for the disk. */
2633         params[0] = SONY_SD_DECODE_PARAM;
2634         if (!sony_xa_mode) {
2635                 params[1] = 0x0f;
2636         } else {
2637                 params[1] = 0x07;
2638         }
2639         do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
2640                        params, 2, res_reg, &res_size);
2641         if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20)) {
2642                 printk("CDU31A: Unable to reset decode params: 0x%2.2x\n",
2643                        res_reg[1]);
2644                 return -EIO;
2645         }
2646
2647         has_cd_task = NULL;
2648         sony_inuse = 0;
2649         wake_up_interruptible(&sony_wait);
2650
2651         return (retval);
2652 }
2653
2654 static int
2655 do_sony_cd_cmd_chk(const char *name,
2656                    unsigned char cmd,
2657                    unsigned char *params,
2658                    unsigned int num_params,
2659                    unsigned char *result_buffer, unsigned int *result_size)
2660 {
2661         do_sony_cd_cmd(cmd, params, num_params, result_buffer,
2662                        result_size);
2663         if ((*result_size < 2) || ((result_buffer[0] & 0xf0) == 0x20)) {
2664                 printk("Sony CDROM error %s (CDROM%s)\n",
2665                        translate_error(result_buffer[1]), name);
2666                 return -EIO;
2667         }
2668         return 0;
2669 }
2670
2671 /*
2672  * Uniform cdrom interface function
2673  * open the tray
2674  */
2675 static int scd_tray_move(struct cdrom_device_info *cdi, int position)
2676 {
2677         if (position == 1 /* open tray */ ) {
2678                 unsigned char res_reg[12];
2679                 unsigned int res_size;
2680
2681                 do_sony_cd_cmd(SONY_AUDIO_STOP_CMD, NULL, 0, res_reg,
2682                                &res_size);
2683                 do_sony_cd_cmd(SONY_SPIN_DOWN_CMD, NULL, 0, res_reg,
2684                                &res_size);
2685
2686                 sony_audio_status = CDROM_AUDIO_INVALID;
2687                 return do_sony_cd_cmd_chk("EJECT", SONY_EJECT_CMD, NULL, 0,
2688                                           res_reg, &res_size);
2689         } else {
2690                 if (0 == scd_spinup())
2691                         sony_spun_up = 1;
2692                 return 0;
2693         }
2694 }
2695
2696 /*
2697  * The big ugly ioctl handler.
2698  */
2699 static int scd_audio_ioctl(struct cdrom_device_info *cdi,
2700                            unsigned int cmd, void *arg)
2701 {
2702         unsigned char res_reg[12];
2703         unsigned int res_size;
2704         unsigned char params[7];
2705         int i;
2706
2707
2708         switch (cmd) {
2709         case CDROMSTART:        /* Spin up the drive */
2710                 return do_sony_cd_cmd_chk("START", SONY_SPIN_UP_CMD, NULL,
2711                                           0, res_reg, &res_size);
2712                 break;
2713
2714         case CDROMSTOP: /* Spin down the drive */
2715                 do_sony_cd_cmd(SONY_AUDIO_STOP_CMD, NULL, 0, res_reg,
2716                                &res_size);
2717
2718                 /*
2719                  * Spin the drive down, ignoring the error if the disk was
2720                  * already not spinning.
2721                  */
2722                 sony_audio_status = CDROM_AUDIO_NO_STATUS;
2723                 return do_sony_cd_cmd_chk("STOP", SONY_SPIN_DOWN_CMD, NULL,
2724                                           0, res_reg, &res_size);
2725
2726         case CDROMPAUSE:        /* Pause the drive */
2727                 if (do_sony_cd_cmd_chk
2728                     ("PAUSE", SONY_AUDIO_STOP_CMD, NULL, 0, res_reg,
2729                      &res_size))
2730                         return -EIO;
2731                 /* Get the current position and save it for resuming */
2732                 if (read_subcode() < 0) {
2733                         return -EIO;
2734                 }
2735                 cur_pos_msf[0] = last_sony_subcode.abs_msf[0];
2736                 cur_pos_msf[1] = last_sony_subcode.abs_msf[1];
2737                 cur_pos_msf[2] = last_sony_subcode.abs_msf[2];
2738                 sony_audio_status = CDROM_AUDIO_PAUSED;
2739                 return 0;
2740                 break;
2741
2742         case CDROMRESUME:       /* Start the drive after being paused */
2743                 if (sony_audio_status != CDROM_AUDIO_PAUSED) {
2744                         return -EINVAL;
2745                 }
2746
2747                 do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg,
2748                                &res_size);
2749
2750                 /* Start the drive at the saved position. */
2751                 params[1] = int_to_bcd(cur_pos_msf[0]);
2752                 params[2] = int_to_bcd(cur_pos_msf[1]);
2753                 params[3] = int_to_bcd(cur_pos_msf[2]);
2754                 params[4] = int_to_bcd(final_pos_msf[0]);
2755                 params[5] = int_to_bcd(final_pos_msf[1]);
2756                 params[6] = int_to_bcd(final_pos_msf[2]);
2757                 params[0] = 0x03;
2758                 if (do_sony_cd_cmd_chk
2759                     ("RESUME", SONY_AUDIO_PLAYBACK_CMD, params, 7, res_reg,
2760                      &res_size) < 0)
2761                         return -EIO;
2762                 sony_audio_status = CDROM_AUDIO_PLAY;
2763                 return 0;
2764
2765         case CDROMPLAYMSF:      /* Play starting at the given MSF address. */
2766                 do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg,
2767                                &res_size);
2768
2769                 /* The parameters are given in int, must be converted */
2770                 for (i = 1; i < 7; i++) {
2771                         params[i] =
2772                             int_to_bcd(((unsigned char *) arg)[i - 1]);
2773                 }
2774                 params[0] = 0x03;
2775                 if (do_sony_cd_cmd_chk
2776                     ("PLAYMSF", SONY_AUDIO_PLAYBACK_CMD, params, 7,
2777                      res_reg, &res_size) < 0)
2778                         return -EIO;
2779
2780                 /* Save the final position for pauses and resumes */
2781                 final_pos_msf[0] = bcd_to_int(params[4]);
2782                 final_pos_msf[1] = bcd_to_int(params[5]);
2783                 final_pos_msf[2] = bcd_to_int(params[6]);
2784                 sony_audio_status = CDROM_AUDIO_PLAY;
2785                 return 0;
2786
2787         case CDROMREADTOCHDR:   /* Read the table of contents header */
2788                 {
2789                         struct cdrom_tochdr *hdr;
2790
2791                         sony_get_toc();
2792                         if (!sony_toc_read) {
2793                                 return -EIO;
2794                         }
2795
2796                         hdr = (struct cdrom_tochdr *) arg;
2797                         hdr->cdth_trk0 = sony_toc.first_track_num;
2798                         hdr->cdth_trk1 = sony_toc.last_track_num;
2799                 }
2800                 return 0;
2801
2802         case CDROMREADTOCENTRY: /* Read a given table of contents entry */
2803                 {
2804                         struct cdrom_tocentry *entry;
2805                         int track_idx;
2806                         unsigned char *msf_val = NULL;
2807
2808                         sony_get_toc();
2809                         if (!sony_toc_read) {
2810                                 return -EIO;
2811                         }
2812
2813                         entry = (struct cdrom_tocentry *) arg;
2814
2815                         track_idx = find_track(entry->cdte_track);
2816                         if (track_idx < 0) {
2817                                 return -EINVAL;
2818                         }
2819
2820                         entry->cdte_adr =
2821                             sony_toc.tracks[track_idx].address;
2822                         entry->cdte_ctrl =
2823                             sony_toc.tracks[track_idx].control;
2824                         msf_val =
2825                             sony_toc.tracks[track_idx].track_start_msf;
2826
2827                         /* Logical buffer address or MSF format requested? */
2828                         if (entry->cdte_format == CDROM_LBA) {
2829                                 entry->cdte_addr.lba = msf_to_log(msf_val);
2830                         } else if (entry->cdte_format == CDROM_MSF) {
2831                                 entry->cdte_addr.msf.minute = *msf_val;
2832                                 entry->cdte_addr.msf.second =
2833                                     *(msf_val + 1);
2834                                 entry->cdte_addr.msf.frame =
2835                                     *(msf_val + 2);
2836                         }
2837                 }
2838                 return 0;
2839                 break;
2840
2841         case CDROMPLAYTRKIND:   /* Play a track.  This currently ignores index. */
2842                 {
2843                         struct cdrom_ti *ti = (struct cdrom_ti *) arg;
2844                         int track_idx;
2845
2846                         sony_get_toc();
2847                         if (!sony_toc_read) {
2848                                 return -EIO;
2849                         }
2850
2851                         if ((ti->cdti_trk0 < sony_toc.first_track_num)
2852                             || (ti->cdti_trk0 > sony_toc.last_track_num)
2853                             || (ti->cdti_trk1 < ti->cdti_trk0)) {
2854                                 return -EINVAL;
2855                         }
2856
2857                         track_idx = find_track(ti->cdti_trk0);
2858                         if (track_idx < 0) {
2859                                 return -EINVAL;
2860                         }
2861                         params[1] =
2862                             int_to_bcd(sony_toc.tracks[track_idx].
2863                                        track_start_msf[0]);
2864                         params[2] =
2865                             int_to_bcd(sony_toc.tracks[track_idx].
2866                                        track_start_msf[1]);
2867                         params[3] =
2868                             int_to_bcd(sony_toc.tracks[track_idx].
2869                                        track_start_msf[2]);
2870
2871                         /*
2872                          * If we want to stop after the last track, use the lead-out
2873                          * MSF to do that.
2874                          */
2875                         if (ti->cdti_trk1 >= sony_toc.last_track_num) {
2876                                 track_idx = find_track(CDROM_LEADOUT);
2877                         } else {
2878                                 track_idx = find_track(ti->cdti_trk1 + 1);
2879                         }
2880                         if (track_idx < 0) {
2881                                 return -EINVAL;
2882                         }
2883                         params[4] =
2884                             int_to_bcd(sony_toc.tracks[track_idx].
2885                                        track_start_msf[0]);
2886                         params[5] =
2887                             int_to_bcd(sony_toc.tracks[track_idx].
2888                                        track_start_msf[1]);
2889                         params[6] =
2890                             int_to_bcd(sony_toc.tracks[track_idx].
2891                                        track_start_msf[2]);
2892                         params[0] = 0x03;
2893
2894                         do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg,
2895                                        &res_size);
2896
2897                         do_sony_cd_cmd(SONY_AUDIO_PLAYBACK_CMD, params, 7,
2898                                        res_reg, &res_size);
2899
2900                         if ((res_size < 2)
2901                             || ((res_reg[0] & 0xf0) == 0x20)) {
2902                                 printk("Params: %x %x %x %x %x %x %x\n",
2903                                        params[0], params[1], params[2],
2904                                        params[3], params[4], params[5],
2905                                        params[6]);
2906                                 printk
2907                                     ("Sony CDROM error %s (CDROMPLAYTRKIND)\n",
2908                                      translate_error(res_reg[1]));
2909                                 return -EIO;
2910                         }
2911
2912                         /* Save the final position for pauses and resumes */
2913                         final_pos_msf[0] = bcd_to_int(params[4]);
2914                         final_pos_msf[1] = bcd_to_int(params[5]);
2915                         final_pos_msf[2] = bcd_to_int(params[6]);
2916                         sony_audio_status = CDROM_AUDIO_PLAY;
2917                         return 0;
2918                 }
2919
2920         case CDROMVOLCTRL:      /* Volume control.  What volume does this change, anyway? */
2921                 {
2922                         struct cdrom_volctrl *volctrl =
2923                             (struct cdrom_volctrl *) arg;
2924
2925                         params[0] = SONY_SD_AUDIO_VOLUME;
2926                         params[1] = volctrl->channel0;
2927                         params[2] = volctrl->channel1;
2928                         return do_sony_cd_cmd_chk("VOLCTRL",
2929                                                   SONY_SET_DRIVE_PARAM_CMD,
2930                                                   params, 3, res_reg,
2931                                                   &res_size);
2932                 }
2933         case CDROMSUBCHNL:      /* Get subchannel info */
2934                 return sony_get_subchnl_info((struct cdrom_subchnl *) arg);
2935
2936         default:
2937                 return -EINVAL;
2938         }
2939 }
2940
2941 static int scd_dev_ioctl(struct cdrom_device_info *cdi,
2942                          unsigned int cmd, unsigned long arg)
2943 {
2944         void __user *argp = (void __user *)arg;
2945         int i;
2946
2947         switch (cmd) {
2948         case CDROMREADAUDIO:    /* Read 2352 byte audio tracks and 2340 byte
2949                                    raw data tracks. */
2950                 {
2951                         struct cdrom_read_audio ra;
2952
2953
2954                         sony_get_toc();
2955                         if (!sony_toc_read) {
2956                                 return -EIO;
2957                         }
2958
2959                         if (copy_from_user(&ra, argp, sizeof(ra)))
2960                                 return -EFAULT;
2961
2962                         if (ra.nframes == 0) {
2963                                 return 0;
2964                         }
2965
2966                         i = verify_area(VERIFY_WRITE, ra.buf,
2967                                         CD_FRAMESIZE_RAW * ra.nframes);
2968                         if (i < 0)
2969                                 return i;
2970
2971                         if (ra.addr_format == CDROM_LBA) {
2972                                 if ((ra.addr.lba >=
2973                                      sony_toc.lead_out_start_lba)
2974                                     || (ra.addr.lba + ra.nframes >=
2975                                         sony_toc.lead_out_start_lba)) {
2976                                         return -EINVAL;
2977                                 }
2978                         } else if (ra.addr_format == CDROM_MSF) {
2979                                 if ((ra.addr.msf.minute >= 75)
2980                                     || (ra.addr.msf.second >= 60)
2981                                     || (ra.addr.msf.frame >= 75)) {
2982                                         return -EINVAL;
2983                                 }
2984
2985                                 ra.addr.lba = ((ra.addr.msf.minute * 4500)
2986                                                + (ra.addr.msf.second * 75)
2987                                                + ra.addr.msf.frame);
2988                                 if ((ra.addr.lba >=
2989                                      sony_toc.lead_out_start_lba)
2990                                     || (ra.addr.lba + ra.nframes >=
2991                                         sony_toc.lead_out_start_lba)) {
2992                                         return -EINVAL;
2993                                 }
2994
2995                                 /* I know, this can go negative on an unsigned.  However,
2996                                    the first thing done to the data is to add this value,
2997                                    so this should compensate and allow direct msf access. */
2998                                 ra.addr.lba -= LOG_START_OFFSET;
2999                         } else {
3000                                 return -EINVAL;
3001                         }
3002
3003                         return (read_audio(&ra));
3004                 }
3005                 return 0;
3006                 break;
3007
3008         default:
3009                 return -EINVAL;
3010         }
3011 }
3012
3013 static int scd_spinup(void)
3014 {
3015         unsigned char res_reg[12];
3016         unsigned int res_size;
3017         int num_spin_ups;
3018
3019         num_spin_ups = 0;
3020
3021       respinup_on_open:
3022         do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg, &res_size);
3023
3024         /* The drive sometimes returns error 0.  I don't know why, but ignore
3025            it.  It seems to mean the drive has already done the operation. */
3026         if ((res_size < 2) || ((res_reg[0] != 0) && (res_reg[1] != 0))) {
3027                 printk("Sony CDROM %s error (scd_open, spin up)\n",
3028                        translate_error(res_reg[1]));
3029                 return 1;
3030         }
3031
3032         do_sony_cd_cmd(SONY_READ_TOC_CMD, NULL, 0, res_reg, &res_size);
3033
3034         /* The drive sometimes returns error 0.  I don't know why, but ignore
3035            it.  It seems to mean the drive has already done the operation. */
3036         if ((res_size < 2) || ((res_reg[0] != 0) && (res_reg[1] != 0))) {
3037                 /* If the drive is already playing, it's ok.  */
3038                 if ((res_reg[1] == SONY_AUDIO_PLAYING_ERR)
3039                     || (res_reg[1] == 0)) {
3040                         return 0;
3041                 }
3042
3043                 /* If the drive says it is not spun up (even though we just did it!)
3044                    then retry the operation at least a few times. */
3045                 if ((res_reg[1] == SONY_NOT_SPIN_ERR)
3046                     && (num_spin_ups < MAX_CDU31A_RETRIES)) {
3047                         num_spin_ups++;
3048                         goto respinup_on_open;
3049                 }
3050
3051                 printk("Sony CDROM error %s (scd_open, read toc)\n",
3052                        translate_error(res_reg[1]));
3053                 do_sony_cd_cmd(SONY_SPIN_DOWN_CMD, NULL, 0, res_reg,
3054                                &res_size);
3055                 return 1;
3056         }
3057         return 0;
3058 }
3059
3060 /*
3061  * Open the drive for operations.  Spin the drive up and read the table of
3062  * contents if these have not already been done.
3063  */
3064 static int scd_open(struct cdrom_device_info *cdi, int openmode)
3065 {
3066         unsigned char res_reg[12];
3067         unsigned int res_size;
3068         unsigned char params[2];
3069
3070         if (sony_usage == 0) {
3071                 if (scd_spinup() != 0)
3072                         return -EIO;
3073                 sony_get_toc();
3074                 if (!sony_toc_read) {
3075                         do_sony_cd_cmd(SONY_SPIN_DOWN_CMD, NULL, 0,
3076                                        res_reg, &res_size);
3077                         return -EIO;
3078                 }
3079
3080                 /* For XA on the CDU31A only, we have to do special reads.
3081                    The CDU33A handles XA automagically. */
3082                 /* if (   (sony_toc.disk_type == SONY_XA_DISK_TYPE) */
3083                 if ((sony_toc.disk_type != 0x00)
3084                     && (!is_double_speed)) {
3085                         params[0] = SONY_SD_DECODE_PARAM;
3086                         params[1] = 0x07;
3087                         do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
3088                                        params, 2, res_reg, &res_size);
3089                         if ((res_size < 2)
3090                             || ((res_reg[0] & 0xf0) == 0x20)) {
3091                                 printk
3092                                     ("CDU31A: Unable to set XA params: 0x%2.2x\n",
3093                                      res_reg[1]);
3094                         }
3095                         sony_xa_mode = 1;
3096                 }
3097                 /* A non-XA disk.  Set the parms back if necessary. */
3098                 else if (sony_xa_mode) {
3099                         params[0] = SONY_SD_DECODE_PARAM;
3100                         params[1] = 0x0f;
3101                         do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
3102                                        params, 2, res_reg, &res_size);
3103                         if ((res_size < 2)
3104                             || ((res_reg[0] & 0xf0) == 0x20)) {
3105                                 printk
3106                                     ("CDU31A: Unable to reset XA params: 0x%2.2x\n",
3107                                      res_reg[1]);
3108                         }
3109                         sony_xa_mode = 0;
3110                 }
3111
3112                 sony_spun_up = 1;
3113         }
3114
3115         sony_usage++;
3116
3117         return 0;
3118 }
3119
3120
3121 /*
3122  * Close the drive.  Spin it down if no task is using it.  The spin
3123  * down will fail if playing audio, so audio play is OK.
3124  */
3125 static void scd_release(struct cdrom_device_info *cdi)
3126 {
3127         if (sony_usage == 1) {
3128                 unsigned char res_reg[12];
3129                 unsigned int res_size;
3130
3131                 do_sony_cd_cmd(SONY_SPIN_DOWN_CMD, NULL, 0, res_reg,
3132                                &res_size);
3133
3134                 sony_spun_up = 0;
3135         }
3136         sony_usage--;
3137 }
3138
3139 static struct cdrom_device_ops scd_dops = {
3140         .open                   = scd_open,
3141         .release                = scd_release,
3142         .drive_status           = scd_drive_status,
3143         .media_changed          = scd_media_changed,
3144         .tray_move              = scd_tray_move,
3145         .lock_door              = scd_lock_door,
3146         .select_speed           = scd_select_speed,
3147         .get_last_session       = scd_get_last_session,
3148         .get_mcn                = scd_get_mcn,
3149         .reset                  = scd_reset,
3150         .audio_ioctl            = scd_audio_ioctl,
3151         .dev_ioctl              = scd_dev_ioctl,
3152         .capability             = CDC_OPEN_TRAY | CDC_CLOSE_TRAY | CDC_LOCK |
3153                                   CDC_SELECT_SPEED | CDC_MULTI_SESSION |
3154                                   CDC_MULTI_SESSION | CDC_MCN |
3155                                   CDC_MEDIA_CHANGED | CDC_PLAY_AUDIO |
3156                                   CDC_RESET | CDC_IOCTLS | CDC_DRIVE_STATUS,
3157         .n_minors               = 1,
3158 };
3159
3160 static struct cdrom_device_info scd_info = {
3161         .ops            = &scd_dops,
3162         .speed          = 2,
3163         .capacity       = 1,
3164         .name           = "cdu31a"
3165 };
3166
3167 static int scd_block_open(struct inode *inode, struct file *file)
3168 {
3169         return cdrom_open(&scd_info, inode, file);
3170 }
3171
3172 static int scd_block_release(struct inode *inode, struct file *file)
3173 {
3174         return cdrom_release(&scd_info, file);
3175 }
3176
3177 static int scd_block_ioctl(struct inode *inode, struct file *file,
3178                                 unsigned cmd, unsigned long arg)
3179 {
3180         return cdrom_ioctl(file, &scd_info, inode, cmd, arg);
3181 }
3182
3183 static int scd_block_media_changed(struct gendisk *disk)
3184 {
3185         return cdrom_media_changed(&scd_info);
3186 }
3187
3188 struct block_device_operations scd_bdops =
3189 {
3190         .owner          = THIS_MODULE,
3191         .open           = scd_block_open,
3192         .release        = scd_block_release,
3193         .ioctl          = scd_block_ioctl,
3194         .media_changed  = scd_block_media_changed,
3195 };
3196
3197 static struct gendisk *scd_gendisk;
3198
3199 /* The different types of disc loading mechanisms supported */
3200 static char *load_mech[] __initdata =
3201     { "caddy", "tray", "pop-up", "unknown" };
3202
3203 static int __init
3204 get_drive_configuration(unsigned short base_io,
3205                         unsigned char res_reg[], unsigned int *res_size)
3206 {
3207         unsigned long retry_count;
3208
3209
3210         if (!request_region(base_io, 4, "cdu31a"))
3211                 return 0;
3212
3213         /* Set the base address */
3214         cdu31a_port = base_io;
3215
3216         /* Set up all the register locations */
3217         sony_cd_cmd_reg = cdu31a_port + SONY_CMD_REG_OFFSET;
3218         sony_cd_param_reg = cdu31a_port + SONY_PARAM_REG_OFFSET;
3219         sony_cd_write_reg = cdu31a_port + SONY_WRITE_REG_OFFSET;
3220         sony_cd_control_reg = cdu31a_port + SONY_CONTROL_REG_OFFSET;
3221         sony_cd_status_reg = cdu31a_port + SONY_STATUS_REG_OFFSET;
3222         sony_cd_result_reg = cdu31a_port + SONY_RESULT_REG_OFFSET;
3223         sony_cd_read_reg = cdu31a_port + SONY_READ_REG_OFFSET;
3224         sony_cd_fifost_reg = cdu31a_port + SONY_FIFOST_REG_OFFSET;
3225
3226         /*
3227          * Check to see if anything exists at the status register location.
3228          * I don't know if this is a good way to check, but it seems to work
3229          * ok for me.
3230          */
3231         if (read_status_register() != 0xff) {
3232                 /*
3233                  * Reset the drive and wait for attention from it (to say it's reset).
3234                  * If you don't wait, the next operation will probably fail.
3235                  */
3236                 reset_drive();
3237                 retry_count = jiffies + SONY_RESET_TIMEOUT;
3238                 while (time_before(jiffies, retry_count)
3239                        && (!is_attention())) {
3240                         sony_sleep();
3241                 }
3242
3243 #if 0
3244                 /* If attention is never seen probably not a CDU31a present */
3245                 if (!is_attention()) {
3246                         res_reg[0] = 0x20;
3247                         goto out_err;
3248                 }
3249 #endif
3250
3251                 /*
3252                  * Get the drive configuration.
3253                  */
3254                 do_sony_cd_cmd(SONY_REQ_DRIVE_CONFIG_CMD,
3255                                NULL,
3256                                0, (unsigned char *) res_reg, res_size);
3257                 if (*res_size <= 2 || (res_reg[0] & 0xf0) != 0)
3258                         goto out_err;
3259                 return 1;
3260         }
3261
3262         /* Return an error */
3263         res_reg[0] = 0x20;
3264 out_err:
3265         release_region(cdu31a_port, 4);
3266         cdu31a_port = 0;
3267         return 0;
3268 }
3269
3270 #ifndef MODULE
3271 /*
3272  * Set up base I/O and interrupts, called from main.c.
3273  
3274  */
3275
3276 static int __init cdu31a_setup(char *strings)
3277 {
3278         int ints[4];
3279
3280         (void) get_options(strings, ARRAY_SIZE(ints), ints);
3281
3282         if (ints[0] > 0) {
3283                 cdu31a_port = ints[1];
3284         }
3285         if (ints[0] > 1) {
3286                 cdu31a_irq = ints[2];
3287         }
3288         if ((strings != NULL) && (*strings != '\0')) {
3289                 if (strcmp(strings, "PAS") == 0) {
3290                         sony_pas_init = 1;
3291                 } else {
3292                         printk("CDU31A: Unknown interface type: %s\n",
3293                                strings);
3294                 }
3295         }
3296
3297         return 1;
3298 }
3299
3300 __setup("cdu31a=", cdu31a_setup);
3301
3302 #endif
3303
3304 /*
3305  * Initialize the driver.
3306  */
3307 int __init cdu31a_init(void)
3308 {
3309         struct s_sony_drive_config drive_config;
3310         struct gendisk *disk;
3311         int deficiency = 0;
3312         unsigned int res_size;
3313         char msg[255];
3314         char buf[40];
3315         int i;
3316         int tmp_irq;
3317
3318         /*
3319          * According to Alex Freed (freed@europa.orion.adobe.com), this is
3320          * required for the Fusion CD-16 package.  If the sound driver is
3321          * loaded, it should work fine, but just in case...
3322          *
3323          * The following turn on the CD-ROM interface for a Fusion CD-16.
3324          */
3325         if (sony_pas_init) {
3326                 outb(0xbc, 0x9a01);
3327                 outb(0xe2, 0x9a01);
3328         }
3329
3330         /* Setting the base I/O address to 0xffff will disable it. */
3331         if (cdu31a_port == 0xffff)
3332                 goto errout3;
3333
3334         if (cdu31a_port != 0) {
3335                 /* Need IRQ 0 because we can't sleep here. */
3336                 tmp_irq = cdu31a_irq;
3337                 cdu31a_irq = 0;
3338                 if (!get_drive_configuration(cdu31a_port,
3339                                             drive_config.exec_status,
3340                                             &res_size))
3341                         goto errout3;
3342                 cdu31a_irq = tmp_irq;
3343         } else {
3344                 cdu31a_irq = 0;
3345                 for (i = 0; cdu31a_addresses[i].base; i++) {
3346                         if (get_drive_configuration(cdu31a_addresses[i].base,
3347                                                      drive_config.exec_status,
3348                                                      &res_size)) {
3349                                 cdu31a_irq = cdu31a_addresses[i].int_num;
3350                                 break;
3351                         }
3352                 }
3353                 if (!cdu31a_port)
3354                         goto errout3;
3355         }
3356
3357         if (register_blkdev(MAJOR_NR, "cdu31a"))
3358                 goto errout2;
3359
3360         disk = alloc_disk(1);
3361         if (!disk)
3362                 goto errout1;
3363         disk->major = MAJOR_NR;
3364         disk->first_minor = 0;
3365         sprintf(disk->disk_name, "cdu31a");
3366         disk->fops = &scd_bdops;
3367         disk->flags = GENHD_FL_CD;
3368
3369         if (SONY_HWC_DOUBLE_SPEED(drive_config))
3370                 is_double_speed = 1;
3371
3372         tmp_irq = cdu31a_irq;   /* Need IRQ 0 because we can't sleep here. */
3373         cdu31a_irq = 0;
3374
3375         set_drive_params(sony_speed);
3376
3377         cdu31a_irq = tmp_irq;
3378
3379         if (cdu31a_irq > 0) {
3380                 if (request_irq
3381                     (cdu31a_irq, cdu31a_interrupt, SA_INTERRUPT,
3382                      "cdu31a", NULL)) {
3383                         printk
3384                             ("Unable to grab IRQ%d for the CDU31A driver\n",
3385                              cdu31a_irq);
3386                         cdu31a_irq = 0;
3387                 }
3388         }
3389
3390         sprintf(msg, "Sony I/F CDROM : %8.8s %16.16s %8.8s\n",
3391                 drive_config.vendor_id,
3392                 drive_config.product_id,
3393                 drive_config.product_rev_level);
3394         sprintf(buf, "  Capabilities: %s",
3395                 load_mech[SONY_HWC_GET_LOAD_MECH(drive_config)]);
3396         strcat(msg, buf);
3397         if (SONY_HWC_AUDIO_PLAYBACK(drive_config))
3398                 strcat(msg, ", audio");
3399         else
3400                 deficiency |= CDC_PLAY_AUDIO;
3401         if (SONY_HWC_EJECT(drive_config))
3402                 strcat(msg, ", eject");
3403         else
3404                 deficiency |= CDC_OPEN_TRAY;
3405         if (SONY_HWC_LED_SUPPORT(drive_config))
3406                 strcat(msg, ", LED");
3407         if (SONY_HWC_ELECTRIC_VOLUME(drive_config))
3408                 strcat(msg, ", elec. Vol");
3409         if (SONY_HWC_ELECTRIC_VOLUME_CTL(drive_config))
3410                 strcat(msg, ", sep. Vol");
3411         if (is_double_speed)
3412                 strcat(msg, ", double speed");
3413         else
3414                 deficiency |= CDC_SELECT_SPEED;
3415         if (cdu31a_irq > 0) {
3416                 sprintf(buf, ", irq %d", cdu31a_irq);
3417                 strcat(msg, buf);
3418         }
3419         strcat(msg, "\n");
3420
3421         is_a_cdu31a =
3422             strcmp("CD-ROM CDU31A", drive_config.product_id) == 0;
3423
3424         cdu31a_queue = blk_init_queue(do_cdu31a_request, &cdu31a_lock);
3425         if (!cdu31a_queue)
3426                 goto errout0;
3427
3428         init_timer(&cdu31a_abort_timer);
3429         cdu31a_abort_timer.function = handle_abort_timeout;
3430
3431         scd_info.mask = deficiency;
3432         scd_gendisk = disk;
3433         if (register_cdrom(&scd_info))
3434                 goto err;
3435         disk->queue = cdu31a_queue;
3436         add_disk(disk);
3437
3438         disk_changed = 1;
3439         return (0);
3440
3441 err:
3442         blk_cleanup_queue(cdu31a_queue);
3443 errout0:
3444         if (cdu31a_irq)
3445                 free_irq(cdu31a_irq, NULL);
3446         printk("Unable to register CDU-31a with Uniform cdrom driver\n");
3447         put_disk(disk);
3448 errout1:
3449         if (unregister_blkdev(MAJOR_NR, "cdu31a")) {
3450                 printk("Can't unregister block device for cdu31a\n");
3451         }
3452 errout2:
3453         release_region(cdu31a_port, 4);
3454 errout3:
3455         return -EIO;
3456 }
3457
3458
3459 void __exit cdu31a_exit(void)
3460 {
3461         del_gendisk(scd_gendisk);
3462         put_disk(scd_gendisk);
3463         if (unregister_cdrom(&scd_info)) {
3464                 printk
3465                     ("Can't unregister cdu31a from Uniform cdrom driver\n");
3466                 return;
3467         }
3468         if ((unregister_blkdev(MAJOR_NR, "cdu31a") == -EINVAL)) {
3469                 printk("Can't unregister cdu31a\n");
3470                 return;
3471         }
3472
3473         blk_cleanup_queue(cdu31a_queue);
3474
3475         if (cdu31a_irq > 0)
3476                 free_irq(cdu31a_irq, NULL);
3477
3478         release_region(cdu31a_port, 4);
3479         printk(KERN_INFO "cdu31a module released.\n");
3480 }
3481
3482 #ifdef MODULE
3483 module_init(cdu31a_init);
3484 #endif
3485 module_exit(cdu31a_exit);
3486
3487 MODULE_LICENSE("GPL");
3488 MODULE_ALIAS_BLOCKDEV_MAJOR(CDU31A_CDROM_MAJOR);