2 * Sony CDU-31A CDROM interface device driver.
4 * Corey Minyard (minyard@wf-rch.cirr.com)
8 * See Documentation/cdrom/cdu31a for additional details about this driver.
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
34 * retry_count = jiffies+ SONY_JIFFIES_TIMEOUT;
35 * while (time_before(jiffies, retry_count) && (! <some condition to wait for))
37 * while (handle_sony_cd_attention())
42 * if (the condition not met)
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.
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.
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!
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.
112 * CDs with form1 and form2 sectors cause problems
113 * with current read-ahead strategy.
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
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>
173 #include <asm/uaccess.h>
176 #include <linux/cdrom.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
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
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
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 */
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
,
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
268 static int sony_pas_init
= 0; /* Initialize the Pro-Audio
271 static struct s_sony_session_toc single_toc
; /* Holds the
275 static struct s_all_sessions_toc sony_toc
; /* entries gathered from all
278 static int sony_toc_read
= 0; /* Has the TOC been read for
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
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
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
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
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.
352 scd_disk_change(kdev_t full_dev
)
356 retval
= disk_changed
;
363 * Uniform cdrom interface function
364 * report back, if disc has changed from time of last request.
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 */
382 if (scd_spinup() == 0) {
385 return sony_spun_up
? CDS_DISC_OK
: CDS_DRIVE_NOT_READY
;
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
);
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.
417 current
->state
= TASK_INTERRUPTIBLE
;
420 else /* Interrupt driven */
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.
438 return((inb(sony_cd_status_reg
) & SONY_ATTN_BIT
) != 0);
444 return((inb(sony_cd_status_reg
) & SONY_BUSY_BIT
) != 0);
450 return((inb(sony_cd_status_reg
) & SONY_DATA_RDY_BIT
) != 0);
454 is_data_requested(void)
456 return((inb(sony_cd_status_reg
) & SONY_DATA_REQUEST_BIT
) != 0);
460 is_result_ready(void)
462 return((inb(sony_cd_status_reg
) & SONY_RES_RDY_BIT
) != 0);
466 is_param_write_rdy(void)
468 return((inb(sony_cd_fifost_reg
) & SONY_PARAM_WRITE_RDY_BIT
) != 0);
472 is_result_reg_not_empty(void)
474 return((inb(sony_cd_fifost_reg
) & SONY_RES_REG_NOT_EMP_BIT
) != 0);
480 curr_control_reg
= 0;
481 readahead_dataleft
= 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
)
496 retry_count
= jiffies
+ SONY_RESET_TIMEOUT
;
497 while (time_before(jiffies
, retry_count
) && (!is_attention()))
506 clear_attention(void)
508 outb(curr_control_reg
| SONY_ATTN_CLR_BIT
, sony_cd_control_reg
);
512 clear_result_ready(void)
514 outb(curr_control_reg
| SONY_RES_RDY_CLR_BIT
, sony_cd_control_reg
);
518 clear_data_ready(void)
520 outb(curr_control_reg
| SONY_DATA_RDY_CLR_BIT
, sony_cd_control_reg
);
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
));
548 write_param(unsigned char param
)
550 outb(param
, sony_cd_param_reg
);
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
);
561 cdu31a_interrupt(int irq
, void *dev_id
, struct pt_regs
*regs
)
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
570 /* Clear out the result registers. */
571 while (is_result_reg_not_empty())
573 val
= read_result_register();
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
);
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];
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
);
659 * Set the drive parameters so the drive will auto-spin-up when a
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
,
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
692 do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD
,
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
)
712 sony_speed
= speed
- 1;
714 set_drive_params(sony_speed
);
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 */
731 set_drive_params(sony_speed
);
736 * This code will reset the drive and attempt to restore sane parameters.
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");
748 retry_count
= jiffies
+ SONY_RESET_TIMEOUT
;
749 while (time_before(jiffies
, retry_count
) && (!is_attention()))
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
);
767 * This routine writes data to the parameter register. Since this should
768 * happen fairly fast, it is polled with no OS waits between.
771 write_params(unsigned char *params
,
774 unsigned int retry_count
;
777 retry_count
= SONY_READY_RETRIES
;
778 while ((retry_count
> 0) && (!is_param_write_rdy()))
782 if (!is_param_write_rdy())
787 while (num_params
> 0)
789 write_param(*params
);
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
805 get_result(unsigned char *result_buffer
,
806 unsigned int *result_size
)
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()))))
821 while (handle_sony_cd_attention())
824 if (is_busy() || (!(is_result_ready())))
827 printk("CDU31A timeout out %d\n", __LINE__
);
829 result_buffer
[0] = 0x20;
830 result_buffer
[1] = SONY_TIMEOUT_OP_ERR
;
836 * Get the first two bytes. This determines what else needs
839 clear_result_ready();
840 a
= read_result_register();
844 /* Check for block error status result. */
845 if ((a
& 0xf0) == 0x50)
851 b
= read_result_register();
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)
870 *result_buffer
= read_result_register();
878 retry_count
= SONY_READY_RETRIES
;
879 while ((retry_count
> 0) && (!is_result_ready()))
883 if (!is_result_ready())
886 printk("CDU31A timeout out %d\n", __LINE__
);
888 result_buffer
[0] = 0x20;
889 result_buffer
[1] = SONY_TIMEOUT_OP_ERR
;
894 clear_result_ready();
898 *result_buffer
= read_result_register();
907 retry_count
= SONY_READY_RETRIES
;
908 while ((retry_count
> 0) && (!is_result_ready()))
912 if (!is_result_ready())
915 printk("CDU31A timeout out %d\n", __LINE__
);
917 result_buffer
[0] = 0x20;
918 result_buffer
[1] = SONY_TIMEOUT_OP_ERR
;
927 *result_buffer
= read_result_register();
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.
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
;
955 if (current
!= has_cd_task
) /* Allow recursive calls to this routine */
959 interruptible_sleep_on(&sony_wait
);
960 if (signal_pending(current
))
962 result_buffer
[0] = 0x20;
963 result_buffer
[1] = SONY_SIGNAL_OP_ERR
;
965 restore_flags(flags
);
970 has_cd_task
= current
;
981 while (handle_sony_cd_attention())
986 retry_count
= jiffies
+ SONY_JIFFIES_TIMEOUT
;
987 while (time_before(jiffies
, retry_count
) && (is_busy()))
991 while (handle_sony_cd_attention())
997 printk("CDU31A timeout out %d\n", __LINE__
);
999 result_buffer
[0] = 0x20;
1000 result_buffer
[1] = SONY_TIMEOUT_OP_ERR
;
1005 clear_result_ready();
1008 write_params(params
, num_params
);
1011 get_result(result_buffer
, result_size
);
1014 if ( ((result_buffer
[0] & 0xf0) == 0x20)
1015 && (num_retries
< MAX_CDU31A_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
)
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
1044 handle_sony_cd_attention(void)
1046 unsigned char atten_code
;
1047 static int num_consecutive_attentions
= 0;
1052 printk("Entering handle_sony_cd_attention\n");
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;
1062 printk("Leaving handle_sony_cd_attention at %d\n", __LINE__
);
1068 atten_code
= read_result_register();
1072 /* Someone changed the CD. Mark it as changed */
1073 case SONY_MECH_LOADED_ATTN
:
1076 sony_audio_status
= CDROM_AUDIO_NO_STATUS
;
1077 sony_blocks_left
= 0;
1080 case SONY_SPIN_DOWN_COMPLETE_ATTN
:
1081 /* Mark the disk as spun down. */
1085 case SONY_AUDIO_PLAY_DONE_ATTN
:
1086 sony_audio_status
= CDROM_AUDIO_COMPLETED
;
1090 case SONY_EJECT_PUSHED_ATTN
:
1093 sony_audio_status
= CDROM_AUDIO_INVALID
;
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
;
1105 num_consecutive_attentions
++;
1107 printk("Leaving handle_sony_cd_attention at %d\n", __LINE__
);
1111 else if (abort_read_started
)
1113 while (is_result_reg_not_empty())
1115 val
= read_result_register();
1118 clear_result_ready();
1119 /* Clear out the data */
1120 while (is_data_requested())
1122 val
= read_data_register();
1124 abort_read_started
= 0;
1126 printk("Leaving handle_sony_cd_attention at %d\n", __LINE__
);
1131 num_consecutive_attentions
= 0;
1133 printk("Leaving handle_sony_cd_attention at %d\n", __LINE__
);
1139 /* Convert from an integer 0-99 to BCD */
1140 static inline unsigned int
1141 int_to_bcd(unsigned int val
)
1146 retval
= (val
/ 10) << 4;
1147 retval
= retval
| val
% 10;
1152 /* Convert from BCD to an integer from 0-99 */
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.
1165 log_to_msf(unsigned int log
, unsigned char *msf
)
1167 log
= log
+ LOG_START_OFFSET
;
1168 msf
[0] = int_to_bcd(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.
1179 msf_to_log(unsigned char *msf
)
1186 log
+= msf
[0] * 4500;
1187 log
= log
- LOG_START_OFFSET
;
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.
1198 size_to_buf(unsigned int size
,
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
1214 start_request(unsigned int sector
,
1216 int read_nsect_only
)
1218 unsigned char params
[6];
1219 unsigned int read_size
;
1220 unsigned int retry_count
;
1224 printk("Entering start_request\n");
1226 log_to_msf(sector
, params
);
1227 /* If requested, read exactly what was asked. */
1228 if (read_nsect_only
)
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. */
1243 read_size
= CDU31A_READAHEAD
/ 4;
1245 size_to_buf(read_size
, ¶ms
[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()))
1259 while (handle_sony_cd_attention())
1265 printk("CDU31A: Timeout while waiting to issue command\n");
1267 printk("Leaving start_request at %d\n", __LINE__
);
1273 /* Issue the command */
1274 clear_result_ready();
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;
1285 printk("Leaving start_request at %d\n", __LINE__
);
1290 printk("Leaving start_request at %d\n", __LINE__
);
1294 /* Abort a pending read operation. Clear all the drive status and
1295 readahead variables. */
1299 unsigned char result_reg
[2];
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",
1312 while (is_result_reg_not_empty())
1314 val
= read_result_register();
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;
1329 /* Called when the timer times out. This will abort the
1330 pending read operation. */
1332 handle_abort_timeout(unsigned long data
)
1334 unsigned long flags
;
1337 printk("Entering handle_abort_timeout\n");
1341 /* If it is in use, ignore it. */
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
1348 clear_result_ready();
1350 write_cmd(SONY_ABORT_CMD
);
1352 sony_blocks_left
= 0;
1353 readahead_dataleft
= 0;
1355 abort_read_started
= 1;
1357 restore_flags(flags
);
1359 printk("Leaving handle_abort_timeout\n");
1363 /* Actually get data and status from the drive. */
1365 input_data(char *buffer
,
1366 unsigned int bytesleft
,
1367 unsigned int nblocks
,
1368 unsigned int offset
,
1372 volatile unsigned char val
;
1376 printk("Entering input_data\n");
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. */
1382 for(i
=0; i
<CD_XA_HEAD
; i
++)
1384 val
= read_data_register();
1390 if (bytesleft
== 2048) /* 2048 byte direct buffer transfer */
1392 insb(sony_cd_read_reg
, buffer
, 2048);
1393 readahead_dataleft
= 0;
1397 /* If the input read did not align with the beginning of the block,
1398 skip the necessary bytes. */
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
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. */
1421 for(i
=0; i
<CD_XA_TAIL
; i
++)
1423 val
= read_data_register();
1427 printk("Leaving input_data at %d\n", __LINE__
);
1431 /* read data from the drive. Note the nsect must be <= 4. */
1433 read_data_block(char *buffer
,
1435 unsigned int nblocks
,
1436 unsigned char res_reg
[],
1439 unsigned int retry_count
;
1440 unsigned int bytesleft
;
1441 unsigned int offset
;
1446 printk("Entering read_data_block\n");
1452 bytesleft
= nblocks
* 512;
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;
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. */
1477 readahead_buffer
+ (2048 - readahead_dataleft
),
1478 readahead_dataleft
);
1479 readahead_dataleft
= 0;
1480 bytesleft
-= readahead_dataleft
;
1481 offset
+= readahead_dataleft
;
1485 /* The readahead will fill the whole buffer, get the data
1488 readahead_buffer
+ (2048 - readahead_dataleft
),
1490 readahead_dataleft
-= bytesleft
;
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. */
1500 res_reg
[1] = SONY_BAD_DATA_ERR
;
1504 if (readahead_dataleft
== 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
);
1515 printk("Leaving read_data_block at %d\n", __LINE__
);
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())
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",
1540 res_reg
[1] = SONY_BAD_DATA_ERR
;
1548 printk("CDU31A timeout out %d\n", __LINE__
);
1551 res_reg
[1] = SONY_TIMEOUT_OP_ERR
;
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())
1570 if (!is_result_ready())
1573 printk("CDU31A timeout out %d\n", __LINE__
);
1576 res_reg
[1] = SONY_TIMEOUT_OP_ERR
;
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
1599 res_reg
[1] = SONY_BAD_DATA_ERR
;
1605 printk("CDU31A: Data block error: 0x%x\n", res_reg
[0]);
1607 res_reg
[1] = SONY_BAD_DATA_ERR
;
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)
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]);
1631 res_reg
[1] = SONY_BAD_DATA_ERR
;
1637 printk("Leaving read_data_block at %d\n", __LINE__
);
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.
1650 do_cdu31a_request(request_queue_t
* q
)
1654 unsigned char res_reg
[12];
1655 unsigned int res_size
;
1657 unsigned long flags
;
1661 printk("Entering do_cdu31a_request\n");
1665 * Make sure no one else is using the driver; wait for them
1666 * to finish if it is so.
1672 interruptible_sleep_on(&sony_wait
);
1673 if (signal_pending(current
))
1675 restore_flags(flags
);
1676 if (!QUEUE_EMPTY
&& CURRENT
->rq_status
!= RQ_INACTIVE
)
1680 restore_flags(flags
);
1682 printk("Leaving do_cdu31a_request at %d\n", __LINE__
);
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. */
1697 spin_unlock_irq(&io_request_lock
);
1699 /* Make sure the timer is cancelled. */
1700 del_timer(&cdu31a_abort_timer
);
1704 cdu31a_request_startover
:
1706 * The beginning here is stolen from the hard disk driver. I hope
1709 if (QUEUE_EMPTY
|| CURRENT
->rq_status
== RQ_INACTIVE
)
1711 goto end_do_cdu31a_request
;
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");
1728 if (!buffer_locked(CURRENT
->bh
))
1730 panic(DEVICE_NAME
": block not locked");
1734 block
= CURRENT
->sector
;
1735 nblock
= CURRENT
->nr_sectors
;
1739 printk("CDU31A: TOC not read\n");
1741 goto cdu31a_request_startover
;
1744 switch(CURRENT
->cmd
)
1748 * If the block address is invalid or the request goes beyond the end of
1749 * the media, return an error.
1752 if ((block
/ 4) < sony_toc
.start_track_lba
)
1754 printk("CDU31A: Request before beginning of media\n");
1756 goto cdu31a_request_startover
;
1759 if ((block
/ 4) >= sony_toc
.lead_out_start_lba
)
1761 printk("CDU31A: Request past end of media\n");
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");
1769 goto cdu31a_request_startover
;
1775 while (handle_sony_cd_attention())
1780 printk("CDU31A: TOC not read\n");
1782 goto cdu31a_request_startover
;
1785 /* If no data is left to be read from the drive, start the
1787 if (sony_blocks_left
== 0)
1789 if (start_request(block
/ 4, CDU31A_READAHEAD
/ 4, 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
1798 else if (block
!= sony_next_block
)
1801 printk("CDU31A Warning: Read for block %d, expected %d\n",
1808 printk("CDU31A: TOC not read\n");
1810 goto cdu31a_request_startover
;
1812 if (start_request(block
/ 4, CDU31A_READAHEAD
/ 4, 0))
1814 printk("CDU31a: start request failed\n");
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
)
1826 goto cdu31a_request_startover
;
1830 if (res_reg
[1] == SONY_NOT_SPIN_ERR
)
1832 do_sony_cd_cmd(SONY_SPIN_UP_CMD
, NULL
, 0, res_reg
, &res_size
);
1836 printk("CDU31A: %s error for block %d, nblock %d\n", translate_error(res_reg
[1]), block
, nblock
);
1838 goto try_read_again
;
1851 panic("CDU31A: Unknown cmd");
1855 end_do_cdu31a_request
:
1856 spin_lock_irq(&io_request_lock
);
1858 /* After finished, cancel any pending operations. */
1861 /* Start a timer to time out after a while to disable
1863 cdu31a_abort_timer
.expires
= jiffies
+ 2*HZ
; /* Wait 2 seconds */
1864 add_timer(&cdu31a_abort_timer
);
1869 wake_up_interruptible(&sony_wait
);
1870 restore_flags(flags
);
1872 printk("Leaving do_cdu31a_request at %d\n", __LINE__
);
1878 * Read the table of contents from the drive and set up TOC if
1884 unsigned char res_reg
[2];
1885 unsigned int res_size
;
1886 unsigned char parms
[1];
1889 int totaltracks
= 0;
1894 printk("Entering sony_get_toc\n");
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
))
1922 goto respinup_on_gettoc
;
1925 printk("cdu31a: Error reading TOC: %x %s\n",
1926 res_reg
[0], translate_error(res_reg
[1]));
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.
1938 memset(&sony_toc
, 0x0e, sizeof(sony_toc
));
1939 memset(&single_toc
, 0x0f, sizeof(single_toc
));
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 */
1948 printk("cdu31a: Trying session %d\n", session
);
1951 do_sony_cd_cmd(SONY_READ_TOC_SPEC_CMD
,
1958 printk("%2.2x %2.2x\n", res_reg
[0], res_reg
[1]);
1961 if ((res_size
< 2) || ((res_reg
[0] & 0xf0) == 0x20))
1963 /* An error reading the TOC, this must be past the last session. */
1965 printk("Yikes! Couldn't read any sessions!");
1969 printk("Reading session %d\n", session
);
1973 do_sony_cd_cmd(SONY_REQ_TOC_DATA_SPEC_CMD
,
1976 (unsigned char *) &single_toc
,
1978 if ((res_size
< 2) || ((single_toc
.exec_status
[0] & 0xf0) == 0x20))
1980 printk("cdu31a: Error reading session %d: %x %s\n",
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
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]);
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,
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,
2093 if (single_toc
.pointb2
!= 0xb2)
2095 memmove(((char *) &single_toc
) + 45,
2096 ((char *) &single_toc
) + 36,
2100 if (single_toc
.pointb3
!= 0xb3)
2102 memmove(((char *) &single_toc
) + 54,
2103 ((char *) &single_toc
) + 45,
2107 if (single_toc
.pointb4
!= 0xb4)
2109 memmove(((char *) &single_toc
) + 63,
2110 ((char *) &single_toc
) + 54,
2114 if (single_toc
.pointc0
!= 0xc0)
2116 memmove(((char *) &single_toc
) + 72,
2117 ((char *) &single_toc
) + 63,
2123 printk("start track lba %u, leadout start lba %u\n",
2124 single_toc
.start_track_lba
, single_toc
.lead_out_start_lba
);
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
);
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;
2157 /* gather track entries from this session */
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]);
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
2186 sony_toc
.disk_type
= single_toc
.disk_type
;
2187 sony_toc
.sessions
= session
;
2189 /* don't believe everything :-) */
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
)
2200 /* Let's not get carried away... */
2203 printk("cdu31a: too many sessions: %d\n", 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];
2223 printk("Disk session %d, start track: %d, stop track: %d\n",
2225 single_toc
.start_track_lba
,
2226 single_toc
.lead_out_start_lba
);
2230 printk("Leaving sony_get_toc\n");
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
)
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 */;
2257 * Search for a specific track in the table of contents.
2260 find_track(int track
)
2264 for (i
= 0; i
<= sony_toc
.track_entries
; i
++)
2266 if (sony_toc
.tracks
[i
].track
== track
)
2277 * Read the subcode and put it in last_sony_subcode for future use.
2282 unsigned int res_size
;
2285 do_sony_cd_cmd(SONY_REQ_SUBCODE_ADDRESS_CMD
,
2288 (unsigned char *) &last_sony_subcode
,
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]));
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]);
2310 * Uniform cdrom interface function
2311 * return the media catalog number found on some older audio cds
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
,
2327 if ((res_size
< 2) || ((resbuffer
[0] & 0xf0) == 0x20))
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';
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.
2358 sony_get_subchnl_info(struct cdrom_subchnl
*schi
)
2360 /* Get attention stuff */
2361 while (handle_sony_cd_attention())
2370 switch (sony_audio_status
)
2372 case CDROM_AUDIO_NO_STATUS
:
2373 case CDROM_AUDIO_PLAY
:
2374 if (read_subcode() < 0)
2380 case CDROM_AUDIO_PAUSED
:
2381 case CDROM_AUDIO_COMPLETED
:
2385 case CDROM_AUDIO_NO_STATUS
:
2386 schi
->cdsc_audiostatus
= sony_audio_status
;
2390 case CDROM_AUDIO_INVALID
:
2391 case CDROM_AUDIO_ERROR
:
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
);
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. */
2425 read_audio_data(char *buffer
,
2426 unsigned char res_reg
[],
2429 unsigned int retry_count
;
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())
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)
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",
2467 res_reg
[1] = SONY_BAD_DATA_ERR
;
2475 printk("CDU31A timeout out %d\n", __LINE__
);
2478 res_reg
[1] = SONY_TIMEOUT_OP_ERR
;
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
);
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. */
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())
2511 if (!is_result_ready())
2514 printk("CDU31A timeout out %d\n", __LINE__
);
2517 res_reg
[1] = SONY_TIMEOUT_OP_ERR
;
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. */
2539 printk("CDU31A: Data block error: 0x%x\n", res_reg
[0]);
2541 res_reg
[1] = SONY_BAD_DATA_ERR
;
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]);
2552 res_reg
[1] = SONY_BAD_DATA_ERR
;
2558 /* Perform a raw data read. This will automatically detect the
2559 track type and read the proper data (audio or data). */
2561 read_audio(struct cdrom_read_audio
*ra
)
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.
2578 interruptible_sleep_on(&sony_wait
);
2579 if (signal_pending(current
))
2581 restore_flags(flags
);
2586 has_cd_task
= current
;
2587 restore_flags(flags
);
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
,
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]);
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
2613 /* start_request clears out any readahead data, so it should be safe. */
2614 if (start_request(ra
->addr
.lba
, ra
->nframes
, 1))
2617 goto exit_read_audio
;
2620 /* For every requested frame. */
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
,
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]);
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))
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
);
2672 printk("CDU31A: Error reading audio data on sector %d: %s\n",
2673 ra
->addr
.lba
+ cframe
,
2674 translate_error(res_reg
[1]));
2676 goto exit_read_audio
;
2681 copy_to_user((char *) (ra
->buf
+ (CD_FRAMESIZE_RAW
* cframe
)),
2682 (char *) readahead_buffer
,
2688 printk("CDU31A: Error reading audio data on sector %d: %s\n",
2689 ra
->addr
.lba
+ cframe
,
2690 translate_error(res_reg
[1]));
2692 goto exit_read_audio
;
2697 copy_to_user((char *) (ra
->buf
+ (CD_FRAMESIZE_RAW
* cframe
)),
2698 (char *) readahead_buffer
,
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]));
2711 goto exit_read_audio
;
2716 /* Set the drive mode back to the proper one for the disk. */
2717 params
[0] = SONY_SD_DECODE_PARAM
;
2726 do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD
,
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]);
2739 wake_up_interruptible(&sony_wait
);
2745 do_sony_cd_cmd_chk(const char *name
,
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
);
2762 * Uniform cdrom interface function
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
);
2778 if (0 == scd_spinup())
2785 * The big ugly ioctl handler.
2787 static int scd_audio_ioctl(struct cdrom_device_info
*cdi
,
2791 unsigned char res_reg
[12];
2792 unsigned int res_size
;
2793 unsigned char params
[7];
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
);
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
))
2816 /* Get the current position and save it for resuming */
2817 if (read_subcode() < 0)
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
;
2828 case CDROMRESUME
: /* Start the drive after being paused */
2829 if (sony_audio_status
!= CDROM_AUDIO_PAUSED
)
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]);
2844 if(do_sony_cd_cmd_chk("RESUME",SONY_AUDIO_PLAYBACK_CMD
, params
, 7, res_reg
, &res_size
)<0)
2846 sony_audio_status
= CDROM_AUDIO_PLAY
;
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 */
2855 params
[i
] = int_to_bcd(((unsigned char *)arg
)[i
-1]);
2858 if(do_sony_cd_cmd_chk("PLAYMSF",SONY_AUDIO_PLAYBACK_CMD
, params
, 7, res_reg
, &res_size
)<0)
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
;
2868 case CDROMREADTOCHDR
: /* Read the table of contents header */
2870 struct cdrom_tochdr
*hdr
;
2878 hdr
= (struct cdrom_tochdr
*) arg
;
2879 hdr
->cdth_trk0
= sony_toc
.first_track_num
;
2880 hdr
->cdth_trk1
= sony_toc
.last_track_num
;
2884 case CDROMREADTOCENTRY
: /* Read a given table of contents entry */
2886 struct cdrom_tocentry
*entry
;
2888 unsigned char *msf_val
= NULL
;
2896 entry
= (struct cdrom_tocentry
*) arg
;
2898 track_idx
= find_track(entry
->cdte_track
);
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);
2923 case CDROMPLAYTRKIND
: /* Play a track. This currently ignores index. */
2925 struct cdrom_ti
*ti
= (struct cdrom_ti
*) arg
;
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
))
2941 track_idx
= find_track(ti
->cdti_trk0
);
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
2954 if (ti
->cdti_trk1
>= sony_toc
.last_track_num
)
2956 track_idx
= find_track(CDROM_LEADOUT
);
2960 track_idx
= find_track(ti
->cdti_trk1
+1);
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]);
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]));
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
;
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
);
3008 static int scd_dev_ioctl(struct cdrom_device_info
*cdi
,
3016 case CDROMREADAUDIO
: /* Read 2352 byte audio tracks and 2340 byte
3019 struct cdrom_read_audio ra
;
3028 if(copy_from_user(&ra
, (char *) arg
, sizeof(ra
)))
3031 if (ra
.nframes
== 0)
3036 i
=verify_area(VERIFY_WRITE
, ra
.buf
, CD_FRAMESIZE_RAW
* ra
.nframes
);
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
))
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))
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
))
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
;
3076 return(read_audio(&ra
));
3086 static int scd_spinup(void)
3088 unsigned char res_reg
[12];
3089 unsigned int res_size
;
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]));
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))
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
))
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
);
3134 * Open the drive for operations. Spin the drive up and read the table of
3135 * contents if these have not already been done.
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];
3145 if (sony_usage
== 0)
3147 if (scd_spinup() != 0) {
3154 do_sony_cd_cmd(SONY_SPIN_DOWN_CMD
, NULL
, 0, res_reg
, &res_size
);
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
;
3167 do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD
,
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]);
3178 /* A non-XA disk. Set the parms back if necessary. */
3179 else if (sony_xa_mode
)
3181 params
[0] = SONY_SD_DECODE_PARAM
;
3183 do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD
,
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]);
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.
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
);
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 */
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" };
3262 get_drive_configuration(unsigned short base_io
,
3263 unsigned char res_reg
[],
3264 unsigned int *res_size
)
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
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.
3294 retry_count
= jiffies
+ SONY_RESET_TIMEOUT
;
3295 while (time_before(jiffies
, retry_count
) && (!is_attention()))
3301 /* If attention is never seen probably not a CDU31a present */
3302 if (!is_attention())
3310 * Get the drive configuration.
3312 do_sony_cd_cmd(SONY_REQ_DRIVE_CONFIG_CMD
,
3315 (unsigned char *) res_reg
,
3320 /* Return an error */
3326 * Set up base I/O and interrupts, called from main.c.
3330 static int __init
cdu31a_setup(char *strings
)
3334 (void)get_options(strings
, ARRAY_SIZE(ints
), ints
);
3338 cdu31a_port
= ints
[1];
3342 cdu31a_irq
= ints
[2];
3344 if ((strings
!= NULL
) && (*strings
!= '\0'))
3346 if (strcmp(strings
, "PAS") == 0)
3352 printk("CDU31A: Unknown interface type: %s\n", strings
);
3359 __setup("cdu31a=", cdu31a_setup
);
3363 static int cdu31a_block_size
;
3366 * Initialize the driver.
3371 struct s_sony_drive_config drive_config
;
3372 unsigned int res_size
;
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.
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. */
3404 get_drive_configuration(cdu31a_port
,
3405 drive_config
.exec_status
,
3407 if ((res_size
> 2) && ((drive_config
.exec_status
[0] & 0xf0) == 0x00))
3412 cdu31a_irq
= tmp_irq
;
3418 while ( (cdu31a_addresses
[i
].base
!= 0)
3421 if (check_region(cdu31a_addresses
[i
].base
, 4)) {
3425 get_drive_configuration(cdu31a_addresses
[i
].base
,
3426 drive_config
.exec_status
,
3428 if ((res_size
> 2) && ((drive_config
.exec_status
[0] & 0xf0) == 0x00))
3431 cdu31a_irq
= cdu31a_addresses
[i
].int_num
;
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
);
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. */
3460 set_drive_params(sony_speed
);
3462 cdu31a_irq
= tmp_irq
;
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
);
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
)]);
3480 if (SONY_HWC_AUDIO_PLAYBACK(drive_config
))
3482 strcat(msg
, ", audio");
3484 deficiency
|= CDC_PLAY_AUDIO
;
3485 if (SONY_HWC_EJECT(drive_config
))
3487 strcat(msg
, ", eject");
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");
3506 deficiency
|= CDC_SELECT_SPEED
;
3509 sprintf(buf
, ", irq %d", cdu31a_irq
);
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
))
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");
3554 release_region(cdu31a_port
,4);
3563 if (unregister_cdrom(&scd_info
))
3565 printk("Can't unregister cdu31a from Uniform cdrom driver\n");
3568 if ((devfs_unregister_blkdev(MAJOR_NR
, "cdu31a") == -EINVAL
))
3570 printk("Can't unregister cdu31a\n");
3574 blk_cleanup_queue(BLK_DEFAULT_QUEUE(MAJOR_NR
));
3577 free_irq(cdu31a_irq
, NULL
);
3579 release_region(cdu31a_port
,4);
3580 printk(KERN_INFO
"cdu31a module released.\n");
3584 module_init(cdu31a_init
);
3586 module_exit(cdu31a_exit
);