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