File: /usr/src/linux/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     	bytesleft = nblocks * 512;
1365     	offset = 0;
1366     
1367     	/* If the data in the read-ahead does not match the block offset,
1368     	   then fix things up. */
1369     	if (((block % 4) * 512) != ((2048 - readahead_dataleft) % 2048)) {
1370     		sony_next_block += block % 4;
1371     		sony_blocks_left -= block % 4;
1372     		skip = (block % 4) * 512;
1373     	} else {
1374     		skip = 0;
1375     	}
1376     
1377     	/* We have readahead data in the buffer, get that first before we
1378     	   decide if a read is necessary. */
1379     	if (readahead_dataleft != 0) {
1380     		if (bytesleft > readahead_dataleft) {
1381     			/* The readahead will not fill the requested buffer, but
1382     			   get the data out of the readahead into the buffer. */
1383     			memcpy(buffer,
1384     			       readahead_buffer + (2048 -
1385     						   readahead_dataleft),
1386     			       readahead_dataleft);
1387     			readahead_dataleft = 0;
1388     			bytesleft -= readahead_dataleft;
1389     			offset += readahead_dataleft;
1390     		} else {
1391     			/* The readahead will fill the whole buffer, get the data
1392     			   and return. */
1393     			memcpy(buffer,
1394     			       readahead_buffer + (2048 -
1395     						   readahead_dataleft),
1396     			       bytesleft);
1397     			readahead_dataleft -= bytesleft;
1398     			bytesleft = 0;
1399     			sony_blocks_left -= nblocks;
1400     			sony_next_block += nblocks;
1401     
1402     			/* If the data in the readahead is bad, return an error so the
1403     			   driver will abort the buffer. */
1404     			if (readahead_bad) {
1405     				res_reg[0] = 0x20;
1406     				res_reg[1] = SONY_BAD_DATA_ERR;
1407     				*res_size = 2;
1408     			}
1409     
1410     			if (readahead_dataleft == 0) {
1411     				readahead_bad = 0;
1412     			}
1413     
1414     			/* Final transfer is done for read command, get final result. */
1415     			if (sony_blocks_left == 0) {
1416     				get_result(res_reg, res_size);
1417     			}
1418     #if DEBUG
1419     			printk("Leaving read_data_block at %d\n",
1420     			       __LINE__);
1421     #endif
1422     			return;
1423     		}
1424     	}
1425     
1426     	/* Wait for the drive to tell us we have something */
1427     	retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
1428     	while (time_before(jiffies, retry_count) && !(is_data_ready())) {
1429     		while (handle_sony_cd_attention());
1430     
1431     		sony_sleep();
1432     	}
1433     	if (!(is_data_ready())) {
1434     		if (is_result_ready()) {
1435     			get_result(res_reg, res_size);
1436     			if ((res_reg[0] & 0xf0) != 0x20) {
1437     				printk
1438     				    ("CDU31A: Got result that should have been error: %d\n",
1439     				     res_reg[0]);
1440     				res_reg[0] = 0x20;
1441     				res_reg[1] = SONY_BAD_DATA_ERR;
1442     				*res_size = 2;
1443     			}
1444     			abort_read();
1445     		} else {
1446     #if DEBUG
1447     			printk("CDU31A timeout out %d\n", __LINE__);
1448     #endif
1449     			res_reg[0] = 0x20;
1450     			res_reg[1] = SONY_TIMEOUT_OP_ERR;
1451     			*res_size = 2;
1452     			abort_read();
1453     		}
1454     	} else {
1455     		input_data(buffer, bytesleft, nblocks, offset, skip);
1456     
1457     		/* Wait for the status from the drive. */
1458     		retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
1459     		while (time_before(jiffies, retry_count)
1460     		       && !(is_result_ready())) {
1461     			while (handle_sony_cd_attention());
1462     
1463     			sony_sleep();
1464     		}
1465     
1466     		if (!is_result_ready()) {
1467     #if DEBUG
1468     			printk("CDU31A timeout out %d\n", __LINE__);
1469     #endif
1470     			res_reg[0] = 0x20;
1471     			res_reg[1] = SONY_TIMEOUT_OP_ERR;
1472     			*res_size = 2;
1473     			abort_read();
1474     		} else {
1475     			get_result(res_reg, res_size);
1476     
1477     			/* If we got a buffer status, handle that. */
1478     			if ((res_reg[0] & 0xf0) == 0x50) {
1479     
1480     				if ((res_reg[0] ==
1481     				     SONY_NO_CIRC_ERR_BLK_STAT)
1482     				    || (res_reg[0] ==
1483     					SONY_NO_LECC_ERR_BLK_STAT)
1484     				    || (res_reg[0] ==
1485     					SONY_RECOV_LECC_ERR_BLK_STAT)) {
1486     					/* The data was successful, but if data was read from
1487     					   the readahead  and it was bad, set the whole
1488     					   buffer as bad. */
1489     					if (readahead_bad) {
1490     						readahead_bad = 0;
1491     						res_reg[0] = 0x20;
1492     						res_reg[1] =
1493     						    SONY_BAD_DATA_ERR;
1494     						*res_size = 2;
1495     					}
1496     				} else {
1497     					printk
1498     					    ("CDU31A: Data block error: 0x%x\n",
1499     					     res_reg[0]);
1500     					res_reg[0] = 0x20;
1501     					res_reg[1] = SONY_BAD_DATA_ERR;
1502     					*res_size = 2;
1503     
1504     					/* Data is in the readahead buffer but an error was returned.
1505     					   Make sure future requests don't use the data. */
1506     					if (bytesleft != 2048) {
1507     						readahead_bad = 1;
1508     					}
1509     				}
1510     
1511     				/* Final transfer is done for read command, get final result. */
1512     				if (sony_blocks_left == 0) {
1513     					get_result(res_reg, res_size);
1514     				}
1515     			} else if ((res_reg[0] & 0xf0) != 0x20) {
1516     				/* The drive gave me bad status, I don't know what to do.
1517     				   Reset the driver and return an error. */
1518     				printk
1519     				    ("CDU31A: Invalid block status: 0x%x\n",
1520     				     res_reg[0]);
1521     				restart_on_error();
1522     				res_reg[0] = 0x20;
1523     				res_reg[1] = SONY_BAD_DATA_ERR;
1524     				*res_size = 2;
1525     			}
1526     		}
1527     	}
1528     #if DEBUG
1529     	printk("Leaving read_data_block at %d\n", __LINE__);
1530     #endif
1531     }
1532     
1533     
1534     /*
1535      * The OS calls this to perform a read or write operation to the drive.
1536      * Write obviously fail.  Reads to a read ahead of sony_buffer_size
1537      * bytes to help speed operations.  This especially helps since the OS
1538      * uses 1024 byte blocks and the drive uses 2048 byte blocks.  Since most
1539      * data access on a CD is done sequentially, this saves a lot of operations.
1540      */
1541     static void do_cdu31a_request(request_queue_t * q)
1542     {
1543     	int block;
1544     	int nblock;
1545     	unsigned char res_reg[12];
1546     	unsigned int res_size;
1547     	int num_retries;
1548     	unsigned long flags;
1549     
1550     
1551     #if DEBUG
1552     	printk("Entering do_cdu31a_request\n");
1553     #endif
1554     
1555     	/* 
1556     	 * Make sure no one else is using the driver; wait for them
1557     	 * to finish if it is so.
1558     	 */
1559     	save_flags(flags);
1560     	cli();
1561     	while (sony_inuse) {
1562     		interruptible_sleep_on(&sony_wait);
1563     		if (signal_pending(current)) {
1564     			restore_flags(flags);
1565     			if (!QUEUE_EMPTY
1566     			    && CURRENT->rq_status != RQ_INACTIVE) {
1567     				end_request(0);
1568     			}
1569     			restore_flags(flags);
1570     #if DEBUG
1571     			printk("Leaving do_cdu31a_request at %d\n",
1572     			       __LINE__);
1573     #endif
1574     			return;
1575     		}
1576     	}
1577     	sony_inuse = 1;
1578     	has_cd_task = current;
1579     
1580     	/* Get drive status before doing anything. */
1581     	while (handle_sony_cd_attention());
1582     
1583     	/* Make sure we have a valid TOC. */
1584     	sony_get_toc();
1585     
1586     	spin_unlock_irq(&io_request_lock);
1587     
1588     	/* Make sure the timer is cancelled. */
1589     	del_timer(&cdu31a_abort_timer);
1590     
1591     	while (1) {
1592     	      cdu31a_request_startover:
1593     		/*
1594     		 * The beginning here is stolen from the hard disk driver.  I hope
1595     		 * it's right.
1596     		 */
1597     		if (QUEUE_EMPTY || CURRENT->rq_status == RQ_INACTIVE) {
1598     			goto end_do_cdu31a_request;
1599     		}
1600     
1601     		if (!sony_spun_up) {
1602     			scd_spinup();
1603     		}
1604     
1605     		/* I don't use INIT_REQUEST because it calls return, which would
1606     		   return without unlocking the device.  It shouldn't matter,
1607     		   but just to be safe... */
1608     		if (MAJOR(CURRENT->rq_dev) != MAJOR_NR) {
1609     			panic(DEVICE_NAME ": request list destroyed");
1610     		}
1611     		if (CURRENT->bh) {
1612     			if (!buffer_locked(CURRENT->bh)) {
1613     				panic(DEVICE_NAME ": block not locked");
1614     			}
1615     		}
1616     
1617     		block = CURRENT->sector;
1618     		nblock = CURRENT->nr_sectors;
1619     
1620     		if (!sony_toc_read) {
1621     			printk("CDU31A: TOC not read\n");
1622     			end_request(0);
1623     			goto cdu31a_request_startover;
1624     		}
1625     
1626     		switch (CURRENT->cmd) {
1627     		case READ:
1628     			/*
1629     			 * If the block address is invalid or the request goes beyond the end of
1630     			 * the media, return an error.
1631     			 */
1632     #if 0
1633     			if ((block / 4) < sony_toc.start_track_lba) {
1634     				printk
1635     				    ("CDU31A: Request before beginning of media\n");
1636     				end_request(0);
1637     				goto cdu31a_request_startover;
1638     			}
1639     #endif
1640     			if ((block / 4) >= sony_toc.lead_out_start_lba) {
1641     				printk
1642     				    ("CDU31A: Request past end of media\n");
1643     				end_request(0);
1644     				goto cdu31a_request_startover;
1645     			}
1646     			if (((block + nblock) / 4) >=
1647     			    sony_toc.lead_out_start_lba) {
1648     				printk
1649     				    ("CDU31A: Request past end of media\n");
1650     				end_request(0);
1651     				goto cdu31a_request_startover;
1652     			}
1653     
1654     			num_retries = 0;
1655     
1656     		      try_read_again:
1657     			while (handle_sony_cd_attention());
1658     
1659     			if (!sony_toc_read) {
1660     				printk("CDU31A: TOC not read\n");
1661     				end_request(0);
1662     				goto cdu31a_request_startover;
1663     			}
1664     
1665     			/* If no data is left to be read from the drive, start the
1666     			   next request. */
1667     			if (sony_blocks_left == 0) {
1668     				if (start_request
1669     				    (block / 4, CDU31A_READAHEAD / 4, 0)) {
1670     					end_request(0);
1671     					goto cdu31a_request_startover;
1672     				}
1673     			}
1674     			/* If the requested block is not the next one waiting in
1675     			   the driver, abort the current operation and start a
1676     			   new one. */
1677     			else if (block != sony_next_block) {
1678     #if DEBUG
1679     				printk
1680     				    ("CDU31A Warning: Read for block %d, expected %d\n",
1681     				     block, sony_next_block);
1682     #endif
1683     				abort_read();
1684     				if (!sony_toc_read) {
1685     					printk("CDU31A: TOC not read\n");
1686     					end_request(0);
1687     					goto cdu31a_request_startover;
1688     				}
1689     				if (start_request
1690     				    (block / 4, CDU31A_READAHEAD / 4, 0)) {
1691     					printk
1692     					    ("CDU31a: start request failed\n");
1693     					end_request(0);
1694     					goto cdu31a_request_startover;
1695     				}
1696     			}
1697     
1698     			read_data_block(CURRENT->buffer, block, nblock,
1699     					res_reg, &res_size);
1700     			if (res_reg[0] == 0x20) {
1701     				if (num_retries > MAX_CDU31A_RETRIES) {
1702     					end_request(0);
1703     					goto cdu31a_request_startover;
1704     				}
1705     
1706     				num_retries++;
1707     				if (res_reg[1] == SONY_NOT_SPIN_ERR) {
1708     					do_sony_cd_cmd(SONY_SPIN_UP_CMD,
1709     						       NULL, 0, res_reg,
1710     						       &res_size);
1711     				} else {
1712     					printk
1713     					    ("CDU31A: %s error for block %d, nblock %d\n",
1714     					     translate_error(res_reg[1]),
1715     					     block, nblock);
1716     				}
1717     				goto try_read_again;
1718     			} else {
1719     				end_request(1);
1720     			}
1721     			break;
1722     
1723     		case WRITE:
1724     			end_request(0);
1725     			break;
1726     
1727     		default:
1728     			panic("CDU31A: Unknown cmd");
1729     		}
1730     	}
1731     
1732           end_do_cdu31a_request:
1733     	spin_lock_irq(&io_request_lock);
1734     #if 0
1735     	/* After finished, cancel any pending operations. */
1736     	abort_read();
1737     #else
1738     	/* Start a timer to time out after a while to disable
1739     	   the read. */
1740     	cdu31a_abort_timer.expires = jiffies + 2 * HZ;	/* Wait 2 seconds */
1741     	add_timer(&cdu31a_abort_timer);
1742     #endif
1743     
1744     	has_cd_task = NULL;
1745     	sony_inuse = 0;
1746     	wake_up_interruptible(&sony_wait);
1747     	restore_flags(flags);
1748     #if DEBUG
1749     	printk("Leaving do_cdu31a_request at %d\n", __LINE__);
1750     #endif
1751     }
1752     
1753     
1754     /*
1755      * Read the table of contents from the drive and set up TOC if
1756      * successful.
1757      */
1758     static void sony_get_toc(void)
1759     {
1760     	unsigned char res_reg[2];
1761     	unsigned int res_size;
1762     	unsigned char parms[1];
1763     	int session;
1764     	int num_spin_ups;
1765     	int totaltracks = 0;
1766     	int mint = 99;
1767     	int maxt = 0;
1768     
1769     #if DEBUG
1770     	printk("Entering sony_get_toc\n");
1771     #endif
1772     
1773     	num_spin_ups = 0;
1774     	if (!sony_toc_read) {
1775     	      respinup_on_gettoc:
1776     		/* Ignore the result, since it might error if spinning already. */
1777     		do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg,
1778     			       &res_size);
1779     
1780     		do_sony_cd_cmd(SONY_READ_TOC_CMD, NULL, 0, res_reg,
1781     			       &res_size);
1782     
1783     		/* The drive sometimes returns error 0.  I don't know why, but ignore
1784     		   it.  It seems to mean the drive has already done the operation. */
1785     		if ((res_size < 2)
1786     		    || ((res_reg[0] != 0) && (res_reg[1] != 0))) {
1787     			/* If the drive is already playing, it's ok.  */
1788     			if ((res_reg[1] == SONY_AUDIO_PLAYING_ERR)
1789     			    || (res_reg[1] == 0)) {
1790     				goto gettoc_drive_spinning;
1791     			}
1792     
1793     			/* If the drive says it is not spun up (even though we just did it!)
1794     			   then retry the operation at least a few times. */
1795     			if ((res_reg[1] == SONY_NOT_SPIN_ERR)
1796     			    && (num_spin_ups < MAX_CDU31A_RETRIES)) {
1797     				num_spin_ups++;
1798     				goto respinup_on_gettoc;
1799     			}
1800     
1801     			printk("cdu31a: Error reading TOC: %x %s\n",
1802     			       res_reg[0], translate_error(res_reg[1]));
1803     			return;
1804     		}
1805     
1806     	      gettoc_drive_spinning:
1807     
1808     		/* The idea here is we keep asking for sessions until the command
1809     		   fails.  Then we know what the last valid session on the disk is.
1810     		   No need to check session 0, since session 0 is the same as session
1811     		   1; the command returns different information if you give it 0. 
1812     		 */
1813     #if DEBUG
1814     		memset(&sony_toc, 0x0e, sizeof(sony_toc));
1815     		memset(&single_toc, 0x0f, sizeof(single_toc));
1816     #endif
1817     		session = 1;
1818     		while (1) {
1819     /* This seems to slow things down enough to make it work.  This
1820      * appears to be a problem in do_sony_cd_cmd.  This printk seems 
1821      * to address the symptoms...  -Erik */
1822     #if 1
1823     			printk("cdu31a: Trying session %d\n", session);
1824     #endif
1825     			parms[0] = session;
1826     			do_sony_cd_cmd(SONY_READ_TOC_SPEC_CMD,
1827     				       parms, 1, res_reg, &res_size);
1828     
1829     #if DEBUG
1830     			printk("%2.2x %2.2x\n", res_reg[0], res_reg[1]);
1831     #endif
1832     
1833     			if ((res_size < 2)
1834     			    || ((res_reg[0] & 0xf0) == 0x20)) {
1835     				/* An error reading the TOC, this must be past the last session. */
1836     				if (session == 1)
1837     					printk
1838     					    ("Yikes! Couldn't read any sessions!");
1839     				break;
1840     			}
1841     #if DEBUG
1842     			printk("Reading session %d\n", session);
1843     #endif
1844     
1845     			parms[0] = session;
1846     			do_sony_cd_cmd(SONY_REQ_TOC_DATA_SPEC_CMD,
1847     				       parms,
1848     				       1,
1849     				       (unsigned char *) &single_toc,
1850     				       &res_size);
1851     			if ((res_size < 2)
1852     			    || ((single_toc.exec_status[0] & 0xf0) ==
1853     				0x20)) {
1854     				printk
1855     				    ("cdu31a: Error reading session %d: %x %s\n",
1856     				     session, single_toc.exec_status[0],
1857     				     translate_error(single_toc.
1858     						     exec_status[1]));
1859     				/* An error reading the TOC.  Return without sony_toc_read
1860     				   set. */
1861     				return;
1862     			}
1863     #if DEBUG
1864     			printk
1865     			    ("add0 %01x, con0 %01x, poi0 %02x, 1st trk %d, dsktyp %x, dum0 %x\n",
1866     			     single_toc.address0, single_toc.control0,
1867     			     single_toc.point0,
1868     			     bcd_to_int(single_toc.first_track_num),
1869     			     single_toc.disk_type, single_toc.dummy0);
1870     			printk
1871     			    ("add1 %01x, con1 %01x, poi1 %02x, lst trk %d, dummy1 %x, dum2 %x\n",
1872     			     single_toc.address1, single_toc.control1,
1873     			     single_toc.point1,
1874     			     bcd_to_int(single_toc.last_track_num),
1875     			     single_toc.dummy1, single_toc.dummy2);
1876     			printk
1877     			    ("add2 %01x, con2 %01x, poi2 %02x leadout start min %d, sec %d, frame %d\n",
1878     			     single_toc.address2, single_toc.control2,
1879     			     single_toc.point2,
1880     			     bcd_to_int(single_toc.lead_out_start_msf[0]),
1881     			     bcd_to_int(single_toc.lead_out_start_msf[1]),
1882     			     bcd_to_int(single_toc.lead_out_start_msf[2]));
1883     			if (res_size > 18 && single_toc.pointb0 > 0xaf)
1884     				printk
1885     				    ("addb0 %01x, conb0 %01x, poib0 %02x, nextsession min %d, sec %d, frame %d\n"
1886     				     "#mode5_ptrs %02d, max_start_outer_leadout_msf min %d, sec %d, frame %d\n",
1887     				     single_toc.addressb0,
1888     				     single_toc.controlb0,
1889     				     single_toc.pointb0,
1890     				     bcd_to_int(single_toc.
1891     						next_poss_prog_area_msf
1892     						[0]),
1893     				     bcd_to_int(single_toc.
1894     						next_poss_prog_area_msf
1895     						[1]),
1896     				     bcd_to_int(single_toc.
1897     						next_poss_prog_area_msf
1898     						[2]),
1899     				     single_toc.num_mode_5_pointers,
1900     				     bcd_to_int(single_toc.
1901     						max_start_outer_leadout_msf
1902     						[0]),
1903     				     bcd_to_int(single_toc.
1904     						max_start_outer_leadout_msf
1905     						[1]),
1906     				     bcd_to_int(single_toc.
1907     						max_start_outer_leadout_msf
1908     						[2]));
1909     			if (res_size > 27 && single_toc.pointb1 > 0xaf)
1910     				printk
1911     				    ("addb1 %01x, conb1 %01x, poib1 %02x, %x %x %x %x #skipint_ptrs %d, #skiptrkassign %d %x\n",
1912     				     single_toc.addressb1,
1913     				     single_toc.controlb1,
1914     				     single_toc.pointb1,
1915     				     single_toc.dummyb0_1[0],
1916     				     single_toc.dummyb0_1[1],
1917     				     single_toc.dummyb0_1[2],
1918     				     single_toc.dummyb0_1[3],
1919     				     single_toc.num_skip_interval_pointers,
1920     				     single_toc.num_skip_track_assignments,
1921     				     single_toc.dummyb0_2);
1922     			if (res_size > 36 && single_toc.pointb2 > 0xaf)
1923     				printk
1924     				    ("addb2 %01x, conb2 %01x, poib2 %02x, %02x %02x %02x %02x %02x %02x %02x\n",
1925     				     single_toc.addressb2,
1926     				     single_toc.controlb2,
1927     				     single_toc.pointb2,
1928     				     single_toc.tracksb2[0],
1929     				     single_toc.tracksb2[1],
1930     				     single_toc.tracksb2[2],
1931     				     single_toc.tracksb2[3],
1932     				     single_toc.tracksb2[4],
1933     				     single_toc.tracksb2[5],
1934     				     single_toc.tracksb2[6]);
1935     			if (res_size > 45 && single_toc.pointb3 > 0xaf)
1936     				printk
1937     				    ("addb3 %01x, conb3 %01x, poib3 %02x, %02x %02x %02x %02x %02x %02x %02x\n",
1938     				     single_toc.addressb3,
1939     				     single_toc.controlb3,
1940     				     single_toc.pointb3,
1941     				     single_toc.tracksb3[0],
1942     				     single_toc.tracksb3[1],
1943     				     single_toc.tracksb3[2],
1944     				     single_toc.tracksb3[3],
1945     				     single_toc.tracksb3[4],
1946     				     single_toc.tracksb3[5],
1947     				     single_toc.tracksb3[6]);
1948     			if (res_size > 54 && single_toc.pointb4 > 0xaf)
1949     				printk
1950     				    ("addb4 %01x, conb4 %01x, poib4 %02x, %02x %02x %02x %02x %02x %02x %02x\n",
1951     				     single_toc.addressb4,
1952     				     single_toc.controlb4,
1953     				     single_toc.pointb4,
1954     				     single_toc.tracksb4[0],
1955     				     single_toc.tracksb4[1],
1956     				     single_toc.tracksb4[2],
1957     				     single_toc.tracksb4[3],
1958     				     single_toc.tracksb4[4],
1959     				     single_toc.tracksb4[5],
1960     				     single_toc.tracksb4[6]);
1961     			if (res_size > 63 && single_toc.pointc0 > 0xaf)
1962     				printk
1963     				    ("addc0 %01x, conc0 %01x, poic0 %02x, %02x %02x %02x %02x %02x %02x %02x\n",
1964     				     single_toc.addressc0,
1965     				     single_toc.controlc0,
1966     				     single_toc.pointc0,
1967     				     single_toc.dummyc0[0],
1968     				     single_toc.dummyc0[1],
1969     				     single_toc.dummyc0[2],
1970     				     single_toc.dummyc0[3],
1971     				     single_toc.dummyc0[4],
1972     				     single_toc.dummyc0[5],
1973     				     single_toc.dummyc0[6]);
1974     #endif
1975     #undef DEBUG
1976     #define DEBUG 0
1977     
1978     			sony_toc.lead_out_start_msf[0] =
1979     			    bcd_to_int(single_toc.lead_out_start_msf[0]);
1980     			sony_toc.lead_out_start_msf[1] =
1981     			    bcd_to_int(single_toc.lead_out_start_msf[1]);
1982     			sony_toc.lead_out_start_msf[2] =
1983     			    bcd_to_int(single_toc.lead_out_start_msf[2]);
1984     			sony_toc.lead_out_start_lba =
1985     			    single_toc.lead_out_start_lba =
1986     			    msf_to_log(sony_toc.lead_out_start_msf);
1987     
1988     			/* For points that do not exist, move the data over them
1989     			   to the right location. */
1990     			if (single_toc.pointb0 != 0xb0) {
1991     				memmove(((char *) &single_toc) + 27,
1992     					((char *) &single_toc) + 18,
1993     					res_size - 18);
1994     				res_size += 9;
1995     			} else if (res_size > 18) {
1996     				sony_toc.lead_out_start_msf[0] =
1997     				    bcd_to_int(single_toc.
1998     					       max_start_outer_leadout_msf
1999     					       [0]);
2000     				sony_toc.lead_out_start_msf[1] =
2001     				    bcd_to_int(single_toc.
2002     					       max_start_outer_leadout_msf
2003     					       [1]);
2004     				sony_toc.lead_out_start_msf[2] =
2005     				    bcd_to_int(single_toc.
2006     					       max_start_outer_leadout_msf
2007     					       [2]);
2008     				sony_toc.lead_out_start_lba =
2009     				    msf_to_log(sony_toc.
2010     					       lead_out_start_msf);
2011     			}
2012     			if (single_toc.pointb1 != 0xb1) {
2013     				memmove(((char *) &single_toc) + 36,
2014     					((char *) &single_toc) + 27,
2015     					res_size - 27);
2016     				res_size += 9;
2017     			}
2018     			if (single_toc.pointb2 != 0xb2) {
2019     				memmove(((char *) &single_toc) + 45,
2020     					((char *) &single_toc) + 36,
2021     					res_size - 36);
2022     				res_size += 9;
2023     			}
2024     			if (single_toc.pointb3 != 0xb3) {
2025     				memmove(((char *) &single_toc) + 54,
2026     					((char *) &single_toc) + 45,
2027     					res_size - 45);
2028     				res_size += 9;
2029     			}
2030     			if (single_toc.pointb4 != 0xb4) {
2031     				memmove(((char *) &single_toc) + 63,
2032     					((char *) &single_toc) + 54,
2033     					res_size - 54);
2034     				res_size += 9;
2035     			}
2036     			if (single_toc.pointc0 != 0xc0) {
2037     				memmove(((char *) &single_toc) + 72,
2038     					((char *) &single_toc) + 63,
2039     					res_size - 63);
2040     				res_size += 9;
2041     			}
2042     #if DEBUG
2043     			printk
2044     			    ("start track lba %u,  leadout start lba %u\n",
2045     			     single_toc.start_track_lba,
2046     			     single_toc.lead_out_start_lba);
2047     			{
2048     				int i;
2049     				for (i = 0;
2050     				     i <
2051     				     1 +
2052     				     bcd_to_int(single_toc.last_track_num)
2053     				     -
2054     				     bcd_to_int(single_toc.
2055     						first_track_num); i++) {
2056     					printk
2057     					    ("trk %02d: add 0x%01x, con 0x%01x,  track %02d, start min %02d, sec %02d, frame %02d\n",
2058     					     i,
2059     					     single_toc.tracks[i].address,
2060     					     single_toc.tracks[i].control,
2061     					     bcd_to_int(single_toc.
2062     							tracks[i].track),
2063     					     bcd_to_int(single_toc.
2064     							tracks[i].
2065     							track_start_msf
2066     							[0]),
2067     					     bcd_to_int(single_toc.
2068     							tracks[i].
2069     							track_start_msf
2070     							[1]),
2071     					     bcd_to_int(single_toc.
2072     							tracks[i].
2073     							track_start_msf
2074     							[2]));
2075     					if (mint >
2076     					    bcd_to_int(single_toc.
2077     						       tracks[i].track))
2078     						mint =
2079     						    bcd_to_int(single_toc.
2080     							       tracks[i].
2081     							       track);
2082     					if (maxt <
2083     					    bcd_to_int(single_toc.
2084     						       tracks[i].track))
2085     						maxt =
2086     						    bcd_to_int(single_toc.
2087     							       tracks[i].
2088     							       track);
2089     				}
2090     				printk
2091     				    ("min track number %d,   max track number %d\n",
2092     				     mint, maxt);
2093     			}
2094     #endif
2095     
2096     			/* prepare a special table of contents for a CD-I disc. They don't have one. */
2097     			if (single_toc.disk_type == 0x10 &&
2098     			    single_toc.first_track_num == 2 &&
2099     			    single_toc.last_track_num == 2 /* CD-I */ ) {
2100     				sony_toc.tracks[totaltracks].address = 1;
2101     				sony_toc.tracks[totaltracks].control = 4;	/* force data tracks */
2102     				sony_toc.tracks[totaltracks].track = 1;
2103     				sony_toc.tracks[totaltracks].
2104     				    track_start_msf[0] = 0;
2105     				sony_toc.tracks[totaltracks].
2106     				    track_start_msf[1] = 2;
2107     				sony_toc.tracks[totaltracks].
2108     				    track_start_msf[2] = 0;
2109     				mint = maxt = 1;
2110     				totaltracks++;
2111     			} else
2112     				/* gather track entries from this session */
2113     			{
2114     				int i;
2115     				for (i = 0;
2116     				     i <
2117     				     1 +
2118     				     bcd_to_int(single_toc.last_track_num)
2119     				     -
2120     				     bcd_to_int(single_toc.
2121     						first_track_num);
2122     				     i++, totaltracks++) {
2123     					sony_toc.tracks[totaltracks].
2124     					    address =
2125     					    single_toc.tracks[i].address;
2126     					sony_toc.tracks[totaltracks].
2127     					    control =
2128     					    single_toc.tracks[i].control;
2129     					sony_toc.tracks[totaltracks].
2130     					    track =
2131     					    bcd_to_int(single_toc.
2132     						       tracks[i].track);
2133     					sony_toc.tracks[totaltracks].
2134     					    track_start_msf[0] =
2135     					    bcd_to_int(single_toc.
2136     						       tracks[i].
2137     						       track_start_msf[0]);
2138     					sony_toc.tracks[totaltracks].
2139     					    track_start_msf[1] =
2140     					    bcd_to_int(single_toc.
2141     						       tracks[i].
2142     						       track_start_msf[1]);
2143     					sony_toc.tracks[totaltracks].
2144     					    track_start_msf[2] =
2145     					    bcd_to_int(single_toc.
2146     						       tracks[i].
2147     						       track_start_msf[2]);
2148     					if (i == 0)
2149     						single_toc.
2150     						    start_track_lba =
2151     						    msf_to_log(sony_toc.
2152     							       tracks
2153     							       [totaltracks].
2154     							       track_start_msf);
2155     					if (mint >
2156     					    sony_toc.tracks[totaltracks].
2157     					    track)
2158     						mint =
2159     						    sony_toc.
2160     						    tracks[totaltracks].
2161     						    track;
2162     					if (maxt <
2163     					    sony_toc.tracks[totaltracks].
2164     					    track)
2165     						maxt =
2166     						    sony_toc.
2167     						    tracks[totaltracks].
2168     						    track;
2169     				}
2170     			}
2171     			sony_toc.first_track_num = mint;
2172     			sony_toc.last_track_num = maxt;
2173     			/* Disk type of last session wins. For example:
2174     			   CD-Extra has disk type 0 for the first session, so
2175     			   a dumb HiFi CD player thinks it is a plain audio CD.
2176     			   We are interested in the disk type of the last session,
2177     			   which is 0x20 (XA) for CD-Extra, so we can access the
2178     			   data track ... */
2179     			sony_toc.disk_type = single_toc.disk_type;
2180     			sony_toc.sessions = session;
2181     
2182     			/* don't believe everything :-) */
2183     			if (session == 1)
2184     				single_toc.start_track_lba = 0;
2185     			sony_toc.start_track_lba =
2186     			    single_toc.start_track_lba;
2187     
2188     			if (session > 1 && single_toc.pointb0 == 0xb0 &&
2189     			    sony_toc.lead_out_start_lba ==
2190     			    single_toc.lead_out_start_lba) {
2191     				break;
2192     			}
2193     
2194     			/* Let's not get carried away... */
2195     			if (session > 40) {
2196     				printk("cdu31a: too many sessions: %d\n",
2197     				       session);
2198     				break;
2199     			}
2200     			session++;
2201     		}
2202     		sony_toc.track_entries = totaltracks;
2203     		/* add one entry for the LAST track with track number CDROM_LEADOUT */
2204     		sony_toc.tracks[totaltracks].address = single_toc.address2;
2205     		sony_toc.tracks[totaltracks].control = single_toc.control2;
2206     		sony_toc.tracks[totaltracks].track = CDROM_LEADOUT;
2207     		sony_toc.tracks[totaltracks].track_start_msf[0] =
2208     		    sony_toc.lead_out_start_msf[0];
2209     		sony_toc.tracks[totaltracks].track_start_msf[1] =
2210     		    sony_toc.lead_out_start_msf[1];
2211     		sony_toc.tracks[totaltracks].track_start_msf[2] =
2212     		    sony_toc.lead_out_start_msf[2];
2213     
2214     		sony_toc_read = 1;
2215     #undef DEBUG
2216     #if DEBUG
2217     		printk
2218     		    ("Disk session %d, start track: %d, stop track: %d\n",
2219     		     session, single_toc.start_track_lba,
2220     		     single_toc.lead_out_start_lba);
2221     #endif
2222     	}
2223     #if DEBUG
2224     	printk("Leaving sony_get_toc\n");
2225     #endif
2226     }
2227     
2228     
2229     /*
2230      * Uniform cdrom interface function
2231      * return multisession offset and sector information
2232      */
2233     static int scd_get_last_session(struct cdrom_device_info *cdi,
2234     				struct cdrom_multisession *ms_info)
2235     {
2236     	if (ms_info == NULL)
2237     		return 1;
2238     
2239     	if (!sony_toc_read)
2240     		sony_get_toc();
2241     
2242     	ms_info->addr_format = CDROM_LBA;
2243     	ms_info->addr.lba = sony_toc.start_track_lba;
2244     	ms_info->xa_flag = sony_toc.disk_type == SONY_XA_DISK_TYPE ||
2245     	    sony_toc.disk_type == 0x10 /* CDI */ ;
2246     
2247     	return 0;
2248     }
2249     
2250     /*
2251      * Search for a specific track in the table of contents.
2252      */
2253     static int find_track(int track)
2254     {
2255     	int i;
2256     
2257     	for (i = 0; i <= sony_toc.track_entries; i++) {
2258     		if (sony_toc.tracks[i].track == track) {
2259     			return i;
2260     		}
2261     	}
2262     
2263     	return -1;
2264     }
2265     
2266     
2267     /*
2268      * Read the subcode and put it in last_sony_subcode for future use.
2269      */
2270     static int read_subcode(void)
2271     {
2272     	unsigned int res_size;
2273     
2274     
2275     	do_sony_cd_cmd(SONY_REQ_SUBCODE_ADDRESS_CMD,
2276     		       NULL,
2277     		       0, (unsigned char *) &last_sony_subcode, &res_size);
2278     	if ((res_size < 2)
2279     	    || ((last_sony_subcode.exec_status[0] & 0xf0) == 0x20)) {
2280     		printk("Sony CDROM error %s (read_subcode)\n",
2281     		       translate_error(last_sony_subcode.exec_status[1]));
2282     		return -EIO;
2283     	}
2284     
2285     	last_sony_subcode.track_num =
2286     	    bcd_to_int(last_sony_subcode.track_num);
2287     	last_sony_subcode.index_num =
2288     	    bcd_to_int(last_sony_subcode.index_num);
2289     	last_sony_subcode.abs_msf[0] =
2290     	    bcd_to_int(last_sony_subcode.abs_msf[0]);
2291     	last_sony_subcode.abs_msf[1] =
2292     	    bcd_to_int(last_sony_subcode.abs_msf[1]);
2293     	last_sony_subcode.abs_msf[2] =
2294     	    bcd_to_int(last_sony_subcode.abs_msf[2]);
2295     
2296     	last_sony_subcode.rel_msf[0] =
2297     	    bcd_to_int(last_sony_subcode.rel_msf[0]);
2298     	last_sony_subcode.rel_msf[1] =
2299     	    bcd_to_int(last_sony_subcode.rel_msf[1]);
2300     	last_sony_subcode.rel_msf[2] =
2301     	    bcd_to_int(last_sony_subcode.rel_msf[2]);
2302     	return 0;
2303     }
2304     
2305     /*
2306      * Uniform cdrom interface function
2307      * return the media catalog number found on some older audio cds
2308      */
2309     static int
2310     scd_get_mcn(struct cdrom_device_info *cdi, struct cdrom_mcn *mcn)
2311     {
2312     	unsigned char resbuffer[2 + 14];
2313     	unsigned char *mcnp = mcn->medium_catalog_number;
2314     	unsigned char *resp = resbuffer + 3;
2315     	unsigned int res_size;
2316     
2317     	memset(mcn->medium_catalog_number, 0, 14);
2318     	do_sony_cd_cmd(SONY_REQ_UPC_EAN_CMD,
2319     		       NULL, 0, resbuffer, &res_size);
2320     	if ((res_size < 2) || ((resbuffer[0] & 0xf0) == 0x20));
2321     	else {
2322     		/* packed bcd to single ASCII digits */
2323     		*mcnp++ = (*resp >> 4) + '0';
2324     		*mcnp++ = (*resp++ & 0x0f) + '0';
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     	}
2337     	*mcnp = '\0';
2338     	return 0;
2339     }
2340     
2341     
2342     /*
2343      * Get the subchannel info like the CDROMSUBCHNL command wants to see it.  If
2344      * the drive is playing, the subchannel needs to be read (since it would be
2345      * changing).  If the drive is paused or completed, the subcode information has
2346      * already been stored, just use that.  The ioctl call wants things in decimal
2347      * (not BCD), so all the conversions are done.
2348      */
2349     static int sony_get_subchnl_info(struct cdrom_subchnl *schi)
2350     {
2351     	/* Get attention stuff */
2352     	while (handle_sony_cd_attention());
2353     
2354     	sony_get_toc();
2355     	if (!sony_toc_read) {
2356     		return -EIO;
2357     	}
2358     
2359     	switch (sony_audio_status) {
2360     	case CDROM_AUDIO_NO_STATUS:
2361     	case CDROM_AUDIO_PLAY:
2362     		if (read_subcode() < 0) {
2363     			return -EIO;
2364     		}
2365     		break;
2366     
2367     	case CDROM_AUDIO_PAUSED:
2368     	case CDROM_AUDIO_COMPLETED:
2369     		break;
2370     
2371     #if 0
2372     	case CDROM_AUDIO_NO_STATUS:
2373     		schi->cdsc_audiostatus = sony_audio_status;
2374     		return 0;
2375     		break;
2376     #endif
2377     	case CDROM_AUDIO_INVALID:
2378     	case CDROM_AUDIO_ERROR:
2379     	default:
2380     		return -EIO;
2381     	}
2382     
2383     	schi->cdsc_audiostatus = sony_audio_status;
2384     	schi->cdsc_adr = last_sony_subcode.address;
2385     	schi->cdsc_ctrl = last_sony_subcode.control;
2386     	schi->cdsc_trk = last_sony_subcode.track_num;
2387     	schi->cdsc_ind = last_sony_subcode.index_num;
2388     	if (schi->cdsc_format == CDROM_MSF) {
2389     		schi->cdsc_absaddr.msf.minute =
2390     		    last_sony_subcode.abs_msf[0];
2391     		schi->cdsc_absaddr.msf.second =
2392     		    last_sony_subcode.abs_msf[1];
2393     		schi->cdsc_absaddr.msf.frame =
2394     		    last_sony_subcode.abs_msf[2];
2395     
2396     		schi->cdsc_reladdr.msf.minute =
2397     		    last_sony_subcode.rel_msf[0];
2398     		schi->cdsc_reladdr.msf.second =
2399     		    last_sony_subcode.rel_msf[1];
2400     		schi->cdsc_reladdr.msf.frame =
2401     		    last_sony_subcode.rel_msf[2];
2402     	} else if (schi->cdsc_format == CDROM_LBA) {
2403     		schi->cdsc_absaddr.lba =
2404     		    msf_to_log(last_sony_subcode.abs_msf);
2405     		schi->cdsc_reladdr.lba =
2406     		    msf_to_log(last_sony_subcode.rel_msf);
2407     	}
2408     
2409     	return 0;
2410     }
2411     
2412     /* Get audio data from the drive.  This is fairly complex because I
2413        am looking for status and data at the same time, but if I get status
2414        then I just look for data.  I need to get the status immediately so
2415        the switch from audio to data tracks will happen quickly. */
2416     static void
2417     read_audio_data(char *buffer, unsigned char res_reg[], int *res_size)
2418     {
2419     	unsigned int retry_count;
2420     	int result_read;
2421     
2422     
2423     	res_reg[0] = 0;
2424     	res_reg[1] = 0;
2425     	*res_size = 0;
2426     	result_read = 0;
2427     
2428     	/* Wait for the drive to tell us we have something */
2429     	retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
2430           continue_read_audio_wait:
2431     	while (time_before(jiffies, retry_count) && !(is_data_ready())
2432     	       && !(is_result_ready() || result_read)) {
2433     		while (handle_sony_cd_attention());
2434     
2435     		sony_sleep();
2436     	}
2437     	if (!(is_data_ready())) {
2438     		if (is_result_ready() && !result_read) {
2439     			get_result(res_reg, res_size);
2440     
2441     			/* Read block status and continue waiting for data. */
2442     			if ((res_reg[0] & 0xf0) == 0x50) {
2443     				result_read = 1;
2444     				goto continue_read_audio_wait;
2445     			}
2446     			/* Invalid data from the drive.  Shut down the operation. */
2447     			else if ((res_reg[0] & 0xf0) != 0x20) {
2448     				printk
2449     				    ("CDU31A: Got result that should have been error: %d\n",
2450     				     res_reg[0]);
2451     				res_reg[0] = 0x20;
2452     				res_reg[1] = SONY_BAD_DATA_ERR;
2453     				*res_size = 2;
2454     			}
2455     			abort_read();
2456     		} else {
2457     #if DEBUG
2458     			printk("CDU31A timeout out %d\n", __LINE__);
2459     #endif
2460     			res_reg[0] = 0x20;
2461     			res_reg[1] = SONY_TIMEOUT_OP_ERR;
2462     			*res_size = 2;
2463     			abort_read();
2464     		}
2465     	} else {
2466     		clear_data_ready();
2467     
2468     		/* If data block, then get 2340 bytes offset by 12. */
2469     		if (sony_raw_data_mode) {
2470     			insb(sony_cd_read_reg, buffer + CD_XA_HEAD,
2471     			     CD_FRAMESIZE_RAW1);
2472     		} else {
2473     			/* Audio gets the whole 2352 bytes. */
2474     			insb(sony_cd_read_reg, buffer, CD_FRAMESIZE_RAW);
2475     		}
2476     
2477     		/* If I haven't already gotten the result, get it now. */
2478     		if (!result_read) {
2479     			/* Wait for the drive to tell us we have something */
2480     			retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
2481     			while (time_before(jiffies, retry_count)
2482     			       && !(is_result_ready())) {
2483     				while (handle_sony_cd_attention());
2484     
2485     				sony_sleep();
2486     			}
2487     
2488     			if (!is_result_ready()) {
2489     #if DEBUG
2490     				printk("CDU31A timeout out %d\n",
2491     				       __LINE__);
2492     #endif
2493     				res_reg[0] = 0x20;
2494     				res_reg[1] = SONY_TIMEOUT_OP_ERR;
2495     				*res_size = 2;
2496     				abort_read();
2497     				return;
2498     			} else {
2499     				get_result(res_reg, res_size);
2500     			}
2501     		}
2502     
2503     		if ((res_reg[0] & 0xf0) == 0x50) {
2504     			if ((res_reg[0] == SONY_NO_CIRC_ERR_BLK_STAT)
2505     			    || (res_reg[0] == SONY_NO_LECC_ERR_BLK_STAT)
2506     			    || (res_reg[0] == SONY_RECOV_LECC_ERR_BLK_STAT)
2507     			    || (res_reg[0] == SONY_NO_ERR_DETECTION_STAT)) {
2508     				/* Ok, nothing to do. */
2509     			} else {
2510     				printk("CDU31A: Data block error: 0x%x\n",
2511     				       res_reg[0]);
2512     				res_reg[0] = 0x20;
2513     				res_reg[1] = SONY_BAD_DATA_ERR;
2514     				*res_size = 2;
2515     			}
2516     		} else if ((res_reg[0] & 0xf0) != 0x20) {
2517     			/* The drive gave me bad status, I don't know what to do.
2518     			   Reset the driver and return an error. */
2519     			printk("CDU31A: Invalid block status: 0x%x\n",
2520     			       res_reg[0]);
2521     			restart_on_error();
2522     			res_reg[0] = 0x20;
2523     			res_reg[1] = SONY_BAD_DATA_ERR;
2524     			*res_size = 2;
2525     		}
2526     	}
2527     }
2528     
2529     /* Perform a raw data read.  This will automatically detect the
2530        track type and read the proper data (audio or data). */
2531     static int read_audio(struct cdrom_read_audio *ra)
2532     {
2533     	int retval;
2534     	unsigned char params[2];
2535     	unsigned char res_reg[12];
2536     	unsigned int res_size;
2537     	unsigned int cframe;
2538     	unsigned long flags;
2539     
2540     	/* 
2541     	 * Make sure no one else is using the driver; wait for them
2542     	 * to finish if it is so.
2543     	 */
2544     	save_flags(flags);
2545     	cli();
2546     	while (sony_inuse) {
2547     		interruptible_sleep_on(&sony_wait);
2548     		if (signal_pending(current)) {
2549     			restore_flags(flags);
2550     			return -EAGAIN;
2551     		}
2552     	}
2553     	sony_inuse = 1;
2554     	has_cd_task = current;
2555     	restore_flags(flags);
2556     
2557     	if (!sony_spun_up) {
2558     		scd_spinup();
2559     	}
2560     
2561     	/* Set the drive to do raw operations. */
2562     	params[0] = SONY_SD_DECODE_PARAM;
2563     	params[1] = 0x06 | sony_raw_data_mode;
2564     	do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
2565     		       params, 2, res_reg, &res_size);
2566     	if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20)) {
2567     		printk("CDU31A: Unable to set decode params: 0x%2.2x\n",
2568     		       res_reg[1]);
2569     		return -EIO;
2570     	}
2571     
2572     	/* From here down, we have to goto exit_read_audio instead of returning
2573     	   because the drive parameters have to be set back to data before
2574     	   return. */
2575     
2576     	retval = 0;
2577     	/* start_request clears out any readahead data, so it should be safe. */
2578     	if (start_request(ra->addr.lba, ra->nframes, 1)) {
2579     		retval = -EIO;
2580     		goto exit_read_audio;
2581     	}
2582     
2583     	/* For every requested frame. */
2584     	cframe = 0;
2585     	while (cframe < ra->nframes) {
2586     		read_audio_data(readahead_buffer, res_reg, &res_size);
2587     		if ((res_reg[0] & 0xf0) == 0x20) {
2588     			if (res_reg[1] == SONY_BAD_DATA_ERR) {
2589     				printk
2590     				    ("CDU31A: Data error on audio sector %d\n",
2591     				     ra->addr.lba + cframe);
2592     			} else if (res_reg[1] == SONY_ILL_TRACK_R_ERR) {
2593     				/* Illegal track type, change track types and start over. */
2594     				sony_raw_data_mode =
2595     				    (sony_raw_data_mode) ? 0 : 1;
2596     
2597     				/* Set the drive mode. */
2598     				params[0] = SONY_SD_DECODE_PARAM;
2599     				params[1] = 0x06 | sony_raw_data_mode;
2600     				do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
2601     					       params,
2602     					       2, res_reg, &res_size);
2603     				if ((res_size < 2)
2604     				    || ((res_reg[0] & 0xf0) == 0x20)) {
2605     					printk
2606     					    ("CDU31A: Unable to set decode params: 0x%2.2x\n",
2607     					     res_reg[1]);
2608     					retval = -EIO;
2609     					goto exit_read_audio;
2610     				}
2611     
2612     				/* Restart the request on the current frame. */
2613     				if (start_request
2614     				    (ra->addr.lba + cframe,
2615     				     ra->nframes - cframe, 1)) {
2616     					retval = -EIO;
2617     					goto exit_read_audio;
2618     				}
2619     
2620     				/* Don't go back to the top because don't want to get into
2621     				   and infinite loop.  A lot of code gets duplicated, but
2622     				   that's no big deal, I don't guess. */
2623     				read_audio_data(readahead_buffer, res_reg,
2624     						&res_size);
2625     				if ((res_reg[0] & 0xf0) == 0x20) {
2626     					if (res_reg[1] ==
2627     					    SONY_BAD_DATA_ERR) {
2628     						printk
2629     						    ("CDU31A: Data error on audio sector %d\n",
2630     						     ra->addr.lba +
2631     						     cframe);
2632     					} else {
2633     						printk
2634     						    ("CDU31A: Error reading audio data on sector %d: %s\n",
2635     						     ra->addr.lba + cframe,
2636     						     translate_error
2637     						     (res_reg[1]));
2638     						retval = -EIO;
2639     						goto exit_read_audio;
2640     					}
2641     				} else {
2642     					copy_to_user((char *) (ra->buf +
2643     							       (CD_FRAMESIZE_RAW
2644     								* cframe)),
2645     						     (char *)
2646     						     readahead_buffer,
2647     						     CD_FRAMESIZE_RAW);
2648     				}
2649     			} else {
2650     				printk
2651     				    ("CDU31A: Error reading audio data on sector %d: %s\n",
2652     				     ra->addr.lba + cframe,
2653     				     translate_error(res_reg[1]));
2654     				retval = -EIO;
2655     				goto exit_read_audio;
2656     			}
2657     		} else {
2658     			copy_to_user((char *) (ra->buf +
2659     					       (CD_FRAMESIZE_RAW *
2660     						cframe)),
2661     				     (char *) readahead_buffer,
2662     				     CD_FRAMESIZE_RAW);
2663     		}
2664     
2665     		cframe++;
2666     	}
2667     
2668     	get_result(res_reg, &res_size);
2669     	if ((res_reg[0] & 0xf0) == 0x20) {
2670     		printk("CDU31A: Error return from audio read: %s\n",
2671     		       translate_error(res_reg[1]));
2672     		retval = -EIO;
2673     		goto exit_read_audio;
2674     	}
2675     
2676           exit_read_audio:
2677     
2678     	/* Set the drive mode back to the proper one for the disk. */
2679     	params[0] = SONY_SD_DECODE_PARAM;
2680     	if (!sony_xa_mode) {
2681     		params[1] = 0x0f;
2682     	} else {
2683     		params[1] = 0x07;
2684     	}
2685     	do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
2686     		       params, 2, res_reg, &res_size);
2687     	if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20)) {
2688     		printk("CDU31A: Unable to reset decode params: 0x%2.2x\n",
2689     		       res_reg[1]);
2690     		return -EIO;
2691     	}
2692     
2693     	has_cd_task = NULL;
2694     	sony_inuse = 0;
2695     	wake_up_interruptible(&sony_wait);
2696     
2697     	return (retval);
2698     }
2699     
2700     static int
2701     do_sony_cd_cmd_chk(const char *name,
2702     		   unsigned char cmd,
2703     		   unsigned char *params,
2704     		   unsigned int num_params,
2705     		   unsigned char *result_buffer, unsigned int *result_size)
2706     {
2707     	do_sony_cd_cmd(cmd, params, num_params, result_buffer,
2708     		       result_size);
2709     	if ((*result_size < 2) || ((result_buffer[0] & 0xf0) == 0x20)) {
2710     		printk("Sony CDROM error %s (CDROM%s)\n",
2711     		       translate_error(result_buffer[1]), name);
2712     		return -EIO;
2713     	}
2714     	return 0;
2715     }
2716     
2717     /*
2718      * Uniform cdrom interface function
2719      * open the tray
2720      */
2721     static int scd_tray_move(struct cdrom_device_info *cdi, int position)
2722     {
2723     	if (position == 1 /* open tray */ ) {
2724     		unsigned char res_reg[12];
2725     		unsigned int res_size;
2726     
2727     		do_sony_cd_cmd(SONY_AUDIO_STOP_CMD, NULL, 0, res_reg,
2728     			       &res_size);
2729     		do_sony_cd_cmd(SONY_SPIN_DOWN_CMD, NULL, 0, res_reg,
2730     			       &res_size);
2731     
2732     		sony_audio_status = CDROM_AUDIO_INVALID;
2733     		return do_sony_cd_cmd_chk("EJECT", SONY_EJECT_CMD, NULL, 0,
2734     					  res_reg, &res_size);
2735     	} else {
2736     		if (0 == scd_spinup())
2737     			sony_spun_up = 1;
2738     		return 0;
2739     	}
2740     }
2741     
2742     /*
2743      * The big ugly ioctl handler.
2744      */
2745     static int scd_audio_ioctl(struct cdrom_device_info *cdi,
2746     			   unsigned int cmd, void *arg)
2747     {
2748     	unsigned char res_reg[12];
2749     	unsigned int res_size;
2750     	unsigned char params[7];
2751     	int i;
2752     
2753     
2754     	switch (cmd) {
2755     	case CDROMSTART:	/* Spin up the drive */
2756     		return do_sony_cd_cmd_chk("START", SONY_SPIN_UP_CMD, NULL,
2757     					  0, res_reg, &res_size);
2758     		break;
2759     
2760     	case CDROMSTOP:	/* Spin down the drive */
2761     		do_sony_cd_cmd(SONY_AUDIO_STOP_CMD, NULL, 0, res_reg,
2762     			       &res_size);
2763     
2764     		/*
2765     		 * Spin the drive down, ignoring the error if the disk was
2766     		 * already not spinning.
2767     		 */
2768     		sony_audio_status = CDROM_AUDIO_NO_STATUS;
2769     		return do_sony_cd_cmd_chk("STOP", SONY_SPIN_DOWN_CMD, NULL,
2770     					  0, res_reg, &res_size);
2771     
2772     	case CDROMPAUSE:	/* Pause the drive */
2773     		if (do_sony_cd_cmd_chk
2774     		    ("PAUSE", SONY_AUDIO_STOP_CMD, NULL, 0, res_reg,
2775     		     &res_size))
2776     			return -EIO;
2777     		/* Get the current position and save it for resuming */
2778     		if (read_subcode() < 0) {
2779     			return -EIO;
2780     		}
2781     		cur_pos_msf[0] = last_sony_subcode.abs_msf[0];
2782     		cur_pos_msf[1] = last_sony_subcode.abs_msf[1];
2783     		cur_pos_msf[2] = last_sony_subcode.abs_msf[2];
2784     		sony_audio_status = CDROM_AUDIO_PAUSED;
2785     		return 0;
2786     		break;
2787     
2788     	case CDROMRESUME:	/* Start the drive after being paused */
2789     		if (sony_audio_status != CDROM_AUDIO_PAUSED) {
2790     			return -EINVAL;
2791     		}
2792     
2793     		do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg,
2794     			       &res_size);
2795     
2796     		/* Start the drive at the saved position. */
2797     		params[1] = int_to_bcd(cur_pos_msf[0]);
2798     		params[2] = int_to_bcd(cur_pos_msf[1]);
2799     		params[3] = int_to_bcd(cur_pos_msf[2]);
2800     		params[4] = int_to_bcd(final_pos_msf[0]);
2801     		params[5] = int_to_bcd(final_pos_msf[1]);
2802     		params[6] = int_to_bcd(final_pos_msf[2]);
2803     		params[0] = 0x03;
2804     		if (do_sony_cd_cmd_chk
2805     		    ("RESUME", SONY_AUDIO_PLAYBACK_CMD, params, 7, res_reg,
2806     		     &res_size) < 0)
2807     			return -EIO;
2808     		sony_audio_status = CDROM_AUDIO_PLAY;
2809     		return 0;
2810     
2811     	case CDROMPLAYMSF:	/* Play starting at the given MSF address. */
2812     		do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg,
2813     			       &res_size);
2814     
2815     		/* The parameters are given in int, must be converted */
2816     		for (i = 1; i < 7; i++) {
2817     			params[i] =
2818     			    int_to_bcd(((unsigned char *) arg)[i - 1]);
2819     		}
2820     		params[0] = 0x03;
2821     		if (do_sony_cd_cmd_chk
2822     		    ("PLAYMSF", SONY_AUDIO_PLAYBACK_CMD, params, 7,
2823     		     res_reg, &res_size) < 0)
2824     			return -EIO;
2825     
2826     		/* Save the final position for pauses and resumes */
2827     		final_pos_msf[0] = bcd_to_int(params[4]);
2828     		final_pos_msf[1] = bcd_to_int(params[5]);
2829     		final_pos_msf[2] = bcd_to_int(params[6]);
2830     		sony_audio_status = CDROM_AUDIO_PLAY;
2831     		return 0;
2832     
2833     	case CDROMREADTOCHDR:	/* Read the table of contents header */
2834     		{
2835     			struct cdrom_tochdr *hdr;
2836     
2837     			sony_get_toc();
2838     			if (!sony_toc_read) {
2839     				return -EIO;
2840     			}
2841     
2842     			hdr = (struct cdrom_tochdr *) arg;
2843     			hdr->cdth_trk0 = sony_toc.first_track_num;
2844     			hdr->cdth_trk1 = sony_toc.last_track_num;
2845     		}
2846     		return 0;
2847     
2848     	case CDROMREADTOCENTRY:	/* Read a given table of contents entry */
2849     		{
2850     			struct cdrom_tocentry *entry;
2851     			int track_idx;
2852     			unsigned char *msf_val = NULL;
2853     
2854     			sony_get_toc();
2855     			if (!sony_toc_read) {
2856     				return -EIO;
2857     			}
2858     
2859     			entry = (struct cdrom_tocentry *) arg;
2860     
2861     			track_idx = find_track(entry->cdte_track);
2862     			if (track_idx < 0) {
2863     				return -EINVAL;
2864     			}
2865     
2866     			entry->cdte_adr =
2867     			    sony_toc.tracks[track_idx].address;
2868     			entry->cdte_ctrl =
2869     			    sony_toc.tracks[track_idx].control;
2870     			msf_val =
2871     			    sony_toc.tracks[track_idx].track_start_msf;
2872     
2873     			/* Logical buffer address or MSF format requested? */
2874     			if (entry->cdte_format == CDROM_LBA) {
2875     				entry->cdte_addr.lba = msf_to_log(msf_val);
2876     			} else if (entry->cdte_format == CDROM_MSF) {
2877     				entry->cdte_addr.msf.minute = *msf_val;
2878     				entry->cdte_addr.msf.second =
2879     				    *(msf_val + 1);
2880     				entry->cdte_addr.msf.frame =
2881     				    *(msf_val + 2);
2882     			}
2883     		}
2884     		return 0;
2885     		break;
2886     
2887     	case CDROMPLAYTRKIND:	/* Play a track.  This currently ignores index. */
2888     		{
2889     			struct cdrom_ti *ti = (struct cdrom_ti *) arg;
2890     			int track_idx;
2891     
2892     			sony_get_toc();
2893     			if (!sony_toc_read) {
2894     				return -EIO;
2895     			}
2896     
2897     			if ((ti->cdti_trk0 < sony_toc.first_track_num)
2898     			    || (ti->cdti_trk0 > sony_toc.last_track_num)
2899     			    || (ti->cdti_trk1 < ti->cdti_trk0)) {
2900     				return -EINVAL;
2901     			}
2902     
2903     			track_idx = find_track(ti->cdti_trk0);
2904     			if (track_idx < 0) {
2905     				return -EINVAL;
2906     			}
2907     			params[1] =
2908     			    int_to_bcd(sony_toc.tracks[track_idx].
2909     				       track_start_msf[0]);
2910     			params[2] =
2911     			    int_to_bcd(sony_toc.tracks[track_idx].
2912     				       track_start_msf[1]);
2913     			params[3] =
2914     			    int_to_bcd(sony_toc.tracks[track_idx].
2915     				       track_start_msf[2]);
2916     
2917     			/*
2918     			 * If we want to stop after the last track, use the lead-out
2919     			 * MSF to do that.
2920     			 */
2921     			if (ti->cdti_trk1 >= sony_toc.last_track_num) {
2922     				track_idx = find_track(CDROM_LEADOUT);
2923     			} else {
2924     				track_idx = find_track(ti->cdti_trk1 + 1);
2925     			}
2926     			if (track_idx < 0) {
2927     				return -EINVAL;
2928     			}
2929     			params[4] =
2930     			    int_to_bcd(sony_toc.tracks[track_idx].
2931     				       track_start_msf[0]);
2932     			params[5] =
2933     			    int_to_bcd(sony_toc.tracks[track_idx].
2934     				       track_start_msf[1]);
2935     			params[6] =
2936     			    int_to_bcd(sony_toc.tracks[track_idx].
2937     				       track_start_msf[2]);
2938     			params[0] = 0x03;
2939     
2940     			do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg,
2941     				       &res_size);
2942     
2943     			do_sony_cd_cmd(SONY_AUDIO_PLAYBACK_CMD, params, 7,
2944     				       res_reg, &res_size);
2945     
2946     			if ((res_size < 2)
2947     			    || ((res_reg[0] & 0xf0) == 0x20)) {
2948     				printk("Params: %x %x %x %x %x %x %x\n",
2949     				       params[0], params[1], params[2],
2950     				       params[3], params[4], params[5],
2951     				       params[6]);
2952     				printk
2953     				    ("Sony CDROM error %s (CDROMPLAYTRKIND)\n",
2954     				     translate_error(res_reg[1]));
2955     				return -EIO;
2956     			}
2957     
2958     			/* Save the final position for pauses and resumes */
2959     			final_pos_msf[0] = bcd_to_int(params[4]);
2960     			final_pos_msf[1] = bcd_to_int(params[5]);
2961     			final_pos_msf[2] = bcd_to_int(params[6]);
2962     			sony_audio_status = CDROM_AUDIO_PLAY;
2963     			return 0;
2964     		}
2965     
2966     	case CDROMVOLCTRL:	/* Volume control.  What volume does this change, anyway? */
2967     		{
2968     			struct cdrom_volctrl *volctrl =
2969     			    (struct cdrom_volctrl *) arg;
2970     
2971     			params[0] = SONY_SD_AUDIO_VOLUME;
2972     			params[1] = volctrl->channel0;
2973     			params[2] = volctrl->channel1;
2974     			return do_sony_cd_cmd_chk("VOLCTRL",
2975     						  SONY_SET_DRIVE_PARAM_CMD,
2976     						  params, 3, res_reg,
2977     						  &res_size);
2978     		}
2979     	case CDROMSUBCHNL:	/* Get subchannel info */
2980     		return sony_get_subchnl_info((struct cdrom_subchnl *) arg);
2981     
2982     	default:
2983     		return -EINVAL;
2984     	}
2985     }
2986     
2987     static int scd_dev_ioctl(struct cdrom_device_info *cdi,
2988     			 unsigned int cmd, unsigned long arg)
2989     {
2990     	int i;
2991     
2992     	switch (cmd) {
2993     	case CDROMREADAUDIO:	/* Read 2352 byte audio tracks and 2340 byte
2994     				   raw data tracks. */
2995     		{
2996     			struct cdrom_read_audio ra;
2997     
2998     
2999     			sony_get_toc();
3000     			if (!sony_toc_read) {
3001     				return -EIO;
3002     			}
3003     
3004     			if (copy_from_user(&ra, (char *) arg, sizeof(ra)))
3005     				return -EFAULT;
3006     
3007     			if (ra.nframes == 0) {
3008     				return 0;
3009     			}
3010     
3011     			i = verify_area(VERIFY_WRITE, ra.buf,
3012     					CD_FRAMESIZE_RAW * ra.nframes);
3013     			if (i < 0)
3014     				return i;
3015     
3016     			if (ra.addr_format == CDROM_LBA) {
3017     				if ((ra.addr.lba >=
3018     				     sony_toc.lead_out_start_lba)
3019     				    || (ra.addr.lba + ra.nframes >=
3020     					sony_toc.lead_out_start_lba)) {
3021     					return -EINVAL;
3022     				}
3023     			} else if (ra.addr_format == CDROM_MSF) {
3024     				if ((ra.addr.msf.minute >= 75)
3025     				    || (ra.addr.msf.second >= 60)
3026     				    || (ra.addr.msf.frame >= 75)) {
3027     					return -EINVAL;
3028     				}
3029     
3030     				ra.addr.lba = ((ra.addr.msf.minute * 4500)
3031     					       + (ra.addr.msf.second * 75)
3032     					       + ra.addr.msf.frame);
3033     				if ((ra.addr.lba >=
3034     				     sony_toc.lead_out_start_lba)
3035     				    || (ra.addr.lba + ra.nframes >=
3036     					sony_toc.lead_out_start_lba)) {
3037     					return -EINVAL;
3038     				}
3039     
3040     				/* I know, this can go negative on an unsigned.  However,
3041     				   the first thing done to the data is to add this value,
3042     				   so this should compensate and allow direct msf access. */
3043     				ra.addr.lba -= LOG_START_OFFSET;
3044     			} else {
3045     				return -EINVAL;
3046     			}
3047     
3048     			return (read_audio(&ra));
3049     		}
3050     		return 0;
3051     		break;
3052     
3053     	default:
3054     		return -EINVAL;
3055     	}
3056     }
3057     
3058     static int scd_spinup(void)
3059     {
3060     	unsigned char res_reg[12];
3061     	unsigned int res_size;
3062     	int num_spin_ups;
3063     
3064     	num_spin_ups = 0;
3065     
3066           respinup_on_open:
3067     	do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg, &res_size);
3068     
3069     	/* The drive sometimes returns error 0.  I don't know why, but ignore
3070     	   it.  It seems to mean the drive has already done the operation. */
3071     	if ((res_size < 2) || ((res_reg[0] != 0) && (res_reg[1] != 0))) {
3072     		printk("Sony CDROM %s error (scd_open, spin up)\n",
3073     		       translate_error(res_reg[1]));
3074     		return 1;
3075     	}
3076     
3077     	do_sony_cd_cmd(SONY_READ_TOC_CMD, NULL, 0, res_reg, &res_size);
3078     
3079     	/* The drive sometimes returns error 0.  I don't know why, but ignore
3080     	   it.  It seems to mean the drive has already done the operation. */
3081     	if ((res_size < 2) || ((res_reg[0] != 0) && (res_reg[1] != 0))) {
3082     		/* If the drive is already playing, it's ok.  */
3083     		if ((res_reg[1] == SONY_AUDIO_PLAYING_ERR)
3084     		    || (res_reg[1] == 0)) {
3085     			return 0;
3086     		}
3087     
3088     		/* If the drive says it is not spun up (even though we just did it!)
3089     		   then retry the operation at least a few times. */
3090     		if ((res_reg[1] == SONY_NOT_SPIN_ERR)
3091     		    && (num_spin_ups < MAX_CDU31A_RETRIES)) {
3092     			num_spin_ups++;
3093     			goto respinup_on_open;
3094     		}
3095     
3096     		printk("Sony CDROM error %s (scd_open, read toc)\n",
3097     		       translate_error(res_reg[1]));
3098     		do_sony_cd_cmd(SONY_SPIN_DOWN_CMD, NULL, 0, res_reg,
3099     			       &res_size);
3100     		return 1;
3101     	}
3102     	return 0;
3103     }
3104     
3105     /*
3106      * Open the drive for operations.  Spin the drive up and read the table of
3107      * contents if these have not already been done.
3108      */
3109     static int scd_open(struct cdrom_device_info *cdi, int openmode)
3110     {
3111     	unsigned char res_reg[12];
3112     	unsigned int res_size;
3113     	unsigned char params[2];
3114     
3115     	MOD_INC_USE_COUNT;
3116     	if (sony_usage == 0) {
3117     		if (scd_spinup() != 0) {
3118     			MOD_DEC_USE_COUNT;
3119     			return -EIO;
3120     		}
3121     		sony_get_toc();
3122     		if (!sony_toc_read) {
3123     			do_sony_cd_cmd(SONY_SPIN_DOWN_CMD, NULL, 0,
3124     				       res_reg, &res_size);
3125     			MOD_DEC_USE_COUNT;
3126     			return -EIO;
3127     		}
3128     
3129     		/* For XA on the CDU31A only, we have to do special reads.
3130     		   The CDU33A handles XA automagically. */
3131     		/* if (   (sony_toc.disk_type == SONY_XA_DISK_TYPE) */
3132     		if ((sony_toc.disk_type != 0x00)
3133     		    && (!is_double_speed)) {
3134     			params[0] = SONY_SD_DECODE_PARAM;
3135     			params[1] = 0x07;
3136     			do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
3137     				       params, 2, res_reg, &res_size);
3138     			if ((res_size < 2)
3139     			    || ((res_reg[0] & 0xf0) == 0x20)) {
3140     				printk
3141     				    ("CDU31A: Unable to set XA params: 0x%2.2x\n",
3142     				     res_reg[1]);
3143     			}
3144     			sony_xa_mode = 1;
3145     		}
3146     		/* A non-XA disk.  Set the parms back if necessary. */
3147     		else if (sony_xa_mode) {
3148     			params[0] = SONY_SD_DECODE_PARAM;
3149     			params[1] = 0x0f;
3150     			do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
3151     				       params, 2, res_reg, &res_size);
3152     			if ((res_size < 2)
3153     			    || ((res_reg[0] & 0xf0) == 0x20)) {
3154     				printk
3155     				    ("CDU31A: Unable to reset XA params: 0x%2.2x\n",
3156     				     res_reg[1]);
3157     			}
3158     			sony_xa_mode = 0;
3159     		}
3160     
3161     		sony_spun_up = 1;
3162     	}
3163     
3164     	sony_usage++;
3165     
3166     	return 0;
3167     }
3168     
3169     
3170     /*
3171      * Close the drive.  Spin it down if no task is using it.  The spin
3172      * down will fail if playing audio, so audio play is OK.
3173      */
3174     static void scd_release(struct cdrom_device_info *cdi)
3175     {
3176     	if (sony_usage == 1) {
3177     		unsigned char res_reg[12];
3178     		unsigned int res_size;
3179     
3180     		do_sony_cd_cmd(SONY_SPIN_DOWN_CMD, NULL, 0, res_reg,
3181     			       &res_size);
3182     
3183     		sony_spun_up = 0;
3184     	}
3185     	sony_usage--;
3186     	MOD_DEC_USE_COUNT;
3187     }
3188     
3189     static struct cdrom_device_ops scd_dops = {
3190     	open:scd_open,
3191     	release:scd_release,
3192     	drive_status:scd_drive_status,
3193     	media_changed:scd_media_changed,
3194     	tray_move:scd_tray_move,
3195     	lock_door:scd_lock_door,
3196     	select_speed:scd_select_speed,
3197     	get_last_session:scd_get_last_session,
3198     	get_mcn:scd_get_mcn,
3199     	reset:scd_reset,
3200     	audio_ioctl:scd_audio_ioctl,
3201     	dev_ioctl:scd_dev_ioctl,
3202     	capability:CDC_OPEN_TRAY | CDC_CLOSE_TRAY | CDC_LOCK |
3203     	    CDC_SELECT_SPEED | CDC_MULTI_SESSION |
3204     	    CDC_MULTI_SESSION | CDC_MCN |
3205     	    CDC_MEDIA_CHANGED | CDC_PLAY_AUDIO |
3206     	    CDC_RESET | CDC_IOCTLS | CDC_DRIVE_STATUS,
3207     	n_minors:1,
3208     };
3209     
3210     static struct cdrom_device_info scd_info = {
3211     	ops:&scd_dops,
3212     	speed:2,
3213     	capacity:1,
3214     	name:"cdu31a"
3215     };
3216     
3217     /* The different types of disc loading mechanisms supported */
3218     static char *load_mech[] __initdata =
3219         { "caddy", "tray", "pop-up", "unknown" };
3220     
3221     static void __init
3222     get_drive_configuration(unsigned short base_io,
3223     			unsigned char res_reg[], unsigned int *res_size)
3224     {
3225     	int retry_count;
3226     
3227     
3228     	/* Set the base address */
3229     	cdu31a_port = base_io;
3230     
3231     	/* Set up all the register locations */
3232     	sony_cd_cmd_reg = cdu31a_port + SONY_CMD_REG_OFFSET;
3233     	sony_cd_param_reg = cdu31a_port + SONY_PARAM_REG_OFFSET;
3234     	sony_cd_write_reg = cdu31a_port + SONY_WRITE_REG_OFFSET;
3235     	sony_cd_control_reg = cdu31a_port + SONY_CONTROL_REG_OFFSET;
3236     	sony_cd_status_reg = cdu31a_port + SONY_STATUS_REG_OFFSET;
3237     	sony_cd_result_reg = cdu31a_port + SONY_RESULT_REG_OFFSET;
3238     	sony_cd_read_reg = cdu31a_port + SONY_READ_REG_OFFSET;
3239     	sony_cd_fifost_reg = cdu31a_port + SONY_FIFOST_REG_OFFSET;
3240     
3241     	/*
3242     	 * Check to see if anything exists at the status register location.
3243     	 * I don't know if this is a good way to check, but it seems to work
3244     	 * ok for me.
3245     	 */
3246     	if (read_status_register() != 0xff) {
3247     		/*
3248     		 * Reset the drive and wait for attention from it (to say it's reset).
3249     		 * If you don't wait, the next operation will probably fail.
3250     		 */
3251     		reset_drive();
3252     		retry_count = jiffies + SONY_RESET_TIMEOUT;
3253     		while (time_before(jiffies, retry_count)
3254     		       && (!is_attention())) {
3255     			sony_sleep();
3256     		}
3257     
3258     #if 0
3259     		/* If attention is never seen probably not a CDU31a present */
3260     		if (!is_attention()) {
3261     			res_reg[0] = 0x20;
3262     			return;
3263     		}
3264     #endif
3265     
3266     		/*
3267     		 * Get the drive configuration.
3268     		 */
3269     		do_sony_cd_cmd(SONY_REQ_DRIVE_CONFIG_CMD,
3270     			       NULL,
3271     			       0, (unsigned char *) res_reg, res_size);
3272     		return;
3273     	}
3274     
3275     	/* Return an error */
3276     	res_reg[0] = 0x20;
3277     }
3278     
3279     #ifndef MODULE
3280     /*
3281      * Set up base I/O and interrupts, called from main.c.
3282      
3283      */
3284     
3285     static int __init cdu31a_setup(char *strings)
3286     {
3287     	int ints[4];
3288     
3289     	(void) get_options(strings, ARRAY_SIZE(ints), ints);
3290     
3291     	if (ints[0] > 0) {
3292     		cdu31a_port = ints[1];
3293     	}
3294     	if (ints[0] > 1) {
3295     		cdu31a_irq = ints[2];
3296     	}
3297     	if ((strings != NULL) && (*strings != '\0')) {
3298     		if (strcmp(strings, "PAS") == 0) {
3299     			sony_pas_init = 1;
3300     		} else {
3301     			printk("CDU31A: Unknown interface type: %s\n",
3302     			       strings);
3303     		}
3304     	}
3305     
3306     	return 1;
3307     }
3308     
3309     __setup("cdu31a=", cdu31a_setup);
3310     
3311     #endif
3312     
3313     static int cdu31a_block_size;
3314     
3315     /*
3316      * Initialize the driver.
3317      */
3318     int __init cdu31a_init(void)
3319     {
3320     	struct s_sony_drive_config drive_config;
3321     	unsigned int res_size;
3322     	char msg[255];
3323     	char buf[40];
3324     	int i;
3325     	int drive_found;
3326     	int tmp_irq;
3327     
3328     
3329     	/*
3330     	 * According to Alex Freed (freed@europa.orion.adobe.com), this is
3331     	 * required for the Fusion CD-16 package.  If the sound driver is
3332     	 * loaded, it should work fine, but just in case...
3333     	 *
3334     	 * The following turn on the CD-ROM interface for a Fusion CD-16.
3335     	 */
3336     	if (sony_pas_init) {
3337     		outb(0xbc, 0x9a01);
3338     		outb(0xe2, 0x9a01);
3339     	}
3340     
3341     	drive_found = 0;
3342     
3343     	/* Setting the base I/O address to 0xffff will disable it. */
3344     	if (cdu31a_port == 0xffff) {
3345     	} else if (cdu31a_port != 0) {
3346     		tmp_irq = cdu31a_irq;	/* Need IRQ 0 because we can't sleep here. */
3347     		cdu31a_irq = 0;
3348     
3349     		get_drive_configuration(cdu31a_port,
3350     					drive_config.exec_status,
3351     					&res_size);
3352     		if ((res_size > 2)
3353     		    && ((drive_config.exec_status[0] & 0xf0) == 0x00)) {
3354     			drive_found = 1;
3355     		}
3356     
3357     		cdu31a_irq = tmp_irq;
3358     	} else {
3359     		cdu31a_irq = 0;
3360     		i = 0;
3361     		while ((cdu31a_addresses[i].base != 0)
3362     		       && (!drive_found)) {
3363     			if (check_region(cdu31a_addresses[i].base, 4)) {
3364     				i++;
3365     				continue;
3366     			}
3367     			get_drive_configuration(cdu31a_addresses[i].base,
3368     						drive_config.exec_status,
3369     						&res_size);
3370     			if ((res_size > 2)
3371     			    && ((drive_config.exec_status[0] & 0xf0) ==
3372     				0x00)) {
3373     				drive_found = 1;
3374     				cdu31a_irq = cdu31a_addresses[i].int_num;
3375     			} else {
3376     				i++;
3377     			}
3378     		}
3379     	}
3380     
3381     	if (drive_found) {
3382     		int deficiency = 0;
3383     
3384     		request_region(cdu31a_port, 4, "cdu31a");
3385     
3386     		if (devfs_register_blkdev(MAJOR_NR, "cdu31a", &cdrom_fops)) {
3387     			printk("Unable to get major %d for CDU-31a\n",
3388     			       MAJOR_NR);
3389     			goto errout2;
3390     		}
3391     
3392     		if (SONY_HWC_DOUBLE_SPEED(drive_config)) {
3393     			is_double_speed = 1;
3394     		}
3395     
3396     		tmp_irq = cdu31a_irq;	/* Need IRQ 0 because we can't sleep here. */
3397     		cdu31a_irq = 0;
3398     
3399     		set_drive_params(sony_speed);
3400     
3401     		cdu31a_irq = tmp_irq;
3402     
3403     		if (cdu31a_irq > 0) {
3404     			if (request_irq
3405     			    (cdu31a_irq, cdu31a_interrupt, SA_INTERRUPT,
3406     			     "cdu31a", NULL)) {
3407     				printk
3408     				    ("Unable to grab IRQ%d for the CDU31A driver\n",
3409     				     cdu31a_irq);
3410     				cdu31a_irq = 0;
3411     			}
3412     		}
3413     
3414     		sprintf(msg, "Sony I/F CDROM : %8.8s %16.16s %8.8s\n",
3415     			drive_config.vendor_id,
3416     			drive_config.product_id,
3417     			drive_config.product_rev_level);
3418     		sprintf(buf, "  Capabilities: %s",
3419     			load_mech[SONY_HWC_GET_LOAD_MECH(drive_config)]);
3420     		strcat(msg, buf);
3421     		if (SONY_HWC_AUDIO_PLAYBACK(drive_config)) {
3422     			strcat(msg, ", audio");
3423     		} else
3424     			deficiency |= CDC_PLAY_AUDIO;
3425     		if (SONY_HWC_EJECT(drive_config)) {
3426     			strcat(msg, ", eject");
3427     		} else
3428     			deficiency |= CDC_OPEN_TRAY;
3429     		if (SONY_HWC_LED_SUPPORT(drive_config)) {
3430     			strcat(msg, ", LED");
3431     		}
3432     		if (SONY_HWC_ELECTRIC_VOLUME(drive_config)) {
3433     			strcat(msg, ", elec. Vol");
3434     		}
3435     		if (SONY_HWC_ELECTRIC_VOLUME_CTL(drive_config)) {
3436     			strcat(msg, ", sep. Vol");
3437     		}
3438     		if (is_double_speed) {
3439     			strcat(msg, ", double speed");
3440     		} else
3441     			deficiency |= CDC_SELECT_SPEED;
3442     		if (cdu31a_irq > 0) {
3443     			sprintf(buf, ", irq %d", cdu31a_irq);
3444     			strcat(msg, buf);
3445     		}
3446     		strcat(msg, "\n");
3447     
3448     		is_a_cdu31a =
3449     		    strcmp("CD-ROM CDU31A", drive_config.product_id) == 0;
3450     
3451     		blk_init_queue(BLK_DEFAULT_QUEUE(MAJOR_NR),
3452     			       DEVICE_REQUEST);
3453     		read_ahead[MAJOR_NR] = CDU31A_READAHEAD;
3454     		cdu31a_block_size = 1024;	/* 1kB default block size */
3455     		/* use 'mount -o block=2048' */
3456     		blksize_size[MAJOR_NR] = &cdu31a_block_size;
3457     
3458     		init_timer(&cdu31a_abort_timer);
3459     		cdu31a_abort_timer.function = handle_abort_timeout;
3460     
3461     		scd_info.dev = MKDEV(MAJOR_NR, 0);
3462     		scd_info.mask = deficiency;
3463     		strncpy(scd_info.name, "cdu31a", sizeof(scd_info.name));
3464     
3465     		if (register_cdrom(&scd_info)) {
3466     			goto errout0;
3467     		}
3468     	}
3469     
3470     
3471     	disk_changed = 1;
3472     
3473     	if (drive_found) {
3474     		return (0);
3475     	} else {
3476     		goto errout3;
3477     	}
3478           errout0:
3479     	printk("Unable to register CDU-31a with Uniform cdrom driver\n");
3480     	blk_cleanup_queue(BLK_DEFAULT_QUEUE(MAJOR_NR));
3481     	if (devfs_unregister_blkdev(MAJOR_NR, "cdu31a")) {
3482     		printk("Can't unregister block device for cdu31a\n");
3483     	}
3484           errout2:
3485     	release_region(cdu31a_port, 4);
3486           errout3:
3487     	return -EIO;
3488     }
3489     
3490     
3491     void __exit cdu31a_exit(void)
3492     {
3493     	if (unregister_cdrom(&scd_info)) {
3494     		printk
3495     		    ("Can't unregister cdu31a from Uniform cdrom driver\n");
3496     		return;
3497     	}
3498     	if ((devfs_unregister_blkdev(MAJOR_NR, "cdu31a") == -EINVAL)) {
3499     		printk("Can't unregister cdu31a\n");
3500     		return;
3501     	}
3502     
3503     	blk_cleanup_queue(BLK_DEFAULT_QUEUE(MAJOR_NR));
3504     
3505     	if (cdu31a_irq > 0)
3506     		free_irq(cdu31a_irq, NULL);
3507     
3508     	release_region(cdu31a_port, 4);
3509     	printk(KERN_INFO "cdu31a module released.\n");
3510     }
3511     
3512     #ifdef MODULE
3513     module_init(cdu31a_init);
3514     #endif
3515     module_exit(cdu31a_exit);
3516     
3517     MODULE_LICENSE("GPL");
3518     EXPORT_NO_SYMBOLS;
3519