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.
70 * The driver should support XA disks for both the CDU31A and CDU33A.
71 * It does this transparently, the using program doesn't need to set it.
75 * A multi-session disk looks just like a normal disk to the user.
76 * Just mount one normally, and all the data should be there.
77 * A special thanks to Koen for help with this!
81 * Using the CDROMREADAUDIO it is possible to read raw audio and data
82 * tracks. Both operations return 2352 bytes per sector. On the data
83 * tracks, the first 12 bytes is not returned by the drive and the value
84 * of that data is indeterminate.
87 * Copyright (C) 1993 Corey Minyard
89 * This program is free software; you can redistribute it and/or modify
90 * it under the terms of the GNU General Public License as published by
91 * the Free Software Foundation; either version 2 of the License, or
92 * (at your option) any later version.
94 * This program is distributed in the hope that it will be useful,
95 * but WITHOUT ANY WARRANTY; without even the implied warranty of
96 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
97 * GNU General Public License for more details.
99 * You should have received a copy of the GNU General Public License
100 * along with this program; if not, write to the Free Software
101 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
104 * CDs with form1 and form2 sectors cause problems
105 * with current read-ahead strategy.
108 * Heiko Eissfeldt <heiko@colossus.escape.de>
109 * For finding abug in the return of the track numbers.
110 * TOC processing redone for proper multisession support.
113 * It probably a little late to be adding a history, but I guess I
116 * 10/24/95 - Added support for disabling the eject button when the
117 * drive is open. Note that there is a small problem
118 * still here, if the eject button is pushed while the
119 * drive light is flashing, the drive will return a bad
120 * status and be reset. It recovers, though.
122 * 03/07/97 - Fixed a problem with timers.
125 * 18 Spetember 1997 -- Ported to Uniform CD-ROM driver by
126 * Heiko Eissfeldt <heiko@colossus.escape.de> with additional
127 * changes by Erik Andersen <andersee@debian.org>
129 * 24 January 1998 -- Removed the scd_disc_status() function, which was now
130 * just dead code left over from the port.
131 * Erik Andersen <andersee@debian.org>
133 * 16 July 1998 -- Drive donated to Erik Andersen by John Kodis
134 * <kodis@jagunet.com>. Work begun on fixing driver to
135 * work under 2.1.X. Added temporary extra printks
136 * which seem to slow it down enough to work.
138 * 9 November 1999 -- Make kernel-parameter implementation work with 2.3.x
139 * Removed init_module & cleanup_module in favor of
140 * module_init & module_exit.
141 * Torben Mathiasen <tmm@image.dk>
143 * 22 October 2004 -- Make the driver work in 2.6.X
144 * Added workaround to fix hard lockups on eject
145 * Fixed door locking problem after mounting empty drive
146 * Set double-speed drives to double speed by default
147 * Removed all readahead things - not needed anymore
148 * Ondrej Zary <rainbow@rainbow-software.org>
153 #include <linux/major.h>
154 #include <linux/module.h>
155 #include <linux/errno.h>
156 #include <linux/signal.h>
157 #include <linux/sched.h>
158 #include <linux/timer.h>
159 #include <linux/fs.h>
160 #include <linux/kernel.h>
161 #include <linux/hdreg.h>
162 #include <linux/genhd.h>
163 #include <linux/ioport.h>
164 #include <linux/devfs_fs_kernel.h>
165 #include <linux/string.h>
166 #include <linux/slab.h>
167 #include <linux/init.h>
168 #include <linux/interrupt.h>
169 #include <linux/cdrom.h>
171 #include <asm/system.h>
173 #include <asm/uaccess.h>
178 #define MAJOR_NR CDU31A_CDROM_MAJOR
179 #include <linux/blkdev.h>
181 #define CDU31A_MAX_CONSECUTIVE_ATTENTIONS 10
183 #define PFX "CDU31A: "
186 ** Edit the following data to change interrupts, DMA channels, etc.
187 ** 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
= {
198 static int handle_sony_cd_attention(void);
199 static int read_subcode(void);
200 static void sony_get_toc(void);
201 static int scd_spinup(void);
202 /*static int scd_open(struct inode *inode, struct file *filp);*/
203 static int scd_open(struct cdrom_device_info
*, int);
204 static void do_sony_cd_cmd(unsigned char cmd
,
205 unsigned char *params
,
206 unsigned int num_params
,
207 unsigned char *result_buffer
,
208 unsigned int *result_size
);
209 static void size_to_buf(unsigned int size
, unsigned char *buf
);
211 /* Parameters for the read-ahead. */
212 static unsigned int sony_next_block
; /* Next 512 byte block offset */
213 static unsigned int sony_blocks_left
= 0; /* Number of 512 byte blocks left
214 in the current read command. */
217 /* The base I/O address of the Sony Interface. This is a variable (not a
218 #define) so it can be easily changed via some future ioctl() */
219 static unsigned int cdu31a_port
= 0;
220 module_param(cdu31a_port
, uint
, 0);
223 * The following are I/O addresses of the various registers for the drive. The
224 * comment for the base address also applies here.
226 static volatile unsigned short sony_cd_cmd_reg
;
227 static volatile unsigned short sony_cd_param_reg
;
228 static volatile unsigned short sony_cd_write_reg
;
229 static volatile unsigned short sony_cd_control_reg
;
230 static volatile unsigned short sony_cd_status_reg
;
231 static volatile unsigned short sony_cd_result_reg
;
232 static volatile unsigned short sony_cd_read_reg
;
233 static volatile unsigned short sony_cd_fifost_reg
;
235 static struct request_queue
*cdu31a_queue
;
236 static DEFINE_SPINLOCK(cdu31a_lock
); /* queue lock */
238 static int sony_spun_up
= 0; /* Has the drive been spun up? */
240 static int sony_speed
= 0; /* Last wanted speed */
242 static int sony_xa_mode
= 0; /* Is an XA disk in the drive
243 and the drive a CDU31A? */
245 static int sony_raw_data_mode
= 1; /* 1 if data tracks, 0 if audio.
246 For raw data reads. */
248 static unsigned int sony_usage
= 0; /* How many processes have the
251 static int sony_pas_init
= 0; /* Initialize the Pro-Audio
254 static struct s_sony_session_toc single_toc
; /* Holds the
258 static struct s_all_sessions_toc sony_toc
; /* entries gathered from all
261 static int sony_toc_read
= 0; /* Has the TOC been read for
264 static struct s_sony_subcode last_sony_subcode
; /* Points to the last
265 subcode address read */
267 static DECLARE_MUTEX(sony_sem
); /* Semaphore for drive hardware access */
269 static int is_double_speed
= 0; /* does the drive support double speed ? */
271 static int is_auto_eject
= 1; /* Door has been locked? 1=No/0=Yes */
274 * The audio status uses the values from read subchannel data as specified
275 * in include/linux/cdrom.h.
277 static volatile int sony_audio_status
= CDROM_AUDIO_NO_STATUS
;
280 * The following are a hack for pausing and resuming audio play. The drive
281 * does not work as I would expect it, if you stop it then start it again,
282 * the drive seeks back to the beginning and starts over. This holds the
283 * position during a pause so a resume can restart it. It uses the
284 * audio status variable above to tell if it is paused.
286 static unsigned volatile char cur_pos_msf
[3] = { 0, 0, 0 };
287 static unsigned volatile char final_pos_msf
[3] = { 0, 0, 0 };
289 /* What IRQ is the drive using? 0 if none. */
290 static int cdu31a_irq
= 0;
291 module_param(cdu31a_irq
, int, 0);
293 /* The interrupt handler will wake this queue up when it gets an
295 DECLARE_WAIT_QUEUE_HEAD(cdu31a_irq_wait
);
296 static int irq_flag
= 0;
298 static int curr_control_reg
= 0; /* Current value of the control register */
300 /* A disk changed variable. When a disk change is detected, it will
301 all be set to TRUE. As the upper layers ask for disk_changed status
302 it will be cleared. */
303 static char disk_changed
;
305 /* This was readahead_buffer once... Now it's used only for audio reads */
306 static char audio_buffer
[CD_FRAMESIZE_RAW
];
308 /* Used to time a short period to abort an operation after the
309 drive has been idle for a while. This keeps the light on
310 the drive from flashing for very long. */
311 static struct timer_list cdu31a_abort_timer
;
313 /* Marks if the timeout has started an abort read. This is used
314 on entry to the drive to tell the code to read out the status
315 from the abort read. */
316 static int abort_read_started
= 0;
319 * Uniform cdrom interface function
320 * report back, if disc has changed from time of last request.
322 static int scd_media_changed(struct cdrom_device_info
*cdi
, int disc_nr
)
326 retval
= disk_changed
;
333 * Uniform cdrom interface function
334 * report back, if drive is ready
336 static int scd_drive_status(struct cdrom_device_info
*cdi
, int slot_nr
)
338 if (CDSL_CURRENT
!= slot_nr
)
339 /* we have no changer support */
343 if (down_interruptible(&sony_sem
))
345 if (scd_spinup() == 0)
348 return sony_spun_up
? CDS_DISC_OK
: CDS_DRIVE_NOT_READY
;
351 static inline void enable_interrupts(void)
353 curr_control_reg
|= (SONY_ATTN_INT_EN_BIT
354 | SONY_RES_RDY_INT_EN_BIT
355 | SONY_DATA_RDY_INT_EN_BIT
);
356 outb(curr_control_reg
, sony_cd_control_reg
);
359 static inline void disable_interrupts(void)
361 curr_control_reg
&= ~(SONY_ATTN_INT_EN_BIT
362 | SONY_RES_RDY_INT_EN_BIT
363 | SONY_DATA_RDY_INT_EN_BIT
);
364 outb(curr_control_reg
, sony_cd_control_reg
);
368 * Wait a little while (used for polling the drive). If in initialization,
369 * setting a timeout doesn't work, so just loop for a while.
371 static inline void sony_sleep(void)
373 if (cdu31a_irq
<= 0) {
375 } else { /* Interrupt driven */
380 prepare_to_wait(&cdu31a_irq_wait
, &w
,
389 if (!signal_pending(current
)) {
393 disable_interrupts();
396 finish_wait(&cdu31a_irq_wait
, &w
);
403 * The following are convenience routine to read various status and set
404 * various conditions in the drive.
406 static inline int is_attention(void)
408 return (inb(sony_cd_status_reg
) & SONY_ATTN_BIT
) != 0;
411 static inline int is_busy(void)
413 return (inb(sony_cd_status_reg
) & SONY_BUSY_BIT
) != 0;
416 static inline int is_data_ready(void)
418 return (inb(sony_cd_status_reg
) & SONY_DATA_RDY_BIT
) != 0;
421 static inline int is_data_requested(void)
423 return (inb(sony_cd_status_reg
) & SONY_DATA_REQUEST_BIT
) != 0;
426 static inline int is_result_ready(void)
428 return (inb(sony_cd_status_reg
) & SONY_RES_RDY_BIT
) != 0;
431 static inline int is_param_write_rdy(void)
433 return (inb(sony_cd_fifost_reg
) & SONY_PARAM_WRITE_RDY_BIT
) != 0;
436 static inline int is_result_reg_not_empty(void)
438 return (inb(sony_cd_fifost_reg
) & SONY_RES_REG_NOT_EMP_BIT
) != 0;
441 static inline void reset_drive(void)
443 curr_control_reg
= 0;
445 outb(SONY_DRIVE_RESET_BIT
, sony_cd_control_reg
);
449 * Uniform cdrom interface function
450 * reset drive and return when it is ready
452 static int scd_reset(struct cdrom_device_info
*cdi
)
454 unsigned long retry_count
;
456 if (down_interruptible(&sony_sem
))
460 retry_count
= jiffies
+ SONY_RESET_TIMEOUT
;
461 while (time_before(jiffies
, retry_count
) && (!is_attention())) {
469 static inline void clear_attention(void)
471 outb(curr_control_reg
| SONY_ATTN_CLR_BIT
, sony_cd_control_reg
);
474 static inline void clear_result_ready(void)
476 outb(curr_control_reg
| SONY_RES_RDY_CLR_BIT
, sony_cd_control_reg
);
479 static inline void clear_data_ready(void)
481 outb(curr_control_reg
| SONY_DATA_RDY_CLR_BIT
,
482 sony_cd_control_reg
);
485 static inline void clear_param_reg(void)
487 outb(curr_control_reg
| SONY_PARAM_CLR_BIT
, sony_cd_control_reg
);
490 static inline unsigned char read_status_register(void)
492 return inb(sony_cd_status_reg
);
495 static inline unsigned char read_result_register(void)
497 return inb(sony_cd_result_reg
);
500 static inline unsigned char read_data_register(void)
502 return inb(sony_cd_read_reg
);
505 static inline void write_param(unsigned char param
)
507 outb(param
, sony_cd_param_reg
);
510 static inline void write_cmd(unsigned char cmd
)
512 outb(curr_control_reg
| SONY_RES_RDY_INT_EN_BIT
,
513 sony_cd_control_reg
);
514 outb(cmd
, sony_cd_cmd_reg
);
517 static irqreturn_t
cdu31a_interrupt(int irq
, void *dev_id
, struct pt_regs
*regs
)
521 if (abort_read_started
) {
522 /* We might be waiting for an abort to finish. Don't
523 disable interrupts yet, though, because we handle
525 /* Clear out the result registers. */
526 while (is_result_reg_not_empty()) {
527 val
= read_result_register();
530 clear_result_ready();
532 /* Clear out the data */
533 while (is_data_requested()) {
534 val
= read_data_register();
536 abort_read_started
= 0;
538 /* If something was waiting, wake it up now. */
539 if (waitqueue_active(&cdu31a_irq_wait
)) {
540 disable_interrupts();
542 wake_up_interruptible(&cdu31a_irq_wait
);
544 } else if (waitqueue_active(&cdu31a_irq_wait
)) {
545 disable_interrupts();
547 wake_up_interruptible(&cdu31a_irq_wait
);
549 disable_interrupts();
550 printk(KERN_NOTICE PFX
551 "Got an interrupt but nothing was waiting\n");
557 * give more verbose error messages
559 static unsigned char *translate_error(unsigned char err_code
)
561 static unsigned char errbuf
[80];
564 case 0x10: return "illegal command ";
565 case 0x11: return "illegal parameter ";
567 case 0x20: return "not loaded ";
568 case 0x21: return "no disc ";
569 case 0x22: return "not spinning ";
570 case 0x23: return "spinning ";
571 case 0x25: return "spindle servo ";
572 case 0x26: return "focus servo ";
573 case 0x29: return "eject mechanism ";
574 case 0x2a: return "audio playing ";
575 case 0x2c: return "emergency eject ";
577 case 0x30: return "focus ";
578 case 0x31: return "frame sync ";
579 case 0x32: return "subcode address ";
580 case 0x33: return "block sync ";
581 case 0x34: return "header address ";
583 case 0x40: return "illegal track read ";
584 case 0x41: return "mode 0 read ";
585 case 0x42: return "illegal mode read ";
586 case 0x43: return "illegal block size read ";
587 case 0x44: return "mode read ";
588 case 0x45: return "form read ";
589 case 0x46: return "leadout read ";
590 case 0x47: return "buffer overrun ";
592 case 0x53: return "unrecoverable CIRC ";
593 case 0x57: return "unrecoverable LECC ";
595 case 0x60: return "no TOC ";
596 case 0x61: return "invalid subcode data ";
597 case 0x63: return "focus on TOC read ";
598 case 0x64: return "frame sync on TOC read ";
599 case 0x65: return "TOC data ";
601 case 0x70: return "hardware failure ";
602 case 0x91: return "leadin ";
603 case 0x92: return "leadout ";
604 case 0x93: return "data track ";
606 sprintf(errbuf
, "unknown 0x%02x ", err_code
);
611 * Set the drive parameters so the drive will auto-spin-up when a
614 static void set_drive_params(int want_doublespeed
)
616 unsigned char res_reg
[12];
617 unsigned int res_size
;
618 unsigned char params
[3];
621 params
[0] = SONY_SD_AUTO_SPIN_DOWN_TIME
;
622 params
[1] = 0x00; /* Never spin down the drive. */
623 do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD
,
624 params
, 2, res_reg
, &res_size
);
625 if ((res_size
< 2) || ((res_reg
[0] & 0xf0) == 0x20)) {
626 printk(KERN_NOTICE PFX
627 "Unable to set spin-down time: 0x%2.2x\n", res_reg
[1]);
630 params
[0] = SONY_SD_MECH_CONTROL
;
631 params
[1] = SONY_AUTO_SPIN_UP_BIT
; /* Set auto spin up */
634 params
[1] |= SONY_AUTO_EJECT_BIT
;
636 if (is_double_speed
&& want_doublespeed
) {
637 params
[1] |= SONY_DOUBLE_SPEED_BIT
; /* Set the drive to double speed if
640 do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD
,
641 params
, 2, res_reg
, &res_size
);
642 if ((res_size
< 2) || ((res_reg
[0] & 0xf0) == 0x20)) {
643 printk(KERN_NOTICE PFX
"Unable to set mechanical "
644 "parameters: 0x%2.2x\n", res_reg
[1]);
649 * Uniform cdrom interface function
650 * select reading speed for data access
652 static int scd_select_speed(struct cdrom_device_info
*cdi
, int speed
)
657 sony_speed
= speed
- 1;
659 if (down_interruptible(&sony_sem
))
661 set_drive_params(sony_speed
);
667 * Uniform cdrom interface function
668 * lock or unlock eject button
670 static int scd_lock_door(struct cdrom_device_info
*cdi
, int lock
)
677 if (down_interruptible(&sony_sem
))
679 set_drive_params(sony_speed
);
685 * This code will reset the drive and attempt to restore sane parameters.
687 static void restart_on_error(void)
689 unsigned char res_reg
[12];
690 unsigned int res_size
;
691 unsigned long retry_count
;
694 printk(KERN_NOTICE PFX
"Resetting drive on error\n");
696 retry_count
= jiffies
+ SONY_RESET_TIMEOUT
;
697 while (time_before(jiffies
, retry_count
) && (!is_attention())) {
700 set_drive_params(sony_speed
);
701 do_sony_cd_cmd(SONY_SPIN_UP_CMD
, NULL
, 0, res_reg
, &res_size
);
702 if ((res_size
< 2) || ((res_reg
[0] & 0xf0) == 0x20)) {
703 printk(KERN_NOTICE PFX
"Unable to spin up drive: 0x%2.2x\n",
713 * This routine writes data to the parameter register. Since this should
714 * happen fairly fast, it is polled with no OS waits between.
716 static int write_params(unsigned char *params
, int num_params
)
718 unsigned int retry_count
;
721 retry_count
= SONY_READY_RETRIES
;
722 while ((retry_count
> 0) && (!is_param_write_rdy())) {
725 if (!is_param_write_rdy()) {
729 while (num_params
> 0) {
730 write_param(*params
);
740 * The following reads data from the command result register. It is a
741 * fairly complex routine, all status info flows back through this
742 * interface. The algorithm is stolen directly from the flowcharts in
746 get_result(unsigned char *result_buffer
, unsigned int *result_size
)
750 unsigned long retry_count
;
753 while (handle_sony_cd_attention());
754 /* Wait for the result data to be ready */
755 retry_count
= jiffies
+ SONY_JIFFIES_TIMEOUT
;
756 while (time_before(jiffies
, retry_count
)
757 && (is_busy() || (!(is_result_ready())))) {
760 while (handle_sony_cd_attention());
762 if (is_busy() || (!(is_result_ready()))) {
763 pr_debug(PFX
"timeout out %d\n", __LINE__
);
764 result_buffer
[0] = 0x20;
765 result_buffer
[1] = SONY_TIMEOUT_OP_ERR
;
771 * Get the first two bytes. This determines what else needs
774 clear_result_ready();
775 a
= read_result_register();
779 /* Check for block error status result. */
780 if ((a
& 0xf0) == 0x50) {
785 b
= read_result_register();
791 * 0x20 means an error occurred. Byte 2 will have the error code.
792 * Otherwise, the command succeeded, byte 2 will have the count of
793 * how many more status bytes are coming.
795 * The result register can be read 10 bytes at a time, a wait for
796 * result ready to be asserted must be done between every 10 bytes.
798 if ((a
& 0xf0) != 0x20) {
800 for (i
= 0; i
< 8; i
++) {
801 *result_buffer
= read_result_register();
808 retry_count
= SONY_READY_RETRIES
;
809 while ((retry_count
> 0)
810 && (!is_result_ready())) {
813 if (!is_result_ready()) {
814 pr_debug(PFX
"timeout out %d\n",
816 result_buffer
[0] = 0x20;
823 clear_result_ready();
825 for (i
= 0; i
< 10; i
++) {
827 read_result_register();
835 retry_count
= SONY_READY_RETRIES
;
836 while ((retry_count
> 0)
837 && (!is_result_ready())) {
840 if (!is_result_ready()) {
841 pr_debug(PFX
"timeout out %d\n",
843 result_buffer
[0] = 0x20;
853 *result_buffer
= read_result_register();
862 * Do a command that does not involve data transfer. This routine must
863 * be re-entrant from the same task to support being called from the
864 * data operation code when an error occurs.
867 do_sony_cd_cmd(unsigned char cmd
,
868 unsigned char *params
,
869 unsigned int num_params
,
870 unsigned char *result_buffer
, unsigned int *result_size
)
872 unsigned long retry_count
;
877 while (handle_sony_cd_attention());
879 retry_count
= jiffies
+ SONY_JIFFIES_TIMEOUT
;
880 while (time_before(jiffies
, retry_count
) && (is_busy())) {
883 while (handle_sony_cd_attention());
886 pr_debug(PFX
"timeout out %d\n", __LINE__
);
887 result_buffer
[0] = 0x20;
888 result_buffer
[1] = SONY_TIMEOUT_OP_ERR
;
891 clear_result_ready();
894 write_params(params
, num_params
);
897 get_result(result_buffer
, result_size
);
900 if (((result_buffer
[0] & 0xf0) == 0x20)
901 && (num_retries
< MAX_CDU31A_RETRIES
)) {
904 goto retry_cd_operation
;
910 * Handle an attention from the drive. This will return 1 if it found one
911 * or 0 if not (if one is found, the caller might want to call again).
913 * This routine counts the number of consecutive times it is called
914 * (since this is always called from a while loop until it returns
915 * a 0), and returns a 0 if it happens too many times. This will help
918 static int handle_sony_cd_attention(void)
920 unsigned char atten_code
;
921 static int num_consecutive_attentions
= 0;
926 pr_debug(PFX
"Entering %s\n", __FUNCTION__
);
928 if (is_attention()) {
929 if (num_consecutive_attentions
>
930 CDU31A_MAX_CONSECUTIVE_ATTENTIONS
) {
931 printk(KERN_NOTICE PFX
"Too many consecutive "
932 "attentions: %d\n", num_consecutive_attentions
);
933 num_consecutive_attentions
= 0;
934 pr_debug(PFX
"Leaving %s at %d\n", __FUNCTION__
,
940 atten_code
= read_result_register();
942 switch (atten_code
) {
943 /* Someone changed the CD. Mark it as changed */
944 case SONY_MECH_LOADED_ATTN
:
947 sony_audio_status
= CDROM_AUDIO_NO_STATUS
;
948 sony_blocks_left
= 0;
951 case SONY_SPIN_DOWN_COMPLETE_ATTN
:
952 /* Mark the disk as spun down. */
956 case SONY_AUDIO_PLAY_DONE_ATTN
:
957 sony_audio_status
= CDROM_AUDIO_COMPLETED
;
961 case SONY_EJECT_PUSHED_ATTN
:
963 sony_audio_status
= CDROM_AUDIO_INVALID
;
967 case SONY_LEAD_IN_ERR_ATTN
:
968 case SONY_LEAD_OUT_ERR_ATTN
:
969 case SONY_DATA_TRACK_ERR_ATTN
:
970 case SONY_AUDIO_PLAYBACK_ERR_ATTN
:
971 sony_audio_status
= CDROM_AUDIO_ERROR
;
975 num_consecutive_attentions
++;
976 pr_debug(PFX
"Leaving %s at %d\n", __FUNCTION__
, __LINE__
);
978 } else if (abort_read_started
) {
979 while (is_result_reg_not_empty()) {
980 val
= read_result_register();
983 clear_result_ready();
984 /* Clear out the data */
985 while (is_data_requested()) {
986 val
= read_data_register();
988 abort_read_started
= 0;
989 pr_debug(PFX
"Leaving %s at %d\n", __FUNCTION__
, __LINE__
);
993 num_consecutive_attentions
= 0;
995 pr_debug(PFX
"Leaving %s at %d\n", __FUNCTION__
, __LINE__
);
1001 /* Convert from an integer 0-99 to BCD */
1002 static inline unsigned int int_to_bcd(unsigned int val
)
1007 retval
= (val
/ 10) << 4;
1008 retval
= retval
| val
% 10;
1013 /* Convert from BCD to an integer from 0-99 */
1014 static unsigned int bcd_to_int(unsigned int bcd
)
1016 return (((bcd
>> 4) & 0x0f) * 10) + (bcd
& 0x0f);
1021 * Convert a logical sector value (like the OS would want to use for
1022 * a block device) to an MSF format.
1024 static void log_to_msf(unsigned int log
, unsigned char *msf
)
1026 log
= log
+ LOG_START_OFFSET
;
1027 msf
[0] = int_to_bcd(log
/ 4500);
1029 msf
[1] = int_to_bcd(log
/ 75);
1030 msf
[2] = int_to_bcd(log
% 75);
1035 * Convert an MSF format to a logical sector.
1037 static unsigned int msf_to_log(unsigned char *msf
)
1044 log
+= msf
[0] * 4500;
1045 log
= log
- LOG_START_OFFSET
;
1052 * Take in integer size value and put it into a buffer like
1053 * the drive would want to see a number-of-sector value.
1055 static void size_to_buf(unsigned int size
, unsigned char *buf
)
1057 buf
[0] = size
/ 65536;
1058 size
= size
% 65536;
1059 buf
[1] = size
/ 256;
1060 buf
[2] = size
% 256;
1063 /* Starts a read operation. Returns 0 on success and 1 on failure.
1064 The read operation used here allows multiple sequential sectors
1065 to be read and status returned for each sector. The driver will
1066 read the output one at a time as the requests come and abort the
1067 operation if the requested sector is not the next one from the
1070 start_request(unsigned int sector
, unsigned int nsect
)
1072 unsigned char params
[6];
1073 unsigned long retry_count
;
1076 pr_debug(PFX
"Entering %s\n", __FUNCTION__
);
1077 log_to_msf(sector
, params
);
1078 size_to_buf(nsect
, ¶ms
[3]);
1081 * Clear any outstanding attentions and wait for the drive to
1082 * complete any pending operations.
1084 while (handle_sony_cd_attention());
1086 retry_count
= jiffies
+ SONY_JIFFIES_TIMEOUT
;
1087 while (time_before(jiffies
, retry_count
) && (is_busy())) {
1090 while (handle_sony_cd_attention());
1094 printk(KERN_NOTICE PFX
"Timeout while waiting "
1095 "to issue command\n");
1096 pr_debug(PFX
"Leaving %s at %d\n", __FUNCTION__
, __LINE__
);
1099 /* Issue the command */
1100 clear_result_ready();
1103 write_params(params
, 6);
1104 write_cmd(SONY_READ_BLKERR_STAT_CMD
);
1106 sony_blocks_left
= nsect
* 4;
1107 sony_next_block
= sector
* 4;
1108 pr_debug(PFX
"Leaving %s at %d\n", __FUNCTION__
, __LINE__
);
1111 pr_debug(PFX
"Leaving %s at %d\n", __FUNCTION__
, __LINE__
);
1114 /* Abort a pending read operation. Clear all the drive status variables. */
1115 static void abort_read(void)
1117 unsigned char result_reg
[2];
1122 do_sony_cd_cmd(SONY_ABORT_CMD
, NULL
, 0, result_reg
, &result_size
);
1123 if ((result_reg
[0] & 0xf0) == 0x20) {
1124 printk(KERN_ERR PFX
"Aborting read, %s error\n",
1125 translate_error(result_reg
[1]));
1128 while (is_result_reg_not_empty()) {
1129 val
= read_result_register();
1132 clear_result_ready();
1133 /* Clear out the data */
1134 while (is_data_requested()) {
1135 val
= read_data_register();
1138 sony_blocks_left
= 0;
1141 /* Called when the timer times out. This will abort the
1142 pending read operation. */
1143 static void handle_abort_timeout(unsigned long data
)
1145 pr_debug(PFX
"Entering %s\n", __FUNCTION__
);
1146 /* If it is in use, ignore it. */
1147 if (down_trylock(&sony_sem
) == 0) {
1148 /* We can't use abort_read(), because it will sleep
1149 or schedule in the timer interrupt. Just start
1150 the operation, finish it on the next access to
1152 clear_result_ready();
1154 write_cmd(SONY_ABORT_CMD
);
1156 sony_blocks_left
= 0;
1157 abort_read_started
= 1;
1160 pr_debug(PFX
"Leaving %s\n", __FUNCTION__
);
1163 /* Actually get one sector of data from the drive. */
1165 input_data_sector(char *buffer
)
1167 pr_debug(PFX
"Entering %s\n", __FUNCTION__
);
1169 /* If an XA disk on a CDU31A, skip the first 12 bytes of data from
1170 the disk. The real data is after that. We can use audio_buffer. */
1172 insb(sony_cd_read_reg
, audio_buffer
, CD_XA_HEAD
);
1176 insb(sony_cd_read_reg
, buffer
, 2048);
1178 /* If an XA disk, we have to clear out the rest of the unused
1179 error correction data. We can use audio_buffer for that. */
1181 insb(sony_cd_read_reg
, audio_buffer
, CD_XA_TAIL
);
1183 pr_debug(PFX
"Leaving %s\n", __FUNCTION__
);
1186 /* read data from the drive. Note the nsect must be <= 4. */
1188 read_data_block(char *buffer
,
1190 unsigned int nblocks
,
1191 unsigned char res_reg
[], int *res_size
)
1193 unsigned long retry_count
;
1195 pr_debug(PFX
"Entering %s\n", __FUNCTION__
);
1201 /* Wait for the drive to tell us we have something */
1202 retry_count
= jiffies
+ SONY_JIFFIES_TIMEOUT
;
1203 while (time_before(jiffies
, retry_count
) && !(is_data_ready())) {
1204 while (handle_sony_cd_attention());
1208 if (!(is_data_ready())) {
1209 if (is_result_ready()) {
1210 get_result(res_reg
, res_size
);
1211 if ((res_reg
[0] & 0xf0) != 0x20) {
1212 printk(KERN_NOTICE PFX
"Got result that should"
1213 " have been error: %d\n", res_reg
[0]);
1215 res_reg
[1] = SONY_BAD_DATA_ERR
;
1220 pr_debug(PFX
"timeout out %d\n", __LINE__
);
1222 res_reg
[1] = SONY_TIMEOUT_OP_ERR
;
1227 input_data_sector(buffer
);
1228 sony_blocks_left
-= nblocks
;
1229 sony_next_block
+= nblocks
;
1231 /* Wait for the status from the drive. */
1232 retry_count
= jiffies
+ SONY_JIFFIES_TIMEOUT
;
1233 while (time_before(jiffies
, retry_count
)
1234 && !(is_result_ready())) {
1235 while (handle_sony_cd_attention());
1240 if (!is_result_ready()) {
1241 pr_debug(PFX
"timeout out %d\n", __LINE__
);
1243 res_reg
[1] = SONY_TIMEOUT_OP_ERR
;
1247 get_result(res_reg
, res_size
);
1249 /* If we got a buffer status, handle that. */
1250 if ((res_reg
[0] & 0xf0) == 0x50) {
1253 SONY_NO_CIRC_ERR_BLK_STAT
)
1255 SONY_NO_LECC_ERR_BLK_STAT
)
1257 SONY_RECOV_LECC_ERR_BLK_STAT
)) {
1260 printk(KERN_ERR PFX
"Data block "
1261 "error: 0x%x\n", res_reg
[0]);
1263 res_reg
[1] = SONY_BAD_DATA_ERR
;
1267 /* Final transfer is done for read command, get final result. */
1268 if (sony_blocks_left
== 0) {
1269 get_result(res_reg
, res_size
);
1271 } else if ((res_reg
[0] & 0xf0) != 0x20) {
1272 /* The drive gave me bad status, I don't know what to do.
1273 Reset the driver and return an error. */
1274 printk(KERN_ERR PFX
"Invalid block "
1275 "status: 0x%x\n", res_reg
[0]);
1278 res_reg
[1] = SONY_BAD_DATA_ERR
;
1283 pr_debug(PFX
"Leaving %s at %d\n", __FUNCTION__
, __LINE__
);
1288 * The OS calls this to perform a read or write operation to the drive.
1289 * Write obviously fail. Reads to a read ahead of sony_buffer_size
1290 * bytes to help speed operations. This especially helps since the OS
1291 * uses 1024 byte blocks and the drive uses 2048 byte blocks. Since most
1292 * data access on a CD is done sequentially, this saves a lot of operations.
1294 static void do_cdu31a_request(request_queue_t
* q
)
1296 struct request
*req
;
1297 int block
, nblock
, num_retries
;
1298 unsigned char res_reg
[12];
1299 unsigned int res_size
;
1301 pr_debug(PFX
"Entering %s\n", __FUNCTION__
);
1303 spin_unlock_irq(q
->queue_lock
);
1304 if (down_interruptible(&sony_sem
)) {
1305 spin_lock_irq(q
->queue_lock
);
1309 /* Get drive status before doing anything. */
1310 while (handle_sony_cd_attention());
1312 /* Make sure we have a valid TOC. */
1316 /* Make sure the timer is cancelled. */
1317 del_timer(&cdu31a_abort_timer
);
1321 * The beginning here is stolen from the hard disk driver. I hope
1324 req
= elv_next_request(q
);
1326 goto end_do_cdu31a_request
;
1331 block
= req
->sector
;
1332 nblock
= req
->nr_sectors
;
1333 pr_debug(PFX
"request at block %d, length %d blocks\n",
1335 if (!sony_toc_read
) {
1336 printk(KERN_NOTICE PFX
"TOC not read\n");
1337 end_request(req
, 0);
1342 if (!(req
->flags
& REQ_CMD
))
1344 if (rq_data_dir(req
) == WRITE
) {
1345 end_request(req
, 0);
1350 * If the block address is invalid or the request goes beyond the end of
1351 * the media, return an error.
1353 if (((block
+ nblock
) / 4) >= sony_toc
.lead_out_start_lba
) {
1354 printk(KERN_NOTICE PFX
"Request past end of media\n");
1355 end_request(req
, 0);
1364 while (handle_sony_cd_attention());
1366 if (!sony_toc_read
) {
1367 printk(KERN_NOTICE PFX
"TOC not read\n");
1368 end_request(req
, 0);
1372 /* If no data is left to be read from the drive, start the
1374 if (sony_blocks_left
== 0) {
1375 if (start_request(block
/ 4, nblock
/ 4)) {
1376 end_request(req
, 0);
1380 /* If the requested block is not the next one waiting in
1381 the driver, abort the current operation and start a
1383 else if (block
!= sony_next_block
) {
1384 pr_debug(PFX
"Read for block %d, expected %d\n",
1385 block
, sony_next_block
);
1387 if (!sony_toc_read
) {
1388 printk(KERN_NOTICE PFX
"TOC not read\n");
1389 end_request(req
, 0);
1392 if (start_request(block
/ 4, nblock
/ 4)) {
1393 printk(KERN_NOTICE PFX
"start request failed\n");
1394 end_request(req
, 0);
1399 read_data_block(req
->buffer
, block
, nblock
, res_reg
, &res_size
);
1401 if (res_reg
[0] != 0x20) {
1402 if (!end_that_request_first(req
, 1, nblock
)) {
1403 spin_lock_irq(q
->queue_lock
);
1404 blkdev_dequeue_request(req
);
1405 end_that_request_last(req
);
1406 spin_unlock_irq(q
->queue_lock
);
1411 if (num_retries
> MAX_CDU31A_RETRIES
) {
1412 end_request(req
, 0);
1417 if (res_reg
[1] == SONY_NOT_SPIN_ERR
) {
1418 do_sony_cd_cmd(SONY_SPIN_UP_CMD
, NULL
, 0, res_reg
,
1421 printk(KERN_NOTICE PFX
"%s error for block %d, nblock %d\n",
1422 translate_error(res_reg
[1]), block
, nblock
);
1424 goto try_read_again
;
1426 end_do_cdu31a_request
:
1428 /* After finished, cancel any pending operations. */
1431 /* Start a timer to time out after a while to disable
1433 cdu31a_abort_timer
.expires
= jiffies
+ 2 * HZ
; /* Wait 2 seconds */
1434 add_timer(&cdu31a_abort_timer
);
1438 spin_lock_irq(q
->queue_lock
);
1439 pr_debug(PFX
"Leaving %s at %d\n", __FUNCTION__
, __LINE__
);
1444 * Read the table of contents from the drive and set up TOC if
1447 static void sony_get_toc(void)
1449 unsigned char res_reg
[2];
1450 unsigned int res_size
;
1451 unsigned char parms
[1];
1454 int totaltracks
= 0;
1458 pr_debug(PFX
"Entering %s\n", __FUNCTION__
);
1461 if (!sony_toc_read
) {
1463 /* Ignore the result, since it might error if spinning already. */
1464 do_sony_cd_cmd(SONY_SPIN_UP_CMD
, NULL
, 0, res_reg
,
1467 do_sony_cd_cmd(SONY_READ_TOC_CMD
, NULL
, 0, res_reg
,
1470 /* The drive sometimes returns error 0. I don't know why, but ignore
1471 it. It seems to mean the drive has already done the operation. */
1473 || ((res_reg
[0] != 0) && (res_reg
[1] != 0))) {
1474 /* If the drive is already playing, it's ok. */
1475 if ((res_reg
[1] == SONY_AUDIO_PLAYING_ERR
)
1476 || (res_reg
[1] == 0)) {
1477 goto gettoc_drive_spinning
;
1480 /* If the drive says it is not spun up (even though we just did it!)
1481 then retry the operation at least a few times. */
1482 if ((res_reg
[1] == SONY_NOT_SPIN_ERR
)
1483 && (num_spin_ups
< MAX_CDU31A_RETRIES
)) {
1485 goto respinup_on_gettoc
;
1488 printk("cdu31a: Error reading TOC: %x %s\n",
1489 res_reg
[0], translate_error(res_reg
[1]));
1493 gettoc_drive_spinning
:
1495 /* The idea here is we keep asking for sessions until the command
1496 fails. Then we know what the last valid session on the disk is.
1497 No need to check session 0, since session 0 is the same as session
1498 1; the command returns different information if you give it 0.
1501 memset(&sony_toc
, 0x0e, sizeof(sony_toc
));
1502 memset(&single_toc
, 0x0f, sizeof(single_toc
));
1506 /* This seems to slow things down enough to make it work. This
1507 * appears to be a problem in do_sony_cd_cmd. This printk seems
1508 * to address the symptoms... -Erik */
1509 pr_debug(PFX
"Trying session %d\n", session
);
1511 do_sony_cd_cmd(SONY_READ_TOC_SPEC_CMD
,
1512 parms
, 1, res_reg
, &res_size
);
1514 pr_debug(PFX
"%2.2x %2.2x\n", res_reg
[0], res_reg
[1]);
1517 || ((res_reg
[0] & 0xf0) == 0x20)) {
1518 /* An error reading the TOC, this must be past the last session. */
1521 ("Yikes! Couldn't read any sessions!");
1524 pr_debug(PFX
"Reading session %d\n", session
);
1527 do_sony_cd_cmd(SONY_REQ_TOC_DATA_SPEC_CMD
,
1530 (unsigned char *) &single_toc
,
1533 || ((single_toc
.exec_status
[0] & 0xf0) ==
1535 printk(KERN_ERR PFX
"Error reading "
1536 "session %d: %x %s\n",
1537 session
, single_toc
.exec_status
[0],
1538 translate_error(single_toc
.
1540 /* An error reading the TOC. Return without sony_toc_read
1544 pr_debug(PFX
"add0 %01x, con0 %01x, poi0 %02x, "
1545 "1st trk %d, dsktyp %x, dum0 %x\n",
1546 single_toc
.address0
, single_toc
.control0
,
1548 bcd_to_int(single_toc
.first_track_num
),
1549 single_toc
.disk_type
, single_toc
.dummy0
);
1550 pr_debug(PFX
"add1 %01x, con1 %01x, poi1 %02x, "
1551 "lst trk %d, dummy1 %x, dum2 %x\n",
1552 single_toc
.address1
, single_toc
.control1
,
1554 bcd_to_int(single_toc
.last_track_num
),
1555 single_toc
.dummy1
, single_toc
.dummy2
);
1556 pr_debug(PFX
"add2 %01x, con2 %01x, poi2 %02x "
1557 "leadout start min %d, sec %d, frame %d\n",
1558 single_toc
.address2
, single_toc
.control2
,
1560 bcd_to_int(single_toc
.lead_out_start_msf
[0]),
1561 bcd_to_int(single_toc
.lead_out_start_msf
[1]),
1562 bcd_to_int(single_toc
.lead_out_start_msf
[2]));
1563 if (res_size
> 18 && single_toc
.pointb0
> 0xaf)
1564 pr_debug(PFX
"addb0 %01x, conb0 %01x, poib0 %02x, nextsession min %d, sec %d, frame %d\n"
1565 "#mode5_ptrs %02d, max_start_outer_leadout_msf min %d, sec %d, frame %d\n",
1566 single_toc
.addressb0
,
1567 single_toc
.controlb0
,
1569 bcd_to_int(single_toc
.
1570 next_poss_prog_area_msf
1572 bcd_to_int(single_toc
.
1573 next_poss_prog_area_msf
1575 bcd_to_int(single_toc
.
1576 next_poss_prog_area_msf
1578 single_toc
.num_mode_5_pointers
,
1579 bcd_to_int(single_toc
.
1580 max_start_outer_leadout_msf
1582 bcd_to_int(single_toc
.
1583 max_start_outer_leadout_msf
1585 bcd_to_int(single_toc
.
1586 max_start_outer_leadout_msf
1588 if (res_size
> 27 && single_toc
.pointb1
> 0xaf)
1589 pr_debug(PFX
"addb1 %01x, conb1 %01x, poib1 %02x, %x %x %x %x #skipint_ptrs %d, #skiptrkassign %d %x\n",
1590 single_toc
.addressb1
,
1591 single_toc
.controlb1
,
1593 single_toc
.dummyb0_1
[0],
1594 single_toc
.dummyb0_1
[1],
1595 single_toc
.dummyb0_1
[2],
1596 single_toc
.dummyb0_1
[3],
1597 single_toc
.num_skip_interval_pointers
,
1598 single_toc
.num_skip_track_assignments
,
1599 single_toc
.dummyb0_2
);
1600 if (res_size
> 36 && single_toc
.pointb2
> 0xaf)
1601 pr_debug(PFX
"addb2 %01x, conb2 %01x, poib2 %02x, %02x %02x %02x %02x %02x %02x %02x\n",
1602 single_toc
.addressb2
,
1603 single_toc
.controlb2
,
1605 single_toc
.tracksb2
[0],
1606 single_toc
.tracksb2
[1],
1607 single_toc
.tracksb2
[2],
1608 single_toc
.tracksb2
[3],
1609 single_toc
.tracksb2
[4],
1610 single_toc
.tracksb2
[5],
1611 single_toc
.tracksb2
[6]);
1612 if (res_size
> 45 && single_toc
.pointb3
> 0xaf)
1613 pr_debug(PFX
"addb3 %01x, conb3 %01x, poib3 %02x, %02x %02x %02x %02x %02x %02x %02x\n",
1614 single_toc
.addressb3
,
1615 single_toc
.controlb3
,
1617 single_toc
.tracksb3
[0],
1618 single_toc
.tracksb3
[1],
1619 single_toc
.tracksb3
[2],
1620 single_toc
.tracksb3
[3],
1621 single_toc
.tracksb3
[4],
1622 single_toc
.tracksb3
[5],
1623 single_toc
.tracksb3
[6]);
1624 if (res_size
> 54 && single_toc
.pointb4
> 0xaf)
1625 pr_debug(PFX
"addb4 %01x, conb4 %01x, poib4 %02x, %02x %02x %02x %02x %02x %02x %02x\n",
1626 single_toc
.addressb4
,
1627 single_toc
.controlb4
,
1629 single_toc
.tracksb4
[0],
1630 single_toc
.tracksb4
[1],
1631 single_toc
.tracksb4
[2],
1632 single_toc
.tracksb4
[3],
1633 single_toc
.tracksb4
[4],
1634 single_toc
.tracksb4
[5],
1635 single_toc
.tracksb4
[6]);
1636 if (res_size
> 63 && single_toc
.pointc0
> 0xaf)
1637 pr_debug(PFX
"addc0 %01x, conc0 %01x, poic0 %02x, %02x %02x %02x %02x %02x %02x %02x\n",
1638 single_toc
.addressc0
,
1639 single_toc
.controlc0
,
1641 single_toc
.dummyc0
[0],
1642 single_toc
.dummyc0
[1],
1643 single_toc
.dummyc0
[2],
1644 single_toc
.dummyc0
[3],
1645 single_toc
.dummyc0
[4],
1646 single_toc
.dummyc0
[5],
1647 single_toc
.dummyc0
[6]);
1651 sony_toc
.lead_out_start_msf
[0] =
1652 bcd_to_int(single_toc
.lead_out_start_msf
[0]);
1653 sony_toc
.lead_out_start_msf
[1] =
1654 bcd_to_int(single_toc
.lead_out_start_msf
[1]);
1655 sony_toc
.lead_out_start_msf
[2] =
1656 bcd_to_int(single_toc
.lead_out_start_msf
[2]);
1657 sony_toc
.lead_out_start_lba
=
1658 single_toc
.lead_out_start_lba
=
1659 msf_to_log(sony_toc
.lead_out_start_msf
);
1661 /* For points that do not exist, move the data over them
1662 to the right location. */
1663 if (single_toc
.pointb0
!= 0xb0) {
1664 memmove(((char *) &single_toc
) + 27,
1665 ((char *) &single_toc
) + 18,
1668 } else if (res_size
> 18) {
1669 sony_toc
.lead_out_start_msf
[0] =
1670 bcd_to_int(single_toc
.
1671 max_start_outer_leadout_msf
1673 sony_toc
.lead_out_start_msf
[1] =
1674 bcd_to_int(single_toc
.
1675 max_start_outer_leadout_msf
1677 sony_toc
.lead_out_start_msf
[2] =
1678 bcd_to_int(single_toc
.
1679 max_start_outer_leadout_msf
1681 sony_toc
.lead_out_start_lba
=
1682 msf_to_log(sony_toc
.
1683 lead_out_start_msf
);
1685 if (single_toc
.pointb1
!= 0xb1) {
1686 memmove(((char *) &single_toc
) + 36,
1687 ((char *) &single_toc
) + 27,
1691 if (single_toc
.pointb2
!= 0xb2) {
1692 memmove(((char *) &single_toc
) + 45,
1693 ((char *) &single_toc
) + 36,
1697 if (single_toc
.pointb3
!= 0xb3) {
1698 memmove(((char *) &single_toc
) + 54,
1699 ((char *) &single_toc
) + 45,
1703 if (single_toc
.pointb4
!= 0xb4) {
1704 memmove(((char *) &single_toc
) + 63,
1705 ((char *) &single_toc
) + 54,
1709 if (single_toc
.pointc0
!= 0xc0) {
1710 memmove(((char *) &single_toc
) + 72,
1711 ((char *) &single_toc
) + 63,
1716 printk(PRINT_INFO PFX
"start track lba %u, "
1717 "leadout start lba %u\n",
1718 single_toc
.start_track_lba
,
1719 single_toc
.lead_out_start_lba
);
1725 bcd_to_int(single_toc
.last_track_num
)
1727 bcd_to_int(single_toc
.
1728 first_track_num
); i
++) {
1729 printk(KERN_INFO PFX
"trk %02d: add 0x%01x, con 0x%01x, track %02d, start min %02d, sec %02d, frame %02d\n",
1731 single_toc
.tracks
[i
].address
,
1732 single_toc
.tracks
[i
].control
,
1733 bcd_to_int(single_toc
.
1735 bcd_to_int(single_toc
.
1739 bcd_to_int(single_toc
.
1743 bcd_to_int(single_toc
.
1748 bcd_to_int(single_toc
.
1751 bcd_to_int(single_toc
.
1755 bcd_to_int(single_toc
.
1758 bcd_to_int(single_toc
.
1762 printk(KERN_INFO PFX
"min track number %d, "
1763 "max track number %d\n",
1768 /* prepare a special table of contents for a CD-I disc. They don't have one. */
1769 if (single_toc
.disk_type
== 0x10 &&
1770 single_toc
.first_track_num
== 2 &&
1771 single_toc
.last_track_num
== 2 /* CD-I */ ) {
1772 sony_toc
.tracks
[totaltracks
].address
= 1;
1773 sony_toc
.tracks
[totaltracks
].control
= 4; /* force data tracks */
1774 sony_toc
.tracks
[totaltracks
].track
= 1;
1775 sony_toc
.tracks
[totaltracks
].
1776 track_start_msf
[0] = 0;
1777 sony_toc
.tracks
[totaltracks
].
1778 track_start_msf
[1] = 2;
1779 sony_toc
.tracks
[totaltracks
].
1780 track_start_msf
[2] = 0;
1784 /* gather track entries from this session */
1790 bcd_to_int(single_toc
.last_track_num
)
1792 bcd_to_int(single_toc
.
1794 i
++, totaltracks
++) {
1795 sony_toc
.tracks
[totaltracks
].
1797 single_toc
.tracks
[i
].address
;
1798 sony_toc
.tracks
[totaltracks
].
1800 single_toc
.tracks
[i
].control
;
1801 sony_toc
.tracks
[totaltracks
].
1803 bcd_to_int(single_toc
.
1805 sony_toc
.tracks
[totaltracks
].
1806 track_start_msf
[0] =
1807 bcd_to_int(single_toc
.
1809 track_start_msf
[0]);
1810 sony_toc
.tracks
[totaltracks
].
1811 track_start_msf
[1] =
1812 bcd_to_int(single_toc
.
1814 track_start_msf
[1]);
1815 sony_toc
.tracks
[totaltracks
].
1816 track_start_msf
[2] =
1817 bcd_to_int(single_toc
.
1819 track_start_msf
[2]);
1823 msf_to_log(sony_toc
.
1828 sony_toc
.tracks
[totaltracks
].
1832 tracks
[totaltracks
].
1835 sony_toc
.tracks
[totaltracks
].
1839 tracks
[totaltracks
].
1843 sony_toc
.first_track_num
= mint
;
1844 sony_toc
.last_track_num
= maxt
;
1845 /* Disk type of last session wins. For example:
1846 CD-Extra has disk type 0 for the first session, so
1847 a dumb HiFi CD player thinks it is a plain audio CD.
1848 We are interested in the disk type of the last session,
1849 which is 0x20 (XA) for CD-Extra, so we can access the
1851 sony_toc
.disk_type
= single_toc
.disk_type
;
1852 sony_toc
.sessions
= session
;
1854 /* don't believe everything :-) */
1856 single_toc
.start_track_lba
= 0;
1857 sony_toc
.start_track_lba
=
1858 single_toc
.start_track_lba
;
1860 if (session
> 1 && single_toc
.pointb0
== 0xb0 &&
1861 sony_toc
.lead_out_start_lba
==
1862 single_toc
.lead_out_start_lba
) {
1866 /* Let's not get carried away... */
1868 printk(KERN_NOTICE PFX
"too many sessions: "
1874 sony_toc
.track_entries
= totaltracks
;
1875 /* add one entry for the LAST track with track number CDROM_LEADOUT */
1876 sony_toc
.tracks
[totaltracks
].address
= single_toc
.address2
;
1877 sony_toc
.tracks
[totaltracks
].control
= single_toc
.control2
;
1878 sony_toc
.tracks
[totaltracks
].track
= CDROM_LEADOUT
;
1879 sony_toc
.tracks
[totaltracks
].track_start_msf
[0] =
1880 sony_toc
.lead_out_start_msf
[0];
1881 sony_toc
.tracks
[totaltracks
].track_start_msf
[1] =
1882 sony_toc
.lead_out_start_msf
[1];
1883 sony_toc
.tracks
[totaltracks
].track_start_msf
[2] =
1884 sony_toc
.lead_out_start_msf
[2];
1888 pr_debug(PFX
"Disk session %d, start track: %d, "
1890 session
, single_toc
.start_track_lba
,
1891 single_toc
.lead_out_start_lba
);
1893 pr_debug(PFX
"Leaving %s\n", __FUNCTION__
);
1898 * Uniform cdrom interface function
1899 * return multisession offset and sector information
1901 static int scd_get_last_session(struct cdrom_device_info
*cdi
,
1902 struct cdrom_multisession
*ms_info
)
1904 if (ms_info
== NULL
)
1907 if (!sony_toc_read
) {
1908 if (down_interruptible(&sony_sem
))
1909 return -ERESTARTSYS
;
1914 ms_info
->addr_format
= CDROM_LBA
;
1915 ms_info
->addr
.lba
= sony_toc
.start_track_lba
;
1916 ms_info
->xa_flag
= sony_toc
.disk_type
== SONY_XA_DISK_TYPE
||
1917 sony_toc
.disk_type
== 0x10 /* CDI */ ;
1923 * Search for a specific track in the table of contents.
1925 static int find_track(int track
)
1929 for (i
= 0; i
<= sony_toc
.track_entries
; i
++) {
1930 if (sony_toc
.tracks
[i
].track
== track
) {
1940 * Read the subcode and put it in last_sony_subcode for future use.
1942 static int read_subcode(void)
1944 unsigned int res_size
;
1947 do_sony_cd_cmd(SONY_REQ_SUBCODE_ADDRESS_CMD
,
1949 0, (unsigned char *) &last_sony_subcode
, &res_size
);
1951 || ((last_sony_subcode
.exec_status
[0] & 0xf0) == 0x20)) {
1952 printk(KERN_ERR PFX
"Sony CDROM error %s (read_subcode)\n",
1953 translate_error(last_sony_subcode
.exec_status
[1]));
1957 last_sony_subcode
.track_num
=
1958 bcd_to_int(last_sony_subcode
.track_num
);
1959 last_sony_subcode
.index_num
=
1960 bcd_to_int(last_sony_subcode
.index_num
);
1961 last_sony_subcode
.abs_msf
[0] =
1962 bcd_to_int(last_sony_subcode
.abs_msf
[0]);
1963 last_sony_subcode
.abs_msf
[1] =
1964 bcd_to_int(last_sony_subcode
.abs_msf
[1]);
1965 last_sony_subcode
.abs_msf
[2] =
1966 bcd_to_int(last_sony_subcode
.abs_msf
[2]);
1968 last_sony_subcode
.rel_msf
[0] =
1969 bcd_to_int(last_sony_subcode
.rel_msf
[0]);
1970 last_sony_subcode
.rel_msf
[1] =
1971 bcd_to_int(last_sony_subcode
.rel_msf
[1]);
1972 last_sony_subcode
.rel_msf
[2] =
1973 bcd_to_int(last_sony_subcode
.rel_msf
[2]);
1978 * Uniform cdrom interface function
1979 * return the media catalog number found on some older audio cds
1982 scd_get_mcn(struct cdrom_device_info
*cdi
, struct cdrom_mcn
*mcn
)
1984 unsigned char resbuffer
[2 + 14];
1985 unsigned char *mcnp
= mcn
->medium_catalog_number
;
1986 unsigned char *resp
= resbuffer
+ 3;
1987 unsigned int res_size
;
1989 memset(mcn
->medium_catalog_number
, 0, 14);
1990 if (down_interruptible(&sony_sem
))
1991 return -ERESTARTSYS
;
1992 do_sony_cd_cmd(SONY_REQ_UPC_EAN_CMD
,
1993 NULL
, 0, resbuffer
, &res_size
);
1995 if ((res_size
< 2) || ((resbuffer
[0] & 0xf0) == 0x20));
1997 /* packed bcd to single ASCII digits */
1998 *mcnp
++ = (*resp
>> 4) + '0';
1999 *mcnp
++ = (*resp
++ & 0x0f) + '0';
2000 *mcnp
++ = (*resp
>> 4) + '0';
2001 *mcnp
++ = (*resp
++ & 0x0f) + '0';
2002 *mcnp
++ = (*resp
>> 4) + '0';
2003 *mcnp
++ = (*resp
++ & 0x0f) + '0';
2004 *mcnp
++ = (*resp
>> 4) + '0';
2005 *mcnp
++ = (*resp
++ & 0x0f) + '0';
2006 *mcnp
++ = (*resp
>> 4) + '0';
2007 *mcnp
++ = (*resp
++ & 0x0f) + '0';
2008 *mcnp
++ = (*resp
>> 4) + '0';
2009 *mcnp
++ = (*resp
++ & 0x0f) + '0';
2010 *mcnp
++ = (*resp
>> 4) + '0';
2018 * Get the subchannel info like the CDROMSUBCHNL command wants to see it. If
2019 * the drive is playing, the subchannel needs to be read (since it would be
2020 * changing). If the drive is paused or completed, the subcode information has
2021 * already been stored, just use that. The ioctl call wants things in decimal
2022 * (not BCD), so all the conversions are done.
2024 static int sony_get_subchnl_info(struct cdrom_subchnl
*schi
)
2026 /* Get attention stuff */
2027 while (handle_sony_cd_attention());
2030 if (!sony_toc_read
) {
2034 switch (sony_audio_status
) {
2035 case CDROM_AUDIO_NO_STATUS
:
2036 case CDROM_AUDIO_PLAY
:
2037 if (read_subcode() < 0) {
2042 case CDROM_AUDIO_PAUSED
:
2043 case CDROM_AUDIO_COMPLETED
:
2047 case CDROM_AUDIO_NO_STATUS
:
2048 schi
->cdsc_audiostatus
= sony_audio_status
;
2052 case CDROM_AUDIO_INVALID
:
2053 case CDROM_AUDIO_ERROR
:
2058 schi
->cdsc_audiostatus
= sony_audio_status
;
2059 schi
->cdsc_adr
= last_sony_subcode
.address
;
2060 schi
->cdsc_ctrl
= last_sony_subcode
.control
;
2061 schi
->cdsc_trk
= last_sony_subcode
.track_num
;
2062 schi
->cdsc_ind
= last_sony_subcode
.index_num
;
2063 if (schi
->cdsc_format
== CDROM_MSF
) {
2064 schi
->cdsc_absaddr
.msf
.minute
=
2065 last_sony_subcode
.abs_msf
[0];
2066 schi
->cdsc_absaddr
.msf
.second
=
2067 last_sony_subcode
.abs_msf
[1];
2068 schi
->cdsc_absaddr
.msf
.frame
=
2069 last_sony_subcode
.abs_msf
[2];
2071 schi
->cdsc_reladdr
.msf
.minute
=
2072 last_sony_subcode
.rel_msf
[0];
2073 schi
->cdsc_reladdr
.msf
.second
=
2074 last_sony_subcode
.rel_msf
[1];
2075 schi
->cdsc_reladdr
.msf
.frame
=
2076 last_sony_subcode
.rel_msf
[2];
2077 } else if (schi
->cdsc_format
== CDROM_LBA
) {
2078 schi
->cdsc_absaddr
.lba
=
2079 msf_to_log(last_sony_subcode
.abs_msf
);
2080 schi
->cdsc_reladdr
.lba
=
2081 msf_to_log(last_sony_subcode
.rel_msf
);
2087 /* Get audio data from the drive. This is fairly complex because I
2088 am looking for status and data at the same time, but if I get status
2089 then I just look for data. I need to get the status immediately so
2090 the switch from audio to data tracks will happen quickly. */
2092 read_audio_data(char *buffer
, unsigned char res_reg
[], int *res_size
)
2094 unsigned long retry_count
;
2103 /* Wait for the drive to tell us we have something */
2104 retry_count
= jiffies
+ SONY_JIFFIES_TIMEOUT
;
2105 continue_read_audio_wait
:
2106 while (time_before(jiffies
, retry_count
) && !(is_data_ready())
2107 && !(is_result_ready() || result_read
)) {
2108 while (handle_sony_cd_attention());
2112 if (!(is_data_ready())) {
2113 if (is_result_ready() && !result_read
) {
2114 get_result(res_reg
, res_size
);
2116 /* Read block status and continue waiting for data. */
2117 if ((res_reg
[0] & 0xf0) == 0x50) {
2119 goto continue_read_audio_wait
;
2121 /* Invalid data from the drive. Shut down the operation. */
2122 else if ((res_reg
[0] & 0xf0) != 0x20) {
2123 printk(KERN_WARNING PFX
"Got result that "
2124 "should have been error: %d\n",
2127 res_reg
[1] = SONY_BAD_DATA_ERR
;
2132 pr_debug(PFX
"timeout out %d\n", __LINE__
);
2134 res_reg
[1] = SONY_TIMEOUT_OP_ERR
;
2141 /* If data block, then get 2340 bytes offset by 12. */
2142 if (sony_raw_data_mode
) {
2143 insb(sony_cd_read_reg
, buffer
+ CD_XA_HEAD
,
2146 /* Audio gets the whole 2352 bytes. */
2147 insb(sony_cd_read_reg
, buffer
, CD_FRAMESIZE_RAW
);
2150 /* If I haven't already gotten the result, get it now. */
2152 /* Wait for the drive to tell us we have something */
2153 retry_count
= jiffies
+ SONY_JIFFIES_TIMEOUT
;
2154 while (time_before(jiffies
, retry_count
)
2155 && !(is_result_ready())) {
2156 while (handle_sony_cd_attention());
2161 if (!is_result_ready()) {
2162 pr_debug(PFX
"timeout out %d\n", __LINE__
);
2164 res_reg
[1] = SONY_TIMEOUT_OP_ERR
;
2169 get_result(res_reg
, res_size
);
2173 if ((res_reg
[0] & 0xf0) == 0x50) {
2174 if ((res_reg
[0] == SONY_NO_CIRC_ERR_BLK_STAT
)
2175 || (res_reg
[0] == SONY_NO_LECC_ERR_BLK_STAT
)
2176 || (res_reg
[0] == SONY_RECOV_LECC_ERR_BLK_STAT
)
2177 || (res_reg
[0] == SONY_NO_ERR_DETECTION_STAT
)) {
2178 /* Ok, nothing to do. */
2180 printk(KERN_ERR PFX
"Data block error: 0x%x\n",
2183 res_reg
[1] = SONY_BAD_DATA_ERR
;
2186 } else if ((res_reg
[0] & 0xf0) != 0x20) {
2187 /* The drive gave me bad status, I don't know what to do.
2188 Reset the driver and return an error. */
2189 printk(KERN_NOTICE PFX
"Invalid block status: 0x%x\n",
2193 res_reg
[1] = SONY_BAD_DATA_ERR
;
2199 /* Perform a raw data read. This will automatically detect the
2200 track type and read the proper data (audio or data). */
2201 static int read_audio(struct cdrom_read_audio
*ra
)
2204 unsigned char params
[2];
2205 unsigned char res_reg
[12];
2206 unsigned int res_size
;
2207 unsigned int cframe
;
2209 if (down_interruptible(&sony_sem
))
2210 return -ERESTARTSYS
;
2214 /* Set the drive to do raw operations. */
2215 params
[0] = SONY_SD_DECODE_PARAM
;
2216 params
[1] = 0x06 | sony_raw_data_mode
;
2217 do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD
,
2218 params
, 2, res_reg
, &res_size
);
2219 if ((res_size
< 2) || ((res_reg
[0] & 0xf0) == 0x20)) {
2220 printk(KERN_ERR PFX
"Unable to set decode params: 0x%2.2x\n",
2226 /* From here down, we have to goto exit_read_audio instead of returning
2227 because the drive parameters have to be set back to data before
2231 if (start_request(ra
->addr
.lba
, ra
->nframes
)) {
2233 goto exit_read_audio
;
2236 /* For every requested frame. */
2238 while (cframe
< ra
->nframes
) {
2239 read_audio_data(audio_buffer
, res_reg
, &res_size
);
2240 if ((res_reg
[0] & 0xf0) == 0x20) {
2241 if (res_reg
[1] == SONY_BAD_DATA_ERR
) {
2242 printk(KERN_ERR PFX
"Data error on audio "
2244 ra
->addr
.lba
+ cframe
);
2245 } else if (res_reg
[1] == SONY_ILL_TRACK_R_ERR
) {
2246 /* Illegal track type, change track types and start over. */
2247 sony_raw_data_mode
=
2248 (sony_raw_data_mode
) ? 0 : 1;
2250 /* Set the drive mode. */
2251 params
[0] = SONY_SD_DECODE_PARAM
;
2252 params
[1] = 0x06 | sony_raw_data_mode
;
2253 do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD
,
2255 2, res_reg
, &res_size
);
2257 || ((res_reg
[0] & 0xf0) == 0x20)) {
2258 printk(KERN_ERR PFX
"Unable to set "
2259 "decode params: 0x%2.2x\n",
2262 goto exit_read_audio
;
2265 /* Restart the request on the current frame. */
2267 (ra
->addr
.lba
+ cframe
,
2268 ra
->nframes
- cframe
)) {
2270 goto exit_read_audio
;
2273 /* Don't go back to the top because don't want to get into
2274 and infinite loop. A lot of code gets duplicated, but
2275 that's no big deal, I don't guess. */
2276 read_audio_data(audio_buffer
, res_reg
,
2278 if ((res_reg
[0] & 0xf0) == 0x20) {
2280 SONY_BAD_DATA_ERR
) {
2281 printk(KERN_ERR PFX
"Data error"
2282 " on audio sector %d\n",
2286 printk(KERN_ERR PFX
"Error reading audio data on sector %d: %s\n",
2287 ra
->addr
.lba
+ cframe
,
2291 goto exit_read_audio
;
2293 } else if (copy_to_user(ra
->buf
+
2297 CD_FRAMESIZE_RAW
)) {
2299 goto exit_read_audio
;
2302 printk(KERN_ERR PFX
"Error reading audio "
2303 "data on sector %d: %s\n",
2304 ra
->addr
.lba
+ cframe
,
2305 translate_error(res_reg
[1]));
2307 goto exit_read_audio
;
2309 } else if (copy_to_user(ra
->buf
+ (CD_FRAMESIZE_RAW
* cframe
),
2310 (char *)audio_buffer
,
2311 CD_FRAMESIZE_RAW
)) {
2313 goto exit_read_audio
;
2319 get_result(res_reg
, &res_size
);
2320 if ((res_reg
[0] & 0xf0) == 0x20) {
2321 printk(KERN_ERR PFX
"Error return from audio read: %s\n",
2322 translate_error(res_reg
[1]));
2324 goto exit_read_audio
;
2329 /* Set the drive mode back to the proper one for the disk. */
2330 params
[0] = SONY_SD_DECODE_PARAM
;
2331 if (!sony_xa_mode
) {
2336 do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD
,
2337 params
, 2, res_reg
, &res_size
);
2338 if ((res_size
< 2) || ((res_reg
[0] & 0xf0) == 0x20)) {
2339 printk(KERN_ERR PFX
"Unable to reset decode params: 0x%2.2x\n",
2351 do_sony_cd_cmd_chk(const char *name
,
2353 unsigned char *params
,
2354 unsigned int num_params
,
2355 unsigned char *result_buffer
, unsigned int *result_size
)
2357 do_sony_cd_cmd(cmd
, params
, num_params
, result_buffer
,
2359 if ((*result_size
< 2) || ((result_buffer
[0] & 0xf0) == 0x20)) {
2360 printk(KERN_ERR PFX
"Error %s (CDROM%s)\n",
2361 translate_error(result_buffer
[1]), name
);
2368 * Uniform cdrom interface function
2371 static int scd_tray_move(struct cdrom_device_info
*cdi
, int position
)
2375 if (down_interruptible(&sony_sem
))
2376 return -ERESTARTSYS
;
2377 if (position
== 1 /* open tray */ ) {
2378 unsigned char res_reg
[12];
2379 unsigned int res_size
;
2381 do_sony_cd_cmd(SONY_AUDIO_STOP_CMD
, NULL
, 0, res_reg
,
2383 do_sony_cd_cmd(SONY_SPIN_DOWN_CMD
, NULL
, 0, res_reg
,
2386 sony_audio_status
= CDROM_AUDIO_INVALID
;
2387 retval
= do_sony_cd_cmd_chk("EJECT", SONY_EJECT_CMD
, NULL
, 0,
2388 res_reg
, &res_size
);
2390 if (0 == scd_spinup())
2399 * The big ugly ioctl handler.
2401 static int scd_audio_ioctl(struct cdrom_device_info
*cdi
,
2402 unsigned int cmd
, void *arg
)
2404 unsigned char res_reg
[12];
2405 unsigned int res_size
;
2406 unsigned char params
[7];
2409 if (down_interruptible(&sony_sem
))
2410 return -ERESTARTSYS
;
2412 case CDROMSTART
: /* Spin up the drive */
2413 retval
= do_sony_cd_cmd_chk("START", SONY_SPIN_UP_CMD
, NULL
,
2414 0, res_reg
, &res_size
);
2417 case CDROMSTOP
: /* Spin down the drive */
2418 do_sony_cd_cmd(SONY_AUDIO_STOP_CMD
, NULL
, 0, res_reg
,
2422 * Spin the drive down, ignoring the error if the disk was
2423 * already not spinning.
2425 sony_audio_status
= CDROM_AUDIO_NO_STATUS
;
2426 retval
= do_sony_cd_cmd_chk("STOP", SONY_SPIN_DOWN_CMD
, NULL
,
2427 0, res_reg
, &res_size
);
2430 case CDROMPAUSE
: /* Pause the drive */
2431 if (do_sony_cd_cmd_chk
2432 ("PAUSE", SONY_AUDIO_STOP_CMD
, NULL
, 0, res_reg
,
2437 /* Get the current position and save it for resuming */
2438 if (read_subcode() < 0) {
2442 cur_pos_msf
[0] = last_sony_subcode
.abs_msf
[0];
2443 cur_pos_msf
[1] = last_sony_subcode
.abs_msf
[1];
2444 cur_pos_msf
[2] = last_sony_subcode
.abs_msf
[2];
2445 sony_audio_status
= CDROM_AUDIO_PAUSED
;
2449 case CDROMRESUME
: /* Start the drive after being paused */
2450 if (sony_audio_status
!= CDROM_AUDIO_PAUSED
) {
2455 do_sony_cd_cmd(SONY_SPIN_UP_CMD
, NULL
, 0, res_reg
,
2458 /* Start the drive at the saved position. */
2459 params
[1] = int_to_bcd(cur_pos_msf
[0]);
2460 params
[2] = int_to_bcd(cur_pos_msf
[1]);
2461 params
[3] = int_to_bcd(cur_pos_msf
[2]);
2462 params
[4] = int_to_bcd(final_pos_msf
[0]);
2463 params
[5] = int_to_bcd(final_pos_msf
[1]);
2464 params
[6] = int_to_bcd(final_pos_msf
[2]);
2466 if (do_sony_cd_cmd_chk
2467 ("RESUME", SONY_AUDIO_PLAYBACK_CMD
, params
, 7, res_reg
,
2472 sony_audio_status
= CDROM_AUDIO_PLAY
;
2476 case CDROMPLAYMSF
: /* Play starting at the given MSF address. */
2477 do_sony_cd_cmd(SONY_SPIN_UP_CMD
, NULL
, 0, res_reg
,
2480 /* The parameters are given in int, must be converted */
2481 for (i
= 1; i
< 7; i
++) {
2483 int_to_bcd(((unsigned char *) arg
)[i
- 1]);
2486 if (do_sony_cd_cmd_chk
2487 ("PLAYMSF", SONY_AUDIO_PLAYBACK_CMD
, params
, 7,
2488 res_reg
, &res_size
) < 0) {
2493 /* Save the final position for pauses and resumes */
2494 final_pos_msf
[0] = bcd_to_int(params
[4]);
2495 final_pos_msf
[1] = bcd_to_int(params
[5]);
2496 final_pos_msf
[2] = bcd_to_int(params
[6]);
2497 sony_audio_status
= CDROM_AUDIO_PLAY
;
2501 case CDROMREADTOCHDR
: /* Read the table of contents header */
2503 struct cdrom_tochdr
*hdr
;
2506 if (!sony_toc_read
) {
2511 hdr
= (struct cdrom_tochdr
*) arg
;
2512 hdr
->cdth_trk0
= sony_toc
.first_track_num
;
2513 hdr
->cdth_trk1
= sony_toc
.last_track_num
;
2518 case CDROMREADTOCENTRY
: /* Read a given table of contents entry */
2520 struct cdrom_tocentry
*entry
;
2522 unsigned char *msf_val
= NULL
;
2525 if (!sony_toc_read
) {
2530 entry
= (struct cdrom_tocentry
*) arg
;
2532 track_idx
= find_track(entry
->cdte_track
);
2533 if (track_idx
< 0) {
2539 sony_toc
.tracks
[track_idx
].address
;
2541 sony_toc
.tracks
[track_idx
].control
;
2543 sony_toc
.tracks
[track_idx
].track_start_msf
;
2545 /* Logical buffer address or MSF format requested? */
2546 if (entry
->cdte_format
== CDROM_LBA
) {
2547 entry
->cdte_addr
.lba
= msf_to_log(msf_val
);
2548 } else if (entry
->cdte_format
== CDROM_MSF
) {
2549 entry
->cdte_addr
.msf
.minute
= *msf_val
;
2550 entry
->cdte_addr
.msf
.second
=
2552 entry
->cdte_addr
.msf
.frame
=
2559 case CDROMPLAYTRKIND
: /* Play a track. This currently ignores index. */
2561 struct cdrom_ti
*ti
= (struct cdrom_ti
*) arg
;
2565 if (!sony_toc_read
) {
2570 if ((ti
->cdti_trk0
< sony_toc
.first_track_num
)
2571 || (ti
->cdti_trk0
> sony_toc
.last_track_num
)
2572 || (ti
->cdti_trk1
< ti
->cdti_trk0
)) {
2577 track_idx
= find_track(ti
->cdti_trk0
);
2578 if (track_idx
< 0) {
2583 int_to_bcd(sony_toc
.tracks
[track_idx
].
2584 track_start_msf
[0]);
2586 int_to_bcd(sony_toc
.tracks
[track_idx
].
2587 track_start_msf
[1]);
2589 int_to_bcd(sony_toc
.tracks
[track_idx
].
2590 track_start_msf
[2]);
2593 * If we want to stop after the last track, use the lead-out
2596 if (ti
->cdti_trk1
>= sony_toc
.last_track_num
) {
2597 track_idx
= find_track(CDROM_LEADOUT
);
2599 track_idx
= find_track(ti
->cdti_trk1
+ 1);
2601 if (track_idx
< 0) {
2606 int_to_bcd(sony_toc
.tracks
[track_idx
].
2607 track_start_msf
[0]);
2609 int_to_bcd(sony_toc
.tracks
[track_idx
].
2610 track_start_msf
[1]);
2612 int_to_bcd(sony_toc
.tracks
[track_idx
].
2613 track_start_msf
[2]);
2616 do_sony_cd_cmd(SONY_SPIN_UP_CMD
, NULL
, 0, res_reg
,
2619 do_sony_cd_cmd(SONY_AUDIO_PLAYBACK_CMD
, params
, 7,
2620 res_reg
, &res_size
);
2623 || ((res_reg
[0] & 0xf0) == 0x20)) {
2625 "Params: %x %x %x %x %x %x %x\n",
2626 params
[0], params
[1], params
[2],
2627 params
[3], params
[4], params
[5],
2630 "Error %s (CDROMPLAYTRKIND)\n",
2631 translate_error(res_reg
[1]));
2636 /* Save the final position for pauses and resumes */
2637 final_pos_msf
[0] = bcd_to_int(params
[4]);
2638 final_pos_msf
[1] = bcd_to_int(params
[5]);
2639 final_pos_msf
[2] = bcd_to_int(params
[6]);
2640 sony_audio_status
= CDROM_AUDIO_PLAY
;
2645 case CDROMVOLCTRL
: /* Volume control. What volume does this change, anyway? */
2647 struct cdrom_volctrl
*volctrl
=
2648 (struct cdrom_volctrl
*) arg
;
2650 params
[0] = SONY_SD_AUDIO_VOLUME
;
2651 params
[1] = volctrl
->channel0
;
2652 params
[2] = volctrl
->channel1
;
2653 retval
= do_sony_cd_cmd_chk("VOLCTRL",
2654 SONY_SET_DRIVE_PARAM_CMD
,
2659 case CDROMSUBCHNL
: /* Get subchannel info */
2660 retval
= sony_get_subchnl_info((struct cdrom_subchnl
*) arg
);
2671 static int scd_dev_ioctl(struct cdrom_device_info
*cdi
,
2672 unsigned int cmd
, unsigned long arg
)
2674 void __user
*argp
= (void __user
*)arg
;
2677 if (down_interruptible(&sony_sem
))
2678 return -ERESTARTSYS
;
2680 case CDROMREADAUDIO
: /* Read 2352 byte audio tracks and 2340 byte
2683 struct cdrom_read_audio ra
;
2687 if (!sony_toc_read
) {
2692 if (copy_from_user(&ra
, argp
, sizeof(ra
))) {
2697 if (ra
.nframes
== 0) {
2702 if (!access_ok(VERIFY_WRITE
, ra
.buf
,
2703 CD_FRAMESIZE_RAW
* ra
.nframes
))
2706 if (ra
.addr_format
== CDROM_LBA
) {
2708 sony_toc
.lead_out_start_lba
)
2709 || (ra
.addr
.lba
+ ra
.nframes
>=
2710 sony_toc
.lead_out_start_lba
)) {
2714 } else if (ra
.addr_format
== CDROM_MSF
) {
2715 if ((ra
.addr
.msf
.minute
>= 75)
2716 || (ra
.addr
.msf
.second
>= 60)
2717 || (ra
.addr
.msf
.frame
>= 75)) {
2722 ra
.addr
.lba
= ((ra
.addr
.msf
.minute
* 4500)
2723 + (ra
.addr
.msf
.second
* 75)
2724 + ra
.addr
.msf
.frame
);
2726 sony_toc
.lead_out_start_lba
)
2727 || (ra
.addr
.lba
+ ra
.nframes
>=
2728 sony_toc
.lead_out_start_lba
)) {
2733 /* I know, this can go negative on an unsigned. However,
2734 the first thing done to the data is to add this value,
2735 so this should compensate and allow direct msf access. */
2736 ra
.addr
.lba
-= LOG_START_OFFSET
;
2742 retval
= read_audio(&ra
);
2755 static int scd_spinup(void)
2757 unsigned char res_reg
[12];
2758 unsigned int res_size
;
2764 do_sony_cd_cmd(SONY_SPIN_UP_CMD
, NULL
, 0, res_reg
, &res_size
);
2766 /* The drive sometimes returns error 0. I don't know why, but ignore
2767 it. It seems to mean the drive has already done the operation. */
2768 if ((res_size
< 2) || ((res_reg
[0] != 0) && (res_reg
[1] != 0))) {
2769 printk(KERN_ERR PFX
"%s error (scd_open, spin up)\n",
2770 translate_error(res_reg
[1]));
2774 do_sony_cd_cmd(SONY_READ_TOC_CMD
, NULL
, 0, res_reg
, &res_size
);
2776 /* The drive sometimes returns error 0. I don't know why, but ignore
2777 it. It seems to mean the drive has already done the operation. */
2778 if ((res_size
< 2) || ((res_reg
[0] != 0) && (res_reg
[1] != 0))) {
2779 /* If the drive is already playing, it's ok. */
2780 if ((res_reg
[1] == SONY_AUDIO_PLAYING_ERR
)
2781 || (res_reg
[1] == 0)) {
2785 /* If the drive says it is not spun up (even though we just did it!)
2786 then retry the operation at least a few times. */
2787 if ((res_reg
[1] == SONY_NOT_SPIN_ERR
)
2788 && (num_spin_ups
< MAX_CDU31A_RETRIES
)) {
2790 goto respinup_on_open
;
2793 printk(KERN_ERR PFX
"Error %s (scd_open, read toc)\n",
2794 translate_error(res_reg
[1]));
2795 do_sony_cd_cmd(SONY_SPIN_DOWN_CMD
, NULL
, 0, res_reg
,
2803 * Open the drive for operations. Spin the drive up and read the table of
2804 * contents if these have not already been done.
2806 static int scd_open(struct cdrom_device_info
*cdi
, int purpose
)
2808 unsigned char res_reg
[12];
2809 unsigned int res_size
;
2810 unsigned char params
[2];
2813 /* Open for IOCTLs only - no media check */
2818 if (sony_usage
== 0) {
2819 if (scd_spinup() != 0)
2822 if (!sony_toc_read
) {
2823 do_sony_cd_cmd(SONY_SPIN_DOWN_CMD
, NULL
, 0,
2824 res_reg
, &res_size
);
2828 /* For XA on the CDU31A only, we have to do special reads.
2829 The CDU33A handles XA automagically. */
2830 /* if ( (sony_toc.disk_type == SONY_XA_DISK_TYPE) */
2831 if ((sony_toc
.disk_type
!= 0x00)
2832 && (!is_double_speed
)) {
2833 params
[0] = SONY_SD_DECODE_PARAM
;
2835 do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD
,
2836 params
, 2, res_reg
, &res_size
);
2838 || ((res_reg
[0] & 0xf0) == 0x20)) {
2839 printk(KERN_WARNING PFX
"Unable to set "
2840 "XA params: 0x%2.2x\n", res_reg
[1]);
2844 /* A non-XA disk. Set the parms back if necessary. */
2845 else if (sony_xa_mode
) {
2846 params
[0] = SONY_SD_DECODE_PARAM
;
2848 do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD
,
2849 params
, 2, res_reg
, &res_size
);
2851 || ((res_reg
[0] & 0xf0) == 0x20)) {
2852 printk(KERN_WARNING PFX
"Unable to reset "
2853 "XA params: 0x%2.2x\n", res_reg
[1]);
2868 * Close the drive. Spin it down if no task is using it. The spin
2869 * down will fail if playing audio, so audio play is OK.
2871 static void scd_release(struct cdrom_device_info
*cdi
)
2873 if (sony_usage
== 1) {
2874 unsigned char res_reg
[12];
2875 unsigned int res_size
;
2877 do_sony_cd_cmd(SONY_SPIN_DOWN_CMD
, NULL
, 0, res_reg
,
2885 static struct cdrom_device_ops scd_dops
= {
2887 .release
= scd_release
,
2888 .drive_status
= scd_drive_status
,
2889 .media_changed
= scd_media_changed
,
2890 .tray_move
= scd_tray_move
,
2891 .lock_door
= scd_lock_door
,
2892 .select_speed
= scd_select_speed
,
2893 .get_last_session
= scd_get_last_session
,
2894 .get_mcn
= scd_get_mcn
,
2896 .audio_ioctl
= scd_audio_ioctl
,
2897 .dev_ioctl
= scd_dev_ioctl
,
2898 .capability
= CDC_OPEN_TRAY
| CDC_CLOSE_TRAY
| CDC_LOCK
|
2899 CDC_SELECT_SPEED
| CDC_MULTI_SESSION
|
2900 CDC_MCN
| CDC_MEDIA_CHANGED
| CDC_PLAY_AUDIO
|
2901 CDC_RESET
| CDC_IOCTLS
| CDC_DRIVE_STATUS
,
2905 static struct cdrom_device_info scd_info
= {
2912 static int scd_block_open(struct inode
*inode
, struct file
*file
)
2914 return cdrom_open(&scd_info
, inode
, file
);
2917 static int scd_block_release(struct inode
*inode
, struct file
*file
)
2919 return cdrom_release(&scd_info
, file
);
2922 static int scd_block_ioctl(struct inode
*inode
, struct file
*file
,
2923 unsigned cmd
, unsigned long arg
)
2927 /* The eject and close commands should be handled by Uniform CD-ROM
2928 * driver - but I always got hard lockup instead of eject
2929 * until I put this here.
2933 scd_lock_door(&scd_info
, 0);
2934 retval
= scd_tray_move(&scd_info
, 1);
2936 case CDROMCLOSETRAY
:
2937 retval
= scd_tray_move(&scd_info
, 0);
2940 retval
= cdrom_ioctl(file
, &scd_info
, inode
, cmd
, arg
);
2945 static int scd_block_media_changed(struct gendisk
*disk
)
2947 return cdrom_media_changed(&scd_info
);
2950 struct block_device_operations scd_bdops
=
2952 .owner
= THIS_MODULE
,
2953 .open
= scd_block_open
,
2954 .release
= scd_block_release
,
2955 .ioctl
= scd_block_ioctl
,
2956 .media_changed
= scd_block_media_changed
,
2959 static struct gendisk
*scd_gendisk
;
2961 /* The different types of disc loading mechanisms supported */
2962 static char *load_mech
[] __initdata
=
2963 { "caddy", "tray", "pop-up", "unknown" };
2966 get_drive_configuration(unsigned short base_io
,
2967 unsigned char res_reg
[], unsigned int *res_size
)
2969 unsigned long retry_count
;
2972 if (!request_region(base_io
, 4, "cdu31a"))
2975 /* Set the base address */
2976 cdu31a_port
= base_io
;
2978 /* Set up all the register locations */
2979 sony_cd_cmd_reg
= cdu31a_port
+ SONY_CMD_REG_OFFSET
;
2980 sony_cd_param_reg
= cdu31a_port
+ SONY_PARAM_REG_OFFSET
;
2981 sony_cd_write_reg
= cdu31a_port
+ SONY_WRITE_REG_OFFSET
;
2982 sony_cd_control_reg
= cdu31a_port
+ SONY_CONTROL_REG_OFFSET
;
2983 sony_cd_status_reg
= cdu31a_port
+ SONY_STATUS_REG_OFFSET
;
2984 sony_cd_result_reg
= cdu31a_port
+ SONY_RESULT_REG_OFFSET
;
2985 sony_cd_read_reg
= cdu31a_port
+ SONY_READ_REG_OFFSET
;
2986 sony_cd_fifost_reg
= cdu31a_port
+ SONY_FIFOST_REG_OFFSET
;
2989 * Check to see if anything exists at the status register location.
2990 * I don't know if this is a good way to check, but it seems to work
2993 if (read_status_register() != 0xff) {
2995 * Reset the drive and wait for attention from it (to say it's reset).
2996 * If you don't wait, the next operation will probably fail.
2999 retry_count
= jiffies
+ SONY_RESET_TIMEOUT
;
3000 while (time_before(jiffies
, retry_count
)
3001 && (!is_attention())) {
3006 /* If attention is never seen probably not a CDU31a present */
3007 if (!is_attention()) {
3014 * Get the drive configuration.
3016 do_sony_cd_cmd(SONY_REQ_DRIVE_CONFIG_CMD
,
3018 0, (unsigned char *) res_reg
, res_size
);
3019 if (*res_size
<= 2 || (res_reg
[0] & 0xf0) != 0)
3024 /* Return an error */
3027 release_region(cdu31a_port
, 4);
3034 * Set up base I/O and interrupts, called from main.c.
3037 static int __init
cdu31a_setup(char *strings
)
3041 (void) get_options(strings
, ARRAY_SIZE(ints
), ints
);
3044 cdu31a_port
= ints
[1];
3047 cdu31a_irq
= ints
[2];
3049 if ((strings
!= NULL
) && (*strings
!= '\0')) {
3050 if (strcmp(strings
, "PAS") == 0) {
3053 printk(KERN_NOTICE PFX
"Unknown interface type: %s\n",
3061 __setup("cdu31a=", cdu31a_setup
);
3066 * Initialize the driver.
3068 int __init
cdu31a_init(void)
3070 struct s_sony_drive_config drive_config
;
3071 struct gendisk
*disk
;
3073 unsigned int res_size
;
3080 * According to Alex Freed (freed@europa.orion.adobe.com), this is
3081 * required for the Fusion CD-16 package. If the sound driver is
3082 * loaded, it should work fine, but just in case...
3084 * The following turn on the CD-ROM interface for a Fusion CD-16.
3086 if (sony_pas_init
) {
3091 /* Setting the base I/O address to 0xffff will disable it. */
3092 if (cdu31a_port
== 0xffff)
3095 if (cdu31a_port
!= 0) {
3096 /* Need IRQ 0 because we can't sleep here. */
3097 tmp_irq
= cdu31a_irq
;
3099 if (!get_drive_configuration(cdu31a_port
,
3100 drive_config
.exec_status
,
3103 cdu31a_irq
= tmp_irq
;
3106 for (i
= 0; cdu31a_addresses
[i
].base
; i
++) {
3107 if (get_drive_configuration(cdu31a_addresses
[i
].base
,
3108 drive_config
.exec_status
,
3110 cdu31a_irq
= cdu31a_addresses
[i
].int_num
;
3118 if (register_blkdev(MAJOR_NR
, "cdu31a"))
3121 disk
= alloc_disk(1);
3124 disk
->major
= MAJOR_NR
;
3125 disk
->first_minor
= 0;
3126 sprintf(disk
->disk_name
, "cdu31a");
3127 disk
->fops
= &scd_bdops
;
3128 disk
->flags
= GENHD_FL_CD
;
3130 if (SONY_HWC_DOUBLE_SPEED(drive_config
))
3131 is_double_speed
= 1;
3133 tmp_irq
= cdu31a_irq
; /* Need IRQ 0 because we can't sleep here. */
3136 sony_speed
= is_double_speed
; /* Set 2X drives to 2X by default */
3137 set_drive_params(sony_speed
);
3139 cdu31a_irq
= tmp_irq
;
3141 if (cdu31a_irq
> 0) {
3143 (cdu31a_irq
, cdu31a_interrupt
, SA_INTERRUPT
,
3145 printk(KERN_WARNING PFX
"Unable to grab IRQ%d for "
3146 "the CDU31A driver\n", cdu31a_irq
);
3151 sprintf(msg
, "Sony I/F CDROM : %8.8s %16.16s %8.8s\n",
3152 drive_config
.vendor_id
,
3153 drive_config
.product_id
,
3154 drive_config
.product_rev_level
);
3155 sprintf(buf
, " Capabilities: %s",
3156 load_mech
[SONY_HWC_GET_LOAD_MECH(drive_config
)]);
3158 if (SONY_HWC_AUDIO_PLAYBACK(drive_config
))
3159 strcat(msg
, ", audio");
3161 deficiency
|= CDC_PLAY_AUDIO
;
3162 if (SONY_HWC_EJECT(drive_config
))
3163 strcat(msg
, ", eject");
3165 deficiency
|= CDC_OPEN_TRAY
;
3166 if (SONY_HWC_LED_SUPPORT(drive_config
))
3167 strcat(msg
, ", LED");
3168 if (SONY_HWC_ELECTRIC_VOLUME(drive_config
))
3169 strcat(msg
, ", elec. Vol");
3170 if (SONY_HWC_ELECTRIC_VOLUME_CTL(drive_config
))
3171 strcat(msg
, ", sep. Vol");
3172 if (is_double_speed
)
3173 strcat(msg
, ", double speed");
3175 deficiency
|= CDC_SELECT_SPEED
;
3176 if (cdu31a_irq
> 0) {
3177 sprintf(buf
, ", irq %d", cdu31a_irq
);
3181 printk(KERN_INFO PFX
"%s",msg
);
3183 cdu31a_queue
= blk_init_queue(do_cdu31a_request
, &cdu31a_lock
);
3186 blk_queue_hardsect_size(cdu31a_queue
, 2048);
3188 init_timer(&cdu31a_abort_timer
);
3189 cdu31a_abort_timer
.function
= handle_abort_timeout
;
3191 scd_info
.mask
= deficiency
;
3193 if (register_cdrom(&scd_info
))
3195 disk
->queue
= cdu31a_queue
;
3202 blk_cleanup_queue(cdu31a_queue
);
3205 free_irq(cdu31a_irq
, NULL
);
3206 printk(KERN_ERR PFX
"Unable to register with Uniform cdrom driver\n");
3209 if (unregister_blkdev(MAJOR_NR
, "cdu31a")) {
3210 printk(KERN_WARNING PFX
"Can't unregister block device\n");
3213 release_region(cdu31a_port
, 4);
3219 void __exit
cdu31a_exit(void)
3221 del_gendisk(scd_gendisk
);
3222 put_disk(scd_gendisk
);
3223 if (unregister_cdrom(&scd_info
)) {
3224 printk(KERN_WARNING PFX
"Can't unregister from Uniform "
3228 if ((unregister_blkdev(MAJOR_NR
, "cdu31a") == -EINVAL
)) {
3229 printk(KERN_WARNING PFX
"Can't unregister\n");
3233 blk_cleanup_queue(cdu31a_queue
);
3236 free_irq(cdu31a_irq
, NULL
);
3238 release_region(cdu31a_port
, 4);
3239 printk(KERN_INFO PFX
"module released.\n");
3243 module_init(cdu31a_init
);
3245 module_exit(cdu31a_exit
);
3247 MODULE_LICENSE("GPL");
3248 MODULE_ALIAS_BLOCKDEV_MAJOR(CDU31A_CDROM_MAJOR
);