2 * Sony CDU-31A CDROM interface device driver.
4 * Corey Minyard (minyard@wf-rch.cirr.com)
8 * See Documentation/cdrom/cdu31a for additional details about this driver.
10 * The Sony interface device driver handles Sony interface CDROM
11 * drives and provides a complete block-level interface as well as an
12 * ioctl() interface compatible with the Sun (as specified in
13 * include/linux/cdrom.h). With this interface, CDROMs can be
14 * accessed and standard audio CDs can be played back normally.
16 * WARNING - All autoprobes have been removed from the driver.
17 * You MUST configure the CDU31A via a LILO config
18 * at boot time or in lilo.conf. I have the
19 * following in my lilo.conf:
21 * append="cdu31a=0x1f88,0,PAS"
23 * The first number is the I/O base address of the
24 * card. The second is the interrupt (0 means none).
25 * The third should be "PAS" if on a Pro-Audio
26 * spectrum, or nothing if on something else.
28 * This interface is (unfortunately) a polled interface. This is
29 * because most Sony interfaces are set up with DMA and interrupts
30 * disables. Some (like mine) do not even have the capability to
31 * handle interrupts or DMA. For this reason you will see a lot of
34 * retry_count = jiffies+ SONY_JIFFIES_TIMEOUT;
35 * while (time_before(jiffies, retry_count) && (! <some condition to wait for))
37 * while (handle_sony_cd_attention())
42 * if (the condition not met)
47 * This ugly hack waits for something to happen, sleeping a little
48 * between every try. it also handles attentions, which are
49 * asynchronous events from the drive informing the driver that a disk
50 * has been inserted, removed, etc.
52 * NEWS FLASH - The driver now supports interrupts but they are
53 * turned off by default. Use of interrupts is highly encouraged, it
54 * cuts CPU usage down to a reasonable level. I had DMA in for a while
55 * but PC DMA is just too slow. Better to just insb() it.
57 * One thing about these drives: They talk in MSF (Minute Second Frame) format.
58 * There are 75 frames a second, 60 seconds a minute, and up to 75 minutes on a
59 * disk. The funny thing is that these are sent to the drive in BCD, but the
60 * interface wants to see them in decimal. A lot of conversion goes on.
62 * DRIVER SPECIAL FEATURES
63 * -----------------------
65 * This section describes features beyond the normal audio and CD-ROM
66 * functions of the drive.
68 * 2048 byte buffer mode
70 * If a disk is mounted with -o block=2048, data is copied straight
71 * from the drive data port to the buffer. Otherwise, the readahead
72 * buffer must be involved to hold the other 1K of data when a 1K
73 * block operation is done. Note that with 2048 byte blocks you
74 * cannot execute files from the CD.
78 * The driver should support XA disks for both the CDU31A and CDU33A.
79 * It does this transparently, the using program doesn't need to set it.
83 * A multi-session disk looks just like a normal disk to the user.
84 * Just mount one normally, and all the data should be there.
85 * A special thanks to Koen for help with this!
89 * Using the CDROMREADAUDIO it is possible to read raw audio and data
90 * tracks. Both operations return 2352 bytes per sector. On the data
91 * tracks, the first 12 bytes is not returned by the drive and the value
92 * of that data is indeterminate.
95 * Copyright (C) 1993 Corey Minyard
97 * This program is free software; you can redistribute it and/or modify
98 * it under the terms of the GNU General Public License as published by
99 * the Free Software Foundation; either version 2 of the License, or
100 * (at your option) any later version.
102 * This program is distributed in the hope that it will be useful,
103 * but WITHOUT ANY WARRANTY; without even the implied warranty of
104 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
105 * GNU General Public License for more details.
107 * You should have received a copy of the GNU General Public License
108 * along with this program; if not, write to the Free Software
109 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
112 * CDs with form1 and form2 sectors cause problems
113 * with current read-ahead strategy.
116 * Heiko Eissfeldt <heiko@colossus.escape.de>
117 * For finding abug in the return of the track numbers.
118 * TOC processing redone for proper multisession support.
121 * It probably a little late to be adding a history, but I guess I
124 * 10/24/95 - Added support for disabling the eject button when the
125 * drive is open. Note that there is a small problem
126 * still here, if the eject button is pushed while the
127 * drive light is flashing, the drive will return a bad
128 * status and be reset. It recovers, though.
130 * 03/07/97 - Fixed a problem with timers.
133 * 18 Spetember 1997 -- Ported to Uniform CD-ROM driver by
134 * Heiko Eissfeldt <heiko@colossus.escape.de> with additional
135 * changes by Erik Andersen <andersee@debian.org>
137 * 24 January 1998 -- Removed the scd_disc_status() function, which was now
138 * just dead code left over from the port.
139 * Erik Andersen <andersee@debian.org>
141 * 16 July 1998 -- Drive donated to Erik Andersen by John Kodis
142 * <kodis@jagunet.com>. Work begun on fixing driver to
143 * work under 2.1.X. Added temporary extra printks
144 * which seem to slow it down enough to work.
146 * 9 November 1999 -- Make kernel-parameter implementation work with 2.3.x
147 * Removed init_module & cleanup_module in favor of
148 * module_init & module_exit.
149 * Torben Mathiasen <tmm@image.dk>
152 #include <linux/major.h>
154 #include <linux/module.h>
156 #include <linux/errno.h>
157 #include <linux/signal.h>
158 #include <linux/sched.h>
159 #include <linux/timer.h>
160 #include <linux/fs.h>
161 #include <linux/kernel.h>
162 #include <linux/hdreg.h>
163 #include <linux/genhd.h>
164 #include <linux/ioport.h>
165 #include <linux/devfs_fs_kernel.h>
166 #include <linux/string.h>
167 #include <linux/slab.h>
168 #include <linux/init.h>
169 #include <linux/interrupt.h>
171 #include <asm/system.h>
173 #include <asm/uaccess.h>
176 #include <linux/cdrom.h>
179 #define MAJOR_NR CDU31A_CDROM_MAJOR
180 #include <linux/blk.h>
182 #define CDU31A_READAHEAD 4 /* 128 sector, 64kB, 32 reads read-ahead */
183 #define CDU31A_MAX_CONSECUTIVE_ATTENTIONS 10
187 /* Define the following if you have data corruption problems. */
188 #undef SONY_POLL_EACH_BYTE
191 ** Edit the following data to change interrupts, DMA channels, etc.
192 ** Default is polled and no DMA. DMA is not recommended for double-speed
196 unsigned short base; /* I/O Base Address */
197 short int_num; /* Interrupt Number (-1 means scan for it,
198 0 means don't use) */
199 } cdu31a_addresses[] __initdata = {
203 static int handle_sony_cd_attention(void);
204 static int read_subcode(void);
205 static void sony_get_toc(void);
206 static int scd_spinup(void);
207 /*static int scd_open(struct inode *inode, struct file *filp);*/
208 static int scd_open(struct cdrom_device_info *, int);
209 static void do_sony_cd_cmd(unsigned char cmd,
210 unsigned char *params,
211 unsigned int num_params,
212 unsigned char *result_buffer,
213 unsigned int *result_size);
214 static void size_to_buf(unsigned int size, unsigned char *buf);
216 /* Parameters for the read-ahead. */
217 static unsigned int sony_next_block; /* Next 512 byte block offset */
218 static unsigned int sony_blocks_left = 0; /* Number of 512 byte blocks left
219 in the current read command. */
222 /* The base I/O address of the Sony Interface. This is a variable (not a
223 #define) so it can be easily changed via some future ioctl() */
224 static unsigned int cdu31a_port = 0;
225 MODULE_PARM(cdu31a_port, "i");
228 * The following are I/O addresses of the various registers for the drive. The
229 * comment for the base address also applies here.
231 static volatile unsigned short sony_cd_cmd_reg;
232 static volatile unsigned short sony_cd_param_reg;
233 static volatile unsigned short sony_cd_write_reg;
234 static volatile unsigned short sony_cd_control_reg;
235 static volatile unsigned short sony_cd_status_reg;
236 static volatile unsigned short sony_cd_result_reg;
237 static volatile unsigned short sony_cd_read_reg;
238 static volatile unsigned short sony_cd_fifost_reg;
241 static int sony_spun_up = 0; /* Has the drive been spun up? */
243 static int sony_speed = 0; /* Last wanted speed */
245 static int sony_xa_mode = 0; /* Is an XA disk in the drive
246 and the drive a CDU31A? */
248 static int sony_raw_data_mode = 1; /* 1 if data tracks, 0 if audio.
249 For raw data reads. */
251 static unsigned int sony_usage = 0; /* How many processes have the
254 static int sony_pas_init = 0; /* Initialize the Pro-Audio
257 static struct s_sony_session_toc single_toc; /* Holds the
261 static struct s_all_sessions_toc sony_toc; /* entries gathered from all
264 static int sony_toc_read = 0; /* Has the TOC been read for
267 static struct s_sony_subcode last_sony_subcode; /* Points to the last
268 subcode address read */
270 static volatile int sony_inuse = 0; /* Is the drive in use? Only one operation
273 static DECLARE_WAIT_QUEUE_HEAD(sony_wait); /* Things waiting for the drive */
275 static struct task_struct *has_cd_task = NULL; /* The task that is currently
276 using the CDROM drive, or
279 static int is_double_speed = 0; /* does the drive support double speed ? */
280 static int is_a_cdu31a = 1; /* Is the drive a CDU31A? */
282 static int is_auto_eject = 1; /* Door has been locked? 1=No/0=Yes */
285 * The audio status uses the values from read subchannel data as specified
286 * in include/linux/cdrom.h.
288 static volatile int sony_audio_status = CDROM_AUDIO_NO_STATUS;
291 * The following are a hack for pausing and resuming audio play. The drive
292 * does not work as I would expect it, if you stop it then start it again,
293 * the drive seeks back to the beginning and starts over. This holds the
294 * position during a pause so a resume can restart it. It uses the
295 * audio status variable above to tell if it is paused.
297 static unsigned volatile char cur_pos_msf[3] = { 0, 0, 0 };
298 static unsigned volatile char final_pos_msf[3] = { 0, 0, 0 };
300 /* What IRQ is the drive using? 0 if none. */
301 static int cdu31a_irq = 0;
302 MODULE_PARM(cdu31a_irq, "i");
304 /* The interrupt handler will wake this queue up when it gets an
306 DECLARE_WAIT_QUEUE_HEAD(cdu31a_irq_wait);
308 static int curr_control_reg = 0; /* Current value of the control register */
310 /* A disk changed variable. When a disk change is detected, it will
311 all be set to TRUE. As the upper layers ask for disk_changed status
312 it will be cleared. */
313 static char disk_changed;
315 /* Variable for using the readahead buffer. The readahead buffer
316 is used for raw sector reads and for blocksizes that are smaller
318 static char readahead_buffer[CD_FRAMESIZE_RAW];
319 static int readahead_dataleft = 0;
320 static int readahead_bad = 0;
322 /* Used to time a short period to abort an operation after the
323 drive has been idle for a while. This keeps the light on
324 the drive from flashing for very long. */
325 static struct timer_list cdu31a_abort_timer;
327 /* Marks if the timeout has started an abort read. This is used
328 on entry to the drive to tell the code to read out the status
329 from the abort read. */
330 static int abort_read_started = 0;
334 * This routine returns 1 if the disk has been changed since the last
335 * check or 0 if it hasn't.
337 static int scd_disk_change(kdev_t full_dev)
341 retval = disk_changed;
348 * Uniform cdrom interface function
349 * report back, if disc has changed from time of last request.
351 static int scd_media_changed(struct cdrom_device_info *cdi, int disc_nr)
353 return scd_disk_change(cdi->dev);
357 * Uniform cdrom interface function
358 * report back, if drive is ready
360 static int scd_drive_status(struct cdrom_device_info *cdi, int slot_nr)
362 if (CDSL_CURRENT != slot_nr) {
363 /* we have no changer support */
366 if (scd_spinup() == 0) {
369 return sony_spun_up ? CDS_DISC_OK : CDS_DRIVE_NOT_READY;
372 static inline void enable_interrupts(void)
374 curr_control_reg |= (SONY_ATTN_INT_EN_BIT
375 | SONY_RES_RDY_INT_EN_BIT
376 | SONY_DATA_RDY_INT_EN_BIT);
377 outb(curr_control_reg, sony_cd_control_reg);
380 static inline void disable_interrupts(void)
382 curr_control_reg &= ~(SONY_ATTN_INT_EN_BIT
383 | SONY_RES_RDY_INT_EN_BIT
384 | SONY_DATA_RDY_INT_EN_BIT);
385 outb(curr_control_reg, sony_cd_control_reg);
389 * Wait a little while (used for polling the drive). If in initialization,
390 * setting a timeout doesn't work, so just loop for a while.
392 static inline void sony_sleep(void)
396 if (cdu31a_irq <= 0) {
397 current->state = TASK_INTERRUPTIBLE;
399 } else { /* Interrupt driven */
404 interruptible_sleep_on(&cdu31a_irq_wait);
405 restore_flags(flags);
411 * The following are convenience routine to read various status and set
412 * various conditions in the drive.
414 static inline int is_attention(void)
416 return ((inb(sony_cd_status_reg) & SONY_ATTN_BIT) != 0);
419 static inline int is_busy(void)
421 return ((inb(sony_cd_status_reg) & SONY_BUSY_BIT) != 0);
424 static inline int is_data_ready(void)
426 return ((inb(sony_cd_status_reg) & SONY_DATA_RDY_BIT) != 0);
429 static inline int is_data_requested(void)
431 return ((inb(sony_cd_status_reg) & SONY_DATA_REQUEST_BIT) != 0);
434 static inline int is_result_ready(void)
436 return ((inb(sony_cd_status_reg) & SONY_RES_RDY_BIT) != 0);
439 static inline int is_param_write_rdy(void)
441 return ((inb(sony_cd_fifost_reg) & SONY_PARAM_WRITE_RDY_BIT) != 0);
444 static inline int is_result_reg_not_empty(void)
446 return ((inb(sony_cd_fifost_reg) & SONY_RES_REG_NOT_EMP_BIT) != 0);
449 static inline void reset_drive(void)
451 curr_control_reg = 0;
452 readahead_dataleft = 0;
454 outb(SONY_DRIVE_RESET_BIT, sony_cd_control_reg);
458 * Uniform cdrom interface function
459 * reset drive and return when it is ready
461 static int scd_reset(struct cdrom_device_info *cdi)
467 retry_count = jiffies + SONY_RESET_TIMEOUT;
468 while (time_before(jiffies, retry_count) && (!is_attention())) {
475 static inline void clear_attention(void)
477 outb(curr_control_reg | SONY_ATTN_CLR_BIT, sony_cd_control_reg);
480 static inline void clear_result_ready(void)
482 outb(curr_control_reg | SONY_RES_RDY_CLR_BIT, sony_cd_control_reg);
485 static inline void clear_data_ready(void)
487 outb(curr_control_reg | SONY_DATA_RDY_CLR_BIT,
488 sony_cd_control_reg);
491 static inline void clear_param_reg(void)
493 outb(curr_control_reg | SONY_PARAM_CLR_BIT, sony_cd_control_reg);
496 static inline unsigned char read_status_register(void)
498 return (inb(sony_cd_status_reg));
501 static inline unsigned char read_result_register(void)
503 return (inb(sony_cd_result_reg));
506 static inline unsigned char read_data_register(void)
508 return (inb(sony_cd_read_reg));
511 static inline void write_param(unsigned char param)
513 outb(param, sony_cd_param_reg);
516 static inline void write_cmd(unsigned char cmd)
518 outb(curr_control_reg | SONY_RES_RDY_INT_EN_BIT,
519 sony_cd_control_reg);
520 outb(cmd, sony_cd_cmd_reg);
523 static void cdu31a_interrupt(int irq, void *dev_id, struct pt_regs *regs)
527 if (abort_read_started) {
528 /* We might be waiting for an abort to finish. Don't
529 disable interrupts yet, though, because we handle
531 /* Clear out the result registers. */
532 while (is_result_reg_not_empty()) {
533 val = read_result_register();
536 clear_result_ready();
538 /* Clear out the data */
539 while (is_data_requested()) {
540 val = read_data_register();
542 abort_read_started = 0;
544 /* If something was waiting, wake it up now. */
545 if (waitqueue_active(&cdu31a_irq_wait)) {
546 disable_interrupts();
547 wake_up(&cdu31a_irq_wait);
549 } else if (waitqueue_active(&cdu31a_irq_wait)) {
550 disable_interrupts();
551 wake_up(&cdu31a_irq_wait);
553 disable_interrupts();
555 ("CDU31A: Got an interrupt but nothing was waiting\n");
560 * give more verbose error messages
562 static unsigned char *translate_error(unsigned char err_code)
564 static unsigned char errbuf[80];
568 return "illegal command ";
570 return "illegal parameter ";
573 return "not loaded ";
577 return "not spinning ";
581 return "spindle servo ";
583 return "focus servo ";
585 return "eject mechanism ";
587 return "audio playing ";
589 return "emergency eject ";
594 return "frame sync ";
596 return "subcode address ";
598 return "block sync ";
600 return "header address ";
603 return "illegal track read ";
605 return "mode 0 read ";
607 return "illegal mode read ";
609 return "illegal block size read ";
615 return "leadout read ";
617 return "buffer overrun ";
620 return "unrecoverable CIRC ";
622 return "unrecoverable LECC ";
627 return "invalid subcode data ";
629 return "focus on TOC read ";
631 return "frame sync on TOC read ";
636 return "hardware failure ";
642 return "data track ";
644 sprintf(errbuf, "unknown 0x%02x ", err_code);
649 * Set the drive parameters so the drive will auto-spin-up when a
652 static void set_drive_params(int want_doublespeed)
654 unsigned char res_reg[12];
655 unsigned int res_size;
656 unsigned char params[3];
659 params[0] = SONY_SD_AUTO_SPIN_DOWN_TIME;
660 params[1] = 0x00; /* Never spin down the drive. */
661 do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
662 params, 2, res_reg, &res_size);
663 if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20)) {
664 printk(" Unable to set spin-down time: 0x%2.2x\n",
668 params[0] = SONY_SD_MECH_CONTROL;
669 params[1] = SONY_AUTO_SPIN_UP_BIT; /* Set auto spin up */
672 params[1] |= SONY_AUTO_EJECT_BIT;
674 if (is_double_speed && want_doublespeed) {
675 params[1] |= SONY_DOUBLE_SPEED_BIT; /* Set the drive to double speed if
678 do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
679 params, 2, res_reg, &res_size);
680 if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20)) {
681 printk(" Unable to set mechanical parameters: 0x%2.2x\n",
687 * Uniform cdrom interface function
688 * select reading speed for data access
690 static int scd_select_speed(struct cdrom_device_info *cdi, int speed)
695 sony_speed = speed - 1;
697 set_drive_params(sony_speed);
702 * Uniform cdrom interface function
703 * lock or unlock eject button
705 static int scd_lock_door(struct cdrom_device_info *cdi, int lock)
707 if (lock == 0 && sony_usage == 1) {
708 /* Unlock the door, only if nobody is using the drive */
713 set_drive_params(sony_speed);
718 * This code will reset the drive and attempt to restore sane parameters.
720 static void restart_on_error(void)
722 unsigned char res_reg[12];
723 unsigned int res_size;
724 unsigned int retry_count;
727 printk("cdu31a: Resetting drive on error\n");
729 retry_count = jiffies + SONY_RESET_TIMEOUT;
730 while (time_before(jiffies, retry_count) && (!is_attention())) {
733 set_drive_params(sony_speed);
734 do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg, &res_size);
735 if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20)) {
736 printk("cdu31a: Unable to spin up drive: 0x%2.2x\n",
740 current->state = TASK_INTERRUPTIBLE;
741 schedule_timeout(2 * HZ);
747 * This routine writes data to the parameter register. Since this should
748 * happen fairly fast, it is polled with no OS waits between.
750 static int write_params(unsigned char *params, int num_params)
752 unsigned int retry_count;
755 retry_count = SONY_READY_RETRIES;
756 while ((retry_count > 0) && (!is_param_write_rdy())) {
759 if (!is_param_write_rdy()) {
763 while (num_params > 0) {
764 write_param(*params);
774 * The following reads data from the command result register. It is a
775 * fairly complex routine, all status info flows back through this
776 * interface. The algorithm is stolen directly from the flowcharts in
780 get_result(unsigned char *result_buffer, unsigned int *result_size)
784 unsigned int retry_count;
787 while (handle_sony_cd_attention());
788 /* Wait for the result data to be ready */
789 retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
790 while (time_before(jiffies, retry_count)
791 && (is_busy() || (!(is_result_ready())))) {
794 while (handle_sony_cd_attention());
796 if (is_busy() || (!(is_result_ready()))) {
798 printk("CDU31A timeout out %d\n", __LINE__);
800 result_buffer[0] = 0x20;
801 result_buffer[1] = SONY_TIMEOUT_OP_ERR;
807 * Get the first two bytes. This determines what else needs
810 clear_result_ready();
811 a = read_result_register();
815 /* Check for block error status result. */
816 if ((a & 0xf0) == 0x50) {
821 b = read_result_register();
827 * 0x20 means an error occurred. Byte 2 will have the error code.
828 * Otherwise, the command succeeded, byte 2 will have the count of
829 * how many more status bytes are coming.
831 * The result register can be read 10 bytes at a time, a wait for
832 * result ready to be asserted must be done between every 10 bytes.
834 if ((a & 0xf0) != 0x20) {
836 for (i = 0; i < 8; i++) {
837 *result_buffer = read_result_register();
844 retry_count = SONY_READY_RETRIES;
845 while ((retry_count > 0)
846 && (!is_result_ready())) {
849 if (!is_result_ready()) {
851 printk("CDU31A timeout out %d\n",
854 result_buffer[0] = 0x20;
861 clear_result_ready();
863 for (i = 0; i < 10; i++) {
865 read_result_register();
873 retry_count = SONY_READY_RETRIES;
874 while ((retry_count > 0)
875 && (!is_result_ready())) {
878 if (!is_result_ready()) {
880 printk("CDU31A timeout out %d\n",
883 result_buffer[0] = 0x20;
893 *result_buffer = read_result_register();
902 * Do a command that does not involve data transfer. This routine must
903 * be re-entrant from the same task to support being called from the
904 * data operation code when an error occurs.
907 do_sony_cd_cmd(unsigned char cmd,
908 unsigned char *params,
909 unsigned int num_params,
910 unsigned char *result_buffer, unsigned int *result_size)
912 unsigned int retry_count;
920 if (current != has_cd_task) { /* Allow recursive calls to this routine */
922 interruptible_sleep_on(&sony_wait);
923 if (signal_pending(current)) {
924 result_buffer[0] = 0x20;
925 result_buffer[1] = SONY_SIGNAL_OP_ERR;
927 restore_flags(flags);
932 has_cd_task = current;
941 while (handle_sony_cd_attention());
945 retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
946 while (time_before(jiffies, retry_count) && (is_busy())) {
949 while (handle_sony_cd_attention());
953 printk("CDU31A timeout out %d\n", __LINE__);
955 result_buffer[0] = 0x20;
956 result_buffer[1] = SONY_TIMEOUT_OP_ERR;
959 clear_result_ready();
962 write_params(params, num_params);
965 get_result(result_buffer, result_size);
968 if (((result_buffer[0] & 0xf0) == 0x20)
969 && (num_retries < MAX_CDU31A_RETRIES)) {
971 current->state = TASK_INTERRUPTIBLE;
972 schedule_timeout(HZ / 10); /* Wait .1 seconds on retries */
973 goto retry_cd_operation;
976 if (!recursive_call) {
979 wake_up_interruptible(&sony_wait);
982 restore_flags(flags);
987 * Handle an attention from the drive. This will return 1 if it found one
988 * or 0 if not (if one is found, the caller might want to call again).
990 * This routine counts the number of consecutive times it is called
991 * (since this is always called from a while loop until it returns
992 * a 0), and returns a 0 if it happens too many times. This will help
995 static int handle_sony_cd_attention(void)
997 unsigned char atten_code;
998 static int num_consecutive_attentions = 0;
1003 printk("Entering handle_sony_cd_attention\n");
1005 if (is_attention()) {
1006 if (num_consecutive_attentions >
1007 CDU31A_MAX_CONSECUTIVE_ATTENTIONS) {
1009 ("cdu31a: Too many consecutive attentions: %d\n",
1010 num_consecutive_attentions);
1011 num_consecutive_attentions = 0;
1013 printk("Leaving handle_sony_cd_attention at %d\n",
1020 atten_code = read_result_register();
1022 switch (atten_code) {
1023 /* Someone changed the CD. Mark it as changed */
1024 case SONY_MECH_LOADED_ATTN:
1027 sony_audio_status = CDROM_AUDIO_NO_STATUS;
1028 sony_blocks_left = 0;
1031 case SONY_SPIN_DOWN_COMPLETE_ATTN:
1032 /* Mark the disk as spun down. */
1036 case SONY_AUDIO_PLAY_DONE_ATTN:
1037 sony_audio_status = CDROM_AUDIO_COMPLETED;
1041 case SONY_EJECT_PUSHED_ATTN:
1042 if (is_auto_eject) {
1043 sony_audio_status = CDROM_AUDIO_INVALID;
1047 case SONY_LEAD_IN_ERR_ATTN:
1048 case SONY_LEAD_OUT_ERR_ATTN:
1049 case SONY_DATA_TRACK_ERR_ATTN:
1050 case SONY_AUDIO_PLAYBACK_ERR_ATTN:
1051 sony_audio_status = CDROM_AUDIO_ERROR;
1055 num_consecutive_attentions++;
1057 printk("Leaving handle_sony_cd_attention at %d\n",
1061 } else if (abort_read_started) {
1062 while (is_result_reg_not_empty()) {
1063 val = read_result_register();
1066 clear_result_ready();
1067 /* Clear out the data */
1068 while (is_data_requested()) {
1069 val = read_data_register();
1071 abort_read_started = 0;
1073 printk("Leaving handle_sony_cd_attention at %d\n",
1079 num_consecutive_attentions = 0;
1081 printk("Leaving handle_sony_cd_attention at %d\n", __LINE__);
1087 /* Convert from an integer 0-99 to BCD */
1088 static inline unsigned int int_to_bcd(unsigned int val)
1093 retval = (val / 10) << 4;
1094 retval = retval | val % 10;
1099 /* Convert from BCD to an integer from 0-99 */
1100 static unsigned int bcd_to_int(unsigned int bcd)
1102 return ((((bcd >> 4) & 0x0f) * 10) + (bcd & 0x0f));
1107 * Convert a logical sector value (like the OS would want to use for
1108 * a block device) to an MSF format.
1110 static void log_to_msf(unsigned int log, unsigned char *msf)
1112 log = log + LOG_START_OFFSET;
1113 msf[0] = int_to_bcd(log / 4500);
1115 msf[1] = int_to_bcd(log / 75);
1116 msf[2] = int_to_bcd(log % 75);
1121 * Convert an MSF format to a logical sector.
1123 static unsigned int msf_to_log(unsigned char *msf)
1130 log += msf[0] * 4500;
1131 log = log - LOG_START_OFFSET;
1138 * Take in integer size value and put it into a buffer like
1139 * the drive would want to see a number-of-sector value.
1141 static void size_to_buf(unsigned int size, unsigned char *buf)
1143 buf[0] = size / 65536;
1144 size = size % 65536;
1145 buf[1] = size / 256;
1146 buf[2] = size % 256;
1149 /* Starts a read operation. Returns 0 on success and 1 on failure.
1150 The read operation used here allows multiple sequential sectors
1151 to be read and status returned for each sector. The driver will
1152 read the output one at a time as the requests come and abort the
1153 operation if the requested sector is not the next one from the
1156 start_request(unsigned int sector, unsigned int nsect, int read_nsect_only)
1158 unsigned char params[6];
1159 unsigned int read_size;
1160 unsigned int retry_count;
1164 printk("Entering start_request\n");
1166 log_to_msf(sector, params);
1167 /* If requested, read exactly what was asked. */
1168 if (read_nsect_only) {
1172 * If the full read-ahead would go beyond the end of the media, trim
1173 * it back to read just till the end of the media.
1175 else if ((sector + nsect) >= sony_toc.lead_out_start_lba) {
1176 read_size = sony_toc.lead_out_start_lba - sector;
1178 /* Read the full readahead amount. */
1180 read_size = CDU31A_READAHEAD / 4;
1182 size_to_buf(read_size, ¶ms[3]);
1185 * Clear any outstanding attentions and wait for the drive to
1186 * complete any pending operations.
1188 while (handle_sony_cd_attention());
1190 retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
1191 while (time_before(jiffies, retry_count) && (is_busy())) {
1194 while (handle_sony_cd_attention());
1198 printk("CDU31A: Timeout while waiting to issue command\n");
1200 printk("Leaving start_request at %d\n", __LINE__);
1204 /* Issue the command */
1205 clear_result_ready();
1208 write_params(params, 6);
1209 write_cmd(SONY_READ_BLKERR_STAT_CMD);
1211 sony_blocks_left = read_size * 4;
1212 sony_next_block = sector * 4;
1213 readahead_dataleft = 0;
1216 printk("Leaving start_request at %d\n", __LINE__);
1221 printk("Leaving start_request at %d\n", __LINE__);
1225 /* Abort a pending read operation. Clear all the drive status and
1226 readahead variables. */
1227 static void abort_read(void)
1229 unsigned char result_reg[2];
1234 do_sony_cd_cmd(SONY_ABORT_CMD, NULL, 0, result_reg, &result_size);
1235 if ((result_reg[0] & 0xf0) == 0x20) {
1236 printk("CDU31A: Error aborting read, %s error\n",
1237 translate_error(result_reg[1]));
1240 while (is_result_reg_not_empty()) {
1241 val = read_result_register();
1244 clear_result_ready();
1245 /* Clear out the data */
1246 while (is_data_requested()) {
1247 val = read_data_register();
1250 sony_blocks_left = 0;
1251 readahead_dataleft = 0;
1255 /* Called when the timer times out. This will abort the
1256 pending read operation. */
1257 static void handle_abort_timeout(unsigned long data)
1259 unsigned long flags;
1262 printk("Entering handle_abort_timeout\n");
1266 /* If it is in use, ignore it. */
1268 /* We can't use abort_read(), because it will sleep
1269 or schedule in the timer interrupt. Just start
1270 the operation, finish it on the next access to
1272 clear_result_ready();
1274 write_cmd(SONY_ABORT_CMD);
1276 sony_blocks_left = 0;
1277 readahead_dataleft = 0;
1279 abort_read_started = 1;
1281 restore_flags(flags);
1283 printk("Leaving handle_abort_timeout\n");
1287 /* Actually get data and status from the drive. */
1289 input_data(char *buffer,
1290 unsigned int bytesleft,
1291 unsigned int nblocks, unsigned int offset, unsigned int skip)
1294 volatile unsigned char val;
1298 printk("Entering input_data\n");
1300 /* If an XA disk on a CDU31A, skip the first 12 bytes of data from
1301 the disk. The real data is after that. */
1303 for (i = 0; i < CD_XA_HEAD; i++) {
1304 val = read_data_register();
1310 if (bytesleft == 2048) { /* 2048 byte direct buffer transfer */
1311 insb(sony_cd_read_reg, buffer, 2048);
1312 readahead_dataleft = 0;
1314 /* If the input read did not align with the beginning of the block,
1315 skip the necessary bytes. */
1317 insb(sony_cd_read_reg, readahead_buffer, skip);
1320 /* Get the data into the buffer. */
1321 insb(sony_cd_read_reg, &buffer[offset], bytesleft);
1323 /* Get the rest of the data into the readahead buffer at the
1325 readahead_dataleft = (2048 - skip) - bytesleft;
1326 insb(sony_cd_read_reg,
1327 readahead_buffer + bytesleft, readahead_dataleft);
1329 sony_blocks_left -= nblocks;
1330 sony_next_block += nblocks;
1332 /* If an XA disk, we have to clear out the rest of the unused
1333 error correction data. */
1335 for (i = 0; i < CD_XA_TAIL; i++) {
1336 val = read_data_register();
1340 printk("Leaving input_data at %d\n", __LINE__);
1344 /* read data from the drive. Note the nsect must be <= 4. */
1346 read_data_block(char *buffer,
1348 unsigned int nblocks,
1349 unsigned char res_reg[], int *res_size)
1351 unsigned int retry_count;
1352 unsigned int bytesleft;
1353 unsigned int offset;
1358 printk("Entering read_data_block\n");
1364 bytesleft = nblocks * 512;
1367 /* If the data in the read-ahead does not match the block offset,
1368 then fix things up. */
1369 if (((block % 4) * 512) != ((2048 - readahead_dataleft) % 2048)) {
1370 sony_next_block += block % 4;
1371 sony_blocks_left -= block % 4;
1372 skip = (block % 4) * 512;
1377 /* We have readahead data in the buffer, get that first before we
1378 decide if a read is necessary. */
1379 if (readahead_dataleft != 0) {
1380 if (bytesleft > readahead_dataleft) {
1381 /* The readahead will not fill the requested buffer, but
1382 get the data out of the readahead into the buffer. */
1384 readahead_buffer + (2048 -
1385 readahead_dataleft),
1386 readahead_dataleft);
1387 readahead_dataleft = 0;
1388 bytesleft -= readahead_dataleft;
1389 offset += readahead_dataleft;
1391 /* The readahead will fill the whole buffer, get the data
1394 readahead_buffer + (2048 -
1395 readahead_dataleft),
1397 readahead_dataleft -= bytesleft;
1399 sony_blocks_left -= nblocks;
1400 sony_next_block += nblocks;
1402 /* If the data in the readahead is bad, return an error so the
1403 driver will abort the buffer. */
1404 if (readahead_bad) {
1406 res_reg[1] = SONY_BAD_DATA_ERR;
1410 if (readahead_dataleft == 0) {
1414 /* Final transfer is done for read command, get final result. */
1415 if (sony_blocks_left == 0) {
1416 get_result(res_reg, res_size);
1419 printk("Leaving read_data_block at %d\n",
1426 /* Wait for the drive to tell us we have something */
1427 retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
1428 while (time_before(jiffies, retry_count) && !(is_data_ready())) {
1429 while (handle_sony_cd_attention());
1433 if (!(is_data_ready())) {
1434 if (is_result_ready()) {
1435 get_result(res_reg, res_size);
1436 if ((res_reg[0] & 0xf0) != 0x20) {
1438 ("CDU31A: Got result that should have been error: %d\n",
1441 res_reg[1] = SONY_BAD_DATA_ERR;
1447 printk("CDU31A timeout out %d\n", __LINE__);
1450 res_reg[1] = SONY_TIMEOUT_OP_ERR;
1455 input_data(buffer, bytesleft, nblocks, offset, skip);
1457 /* Wait for the status from the drive. */
1458 retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
1459 while (time_before(jiffies, retry_count)
1460 && !(is_result_ready())) {
1461 while (handle_sony_cd_attention());
1466 if (!is_result_ready()) {
1468 printk("CDU31A timeout out %d\n", __LINE__);
1471 res_reg[1] = SONY_TIMEOUT_OP_ERR;
1475 get_result(res_reg, res_size);
1477 /* If we got a buffer status, handle that. */
1478 if ((res_reg[0] & 0xf0) == 0x50) {
1481 SONY_NO_CIRC_ERR_BLK_STAT)
1483 SONY_NO_LECC_ERR_BLK_STAT)
1485 SONY_RECOV_LECC_ERR_BLK_STAT)) {
1486 /* The data was successful, but if data was read from
1487 the readahead and it was bad, set the whole
1489 if (readahead_bad) {
1498 ("CDU31A: Data block error: 0x%x\n",
1501 res_reg[1] = SONY_BAD_DATA_ERR;
1504 /* Data is in the readahead buffer but an error was returned.
1505 Make sure future requests don't use the data. */
1506 if (bytesleft != 2048) {
1511 /* Final transfer is done for read command, get final result. */
1512 if (sony_blocks_left == 0) {
1513 get_result(res_reg, res_size);
1515 } else if ((res_reg[0] & 0xf0) != 0x20) {
1516 /* The drive gave me bad status, I don't know what to do.
1517 Reset the driver and return an error. */
1519 ("CDU31A: Invalid block status: 0x%x\n",
1523 res_reg[1] = SONY_BAD_DATA_ERR;
1529 printk("Leaving read_data_block at %d\n", __LINE__);
1535 * The OS calls this to perform a read or write operation to the drive.
1536 * Write obviously fail. Reads to a read ahead of sony_buffer_size
1537 * bytes to help speed operations. This especially helps since the OS
1538 * uses 1024 byte blocks and the drive uses 2048 byte blocks. Since most
1539 * data access on a CD is done sequentially, this saves a lot of operations.
1541 static void do_cdu31a_request(request_queue_t * q)
1545 unsigned char res_reg[12];
1546 unsigned int res_size;
1548 unsigned long flags;
1552 printk("Entering do_cdu31a_request\n");
1556 * Make sure no one else is using the driver; wait for them
1557 * to finish if it is so.
1561 while (sony_inuse) {
1562 interruptible_sleep_on(&sony_wait);
1563 if (signal_pending(current)) {
1564 restore_flags(flags);
1566 && CURRENT->rq_status != RQ_INACTIVE) {
1569 restore_flags(flags);
1571 printk("Leaving do_cdu31a_request at %d\n",
1578 has_cd_task = current;
1580 /* Get drive status before doing anything. */
1581 while (handle_sony_cd_attention());
1583 /* Make sure we have a valid TOC. */
1586 spin_unlock_irq(&io_request_lock);
1588 /* Make sure the timer is cancelled. */
1589 del_timer(&cdu31a_abort_timer);
1592 cdu31a_request_startover:
1594 * The beginning here is stolen from the hard disk driver. I hope
1597 if (QUEUE_EMPTY || CURRENT->rq_status == RQ_INACTIVE) {
1598 goto end_do_cdu31a_request;
1601 if (!sony_spun_up) {
1605 /* I don't use INIT_REQUEST because it calls return, which would
1606 return without unlocking the device. It shouldn't matter,
1607 but just to be safe... */
1608 if (MAJOR(CURRENT->rq_dev) != MAJOR_NR) {
1609 panic(DEVICE_NAME ": request list destroyed");
1612 if (!buffer_locked(CURRENT->bh)) {
1613 panic(DEVICE_NAME ": block not locked");
1617 block = CURRENT->sector;
1618 nblock = CURRENT->nr_sectors;
1620 if (!sony_toc_read) {
1621 printk("CDU31A: TOC not read\n");
1623 goto cdu31a_request_startover;
1626 switch (CURRENT->cmd) {
1629 * If the block address is invalid or the request goes beyond the end of
1630 * the media, return an error.
1633 if ((block / 4) < sony_toc.start_track_lba) {
1635 ("CDU31A: Request before beginning of media\n");
1637 goto cdu31a_request_startover;
1640 if ((block / 4) >= sony_toc.lead_out_start_lba) {
1642 ("CDU31A: Request past end of media\n");
1644 goto cdu31a_request_startover;
1646 if (((block + nblock) / 4) >=
1647 sony_toc.lead_out_start_lba) {
1649 ("CDU31A: Request past end of media\n");
1651 goto cdu31a_request_startover;
1657 while (handle_sony_cd_attention());
1659 if (!sony_toc_read) {
1660 printk("CDU31A: TOC not read\n");
1662 goto cdu31a_request_startover;
1665 /* If no data is left to be read from the drive, start the
1667 if (sony_blocks_left == 0) {
1669 (block / 4, CDU31A_READAHEAD / 4, 0)) {
1671 goto cdu31a_request_startover;
1674 /* If the requested block is not the next one waiting in
1675 the driver, abort the current operation and start a
1677 else if (block != sony_next_block) {
1680 ("CDU31A Warning: Read for block %d, expected %d\n",
1681 block, sony_next_block);
1684 if (!sony_toc_read) {
1685 printk("CDU31A: TOC not read\n");
1687 goto cdu31a_request_startover;
1690 (block / 4, CDU31A_READAHEAD / 4, 0)) {
1692 ("CDU31a: start request failed\n");
1694 goto cdu31a_request_startover;
1698 read_data_block(CURRENT->buffer, block, nblock,
1699 res_reg, &res_size);
1700 if (res_reg[0] == 0x20) {
1701 if (num_retries > MAX_CDU31A_RETRIES) {
1703 goto cdu31a_request_startover;
1707 if (res_reg[1] == SONY_NOT_SPIN_ERR) {
1708 do_sony_cd_cmd(SONY_SPIN_UP_CMD,
1713 ("CDU31A: %s error for block %d, nblock %d\n",
1714 translate_error(res_reg[1]),
1717 goto try_read_again;
1728 panic("CDU31A: Unknown cmd");
1732 end_do_cdu31a_request:
1733 spin_lock_irq(&io_request_lock);
1735 /* After finished, cancel any pending operations. */
1738 /* Start a timer to time out after a while to disable
1740 cdu31a_abort_timer.expires = jiffies + 2 * HZ; /* Wait 2 seconds */
1741 add_timer(&cdu31a_abort_timer);
1746 wake_up_interruptible(&sony_wait);
1747 restore_flags(flags);
1749 printk("Leaving do_cdu31a_request at %d\n", __LINE__);
1755 * Read the table of contents from the drive and set up TOC if
1758 static void sony_get_toc(void)
1760 unsigned char res_reg[2];
1761 unsigned int res_size;
1762 unsigned char parms[1];
1765 int totaltracks = 0;
1770 printk("Entering sony_get_toc\n");
1774 if (!sony_toc_read) {
1776 /* Ignore the result, since it might error if spinning already. */
1777 do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg,
1780 do_sony_cd_cmd(SONY_READ_TOC_CMD, NULL, 0, res_reg,
1783 /* The drive sometimes returns error 0. I don't know why, but ignore
1784 it. It seems to mean the drive has already done the operation. */
1786 || ((res_reg[0] != 0) && (res_reg[1] != 0))) {
1787 /* If the drive is already playing, it's ok. */
1788 if ((res_reg[1] == SONY_AUDIO_PLAYING_ERR)
1789 || (res_reg[1] == 0)) {
1790 goto gettoc_drive_spinning;
1793 /* If the drive says it is not spun up (even though we just did it!)
1794 then retry the operation at least a few times. */
1795 if ((res_reg[1] == SONY_NOT_SPIN_ERR)
1796 && (num_spin_ups < MAX_CDU31A_RETRIES)) {
1798 goto respinup_on_gettoc;
1801 printk("cdu31a: Error reading TOC: %x %s\n",
1802 res_reg[0], translate_error(res_reg[1]));
1806 gettoc_drive_spinning:
1808 /* The idea here is we keep asking for sessions until the command
1809 fails. Then we know what the last valid session on the disk is.
1810 No need to check session 0, since session 0 is the same as session
1811 1; the command returns different information if you give it 0.
1814 memset(&sony_toc, 0x0e, sizeof(sony_toc));
1815 memset(&single_toc, 0x0f, sizeof(single_toc));
1819 /* This seems to slow things down enough to make it work. This
1820 * appears to be a problem in do_sony_cd_cmd. This printk seems
1821 * to address the symptoms... -Erik */
1823 printk("cdu31a: Trying session %d\n", session);
1826 do_sony_cd_cmd(SONY_READ_TOC_SPEC_CMD,
1827 parms, 1, res_reg, &res_size);
1830 printk("%2.2x %2.2x\n", res_reg[0], res_reg[1]);
1834 || ((res_reg[0] & 0xf0) == 0x20)) {
1835 /* An error reading the TOC, this must be past the last session. */
1838 ("Yikes! Couldn't read any sessions!");
1842 printk("Reading session %d\n", session);
1846 do_sony_cd_cmd(SONY_REQ_TOC_DATA_SPEC_CMD,
1849 (unsigned char *) &single_toc,
1852 || ((single_toc.exec_status[0] & 0xf0) ==
1855 ("cdu31a: Error reading session %d: %x %s\n",
1856 session, single_toc.exec_status[0],
1857 translate_error(single_toc.
1859 /* An error reading the TOC. Return without sony_toc_read
1865 ("add0 %01x, con0 %01x, poi0 %02x, 1st trk %d, dsktyp %x, dum0 %x\n",
1866 single_toc.address0, single_toc.control0,
1868 bcd_to_int(single_toc.first_track_num),
1869 single_toc.disk_type, single_toc.dummy0);
1871 ("add1 %01x, con1 %01x, poi1 %02x, lst trk %d, dummy1 %x, dum2 %x\n",
1872 single_toc.address1, single_toc.control1,
1874 bcd_to_int(single_toc.last_track_num),
1875 single_toc.dummy1, single_toc.dummy2);
1877 ("add2 %01x, con2 %01x, poi2 %02x leadout start min %d, sec %d, frame %d\n",
1878 single_toc.address2, single_toc.control2,
1880 bcd_to_int(single_toc.lead_out_start_msf[0]),
1881 bcd_to_int(single_toc.lead_out_start_msf[1]),
1882 bcd_to_int(single_toc.lead_out_start_msf[2]));
1883 if (res_size > 18 && single_toc.pointb0 > 0xaf)
1885 ("addb0 %01x, conb0 %01x, poib0 %02x, nextsession min %d, sec %d, frame %d\n"
1886 "#mode5_ptrs %02d, max_start_outer_leadout_msf min %d, sec %d, frame %d\n",
1887 single_toc.addressb0,
1888 single_toc.controlb0,
1890 bcd_to_int(single_toc.
1891 next_poss_prog_area_msf
1893 bcd_to_int(single_toc.
1894 next_poss_prog_area_msf
1896 bcd_to_int(single_toc.
1897 next_poss_prog_area_msf
1899 single_toc.num_mode_5_pointers,
1900 bcd_to_int(single_toc.
1901 max_start_outer_leadout_msf
1903 bcd_to_int(single_toc.
1904 max_start_outer_leadout_msf
1906 bcd_to_int(single_toc.
1907 max_start_outer_leadout_msf
1909 if (res_size > 27 && single_toc.pointb1 > 0xaf)
1911 ("addb1 %01x, conb1 %01x, poib1 %02x, %x %x %x %x #skipint_ptrs %d, #skiptrkassign %d %x\n",
1912 single_toc.addressb1,
1913 single_toc.controlb1,
1915 single_toc.dummyb0_1[0],
1916 single_toc.dummyb0_1[1],
1917 single_toc.dummyb0_1[2],
1918 single_toc.dummyb0_1[3],
1919 single_toc.num_skip_interval_pointers,
1920 single_toc.num_skip_track_assignments,
1921 single_toc.dummyb0_2);
1922 if (res_size > 36 && single_toc.pointb2 > 0xaf)
1924 ("addb2 %01x, conb2 %01x, poib2 %02x, %02x %02x %02x %02x %02x %02x %02x\n",
1925 single_toc.addressb2,
1926 single_toc.controlb2,
1928 single_toc.tracksb2[0],
1929 single_toc.tracksb2[1],
1930 single_toc.tracksb2[2],
1931 single_toc.tracksb2[3],
1932 single_toc.tracksb2[4],
1933 single_toc.tracksb2[5],
1934 single_toc.tracksb2[6]);
1935 if (res_size > 45 && single_toc.pointb3 > 0xaf)
1937 ("addb3 %01x, conb3 %01x, poib3 %02x, %02x %02x %02x %02x %02x %02x %02x\n",
1938 single_toc.addressb3,
1939 single_toc.controlb3,
1941 single_toc.tracksb3[0],
1942 single_toc.tracksb3[1],
1943 single_toc.tracksb3[2],
1944 single_toc.tracksb3[3],
1945 single_toc.tracksb3[4],
1946 single_toc.tracksb3[5],
1947 single_toc.tracksb3[6]);
1948 if (res_size > 54 && single_toc.pointb4 > 0xaf)
1950 ("addb4 %01x, conb4 %01x, poib4 %02x, %02x %02x %02x %02x %02x %02x %02x\n",
1951 single_toc.addressb4,
1952 single_toc.controlb4,
1954 single_toc.tracksb4[0],
1955 single_toc.tracksb4[1],
1956 single_toc.tracksb4[2],
1957 single_toc.tracksb4[3],
1958 single_toc.tracksb4[4],
1959 single_toc.tracksb4[5],
1960 single_toc.tracksb4[6]);
1961 if (res_size > 63 && single_toc.pointc0 > 0xaf)
1963 ("addc0 %01x, conc0 %01x, poic0 %02x, %02x %02x %02x %02x %02x %02x %02x\n",
1964 single_toc.addressc0,
1965 single_toc.controlc0,
1967 single_toc.dummyc0[0],
1968 single_toc.dummyc0[1],
1969 single_toc.dummyc0[2],
1970 single_toc.dummyc0[3],
1971 single_toc.dummyc0[4],
1972 single_toc.dummyc0[5],
1973 single_toc.dummyc0[6]);
1978 sony_toc.lead_out_start_msf[0] =
1979 bcd_to_int(single_toc.lead_out_start_msf[0]);
1980 sony_toc.lead_out_start_msf[1] =
1981 bcd_to_int(single_toc.lead_out_start_msf[1]);
1982 sony_toc.lead_out_start_msf[2] =
1983 bcd_to_int(single_toc.lead_out_start_msf[2]);
1984 sony_toc.lead_out_start_lba =
1985 single_toc.lead_out_start_lba =
1986 msf_to_log(sony_toc.lead_out_start_msf);
1988 /* For points that do not exist, move the data over them
1989 to the right location. */
1990 if (single_toc.pointb0 != 0xb0) {
1991 memmove(((char *) &single_toc) + 27,
1992 ((char *) &single_toc) + 18,
1995 } else if (res_size > 18) {
1996 sony_toc.lead_out_start_msf[0] =
1997 bcd_to_int(single_toc.
1998 max_start_outer_leadout_msf
2000 sony_toc.lead_out_start_msf[1] =
2001 bcd_to_int(single_toc.
2002 max_start_outer_leadout_msf
2004 sony_toc.lead_out_start_msf[2] =
2005 bcd_to_int(single_toc.
2006 max_start_outer_leadout_msf
2008 sony_toc.lead_out_start_lba =
2009 msf_to_log(sony_toc.
2010 lead_out_start_msf);
2012 if (single_toc.pointb1 != 0xb1) {
2013 memmove(((char *) &single_toc) + 36,
2014 ((char *) &single_toc) + 27,
2018 if (single_toc.pointb2 != 0xb2) {
2019 memmove(((char *) &single_toc) + 45,
2020 ((char *) &single_toc) + 36,
2024 if (single_toc.pointb3 != 0xb3) {
2025 memmove(((char *) &single_toc) + 54,
2026 ((char *) &single_toc) + 45,
2030 if (single_toc.pointb4 != 0xb4) {
2031 memmove(((char *) &single_toc) + 63,
2032 ((char *) &single_toc) + 54,
2036 if (single_toc.pointc0 != 0xc0) {
2037 memmove(((char *) &single_toc) + 72,
2038 ((char *) &single_toc) + 63,
2044 ("start track lba %u, leadout start lba %u\n",
2045 single_toc.start_track_lba,
2046 single_toc.lead_out_start_lba);
2052 bcd_to_int(single_toc.last_track_num)
2054 bcd_to_int(single_toc.
2055 first_track_num); i++) {
2057 ("trk %02d: add 0x%01x, con 0x%01x, track %02d, start min %02d, sec %02d, frame %02d\n",
2059 single_toc.tracks[i].address,
2060 single_toc.tracks[i].control,
2061 bcd_to_int(single_toc.
2063 bcd_to_int(single_toc.
2067 bcd_to_int(single_toc.
2071 bcd_to_int(single_toc.
2076 bcd_to_int(single_toc.
2079 bcd_to_int(single_toc.
2083 bcd_to_int(single_toc.
2086 bcd_to_int(single_toc.
2091 ("min track number %d, max track number %d\n",
2096 /* prepare a special table of contents for a CD-I disc. They don't have one. */
2097 if (single_toc.disk_type == 0x10 &&
2098 single_toc.first_track_num == 2 &&
2099 single_toc.last_track_num == 2 /* CD-I */ ) {
2100 sony_toc.tracks[totaltracks].address = 1;
2101 sony_toc.tracks[totaltracks].control = 4; /* force data tracks */
2102 sony_toc.tracks[totaltracks].track = 1;
2103 sony_toc.tracks[totaltracks].
2104 track_start_msf[0] = 0;
2105 sony_toc.tracks[totaltracks].
2106 track_start_msf[1] = 2;
2107 sony_toc.tracks[totaltracks].
2108 track_start_msf[2] = 0;
2112 /* gather track entries from this session */
2118 bcd_to_int(single_toc.last_track_num)
2120 bcd_to_int(single_toc.
2122 i++, totaltracks++) {
2123 sony_toc.tracks[totaltracks].
2125 single_toc.tracks[i].address;
2126 sony_toc.tracks[totaltracks].
2128 single_toc.tracks[i].control;
2129 sony_toc.tracks[totaltracks].
2131 bcd_to_int(single_toc.
2133 sony_toc.tracks[totaltracks].
2134 track_start_msf[0] =
2135 bcd_to_int(single_toc.
2137 track_start_msf[0]);
2138 sony_toc.tracks[totaltracks].
2139 track_start_msf[1] =
2140 bcd_to_int(single_toc.
2142 track_start_msf[1]);
2143 sony_toc.tracks[totaltracks].
2144 track_start_msf[2] =
2145 bcd_to_int(single_toc.
2147 track_start_msf[2]);
2151 msf_to_log(sony_toc.
2156 sony_toc.tracks[totaltracks].
2160 tracks[totaltracks].
2163 sony_toc.tracks[totaltracks].
2167 tracks[totaltracks].
2171 sony_toc.first_track_num = mint;
2172 sony_toc.last_track_num = maxt;
2173 /* Disk type of last session wins. For example:
2174 CD-Extra has disk type 0 for the first session, so
2175 a dumb HiFi CD player thinks it is a plain audio CD.
2176 We are interested in the disk type of the last session,
2177 which is 0x20 (XA) for CD-Extra, so we can access the
2179 sony_toc.disk_type = single_toc.disk_type;
2180 sony_toc.sessions = session;
2182 /* don't believe everything :-) */
2184 single_toc.start_track_lba = 0;
2185 sony_toc.start_track_lba =
2186 single_toc.start_track_lba;
2188 if (session > 1 && single_toc.pointb0 == 0xb0 &&
2189 sony_toc.lead_out_start_lba ==
2190 single_toc.lead_out_start_lba) {
2194 /* Let's not get carried away... */
2196 printk("cdu31a: too many sessions: %d\n",
2202 sony_toc.track_entries = totaltracks;
2203 /* add one entry for the LAST track with track number CDROM_LEADOUT */
2204 sony_toc.tracks[totaltracks].address = single_toc.address2;
2205 sony_toc.tracks[totaltracks].control = single_toc.control2;
2206 sony_toc.tracks[totaltracks].track = CDROM_LEADOUT;
2207 sony_toc.tracks[totaltracks].track_start_msf[0] =
2208 sony_toc.lead_out_start_msf[0];
2209 sony_toc.tracks[totaltracks].track_start_msf[1] =
2210 sony_toc.lead_out_start_msf[1];
2211 sony_toc.tracks[totaltracks].track_start_msf[2] =
2212 sony_toc.lead_out_start_msf[2];
2218 ("Disk session %d, start track: %d, stop track: %d\n",
2219 session, single_toc.start_track_lba,
2220 single_toc.lead_out_start_lba);
2224 printk("Leaving sony_get_toc\n");
2230 * Uniform cdrom interface function
2231 * return multisession offset and sector information
2233 static int scd_get_last_session(struct cdrom_device_info *cdi,
2234 struct cdrom_multisession *ms_info)
2236 if (ms_info == NULL)
2242 ms_info->addr_format = CDROM_LBA;
2243 ms_info->addr.lba = sony_toc.start_track_lba;
2244 ms_info->xa_flag = sony_toc.disk_type == SONY_XA_DISK_TYPE ||
2245 sony_toc.disk_type == 0x10 /* CDI */ ;
2251 * Search for a specific track in the table of contents.
2253 static int find_track(int track)
2257 for (i = 0; i <= sony_toc.track_entries; i++) {
2258 if (sony_toc.tracks[i].track == track) {
2268 * Read the subcode and put it in last_sony_subcode for future use.
2270 static int read_subcode(void)
2272 unsigned int res_size;
2275 do_sony_cd_cmd(SONY_REQ_SUBCODE_ADDRESS_CMD,
2277 0, (unsigned char *) &last_sony_subcode, &res_size);
2279 || ((last_sony_subcode.exec_status[0] & 0xf0) == 0x20)) {
2280 printk("Sony CDROM error %s (read_subcode)\n",
2281 translate_error(last_sony_subcode.exec_status[1]));
2285 last_sony_subcode.track_num =
2286 bcd_to_int(last_sony_subcode.track_num);
2287 last_sony_subcode.index_num =
2288 bcd_to_int(last_sony_subcode.index_num);
2289 last_sony_subcode.abs_msf[0] =
2290 bcd_to_int(last_sony_subcode.abs_msf[0]);
2291 last_sony_subcode.abs_msf[1] =
2292 bcd_to_int(last_sony_subcode.abs_msf[1]);
2293 last_sony_subcode.abs_msf[2] =
2294 bcd_to_int(last_sony_subcode.abs_msf[2]);
2296 last_sony_subcode.rel_msf[0] =
2297 bcd_to_int(last_sony_subcode.rel_msf[0]);
2298 last_sony_subcode.rel_msf[1] =
2299 bcd_to_int(last_sony_subcode.rel_msf[1]);
2300 last_sony_subcode.rel_msf[2] =
2301 bcd_to_int(last_sony_subcode.rel_msf[2]);
2306 * Uniform cdrom interface function
2307 * return the media catalog number found on some older audio cds
2310 scd_get_mcn(struct cdrom_device_info *cdi, struct cdrom_mcn *mcn)
2312 unsigned char resbuffer[2 + 14];
2313 unsigned char *mcnp = mcn->medium_catalog_number;
2314 unsigned char *resp = resbuffer + 3;
2315 unsigned int res_size;
2317 memset(mcn->medium_catalog_number, 0, 14);
2318 do_sony_cd_cmd(SONY_REQ_UPC_EAN_CMD,
2319 NULL, 0, resbuffer, &res_size);
2320 if ((res_size < 2) || ((resbuffer[0] & 0xf0) == 0x20));
2322 /* packed bcd to single ASCII digits */
2323 *mcnp++ = (*resp >> 4) + '0';
2324 *mcnp++ = (*resp++ & 0x0f) + '0';
2325 *mcnp++ = (*resp >> 4) + '0';
2326 *mcnp++ = (*resp++ & 0x0f) + '0';
2327 *mcnp++ = (*resp >> 4) + '0';
2328 *mcnp++ = (*resp++ & 0x0f) + '0';
2329 *mcnp++ = (*resp >> 4) + '0';
2330 *mcnp++ = (*resp++ & 0x0f) + '0';
2331 *mcnp++ = (*resp >> 4) + '0';
2332 *mcnp++ = (*resp++ & 0x0f) + '0';
2333 *mcnp++ = (*resp >> 4) + '0';
2334 *mcnp++ = (*resp++ & 0x0f) + '0';
2335 *mcnp++ = (*resp >> 4) + '0';
2343 * Get the subchannel info like the CDROMSUBCHNL command wants to see it. If
2344 * the drive is playing, the subchannel needs to be read (since it would be
2345 * changing). If the drive is paused or completed, the subcode information has
2346 * already been stored, just use that. The ioctl call wants things in decimal
2347 * (not BCD), so all the conversions are done.
2349 static int sony_get_subchnl_info(struct cdrom_subchnl *schi)
2351 /* Get attention stuff */
2352 while (handle_sony_cd_attention());
2355 if (!sony_toc_read) {
2359 switch (sony_audio_status) {
2360 case CDROM_AUDIO_NO_STATUS:
2361 case CDROM_AUDIO_PLAY:
2362 if (read_subcode() < 0) {
2367 case CDROM_AUDIO_PAUSED:
2368 case CDROM_AUDIO_COMPLETED:
2372 case CDROM_AUDIO_NO_STATUS:
2373 schi->cdsc_audiostatus = sony_audio_status;
2377 case CDROM_AUDIO_INVALID:
2378 case CDROM_AUDIO_ERROR:
2383 schi->cdsc_audiostatus = sony_audio_status;
2384 schi->cdsc_adr = last_sony_subcode.address;
2385 schi->cdsc_ctrl = last_sony_subcode.control;
2386 schi->cdsc_trk = last_sony_subcode.track_num;
2387 schi->cdsc_ind = last_sony_subcode.index_num;
2388 if (schi->cdsc_format == CDROM_MSF) {
2389 schi->cdsc_absaddr.msf.minute =
2390 last_sony_subcode.abs_msf[0];
2391 schi->cdsc_absaddr.msf.second =
2392 last_sony_subcode.abs_msf[1];
2393 schi->cdsc_absaddr.msf.frame =
2394 last_sony_subcode.abs_msf[2];
2396 schi->cdsc_reladdr.msf.minute =
2397 last_sony_subcode.rel_msf[0];
2398 schi->cdsc_reladdr.msf.second =
2399 last_sony_subcode.rel_msf[1];
2400 schi->cdsc_reladdr.msf.frame =
2401 last_sony_subcode.rel_msf[2];
2402 } else if (schi->cdsc_format == CDROM_LBA) {
2403 schi->cdsc_absaddr.lba =
2404 msf_to_log(last_sony_subcode.abs_msf);
2405 schi->cdsc_reladdr.lba =
2406 msf_to_log(last_sony_subcode.rel_msf);
2412 /* Get audio data from the drive. This is fairly complex because I
2413 am looking for status and data at the same time, but if I get status
2414 then I just look for data. I need to get the status immediately so
2415 the switch from audio to data tracks will happen quickly. */
2417 read_audio_data(char *buffer, unsigned char res_reg[], int *res_size)
2419 unsigned int retry_count;
2428 /* Wait for the drive to tell us we have something */
2429 retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
2430 continue_read_audio_wait:
2431 while (time_before(jiffies, retry_count) && !(is_data_ready())
2432 && !(is_result_ready() || result_read)) {
2433 while (handle_sony_cd_attention());
2437 if (!(is_data_ready())) {
2438 if (is_result_ready() && !result_read) {
2439 get_result(res_reg, res_size);
2441 /* Read block status and continue waiting for data. */
2442 if ((res_reg[0] & 0xf0) == 0x50) {
2444 goto continue_read_audio_wait;
2446 /* Invalid data from the drive. Shut down the operation. */
2447 else if ((res_reg[0] & 0xf0) != 0x20) {
2449 ("CDU31A: Got result that should have been error: %d\n",
2452 res_reg[1] = SONY_BAD_DATA_ERR;
2458 printk("CDU31A timeout out %d\n", __LINE__);
2461 res_reg[1] = SONY_TIMEOUT_OP_ERR;
2468 /* If data block, then get 2340 bytes offset by 12. */
2469 if (sony_raw_data_mode) {
2470 insb(sony_cd_read_reg, buffer + CD_XA_HEAD,
2473 /* Audio gets the whole 2352 bytes. */
2474 insb(sony_cd_read_reg, buffer, CD_FRAMESIZE_RAW);
2477 /* If I haven't already gotten the result, get it now. */
2479 /* Wait for the drive to tell us we have something */
2480 retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
2481 while (time_before(jiffies, retry_count)
2482 && !(is_result_ready())) {
2483 while (handle_sony_cd_attention());
2488 if (!is_result_ready()) {
2490 printk("CDU31A timeout out %d\n",
2494 res_reg[1] = SONY_TIMEOUT_OP_ERR;
2499 get_result(res_reg, res_size);
2503 if ((res_reg[0] & 0xf0) == 0x50) {
2504 if ((res_reg[0] == SONY_NO_CIRC_ERR_BLK_STAT)
2505 || (res_reg[0] == SONY_NO_LECC_ERR_BLK_STAT)
2506 || (res_reg[0] == SONY_RECOV_LECC_ERR_BLK_STAT)
2507 || (res_reg[0] == SONY_NO_ERR_DETECTION_STAT)) {
2508 /* Ok, nothing to do. */
2510 printk("CDU31A: Data block error: 0x%x\n",
2513 res_reg[1] = SONY_BAD_DATA_ERR;
2516 } else if ((res_reg[0] & 0xf0) != 0x20) {
2517 /* The drive gave me bad status, I don't know what to do.
2518 Reset the driver and return an error. */
2519 printk("CDU31A: Invalid block status: 0x%x\n",
2523 res_reg[1] = SONY_BAD_DATA_ERR;
2529 /* Perform a raw data read. This will automatically detect the
2530 track type and read the proper data (audio or data). */
2531 static int read_audio(struct cdrom_read_audio *ra)
2534 unsigned char params[2];
2535 unsigned char res_reg[12];
2536 unsigned int res_size;
2537 unsigned int cframe;
2538 unsigned long flags;
2541 * Make sure no one else is using the driver; wait for them
2542 * to finish if it is so.
2546 while (sony_inuse) {
2547 interruptible_sleep_on(&sony_wait);
2548 if (signal_pending(current)) {
2549 restore_flags(flags);
2554 has_cd_task = current;
2555 restore_flags(flags);
2557 if (!sony_spun_up) {
2561 /* Set the drive to do raw operations. */
2562 params[0] = SONY_SD_DECODE_PARAM;
2563 params[1] = 0x06 | sony_raw_data_mode;
2564 do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
2565 params, 2, res_reg, &res_size);
2566 if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20)) {
2567 printk("CDU31A: Unable to set decode params: 0x%2.2x\n",
2572 /* From here down, we have to goto exit_read_audio instead of returning
2573 because the drive parameters have to be set back to data before
2577 /* start_request clears out any readahead data, so it should be safe. */
2578 if (start_request(ra->addr.lba, ra->nframes, 1)) {
2580 goto exit_read_audio;
2583 /* For every requested frame. */
2585 while (cframe < ra->nframes) {
2586 read_audio_data(readahead_buffer, res_reg, &res_size);
2587 if ((res_reg[0] & 0xf0) == 0x20) {
2588 if (res_reg[1] == SONY_BAD_DATA_ERR) {
2590 ("CDU31A: Data error on audio sector %d\n",
2591 ra->addr.lba + cframe);
2592 } else if (res_reg[1] == SONY_ILL_TRACK_R_ERR) {
2593 /* Illegal track type, change track types and start over. */
2594 sony_raw_data_mode =
2595 (sony_raw_data_mode) ? 0 : 1;
2597 /* Set the drive mode. */
2598 params[0] = SONY_SD_DECODE_PARAM;
2599 params[1] = 0x06 | sony_raw_data_mode;
2600 do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
2602 2, res_reg, &res_size);
2604 || ((res_reg[0] & 0xf0) == 0x20)) {
2606 ("CDU31A: Unable to set decode params: 0x%2.2x\n",
2609 goto exit_read_audio;
2612 /* Restart the request on the current frame. */
2614 (ra->addr.lba + cframe,
2615 ra->nframes - cframe, 1)) {
2617 goto exit_read_audio;
2620 /* Don't go back to the top because don't want to get into
2621 and infinite loop. A lot of code gets duplicated, but
2622 that's no big deal, I don't guess. */
2623 read_audio_data(readahead_buffer, res_reg,
2625 if ((res_reg[0] & 0xf0) == 0x20) {
2627 SONY_BAD_DATA_ERR) {
2629 ("CDU31A: Data error on audio sector %d\n",
2634 ("CDU31A: Error reading audio data on sector %d: %s\n",
2635 ra->addr.lba + cframe,
2639 goto exit_read_audio;
2642 copy_to_user((char *) (ra->buf +
2651 ("CDU31A: Error reading audio data on sector %d: %s\n",
2652 ra->addr.lba + cframe,
2653 translate_error(res_reg[1]));
2655 goto exit_read_audio;
2658 copy_to_user((char *) (ra->buf +
2661 (char *) readahead_buffer,
2668 get_result(res_reg, &res_size);
2669 if ((res_reg[0] & 0xf0) == 0x20) {
2670 printk("CDU31A: Error return from audio read: %s\n",
2671 translate_error(res_reg[1]));
2673 goto exit_read_audio;
2678 /* Set the drive mode back to the proper one for the disk. */
2679 params[0] = SONY_SD_DECODE_PARAM;
2680 if (!sony_xa_mode) {
2685 do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
2686 params, 2, res_reg, &res_size);
2687 if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20)) {
2688 printk("CDU31A: Unable to reset decode params: 0x%2.2x\n",
2695 wake_up_interruptible(&sony_wait);
2701 do_sony_cd_cmd_chk(const char *name,
2703 unsigned char *params,
2704 unsigned int num_params,
2705 unsigned char *result_buffer, unsigned int *result_size)
2707 do_sony_cd_cmd(cmd, params, num_params, result_buffer,
2709 if ((*result_size < 2) || ((result_buffer[0] & 0xf0) == 0x20)) {
2710 printk("Sony CDROM error %s (CDROM%s)\n",
2711 translate_error(result_buffer[1]), name);
2718 * Uniform cdrom interface function
2721 static int scd_tray_move(struct cdrom_device_info *cdi, int position)
2723 if (position == 1 /* open tray */ ) {
2724 unsigned char res_reg[12];
2725 unsigned int res_size;
2727 do_sony_cd_cmd(SONY_AUDIO_STOP_CMD, NULL, 0, res_reg,
2729 do_sony_cd_cmd(SONY_SPIN_DOWN_CMD, NULL, 0, res_reg,
2732 sony_audio_status = CDROM_AUDIO_INVALID;
2733 return do_sony_cd_cmd_chk("EJECT", SONY_EJECT_CMD, NULL, 0,
2734 res_reg, &res_size);
2736 if (0 == scd_spinup())
2743 * The big ugly ioctl handler.
2745 static int scd_audio_ioctl(struct cdrom_device_info *cdi,
2746 unsigned int cmd, void *arg)
2748 unsigned char res_reg[12];
2749 unsigned int res_size;
2750 unsigned char params[7];
2755 case CDROMSTART: /* Spin up the drive */
2756 return do_sony_cd_cmd_chk("START", SONY_SPIN_UP_CMD, NULL,
2757 0, res_reg, &res_size);
2760 case CDROMSTOP: /* Spin down the drive */
2761 do_sony_cd_cmd(SONY_AUDIO_STOP_CMD, NULL, 0, res_reg,
2765 * Spin the drive down, ignoring the error if the disk was
2766 * already not spinning.
2768 sony_audio_status = CDROM_AUDIO_NO_STATUS;
2769 return do_sony_cd_cmd_chk("STOP", SONY_SPIN_DOWN_CMD, NULL,
2770 0, res_reg, &res_size);
2772 case CDROMPAUSE: /* Pause the drive */
2773 if (do_sony_cd_cmd_chk
2774 ("PAUSE", SONY_AUDIO_STOP_CMD, NULL, 0, res_reg,
2777 /* Get the current position and save it for resuming */
2778 if (read_subcode() < 0) {
2781 cur_pos_msf[0] = last_sony_subcode.abs_msf[0];
2782 cur_pos_msf[1] = last_sony_subcode.abs_msf[1];
2783 cur_pos_msf[2] = last_sony_subcode.abs_msf[2];
2784 sony_audio_status = CDROM_AUDIO_PAUSED;
2788 case CDROMRESUME: /* Start the drive after being paused */
2789 if (sony_audio_status != CDROM_AUDIO_PAUSED) {
2793 do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg,
2796 /* Start the drive at the saved position. */
2797 params[1] = int_to_bcd(cur_pos_msf[0]);
2798 params[2] = int_to_bcd(cur_pos_msf[1]);
2799 params[3] = int_to_bcd(cur_pos_msf[2]);
2800 params[4] = int_to_bcd(final_pos_msf[0]);
2801 params[5] = int_to_bcd(final_pos_msf[1]);
2802 params[6] = int_to_bcd(final_pos_msf[2]);
2804 if (do_sony_cd_cmd_chk
2805 ("RESUME", SONY_AUDIO_PLAYBACK_CMD, params, 7, res_reg,
2808 sony_audio_status = CDROM_AUDIO_PLAY;
2811 case CDROMPLAYMSF: /* Play starting at the given MSF address. */
2812 do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg,
2815 /* The parameters are given in int, must be converted */
2816 for (i = 1; i < 7; i++) {
2818 int_to_bcd(((unsigned char *) arg)[i - 1]);
2821 if (do_sony_cd_cmd_chk
2822 ("PLAYMSF", SONY_AUDIO_PLAYBACK_CMD, params, 7,
2823 res_reg, &res_size) < 0)
2826 /* Save the final position for pauses and resumes */
2827 final_pos_msf[0] = bcd_to_int(params[4]);
2828 final_pos_msf[1] = bcd_to_int(params[5]);
2829 final_pos_msf[2] = bcd_to_int(params[6]);
2830 sony_audio_status = CDROM_AUDIO_PLAY;
2833 case CDROMREADTOCHDR: /* Read the table of contents header */
2835 struct cdrom_tochdr *hdr;
2838 if (!sony_toc_read) {
2842 hdr = (struct cdrom_tochdr *) arg;
2843 hdr->cdth_trk0 = sony_toc.first_track_num;
2844 hdr->cdth_trk1 = sony_toc.last_track_num;
2848 case CDROMREADTOCENTRY: /* Read a given table of contents entry */
2850 struct cdrom_tocentry *entry;
2852 unsigned char *msf_val = NULL;
2855 if (!sony_toc_read) {
2859 entry = (struct cdrom_tocentry *) arg;
2861 track_idx = find_track(entry->cdte_track);
2862 if (track_idx < 0) {
2867 sony_toc.tracks[track_idx].address;
2869 sony_toc.tracks[track_idx].control;
2871 sony_toc.tracks[track_idx].track_start_msf;
2873 /* Logical buffer address or MSF format requested? */
2874 if (entry->cdte_format == CDROM_LBA) {
2875 entry->cdte_addr.lba = msf_to_log(msf_val);
2876 } else if (entry->cdte_format == CDROM_MSF) {
2877 entry->cdte_addr.msf.minute = *msf_val;
2878 entry->cdte_addr.msf.second =
2880 entry->cdte_addr.msf.frame =
2887 case CDROMPLAYTRKIND: /* Play a track. This currently ignores index. */
2889 struct cdrom_ti *ti = (struct cdrom_ti *) arg;
2893 if (!sony_toc_read) {
2897 if ((ti->cdti_trk0 < sony_toc.first_track_num)
2898 || (ti->cdti_trk0 > sony_toc.last_track_num)
2899 || (ti->cdti_trk1 < ti->cdti_trk0)) {
2903 track_idx = find_track(ti->cdti_trk0);
2904 if (track_idx < 0) {
2908 int_to_bcd(sony_toc.tracks[track_idx].
2909 track_start_msf[0]);
2911 int_to_bcd(sony_toc.tracks[track_idx].
2912 track_start_msf[1]);
2914 int_to_bcd(sony_toc.tracks[track_idx].
2915 track_start_msf[2]);
2918 * If we want to stop after the last track, use the lead-out
2921 if (ti->cdti_trk1 >= sony_toc.last_track_num) {
2922 track_idx = find_track(CDROM_LEADOUT);
2924 track_idx = find_track(ti->cdti_trk1 + 1);
2926 if (track_idx < 0) {
2930 int_to_bcd(sony_toc.tracks[track_idx].
2931 track_start_msf[0]);
2933 int_to_bcd(sony_toc.tracks[track_idx].
2934 track_start_msf[1]);
2936 int_to_bcd(sony_toc.tracks[track_idx].
2937 track_start_msf[2]);
2940 do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg,
2943 do_sony_cd_cmd(SONY_AUDIO_PLAYBACK_CMD, params, 7,
2944 res_reg, &res_size);
2947 || ((res_reg[0] & 0xf0) == 0x20)) {
2948 printk("Params: %x %x %x %x %x %x %x\n",
2949 params[0], params[1], params[2],
2950 params[3], params[4], params[5],
2953 ("Sony CDROM error %s (CDROMPLAYTRKIND)\n",
2954 translate_error(res_reg[1]));
2958 /* Save the final position for pauses and resumes */
2959 final_pos_msf[0] = bcd_to_int(params[4]);
2960 final_pos_msf[1] = bcd_to_int(params[5]);
2961 final_pos_msf[2] = bcd_to_int(params[6]);
2962 sony_audio_status = CDROM_AUDIO_PLAY;
2966 case CDROMVOLCTRL: /* Volume control. What volume does this change, anyway? */
2968 struct cdrom_volctrl *volctrl =
2969 (struct cdrom_volctrl *) arg;
2971 params[0] = SONY_SD_AUDIO_VOLUME;
2972 params[1] = volctrl->channel0;
2973 params[2] = volctrl->channel1;
2974 return do_sony_cd_cmd_chk("VOLCTRL",
2975 SONY_SET_DRIVE_PARAM_CMD,
2979 case CDROMSUBCHNL: /* Get subchannel info */
2980 return sony_get_subchnl_info((struct cdrom_subchnl *) arg);
2987 static int scd_dev_ioctl(struct cdrom_device_info *cdi,
2988 unsigned int cmd, unsigned long arg)
2993 case CDROMREADAUDIO: /* Read 2352 byte audio tracks and 2340 byte
2996 struct cdrom_read_audio ra;
3000 if (!sony_toc_read) {
3004 if (copy_from_user(&ra, (char *) arg, sizeof(ra)))
3007 if (ra.nframes == 0) {
3011 i = verify_area(VERIFY_WRITE, ra.buf,
3012 CD_FRAMESIZE_RAW * ra.nframes);
3016 if (ra.addr_format == CDROM_LBA) {
3018 sony_toc.lead_out_start_lba)
3019 || (ra.addr.lba + ra.nframes >=
3020 sony_toc.lead_out_start_lba)) {
3023 } else if (ra.addr_format == CDROM_MSF) {
3024 if ((ra.addr.msf.minute >= 75)
3025 || (ra.addr.msf.second >= 60)
3026 || (ra.addr.msf.frame >= 75)) {
3030 ra.addr.lba = ((ra.addr.msf.minute * 4500)
3031 + (ra.addr.msf.second * 75)
3032 + ra.addr.msf.frame);
3034 sony_toc.lead_out_start_lba)
3035 || (ra.addr.lba + ra.nframes >=
3036 sony_toc.lead_out_start_lba)) {
3040 /* I know, this can go negative on an unsigned. However,
3041 the first thing done to the data is to add this value,
3042 so this should compensate and allow direct msf access. */
3043 ra.addr.lba -= LOG_START_OFFSET;
3048 return (read_audio(&ra));
3058 static int scd_spinup(void)
3060 unsigned char res_reg[12];
3061 unsigned int res_size;
3067 do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg, &res_size);
3069 /* The drive sometimes returns error 0. I don't know why, but ignore
3070 it. It seems to mean the drive has already done the operation. */
3071 if ((res_size < 2) || ((res_reg[0] != 0) && (res_reg[1] != 0))) {
3072 printk("Sony CDROM %s error (scd_open, spin up)\n",
3073 translate_error(res_reg[1]));
3077 do_sony_cd_cmd(SONY_READ_TOC_CMD, NULL, 0, res_reg, &res_size);
3079 /* The drive sometimes returns error 0. I don't know why, but ignore
3080 it. It seems to mean the drive has already done the operation. */
3081 if ((res_size < 2) || ((res_reg[0] != 0) && (res_reg[1] != 0))) {
3082 /* If the drive is already playing, it's ok. */
3083 if ((res_reg[1] == SONY_AUDIO_PLAYING_ERR)
3084 || (res_reg[1] == 0)) {
3088 /* If the drive says it is not spun up (even though we just did it!)
3089 then retry the operation at least a few times. */
3090 if ((res_reg[1] == SONY_NOT_SPIN_ERR)
3091 && (num_spin_ups < MAX_CDU31A_RETRIES)) {
3093 goto respinup_on_open;
3096 printk("Sony CDROM error %s (scd_open, read toc)\n",
3097 translate_error(res_reg[1]));
3098 do_sony_cd_cmd(SONY_SPIN_DOWN_CMD, NULL, 0, res_reg,
3106 * Open the drive for operations. Spin the drive up and read the table of
3107 * contents if these have not already been done.
3109 static int scd_open(struct cdrom_device_info *cdi, int openmode)
3111 unsigned char res_reg[12];
3112 unsigned int res_size;
3113 unsigned char params[2];
3115 if (sony_usage == 0) {
3116 if (scd_spinup() != 0)
3119 if (!sony_toc_read) {
3120 do_sony_cd_cmd(SONY_SPIN_DOWN_CMD, NULL, 0,
3121 res_reg, &res_size);
3125 /* For XA on the CDU31A only, we have to do special reads.
3126 The CDU33A handles XA automagically. */
3127 /* if ( (sony_toc.disk_type == SONY_XA_DISK_TYPE) */
3128 if ((sony_toc.disk_type != 0x00)
3129 && (!is_double_speed)) {
3130 params[0] = SONY_SD_DECODE_PARAM;
3132 do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
3133 params, 2, res_reg, &res_size);
3135 || ((res_reg[0] & 0xf0) == 0x20)) {
3137 ("CDU31A: Unable to set XA params: 0x%2.2x\n",
3142 /* A non-XA disk. Set the parms back if necessary. */
3143 else if (sony_xa_mode) {
3144 params[0] = SONY_SD_DECODE_PARAM;
3146 do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
3147 params, 2, res_reg, &res_size);
3149 || ((res_reg[0] & 0xf0) == 0x20)) {
3151 ("CDU31A: Unable to reset XA params: 0x%2.2x\n",
3167 * Close the drive. Spin it down if no task is using it. The spin
3168 * down will fail if playing audio, so audio play is OK.
3170 static void scd_release(struct cdrom_device_info *cdi)
3172 if (sony_usage == 1) {
3173 unsigned char res_reg[12];
3174 unsigned int res_size;
3176 do_sony_cd_cmd(SONY_SPIN_DOWN_CMD, NULL, 0, res_reg,
3184 struct block_device_operations scd_bdops =
3188 release: cdrom_release,
3190 check_media_change: cdrom_media_changed,
3193 static struct cdrom_device_ops scd_dops = {
3195 release:scd_release,
3196 drive_status:scd_drive_status,
3197 media_changed:scd_media_changed,
3198 tray_move:scd_tray_move,
3199 lock_door:scd_lock_door,
3200 select_speed:scd_select_speed,
3201 get_last_session:scd_get_last_session,
3202 get_mcn:scd_get_mcn,
3204 audio_ioctl:scd_audio_ioctl,
3205 dev_ioctl:scd_dev_ioctl,
3206 capability:CDC_OPEN_TRAY | CDC_CLOSE_TRAY | CDC_LOCK |
3207 CDC_SELECT_SPEED | CDC_MULTI_SESSION |
3208 CDC_MULTI_SESSION | CDC_MCN |
3209 CDC_MEDIA_CHANGED | CDC_PLAY_AUDIO |
3210 CDC_RESET | CDC_IOCTLS | CDC_DRIVE_STATUS,
3214 static struct cdrom_device_info scd_info = {
3221 /* The different types of disc loading mechanisms supported */
3222 static char *load_mech[] __initdata =
3223 { "caddy", "tray", "pop-up", "unknown" };
3226 get_drive_configuration(unsigned short base_io,
3227 unsigned char res_reg[], unsigned int *res_size)
3232 /* Set the base address */
3233 cdu31a_port = base_io;
3235 /* Set up all the register locations */
3236 sony_cd_cmd_reg = cdu31a_port + SONY_CMD_REG_OFFSET;
3237 sony_cd_param_reg = cdu31a_port + SONY_PARAM_REG_OFFSET;
3238 sony_cd_write_reg = cdu31a_port + SONY_WRITE_REG_OFFSET;
3239 sony_cd_control_reg = cdu31a_port + SONY_CONTROL_REG_OFFSET;
3240 sony_cd_status_reg = cdu31a_port + SONY_STATUS_REG_OFFSET;
3241 sony_cd_result_reg = cdu31a_port + SONY_RESULT_REG_OFFSET;
3242 sony_cd_read_reg = cdu31a_port + SONY_READ_REG_OFFSET;
3243 sony_cd_fifost_reg = cdu31a_port + SONY_FIFOST_REG_OFFSET;
3246 * Check to see if anything exists at the status register location.
3247 * I don't know if this is a good way to check, but it seems to work
3250 if (read_status_register() != 0xff) {
3252 * Reset the drive and wait for attention from it (to say it's reset).
3253 * If you don't wait, the next operation will probably fail.
3256 retry_count = jiffies + SONY_RESET_TIMEOUT;
3257 while (time_before(jiffies, retry_count)
3258 && (!is_attention())) {
3263 /* If attention is never seen probably not a CDU31a present */
3264 if (!is_attention()) {
3271 * Get the drive configuration.
3273 do_sony_cd_cmd(SONY_REQ_DRIVE_CONFIG_CMD,
3275 0, (unsigned char *) res_reg, res_size);
3279 /* Return an error */
3285 * Set up base I/O and interrupts, called from main.c.
3289 static int __init cdu31a_setup(char *strings)
3293 (void) get_options(strings, ARRAY_SIZE(ints), ints);
3296 cdu31a_port = ints[1];
3299 cdu31a_irq = ints[2];
3301 if ((strings != NULL) && (*strings != '\0')) {
3302 if (strcmp(strings, "PAS") == 0) {
3305 printk("CDU31A: Unknown interface type: %s\n",
3313 __setup("cdu31a=", cdu31a_setup);
3317 static int cdu31a_block_size;
3320 * Initialize the driver.
3322 int __init cdu31a_init(void)
3324 struct s_sony_drive_config drive_config;
3325 unsigned int res_size;
3334 * According to Alex Freed (freed@europa.orion.adobe.com), this is
3335 * required for the Fusion CD-16 package. If the sound driver is
3336 * loaded, it should work fine, but just in case...
3338 * The following turn on the CD-ROM interface for a Fusion CD-16.
3340 if (sony_pas_init) {
3347 /* Setting the base I/O address to 0xffff will disable it. */
3348 if (cdu31a_port == 0xffff) {
3349 } else if (cdu31a_port != 0) {
3350 tmp_irq = cdu31a_irq; /* Need IRQ 0 because we can't sleep here. */
3353 get_drive_configuration(cdu31a_port,
3354 drive_config.exec_status,
3357 && ((drive_config.exec_status[0] & 0xf0) == 0x00)) {
3361 cdu31a_irq = tmp_irq;
3365 while ((cdu31a_addresses[i].base != 0)
3366 && (!drive_found)) {
3367 if (check_region(cdu31a_addresses[i].base, 4)) {
3371 get_drive_configuration(cdu31a_addresses[i].base,
3372 drive_config.exec_status,
3375 && ((drive_config.exec_status[0] & 0xf0) ==
3378 cdu31a_irq = cdu31a_addresses[i].int_num;
3388 if (!request_region(cdu31a_port, 4, "cdu31a"))
3391 if (devfs_register_blkdev(MAJOR_NR, "cdu31a", &scd_bdops)) {
3392 printk("Unable to get major %d for CDU-31a\n",
3397 if (SONY_HWC_DOUBLE_SPEED(drive_config)) {
3398 is_double_speed = 1;
3401 tmp_irq = cdu31a_irq; /* Need IRQ 0 because we can't sleep here. */
3404 set_drive_params(sony_speed);
3406 cdu31a_irq = tmp_irq;
3408 if (cdu31a_irq > 0) {
3410 (cdu31a_irq, cdu31a_interrupt, SA_INTERRUPT,
3413 ("Unable to grab IRQ%d for the CDU31A driver\n",
3419 sprintf(msg, "Sony I/F CDROM : %8.8s %16.16s %8.8s\n",
3420 drive_config.vendor_id,
3421 drive_config.product_id,
3422 drive_config.product_rev_level);
3423 sprintf(buf, " Capabilities: %s",
3424 load_mech[SONY_HWC_GET_LOAD_MECH(drive_config)]);
3426 if (SONY_HWC_AUDIO_PLAYBACK(drive_config)) {
3427 strcat(msg, ", audio");
3429 deficiency |= CDC_PLAY_AUDIO;
3430 if (SONY_HWC_EJECT(drive_config)) {
3431 strcat(msg, ", eject");
3433 deficiency |= CDC_OPEN_TRAY;
3434 if (SONY_HWC_LED_SUPPORT(drive_config)) {
3435 strcat(msg, ", LED");
3437 if (SONY_HWC_ELECTRIC_VOLUME(drive_config)) {
3438 strcat(msg, ", elec. Vol");
3440 if (SONY_HWC_ELECTRIC_VOLUME_CTL(drive_config)) {
3441 strcat(msg, ", sep. Vol");
3443 if (is_double_speed) {
3444 strcat(msg, ", double speed");
3446 deficiency |= CDC_SELECT_SPEED;
3447 if (cdu31a_irq > 0) {
3448 sprintf(buf, ", irq %d", cdu31a_irq);
3454 strcmp("CD-ROM CDU31A", drive_config.product_id) == 0;
3456 blk_init_queue(BLK_DEFAULT_QUEUE(MAJOR_NR),
3458 read_ahead[MAJOR_NR] = CDU31A_READAHEAD;
3459 cdu31a_block_size = 1024; /* 1kB default block size */
3460 /* use 'mount -o block=2048' */
3461 blksize_size[MAJOR_NR] = &cdu31a_block_size;
3463 init_timer(&cdu31a_abort_timer);
3464 cdu31a_abort_timer.function = handle_abort_timeout;
3466 scd_info.dev = MKDEV(MAJOR_NR, 0);
3467 scd_info.mask = deficiency;
3468 strncpy(scd_info.name, "cdu31a", sizeof(scd_info.name));
3470 if (register_cdrom(&scd_info)) {
3473 devfs_plain_cdrom(&scd_info, &scd_bdops);
3485 printk("Unable to register CDU-31a with Uniform cdrom driver\n");
3486 blk_cleanup_queue(BLK_DEFAULT_QUEUE(MAJOR_NR));
3487 if (devfs_unregister_blkdev(MAJOR_NR, "cdu31a")) {
3488 printk("Can't unregister block device for cdu31a\n");
3491 release_region(cdu31a_port, 4);
3497 void __exit cdu31a_exit(void)
3499 if (unregister_cdrom(&scd_info)) {
3501 ("Can't unregister cdu31a from Uniform cdrom driver\n");
3504 if ((devfs_unregister_blkdev(MAJOR_NR, "cdu31a") == -EINVAL)) {
3505 printk("Can't unregister cdu31a\n");
3509 blk_cleanup_queue(BLK_DEFAULT_QUEUE(MAJOR_NR));
3512 free_irq(cdu31a_irq, NULL);
3514 release_region(cdu31a_port, 4);
3515 printk(KERN_INFO "cdu31a module released.\n");
3519 module_init(cdu31a_init);
3521 module_exit(cdu31a_exit);
3523 MODULE_LICENSE("GPL");