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.
147 #include <linux/major.h>
149 #include <linux/module.h>
151 #include <linux/errno.h>
152 #include <linux/signal.h>
153 #include <linux/sched.h>
154 #include <linux/timer.h>
155 #include <linux/fs.h>
156 #include <linux/kernel.h>
157 #include <linux/hdreg.h>
158 #include <linux/genhd.h>
159 #include <linux/ioport.h>
160 #include <linux/string.h>
161 #include <linux/malloc.h>
162 #include <linux/init.h>
163 #include <linux/interrupt.h>
165 #include <asm/system.h>
167 #include <asm/uaccess.h>
170 #include <linux/cdrom.h>
173 #define MAJOR_NR CDU31A_CDROM_MAJOR
174 #include <linux/blk.h>
176 #define CDU31A_READAHEAD 4 /* 128 sector, 64kB, 32 reads read-ahead */
177 #define CDU31A_MAX_CONSECUTIVE_ATTENTIONS 10
181 /* Define the following if you have data corruption problems. */
182 #undef SONY_POLL_EACH_BYTE
185 ** Edit the following data to change interrupts, DMA channels, etc.
186 ** Default is polled and no DMA. DMA is not recommended for double-speed
191 unsigned short base
; /* I/O Base Address */
192 short int_num
; /* Interrupt Number (-1 means scan for it,
193 0 means don't use) */
194 } cdu31a_addresses
[] __initdata
=
196 #if 0 /* No autoconfig any more. See Note at beginning
198 { 0x340, 0 }, /* Standard configuration Sony Interface */
199 { 0x1f88, 0 }, /* Fusion CD-16 */
200 { 0x230, 0 }, /* SoundBlaster 16 card */
201 { 0x360, 0 }, /* Secondary standard Sony Interface */
202 { 0x320, 0 }, /* Secondary standard Sony Interface */
203 { 0x330, 0 }, /* Secondary standard Sony Interface */
204 { 0x634, 0 }, /* Sound FX SC400 */
205 { 0x654, 0 }, /* Sound FX SC400 */
210 static int handle_sony_cd_attention(void);
211 static int read_subcode(void);
212 static void sony_get_toc(void);
213 static int scd_spinup(void);
214 /*static int scd_open(struct inode *inode, struct file *filp);*/
215 static int scd_open(struct cdrom_device_info
*, int);
216 static void do_sony_cd_cmd(unsigned char cmd
,
217 unsigned char *params
,
218 unsigned int num_params
,
219 unsigned char *result_buffer
,
220 unsigned int *result_size
);
221 static void size_to_buf(unsigned int size
,
224 /* Parameters for the read-ahead. */
225 static unsigned int sony_next_block
; /* Next 512 byte block offset */
226 static unsigned int sony_blocks_left
= 0; /* Number of 512 byte blocks left
227 in the current read command. */
230 /* The base I/O address of the Sony Interface. This is a variable (not a
231 #define) so it can be easily changed via some future ioctl() */
232 static unsigned int cdu31a_port
= 0;
233 MODULE_PARM(cdu31a_port
, "i");
236 * The following are I/O addresses of the various registers for the drive. The
237 * comment for the base address also applies here.
239 static volatile unsigned short sony_cd_cmd_reg
;
240 static volatile unsigned short sony_cd_param_reg
;
241 static volatile unsigned short sony_cd_write_reg
;
242 static volatile unsigned short sony_cd_control_reg
;
243 static volatile unsigned short sony_cd_status_reg
;
244 static volatile unsigned short sony_cd_result_reg
;
245 static volatile unsigned short sony_cd_read_reg
;
246 static volatile unsigned short sony_cd_fifost_reg
;
249 static int sony_spun_up
= 0; /* Has the drive been spun up? */
251 static int sony_speed
= 0; /* Last wanted speed */
253 static int sony_xa_mode
= 0; /* Is an XA disk in the drive
254 and the drive a CDU31A? */
256 static int sony_raw_data_mode
= 1; /* 1 if data tracks, 0 if audio.
257 For raw data reads. */
259 static unsigned int sony_usage
= 0; /* How many processes have the
262 static int sony_pas_init
= 0; /* Initialize the Pro-Audio
265 static struct s_sony_session_toc single_toc
; /* Holds the
269 static struct s_all_sessions_toc sony_toc
; /* entries gathered from all
272 static int sony_toc_read
= 0; /* Has the TOC been read for
275 static struct s_sony_subcode last_sony_subcode
; /* Points to the last
276 subcode address read */
278 static volatile int sony_inuse
= 0; /* Is the drive in use? Only one operation
281 static DECLARE_WAIT_QUEUE_HEAD(sony_wait
); /* Things waiting for the drive */
283 static struct task_struct
*has_cd_task
= NULL
; /* The task that is currently
284 using the CDROM drive, or
287 static int is_double_speed
= 0; /* does the drive support double speed ? */
288 static int is_a_cdu31a
= 1; /* Is the drive a CDU31A? */
290 static int is_auto_eject
= 1; /* Door has been locked? 1=No/0=Yes */
293 * The audio status uses the values from read subchannel data as specified
294 * in include/linux/cdrom.h.
296 static volatile int sony_audio_status
= CDROM_AUDIO_NO_STATUS
;
299 * The following are a hack for pausing and resuming audio play. The drive
300 * does not work as I would expect it, if you stop it then start it again,
301 * the drive seeks back to the beginning and starts over. This holds the
302 * position during a pause so a resume can restart it. It uses the
303 * audio status variable above to tell if it is paused.
305 static unsigned volatile char cur_pos_msf
[3] = { 0, 0, 0 };
306 static unsigned volatile char final_pos_msf
[3] = { 0, 0, 0 };
308 /* What IRQ is the drive using? 0 if none. */
309 static int cdu31a_irq
= 0;
310 MODULE_PARM(cdu31a_irq
, "i");
312 /* The interrupt handler will wake this queue up when it gets an
314 DECLARE_WAIT_QUEUE_HEAD(cdu31a_irq_wait
);
316 static int curr_control_reg
= 0; /* Current value of the control register */
318 /* A disk changed variable. When a disk change is detected, it will
319 all be set to TRUE. As the upper layers ask for disk_changed status
320 it will be cleared. */
321 static char disk_changed
;
323 /* Variable for using the readahead buffer. The readahead buffer
324 is used for raw sector reads and for blocksizes that are smaller
326 static char readahead_buffer
[CD_FRAMESIZE_RAW
];
327 static int readahead_dataleft
= 0;
328 static int readahead_bad
= 0;
330 /* Used to time a short period to abort an operation after the
331 drive has been idle for a while. This keeps the light on
332 the drive from flashing for very long. */
333 static struct timer_list cdu31a_abort_timer
;
335 /* Marks if the timeout has started an abort read. This is used
336 on entry to the drive to tell the code to read out the status
337 from the abort read. */
338 static int abort_read_started
= 0;
342 * This routine returns 1 if the disk has been changed since the last
343 * check or 0 if it hasn't.
346 scd_disk_change(kdev_t full_dev
)
350 retval
= disk_changed
;
357 * Uniform cdrom interface function
358 * report back, if disc has changed from time of last request.
361 scd_media_changed(struct cdrom_device_info
*cdi
, int disc_nr
)
363 return scd_disk_change(cdi
->dev
);
367 * Uniform cdrom interface function
368 * report back, if drive is ready
370 static int scd_drive_status(struct cdrom_device_info
*cdi
, int slot_nr
)
372 if (CDSL_CURRENT
!= slot_nr
) {
373 /* we have no changer support */
376 if (scd_spinup() == 0) {
379 return sony_spun_up
? CDS_DISC_OK
: CDS_DRIVE_NOT_READY
;
383 enable_interrupts(void)
385 curr_control_reg
|= ( SONY_ATTN_INT_EN_BIT
386 | SONY_RES_RDY_INT_EN_BIT
387 | SONY_DATA_RDY_INT_EN_BIT
);
388 outb(curr_control_reg
, sony_cd_control_reg
);
392 disable_interrupts(void)
394 curr_control_reg
&= ~( SONY_ATTN_INT_EN_BIT
395 | SONY_RES_RDY_INT_EN_BIT
396 | SONY_DATA_RDY_INT_EN_BIT
);
397 outb(curr_control_reg
, sony_cd_control_reg
);
401 * Wait a little while (used for polling the drive). If in initialization,
402 * setting a timeout doesn't work, so just loop for a while.
411 current
->state
= TASK_INTERRUPTIBLE
;
414 else /* Interrupt driven */
419 interruptible_sleep_on(&cdu31a_irq_wait
);
420 restore_flags(flags
);
426 * The following are convenience routine to read various status and set
427 * various conditions in the drive.
432 return((inb(sony_cd_status_reg
) & SONY_ATTN_BIT
) != 0);
438 return((inb(sony_cd_status_reg
) & SONY_BUSY_BIT
) != 0);
444 return((inb(sony_cd_status_reg
) & SONY_DATA_RDY_BIT
) != 0);
448 is_data_requested(void)
450 return((inb(sony_cd_status_reg
) & SONY_DATA_REQUEST_BIT
) != 0);
454 is_result_ready(void)
456 return((inb(sony_cd_status_reg
) & SONY_RES_RDY_BIT
) != 0);
460 is_param_write_rdy(void)
462 return((inb(sony_cd_fifost_reg
) & SONY_PARAM_WRITE_RDY_BIT
) != 0);
466 is_result_reg_not_empty(void)
468 return((inb(sony_cd_fifost_reg
) & SONY_RES_REG_NOT_EMP_BIT
) != 0);
474 curr_control_reg
= 0;
475 readahead_dataleft
= 0;
477 outb(SONY_DRIVE_RESET_BIT
, sony_cd_control_reg
);
481 * Uniform cdrom interface function
482 * reset drive and return when it is ready
484 static int scd_reset(struct cdrom_device_info
* cdi
)
490 retry_count
= jiffies
+ SONY_RESET_TIMEOUT
;
491 while (time_before(jiffies
, retry_count
) && (!is_attention()))
500 clear_attention(void)
502 outb(curr_control_reg
| SONY_ATTN_CLR_BIT
, sony_cd_control_reg
);
506 clear_result_ready(void)
508 outb(curr_control_reg
| SONY_RES_RDY_CLR_BIT
, sony_cd_control_reg
);
512 clear_data_ready(void)
514 outb(curr_control_reg
| SONY_DATA_RDY_CLR_BIT
, sony_cd_control_reg
);
518 clear_param_reg(void)
520 outb(curr_control_reg
| SONY_PARAM_CLR_BIT
, sony_cd_control_reg
);
523 static inline unsigned char
524 read_status_register(void)
526 return(inb(sony_cd_status_reg
));
529 static inline unsigned char
530 read_result_register(void)
532 return(inb(sony_cd_result_reg
));
535 static inline unsigned char
536 read_data_register(void)
538 return(inb(sony_cd_read_reg
));
542 write_param(unsigned char param
)
544 outb(param
, sony_cd_param_reg
);
548 write_cmd(unsigned char cmd
)
550 outb(curr_control_reg
| SONY_RES_RDY_INT_EN_BIT
, sony_cd_control_reg
);
551 outb(cmd
, sony_cd_cmd_reg
);
555 cdu31a_interrupt(int irq
, void *dev_id
, struct pt_regs
*regs
)
559 if (abort_read_started
)
561 /* We might be waiting for an abort to finish. Don't
562 disable interrupts yet, though, because we handle
564 /* Clear out the result registers. */
565 while (is_result_reg_not_empty())
567 val
= read_result_register();
570 clear_result_ready();
572 /* Clear out the data */
573 while (is_data_requested())
575 val
= read_data_register();
577 abort_read_started
= 0;
579 /* If something was waiting, wake it up now. */
580 if (waitqueue_active(&cdu31a_irq_wait
))
582 disable_interrupts();
583 wake_up(&cdu31a_irq_wait
);
586 else if (waitqueue_active(&cdu31a_irq_wait
))
588 disable_interrupts();
589 wake_up(&cdu31a_irq_wait
);
593 disable_interrupts();
594 printk("CDU31A: Got an interrupt but nothing was waiting\n");
599 * give more verbose error messages
601 static unsigned char *translate_error( unsigned char err_code
)
603 static unsigned char errbuf
[80];
606 case 0x10: return "illegal command ";
607 case 0x11: return "illegal parameter ";
609 case 0x20: return "not loaded ";
610 case 0x21: return "no disc ";
611 case 0x22: return "not spinning ";
612 case 0x23: return "spinning ";
613 case 0x25: return "spindle servo ";
614 case 0x26: return "focus servo ";
615 case 0x29: return "eject mechanism ";
616 case 0x2a: return "audio playing ";
617 case 0x2c: return "emergency eject ";
619 case 0x30: return "focus ";
620 case 0x31: return "frame sync ";
621 case 0x32: return "subcode address ";
622 case 0x33: return "block sync ";
623 case 0x34: return "header address ";
625 case 0x40: return "illegal track read ";
626 case 0x41: return "mode 0 read ";
627 case 0x42: return "illegal mode read ";
628 case 0x43: return "illegal block size read ";
629 case 0x44: return "mode read ";
630 case 0x45: return "form read ";
631 case 0x46: return "leadout read ";
632 case 0x47: return "buffer overrun ";
634 case 0x53: return "unrecoverable CIRC ";
635 case 0x57: return "unrecoverable LECC ";
637 case 0x60: return "no TOC ";
638 case 0x61: return "invalid subcode data ";
639 case 0x63: return "focus on TOC read ";
640 case 0x64: return "frame sync on TOC read ";
641 case 0x65: return "TOC data ";
643 case 0x70: return "hardware failure ";
644 case 0x91: return "leadin ";
645 case 0x92: return "leadout ";
646 case 0x93: return "data track ";
648 sprintf(errbuf
, "unknown 0x%02x ", err_code
);
653 * Set the drive parameters so the drive will auto-spin-up when a
657 set_drive_params(int want_doublespeed
)
659 unsigned char res_reg
[12];
660 unsigned int res_size
;
661 unsigned char params
[3];
664 params
[0] = SONY_SD_AUTO_SPIN_DOWN_TIME
;
665 params
[1] = 0x00; /* Never spin down the drive. */
666 do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD
,
671 if ((res_size
< 2) || ((res_reg
[0] & 0xf0) == 0x20))
673 printk(" Unable to set spin-down time: 0x%2.2x\n", res_reg
[1]);
676 params
[0] = SONY_SD_MECH_CONTROL
;
677 params
[1] = SONY_AUTO_SPIN_UP_BIT
; /* Set auto spin up */
679 if (is_auto_eject
) params
[1] |= SONY_AUTO_EJECT_BIT
;
681 if (is_double_speed
&& want_doublespeed
)
683 params
[1] |= SONY_DOUBLE_SPEED_BIT
; /* Set the drive to double speed if
686 do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD
,
691 if ((res_size
< 2) || ((res_reg
[0] & 0xf0) == 0x20))
693 printk(" Unable to set mechanical parameters: 0x%2.2x\n", res_reg
[1]);
698 * Uniform cdrom interface function
699 * select reading speed for data access
701 static int scd_select_speed(struct cdrom_device_info
*cdi
, int speed
)
706 sony_speed
= speed
- 1;
708 set_drive_params(sony_speed
);
713 * Uniform cdrom interface function
714 * lock or unlock eject button
716 static int scd_lock_door(struct cdrom_device_info
*cdi
, int lock
)
718 if (lock
== 0 && sony_usage
== 1)
720 /* Unlock the door, only if nobody is using the drive */
725 set_drive_params(sony_speed
);
730 * This code will reset the drive and attempt to restore sane parameters.
733 restart_on_error(void)
735 unsigned char res_reg
[12];
736 unsigned int res_size
;
737 unsigned int retry_count
;
740 printk("cdu31a: Resetting drive on error\n");
742 retry_count
= jiffies
+ SONY_RESET_TIMEOUT
;
743 while (time_before(jiffies
, retry_count
) && (!is_attention()))
747 set_drive_params(sony_speed
);
748 do_sony_cd_cmd(SONY_SPIN_UP_CMD
, NULL
, 0, res_reg
, &res_size
);
749 if ((res_size
< 2) || ((res_reg
[0] & 0xf0) == 0x20))
751 printk("cdu31a: Unable to spin up drive: 0x%2.2x\n", res_reg
[1]);
754 current
->state
= TASK_INTERRUPTIBLE
;
755 schedule_timeout(2*HZ
);
761 * This routine writes data to the parameter register. Since this should
762 * happen fairly fast, it is polled with no OS waits between.
765 write_params(unsigned char *params
,
768 unsigned int retry_count
;
771 retry_count
= SONY_READY_RETRIES
;
772 while ((retry_count
> 0) && (!is_param_write_rdy()))
776 if (!is_param_write_rdy())
781 while (num_params
> 0)
783 write_param(*params
);
793 * The following reads data from the command result register. It is a
794 * fairly complex routine, all status info flows back through this
795 * interface. The algorithm is stolen directly from the flowcharts in
799 get_result(unsigned char *result_buffer
,
800 unsigned int *result_size
)
804 unsigned int retry_count
;
807 while (handle_sony_cd_attention())
809 /* Wait for the result data to be ready */
810 retry_count
= jiffies
+ SONY_JIFFIES_TIMEOUT
;
811 while (time_before(jiffies
, retry_count
) && (is_busy() || (!(is_result_ready()))))
815 while (handle_sony_cd_attention())
818 if (is_busy() || (!(is_result_ready())))
821 printk("CDU31A timeout out %d\n", __LINE__
);
823 result_buffer
[0] = 0x20;
824 result_buffer
[1] = SONY_TIMEOUT_OP_ERR
;
830 * Get the first two bytes. This determines what else needs
833 clear_result_ready();
834 a
= read_result_register();
838 /* Check for block error status result. */
839 if ((a
& 0xf0) == 0x50)
845 b
= read_result_register();
851 * 0x20 means an error occurred. Byte 2 will have the error code.
852 * Otherwise, the command succeeded, byte 2 will have the count of
853 * how many more status bytes are coming.
855 * The result register can be read 10 bytes at a time, a wait for
856 * result ready to be asserted must be done between every 10 bytes.
858 if ((a
& 0xf0) != 0x20)
864 *result_buffer
= read_result_register();
872 retry_count
= SONY_READY_RETRIES
;
873 while ((retry_count
> 0) && (!is_result_ready()))
877 if (!is_result_ready())
880 printk("CDU31A timeout out %d\n", __LINE__
);
882 result_buffer
[0] = 0x20;
883 result_buffer
[1] = SONY_TIMEOUT_OP_ERR
;
888 clear_result_ready();
892 *result_buffer
= read_result_register();
901 retry_count
= SONY_READY_RETRIES
;
902 while ((retry_count
> 0) && (!is_result_ready()))
906 if (!is_result_ready())
909 printk("CDU31A timeout out %d\n", __LINE__
);
911 result_buffer
[0] = 0x20;
912 result_buffer
[1] = SONY_TIMEOUT_OP_ERR
;
921 *result_buffer
= read_result_register();
930 * Do a command that does not involve data transfer. This routine must
931 * be re-entrant from the same task to support being called from the
932 * data operation code when an error occurs.
935 do_sony_cd_cmd(unsigned char cmd
,
936 unsigned char *params
,
937 unsigned int num_params
,
938 unsigned char *result_buffer
,
939 unsigned int *result_size
)
941 unsigned int retry_count
;
949 if (current
!= has_cd_task
) /* Allow recursive calls to this routine */
953 interruptible_sleep_on(&sony_wait
);
954 if (signal_pending(current
))
956 result_buffer
[0] = 0x20;
957 result_buffer
[1] = SONY_SIGNAL_OP_ERR
;
959 restore_flags(flags
);
964 has_cd_task
= current
;
975 while (handle_sony_cd_attention())
980 retry_count
= jiffies
+ SONY_JIFFIES_TIMEOUT
;
981 while (time_before(jiffies
, retry_count
) && (is_busy()))
985 while (handle_sony_cd_attention())
991 printk("CDU31A timeout out %d\n", __LINE__
);
993 result_buffer
[0] = 0x20;
994 result_buffer
[1] = SONY_TIMEOUT_OP_ERR
;
999 clear_result_ready();
1002 write_params(params
, num_params
);
1005 get_result(result_buffer
, result_size
);
1008 if ( ((result_buffer
[0] & 0xf0) == 0x20)
1009 && (num_retries
< MAX_CDU31A_RETRIES
))
1012 current
->state
= TASK_INTERRUPTIBLE
;
1013 schedule_timeout(HZ
/10); /* Wait .1 seconds on retries */
1014 goto retry_cd_operation
;
1017 if (!recursive_call
)
1021 wake_up_interruptible(&sony_wait
);
1024 restore_flags(flags
);
1029 * Handle an attention from the drive. This will return 1 if it found one
1030 * or 0 if not (if one is found, the caller might want to call again).
1032 * This routine counts the number of consecutive times it is called
1033 * (since this is always called from a while loop until it returns
1034 * a 0), and returns a 0 if it happens too many times. This will help
1038 handle_sony_cd_attention(void)
1040 unsigned char atten_code
;
1041 static int num_consecutive_attentions
= 0;
1046 printk("Entering handle_sony_cd_attention\n");
1050 if (num_consecutive_attentions
> CDU31A_MAX_CONSECUTIVE_ATTENTIONS
)
1052 printk("cdu31a: Too many consecutive attentions: %d\n",
1053 num_consecutive_attentions
);
1054 num_consecutive_attentions
= 0;
1056 printk("Leaving handle_sony_cd_attention at %d\n", __LINE__
);
1062 atten_code
= read_result_register();
1066 /* Someone changed the CD. Mark it as changed */
1067 case SONY_MECH_LOADED_ATTN
:
1070 sony_audio_status
= CDROM_AUDIO_NO_STATUS
;
1071 sony_blocks_left
= 0;
1074 case SONY_SPIN_DOWN_COMPLETE_ATTN
:
1075 /* Mark the disk as spun down. */
1079 case SONY_AUDIO_PLAY_DONE_ATTN
:
1080 sony_audio_status
= CDROM_AUDIO_COMPLETED
;
1084 case SONY_EJECT_PUSHED_ATTN
:
1087 sony_audio_status
= CDROM_AUDIO_INVALID
;
1091 case SONY_LEAD_IN_ERR_ATTN
:
1092 case SONY_LEAD_OUT_ERR_ATTN
:
1093 case SONY_DATA_TRACK_ERR_ATTN
:
1094 case SONY_AUDIO_PLAYBACK_ERR_ATTN
:
1095 sony_audio_status
= CDROM_AUDIO_ERROR
;
1099 num_consecutive_attentions
++;
1101 printk("Leaving handle_sony_cd_attention at %d\n", __LINE__
);
1105 else if (abort_read_started
)
1107 while (is_result_reg_not_empty())
1109 val
= read_result_register();
1112 clear_result_ready();
1113 /* Clear out the data */
1114 while (is_data_requested())
1116 val
= read_data_register();
1118 abort_read_started
= 0;
1120 printk("Leaving handle_sony_cd_attention at %d\n", __LINE__
);
1125 num_consecutive_attentions
= 0;
1127 printk("Leaving handle_sony_cd_attention at %d\n", __LINE__
);
1133 /* Convert from an integer 0-99 to BCD */
1134 static inline unsigned int
1135 int_to_bcd(unsigned int val
)
1140 retval
= (val
/ 10) << 4;
1141 retval
= retval
| val
% 10;
1146 /* Convert from BCD to an integer from 0-99 */
1148 bcd_to_int(unsigned int bcd
)
1150 return((((bcd
>> 4) & 0x0f) * 10) + (bcd
& 0x0f));
1155 * Convert a logical sector value (like the OS would want to use for
1156 * a block device) to an MSF format.
1159 log_to_msf(unsigned int log
, unsigned char *msf
)
1161 log
= log
+ LOG_START_OFFSET
;
1162 msf
[0] = int_to_bcd(log
/ 4500);
1164 msf
[1] = int_to_bcd(log
/ 75);
1165 msf
[2] = int_to_bcd(log
% 75);
1170 * Convert an MSF format to a logical sector.
1173 msf_to_log(unsigned char *msf
)
1180 log
+= msf
[0] * 4500;
1181 log
= log
- LOG_START_OFFSET
;
1188 * Take in integer size value and put it into a buffer like
1189 * the drive would want to see a number-of-sector value.
1192 size_to_buf(unsigned int size
,
1195 buf
[0] = size
/ 65536;
1196 size
= size
% 65536;
1197 buf
[1] = size
/ 256;
1198 buf
[2] = size
% 256;
1201 /* Starts a read operation. Returns 0 on success and 1 on failure.
1202 The read operation used here allows multiple sequential sectors
1203 to be read and status returned for each sector. The driver will
1204 read the output one at a time as the requests come and abort the
1205 operation if the requested sector is not the next one from the
1208 start_request(unsigned int sector
,
1210 int read_nsect_only
)
1212 unsigned char params
[6];
1213 unsigned int read_size
;
1214 unsigned int retry_count
;
1218 printk("Entering start_request\n");
1220 log_to_msf(sector
, params
);
1221 /* If requested, read exactly what was asked. */
1222 if (read_nsect_only
)
1227 * If the full read-ahead would go beyond the end of the media, trim
1228 * it back to read just till the end of the media.
1230 else if ((sector
+ nsect
) >= sony_toc
.lead_out_start_lba
)
1232 read_size
= sony_toc
.lead_out_start_lba
- sector
;
1234 /* Read the full readahead amount. */
1237 read_size
= CDU31A_READAHEAD
/ 4;
1239 size_to_buf(read_size
, ¶ms
[3]);
1242 * Clear any outstanding attentions and wait for the drive to
1243 * complete any pending operations.
1245 while (handle_sony_cd_attention())
1248 retry_count
= jiffies
+ SONY_JIFFIES_TIMEOUT
;
1249 while (time_before(jiffies
, retry_count
) && (is_busy()))
1253 while (handle_sony_cd_attention())
1259 printk("CDU31A: Timeout while waiting to issue command\n");
1261 printk("Leaving start_request at %d\n", __LINE__
);
1267 /* Issue the command */
1268 clear_result_ready();
1271 write_params(params
, 6);
1272 write_cmd(SONY_READ_BLKERR_STAT_CMD
);
1274 sony_blocks_left
= read_size
* 4;
1275 sony_next_block
= sector
* 4;
1276 readahead_dataleft
= 0;
1279 printk("Leaving start_request at %d\n", __LINE__
);
1284 printk("Leaving start_request at %d\n", __LINE__
);
1288 /* Abort a pending read operation. Clear all the drive status and
1289 readahead variables. */
1293 unsigned char result_reg
[2];
1298 do_sony_cd_cmd(SONY_ABORT_CMD
, NULL
, 0, result_reg
, &result_size
);
1299 if ((result_reg
[0] & 0xf0) == 0x20)
1301 printk("CDU31A: Error aborting read, %s error\n",
1306 while (is_result_reg_not_empty())
1308 val
= read_result_register();
1311 clear_result_ready();
1312 /* Clear out the data */
1313 while (is_data_requested())
1315 val
= read_data_register();
1318 sony_blocks_left
= 0;
1319 readahead_dataleft
= 0;
1323 /* Called when the timer times out. This will abort the
1324 pending read operation. */
1326 handle_abort_timeout(unsigned long data
)
1328 unsigned long flags
;
1331 printk("Entering handle_abort_timeout\n");
1335 /* If it is in use, ignore it. */
1338 /* We can't use abort_read(), because it will sleep
1339 or schedule in the timer interrupt. Just start
1340 the operation, finish it on the next access to
1342 clear_result_ready();
1344 write_cmd(SONY_ABORT_CMD
);
1346 sony_blocks_left
= 0;
1347 readahead_dataleft
= 0;
1349 abort_read_started
= 1;
1351 restore_flags(flags
);
1353 printk("Leaving handle_abort_timeout\n");
1357 /* Actually get data and status from the drive. */
1359 input_data(char *buffer
,
1360 unsigned int bytesleft
,
1361 unsigned int nblocks
,
1362 unsigned int offset
,
1366 volatile unsigned char val
;
1370 printk("Entering input_data\n");
1372 /* If an XA disk on a CDU31A, skip the first 12 bytes of data from
1373 the disk. The real data is after that. */
1376 for(i
=0; i
<CD_XA_HEAD
; i
++)
1378 val
= read_data_register();
1384 if (bytesleft
== 2048) /* 2048 byte direct buffer transfer */
1386 insb(sony_cd_read_reg
, buffer
, 2048);
1387 readahead_dataleft
= 0;
1391 /* If the input read did not align with the beginning of the block,
1392 skip the necessary bytes. */
1395 insb(sony_cd_read_reg
, readahead_buffer
, skip
);
1398 /* Get the data into the buffer. */
1399 insb(sony_cd_read_reg
, &buffer
[offset
], bytesleft
);
1401 /* Get the rest of the data into the readahead buffer at the
1403 readahead_dataleft
= (2048 - skip
) - bytesleft
;
1404 insb(sony_cd_read_reg
,
1405 readahead_buffer
+ bytesleft
,
1406 readahead_dataleft
);
1408 sony_blocks_left
-= nblocks
;
1409 sony_next_block
+= nblocks
;
1411 /* If an XA disk, we have to clear out the rest of the unused
1412 error correction data. */
1415 for(i
=0; i
<CD_XA_TAIL
; i
++)
1417 val
= read_data_register();
1421 printk("Leaving input_data at %d\n", __LINE__
);
1425 /* read data from the drive. Note the nsect must be <= 4. */
1427 read_data_block(char *buffer
,
1429 unsigned int nblocks
,
1430 unsigned char res_reg
[],
1433 unsigned int retry_count
;
1434 unsigned int bytesleft
;
1435 unsigned int offset
;
1440 printk("Entering read_data_block\n");
1446 bytesleft
= nblocks
* 512;
1449 /* If the data in the read-ahead does not match the block offset,
1450 then fix things up. */
1451 if (((block
% 4) * 512) != ((2048 - readahead_dataleft
) % 2048))
1453 sony_next_block
+= block
% 4;
1454 sony_blocks_left
-= block
% 4;
1455 skip
= (block
% 4) * 512;
1462 /* We have readahead data in the buffer, get that first before we
1463 decide if a read is necessary. */
1464 if (readahead_dataleft
!= 0)
1466 if (bytesleft
> readahead_dataleft
)
1468 /* The readahead will not fill the requested buffer, but
1469 get the data out of the readahead into the buffer. */
1471 readahead_buffer
+ (2048 - readahead_dataleft
),
1472 readahead_dataleft
);
1473 readahead_dataleft
= 0;
1474 bytesleft
-= readahead_dataleft
;
1475 offset
+= readahead_dataleft
;
1479 /* The readahead will fill the whole buffer, get the data
1482 readahead_buffer
+ (2048 - readahead_dataleft
),
1484 readahead_dataleft
-= bytesleft
;
1486 sony_blocks_left
-= nblocks
;
1487 sony_next_block
+= nblocks
;
1489 /* If the data in the readahead is bad, return an error so the
1490 driver will abort the buffer. */
1494 res_reg
[1] = SONY_BAD_DATA_ERR
;
1498 if (readahead_dataleft
== 0)
1503 /* Final transfer is done for read command, get final result. */
1504 if (sony_blocks_left
== 0)
1506 get_result(res_reg
, res_size
);
1509 printk("Leaving read_data_block at %d\n", __LINE__
);
1515 /* Wait for the drive to tell us we have something */
1516 retry_count
= jiffies
+ SONY_JIFFIES_TIMEOUT
;
1517 while (time_before(jiffies
, retry_count
) && !(is_data_ready()))
1519 while (handle_sony_cd_attention())
1524 if (!(is_data_ready()))
1526 if (is_result_ready())
1528 get_result(res_reg
, res_size
);
1529 if ((res_reg
[0] & 0xf0) != 0x20)
1531 printk("CDU31A: Got result that should have been error: %d\n",
1534 res_reg
[1] = SONY_BAD_DATA_ERR
;
1542 printk("CDU31A timeout out %d\n", __LINE__
);
1545 res_reg
[1] = SONY_TIMEOUT_OP_ERR
;
1552 input_data(buffer
, bytesleft
, nblocks
, offset
, skip
);
1554 /* Wait for the status from the drive. */
1555 retry_count
= jiffies
+ SONY_JIFFIES_TIMEOUT
;
1556 while (time_before(jiffies
, retry_count
) && !(is_result_ready()))
1558 while (handle_sony_cd_attention())
1564 if (!is_result_ready())
1567 printk("CDU31A timeout out %d\n", __LINE__
);
1570 res_reg
[1] = SONY_TIMEOUT_OP_ERR
;
1576 get_result(res_reg
, res_size
);
1578 /* If we got a buffer status, handle that. */
1579 if ((res_reg
[0] & 0xf0) == 0x50)
1582 if ( (res_reg
[0] == SONY_NO_CIRC_ERR_BLK_STAT
)
1583 || (res_reg
[0] == SONY_NO_LECC_ERR_BLK_STAT
)
1584 || (res_reg
[0] == SONY_RECOV_LECC_ERR_BLK_STAT
))
1586 /* The data was successful, but if data was read from
1587 the readahead and it was bad, set the whole
1593 res_reg
[1] = SONY_BAD_DATA_ERR
;
1599 printk("CDU31A: Data block error: 0x%x\n", res_reg
[0]);
1601 res_reg
[1] = SONY_BAD_DATA_ERR
;
1604 /* Data is in the readahead buffer but an error was returned.
1605 Make sure future requests don't use the data. */
1606 if (bytesleft
!= 2048)
1612 /* Final transfer is done for read command, get final result. */
1613 if (sony_blocks_left
== 0)
1615 get_result(res_reg
, res_size
);
1618 else if ((res_reg
[0] & 0xf0) != 0x20)
1620 /* The drive gave me bad status, I don't know what to do.
1621 Reset the driver and return an error. */
1622 printk("CDU31A: Invalid block status: 0x%x\n", res_reg
[0]);
1625 res_reg
[1] = SONY_BAD_DATA_ERR
;
1631 printk("Leaving read_data_block at %d\n", __LINE__
);
1637 * The OS calls this to perform a read or write operation to the drive.
1638 * Write obviously fail. Reads to a read ahead of sony_buffer_size
1639 * bytes to help speed operations. This especially helps since the OS
1640 * uses 1024 byte blocks and the drive uses 2048 byte blocks. Since most
1641 * data access on a CD is done sequentially, this saves a lot of operations.
1644 do_cdu31a_request(void)
1648 unsigned char res_reg
[12];
1649 unsigned int res_size
;
1651 unsigned long flags
;
1655 printk("Entering do_cdu31a_request\n");
1659 * Make sure no one else is using the driver; wait for them
1660 * to finish if it is so.
1666 interruptible_sleep_on(&sony_wait
);
1667 if (signal_pending(current
))
1669 restore_flags(flags
);
1670 if (CURRENT
&& CURRENT
->rq_status
!= RQ_INACTIVE
)
1674 restore_flags(flags
);
1676 printk("Leaving do_cdu31a_request at %d\n", __LINE__
);
1682 has_cd_task
= current
;
1684 /* Get drive status before doing anything. */
1685 while (handle_sony_cd_attention())
1688 /* Make sure we have a valid TOC. */
1691 spin_unlock_irq(&io_request_lock
);
1693 /* Make sure the timer is cancelled. */
1694 del_timer(&cdu31a_abort_timer
);
1698 cdu31a_request_startover
:
1700 * The beginning here is stolen from the hard disk driver. I hope
1703 if (!(CURRENT
) || CURRENT
->rq_status
== RQ_INACTIVE
)
1705 goto end_do_cdu31a_request
;
1713 /* I don't use INIT_REQUEST because it calls return, which would
1714 return without unlocking the device. It shouldn't matter,
1715 but just to be safe... */
1716 if (MAJOR(CURRENT
->rq_dev
) != MAJOR_NR
)
1718 panic(DEVICE_NAME
": request list destroyed");
1722 if (!buffer_locked(CURRENT
->bh
))
1724 panic(DEVICE_NAME
": block not locked");
1728 block
= CURRENT
->sector
;
1729 nblock
= CURRENT
->nr_sectors
;
1733 printk("CDU31A: TOC not read\n");
1735 goto cdu31a_request_startover
;
1738 switch(CURRENT
->cmd
)
1742 * If the block address is invalid or the request goes beyond the end of
1743 * the media, return an error.
1746 if ((block
/ 4) < sony_toc
.start_track_lba
)
1748 printk("CDU31A: Request before beginning of media\n");
1750 goto cdu31a_request_startover
;
1753 if ((block
/ 4) >= sony_toc
.lead_out_start_lba
)
1755 printk("CDU31A: Request past end of media\n");
1757 goto cdu31a_request_startover
;
1759 if (((block
+ nblock
) / 4) >= sony_toc
.lead_out_start_lba
)
1761 printk("CDU31A: Request past end of media\n");
1763 goto cdu31a_request_startover
;
1769 while (handle_sony_cd_attention())
1774 printk("CDU31A: TOC not read\n");
1776 goto cdu31a_request_startover
;
1779 /* If no data is left to be read from the drive, start the
1781 if (sony_blocks_left
== 0)
1783 if (start_request(block
/ 4, CDU31A_READAHEAD
/ 4, 0))
1786 goto cdu31a_request_startover
;
1789 /* If the requested block is not the next one waiting in
1790 the driver, abort the current operation and start a
1792 else if (block
!= sony_next_block
)
1795 printk("CDU31A Warning: Read for block %d, expected %d\n",
1802 printk("CDU31A: TOC not read\n");
1804 goto cdu31a_request_startover
;
1806 if (start_request(block
/ 4, CDU31A_READAHEAD
/ 4, 0))
1808 printk("CDU31a: start request failed\n");
1810 goto cdu31a_request_startover
;
1814 read_data_block(CURRENT
->buffer
, block
, nblock
, res_reg
, &res_size
);
1815 if (res_reg
[0] == 0x20)
1817 if (num_retries
> MAX_CDU31A_RETRIES
)
1820 goto cdu31a_request_startover
;
1824 if (res_reg
[1] == SONY_NOT_SPIN_ERR
)
1826 do_sony_cd_cmd(SONY_SPIN_UP_CMD
, NULL
, 0, res_reg
, &res_size
);
1830 printk("CDU31A: %s error for block %d, nblock %d\n", translate_error(res_reg
[1]), block
, nblock
);
1832 goto try_read_again
;
1845 panic("CDU31A: Unknown cmd");
1849 end_do_cdu31a_request
:
1850 spin_lock_irq(&io_request_lock
);
1852 /* After finished, cancel any pending operations. */
1855 /* Start a timer to time out after a while to disable
1857 cdu31a_abort_timer
.expires
= jiffies
+ 2*HZ
; /* Wait 2 seconds */
1858 add_timer(&cdu31a_abort_timer
);
1863 wake_up_interruptible(&sony_wait
);
1864 restore_flags(flags
);
1866 printk("Leaving do_cdu31a_request at %d\n", __LINE__
);
1872 * Read the table of contents from the drive and set up TOC if
1878 unsigned char res_reg
[2];
1879 unsigned int res_size
;
1880 unsigned char parms
[1];
1883 int totaltracks
= 0;
1888 printk("Entering sony_get_toc\n");
1895 /* Ignore the result, since it might error if spinning already. */
1896 do_sony_cd_cmd(SONY_SPIN_UP_CMD
, NULL
, 0, res_reg
, &res_size
);
1898 do_sony_cd_cmd(SONY_READ_TOC_CMD
, NULL
, 0, res_reg
, &res_size
);
1900 /* The drive sometimes returns error 0. I don't know why, but ignore
1901 it. It seems to mean the drive has already done the operation. */
1902 if ((res_size
< 2) || ((res_reg
[0] != 0) && (res_reg
[1] != 0)))
1904 /* If the drive is already playing, it's ok. */
1905 if ((res_reg
[1] == SONY_AUDIO_PLAYING_ERR
) || (res_reg
[1] == 0))
1907 goto gettoc_drive_spinning
;
1910 /* If the drive says it is not spun up (even though we just did it!)
1911 then retry the operation at least a few times. */
1912 if ( (res_reg
[1] == SONY_NOT_SPIN_ERR
)
1913 && (num_spin_ups
< MAX_CDU31A_RETRIES
))
1916 goto respinup_on_gettoc
;
1919 printk("cdu31a: Error reading TOC: %x %s\n",
1920 res_reg
[0], translate_error(res_reg
[1]));
1924 gettoc_drive_spinning
:
1926 /* The idea here is we keep asking for sessions until the command
1927 fails. Then we know what the last valid session on the disk is.
1928 No need to check session 0, since session 0 is the same as session
1929 1; the command returns different information if you give it 0.
1932 memset(&sony_toc
, 0x0e, sizeof(sony_toc
));
1933 memset(&single_toc
, 0x0f, sizeof(single_toc
));
1938 /* This seems to slow things down enough to make it work. This
1939 * appears to be a problem in do_sony_cd_cmd. This printk seems
1940 * to address the symptoms... -Erik */
1942 printk("cdu31a: Trying session %d\n", session
);
1945 do_sony_cd_cmd(SONY_READ_TOC_SPEC_CMD
,
1952 printk("%2.2x %2.2x\n", res_reg
[0], res_reg
[1]);
1955 if ((res_size
< 2) || ((res_reg
[0] & 0xf0) == 0x20))
1957 /* An error reading the TOC, this must be past the last session. */
1959 printk("Yikes! Couldn't read any sessions!");
1963 printk("Reading session %d\n", session
);
1967 do_sony_cd_cmd(SONY_REQ_TOC_DATA_SPEC_CMD
,
1970 (unsigned char *) &single_toc
,
1972 if ((res_size
< 2) || ((single_toc
.exec_status
[0] & 0xf0) == 0x20))
1974 printk("cdu31a: Error reading session %d: %x %s\n",
1976 single_toc
.exec_status
[0],
1977 translate_error(single_toc
.exec_status
[1]));
1978 /* An error reading the TOC. Return without sony_toc_read
1983 printk("add0 %01x, con0 %01x, poi0 %02x, 1st trk %d, dsktyp %x, dum0 %x\n",
1984 single_toc
.address0
, single_toc
.control0
, single_toc
.point0
,
1985 bcd_to_int(single_toc
.first_track_num
), single_toc
.disk_type
, single_toc
.dummy0
);
1986 printk("add1 %01x, con1 %01x, poi1 %02x, lst trk %d, dummy1 %x, dum2 %x\n",
1987 single_toc
.address1
, single_toc
.control1
, single_toc
.point1
,
1988 bcd_to_int(single_toc
.last_track_num
), single_toc
.dummy1
, single_toc
.dummy2
);
1989 printk("add2 %01x, con2 %01x, poi2 %02x leadout start min %d, sec %d, frame %d\n",
1990 single_toc
.address2
, single_toc
.control2
, single_toc
.point2
,
1991 bcd_to_int(single_toc
.lead_out_start_msf
[0]),
1992 bcd_to_int(single_toc
.lead_out_start_msf
[1]),
1993 bcd_to_int(single_toc
.lead_out_start_msf
[2]));
1994 if (res_size
> 18 && single_toc
.pointb0
> 0xaf)
1995 printk("addb0 %01x, conb0 %01x, poib0 %02x, nextsession min %d, sec %d, frame %d\n"
1996 "#mode5_ptrs %02d, max_start_outer_leadout_msf min %d, sec %d, frame %d\n",
1997 single_toc
.addressb0
, single_toc
.controlb0
, single_toc
.pointb0
,
1998 bcd_to_int(single_toc
.next_poss_prog_area_msf
[0]),
1999 bcd_to_int(single_toc
.next_poss_prog_area_msf
[1]),
2000 bcd_to_int(single_toc
.next_poss_prog_area_msf
[2]),
2001 single_toc
.num_mode_5_pointers
,
2002 bcd_to_int(single_toc
.max_start_outer_leadout_msf
[0]),
2003 bcd_to_int(single_toc
.max_start_outer_leadout_msf
[1]),
2004 bcd_to_int(single_toc
.max_start_outer_leadout_msf
[2]));
2005 if (res_size
> 27 && single_toc
.pointb1
> 0xaf)
2006 printk("addb1 %01x, conb1 %01x, poib1 %02x, %x %x %x %x #skipint_ptrs %d, #skiptrkassign %d %x\n",
2007 single_toc
.addressb1
, single_toc
.controlb1
, single_toc
.pointb1
,
2008 single_toc
.dummyb0_1
[0],
2009 single_toc
.dummyb0_1
[1],
2010 single_toc
.dummyb0_1
[2],
2011 single_toc
.dummyb0_1
[3],
2012 single_toc
.num_skip_interval_pointers
,
2013 single_toc
.num_skip_track_assignments
,
2014 single_toc
.dummyb0_2
);
2015 if (res_size
> 36 && single_toc
.pointb2
> 0xaf)
2016 printk("addb2 %01x, conb2 %01x, poib2 %02x, %02x %02x %02x %02x %02x %02x %02x\n",
2017 single_toc
.addressb2
, single_toc
.controlb2
, single_toc
.pointb2
,
2018 single_toc
.tracksb2
[0],
2019 single_toc
.tracksb2
[1],
2020 single_toc
.tracksb2
[2],
2021 single_toc
.tracksb2
[3],
2022 single_toc
.tracksb2
[4],
2023 single_toc
.tracksb2
[5],
2024 single_toc
.tracksb2
[6]);
2025 if (res_size
> 45 && single_toc
.pointb3
> 0xaf)
2026 printk("addb3 %01x, conb3 %01x, poib3 %02x, %02x %02x %02x %02x %02x %02x %02x\n",
2027 single_toc
.addressb3
, single_toc
.controlb3
, single_toc
.pointb3
,
2028 single_toc
.tracksb3
[0],
2029 single_toc
.tracksb3
[1],
2030 single_toc
.tracksb3
[2],
2031 single_toc
.tracksb3
[3],
2032 single_toc
.tracksb3
[4],
2033 single_toc
.tracksb3
[5],
2034 single_toc
.tracksb3
[6]);
2035 if (res_size
> 54 && single_toc
.pointb4
> 0xaf)
2036 printk("addb4 %01x, conb4 %01x, poib4 %02x, %02x %02x %02x %02x %02x %02x %02x\n",
2037 single_toc
.addressb4
, single_toc
.controlb4
, single_toc
.pointb4
,
2038 single_toc
.tracksb4
[0],
2039 single_toc
.tracksb4
[1],
2040 single_toc
.tracksb4
[2],
2041 single_toc
.tracksb4
[3],
2042 single_toc
.tracksb4
[4],
2043 single_toc
.tracksb4
[5],
2044 single_toc
.tracksb4
[6]);
2045 if (res_size
> 63 && single_toc
.pointc0
> 0xaf)
2046 printk("addc0 %01x, conc0 %01x, poic0 %02x, %02x %02x %02x %02x %02x %02x %02x\n",
2047 single_toc
.addressc0
, single_toc
.controlc0
, single_toc
.pointc0
,
2048 single_toc
.dummyc0
[0],
2049 single_toc
.dummyc0
[1],
2050 single_toc
.dummyc0
[2],
2051 single_toc
.dummyc0
[3],
2052 single_toc
.dummyc0
[4],
2053 single_toc
.dummyc0
[5],
2054 single_toc
.dummyc0
[6]);
2059 sony_toc
.lead_out_start_msf
[0] = bcd_to_int(single_toc
.lead_out_start_msf
[0]);
2060 sony_toc
.lead_out_start_msf
[1] = bcd_to_int(single_toc
.lead_out_start_msf
[1]);
2061 sony_toc
.lead_out_start_msf
[2] = bcd_to_int(single_toc
.lead_out_start_msf
[2]);
2062 sony_toc
.lead_out_start_lba
= single_toc
.lead_out_start_lba
=
2063 msf_to_log(sony_toc
.lead_out_start_msf
);
2065 /* For points that do not exist, move the data over them
2066 to the right location. */
2067 if (single_toc
.pointb0
!= 0xb0)
2069 memmove(((char *) &single_toc
) + 27,
2070 ((char *) &single_toc
) + 18,
2074 else if (res_size
> 18) {
2075 sony_toc
.lead_out_start_msf
[0] = bcd_to_int(single_toc
.max_start_outer_leadout_msf
[0]);
2076 sony_toc
.lead_out_start_msf
[1] = bcd_to_int(single_toc
.max_start_outer_leadout_msf
[1]);
2077 sony_toc
.lead_out_start_msf
[2] = bcd_to_int(single_toc
.max_start_outer_leadout_msf
[2]);
2078 sony_toc
.lead_out_start_lba
= msf_to_log(sony_toc
.lead_out_start_msf
);
2080 if (single_toc
.pointb1
!= 0xb1)
2082 memmove(((char *) &single_toc
) + 36,
2083 ((char *) &single_toc
) + 27,
2087 if (single_toc
.pointb2
!= 0xb2)
2089 memmove(((char *) &single_toc
) + 45,
2090 ((char *) &single_toc
) + 36,
2094 if (single_toc
.pointb3
!= 0xb3)
2096 memmove(((char *) &single_toc
) + 54,
2097 ((char *) &single_toc
) + 45,
2101 if (single_toc
.pointb4
!= 0xb4)
2103 memmove(((char *) &single_toc
) + 63,
2104 ((char *) &single_toc
) + 54,
2108 if (single_toc
.pointc0
!= 0xc0)
2110 memmove(((char *) &single_toc
) + 72,
2111 ((char *) &single_toc
) + 63,
2117 printk("start track lba %u, leadout start lba %u\n",
2118 single_toc
.start_track_lba
, single_toc
.lead_out_start_lba
);
2120 for (i
= 0; i
< 1 + bcd_to_int(single_toc
.last_track_num
)
2121 - bcd_to_int(single_toc
.first_track_num
); i
++) {
2122 printk("trk %02d: add 0x%01x, con 0x%01x, track %02d, start min %02d, sec %02d, frame %02d\n",
2123 i
, single_toc
.tracks
[i
].address
, single_toc
.tracks
[i
].control
,
2124 bcd_to_int(single_toc
.tracks
[i
].track
),
2125 bcd_to_int(single_toc
.tracks
[i
].track_start_msf
[0]),
2126 bcd_to_int(single_toc
.tracks
[i
].track_start_msf
[1]),
2127 bcd_to_int(single_toc
.tracks
[i
].track_start_msf
[2]));
2128 if (mint
> bcd_to_int(single_toc
.tracks
[i
].track
))
2129 mint
= bcd_to_int(single_toc
.tracks
[i
].track
);
2130 if (maxt
< bcd_to_int(single_toc
.tracks
[i
].track
))
2131 maxt
= bcd_to_int(single_toc
.tracks
[i
].track
);
2133 printk("min track number %d, max track number %d\n", mint
, maxt
);
2137 /* prepare a special table of contents for a CD-I disc. They don't have one. */
2138 if (single_toc
.disk_type
== 0x10 &&
2139 single_toc
.first_track_num
== 2 &&
2140 single_toc
.last_track_num
== 2 /* CD-I */)
2142 sony_toc
.tracks
[totaltracks
].address
= 1;
2143 sony_toc
.tracks
[totaltracks
].control
= 4; /* force data tracks */
2144 sony_toc
.tracks
[totaltracks
].track
= 1;
2145 sony_toc
.tracks
[totaltracks
].track_start_msf
[0] = 0;
2146 sony_toc
.tracks
[totaltracks
].track_start_msf
[1] = 2;
2147 sony_toc
.tracks
[totaltracks
].track_start_msf
[2] = 0;
2151 /* gather track entries from this session */
2153 for (i
= 0; i
< 1 + bcd_to_int(single_toc
.last_track_num
)
2154 - bcd_to_int(single_toc
.first_track_num
); i
++, totaltracks
++) {
2155 sony_toc
.tracks
[totaltracks
].address
= single_toc
.tracks
[i
].address
;
2156 sony_toc
.tracks
[totaltracks
].control
= single_toc
.tracks
[i
].control
;
2157 sony_toc
.tracks
[totaltracks
].track
= bcd_to_int(single_toc
.tracks
[i
].track
);
2158 sony_toc
.tracks
[totaltracks
].track_start_msf
[0] =
2159 bcd_to_int(single_toc
.tracks
[i
].track_start_msf
[0]);
2160 sony_toc
.tracks
[totaltracks
].track_start_msf
[1] =
2161 bcd_to_int(single_toc
.tracks
[i
].track_start_msf
[1]);
2162 sony_toc
.tracks
[totaltracks
].track_start_msf
[2] =
2163 bcd_to_int(single_toc
.tracks
[i
].track_start_msf
[2]);
2165 single_toc
.start_track_lba
= msf_to_log(sony_toc
.tracks
[totaltracks
].track_start_msf
);
2166 if (mint
> sony_toc
.tracks
[totaltracks
].track
)
2167 mint
= sony_toc
.tracks
[totaltracks
].track
;
2168 if (maxt
< sony_toc
.tracks
[totaltracks
].track
)
2169 maxt
= sony_toc
.tracks
[totaltracks
].track
;
2172 sony_toc
.first_track_num
= mint
;
2173 sony_toc
.last_track_num
= maxt
;
2174 /* Disk type of last session wins. For example:
2175 CD-Extra has disk type 0 for the first session, so
2176 a dumb HiFi CD player thinks it is a plain audio CD.
2177 We are interested in the disk type of the last session,
2178 which is 0x20 (XA) for CD-Extra, so we can access the
2180 sony_toc
.disk_type
= single_toc
.disk_type
;
2181 sony_toc
.sessions
= session
;
2183 /* don't believe everything :-) */
2185 single_toc
.start_track_lba
= 0;
2186 sony_toc
.start_track_lba
= single_toc
.start_track_lba
;
2188 if (session
> 1 && single_toc
.pointb0
== 0xb0 &&
2189 sony_toc
.lead_out_start_lba
== single_toc
.lead_out_start_lba
)
2194 /* Let's not get carried away... */
2197 printk("cdu31a: too many sessions: %d\n", session
);
2202 sony_toc
.track_entries
= totaltracks
;
2203 /* add one entry for the LAST track with track number CDROM_LEADOUT */
2204 sony_toc
.tracks
[totaltracks
].address
= single_toc
.address2
;
2205 sony_toc
.tracks
[totaltracks
].control
= single_toc
.control2
;
2206 sony_toc
.tracks
[totaltracks
].track
= CDROM_LEADOUT
;
2207 sony_toc
.tracks
[totaltracks
].track_start_msf
[0] =
2208 sony_toc
.lead_out_start_msf
[0];
2209 sony_toc
.tracks
[totaltracks
].track_start_msf
[1] =
2210 sony_toc
.lead_out_start_msf
[1];
2211 sony_toc
.tracks
[totaltracks
].track_start_msf
[2] =
2212 sony_toc
.lead_out_start_msf
[2];
2217 printk("Disk session %d, start track: %d, stop track: %d\n",
2219 single_toc
.start_track_lba
,
2220 single_toc
.lead_out_start_lba
);
2224 printk("Leaving sony_get_toc\n");
2230 * Uniform cdrom interface function
2231 * return multisession offset and sector information
2233 static int scd_get_last_session(struct cdrom_device_info
*cdi
,
2234 struct cdrom_multisession
*ms_info
)
2236 if (ms_info
== NULL
)
2242 ms_info
->addr_format
= CDROM_LBA
;
2243 ms_info
->addr
.lba
= sony_toc
.start_track_lba
;
2244 ms_info
->xa_flag
= sony_toc
.disk_type
== SONY_XA_DISK_TYPE
||
2245 sony_toc
.disk_type
== 0x10 /* CDI */;
2251 * Search for a specific track in the table of contents.
2254 find_track(int track
)
2258 for (i
= 0; i
<= sony_toc
.track_entries
; i
++)
2260 if (sony_toc
.tracks
[i
].track
== track
)
2271 * Read the subcode and put it in last_sony_subcode for future use.
2276 unsigned int res_size
;
2279 do_sony_cd_cmd(SONY_REQ_SUBCODE_ADDRESS_CMD
,
2282 (unsigned char *) &last_sony_subcode
,
2284 if ((res_size
< 2) || ((last_sony_subcode
.exec_status
[0] & 0xf0) == 0x20))
2286 printk("Sony CDROM error %s (read_subcode)\n",
2287 translate_error(last_sony_subcode
.exec_status
[1]));
2291 last_sony_subcode
.track_num
= bcd_to_int(last_sony_subcode
.track_num
);
2292 last_sony_subcode
.index_num
= bcd_to_int(last_sony_subcode
.index_num
);
2293 last_sony_subcode
.abs_msf
[0] = bcd_to_int(last_sony_subcode
.abs_msf
[0]);
2294 last_sony_subcode
.abs_msf
[1] = bcd_to_int(last_sony_subcode
.abs_msf
[1]);
2295 last_sony_subcode
.abs_msf
[2] = bcd_to_int(last_sony_subcode
.abs_msf
[2]);
2297 last_sony_subcode
.rel_msf
[0] = bcd_to_int(last_sony_subcode
.rel_msf
[0]);
2298 last_sony_subcode
.rel_msf
[1] = bcd_to_int(last_sony_subcode
.rel_msf
[1]);
2299 last_sony_subcode
.rel_msf
[2] = bcd_to_int(last_sony_subcode
.rel_msf
[2]);
2304 * Uniform cdrom interface function
2305 * return the media catalog number found on some older audio cds
2308 scd_get_mcn(struct cdrom_device_info
*cdi
, struct cdrom_mcn
*mcn
)
2310 unsigned char resbuffer
[2 + 14];
2311 unsigned char *mcnp
= mcn
->medium_catalog_number
;
2312 unsigned char *resp
= resbuffer
+ 3;
2313 unsigned int res_size
;
2315 memset(mcn
->medium_catalog_number
, 0, 14);
2316 do_sony_cd_cmd(SONY_REQ_UPC_EAN_CMD
,
2321 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.
2352 sony_get_subchnl_info(struct cdrom_subchnl
*schi
)
2354 /* Get attention stuff */
2355 while (handle_sony_cd_attention())
2364 switch (sony_audio_status
)
2366 case CDROM_AUDIO_NO_STATUS
:
2367 case CDROM_AUDIO_PLAY
:
2368 if (read_subcode() < 0)
2374 case CDROM_AUDIO_PAUSED
:
2375 case CDROM_AUDIO_COMPLETED
:
2379 case CDROM_AUDIO_NO_STATUS
:
2380 schi
->cdsc_audiostatus
= sony_audio_status
;
2384 case CDROM_AUDIO_INVALID
:
2385 case CDROM_AUDIO_ERROR
:
2390 schi
->cdsc_audiostatus
= sony_audio_status
;
2391 schi
->cdsc_adr
= last_sony_subcode
.address
;
2392 schi
->cdsc_ctrl
= last_sony_subcode
.control
;
2393 schi
->cdsc_trk
= last_sony_subcode
.track_num
;
2394 schi
->cdsc_ind
= last_sony_subcode
.index_num
;
2395 if (schi
->cdsc_format
== CDROM_MSF
)
2397 schi
->cdsc_absaddr
.msf
.minute
= last_sony_subcode
.abs_msf
[0];
2398 schi
->cdsc_absaddr
.msf
.second
= last_sony_subcode
.abs_msf
[1];
2399 schi
->cdsc_absaddr
.msf
.frame
= last_sony_subcode
.abs_msf
[2];
2401 schi
->cdsc_reladdr
.msf
.minute
= last_sony_subcode
.rel_msf
[0];
2402 schi
->cdsc_reladdr
.msf
.second
= last_sony_subcode
.rel_msf
[1];
2403 schi
->cdsc_reladdr
.msf
.frame
= last_sony_subcode
.rel_msf
[2];
2405 else if (schi
->cdsc_format
== CDROM_LBA
)
2407 schi
->cdsc_absaddr
.lba
= msf_to_log(last_sony_subcode
.abs_msf
);
2408 schi
->cdsc_reladdr
.lba
= 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
,
2420 unsigned char res_reg
[],
2423 unsigned int retry_count
;
2432 /* Wait for the drive to tell us we have something */
2433 retry_count
= jiffies
+ SONY_JIFFIES_TIMEOUT
;
2434 continue_read_audio_wait
:
2435 while (time_before(jiffies
, retry_count
) && !(is_data_ready())
2436 && !(is_result_ready() || result_read
))
2438 while (handle_sony_cd_attention())
2443 if (!(is_data_ready()))
2445 if (is_result_ready() && !result_read
)
2447 get_result(res_reg
, res_size
);
2449 /* Read block status and continue waiting for data. */
2450 if ((res_reg
[0] & 0xf0) == 0x50)
2453 goto continue_read_audio_wait
;
2455 /* Invalid data from the drive. Shut down the operation. */
2456 else if ((res_reg
[0] & 0xf0) != 0x20)
2458 printk("CDU31A: Got result that should have been error: %d\n",
2461 res_reg
[1] = SONY_BAD_DATA_ERR
;
2469 printk("CDU31A timeout out %d\n", __LINE__
);
2472 res_reg
[1] = SONY_TIMEOUT_OP_ERR
;
2481 /* If data block, then get 2340 bytes offset by 12. */
2482 if (sony_raw_data_mode
)
2484 insb(sony_cd_read_reg
, buffer
+ CD_XA_HEAD
, CD_FRAMESIZE_RAW1
);
2488 /* Audio gets the whole 2352 bytes. */
2489 insb(sony_cd_read_reg
, buffer
, CD_FRAMESIZE_RAW
);
2492 /* If I haven't already gotten the result, get it now. */
2495 /* Wait for the drive to tell us we have something */
2496 retry_count
= jiffies
+ SONY_JIFFIES_TIMEOUT
;
2497 while (time_before(jiffies
, retry_count
) && !(is_result_ready()))
2499 while (handle_sony_cd_attention())
2505 if (!is_result_ready())
2508 printk("CDU31A timeout out %d\n", __LINE__
);
2511 res_reg
[1] = SONY_TIMEOUT_OP_ERR
;
2518 get_result(res_reg
, res_size
);
2522 if ((res_reg
[0] & 0xf0) == 0x50)
2524 if ( (res_reg
[0] == SONY_NO_CIRC_ERR_BLK_STAT
)
2525 || (res_reg
[0] == SONY_NO_LECC_ERR_BLK_STAT
)
2526 || (res_reg
[0] == SONY_RECOV_LECC_ERR_BLK_STAT
)
2527 || (res_reg
[0] == SONY_NO_ERR_DETECTION_STAT
))
2529 /* Ok, nothing to do. */
2533 printk("CDU31A: Data block error: 0x%x\n", res_reg
[0]);
2535 res_reg
[1] = SONY_BAD_DATA_ERR
;
2539 else if ((res_reg
[0] & 0xf0) != 0x20)
2541 /* The drive gave me bad status, I don't know what to do.
2542 Reset the driver and return an error. */
2543 printk("CDU31A: Invalid block status: 0x%x\n", res_reg
[0]);
2546 res_reg
[1] = SONY_BAD_DATA_ERR
;
2552 /* Perform a raw data read. This will automatically detect the
2553 track type and read the proper data (audio or data). */
2555 read_audio(struct cdrom_read_audio
*ra
)
2558 unsigned char params
[2];
2559 unsigned char res_reg
[12];
2560 unsigned int res_size
;
2561 unsigned int cframe
;
2562 unsigned long flags
;
2565 * Make sure no one else is using the driver; wait for them
2566 * to finish if it is so.
2572 interruptible_sleep_on(&sony_wait
);
2573 if (signal_pending(current
))
2575 restore_flags(flags
);
2580 has_cd_task
= current
;
2581 restore_flags(flags
);
2588 /* Set the drive to do raw operations. */
2589 params
[0] = SONY_SD_DECODE_PARAM
;
2590 params
[1] = 0x06 | sony_raw_data_mode
;
2591 do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD
,
2596 if ((res_size
< 2) || ((res_reg
[0] & 0xf0) == 0x20))
2598 printk("CDU31A: Unable to set decode params: 0x%2.2x\n", res_reg
[1]);
2602 /* From here down, we have to goto exit_read_audio instead of returning
2603 because the drive parameters have to be set back to data before
2607 /* start_request clears out any readahead data, so it should be safe. */
2608 if (start_request(ra
->addr
.lba
, ra
->nframes
, 1))
2611 goto exit_read_audio
;
2614 /* For every requested frame. */
2616 while (cframe
< ra
->nframes
)
2618 read_audio_data(readahead_buffer
, res_reg
, &res_size
);
2619 if ((res_reg
[0] & 0xf0) == 0x20)
2621 if (res_reg
[1] == SONY_BAD_DATA_ERR
)
2623 printk("CDU31A: Data error on audio sector %d\n",
2624 ra
->addr
.lba
+ cframe
);
2626 else if (res_reg
[1] == SONY_ILL_TRACK_R_ERR
)
2628 /* Illegal track type, change track types and start over. */
2629 sony_raw_data_mode
= (sony_raw_data_mode
) ? 0 : 1;
2631 /* Set the drive mode. */
2632 params
[0] = SONY_SD_DECODE_PARAM
;
2633 params
[1] = 0x06 | sony_raw_data_mode
;
2634 do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD
,
2639 if ((res_size
< 2) || ((res_reg
[0] & 0xf0) == 0x20))
2641 printk("CDU31A: Unable to set decode params: 0x%2.2x\n", res_reg
[1]);
2643 goto exit_read_audio
;
2646 /* Restart the request on the current frame. */
2647 if (start_request(ra
->addr
.lba
+ cframe
, ra
->nframes
- cframe
, 1))
2650 goto exit_read_audio
;
2653 /* Don't go back to the top because don't want to get into
2654 and infinite loop. A lot of code gets duplicated, but
2655 that's no big deal, I don't guess. */
2656 read_audio_data(readahead_buffer
, res_reg
, &res_size
);
2657 if ((res_reg
[0] & 0xf0) == 0x20)
2659 if (res_reg
[1] == SONY_BAD_DATA_ERR
)
2661 printk("CDU31A: Data error on audio sector %d\n",
2662 ra
->addr
.lba
+ cframe
);
2666 printk("CDU31A: Error reading audio data on sector %d: %s\n",
2667 ra
->addr
.lba
+ cframe
,
2668 translate_error(res_reg
[1]));
2670 goto exit_read_audio
;
2675 copy_to_user((char *) (ra
->buf
+ (CD_FRAMESIZE_RAW
* cframe
)),
2676 (char *) readahead_buffer
,
2682 printk("CDU31A: Error reading audio data on sector %d: %s\n",
2683 ra
->addr
.lba
+ cframe
,
2684 translate_error(res_reg
[1]));
2686 goto exit_read_audio
;
2691 copy_to_user((char *) (ra
->buf
+ (CD_FRAMESIZE_RAW
* cframe
)),
2692 (char *) readahead_buffer
,
2699 get_result(res_reg
, &res_size
);
2700 if ((res_reg
[0] & 0xf0) == 0x20)
2702 printk("CDU31A: Error return from audio read: %s\n",
2703 translate_error(res_reg
[1]));
2705 goto exit_read_audio
;
2710 /* Set the drive mode back to the proper one for the disk. */
2711 params
[0] = SONY_SD_DECODE_PARAM
;
2720 do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD
,
2725 if ((res_size
< 2) || ((res_reg
[0] & 0xf0) == 0x20))
2727 printk("CDU31A: Unable to reset decode params: 0x%2.2x\n", res_reg
[1]);
2733 wake_up_interruptible(&sony_wait
);
2739 do_sony_cd_cmd_chk(const char *name
,
2741 unsigned char *params
,
2742 unsigned int num_params
,
2743 unsigned char *result_buffer
,
2744 unsigned int *result_size
)
2746 do_sony_cd_cmd(cmd
, params
, num_params
, result_buffer
, result_size
);
2747 if ((*result_size
< 2) || ((result_buffer
[0] & 0xf0) == 0x20))
2749 printk("Sony CDROM error %s (CDROM%s)\n", translate_error(result_buffer
[1]), name
);
2756 * Uniform cdrom interface function
2759 static int scd_tray_move(struct cdrom_device_info
*cdi
, int position
)
2761 if (position
== 1 /* open tray */)
2763 unsigned char res_reg
[12];
2764 unsigned int res_size
;
2766 do_sony_cd_cmd(SONY_AUDIO_STOP_CMD
, NULL
, 0, res_reg
, &res_size
);
2767 do_sony_cd_cmd(SONY_SPIN_DOWN_CMD
, NULL
, 0, res_reg
, &res_size
);
2769 sony_audio_status
= CDROM_AUDIO_INVALID
;
2770 return do_sony_cd_cmd_chk("EJECT",SONY_EJECT_CMD
, NULL
, 0, res_reg
, &res_size
);
2772 if (0 == scd_spinup())
2779 * The big ugly ioctl handler.
2781 static int scd_audio_ioctl(struct cdrom_device_info
*cdi
,
2785 unsigned char res_reg
[12];
2786 unsigned int res_size
;
2787 unsigned char params
[7];
2793 case CDROMSTART
: /* Spin up the drive */
2794 return do_sony_cd_cmd_chk("START",SONY_SPIN_UP_CMD
, NULL
, 0, res_reg
, &res_size
);
2797 case CDROMSTOP
: /* Spin down the drive */
2798 do_sony_cd_cmd(SONY_AUDIO_STOP_CMD
, NULL
, 0, res_reg
, &res_size
);
2801 * Spin the drive down, ignoring the error if the disk was
2802 * already not spinning.
2804 sony_audio_status
= CDROM_AUDIO_NO_STATUS
;
2805 return do_sony_cd_cmd_chk("STOP",SONY_SPIN_DOWN_CMD
, NULL
, 0, res_reg
, &res_size
);
2807 case CDROMPAUSE
: /* Pause the drive */
2808 if(do_sony_cd_cmd_chk("PAUSE", SONY_AUDIO_STOP_CMD
, NULL
, 0, res_reg
, &res_size
))
2810 /* Get the current position and save it for resuming */
2811 if (read_subcode() < 0)
2815 cur_pos_msf
[0] = last_sony_subcode
.abs_msf
[0];
2816 cur_pos_msf
[1] = last_sony_subcode
.abs_msf
[1];
2817 cur_pos_msf
[2] = last_sony_subcode
.abs_msf
[2];
2818 sony_audio_status
= CDROM_AUDIO_PAUSED
;
2822 case CDROMRESUME
: /* Start the drive after being paused */
2823 if (sony_audio_status
!= CDROM_AUDIO_PAUSED
)
2828 do_sony_cd_cmd(SONY_SPIN_UP_CMD
, NULL
, 0, res_reg
, &res_size
);
2830 /* Start the drive at the saved position. */
2831 params
[1] = int_to_bcd(cur_pos_msf
[0]);
2832 params
[2] = int_to_bcd(cur_pos_msf
[1]);
2833 params
[3] = int_to_bcd(cur_pos_msf
[2]);
2834 params
[4] = int_to_bcd(final_pos_msf
[0]);
2835 params
[5] = int_to_bcd(final_pos_msf
[1]);
2836 params
[6] = int_to_bcd(final_pos_msf
[2]);
2838 if(do_sony_cd_cmd_chk("RESUME",SONY_AUDIO_PLAYBACK_CMD
, params
, 7, res_reg
, &res_size
)<0)
2840 sony_audio_status
= CDROM_AUDIO_PLAY
;
2843 case CDROMPLAYMSF
: /* Play starting at the given MSF address. */
2844 do_sony_cd_cmd(SONY_SPIN_UP_CMD
, NULL
, 0, res_reg
, &res_size
);
2846 /* The parameters are given in int, must be converted */
2849 params
[i
] = int_to_bcd(((unsigned char *)arg
)[i
-1]);
2852 if(do_sony_cd_cmd_chk("PLAYMSF",SONY_AUDIO_PLAYBACK_CMD
, params
, 7, res_reg
, &res_size
)<0)
2855 /* Save the final position for pauses and resumes */
2856 final_pos_msf
[0] = bcd_to_int(params
[4]);
2857 final_pos_msf
[1] = bcd_to_int(params
[5]);
2858 final_pos_msf
[2] = bcd_to_int(params
[6]);
2859 sony_audio_status
= CDROM_AUDIO_PLAY
;
2862 case CDROMREADTOCHDR
: /* Read the table of contents header */
2864 struct cdrom_tochdr
*hdr
;
2872 hdr
= (struct cdrom_tochdr
*) arg
;
2873 hdr
->cdth_trk0
= sony_toc
.first_track_num
;
2874 hdr
->cdth_trk1
= sony_toc
.last_track_num
;
2878 case CDROMREADTOCENTRY
: /* Read a given table of contents entry */
2880 struct cdrom_tocentry
*entry
;
2882 unsigned char *msf_val
= NULL
;
2890 entry
= (struct cdrom_tocentry
*) arg
;
2892 track_idx
= find_track(entry
->cdte_track
);
2898 entry
->cdte_adr
= sony_toc
.tracks
[track_idx
].address
;
2899 entry
->cdte_ctrl
= sony_toc
.tracks
[track_idx
].control
;
2900 msf_val
= sony_toc
.tracks
[track_idx
].track_start_msf
;
2902 /* Logical buffer address or MSF format requested? */
2903 if (entry
->cdte_format
== CDROM_LBA
)
2905 entry
->cdte_addr
.lba
= msf_to_log(msf_val
);
2907 else if (entry
->cdte_format
== CDROM_MSF
)
2909 entry
->cdte_addr
.msf
.minute
= *msf_val
;
2910 entry
->cdte_addr
.msf
.second
= *(msf_val
+1);
2911 entry
->cdte_addr
.msf
.frame
= *(msf_val
+2);
2917 case CDROMPLAYTRKIND
: /* Play a track. This currently ignores index. */
2919 struct cdrom_ti
*ti
= (struct cdrom_ti
*) arg
;
2928 if ( (ti
->cdti_trk0
< sony_toc
.first_track_num
)
2929 || (ti
->cdti_trk0
> sony_toc
.last_track_num
)
2930 || (ti
->cdti_trk1
< ti
->cdti_trk0
))
2935 track_idx
= find_track(ti
->cdti_trk0
);
2940 params
[1] = int_to_bcd(sony_toc
.tracks
[track_idx
].track_start_msf
[0]);
2941 params
[2] = int_to_bcd(sony_toc
.tracks
[track_idx
].track_start_msf
[1]);
2942 params
[3] = int_to_bcd(sony_toc
.tracks
[track_idx
].track_start_msf
[2]);
2945 * If we want to stop after the last track, use the lead-out
2948 if (ti
->cdti_trk1
>= sony_toc
.last_track_num
)
2950 track_idx
= find_track(CDROM_LEADOUT
);
2954 track_idx
= find_track(ti
->cdti_trk1
+1);
2960 params
[4] = int_to_bcd(sony_toc
.tracks
[track_idx
].track_start_msf
[0]);
2961 params
[5] = int_to_bcd(sony_toc
.tracks
[track_idx
].track_start_msf
[1]);
2962 params
[6] = int_to_bcd(sony_toc
.tracks
[track_idx
].track_start_msf
[2]);
2965 do_sony_cd_cmd(SONY_SPIN_UP_CMD
, NULL
, 0, res_reg
, &res_size
);
2967 do_sony_cd_cmd(SONY_AUDIO_PLAYBACK_CMD
, params
, 7, res_reg
, &res_size
);
2969 if ((res_size
< 2) || ((res_reg
[0] & 0xf0) == 0x20))
2971 printk("Params: %x %x %x %x %x %x %x\n", params
[0], params
[1],
2972 params
[2], params
[3], params
[4], params
[5], params
[6]);
2973 printk("Sony CDROM error %s (CDROMPLAYTRKIND)\n", translate_error(res_reg
[1]));
2977 /* Save the final position for pauses and resumes */
2978 final_pos_msf
[0] = bcd_to_int(params
[4]);
2979 final_pos_msf
[1] = bcd_to_int(params
[5]);
2980 final_pos_msf
[2] = bcd_to_int(params
[6]);
2981 sony_audio_status
= CDROM_AUDIO_PLAY
;
2985 case CDROMVOLCTRL
: /* Volume control. What volume does this change, anyway? */
2987 struct cdrom_volctrl
*volctrl
= (struct cdrom_volctrl
*) arg
;
2989 params
[0] = SONY_SD_AUDIO_VOLUME
;
2990 params
[1] = volctrl
->channel0
;
2991 params
[2] = volctrl
->channel1
;
2992 return do_sony_cd_cmd_chk("VOLCTRL",SONY_SET_DRIVE_PARAM_CMD
, params
, 3, res_reg
, &res_size
);
2994 case CDROMSUBCHNL
: /* Get subchannel info */
2995 return sony_get_subchnl_info((struct cdrom_subchnl
*)arg
);
3002 static int scd_dev_ioctl(struct cdrom_device_info
*cdi
,
3010 case CDROMREADAUDIO
: /* Read 2352 byte audio tracks and 2340 byte
3013 struct cdrom_read_audio ra
;
3022 if(copy_from_user(&ra
, (char *) arg
, sizeof(ra
)))
3025 if (ra
.nframes
== 0)
3030 i
=verify_area(VERIFY_WRITE
, ra
.buf
, CD_FRAMESIZE_RAW
* ra
.nframes
);
3034 if (ra
.addr_format
== CDROM_LBA
)
3036 if ( (ra
.addr
.lba
>= sony_toc
.lead_out_start_lba
)
3037 || (ra
.addr
.lba
+ ra
.nframes
>= sony_toc
.lead_out_start_lba
))
3042 else if (ra
.addr_format
== CDROM_MSF
)
3044 if ( (ra
.addr
.msf
.minute
>= 75)
3045 || (ra
.addr
.msf
.second
>= 60)
3046 || (ra
.addr
.msf
.frame
>= 75))
3051 ra
.addr
.lba
= ( (ra
.addr
.msf
.minute
* 4500)
3052 + (ra
.addr
.msf
.second
* 75)
3053 + ra
.addr
.msf
.frame
);
3054 if ( (ra
.addr
.lba
>= sony_toc
.lead_out_start_lba
)
3055 || (ra
.addr
.lba
+ ra
.nframes
>= sony_toc
.lead_out_start_lba
))
3060 /* I know, this can go negative on an unsigned. However,
3061 the first thing done to the data is to add this value,
3062 so this should compensate and allow direct msf access. */
3063 ra
.addr
.lba
-= LOG_START_OFFSET
;
3070 return(read_audio(&ra
));
3080 static int scd_spinup(void)
3082 unsigned char res_reg
[12];
3083 unsigned int res_size
;
3089 do_sony_cd_cmd(SONY_SPIN_UP_CMD
, NULL
, 0, res_reg
, &res_size
);
3091 /* The drive sometimes returns error 0. I don't know why, but ignore
3092 it. It seems to mean the drive has already done the operation. */
3093 if ((res_size
< 2) || ((res_reg
[0] != 0) && (res_reg
[1] != 0)))
3095 printk("Sony CDROM %s error (scd_open, spin up)\n", translate_error(res_reg
[1]));
3099 do_sony_cd_cmd(SONY_READ_TOC_CMD
, NULL
, 0, res_reg
, &res_size
);
3101 /* The drive sometimes returns error 0. I don't know why, but ignore
3102 it. It seems to mean the drive has already done the operation. */
3103 if ((res_size
< 2) || ((res_reg
[0] != 0) && (res_reg
[1] != 0)))
3105 /* If the drive is already playing, it's ok. */
3106 if ((res_reg
[1] == SONY_AUDIO_PLAYING_ERR
) || (res_reg
[1] == 0))
3111 /* If the drive says it is not spun up (even though we just did it!)
3112 then retry the operation at least a few times. */
3113 if ( (res_reg
[1] == SONY_NOT_SPIN_ERR
)
3114 && (num_spin_ups
< MAX_CDU31A_RETRIES
))
3117 goto respinup_on_open
;
3120 printk("Sony CDROM error %s (scd_open, read toc)\n", translate_error(res_reg
[1]));
3121 do_sony_cd_cmd(SONY_SPIN_DOWN_CMD
, NULL
, 0, res_reg
, &res_size
);
3128 * Open the drive for operations. Spin the drive up and read the table of
3129 * contents if these have not already been done.
3132 scd_open(struct cdrom_device_info
*cdi
, int openmode
)
3134 unsigned char res_reg
[12];
3135 unsigned int res_size
;
3136 unsigned char params
[2];
3139 if (sony_usage
== 0)
3141 if (scd_spinup() != 0) {
3148 do_sony_cd_cmd(SONY_SPIN_DOWN_CMD
, NULL
, 0, res_reg
, &res_size
);
3153 /* For XA on the CDU31A only, we have to do special reads.
3154 The CDU33A handles XA automagically. */
3155 /* if ( (sony_toc.disk_type == SONY_XA_DISK_TYPE) */
3156 if ( (sony_toc
.disk_type
!= 0x00)
3157 && (!is_double_speed
))
3159 params
[0] = SONY_SD_DECODE_PARAM
;
3161 do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD
,
3166 if ((res_size
< 2) || ((res_reg
[0] & 0xf0) == 0x20))
3168 printk("CDU31A: Unable to set XA params: 0x%2.2x\n", res_reg
[1]);
3172 /* A non-XA disk. Set the parms back if necessary. */
3173 else if (sony_xa_mode
)
3175 params
[0] = SONY_SD_DECODE_PARAM
;
3177 do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD
,
3182 if ((res_size
< 2) || ((res_reg
[0] & 0xf0) == 0x20))
3184 printk("CDU31A: Unable to reset XA params: 0x%2.2x\n", res_reg
[1]);
3199 * Close the drive. Spin it down if no task is using it. The spin
3200 * down will fail if playing audio, so audio play is OK.
3203 scd_release(struct cdrom_device_info
*cdi
)
3205 if (sony_usage
== 1)
3207 unsigned char res_reg
[12];
3208 unsigned int res_size
;
3210 do_sony_cd_cmd(SONY_SPIN_DOWN_CMD
, NULL
, 0, res_reg
, &res_size
);
3218 static struct cdrom_device_ops scd_dops
= {
3219 scd_open
, /* open */
3220 scd_release
, /* release */
3221 scd_drive_status
, /* drive status */
3222 scd_media_changed
, /* media changed */
3223 scd_tray_move
, /* tray move */
3224 scd_lock_door
, /* lock door */
3225 scd_select_speed
, /* select speed */
3226 NULL
, /* select disc */
3227 scd_get_last_session
, /* get last session */
3228 scd_get_mcn
, /* get universal product code */
3229 scd_reset
, /* hard reset */
3230 scd_audio_ioctl
, /* audio ioctl */
3231 scd_dev_ioctl
, /* device-specific ioctl */
3232 CDC_OPEN_TRAY
| CDC_CLOSE_TRAY
| CDC_LOCK
| CDC_SELECT_SPEED
| CDC_MULTI_SESSION
|
3233 CDC_MULTI_SESSION
| CDC_MCN
| CDC_MEDIA_CHANGED
| CDC_PLAY_AUDIO
|
3234 CDC_RESET
| CDC_IOCTLS
| CDC_DRIVE_STATUS
, /* capability */
3235 1, /* number of minor devices */
3238 static struct cdrom_device_info scd_info
= {
3239 &scd_dops
, /* device operations */
3244 2, /* maximum speed */
3245 1, /* number of discs */
3246 0, /* options, not owned */
3247 0, /* mc_flags, not owned */
3248 0 /* use count, not owned */
3251 /* The different types of disc loading mechanisms supported */
3252 static const char *load_mech
[] __initdata
= { "caddy", "tray", "pop-up", "unknown" };
3255 get_drive_configuration(unsigned short base_io
,
3256 unsigned char res_reg
[],
3257 unsigned int *res_size
)
3262 /* Set the base address */
3263 cdu31a_port
= base_io
;
3265 /* Set up all the register locations */
3266 sony_cd_cmd_reg
= cdu31a_port
+ SONY_CMD_REG_OFFSET
;
3267 sony_cd_param_reg
= cdu31a_port
+ SONY_PARAM_REG_OFFSET
;
3268 sony_cd_write_reg
= cdu31a_port
+ SONY_WRITE_REG_OFFSET
;
3269 sony_cd_control_reg
= cdu31a_port
+ SONY_CONTROL_REG_OFFSET
;
3270 sony_cd_status_reg
= cdu31a_port
+ SONY_STATUS_REG_OFFSET
;
3271 sony_cd_result_reg
= cdu31a_port
+ SONY_RESULT_REG_OFFSET
;
3272 sony_cd_read_reg
= cdu31a_port
+ SONY_READ_REG_OFFSET
;
3273 sony_cd_fifost_reg
= cdu31a_port
+ SONY_FIFOST_REG_OFFSET
;
3276 * Check to see if anything exists at the status register location.
3277 * I don't know if this is a good way to check, but it seems to work
3280 if (read_status_register() != 0xff)
3283 * Reset the drive and wait for attention from it (to say it's reset).
3284 * If you don't wait, the next operation will probably fail.
3287 retry_count
= jiffies
+ SONY_RESET_TIMEOUT
;
3288 while (time_before(jiffies
, retry_count
) && (!is_attention()))
3294 /* If attention is never seen probably not a CDU31a present */
3295 if (!is_attention())
3303 * Get the drive configuration.
3305 do_sony_cd_cmd(SONY_REQ_DRIVE_CONFIG_CMD
,
3308 (unsigned char *) res_reg
,
3313 /* Return an error */
3319 * Set up base I/O and interrupts, called from main.c.
3322 cdu31a_setup(char *strings
,
3327 cdu31a_port
= ints
[1];
3331 cdu31a_irq
= ints
[2];
3333 if ((strings
!= NULL
) && (*strings
!= '\0'))
3335 if (strcmp(strings
, "PAS") == 0)
3341 printk("CDU31A: Unknown interface type: %s\n", strings
);
3347 static int cdu31a_block_size
;
3350 * Initialize the driver.
3355 struct s_sony_drive_config drive_config
;
3356 unsigned int res_size
;
3365 * According to Alex Freed (freed@europa.orion.adobe.com), this is
3366 * required for the Fusion CD-16 package. If the sound driver is
3367 * loaded, it should work fine, but just in case...
3369 * The following turn on the CD-ROM interface for a Fusion CD-16.
3379 /* Setting the base I/O address to 0xffff will disable it. */
3380 if (cdu31a_port
== 0xffff)
3383 else if (cdu31a_port
!= 0)
3385 tmp_irq
= cdu31a_irq
; /* Need IRQ 0 because we can't sleep here. */
3388 get_drive_configuration(cdu31a_port
,
3389 drive_config
.exec_status
,
3391 if ((res_size
> 2) && ((drive_config
.exec_status
[0] & 0xf0) == 0x00))
3396 cdu31a_irq
= tmp_irq
;
3402 while ( (cdu31a_addresses
[i
].base
!= 0)
3405 if (check_region(cdu31a_addresses
[i
].base
, 4)) {
3409 get_drive_configuration(cdu31a_addresses
[i
].base
,
3410 drive_config
.exec_status
,
3412 if ((res_size
> 2) && ((drive_config
.exec_status
[0] & 0xf0) == 0x00))
3415 cdu31a_irq
= cdu31a_addresses
[i
].int_num
;
3428 request_region(cdu31a_port
, 4,"cdu31a");
3430 if (register_blkdev(MAJOR_NR
,"cdu31a",&cdrom_fops
))
3432 printk("Unable to get major %d for CDU-31a\n", MAJOR_NR
);
3436 if (SONY_HWC_DOUBLE_SPEED(drive_config
))
3438 is_double_speed
= 1;
3441 tmp_irq
= cdu31a_irq
; /* Need IRQ 0 because we can't sleep here. */
3444 set_drive_params(sony_speed
);
3446 cdu31a_irq
= tmp_irq
;
3450 if (request_irq(cdu31a_irq
, cdu31a_interrupt
, SA_INTERRUPT
, "cdu31a", NULL
))
3452 printk("Unable to grab IRQ%d for the CDU31A driver\n", cdu31a_irq
);
3457 sprintf(msg
, "Sony I/F CDROM : %8.8s %16.16s %8.8s\n",
3458 drive_config
.vendor_id
,
3459 drive_config
.product_id
,
3460 drive_config
.product_rev_level
);
3461 sprintf(buf
, " Capabilities: %s",
3462 load_mech
[SONY_HWC_GET_LOAD_MECH(drive_config
)]);
3464 if (SONY_HWC_AUDIO_PLAYBACK(drive_config
))
3466 strcat(msg
, ", audio");
3468 deficiency
|= CDC_PLAY_AUDIO
;
3469 if (SONY_HWC_EJECT(drive_config
))
3471 strcat(msg
, ", eject");
3473 deficiency
|= CDC_OPEN_TRAY
;
3474 if (SONY_HWC_LED_SUPPORT(drive_config
))
3476 strcat(msg
, ", LED");
3478 if (SONY_HWC_ELECTRIC_VOLUME(drive_config
))
3480 strcat(msg
, ", elec. Vol");
3482 if (SONY_HWC_ELECTRIC_VOLUME_CTL(drive_config
))
3484 strcat(msg
, ", sep. Vol");
3486 if (is_double_speed
)
3488 strcat(msg
, ", double speed");
3490 deficiency
|= CDC_SELECT_SPEED
;
3493 sprintf(buf
, ", irq %d", cdu31a_irq
);
3498 is_a_cdu31a
= strcmp("CD-ROM CDU31A", drive_config
.product_id
) == 0;
3500 blk_dev
[MAJOR_NR
].request_fn
= DEVICE_REQUEST
;
3501 read_ahead
[MAJOR_NR
] = CDU31A_READAHEAD
;
3502 cdu31a_block_size
= 1024; /* 1kB default block size */
3503 /* use 'mount -o block=2048' */
3504 blksize_size
[MAJOR_NR
] = &cdu31a_block_size
;
3506 init_timer(&cdu31a_abort_timer
);
3507 cdu31a_abort_timer
.function
= handle_abort_timeout
;
3509 scd_info
.dev
= MKDEV(MAJOR_NR
,0);
3510 scd_info
.mask
= deficiency
;
3511 strncpy(scd_info
.name
, "cdu31a", sizeof(scd_info
.name
));
3513 if (register_cdrom(&scd_info
))
3531 printk("Unable to register CDU-31a with Uniform cdrom driver\n");
3532 if (unregister_blkdev(MAJOR_NR
, "cdu31a"))
3534 printk("Can't unregister block device for cdu31a\n");
3537 release_region(cdu31a_port
,4);
3547 return cdu31a_init();
3551 cleanup_module(void)
3553 if (unregister_cdrom(&scd_info
))
3555 printk("Can't unregister cdu31a from Uniform cdrom driver\n");
3558 if ((unregister_blkdev(MAJOR_NR
, "cdu31a") == -EINVAL
))
3560 printk("Can't unregister cdu31a\n");
3565 free_irq(cdu31a_irq
, NULL
);
3567 release_region(cdu31a_port
,4);
3568 printk(KERN_INFO
"cdu31a module released.\n");