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/string.h>
165 #include <linux/slab.h>
166 #include <linux/init.h>
167 #include <linux/interrupt.h>
168 #include <linux/cdrom.h>
170 #include <asm/system.h>
172 #include <asm/uaccess.h>
177 #define MAJOR_NR CDU31A_CDROM_MAJOR
178 #include <linux/blkdev.h>
180 #define CDU31A_MAX_CONSECUTIVE_ATTENTIONS 10
182 #define PFX "CDU31A: "
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
190 unsigned short base
; /* I/O Base Address */
191 short int_num
; /* Interrupt Number (-1 means scan for it,
192 0 means don't use) */
193 } cdu31a_addresses
[] __initdata
= {
197 static int handle_sony_cd_attention(void);
198 static int read_subcode(void);
199 static void sony_get_toc(void);
200 static int scd_spinup(void);
201 /*static int scd_open(struct inode *inode, struct file *filp);*/
202 static int scd_open(struct cdrom_device_info
*, int);
203 static void do_sony_cd_cmd(unsigned char cmd
,
204 unsigned char *params
,
205 unsigned int num_params
,
206 unsigned char *result_buffer
,
207 unsigned int *result_size
);
208 static void size_to_buf(unsigned int size
, unsigned char *buf
);
210 /* Parameters for the read-ahead. */
211 static unsigned int sony_next_block
; /* Next 512 byte block offset */
212 static unsigned int sony_blocks_left
= 0; /* Number of 512 byte blocks left
213 in the current read command. */
216 /* The base I/O address of the Sony Interface. This is a variable (not a
217 #define) so it can be easily changed via some future ioctl() */
218 static unsigned int cdu31a_port
= 0;
219 module_param(cdu31a_port
, uint
, 0);
222 * The following are I/O addresses of the various registers for the drive. The
223 * comment for the base address also applies here.
225 static volatile unsigned short sony_cd_cmd_reg
;
226 static volatile unsigned short sony_cd_param_reg
;
227 static volatile unsigned short sony_cd_write_reg
;
228 static volatile unsigned short sony_cd_control_reg
;
229 static volatile unsigned short sony_cd_status_reg
;
230 static volatile unsigned short sony_cd_result_reg
;
231 static volatile unsigned short sony_cd_read_reg
;
232 static volatile unsigned short sony_cd_fifost_reg
;
234 static struct request_queue
*cdu31a_queue
;
235 static DEFINE_SPINLOCK(cdu31a_lock
); /* queue lock */
237 static int sony_spun_up
= 0; /* Has the drive been spun up? */
239 static int sony_speed
= 0; /* Last wanted speed */
241 static int sony_xa_mode
= 0; /* Is an XA disk in the drive
242 and the drive a CDU31A? */
244 static int sony_raw_data_mode
= 1; /* 1 if data tracks, 0 if audio.
245 For raw data reads. */
247 static unsigned int sony_usage
= 0; /* How many processes have the
250 static int sony_pas_init
= 0; /* Initialize the Pro-Audio
253 static struct s_sony_session_toc single_toc
; /* Holds the
257 static struct s_all_sessions_toc sony_toc
; /* entries gathered from all
260 static int sony_toc_read
= 0; /* Has the TOC been read for
263 static struct s_sony_subcode last_sony_subcode
; /* Points to the last
264 subcode address read */
266 static DECLARE_MUTEX(sony_sem
); /* Semaphore for drive hardware access */
268 static int is_double_speed
= 0; /* does the drive support double speed ? */
270 static int is_auto_eject
= 1; /* Door has been locked? 1=No/0=Yes */
273 * The audio status uses the values from read subchannel data as specified
274 * in include/linux/cdrom.h.
276 static volatile int sony_audio_status
= CDROM_AUDIO_NO_STATUS
;
279 * The following are a hack for pausing and resuming audio play. The drive
280 * does not work as I would expect it, if you stop it then start it again,
281 * the drive seeks back to the beginning and starts over. This holds the
282 * position during a pause so a resume can restart it. It uses the
283 * audio status variable above to tell if it is paused.
285 static unsigned volatile char cur_pos_msf
[3] = { 0, 0, 0 };
286 static unsigned volatile char final_pos_msf
[3] = { 0, 0, 0 };
288 /* What IRQ is the drive using? 0 if none. */
289 static int cdu31a_irq
= 0;
290 module_param(cdu31a_irq
, int, 0);
292 /* The interrupt handler will wake this queue up when it gets an
294 static DECLARE_WAIT_QUEUE_HEAD(cdu31a_irq_wait
);
295 static int irq_flag
= 0;
297 static int curr_control_reg
= 0; /* Current value of the control register */
299 /* A disk changed variable. When a disk change is detected, it will
300 all be set to TRUE. As the upper layers ask for disk_changed status
301 it will be cleared. */
302 static char disk_changed
;
304 /* This was readahead_buffer once... Now it's used only for audio reads */
305 static char audio_buffer
[CD_FRAMESIZE_RAW
];
307 /* Used to time a short period to abort an operation after the
308 drive has been idle for a while. This keeps the light on
309 the drive from flashing for very long. */
310 static struct timer_list cdu31a_abort_timer
;
312 /* Marks if the timeout has started an abort read. This is used
313 on entry to the drive to tell the code to read out the status
314 from the abort read. */
315 static int abort_read_started
= 0;
318 * Uniform cdrom interface function
319 * report back, if disc has changed from time of last request.
321 static int scd_media_changed(struct cdrom_device_info
*cdi
, int disc_nr
)
325 retval
= disk_changed
;
332 * Uniform cdrom interface function
333 * report back, if drive is ready
335 static int scd_drive_status(struct cdrom_device_info
*cdi
, int slot_nr
)
337 if (CDSL_CURRENT
!= slot_nr
)
338 /* we have no changer support */
342 if (down_interruptible(&sony_sem
))
344 if (scd_spinup() == 0)
347 return sony_spun_up
? CDS_DISC_OK
: CDS_DRIVE_NOT_READY
;
350 static inline void enable_interrupts(void)
352 curr_control_reg
|= (SONY_ATTN_INT_EN_BIT
353 | SONY_RES_RDY_INT_EN_BIT
354 | SONY_DATA_RDY_INT_EN_BIT
);
355 outb(curr_control_reg
, sony_cd_control_reg
);
358 static inline void disable_interrupts(void)
360 curr_control_reg
&= ~(SONY_ATTN_INT_EN_BIT
361 | SONY_RES_RDY_INT_EN_BIT
362 | SONY_DATA_RDY_INT_EN_BIT
);
363 outb(curr_control_reg
, sony_cd_control_reg
);
367 * Wait a little while (used for polling the drive). If in initialization,
368 * setting a timeout doesn't work, so just loop for a while.
370 static inline void sony_sleep(void)
372 if (cdu31a_irq
<= 0) {
374 } else { /* Interrupt driven */
379 prepare_to_wait(&cdu31a_irq_wait
, &w
,
388 if (!signal_pending(current
)) {
392 disable_interrupts();
395 finish_wait(&cdu31a_irq_wait
, &w
);
402 * The following are convenience routine to read various status and set
403 * various conditions in the drive.
405 static inline int is_attention(void)
407 return (inb(sony_cd_status_reg
) & SONY_ATTN_BIT
) != 0;
410 static inline int is_busy(void)
412 return (inb(sony_cd_status_reg
) & SONY_BUSY_BIT
) != 0;
415 static inline int is_data_ready(void)
417 return (inb(sony_cd_status_reg
) & SONY_DATA_RDY_BIT
) != 0;
420 static inline int is_data_requested(void)
422 return (inb(sony_cd_status_reg
) & SONY_DATA_REQUEST_BIT
) != 0;
425 static inline int is_result_ready(void)
427 return (inb(sony_cd_status_reg
) & SONY_RES_RDY_BIT
) != 0;
430 static inline int is_param_write_rdy(void)
432 return (inb(sony_cd_fifost_reg
) & SONY_PARAM_WRITE_RDY_BIT
) != 0;
435 static inline int is_result_reg_not_empty(void)
437 return (inb(sony_cd_fifost_reg
) & SONY_RES_REG_NOT_EMP_BIT
) != 0;
440 static inline void reset_drive(void)
442 curr_control_reg
= 0;
444 outb(SONY_DRIVE_RESET_BIT
, sony_cd_control_reg
);
448 * Uniform cdrom interface function
449 * reset drive and return when it is ready
451 static int scd_reset(struct cdrom_device_info
*cdi
)
453 unsigned long retry_count
;
455 if (down_interruptible(&sony_sem
))
459 retry_count
= jiffies
+ SONY_RESET_TIMEOUT
;
460 while (time_before(jiffies
, retry_count
) && (!is_attention())) {
468 static inline void clear_attention(void)
470 outb(curr_control_reg
| SONY_ATTN_CLR_BIT
, sony_cd_control_reg
);
473 static inline void clear_result_ready(void)
475 outb(curr_control_reg
| SONY_RES_RDY_CLR_BIT
, sony_cd_control_reg
);
478 static inline void clear_data_ready(void)
480 outb(curr_control_reg
| SONY_DATA_RDY_CLR_BIT
,
481 sony_cd_control_reg
);
484 static inline void clear_param_reg(void)
486 outb(curr_control_reg
| SONY_PARAM_CLR_BIT
, sony_cd_control_reg
);
489 static inline unsigned char read_status_register(void)
491 return inb(sony_cd_status_reg
);
494 static inline unsigned char read_result_register(void)
496 return inb(sony_cd_result_reg
);
499 static inline unsigned char read_data_register(void)
501 return inb(sony_cd_read_reg
);
504 static inline void write_param(unsigned char param
)
506 outb(param
, sony_cd_param_reg
);
509 static inline void write_cmd(unsigned char cmd
)
511 outb(curr_control_reg
| SONY_RES_RDY_INT_EN_BIT
,
512 sony_cd_control_reg
);
513 outb(cmd
, sony_cd_cmd_reg
);
516 static irqreturn_t
cdu31a_interrupt(int irq
, void *dev_id
, struct pt_regs
*regs
)
520 if (abort_read_started
) {
521 /* We might be waiting for an abort to finish. Don't
522 disable interrupts yet, though, because we handle
524 /* Clear out the result registers. */
525 while (is_result_reg_not_empty()) {
526 val
= read_result_register();
529 clear_result_ready();
531 /* Clear out the data */
532 while (is_data_requested()) {
533 val
= read_data_register();
535 abort_read_started
= 0;
537 /* If something was waiting, wake it up now. */
538 if (waitqueue_active(&cdu31a_irq_wait
)) {
539 disable_interrupts();
541 wake_up_interruptible(&cdu31a_irq_wait
);
543 } else if (waitqueue_active(&cdu31a_irq_wait
)) {
544 disable_interrupts();
546 wake_up_interruptible(&cdu31a_irq_wait
);
548 disable_interrupts();
549 printk(KERN_NOTICE PFX
550 "Got an interrupt but nothing was waiting\n");
556 * give more verbose error messages
558 static unsigned char *translate_error(unsigned char err_code
)
560 static unsigned char errbuf
[80];
563 case 0x10: return "illegal command ";
564 case 0x11: return "illegal parameter ";
566 case 0x20: return "not loaded ";
567 case 0x21: return "no disc ";
568 case 0x22: return "not spinning ";
569 case 0x23: return "spinning ";
570 case 0x25: return "spindle servo ";
571 case 0x26: return "focus servo ";
572 case 0x29: return "eject mechanism ";
573 case 0x2a: return "audio playing ";
574 case 0x2c: return "emergency eject ";
576 case 0x30: return "focus ";
577 case 0x31: return "frame sync ";
578 case 0x32: return "subcode address ";
579 case 0x33: return "block sync ";
580 case 0x34: return "header address ";
582 case 0x40: return "illegal track read ";
583 case 0x41: return "mode 0 read ";
584 case 0x42: return "illegal mode read ";
585 case 0x43: return "illegal block size read ";
586 case 0x44: return "mode read ";
587 case 0x45: return "form read ";
588 case 0x46: return "leadout read ";
589 case 0x47: return "buffer overrun ";
591 case 0x53: return "unrecoverable CIRC ";
592 case 0x57: return "unrecoverable LECC ";
594 case 0x60: return "no TOC ";
595 case 0x61: return "invalid subcode data ";
596 case 0x63: return "focus on TOC read ";
597 case 0x64: return "frame sync on TOC read ";
598 case 0x65: return "TOC data ";
600 case 0x70: return "hardware failure ";
601 case 0x91: return "leadin ";
602 case 0x92: return "leadout ";
603 case 0x93: return "data track ";
605 sprintf(errbuf
, "unknown 0x%02x ", err_code
);
610 * Set the drive parameters so the drive will auto-spin-up when a
613 static void set_drive_params(int want_doublespeed
)
615 unsigned char res_reg
[12];
616 unsigned int res_size
;
617 unsigned char params
[3];
620 params
[0] = SONY_SD_AUTO_SPIN_DOWN_TIME
;
621 params
[1] = 0x00; /* Never spin down the drive. */
622 do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD
,
623 params
, 2, res_reg
, &res_size
);
624 if ((res_size
< 2) || ((res_reg
[0] & 0xf0) == 0x20)) {
625 printk(KERN_NOTICE PFX
626 "Unable to set spin-down time: 0x%2.2x\n", res_reg
[1]);
629 params
[0] = SONY_SD_MECH_CONTROL
;
630 params
[1] = SONY_AUTO_SPIN_UP_BIT
; /* Set auto spin up */
633 params
[1] |= SONY_AUTO_EJECT_BIT
;
635 if (is_double_speed
&& want_doublespeed
) {
636 params
[1] |= SONY_DOUBLE_SPEED_BIT
; /* Set the drive to double speed if
639 do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD
,
640 params
, 2, res_reg
, &res_size
);
641 if ((res_size
< 2) || ((res_reg
[0] & 0xf0) == 0x20)) {
642 printk(KERN_NOTICE PFX
"Unable to set mechanical "
643 "parameters: 0x%2.2x\n", res_reg
[1]);
648 * Uniform cdrom interface function
649 * select reading speed for data access
651 static int scd_select_speed(struct cdrom_device_info
*cdi
, int speed
)
656 sony_speed
= speed
- 1;
658 if (down_interruptible(&sony_sem
))
660 set_drive_params(sony_speed
);
666 * Uniform cdrom interface function
667 * lock or unlock eject button
669 static int scd_lock_door(struct cdrom_device_info
*cdi
, int lock
)
676 if (down_interruptible(&sony_sem
))
678 set_drive_params(sony_speed
);
684 * This code will reset the drive and attempt to restore sane parameters.
686 static void restart_on_error(void)
688 unsigned char res_reg
[12];
689 unsigned int res_size
;
690 unsigned long retry_count
;
693 printk(KERN_NOTICE PFX
"Resetting drive on error\n");
695 retry_count
= jiffies
+ SONY_RESET_TIMEOUT
;
696 while (time_before(jiffies
, retry_count
) && (!is_attention())) {
699 set_drive_params(sony_speed
);
700 do_sony_cd_cmd(SONY_SPIN_UP_CMD
, NULL
, 0, res_reg
, &res_size
);
701 if ((res_size
< 2) || ((res_reg
[0] & 0xf0) == 0x20)) {
702 printk(KERN_NOTICE PFX
"Unable to spin up drive: 0x%2.2x\n",
712 * This routine writes data to the parameter register. Since this should
713 * happen fairly fast, it is polled with no OS waits between.
715 static int write_params(unsigned char *params
, int num_params
)
717 unsigned int retry_count
;
720 retry_count
= SONY_READY_RETRIES
;
721 while ((retry_count
> 0) && (!is_param_write_rdy())) {
724 if (!is_param_write_rdy()) {
728 while (num_params
> 0) {
729 write_param(*params
);
739 * The following reads data from the command result register. It is a
740 * fairly complex routine, all status info flows back through this
741 * interface. The algorithm is stolen directly from the flowcharts in
745 get_result(unsigned char *result_buffer
, unsigned int *result_size
)
749 unsigned long retry_count
;
752 while (handle_sony_cd_attention());
753 /* Wait for the result data to be ready */
754 retry_count
= jiffies
+ SONY_JIFFIES_TIMEOUT
;
755 while (time_before(jiffies
, retry_count
)
756 && (is_busy() || (!(is_result_ready())))) {
759 while (handle_sony_cd_attention());
761 if (is_busy() || (!(is_result_ready()))) {
762 pr_debug(PFX
"timeout out %d\n", __LINE__
);
763 result_buffer
[0] = 0x20;
764 result_buffer
[1] = SONY_TIMEOUT_OP_ERR
;
770 * Get the first two bytes. This determines what else needs
773 clear_result_ready();
774 a
= read_result_register();
778 /* Check for block error status result. */
779 if ((a
& 0xf0) == 0x50) {
784 b
= read_result_register();
790 * 0x20 means an error occurred. Byte 2 will have the error code.
791 * Otherwise, the command succeeded, byte 2 will have the count of
792 * how many more status bytes are coming.
794 * The result register can be read 10 bytes at a time, a wait for
795 * result ready to be asserted must be done between every 10 bytes.
797 if ((a
& 0xf0) != 0x20) {
799 for (i
= 0; i
< 8; i
++) {
800 *result_buffer
= read_result_register();
807 retry_count
= SONY_READY_RETRIES
;
808 while ((retry_count
> 0)
809 && (!is_result_ready())) {
812 if (!is_result_ready()) {
813 pr_debug(PFX
"timeout out %d\n",
815 result_buffer
[0] = 0x20;
822 clear_result_ready();
824 for (i
= 0; i
< 10; i
++) {
826 read_result_register();
834 retry_count
= SONY_READY_RETRIES
;
835 while ((retry_count
> 0)
836 && (!is_result_ready())) {
839 if (!is_result_ready()) {
840 pr_debug(PFX
"timeout out %d\n",
842 result_buffer
[0] = 0x20;
852 *result_buffer
= read_result_register();
861 * Do a command that does not involve data transfer. This routine must
862 * be re-entrant from the same task to support being called from the
863 * data operation code when an error occurs.
866 do_sony_cd_cmd(unsigned char cmd
,
867 unsigned char *params
,
868 unsigned int num_params
,
869 unsigned char *result_buffer
, unsigned int *result_size
)
871 unsigned long retry_count
;
876 while (handle_sony_cd_attention());
878 retry_count
= jiffies
+ SONY_JIFFIES_TIMEOUT
;
879 while (time_before(jiffies
, retry_count
) && (is_busy())) {
882 while (handle_sony_cd_attention());
885 pr_debug(PFX
"timeout out %d\n", __LINE__
);
886 result_buffer
[0] = 0x20;
887 result_buffer
[1] = SONY_TIMEOUT_OP_ERR
;
890 clear_result_ready();
893 write_params(params
, num_params
);
896 get_result(result_buffer
, result_size
);
899 if (((result_buffer
[0] & 0xf0) == 0x20)
900 && (num_retries
< MAX_CDU31A_RETRIES
)) {
903 goto retry_cd_operation
;
909 * Handle an attention from the drive. This will return 1 if it found one
910 * or 0 if not (if one is found, the caller might want to call again).
912 * This routine counts the number of consecutive times it is called
913 * (since this is always called from a while loop until it returns
914 * a 0), and returns a 0 if it happens too many times. This will help
917 static int handle_sony_cd_attention(void)
919 unsigned char atten_code
;
920 static int num_consecutive_attentions
= 0;
925 pr_debug(PFX
"Entering %s\n", __FUNCTION__
);
927 if (is_attention()) {
928 if (num_consecutive_attentions
>
929 CDU31A_MAX_CONSECUTIVE_ATTENTIONS
) {
930 printk(KERN_NOTICE PFX
"Too many consecutive "
931 "attentions: %d\n", num_consecutive_attentions
);
932 num_consecutive_attentions
= 0;
933 pr_debug(PFX
"Leaving %s at %d\n", __FUNCTION__
,
939 atten_code
= read_result_register();
941 switch (atten_code
) {
942 /* Someone changed the CD. Mark it as changed */
943 case SONY_MECH_LOADED_ATTN
:
946 sony_audio_status
= CDROM_AUDIO_NO_STATUS
;
947 sony_blocks_left
= 0;
950 case SONY_SPIN_DOWN_COMPLETE_ATTN
:
951 /* Mark the disk as spun down. */
955 case SONY_AUDIO_PLAY_DONE_ATTN
:
956 sony_audio_status
= CDROM_AUDIO_COMPLETED
;
960 case SONY_EJECT_PUSHED_ATTN
:
962 sony_audio_status
= CDROM_AUDIO_INVALID
;
966 case SONY_LEAD_IN_ERR_ATTN
:
967 case SONY_LEAD_OUT_ERR_ATTN
:
968 case SONY_DATA_TRACK_ERR_ATTN
:
969 case SONY_AUDIO_PLAYBACK_ERR_ATTN
:
970 sony_audio_status
= CDROM_AUDIO_ERROR
;
974 num_consecutive_attentions
++;
975 pr_debug(PFX
"Leaving %s at %d\n", __FUNCTION__
, __LINE__
);
977 } else if (abort_read_started
) {
978 while (is_result_reg_not_empty()) {
979 val
= read_result_register();
982 clear_result_ready();
983 /* Clear out the data */
984 while (is_data_requested()) {
985 val
= read_data_register();
987 abort_read_started
= 0;
988 pr_debug(PFX
"Leaving %s at %d\n", __FUNCTION__
, __LINE__
);
992 num_consecutive_attentions
= 0;
994 pr_debug(PFX
"Leaving %s at %d\n", __FUNCTION__
, __LINE__
);
1000 /* Convert from an integer 0-99 to BCD */
1001 static inline unsigned int int_to_bcd(unsigned int val
)
1006 retval
= (val
/ 10) << 4;
1007 retval
= retval
| val
% 10;
1012 /* Convert from BCD to an integer from 0-99 */
1013 static unsigned int bcd_to_int(unsigned int bcd
)
1015 return (((bcd
>> 4) & 0x0f) * 10) + (bcd
& 0x0f);
1020 * Convert a logical sector value (like the OS would want to use for
1021 * a block device) to an MSF format.
1023 static void log_to_msf(unsigned int log
, unsigned char *msf
)
1025 log
= log
+ LOG_START_OFFSET
;
1026 msf
[0] = int_to_bcd(log
/ 4500);
1028 msf
[1] = int_to_bcd(log
/ 75);
1029 msf
[2] = int_to_bcd(log
% 75);
1034 * Convert an MSF format to a logical sector.
1036 static unsigned int msf_to_log(unsigned char *msf
)
1043 log
+= msf
[0] * 4500;
1044 log
= log
- LOG_START_OFFSET
;
1051 * Take in integer size value and put it into a buffer like
1052 * the drive would want to see a number-of-sector value.
1054 static void size_to_buf(unsigned int size
, unsigned char *buf
)
1056 buf
[0] = size
/ 65536;
1057 size
= size
% 65536;
1058 buf
[1] = size
/ 256;
1059 buf
[2] = size
% 256;
1062 /* Starts a read operation. Returns 0 on success and 1 on failure.
1063 The read operation used here allows multiple sequential sectors
1064 to be read and status returned for each sector. The driver will
1065 read the output one at a time as the requests come and abort the
1066 operation if the requested sector is not the next one from the
1069 start_request(unsigned int sector
, unsigned int nsect
)
1071 unsigned char params
[6];
1072 unsigned long retry_count
;
1075 pr_debug(PFX
"Entering %s\n", __FUNCTION__
);
1076 log_to_msf(sector
, params
);
1077 size_to_buf(nsect
, ¶ms
[3]);
1080 * Clear any outstanding attentions and wait for the drive to
1081 * complete any pending operations.
1083 while (handle_sony_cd_attention());
1085 retry_count
= jiffies
+ SONY_JIFFIES_TIMEOUT
;
1086 while (time_before(jiffies
, retry_count
) && (is_busy())) {
1089 while (handle_sony_cd_attention());
1093 printk(KERN_NOTICE PFX
"Timeout while waiting "
1094 "to issue command\n");
1095 pr_debug(PFX
"Leaving %s at %d\n", __FUNCTION__
, __LINE__
);
1098 /* Issue the command */
1099 clear_result_ready();
1102 write_params(params
, 6);
1103 write_cmd(SONY_READ_BLKERR_STAT_CMD
);
1105 sony_blocks_left
= nsect
* 4;
1106 sony_next_block
= sector
* 4;
1107 pr_debug(PFX
"Leaving %s at %d\n", __FUNCTION__
, __LINE__
);
1110 pr_debug(PFX
"Leaving %s at %d\n", __FUNCTION__
, __LINE__
);
1113 /* Abort a pending read operation. Clear all the drive status variables. */
1114 static void abort_read(void)
1116 unsigned char result_reg
[2];
1121 do_sony_cd_cmd(SONY_ABORT_CMD
, NULL
, 0, result_reg
, &result_size
);
1122 if ((result_reg
[0] & 0xf0) == 0x20) {
1123 printk(KERN_ERR PFX
"Aborting read, %s error\n",
1124 translate_error(result_reg
[1]));
1127 while (is_result_reg_not_empty()) {
1128 val
= read_result_register();
1131 clear_result_ready();
1132 /* Clear out the data */
1133 while (is_data_requested()) {
1134 val
= read_data_register();
1137 sony_blocks_left
= 0;
1140 /* Called when the timer times out. This will abort the
1141 pending read operation. */
1142 static void handle_abort_timeout(unsigned long data
)
1144 pr_debug(PFX
"Entering %s\n", __FUNCTION__
);
1145 /* If it is in use, ignore it. */
1146 if (down_trylock(&sony_sem
) == 0) {
1147 /* We can't use abort_read(), because it will sleep
1148 or schedule in the timer interrupt. Just start
1149 the operation, finish it on the next access to
1151 clear_result_ready();
1153 write_cmd(SONY_ABORT_CMD
);
1155 sony_blocks_left
= 0;
1156 abort_read_started
= 1;
1159 pr_debug(PFX
"Leaving %s\n", __FUNCTION__
);
1162 /* Actually get one sector of data from the drive. */
1164 input_data_sector(char *buffer
)
1166 pr_debug(PFX
"Entering %s\n", __FUNCTION__
);
1168 /* If an XA disk on a CDU31A, skip the first 12 bytes of data from
1169 the disk. The real data is after that. We can use audio_buffer. */
1171 insb(sony_cd_read_reg
, audio_buffer
, CD_XA_HEAD
);
1175 insb(sony_cd_read_reg
, buffer
, 2048);
1177 /* If an XA disk, we have to clear out the rest of the unused
1178 error correction data. We can use audio_buffer for that. */
1180 insb(sony_cd_read_reg
, audio_buffer
, CD_XA_TAIL
);
1182 pr_debug(PFX
"Leaving %s\n", __FUNCTION__
);
1185 /* read data from the drive. Note the nsect must be <= 4. */
1187 read_data_block(char *buffer
,
1189 unsigned int nblocks
,
1190 unsigned char res_reg
[], int *res_size
)
1192 unsigned long retry_count
;
1194 pr_debug(PFX
"Entering %s\n", __FUNCTION__
);
1200 /* Wait for the drive to tell us we have something */
1201 retry_count
= jiffies
+ SONY_JIFFIES_TIMEOUT
;
1202 while (time_before(jiffies
, retry_count
) && !(is_data_ready())) {
1203 while (handle_sony_cd_attention());
1207 if (!(is_data_ready())) {
1208 if (is_result_ready()) {
1209 get_result(res_reg
, res_size
);
1210 if ((res_reg
[0] & 0xf0) != 0x20) {
1211 printk(KERN_NOTICE PFX
"Got result that should"
1212 " have been error: %d\n", res_reg
[0]);
1214 res_reg
[1] = SONY_BAD_DATA_ERR
;
1219 pr_debug(PFX
"timeout out %d\n", __LINE__
);
1221 res_reg
[1] = SONY_TIMEOUT_OP_ERR
;
1226 input_data_sector(buffer
);
1227 sony_blocks_left
-= nblocks
;
1228 sony_next_block
+= nblocks
;
1230 /* Wait for the status from the drive. */
1231 retry_count
= jiffies
+ SONY_JIFFIES_TIMEOUT
;
1232 while (time_before(jiffies
, retry_count
)
1233 && !(is_result_ready())) {
1234 while (handle_sony_cd_attention());
1239 if (!is_result_ready()) {
1240 pr_debug(PFX
"timeout out %d\n", __LINE__
);
1242 res_reg
[1] = SONY_TIMEOUT_OP_ERR
;
1246 get_result(res_reg
, res_size
);
1248 /* If we got a buffer status, handle that. */
1249 if ((res_reg
[0] & 0xf0) == 0x50) {
1252 SONY_NO_CIRC_ERR_BLK_STAT
)
1254 SONY_NO_LECC_ERR_BLK_STAT
)
1256 SONY_RECOV_LECC_ERR_BLK_STAT
)) {
1259 printk(KERN_ERR PFX
"Data block "
1260 "error: 0x%x\n", res_reg
[0]);
1262 res_reg
[1] = SONY_BAD_DATA_ERR
;
1266 /* Final transfer is done for read command, get final result. */
1267 if (sony_blocks_left
== 0) {
1268 get_result(res_reg
, res_size
);
1270 } else if ((res_reg
[0] & 0xf0) != 0x20) {
1271 /* The drive gave me bad status, I don't know what to do.
1272 Reset the driver and return an error. */
1273 printk(KERN_ERR PFX
"Invalid block "
1274 "status: 0x%x\n", res_reg
[0]);
1277 res_reg
[1] = SONY_BAD_DATA_ERR
;
1282 pr_debug(PFX
"Leaving %s at %d\n", __FUNCTION__
, __LINE__
);
1287 * The OS calls this to perform a read or write operation to the drive.
1288 * Write obviously fail. Reads to a read ahead of sony_buffer_size
1289 * bytes to help speed operations. This especially helps since the OS
1290 * uses 1024 byte blocks and the drive uses 2048 byte blocks. Since most
1291 * data access on a CD is done sequentially, this saves a lot of operations.
1293 static void do_cdu31a_request(request_queue_t
* q
)
1295 struct request
*req
;
1296 int block
, nblock
, num_retries
;
1297 unsigned char res_reg
[12];
1298 unsigned int res_size
;
1300 pr_debug(PFX
"Entering %s\n", __FUNCTION__
);
1302 spin_unlock_irq(q
->queue_lock
);
1303 if (down_interruptible(&sony_sem
)) {
1304 spin_lock_irq(q
->queue_lock
);
1308 /* Get drive status before doing anything. */
1309 while (handle_sony_cd_attention());
1311 /* Make sure we have a valid TOC. */
1315 /* Make sure the timer is cancelled. */
1316 del_timer(&cdu31a_abort_timer
);
1320 * The beginning here is stolen from the hard disk driver. I hope
1323 req
= elv_next_request(q
);
1325 goto end_do_cdu31a_request
;
1330 block
= req
->sector
;
1331 nblock
= req
->nr_sectors
;
1332 pr_debug(PFX
"request at block %d, length %d blocks\n",
1334 if (!sony_toc_read
) {
1335 printk(KERN_NOTICE PFX
"TOC not read\n");
1336 end_request(req
, 0);
1341 if (!(req
->flags
& REQ_CMD
))
1343 if (rq_data_dir(req
) == WRITE
) {
1344 end_request(req
, 0);
1349 * If the block address is invalid or the request goes beyond the end of
1350 * the media, return an error.
1352 if (((block
+ nblock
) / 4) >= sony_toc
.lead_out_start_lba
) {
1353 printk(KERN_NOTICE PFX
"Request past end of media\n");
1354 end_request(req
, 0);
1363 while (handle_sony_cd_attention());
1365 if (!sony_toc_read
) {
1366 printk(KERN_NOTICE PFX
"TOC not read\n");
1367 end_request(req
, 0);
1371 /* If no data is left to be read from the drive, start the
1373 if (sony_blocks_left
== 0) {
1374 if (start_request(block
/ 4, nblock
/ 4)) {
1375 end_request(req
, 0);
1379 /* If the requested block is not the next one waiting in
1380 the driver, abort the current operation and start a
1382 else if (block
!= sony_next_block
) {
1383 pr_debug(PFX
"Read for block %d, expected %d\n",
1384 block
, sony_next_block
);
1386 if (!sony_toc_read
) {
1387 printk(KERN_NOTICE PFX
"TOC not read\n");
1388 end_request(req
, 0);
1391 if (start_request(block
/ 4, nblock
/ 4)) {
1392 printk(KERN_NOTICE PFX
"start request failed\n");
1393 end_request(req
, 0);
1398 read_data_block(req
->buffer
, block
, nblock
, res_reg
, &res_size
);
1400 if (res_reg
[0] != 0x20) {
1401 if (!end_that_request_first(req
, 1, nblock
)) {
1402 spin_lock_irq(q
->queue_lock
);
1403 blkdev_dequeue_request(req
);
1404 end_that_request_last(req
, 1);
1405 spin_unlock_irq(q
->queue_lock
);
1410 if (num_retries
> MAX_CDU31A_RETRIES
) {
1411 end_request(req
, 0);
1416 if (res_reg
[1] == SONY_NOT_SPIN_ERR
) {
1417 do_sony_cd_cmd(SONY_SPIN_UP_CMD
, NULL
, 0, res_reg
,
1420 printk(KERN_NOTICE PFX
"%s error for block %d, nblock %d\n",
1421 translate_error(res_reg
[1]), block
, nblock
);
1423 goto try_read_again
;
1425 end_do_cdu31a_request
:
1427 /* After finished, cancel any pending operations. */
1430 /* Start a timer to time out after a while to disable
1432 cdu31a_abort_timer
.expires
= jiffies
+ 2 * HZ
; /* Wait 2 seconds */
1433 add_timer(&cdu31a_abort_timer
);
1437 spin_lock_irq(q
->queue_lock
);
1438 pr_debug(PFX
"Leaving %s at %d\n", __FUNCTION__
, __LINE__
);
1443 * Read the table of contents from the drive and set up TOC if
1446 static void sony_get_toc(void)
1448 unsigned char res_reg
[2];
1449 unsigned int res_size
;
1450 unsigned char parms
[1];
1453 int totaltracks
= 0;
1457 pr_debug(PFX
"Entering %s\n", __FUNCTION__
);
1460 if (!sony_toc_read
) {
1462 /* Ignore the result, since it might error if spinning already. */
1463 do_sony_cd_cmd(SONY_SPIN_UP_CMD
, NULL
, 0, res_reg
,
1466 do_sony_cd_cmd(SONY_READ_TOC_CMD
, NULL
, 0, res_reg
,
1469 /* The drive sometimes returns error 0. I don't know why, but ignore
1470 it. It seems to mean the drive has already done the operation. */
1472 || ((res_reg
[0] != 0) && (res_reg
[1] != 0))) {
1473 /* If the drive is already playing, it's ok. */
1474 if ((res_reg
[1] == SONY_AUDIO_PLAYING_ERR
)
1475 || (res_reg
[1] == 0)) {
1476 goto gettoc_drive_spinning
;
1479 /* If the drive says it is not spun up (even though we just did it!)
1480 then retry the operation at least a few times. */
1481 if ((res_reg
[1] == SONY_NOT_SPIN_ERR
)
1482 && (num_spin_ups
< MAX_CDU31A_RETRIES
)) {
1484 goto respinup_on_gettoc
;
1487 printk("cdu31a: Error reading TOC: %x %s\n",
1488 res_reg
[0], translate_error(res_reg
[1]));
1492 gettoc_drive_spinning
:
1494 /* The idea here is we keep asking for sessions until the command
1495 fails. Then we know what the last valid session on the disk is.
1496 No need to check session 0, since session 0 is the same as session
1497 1; the command returns different information if you give it 0.
1500 memset(&sony_toc
, 0x0e, sizeof(sony_toc
));
1501 memset(&single_toc
, 0x0f, sizeof(single_toc
));
1505 /* This seems to slow things down enough to make it work. This
1506 * appears to be a problem in do_sony_cd_cmd. This printk seems
1507 * to address the symptoms... -Erik */
1508 pr_debug(PFX
"Trying session %d\n", session
);
1510 do_sony_cd_cmd(SONY_READ_TOC_SPEC_CMD
,
1511 parms
, 1, res_reg
, &res_size
);
1513 pr_debug(PFX
"%2.2x %2.2x\n", res_reg
[0], res_reg
[1]);
1516 || ((res_reg
[0] & 0xf0) == 0x20)) {
1517 /* An error reading the TOC, this must be past the last session. */
1520 ("Yikes! Couldn't read any sessions!");
1523 pr_debug(PFX
"Reading session %d\n", session
);
1526 do_sony_cd_cmd(SONY_REQ_TOC_DATA_SPEC_CMD
,
1529 (unsigned char *) &single_toc
,
1532 || ((single_toc
.exec_status
[0] & 0xf0) ==
1534 printk(KERN_ERR PFX
"Error reading "
1535 "session %d: %x %s\n",
1536 session
, single_toc
.exec_status
[0],
1537 translate_error(single_toc
.
1539 /* An error reading the TOC. Return without sony_toc_read
1543 pr_debug(PFX
"add0 %01x, con0 %01x, poi0 %02x, "
1544 "1st trk %d, dsktyp %x, dum0 %x\n",
1545 single_toc
.address0
, single_toc
.control0
,
1547 bcd_to_int(single_toc
.first_track_num
),
1548 single_toc
.disk_type
, single_toc
.dummy0
);
1549 pr_debug(PFX
"add1 %01x, con1 %01x, poi1 %02x, "
1550 "lst trk %d, dummy1 %x, dum2 %x\n",
1551 single_toc
.address1
, single_toc
.control1
,
1553 bcd_to_int(single_toc
.last_track_num
),
1554 single_toc
.dummy1
, single_toc
.dummy2
);
1555 pr_debug(PFX
"add2 %01x, con2 %01x, poi2 %02x "
1556 "leadout start min %d, sec %d, frame %d\n",
1557 single_toc
.address2
, single_toc
.control2
,
1559 bcd_to_int(single_toc
.lead_out_start_msf
[0]),
1560 bcd_to_int(single_toc
.lead_out_start_msf
[1]),
1561 bcd_to_int(single_toc
.lead_out_start_msf
[2]));
1562 if (res_size
> 18 && single_toc
.pointb0
> 0xaf)
1563 pr_debug(PFX
"addb0 %01x, conb0 %01x, poib0 %02x, nextsession min %d, sec %d, frame %d\n"
1564 "#mode5_ptrs %02d, max_start_outer_leadout_msf min %d, sec %d, frame %d\n",
1565 single_toc
.addressb0
,
1566 single_toc
.controlb0
,
1568 bcd_to_int(single_toc
.
1569 next_poss_prog_area_msf
1571 bcd_to_int(single_toc
.
1572 next_poss_prog_area_msf
1574 bcd_to_int(single_toc
.
1575 next_poss_prog_area_msf
1577 single_toc
.num_mode_5_pointers
,
1578 bcd_to_int(single_toc
.
1579 max_start_outer_leadout_msf
1581 bcd_to_int(single_toc
.
1582 max_start_outer_leadout_msf
1584 bcd_to_int(single_toc
.
1585 max_start_outer_leadout_msf
1587 if (res_size
> 27 && single_toc
.pointb1
> 0xaf)
1588 pr_debug(PFX
"addb1 %01x, conb1 %01x, poib1 %02x, %x %x %x %x #skipint_ptrs %d, #skiptrkassign %d %x\n",
1589 single_toc
.addressb1
,
1590 single_toc
.controlb1
,
1592 single_toc
.dummyb0_1
[0],
1593 single_toc
.dummyb0_1
[1],
1594 single_toc
.dummyb0_1
[2],
1595 single_toc
.dummyb0_1
[3],
1596 single_toc
.num_skip_interval_pointers
,
1597 single_toc
.num_skip_track_assignments
,
1598 single_toc
.dummyb0_2
);
1599 if (res_size
> 36 && single_toc
.pointb2
> 0xaf)
1600 pr_debug(PFX
"addb2 %01x, conb2 %01x, poib2 %02x, %02x %02x %02x %02x %02x %02x %02x\n",
1601 single_toc
.addressb2
,
1602 single_toc
.controlb2
,
1604 single_toc
.tracksb2
[0],
1605 single_toc
.tracksb2
[1],
1606 single_toc
.tracksb2
[2],
1607 single_toc
.tracksb2
[3],
1608 single_toc
.tracksb2
[4],
1609 single_toc
.tracksb2
[5],
1610 single_toc
.tracksb2
[6]);
1611 if (res_size
> 45 && single_toc
.pointb3
> 0xaf)
1612 pr_debug(PFX
"addb3 %01x, conb3 %01x, poib3 %02x, %02x %02x %02x %02x %02x %02x %02x\n",
1613 single_toc
.addressb3
,
1614 single_toc
.controlb3
,
1616 single_toc
.tracksb3
[0],
1617 single_toc
.tracksb3
[1],
1618 single_toc
.tracksb3
[2],
1619 single_toc
.tracksb3
[3],
1620 single_toc
.tracksb3
[4],
1621 single_toc
.tracksb3
[5],
1622 single_toc
.tracksb3
[6]);
1623 if (res_size
> 54 && single_toc
.pointb4
> 0xaf)
1624 pr_debug(PFX
"addb4 %01x, conb4 %01x, poib4 %02x, %02x %02x %02x %02x %02x %02x %02x\n",
1625 single_toc
.addressb4
,
1626 single_toc
.controlb4
,
1628 single_toc
.tracksb4
[0],
1629 single_toc
.tracksb4
[1],
1630 single_toc
.tracksb4
[2],
1631 single_toc
.tracksb4
[3],
1632 single_toc
.tracksb4
[4],
1633 single_toc
.tracksb4
[5],
1634 single_toc
.tracksb4
[6]);
1635 if (res_size
> 63 && single_toc
.pointc0
> 0xaf)
1636 pr_debug(PFX
"addc0 %01x, conc0 %01x, poic0 %02x, %02x %02x %02x %02x %02x %02x %02x\n",
1637 single_toc
.addressc0
,
1638 single_toc
.controlc0
,
1640 single_toc
.dummyc0
[0],
1641 single_toc
.dummyc0
[1],
1642 single_toc
.dummyc0
[2],
1643 single_toc
.dummyc0
[3],
1644 single_toc
.dummyc0
[4],
1645 single_toc
.dummyc0
[5],
1646 single_toc
.dummyc0
[6]);
1650 sony_toc
.lead_out_start_msf
[0] =
1651 bcd_to_int(single_toc
.lead_out_start_msf
[0]);
1652 sony_toc
.lead_out_start_msf
[1] =
1653 bcd_to_int(single_toc
.lead_out_start_msf
[1]);
1654 sony_toc
.lead_out_start_msf
[2] =
1655 bcd_to_int(single_toc
.lead_out_start_msf
[2]);
1656 sony_toc
.lead_out_start_lba
=
1657 single_toc
.lead_out_start_lba
=
1658 msf_to_log(sony_toc
.lead_out_start_msf
);
1660 /* For points that do not exist, move the data over them
1661 to the right location. */
1662 if (single_toc
.pointb0
!= 0xb0) {
1663 memmove(((char *) &single_toc
) + 27,
1664 ((char *) &single_toc
) + 18,
1667 } else if (res_size
> 18) {
1668 sony_toc
.lead_out_start_msf
[0] =
1669 bcd_to_int(single_toc
.
1670 max_start_outer_leadout_msf
1672 sony_toc
.lead_out_start_msf
[1] =
1673 bcd_to_int(single_toc
.
1674 max_start_outer_leadout_msf
1676 sony_toc
.lead_out_start_msf
[2] =
1677 bcd_to_int(single_toc
.
1678 max_start_outer_leadout_msf
1680 sony_toc
.lead_out_start_lba
=
1681 msf_to_log(sony_toc
.
1682 lead_out_start_msf
);
1684 if (single_toc
.pointb1
!= 0xb1) {
1685 memmove(((char *) &single_toc
) + 36,
1686 ((char *) &single_toc
) + 27,
1690 if (single_toc
.pointb2
!= 0xb2) {
1691 memmove(((char *) &single_toc
) + 45,
1692 ((char *) &single_toc
) + 36,
1696 if (single_toc
.pointb3
!= 0xb3) {
1697 memmove(((char *) &single_toc
) + 54,
1698 ((char *) &single_toc
) + 45,
1702 if (single_toc
.pointb4
!= 0xb4) {
1703 memmove(((char *) &single_toc
) + 63,
1704 ((char *) &single_toc
) + 54,
1708 if (single_toc
.pointc0
!= 0xc0) {
1709 memmove(((char *) &single_toc
) + 72,
1710 ((char *) &single_toc
) + 63,
1715 printk(PRINT_INFO PFX
"start track lba %u, "
1716 "leadout start lba %u\n",
1717 single_toc
.start_track_lba
,
1718 single_toc
.lead_out_start_lba
);
1724 bcd_to_int(single_toc
.last_track_num
)
1726 bcd_to_int(single_toc
.
1727 first_track_num
); i
++) {
1728 printk(KERN_INFO PFX
"trk %02d: add 0x%01x, con 0x%01x, track %02d, start min %02d, sec %02d, frame %02d\n",
1730 single_toc
.tracks
[i
].address
,
1731 single_toc
.tracks
[i
].control
,
1732 bcd_to_int(single_toc
.
1734 bcd_to_int(single_toc
.
1738 bcd_to_int(single_toc
.
1742 bcd_to_int(single_toc
.
1747 bcd_to_int(single_toc
.
1750 bcd_to_int(single_toc
.
1754 bcd_to_int(single_toc
.
1757 bcd_to_int(single_toc
.
1761 printk(KERN_INFO PFX
"min track number %d, "
1762 "max track number %d\n",
1767 /* prepare a special table of contents for a CD-I disc. They don't have one. */
1768 if (single_toc
.disk_type
== 0x10 &&
1769 single_toc
.first_track_num
== 2 &&
1770 single_toc
.last_track_num
== 2 /* CD-I */ ) {
1771 sony_toc
.tracks
[totaltracks
].address
= 1;
1772 sony_toc
.tracks
[totaltracks
].control
= 4; /* force data tracks */
1773 sony_toc
.tracks
[totaltracks
].track
= 1;
1774 sony_toc
.tracks
[totaltracks
].
1775 track_start_msf
[0] = 0;
1776 sony_toc
.tracks
[totaltracks
].
1777 track_start_msf
[1] = 2;
1778 sony_toc
.tracks
[totaltracks
].
1779 track_start_msf
[2] = 0;
1783 /* gather track entries from this session */
1789 bcd_to_int(single_toc
.last_track_num
)
1791 bcd_to_int(single_toc
.
1793 i
++, totaltracks
++) {
1794 sony_toc
.tracks
[totaltracks
].
1796 single_toc
.tracks
[i
].address
;
1797 sony_toc
.tracks
[totaltracks
].
1799 single_toc
.tracks
[i
].control
;
1800 sony_toc
.tracks
[totaltracks
].
1802 bcd_to_int(single_toc
.
1804 sony_toc
.tracks
[totaltracks
].
1805 track_start_msf
[0] =
1806 bcd_to_int(single_toc
.
1808 track_start_msf
[0]);
1809 sony_toc
.tracks
[totaltracks
].
1810 track_start_msf
[1] =
1811 bcd_to_int(single_toc
.
1813 track_start_msf
[1]);
1814 sony_toc
.tracks
[totaltracks
].
1815 track_start_msf
[2] =
1816 bcd_to_int(single_toc
.
1818 track_start_msf
[2]);
1822 msf_to_log(sony_toc
.
1827 sony_toc
.tracks
[totaltracks
].
1831 tracks
[totaltracks
].
1834 sony_toc
.tracks
[totaltracks
].
1838 tracks
[totaltracks
].
1842 sony_toc
.first_track_num
= mint
;
1843 sony_toc
.last_track_num
= maxt
;
1844 /* Disk type of last session wins. For example:
1845 CD-Extra has disk type 0 for the first session, so
1846 a dumb HiFi CD player thinks it is a plain audio CD.
1847 We are interested in the disk type of the last session,
1848 which is 0x20 (XA) for CD-Extra, so we can access the
1850 sony_toc
.disk_type
= single_toc
.disk_type
;
1851 sony_toc
.sessions
= session
;
1853 /* don't believe everything :-) */
1855 single_toc
.start_track_lba
= 0;
1856 sony_toc
.start_track_lba
=
1857 single_toc
.start_track_lba
;
1859 if (session
> 1 && single_toc
.pointb0
== 0xb0 &&
1860 sony_toc
.lead_out_start_lba
==
1861 single_toc
.lead_out_start_lba
) {
1865 /* Let's not get carried away... */
1867 printk(KERN_NOTICE PFX
"too many sessions: "
1873 sony_toc
.track_entries
= totaltracks
;
1874 /* add one entry for the LAST track with track number CDROM_LEADOUT */
1875 sony_toc
.tracks
[totaltracks
].address
= single_toc
.address2
;
1876 sony_toc
.tracks
[totaltracks
].control
= single_toc
.control2
;
1877 sony_toc
.tracks
[totaltracks
].track
= CDROM_LEADOUT
;
1878 sony_toc
.tracks
[totaltracks
].track_start_msf
[0] =
1879 sony_toc
.lead_out_start_msf
[0];
1880 sony_toc
.tracks
[totaltracks
].track_start_msf
[1] =
1881 sony_toc
.lead_out_start_msf
[1];
1882 sony_toc
.tracks
[totaltracks
].track_start_msf
[2] =
1883 sony_toc
.lead_out_start_msf
[2];
1887 pr_debug(PFX
"Disk session %d, start track: %d, "
1889 session
, single_toc
.start_track_lba
,
1890 single_toc
.lead_out_start_lba
);
1892 pr_debug(PFX
"Leaving %s\n", __FUNCTION__
);
1897 * Uniform cdrom interface function
1898 * return multisession offset and sector information
1900 static int scd_get_last_session(struct cdrom_device_info
*cdi
,
1901 struct cdrom_multisession
*ms_info
)
1903 if (ms_info
== NULL
)
1906 if (!sony_toc_read
) {
1907 if (down_interruptible(&sony_sem
))
1908 return -ERESTARTSYS
;
1913 ms_info
->addr_format
= CDROM_LBA
;
1914 ms_info
->addr
.lba
= sony_toc
.start_track_lba
;
1915 ms_info
->xa_flag
= sony_toc
.disk_type
== SONY_XA_DISK_TYPE
||
1916 sony_toc
.disk_type
== 0x10 /* CDI */ ;
1922 * Search for a specific track in the table of contents.
1924 static int find_track(int track
)
1928 for (i
= 0; i
<= sony_toc
.track_entries
; i
++) {
1929 if (sony_toc
.tracks
[i
].track
== track
) {
1939 * Read the subcode and put it in last_sony_subcode for future use.
1941 static int read_subcode(void)
1943 unsigned int res_size
;
1946 do_sony_cd_cmd(SONY_REQ_SUBCODE_ADDRESS_CMD
,
1948 0, (unsigned char *) &last_sony_subcode
, &res_size
);
1950 || ((last_sony_subcode
.exec_status
[0] & 0xf0) == 0x20)) {
1951 printk(KERN_ERR PFX
"Sony CDROM error %s (read_subcode)\n",
1952 translate_error(last_sony_subcode
.exec_status
[1]));
1956 last_sony_subcode
.track_num
=
1957 bcd_to_int(last_sony_subcode
.track_num
);
1958 last_sony_subcode
.index_num
=
1959 bcd_to_int(last_sony_subcode
.index_num
);
1960 last_sony_subcode
.abs_msf
[0] =
1961 bcd_to_int(last_sony_subcode
.abs_msf
[0]);
1962 last_sony_subcode
.abs_msf
[1] =
1963 bcd_to_int(last_sony_subcode
.abs_msf
[1]);
1964 last_sony_subcode
.abs_msf
[2] =
1965 bcd_to_int(last_sony_subcode
.abs_msf
[2]);
1967 last_sony_subcode
.rel_msf
[0] =
1968 bcd_to_int(last_sony_subcode
.rel_msf
[0]);
1969 last_sony_subcode
.rel_msf
[1] =
1970 bcd_to_int(last_sony_subcode
.rel_msf
[1]);
1971 last_sony_subcode
.rel_msf
[2] =
1972 bcd_to_int(last_sony_subcode
.rel_msf
[2]);
1977 * Uniform cdrom interface function
1978 * return the media catalog number found on some older audio cds
1981 scd_get_mcn(struct cdrom_device_info
*cdi
, struct cdrom_mcn
*mcn
)
1983 unsigned char resbuffer
[2 + 14];
1984 unsigned char *mcnp
= mcn
->medium_catalog_number
;
1985 unsigned char *resp
= resbuffer
+ 3;
1986 unsigned int res_size
;
1988 memset(mcn
->medium_catalog_number
, 0, 14);
1989 if (down_interruptible(&sony_sem
))
1990 return -ERESTARTSYS
;
1991 do_sony_cd_cmd(SONY_REQ_UPC_EAN_CMD
,
1992 NULL
, 0, resbuffer
, &res_size
);
1994 if ((res_size
< 2) || ((resbuffer
[0] & 0xf0) == 0x20));
1996 /* packed bcd to single ASCII digits */
1997 *mcnp
++ = (*resp
>> 4) + '0';
1998 *mcnp
++ = (*resp
++ & 0x0f) + '0';
1999 *mcnp
++ = (*resp
>> 4) + '0';
2000 *mcnp
++ = (*resp
++ & 0x0f) + '0';
2001 *mcnp
++ = (*resp
>> 4) + '0';
2002 *mcnp
++ = (*resp
++ & 0x0f) + '0';
2003 *mcnp
++ = (*resp
>> 4) + '0';
2004 *mcnp
++ = (*resp
++ & 0x0f) + '0';
2005 *mcnp
++ = (*resp
>> 4) + '0';
2006 *mcnp
++ = (*resp
++ & 0x0f) + '0';
2007 *mcnp
++ = (*resp
>> 4) + '0';
2008 *mcnp
++ = (*resp
++ & 0x0f) + '0';
2009 *mcnp
++ = (*resp
>> 4) + '0';
2017 * Get the subchannel info like the CDROMSUBCHNL command wants to see it. If
2018 * the drive is playing, the subchannel needs to be read (since it would be
2019 * changing). If the drive is paused or completed, the subcode information has
2020 * already been stored, just use that. The ioctl call wants things in decimal
2021 * (not BCD), so all the conversions are done.
2023 static int sony_get_subchnl_info(struct cdrom_subchnl
*schi
)
2025 /* Get attention stuff */
2026 while (handle_sony_cd_attention());
2029 if (!sony_toc_read
) {
2033 switch (sony_audio_status
) {
2034 case CDROM_AUDIO_NO_STATUS
:
2035 case CDROM_AUDIO_PLAY
:
2036 if (read_subcode() < 0) {
2041 case CDROM_AUDIO_PAUSED
:
2042 case CDROM_AUDIO_COMPLETED
:
2046 case CDROM_AUDIO_NO_STATUS
:
2047 schi
->cdsc_audiostatus
= sony_audio_status
;
2051 case CDROM_AUDIO_INVALID
:
2052 case CDROM_AUDIO_ERROR
:
2057 schi
->cdsc_audiostatus
= sony_audio_status
;
2058 schi
->cdsc_adr
= last_sony_subcode
.address
;
2059 schi
->cdsc_ctrl
= last_sony_subcode
.control
;
2060 schi
->cdsc_trk
= last_sony_subcode
.track_num
;
2061 schi
->cdsc_ind
= last_sony_subcode
.index_num
;
2062 if (schi
->cdsc_format
== CDROM_MSF
) {
2063 schi
->cdsc_absaddr
.msf
.minute
=
2064 last_sony_subcode
.abs_msf
[0];
2065 schi
->cdsc_absaddr
.msf
.second
=
2066 last_sony_subcode
.abs_msf
[1];
2067 schi
->cdsc_absaddr
.msf
.frame
=
2068 last_sony_subcode
.abs_msf
[2];
2070 schi
->cdsc_reladdr
.msf
.minute
=
2071 last_sony_subcode
.rel_msf
[0];
2072 schi
->cdsc_reladdr
.msf
.second
=
2073 last_sony_subcode
.rel_msf
[1];
2074 schi
->cdsc_reladdr
.msf
.frame
=
2075 last_sony_subcode
.rel_msf
[2];
2076 } else if (schi
->cdsc_format
== CDROM_LBA
) {
2077 schi
->cdsc_absaddr
.lba
=
2078 msf_to_log(last_sony_subcode
.abs_msf
);
2079 schi
->cdsc_reladdr
.lba
=
2080 msf_to_log(last_sony_subcode
.rel_msf
);
2086 /* Get audio data from the drive. This is fairly complex because I
2087 am looking for status and data at the same time, but if I get status
2088 then I just look for data. I need to get the status immediately so
2089 the switch from audio to data tracks will happen quickly. */
2091 read_audio_data(char *buffer
, unsigned char res_reg
[], int *res_size
)
2093 unsigned long retry_count
;
2102 /* Wait for the drive to tell us we have something */
2103 retry_count
= jiffies
+ SONY_JIFFIES_TIMEOUT
;
2104 continue_read_audio_wait
:
2105 while (time_before(jiffies
, retry_count
) && !(is_data_ready())
2106 && !(is_result_ready() || result_read
)) {
2107 while (handle_sony_cd_attention());
2111 if (!(is_data_ready())) {
2112 if (is_result_ready() && !result_read
) {
2113 get_result(res_reg
, res_size
);
2115 /* Read block status and continue waiting for data. */
2116 if ((res_reg
[0] & 0xf0) == 0x50) {
2118 goto continue_read_audio_wait
;
2120 /* Invalid data from the drive. Shut down the operation. */
2121 else if ((res_reg
[0] & 0xf0) != 0x20) {
2122 printk(KERN_WARNING PFX
"Got result that "
2123 "should have been error: %d\n",
2126 res_reg
[1] = SONY_BAD_DATA_ERR
;
2131 pr_debug(PFX
"timeout out %d\n", __LINE__
);
2133 res_reg
[1] = SONY_TIMEOUT_OP_ERR
;
2140 /* If data block, then get 2340 bytes offset by 12. */
2141 if (sony_raw_data_mode
) {
2142 insb(sony_cd_read_reg
, buffer
+ CD_XA_HEAD
,
2145 /* Audio gets the whole 2352 bytes. */
2146 insb(sony_cd_read_reg
, buffer
, CD_FRAMESIZE_RAW
);
2149 /* If I haven't already gotten the result, get it now. */
2151 /* Wait for the drive to tell us we have something */
2152 retry_count
= jiffies
+ SONY_JIFFIES_TIMEOUT
;
2153 while (time_before(jiffies
, retry_count
)
2154 && !(is_result_ready())) {
2155 while (handle_sony_cd_attention());
2160 if (!is_result_ready()) {
2161 pr_debug(PFX
"timeout out %d\n", __LINE__
);
2163 res_reg
[1] = SONY_TIMEOUT_OP_ERR
;
2168 get_result(res_reg
, res_size
);
2172 if ((res_reg
[0] & 0xf0) == 0x50) {
2173 if ((res_reg
[0] == SONY_NO_CIRC_ERR_BLK_STAT
)
2174 || (res_reg
[0] == SONY_NO_LECC_ERR_BLK_STAT
)
2175 || (res_reg
[0] == SONY_RECOV_LECC_ERR_BLK_STAT
)
2176 || (res_reg
[0] == SONY_NO_ERR_DETECTION_STAT
)) {
2177 /* Ok, nothing to do. */
2179 printk(KERN_ERR PFX
"Data block error: 0x%x\n",
2182 res_reg
[1] = SONY_BAD_DATA_ERR
;
2185 } else if ((res_reg
[0] & 0xf0) != 0x20) {
2186 /* The drive gave me bad status, I don't know what to do.
2187 Reset the driver and return an error. */
2188 printk(KERN_NOTICE PFX
"Invalid block status: 0x%x\n",
2192 res_reg
[1] = SONY_BAD_DATA_ERR
;
2198 /* Perform a raw data read. This will automatically detect the
2199 track type and read the proper data (audio or data). */
2200 static int read_audio(struct cdrom_read_audio
*ra
)
2203 unsigned char params
[2];
2204 unsigned char res_reg
[12];
2205 unsigned int res_size
;
2206 unsigned int cframe
;
2208 if (down_interruptible(&sony_sem
))
2209 return -ERESTARTSYS
;
2213 /* Set the drive to do raw operations. */
2214 params
[0] = SONY_SD_DECODE_PARAM
;
2215 params
[1] = 0x06 | sony_raw_data_mode
;
2216 do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD
,
2217 params
, 2, res_reg
, &res_size
);
2218 if ((res_size
< 2) || ((res_reg
[0] & 0xf0) == 0x20)) {
2219 printk(KERN_ERR PFX
"Unable to set decode params: 0x%2.2x\n",
2225 /* From here down, we have to goto exit_read_audio instead of returning
2226 because the drive parameters have to be set back to data before
2230 if (start_request(ra
->addr
.lba
, ra
->nframes
)) {
2232 goto exit_read_audio
;
2235 /* For every requested frame. */
2237 while (cframe
< ra
->nframes
) {
2238 read_audio_data(audio_buffer
, res_reg
, &res_size
);
2239 if ((res_reg
[0] & 0xf0) == 0x20) {
2240 if (res_reg
[1] == SONY_BAD_DATA_ERR
) {
2241 printk(KERN_ERR PFX
"Data error on audio "
2243 ra
->addr
.lba
+ cframe
);
2244 } else if (res_reg
[1] == SONY_ILL_TRACK_R_ERR
) {
2245 /* Illegal track type, change track types and start over. */
2246 sony_raw_data_mode
=
2247 (sony_raw_data_mode
) ? 0 : 1;
2249 /* Set the drive mode. */
2250 params
[0] = SONY_SD_DECODE_PARAM
;
2251 params
[1] = 0x06 | sony_raw_data_mode
;
2252 do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD
,
2254 2, res_reg
, &res_size
);
2256 || ((res_reg
[0] & 0xf0) == 0x20)) {
2257 printk(KERN_ERR PFX
"Unable to set "
2258 "decode params: 0x%2.2x\n",
2261 goto exit_read_audio
;
2264 /* Restart the request on the current frame. */
2266 (ra
->addr
.lba
+ cframe
,
2267 ra
->nframes
- cframe
)) {
2269 goto exit_read_audio
;
2272 /* Don't go back to the top because don't want to get into
2273 and infinite loop. A lot of code gets duplicated, but
2274 that's no big deal, I don't guess. */
2275 read_audio_data(audio_buffer
, res_reg
,
2277 if ((res_reg
[0] & 0xf0) == 0x20) {
2279 SONY_BAD_DATA_ERR
) {
2280 printk(KERN_ERR PFX
"Data error"
2281 " on audio sector %d\n",
2285 printk(KERN_ERR PFX
"Error reading audio data on sector %d: %s\n",
2286 ra
->addr
.lba
+ cframe
,
2290 goto exit_read_audio
;
2292 } else if (copy_to_user(ra
->buf
+
2296 CD_FRAMESIZE_RAW
)) {
2298 goto exit_read_audio
;
2301 printk(KERN_ERR PFX
"Error reading audio "
2302 "data on sector %d: %s\n",
2303 ra
->addr
.lba
+ cframe
,
2304 translate_error(res_reg
[1]));
2306 goto exit_read_audio
;
2308 } else if (copy_to_user(ra
->buf
+ (CD_FRAMESIZE_RAW
* cframe
),
2309 (char *)audio_buffer
,
2310 CD_FRAMESIZE_RAW
)) {
2312 goto exit_read_audio
;
2318 get_result(res_reg
, &res_size
);
2319 if ((res_reg
[0] & 0xf0) == 0x20) {
2320 printk(KERN_ERR PFX
"Error return from audio read: %s\n",
2321 translate_error(res_reg
[1]));
2323 goto exit_read_audio
;
2328 /* Set the drive mode back to the proper one for the disk. */
2329 params
[0] = SONY_SD_DECODE_PARAM
;
2330 if (!sony_xa_mode
) {
2335 do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD
,
2336 params
, 2, res_reg
, &res_size
);
2337 if ((res_size
< 2) || ((res_reg
[0] & 0xf0) == 0x20)) {
2338 printk(KERN_ERR PFX
"Unable to reset decode params: 0x%2.2x\n",
2350 do_sony_cd_cmd_chk(const char *name
,
2352 unsigned char *params
,
2353 unsigned int num_params
,
2354 unsigned char *result_buffer
, unsigned int *result_size
)
2356 do_sony_cd_cmd(cmd
, params
, num_params
, result_buffer
,
2358 if ((*result_size
< 2) || ((result_buffer
[0] & 0xf0) == 0x20)) {
2359 printk(KERN_ERR PFX
"Error %s (CDROM%s)\n",
2360 translate_error(result_buffer
[1]), name
);
2367 * Uniform cdrom interface function
2370 static int scd_tray_move(struct cdrom_device_info
*cdi
, int position
)
2374 if (down_interruptible(&sony_sem
))
2375 return -ERESTARTSYS
;
2376 if (position
== 1 /* open tray */ ) {
2377 unsigned char res_reg
[12];
2378 unsigned int res_size
;
2380 do_sony_cd_cmd(SONY_AUDIO_STOP_CMD
, NULL
, 0, res_reg
,
2382 do_sony_cd_cmd(SONY_SPIN_DOWN_CMD
, NULL
, 0, res_reg
,
2385 sony_audio_status
= CDROM_AUDIO_INVALID
;
2386 retval
= do_sony_cd_cmd_chk("EJECT", SONY_EJECT_CMD
, NULL
, 0,
2387 res_reg
, &res_size
);
2389 if (0 == scd_spinup())
2398 * The big ugly ioctl handler.
2400 static int scd_audio_ioctl(struct cdrom_device_info
*cdi
,
2401 unsigned int cmd
, void *arg
)
2403 unsigned char res_reg
[12];
2404 unsigned int res_size
;
2405 unsigned char params
[7];
2408 if (down_interruptible(&sony_sem
))
2409 return -ERESTARTSYS
;
2411 case CDROMSTART
: /* Spin up the drive */
2412 retval
= do_sony_cd_cmd_chk("START", SONY_SPIN_UP_CMD
, NULL
,
2413 0, res_reg
, &res_size
);
2416 case CDROMSTOP
: /* Spin down the drive */
2417 do_sony_cd_cmd(SONY_AUDIO_STOP_CMD
, NULL
, 0, res_reg
,
2421 * Spin the drive down, ignoring the error if the disk was
2422 * already not spinning.
2424 sony_audio_status
= CDROM_AUDIO_NO_STATUS
;
2425 retval
= do_sony_cd_cmd_chk("STOP", SONY_SPIN_DOWN_CMD
, NULL
,
2426 0, res_reg
, &res_size
);
2429 case CDROMPAUSE
: /* Pause the drive */
2430 if (do_sony_cd_cmd_chk
2431 ("PAUSE", SONY_AUDIO_STOP_CMD
, NULL
, 0, res_reg
,
2436 /* Get the current position and save it for resuming */
2437 if (read_subcode() < 0) {
2441 cur_pos_msf
[0] = last_sony_subcode
.abs_msf
[0];
2442 cur_pos_msf
[1] = last_sony_subcode
.abs_msf
[1];
2443 cur_pos_msf
[2] = last_sony_subcode
.abs_msf
[2];
2444 sony_audio_status
= CDROM_AUDIO_PAUSED
;
2448 case CDROMRESUME
: /* Start the drive after being paused */
2449 if (sony_audio_status
!= CDROM_AUDIO_PAUSED
) {
2454 do_sony_cd_cmd(SONY_SPIN_UP_CMD
, NULL
, 0, res_reg
,
2457 /* Start the drive at the saved position. */
2458 params
[1] = int_to_bcd(cur_pos_msf
[0]);
2459 params
[2] = int_to_bcd(cur_pos_msf
[1]);
2460 params
[3] = int_to_bcd(cur_pos_msf
[2]);
2461 params
[4] = int_to_bcd(final_pos_msf
[0]);
2462 params
[5] = int_to_bcd(final_pos_msf
[1]);
2463 params
[6] = int_to_bcd(final_pos_msf
[2]);
2465 if (do_sony_cd_cmd_chk
2466 ("RESUME", SONY_AUDIO_PLAYBACK_CMD
, params
, 7, res_reg
,
2471 sony_audio_status
= CDROM_AUDIO_PLAY
;
2475 case CDROMPLAYMSF
: /* Play starting at the given MSF address. */
2476 do_sony_cd_cmd(SONY_SPIN_UP_CMD
, NULL
, 0, res_reg
,
2479 /* The parameters are given in int, must be converted */
2480 for (i
= 1; i
< 7; i
++) {
2482 int_to_bcd(((unsigned char *) arg
)[i
- 1]);
2485 if (do_sony_cd_cmd_chk
2486 ("PLAYMSF", SONY_AUDIO_PLAYBACK_CMD
, params
, 7,
2487 res_reg
, &res_size
) < 0) {
2492 /* Save the final position for pauses and resumes */
2493 final_pos_msf
[0] = bcd_to_int(params
[4]);
2494 final_pos_msf
[1] = bcd_to_int(params
[5]);
2495 final_pos_msf
[2] = bcd_to_int(params
[6]);
2496 sony_audio_status
= CDROM_AUDIO_PLAY
;
2500 case CDROMREADTOCHDR
: /* Read the table of contents header */
2502 struct cdrom_tochdr
*hdr
;
2505 if (!sony_toc_read
) {
2510 hdr
= (struct cdrom_tochdr
*) arg
;
2511 hdr
->cdth_trk0
= sony_toc
.first_track_num
;
2512 hdr
->cdth_trk1
= sony_toc
.last_track_num
;
2517 case CDROMREADTOCENTRY
: /* Read a given table of contents entry */
2519 struct cdrom_tocentry
*entry
;
2521 unsigned char *msf_val
= NULL
;
2524 if (!sony_toc_read
) {
2529 entry
= (struct cdrom_tocentry
*) arg
;
2531 track_idx
= find_track(entry
->cdte_track
);
2532 if (track_idx
< 0) {
2538 sony_toc
.tracks
[track_idx
].address
;
2540 sony_toc
.tracks
[track_idx
].control
;
2542 sony_toc
.tracks
[track_idx
].track_start_msf
;
2544 /* Logical buffer address or MSF format requested? */
2545 if (entry
->cdte_format
== CDROM_LBA
) {
2546 entry
->cdte_addr
.lba
= msf_to_log(msf_val
);
2547 } else if (entry
->cdte_format
== CDROM_MSF
) {
2548 entry
->cdte_addr
.msf
.minute
= *msf_val
;
2549 entry
->cdte_addr
.msf
.second
=
2551 entry
->cdte_addr
.msf
.frame
=
2558 case CDROMPLAYTRKIND
: /* Play a track. This currently ignores index. */
2560 struct cdrom_ti
*ti
= (struct cdrom_ti
*) arg
;
2564 if (!sony_toc_read
) {
2569 if ((ti
->cdti_trk0
< sony_toc
.first_track_num
)
2570 || (ti
->cdti_trk0
> sony_toc
.last_track_num
)
2571 || (ti
->cdti_trk1
< ti
->cdti_trk0
)) {
2576 track_idx
= find_track(ti
->cdti_trk0
);
2577 if (track_idx
< 0) {
2582 int_to_bcd(sony_toc
.tracks
[track_idx
].
2583 track_start_msf
[0]);
2585 int_to_bcd(sony_toc
.tracks
[track_idx
].
2586 track_start_msf
[1]);
2588 int_to_bcd(sony_toc
.tracks
[track_idx
].
2589 track_start_msf
[2]);
2592 * If we want to stop after the last track, use the lead-out
2595 if (ti
->cdti_trk1
>= sony_toc
.last_track_num
) {
2596 track_idx
= find_track(CDROM_LEADOUT
);
2598 track_idx
= find_track(ti
->cdti_trk1
+ 1);
2600 if (track_idx
< 0) {
2605 int_to_bcd(sony_toc
.tracks
[track_idx
].
2606 track_start_msf
[0]);
2608 int_to_bcd(sony_toc
.tracks
[track_idx
].
2609 track_start_msf
[1]);
2611 int_to_bcd(sony_toc
.tracks
[track_idx
].
2612 track_start_msf
[2]);
2615 do_sony_cd_cmd(SONY_SPIN_UP_CMD
, NULL
, 0, res_reg
,
2618 do_sony_cd_cmd(SONY_AUDIO_PLAYBACK_CMD
, params
, 7,
2619 res_reg
, &res_size
);
2622 || ((res_reg
[0] & 0xf0) == 0x20)) {
2624 "Params: %x %x %x %x %x %x %x\n",
2625 params
[0], params
[1], params
[2],
2626 params
[3], params
[4], params
[5],
2629 "Error %s (CDROMPLAYTRKIND)\n",
2630 translate_error(res_reg
[1]));
2635 /* Save the final position for pauses and resumes */
2636 final_pos_msf
[0] = bcd_to_int(params
[4]);
2637 final_pos_msf
[1] = bcd_to_int(params
[5]);
2638 final_pos_msf
[2] = bcd_to_int(params
[6]);
2639 sony_audio_status
= CDROM_AUDIO_PLAY
;
2644 case CDROMVOLCTRL
: /* Volume control. What volume does this change, anyway? */
2646 struct cdrom_volctrl
*volctrl
=
2647 (struct cdrom_volctrl
*) arg
;
2649 params
[0] = SONY_SD_AUDIO_VOLUME
;
2650 params
[1] = volctrl
->channel0
;
2651 params
[2] = volctrl
->channel1
;
2652 retval
= do_sony_cd_cmd_chk("VOLCTRL",
2653 SONY_SET_DRIVE_PARAM_CMD
,
2658 case CDROMSUBCHNL
: /* Get subchannel info */
2659 retval
= sony_get_subchnl_info((struct cdrom_subchnl
*) arg
);
2670 static int scd_read_audio(struct cdrom_device_info
*cdi
,
2671 unsigned int cmd
, unsigned long arg
)
2673 void __user
*argp
= (void __user
*)arg
;
2676 if (down_interruptible(&sony_sem
))
2677 return -ERESTARTSYS
;
2679 case CDROMREADAUDIO
: /* Read 2352 byte audio tracks and 2340 byte
2682 struct cdrom_read_audio ra
;
2686 if (!sony_toc_read
) {
2691 if (copy_from_user(&ra
, argp
, sizeof(ra
))) {
2696 if (ra
.nframes
== 0) {
2701 if (!access_ok(VERIFY_WRITE
, ra
.buf
,
2702 CD_FRAMESIZE_RAW
* ra
.nframes
))
2705 if (ra
.addr_format
== CDROM_LBA
) {
2707 sony_toc
.lead_out_start_lba
)
2708 || (ra
.addr
.lba
+ ra
.nframes
>=
2709 sony_toc
.lead_out_start_lba
)) {
2713 } else if (ra
.addr_format
== CDROM_MSF
) {
2714 if ((ra
.addr
.msf
.minute
>= 75)
2715 || (ra
.addr
.msf
.second
>= 60)
2716 || (ra
.addr
.msf
.frame
>= 75)) {
2721 ra
.addr
.lba
= ((ra
.addr
.msf
.minute
* 4500)
2722 + (ra
.addr
.msf
.second
* 75)
2723 + ra
.addr
.msf
.frame
);
2725 sony_toc
.lead_out_start_lba
)
2726 || (ra
.addr
.lba
+ ra
.nframes
>=
2727 sony_toc
.lead_out_start_lba
)) {
2732 /* I know, this can go negative on an unsigned. However,
2733 the first thing done to the data is to add this value,
2734 so this should compensate and allow direct msf access. */
2735 ra
.addr
.lba
-= LOG_START_OFFSET
;
2741 retval
= read_audio(&ra
);
2754 static int scd_spinup(void)
2756 unsigned char res_reg
[12];
2757 unsigned int res_size
;
2763 do_sony_cd_cmd(SONY_SPIN_UP_CMD
, NULL
, 0, res_reg
, &res_size
);
2765 /* The drive sometimes returns error 0. I don't know why, but ignore
2766 it. It seems to mean the drive has already done the operation. */
2767 if ((res_size
< 2) || ((res_reg
[0] != 0) && (res_reg
[1] != 0))) {
2768 printk(KERN_ERR PFX
"%s error (scd_open, spin up)\n",
2769 translate_error(res_reg
[1]));
2773 do_sony_cd_cmd(SONY_READ_TOC_CMD
, NULL
, 0, res_reg
, &res_size
);
2775 /* The drive sometimes returns error 0. I don't know why, but ignore
2776 it. It seems to mean the drive has already done the operation. */
2777 if ((res_size
< 2) || ((res_reg
[0] != 0) && (res_reg
[1] != 0))) {
2778 /* If the drive is already playing, it's ok. */
2779 if ((res_reg
[1] == SONY_AUDIO_PLAYING_ERR
)
2780 || (res_reg
[1] == 0)) {
2784 /* If the drive says it is not spun up (even though we just did it!)
2785 then retry the operation at least a few times. */
2786 if ((res_reg
[1] == SONY_NOT_SPIN_ERR
)
2787 && (num_spin_ups
< MAX_CDU31A_RETRIES
)) {
2789 goto respinup_on_open
;
2792 printk(KERN_ERR PFX
"Error %s (scd_open, read toc)\n",
2793 translate_error(res_reg
[1]));
2794 do_sony_cd_cmd(SONY_SPIN_DOWN_CMD
, NULL
, 0, res_reg
,
2802 * Open the drive for operations. Spin the drive up and read the table of
2803 * contents if these have not already been done.
2805 static int scd_open(struct cdrom_device_info
*cdi
, int purpose
)
2807 unsigned char res_reg
[12];
2808 unsigned int res_size
;
2809 unsigned char params
[2];
2812 /* Open for IOCTLs only - no media check */
2817 if (sony_usage
== 0) {
2818 if (scd_spinup() != 0)
2821 if (!sony_toc_read
) {
2822 do_sony_cd_cmd(SONY_SPIN_DOWN_CMD
, NULL
, 0,
2823 res_reg
, &res_size
);
2827 /* For XA on the CDU31A only, we have to do special reads.
2828 The CDU33A handles XA automagically. */
2829 /* if ( (sony_toc.disk_type == SONY_XA_DISK_TYPE) */
2830 if ((sony_toc
.disk_type
!= 0x00)
2831 && (!is_double_speed
)) {
2832 params
[0] = SONY_SD_DECODE_PARAM
;
2834 do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD
,
2835 params
, 2, res_reg
, &res_size
);
2837 || ((res_reg
[0] & 0xf0) == 0x20)) {
2838 printk(KERN_WARNING PFX
"Unable to set "
2839 "XA params: 0x%2.2x\n", res_reg
[1]);
2843 /* A non-XA disk. Set the parms back if necessary. */
2844 else if (sony_xa_mode
) {
2845 params
[0] = SONY_SD_DECODE_PARAM
;
2847 do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD
,
2848 params
, 2, res_reg
, &res_size
);
2850 || ((res_reg
[0] & 0xf0) == 0x20)) {
2851 printk(KERN_WARNING PFX
"Unable to reset "
2852 "XA params: 0x%2.2x\n", res_reg
[1]);
2867 * Close the drive. Spin it down if no task is using it. The spin
2868 * down will fail if playing audio, so audio play is OK.
2870 static void scd_release(struct cdrom_device_info
*cdi
)
2872 if (sony_usage
== 1) {
2873 unsigned char res_reg
[12];
2874 unsigned int res_size
;
2876 do_sony_cd_cmd(SONY_SPIN_DOWN_CMD
, NULL
, 0, res_reg
,
2884 static struct cdrom_device_ops scd_dops
= {
2886 .release
= scd_release
,
2887 .drive_status
= scd_drive_status
,
2888 .media_changed
= scd_media_changed
,
2889 .tray_move
= scd_tray_move
,
2890 .lock_door
= scd_lock_door
,
2891 .select_speed
= scd_select_speed
,
2892 .get_last_session
= scd_get_last_session
,
2893 .get_mcn
= scd_get_mcn
,
2895 .audio_ioctl
= scd_audio_ioctl
,
2896 .capability
= CDC_OPEN_TRAY
| CDC_CLOSE_TRAY
| CDC_LOCK
|
2897 CDC_SELECT_SPEED
| CDC_MULTI_SESSION
|
2898 CDC_MCN
| CDC_MEDIA_CHANGED
| CDC_PLAY_AUDIO
|
2899 CDC_RESET
| CDC_DRIVE_STATUS
,
2903 static struct cdrom_device_info scd_info
= {
2910 static int scd_block_open(struct inode
*inode
, struct file
*file
)
2912 return cdrom_open(&scd_info
, inode
, file
);
2915 static int scd_block_release(struct inode
*inode
, struct file
*file
)
2917 return cdrom_release(&scd_info
, file
);
2920 static int scd_block_ioctl(struct inode
*inode
, struct file
*file
,
2921 unsigned cmd
, unsigned long arg
)
2925 /* The eject and close commands should be handled by Uniform CD-ROM
2926 * driver - but I always got hard lockup instead of eject
2927 * until I put this here.
2931 scd_lock_door(&scd_info
, 0);
2932 retval
= scd_tray_move(&scd_info
, 1);
2934 case CDROMCLOSETRAY
:
2935 retval
= scd_tray_move(&scd_info
, 0);
2937 case CDROMREADAUDIO
:
2938 retval
= scd_read_audio(&scd_info
, CDROMREADAUDIO
, arg
);
2941 retval
= cdrom_ioctl(file
, &scd_info
, inode
, cmd
, arg
);
2946 static int scd_block_media_changed(struct gendisk
*disk
)
2948 return cdrom_media_changed(&scd_info
);
2951 static struct block_device_operations scd_bdops
=
2953 .owner
= THIS_MODULE
,
2954 .open
= scd_block_open
,
2955 .release
= scd_block_release
,
2956 .ioctl
= scd_block_ioctl
,
2957 .media_changed
= scd_block_media_changed
,
2960 static struct gendisk
*scd_gendisk
;
2962 /* The different types of disc loading mechanisms supported */
2963 static char *load_mech
[] __initdata
=
2964 { "caddy", "tray", "pop-up", "unknown" };
2967 get_drive_configuration(unsigned short base_io
,
2968 unsigned char res_reg
[], unsigned int *res_size
)
2970 unsigned long retry_count
;
2973 if (!request_region(base_io
, 4, "cdu31a"))
2976 /* Set the base address */
2977 cdu31a_port
= base_io
;
2979 /* Set up all the register locations */
2980 sony_cd_cmd_reg
= cdu31a_port
+ SONY_CMD_REG_OFFSET
;
2981 sony_cd_param_reg
= cdu31a_port
+ SONY_PARAM_REG_OFFSET
;
2982 sony_cd_write_reg
= cdu31a_port
+ SONY_WRITE_REG_OFFSET
;
2983 sony_cd_control_reg
= cdu31a_port
+ SONY_CONTROL_REG_OFFSET
;
2984 sony_cd_status_reg
= cdu31a_port
+ SONY_STATUS_REG_OFFSET
;
2985 sony_cd_result_reg
= cdu31a_port
+ SONY_RESULT_REG_OFFSET
;
2986 sony_cd_read_reg
= cdu31a_port
+ SONY_READ_REG_OFFSET
;
2987 sony_cd_fifost_reg
= cdu31a_port
+ SONY_FIFOST_REG_OFFSET
;
2990 * Check to see if anything exists at the status register location.
2991 * I don't know if this is a good way to check, but it seems to work
2994 if (read_status_register() != 0xff) {
2996 * Reset the drive and wait for attention from it (to say it's reset).
2997 * If you don't wait, the next operation will probably fail.
3000 retry_count
= jiffies
+ SONY_RESET_TIMEOUT
;
3001 while (time_before(jiffies
, retry_count
)
3002 && (!is_attention())) {
3007 /* If attention is never seen probably not a CDU31a present */
3008 if (!is_attention()) {
3015 * Get the drive configuration.
3017 do_sony_cd_cmd(SONY_REQ_DRIVE_CONFIG_CMD
,
3019 0, (unsigned char *) res_reg
, res_size
);
3020 if (*res_size
<= 2 || (res_reg
[0] & 0xf0) != 0)
3025 /* Return an error */
3028 release_region(cdu31a_port
, 4);
3035 * Set up base I/O and interrupts, called from main.c.
3038 static int __init
cdu31a_setup(char *strings
)
3042 (void) get_options(strings
, ARRAY_SIZE(ints
), ints
);
3045 cdu31a_port
= ints
[1];
3048 cdu31a_irq
= ints
[2];
3050 if ((strings
!= NULL
) && (*strings
!= '\0')) {
3051 if (strcmp(strings
, "PAS") == 0) {
3054 printk(KERN_NOTICE PFX
"Unknown interface type: %s\n",
3062 __setup("cdu31a=", cdu31a_setup
);
3067 * Initialize the driver.
3069 int __init
cdu31a_init(void)
3071 struct s_sony_drive_config drive_config
;
3072 struct gendisk
*disk
;
3074 unsigned int res_size
;
3081 * According to Alex Freed (freed@europa.orion.adobe.com), this is
3082 * required for the Fusion CD-16 package. If the sound driver is
3083 * loaded, it should work fine, but just in case...
3085 * The following turn on the CD-ROM interface for a Fusion CD-16.
3087 if (sony_pas_init
) {
3092 /* Setting the base I/O address to 0xffff will disable it. */
3093 if (cdu31a_port
== 0xffff)
3096 if (cdu31a_port
!= 0) {
3097 /* Need IRQ 0 because we can't sleep here. */
3098 tmp_irq
= cdu31a_irq
;
3100 if (!get_drive_configuration(cdu31a_port
,
3101 drive_config
.exec_status
,
3104 cdu31a_irq
= tmp_irq
;
3107 for (i
= 0; cdu31a_addresses
[i
].base
; i
++) {
3108 if (get_drive_configuration(cdu31a_addresses
[i
].base
,
3109 drive_config
.exec_status
,
3111 cdu31a_irq
= cdu31a_addresses
[i
].int_num
;
3119 if (register_blkdev(MAJOR_NR
, "cdu31a"))
3122 disk
= alloc_disk(1);
3125 disk
->major
= MAJOR_NR
;
3126 disk
->first_minor
= 0;
3127 sprintf(disk
->disk_name
, "cdu31a");
3128 disk
->fops
= &scd_bdops
;
3129 disk
->flags
= GENHD_FL_CD
;
3131 if (SONY_HWC_DOUBLE_SPEED(drive_config
))
3132 is_double_speed
= 1;
3134 tmp_irq
= cdu31a_irq
; /* Need IRQ 0 because we can't sleep here. */
3137 sony_speed
= is_double_speed
; /* Set 2X drives to 2X by default */
3138 set_drive_params(sony_speed
);
3140 cdu31a_irq
= tmp_irq
;
3142 if (cdu31a_irq
> 0) {
3144 (cdu31a_irq
, cdu31a_interrupt
, IRQF_DISABLED
,
3146 printk(KERN_WARNING PFX
"Unable to grab IRQ%d for "
3147 "the CDU31A driver\n", cdu31a_irq
);
3152 sprintf(msg
, "Sony I/F CDROM : %8.8s %16.16s %8.8s\n",
3153 drive_config
.vendor_id
,
3154 drive_config
.product_id
,
3155 drive_config
.product_rev_level
);
3156 sprintf(buf
, " Capabilities: %s",
3157 load_mech
[SONY_HWC_GET_LOAD_MECH(drive_config
)]);
3159 if (SONY_HWC_AUDIO_PLAYBACK(drive_config
))
3160 strcat(msg
, ", audio");
3162 deficiency
|= CDC_PLAY_AUDIO
;
3163 if (SONY_HWC_EJECT(drive_config
))
3164 strcat(msg
, ", eject");
3166 deficiency
|= CDC_OPEN_TRAY
;
3167 if (SONY_HWC_LED_SUPPORT(drive_config
))
3168 strcat(msg
, ", LED");
3169 if (SONY_HWC_ELECTRIC_VOLUME(drive_config
))
3170 strcat(msg
, ", elec. Vol");
3171 if (SONY_HWC_ELECTRIC_VOLUME_CTL(drive_config
))
3172 strcat(msg
, ", sep. Vol");
3173 if (is_double_speed
)
3174 strcat(msg
, ", double speed");
3176 deficiency
|= CDC_SELECT_SPEED
;
3177 if (cdu31a_irq
> 0) {
3178 sprintf(buf
, ", irq %d", cdu31a_irq
);
3182 printk(KERN_INFO PFX
"%s",msg
);
3184 cdu31a_queue
= blk_init_queue(do_cdu31a_request
, &cdu31a_lock
);
3187 blk_queue_hardsect_size(cdu31a_queue
, 2048);
3189 init_timer(&cdu31a_abort_timer
);
3190 cdu31a_abort_timer
.function
= handle_abort_timeout
;
3192 scd_info
.mask
= deficiency
;
3194 if (register_cdrom(&scd_info
))
3196 disk
->queue
= cdu31a_queue
;
3203 blk_cleanup_queue(cdu31a_queue
);
3206 free_irq(cdu31a_irq
, NULL
);
3207 printk(KERN_ERR PFX
"Unable to register with Uniform cdrom driver\n");
3210 if (unregister_blkdev(MAJOR_NR
, "cdu31a")) {
3211 printk(KERN_WARNING PFX
"Can't unregister block device\n");
3214 release_region(cdu31a_port
, 4);
3220 static void __exit
cdu31a_exit(void)
3222 del_gendisk(scd_gendisk
);
3223 put_disk(scd_gendisk
);
3224 if (unregister_cdrom(&scd_info
)) {
3225 printk(KERN_WARNING PFX
"Can't unregister from Uniform "
3229 if ((unregister_blkdev(MAJOR_NR
, "cdu31a") == -EINVAL
)) {
3230 printk(KERN_WARNING PFX
"Can't unregister\n");
3234 blk_cleanup_queue(cdu31a_queue
);
3237 free_irq(cdu31a_irq
, NULL
);
3239 release_region(cdu31a_port
, 4);
3240 printk(KERN_INFO PFX
"module released.\n");
3244 module_init(cdu31a_init
);
3246 module_exit(cdu31a_exit
);
3248 MODULE_LICENSE("GPL");
3249 MODULE_ALIAS_BLOCKDEV_MAJOR(CDU31A_CDROM_MAJOR
);