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/slab.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
196 unsigned short base; /* I/O Base Address */
197 short int_num; /* Interrupt Number (-1 means scan for it,
198 0 means don't use) */
199 } cdu31a_addresses[] __initdata = {
203 static int handle_sony_cd_attention(void);
204 static int read_subcode(void);
205 static void sony_get_toc(void);
206 static int scd_spinup(void);
207 /*static int scd_open(struct inode *inode, struct file *filp);*/
208 static int scd_open(struct cdrom_device_info *, int);
209 static void do_sony_cd_cmd(unsigned char cmd,
210 unsigned char *params,
211 unsigned int num_params,
212 unsigned char *result_buffer,
213 unsigned int *result_size);
214 static void size_to_buf(unsigned int size, unsigned char *buf);
216 /* Parameters for the read-ahead. */
217 static unsigned int sony_next_block; /* Next 512 byte block offset */
218 static unsigned int sony_blocks_left = 0; /* Number of 512 byte blocks left
219 in the current read command. */
222 /* The base I/O address of the Sony Interface. This is a variable (not a
223 #define) so it can be easily changed via some future ioctl() */
224 static unsigned int cdu31a_port = 0;
225 MODULE_PARM(cdu31a_port, "i");
228 * The following are I/O addresses of the various registers for the drive. The
229 * comment for the base address also applies here.
231 static volatile unsigned short sony_cd_cmd_reg;
232 static volatile unsigned short sony_cd_param_reg;
233 static volatile unsigned short sony_cd_write_reg;
234 static volatile unsigned short sony_cd_control_reg;
235 static volatile unsigned short sony_cd_status_reg;
236 static volatile unsigned short sony_cd_result_reg;
237 static volatile unsigned short sony_cd_read_reg;
238 static volatile unsigned short sony_cd_fifost_reg;
241 static int sony_spun_up = 0; /* Has the drive been spun up? */
243 static int sony_speed = 0; /* Last wanted speed */
245 static int sony_xa_mode = 0; /* Is an XA disk in the drive
246 and the drive a CDU31A? */
248 static int sony_raw_data_mode = 1; /* 1 if data tracks, 0 if audio.
249 For raw data reads. */
251 static unsigned int sony_usage = 0; /* How many processes have the
254 static int sony_pas_init = 0; /* Initialize the Pro-Audio
257 static struct s_sony_session_toc single_toc; /* Holds the
261 static struct s_all_sessions_toc sony_toc; /* entries gathered from all
264 static int sony_toc_read = 0; /* Has the TOC been read for
267 static struct s_sony_subcode last_sony_subcode; /* Points to the last
268 subcode address read */
270 static volatile int sony_inuse = 0; /* Is the drive in use? Only one operation
273 static DECLARE_WAIT_QUEUE_HEAD(sony_wait); /* Things waiting for the drive */
275 static struct task_struct *has_cd_task = NULL; /* The task that is currently
276 using the CDROM drive, or
279 static int is_double_speed = 0; /* does the drive support double speed ? */
280 static int is_a_cdu31a = 1; /* Is the drive a CDU31A? */
282 static int is_auto_eject = 1; /* Door has been locked? 1=No/0=Yes */
285 * The audio status uses the values from read subchannel data as specified
286 * in include/linux/cdrom.h.
288 static volatile int sony_audio_status = CDROM_AUDIO_NO_STATUS;
291 * The following are a hack for pausing and resuming audio play. The drive
292 * does not work as I would expect it, if you stop it then start it again,
293 * the drive seeks back to the beginning and starts over. This holds the
294 * position during a pause so a resume can restart it. It uses the
295 * audio status variable above to tell if it is paused.
297 static unsigned volatile char cur_pos_msf[3] = { 0, 0, 0 };
298 static unsigned volatile char final_pos_msf[3] = { 0, 0, 0 };
300 /* What IRQ is the drive using? 0 if none. */
301 static int cdu31a_irq = 0;
302 MODULE_PARM(cdu31a_irq, "i");
304 /* The interrupt handler will wake this queue up when it gets an
306 DECLARE_WAIT_QUEUE_HEAD(cdu31a_irq_wait);
308 static int curr_control_reg = 0; /* Current value of the control register */
310 /* A disk changed variable. When a disk change is detected, it will
311 all be set to TRUE. As the upper layers ask for disk_changed status
312 it will be cleared. */
313 static char disk_changed;
315 /* Variable for using the readahead buffer. The readahead buffer
316 is used for raw sector reads and for blocksizes that are smaller
318 static char readahead_buffer[CD_FRAMESIZE_RAW];
319 static int readahead_dataleft = 0;
320 static int readahead_bad = 0;
322 /* Used to time a short period to abort an operation after the
323 drive has been idle for a while. This keeps the light on
324 the drive from flashing for very long. */
325 static struct timer_list cdu31a_abort_timer;
327 /* Marks if the timeout has started an abort read. This is used
328 on entry to the drive to tell the code to read out the status
329 from the abort read. */
330 static int abort_read_started = 0;
334 * This routine returns 1 if the disk has been changed since the last
335 * check or 0 if it hasn't.
337 static int scd_disk_change(kdev_t full_dev)
341 retval = disk_changed;
348 * Uniform cdrom interface function
349 * report back, if disc has changed from time of last request.
351 static int scd_media_changed(struct cdrom_device_info *cdi, int disc_nr)
353 return scd_disk_change(cdi->dev);
357 * Uniform cdrom interface function
358 * report back, if drive is ready
360 static int scd_drive_status(struct cdrom_device_info *cdi, int slot_nr)
362 if (CDSL_CURRENT != slot_nr) {
363 /* we have no changer support */
366 if (scd_spinup() == 0) {
369 return sony_spun_up ? CDS_DISC_OK : CDS_DRIVE_NOT_READY;
372 static inline void enable_interrupts(void)
374 curr_control_reg |= (SONY_ATTN_INT_EN_BIT
375 | SONY_RES_RDY_INT_EN_BIT
376 | SONY_DATA_RDY_INT_EN_BIT);
377 outb(curr_control_reg, sony_cd_control_reg);
380 static inline void disable_interrupts(void)
382 curr_control_reg &= ~(SONY_ATTN_INT_EN_BIT
383 | SONY_RES_RDY_INT_EN_BIT
384 | SONY_DATA_RDY_INT_EN_BIT);
385 outb(curr_control_reg, sony_cd_control_reg);
389 * Wait a little while (used for polling the drive). If in initialization,
390 * setting a timeout doesn't work, so just loop for a while.
392 static inline void sony_sleep(void)
396 if (cdu31a_irq <= 0) {
397 current->state = TASK_INTERRUPTIBLE;
399 } else { /* Interrupt driven */
404 interruptible_sleep_on(&cdu31a_irq_wait);
405 restore_flags(flags);
411 * The following are convenience routine to read various status and set
412 * various conditions in the drive.
414 static inline int is_attention(void)
416 return ((inb(sony_cd_status_reg) & SONY_ATTN_BIT) != 0);
419 static inline int is_busy(void)
421 return ((inb(sony_cd_status_reg) & SONY_BUSY_BIT) != 0);
424 static inline int is_data_ready(void)
426 return ((inb(sony_cd_status_reg) & SONY_DATA_RDY_BIT) != 0);
429 static inline int is_data_requested(void)
431 return ((inb(sony_cd_status_reg) & SONY_DATA_REQUEST_BIT) != 0);
434 static inline int is_result_ready(void)
436 return ((inb(sony_cd_status_reg) & SONY_RES_RDY_BIT) != 0);
439 static inline int is_param_write_rdy(void)
441 return ((inb(sony_cd_fifost_reg) & SONY_PARAM_WRITE_RDY_BIT) != 0);
444 static inline int is_result_reg_not_empty(void)
446 return ((inb(sony_cd_fifost_reg) & SONY_RES_REG_NOT_EMP_BIT) != 0);
449 static inline void reset_drive(void)
451 curr_control_reg = 0;
452 readahead_dataleft = 0;
454 outb(SONY_DRIVE_RESET_BIT, sony_cd_control_reg);
458 * Uniform cdrom interface function
459 * reset drive and return when it is ready
461 static int scd_reset(struct cdrom_device_info *cdi)
467 retry_count = jiffies + SONY_RESET_TIMEOUT;
468 while (time_before(jiffies, retry_count) && (!is_attention())) {
475 static inline void clear_attention(void)
477 outb(curr_control_reg | SONY_ATTN_CLR_BIT, sony_cd_control_reg);
480 static inline void clear_result_ready(void)
482 outb(curr_control_reg | SONY_RES_RDY_CLR_BIT, sony_cd_control_reg);
485 static inline void clear_data_ready(void)
487 outb(curr_control_reg | SONY_DATA_RDY_CLR_BIT,
488 sony_cd_control_reg);
491 static inline void clear_param_reg(void)
493 outb(curr_control_reg | SONY_PARAM_CLR_BIT, sony_cd_control_reg);
496 static inline unsigned char read_status_register(void)
498 return (inb(sony_cd_status_reg));
501 static inline unsigned char read_result_register(void)
503 return (inb(sony_cd_result_reg));
506 static inline unsigned char read_data_register(void)
508 return (inb(sony_cd_read_reg));
511 static inline void write_param(unsigned char param)
513 outb(param, sony_cd_param_reg);
516 static inline void write_cmd(unsigned char cmd)
518 outb(curr_control_reg | SONY_RES_RDY_INT_EN_BIT,
519 sony_cd_control_reg);
520 outb(cmd, sony_cd_cmd_reg);
523 static void cdu31a_interrupt(int irq, void *dev_id, struct pt_regs *regs)
527 if (abort_read_started) {
528 /* We might be waiting for an abort to finish. Don't
529 disable interrupts yet, though, because we handle
531 /* Clear out the result registers. */
532 while (is_result_reg_not_empty()) {
533 val = read_result_register();
536 clear_result_ready();
538 /* Clear out the data */
539 while (is_data_requested()) {
540 val = read_data_register();
542 abort_read_started = 0;
544 /* If something was waiting, wake it up now. */
545 if (waitqueue_active(&cdu31a_irq_wait)) {
546 disable_interrupts();
547 wake_up(&cdu31a_irq_wait);
549 } else if (waitqueue_active(&cdu31a_irq_wait)) {
550 disable_interrupts();
551 wake_up(&cdu31a_irq_wait);
553 disable_interrupts();
555 ("CDU31A: Got an interrupt but nothing was waiting\n");
560 * give more verbose error messages
562 static unsigned char *translate_error(unsigned char err_code)
564 static unsigned char errbuf[80];
568 return "illegal command ";
570 return "illegal parameter ";
573 return "not loaded ";
577 return "not spinning ";
581 return "spindle servo ";
583 return "focus servo ";
585 return "eject mechanism ";
587 return "audio playing ";
589 return "emergency eject ";
594 return "frame sync ";
596 return "subcode address ";
598 return "block sync ";
600 return "header address ";
603 return "illegal track read ";
605 return "mode 0 read ";
607 return "illegal mode read ";
609 return "illegal block size read ";
615 return "leadout read ";
617 return "buffer overrun ";
620 return "unrecoverable CIRC ";
622 return "unrecoverable LECC ";
627 return "invalid subcode data ";
629 return "focus on TOC read ";
631 return "frame sync on TOC read ";
636 return "hardware failure ";
642 return "data track ";
644 sprintf(errbuf, "unknown 0x%02x ", err_code);
649 * Set the drive parameters so the drive will auto-spin-up when a
652 static void set_drive_params(int want_doublespeed)
654 unsigned char res_reg[12];
655 unsigned int res_size;
656 unsigned char params[3];
659 params[0] = SONY_SD_AUTO_SPIN_DOWN_TIME;
660 params[1] = 0x00; /* Never spin down the drive. */
661 do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
662 params, 2, res_reg, &res_size);
663 if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20)) {
664 printk(" Unable to set spin-down time: 0x%2.2x\n",
668 params[0] = SONY_SD_MECH_CONTROL;
669 params[1] = SONY_AUTO_SPIN_UP_BIT; /* Set auto spin up */
672 params[1] |= SONY_AUTO_EJECT_BIT;
674 if (is_double_speed && want_doublespeed) {
675 params[1] |= SONY_DOUBLE_SPEED_BIT; /* Set the drive to double speed if
678 do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
679 params, 2, res_reg, &res_size);
680 if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20)) {
681 printk(" Unable to set mechanical parameters: 0x%2.2x\n",
687 * Uniform cdrom interface function
688 * select reading speed for data access
690 static int scd_select_speed(struct cdrom_device_info *cdi, int speed)
695 sony_speed = speed - 1;
697 set_drive_params(sony_speed);
702 * Uniform cdrom interface function
703 * lock or unlock eject button
705 static int scd_lock_door(struct cdrom_device_info *cdi, int lock)
707 if (lock == 0 && sony_usage == 1) {
708 /* Unlock the door, only if nobody is using the drive */
713 set_drive_params(sony_speed);
718 * This code will reset the drive and attempt to restore sane parameters.
720 static void restart_on_error(void)
722 unsigned char res_reg[12];
723 unsigned int res_size;
724 unsigned int retry_count;
727 printk("cdu31a: Resetting drive on error\n");
729 retry_count = jiffies + SONY_RESET_TIMEOUT;
730 while (time_before(jiffies, retry_count) && (!is_attention())) {
733 set_drive_params(sony_speed);
734 do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg, &res_size);
735 if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20)) {
736 printk("cdu31a: Unable to spin up drive: 0x%2.2x\n",
740 current->state = TASK_INTERRUPTIBLE;
741 schedule_timeout(2 * HZ);
747 * This routine writes data to the parameter register. Since this should
748 * happen fairly fast, it is polled with no OS waits between.
750 static int write_params(unsigned char *params, int num_params)
752 unsigned int retry_count;
755 retry_count = SONY_READY_RETRIES;
756 while ((retry_count > 0) && (!is_param_write_rdy())) {
759 if (!is_param_write_rdy()) {
763 while (num_params > 0) {
764 write_param(*params);
774 * The following reads data from the command result register. It is a
775 * fairly complex routine, all status info flows back through this
776 * interface. The algorithm is stolen directly from the flowcharts in
780 get_result(unsigned char *result_buffer, unsigned int *result_size)
784 unsigned int retry_count;
787 while (handle_sony_cd_attention());
788 /* Wait for the result data to be ready */
789 retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
790 while (time_before(jiffies, retry_count)
791 && (is_busy() || (!(is_result_ready())))) {
794 while (handle_sony_cd_attention());
796 if (is_busy() || (!(is_result_ready()))) {
798 printk("CDU31A timeout out %d\n", __LINE__);
800 result_buffer[0] = 0x20;
801 result_buffer[1] = SONY_TIMEOUT_OP_ERR;
807 * Get the first two bytes. This determines what else needs
810 clear_result_ready();
811 a = read_result_register();
815 /* Check for block error status result. */
816 if ((a & 0xf0) == 0x50) {
821 b = read_result_register();
827 * 0x20 means an error occurred. Byte 2 will have the error code.
828 * Otherwise, the command succeeded, byte 2 will have the count of
829 * how many more status bytes are coming.
831 * The result register can be read 10 bytes at a time, a wait for
832 * result ready to be asserted must be done between every 10 bytes.
834 if ((a & 0xf0) != 0x20) {
836 for (i = 0; i < 8; i++) {
837 *result_buffer = read_result_register();
844 retry_count = SONY_READY_RETRIES;
845 while ((retry_count > 0)
846 && (!is_result_ready())) {
849 if (!is_result_ready()) {
851 printk("CDU31A timeout out %d\n",
854 result_buffer[0] = 0x20;
861 clear_result_ready();
863 for (i = 0; i < 10; i++) {
865 read_result_register();
873 retry_count = SONY_READY_RETRIES;
874 while ((retry_count > 0)
875 && (!is_result_ready())) {
878 if (!is_result_ready()) {
880 printk("CDU31A timeout out %d\n",
883 result_buffer[0] = 0x20;
893 *result_buffer = read_result_register();
902 * Do a command that does not involve data transfer. This routine must
903 * be re-entrant from the same task to support being called from the
904 * data operation code when an error occurs.
907 do_sony_cd_cmd(unsigned char cmd,
908 unsigned char *params,
909 unsigned int num_params,
910 unsigned char *result_buffer, unsigned int *result_size)
912 unsigned int retry_count;
920 if (current != has_cd_task) { /* Allow recursive calls to this routine */
922 interruptible_sleep_on(&sony_wait);
923 if (signal_pending(current)) {
924 result_buffer[0] = 0x20;
925 result_buffer[1] = SONY_SIGNAL_OP_ERR;
927 restore_flags(flags);
932 has_cd_task = current;
941 while (handle_sony_cd_attention());
945 retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
946 while (time_before(jiffies, retry_count) && (is_busy())) {
949 while (handle_sony_cd_attention());
953 printk("CDU31A timeout out %d\n", __LINE__);
955 result_buffer[0] = 0x20;
956 result_buffer[1] = SONY_TIMEOUT_OP_ERR;
959 clear_result_ready();
962 write_params(params, num_params);
965 get_result(result_buffer, result_size);
968 if (((result_buffer[0] & 0xf0) == 0x20)
969 && (num_retries < MAX_CDU31A_RETRIES)) {
971 current->state = TASK_INTERRUPTIBLE;
972 schedule_timeout(HZ / 10); /* Wait .1 seconds on retries */
973 goto retry_cd_operation;
976 if (!recursive_call) {
979 wake_up_interruptible(&sony_wait);
982 restore_flags(flags);
987 * Handle an attention from the drive. This will return 1 if it found one
988 * or 0 if not (if one is found, the caller might want to call again).
990 * This routine counts the number of consecutive times it is called
991 * (since this is always called from a while loop until it returns
992 * a 0), and returns a 0 if it happens too many times. This will help
995 static int handle_sony_cd_attention(void)
997 unsigned char atten_code;
998 static int num_consecutive_attentions = 0;
1003 printk("Entering handle_sony_cd_attention\n");
1005 if (is_attention()) {
1006 if (num_consecutive_attentions >
1007 CDU31A_MAX_CONSECUTIVE_ATTENTIONS) {
1009 ("cdu31a: Too many consecutive attentions: %d\n",
1010 num_consecutive_attentions);
1011 num_consecutive_attentions = 0;
1013 printk("Leaving handle_sony_cd_attention at %d\n",
1020 atten_code = read_result_register();
1022 switch (atten_code) {
1023 /* Someone changed the CD. Mark it as changed */
1024 case SONY_MECH_LOADED_ATTN:
1027 sony_audio_status = CDROM_AUDIO_NO_STATUS;
1028 sony_blocks_left = 0;
1031 case SONY_SPIN_DOWN_COMPLETE_ATTN:
1032 /* Mark the disk as spun down. */
1036 case SONY_AUDIO_PLAY_DONE_ATTN:
1037 sony_audio_status = CDROM_AUDIO_COMPLETED;
1041 case SONY_EJECT_PUSHED_ATTN:
1042 if (is_auto_eject) {
1043 sony_audio_status = CDROM_AUDIO_INVALID;
1047 case SONY_LEAD_IN_ERR_ATTN:
1048 case SONY_LEAD_OUT_ERR_ATTN:
1049 case SONY_DATA_TRACK_ERR_ATTN:
1050 case SONY_AUDIO_PLAYBACK_ERR_ATTN:
1051 sony_audio_status = CDROM_AUDIO_ERROR;
1055 num_consecutive_attentions++;
1057 printk("Leaving handle_sony_cd_attention at %d\n",
1061 } else if (abort_read_started) {
1062 while (is_result_reg_not_empty()) {
1063 val = read_result_register();
1066 clear_result_ready();
1067 /* Clear out the data */
1068 while (is_data_requested()) {
1069 val = read_data_register();
1071 abort_read_started = 0;
1073 printk("Leaving handle_sony_cd_attention at %d\n",
1079 num_consecutive_attentions = 0;
1081 printk("Leaving handle_sony_cd_attention at %d\n", __LINE__);
1087 /* Convert from an integer 0-99 to BCD */
1088 static inline unsigned int int_to_bcd(unsigned int val)
1093 retval = (val / 10) << 4;
1094 retval = retval | val % 10;
1099 /* Convert from BCD to an integer from 0-99 */
1100 static unsigned int bcd_to_int(unsigned int bcd)
1102 return ((((bcd >> 4) & 0x0f) * 10) + (bcd & 0x0f));
1107 * Convert a logical sector value (like the OS would want to use for
1108 * a block device) to an MSF format.
1110 static void log_to_msf(unsigned int log, unsigned char *msf)
1112 log = log + LOG_START_OFFSET;
1113 msf[0] = int_to_bcd(log / 4500);
1115 msf[1] = int_to_bcd(log / 75);
1116 msf[2] = int_to_bcd(log % 75);
1121 * Convert an MSF format to a logical sector.
1123 static unsigned int msf_to_log(unsigned char *msf)
1130 log += msf[0] * 4500;
1131 log = log - LOG_START_OFFSET;
1138 * Take in integer size value and put it into a buffer like
1139 * the drive would want to see a number-of-sector value.
1141 static void size_to_buf(unsigned int size, unsigned char *buf)
1143 buf[0] = size / 65536;
1144 size = size % 65536;
1145 buf[1] = size / 256;
1146 buf[2] = size % 256;
1149 /* Starts a read operation. Returns 0 on success and 1 on failure.
1150 The read operation used here allows multiple sequential sectors
1151 to be read and status returned for each sector. The driver will
1152 read the output one at a time as the requests come and abort the
1153 operation if the requested sector is not the next one from the
1156 start_request(unsigned int sector, unsigned int nsect, int read_nsect_only)
1158 unsigned char params[6];
1159 unsigned int read_size;
1160 unsigned int retry_count;
1164 printk("Entering start_request\n");
1166 log_to_msf(sector, params);
1167 /* If requested, read exactly what was asked. */
1168 if (read_nsect_only) {
1172 * If the full read-ahead would go beyond the end of the media, trim
1173 * it back to read just till the end of the media.
1175 else if ((sector + nsect) >= sony_toc.lead_out_start_lba) {
1176 read_size = sony_toc.lead_out_start_lba - sector;
1178 /* Read the full readahead amount. */
1180 read_size = CDU31A_READAHEAD / 4;
1182 size_to_buf(read_size, ¶ms[3]);
1185 * Clear any outstanding attentions and wait for the drive to
1186 * complete any pending operations.
1188 while (handle_sony_cd_attention());
1190 retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
1191 while (time_before(jiffies, retry_count) && (is_busy())) {
1194 while (handle_sony_cd_attention());
1198 printk("CDU31A: Timeout while waiting to issue command\n");
1200 printk("Leaving start_request at %d\n", __LINE__);
1204 /* Issue the command */
1205 clear_result_ready();
1208 write_params(params, 6);
1209 write_cmd(SONY_READ_BLKERR_STAT_CMD);
1211 sony_blocks_left = read_size * 4;
1212 sony_next_block = sector * 4;
1213 readahead_dataleft = 0;
1216 printk("Leaving start_request at %d\n", __LINE__);
1221 printk("Leaving start_request at %d\n", __LINE__);
1225 /* Abort a pending read operation. Clear all the drive status and
1226 readahead variables. */
1227 static void abort_read(void)
1229 unsigned char result_reg[2];
1234 do_sony_cd_cmd(SONY_ABORT_CMD, NULL, 0, result_reg, &result_size);
1235 if ((result_reg[0] & 0xf0) == 0x20) {
1236 printk("CDU31A: Error aborting read, %s error\n",
1237 translate_error(result_reg[1]));
1240 while (is_result_reg_not_empty()) {
1241 val = read_result_register();
1244 clear_result_ready();
1245 /* Clear out the data */
1246 while (is_data_requested()) {
1247 val = read_data_register();
1250 sony_blocks_left = 0;
1251 readahead_dataleft = 0;
1255 /* Called when the timer times out. This will abort the
1256 pending read operation. */
1257 static void handle_abort_timeout(unsigned long data)
1259 unsigned long flags;
1262 printk("Entering handle_abort_timeout\n");
1266 /* If it is in use, ignore it. */
1268 /* We can't use abort_read(), because it will sleep
1269 or schedule in the timer interrupt. Just start
1270 the operation, finish it on the next access to
1272 clear_result_ready();
1274 write_cmd(SONY_ABORT_CMD);
1276 sony_blocks_left = 0;
1277 readahead_dataleft = 0;
1279 abort_read_started = 1;
1281 restore_flags(flags);
1283 printk("Leaving handle_abort_timeout\n");
1287 /* Actually get data and status from the drive. */
1289 input_data(char *buffer,
1290 unsigned int bytesleft,
1291 unsigned int nblocks, unsigned int offset, unsigned int skip)
1294 volatile unsigned char val;
1298 printk("Entering input_data\n");
1300 /* If an XA disk on a CDU31A, skip the first 12 bytes of data from
1301 the disk. The real data is after that. */
1303 for (i = 0; i < CD_XA_HEAD; i++) {
1304 val = read_data_register();
1310 if (bytesleft == 2048) { /* 2048 byte direct buffer transfer */
1311 insb(sony_cd_read_reg, buffer, 2048);
1312 readahead_dataleft = 0;
1314 /* If the input read did not align with the beginning of the block,
1315 skip the necessary bytes. */
1317 insb(sony_cd_read_reg, readahead_buffer, skip);
1320 /* Get the data into the buffer. */
1321 insb(sony_cd_read_reg, &buffer[offset], bytesleft);
1323 /* Get the rest of the data into the readahead buffer at the
1325 readahead_dataleft = (2048 - skip) - bytesleft;
1326 insb(sony_cd_read_reg,
1327 readahead_buffer + bytesleft, readahead_dataleft);
1329 sony_blocks_left -= nblocks;
1330 sony_next_block += nblocks;
1332 /* If an XA disk, we have to clear out the rest of the unused
1333 error correction data. */
1335 for (i = 0; i < CD_XA_TAIL; i++) {
1336 val = read_data_register();
1340 printk("Leaving input_data at %d\n", __LINE__);
1344 /* read data from the drive. Note the nsect must be <= 4. */
1346 read_data_block(char *buffer,
1348 unsigned int nblocks,
1349 unsigned char res_reg[], int *res_size)
1351 unsigned int retry_count;
1352 unsigned int bytesleft;
1353 unsigned int offset;
1358 printk("Entering read_data_block\n");
1364 /* Make sure that bytesleft doesn't exceed the buffer size */
1365 if (nblocks > 4) nblocks = 4;
1366 bytesleft = nblocks * 512;
1369 /* If the data in the read-ahead does not match the block offset,
1370 then fix things up. */
1371 if (((block % 4) * 512) != ((2048 - readahead_dataleft) % 2048)) {
1372 sony_next_block += block % 4;
1373 sony_blocks_left -= block % 4;
1374 skip = (block % 4) * 512;
1379 /* We have readahead data in the buffer, get that first before we
1380 decide if a read is necessary. */
1381 if (readahead_dataleft != 0) {
1382 if (bytesleft > readahead_dataleft) {
1383 /* The readahead will not fill the requested buffer, but
1384 get the data out of the readahead into the buffer. */
1386 readahead_buffer + (2048 -
1387 readahead_dataleft),
1388 readahead_dataleft);
1389 bytesleft -= readahead_dataleft;
1390 offset += readahead_dataleft;
1391 readahead_dataleft = 0;
1393 /* The readahead will fill the whole buffer, get the data
1396 readahead_buffer + (2048 -
1397 readahead_dataleft),
1399 readahead_dataleft -= bytesleft;
1401 sony_blocks_left -= nblocks;
1402 sony_next_block += nblocks;
1404 /* If the data in the readahead is bad, return an error so the
1405 driver will abort the buffer. */
1406 if (readahead_bad) {
1408 res_reg[1] = SONY_BAD_DATA_ERR;
1412 if (readahead_dataleft == 0) {
1416 /* Final transfer is done for read command, get final result. */
1417 if (sony_blocks_left == 0) {
1418 get_result(res_reg, res_size);
1421 printk("Leaving read_data_block at %d\n",
1428 /* Wait for the drive to tell us we have something */
1429 retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
1430 while (time_before(jiffies, retry_count) && !(is_data_ready())) {
1431 while (handle_sony_cd_attention());
1435 if (!(is_data_ready())) {
1436 if (is_result_ready()) {
1437 get_result(res_reg, res_size);
1438 if ((res_reg[0] & 0xf0) != 0x20) {
1440 ("CDU31A: Got result that should have been error: %d\n",
1443 res_reg[1] = SONY_BAD_DATA_ERR;
1449 printk("CDU31A timeout out %d\n", __LINE__);
1452 res_reg[1] = SONY_TIMEOUT_OP_ERR;
1457 input_data(buffer, bytesleft, nblocks, offset, skip);
1459 /* Wait for the status from the drive. */
1460 retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
1461 while (time_before(jiffies, retry_count)
1462 && !(is_result_ready())) {
1463 while (handle_sony_cd_attention());
1468 if (!is_result_ready()) {
1470 printk("CDU31A timeout out %d\n", __LINE__);
1473 res_reg[1] = SONY_TIMEOUT_OP_ERR;
1477 get_result(res_reg, res_size);
1479 /* If we got a buffer status, handle that. */
1480 if ((res_reg[0] & 0xf0) == 0x50) {
1483 SONY_NO_CIRC_ERR_BLK_STAT)
1485 SONY_NO_LECC_ERR_BLK_STAT)
1487 SONY_RECOV_LECC_ERR_BLK_STAT)) {
1488 /* The data was successful, but if data was read from
1489 the readahead and it was bad, set the whole
1491 if (readahead_bad) {
1500 ("CDU31A: Data block error: 0x%x\n",
1503 res_reg[1] = SONY_BAD_DATA_ERR;
1506 /* Data is in the readahead buffer but an error was returned.
1507 Make sure future requests don't use the data. */
1508 if (bytesleft != 2048) {
1513 /* Final transfer is done for read command, get final result. */
1514 if (sony_blocks_left == 0) {
1515 get_result(res_reg, res_size);
1517 } else if ((res_reg[0] & 0xf0) != 0x20) {
1518 /* The drive gave me bad status, I don't know what to do.
1519 Reset the driver and return an error. */
1521 ("CDU31A: Invalid block status: 0x%x\n",
1525 res_reg[1] = SONY_BAD_DATA_ERR;
1531 printk("Leaving read_data_block at %d\n", __LINE__);
1537 * The OS calls this to perform a read or write operation to the drive.
1538 * Write obviously fail. Reads to a read ahead of sony_buffer_size
1539 * bytes to help speed operations. This especially helps since the OS
1540 * uses 1024 byte blocks and the drive uses 2048 byte blocks. Since most
1541 * data access on a CD is done sequentially, this saves a lot of operations.
1543 static void do_cdu31a_request(request_queue_t * q)
1547 unsigned char res_reg[12];
1548 unsigned int res_size;
1550 unsigned long flags;
1554 printk("Entering do_cdu31a_request\n");
1558 * Make sure no one else is using the driver; wait for them
1559 * to finish if it is so.
1563 while (sony_inuse) {
1564 interruptible_sleep_on(&sony_wait);
1565 if (signal_pending(current)) {
1566 restore_flags(flags);
1568 && CURRENT->rq_status != RQ_INACTIVE) {
1571 restore_flags(flags);
1573 printk("Leaving do_cdu31a_request at %d\n",
1580 has_cd_task = current;
1582 /* Get drive status before doing anything. */
1583 while (handle_sony_cd_attention());
1585 /* Make sure we have a valid TOC. */
1588 spin_unlock_irq(&io_request_lock);
1590 /* Make sure the timer is cancelled. */
1591 del_timer(&cdu31a_abort_timer);
1594 cdu31a_request_startover:
1596 * The beginning here is stolen from the hard disk driver. I hope
1599 if (QUEUE_EMPTY || CURRENT->rq_status == RQ_INACTIVE) {
1600 goto end_do_cdu31a_request;
1603 if (!sony_spun_up) {
1607 /* I don't use INIT_REQUEST because it calls return, which would
1608 return without unlocking the device. It shouldn't matter,
1609 but just to be safe... */
1610 if (MAJOR(CURRENT->rq_dev) != MAJOR_NR) {
1611 panic(DEVICE_NAME ": request list destroyed");
1614 if (!buffer_locked(CURRENT->bh)) {
1615 panic(DEVICE_NAME ": block not locked");
1619 block = CURRENT->sector;
1620 nblock = CURRENT->nr_sectors;
1622 if (!sony_toc_read) {
1623 printk("CDU31A: TOC not read\n");
1625 goto cdu31a_request_startover;
1628 switch (CURRENT->cmd) {
1631 * If the block address is invalid or the request goes beyond the end of
1632 * the media, return an error.
1635 if ((block / 4) < sony_toc.start_track_lba) {
1637 ("CDU31A: Request before beginning of media\n");
1639 goto cdu31a_request_startover;
1642 if ((block / 4) >= sony_toc.lead_out_start_lba) {
1644 ("CDU31A: Request past end of media\n");
1646 goto cdu31a_request_startover;
1648 if (((block + nblock) / 4) >=
1649 sony_toc.lead_out_start_lba) {
1651 ("CDU31A: Request past end of media\n");
1653 goto cdu31a_request_startover;
1659 while (handle_sony_cd_attention());
1661 if (!sony_toc_read) {
1662 printk("CDU31A: TOC not read\n");
1664 goto cdu31a_request_startover;
1667 /* If no data is left to be read from the drive, start the
1669 if (sony_blocks_left == 0) {
1671 (block / 4, CDU31A_READAHEAD / 4, 0)) {
1673 goto cdu31a_request_startover;
1676 /* If the requested block is not the next one waiting in
1677 the driver, abort the current operation and start a
1679 else if (block != sony_next_block) {
1682 ("CDU31A Warning: Read for block %d, expected %d\n",
1683 block, sony_next_block);
1686 if (!sony_toc_read) {
1687 printk("CDU31A: TOC not read\n");
1689 goto cdu31a_request_startover;
1692 (block / 4, CDU31A_READAHEAD / 4, 0)) {
1694 ("CDU31a: start request failed\n");
1696 goto cdu31a_request_startover;
1700 read_data_block(CURRENT->buffer, block, nblock,
1701 res_reg, &res_size);
1702 if (res_reg[0] == 0x20) {
1703 if (num_retries > MAX_CDU31A_RETRIES) {
1705 goto cdu31a_request_startover;
1709 if (res_reg[1] == SONY_NOT_SPIN_ERR) {
1710 do_sony_cd_cmd(SONY_SPIN_UP_CMD,
1715 ("CDU31A: %s error for block %d, nblock %d\n",
1716 translate_error(res_reg[1]),
1719 goto try_read_again;
1730 panic("CDU31A: Unknown cmd");
1734 end_do_cdu31a_request:
1735 spin_lock_irq(&io_request_lock);
1737 /* After finished, cancel any pending operations. */
1740 /* Start a timer to time out after a while to disable
1742 cdu31a_abort_timer.expires = jiffies + 2 * HZ; /* Wait 2 seconds */
1743 add_timer(&cdu31a_abort_timer);
1748 wake_up_interruptible(&sony_wait);
1749 restore_flags(flags);
1751 printk("Leaving do_cdu31a_request at %d\n", __LINE__);
1757 * Read the table of contents from the drive and set up TOC if
1760 static void sony_get_toc(void)
1762 unsigned char res_reg[2];
1763 unsigned int res_size;
1764 unsigned char parms[1];
1767 int totaltracks = 0;
1772 printk("Entering sony_get_toc\n");
1776 if (!sony_toc_read) {
1778 /* Ignore the result, since it might error if spinning already. */
1779 do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg,
1782 do_sony_cd_cmd(SONY_READ_TOC_CMD, NULL, 0, res_reg,
1785 /* The drive sometimes returns error 0. I don't know why, but ignore
1786 it. It seems to mean the drive has already done the operation. */
1788 || ((res_reg[0] != 0) && (res_reg[1] != 0))) {
1789 /* If the drive is already playing, it's ok. */
1790 if ((res_reg[1] == SONY_AUDIO_PLAYING_ERR)
1791 || (res_reg[1] == 0)) {
1792 goto gettoc_drive_spinning;
1795 /* If the drive says it is not spun up (even though we just did it!)
1796 then retry the operation at least a few times. */
1797 if ((res_reg[1] == SONY_NOT_SPIN_ERR)
1798 && (num_spin_ups < MAX_CDU31A_RETRIES)) {
1800 goto respinup_on_gettoc;
1803 printk("cdu31a: Error reading TOC: %x %s\n",
1804 res_reg[0], translate_error(res_reg[1]));
1808 gettoc_drive_spinning:
1810 /* The idea here is we keep asking for sessions until the command
1811 fails. Then we know what the last valid session on the disk is.
1812 No need to check session 0, since session 0 is the same as session
1813 1; the command returns different information if you give it 0.
1816 memset(&sony_toc, 0x0e, sizeof(sony_toc));
1817 memset(&single_toc, 0x0f, sizeof(single_toc));
1821 /* This seems to slow things down enough to make it work. This
1822 * appears to be a problem in do_sony_cd_cmd. This printk seems
1823 * to address the symptoms... -Erik */
1825 printk("cdu31a: Trying session %d\n", session);
1828 do_sony_cd_cmd(SONY_READ_TOC_SPEC_CMD,
1829 parms, 1, res_reg, &res_size);
1832 printk("%2.2x %2.2x\n", res_reg[0], res_reg[1]);
1836 || ((res_reg[0] & 0xf0) == 0x20)) {
1837 /* An error reading the TOC, this must be past the last session. */
1840 ("Yikes! Couldn't read any sessions!");
1844 printk("Reading session %d\n", session);
1848 do_sony_cd_cmd(SONY_REQ_TOC_DATA_SPEC_CMD,
1851 (unsigned char *) &single_toc,
1854 || ((single_toc.exec_status[0] & 0xf0) ==
1857 ("cdu31a: Error reading session %d: %x %s\n",
1858 session, single_toc.exec_status[0],
1859 translate_error(single_toc.
1861 /* An error reading the TOC. Return without sony_toc_read
1867 ("add0 %01x, con0 %01x, poi0 %02x, 1st trk %d, dsktyp %x, dum0 %x\n",
1868 single_toc.address0, single_toc.control0,
1870 bcd_to_int(single_toc.first_track_num),
1871 single_toc.disk_type, single_toc.dummy0);
1873 ("add1 %01x, con1 %01x, poi1 %02x, lst trk %d, dummy1 %x, dum2 %x\n",
1874 single_toc.address1, single_toc.control1,
1876 bcd_to_int(single_toc.last_track_num),
1877 single_toc.dummy1, single_toc.dummy2);
1879 ("add2 %01x, con2 %01x, poi2 %02x leadout start min %d, sec %d, frame %d\n",
1880 single_toc.address2, single_toc.control2,
1882 bcd_to_int(single_toc.lead_out_start_msf[0]),
1883 bcd_to_int(single_toc.lead_out_start_msf[1]),
1884 bcd_to_int(single_toc.lead_out_start_msf[2]));
1885 if (res_size > 18 && single_toc.pointb0 > 0xaf)
1887 ("addb0 %01x, conb0 %01x, poib0 %02x, nextsession min %d, sec %d, frame %d\n"
1888 "#mode5_ptrs %02d, max_start_outer_leadout_msf min %d, sec %d, frame %d\n",
1889 single_toc.addressb0,
1890 single_toc.controlb0,
1892 bcd_to_int(single_toc.
1893 next_poss_prog_area_msf
1895 bcd_to_int(single_toc.
1896 next_poss_prog_area_msf
1898 bcd_to_int(single_toc.
1899 next_poss_prog_area_msf
1901 single_toc.num_mode_5_pointers,
1902 bcd_to_int(single_toc.
1903 max_start_outer_leadout_msf
1905 bcd_to_int(single_toc.
1906 max_start_outer_leadout_msf
1908 bcd_to_int(single_toc.
1909 max_start_outer_leadout_msf
1911 if (res_size > 27 && single_toc.pointb1 > 0xaf)
1913 ("addb1 %01x, conb1 %01x, poib1 %02x, %x %x %x %x #skipint_ptrs %d, #skiptrkassign %d %x\n",
1914 single_toc.addressb1,
1915 single_toc.controlb1,
1917 single_toc.dummyb0_1[0],
1918 single_toc.dummyb0_1[1],
1919 single_toc.dummyb0_1[2],
1920 single_toc.dummyb0_1[3],
1921 single_toc.num_skip_interval_pointers,
1922 single_toc.num_skip_track_assignments,
1923 single_toc.dummyb0_2);
1924 if (res_size > 36 && single_toc.pointb2 > 0xaf)
1926 ("addb2 %01x, conb2 %01x, poib2 %02x, %02x %02x %02x %02x %02x %02x %02x\n",
1927 single_toc.addressb2,
1928 single_toc.controlb2,
1930 single_toc.tracksb2[0],
1931 single_toc.tracksb2[1],
1932 single_toc.tracksb2[2],
1933 single_toc.tracksb2[3],
1934 single_toc.tracksb2[4],
1935 single_toc.tracksb2[5],
1936 single_toc.tracksb2[6]);
1937 if (res_size > 45 && single_toc.pointb3 > 0xaf)
1939 ("addb3 %01x, conb3 %01x, poib3 %02x, %02x %02x %02x %02x %02x %02x %02x\n",
1940 single_toc.addressb3,
1941 single_toc.controlb3,
1943 single_toc.tracksb3[0],
1944 single_toc.tracksb3[1],
1945 single_toc.tracksb3[2],
1946 single_toc.tracksb3[3],
1947 single_toc.tracksb3[4],
1948 single_toc.tracksb3[5],
1949 single_toc.tracksb3[6]);
1950 if (res_size > 54 && single_toc.pointb4 > 0xaf)
1952 ("addb4 %01x, conb4 %01x, poib4 %02x, %02x %02x %02x %02x %02x %02x %02x\n",
1953 single_toc.addressb4,
1954 single_toc.controlb4,
1956 single_toc.tracksb4[0],
1957 single_toc.tracksb4[1],
1958 single_toc.tracksb4[2],
1959 single_toc.tracksb4[3],
1960 single_toc.tracksb4[4],
1961 single_toc.tracksb4[5],
1962 single_toc.tracksb4[6]);
1963 if (res_size > 63 && single_toc.pointc0 > 0xaf)
1965 ("addc0 %01x, conc0 %01x, poic0 %02x, %02x %02x %02x %02x %02x %02x %02x\n",
1966 single_toc.addressc0,
1967 single_toc.controlc0,
1969 single_toc.dummyc0[0],
1970 single_toc.dummyc0[1],
1971 single_toc.dummyc0[2],
1972 single_toc.dummyc0[3],
1973 single_toc.dummyc0[4],
1974 single_toc.dummyc0[5],
1975 single_toc.dummyc0[6]);
1980 sony_toc.lead_out_start_msf[0] =
1981 bcd_to_int(single_toc.lead_out_start_msf[0]);
1982 sony_toc.lead_out_start_msf[1] =
1983 bcd_to_int(single_toc.lead_out_start_msf[1]);
1984 sony_toc.lead_out_start_msf[2] =
1985 bcd_to_int(single_toc.lead_out_start_msf[2]);
1986 sony_toc.lead_out_start_lba =
1987 single_toc.lead_out_start_lba =
1988 msf_to_log(sony_toc.lead_out_start_msf);
1990 /* For points that do not exist, move the data over them
1991 to the right location. */
1992 if (single_toc.pointb0 != 0xb0) {
1993 memmove(((char *) &single_toc) + 27,
1994 ((char *) &single_toc) + 18,
1997 } else if (res_size > 18) {
1998 sony_toc.lead_out_start_msf[0] =
1999 bcd_to_int(single_toc.
2000 max_start_outer_leadout_msf
2002 sony_toc.lead_out_start_msf[1] =
2003 bcd_to_int(single_toc.
2004 max_start_outer_leadout_msf
2006 sony_toc.lead_out_start_msf[2] =
2007 bcd_to_int(single_toc.
2008 max_start_outer_leadout_msf
2010 sony_toc.lead_out_start_lba =
2011 msf_to_log(sony_toc.
2012 lead_out_start_msf);
2014 if (single_toc.pointb1 != 0xb1) {
2015 memmove(((char *) &single_toc) + 36,
2016 ((char *) &single_toc) + 27,
2020 if (single_toc.pointb2 != 0xb2) {
2021 memmove(((char *) &single_toc) + 45,
2022 ((char *) &single_toc) + 36,
2026 if (single_toc.pointb3 != 0xb3) {
2027 memmove(((char *) &single_toc) + 54,
2028 ((char *) &single_toc) + 45,
2032 if (single_toc.pointb4 != 0xb4) {
2033 memmove(((char *) &single_toc) + 63,
2034 ((char *) &single_toc) + 54,
2038 if (single_toc.pointc0 != 0xc0) {
2039 memmove(((char *) &single_toc) + 72,
2040 ((char *) &single_toc) + 63,
2046 ("start track lba %u, leadout start lba %u\n",
2047 single_toc.start_track_lba,
2048 single_toc.lead_out_start_lba);
2054 bcd_to_int(single_toc.last_track_num)
2056 bcd_to_int(single_toc.
2057 first_track_num); i++) {
2059 ("trk %02d: add 0x%01x, con 0x%01x, track %02d, start min %02d, sec %02d, frame %02d\n",
2061 single_toc.tracks[i].address,
2062 single_toc.tracks[i].control,
2063 bcd_to_int(single_toc.
2065 bcd_to_int(single_toc.
2069 bcd_to_int(single_toc.
2073 bcd_to_int(single_toc.
2078 bcd_to_int(single_toc.
2081 bcd_to_int(single_toc.
2085 bcd_to_int(single_toc.
2088 bcd_to_int(single_toc.
2093 ("min track number %d, max track number %d\n",
2098 /* prepare a special table of contents for a CD-I disc. They don't have one. */
2099 if (single_toc.disk_type == 0x10 &&
2100 single_toc.first_track_num == 2 &&
2101 single_toc.last_track_num == 2 /* CD-I */ ) {
2102 sony_toc.tracks[totaltracks].address = 1;
2103 sony_toc.tracks[totaltracks].control = 4; /* force data tracks */
2104 sony_toc.tracks[totaltracks].track = 1;
2105 sony_toc.tracks[totaltracks].
2106 track_start_msf[0] = 0;
2107 sony_toc.tracks[totaltracks].
2108 track_start_msf[1] = 2;
2109 sony_toc.tracks[totaltracks].
2110 track_start_msf[2] = 0;
2114 /* gather track entries from this session */
2120 bcd_to_int(single_toc.last_track_num)
2122 bcd_to_int(single_toc.
2124 i++, totaltracks++) {
2125 sony_toc.tracks[totaltracks].
2127 single_toc.tracks[i].address;
2128 sony_toc.tracks[totaltracks].
2130 single_toc.tracks[i].control;
2131 sony_toc.tracks[totaltracks].
2133 bcd_to_int(single_toc.
2135 sony_toc.tracks[totaltracks].
2136 track_start_msf[0] =
2137 bcd_to_int(single_toc.
2139 track_start_msf[0]);
2140 sony_toc.tracks[totaltracks].
2141 track_start_msf[1] =
2142 bcd_to_int(single_toc.
2144 track_start_msf[1]);
2145 sony_toc.tracks[totaltracks].
2146 track_start_msf[2] =
2147 bcd_to_int(single_toc.
2149 track_start_msf[2]);
2153 msf_to_log(sony_toc.
2158 sony_toc.tracks[totaltracks].
2162 tracks[totaltracks].
2165 sony_toc.tracks[totaltracks].
2169 tracks[totaltracks].
2173 sony_toc.first_track_num = mint;
2174 sony_toc.last_track_num = maxt;
2175 /* Disk type of last session wins. For example:
2176 CD-Extra has disk type 0 for the first session, so
2177 a dumb HiFi CD player thinks it is a plain audio CD.
2178 We are interested in the disk type of the last session,
2179 which is 0x20 (XA) for CD-Extra, so we can access the
2181 sony_toc.disk_type = single_toc.disk_type;
2182 sony_toc.sessions = session;
2184 /* don't believe everything :-) */
2186 single_toc.start_track_lba = 0;
2187 sony_toc.start_track_lba =
2188 single_toc.start_track_lba;
2190 if (session > 1 && single_toc.pointb0 == 0xb0 &&
2191 sony_toc.lead_out_start_lba ==
2192 single_toc.lead_out_start_lba) {
2196 /* Let's not get carried away... */
2198 printk("cdu31a: too many sessions: %d\n",
2204 sony_toc.track_entries = totaltracks;
2205 /* add one entry for the LAST track with track number CDROM_LEADOUT */
2206 sony_toc.tracks[totaltracks].address = single_toc.address2;
2207 sony_toc.tracks[totaltracks].control = single_toc.control2;
2208 sony_toc.tracks[totaltracks].track = CDROM_LEADOUT;
2209 sony_toc.tracks[totaltracks].track_start_msf[0] =
2210 sony_toc.lead_out_start_msf[0];
2211 sony_toc.tracks[totaltracks].track_start_msf[1] =
2212 sony_toc.lead_out_start_msf[1];
2213 sony_toc.tracks[totaltracks].track_start_msf[2] =
2214 sony_toc.lead_out_start_msf[2];
2220 ("Disk session %d, start track: %d, stop track: %d\n",
2221 session, single_toc.start_track_lba,
2222 single_toc.lead_out_start_lba);
2226 printk("Leaving sony_get_toc\n");
2232 * Uniform cdrom interface function
2233 * return multisession offset and sector information
2235 static int scd_get_last_session(struct cdrom_device_info *cdi,
2236 struct cdrom_multisession *ms_info)
2238 if (ms_info == NULL)
2244 ms_info->addr_format = CDROM_LBA;
2245 ms_info->addr.lba = sony_toc.start_track_lba;
2246 ms_info->xa_flag = sony_toc.disk_type == SONY_XA_DISK_TYPE ||
2247 sony_toc.disk_type == 0x10 /* CDI */ ;
2253 * Search for a specific track in the table of contents.
2255 static int find_track(int track)
2259 for (i = 0; i <= sony_toc.track_entries; i++) {
2260 if (sony_toc.tracks[i].track == track) {
2270 * Read the subcode and put it in last_sony_subcode for future use.
2272 static int read_subcode(void)
2274 unsigned int res_size;
2277 do_sony_cd_cmd(SONY_REQ_SUBCODE_ADDRESS_CMD,
2279 0, (unsigned char *) &last_sony_subcode, &res_size);
2281 || ((last_sony_subcode.exec_status[0] & 0xf0) == 0x20)) {
2282 printk("Sony CDROM error %s (read_subcode)\n",
2283 translate_error(last_sony_subcode.exec_status[1]));
2287 last_sony_subcode.track_num =
2288 bcd_to_int(last_sony_subcode.track_num);
2289 last_sony_subcode.index_num =
2290 bcd_to_int(last_sony_subcode.index_num);
2291 last_sony_subcode.abs_msf[0] =
2292 bcd_to_int(last_sony_subcode.abs_msf[0]);
2293 last_sony_subcode.abs_msf[1] =
2294 bcd_to_int(last_sony_subcode.abs_msf[1]);
2295 last_sony_subcode.abs_msf[2] =
2296 bcd_to_int(last_sony_subcode.abs_msf[2]);
2298 last_sony_subcode.rel_msf[0] =
2299 bcd_to_int(last_sony_subcode.rel_msf[0]);
2300 last_sony_subcode.rel_msf[1] =
2301 bcd_to_int(last_sony_subcode.rel_msf[1]);
2302 last_sony_subcode.rel_msf[2] =
2303 bcd_to_int(last_sony_subcode.rel_msf[2]);
2308 * Uniform cdrom interface function
2309 * return the media catalog number found on some older audio cds
2312 scd_get_mcn(struct cdrom_device_info *cdi, struct cdrom_mcn *mcn)
2314 unsigned char resbuffer[2 + 14];
2315 unsigned char *mcnp = mcn->medium_catalog_number;
2316 unsigned char *resp = resbuffer + 3;
2317 unsigned int res_size;
2319 memset(mcn->medium_catalog_number, 0, 14);
2320 do_sony_cd_cmd(SONY_REQ_UPC_EAN_CMD,
2321 NULL, 0, resbuffer, &res_size);
2322 if ((res_size < 2) || ((resbuffer[0] & 0xf0) == 0x20));
2324 /* packed bcd to single ASCII digits */
2325 *mcnp++ = (*resp >> 4) + '0';
2326 *mcnp++ = (*resp++ & 0x0f) + '0';
2327 *mcnp++ = (*resp >> 4) + '0';
2328 *mcnp++ = (*resp++ & 0x0f) + '0';
2329 *mcnp++ = (*resp >> 4) + '0';
2330 *mcnp++ = (*resp++ & 0x0f) + '0';
2331 *mcnp++ = (*resp >> 4) + '0';
2332 *mcnp++ = (*resp++ & 0x0f) + '0';
2333 *mcnp++ = (*resp >> 4) + '0';
2334 *mcnp++ = (*resp++ & 0x0f) + '0';
2335 *mcnp++ = (*resp >> 4) + '0';
2336 *mcnp++ = (*resp++ & 0x0f) + '0';
2337 *mcnp++ = (*resp >> 4) + '0';
2345 * Get the subchannel info like the CDROMSUBCHNL command wants to see it. If
2346 * the drive is playing, the subchannel needs to be read (since it would be
2347 * changing). If the drive is paused or completed, the subcode information has
2348 * already been stored, just use that. The ioctl call wants things in decimal
2349 * (not BCD), so all the conversions are done.
2351 static int sony_get_subchnl_info(struct cdrom_subchnl *schi)
2353 /* Get attention stuff */
2354 while (handle_sony_cd_attention());
2357 if (!sony_toc_read) {
2361 switch (sony_audio_status) {
2362 case CDROM_AUDIO_NO_STATUS:
2363 case CDROM_AUDIO_PLAY:
2364 if (read_subcode() < 0) {
2369 case CDROM_AUDIO_PAUSED:
2370 case CDROM_AUDIO_COMPLETED:
2374 case CDROM_AUDIO_NO_STATUS:
2375 schi->cdsc_audiostatus = sony_audio_status;
2379 case CDROM_AUDIO_INVALID:
2380 case CDROM_AUDIO_ERROR:
2385 schi->cdsc_audiostatus = sony_audio_status;
2386 schi->cdsc_adr = last_sony_subcode.address;
2387 schi->cdsc_ctrl = last_sony_subcode.control;
2388 schi->cdsc_trk = last_sony_subcode.track_num;
2389 schi->cdsc_ind = last_sony_subcode.index_num;
2390 if (schi->cdsc_format == CDROM_MSF) {
2391 schi->cdsc_absaddr.msf.minute =
2392 last_sony_subcode.abs_msf[0];
2393 schi->cdsc_absaddr.msf.second =
2394 last_sony_subcode.abs_msf[1];
2395 schi->cdsc_absaddr.msf.frame =
2396 last_sony_subcode.abs_msf[2];
2398 schi->cdsc_reladdr.msf.minute =
2399 last_sony_subcode.rel_msf[0];
2400 schi->cdsc_reladdr.msf.second =
2401 last_sony_subcode.rel_msf[1];
2402 schi->cdsc_reladdr.msf.frame =
2403 last_sony_subcode.rel_msf[2];
2404 } else if (schi->cdsc_format == CDROM_LBA) {
2405 schi->cdsc_absaddr.lba =
2406 msf_to_log(last_sony_subcode.abs_msf);
2407 schi->cdsc_reladdr.lba =
2408 msf_to_log(last_sony_subcode.rel_msf);
2414 /* Get audio data from the drive. This is fairly complex because I
2415 am looking for status and data at the same time, but if I get status
2416 then I just look for data. I need to get the status immediately so
2417 the switch from audio to data tracks will happen quickly. */
2419 read_audio_data(char *buffer, unsigned char res_reg[], int *res_size)
2421 unsigned int retry_count;
2430 /* Wait for the drive to tell us we have something */
2431 retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
2432 continue_read_audio_wait:
2433 while (time_before(jiffies, retry_count) && !(is_data_ready())
2434 && !(is_result_ready() || result_read)) {
2435 while (handle_sony_cd_attention());
2439 if (!(is_data_ready())) {
2440 if (is_result_ready() && !result_read) {
2441 get_result(res_reg, res_size);
2443 /* Read block status and continue waiting for data. */
2444 if ((res_reg[0] & 0xf0) == 0x50) {
2446 goto continue_read_audio_wait;
2448 /* Invalid data from the drive. Shut down the operation. */
2449 else if ((res_reg[0] & 0xf0) != 0x20) {
2451 ("CDU31A: Got result that should have been error: %d\n",
2454 res_reg[1] = SONY_BAD_DATA_ERR;
2460 printk("CDU31A timeout out %d\n", __LINE__);
2463 res_reg[1] = SONY_TIMEOUT_OP_ERR;
2470 /* If data block, then get 2340 bytes offset by 12. */
2471 if (sony_raw_data_mode) {
2472 insb(sony_cd_read_reg, buffer + CD_XA_HEAD,
2475 /* Audio gets the whole 2352 bytes. */
2476 insb(sony_cd_read_reg, buffer, CD_FRAMESIZE_RAW);
2479 /* If I haven't already gotten the result, get it now. */
2481 /* Wait for the drive to tell us we have something */
2482 retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
2483 while (time_before(jiffies, retry_count)
2484 && !(is_result_ready())) {
2485 while (handle_sony_cd_attention());
2490 if (!is_result_ready()) {
2492 printk("CDU31A timeout out %d\n",
2496 res_reg[1] = SONY_TIMEOUT_OP_ERR;
2501 get_result(res_reg, res_size);
2505 if ((res_reg[0] & 0xf0) == 0x50) {
2506 if ((res_reg[0] == SONY_NO_CIRC_ERR_BLK_STAT)
2507 || (res_reg[0] == SONY_NO_LECC_ERR_BLK_STAT)
2508 || (res_reg[0] == SONY_RECOV_LECC_ERR_BLK_STAT)
2509 || (res_reg[0] == SONY_NO_ERR_DETECTION_STAT)) {
2510 /* Ok, nothing to do. */
2512 printk("CDU31A: Data block error: 0x%x\n",
2515 res_reg[1] = SONY_BAD_DATA_ERR;
2518 } else if ((res_reg[0] & 0xf0) != 0x20) {
2519 /* The drive gave me bad status, I don't know what to do.
2520 Reset the driver and return an error. */
2521 printk("CDU31A: Invalid block status: 0x%x\n",
2525 res_reg[1] = SONY_BAD_DATA_ERR;
2531 /* Perform a raw data read. This will automatically detect the
2532 track type and read the proper data (audio or data). */
2533 static int read_audio(struct cdrom_read_audio *ra)
2536 unsigned char params[2];
2537 unsigned char res_reg[12];
2538 unsigned int res_size;
2539 unsigned int cframe;
2540 unsigned long flags;
2543 * Make sure no one else is using the driver; wait for them
2544 * to finish if it is so.
2548 while (sony_inuse) {
2549 interruptible_sleep_on(&sony_wait);
2550 if (signal_pending(current)) {
2551 restore_flags(flags);
2556 has_cd_task = current;
2557 restore_flags(flags);
2559 if (!sony_spun_up) {
2563 /* Set the drive to do raw operations. */
2564 params[0] = SONY_SD_DECODE_PARAM;
2565 params[1] = 0x06 | sony_raw_data_mode;
2566 do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
2567 params, 2, res_reg, &res_size);
2568 if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20)) {
2569 printk("CDU31A: Unable to set decode params: 0x%2.2x\n",
2574 /* From here down, we have to goto exit_read_audio instead of returning
2575 because the drive parameters have to be set back to data before
2579 /* start_request clears out any readahead data, so it should be safe. */
2580 if (start_request(ra->addr.lba, ra->nframes, 1)) {
2582 goto exit_read_audio;
2585 /* For every requested frame. */
2587 while (cframe < ra->nframes) {
2588 read_audio_data(readahead_buffer, res_reg, &res_size);
2589 if ((res_reg[0] & 0xf0) == 0x20) {
2590 if (res_reg[1] == SONY_BAD_DATA_ERR) {
2592 ("CDU31A: Data error on audio sector %d\n",
2593 ra->addr.lba + cframe);
2594 } else if (res_reg[1] == SONY_ILL_TRACK_R_ERR) {
2595 /* Illegal track type, change track types and start over. */
2596 sony_raw_data_mode =
2597 (sony_raw_data_mode) ? 0 : 1;
2599 /* Set the drive mode. */
2600 params[0] = SONY_SD_DECODE_PARAM;
2601 params[1] = 0x06 | sony_raw_data_mode;
2602 do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
2604 2, res_reg, &res_size);
2606 || ((res_reg[0] & 0xf0) == 0x20)) {
2608 ("CDU31A: Unable to set decode params: 0x%2.2x\n",
2611 goto exit_read_audio;
2614 /* Restart the request on the current frame. */
2616 (ra->addr.lba + cframe,
2617 ra->nframes - cframe, 1)) {
2619 goto exit_read_audio;
2622 /* Don't go back to the top because don't want to get into
2623 and infinite loop. A lot of code gets duplicated, but
2624 that's no big deal, I don't guess. */
2625 read_audio_data(readahead_buffer, res_reg,
2627 if ((res_reg[0] & 0xf0) == 0x20) {
2629 SONY_BAD_DATA_ERR) {
2631 ("CDU31A: Data error on audio sector %d\n",
2636 ("CDU31A: Error reading audio data on sector %d: %s\n",
2637 ra->addr.lba + cframe,
2641 goto exit_read_audio;
2644 copy_to_user((char *) (ra->buf +
2653 ("CDU31A: Error reading audio data on sector %d: %s\n",
2654 ra->addr.lba + cframe,
2655 translate_error(res_reg[1]));
2657 goto exit_read_audio;
2660 copy_to_user((char *) (ra->buf +
2663 (char *) readahead_buffer,
2670 get_result(res_reg, &res_size);
2671 if ((res_reg[0] & 0xf0) == 0x20) {
2672 printk("CDU31A: Error return from audio read: %s\n",
2673 translate_error(res_reg[1]));
2675 goto exit_read_audio;
2680 /* Set the drive mode back to the proper one for the disk. */
2681 params[0] = SONY_SD_DECODE_PARAM;
2682 if (!sony_xa_mode) {
2687 do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
2688 params, 2, res_reg, &res_size);
2689 if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20)) {
2690 printk("CDU31A: Unable to reset decode params: 0x%2.2x\n",
2697 wake_up_interruptible(&sony_wait);
2703 do_sony_cd_cmd_chk(const char *name,
2705 unsigned char *params,
2706 unsigned int num_params,
2707 unsigned char *result_buffer, unsigned int *result_size)
2709 do_sony_cd_cmd(cmd, params, num_params, result_buffer,
2711 if ((*result_size < 2) || ((result_buffer[0] & 0xf0) == 0x20)) {
2712 printk("Sony CDROM error %s (CDROM%s)\n",
2713 translate_error(result_buffer[1]), name);
2720 * Uniform cdrom interface function
2723 static int scd_tray_move(struct cdrom_device_info *cdi, int position)
2725 if (position == 1 /* open tray */ ) {
2726 unsigned char res_reg[12];
2727 unsigned int res_size;
2729 do_sony_cd_cmd(SONY_AUDIO_STOP_CMD, NULL, 0, res_reg,
2731 do_sony_cd_cmd(SONY_SPIN_DOWN_CMD, NULL, 0, res_reg,
2734 sony_audio_status = CDROM_AUDIO_INVALID;
2735 return do_sony_cd_cmd_chk("EJECT", SONY_EJECT_CMD, NULL, 0,
2736 res_reg, &res_size);
2738 if (0 == scd_spinup())
2745 * The big ugly ioctl handler.
2747 static int scd_audio_ioctl(struct cdrom_device_info *cdi,
2748 unsigned int cmd, void *arg)
2750 unsigned char res_reg[12];
2751 unsigned int res_size;
2752 unsigned char params[7];
2757 case CDROMSTART: /* Spin up the drive */
2758 return do_sony_cd_cmd_chk("START", SONY_SPIN_UP_CMD, NULL,
2759 0, res_reg, &res_size);
2762 case CDROMSTOP: /* Spin down the drive */
2763 do_sony_cd_cmd(SONY_AUDIO_STOP_CMD, NULL, 0, res_reg,
2767 * Spin the drive down, ignoring the error if the disk was
2768 * already not spinning.
2770 sony_audio_status = CDROM_AUDIO_NO_STATUS;
2771 return do_sony_cd_cmd_chk("STOP", SONY_SPIN_DOWN_CMD, NULL,
2772 0, res_reg, &res_size);
2774 case CDROMPAUSE: /* Pause the drive */
2775 if (do_sony_cd_cmd_chk
2776 ("PAUSE", SONY_AUDIO_STOP_CMD, NULL, 0, res_reg,
2779 /* Get the current position and save it for resuming */
2780 if (read_subcode() < 0) {
2783 cur_pos_msf[0] = last_sony_subcode.abs_msf[0];
2784 cur_pos_msf[1] = last_sony_subcode.abs_msf[1];
2785 cur_pos_msf[2] = last_sony_subcode.abs_msf[2];
2786 sony_audio_status = CDROM_AUDIO_PAUSED;
2790 case CDROMRESUME: /* Start the drive after being paused */
2791 if (sony_audio_status != CDROM_AUDIO_PAUSED) {
2795 do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg,
2798 /* Start the drive at the saved position. */
2799 params[1] = int_to_bcd(cur_pos_msf[0]);
2800 params[2] = int_to_bcd(cur_pos_msf[1]);
2801 params[3] = int_to_bcd(cur_pos_msf[2]);
2802 params[4] = int_to_bcd(final_pos_msf[0]);
2803 params[5] = int_to_bcd(final_pos_msf[1]);
2804 params[6] = int_to_bcd(final_pos_msf[2]);
2806 if (do_sony_cd_cmd_chk
2807 ("RESUME", SONY_AUDIO_PLAYBACK_CMD, params, 7, res_reg,
2810 sony_audio_status = CDROM_AUDIO_PLAY;
2813 case CDROMPLAYMSF: /* Play starting at the given MSF address. */
2814 do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg,
2817 /* The parameters are given in int, must be converted */
2818 for (i = 1; i < 7; i++) {
2820 int_to_bcd(((unsigned char *) arg)[i - 1]);
2823 if (do_sony_cd_cmd_chk
2824 ("PLAYMSF", SONY_AUDIO_PLAYBACK_CMD, params, 7,
2825 res_reg, &res_size) < 0)
2828 /* Save the final position for pauses and resumes */
2829 final_pos_msf[0] = bcd_to_int(params[4]);
2830 final_pos_msf[1] = bcd_to_int(params[5]);
2831 final_pos_msf[2] = bcd_to_int(params[6]);
2832 sony_audio_status = CDROM_AUDIO_PLAY;
2835 case CDROMREADTOCHDR: /* Read the table of contents header */
2837 struct cdrom_tochdr *hdr;
2840 if (!sony_toc_read) {
2844 hdr = (struct cdrom_tochdr *) arg;
2845 hdr->cdth_trk0 = sony_toc.first_track_num;
2846 hdr->cdth_trk1 = sony_toc.last_track_num;
2850 case CDROMREADTOCENTRY: /* Read a given table of contents entry */
2852 struct cdrom_tocentry *entry;
2854 unsigned char *msf_val = NULL;
2857 if (!sony_toc_read) {
2861 entry = (struct cdrom_tocentry *) arg;
2863 track_idx = find_track(entry->cdte_track);
2864 if (track_idx < 0) {
2869 sony_toc.tracks[track_idx].address;
2871 sony_toc.tracks[track_idx].control;
2873 sony_toc.tracks[track_idx].track_start_msf;
2875 /* Logical buffer address or MSF format requested? */
2876 if (entry->cdte_format == CDROM_LBA) {
2877 entry->cdte_addr.lba = msf_to_log(msf_val);
2878 } else if (entry->cdte_format == CDROM_MSF) {
2879 entry->cdte_addr.msf.minute = *msf_val;
2880 entry->cdte_addr.msf.second =
2882 entry->cdte_addr.msf.frame =
2889 case CDROMPLAYTRKIND: /* Play a track. This currently ignores index. */
2891 struct cdrom_ti *ti = (struct cdrom_ti *) arg;
2895 if (!sony_toc_read) {
2899 if ((ti->cdti_trk0 < sony_toc.first_track_num)
2900 || (ti->cdti_trk0 > sony_toc.last_track_num)
2901 || (ti->cdti_trk1 < ti->cdti_trk0)) {
2905 track_idx = find_track(ti->cdti_trk0);
2906 if (track_idx < 0) {
2910 int_to_bcd(sony_toc.tracks[track_idx].
2911 track_start_msf[0]);
2913 int_to_bcd(sony_toc.tracks[track_idx].
2914 track_start_msf[1]);
2916 int_to_bcd(sony_toc.tracks[track_idx].
2917 track_start_msf[2]);
2920 * If we want to stop after the last track, use the lead-out
2923 if (ti->cdti_trk1 >= sony_toc.last_track_num) {
2924 track_idx = find_track(CDROM_LEADOUT);
2926 track_idx = find_track(ti->cdti_trk1 + 1);
2928 if (track_idx < 0) {
2932 int_to_bcd(sony_toc.tracks[track_idx].
2933 track_start_msf[0]);
2935 int_to_bcd(sony_toc.tracks[track_idx].
2936 track_start_msf[1]);
2938 int_to_bcd(sony_toc.tracks[track_idx].
2939 track_start_msf[2]);
2942 do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg,
2945 do_sony_cd_cmd(SONY_AUDIO_PLAYBACK_CMD, params, 7,
2946 res_reg, &res_size);
2949 || ((res_reg[0] & 0xf0) == 0x20)) {
2950 printk("Params: %x %x %x %x %x %x %x\n",
2951 params[0], params[1], params[2],
2952 params[3], params[4], params[5],
2955 ("Sony CDROM error %s (CDROMPLAYTRKIND)\n",
2956 translate_error(res_reg[1]));
2960 /* Save the final position for pauses and resumes */
2961 final_pos_msf[0] = bcd_to_int(params[4]);
2962 final_pos_msf[1] = bcd_to_int(params[5]);
2963 final_pos_msf[2] = bcd_to_int(params[6]);
2964 sony_audio_status = CDROM_AUDIO_PLAY;
2968 case CDROMVOLCTRL: /* Volume control. What volume does this change, anyway? */
2970 struct cdrom_volctrl *volctrl =
2971 (struct cdrom_volctrl *) arg;
2973 params[0] = SONY_SD_AUDIO_VOLUME;
2974 params[1] = volctrl->channel0;
2975 params[2] = volctrl->channel1;
2976 return do_sony_cd_cmd_chk("VOLCTRL",
2977 SONY_SET_DRIVE_PARAM_CMD,
2981 case CDROMSUBCHNL: /* Get subchannel info */
2982 return sony_get_subchnl_info((struct cdrom_subchnl *) arg);
2989 static int scd_dev_ioctl(struct cdrom_device_info *cdi,
2990 unsigned int cmd, unsigned long arg)
2995 case CDROMREADAUDIO: /* Read 2352 byte audio tracks and 2340 byte
2998 struct cdrom_read_audio ra;
3002 if (!sony_toc_read) {
3006 if (copy_from_user(&ra, (char *) arg, sizeof(ra)))
3009 if (ra.nframes == 0) {
3013 i = verify_area(VERIFY_WRITE, ra.buf,
3014 CD_FRAMESIZE_RAW * ra.nframes);
3018 if (ra.addr_format == CDROM_LBA) {
3020 sony_toc.lead_out_start_lba)
3021 || (ra.addr.lba + ra.nframes >=
3022 sony_toc.lead_out_start_lba)) {
3025 } else if (ra.addr_format == CDROM_MSF) {
3026 if ((ra.addr.msf.minute >= 75)
3027 || (ra.addr.msf.second >= 60)
3028 || (ra.addr.msf.frame >= 75)) {
3032 ra.addr.lba = ((ra.addr.msf.minute * 4500)
3033 + (ra.addr.msf.second * 75)
3034 + ra.addr.msf.frame);
3036 sony_toc.lead_out_start_lba)
3037 || (ra.addr.lba + ra.nframes >=
3038 sony_toc.lead_out_start_lba)) {
3042 /* I know, this can go negative on an unsigned. However,
3043 the first thing done to the data is to add this value,
3044 so this should compensate and allow direct msf access. */
3045 ra.addr.lba -= LOG_START_OFFSET;
3050 return (read_audio(&ra));
3060 static int scd_spinup(void)
3062 unsigned char res_reg[12];
3063 unsigned int res_size;
3069 do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg, &res_size);
3071 /* The drive sometimes returns error 0. I don't know why, but ignore
3072 it. It seems to mean the drive has already done the operation. */
3073 if ((res_size < 2) || ((res_reg[0] != 0) && (res_reg[1] != 0))) {
3074 printk("Sony CDROM %s error (scd_open, spin up)\n",
3075 translate_error(res_reg[1]));
3079 do_sony_cd_cmd(SONY_READ_TOC_CMD, NULL, 0, res_reg, &res_size);
3081 /* The drive sometimes returns error 0. I don't know why, but ignore
3082 it. It seems to mean the drive has already done the operation. */
3083 if ((res_size < 2) || ((res_reg[0] != 0) && (res_reg[1] != 0))) {
3084 /* If the drive is already playing, it's ok. */
3085 if ((res_reg[1] == SONY_AUDIO_PLAYING_ERR)
3086 || (res_reg[1] == 0)) {
3090 /* If the drive says it is not spun up (even though we just did it!)
3091 then retry the operation at least a few times. */
3092 if ((res_reg[1] == SONY_NOT_SPIN_ERR)
3093 && (num_spin_ups < MAX_CDU31A_RETRIES)) {
3095 goto respinup_on_open;
3098 printk("Sony CDROM error %s (scd_open, read toc)\n",
3099 translate_error(res_reg[1]));
3100 do_sony_cd_cmd(SONY_SPIN_DOWN_CMD, NULL, 0, res_reg,
3108 * Open the drive for operations. Spin the drive up and read the table of
3109 * contents if these have not already been done.
3111 static int scd_open(struct cdrom_device_info *cdi, int openmode)
3113 unsigned char res_reg[12];
3114 unsigned int res_size;
3115 unsigned char params[2];
3117 if (sony_usage == 0) {
3118 if (scd_spinup() != 0)
3121 if (!sony_toc_read) {
3122 do_sony_cd_cmd(SONY_SPIN_DOWN_CMD, NULL, 0,
3123 res_reg, &res_size);
3127 /* For XA on the CDU31A only, we have to do special reads.
3128 The CDU33A handles XA automagically. */
3129 /* if ( (sony_toc.disk_type == SONY_XA_DISK_TYPE) */
3130 if ((sony_toc.disk_type != 0x00)
3131 && (!is_double_speed)) {
3132 params[0] = SONY_SD_DECODE_PARAM;
3134 do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
3135 params, 2, res_reg, &res_size);
3137 || ((res_reg[0] & 0xf0) == 0x20)) {
3139 ("CDU31A: Unable to set XA params: 0x%2.2x\n",
3144 /* A non-XA disk. Set the parms back if necessary. */
3145 else if (sony_xa_mode) {
3146 params[0] = SONY_SD_DECODE_PARAM;
3148 do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
3149 params, 2, res_reg, &res_size);
3151 || ((res_reg[0] & 0xf0) == 0x20)) {
3153 ("CDU31A: Unable to reset XA params: 0x%2.2x\n",
3169 * Close the drive. Spin it down if no task is using it. The spin
3170 * down will fail if playing audio, so audio play is OK.
3172 static void scd_release(struct cdrom_device_info *cdi)
3174 if (sony_usage == 1) {
3175 unsigned char res_reg[12];
3176 unsigned int res_size;
3178 do_sony_cd_cmd(SONY_SPIN_DOWN_CMD, NULL, 0, res_reg,
3186 struct block_device_operations scd_bdops =
3190 release: cdrom_release,
3192 check_media_change: cdrom_media_changed,
3195 static struct cdrom_device_ops scd_dops = {
3197 release:scd_release,
3198 drive_status:scd_drive_status,
3199 media_changed:scd_media_changed,
3200 tray_move:scd_tray_move,
3201 lock_door:scd_lock_door,
3202 select_speed:scd_select_speed,
3203 get_last_session:scd_get_last_session,
3204 get_mcn:scd_get_mcn,
3206 audio_ioctl:scd_audio_ioctl,
3207 dev_ioctl:scd_dev_ioctl,
3208 capability:CDC_OPEN_TRAY | CDC_CLOSE_TRAY | CDC_LOCK |
3209 CDC_SELECT_SPEED | CDC_MULTI_SESSION |
3210 CDC_MULTI_SESSION | CDC_MCN |
3211 CDC_MEDIA_CHANGED | CDC_PLAY_AUDIO |
3212 CDC_RESET | CDC_IOCTLS | CDC_DRIVE_STATUS,
3216 static struct cdrom_device_info scd_info = {
3223 /* The different types of disc loading mechanisms supported */
3224 static char *load_mech[] __initdata =
3225 { "caddy", "tray", "pop-up", "unknown" };
3228 get_drive_configuration(unsigned short base_io,
3229 unsigned char res_reg[], unsigned int *res_size)
3234 /* Set the base address */
3235 cdu31a_port = base_io;
3237 /* Set up all the register locations */
3238 sony_cd_cmd_reg = cdu31a_port + SONY_CMD_REG_OFFSET;
3239 sony_cd_param_reg = cdu31a_port + SONY_PARAM_REG_OFFSET;
3240 sony_cd_write_reg = cdu31a_port + SONY_WRITE_REG_OFFSET;
3241 sony_cd_control_reg = cdu31a_port + SONY_CONTROL_REG_OFFSET;
3242 sony_cd_status_reg = cdu31a_port + SONY_STATUS_REG_OFFSET;
3243 sony_cd_result_reg = cdu31a_port + SONY_RESULT_REG_OFFSET;
3244 sony_cd_read_reg = cdu31a_port + SONY_READ_REG_OFFSET;
3245 sony_cd_fifost_reg = cdu31a_port + SONY_FIFOST_REG_OFFSET;
3248 * Check to see if anything exists at the status register location.
3249 * I don't know if this is a good way to check, but it seems to work
3252 if (read_status_register() != 0xff) {
3254 * Reset the drive and wait for attention from it (to say it's reset).
3255 * If you don't wait, the next operation will probably fail.
3258 retry_count = jiffies + SONY_RESET_TIMEOUT;
3259 while (time_before(jiffies, retry_count)
3260 && (!is_attention())) {
3265 /* If attention is never seen probably not a CDU31a present */
3266 if (!is_attention()) {
3273 * Get the drive configuration.
3275 do_sony_cd_cmd(SONY_REQ_DRIVE_CONFIG_CMD,
3277 0, (unsigned char *) res_reg, res_size);
3281 /* Return an error */
3287 * Set up base I/O and interrupts, called from main.c.
3291 static int __init cdu31a_setup(char *strings)
3295 (void) get_options(strings, ARRAY_SIZE(ints), ints);
3298 cdu31a_port = ints[1];
3301 cdu31a_irq = ints[2];
3303 if ((strings != NULL) && (*strings != '\0')) {
3304 if (strcmp(strings, "PAS") == 0) {
3307 printk("CDU31A: Unknown interface type: %s\n",
3315 __setup("cdu31a=", cdu31a_setup);
3319 static int cdu31a_block_size;
3322 * Initialize the driver.
3324 int __init cdu31a_init(void)
3326 struct s_sony_drive_config drive_config;
3327 unsigned int res_size;
3336 * According to Alex Freed (freed@europa.orion.adobe.com), this is
3337 * required for the Fusion CD-16 package. If the sound driver is
3338 * loaded, it should work fine, but just in case...
3340 * The following turn on the CD-ROM interface for a Fusion CD-16.
3342 if (sony_pas_init) {
3349 /* Setting the base I/O address to 0xffff will disable it. */
3350 if (cdu31a_port == 0xffff) {
3351 } else if (cdu31a_port != 0) {
3352 tmp_irq = cdu31a_irq; /* Need IRQ 0 because we can't sleep here. */
3355 get_drive_configuration(cdu31a_port,
3356 drive_config.exec_status,
3359 && ((drive_config.exec_status[0] & 0xf0) == 0x00)) {
3363 cdu31a_irq = tmp_irq;
3367 while ((cdu31a_addresses[i].base != 0)
3368 && (!drive_found)) {
3369 if (check_region(cdu31a_addresses[i].base, 4)) {
3373 get_drive_configuration(cdu31a_addresses[i].base,
3374 drive_config.exec_status,
3377 && ((drive_config.exec_status[0] & 0xf0) ==
3380 cdu31a_irq = cdu31a_addresses[i].int_num;
3390 if (!request_region(cdu31a_port, 4, "cdu31a"))
3393 if (devfs_register_blkdev(MAJOR_NR, "cdu31a", &scd_bdops)) {
3394 printk("Unable to get major %d for CDU-31a\n",
3399 if (SONY_HWC_DOUBLE_SPEED(drive_config)) {
3400 is_double_speed = 1;
3403 tmp_irq = cdu31a_irq; /* Need IRQ 0 because we can't sleep here. */
3406 set_drive_params(sony_speed);
3408 cdu31a_irq = tmp_irq;
3410 if (cdu31a_irq > 0) {
3412 (cdu31a_irq, cdu31a_interrupt, SA_INTERRUPT,
3415 ("Unable to grab IRQ%d for the CDU31A driver\n",
3421 sprintf(msg, "Sony I/F CDROM : %8.8s %16.16s %8.8s\n",
3422 drive_config.vendor_id,
3423 drive_config.product_id,
3424 drive_config.product_rev_level);
3425 sprintf(buf, " Capabilities: %s",
3426 load_mech[SONY_HWC_GET_LOAD_MECH(drive_config)]);
3428 if (SONY_HWC_AUDIO_PLAYBACK(drive_config)) {
3429 strcat(msg, ", audio");
3431 deficiency |= CDC_PLAY_AUDIO;
3432 if (SONY_HWC_EJECT(drive_config)) {
3433 strcat(msg, ", eject");
3435 deficiency |= CDC_OPEN_TRAY;
3436 if (SONY_HWC_LED_SUPPORT(drive_config)) {
3437 strcat(msg, ", LED");
3439 if (SONY_HWC_ELECTRIC_VOLUME(drive_config)) {
3440 strcat(msg, ", elec. Vol");
3442 if (SONY_HWC_ELECTRIC_VOLUME_CTL(drive_config)) {
3443 strcat(msg, ", sep. Vol");
3445 if (is_double_speed) {
3446 strcat(msg, ", double speed");
3448 deficiency |= CDC_SELECT_SPEED;
3449 if (cdu31a_irq > 0) {
3450 sprintf(buf, ", irq %d", cdu31a_irq);
3456 strcmp("CD-ROM CDU31A", drive_config.product_id) == 0;
3458 blk_init_queue(BLK_DEFAULT_QUEUE(MAJOR_NR),
3460 read_ahead[MAJOR_NR] = CDU31A_READAHEAD;
3461 cdu31a_block_size = 1024; /* 1kB default block size */
3462 /* use 'mount -o block=2048' */
3463 blksize_size[MAJOR_NR] = &cdu31a_block_size;
3465 init_timer(&cdu31a_abort_timer);
3466 cdu31a_abort_timer.function = handle_abort_timeout;
3468 scd_info.dev = MKDEV(MAJOR_NR, 0);
3469 scd_info.mask = deficiency;
3470 strncpy(scd_info.name, "cdu31a", sizeof(scd_info.name));
3472 if (register_cdrom(&scd_info)) {
3475 devfs_plain_cdrom(&scd_info, &scd_bdops);
3487 printk("Unable to register CDU-31a with Uniform cdrom driver\n");
3488 blk_cleanup_queue(BLK_DEFAULT_QUEUE(MAJOR_NR));
3489 if (devfs_unregister_blkdev(MAJOR_NR, "cdu31a")) {
3490 printk("Can't unregister block device for cdu31a\n");
3493 release_region(cdu31a_port, 4);
3499 void __exit cdu31a_exit(void)
3501 if (unregister_cdrom(&scd_info)) {
3503 ("Can't unregister cdu31a from Uniform cdrom driver\n");
3506 if ((devfs_unregister_blkdev(MAJOR_NR, "cdu31a") == -EINVAL)) {
3507 printk("Can't unregister cdu31a\n");
3511 blk_cleanup_queue(BLK_DEFAULT_QUEUE(MAJOR_NR));
3514 free_irq(cdu31a_irq, NULL);
3516 release_region(cdu31a_port, 4);
3517 printk(KERN_INFO "cdu31a module released.\n");
3521 module_init(cdu31a_init);
3523 module_exit(cdu31a_exit);
3525 MODULE_LICENSE("GPL");