* better
[mascara-docs.git] / i386 / linux-2.3.21 / drivers / cdrom / cdu31a.c
blobedb90ee76e45c2d82bea509bf50723ea758e0b39
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 * 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.
76 * XA compatibility
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.
81 * Multi-Session
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!
87 * Raw sector I/O
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.
111 * TODO:
112 * CDs with form1 and form2 sectors cause problems
113 * with current read-ahead strategy.
115 * Credits:
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
122 * will start.
124 * 10/24/95 - Added support for disabling the eject button when the
125 * drive is open. Note that there is a small problem
126 * still here, if the eject button is pushed while the
127 * drive light is flashing, the drive will return a bad
128 * status and be reset. It recovers, though.
130 * 03/07/97 - Fixed a problem with timers.
133 * 18 Spetember 1997 -- Ported to Uniform CD-ROM driver by
134 * Heiko Eissfeldt <heiko@colossus.escape.de> with additional
135 * changes by Erik Andersen <andersee@debian.org>
137 * 24 January 1998 -- Removed the scd_disc_status() function, which was now
138 * just dead code left over from the port.
139 * Erik Andersen <andersee@debian.org>
141 * 16 July 1998 -- Drive donated to Erik Andersen by John Kodis
142 * <kodis@jagunet.com>. Work begun on fixing driver to
143 * work under 2.1.X. Added temporary extra printks
144 * which seem to slow it down enough to work.
147 #include <linux/major.h>
149 #include <linux/module.h>
151 #include <linux/errno.h>
152 #include <linux/signal.h>
153 #include <linux/sched.h>
154 #include <linux/timer.h>
155 #include <linux/fs.h>
156 #include <linux/kernel.h>
157 #include <linux/hdreg.h>
158 #include <linux/genhd.h>
159 #include <linux/ioport.h>
160 #include <linux/string.h>
161 #include <linux/malloc.h>
162 #include <linux/init.h>
163 #include <linux/interrupt.h>
165 #include <asm/system.h>
166 #include <asm/io.h>
167 #include <asm/uaccess.h>
168 #include <asm/dma.h>
170 #include <linux/cdrom.h>
171 #include "cdu31a.h"
173 #define MAJOR_NR CDU31A_CDROM_MAJOR
174 #include <linux/blk.h>
176 #define CDU31A_READAHEAD 4 /* 128 sector, 64kB, 32 reads read-ahead */
177 #define CDU31A_MAX_CONSECUTIVE_ATTENTIONS 10
179 #define DEBUG 0
181 /* Define the following if you have data corruption problems. */
182 #undef SONY_POLL_EACH_BYTE
185 ** Edit the following data to change interrupts, DMA channels, etc.
186 ** Default is polled and no DMA. DMA is not recommended for double-speed
187 ** drives.
189 static struct
191 unsigned short base; /* I/O Base Address */
192 short int_num; /* Interrupt Number (-1 means scan for it,
193 0 means don't use) */
194 } cdu31a_addresses[] __initdata =
196 #if 0 /* No autoconfig any more. See Note at beginning
197 of this file. */
198 { 0x340, 0 }, /* Standard configuration Sony Interface */
199 { 0x1f88, 0 }, /* Fusion CD-16 */
200 { 0x230, 0 }, /* SoundBlaster 16 card */
201 { 0x360, 0 }, /* Secondary standard Sony Interface */
202 { 0x320, 0 }, /* Secondary standard Sony Interface */
203 { 0x330, 0 }, /* Secondary standard Sony Interface */
204 { 0x634, 0 }, /* Sound FX SC400 */
205 { 0x654, 0 }, /* Sound FX SC400 */
206 #endif
207 { 0 }
210 static int handle_sony_cd_attention(void);
211 static int read_subcode(void);
212 static void sony_get_toc(void);
213 static int scd_spinup(void);
214 /*static int scd_open(struct inode *inode, struct file *filp);*/
215 static int scd_open(struct cdrom_device_info *, int);
216 static void do_sony_cd_cmd(unsigned char cmd,
217 unsigned char *params,
218 unsigned int num_params,
219 unsigned char *result_buffer,
220 unsigned int *result_size);
221 static void size_to_buf(unsigned int size,
222 unsigned char *buf);
224 /* Parameters for the read-ahead. */
225 static unsigned int sony_next_block; /* Next 512 byte block offset */
226 static unsigned int sony_blocks_left = 0; /* Number of 512 byte blocks left
227 in the current read command. */
230 /* The base I/O address of the Sony Interface. This is a variable (not a
231 #define) so it can be easily changed via some future ioctl() */
232 static unsigned int cdu31a_port = 0;
233 MODULE_PARM(cdu31a_port, "i");
236 * The following are I/O addresses of the various registers for the drive. The
237 * comment for the base address also applies here.
239 static volatile unsigned short sony_cd_cmd_reg;
240 static volatile unsigned short sony_cd_param_reg;
241 static volatile unsigned short sony_cd_write_reg;
242 static volatile unsigned short sony_cd_control_reg;
243 static volatile unsigned short sony_cd_status_reg;
244 static volatile unsigned short sony_cd_result_reg;
245 static volatile unsigned short sony_cd_read_reg;
246 static volatile unsigned short sony_cd_fifost_reg;
249 static int sony_spun_up = 0; /* Has the drive been spun up? */
251 static int sony_speed = 0; /* Last wanted speed */
253 static int sony_xa_mode = 0; /* Is an XA disk in the drive
254 and the drive a CDU31A? */
256 static int sony_raw_data_mode = 1; /* 1 if data tracks, 0 if audio.
257 For raw data reads. */
259 static unsigned int sony_usage = 0; /* How many processes have the
260 drive open. */
262 static int sony_pas_init = 0; /* Initialize the Pro-Audio
263 Spectrum card? */
265 static struct s_sony_session_toc single_toc; /* Holds the
266 table of
267 contents. */
269 static struct s_all_sessions_toc sony_toc; /* entries gathered from all
270 sessions */
272 static int sony_toc_read = 0; /* Has the TOC been read for
273 the drive? */
275 static struct s_sony_subcode last_sony_subcode; /* Points to the last
276 subcode address read */
278 static volatile int sony_inuse = 0; /* Is the drive in use? Only one operation
279 at a time allowed */
281 static DECLARE_WAIT_QUEUE_HEAD(sony_wait); /* Things waiting for the drive */
283 static struct task_struct *has_cd_task = NULL; /* The task that is currently
284 using the CDROM drive, or
285 NULL if none. */
287 static int is_double_speed = 0; /* does the drive support double speed ? */
288 static int is_a_cdu31a = 1; /* Is the drive a CDU31A? */
290 static int is_auto_eject = 1; /* Door has been locked? 1=No/0=Yes */
293 * The audio status uses the values from read subchannel data as specified
294 * in include/linux/cdrom.h.
296 static volatile int sony_audio_status = CDROM_AUDIO_NO_STATUS;
299 * The following are a hack for pausing and resuming audio play. The drive
300 * does not work as I would expect it, if you stop it then start it again,
301 * the drive seeks back to the beginning and starts over. This holds the
302 * position during a pause so a resume can restart it. It uses the
303 * audio status variable above to tell if it is paused.
305 static unsigned volatile char cur_pos_msf[3] = { 0, 0, 0 };
306 static unsigned volatile char final_pos_msf[3] = { 0, 0, 0 };
308 /* What IRQ is the drive using? 0 if none. */
309 static int cdu31a_irq = 0;
310 MODULE_PARM(cdu31a_irq, "i");
312 /* The interrupt handler will wake this queue up when it gets an
313 interrupts. */
314 DECLARE_WAIT_QUEUE_HEAD(cdu31a_irq_wait);
316 static int curr_control_reg = 0; /* Current value of the control register */
318 /* A disk changed variable. When a disk change is detected, it will
319 all be set to TRUE. As the upper layers ask for disk_changed status
320 it will be cleared. */
321 static char disk_changed;
323 /* Variable for using the readahead buffer. The readahead buffer
324 is used for raw sector reads and for blocksizes that are smaller
325 than 2048 bytes. */
326 static char readahead_buffer[CD_FRAMESIZE_RAW];
327 static int readahead_dataleft = 0;
328 static int readahead_bad = 0;
330 /* Used to time a short period to abort an operation after the
331 drive has been idle for a while. This keeps the light on
332 the drive from flashing for very long. */
333 static struct timer_list cdu31a_abort_timer;
335 /* Marks if the timeout has started an abort read. This is used
336 on entry to the drive to tell the code to read out the status
337 from the abort read. */
338 static int abort_read_started = 0;
342 * This routine returns 1 if the disk has been changed since the last
343 * check or 0 if it hasn't.
345 static int
346 scd_disk_change(kdev_t full_dev)
348 int retval;
350 retval = disk_changed;
351 disk_changed = 0;
353 return retval;
357 * Uniform cdrom interface function
358 * report back, if disc has changed from time of last request.
360 static int
361 scd_media_changed(struct cdrom_device_info *cdi, int disc_nr)
363 return scd_disk_change(cdi->dev);
367 * Uniform cdrom interface function
368 * report back, if drive is ready
370 static int scd_drive_status(struct cdrom_device_info *cdi, int slot_nr)
372 if (CDSL_CURRENT != slot_nr) {
373 /* we have no changer support */
374 return -EINVAL;
376 if (scd_spinup() == 0) {
377 sony_spun_up = 1;
379 return sony_spun_up ? CDS_DISC_OK : CDS_DRIVE_NOT_READY;
382 static inline void
383 enable_interrupts(void)
385 curr_control_reg |= ( SONY_ATTN_INT_EN_BIT
386 | SONY_RES_RDY_INT_EN_BIT
387 | SONY_DATA_RDY_INT_EN_BIT);
388 outb(curr_control_reg, sony_cd_control_reg);
391 static inline void
392 disable_interrupts(void)
394 curr_control_reg &= ~( SONY_ATTN_INT_EN_BIT
395 | SONY_RES_RDY_INT_EN_BIT
396 | SONY_DATA_RDY_INT_EN_BIT);
397 outb(curr_control_reg, sony_cd_control_reg);
401 * Wait a little while (used for polling the drive). If in initialization,
402 * setting a timeout doesn't work, so just loop for a while.
404 static inline void
405 sony_sleep(void)
407 unsigned long flags;
409 if (cdu31a_irq <= 0)
411 current->state = TASK_INTERRUPTIBLE;
412 schedule_timeout(0);
414 else /* Interrupt driven */
416 save_flags(flags);
417 cli();
418 enable_interrupts();
419 interruptible_sleep_on(&cdu31a_irq_wait);
420 restore_flags(flags);
426 * The following are convenience routine to read various status and set
427 * various conditions in the drive.
429 static inline int
430 is_attention(void)
432 return((inb(sony_cd_status_reg) & SONY_ATTN_BIT) != 0);
435 static inline int
436 is_busy(void)
438 return((inb(sony_cd_status_reg) & SONY_BUSY_BIT) != 0);
441 static inline int
442 is_data_ready(void)
444 return((inb(sony_cd_status_reg) & SONY_DATA_RDY_BIT) != 0);
447 static inline int
448 is_data_requested(void)
450 return((inb(sony_cd_status_reg) & SONY_DATA_REQUEST_BIT) != 0);
453 static inline int
454 is_result_ready(void)
456 return((inb(sony_cd_status_reg) & SONY_RES_RDY_BIT) != 0);
459 static inline int
460 is_param_write_rdy(void)
462 return((inb(sony_cd_fifost_reg) & SONY_PARAM_WRITE_RDY_BIT) != 0);
465 static inline int
466 is_result_reg_not_empty(void)
468 return((inb(sony_cd_fifost_reg) & SONY_RES_REG_NOT_EMP_BIT) != 0);
471 static inline void
472 reset_drive(void)
474 curr_control_reg = 0;
475 readahead_dataleft = 0;
476 sony_toc_read = 0;
477 outb(SONY_DRIVE_RESET_BIT, sony_cd_control_reg);
481 * Uniform cdrom interface function
482 * reset drive and return when it is ready
484 static int scd_reset(struct cdrom_device_info * cdi)
486 int retry_count;
488 reset_drive();
490 retry_count = jiffies + SONY_RESET_TIMEOUT;
491 while (time_before(jiffies, retry_count) && (!is_attention()))
493 sony_sleep();
496 return 0;
499 static inline void
500 clear_attention(void)
502 outb(curr_control_reg | SONY_ATTN_CLR_BIT, sony_cd_control_reg);
505 static inline void
506 clear_result_ready(void)
508 outb(curr_control_reg | SONY_RES_RDY_CLR_BIT, sony_cd_control_reg);
511 static inline void
512 clear_data_ready(void)
514 outb(curr_control_reg | SONY_DATA_RDY_CLR_BIT, sony_cd_control_reg);
517 static inline void
518 clear_param_reg(void)
520 outb(curr_control_reg | SONY_PARAM_CLR_BIT, sony_cd_control_reg);
523 static inline unsigned char
524 read_status_register(void)
526 return(inb(sony_cd_status_reg));
529 static inline unsigned char
530 read_result_register(void)
532 return(inb(sony_cd_result_reg));
535 static inline unsigned char
536 read_data_register(void)
538 return(inb(sony_cd_read_reg));
541 static inline void
542 write_param(unsigned char param)
544 outb(param, sony_cd_param_reg);
547 static inline void
548 write_cmd(unsigned char cmd)
550 outb(curr_control_reg | SONY_RES_RDY_INT_EN_BIT, sony_cd_control_reg);
551 outb(cmd, sony_cd_cmd_reg);
554 static void
555 cdu31a_interrupt(int irq, void *dev_id, struct pt_regs *regs)
557 unsigned char val;
559 if (abort_read_started)
561 /* We might be waiting for an abort to finish. Don't
562 disable interrupts yet, though, because we handle
563 this one here. */
564 /* Clear out the result registers. */
565 while (is_result_reg_not_empty())
567 val = read_result_register();
569 clear_data_ready();
570 clear_result_ready();
572 /* Clear out the data */
573 while (is_data_requested())
575 val = read_data_register();
577 abort_read_started = 0;
579 /* If something was waiting, wake it up now. */
580 if (waitqueue_active(&cdu31a_irq_wait))
582 disable_interrupts();
583 wake_up(&cdu31a_irq_wait);
586 else if (waitqueue_active(&cdu31a_irq_wait))
588 disable_interrupts();
589 wake_up(&cdu31a_irq_wait);
591 else
593 disable_interrupts();
594 printk("CDU31A: Got an interrupt but nothing was waiting\n");
599 * give more verbose error messages
601 static unsigned char *translate_error( unsigned char err_code )
603 static unsigned char errbuf[80];
605 switch (err_code) {
606 case 0x10: return "illegal command ";
607 case 0x11: return "illegal parameter ";
609 case 0x20: return "not loaded ";
610 case 0x21: return "no disc ";
611 case 0x22: return "not spinning ";
612 case 0x23: return "spinning ";
613 case 0x25: return "spindle servo ";
614 case 0x26: return "focus servo ";
615 case 0x29: return "eject mechanism ";
616 case 0x2a: return "audio playing ";
617 case 0x2c: return "emergency eject ";
619 case 0x30: return "focus ";
620 case 0x31: return "frame sync ";
621 case 0x32: return "subcode address ";
622 case 0x33: return "block sync ";
623 case 0x34: return "header address ";
625 case 0x40: return "illegal track read ";
626 case 0x41: return "mode 0 read ";
627 case 0x42: return "illegal mode read ";
628 case 0x43: return "illegal block size read ";
629 case 0x44: return "mode read ";
630 case 0x45: return "form read ";
631 case 0x46: return "leadout read ";
632 case 0x47: return "buffer overrun ";
634 case 0x53: return "unrecoverable CIRC ";
635 case 0x57: return "unrecoverable LECC ";
637 case 0x60: return "no TOC ";
638 case 0x61: return "invalid subcode data ";
639 case 0x63: return "focus on TOC read ";
640 case 0x64: return "frame sync on TOC read ";
641 case 0x65: return "TOC data ";
643 case 0x70: return "hardware failure ";
644 case 0x91: return "leadin ";
645 case 0x92: return "leadout ";
646 case 0x93: return "data track ";
648 sprintf(errbuf, "unknown 0x%02x ", err_code);
649 return errbuf;
653 * Set the drive parameters so the drive will auto-spin-up when a
654 * disk is inserted.
656 static void
657 set_drive_params(int want_doublespeed)
659 unsigned char res_reg[12];
660 unsigned int res_size;
661 unsigned char params[3];
664 params[0] = SONY_SD_AUTO_SPIN_DOWN_TIME;
665 params[1] = 0x00; /* Never spin down the drive. */
666 do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
667 params,
669 res_reg,
670 &res_size);
671 if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20))
673 printk(" Unable to set spin-down time: 0x%2.2x\n", res_reg[1]);
676 params[0] = SONY_SD_MECH_CONTROL;
677 params[1] = SONY_AUTO_SPIN_UP_BIT; /* Set auto spin up */
679 if (is_auto_eject) params[1] |= SONY_AUTO_EJECT_BIT;
681 if (is_double_speed && want_doublespeed)
683 params[1] |= SONY_DOUBLE_SPEED_BIT; /* Set the drive to double speed if
684 possible */
686 do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
687 params,
689 res_reg,
690 &res_size);
691 if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20))
693 printk(" Unable to set mechanical parameters: 0x%2.2x\n", res_reg[1]);
698 * Uniform cdrom interface function
699 * select reading speed for data access
701 static int scd_select_speed(struct cdrom_device_info *cdi, int speed)
703 if (speed == 0)
704 sony_speed = 1;
705 else
706 sony_speed = speed - 1;
708 set_drive_params(sony_speed);
709 return 0;
713 * Uniform cdrom interface function
714 * lock or unlock eject button
716 static int scd_lock_door(struct cdrom_device_info *cdi, int lock)
718 if (lock == 0 && sony_usage == 1)
720 /* Unlock the door, only if nobody is using the drive */
721 is_auto_eject = 1;
722 } else {
723 is_auto_eject = 0;
725 set_drive_params(sony_speed);
726 return 0;
730 * This code will reset the drive and attempt to restore sane parameters.
732 static void
733 restart_on_error(void)
735 unsigned char res_reg[12];
736 unsigned int res_size;
737 unsigned int retry_count;
740 printk("cdu31a: Resetting drive on error\n");
741 reset_drive();
742 retry_count = jiffies + SONY_RESET_TIMEOUT;
743 while (time_before(jiffies, retry_count) && (!is_attention()))
745 sony_sleep();
747 set_drive_params(sony_speed);
748 do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg, &res_size);
749 if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20))
751 printk("cdu31a: Unable to spin up drive: 0x%2.2x\n", res_reg[1]);
754 current->state = TASK_INTERRUPTIBLE;
755 schedule_timeout(2*HZ);
757 sony_get_toc();
761 * This routine writes data to the parameter register. Since this should
762 * happen fairly fast, it is polled with no OS waits between.
764 static int
765 write_params(unsigned char *params,
766 int num_params)
768 unsigned int retry_count;
771 retry_count = SONY_READY_RETRIES;
772 while ((retry_count > 0) && (!is_param_write_rdy()))
774 retry_count--;
776 if (!is_param_write_rdy())
778 return -EIO;
781 while (num_params > 0)
783 write_param(*params);
784 params++;
785 num_params--;
788 return 0;
793 * The following reads data from the command result register. It is a
794 * fairly complex routine, all status info flows back through this
795 * interface. The algorithm is stolen directly from the flowcharts in
796 * the drive manual.
798 static void
799 get_result(unsigned char *result_buffer,
800 unsigned int *result_size)
802 unsigned char a, b;
803 int i;
804 unsigned int retry_count;
807 while (handle_sony_cd_attention())
809 /* Wait for the result data to be ready */
810 retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
811 while (time_before(jiffies, retry_count) && (is_busy() || (!(is_result_ready()))))
813 sony_sleep();
815 while (handle_sony_cd_attention())
818 if (is_busy() || (!(is_result_ready())))
820 #if DEBUG
821 printk("CDU31A timeout out %d\n", __LINE__);
822 #endif
823 result_buffer[0] = 0x20;
824 result_buffer[1] = SONY_TIMEOUT_OP_ERR;
825 *result_size = 2;
826 return;
830 * Get the first two bytes. This determines what else needs
831 * to be done.
833 clear_result_ready();
834 a = read_result_register();
835 *result_buffer = a;
836 result_buffer++;
838 /* Check for block error status result. */
839 if ((a & 0xf0) == 0x50)
841 *result_size = 1;
842 return;
845 b = read_result_register();
846 *result_buffer = b;
847 result_buffer++;
848 *result_size = 2;
851 * 0x20 means an error occurred. Byte 2 will have the error code.
852 * Otherwise, the command succeeded, byte 2 will have the count of
853 * how many more status bytes are coming.
855 * The result register can be read 10 bytes at a time, a wait for
856 * result ready to be asserted must be done between every 10 bytes.
858 if ((a & 0xf0) != 0x20)
860 if (b > 8)
862 for (i=0; i<8; i++)
864 *result_buffer = read_result_register();
865 result_buffer++;
866 (*result_size)++;
868 b = b - 8;
870 while (b > 10)
872 retry_count = SONY_READY_RETRIES;
873 while ((retry_count > 0) && (!is_result_ready()))
875 retry_count--;
877 if (!is_result_ready())
879 #if DEBUG
880 printk("CDU31A timeout out %d\n", __LINE__);
881 #endif
882 result_buffer[0] = 0x20;
883 result_buffer[1] = SONY_TIMEOUT_OP_ERR;
884 *result_size = 2;
885 return;
888 clear_result_ready();
890 for (i=0; i<10; i++)
892 *result_buffer = read_result_register();
893 result_buffer++;
894 (*result_size)++;
896 b = b - 10;
899 if (b > 0)
901 retry_count = SONY_READY_RETRIES;
902 while ((retry_count > 0) && (!is_result_ready()))
904 retry_count--;
906 if (!is_result_ready())
908 #if DEBUG
909 printk("CDU31A timeout out %d\n", __LINE__);
910 #endif
911 result_buffer[0] = 0x20;
912 result_buffer[1] = SONY_TIMEOUT_OP_ERR;
913 *result_size = 2;
914 return;
919 while (b > 0)
921 *result_buffer = read_result_register();
922 result_buffer++;
923 (*result_size)++;
924 b--;
930 * Do a command that does not involve data transfer. This routine must
931 * be re-entrant from the same task to support being called from the
932 * data operation code when an error occurs.
934 static void
935 do_sony_cd_cmd(unsigned char cmd,
936 unsigned char *params,
937 unsigned int num_params,
938 unsigned char *result_buffer,
939 unsigned int *result_size)
941 unsigned int retry_count;
942 int num_retries;
943 int recursive_call;
944 unsigned long flags;
947 save_flags(flags);
948 cli();
949 if (current != has_cd_task) /* Allow recursive calls to this routine */
951 while (sony_inuse)
953 interruptible_sleep_on(&sony_wait);
954 if (signal_pending(current))
956 result_buffer[0] = 0x20;
957 result_buffer[1] = SONY_SIGNAL_OP_ERR;
958 *result_size = 2;
959 restore_flags(flags);
960 return;
963 sony_inuse = 1;
964 has_cd_task = current;
965 recursive_call = 0;
967 else
969 recursive_call = 1;
972 num_retries = 0;
973 retry_cd_operation:
975 while (handle_sony_cd_attention())
978 sti();
980 retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
981 while (time_before(jiffies, retry_count) && (is_busy()))
983 sony_sleep();
985 while (handle_sony_cd_attention())
988 if (is_busy())
990 #if DEBUG
991 printk("CDU31A timeout out %d\n", __LINE__);
992 #endif
993 result_buffer[0] = 0x20;
994 result_buffer[1] = SONY_TIMEOUT_OP_ERR;
995 *result_size = 2;
997 else
999 clear_result_ready();
1000 clear_param_reg();
1002 write_params(params, num_params);
1003 write_cmd(cmd);
1005 get_result(result_buffer, result_size);
1008 if ( ((result_buffer[0] & 0xf0) == 0x20)
1009 && (num_retries < MAX_CDU31A_RETRIES))
1011 num_retries++;
1012 current->state = TASK_INTERRUPTIBLE;
1013 schedule_timeout(HZ/10); /* Wait .1 seconds on retries */
1014 goto retry_cd_operation;
1017 if (!recursive_call)
1019 has_cd_task = NULL;
1020 sony_inuse = 0;
1021 wake_up_interruptible(&sony_wait);
1024 restore_flags(flags);
1029 * Handle an attention from the drive. This will return 1 if it found one
1030 * or 0 if not (if one is found, the caller might want to call again).
1032 * This routine counts the number of consecutive times it is called
1033 * (since this is always called from a while loop until it returns
1034 * a 0), and returns a 0 if it happens too many times. This will help
1035 * prevent a lockup.
1037 static int
1038 handle_sony_cd_attention(void)
1040 unsigned char atten_code;
1041 static int num_consecutive_attentions = 0;
1042 volatile int val;
1045 #if 0*DEBUG
1046 printk("Entering handle_sony_cd_attention\n");
1047 #endif
1048 if (is_attention())
1050 if (num_consecutive_attentions > CDU31A_MAX_CONSECUTIVE_ATTENTIONS)
1052 printk("cdu31a: Too many consecutive attentions: %d\n",
1053 num_consecutive_attentions);
1054 num_consecutive_attentions = 0;
1055 #if DEBUG
1056 printk("Leaving handle_sony_cd_attention at %d\n", __LINE__);
1057 #endif
1058 return(0);
1061 clear_attention();
1062 atten_code = read_result_register();
1064 switch (atten_code)
1066 /* Someone changed the CD. Mark it as changed */
1067 case SONY_MECH_LOADED_ATTN:
1068 disk_changed = 1;
1069 sony_toc_read = 0;
1070 sony_audio_status = CDROM_AUDIO_NO_STATUS;
1071 sony_blocks_left = 0;
1072 break;
1074 case SONY_SPIN_DOWN_COMPLETE_ATTN:
1075 /* Mark the disk as spun down. */
1076 sony_spun_up = 0;
1077 break;
1079 case SONY_AUDIO_PLAY_DONE_ATTN:
1080 sony_audio_status = CDROM_AUDIO_COMPLETED;
1081 read_subcode();
1082 break;
1084 case SONY_EJECT_PUSHED_ATTN:
1085 if (is_auto_eject)
1087 sony_audio_status = CDROM_AUDIO_INVALID;
1089 break;
1091 case SONY_LEAD_IN_ERR_ATTN:
1092 case SONY_LEAD_OUT_ERR_ATTN:
1093 case SONY_DATA_TRACK_ERR_ATTN:
1094 case SONY_AUDIO_PLAYBACK_ERR_ATTN:
1095 sony_audio_status = CDROM_AUDIO_ERROR;
1096 break;
1099 num_consecutive_attentions++;
1100 #if DEBUG
1101 printk("Leaving handle_sony_cd_attention at %d\n", __LINE__);
1102 #endif
1103 return(1);
1105 else if (abort_read_started)
1107 while (is_result_reg_not_empty())
1109 val = read_result_register();
1111 clear_data_ready();
1112 clear_result_ready();
1113 /* Clear out the data */
1114 while (is_data_requested())
1116 val = read_data_register();
1118 abort_read_started = 0;
1119 #if DEBUG
1120 printk("Leaving handle_sony_cd_attention at %d\n", __LINE__);
1121 #endif
1122 return(1);
1125 num_consecutive_attentions = 0;
1126 #if 0*DEBUG
1127 printk("Leaving handle_sony_cd_attention at %d\n", __LINE__);
1128 #endif
1129 return(0);
1133 /* Convert from an integer 0-99 to BCD */
1134 static inline unsigned int
1135 int_to_bcd(unsigned int val)
1137 int retval;
1140 retval = (val / 10) << 4;
1141 retval = retval | val % 10;
1142 return(retval);
1146 /* Convert from BCD to an integer from 0-99 */
1147 static unsigned int
1148 bcd_to_int(unsigned int bcd)
1150 return((((bcd >> 4) & 0x0f) * 10) + (bcd & 0x0f));
1155 * Convert a logical sector value (like the OS would want to use for
1156 * a block device) to an MSF format.
1158 static void
1159 log_to_msf(unsigned int log, unsigned char *msf)
1161 log = log + LOG_START_OFFSET;
1162 msf[0] = int_to_bcd(log / 4500);
1163 log = log % 4500;
1164 msf[1] = int_to_bcd(log / 75);
1165 msf[2] = int_to_bcd(log % 75);
1170 * Convert an MSF format to a logical sector.
1172 static unsigned int
1173 msf_to_log(unsigned char *msf)
1175 unsigned int log;
1178 log = msf[2];
1179 log += msf[1] * 75;
1180 log += msf[0] * 4500;
1181 log = log - LOG_START_OFFSET;
1183 return log;
1188 * Take in integer size value and put it into a buffer like
1189 * the drive would want to see a number-of-sector value.
1191 static void
1192 size_to_buf(unsigned int size,
1193 unsigned char *buf)
1195 buf[0] = size / 65536;
1196 size = size % 65536;
1197 buf[1] = size / 256;
1198 buf[2] = size % 256;
1201 /* Starts a read operation. Returns 0 on success and 1 on failure.
1202 The read operation used here allows multiple sequential sectors
1203 to be read and status returned for each sector. The driver will
1204 read the output one at a time as the requests come and abort the
1205 operation if the requested sector is not the next one from the
1206 drive. */
1207 static int
1208 start_request(unsigned int sector,
1209 unsigned int nsect,
1210 int read_nsect_only)
1212 unsigned char params[6];
1213 unsigned int read_size;
1214 unsigned int retry_count;
1217 #if DEBUG
1218 printk("Entering start_request\n");
1219 #endif
1220 log_to_msf(sector, params);
1221 /* If requested, read exactly what was asked. */
1222 if (read_nsect_only)
1224 read_size = nsect;
1227 * If the full read-ahead would go beyond the end of the media, trim
1228 * it back to read just till the end of the media.
1230 else if ((sector + nsect) >= sony_toc.lead_out_start_lba)
1232 read_size = sony_toc.lead_out_start_lba - sector;
1234 /* Read the full readahead amount. */
1235 else
1237 read_size = CDU31A_READAHEAD / 4;
1239 size_to_buf(read_size, &params[3]);
1242 * Clear any outstanding attentions and wait for the drive to
1243 * complete any pending operations.
1245 while (handle_sony_cd_attention())
1248 retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
1249 while (time_before(jiffies, retry_count) && (is_busy()))
1251 sony_sleep();
1253 while (handle_sony_cd_attention())
1257 if (is_busy())
1259 printk("CDU31A: Timeout while waiting to issue command\n");
1260 #if DEBUG
1261 printk("Leaving start_request at %d\n", __LINE__);
1262 #endif
1263 return(1);
1265 else
1267 /* Issue the command */
1268 clear_result_ready();
1269 clear_param_reg();
1271 write_params(params, 6);
1272 write_cmd(SONY_READ_BLKERR_STAT_CMD);
1274 sony_blocks_left = read_size * 4;
1275 sony_next_block = sector * 4;
1276 readahead_dataleft = 0;
1277 readahead_bad = 0;
1278 #if DEBUG
1279 printk("Leaving start_request at %d\n", __LINE__);
1280 #endif
1281 return(0);
1283 #if DEBUG
1284 printk("Leaving start_request at %d\n", __LINE__);
1285 #endif
1288 /* Abort a pending read operation. Clear all the drive status and
1289 readahead variables. */
1290 static void
1291 abort_read(void)
1293 unsigned char result_reg[2];
1294 int result_size;
1295 volatile int val;
1298 do_sony_cd_cmd(SONY_ABORT_CMD, NULL, 0, result_reg, &result_size);
1299 if ((result_reg[0] & 0xf0) == 0x20)
1301 printk("CDU31A: Error aborting read, %s error\n",
1302 translate_error(
1303 result_reg[1]));
1306 while (is_result_reg_not_empty())
1308 val = read_result_register();
1310 clear_data_ready();
1311 clear_result_ready();
1312 /* Clear out the data */
1313 while (is_data_requested())
1315 val = read_data_register();
1318 sony_blocks_left = 0;
1319 readahead_dataleft = 0;
1320 readahead_bad = 0;
1323 /* Called when the timer times out. This will abort the
1324 pending read operation. */
1325 static void
1326 handle_abort_timeout(unsigned long data)
1328 unsigned long flags;
1330 #if DEBUG
1331 printk("Entering handle_abort_timeout\n");
1332 #endif
1333 save_flags(flags);
1334 cli();
1335 /* If it is in use, ignore it. */
1336 if (!sony_inuse)
1338 /* We can't use abort_read(), because it will sleep
1339 or schedule in the timer interrupt. Just start
1340 the operation, finish it on the next access to
1341 the drive. */
1342 clear_result_ready();
1343 clear_param_reg();
1344 write_cmd(SONY_ABORT_CMD);
1346 sony_blocks_left = 0;
1347 readahead_dataleft = 0;
1348 readahead_bad = 0;
1349 abort_read_started = 1;
1351 restore_flags(flags);
1352 #if DEBUG
1353 printk("Leaving handle_abort_timeout\n");
1354 #endif
1357 /* Actually get data and status from the drive. */
1358 static void
1359 input_data(char *buffer,
1360 unsigned int bytesleft,
1361 unsigned int nblocks,
1362 unsigned int offset,
1363 unsigned int skip)
1365 int i;
1366 volatile unsigned char val;
1369 #if DEBUG
1370 printk("Entering input_data\n");
1371 #endif
1372 /* If an XA disk on a CDU31A, skip the first 12 bytes of data from
1373 the disk. The real data is after that. */
1374 if (sony_xa_mode)
1376 for(i=0; i<CD_XA_HEAD; i++)
1378 val = read_data_register();
1382 clear_data_ready();
1384 if (bytesleft == 2048) /* 2048 byte direct buffer transfer */
1386 insb(sony_cd_read_reg, buffer, 2048);
1387 readahead_dataleft = 0;
1389 else
1391 /* If the input read did not align with the beginning of the block,
1392 skip the necessary bytes. */
1393 if (skip != 0)
1395 insb(sony_cd_read_reg, readahead_buffer, skip);
1398 /* Get the data into the buffer. */
1399 insb(sony_cd_read_reg, &buffer[offset], bytesleft);
1401 /* Get the rest of the data into the readahead buffer at the
1402 proper location. */
1403 readahead_dataleft = (2048 - skip) - bytesleft;
1404 insb(sony_cd_read_reg,
1405 readahead_buffer + bytesleft,
1406 readahead_dataleft);
1408 sony_blocks_left -= nblocks;
1409 sony_next_block += nblocks;
1411 /* If an XA disk, we have to clear out the rest of the unused
1412 error correction data. */
1413 if (sony_xa_mode)
1415 for(i=0; i<CD_XA_TAIL; i++)
1417 val = read_data_register();
1420 #if DEBUG
1421 printk("Leaving input_data at %d\n", __LINE__);
1422 #endif
1425 /* read data from the drive. Note the nsect must be <= 4. */
1426 static void
1427 read_data_block(char *buffer,
1428 unsigned int block,
1429 unsigned int nblocks,
1430 unsigned char res_reg[],
1431 int *res_size)
1433 unsigned int retry_count;
1434 unsigned int bytesleft;
1435 unsigned int offset;
1436 unsigned int skip;
1439 #if DEBUG
1440 printk("Entering read_data_block\n");
1441 #endif
1443 res_reg[0] = 0;
1444 res_reg[1] = 0;
1445 *res_size = 0;
1446 bytesleft = nblocks * 512;
1447 offset = 0;
1449 /* If the data in the read-ahead does not match the block offset,
1450 then fix things up. */
1451 if (((block % 4) * 512) != ((2048 - readahead_dataleft) % 2048))
1453 sony_next_block += block % 4;
1454 sony_blocks_left -= block % 4;
1455 skip = (block % 4) * 512;
1457 else
1459 skip = 0;
1462 /* We have readahead data in the buffer, get that first before we
1463 decide if a read is necessary. */
1464 if (readahead_dataleft != 0)
1466 if (bytesleft > readahead_dataleft)
1468 /* The readahead will not fill the requested buffer, but
1469 get the data out of the readahead into the buffer. */
1470 memcpy(buffer,
1471 readahead_buffer + (2048 - readahead_dataleft),
1472 readahead_dataleft);
1473 readahead_dataleft = 0;
1474 bytesleft -= readahead_dataleft;
1475 offset += readahead_dataleft;
1477 else
1479 /* The readahead will fill the whole buffer, get the data
1480 and return. */
1481 memcpy(buffer,
1482 readahead_buffer + (2048 - readahead_dataleft),
1483 bytesleft);
1484 readahead_dataleft -= bytesleft;
1485 bytesleft = 0;
1486 sony_blocks_left -= nblocks;
1487 sony_next_block += nblocks;
1489 /* If the data in the readahead is bad, return an error so the
1490 driver will abort the buffer. */
1491 if (readahead_bad)
1493 res_reg[0] = 0x20;
1494 res_reg[1] = SONY_BAD_DATA_ERR;
1495 *res_size = 2;
1498 if (readahead_dataleft == 0)
1500 readahead_bad = 0;
1503 /* Final transfer is done for read command, get final result. */
1504 if (sony_blocks_left == 0)
1506 get_result(res_reg, res_size);
1508 #if DEBUG
1509 printk("Leaving read_data_block at %d\n", __LINE__);
1510 #endif
1511 return;
1515 /* Wait for the drive to tell us we have something */
1516 retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
1517 while (time_before(jiffies, retry_count) && !(is_data_ready()))
1519 while (handle_sony_cd_attention())
1522 sony_sleep();
1524 if (!(is_data_ready()))
1526 if (is_result_ready())
1528 get_result(res_reg, res_size);
1529 if ((res_reg[0] & 0xf0) != 0x20)
1531 printk("CDU31A: Got result that should have been error: %d\n",
1532 res_reg[0]);
1533 res_reg[0] = 0x20;
1534 res_reg[1] = SONY_BAD_DATA_ERR;
1535 *res_size = 2;
1537 abort_read();
1539 else
1541 #if DEBUG
1542 printk("CDU31A timeout out %d\n", __LINE__);
1543 #endif
1544 res_reg[0] = 0x20;
1545 res_reg[1] = SONY_TIMEOUT_OP_ERR;
1546 *res_size = 2;
1547 abort_read();
1550 else
1552 input_data(buffer, bytesleft, nblocks, offset, skip);
1554 /* Wait for the status from the drive. */
1555 retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
1556 while (time_before(jiffies, retry_count) && !(is_result_ready()))
1558 while (handle_sony_cd_attention())
1561 sony_sleep();
1564 if (!is_result_ready())
1566 #if DEBUG
1567 printk("CDU31A timeout out %d\n", __LINE__);
1568 #endif
1569 res_reg[0] = 0x20;
1570 res_reg[1] = SONY_TIMEOUT_OP_ERR;
1571 *res_size = 2;
1572 abort_read();
1574 else
1576 get_result(res_reg, res_size);
1578 /* If we got a buffer status, handle that. */
1579 if ((res_reg[0] & 0xf0) == 0x50)
1582 if ( (res_reg[0] == SONY_NO_CIRC_ERR_BLK_STAT)
1583 || (res_reg[0] == SONY_NO_LECC_ERR_BLK_STAT)
1584 || (res_reg[0] == SONY_RECOV_LECC_ERR_BLK_STAT))
1586 /* The data was successful, but if data was read from
1587 the readahead and it was bad, set the whole
1588 buffer as bad. */
1589 if (readahead_bad)
1591 readahead_bad = 0;
1592 res_reg[0] = 0x20;
1593 res_reg[1] = SONY_BAD_DATA_ERR;
1594 *res_size = 2;
1597 else
1599 printk("CDU31A: Data block error: 0x%x\n", res_reg[0]);
1600 res_reg[0] = 0x20;
1601 res_reg[1] = SONY_BAD_DATA_ERR;
1602 *res_size = 2;
1604 /* Data is in the readahead buffer but an error was returned.
1605 Make sure future requests don't use the data. */
1606 if (bytesleft != 2048)
1608 readahead_bad = 1;
1612 /* Final transfer is done for read command, get final result. */
1613 if (sony_blocks_left == 0)
1615 get_result(res_reg, res_size);
1618 else if ((res_reg[0] & 0xf0) != 0x20)
1620 /* The drive gave me bad status, I don't know what to do.
1621 Reset the driver and return an error. */
1622 printk("CDU31A: Invalid block status: 0x%x\n", res_reg[0]);
1623 restart_on_error();
1624 res_reg[0] = 0x20;
1625 res_reg[1] = SONY_BAD_DATA_ERR;
1626 *res_size = 2;
1630 #if DEBUG
1631 printk("Leaving read_data_block at %d\n", __LINE__);
1632 #endif
1637 * The OS calls this to perform a read or write operation to the drive.
1638 * Write obviously fail. Reads to a read ahead of sony_buffer_size
1639 * bytes to help speed operations. This especially helps since the OS
1640 * uses 1024 byte blocks and the drive uses 2048 byte blocks. Since most
1641 * data access on a CD is done sequentially, this saves a lot of operations.
1643 static void
1644 do_cdu31a_request(void)
1646 int block;
1647 int nblock;
1648 unsigned char res_reg[12];
1649 unsigned int res_size;
1650 int num_retries;
1651 unsigned long flags;
1654 #if DEBUG
1655 printk("Entering do_cdu31a_request\n");
1656 #endif
1659 * Make sure no one else is using the driver; wait for them
1660 * to finish if it is so.
1662 save_flags(flags);
1663 cli();
1664 while (sony_inuse)
1666 interruptible_sleep_on(&sony_wait);
1667 if (signal_pending(current))
1669 restore_flags(flags);
1670 if (CURRENT && CURRENT->rq_status != RQ_INACTIVE)
1672 end_request(0);
1674 restore_flags(flags);
1675 #if DEBUG
1676 printk("Leaving do_cdu31a_request at %d\n", __LINE__);
1677 #endif
1678 return;
1681 sony_inuse = 1;
1682 has_cd_task = current;
1684 /* Get drive status before doing anything. */
1685 while (handle_sony_cd_attention())
1688 /* Make sure we have a valid TOC. */
1689 sony_get_toc();
1691 spin_unlock_irq(&io_request_lock);
1693 /* Make sure the timer is cancelled. */
1694 del_timer(&cdu31a_abort_timer);
1696 while (1)
1698 cdu31a_request_startover:
1700 * The beginning here is stolen from the hard disk driver. I hope
1701 * it's right.
1703 if (!(CURRENT) || CURRENT->rq_status == RQ_INACTIVE)
1705 goto end_do_cdu31a_request;
1708 if (!sony_spun_up)
1710 scd_spinup();
1713 /* I don't use INIT_REQUEST because it calls return, which would
1714 return without unlocking the device. It shouldn't matter,
1715 but just to be safe... */
1716 if (MAJOR(CURRENT->rq_dev) != MAJOR_NR)
1718 panic(DEVICE_NAME ": request list destroyed");
1720 if (CURRENT->bh)
1722 if (!buffer_locked(CURRENT->bh))
1724 panic(DEVICE_NAME ": block not locked");
1728 block = CURRENT->sector;
1729 nblock = CURRENT->nr_sectors;
1731 if (!sony_toc_read)
1733 printk("CDU31A: TOC not read\n");
1734 end_request(0);
1735 goto cdu31a_request_startover;
1738 switch(CURRENT->cmd)
1740 case READ:
1742 * If the block address is invalid or the request goes beyond the end of
1743 * the media, return an error.
1745 #if 0
1746 if ((block / 4) < sony_toc.start_track_lba)
1748 printk("CDU31A: Request before beginning of media\n");
1749 end_request(0);
1750 goto cdu31a_request_startover;
1752 #endif
1753 if ((block / 4) >= sony_toc.lead_out_start_lba)
1755 printk("CDU31A: Request past end of media\n");
1756 end_request(0);
1757 goto cdu31a_request_startover;
1759 if (((block + nblock) / 4) >= sony_toc.lead_out_start_lba)
1761 printk("CDU31A: Request past end of media\n");
1762 end_request(0);
1763 goto cdu31a_request_startover;
1766 num_retries = 0;
1768 try_read_again:
1769 while (handle_sony_cd_attention())
1772 if (!sony_toc_read)
1774 printk("CDU31A: TOC not read\n");
1775 end_request(0);
1776 goto cdu31a_request_startover;
1779 /* If no data is left to be read from the drive, start the
1780 next request. */
1781 if (sony_blocks_left == 0)
1783 if (start_request(block / 4, CDU31A_READAHEAD / 4, 0))
1785 end_request(0);
1786 goto cdu31a_request_startover;
1789 /* If the requested block is not the next one waiting in
1790 the driver, abort the current operation and start a
1791 new one. */
1792 else if (block != sony_next_block)
1794 #if DEBUG
1795 printk("CDU31A Warning: Read for block %d, expected %d\n",
1796 block,
1797 sony_next_block);
1798 #endif
1799 abort_read();
1800 if (!sony_toc_read)
1802 printk("CDU31A: TOC not read\n");
1803 end_request(0);
1804 goto cdu31a_request_startover;
1806 if (start_request(block / 4, CDU31A_READAHEAD / 4, 0))
1808 printk("CDU31a: start request failed\n");
1809 end_request(0);
1810 goto cdu31a_request_startover;
1814 read_data_block(CURRENT->buffer, block, nblock, res_reg, &res_size);
1815 if (res_reg[0] == 0x20)
1817 if (num_retries > MAX_CDU31A_RETRIES)
1819 end_request(0);
1820 goto cdu31a_request_startover;
1823 num_retries++;
1824 if (res_reg[1] == SONY_NOT_SPIN_ERR)
1826 do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg, &res_size);
1828 else
1830 printk("CDU31A: %s error for block %d, nblock %d\n", translate_error(res_reg[1]), block, nblock);
1832 goto try_read_again;
1834 else
1836 end_request(1);
1838 break;
1840 case WRITE:
1841 end_request(0);
1842 break;
1844 default:
1845 panic("CDU31A: Unknown cmd");
1849 end_do_cdu31a_request:
1850 spin_lock_irq(&io_request_lock);
1851 #if 0
1852 /* After finished, cancel any pending operations. */
1853 abort_read();
1854 #else
1855 /* Start a timer to time out after a while to disable
1856 the read. */
1857 cdu31a_abort_timer.expires = jiffies + 2*HZ; /* Wait 2 seconds */
1858 add_timer(&cdu31a_abort_timer);
1859 #endif
1861 has_cd_task = NULL;
1862 sony_inuse = 0;
1863 wake_up_interruptible(&sony_wait);
1864 restore_flags(flags);
1865 #if DEBUG
1866 printk("Leaving do_cdu31a_request at %d\n", __LINE__);
1867 #endif
1872 * Read the table of contents from the drive and set up TOC if
1873 * successful.
1875 static void
1876 sony_get_toc(void)
1878 unsigned char res_reg[2];
1879 unsigned int res_size;
1880 unsigned char parms[1];
1881 int session;
1882 int num_spin_ups;
1883 int totaltracks = 0;
1884 int mint = 99;
1885 int maxt = 0;
1887 #if DEBUG
1888 printk("Entering sony_get_toc\n");
1889 #endif
1891 num_spin_ups = 0;
1892 if (!sony_toc_read)
1894 respinup_on_gettoc:
1895 /* Ignore the result, since it might error if spinning already. */
1896 do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg, &res_size);
1898 do_sony_cd_cmd(SONY_READ_TOC_CMD, NULL, 0, res_reg, &res_size);
1900 /* The drive sometimes returns error 0. I don't know why, but ignore
1901 it. It seems to mean the drive has already done the operation. */
1902 if ((res_size < 2) || ((res_reg[0] != 0) && (res_reg[1] != 0)))
1904 /* If the drive is already playing, it's ok. */
1905 if ((res_reg[1] == SONY_AUDIO_PLAYING_ERR) || (res_reg[1] == 0))
1907 goto gettoc_drive_spinning;
1910 /* If the drive says it is not spun up (even though we just did it!)
1911 then retry the operation at least a few times. */
1912 if ( (res_reg[1] == SONY_NOT_SPIN_ERR)
1913 && (num_spin_ups < MAX_CDU31A_RETRIES))
1915 num_spin_ups++;
1916 goto respinup_on_gettoc;
1919 printk("cdu31a: Error reading TOC: %x %s\n",
1920 res_reg[0], translate_error(res_reg[1]));
1921 return;
1924 gettoc_drive_spinning:
1926 /* The idea here is we keep asking for sessions until the command
1927 fails. Then we know what the last valid session on the disk is.
1928 No need to check session 0, since session 0 is the same as session
1929 1; the command returns different information if you give it 0.
1931 #if DEBUG
1932 memset(&sony_toc, 0x0e, sizeof(sony_toc));
1933 memset(&single_toc, 0x0f, sizeof(single_toc));
1934 #endif
1935 session = 1;
1936 while (1)
1938 /* This seems to slow things down enough to make it work. This
1939 * appears to be a problem in do_sony_cd_cmd. This printk seems
1940 * to address the symptoms... -Erik */
1941 #if 1
1942 printk("cdu31a: Trying session %d\n", session);
1943 #endif
1944 parms[0] = session;
1945 do_sony_cd_cmd(SONY_READ_TOC_SPEC_CMD,
1946 parms,
1948 res_reg,
1949 &res_size);
1951 #if DEBUG
1952 printk("%2.2x %2.2x\n", res_reg[0], res_reg[1]);
1953 #endif
1955 if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20))
1957 /* An error reading the TOC, this must be past the last session. */
1958 if (session == 1)
1959 printk("Yikes! Couldn't read any sessions!");
1960 break;
1962 #if DEBUG
1963 printk("Reading session %d\n", session);
1964 #endif
1966 parms[0] = session;
1967 do_sony_cd_cmd(SONY_REQ_TOC_DATA_SPEC_CMD,
1968 parms,
1970 (unsigned char *) &single_toc,
1971 &res_size);
1972 if ((res_size < 2) || ((single_toc.exec_status[0] & 0xf0) == 0x20))
1974 printk("cdu31a: Error reading session %d: %x %s\n",
1975 session,
1976 single_toc.exec_status[0],
1977 translate_error(single_toc.exec_status[1]));
1978 /* An error reading the TOC. Return without sony_toc_read
1979 set. */
1980 return;
1982 #if DEBUG
1983 printk("add0 %01x, con0 %01x, poi0 %02x, 1st trk %d, dsktyp %x, dum0 %x\n",
1984 single_toc.address0, single_toc.control0, single_toc.point0,
1985 bcd_to_int(single_toc.first_track_num), single_toc.disk_type, single_toc.dummy0);
1986 printk("add1 %01x, con1 %01x, poi1 %02x, lst trk %d, dummy1 %x, dum2 %x\n",
1987 single_toc.address1, single_toc.control1, single_toc.point1,
1988 bcd_to_int(single_toc.last_track_num), single_toc.dummy1, single_toc.dummy2);
1989 printk("add2 %01x, con2 %01x, poi2 %02x leadout start min %d, sec %d, frame %d\n",
1990 single_toc.address2, single_toc.control2, single_toc.point2,
1991 bcd_to_int(single_toc.lead_out_start_msf[0]),
1992 bcd_to_int(single_toc.lead_out_start_msf[1]),
1993 bcd_to_int(single_toc.lead_out_start_msf[2]));
1994 if (res_size > 18 && single_toc.pointb0 > 0xaf)
1995 printk("addb0 %01x, conb0 %01x, poib0 %02x, nextsession min %d, sec %d, frame %d\n"
1996 "#mode5_ptrs %02d, max_start_outer_leadout_msf min %d, sec %d, frame %d\n",
1997 single_toc.addressb0, single_toc.controlb0, single_toc.pointb0,
1998 bcd_to_int(single_toc.next_poss_prog_area_msf[0]),
1999 bcd_to_int(single_toc.next_poss_prog_area_msf[1]),
2000 bcd_to_int(single_toc.next_poss_prog_area_msf[2]),
2001 single_toc.num_mode_5_pointers,
2002 bcd_to_int(single_toc.max_start_outer_leadout_msf[0]),
2003 bcd_to_int(single_toc.max_start_outer_leadout_msf[1]),
2004 bcd_to_int(single_toc.max_start_outer_leadout_msf[2]));
2005 if (res_size > 27 && single_toc.pointb1 > 0xaf)
2006 printk("addb1 %01x, conb1 %01x, poib1 %02x, %x %x %x %x #skipint_ptrs %d, #skiptrkassign %d %x\n",
2007 single_toc.addressb1, single_toc.controlb1, single_toc.pointb1,
2008 single_toc.dummyb0_1[0],
2009 single_toc.dummyb0_1[1],
2010 single_toc.dummyb0_1[2],
2011 single_toc.dummyb0_1[3],
2012 single_toc.num_skip_interval_pointers,
2013 single_toc.num_skip_track_assignments,
2014 single_toc.dummyb0_2);
2015 if (res_size > 36 && single_toc.pointb2 > 0xaf)
2016 printk("addb2 %01x, conb2 %01x, poib2 %02x, %02x %02x %02x %02x %02x %02x %02x\n",
2017 single_toc.addressb2, single_toc.controlb2, single_toc.pointb2,
2018 single_toc.tracksb2[0],
2019 single_toc.tracksb2[1],
2020 single_toc.tracksb2[2],
2021 single_toc.tracksb2[3],
2022 single_toc.tracksb2[4],
2023 single_toc.tracksb2[5],
2024 single_toc.tracksb2[6]);
2025 if (res_size > 45 && single_toc.pointb3 > 0xaf)
2026 printk("addb3 %01x, conb3 %01x, poib3 %02x, %02x %02x %02x %02x %02x %02x %02x\n",
2027 single_toc.addressb3, single_toc.controlb3, single_toc.pointb3,
2028 single_toc.tracksb3[0],
2029 single_toc.tracksb3[1],
2030 single_toc.tracksb3[2],
2031 single_toc.tracksb3[3],
2032 single_toc.tracksb3[4],
2033 single_toc.tracksb3[5],
2034 single_toc.tracksb3[6]);
2035 if (res_size > 54 && single_toc.pointb4 > 0xaf)
2036 printk("addb4 %01x, conb4 %01x, poib4 %02x, %02x %02x %02x %02x %02x %02x %02x\n",
2037 single_toc.addressb4, single_toc.controlb4, single_toc.pointb4,
2038 single_toc.tracksb4[0],
2039 single_toc.tracksb4[1],
2040 single_toc.tracksb4[2],
2041 single_toc.tracksb4[3],
2042 single_toc.tracksb4[4],
2043 single_toc.tracksb4[5],
2044 single_toc.tracksb4[6]);
2045 if (res_size > 63 && single_toc.pointc0 > 0xaf)
2046 printk("addc0 %01x, conc0 %01x, poic0 %02x, %02x %02x %02x %02x %02x %02x %02x\n",
2047 single_toc.addressc0, single_toc.controlc0, single_toc.pointc0,
2048 single_toc.dummyc0[0],
2049 single_toc.dummyc0[1],
2050 single_toc.dummyc0[2],
2051 single_toc.dummyc0[3],
2052 single_toc.dummyc0[4],
2053 single_toc.dummyc0[5],
2054 single_toc.dummyc0[6]);
2055 #endif
2056 #undef DEBUG
2057 #define DEBUG 0
2059 sony_toc.lead_out_start_msf[0] = bcd_to_int(single_toc.lead_out_start_msf[0]);
2060 sony_toc.lead_out_start_msf[1] = bcd_to_int(single_toc.lead_out_start_msf[1]);
2061 sony_toc.lead_out_start_msf[2] = bcd_to_int(single_toc.lead_out_start_msf[2]);
2062 sony_toc.lead_out_start_lba = single_toc.lead_out_start_lba =
2063 msf_to_log(sony_toc.lead_out_start_msf);
2065 /* For points that do not exist, move the data over them
2066 to the right location. */
2067 if (single_toc.pointb0 != 0xb0)
2069 memmove(((char *) &single_toc) + 27,
2070 ((char *) &single_toc) + 18,
2071 res_size - 18);
2072 res_size += 9;
2074 else if (res_size > 18) {
2075 sony_toc.lead_out_start_msf[0] = bcd_to_int(single_toc.max_start_outer_leadout_msf[0]);
2076 sony_toc.lead_out_start_msf[1] = bcd_to_int(single_toc.max_start_outer_leadout_msf[1]);
2077 sony_toc.lead_out_start_msf[2] = bcd_to_int(single_toc.max_start_outer_leadout_msf[2]);
2078 sony_toc.lead_out_start_lba = msf_to_log(sony_toc.lead_out_start_msf);
2080 if (single_toc.pointb1 != 0xb1)
2082 memmove(((char *) &single_toc) + 36,
2083 ((char *) &single_toc) + 27,
2084 res_size - 27);
2085 res_size += 9;
2087 if (single_toc.pointb2 != 0xb2)
2089 memmove(((char *) &single_toc) + 45,
2090 ((char *) &single_toc) + 36,
2091 res_size - 36);
2092 res_size += 9;
2094 if (single_toc.pointb3 != 0xb3)
2096 memmove(((char *) &single_toc) + 54,
2097 ((char *) &single_toc) + 45,
2098 res_size - 45);
2099 res_size += 9;
2101 if (single_toc.pointb4 != 0xb4)
2103 memmove(((char *) &single_toc) + 63,
2104 ((char *) &single_toc) + 54,
2105 res_size - 54);
2106 res_size += 9;
2108 if (single_toc.pointc0 != 0xc0)
2110 memmove(((char *) &single_toc) + 72,
2111 ((char *) &single_toc) + 63,
2112 res_size - 63);
2113 res_size += 9;
2116 #if DEBUG
2117 printk("start track lba %u, leadout start lba %u\n",
2118 single_toc.start_track_lba, single_toc.lead_out_start_lba);
2119 { int i;
2120 for (i = 0; i < 1 + bcd_to_int(single_toc.last_track_num)
2121 - bcd_to_int(single_toc.first_track_num); i++) {
2122 printk("trk %02d: add 0x%01x, con 0x%01x, track %02d, start min %02d, sec %02d, frame %02d\n",
2123 i, single_toc.tracks[i].address, single_toc.tracks[i].control,
2124 bcd_to_int(single_toc.tracks[i].track),
2125 bcd_to_int(single_toc.tracks[i].track_start_msf[0]),
2126 bcd_to_int(single_toc.tracks[i].track_start_msf[1]),
2127 bcd_to_int(single_toc.tracks[i].track_start_msf[2]));
2128 if (mint > bcd_to_int(single_toc.tracks[i].track))
2129 mint = bcd_to_int(single_toc.tracks[i].track);
2130 if (maxt < bcd_to_int(single_toc.tracks[i].track))
2131 maxt = bcd_to_int(single_toc.tracks[i].track);
2133 printk("min track number %d, max track number %d\n", mint, maxt);
2135 #endif
2137 /* prepare a special table of contents for a CD-I disc. They don't have one. */
2138 if (single_toc.disk_type == 0x10 &&
2139 single_toc.first_track_num == 2 &&
2140 single_toc.last_track_num == 2 /* CD-I */)
2142 sony_toc.tracks[totaltracks].address = 1;
2143 sony_toc.tracks[totaltracks].control = 4; /* force data tracks */
2144 sony_toc.tracks[totaltracks].track = 1;
2145 sony_toc.tracks[totaltracks].track_start_msf[0] = 0;
2146 sony_toc.tracks[totaltracks].track_start_msf[1] = 2;
2147 sony_toc.tracks[totaltracks].track_start_msf[2] = 0;
2148 mint = maxt = 1;
2149 totaltracks++;
2150 } else
2151 /* gather track entries from this session */
2152 { int i;
2153 for (i = 0; i < 1 + bcd_to_int(single_toc.last_track_num)
2154 - bcd_to_int(single_toc.first_track_num); i++, totaltracks++) {
2155 sony_toc.tracks[totaltracks].address = single_toc.tracks[i].address;
2156 sony_toc.tracks[totaltracks].control = single_toc.tracks[i].control;
2157 sony_toc.tracks[totaltracks].track = bcd_to_int(single_toc.tracks[i].track);
2158 sony_toc.tracks[totaltracks].track_start_msf[0] =
2159 bcd_to_int(single_toc.tracks[i].track_start_msf[0]);
2160 sony_toc.tracks[totaltracks].track_start_msf[1] =
2161 bcd_to_int(single_toc.tracks[i].track_start_msf[1]);
2162 sony_toc.tracks[totaltracks].track_start_msf[2] =
2163 bcd_to_int(single_toc.tracks[i].track_start_msf[2]);
2164 if (i == 0)
2165 single_toc.start_track_lba = msf_to_log(sony_toc.tracks[totaltracks].track_start_msf);
2166 if (mint > sony_toc.tracks[totaltracks].track)
2167 mint = sony_toc.tracks[totaltracks].track;
2168 if (maxt < sony_toc.tracks[totaltracks].track)
2169 maxt = sony_toc.tracks[totaltracks].track;
2172 sony_toc.first_track_num = mint;
2173 sony_toc.last_track_num = maxt;
2174 /* Disk type of last session wins. For example:
2175 CD-Extra has disk type 0 for the first session, so
2176 a dumb HiFi CD player thinks it is a plain audio CD.
2177 We are interested in the disk type of the last session,
2178 which is 0x20 (XA) for CD-Extra, so we can access the
2179 data track ... */
2180 sony_toc.disk_type = single_toc.disk_type;
2181 sony_toc.sessions = session;
2183 /* don't believe everything :-) */
2184 if (session == 1)
2185 single_toc.start_track_lba = 0;
2186 sony_toc.start_track_lba = single_toc.start_track_lba;
2188 if (session > 1 && single_toc.pointb0 == 0xb0 &&
2189 sony_toc.lead_out_start_lba == single_toc.lead_out_start_lba)
2191 break;
2194 /* Let's not get carried away... */
2195 if (session > 40)
2197 printk("cdu31a: too many sessions: %d\n", session);
2198 break;
2200 session++;
2202 sony_toc.track_entries = totaltracks;
2203 /* add one entry for the LAST track with track number CDROM_LEADOUT */
2204 sony_toc.tracks[totaltracks].address = single_toc.address2;
2205 sony_toc.tracks[totaltracks].control = single_toc.control2;
2206 sony_toc.tracks[totaltracks].track = CDROM_LEADOUT;
2207 sony_toc.tracks[totaltracks].track_start_msf[0] =
2208 sony_toc.lead_out_start_msf[0];
2209 sony_toc.tracks[totaltracks].track_start_msf[1] =
2210 sony_toc.lead_out_start_msf[1];
2211 sony_toc.tracks[totaltracks].track_start_msf[2] =
2212 sony_toc.lead_out_start_msf[2];
2214 sony_toc_read = 1;
2215 #undef DEBUG
2216 #if DEBUG
2217 printk("Disk session %d, start track: %d, stop track: %d\n",
2218 session,
2219 single_toc.start_track_lba,
2220 single_toc.lead_out_start_lba);
2221 #endif
2223 #if DEBUG
2224 printk("Leaving sony_get_toc\n");
2225 #endif
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)
2237 return 1;
2239 if (!sony_toc_read)
2240 sony_get_toc();
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 */;
2247 return 0;
2251 * Search for a specific track in the table of contents.
2253 static int
2254 find_track(int track)
2256 int i;
2258 for (i = 0; i <= sony_toc.track_entries; i++)
2260 if (sony_toc.tracks[i].track == track)
2262 return i;
2266 return -1;
2271 * Read the subcode and put it in last_sony_subcode for future use.
2273 static int
2274 read_subcode(void)
2276 unsigned int res_size;
2279 do_sony_cd_cmd(SONY_REQ_SUBCODE_ADDRESS_CMD,
2280 NULL,
2282 (unsigned char *) &last_sony_subcode,
2283 &res_size);
2284 if ((res_size < 2) || ((last_sony_subcode.exec_status[0] & 0xf0) == 0x20))
2286 printk("Sony CDROM error %s (read_subcode)\n",
2287 translate_error(last_sony_subcode.exec_status[1]));
2288 return -EIO;
2291 last_sony_subcode.track_num = bcd_to_int(last_sony_subcode.track_num);
2292 last_sony_subcode.index_num = bcd_to_int(last_sony_subcode.index_num);
2293 last_sony_subcode.abs_msf[0] = bcd_to_int(last_sony_subcode.abs_msf[0]);
2294 last_sony_subcode.abs_msf[1] = bcd_to_int(last_sony_subcode.abs_msf[1]);
2295 last_sony_subcode.abs_msf[2] = bcd_to_int(last_sony_subcode.abs_msf[2]);
2297 last_sony_subcode.rel_msf[0] = bcd_to_int(last_sony_subcode.rel_msf[0]);
2298 last_sony_subcode.rel_msf[1] = bcd_to_int(last_sony_subcode.rel_msf[1]);
2299 last_sony_subcode.rel_msf[2] = bcd_to_int(last_sony_subcode.rel_msf[2]);
2300 return 0;
2304 * Uniform cdrom interface function
2305 * return the media catalog number found on some older audio cds
2307 static int
2308 scd_get_mcn(struct cdrom_device_info *cdi, struct cdrom_mcn *mcn)
2310 unsigned char resbuffer[2 + 14];
2311 unsigned char *mcnp = mcn->medium_catalog_number;
2312 unsigned char *resp = resbuffer + 3;
2313 unsigned int res_size;
2315 memset(mcn->medium_catalog_number, 0, 14);
2316 do_sony_cd_cmd(SONY_REQ_UPC_EAN_CMD,
2317 NULL,
2319 resbuffer,
2320 &res_size);
2321 if ((res_size < 2) || ((resbuffer[0] & 0xf0) == 0x20))
2323 else {
2324 /* packed bcd to single ASCII digits */
2325 *mcnp++ = (*resp >> 4) + '0';
2326 *mcnp++ = (*resp++ & 0x0f) + '0';
2327 *mcnp++ = (*resp >> 4) + '0';
2328 *mcnp++ = (*resp++ & 0x0f) + '0';
2329 *mcnp++ = (*resp >> 4) + '0';
2330 *mcnp++ = (*resp++ & 0x0f) + '0';
2331 *mcnp++ = (*resp >> 4) + '0';
2332 *mcnp++ = (*resp++ & 0x0f) + '0';
2333 *mcnp++ = (*resp >> 4) + '0';
2334 *mcnp++ = (*resp++ & 0x0f) + '0';
2335 *mcnp++ = (*resp >> 4) + '0';
2336 *mcnp++ = (*resp++ & 0x0f) + '0';
2337 *mcnp++ = (*resp >> 4) + '0';
2339 *mcnp = '\0';
2340 return 0;
2345 * Get the subchannel info like the CDROMSUBCHNL command wants to see it. If
2346 * the drive is playing, the subchannel needs to be read (since it would be
2347 * changing). If the drive is paused or completed, the subcode information has
2348 * already been stored, just use that. The ioctl call wants things in decimal
2349 * (not BCD), so all the conversions are done.
2351 static int
2352 sony_get_subchnl_info(struct cdrom_subchnl *schi)
2354 /* Get attention stuff */
2355 while (handle_sony_cd_attention())
2358 sony_get_toc();
2359 if (!sony_toc_read)
2361 return -EIO;
2364 switch (sony_audio_status)
2366 case CDROM_AUDIO_NO_STATUS:
2367 case CDROM_AUDIO_PLAY:
2368 if (read_subcode() < 0)
2370 return -EIO;
2372 break;
2374 case CDROM_AUDIO_PAUSED:
2375 case CDROM_AUDIO_COMPLETED:
2376 break;
2378 #if 0
2379 case CDROM_AUDIO_NO_STATUS:
2380 schi->cdsc_audiostatus = sony_audio_status;
2381 return 0;
2382 break;
2383 #endif
2384 case CDROM_AUDIO_INVALID:
2385 case CDROM_AUDIO_ERROR:
2386 default:
2387 return -EIO;
2390 schi->cdsc_audiostatus = sony_audio_status;
2391 schi->cdsc_adr = last_sony_subcode.address;
2392 schi->cdsc_ctrl = last_sony_subcode.control;
2393 schi->cdsc_trk = last_sony_subcode.track_num;
2394 schi->cdsc_ind = last_sony_subcode.index_num;
2395 if (schi->cdsc_format == CDROM_MSF)
2397 schi->cdsc_absaddr.msf.minute = last_sony_subcode.abs_msf[0];
2398 schi->cdsc_absaddr.msf.second = last_sony_subcode.abs_msf[1];
2399 schi->cdsc_absaddr.msf.frame = last_sony_subcode.abs_msf[2];
2401 schi->cdsc_reladdr.msf.minute = last_sony_subcode.rel_msf[0];
2402 schi->cdsc_reladdr.msf.second = last_sony_subcode.rel_msf[1];
2403 schi->cdsc_reladdr.msf.frame = last_sony_subcode.rel_msf[2];
2405 else if (schi->cdsc_format == CDROM_LBA)
2407 schi->cdsc_absaddr.lba = msf_to_log(last_sony_subcode.abs_msf);
2408 schi->cdsc_reladdr.lba = msf_to_log(last_sony_subcode.rel_msf);
2411 return 0;
2414 /* Get audio data from the drive. This is fairly complex because I
2415 am looking for status and data at the same time, but if I get status
2416 then I just look for data. I need to get the status immediately so
2417 the switch from audio to data tracks will happen quickly. */
2418 static void
2419 read_audio_data(char *buffer,
2420 unsigned char res_reg[],
2421 int *res_size)
2423 unsigned int retry_count;
2424 int result_read;
2427 res_reg[0] = 0;
2428 res_reg[1] = 0;
2429 *res_size = 0;
2430 result_read = 0;
2432 /* Wait for the drive to tell us we have something */
2433 retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
2434 continue_read_audio_wait:
2435 while (time_before(jiffies, retry_count) && !(is_data_ready())
2436 && !(is_result_ready() || result_read))
2438 while (handle_sony_cd_attention())
2441 sony_sleep();
2443 if (!(is_data_ready()))
2445 if (is_result_ready() && !result_read)
2447 get_result(res_reg, res_size);
2449 /* Read block status and continue waiting for data. */
2450 if ((res_reg[0] & 0xf0) == 0x50)
2452 result_read = 1;
2453 goto continue_read_audio_wait;
2455 /* Invalid data from the drive. Shut down the operation. */
2456 else if ((res_reg[0] & 0xf0) != 0x20)
2458 printk("CDU31A: Got result that should have been error: %d\n",
2459 res_reg[0]);
2460 res_reg[0] = 0x20;
2461 res_reg[1] = SONY_BAD_DATA_ERR;
2462 *res_size = 2;
2464 abort_read();
2466 else
2468 #if DEBUG
2469 printk("CDU31A timeout out %d\n", __LINE__);
2470 #endif
2471 res_reg[0] = 0x20;
2472 res_reg[1] = SONY_TIMEOUT_OP_ERR;
2473 *res_size = 2;
2474 abort_read();
2477 else
2479 clear_data_ready();
2481 /* If data block, then get 2340 bytes offset by 12. */
2482 if (sony_raw_data_mode)
2484 insb(sony_cd_read_reg, buffer + CD_XA_HEAD, CD_FRAMESIZE_RAW1);
2486 else
2488 /* Audio gets the whole 2352 bytes. */
2489 insb(sony_cd_read_reg, buffer, CD_FRAMESIZE_RAW);
2492 /* If I haven't already gotten the result, get it now. */
2493 if (!result_read)
2495 /* Wait for the drive to tell us we have something */
2496 retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
2497 while (time_before(jiffies, retry_count) && !(is_result_ready()))
2499 while (handle_sony_cd_attention())
2502 sony_sleep();
2505 if (!is_result_ready())
2507 #if DEBUG
2508 printk("CDU31A timeout out %d\n", __LINE__);
2509 #endif
2510 res_reg[0] = 0x20;
2511 res_reg[1] = SONY_TIMEOUT_OP_ERR;
2512 *res_size = 2;
2513 abort_read();
2514 return;
2516 else
2518 get_result(res_reg, res_size);
2522 if ((res_reg[0] & 0xf0) == 0x50)
2524 if ( (res_reg[0] == SONY_NO_CIRC_ERR_BLK_STAT)
2525 || (res_reg[0] == SONY_NO_LECC_ERR_BLK_STAT)
2526 || (res_reg[0] == SONY_RECOV_LECC_ERR_BLK_STAT)
2527 || (res_reg[0] == SONY_NO_ERR_DETECTION_STAT))
2529 /* Ok, nothing to do. */
2531 else
2533 printk("CDU31A: Data block error: 0x%x\n", res_reg[0]);
2534 res_reg[0] = 0x20;
2535 res_reg[1] = SONY_BAD_DATA_ERR;
2536 *res_size = 2;
2539 else if ((res_reg[0] & 0xf0) != 0x20)
2541 /* The drive gave me bad status, I don't know what to do.
2542 Reset the driver and return an error. */
2543 printk("CDU31A: Invalid block status: 0x%x\n", res_reg[0]);
2544 restart_on_error();
2545 res_reg[0] = 0x20;
2546 res_reg[1] = SONY_BAD_DATA_ERR;
2547 *res_size = 2;
2552 /* Perform a raw data read. This will automatically detect the
2553 track type and read the proper data (audio or data). */
2554 static int
2555 read_audio(struct cdrom_read_audio *ra)
2557 int retval;
2558 unsigned char params[2];
2559 unsigned char res_reg[12];
2560 unsigned int res_size;
2561 unsigned int cframe;
2562 unsigned long flags;
2565 * Make sure no one else is using the driver; wait for them
2566 * to finish if it is so.
2568 save_flags(flags);
2569 cli();
2570 while (sony_inuse)
2572 interruptible_sleep_on(&sony_wait);
2573 if (signal_pending(current))
2575 restore_flags(flags);
2576 return -EAGAIN;
2579 sony_inuse = 1;
2580 has_cd_task = current;
2581 restore_flags(flags);
2583 if (!sony_spun_up)
2585 scd_spinup();
2588 /* Set the drive to do raw operations. */
2589 params[0] = SONY_SD_DECODE_PARAM;
2590 params[1] = 0x06 | sony_raw_data_mode;
2591 do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
2592 params,
2594 res_reg,
2595 &res_size);
2596 if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20))
2598 printk("CDU31A: Unable to set decode params: 0x%2.2x\n", res_reg[1]);
2599 return -EIO;
2602 /* From here down, we have to goto exit_read_audio instead of returning
2603 because the drive parameters have to be set back to data before
2604 return. */
2606 retval = 0;
2607 /* start_request clears out any readahead data, so it should be safe. */
2608 if (start_request(ra->addr.lba, ra->nframes, 1))
2610 retval = -EIO;
2611 goto exit_read_audio;
2614 /* For every requested frame. */
2615 cframe = 0;
2616 while (cframe < ra->nframes)
2618 read_audio_data(readahead_buffer, res_reg, &res_size);
2619 if ((res_reg[0] & 0xf0) == 0x20)
2621 if (res_reg[1] == SONY_BAD_DATA_ERR)
2623 printk("CDU31A: Data error on audio sector %d\n",
2624 ra->addr.lba + cframe);
2626 else if (res_reg[1] == SONY_ILL_TRACK_R_ERR)
2628 /* Illegal track type, change track types and start over. */
2629 sony_raw_data_mode = (sony_raw_data_mode) ? 0 : 1;
2631 /* Set the drive mode. */
2632 params[0] = SONY_SD_DECODE_PARAM;
2633 params[1] = 0x06 | sony_raw_data_mode;
2634 do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
2635 params,
2637 res_reg,
2638 &res_size);
2639 if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20))
2641 printk("CDU31A: Unable to set decode params: 0x%2.2x\n", res_reg[1]);
2642 retval = -EIO;
2643 goto exit_read_audio;
2646 /* Restart the request on the current frame. */
2647 if (start_request(ra->addr.lba + cframe, ra->nframes - cframe, 1))
2649 retval = -EIO;
2650 goto exit_read_audio;
2653 /* Don't go back to the top because don't want to get into
2654 and infinite loop. A lot of code gets duplicated, but
2655 that's no big deal, I don't guess. */
2656 read_audio_data(readahead_buffer, res_reg, &res_size);
2657 if ((res_reg[0] & 0xf0) == 0x20)
2659 if (res_reg[1] == SONY_BAD_DATA_ERR)
2661 printk("CDU31A: Data error on audio sector %d\n",
2662 ra->addr.lba + cframe);
2664 else
2666 printk("CDU31A: Error reading audio data on sector %d: %s\n",
2667 ra->addr.lba + cframe,
2668 translate_error(res_reg[1]));
2669 retval = -EIO;
2670 goto exit_read_audio;
2673 else
2675 copy_to_user((char *) (ra->buf + (CD_FRAMESIZE_RAW * cframe)),
2676 (char *) readahead_buffer,
2677 CD_FRAMESIZE_RAW);
2680 else
2682 printk("CDU31A: Error reading audio data on sector %d: %s\n",
2683 ra->addr.lba + cframe,
2684 translate_error(res_reg[1]));
2685 retval = -EIO;
2686 goto exit_read_audio;
2689 else
2691 copy_to_user((char *) (ra->buf + (CD_FRAMESIZE_RAW * cframe)),
2692 (char *) readahead_buffer,
2693 CD_FRAMESIZE_RAW);
2696 cframe++;
2699 get_result(res_reg, &res_size);
2700 if ((res_reg[0] & 0xf0) == 0x20)
2702 printk("CDU31A: Error return from audio read: %s\n",
2703 translate_error(res_reg[1]));
2704 retval = -EIO;
2705 goto exit_read_audio;
2708 exit_read_audio:
2710 /* Set the drive mode back to the proper one for the disk. */
2711 params[0] = SONY_SD_DECODE_PARAM;
2712 if (!sony_xa_mode)
2714 params[1] = 0x0f;
2716 else
2718 params[1] = 0x07;
2720 do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
2721 params,
2723 res_reg,
2724 &res_size);
2725 if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20))
2727 printk("CDU31A: Unable to reset decode params: 0x%2.2x\n", res_reg[1]);
2728 return -EIO;
2731 has_cd_task = NULL;
2732 sony_inuse = 0;
2733 wake_up_interruptible(&sony_wait);
2735 return(retval);
2738 static int
2739 do_sony_cd_cmd_chk(const char *name,
2740 unsigned char cmd,
2741 unsigned char *params,
2742 unsigned int num_params,
2743 unsigned char *result_buffer,
2744 unsigned int *result_size)
2746 do_sony_cd_cmd(cmd, params, num_params, result_buffer, result_size);
2747 if ((*result_size < 2) || ((result_buffer[0] & 0xf0) == 0x20))
2749 printk("Sony CDROM error %s (CDROM%s)\n", translate_error(result_buffer[1]), name);
2750 return -EIO;
2752 return 0;
2756 * Uniform cdrom interface function
2757 * open the tray
2759 static int scd_tray_move(struct cdrom_device_info *cdi, int position)
2761 if (position == 1 /* open tray */)
2763 unsigned char res_reg[12];
2764 unsigned int res_size;
2766 do_sony_cd_cmd(SONY_AUDIO_STOP_CMD, NULL, 0, res_reg, &res_size);
2767 do_sony_cd_cmd(SONY_SPIN_DOWN_CMD, NULL, 0, res_reg, &res_size);
2769 sony_audio_status = CDROM_AUDIO_INVALID;
2770 return do_sony_cd_cmd_chk("EJECT",SONY_EJECT_CMD, NULL, 0, res_reg, &res_size);
2771 } else {
2772 if (0 == scd_spinup())
2773 sony_spun_up = 1;
2774 return 0;
2779 * The big ugly ioctl handler.
2781 static int scd_audio_ioctl(struct cdrom_device_info *cdi,
2782 unsigned int cmd,
2783 void * arg)
2785 unsigned char res_reg[12];
2786 unsigned int res_size;
2787 unsigned char params[7];
2788 int i;
2791 switch (cmd)
2793 case CDROMSTART: /* Spin up the drive */
2794 return do_sony_cd_cmd_chk("START",SONY_SPIN_UP_CMD, NULL, 0, res_reg, &res_size);
2795 break;
2797 case CDROMSTOP: /* Spin down the drive */
2798 do_sony_cd_cmd(SONY_AUDIO_STOP_CMD, NULL, 0, res_reg, &res_size);
2801 * Spin the drive down, ignoring the error if the disk was
2802 * already not spinning.
2804 sony_audio_status = CDROM_AUDIO_NO_STATUS;
2805 return do_sony_cd_cmd_chk("STOP",SONY_SPIN_DOWN_CMD, NULL, 0, res_reg, &res_size);
2807 case CDROMPAUSE: /* Pause the drive */
2808 if(do_sony_cd_cmd_chk("PAUSE", SONY_AUDIO_STOP_CMD, NULL, 0, res_reg, &res_size))
2809 return -EIO;
2810 /* Get the current position and save it for resuming */
2811 if (read_subcode() < 0)
2813 return -EIO;
2815 cur_pos_msf[0] = last_sony_subcode.abs_msf[0];
2816 cur_pos_msf[1] = last_sony_subcode.abs_msf[1];
2817 cur_pos_msf[2] = last_sony_subcode.abs_msf[2];
2818 sony_audio_status = CDROM_AUDIO_PAUSED;
2819 return 0;
2820 break;
2822 case CDROMRESUME: /* Start the drive after being paused */
2823 if (sony_audio_status != CDROM_AUDIO_PAUSED)
2825 return -EINVAL;
2828 do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg, &res_size);
2830 /* Start the drive at the saved position. */
2831 params[1] = int_to_bcd(cur_pos_msf[0]);
2832 params[2] = int_to_bcd(cur_pos_msf[1]);
2833 params[3] = int_to_bcd(cur_pos_msf[2]);
2834 params[4] = int_to_bcd(final_pos_msf[0]);
2835 params[5] = int_to_bcd(final_pos_msf[1]);
2836 params[6] = int_to_bcd(final_pos_msf[2]);
2837 params[0] = 0x03;
2838 if(do_sony_cd_cmd_chk("RESUME",SONY_AUDIO_PLAYBACK_CMD, params, 7, res_reg, &res_size)<0)
2839 return -EIO;
2840 sony_audio_status = CDROM_AUDIO_PLAY;
2841 return 0;
2843 case CDROMPLAYMSF: /* Play starting at the given MSF address. */
2844 do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg, &res_size);
2846 /* The parameters are given in int, must be converted */
2847 for (i=1; i<7; i++)
2849 params[i] = int_to_bcd(((unsigned char *)arg)[i-1]);
2851 params[0] = 0x03;
2852 if(do_sony_cd_cmd_chk("PLAYMSF",SONY_AUDIO_PLAYBACK_CMD, params, 7, res_reg, &res_size)<0)
2853 return -EIO;
2855 /* Save the final position for pauses and resumes */
2856 final_pos_msf[0] = bcd_to_int(params[4]);
2857 final_pos_msf[1] = bcd_to_int(params[5]);
2858 final_pos_msf[2] = bcd_to_int(params[6]);
2859 sony_audio_status = CDROM_AUDIO_PLAY;
2860 return 0;
2862 case CDROMREADTOCHDR: /* Read the table of contents header */
2864 struct cdrom_tochdr *hdr;
2866 sony_get_toc();
2867 if (!sony_toc_read)
2869 return -EIO;
2872 hdr = (struct cdrom_tochdr *) arg;
2873 hdr->cdth_trk0 = sony_toc.first_track_num;
2874 hdr->cdth_trk1 = sony_toc.last_track_num;
2876 return 0;
2878 case CDROMREADTOCENTRY: /* Read a given table of contents entry */
2880 struct cdrom_tocentry *entry;
2881 int track_idx;
2882 unsigned char *msf_val = NULL;
2884 sony_get_toc();
2885 if (!sony_toc_read)
2887 return -EIO;
2890 entry = (struct cdrom_tocentry *) arg;
2892 track_idx = find_track(entry->cdte_track);
2893 if (track_idx < 0)
2895 return -EINVAL;
2898 entry->cdte_adr = sony_toc.tracks[track_idx].address;
2899 entry->cdte_ctrl = sony_toc.tracks[track_idx].control;
2900 msf_val = sony_toc.tracks[track_idx].track_start_msf;
2902 /* Logical buffer address or MSF format requested? */
2903 if (entry->cdte_format == CDROM_LBA)
2905 entry->cdte_addr.lba = msf_to_log(msf_val);
2907 else if (entry->cdte_format == CDROM_MSF)
2909 entry->cdte_addr.msf.minute = *msf_val;
2910 entry->cdte_addr.msf.second = *(msf_val+1);
2911 entry->cdte_addr.msf.frame = *(msf_val+2);
2914 return 0;
2915 break;
2917 case CDROMPLAYTRKIND: /* Play a track. This currently ignores index. */
2919 struct cdrom_ti *ti = (struct cdrom_ti *) arg;
2920 int track_idx;
2922 sony_get_toc();
2923 if (!sony_toc_read)
2925 return -EIO;
2928 if ( (ti->cdti_trk0 < sony_toc.first_track_num)
2929 || (ti->cdti_trk0 > sony_toc.last_track_num)
2930 || (ti->cdti_trk1 < ti->cdti_trk0))
2932 return -EINVAL;
2935 track_idx = find_track(ti->cdti_trk0);
2936 if (track_idx < 0)
2938 return -EINVAL;
2940 params[1] = int_to_bcd(sony_toc.tracks[track_idx].track_start_msf[0]);
2941 params[2] = int_to_bcd(sony_toc.tracks[track_idx].track_start_msf[1]);
2942 params[3] = int_to_bcd(sony_toc.tracks[track_idx].track_start_msf[2]);
2945 * If we want to stop after the last track, use the lead-out
2946 * MSF to do that.
2948 if (ti->cdti_trk1 >= sony_toc.last_track_num)
2950 track_idx = find_track(CDROM_LEADOUT);
2952 else
2954 track_idx = find_track(ti->cdti_trk1+1);
2956 if (track_idx < 0)
2958 return -EINVAL;
2960 params[4] = int_to_bcd(sony_toc.tracks[track_idx].track_start_msf[0]);
2961 params[5] = int_to_bcd(sony_toc.tracks[track_idx].track_start_msf[1]);
2962 params[6] = int_to_bcd(sony_toc.tracks[track_idx].track_start_msf[2]);
2963 params[0] = 0x03;
2965 do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg, &res_size);
2967 do_sony_cd_cmd(SONY_AUDIO_PLAYBACK_CMD, params, 7, res_reg, &res_size);
2969 if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20))
2971 printk("Params: %x %x %x %x %x %x %x\n", params[0], params[1],
2972 params[2], params[3], params[4], params[5], params[6]);
2973 printk("Sony CDROM error %s (CDROMPLAYTRKIND)\n", translate_error(res_reg[1]));
2974 return -EIO;
2977 /* Save the final position for pauses and resumes */
2978 final_pos_msf[0] = bcd_to_int(params[4]);
2979 final_pos_msf[1] = bcd_to_int(params[5]);
2980 final_pos_msf[2] = bcd_to_int(params[6]);
2981 sony_audio_status = CDROM_AUDIO_PLAY;
2982 return 0;
2985 case CDROMVOLCTRL: /* Volume control. What volume does this change, anyway? */
2987 struct cdrom_volctrl *volctrl = (struct cdrom_volctrl *) arg;
2989 params[0] = SONY_SD_AUDIO_VOLUME;
2990 params[1] = volctrl->channel0;
2991 params[2] = volctrl->channel1;
2992 return do_sony_cd_cmd_chk("VOLCTRL",SONY_SET_DRIVE_PARAM_CMD, params, 3, res_reg, &res_size);
2994 case CDROMSUBCHNL: /* Get subchannel info */
2995 return sony_get_subchnl_info((struct cdrom_subchnl *)arg);
2997 default:
2998 return -EINVAL;
3002 static int scd_dev_ioctl(struct cdrom_device_info *cdi,
3003 unsigned int cmd,
3004 unsigned long arg)
3006 int i;
3008 switch (cmd)
3010 case CDROMREADAUDIO: /* Read 2352 byte audio tracks and 2340 byte
3011 raw data tracks. */
3013 struct cdrom_read_audio ra;
3016 sony_get_toc();
3017 if (!sony_toc_read)
3019 return -EIO;
3022 if(copy_from_user(&ra, (char *) arg, sizeof(ra)))
3023 return -EFAULT;
3025 if (ra.nframes == 0)
3027 return 0;
3030 i=verify_area(VERIFY_WRITE, ra.buf, CD_FRAMESIZE_RAW * ra.nframes);
3031 if(i<0)
3032 return i;
3034 if (ra.addr_format == CDROM_LBA)
3036 if ( (ra.addr.lba >= sony_toc.lead_out_start_lba)
3037 || (ra.addr.lba + ra.nframes >= sony_toc.lead_out_start_lba))
3039 return -EINVAL;
3042 else if (ra.addr_format == CDROM_MSF)
3044 if ( (ra.addr.msf.minute >= 75)
3045 || (ra.addr.msf.second >= 60)
3046 || (ra.addr.msf.frame >= 75))
3048 return -EINVAL;
3051 ra.addr.lba = ( (ra.addr.msf.minute * 4500)
3052 + (ra.addr.msf.second * 75)
3053 + ra.addr.msf.frame);
3054 if ( (ra.addr.lba >= sony_toc.lead_out_start_lba)
3055 || (ra.addr.lba + ra.nframes >= sony_toc.lead_out_start_lba))
3057 return -EINVAL;
3060 /* I know, this can go negative on an unsigned. However,
3061 the first thing done to the data is to add this value,
3062 so this should compensate and allow direct msf access. */
3063 ra.addr.lba -= LOG_START_OFFSET;
3065 else
3067 return -EINVAL;
3070 return(read_audio(&ra));
3072 return 0;
3073 break;
3075 default:
3076 return -EINVAL;
3080 static int scd_spinup(void)
3082 unsigned char res_reg[12];
3083 unsigned int res_size;
3084 int num_spin_ups;
3086 num_spin_ups = 0;
3088 respinup_on_open:
3089 do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg, &res_size);
3091 /* The drive sometimes returns error 0. I don't know why, but ignore
3092 it. It seems to mean the drive has already done the operation. */
3093 if ((res_size < 2) || ((res_reg[0] != 0) && (res_reg[1] != 0)))
3095 printk("Sony CDROM %s error (scd_open, spin up)\n", translate_error(res_reg[1]));
3096 return 1;
3099 do_sony_cd_cmd(SONY_READ_TOC_CMD, NULL, 0, res_reg, &res_size);
3101 /* The drive sometimes returns error 0. I don't know why, but ignore
3102 it. It seems to mean the drive has already done the operation. */
3103 if ((res_size < 2) || ((res_reg[0] != 0) && (res_reg[1] != 0)))
3105 /* If the drive is already playing, it's ok. */
3106 if ((res_reg[1] == SONY_AUDIO_PLAYING_ERR) || (res_reg[1] == 0))
3108 return 0;
3111 /* If the drive says it is not spun up (even though we just did it!)
3112 then retry the operation at least a few times. */
3113 if ( (res_reg[1] == SONY_NOT_SPIN_ERR)
3114 && (num_spin_ups < MAX_CDU31A_RETRIES))
3116 num_spin_ups++;
3117 goto respinup_on_open;
3120 printk("Sony CDROM error %s (scd_open, read toc)\n", translate_error(res_reg[1]));
3121 do_sony_cd_cmd(SONY_SPIN_DOWN_CMD, NULL, 0, res_reg, &res_size);
3122 return 1;
3124 return 0;
3128 * Open the drive for operations. Spin the drive up and read the table of
3129 * contents if these have not already been done.
3131 static int
3132 scd_open(struct cdrom_device_info *cdi, int openmode)
3134 unsigned char res_reg[12];
3135 unsigned int res_size;
3136 unsigned char params[2];
3138 MOD_INC_USE_COUNT;
3139 if (sony_usage == 0)
3141 if (scd_spinup() != 0) {
3142 MOD_DEC_USE_COUNT;
3143 return -EIO;
3145 sony_get_toc();
3146 if (!sony_toc_read)
3148 do_sony_cd_cmd(SONY_SPIN_DOWN_CMD, NULL, 0, res_reg, &res_size);
3149 MOD_DEC_USE_COUNT;
3150 return -EIO;
3153 /* For XA on the CDU31A only, we have to do special reads.
3154 The CDU33A handles XA automagically. */
3155 /* if ( (sony_toc.disk_type == SONY_XA_DISK_TYPE) */
3156 if ( (sony_toc.disk_type != 0x00)
3157 && (!is_double_speed))
3159 params[0] = SONY_SD_DECODE_PARAM;
3160 params[1] = 0x07;
3161 do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
3162 params,
3164 res_reg,
3165 &res_size);
3166 if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20))
3168 printk("CDU31A: Unable to set XA params: 0x%2.2x\n", res_reg[1]);
3170 sony_xa_mode = 1;
3172 /* A non-XA disk. Set the parms back if necessary. */
3173 else if (sony_xa_mode)
3175 params[0] = SONY_SD_DECODE_PARAM;
3176 params[1] = 0x0f;
3177 do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
3178 params,
3180 res_reg,
3181 &res_size);
3182 if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20))
3184 printk("CDU31A: Unable to reset XA params: 0x%2.2x\n", res_reg[1]);
3186 sony_xa_mode = 0;
3189 sony_spun_up = 1;
3192 sony_usage++;
3194 return 0;
3199 * Close the drive. Spin it down if no task is using it. The spin
3200 * down will fail if playing audio, so audio play is OK.
3202 static void
3203 scd_release(struct cdrom_device_info *cdi)
3205 if (sony_usage == 1)
3207 unsigned char res_reg[12];
3208 unsigned int res_size;
3210 do_sony_cd_cmd(SONY_SPIN_DOWN_CMD, NULL, 0, res_reg, &res_size);
3212 sony_spun_up = 0;
3214 sony_usage--;
3215 MOD_DEC_USE_COUNT;
3218 static struct cdrom_device_ops scd_dops = {
3219 scd_open, /* open */
3220 scd_release, /* release */
3221 scd_drive_status, /* drive status */
3222 scd_media_changed, /* media changed */
3223 scd_tray_move, /* tray move */
3224 scd_lock_door, /* lock door */
3225 scd_select_speed, /* select speed */
3226 NULL, /* select disc */
3227 scd_get_last_session, /* get last session */
3228 scd_get_mcn, /* get universal product code */
3229 scd_reset, /* hard reset */
3230 scd_audio_ioctl, /* audio ioctl */
3231 scd_dev_ioctl, /* device-specific ioctl */
3232 CDC_OPEN_TRAY | CDC_CLOSE_TRAY | CDC_LOCK | CDC_SELECT_SPEED | CDC_MULTI_SESSION |
3233 CDC_MULTI_SESSION | CDC_MCN | CDC_MEDIA_CHANGED | CDC_PLAY_AUDIO |
3234 CDC_RESET | CDC_IOCTLS | CDC_DRIVE_STATUS, /* capability */
3235 1, /* number of minor devices */
3238 static struct cdrom_device_info scd_info = {
3239 &scd_dops, /* device operations */
3240 NULL, /* link */
3241 NULL, /* handle */
3242 0, /* dev */
3243 0, /* mask */
3244 2, /* maximum speed */
3245 1, /* number of discs */
3246 0, /* options, not owned */
3247 0, /* mc_flags, not owned */
3248 0 /* use count, not owned */
3251 /* The different types of disc loading mechanisms supported */
3252 static const char *load_mech[] __initdata = { "caddy", "tray", "pop-up", "unknown" };
3254 static void __init
3255 get_drive_configuration(unsigned short base_io,
3256 unsigned char res_reg[],
3257 unsigned int *res_size)
3259 int retry_count;
3262 /* Set the base address */
3263 cdu31a_port = base_io;
3265 /* Set up all the register locations */
3266 sony_cd_cmd_reg = cdu31a_port + SONY_CMD_REG_OFFSET;
3267 sony_cd_param_reg = cdu31a_port + SONY_PARAM_REG_OFFSET;
3268 sony_cd_write_reg = cdu31a_port + SONY_WRITE_REG_OFFSET;
3269 sony_cd_control_reg = cdu31a_port + SONY_CONTROL_REG_OFFSET;
3270 sony_cd_status_reg = cdu31a_port + SONY_STATUS_REG_OFFSET;
3271 sony_cd_result_reg = cdu31a_port + SONY_RESULT_REG_OFFSET;
3272 sony_cd_read_reg = cdu31a_port + SONY_READ_REG_OFFSET;
3273 sony_cd_fifost_reg = cdu31a_port + SONY_FIFOST_REG_OFFSET;
3276 * Check to see if anything exists at the status register location.
3277 * I don't know if this is a good way to check, but it seems to work
3278 * ok for me.
3280 if (read_status_register() != 0xff)
3283 * Reset the drive and wait for attention from it (to say it's reset).
3284 * If you don't wait, the next operation will probably fail.
3286 reset_drive();
3287 retry_count = jiffies + SONY_RESET_TIMEOUT;
3288 while (time_before(jiffies, retry_count) && (!is_attention()))
3290 sony_sleep();
3293 #if 0
3294 /* If attention is never seen probably not a CDU31a present */
3295 if (!is_attention())
3297 res_reg[0] = 0x20;
3298 return;
3300 #endif
3303 * Get the drive configuration.
3305 do_sony_cd_cmd(SONY_REQ_DRIVE_CONFIG_CMD,
3306 NULL,
3308 (unsigned char *) res_reg,
3309 res_size);
3310 return;
3313 /* Return an error */
3314 res_reg[0] = 0x20;
3317 #ifndef MODULE
3319 * Set up base I/O and interrupts, called from main.c.
3321 void __init
3322 cdu31a_setup(char *strings,
3323 int *ints)
3325 if (ints[0] > 0)
3327 cdu31a_port = ints[1];
3329 if (ints[0] > 1)
3331 cdu31a_irq = ints[2];
3333 if ((strings != NULL) && (*strings != '\0'))
3335 if (strcmp(strings, "PAS") == 0)
3337 sony_pas_init = 1;
3339 else
3341 printk("CDU31A: Unknown interface type: %s\n", strings);
3345 #endif
3347 static int cdu31a_block_size;
3350 * Initialize the driver.
3352 int __init
3353 cdu31a_init(void)
3355 struct s_sony_drive_config drive_config;
3356 unsigned int res_size;
3357 char msg[255];
3358 char buf[40];
3359 int i;
3360 int drive_found;
3361 int tmp_irq;
3365 * According to Alex Freed (freed@europa.orion.adobe.com), this is
3366 * required for the Fusion CD-16 package. If the sound driver is
3367 * loaded, it should work fine, but just in case...
3369 * The following turn on the CD-ROM interface for a Fusion CD-16.
3371 if (sony_pas_init)
3373 outb(0xbc, 0x9a01);
3374 outb(0xe2, 0x9a01);
3377 drive_found = 0;
3379 /* Setting the base I/O address to 0xffff will disable it. */
3380 if (cdu31a_port == 0xffff)
3383 else if (cdu31a_port != 0)
3385 tmp_irq = cdu31a_irq; /* Need IRQ 0 because we can't sleep here. */
3386 cdu31a_irq = 0;
3388 get_drive_configuration(cdu31a_port,
3389 drive_config.exec_status,
3390 &res_size);
3391 if ((res_size > 2) && ((drive_config.exec_status[0] & 0xf0) == 0x00))
3393 drive_found = 1;
3396 cdu31a_irq = tmp_irq;
3398 else
3400 cdu31a_irq = 0;
3401 i = 0;
3402 while ( (cdu31a_addresses[i].base != 0)
3403 && (!drive_found))
3405 if (check_region(cdu31a_addresses[i].base, 4)) {
3406 i++;
3407 continue;
3409 get_drive_configuration(cdu31a_addresses[i].base,
3410 drive_config.exec_status,
3411 &res_size);
3412 if ((res_size > 2) && ((drive_config.exec_status[0] & 0xf0) == 0x00))
3414 drive_found = 1;
3415 cdu31a_irq = cdu31a_addresses[i].int_num;
3417 else
3419 i++;
3424 if (drive_found)
3426 int deficiency=0;
3428 request_region(cdu31a_port, 4,"cdu31a");
3430 if (register_blkdev(MAJOR_NR,"cdu31a",&cdrom_fops))
3432 printk("Unable to get major %d for CDU-31a\n", MAJOR_NR);
3433 goto errout2;
3436 if (SONY_HWC_DOUBLE_SPEED(drive_config))
3438 is_double_speed = 1;
3441 tmp_irq = cdu31a_irq; /* Need IRQ 0 because we can't sleep here. */
3442 cdu31a_irq = 0;
3444 set_drive_params(sony_speed);
3446 cdu31a_irq = tmp_irq;
3448 if (cdu31a_irq > 0)
3450 if (request_irq(cdu31a_irq, cdu31a_interrupt, SA_INTERRUPT, "cdu31a", NULL))
3452 printk("Unable to grab IRQ%d for the CDU31A driver\n", cdu31a_irq);
3453 cdu31a_irq = 0;
3457 sprintf(msg, "Sony I/F CDROM : %8.8s %16.16s %8.8s\n",
3458 drive_config.vendor_id,
3459 drive_config.product_id,
3460 drive_config.product_rev_level);
3461 sprintf(buf, " Capabilities: %s",
3462 load_mech[SONY_HWC_GET_LOAD_MECH(drive_config)]);
3463 strcat(msg, buf);
3464 if (SONY_HWC_AUDIO_PLAYBACK(drive_config))
3466 strcat(msg, ", audio");
3467 } else
3468 deficiency |= CDC_PLAY_AUDIO;
3469 if (SONY_HWC_EJECT(drive_config))
3471 strcat(msg, ", eject");
3472 } else
3473 deficiency |= CDC_OPEN_TRAY;
3474 if (SONY_HWC_LED_SUPPORT(drive_config))
3476 strcat(msg, ", LED");
3478 if (SONY_HWC_ELECTRIC_VOLUME(drive_config))
3480 strcat(msg, ", elec. Vol");
3482 if (SONY_HWC_ELECTRIC_VOLUME_CTL(drive_config))
3484 strcat(msg, ", sep. Vol");
3486 if (is_double_speed)
3488 strcat(msg, ", double speed");
3489 } else
3490 deficiency |= CDC_SELECT_SPEED;
3491 if (cdu31a_irq > 0)
3493 sprintf(buf, ", irq %d", cdu31a_irq);
3494 strcat(msg, buf);
3496 strcat(msg, "\n");
3498 is_a_cdu31a = strcmp("CD-ROM CDU31A", drive_config.product_id) == 0;
3500 blk_dev[MAJOR_NR].request_fn = DEVICE_REQUEST;
3501 read_ahead[MAJOR_NR] = CDU31A_READAHEAD;
3502 cdu31a_block_size = 1024; /* 1kB default block size */
3503 /* use 'mount -o block=2048' */
3504 blksize_size[MAJOR_NR] = &cdu31a_block_size;
3506 init_timer(&cdu31a_abort_timer);
3507 cdu31a_abort_timer.function = handle_abort_timeout;
3509 scd_info.dev = MKDEV(MAJOR_NR,0);
3510 scd_info.mask = deficiency;
3511 strncpy(scd_info.name, "cdu31a", sizeof(scd_info.name));
3513 if (register_cdrom(&scd_info))
3515 goto errout0;
3520 disk_changed = 1;
3522 if (drive_found)
3524 return(0);
3526 else
3528 goto errout3;
3530 errout0:
3531 printk("Unable to register CDU-31a with Uniform cdrom driver\n");
3532 if (unregister_blkdev(MAJOR_NR, "cdu31a"))
3534 printk("Can't unregister block device for cdu31a\n");
3536 errout2:
3537 release_region(cdu31a_port,4);
3538 errout3:
3539 return -EIO;
3542 #ifdef MODULE
3545 init_module(void)
3547 return cdu31a_init();
3550 void
3551 cleanup_module(void)
3553 if (unregister_cdrom(&scd_info))
3555 printk("Can't unregister cdu31a from Uniform cdrom driver\n");
3556 return;
3558 if ((unregister_blkdev(MAJOR_NR, "cdu31a") == -EINVAL))
3560 printk("Can't unregister cdu31a\n");
3561 return;
3564 if (cdu31a_irq > 0)
3565 free_irq(cdu31a_irq, NULL);
3567 release_region(cdu31a_port,4);
3568 printk(KERN_INFO "cdu31a module released.\n");
3570 #endif MODULE