Revert last change. Bug noticed by Linus.
[linux-2.6/linux-mips.git] / drivers / cdrom / cdu31a.c
blob27405b3c3873e8db02c00f746c6793acd22d96b7
1 /*
2 * Sony CDU-31A CDROM interface device driver.
4 * Corey Minyard (minyard@wf-rch.cirr.com)
6 * Colossians 3:17
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.
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:
21 * append="cdu31a=0x1f88,0,PAS"
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.
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:
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 * ;
40 * sony_sleep();
41 * }
42 * if (the condition not met)
43 * {
44 * return an error;
45 * }
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.
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.
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.
62 * DRIVER SPECIAL FEATURES
63 * -----------------------
65 * This section describes features beyond the normal audio and CD-ROM
66 * functions of the drive.
68 * 2048 byte buffer mode
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.
76 * XA compatibility
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.
81 * Multi-Session
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!
87 * Raw sector I/O
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.
95 * Copyright (C) 1993 Corey Minyard
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.
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.
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.
111 * TODO:
112 * CDs with form1 and form2 sectors cause problems
113 * with current read-ahead strategy.
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.
121 * It probably a little late to be adding a history, but I guess I
122 * will start.
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.
130 * 03/07/97 - Fixed a problem with timers.
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>
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>
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.
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>
152 #include <linux/major.h>
154 #include <linux/module.h>
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/malloc.h>
168 #include <linux/init.h>
169 #include <linux/interrupt.h>
171 #include <asm/system.h>
172 #include <asm/io.h>
173 #include <asm/uaccess.h>
174 #include <asm/dma.h>
176 #include <linux/cdrom.h>
177 #include "cdu31a.h"
179 #define MAJOR_NR CDU31A_CDROM_MAJOR
180 #include <linux/blk.h>
182 #define CDU31A_READAHEAD 4 /* 128 sector, 64kB, 32 reads read-ahead */
183 #define CDU31A_MAX_CONSECUTIVE_ATTENTIONS 10
185 #define DEBUG 0
187 /* Define the following if you have data corruption problems. */
188 #undef SONY_POLL_EACH_BYTE
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.
195 static struct
197 unsigned short base; /* I/O Base Address */
198 short int_num; /* Interrupt Number (-1 means scan for it,
199 0 means don't use) */
200 } cdu31a_addresses[] __initdata =
202 #if 0 /* No autoconfig any more. See Note at beginning
203 of this file. */
204 { 0x340, 0 }, /* Standard configuration Sony Interface */
205 { 0x1f88, 0 }, /* Fusion CD-16 */
206 { 0x230, 0 }, /* SoundBlaster 16 card */
207 { 0x360, 0 }, /* Secondary standard Sony Interface */
208 { 0x320, 0 }, /* Secondary standard Sony Interface */
209 { 0x330, 0 }, /* Secondary standard Sony Interface */
210 { 0x634, 0 }, /* Sound FX SC400 */
211 { 0x654, 0 }, /* Sound FX SC400 */
212 #endif
213 { 0 }
216 static int handle_sony_cd_attention(void);
217 static int read_subcode(void);
218 static void sony_get_toc(void);
219 static int scd_spinup(void);
220 /*static int scd_open(struct inode *inode, struct file *filp);*/
221 static int scd_open(struct cdrom_device_info *, int);
222 static void do_sony_cd_cmd(unsigned char cmd,
223 unsigned char *params,
224 unsigned int num_params,
225 unsigned char *result_buffer,
226 unsigned int *result_size);
227 static void size_to_buf(unsigned int size,
228 unsigned char *buf);
230 /* Parameters for the read-ahead. */
231 static unsigned int sony_next_block; /* Next 512 byte block offset */
232 static unsigned int sony_blocks_left = 0; /* Number of 512 byte blocks left
233 in the current read command. */
236 /* The base I/O address of the Sony Interface. This is a variable (not a
237 #define) so it can be easily changed via some future ioctl() */
238 static unsigned int cdu31a_port = 0;
239 MODULE_PARM(cdu31a_port, "i");
242 * The following are I/O addresses of the various registers for the drive. The
243 * comment for the base address also applies here.
245 static volatile unsigned short sony_cd_cmd_reg;
246 static volatile unsigned short sony_cd_param_reg;
247 static volatile unsigned short sony_cd_write_reg;
248 static volatile unsigned short sony_cd_control_reg;
249 static volatile unsigned short sony_cd_status_reg;
250 static volatile unsigned short sony_cd_result_reg;
251 static volatile unsigned short sony_cd_read_reg;
252 static volatile unsigned short sony_cd_fifost_reg;
255 static int sony_spun_up = 0; /* Has the drive been spun up? */
257 static int sony_speed = 0; /* Last wanted speed */
259 static int sony_xa_mode = 0; /* Is an XA disk in the drive
260 and the drive a CDU31A? */
262 static int sony_raw_data_mode = 1; /* 1 if data tracks, 0 if audio.
263 For raw data reads. */
265 static unsigned int sony_usage = 0; /* How many processes have the
266 drive open. */
268 static int sony_pas_init = 0; /* Initialize the Pro-Audio
269 Spectrum card? */
271 static struct s_sony_session_toc single_toc; /* Holds the
272 table of
273 contents. */
275 static struct s_all_sessions_toc sony_toc; /* entries gathered from all
276 sessions */
278 static int sony_toc_read = 0; /* Has the TOC been read for
279 the drive? */
281 static struct s_sony_subcode last_sony_subcode; /* Points to the last
282 subcode address read */
284 static volatile int sony_inuse = 0; /* Is the drive in use? Only one operation
285 at a time allowed */
287 static DECLARE_WAIT_QUEUE_HEAD(sony_wait); /* Things waiting for the drive */
289 static struct task_struct *has_cd_task = NULL; /* The task that is currently
290 using the CDROM drive, or
291 NULL if none. */
293 static int is_double_speed = 0; /* does the drive support double speed ? */
294 static int is_a_cdu31a = 1; /* Is the drive a CDU31A? */
296 static int is_auto_eject = 1; /* Door has been locked? 1=No/0=Yes */
299 * The audio status uses the values from read subchannel data as specified
300 * in include/linux/cdrom.h.
302 static volatile int sony_audio_status = CDROM_AUDIO_NO_STATUS;
305 * The following are a hack for pausing and resuming audio play. The drive
306 * does not work as I would expect it, if you stop it then start it again,
307 * the drive seeks back to the beginning and starts over. This holds the
308 * position during a pause so a resume can restart it. It uses the
309 * audio status variable above to tell if it is paused.
311 static unsigned volatile char cur_pos_msf[3] = { 0, 0, 0 };
312 static unsigned volatile char final_pos_msf[3] = { 0, 0, 0 };
314 /* What IRQ is the drive using? 0 if none. */
315 static int cdu31a_irq = 0;
316 MODULE_PARM(cdu31a_irq, "i");
318 /* The interrupt handler will wake this queue up when it gets an
319 interrupts. */
320 DECLARE_WAIT_QUEUE_HEAD(cdu31a_irq_wait);
322 static int curr_control_reg = 0; /* Current value of the control register */
324 /* A disk changed variable. When a disk change is detected, it will
325 all be set to TRUE. As the upper layers ask for disk_changed status
326 it will be cleared. */
327 static char disk_changed;
329 /* Variable for using the readahead buffer. The readahead buffer
330 is used for raw sector reads and for blocksizes that are smaller
331 than 2048 bytes. */
332 static char readahead_buffer[CD_FRAMESIZE_RAW];
333 static int readahead_dataleft = 0;
334 static int readahead_bad = 0;
336 /* Used to time a short period to abort an operation after the
337 drive has been idle for a while. This keeps the light on
338 the drive from flashing for very long. */
339 static struct timer_list cdu31a_abort_timer;
341 /* Marks if the timeout has started an abort read. This is used
342 on entry to the drive to tell the code to read out the status
343 from the abort read. */
344 static int abort_read_started = 0;
348 * This routine returns 1 if the disk has been changed since the last
349 * check or 0 if it hasn't.
351 static int
352 scd_disk_change(kdev_t full_dev)
354 int retval;
356 retval = disk_changed;
357 disk_changed = 0;
359 return retval;
363 * Uniform cdrom interface function
364 * report back, if disc has changed from time of last request.
366 static int
367 scd_media_changed(struct cdrom_device_info *cdi, int disc_nr)
369 return scd_disk_change(cdi->dev);
373 * Uniform cdrom interface function
374 * report back, if drive is ready
376 static int scd_drive_status(struct cdrom_device_info *cdi, int slot_nr)
378 if (CDSL_CURRENT != slot_nr) {
379 /* we have no changer support */
380 return -EINVAL;
382 if (scd_spinup() == 0) {
383 sony_spun_up = 1;
385 return sony_spun_up ? CDS_DISC_OK : CDS_DRIVE_NOT_READY;
388 static inline void
389 enable_interrupts(void)
391 curr_control_reg |= ( SONY_ATTN_INT_EN_BIT
392 | SONY_RES_RDY_INT_EN_BIT
393 | SONY_DATA_RDY_INT_EN_BIT);
394 outb(curr_control_reg, sony_cd_control_reg);
397 static inline void
398 disable_interrupts(void)
400 curr_control_reg &= ~( SONY_ATTN_INT_EN_BIT
401 | SONY_RES_RDY_INT_EN_BIT
402 | SONY_DATA_RDY_INT_EN_BIT);
403 outb(curr_control_reg, sony_cd_control_reg);
407 * Wait a little while (used for polling the drive). If in initialization,
408 * setting a timeout doesn't work, so just loop for a while.
410 static inline void
411 sony_sleep(void)
413 unsigned long flags;
415 if (cdu31a_irq <= 0)
417 current->state = TASK_INTERRUPTIBLE;
418 schedule_timeout(0);
420 else /* Interrupt driven */
422 save_flags(flags);
423 cli();
424 enable_interrupts();
425 interruptible_sleep_on(&cdu31a_irq_wait);
426 restore_flags(flags);
432 * The following are convenience routine to read various status and set
433 * various conditions in the drive.
435 static inline int
436 is_attention(void)
438 return((inb(sony_cd_status_reg) & SONY_ATTN_BIT) != 0);
441 static inline int
442 is_busy(void)
444 return((inb(sony_cd_status_reg) & SONY_BUSY_BIT) != 0);
447 static inline int
448 is_data_ready(void)
450 return((inb(sony_cd_status_reg) & SONY_DATA_RDY_BIT) != 0);
453 static inline int
454 is_data_requested(void)
456 return((inb(sony_cd_status_reg) & SONY_DATA_REQUEST_BIT) != 0);
459 static inline int
460 is_result_ready(void)
462 return((inb(sony_cd_status_reg) & SONY_RES_RDY_BIT) != 0);
465 static inline int
466 is_param_write_rdy(void)
468 return((inb(sony_cd_fifost_reg) & SONY_PARAM_WRITE_RDY_BIT) != 0);
471 static inline int
472 is_result_reg_not_empty(void)
474 return((inb(sony_cd_fifost_reg) & SONY_RES_REG_NOT_EMP_BIT) != 0);
477 static inline void
478 reset_drive(void)
480 curr_control_reg = 0;
481 readahead_dataleft = 0;
482 sony_toc_read = 0;
483 outb(SONY_DRIVE_RESET_BIT, sony_cd_control_reg);
487 * Uniform cdrom interface function
488 * reset drive and return when it is ready
490 static int scd_reset(struct cdrom_device_info * cdi)
492 int retry_count;
494 reset_drive();
496 retry_count = jiffies + SONY_RESET_TIMEOUT;
497 while (time_before(jiffies, retry_count) && (!is_attention()))
499 sony_sleep();
502 return 0;
505 static inline void
506 clear_attention(void)
508 outb(curr_control_reg | SONY_ATTN_CLR_BIT, sony_cd_control_reg);
511 static inline void
512 clear_result_ready(void)
514 outb(curr_control_reg | SONY_RES_RDY_CLR_BIT, sony_cd_control_reg);
517 static inline void
518 clear_data_ready(void)
520 outb(curr_control_reg | SONY_DATA_RDY_CLR_BIT, sony_cd_control_reg);
523 static inline void
524 clear_param_reg(void)
526 outb(curr_control_reg | SONY_PARAM_CLR_BIT, sony_cd_control_reg);
529 static inline unsigned char
530 read_status_register(void)
532 return(inb(sony_cd_status_reg));
535 static inline unsigned char
536 read_result_register(void)
538 return(inb(sony_cd_result_reg));
541 static inline unsigned char
542 read_data_register(void)
544 return(inb(sony_cd_read_reg));
547 static inline void
548 write_param(unsigned char param)
550 outb(param, sony_cd_param_reg);
553 static inline void
554 write_cmd(unsigned char cmd)
556 outb(curr_control_reg | SONY_RES_RDY_INT_EN_BIT, sony_cd_control_reg);
557 outb(cmd, sony_cd_cmd_reg);
560 static void
561 cdu31a_interrupt(int irq, void *dev_id, struct pt_regs *regs)
563 unsigned char val;
565 if (abort_read_started)
567 /* We might be waiting for an abort to finish. Don't
568 disable interrupts yet, though, because we handle
569 this one here. */
570 /* Clear out the result registers. */
571 while (is_result_reg_not_empty())
573 val = read_result_register();
575 clear_data_ready();
576 clear_result_ready();
578 /* Clear out the data */
579 while (is_data_requested())
581 val = read_data_register();
583 abort_read_started = 0;
585 /* If something was waiting, wake it up now. */
586 if (waitqueue_active(&cdu31a_irq_wait))
588 disable_interrupts();
589 wake_up(&cdu31a_irq_wait);
592 else if (waitqueue_active(&cdu31a_irq_wait))
594 disable_interrupts();
595 wake_up(&cdu31a_irq_wait);
597 else
599 disable_interrupts();
600 printk("CDU31A: Got an interrupt but nothing was waiting\n");
605 * give more verbose error messages
607 static unsigned char *translate_error( unsigned char err_code )
609 static unsigned char errbuf[80];
611 switch (err_code) {
612 case 0x10: return "illegal command ";
613 case 0x11: return "illegal parameter ";
615 case 0x20: return "not loaded ";
616 case 0x21: return "no disc ";
617 case 0x22: return "not spinning ";
618 case 0x23: return "spinning ";
619 case 0x25: return "spindle servo ";
620 case 0x26: return "focus servo ";
621 case 0x29: return "eject mechanism ";
622 case 0x2a: return "audio playing ";
623 case 0x2c: return "emergency eject ";
625 case 0x30: return "focus ";
626 case 0x31: return "frame sync ";
627 case 0x32: return "subcode address ";
628 case 0x33: return "block sync ";
629 case 0x34: return "header address ";
631 case 0x40: return "illegal track read ";
632 case 0x41: return "mode 0 read ";
633 case 0x42: return "illegal mode read ";
634 case 0x43: return "illegal block size read ";
635 case 0x44: return "mode read ";
636 case 0x45: return "form read ";
637 case 0x46: return "leadout read ";
638 case 0x47: return "buffer overrun ";
640 case 0x53: return "unrecoverable CIRC ";
641 case 0x57: return "unrecoverable LECC ";
643 case 0x60: return "no TOC ";
644 case 0x61: return "invalid subcode data ";
645 case 0x63: return "focus on TOC read ";
646 case 0x64: return "frame sync on TOC read ";
647 case 0x65: return "TOC data ";
649 case 0x70: return "hardware failure ";
650 case 0x91: return "leadin ";
651 case 0x92: return "leadout ";
652 case 0x93: return "data track ";
654 sprintf(errbuf, "unknown 0x%02x ", err_code);
655 return errbuf;
659 * Set the drive parameters so the drive will auto-spin-up when a
660 * disk is inserted.
662 static void
663 set_drive_params(int want_doublespeed)
665 unsigned char res_reg[12];
666 unsigned int res_size;
667 unsigned char params[3];
670 params[0] = SONY_SD_AUTO_SPIN_DOWN_TIME;
671 params[1] = 0x00; /* Never spin down the drive. */
672 do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
673 params,
675 res_reg,
676 &res_size);
677 if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20))
679 printk(" Unable to set spin-down time: 0x%2.2x\n", res_reg[1]);
682 params[0] = SONY_SD_MECH_CONTROL;
683 params[1] = SONY_AUTO_SPIN_UP_BIT; /* Set auto spin up */
685 if (is_auto_eject) params[1] |= SONY_AUTO_EJECT_BIT;
687 if (is_double_speed && want_doublespeed)
689 params[1] |= SONY_DOUBLE_SPEED_BIT; /* Set the drive to double speed if
690 possible */
692 do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
693 params,
695 res_reg,
696 &res_size);
697 if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20))
699 printk(" Unable to set mechanical parameters: 0x%2.2x\n", res_reg[1]);
704 * Uniform cdrom interface function
705 * select reading speed for data access
707 static int scd_select_speed(struct cdrom_device_info *cdi, int speed)
709 if (speed == 0)
710 sony_speed = 1;
711 else
712 sony_speed = speed - 1;
714 set_drive_params(sony_speed);
715 return 0;
719 * Uniform cdrom interface function
720 * lock or unlock eject button
722 static int scd_lock_door(struct cdrom_device_info *cdi, int lock)
724 if (lock == 0 && sony_usage == 1)
726 /* Unlock the door, only if nobody is using the drive */
727 is_auto_eject = 1;
728 } else {
729 is_auto_eject = 0;
731 set_drive_params(sony_speed);
732 return 0;
736 * This code will reset the drive and attempt to restore sane parameters.
738 static void
739 restart_on_error(void)
741 unsigned char res_reg[12];
742 unsigned int res_size;
743 unsigned int retry_count;
746 printk("cdu31a: Resetting drive on error\n");
747 reset_drive();
748 retry_count = jiffies + SONY_RESET_TIMEOUT;
749 while (time_before(jiffies, retry_count) && (!is_attention()))
751 sony_sleep();
753 set_drive_params(sony_speed);
754 do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg, &res_size);
755 if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20))
757 printk("cdu31a: Unable to spin up drive: 0x%2.2x\n", res_reg[1]);
760 current->state = TASK_INTERRUPTIBLE;
761 schedule_timeout(2*HZ);
763 sony_get_toc();
767 * This routine writes data to the parameter register. Since this should
768 * happen fairly fast, it is polled with no OS waits between.
770 static int
771 write_params(unsigned char *params,
772 int num_params)
774 unsigned int retry_count;
777 retry_count = SONY_READY_RETRIES;
778 while ((retry_count > 0) && (!is_param_write_rdy()))
780 retry_count--;
782 if (!is_param_write_rdy())
784 return -EIO;
787 while (num_params > 0)
789 write_param(*params);
790 params++;
791 num_params--;
794 return 0;
799 * The following reads data from the command result register. It is a
800 * fairly complex routine, all status info flows back through this
801 * interface. The algorithm is stolen directly from the flowcharts in
802 * the drive manual.
804 static void
805 get_result(unsigned char *result_buffer,
806 unsigned int *result_size)
808 unsigned char a, b;
809 int i;
810 unsigned int retry_count;
813 while (handle_sony_cd_attention())
815 /* Wait for the result data to be ready */
816 retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
817 while (time_before(jiffies, retry_count) && (is_busy() || (!(is_result_ready()))))
819 sony_sleep();
821 while (handle_sony_cd_attention())
824 if (is_busy() || (!(is_result_ready())))
826 #if DEBUG
827 printk("CDU31A timeout out %d\n", __LINE__);
828 #endif
829 result_buffer[0] = 0x20;
830 result_buffer[1] = SONY_TIMEOUT_OP_ERR;
831 *result_size = 2;
832 return;
836 * Get the first two bytes. This determines what else needs
837 * to be done.
839 clear_result_ready();
840 a = read_result_register();
841 *result_buffer = a;
842 result_buffer++;
844 /* Check for block error status result. */
845 if ((a & 0xf0) == 0x50)
847 *result_size = 1;
848 return;
851 b = read_result_register();
852 *result_buffer = b;
853 result_buffer++;
854 *result_size = 2;
857 * 0x20 means an error occurred. Byte 2 will have the error code.
858 * Otherwise, the command succeeded, byte 2 will have the count of
859 * how many more status bytes are coming.
861 * The result register can be read 10 bytes at a time, a wait for
862 * result ready to be asserted must be done between every 10 bytes.
864 if ((a & 0xf0) != 0x20)
866 if (b > 8)
868 for (i=0; i<8; i++)
870 *result_buffer = read_result_register();
871 result_buffer++;
872 (*result_size)++;
874 b = b - 8;
876 while (b > 10)
878 retry_count = SONY_READY_RETRIES;
879 while ((retry_count > 0) && (!is_result_ready()))
881 retry_count--;
883 if (!is_result_ready())
885 #if DEBUG
886 printk("CDU31A timeout out %d\n", __LINE__);
887 #endif
888 result_buffer[0] = 0x20;
889 result_buffer[1] = SONY_TIMEOUT_OP_ERR;
890 *result_size = 2;
891 return;
894 clear_result_ready();
896 for (i=0; i<10; i++)
898 *result_buffer = read_result_register();
899 result_buffer++;
900 (*result_size)++;
902 b = b - 10;
905 if (b > 0)
907 retry_count = SONY_READY_RETRIES;
908 while ((retry_count > 0) && (!is_result_ready()))
910 retry_count--;
912 if (!is_result_ready())
914 #if DEBUG
915 printk("CDU31A timeout out %d\n", __LINE__);
916 #endif
917 result_buffer[0] = 0x20;
918 result_buffer[1] = SONY_TIMEOUT_OP_ERR;
919 *result_size = 2;
920 return;
925 while (b > 0)
927 *result_buffer = read_result_register();
928 result_buffer++;
929 (*result_size)++;
930 b--;
936 * Do a command that does not involve data transfer. This routine must
937 * be re-entrant from the same task to support being called from the
938 * data operation code when an error occurs.
940 static void
941 do_sony_cd_cmd(unsigned char cmd,
942 unsigned char *params,
943 unsigned int num_params,
944 unsigned char *result_buffer,
945 unsigned int *result_size)
947 unsigned int retry_count;
948 int num_retries;
949 int recursive_call;
950 unsigned long flags;
953 save_flags(flags);
954 cli();
955 if (current != has_cd_task) /* Allow recursive calls to this routine */
957 while (sony_inuse)
959 interruptible_sleep_on(&sony_wait);
960 if (signal_pending(current))
962 result_buffer[0] = 0x20;
963 result_buffer[1] = SONY_SIGNAL_OP_ERR;
964 *result_size = 2;
965 restore_flags(flags);
966 return;
969 sony_inuse = 1;
970 has_cd_task = current;
971 recursive_call = 0;
973 else
975 recursive_call = 1;
978 num_retries = 0;
979 retry_cd_operation:
981 while (handle_sony_cd_attention())
984 sti();
986 retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
987 while (time_before(jiffies, retry_count) && (is_busy()))
989 sony_sleep();
991 while (handle_sony_cd_attention())
994 if (is_busy())
996 #if DEBUG
997 printk("CDU31A timeout out %d\n", __LINE__);
998 #endif
999 result_buffer[0] = 0x20;
1000 result_buffer[1] = SONY_TIMEOUT_OP_ERR;
1001 *result_size = 2;
1003 else
1005 clear_result_ready();
1006 clear_param_reg();
1008 write_params(params, num_params);
1009 write_cmd(cmd);
1011 get_result(result_buffer, result_size);
1014 if ( ((result_buffer[0] & 0xf0) == 0x20)
1015 && (num_retries < MAX_CDU31A_RETRIES))
1017 num_retries++;
1018 current->state = TASK_INTERRUPTIBLE;
1019 schedule_timeout(HZ/10); /* Wait .1 seconds on retries */
1020 goto retry_cd_operation;
1023 if (!recursive_call)
1025 has_cd_task = NULL;
1026 sony_inuse = 0;
1027 wake_up_interruptible(&sony_wait);
1030 restore_flags(flags);
1035 * Handle an attention from the drive. This will return 1 if it found one
1036 * or 0 if not (if one is found, the caller might want to call again).
1038 * This routine counts the number of consecutive times it is called
1039 * (since this is always called from a while loop until it returns
1040 * a 0), and returns a 0 if it happens too many times. This will help
1041 * prevent a lockup.
1043 static int
1044 handle_sony_cd_attention(void)
1046 unsigned char atten_code;
1047 static int num_consecutive_attentions = 0;
1048 volatile int val;
1051 #if 0*DEBUG
1052 printk("Entering handle_sony_cd_attention\n");
1053 #endif
1054 if (is_attention())
1056 if (num_consecutive_attentions > CDU31A_MAX_CONSECUTIVE_ATTENTIONS)
1058 printk("cdu31a: Too many consecutive attentions: %d\n",
1059 num_consecutive_attentions);
1060 num_consecutive_attentions = 0;
1061 #if DEBUG
1062 printk("Leaving handle_sony_cd_attention at %d\n", __LINE__);
1063 #endif
1064 return(0);
1067 clear_attention();
1068 atten_code = read_result_register();
1070 switch (atten_code)
1072 /* Someone changed the CD. Mark it as changed */
1073 case SONY_MECH_LOADED_ATTN:
1074 disk_changed = 1;
1075 sony_toc_read = 0;
1076 sony_audio_status = CDROM_AUDIO_NO_STATUS;
1077 sony_blocks_left = 0;
1078 break;
1080 case SONY_SPIN_DOWN_COMPLETE_ATTN:
1081 /* Mark the disk as spun down. */
1082 sony_spun_up = 0;
1083 break;
1085 case SONY_AUDIO_PLAY_DONE_ATTN:
1086 sony_audio_status = CDROM_AUDIO_COMPLETED;
1087 read_subcode();
1088 break;
1090 case SONY_EJECT_PUSHED_ATTN:
1091 if (is_auto_eject)
1093 sony_audio_status = CDROM_AUDIO_INVALID;
1095 break;
1097 case SONY_LEAD_IN_ERR_ATTN:
1098 case SONY_LEAD_OUT_ERR_ATTN:
1099 case SONY_DATA_TRACK_ERR_ATTN:
1100 case SONY_AUDIO_PLAYBACK_ERR_ATTN:
1101 sony_audio_status = CDROM_AUDIO_ERROR;
1102 break;
1105 num_consecutive_attentions++;
1106 #if DEBUG
1107 printk("Leaving handle_sony_cd_attention at %d\n", __LINE__);
1108 #endif
1109 return(1);
1111 else if (abort_read_started)
1113 while (is_result_reg_not_empty())
1115 val = read_result_register();
1117 clear_data_ready();
1118 clear_result_ready();
1119 /* Clear out the data */
1120 while (is_data_requested())
1122 val = read_data_register();
1124 abort_read_started = 0;
1125 #if DEBUG
1126 printk("Leaving handle_sony_cd_attention at %d\n", __LINE__);
1127 #endif
1128 return(1);
1131 num_consecutive_attentions = 0;
1132 #if 0*DEBUG
1133 printk("Leaving handle_sony_cd_attention at %d\n", __LINE__);
1134 #endif
1135 return(0);
1139 /* Convert from an integer 0-99 to BCD */
1140 static inline unsigned int
1141 int_to_bcd(unsigned int val)
1143 int retval;
1146 retval = (val / 10) << 4;
1147 retval = retval | val % 10;
1148 return(retval);
1152 /* Convert from BCD to an integer from 0-99 */
1153 static unsigned int
1154 bcd_to_int(unsigned int bcd)
1156 return((((bcd >> 4) & 0x0f) * 10) + (bcd & 0x0f));
1161 * Convert a logical sector value (like the OS would want to use for
1162 * a block device) to an MSF format.
1164 static void
1165 log_to_msf(unsigned int log, unsigned char *msf)
1167 log = log + LOG_START_OFFSET;
1168 msf[0] = int_to_bcd(log / 4500);
1169 log = log % 4500;
1170 msf[1] = int_to_bcd(log / 75);
1171 msf[2] = int_to_bcd(log % 75);
1176 * Convert an MSF format to a logical sector.
1178 static unsigned int
1179 msf_to_log(unsigned char *msf)
1181 unsigned int log;
1184 log = msf[2];
1185 log += msf[1] * 75;
1186 log += msf[0] * 4500;
1187 log = log - LOG_START_OFFSET;
1189 return log;
1194 * Take in integer size value and put it into a buffer like
1195 * the drive would want to see a number-of-sector value.
1197 static void
1198 size_to_buf(unsigned int size,
1199 unsigned char *buf)
1201 buf[0] = size / 65536;
1202 size = size % 65536;
1203 buf[1] = size / 256;
1204 buf[2] = size % 256;
1207 /* Starts a read operation. Returns 0 on success and 1 on failure.
1208 The read operation used here allows multiple sequential sectors
1209 to be read and status returned for each sector. The driver will
1210 read the output one at a time as the requests come and abort the
1211 operation if the requested sector is not the next one from the
1212 drive. */
1213 static int
1214 start_request(unsigned int sector,
1215 unsigned int nsect,
1216 int read_nsect_only)
1218 unsigned char params[6];
1219 unsigned int read_size;
1220 unsigned int retry_count;
1223 #if DEBUG
1224 printk("Entering start_request\n");
1225 #endif
1226 log_to_msf(sector, params);
1227 /* If requested, read exactly what was asked. */
1228 if (read_nsect_only)
1230 read_size = nsect;
1233 * If the full read-ahead would go beyond the end of the media, trim
1234 * it back to read just till the end of the media.
1236 else if ((sector + nsect) >= sony_toc.lead_out_start_lba)
1238 read_size = sony_toc.lead_out_start_lba - sector;
1240 /* Read the full readahead amount. */
1241 else
1243 read_size = CDU31A_READAHEAD / 4;
1245 size_to_buf(read_size, &params[3]);
1248 * Clear any outstanding attentions and wait for the drive to
1249 * complete any pending operations.
1251 while (handle_sony_cd_attention())
1254 retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
1255 while (time_before(jiffies, retry_count) && (is_busy()))
1257 sony_sleep();
1259 while (handle_sony_cd_attention())
1263 if (is_busy())
1265 printk("CDU31A: Timeout while waiting to issue command\n");
1266 #if DEBUG
1267 printk("Leaving start_request at %d\n", __LINE__);
1268 #endif
1269 return(1);
1271 else
1273 /* Issue the command */
1274 clear_result_ready();
1275 clear_param_reg();
1277 write_params(params, 6);
1278 write_cmd(SONY_READ_BLKERR_STAT_CMD);
1280 sony_blocks_left = read_size * 4;
1281 sony_next_block = sector * 4;
1282 readahead_dataleft = 0;
1283 readahead_bad = 0;
1284 #if DEBUG
1285 printk("Leaving start_request at %d\n", __LINE__);
1286 #endif
1287 return(0);
1289 #if DEBUG
1290 printk("Leaving start_request at %d\n", __LINE__);
1291 #endif
1294 /* Abort a pending read operation. Clear all the drive status and
1295 readahead variables. */
1296 static void
1297 abort_read(void)
1299 unsigned char result_reg[2];
1300 int result_size;
1301 volatile int val;
1304 do_sony_cd_cmd(SONY_ABORT_CMD, NULL, 0, result_reg, &result_size);
1305 if ((result_reg[0] & 0xf0) == 0x20)
1307 printk("CDU31A: Error aborting read, %s error\n",
1308 translate_error(
1309 result_reg[1]));
1312 while (is_result_reg_not_empty())
1314 val = read_result_register();
1316 clear_data_ready();
1317 clear_result_ready();
1318 /* Clear out the data */
1319 while (is_data_requested())
1321 val = read_data_register();
1324 sony_blocks_left = 0;
1325 readahead_dataleft = 0;
1326 readahead_bad = 0;
1329 /* Called when the timer times out. This will abort the
1330 pending read operation. */
1331 static void
1332 handle_abort_timeout(unsigned long data)
1334 unsigned long flags;
1336 #if DEBUG
1337 printk("Entering handle_abort_timeout\n");
1338 #endif
1339 save_flags(flags);
1340 cli();
1341 /* If it is in use, ignore it. */
1342 if (!sony_inuse)
1344 /* We can't use abort_read(), because it will sleep
1345 or schedule in the timer interrupt. Just start
1346 the operation, finish it on the next access to
1347 the drive. */
1348 clear_result_ready();
1349 clear_param_reg();
1350 write_cmd(SONY_ABORT_CMD);
1352 sony_blocks_left = 0;
1353 readahead_dataleft = 0;
1354 readahead_bad = 0;
1355 abort_read_started = 1;
1357 restore_flags(flags);
1358 #if DEBUG
1359 printk("Leaving handle_abort_timeout\n");
1360 #endif
1363 /* Actually get data and status from the drive. */
1364 static void
1365 input_data(char *buffer,
1366 unsigned int bytesleft,
1367 unsigned int nblocks,
1368 unsigned int offset,
1369 unsigned int skip)
1371 int i;
1372 volatile unsigned char val;
1375 #if DEBUG
1376 printk("Entering input_data\n");
1377 #endif
1378 /* If an XA disk on a CDU31A, skip the first 12 bytes of data from
1379 the disk. The real data is after that. */
1380 if (sony_xa_mode)
1382 for(i=0; i<CD_XA_HEAD; i++)
1384 val = read_data_register();
1388 clear_data_ready();
1390 if (bytesleft == 2048) /* 2048 byte direct buffer transfer */
1392 insb(sony_cd_read_reg, buffer, 2048);
1393 readahead_dataleft = 0;
1395 else
1397 /* If the input read did not align with the beginning of the block,
1398 skip the necessary bytes. */
1399 if (skip != 0)
1401 insb(sony_cd_read_reg, readahead_buffer, skip);
1404 /* Get the data into the buffer. */
1405 insb(sony_cd_read_reg, &buffer[offset], bytesleft);
1407 /* Get the rest of the data into the readahead buffer at the
1408 proper location. */
1409 readahead_dataleft = (2048 - skip) - bytesleft;
1410 insb(sony_cd_read_reg,
1411 readahead_buffer + bytesleft,
1412 readahead_dataleft);
1414 sony_blocks_left -= nblocks;
1415 sony_next_block += nblocks;
1417 /* If an XA disk, we have to clear out the rest of the unused
1418 error correction data. */
1419 if (sony_xa_mode)
1421 for(i=0; i<CD_XA_TAIL; i++)
1423 val = read_data_register();
1426 #if DEBUG
1427 printk("Leaving input_data at %d\n", __LINE__);
1428 #endif
1431 /* read data from the drive. Note the nsect must be <= 4. */
1432 static void
1433 read_data_block(char *buffer,
1434 unsigned int block,
1435 unsigned int nblocks,
1436 unsigned char res_reg[],
1437 int *res_size)
1439 unsigned int retry_count;
1440 unsigned int bytesleft;
1441 unsigned int offset;
1442 unsigned int skip;
1445 #if DEBUG
1446 printk("Entering read_data_block\n");
1447 #endif
1449 res_reg[0] = 0;
1450 res_reg[1] = 0;
1451 *res_size = 0;
1452 bytesleft = nblocks * 512;
1453 offset = 0;
1455 /* If the data in the read-ahead does not match the block offset,
1456 then fix things up. */
1457 if (((block % 4) * 512) != ((2048 - readahead_dataleft) % 2048))
1459 sony_next_block += block % 4;
1460 sony_blocks_left -= block % 4;
1461 skip = (block % 4) * 512;
1463 else
1465 skip = 0;
1468 /* We have readahead data in the buffer, get that first before we
1469 decide if a read is necessary. */
1470 if (readahead_dataleft != 0)
1472 if (bytesleft > readahead_dataleft)
1474 /* The readahead will not fill the requested buffer, but
1475 get the data out of the readahead into the buffer. */
1476 memcpy(buffer,
1477 readahead_buffer + (2048 - readahead_dataleft),
1478 readahead_dataleft);
1479 readahead_dataleft = 0;
1480 bytesleft -= readahead_dataleft;
1481 offset += readahead_dataleft;
1483 else
1485 /* The readahead will fill the whole buffer, get the data
1486 and return. */
1487 memcpy(buffer,
1488 readahead_buffer + (2048 - readahead_dataleft),
1489 bytesleft);
1490 readahead_dataleft -= bytesleft;
1491 bytesleft = 0;
1492 sony_blocks_left -= nblocks;
1493 sony_next_block += nblocks;
1495 /* If the data in the readahead is bad, return an error so the
1496 driver will abort the buffer. */
1497 if (readahead_bad)
1499 res_reg[0] = 0x20;
1500 res_reg[1] = SONY_BAD_DATA_ERR;
1501 *res_size = 2;
1504 if (readahead_dataleft == 0)
1506 readahead_bad = 0;
1509 /* Final transfer is done for read command, get final result. */
1510 if (sony_blocks_left == 0)
1512 get_result(res_reg, res_size);
1514 #if DEBUG
1515 printk("Leaving read_data_block at %d\n", __LINE__);
1516 #endif
1517 return;
1521 /* Wait for the drive to tell us we have something */
1522 retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
1523 while (time_before(jiffies, retry_count) && !(is_data_ready()))
1525 while (handle_sony_cd_attention())
1528 sony_sleep();
1530 if (!(is_data_ready()))
1532 if (is_result_ready())
1534 get_result(res_reg, res_size);
1535 if ((res_reg[0] & 0xf0) != 0x20)
1537 printk("CDU31A: Got result that should have been error: %d\n",
1538 res_reg[0]);
1539 res_reg[0] = 0x20;
1540 res_reg[1] = SONY_BAD_DATA_ERR;
1541 *res_size = 2;
1543 abort_read();
1545 else
1547 #if DEBUG
1548 printk("CDU31A timeout out %d\n", __LINE__);
1549 #endif
1550 res_reg[0] = 0x20;
1551 res_reg[1] = SONY_TIMEOUT_OP_ERR;
1552 *res_size = 2;
1553 abort_read();
1556 else
1558 input_data(buffer, bytesleft, nblocks, offset, skip);
1560 /* Wait for the status from the drive. */
1561 retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
1562 while (time_before(jiffies, retry_count) && !(is_result_ready()))
1564 while (handle_sony_cd_attention())
1567 sony_sleep();
1570 if (!is_result_ready())
1572 #if DEBUG
1573 printk("CDU31A timeout out %d\n", __LINE__);
1574 #endif
1575 res_reg[0] = 0x20;
1576 res_reg[1] = SONY_TIMEOUT_OP_ERR;
1577 *res_size = 2;
1578 abort_read();
1580 else
1582 get_result(res_reg, res_size);
1584 /* If we got a buffer status, handle that. */
1585 if ((res_reg[0] & 0xf0) == 0x50)
1588 if ( (res_reg[0] == SONY_NO_CIRC_ERR_BLK_STAT)
1589 || (res_reg[0] == SONY_NO_LECC_ERR_BLK_STAT)
1590 || (res_reg[0] == SONY_RECOV_LECC_ERR_BLK_STAT))
1592 /* The data was successful, but if data was read from
1593 the readahead and it was bad, set the whole
1594 buffer as bad. */
1595 if (readahead_bad)
1597 readahead_bad = 0;
1598 res_reg[0] = 0x20;
1599 res_reg[1] = SONY_BAD_DATA_ERR;
1600 *res_size = 2;
1603 else
1605 printk("CDU31A: Data block error: 0x%x\n", res_reg[0]);
1606 res_reg[0] = 0x20;
1607 res_reg[1] = SONY_BAD_DATA_ERR;
1608 *res_size = 2;
1610 /* Data is in the readahead buffer but an error was returned.
1611 Make sure future requests don't use the data. */
1612 if (bytesleft != 2048)
1614 readahead_bad = 1;
1618 /* Final transfer is done for read command, get final result. */
1619 if (sony_blocks_left == 0)
1621 get_result(res_reg, res_size);
1624 else if ((res_reg[0] & 0xf0) != 0x20)
1626 /* The drive gave me bad status, I don't know what to do.
1627 Reset the driver and return an error. */
1628 printk("CDU31A: Invalid block status: 0x%x\n", res_reg[0]);
1629 restart_on_error();
1630 res_reg[0] = 0x20;
1631 res_reg[1] = SONY_BAD_DATA_ERR;
1632 *res_size = 2;
1636 #if DEBUG
1637 printk("Leaving read_data_block at %d\n", __LINE__);
1638 #endif
1643 * The OS calls this to perform a read or write operation to the drive.
1644 * Write obviously fail. Reads to a read ahead of sony_buffer_size
1645 * bytes to help speed operations. This especially helps since the OS
1646 * uses 1024 byte blocks and the drive uses 2048 byte blocks. Since most
1647 * data access on a CD is done sequentially, this saves a lot of operations.
1649 static void
1650 do_cdu31a_request(request_queue_t * q)
1652 int block;
1653 int nblock;
1654 unsigned char res_reg[12];
1655 unsigned int res_size;
1656 int num_retries;
1657 unsigned long flags;
1660 #if DEBUG
1661 printk("Entering do_cdu31a_request\n");
1662 #endif
1665 * Make sure no one else is using the driver; wait for them
1666 * to finish if it is so.
1668 save_flags(flags);
1669 cli();
1670 while (sony_inuse)
1672 interruptible_sleep_on(&sony_wait);
1673 if (signal_pending(current))
1675 restore_flags(flags);
1676 if (!QUEUE_EMPTY && CURRENT->rq_status != RQ_INACTIVE)
1678 end_request(0);
1680 restore_flags(flags);
1681 #if DEBUG
1682 printk("Leaving do_cdu31a_request at %d\n", __LINE__);
1683 #endif
1684 return;
1687 sony_inuse = 1;
1688 has_cd_task = current;
1690 /* Get drive status before doing anything. */
1691 while (handle_sony_cd_attention())
1694 /* Make sure we have a valid TOC. */
1695 sony_get_toc();
1697 spin_unlock_irq(&io_request_lock);
1699 /* Make sure the timer is cancelled. */
1700 del_timer(&cdu31a_abort_timer);
1702 while (1)
1704 cdu31a_request_startover:
1706 * The beginning here is stolen from the hard disk driver. I hope
1707 * it's right.
1709 if (QUEUE_EMPTY || CURRENT->rq_status == RQ_INACTIVE)
1711 goto end_do_cdu31a_request;
1714 if (!sony_spun_up)
1716 scd_spinup();
1719 /* I don't use INIT_REQUEST because it calls return, which would
1720 return without unlocking the device. It shouldn't matter,
1721 but just to be safe... */
1722 if (MAJOR(CURRENT->rq_dev) != MAJOR_NR)
1724 panic(DEVICE_NAME ": request list destroyed");
1726 if (CURRENT->bh)
1728 if (!buffer_locked(CURRENT->bh))
1730 panic(DEVICE_NAME ": block not locked");
1734 block = CURRENT->sector;
1735 nblock = CURRENT->nr_sectors;
1737 if (!sony_toc_read)
1739 printk("CDU31A: TOC not read\n");
1740 end_request(0);
1741 goto cdu31a_request_startover;
1744 switch(CURRENT->cmd)
1746 case READ:
1748 * If the block address is invalid or the request goes beyond the end of
1749 * the media, return an error.
1751 #if 0
1752 if ((block / 4) < sony_toc.start_track_lba)
1754 printk("CDU31A: Request before beginning of media\n");
1755 end_request(0);
1756 goto cdu31a_request_startover;
1758 #endif
1759 if ((block / 4) >= sony_toc.lead_out_start_lba)
1761 printk("CDU31A: Request past end of media\n");
1762 end_request(0);
1763 goto cdu31a_request_startover;
1765 if (((block + nblock) / 4) >= sony_toc.lead_out_start_lba)
1767 printk("CDU31A: Request past end of media\n");
1768 end_request(0);
1769 goto cdu31a_request_startover;
1772 num_retries = 0;
1774 try_read_again:
1775 while (handle_sony_cd_attention())
1778 if (!sony_toc_read)
1780 printk("CDU31A: TOC not read\n");
1781 end_request(0);
1782 goto cdu31a_request_startover;
1785 /* If no data is left to be read from the drive, start the
1786 next request. */
1787 if (sony_blocks_left == 0)
1789 if (start_request(block / 4, CDU31A_READAHEAD / 4, 0))
1791 end_request(0);
1792 goto cdu31a_request_startover;
1795 /* If the requested block is not the next one waiting in
1796 the driver, abort the current operation and start a
1797 new one. */
1798 else if (block != sony_next_block)
1800 #if DEBUG
1801 printk("CDU31A Warning: Read for block %d, expected %d\n",
1802 block,
1803 sony_next_block);
1804 #endif
1805 abort_read();
1806 if (!sony_toc_read)
1808 printk("CDU31A: TOC not read\n");
1809 end_request(0);
1810 goto cdu31a_request_startover;
1812 if (start_request(block / 4, CDU31A_READAHEAD / 4, 0))
1814 printk("CDU31a: start request failed\n");
1815 end_request(0);
1816 goto cdu31a_request_startover;
1820 read_data_block(CURRENT->buffer, block, nblock, res_reg, &res_size);
1821 if (res_reg[0] == 0x20)
1823 if (num_retries > MAX_CDU31A_RETRIES)
1825 end_request(0);
1826 goto cdu31a_request_startover;
1829 num_retries++;
1830 if (res_reg[1] == SONY_NOT_SPIN_ERR)
1832 do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg, &res_size);
1834 else
1836 printk("CDU31A: %s error for block %d, nblock %d\n", translate_error(res_reg[1]), block, nblock);
1838 goto try_read_again;
1840 else
1842 end_request(1);
1844 break;
1846 case WRITE:
1847 end_request(0);
1848 break;
1850 default:
1851 panic("CDU31A: Unknown cmd");
1855 end_do_cdu31a_request:
1856 spin_lock_irq(&io_request_lock);
1857 #if 0
1858 /* After finished, cancel any pending operations. */
1859 abort_read();
1860 #else
1861 /* Start a timer to time out after a while to disable
1862 the read. */
1863 cdu31a_abort_timer.expires = jiffies + 2*HZ; /* Wait 2 seconds */
1864 add_timer(&cdu31a_abort_timer);
1865 #endif
1867 has_cd_task = NULL;
1868 sony_inuse = 0;
1869 wake_up_interruptible(&sony_wait);
1870 restore_flags(flags);
1871 #if DEBUG
1872 printk("Leaving do_cdu31a_request at %d\n", __LINE__);
1873 #endif
1878 * Read the table of contents from the drive and set up TOC if
1879 * successful.
1881 static void
1882 sony_get_toc(void)
1884 unsigned char res_reg[2];
1885 unsigned int res_size;
1886 unsigned char parms[1];
1887 int session;
1888 int num_spin_ups;
1889 int totaltracks = 0;
1890 int mint = 99;
1891 int maxt = 0;
1893 #if DEBUG
1894 printk("Entering sony_get_toc\n");
1895 #endif
1897 num_spin_ups = 0;
1898 if (!sony_toc_read)
1900 respinup_on_gettoc:
1901 /* Ignore the result, since it might error if spinning already. */
1902 do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg, &res_size);
1904 do_sony_cd_cmd(SONY_READ_TOC_CMD, NULL, 0, res_reg, &res_size);
1906 /* The drive sometimes returns error 0. I don't know why, but ignore
1907 it. It seems to mean the drive has already done the operation. */
1908 if ((res_size < 2) || ((res_reg[0] != 0) && (res_reg[1] != 0)))
1910 /* If the drive is already playing, it's ok. */
1911 if ((res_reg[1] == SONY_AUDIO_PLAYING_ERR) || (res_reg[1] == 0))
1913 goto gettoc_drive_spinning;
1916 /* If the drive says it is not spun up (even though we just did it!)
1917 then retry the operation at least a few times. */
1918 if ( (res_reg[1] == SONY_NOT_SPIN_ERR)
1919 && (num_spin_ups < MAX_CDU31A_RETRIES))
1921 num_spin_ups++;
1922 goto respinup_on_gettoc;
1925 printk("cdu31a: Error reading TOC: %x %s\n",
1926 res_reg[0], translate_error(res_reg[1]));
1927 return;
1930 gettoc_drive_spinning:
1932 /* The idea here is we keep asking for sessions until the command
1933 fails. Then we know what the last valid session on the disk is.
1934 No need to check session 0, since session 0 is the same as session
1935 1; the command returns different information if you give it 0.
1937 #if DEBUG
1938 memset(&sony_toc, 0x0e, sizeof(sony_toc));
1939 memset(&single_toc, 0x0f, sizeof(single_toc));
1940 #endif
1941 session = 1;
1942 while (1)
1944 /* This seems to slow things down enough to make it work. This
1945 * appears to be a problem in do_sony_cd_cmd. This printk seems
1946 * to address the symptoms... -Erik */
1947 #if 1
1948 printk("cdu31a: Trying session %d\n", session);
1949 #endif
1950 parms[0] = session;
1951 do_sony_cd_cmd(SONY_READ_TOC_SPEC_CMD,
1952 parms,
1954 res_reg,
1955 &res_size);
1957 #if DEBUG
1958 printk("%2.2x %2.2x\n", res_reg[0], res_reg[1]);
1959 #endif
1961 if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20))
1963 /* An error reading the TOC, this must be past the last session. */
1964 if (session == 1)
1965 printk("Yikes! Couldn't read any sessions!");
1966 break;
1968 #if DEBUG
1969 printk("Reading session %d\n", session);
1970 #endif
1972 parms[0] = session;
1973 do_sony_cd_cmd(SONY_REQ_TOC_DATA_SPEC_CMD,
1974 parms,
1976 (unsigned char *) &single_toc,
1977 &res_size);
1978 if ((res_size < 2) || ((single_toc.exec_status[0] & 0xf0) == 0x20))
1980 printk("cdu31a: Error reading session %d: %x %s\n",
1981 session,
1982 single_toc.exec_status[0],
1983 translate_error(single_toc.exec_status[1]));
1984 /* An error reading the TOC. Return without sony_toc_read
1985 set. */
1986 return;
1988 #if DEBUG
1989 printk("add0 %01x, con0 %01x, poi0 %02x, 1st trk %d, dsktyp %x, dum0 %x\n",
1990 single_toc.address0, single_toc.control0, single_toc.point0,
1991 bcd_to_int(single_toc.first_track_num), single_toc.disk_type, single_toc.dummy0);
1992 printk("add1 %01x, con1 %01x, poi1 %02x, lst trk %d, dummy1 %x, dum2 %x\n",
1993 single_toc.address1, single_toc.control1, single_toc.point1,
1994 bcd_to_int(single_toc.last_track_num), single_toc.dummy1, single_toc.dummy2);
1995 printk("add2 %01x, con2 %01x, poi2 %02x leadout start min %d, sec %d, frame %d\n",
1996 single_toc.address2, single_toc.control2, single_toc.point2,
1997 bcd_to_int(single_toc.lead_out_start_msf[0]),
1998 bcd_to_int(single_toc.lead_out_start_msf[1]),
1999 bcd_to_int(single_toc.lead_out_start_msf[2]));
2000 if (res_size > 18 && single_toc.pointb0 > 0xaf)
2001 printk("addb0 %01x, conb0 %01x, poib0 %02x, nextsession min %d, sec %d, frame %d\n"
2002 "#mode5_ptrs %02d, max_start_outer_leadout_msf min %d, sec %d, frame %d\n",
2003 single_toc.addressb0, single_toc.controlb0, single_toc.pointb0,
2004 bcd_to_int(single_toc.next_poss_prog_area_msf[0]),
2005 bcd_to_int(single_toc.next_poss_prog_area_msf[1]),
2006 bcd_to_int(single_toc.next_poss_prog_area_msf[2]),
2007 single_toc.num_mode_5_pointers,
2008 bcd_to_int(single_toc.max_start_outer_leadout_msf[0]),
2009 bcd_to_int(single_toc.max_start_outer_leadout_msf[1]),
2010 bcd_to_int(single_toc.max_start_outer_leadout_msf[2]));
2011 if (res_size > 27 && single_toc.pointb1 > 0xaf)
2012 printk("addb1 %01x, conb1 %01x, poib1 %02x, %x %x %x %x #skipint_ptrs %d, #skiptrkassign %d %x\n",
2013 single_toc.addressb1, single_toc.controlb1, single_toc.pointb1,
2014 single_toc.dummyb0_1[0],
2015 single_toc.dummyb0_1[1],
2016 single_toc.dummyb0_1[2],
2017 single_toc.dummyb0_1[3],
2018 single_toc.num_skip_interval_pointers,
2019 single_toc.num_skip_track_assignments,
2020 single_toc.dummyb0_2);
2021 if (res_size > 36 && single_toc.pointb2 > 0xaf)
2022 printk("addb2 %01x, conb2 %01x, poib2 %02x, %02x %02x %02x %02x %02x %02x %02x\n",
2023 single_toc.addressb2, single_toc.controlb2, single_toc.pointb2,
2024 single_toc.tracksb2[0],
2025 single_toc.tracksb2[1],
2026 single_toc.tracksb2[2],
2027 single_toc.tracksb2[3],
2028 single_toc.tracksb2[4],
2029 single_toc.tracksb2[5],
2030 single_toc.tracksb2[6]);
2031 if (res_size > 45 && single_toc.pointb3 > 0xaf)
2032 printk("addb3 %01x, conb3 %01x, poib3 %02x, %02x %02x %02x %02x %02x %02x %02x\n",
2033 single_toc.addressb3, single_toc.controlb3, single_toc.pointb3,
2034 single_toc.tracksb3[0],
2035 single_toc.tracksb3[1],
2036 single_toc.tracksb3[2],
2037 single_toc.tracksb3[3],
2038 single_toc.tracksb3[4],
2039 single_toc.tracksb3[5],
2040 single_toc.tracksb3[6]);
2041 if (res_size > 54 && single_toc.pointb4 > 0xaf)
2042 printk("addb4 %01x, conb4 %01x, poib4 %02x, %02x %02x %02x %02x %02x %02x %02x\n",
2043 single_toc.addressb4, single_toc.controlb4, single_toc.pointb4,
2044 single_toc.tracksb4[0],
2045 single_toc.tracksb4[1],
2046 single_toc.tracksb4[2],
2047 single_toc.tracksb4[3],
2048 single_toc.tracksb4[4],
2049 single_toc.tracksb4[5],
2050 single_toc.tracksb4[6]);
2051 if (res_size > 63 && single_toc.pointc0 > 0xaf)
2052 printk("addc0 %01x, conc0 %01x, poic0 %02x, %02x %02x %02x %02x %02x %02x %02x\n",
2053 single_toc.addressc0, single_toc.controlc0, single_toc.pointc0,
2054 single_toc.dummyc0[0],
2055 single_toc.dummyc0[1],
2056 single_toc.dummyc0[2],
2057 single_toc.dummyc0[3],
2058 single_toc.dummyc0[4],
2059 single_toc.dummyc0[5],
2060 single_toc.dummyc0[6]);
2061 #endif
2062 #undef DEBUG
2063 #define DEBUG 0
2065 sony_toc.lead_out_start_msf[0] = bcd_to_int(single_toc.lead_out_start_msf[0]);
2066 sony_toc.lead_out_start_msf[1] = bcd_to_int(single_toc.lead_out_start_msf[1]);
2067 sony_toc.lead_out_start_msf[2] = bcd_to_int(single_toc.lead_out_start_msf[2]);
2068 sony_toc.lead_out_start_lba = single_toc.lead_out_start_lba =
2069 msf_to_log(sony_toc.lead_out_start_msf);
2071 /* For points that do not exist, move the data over them
2072 to the right location. */
2073 if (single_toc.pointb0 != 0xb0)
2075 memmove(((char *) &single_toc) + 27,
2076 ((char *) &single_toc) + 18,
2077 res_size - 18);
2078 res_size += 9;
2080 else if (res_size > 18) {
2081 sony_toc.lead_out_start_msf[0] = bcd_to_int(single_toc.max_start_outer_leadout_msf[0]);
2082 sony_toc.lead_out_start_msf[1] = bcd_to_int(single_toc.max_start_outer_leadout_msf[1]);
2083 sony_toc.lead_out_start_msf[2] = bcd_to_int(single_toc.max_start_outer_leadout_msf[2]);
2084 sony_toc.lead_out_start_lba = msf_to_log(sony_toc.lead_out_start_msf);
2086 if (single_toc.pointb1 != 0xb1)
2088 memmove(((char *) &single_toc) + 36,
2089 ((char *) &single_toc) + 27,
2090 res_size - 27);
2091 res_size += 9;
2093 if (single_toc.pointb2 != 0xb2)
2095 memmove(((char *) &single_toc) + 45,
2096 ((char *) &single_toc) + 36,
2097 res_size - 36);
2098 res_size += 9;
2100 if (single_toc.pointb3 != 0xb3)
2102 memmove(((char *) &single_toc) + 54,
2103 ((char *) &single_toc) + 45,
2104 res_size - 45);
2105 res_size += 9;
2107 if (single_toc.pointb4 != 0xb4)
2109 memmove(((char *) &single_toc) + 63,
2110 ((char *) &single_toc) + 54,
2111 res_size - 54);
2112 res_size += 9;
2114 if (single_toc.pointc0 != 0xc0)
2116 memmove(((char *) &single_toc) + 72,
2117 ((char *) &single_toc) + 63,
2118 res_size - 63);
2119 res_size += 9;
2122 #if DEBUG
2123 printk("start track lba %u, leadout start lba %u\n",
2124 single_toc.start_track_lba, single_toc.lead_out_start_lba);
2125 { int i;
2126 for (i = 0; i < 1 + bcd_to_int(single_toc.last_track_num)
2127 - bcd_to_int(single_toc.first_track_num); i++) {
2128 printk("trk %02d: add 0x%01x, con 0x%01x, track %02d, start min %02d, sec %02d, frame %02d\n",
2129 i, single_toc.tracks[i].address, single_toc.tracks[i].control,
2130 bcd_to_int(single_toc.tracks[i].track),
2131 bcd_to_int(single_toc.tracks[i].track_start_msf[0]),
2132 bcd_to_int(single_toc.tracks[i].track_start_msf[1]),
2133 bcd_to_int(single_toc.tracks[i].track_start_msf[2]));
2134 if (mint > bcd_to_int(single_toc.tracks[i].track))
2135 mint = bcd_to_int(single_toc.tracks[i].track);
2136 if (maxt < bcd_to_int(single_toc.tracks[i].track))
2137 maxt = bcd_to_int(single_toc.tracks[i].track);
2139 printk("min track number %d, max track number %d\n", mint, maxt);
2141 #endif
2143 /* prepare a special table of contents for a CD-I disc. They don't have one. */
2144 if (single_toc.disk_type == 0x10 &&
2145 single_toc.first_track_num == 2 &&
2146 single_toc.last_track_num == 2 /* CD-I */)
2148 sony_toc.tracks[totaltracks].address = 1;
2149 sony_toc.tracks[totaltracks].control = 4; /* force data tracks */
2150 sony_toc.tracks[totaltracks].track = 1;
2151 sony_toc.tracks[totaltracks].track_start_msf[0] = 0;
2152 sony_toc.tracks[totaltracks].track_start_msf[1] = 2;
2153 sony_toc.tracks[totaltracks].track_start_msf[2] = 0;
2154 mint = maxt = 1;
2155 totaltracks++;
2156 } else
2157 /* gather track entries from this session */
2158 { int i;
2159 for (i = 0; i < 1 + bcd_to_int(single_toc.last_track_num)
2160 - bcd_to_int(single_toc.first_track_num); i++, totaltracks++) {
2161 sony_toc.tracks[totaltracks].address = single_toc.tracks[i].address;
2162 sony_toc.tracks[totaltracks].control = single_toc.tracks[i].control;
2163 sony_toc.tracks[totaltracks].track = bcd_to_int(single_toc.tracks[i].track);
2164 sony_toc.tracks[totaltracks].track_start_msf[0] =
2165 bcd_to_int(single_toc.tracks[i].track_start_msf[0]);
2166 sony_toc.tracks[totaltracks].track_start_msf[1] =
2167 bcd_to_int(single_toc.tracks[i].track_start_msf[1]);
2168 sony_toc.tracks[totaltracks].track_start_msf[2] =
2169 bcd_to_int(single_toc.tracks[i].track_start_msf[2]);
2170 if (i == 0)
2171 single_toc.start_track_lba = msf_to_log(sony_toc.tracks[totaltracks].track_start_msf);
2172 if (mint > sony_toc.tracks[totaltracks].track)
2173 mint = sony_toc.tracks[totaltracks].track;
2174 if (maxt < sony_toc.tracks[totaltracks].track)
2175 maxt = sony_toc.tracks[totaltracks].track;
2178 sony_toc.first_track_num = mint;
2179 sony_toc.last_track_num = maxt;
2180 /* Disk type of last session wins. For example:
2181 CD-Extra has disk type 0 for the first session, so
2182 a dumb HiFi CD player thinks it is a plain audio CD.
2183 We are interested in the disk type of the last session,
2184 which is 0x20 (XA) for CD-Extra, so we can access the
2185 data track ... */
2186 sony_toc.disk_type = single_toc.disk_type;
2187 sony_toc.sessions = session;
2189 /* don't believe everything :-) */
2190 if (session == 1)
2191 single_toc.start_track_lba = 0;
2192 sony_toc.start_track_lba = single_toc.start_track_lba;
2194 if (session > 1 && single_toc.pointb0 == 0xb0 &&
2195 sony_toc.lead_out_start_lba == single_toc.lead_out_start_lba)
2197 break;
2200 /* Let's not get carried away... */
2201 if (session > 40)
2203 printk("cdu31a: too many sessions: %d\n", session);
2204 break;
2206 session++;
2208 sony_toc.track_entries = totaltracks;
2209 /* add one entry for the LAST track with track number CDROM_LEADOUT */
2210 sony_toc.tracks[totaltracks].address = single_toc.address2;
2211 sony_toc.tracks[totaltracks].control = single_toc.control2;
2212 sony_toc.tracks[totaltracks].track = CDROM_LEADOUT;
2213 sony_toc.tracks[totaltracks].track_start_msf[0] =
2214 sony_toc.lead_out_start_msf[0];
2215 sony_toc.tracks[totaltracks].track_start_msf[1] =
2216 sony_toc.lead_out_start_msf[1];
2217 sony_toc.tracks[totaltracks].track_start_msf[2] =
2218 sony_toc.lead_out_start_msf[2];
2220 sony_toc_read = 1;
2221 #undef DEBUG
2222 #if DEBUG
2223 printk("Disk session %d, start track: %d, stop track: %d\n",
2224 session,
2225 single_toc.start_track_lba,
2226 single_toc.lead_out_start_lba);
2227 #endif
2229 #if DEBUG
2230 printk("Leaving sony_get_toc\n");
2231 #endif
2236 * Uniform cdrom interface function
2237 * return multisession offset and sector information
2239 static int scd_get_last_session(struct cdrom_device_info *cdi,
2240 struct cdrom_multisession *ms_info)
2242 if (ms_info == NULL)
2243 return 1;
2245 if (!sony_toc_read)
2246 sony_get_toc();
2248 ms_info->addr_format = CDROM_LBA;
2249 ms_info->addr.lba = sony_toc.start_track_lba;
2250 ms_info->xa_flag = sony_toc.disk_type == SONY_XA_DISK_TYPE ||
2251 sony_toc.disk_type == 0x10 /* CDI */;
2253 return 0;
2257 * Search for a specific track in the table of contents.
2259 static int
2260 find_track(int track)
2262 int i;
2264 for (i = 0; i <= sony_toc.track_entries; i++)
2266 if (sony_toc.tracks[i].track == track)
2268 return i;
2272 return -1;
2277 * Read the subcode and put it in last_sony_subcode for future use.
2279 static int
2280 read_subcode(void)
2282 unsigned int res_size;
2285 do_sony_cd_cmd(SONY_REQ_SUBCODE_ADDRESS_CMD,
2286 NULL,
2288 (unsigned char *) &last_sony_subcode,
2289 &res_size);
2290 if ((res_size < 2) || ((last_sony_subcode.exec_status[0] & 0xf0) == 0x20))
2292 printk("Sony CDROM error %s (read_subcode)\n",
2293 translate_error(last_sony_subcode.exec_status[1]));
2294 return -EIO;
2297 last_sony_subcode.track_num = bcd_to_int(last_sony_subcode.track_num);
2298 last_sony_subcode.index_num = bcd_to_int(last_sony_subcode.index_num);
2299 last_sony_subcode.abs_msf[0] = bcd_to_int(last_sony_subcode.abs_msf[0]);
2300 last_sony_subcode.abs_msf[1] = bcd_to_int(last_sony_subcode.abs_msf[1]);
2301 last_sony_subcode.abs_msf[2] = bcd_to_int(last_sony_subcode.abs_msf[2]);
2303 last_sony_subcode.rel_msf[0] = bcd_to_int(last_sony_subcode.rel_msf[0]);
2304 last_sony_subcode.rel_msf[1] = bcd_to_int(last_sony_subcode.rel_msf[1]);
2305 last_sony_subcode.rel_msf[2] = bcd_to_int(last_sony_subcode.rel_msf[2]);
2306 return 0;
2310 * Uniform cdrom interface function
2311 * return the media catalog number found on some older audio cds
2313 static int
2314 scd_get_mcn(struct cdrom_device_info *cdi, struct cdrom_mcn *mcn)
2316 unsigned char resbuffer[2 + 14];
2317 unsigned char *mcnp = mcn->medium_catalog_number;
2318 unsigned char *resp = resbuffer + 3;
2319 unsigned int res_size;
2321 memset(mcn->medium_catalog_number, 0, 14);
2322 do_sony_cd_cmd(SONY_REQ_UPC_EAN_CMD,
2323 NULL,
2325 resbuffer,
2326 &res_size);
2327 if ((res_size < 2) || ((resbuffer[0] & 0xf0) == 0x20))
2329 else {
2330 /* packed bcd to single ASCII digits */
2331 *mcnp++ = (*resp >> 4) + '0';
2332 *mcnp++ = (*resp++ & 0x0f) + '0';
2333 *mcnp++ = (*resp >> 4) + '0';
2334 *mcnp++ = (*resp++ & 0x0f) + '0';
2335 *mcnp++ = (*resp >> 4) + '0';
2336 *mcnp++ = (*resp++ & 0x0f) + '0';
2337 *mcnp++ = (*resp >> 4) + '0';
2338 *mcnp++ = (*resp++ & 0x0f) + '0';
2339 *mcnp++ = (*resp >> 4) + '0';
2340 *mcnp++ = (*resp++ & 0x0f) + '0';
2341 *mcnp++ = (*resp >> 4) + '0';
2342 *mcnp++ = (*resp++ & 0x0f) + '0';
2343 *mcnp++ = (*resp >> 4) + '0';
2345 *mcnp = '\0';
2346 return 0;
2351 * Get the subchannel info like the CDROMSUBCHNL command wants to see it. If
2352 * the drive is playing, the subchannel needs to be read (since it would be
2353 * changing). If the drive is paused or completed, the subcode information has
2354 * already been stored, just use that. The ioctl call wants things in decimal
2355 * (not BCD), so all the conversions are done.
2357 static int
2358 sony_get_subchnl_info(struct cdrom_subchnl *schi)
2360 /* Get attention stuff */
2361 while (handle_sony_cd_attention())
2364 sony_get_toc();
2365 if (!sony_toc_read)
2367 return -EIO;
2370 switch (sony_audio_status)
2372 case CDROM_AUDIO_NO_STATUS:
2373 case CDROM_AUDIO_PLAY:
2374 if (read_subcode() < 0)
2376 return -EIO;
2378 break;
2380 case CDROM_AUDIO_PAUSED:
2381 case CDROM_AUDIO_COMPLETED:
2382 break;
2384 #if 0
2385 case CDROM_AUDIO_NO_STATUS:
2386 schi->cdsc_audiostatus = sony_audio_status;
2387 return 0;
2388 break;
2389 #endif
2390 case CDROM_AUDIO_INVALID:
2391 case CDROM_AUDIO_ERROR:
2392 default:
2393 return -EIO;
2396 schi->cdsc_audiostatus = sony_audio_status;
2397 schi->cdsc_adr = last_sony_subcode.address;
2398 schi->cdsc_ctrl = last_sony_subcode.control;
2399 schi->cdsc_trk = last_sony_subcode.track_num;
2400 schi->cdsc_ind = last_sony_subcode.index_num;
2401 if (schi->cdsc_format == CDROM_MSF)
2403 schi->cdsc_absaddr.msf.minute = last_sony_subcode.abs_msf[0];
2404 schi->cdsc_absaddr.msf.second = last_sony_subcode.abs_msf[1];
2405 schi->cdsc_absaddr.msf.frame = last_sony_subcode.abs_msf[2];
2407 schi->cdsc_reladdr.msf.minute = last_sony_subcode.rel_msf[0];
2408 schi->cdsc_reladdr.msf.second = last_sony_subcode.rel_msf[1];
2409 schi->cdsc_reladdr.msf.frame = last_sony_subcode.rel_msf[2];
2411 else if (schi->cdsc_format == CDROM_LBA)
2413 schi->cdsc_absaddr.lba = msf_to_log(last_sony_subcode.abs_msf);
2414 schi->cdsc_reladdr.lba = msf_to_log(last_sony_subcode.rel_msf);
2417 return 0;
2420 /* Get audio data from the drive. This is fairly complex because I
2421 am looking for status and data at the same time, but if I get status
2422 then I just look for data. I need to get the status immediately so
2423 the switch from audio to data tracks will happen quickly. */
2424 static void
2425 read_audio_data(char *buffer,
2426 unsigned char res_reg[],
2427 int *res_size)
2429 unsigned int retry_count;
2430 int result_read;
2433 res_reg[0] = 0;
2434 res_reg[1] = 0;
2435 *res_size = 0;
2436 result_read = 0;
2438 /* Wait for the drive to tell us we have something */
2439 retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
2440 continue_read_audio_wait:
2441 while (time_before(jiffies, retry_count) && !(is_data_ready())
2442 && !(is_result_ready() || result_read))
2444 while (handle_sony_cd_attention())
2447 sony_sleep();
2449 if (!(is_data_ready()))
2451 if (is_result_ready() && !result_read)
2453 get_result(res_reg, res_size);
2455 /* Read block status and continue waiting for data. */
2456 if ((res_reg[0] & 0xf0) == 0x50)
2458 result_read = 1;
2459 goto continue_read_audio_wait;
2461 /* Invalid data from the drive. Shut down the operation. */
2462 else if ((res_reg[0] & 0xf0) != 0x20)
2464 printk("CDU31A: Got result that should have been error: %d\n",
2465 res_reg[0]);
2466 res_reg[0] = 0x20;
2467 res_reg[1] = SONY_BAD_DATA_ERR;
2468 *res_size = 2;
2470 abort_read();
2472 else
2474 #if DEBUG
2475 printk("CDU31A timeout out %d\n", __LINE__);
2476 #endif
2477 res_reg[0] = 0x20;
2478 res_reg[1] = SONY_TIMEOUT_OP_ERR;
2479 *res_size = 2;
2480 abort_read();
2483 else
2485 clear_data_ready();
2487 /* If data block, then get 2340 bytes offset by 12. */
2488 if (sony_raw_data_mode)
2490 insb(sony_cd_read_reg, buffer + CD_XA_HEAD, CD_FRAMESIZE_RAW1);
2492 else
2494 /* Audio gets the whole 2352 bytes. */
2495 insb(sony_cd_read_reg, buffer, CD_FRAMESIZE_RAW);
2498 /* If I haven't already gotten the result, get it now. */
2499 if (!result_read)
2501 /* Wait for the drive to tell us we have something */
2502 retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
2503 while (time_before(jiffies, retry_count) && !(is_result_ready()))
2505 while (handle_sony_cd_attention())
2508 sony_sleep();
2511 if (!is_result_ready())
2513 #if DEBUG
2514 printk("CDU31A timeout out %d\n", __LINE__);
2515 #endif
2516 res_reg[0] = 0x20;
2517 res_reg[1] = SONY_TIMEOUT_OP_ERR;
2518 *res_size = 2;
2519 abort_read();
2520 return;
2522 else
2524 get_result(res_reg, res_size);
2528 if ((res_reg[0] & 0xf0) == 0x50)
2530 if ( (res_reg[0] == SONY_NO_CIRC_ERR_BLK_STAT)
2531 || (res_reg[0] == SONY_NO_LECC_ERR_BLK_STAT)
2532 || (res_reg[0] == SONY_RECOV_LECC_ERR_BLK_STAT)
2533 || (res_reg[0] == SONY_NO_ERR_DETECTION_STAT))
2535 /* Ok, nothing to do. */
2537 else
2539 printk("CDU31A: Data block error: 0x%x\n", res_reg[0]);
2540 res_reg[0] = 0x20;
2541 res_reg[1] = SONY_BAD_DATA_ERR;
2542 *res_size = 2;
2545 else if ((res_reg[0] & 0xf0) != 0x20)
2547 /* The drive gave me bad status, I don't know what to do.
2548 Reset the driver and return an error. */
2549 printk("CDU31A: Invalid block status: 0x%x\n", res_reg[0]);
2550 restart_on_error();
2551 res_reg[0] = 0x20;
2552 res_reg[1] = SONY_BAD_DATA_ERR;
2553 *res_size = 2;
2558 /* Perform a raw data read. This will automatically detect the
2559 track type and read the proper data (audio or data). */
2560 static int
2561 read_audio(struct cdrom_read_audio *ra)
2563 int retval;
2564 unsigned char params[2];
2565 unsigned char res_reg[12];
2566 unsigned int res_size;
2567 unsigned int cframe;
2568 unsigned long flags;
2571 * Make sure no one else is using the driver; wait for them
2572 * to finish if it is so.
2574 save_flags(flags);
2575 cli();
2576 while (sony_inuse)
2578 interruptible_sleep_on(&sony_wait);
2579 if (signal_pending(current))
2581 restore_flags(flags);
2582 return -EAGAIN;
2585 sony_inuse = 1;
2586 has_cd_task = current;
2587 restore_flags(flags);
2589 if (!sony_spun_up)
2591 scd_spinup();
2594 /* Set the drive to do raw operations. */
2595 params[0] = SONY_SD_DECODE_PARAM;
2596 params[1] = 0x06 | sony_raw_data_mode;
2597 do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
2598 params,
2600 res_reg,
2601 &res_size);
2602 if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20))
2604 printk("CDU31A: Unable to set decode params: 0x%2.2x\n", res_reg[1]);
2605 return -EIO;
2608 /* From here down, we have to goto exit_read_audio instead of returning
2609 because the drive parameters have to be set back to data before
2610 return. */
2612 retval = 0;
2613 /* start_request clears out any readahead data, so it should be safe. */
2614 if (start_request(ra->addr.lba, ra->nframes, 1))
2616 retval = -EIO;
2617 goto exit_read_audio;
2620 /* For every requested frame. */
2621 cframe = 0;
2622 while (cframe < ra->nframes)
2624 read_audio_data(readahead_buffer, res_reg, &res_size);
2625 if ((res_reg[0] & 0xf0) == 0x20)
2627 if (res_reg[1] == SONY_BAD_DATA_ERR)
2629 printk("CDU31A: Data error on audio sector %d\n",
2630 ra->addr.lba + cframe);
2632 else if (res_reg[1] == SONY_ILL_TRACK_R_ERR)
2634 /* Illegal track type, change track types and start over. */
2635 sony_raw_data_mode = (sony_raw_data_mode) ? 0 : 1;
2637 /* Set the drive mode. */
2638 params[0] = SONY_SD_DECODE_PARAM;
2639 params[1] = 0x06 | sony_raw_data_mode;
2640 do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
2641 params,
2643 res_reg,
2644 &res_size);
2645 if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20))
2647 printk("CDU31A: Unable to set decode params: 0x%2.2x\n", res_reg[1]);
2648 retval = -EIO;
2649 goto exit_read_audio;
2652 /* Restart the request on the current frame. */
2653 if (start_request(ra->addr.lba + cframe, ra->nframes - cframe, 1))
2655 retval = -EIO;
2656 goto exit_read_audio;
2659 /* Don't go back to the top because don't want to get into
2660 and infinite loop. A lot of code gets duplicated, but
2661 that's no big deal, I don't guess. */
2662 read_audio_data(readahead_buffer, res_reg, &res_size);
2663 if ((res_reg[0] & 0xf0) == 0x20)
2665 if (res_reg[1] == SONY_BAD_DATA_ERR)
2667 printk("CDU31A: Data error on audio sector %d\n",
2668 ra->addr.lba + cframe);
2670 else
2672 printk("CDU31A: Error reading audio data on sector %d: %s\n",
2673 ra->addr.lba + cframe,
2674 translate_error(res_reg[1]));
2675 retval = -EIO;
2676 goto exit_read_audio;
2679 else
2681 copy_to_user((char *) (ra->buf + (CD_FRAMESIZE_RAW * cframe)),
2682 (char *) readahead_buffer,
2683 CD_FRAMESIZE_RAW);
2686 else
2688 printk("CDU31A: Error reading audio data on sector %d: %s\n",
2689 ra->addr.lba + cframe,
2690 translate_error(res_reg[1]));
2691 retval = -EIO;
2692 goto exit_read_audio;
2695 else
2697 copy_to_user((char *) (ra->buf + (CD_FRAMESIZE_RAW * cframe)),
2698 (char *) readahead_buffer,
2699 CD_FRAMESIZE_RAW);
2702 cframe++;
2705 get_result(res_reg, &res_size);
2706 if ((res_reg[0] & 0xf0) == 0x20)
2708 printk("CDU31A: Error return from audio read: %s\n",
2709 translate_error(res_reg[1]));
2710 retval = -EIO;
2711 goto exit_read_audio;
2714 exit_read_audio:
2716 /* Set the drive mode back to the proper one for the disk. */
2717 params[0] = SONY_SD_DECODE_PARAM;
2718 if (!sony_xa_mode)
2720 params[1] = 0x0f;
2722 else
2724 params[1] = 0x07;
2726 do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
2727 params,
2729 res_reg,
2730 &res_size);
2731 if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20))
2733 printk("CDU31A: Unable to reset decode params: 0x%2.2x\n", res_reg[1]);
2734 return -EIO;
2737 has_cd_task = NULL;
2738 sony_inuse = 0;
2739 wake_up_interruptible(&sony_wait);
2741 return(retval);
2744 static int
2745 do_sony_cd_cmd_chk(const char *name,
2746 unsigned char cmd,
2747 unsigned char *params,
2748 unsigned int num_params,
2749 unsigned char *result_buffer,
2750 unsigned int *result_size)
2752 do_sony_cd_cmd(cmd, params, num_params, result_buffer, result_size);
2753 if ((*result_size < 2) || ((result_buffer[0] & 0xf0) == 0x20))
2755 printk("Sony CDROM error %s (CDROM%s)\n", translate_error(result_buffer[1]), name);
2756 return -EIO;
2758 return 0;
2762 * Uniform cdrom interface function
2763 * open the tray
2765 static int scd_tray_move(struct cdrom_device_info *cdi, int position)
2767 if (position == 1 /* open tray */)
2769 unsigned char res_reg[12];
2770 unsigned int res_size;
2772 do_sony_cd_cmd(SONY_AUDIO_STOP_CMD, NULL, 0, res_reg, &res_size);
2773 do_sony_cd_cmd(SONY_SPIN_DOWN_CMD, NULL, 0, res_reg, &res_size);
2775 sony_audio_status = CDROM_AUDIO_INVALID;
2776 return do_sony_cd_cmd_chk("EJECT",SONY_EJECT_CMD, NULL, 0, res_reg, &res_size);
2777 } else {
2778 if (0 == scd_spinup())
2779 sony_spun_up = 1;
2780 return 0;
2785 * The big ugly ioctl handler.
2787 static int scd_audio_ioctl(struct cdrom_device_info *cdi,
2788 unsigned int cmd,
2789 void * arg)
2791 unsigned char res_reg[12];
2792 unsigned int res_size;
2793 unsigned char params[7];
2794 int i;
2797 switch (cmd)
2799 case CDROMSTART: /* Spin up the drive */
2800 return do_sony_cd_cmd_chk("START",SONY_SPIN_UP_CMD, NULL, 0, res_reg, &res_size);
2801 break;
2803 case CDROMSTOP: /* Spin down the drive */
2804 do_sony_cd_cmd(SONY_AUDIO_STOP_CMD, NULL, 0, res_reg, &res_size);
2807 * Spin the drive down, ignoring the error if the disk was
2808 * already not spinning.
2810 sony_audio_status = CDROM_AUDIO_NO_STATUS;
2811 return do_sony_cd_cmd_chk("STOP",SONY_SPIN_DOWN_CMD, NULL, 0, res_reg, &res_size);
2813 case CDROMPAUSE: /* Pause the drive */
2814 if(do_sony_cd_cmd_chk("PAUSE", SONY_AUDIO_STOP_CMD, NULL, 0, res_reg, &res_size))
2815 return -EIO;
2816 /* Get the current position and save it for resuming */
2817 if (read_subcode() < 0)
2819 return -EIO;
2821 cur_pos_msf[0] = last_sony_subcode.abs_msf[0];
2822 cur_pos_msf[1] = last_sony_subcode.abs_msf[1];
2823 cur_pos_msf[2] = last_sony_subcode.abs_msf[2];
2824 sony_audio_status = CDROM_AUDIO_PAUSED;
2825 return 0;
2826 break;
2828 case CDROMRESUME: /* Start the drive after being paused */
2829 if (sony_audio_status != CDROM_AUDIO_PAUSED)
2831 return -EINVAL;
2834 do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg, &res_size);
2836 /* Start the drive at the saved position. */
2837 params[1] = int_to_bcd(cur_pos_msf[0]);
2838 params[2] = int_to_bcd(cur_pos_msf[1]);
2839 params[3] = int_to_bcd(cur_pos_msf[2]);
2840 params[4] = int_to_bcd(final_pos_msf[0]);
2841 params[5] = int_to_bcd(final_pos_msf[1]);
2842 params[6] = int_to_bcd(final_pos_msf[2]);
2843 params[0] = 0x03;
2844 if(do_sony_cd_cmd_chk("RESUME",SONY_AUDIO_PLAYBACK_CMD, params, 7, res_reg, &res_size)<0)
2845 return -EIO;
2846 sony_audio_status = CDROM_AUDIO_PLAY;
2847 return 0;
2849 case CDROMPLAYMSF: /* Play starting at the given MSF address. */
2850 do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg, &res_size);
2852 /* The parameters are given in int, must be converted */
2853 for (i=1; i<7; i++)
2855 params[i] = int_to_bcd(((unsigned char *)arg)[i-1]);
2857 params[0] = 0x03;
2858 if(do_sony_cd_cmd_chk("PLAYMSF",SONY_AUDIO_PLAYBACK_CMD, params, 7, res_reg, &res_size)<0)
2859 return -EIO;
2861 /* Save the final position for pauses and resumes */
2862 final_pos_msf[0] = bcd_to_int(params[4]);
2863 final_pos_msf[1] = bcd_to_int(params[5]);
2864 final_pos_msf[2] = bcd_to_int(params[6]);
2865 sony_audio_status = CDROM_AUDIO_PLAY;
2866 return 0;
2868 case CDROMREADTOCHDR: /* Read the table of contents header */
2870 struct cdrom_tochdr *hdr;
2872 sony_get_toc();
2873 if (!sony_toc_read)
2875 return -EIO;
2878 hdr = (struct cdrom_tochdr *) arg;
2879 hdr->cdth_trk0 = sony_toc.first_track_num;
2880 hdr->cdth_trk1 = sony_toc.last_track_num;
2882 return 0;
2884 case CDROMREADTOCENTRY: /* Read a given table of contents entry */
2886 struct cdrom_tocentry *entry;
2887 int track_idx;
2888 unsigned char *msf_val = NULL;
2890 sony_get_toc();
2891 if (!sony_toc_read)
2893 return -EIO;
2896 entry = (struct cdrom_tocentry *) arg;
2898 track_idx = find_track(entry->cdte_track);
2899 if (track_idx < 0)
2901 return -EINVAL;
2904 entry->cdte_adr = sony_toc.tracks[track_idx].address;
2905 entry->cdte_ctrl = sony_toc.tracks[track_idx].control;
2906 msf_val = sony_toc.tracks[track_idx].track_start_msf;
2908 /* Logical buffer address or MSF format requested? */
2909 if (entry->cdte_format == CDROM_LBA)
2911 entry->cdte_addr.lba = msf_to_log(msf_val);
2913 else if (entry->cdte_format == CDROM_MSF)
2915 entry->cdte_addr.msf.minute = *msf_val;
2916 entry->cdte_addr.msf.second = *(msf_val+1);
2917 entry->cdte_addr.msf.frame = *(msf_val+2);
2920 return 0;
2921 break;
2923 case CDROMPLAYTRKIND: /* Play a track. This currently ignores index. */
2925 struct cdrom_ti *ti = (struct cdrom_ti *) arg;
2926 int track_idx;
2928 sony_get_toc();
2929 if (!sony_toc_read)
2931 return -EIO;
2934 if ( (ti->cdti_trk0 < sony_toc.first_track_num)
2935 || (ti->cdti_trk0 > sony_toc.last_track_num)
2936 || (ti->cdti_trk1 < ti->cdti_trk0))
2938 return -EINVAL;
2941 track_idx = find_track(ti->cdti_trk0);
2942 if (track_idx < 0)
2944 return -EINVAL;
2946 params[1] = int_to_bcd(sony_toc.tracks[track_idx].track_start_msf[0]);
2947 params[2] = int_to_bcd(sony_toc.tracks[track_idx].track_start_msf[1]);
2948 params[3] = int_to_bcd(sony_toc.tracks[track_idx].track_start_msf[2]);
2951 * If we want to stop after the last track, use the lead-out
2952 * MSF to do that.
2954 if (ti->cdti_trk1 >= sony_toc.last_track_num)
2956 track_idx = find_track(CDROM_LEADOUT);
2958 else
2960 track_idx = find_track(ti->cdti_trk1+1);
2962 if (track_idx < 0)
2964 return -EINVAL;
2966 params[4] = int_to_bcd(sony_toc.tracks[track_idx].track_start_msf[0]);
2967 params[5] = int_to_bcd(sony_toc.tracks[track_idx].track_start_msf[1]);
2968 params[6] = int_to_bcd(sony_toc.tracks[track_idx].track_start_msf[2]);
2969 params[0] = 0x03;
2971 do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg, &res_size);
2973 do_sony_cd_cmd(SONY_AUDIO_PLAYBACK_CMD, params, 7, res_reg, &res_size);
2975 if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20))
2977 printk("Params: %x %x %x %x %x %x %x\n", params[0], params[1],
2978 params[2], params[3], params[4], params[5], params[6]);
2979 printk("Sony CDROM error %s (CDROMPLAYTRKIND)\n", translate_error(res_reg[1]));
2980 return -EIO;
2983 /* Save the final position for pauses and resumes */
2984 final_pos_msf[0] = bcd_to_int(params[4]);
2985 final_pos_msf[1] = bcd_to_int(params[5]);
2986 final_pos_msf[2] = bcd_to_int(params[6]);
2987 sony_audio_status = CDROM_AUDIO_PLAY;
2988 return 0;
2991 case CDROMVOLCTRL: /* Volume control. What volume does this change, anyway? */
2993 struct cdrom_volctrl *volctrl = (struct cdrom_volctrl *) arg;
2995 params[0] = SONY_SD_AUDIO_VOLUME;
2996 params[1] = volctrl->channel0;
2997 params[2] = volctrl->channel1;
2998 return do_sony_cd_cmd_chk("VOLCTRL",SONY_SET_DRIVE_PARAM_CMD, params, 3, res_reg, &res_size);
3000 case CDROMSUBCHNL: /* Get subchannel info */
3001 return sony_get_subchnl_info((struct cdrom_subchnl *)arg);
3003 default:
3004 return -EINVAL;
3008 static int scd_dev_ioctl(struct cdrom_device_info *cdi,
3009 unsigned int cmd,
3010 unsigned long arg)
3012 int i;
3014 switch (cmd)
3016 case CDROMREADAUDIO: /* Read 2352 byte audio tracks and 2340 byte
3017 raw data tracks. */
3019 struct cdrom_read_audio ra;
3022 sony_get_toc();
3023 if (!sony_toc_read)
3025 return -EIO;
3028 if(copy_from_user(&ra, (char *) arg, sizeof(ra)))
3029 return -EFAULT;
3031 if (ra.nframes == 0)
3033 return 0;
3036 i=verify_area(VERIFY_WRITE, ra.buf, CD_FRAMESIZE_RAW * ra.nframes);
3037 if(i<0)
3038 return i;
3040 if (ra.addr_format == CDROM_LBA)
3042 if ( (ra.addr.lba >= sony_toc.lead_out_start_lba)
3043 || (ra.addr.lba + ra.nframes >= sony_toc.lead_out_start_lba))
3045 return -EINVAL;
3048 else if (ra.addr_format == CDROM_MSF)
3050 if ( (ra.addr.msf.minute >= 75)
3051 || (ra.addr.msf.second >= 60)
3052 || (ra.addr.msf.frame >= 75))
3054 return -EINVAL;
3057 ra.addr.lba = ( (ra.addr.msf.minute * 4500)
3058 + (ra.addr.msf.second * 75)
3059 + ra.addr.msf.frame);
3060 if ( (ra.addr.lba >= sony_toc.lead_out_start_lba)
3061 || (ra.addr.lba + ra.nframes >= sony_toc.lead_out_start_lba))
3063 return -EINVAL;
3066 /* I know, this can go negative on an unsigned. However,
3067 the first thing done to the data is to add this value,
3068 so this should compensate and allow direct msf access. */
3069 ra.addr.lba -= LOG_START_OFFSET;
3071 else
3073 return -EINVAL;
3076 return(read_audio(&ra));
3078 return 0;
3079 break;
3081 default:
3082 return -EINVAL;
3086 static int scd_spinup(void)
3088 unsigned char res_reg[12];
3089 unsigned int res_size;
3090 int num_spin_ups;
3092 num_spin_ups = 0;
3094 respinup_on_open:
3095 do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg, &res_size);
3097 /* The drive sometimes returns error 0. I don't know why, but ignore
3098 it. It seems to mean the drive has already done the operation. */
3099 if ((res_size < 2) || ((res_reg[0] != 0) && (res_reg[1] != 0)))
3101 printk("Sony CDROM %s error (scd_open, spin up)\n", translate_error(res_reg[1]));
3102 return 1;
3105 do_sony_cd_cmd(SONY_READ_TOC_CMD, NULL, 0, res_reg, &res_size);
3107 /* The drive sometimes returns error 0. I don't know why, but ignore
3108 it. It seems to mean the drive has already done the operation. */
3109 if ((res_size < 2) || ((res_reg[0] != 0) && (res_reg[1] != 0)))
3111 /* If the drive is already playing, it's ok. */
3112 if ((res_reg[1] == SONY_AUDIO_PLAYING_ERR) || (res_reg[1] == 0))
3114 return 0;
3117 /* If the drive says it is not spun up (even though we just did it!)
3118 then retry the operation at least a few times. */
3119 if ( (res_reg[1] == SONY_NOT_SPIN_ERR)
3120 && (num_spin_ups < MAX_CDU31A_RETRIES))
3122 num_spin_ups++;
3123 goto respinup_on_open;
3126 printk("Sony CDROM error %s (scd_open, read toc)\n", translate_error(res_reg[1]));
3127 do_sony_cd_cmd(SONY_SPIN_DOWN_CMD, NULL, 0, res_reg, &res_size);
3128 return 1;
3130 return 0;
3134 * Open the drive for operations. Spin the drive up and read the table of
3135 * contents if these have not already been done.
3137 static int
3138 scd_open(struct cdrom_device_info *cdi, int openmode)
3140 unsigned char res_reg[12];
3141 unsigned int res_size;
3142 unsigned char params[2];
3144 MOD_INC_USE_COUNT;
3145 if (sony_usage == 0)
3147 if (scd_spinup() != 0) {
3148 MOD_DEC_USE_COUNT;
3149 return -EIO;
3151 sony_get_toc();
3152 if (!sony_toc_read)
3154 do_sony_cd_cmd(SONY_SPIN_DOWN_CMD, NULL, 0, res_reg, &res_size);
3155 MOD_DEC_USE_COUNT;
3156 return -EIO;
3159 /* For XA on the CDU31A only, we have to do special reads.
3160 The CDU33A handles XA automagically. */
3161 /* if ( (sony_toc.disk_type == SONY_XA_DISK_TYPE) */
3162 if ( (sony_toc.disk_type != 0x00)
3163 && (!is_double_speed))
3165 params[0] = SONY_SD_DECODE_PARAM;
3166 params[1] = 0x07;
3167 do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
3168 params,
3170 res_reg,
3171 &res_size);
3172 if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20))
3174 printk("CDU31A: Unable to set XA params: 0x%2.2x\n", res_reg[1]);
3176 sony_xa_mode = 1;
3178 /* A non-XA disk. Set the parms back if necessary. */
3179 else if (sony_xa_mode)
3181 params[0] = SONY_SD_DECODE_PARAM;
3182 params[1] = 0x0f;
3183 do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
3184 params,
3186 res_reg,
3187 &res_size);
3188 if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20))
3190 printk("CDU31A: Unable to reset XA params: 0x%2.2x\n", res_reg[1]);
3192 sony_xa_mode = 0;
3195 sony_spun_up = 1;
3198 sony_usage++;
3200 return 0;
3205 * Close the drive. Spin it down if no task is using it. The spin
3206 * down will fail if playing audio, so audio play is OK.
3208 static void
3209 scd_release(struct cdrom_device_info *cdi)
3211 if (sony_usage == 1)
3213 unsigned char res_reg[12];
3214 unsigned int res_size;
3216 do_sony_cd_cmd(SONY_SPIN_DOWN_CMD, NULL, 0, res_reg, &res_size);
3218 sony_spun_up = 0;
3220 sony_usage--;
3221 MOD_DEC_USE_COUNT;
3224 static struct cdrom_device_ops scd_dops = {
3225 scd_open, /* open */
3226 scd_release, /* release */
3227 scd_drive_status, /* drive status */
3228 scd_media_changed, /* media changed */
3229 scd_tray_move, /* tray move */
3230 scd_lock_door, /* lock door */
3231 scd_select_speed, /* select speed */
3232 NULL, /* select disc */
3233 scd_get_last_session, /* get last session */
3234 scd_get_mcn, /* get universal product code */
3235 scd_reset, /* hard reset */
3236 scd_audio_ioctl, /* audio ioctl */
3237 scd_dev_ioctl, /* device-specific ioctl */
3238 CDC_OPEN_TRAY | CDC_CLOSE_TRAY | CDC_LOCK | CDC_SELECT_SPEED | CDC_MULTI_SESSION |
3239 CDC_MULTI_SESSION | CDC_MCN | CDC_MEDIA_CHANGED | CDC_PLAY_AUDIO |
3240 CDC_RESET | CDC_IOCTLS | CDC_DRIVE_STATUS, /* capability */
3241 1, /* number of minor devices */
3244 static struct cdrom_device_info scd_info = {
3245 &scd_dops, /* device operations */
3246 NULL, /* link */
3247 NULL, /* handle */
3248 0, /* devfs */
3249 0, /* dev */
3250 0, /* mask */
3251 2, /* maximum speed */
3252 1, /* number of discs */
3253 0, /* options, not owned */
3254 0, /* mc_flags, not owned */
3255 0 /* use count, not owned */
3258 /* The different types of disc loading mechanisms supported */
3259 static const char *load_mech[] __initdata = { "caddy", "tray", "pop-up", "unknown" };
3261 static void __init
3262 get_drive_configuration(unsigned short base_io,
3263 unsigned char res_reg[],
3264 unsigned int *res_size)
3266 int retry_count;
3269 /* Set the base address */
3270 cdu31a_port = base_io;
3272 /* Set up all the register locations */
3273 sony_cd_cmd_reg = cdu31a_port + SONY_CMD_REG_OFFSET;
3274 sony_cd_param_reg = cdu31a_port + SONY_PARAM_REG_OFFSET;
3275 sony_cd_write_reg = cdu31a_port + SONY_WRITE_REG_OFFSET;
3276 sony_cd_control_reg = cdu31a_port + SONY_CONTROL_REG_OFFSET;
3277 sony_cd_status_reg = cdu31a_port + SONY_STATUS_REG_OFFSET;
3278 sony_cd_result_reg = cdu31a_port + SONY_RESULT_REG_OFFSET;
3279 sony_cd_read_reg = cdu31a_port + SONY_READ_REG_OFFSET;
3280 sony_cd_fifost_reg = cdu31a_port + SONY_FIFOST_REG_OFFSET;
3283 * Check to see if anything exists at the status register location.
3284 * I don't know if this is a good way to check, but it seems to work
3285 * ok for me.
3287 if (read_status_register() != 0xff)
3290 * Reset the drive and wait for attention from it (to say it's reset).
3291 * If you don't wait, the next operation will probably fail.
3293 reset_drive();
3294 retry_count = jiffies + SONY_RESET_TIMEOUT;
3295 while (time_before(jiffies, retry_count) && (!is_attention()))
3297 sony_sleep();
3300 #if 0
3301 /* If attention is never seen probably not a CDU31a present */
3302 if (!is_attention())
3304 res_reg[0] = 0x20;
3305 return;
3307 #endif
3310 * Get the drive configuration.
3312 do_sony_cd_cmd(SONY_REQ_DRIVE_CONFIG_CMD,
3313 NULL,
3315 (unsigned char *) res_reg,
3316 res_size);
3317 return;
3320 /* Return an error */
3321 res_reg[0] = 0x20;
3324 #ifndef MODULE
3326 * Set up base I/O and interrupts, called from main.c.
3330 static int __init cdu31a_setup(char *strings)
3332 int ints[4];
3334 (void)get_options(strings, ARRAY_SIZE(ints), ints);
3336 if (ints[0] > 0)
3338 cdu31a_port = ints[1];
3340 if (ints[0] > 1)
3342 cdu31a_irq = ints[2];
3344 if ((strings != NULL) && (*strings != '\0'))
3346 if (strcmp(strings, "PAS") == 0)
3348 sony_pas_init = 1;
3350 else
3352 printk("CDU31A: Unknown interface type: %s\n", strings);
3356 return 1;
3359 __setup("cdu31a=", cdu31a_setup);
3361 #endif
3363 static int cdu31a_block_size;
3366 * Initialize the driver.
3368 int __init
3369 cdu31a_init(void)
3371 struct s_sony_drive_config drive_config;
3372 unsigned int res_size;
3373 char msg[255];
3374 char buf[40];
3375 int i;
3376 int drive_found;
3377 int tmp_irq;
3381 * According to Alex Freed (freed@europa.orion.adobe.com), this is
3382 * required for the Fusion CD-16 package. If the sound driver is
3383 * loaded, it should work fine, but just in case...
3385 * The following turn on the CD-ROM interface for a Fusion CD-16.
3387 if (sony_pas_init)
3389 outb(0xbc, 0x9a01);
3390 outb(0xe2, 0x9a01);
3393 drive_found = 0;
3395 /* Setting the base I/O address to 0xffff will disable it. */
3396 if (cdu31a_port == 0xffff)
3399 else if (cdu31a_port != 0)
3401 tmp_irq = cdu31a_irq; /* Need IRQ 0 because we can't sleep here. */
3402 cdu31a_irq = 0;
3404 get_drive_configuration(cdu31a_port,
3405 drive_config.exec_status,
3406 &res_size);
3407 if ((res_size > 2) && ((drive_config.exec_status[0] & 0xf0) == 0x00))
3409 drive_found = 1;
3412 cdu31a_irq = tmp_irq;
3414 else
3416 cdu31a_irq = 0;
3417 i = 0;
3418 while ( (cdu31a_addresses[i].base != 0)
3419 && (!drive_found))
3421 if (check_region(cdu31a_addresses[i].base, 4)) {
3422 i++;
3423 continue;
3425 get_drive_configuration(cdu31a_addresses[i].base,
3426 drive_config.exec_status,
3427 &res_size);
3428 if ((res_size > 2) && ((drive_config.exec_status[0] & 0xf0) == 0x00))
3430 drive_found = 1;
3431 cdu31a_irq = cdu31a_addresses[i].int_num;
3433 else
3435 i++;
3440 if (drive_found)
3442 int deficiency=0;
3444 request_region(cdu31a_port, 4,"cdu31a");
3446 if (devfs_register_blkdev(MAJOR_NR,"cdu31a",&cdrom_fops))
3448 printk("Unable to get major %d for CDU-31a\n", MAJOR_NR);
3449 goto errout2;
3452 if (SONY_HWC_DOUBLE_SPEED(drive_config))
3454 is_double_speed = 1;
3457 tmp_irq = cdu31a_irq; /* Need IRQ 0 because we can't sleep here. */
3458 cdu31a_irq = 0;
3460 set_drive_params(sony_speed);
3462 cdu31a_irq = tmp_irq;
3464 if (cdu31a_irq > 0)
3466 if (request_irq(cdu31a_irq, cdu31a_interrupt, SA_INTERRUPT, "cdu31a", NULL))
3468 printk("Unable to grab IRQ%d for the CDU31A driver\n", cdu31a_irq);
3469 cdu31a_irq = 0;
3473 sprintf(msg, "Sony I/F CDROM : %8.8s %16.16s %8.8s\n",
3474 drive_config.vendor_id,
3475 drive_config.product_id,
3476 drive_config.product_rev_level);
3477 sprintf(buf, " Capabilities: %s",
3478 load_mech[SONY_HWC_GET_LOAD_MECH(drive_config)]);
3479 strcat(msg, buf);
3480 if (SONY_HWC_AUDIO_PLAYBACK(drive_config))
3482 strcat(msg, ", audio");
3483 } else
3484 deficiency |= CDC_PLAY_AUDIO;
3485 if (SONY_HWC_EJECT(drive_config))
3487 strcat(msg, ", eject");
3488 } else
3489 deficiency |= CDC_OPEN_TRAY;
3490 if (SONY_HWC_LED_SUPPORT(drive_config))
3492 strcat(msg, ", LED");
3494 if (SONY_HWC_ELECTRIC_VOLUME(drive_config))
3496 strcat(msg, ", elec. Vol");
3498 if (SONY_HWC_ELECTRIC_VOLUME_CTL(drive_config))
3500 strcat(msg, ", sep. Vol");
3502 if (is_double_speed)
3504 strcat(msg, ", double speed");
3505 } else
3506 deficiency |= CDC_SELECT_SPEED;
3507 if (cdu31a_irq > 0)
3509 sprintf(buf, ", irq %d", cdu31a_irq);
3510 strcat(msg, buf);
3512 strcat(msg, "\n");
3514 is_a_cdu31a = strcmp("CD-ROM CDU31A", drive_config.product_id) == 0;
3516 blk_init_queue(BLK_DEFAULT_QUEUE(MAJOR_NR), DEVICE_REQUEST);
3517 read_ahead[MAJOR_NR] = CDU31A_READAHEAD;
3518 cdu31a_block_size = 1024; /* 1kB default block size */
3519 /* use 'mount -o block=2048' */
3520 blksize_size[MAJOR_NR] = &cdu31a_block_size;
3522 init_timer(&cdu31a_abort_timer);
3523 cdu31a_abort_timer.function = handle_abort_timeout;
3525 scd_info.dev = MKDEV(MAJOR_NR,0);
3526 scd_info.mask = deficiency;
3527 strncpy(scd_info.name, "cdu31a", sizeof(scd_info.name));
3529 if (register_cdrom(&scd_info))
3531 goto errout0;
3536 disk_changed = 1;
3538 if (drive_found)
3540 return(0);
3542 else
3544 goto errout3;
3546 errout0:
3547 printk("Unable to register CDU-31a with Uniform cdrom driver\n");
3548 blk_cleanup_queue(BLK_DEFAULT_QUEUE(MAJOR_NR));
3549 if (devfs_unregister_blkdev(MAJOR_NR, "cdu31a"))
3551 printk("Can't unregister block device for cdu31a\n");
3553 errout2:
3554 release_region(cdu31a_port,4);
3555 errout3:
3556 return -EIO;
3560 void __exit
3561 cdu31a_exit(void)
3563 if (unregister_cdrom(&scd_info))
3565 printk("Can't unregister cdu31a from Uniform cdrom driver\n");
3566 return;
3568 if ((devfs_unregister_blkdev(MAJOR_NR, "cdu31a") == -EINVAL))
3570 printk("Can't unregister cdu31a\n");
3571 return;
3574 blk_cleanup_queue(BLK_DEFAULT_QUEUE(MAJOR_NR));
3576 if (cdu31a_irq > 0)
3577 free_irq(cdu31a_irq, NULL);
3579 release_region(cdu31a_port,4);
3580 printk(KERN_INFO "cdu31a module released.\n");
3583 #ifdef MODULE
3584 module_init(cdu31a_init);
3585 #endif
3586 module_exit(cdu31a_exit);