[XFRM]: Trace which secpath state is reject factor.
[linux-2.6/linux-loongson.git] / drivers / cdrom / cdu31a.c
blob37bdb0163f0d1b47acf2df88392b318de984480a
1 /*
2 * Sony CDU-31A CDROM interface device driver.
4 * Corey Minyard (minyard@wf-rch.cirr.com)
6 * Colossians 3:17
8 * See Documentation/cdrom/cdu31a for additional details about this driver.
9 *
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
32 * the following:
34 * retry_count = jiffies+ SONY_JIFFIES_TIMEOUT;
35 * while (time_before(jiffies, retry_count) && (! <some condition to wait for))
36 * {
37 * while (handle_sony_cd_attention())
38 * ;
40 * sony_sleep();
41 * }
42 * if (the condition not met)
43 * {
44 * return an error;
45 * }
47 * This ugly hack waits for something to happen, sleeping a little
48 * between every try. it also handles attentions, which are
49 * asynchronous events from the drive informing the driver that a disk
50 * has been inserted, removed, etc.
52 * NEWS FLASH - The driver now supports interrupts but they are
53 * turned off by default. Use of interrupts is highly encouraged, it
54 * cuts CPU usage down to a reasonable level. I had DMA in for a while
55 * but PC DMA is just too slow. Better to just insb() it.
57 * One thing about these drives: They talk in MSF (Minute Second Frame) format.
58 * There are 75 frames a second, 60 seconds a minute, and up to 75 minutes on a
59 * disk. The funny thing is that these are sent to the drive in BCD, but the
60 * interface wants to see them in decimal. A lot of conversion goes on.
62 * DRIVER SPECIAL FEATURES
63 * -----------------------
65 * This section describes features beyond the normal audio and CD-ROM
66 * functions of the drive.
68 * XA compatibility
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.
73 * Multi-Session
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!
79 * Raw sector I/O
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.
103 * TODO:
104 * CDs with form1 and form2 sectors cause problems
105 * with current read-ahead strategy.
107 * Credits:
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
114 * will start.
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>
151 #define DEBUG 1
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>
171 #include <asm/io.h>
172 #include <asm/uaccess.h>
173 #include <asm/dma.h>
175 #include "cdu31a.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
187 ** drives.
189 static struct {
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
248 drive open. */
250 static int sony_pas_init = 0; /* Initialize the Pro-Audio
251 Spectrum card? */
253 static struct s_sony_session_toc single_toc; /* Holds the
254 table of
255 contents. */
257 static struct s_all_sessions_toc sony_toc; /* entries gathered from all
258 sessions */
260 static int sony_toc_read = 0; /* Has the TOC been read for
261 the drive? */
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
293 interrupts. */
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)
323 int retval;
325 retval = disk_changed;
326 disk_changed = 0;
328 return retval;
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 */
339 return -EINVAL;
340 if (sony_spun_up)
341 return CDS_DISC_OK;
342 if (down_interruptible(&sony_sem))
343 return -ERESTARTSYS;
344 if (scd_spinup() == 0)
345 sony_spun_up = 1;
346 up(&sony_sem);
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) {
373 yield();
374 } else { /* Interrupt driven */
375 DEFINE_WAIT(w);
376 int first = 1;
378 while (1) {
379 prepare_to_wait(&cdu31a_irq_wait, &w,
380 TASK_INTERRUPTIBLE);
381 if (first) {
382 enable_interrupts();
383 first = 0;
386 if (irq_flag != 0)
387 break;
388 if (!signal_pending(current)) {
389 schedule();
390 continue;
391 } else
392 disable_interrupts();
393 break;
395 finish_wait(&cdu31a_irq_wait, &w);
396 irq_flag = 0;
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;
443 sony_toc_read = 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))
456 return -ERESTARTSYS;
457 reset_drive();
459 retry_count = jiffies + SONY_RESET_TIMEOUT;
460 while (time_before(jiffies, retry_count) && (!is_attention())) {
461 sony_sleep();
464 up(&sony_sem);
465 return 0;
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)
518 unsigned char val;
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
523 this one here. */
524 /* Clear out the result registers. */
525 while (is_result_reg_not_empty()) {
526 val = read_result_register();
528 clear_data_ready();
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();
540 irq_flag = 1;
541 wake_up_interruptible(&cdu31a_irq_wait);
543 } else if (waitqueue_active(&cdu31a_irq_wait)) {
544 disable_interrupts();
545 irq_flag = 1;
546 wake_up_interruptible(&cdu31a_irq_wait);
547 } else {
548 disable_interrupts();
549 printk(KERN_NOTICE PFX
550 "Got an interrupt but nothing was waiting\n");
552 return IRQ_HANDLED;
556 * give more verbose error messages
558 static unsigned char *translate_error(unsigned char err_code)
560 static unsigned char errbuf[80];
562 switch (err_code) {
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);
606 return errbuf;
610 * Set the drive parameters so the drive will auto-spin-up when a
611 * disk is inserted.
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 */
632 if (is_auto_eject)
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
637 possible */
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)
653 if (speed == 0)
654 sony_speed = 1;
655 else
656 sony_speed = speed - 1;
658 if (down_interruptible(&sony_sem))
659 return -ERESTARTSYS;
660 set_drive_params(sony_speed);
661 up(&sony_sem);
662 return 0;
666 * Uniform cdrom interface function
667 * lock or unlock eject button
669 static int scd_lock_door(struct cdrom_device_info *cdi, int lock)
671 if (lock == 0) {
672 is_auto_eject = 1;
673 } else {
674 is_auto_eject = 0;
676 if (down_interruptible(&sony_sem))
677 return -ERESTARTSYS;
678 set_drive_params(sony_speed);
679 up(&sony_sem);
680 return 0;
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");
694 reset_drive();
695 retry_count = jiffies + SONY_RESET_TIMEOUT;
696 while (time_before(jiffies, retry_count) && (!is_attention())) {
697 sony_sleep();
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",
703 res_reg[1]);
706 msleep(2000);
708 sony_get_toc();
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())) {
722 retry_count--;
724 if (!is_param_write_rdy()) {
725 return -EIO;
728 while (num_params > 0) {
729 write_param(*params);
730 params++;
731 num_params--;
734 return 0;
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
742 * the drive manual.
744 static void
745 get_result(unsigned char *result_buffer, unsigned int *result_size)
747 unsigned char a, b;
748 int i;
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())))) {
757 sony_sleep();
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;
765 *result_size = 2;
766 return;
770 * Get the first two bytes. This determines what else needs
771 * to be done.
773 clear_result_ready();
774 a = read_result_register();
775 *result_buffer = a;
776 result_buffer++;
778 /* Check for block error status result. */
779 if ((a & 0xf0) == 0x50) {
780 *result_size = 1;
781 return;
784 b = read_result_register();
785 *result_buffer = b;
786 result_buffer++;
787 *result_size = 2;
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) {
798 if (b > 8) {
799 for (i = 0; i < 8; i++) {
800 *result_buffer = read_result_register();
801 result_buffer++;
802 (*result_size)++;
804 b = b - 8;
806 while (b > 10) {
807 retry_count = SONY_READY_RETRIES;
808 while ((retry_count > 0)
809 && (!is_result_ready())) {
810 retry_count--;
812 if (!is_result_ready()) {
813 pr_debug(PFX "timeout out %d\n",
814 __LINE__);
815 result_buffer[0] = 0x20;
816 result_buffer[1] =
817 SONY_TIMEOUT_OP_ERR;
818 *result_size = 2;
819 return;
822 clear_result_ready();
824 for (i = 0; i < 10; i++) {
825 *result_buffer =
826 read_result_register();
827 result_buffer++;
828 (*result_size)++;
830 b = b - 10;
833 if (b > 0) {
834 retry_count = SONY_READY_RETRIES;
835 while ((retry_count > 0)
836 && (!is_result_ready())) {
837 retry_count--;
839 if (!is_result_ready()) {
840 pr_debug(PFX "timeout out %d\n",
841 __LINE__);
842 result_buffer[0] = 0x20;
843 result_buffer[1] =
844 SONY_TIMEOUT_OP_ERR;
845 *result_size = 2;
846 return;
851 while (b > 0) {
852 *result_buffer = read_result_register();
853 result_buffer++;
854 (*result_size)++;
855 b--;
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.
865 static void
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;
872 int num_retries = 0;
874 retry_cd_operation:
876 while (handle_sony_cd_attention());
878 retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
879 while (time_before(jiffies, retry_count) && (is_busy())) {
880 sony_sleep();
882 while (handle_sony_cd_attention());
884 if (is_busy()) {
885 pr_debug(PFX "timeout out %d\n", __LINE__);
886 result_buffer[0] = 0x20;
887 result_buffer[1] = SONY_TIMEOUT_OP_ERR;
888 *result_size = 2;
889 } else {
890 clear_result_ready();
891 clear_param_reg();
893 write_params(params, num_params);
894 write_cmd(cmd);
896 get_result(result_buffer, result_size);
899 if (((result_buffer[0] & 0xf0) == 0x20)
900 && (num_retries < MAX_CDU31A_RETRIES)) {
901 num_retries++;
902 msleep(100);
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
915 * prevent a lockup.
917 static int handle_sony_cd_attention(void)
919 unsigned char atten_code;
920 static int num_consecutive_attentions = 0;
921 volatile int val;
924 #if 0
925 pr_debug(PFX "Entering %s\n", __FUNCTION__);
926 #endif
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__,
934 __LINE__);
935 return 0;
938 clear_attention();
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:
944 disk_changed = 1;
945 sony_toc_read = 0;
946 sony_audio_status = CDROM_AUDIO_NO_STATUS;
947 sony_blocks_left = 0;
948 break;
950 case SONY_SPIN_DOWN_COMPLETE_ATTN:
951 /* Mark the disk as spun down. */
952 sony_spun_up = 0;
953 break;
955 case SONY_AUDIO_PLAY_DONE_ATTN:
956 sony_audio_status = CDROM_AUDIO_COMPLETED;
957 read_subcode();
958 break;
960 case SONY_EJECT_PUSHED_ATTN:
961 if (is_auto_eject) {
962 sony_audio_status = CDROM_AUDIO_INVALID;
964 break;
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;
971 break;
974 num_consecutive_attentions++;
975 pr_debug(PFX "Leaving %s at %d\n", __FUNCTION__, __LINE__);
976 return 1;
977 } else if (abort_read_started) {
978 while (is_result_reg_not_empty()) {
979 val = read_result_register();
981 clear_data_ready();
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__);
989 return 1;
992 num_consecutive_attentions = 0;
993 #if 0
994 pr_debug(PFX "Leaving %s at %d\n", __FUNCTION__, __LINE__);
995 #endif
996 return 0;
1000 /* Convert from an integer 0-99 to BCD */
1001 static inline unsigned int int_to_bcd(unsigned int val)
1003 int retval;
1006 retval = (val / 10) << 4;
1007 retval = retval | val % 10;
1008 return retval;
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);
1027 log = 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)
1038 unsigned int log;
1041 log = msf[2];
1042 log += msf[1] * 75;
1043 log += msf[0] * 4500;
1044 log = log - LOG_START_OFFSET;
1046 return log;
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
1067 drive. */
1068 static int
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, &params[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())) {
1087 sony_sleep();
1089 while (handle_sony_cd_attention());
1092 if (is_busy()) {
1093 printk(KERN_NOTICE PFX "Timeout while waiting "
1094 "to issue command\n");
1095 pr_debug(PFX "Leaving %s at %d\n", __FUNCTION__, __LINE__);
1096 return 1;
1097 } else {
1098 /* Issue the command */
1099 clear_result_ready();
1100 clear_param_reg();
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__);
1108 return 0;
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];
1117 int result_size;
1118 volatile int val;
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();
1130 clear_data_ready();
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
1150 the drive. */
1151 clear_result_ready();
1152 clear_param_reg();
1153 write_cmd(SONY_ABORT_CMD);
1155 sony_blocks_left = 0;
1156 abort_read_started = 1;
1157 up(&sony_sem);
1159 pr_debug(PFX "Leaving %s\n", __FUNCTION__);
1162 /* Actually get one sector of data from the drive. */
1163 static void
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. */
1170 if (sony_xa_mode)
1171 insb(sony_cd_read_reg, audio_buffer, CD_XA_HEAD);
1173 clear_data_ready();
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. */
1179 if (sony_xa_mode)
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. */
1186 static void
1187 read_data_block(char *buffer,
1188 unsigned int block,
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__);
1196 res_reg[0] = 0;
1197 res_reg[1] = 0;
1198 *res_size = 0;
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());
1205 sony_sleep();
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]);
1213 res_reg[0] = 0x20;
1214 res_reg[1] = SONY_BAD_DATA_ERR;
1215 *res_size = 2;
1217 abort_read();
1218 } else {
1219 pr_debug(PFX "timeout out %d\n", __LINE__);
1220 res_reg[0] = 0x20;
1221 res_reg[1] = SONY_TIMEOUT_OP_ERR;
1222 *res_size = 2;
1223 abort_read();
1225 } else {
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());
1236 sony_sleep();
1239 if (!is_result_ready()) {
1240 pr_debug(PFX "timeout out %d\n", __LINE__);
1241 res_reg[0] = 0x20;
1242 res_reg[1] = SONY_TIMEOUT_OP_ERR;
1243 *res_size = 2;
1244 abort_read();
1245 } else {
1246 get_result(res_reg, res_size);
1248 /* If we got a buffer status, handle that. */
1249 if ((res_reg[0] & 0xf0) == 0x50) {
1251 if ((res_reg[0] ==
1252 SONY_NO_CIRC_ERR_BLK_STAT)
1253 || (res_reg[0] ==
1254 SONY_NO_LECC_ERR_BLK_STAT)
1255 || (res_reg[0] ==
1256 SONY_RECOV_LECC_ERR_BLK_STAT)) {
1257 /* nothing here */
1258 } else {
1259 printk(KERN_ERR PFX "Data block "
1260 "error: 0x%x\n", res_reg[0]);
1261 res_reg[0] = 0x20;
1262 res_reg[1] = SONY_BAD_DATA_ERR;
1263 *res_size = 2;
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]);
1275 restart_on_error();
1276 res_reg[0] = 0x20;
1277 res_reg[1] = SONY_BAD_DATA_ERR;
1278 *res_size = 2;
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);
1305 return;
1308 /* Get drive status before doing anything. */
1309 while (handle_sony_cd_attention());
1311 /* Make sure we have a valid TOC. */
1312 sony_get_toc();
1315 /* Make sure the timer is cancelled. */
1316 del_timer(&cdu31a_abort_timer);
1318 while (1) {
1320 * The beginning here is stolen from the hard disk driver. I hope
1321 * it's right.
1323 req = elv_next_request(q);
1324 if (!req)
1325 goto end_do_cdu31a_request;
1327 if (!sony_spun_up)
1328 scd_spinup();
1330 block = req->sector;
1331 nblock = req->nr_sectors;
1332 pr_debug(PFX "request at block %d, length %d blocks\n",
1333 block, nblock);
1334 if (!sony_toc_read) {
1335 printk(KERN_NOTICE PFX "TOC not read\n");
1336 end_request(req, 0);
1337 continue;
1340 /* WTF??? */
1341 if (!(req->flags & REQ_CMD))
1342 continue;
1343 if (rq_data_dir(req) == WRITE) {
1344 end_request(req, 0);
1345 continue;
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);
1355 continue;
1358 if (nblock > 4)
1359 nblock = 4;
1360 num_retries = 0;
1362 try_read_again:
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);
1368 continue;
1371 /* If no data is left to be read from the drive, start the
1372 next request. */
1373 if (sony_blocks_left == 0) {
1374 if (start_request(block / 4, nblock / 4)) {
1375 end_request(req, 0);
1376 continue;
1379 /* If the requested block is not the next one waiting in
1380 the driver, abort the current operation and start a
1381 new one. */
1382 else if (block != sony_next_block) {
1383 pr_debug(PFX "Read for block %d, expected %d\n",
1384 block, sony_next_block);
1385 abort_read();
1386 if (!sony_toc_read) {
1387 printk(KERN_NOTICE PFX "TOC not read\n");
1388 end_request(req, 0);
1389 continue;
1391 if (start_request(block / 4, nblock / 4)) {
1392 printk(KERN_NOTICE PFX "start request failed\n");
1393 end_request(req, 0);
1394 continue;
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);
1407 continue;
1410 if (num_retries > MAX_CDU31A_RETRIES) {
1411 end_request(req, 0);
1412 continue;
1415 num_retries++;
1416 if (res_reg[1] == SONY_NOT_SPIN_ERR) {
1417 do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg,
1418 &res_size);
1419 } else {
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:
1426 #if 0
1427 /* After finished, cancel any pending operations. */
1428 abort_read();
1429 #else
1430 /* Start a timer to time out after a while to disable
1431 the read. */
1432 cdu31a_abort_timer.expires = jiffies + 2 * HZ; /* Wait 2 seconds */
1433 add_timer(&cdu31a_abort_timer);
1434 #endif
1436 up(&sony_sem);
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
1444 * successful.
1446 static void sony_get_toc(void)
1448 unsigned char res_reg[2];
1449 unsigned int res_size;
1450 unsigned char parms[1];
1451 int session;
1452 int num_spin_ups;
1453 int totaltracks = 0;
1454 int mint = 99;
1455 int maxt = 0;
1457 pr_debug(PFX "Entering %s\n", __FUNCTION__);
1459 num_spin_ups = 0;
1460 if (!sony_toc_read) {
1461 respinup_on_gettoc:
1462 /* Ignore the result, since it might error if spinning already. */
1463 do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg,
1464 &res_size);
1466 do_sony_cd_cmd(SONY_READ_TOC_CMD, NULL, 0, res_reg,
1467 &res_size);
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. */
1471 if ((res_size < 2)
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)) {
1483 num_spin_ups++;
1484 goto respinup_on_gettoc;
1487 printk("cdu31a: Error reading TOC: %x %s\n",
1488 res_reg[0], translate_error(res_reg[1]));
1489 return;
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.
1499 #if DEBUG
1500 memset(&sony_toc, 0x0e, sizeof(sony_toc));
1501 memset(&single_toc, 0x0f, sizeof(single_toc));
1502 #endif
1503 session = 1;
1504 while (1) {
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);
1509 parms[0] = 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]);
1515 if ((res_size < 2)
1516 || ((res_reg[0] & 0xf0) == 0x20)) {
1517 /* An error reading the TOC, this must be past the last session. */
1518 if (session == 1)
1519 printk
1520 ("Yikes! Couldn't read any sessions!");
1521 break;
1523 pr_debug(PFX "Reading session %d\n", session);
1525 parms[0] = session;
1526 do_sony_cd_cmd(SONY_REQ_TOC_DATA_SPEC_CMD,
1527 parms,
1529 (unsigned char *) &single_toc,
1530 &res_size);
1531 if ((res_size < 2)
1532 || ((single_toc.exec_status[0] & 0xf0) ==
1533 0x20)) {
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.
1538 exec_status[1]));
1539 /* An error reading the TOC. Return without sony_toc_read
1540 set. */
1541 return;
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,
1546 single_toc.point0,
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,
1552 single_toc.point1,
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,
1558 single_toc.point2,
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,
1567 single_toc.pointb0,
1568 bcd_to_int(single_toc.
1569 next_poss_prog_area_msf
1570 [0]),
1571 bcd_to_int(single_toc.
1572 next_poss_prog_area_msf
1573 [1]),
1574 bcd_to_int(single_toc.
1575 next_poss_prog_area_msf
1576 [2]),
1577 single_toc.num_mode_5_pointers,
1578 bcd_to_int(single_toc.
1579 max_start_outer_leadout_msf
1580 [0]),
1581 bcd_to_int(single_toc.
1582 max_start_outer_leadout_msf
1583 [1]),
1584 bcd_to_int(single_toc.
1585 max_start_outer_leadout_msf
1586 [2]));
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,
1591 single_toc.pointb1,
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,
1603 single_toc.pointb2,
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,
1615 single_toc.pointb3,
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,
1627 single_toc.pointb4,
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,
1639 single_toc.pointc0,
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]);
1647 #undef DEBUG
1648 #define DEBUG 0
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,
1665 res_size - 18);
1666 res_size += 9;
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
1671 [0]);
1672 sony_toc.lead_out_start_msf[1] =
1673 bcd_to_int(single_toc.
1674 max_start_outer_leadout_msf
1675 [1]);
1676 sony_toc.lead_out_start_msf[2] =
1677 bcd_to_int(single_toc.
1678 max_start_outer_leadout_msf
1679 [2]);
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,
1687 res_size - 27);
1688 res_size += 9;
1690 if (single_toc.pointb2 != 0xb2) {
1691 memmove(((char *) &single_toc) + 45,
1692 ((char *) &single_toc) + 36,
1693 res_size - 36);
1694 res_size += 9;
1696 if (single_toc.pointb3 != 0xb3) {
1697 memmove(((char *) &single_toc) + 54,
1698 ((char *) &single_toc) + 45,
1699 res_size - 45);
1700 res_size += 9;
1702 if (single_toc.pointb4 != 0xb4) {
1703 memmove(((char *) &single_toc) + 63,
1704 ((char *) &single_toc) + 54,
1705 res_size - 54);
1706 res_size += 9;
1708 if (single_toc.pointc0 != 0xc0) {
1709 memmove(((char *) &single_toc) + 72,
1710 ((char *) &single_toc) + 63,
1711 res_size - 63);
1712 res_size += 9;
1714 #if DEBUG
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);
1720 int i;
1721 for (i = 0;
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.
1733 tracks[i].track),
1734 bcd_to_int(single_toc.
1735 tracks[i].
1736 track_start_msf
1737 [0]),
1738 bcd_to_int(single_toc.
1739 tracks[i].
1740 track_start_msf
1741 [1]),
1742 bcd_to_int(single_toc.
1743 tracks[i].
1744 track_start_msf
1745 [2]));
1746 if (mint >
1747 bcd_to_int(single_toc.
1748 tracks[i].track))
1749 mint =
1750 bcd_to_int(single_toc.
1751 tracks[i].
1752 track);
1753 if (maxt <
1754 bcd_to_int(single_toc.
1755 tracks[i].track))
1756 maxt =
1757 bcd_to_int(single_toc.
1758 tracks[i].
1759 track);
1761 printk(KERN_INFO PFX "min track number %d, "
1762 "max track number %d\n",
1763 mint, maxt);
1765 #endif
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;
1780 mint = maxt = 1;
1781 totaltracks++;
1782 } else
1783 /* gather track entries from this session */
1785 int i;
1786 for (i = 0;
1789 bcd_to_int(single_toc.last_track_num)
1791 bcd_to_int(single_toc.
1792 first_track_num);
1793 i++, totaltracks++) {
1794 sony_toc.tracks[totaltracks].
1795 address =
1796 single_toc.tracks[i].address;
1797 sony_toc.tracks[totaltracks].
1798 control =
1799 single_toc.tracks[i].control;
1800 sony_toc.tracks[totaltracks].
1801 track =
1802 bcd_to_int(single_toc.
1803 tracks[i].track);
1804 sony_toc.tracks[totaltracks].
1805 track_start_msf[0] =
1806 bcd_to_int(single_toc.
1807 tracks[i].
1808 track_start_msf[0]);
1809 sony_toc.tracks[totaltracks].
1810 track_start_msf[1] =
1811 bcd_to_int(single_toc.
1812 tracks[i].
1813 track_start_msf[1]);
1814 sony_toc.tracks[totaltracks].
1815 track_start_msf[2] =
1816 bcd_to_int(single_toc.
1817 tracks[i].
1818 track_start_msf[2]);
1819 if (i == 0)
1820 single_toc.
1821 start_track_lba =
1822 msf_to_log(sony_toc.
1823 tracks
1824 [totaltracks].
1825 track_start_msf);
1826 if (mint >
1827 sony_toc.tracks[totaltracks].
1828 track)
1829 mint =
1830 sony_toc.
1831 tracks[totaltracks].
1832 track;
1833 if (maxt <
1834 sony_toc.tracks[totaltracks].
1835 track)
1836 maxt =
1837 sony_toc.
1838 tracks[totaltracks].
1839 track;
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
1849 data track ... */
1850 sony_toc.disk_type = single_toc.disk_type;
1851 sony_toc.sessions = session;
1853 /* don't believe everything :-) */
1854 if (session == 1)
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) {
1862 break;
1865 /* Let's not get carried away... */
1866 if (session > 40) {
1867 printk(KERN_NOTICE PFX "too many sessions: "
1868 "%d\n", session);
1869 break;
1871 session++;
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];
1885 sony_toc_read = 1;
1887 pr_debug(PFX "Disk session %d, start track: %d, "
1888 "stop track: %d\n",
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)
1904 return 1;
1906 if (!sony_toc_read) {
1907 if (down_interruptible(&sony_sem))
1908 return -ERESTARTSYS;
1909 sony_get_toc();
1910 up(&sony_sem);
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 */ ;
1918 return 0;
1922 * Search for a specific track in the table of contents.
1924 static int find_track(int track)
1926 int i;
1928 for (i = 0; i <= sony_toc.track_entries; i++) {
1929 if (sony_toc.tracks[i].track == track) {
1930 return i;
1934 return -1;
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,
1947 NULL,
1948 0, (unsigned char *) &last_sony_subcode, &res_size);
1949 if ((res_size < 2)
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]));
1953 return -EIO;
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]);
1973 return 0;
1977 * Uniform cdrom interface function
1978 * return the media catalog number found on some older audio cds
1980 static int
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);
1993 up(&sony_sem);
1994 if ((res_size < 2) || ((resbuffer[0] & 0xf0) == 0x20));
1995 else {
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';
2011 *mcnp = '\0';
2012 return 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());
2028 sony_get_toc();
2029 if (!sony_toc_read) {
2030 return -EIO;
2033 switch (sony_audio_status) {
2034 case CDROM_AUDIO_NO_STATUS:
2035 case CDROM_AUDIO_PLAY:
2036 if (read_subcode() < 0) {
2037 return -EIO;
2039 break;
2041 case CDROM_AUDIO_PAUSED:
2042 case CDROM_AUDIO_COMPLETED:
2043 break;
2045 #if 0
2046 case CDROM_AUDIO_NO_STATUS:
2047 schi->cdsc_audiostatus = sony_audio_status;
2048 return 0;
2049 break;
2050 #endif
2051 case CDROM_AUDIO_INVALID:
2052 case CDROM_AUDIO_ERROR:
2053 default:
2054 return -EIO;
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);
2083 return 0;
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. */
2090 static void
2091 read_audio_data(char *buffer, unsigned char res_reg[], int *res_size)
2093 unsigned long retry_count;
2094 int result_read;
2097 res_reg[0] = 0;
2098 res_reg[1] = 0;
2099 *res_size = 0;
2100 result_read = 0;
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());
2109 sony_sleep();
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) {
2117 result_read = 1;
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",
2124 res_reg[0]);
2125 res_reg[0] = 0x20;
2126 res_reg[1] = SONY_BAD_DATA_ERR;
2127 *res_size = 2;
2129 abort_read();
2130 } else {
2131 pr_debug(PFX "timeout out %d\n", __LINE__);
2132 res_reg[0] = 0x20;
2133 res_reg[1] = SONY_TIMEOUT_OP_ERR;
2134 *res_size = 2;
2135 abort_read();
2137 } else {
2138 clear_data_ready();
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,
2143 CD_FRAMESIZE_RAW1);
2144 } else {
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. */
2150 if (!result_read) {
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());
2157 sony_sleep();
2160 if (!is_result_ready()) {
2161 pr_debug(PFX "timeout out %d\n", __LINE__);
2162 res_reg[0] = 0x20;
2163 res_reg[1] = SONY_TIMEOUT_OP_ERR;
2164 *res_size = 2;
2165 abort_read();
2166 return;
2167 } else {
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. */
2178 } else {
2179 printk(KERN_ERR PFX "Data block error: 0x%x\n",
2180 res_reg[0]);
2181 res_reg[0] = 0x20;
2182 res_reg[1] = SONY_BAD_DATA_ERR;
2183 *res_size = 2;
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",
2189 res_reg[0]);
2190 restart_on_error();
2191 res_reg[0] = 0x20;
2192 res_reg[1] = SONY_BAD_DATA_ERR;
2193 *res_size = 2;
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)
2202 int retval;
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;
2210 if (!sony_spun_up)
2211 scd_spinup();
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",
2220 res_reg[1]);
2221 retval = -EIO;
2222 goto out_up;
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
2227 return. */
2229 retval = 0;
2230 if (start_request(ra->addr.lba, ra->nframes)) {
2231 retval = -EIO;
2232 goto exit_read_audio;
2235 /* For every requested frame. */
2236 cframe = 0;
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 "
2242 "sector %d\n",
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,
2253 params,
2254 2, res_reg, &res_size);
2255 if ((res_size < 2)
2256 || ((res_reg[0] & 0xf0) == 0x20)) {
2257 printk(KERN_ERR PFX "Unable to set "
2258 "decode params: 0x%2.2x\n",
2259 res_reg[1]);
2260 retval = -EIO;
2261 goto exit_read_audio;
2264 /* Restart the request on the current frame. */
2265 if (start_request
2266 (ra->addr.lba + cframe,
2267 ra->nframes - cframe)) {
2268 retval = -EIO;
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,
2276 &res_size);
2277 if ((res_reg[0] & 0xf0) == 0x20) {
2278 if (res_reg[1] ==
2279 SONY_BAD_DATA_ERR) {
2280 printk(KERN_ERR PFX "Data error"
2281 " on audio sector %d\n",
2282 ra->addr.lba +
2283 cframe);
2284 } else {
2285 printk(KERN_ERR PFX "Error reading audio data on sector %d: %s\n",
2286 ra->addr.lba + cframe,
2287 translate_error
2288 (res_reg[1]));
2289 retval = -EIO;
2290 goto exit_read_audio;
2292 } else if (copy_to_user(ra->buf +
2293 (CD_FRAMESIZE_RAW
2294 * cframe),
2295 audio_buffer,
2296 CD_FRAMESIZE_RAW)) {
2297 retval = -EFAULT;
2298 goto exit_read_audio;
2300 } else {
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]));
2305 retval = -EIO;
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)) {
2311 retval = -EFAULT;
2312 goto exit_read_audio;
2315 cframe++;
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]));
2322 retval = -EIO;
2323 goto exit_read_audio;
2326 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) {
2331 params[1] = 0x0f;
2332 } else {
2333 params[1] = 0x07;
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",
2339 res_reg[1]);
2340 retval = -EIO;
2343 out_up:
2344 up(&sony_sem);
2346 return retval;
2349 static int
2350 do_sony_cd_cmd_chk(const char *name,
2351 unsigned char cmd,
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,
2357 result_size);
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);
2361 return -EIO;
2363 return 0;
2367 * Uniform cdrom interface function
2368 * open the tray
2370 static int scd_tray_move(struct cdrom_device_info *cdi, int position)
2372 int retval;
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,
2381 &res_size);
2382 do_sony_cd_cmd(SONY_SPIN_DOWN_CMD, NULL, 0, res_reg,
2383 &res_size);
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);
2388 } else {
2389 if (0 == scd_spinup())
2390 sony_spun_up = 1;
2391 retval = 0;
2393 up(&sony_sem);
2394 return retval;
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];
2406 int i, retval;
2408 if (down_interruptible(&sony_sem))
2409 return -ERESTARTSYS;
2410 switch (cmd) {
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);
2414 break;
2416 case CDROMSTOP: /* Spin down the drive */
2417 do_sony_cd_cmd(SONY_AUDIO_STOP_CMD, NULL, 0, res_reg,
2418 &res_size);
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);
2427 break;
2429 case CDROMPAUSE: /* Pause the drive */
2430 if (do_sony_cd_cmd_chk
2431 ("PAUSE", SONY_AUDIO_STOP_CMD, NULL, 0, res_reg,
2432 &res_size)) {
2433 retval = -EIO;
2434 break;
2436 /* Get the current position and save it for resuming */
2437 if (read_subcode() < 0) {
2438 retval = -EIO;
2439 break;
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;
2445 retval = 0;
2446 break;
2448 case CDROMRESUME: /* Start the drive after being paused */
2449 if (sony_audio_status != CDROM_AUDIO_PAUSED) {
2450 retval = -EINVAL;
2451 break;
2454 do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg,
2455 &res_size);
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]);
2464 params[0] = 0x03;
2465 if (do_sony_cd_cmd_chk
2466 ("RESUME", SONY_AUDIO_PLAYBACK_CMD, params, 7, res_reg,
2467 &res_size) < 0) {
2468 retval = -EIO;
2469 break;
2471 sony_audio_status = CDROM_AUDIO_PLAY;
2472 retval = 0;
2473 break;
2475 case CDROMPLAYMSF: /* Play starting at the given MSF address. */
2476 do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg,
2477 &res_size);
2479 /* The parameters are given in int, must be converted */
2480 for (i = 1; i < 7; i++) {
2481 params[i] =
2482 int_to_bcd(((unsigned char *) arg)[i - 1]);
2484 params[0] = 0x03;
2485 if (do_sony_cd_cmd_chk
2486 ("PLAYMSF", SONY_AUDIO_PLAYBACK_CMD, params, 7,
2487 res_reg, &res_size) < 0) {
2488 retval = -EIO;
2489 break;
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;
2497 retval = 0;
2498 break;
2500 case CDROMREADTOCHDR: /* Read the table of contents header */
2502 struct cdrom_tochdr *hdr;
2504 sony_get_toc();
2505 if (!sony_toc_read) {
2506 retval = -EIO;
2507 break;
2510 hdr = (struct cdrom_tochdr *) arg;
2511 hdr->cdth_trk0 = sony_toc.first_track_num;
2512 hdr->cdth_trk1 = sony_toc.last_track_num;
2514 retval = 0;
2515 break;
2517 case CDROMREADTOCENTRY: /* Read a given table of contents entry */
2519 struct cdrom_tocentry *entry;
2520 int track_idx;
2521 unsigned char *msf_val = NULL;
2523 sony_get_toc();
2524 if (!sony_toc_read) {
2525 retval = -EIO;
2526 break;
2529 entry = (struct cdrom_tocentry *) arg;
2531 track_idx = find_track(entry->cdte_track);
2532 if (track_idx < 0) {
2533 retval = -EINVAL;
2534 break;
2537 entry->cdte_adr =
2538 sony_toc.tracks[track_idx].address;
2539 entry->cdte_ctrl =
2540 sony_toc.tracks[track_idx].control;
2541 msf_val =
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 =
2550 *(msf_val + 1);
2551 entry->cdte_addr.msf.frame =
2552 *(msf_val + 2);
2555 retval = 0;
2556 break;
2558 case CDROMPLAYTRKIND: /* Play a track. This currently ignores index. */
2560 struct cdrom_ti *ti = (struct cdrom_ti *) arg;
2561 int track_idx;
2563 sony_get_toc();
2564 if (!sony_toc_read) {
2565 retval = -EIO;
2566 break;
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)) {
2572 retval = -EINVAL;
2573 break;
2576 track_idx = find_track(ti->cdti_trk0);
2577 if (track_idx < 0) {
2578 retval = -EINVAL;
2579 break;
2581 params[1] =
2582 int_to_bcd(sony_toc.tracks[track_idx].
2583 track_start_msf[0]);
2584 params[2] =
2585 int_to_bcd(sony_toc.tracks[track_idx].
2586 track_start_msf[1]);
2587 params[3] =
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
2593 * MSF to do that.
2595 if (ti->cdti_trk1 >= sony_toc.last_track_num) {
2596 track_idx = find_track(CDROM_LEADOUT);
2597 } else {
2598 track_idx = find_track(ti->cdti_trk1 + 1);
2600 if (track_idx < 0) {
2601 retval = -EINVAL;
2602 break;
2604 params[4] =
2605 int_to_bcd(sony_toc.tracks[track_idx].
2606 track_start_msf[0]);
2607 params[5] =
2608 int_to_bcd(sony_toc.tracks[track_idx].
2609 track_start_msf[1]);
2610 params[6] =
2611 int_to_bcd(sony_toc.tracks[track_idx].
2612 track_start_msf[2]);
2613 params[0] = 0x03;
2615 do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg,
2616 &res_size);
2618 do_sony_cd_cmd(SONY_AUDIO_PLAYBACK_CMD, params, 7,
2619 res_reg, &res_size);
2621 if ((res_size < 2)
2622 || ((res_reg[0] & 0xf0) == 0x20)) {
2623 printk(KERN_ERR PFX
2624 "Params: %x %x %x %x %x %x %x\n",
2625 params[0], params[1], params[2],
2626 params[3], params[4], params[5],
2627 params[6]);
2628 printk(KERN_ERR PFX
2629 "Error %s (CDROMPLAYTRKIND)\n",
2630 translate_error(res_reg[1]));
2631 retval = -EIO;
2632 break;
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;
2640 retval = 0;
2641 break;
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,
2654 params, 3, res_reg,
2655 &res_size);
2656 break;
2658 case CDROMSUBCHNL: /* Get subchannel info */
2659 retval = sony_get_subchnl_info((struct cdrom_subchnl *) arg);
2660 break;
2662 default:
2663 retval = -EINVAL;
2664 break;
2666 up(&sony_sem);
2667 return retval;
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;
2674 int retval;
2676 if (down_interruptible(&sony_sem))
2677 return -ERESTARTSYS;
2678 switch (cmd) {
2679 case CDROMREADAUDIO: /* Read 2352 byte audio tracks and 2340 byte
2680 raw data tracks. */
2682 struct cdrom_read_audio ra;
2685 sony_get_toc();
2686 if (!sony_toc_read) {
2687 retval = -EIO;
2688 break;
2691 if (copy_from_user(&ra, argp, sizeof(ra))) {
2692 retval = -EFAULT;
2693 break;
2696 if (ra.nframes == 0) {
2697 retval = 0;
2698 break;
2701 if (!access_ok(VERIFY_WRITE, ra.buf,
2702 CD_FRAMESIZE_RAW * ra.nframes))
2703 return -EFAULT;
2705 if (ra.addr_format == CDROM_LBA) {
2706 if ((ra.addr.lba >=
2707 sony_toc.lead_out_start_lba)
2708 || (ra.addr.lba + ra.nframes >=
2709 sony_toc.lead_out_start_lba)) {
2710 retval = -EINVAL;
2711 break;
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)) {
2717 retval = -EINVAL;
2718 break;
2721 ra.addr.lba = ((ra.addr.msf.minute * 4500)
2722 + (ra.addr.msf.second * 75)
2723 + ra.addr.msf.frame);
2724 if ((ra.addr.lba >=
2725 sony_toc.lead_out_start_lba)
2726 || (ra.addr.lba + ra.nframes >=
2727 sony_toc.lead_out_start_lba)) {
2728 retval = -EINVAL;
2729 break;
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;
2736 } else {
2737 retval = -EINVAL;
2738 break;
2741 retval = read_audio(&ra);
2742 break;
2744 retval = 0;
2745 break;
2747 default:
2748 retval = -EINVAL;
2750 up(&sony_sem);
2751 return retval;
2754 static int scd_spinup(void)
2756 unsigned char res_reg[12];
2757 unsigned int res_size;
2758 int num_spin_ups;
2760 num_spin_ups = 0;
2762 respinup_on_open:
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]));
2770 return 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)) {
2781 return 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)) {
2788 num_spin_ups++;
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,
2795 &res_size);
2796 return 1;
2798 return 0;
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];
2811 if (purpose == 1) {
2812 /* Open for IOCTLs only - no media check */
2813 sony_usage++;
2814 return 0;
2817 if (sony_usage == 0) {
2818 if (scd_spinup() != 0)
2819 return -EIO;
2820 sony_get_toc();
2821 if (!sony_toc_read) {
2822 do_sony_cd_cmd(SONY_SPIN_DOWN_CMD, NULL, 0,
2823 res_reg, &res_size);
2824 return -EIO;
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;
2833 params[1] = 0x07;
2834 do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
2835 params, 2, res_reg, &res_size);
2836 if ((res_size < 2)
2837 || ((res_reg[0] & 0xf0) == 0x20)) {
2838 printk(KERN_WARNING PFX "Unable to set "
2839 "XA params: 0x%2.2x\n", res_reg[1]);
2841 sony_xa_mode = 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;
2846 params[1] = 0x0f;
2847 do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
2848 params, 2, res_reg, &res_size);
2849 if ((res_size < 2)
2850 || ((res_reg[0] & 0xf0) == 0x20)) {
2851 printk(KERN_WARNING PFX "Unable to reset "
2852 "XA params: 0x%2.2x\n", res_reg[1]);
2854 sony_xa_mode = 0;
2857 sony_spun_up = 1;
2860 sony_usage++;
2862 return 0;
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,
2877 &res_size);
2879 sony_spun_up = 0;
2881 sony_usage--;
2884 static struct cdrom_device_ops scd_dops = {
2885 .open = scd_open,
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,
2894 .reset = scd_reset,
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,
2900 .n_minors = 1,
2903 static struct cdrom_device_info scd_info = {
2904 .ops = &scd_dops,
2905 .speed = 2,
2906 .capacity = 1,
2907 .name = "cdu31a"
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)
2923 int retval;
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.
2929 switch (cmd) {
2930 case CDROMEJECT:
2931 scd_lock_door(&scd_info, 0);
2932 retval = scd_tray_move(&scd_info, 1);
2933 break;
2934 case CDROMCLOSETRAY:
2935 retval = scd_tray_move(&scd_info, 0);
2936 break;
2937 case CDROMREADAUDIO:
2938 retval = scd_read_audio(&scd_info, CDROMREADAUDIO, arg);
2939 break;
2940 default:
2941 retval = cdrom_ioctl(file, &scd_info, inode, cmd, arg);
2943 return retval;
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" };
2966 static int __init
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"))
2974 return 0;
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
2992 * ok for me.
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.
2999 reset_drive();
3000 retry_count = jiffies + SONY_RESET_TIMEOUT;
3001 while (time_before(jiffies, retry_count)
3002 && (!is_attention())) {
3003 sony_sleep();
3006 #if 0
3007 /* If attention is never seen probably not a CDU31a present */
3008 if (!is_attention()) {
3009 res_reg[0] = 0x20;
3010 goto out_err;
3012 #endif
3015 * Get the drive configuration.
3017 do_sony_cd_cmd(SONY_REQ_DRIVE_CONFIG_CMD,
3018 NULL,
3019 0, (unsigned char *) res_reg, res_size);
3020 if (*res_size <= 2 || (res_reg[0] & 0xf0) != 0)
3021 goto out_err;
3022 return 1;
3025 /* Return an error */
3026 res_reg[0] = 0x20;
3027 out_err:
3028 release_region(cdu31a_port, 4);
3029 cdu31a_port = 0;
3030 return 0;
3033 #ifndef MODULE
3035 * Set up base I/O and interrupts, called from main.c.
3038 static int __init cdu31a_setup(char *strings)
3040 int ints[4];
3042 (void) get_options(strings, ARRAY_SIZE(ints), ints);
3044 if (ints[0] > 0) {
3045 cdu31a_port = ints[1];
3047 if (ints[0] > 1) {
3048 cdu31a_irq = ints[2];
3050 if ((strings != NULL) && (*strings != '\0')) {
3051 if (strcmp(strings, "PAS") == 0) {
3052 sony_pas_init = 1;
3053 } else {
3054 printk(KERN_NOTICE PFX "Unknown interface type: %s\n",
3055 strings);
3059 return 1;
3062 __setup("cdu31a=", cdu31a_setup);
3064 #endif
3067 * Initialize the driver.
3069 int __init cdu31a_init(void)
3071 struct s_sony_drive_config drive_config;
3072 struct gendisk *disk;
3073 int deficiency = 0;
3074 unsigned int res_size;
3075 char msg[255];
3076 char buf[40];
3077 int i;
3078 int tmp_irq;
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) {
3088 outb(0xbc, 0x9a01);
3089 outb(0xe2, 0x9a01);
3092 /* Setting the base I/O address to 0xffff will disable it. */
3093 if (cdu31a_port == 0xffff)
3094 goto errout3;
3096 if (cdu31a_port != 0) {
3097 /* Need IRQ 0 because we can't sleep here. */
3098 tmp_irq = cdu31a_irq;
3099 cdu31a_irq = 0;
3100 if (!get_drive_configuration(cdu31a_port,
3101 drive_config.exec_status,
3102 &res_size))
3103 goto errout3;
3104 cdu31a_irq = tmp_irq;
3105 } else {
3106 cdu31a_irq = 0;
3107 for (i = 0; cdu31a_addresses[i].base; i++) {
3108 if (get_drive_configuration(cdu31a_addresses[i].base,
3109 drive_config.exec_status,
3110 &res_size)) {
3111 cdu31a_irq = cdu31a_addresses[i].int_num;
3112 break;
3115 if (!cdu31a_port)
3116 goto errout3;
3119 if (register_blkdev(MAJOR_NR, "cdu31a"))
3120 goto errout2;
3122 disk = alloc_disk(1);
3123 if (!disk)
3124 goto errout1;
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. */
3135 cdu31a_irq = 0;
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) {
3143 if (request_irq
3144 (cdu31a_irq, cdu31a_interrupt, IRQF_DISABLED,
3145 "cdu31a", NULL)) {
3146 printk(KERN_WARNING PFX "Unable to grab IRQ%d for "
3147 "the CDU31A driver\n", cdu31a_irq);
3148 cdu31a_irq = 0;
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)]);
3158 strcat(msg, buf);
3159 if (SONY_HWC_AUDIO_PLAYBACK(drive_config))
3160 strcat(msg, ", audio");
3161 else
3162 deficiency |= CDC_PLAY_AUDIO;
3163 if (SONY_HWC_EJECT(drive_config))
3164 strcat(msg, ", eject");
3165 else
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");
3175 else
3176 deficiency |= CDC_SELECT_SPEED;
3177 if (cdu31a_irq > 0) {
3178 sprintf(buf, ", irq %d", cdu31a_irq);
3179 strcat(msg, buf);
3181 strcat(msg, "\n");
3182 printk(KERN_INFO PFX "%s",msg);
3184 cdu31a_queue = blk_init_queue(do_cdu31a_request, &cdu31a_lock);
3185 if (!cdu31a_queue)
3186 goto errout0;
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;
3193 scd_gendisk = disk;
3194 if (register_cdrom(&scd_info))
3195 goto err;
3196 disk->queue = cdu31a_queue;
3197 add_disk(disk);
3199 disk_changed = 1;
3200 return 0;
3202 err:
3203 blk_cleanup_queue(cdu31a_queue);
3204 errout0:
3205 if (cdu31a_irq)
3206 free_irq(cdu31a_irq, NULL);
3207 printk(KERN_ERR PFX "Unable to register with Uniform cdrom driver\n");
3208 put_disk(disk);
3209 errout1:
3210 if (unregister_blkdev(MAJOR_NR, "cdu31a")) {
3211 printk(KERN_WARNING PFX "Can't unregister block device\n");
3213 errout2:
3214 release_region(cdu31a_port, 4);
3215 errout3:
3216 return -EIO;
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 "
3226 "cdrom driver\n");
3227 return;
3229 if ((unregister_blkdev(MAJOR_NR, "cdu31a") == -EINVAL)) {
3230 printk(KERN_WARNING PFX "Can't unregister\n");
3231 return;
3234 blk_cleanup_queue(cdu31a_queue);
3236 if (cdu31a_irq > 0)
3237 free_irq(cdu31a_irq, NULL);
3239 release_region(cdu31a_port, 4);
3240 printk(KERN_INFO PFX "module released.\n");
3243 #ifdef MODULE
3244 module_init(cdu31a_init);
3245 #endif
3246 module_exit(cdu31a_exit);
3248 MODULE_LICENSE("GPL");
3249 MODULE_ALIAS_BLOCKDEV_MAJOR(CDU31A_CDROM_MAJOR);