Import 2.1.81
[davej-history.git] / drivers / cdrom / cdu31a.c
blob9a7b268716cddc0ece32748911bd339c2fd7d98d
1 /*
2 * Sony CDU-31A CDROM interface device driver.
4 * Corey Minyard (minyard@wf-rch.cirr.com)
6 * Colossians 3:17
8 * The Sony interface device driver handles Sony interface CDROM
9 * drives and provides a complete block-level interface as well as an
10 * ioctl() interface compatible with the Sun (as specified in
11 * include/linux/cdrom.h). With this interface, CDROMs can be
12 * accessed and standard audio CDs can be played back normally.
14 * WARNING - All autoprobes have been removed from the driver.
15 * You MUST configure the CDU31A via a LILO config
16 * at boot time or in lilo.conf. I have the
17 * following in my lilo.conf:
19 * append="cdu31a=0x1f88,0,PAS"
21 * The first number is the I/O base address of the
22 * card. The second is the interrupt (0 means none).
23 * The third should be "PAS" if on a Pro-Audio
24 * spectrum, or nothing if on something else.
26 * This interface is (unfortunately) a polled interface. This is
27 * because most Sony interfaces are set up with DMA and interrupts
28 * disables. Some (like mine) do not even have the capability to
29 * handle interrupts or DMA. For this reason you will see a lot of
30 * the following:
32 * retry_count = jiffies+ SONY_JIFFIES_TIMEOUT;
33 * while ((retry_count > jiffies) && (! <some condition to wait for))
34 * {
35 * while (handle_sony_cd_attention())
36 * ;
38 * sony_sleep();
39 * }
40 * if (the condition not met)
41 * {
42 * return an error;
43 * }
45 * This ugly hack waits for something to happen, sleeping a little
46 * between every try. it also handles attentions, which are
47 * asynchronous events from the drive informing the driver that a disk
48 * has been inserted, removed, etc.
50 * NEWS FLASH - The driver now supports interrupts but they are
51 * turned off by default. Use of interrupts is highly encouraged, it
52 * cuts CPU usage down to a reasonable level. I had DMA in for a while
53 * but PC DMA is just too slow. Better to just insb() it.
55 * One thing about these drives: They talk in MSF (Minute Second Frame) format.
56 * There are 75 frames a second, 60 seconds a minute, and up to 75 minutes on a
57 * disk. The funny thing is that these are sent to the drive in BCD, but the
58 * interface wants to see them in decimal. A lot of conversion goes on.
60 * DRIVER SPECIAL FEATURES
61 * -----------------------
63 * This section describes features beyond the normal audio and CD-ROM
64 * functions of the drive.
66 * 2048 byte buffer mode
68 * If a disk is mounted with -o block=2048, data is copied straight
69 * from the drive data port to the buffer. Otherwise, the readahead
70 * buffer must be involved to hold the other 1K of data when a 1K
71 * block operation is done. Note that with 2048 byte blocks you
72 * cannot execute files from the CD.
74 * XA compatibility
76 * The driver should support XA disks for both the CDU31A and CDU33A.
77 * It does this transparently, the using program doesn't need to set it.
79 * Multi-Session
81 * A multi-session disk looks just like a normal disk to the user.
82 * Just mount one normally, and all the data should be there.
83 * A special thanks to Koen for help with this!
85 * Raw sector I/O
87 * Using the CDROMREADAUDIO it is possible to read raw audio and data
88 * tracks. Both operations return 2352 bytes per sector. On the data
89 * tracks, the first 12 bytes is not returned by the drive and the value
90 * of that data is indeterminate.
93 * Copyright (C) 1993 Corey Minyard
95 * This program is free software; you can redistribute it and/or modify
96 * it under the terms of the GNU General Public License as published by
97 * the Free Software Foundation; either version 2 of the License, or
98 * (at your option) any later version.
100 * This program is distributed in the hope that it will be useful,
101 * but WITHOUT ANY WARRANTY; without even the implied warranty of
102 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
103 * GNU General Public License for more details.
105 * You should have received a copy of the GNU General Public License
106 * along with this program; if not, write to the Free Software
107 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
110 * Credits:
111 * Heiko Eissfeldt <heiko@colossus.escape.de>
112 * For finding abug in the return of the track numbers.
114 /* conversion to Uniform cdrom layer.
115 TOC processing redone for proper multisession support.
117 TODO:
118 CDs with form1 and form2 sectors cause problems
119 with current read-ahead strategy.
120 Heiko Eissfeldt Sep 97 */
124 * Setting up the Sony CDU31A/CDU33A drive interface card. If
125 * You have another card, you are on your own.
127 * +----------+-----------------+----------------------+
128 * | JP1 | 34 Pin Conn | |
129 * | JP2 +-----------------+ |
130 * | JP3 |
131 * | JP4 |
132 * | +--+
133 * | | +-+
134 * | | | | External
135 * | | | | Connector
136 * | | | |
137 * | | +-+
138 * | +--+
139 * | |
140 * | +--------+
141 * | |
142 * +------------------------------------------+
144 * JP1 sets the Base Address, using the following settings:
146 * Address Pin 1 Pin 2
147 * ------- ----- -----
148 * 0x320 Short Short
149 * 0x330 Short Open
150 * 0x340 Open Short
151 * 0x360 Open Open
153 * JP2 and JP3 configure the DMA channel; they must be set the same.
155 * DMA Pin 1 Pin 2 Pin 3
156 * --- ----- ----- -----
157 * 1 On Off On
158 * 2 Off On Off
159 * 3 Off Off On
161 * JP4 Configures the IRQ:
163 * IRQ Pin 1 Pin 2 Pin 3 Pin 4
164 * --- ----- ----- ----- -----
165 * 3 Off Off On Off
166 * 4 Off Off* Off On
167 * 5 On Off Off Off
168 * 6 Off On Off Off
170 * * The documentation states to set this for interrupt
171 * 4, but I think that is a mistake.
173 * It probably a little late to be adding a history, but I guess I
174 * will start.
176 * 10/24/95 - Added support for disabling the eject button when the
177 * drive is open. Note that there is a small problem
178 * still here, if the eject button is pushed while the
179 * drive light is flashing, the drive will return a bad
180 * status and be reset. It recovers, though.
182 * 03/07/97 - Fixed a problem with timers.
185 * 18 Spetember 1997 -- Ported to Uniform CD-ROM driver by
186 * Heiko Eissfeldt <heiko@colossus.escape.de> with additional
187 * changes by Erik Andersen <andersee@debian.org>
191 #include <linux/major.h>
193 #include <linux/module.h>
195 #include <linux/errno.h>
196 #include <linux/signal.h>
197 #include <linux/sched.h>
198 #include <linux/timer.h>
199 #include <linux/fs.h>
200 #include <linux/kernel.h>
201 #include <linux/hdreg.h>
202 #include <linux/genhd.h>
203 #include <linux/ioport.h>
204 #include <linux/string.h>
205 #include <linux/malloc.h>
206 #include <linux/init.h>
208 #include <asm/system.h>
209 #include <asm/io.h>
210 #include <asm/uaccess.h>
211 #include <asm/dma.h>
213 #include <linux/cdrom.h>
214 #include "cdu31a.h"
216 #define MAJOR_NR CDU31A_CDROM_MAJOR
217 #include <linux/blk.h>
219 #define CDU31A_READAHEAD 4 /* 128 sector, 64kB, 32 reads read-ahead */
220 #define CDU31A_MAX_CONSECUTIVE_ATTENTIONS 10
222 #define DEBUG 0
224 /* Define the following if you have data corruption problems. */
225 #undef SONY_POLL_EACH_BYTE
228 ** Edit the following data to change interrupts, DMA channels, etc.
229 ** Default is polled and no DMA. DMA is not recommended for double-speed
230 ** drives.
232 static struct
234 unsigned short base; /* I/O Base Address */
235 short int_num; /* Interrupt Number (-1 means scan for it,
236 0 means don't use) */
237 } cdu31a_addresses[] __initdata =
239 #if 0 /* No autoconfig any more. See Note at beginning
240 of this file. */
241 { 0x340, 0 }, /* Standard configuration Sony Interface */
242 { 0x1f88, 0 }, /* Fusion CD-16 */
243 { 0x230, 0 }, /* SoundBlaster 16 card */
244 { 0x360, 0 }, /* Secondary standard Sony Interface */
245 { 0x320, 0 }, /* Secondary standard Sony Interface */
246 { 0x330, 0 }, /* Secondary standard Sony Interface */
247 { 0x634, 0 }, /* Sound FX SC400 */
248 { 0x654, 0 }, /* Sound FX SC400 */
249 #endif
250 { 0 }
253 static int handle_sony_cd_attention(void);
254 static int read_subcode(void);
255 static void sony_get_toc(void);
256 /*static int scd_open(struct inode *inode, struct file *filp);*/
257 static int scd_open(struct cdrom_device_info *, int);
258 static void do_sony_cd_cmd(unsigned char cmd,
259 unsigned char *params,
260 unsigned int num_params,
261 unsigned char *result_buffer,
262 unsigned int *result_size);
263 static void size_to_buf(unsigned int size,
264 unsigned char *buf);
266 /* Parameters for the read-ahead. */
267 static unsigned int sony_next_block; /* Next 512 byte block offset */
268 static unsigned int sony_blocks_left = 0; /* Number of 512 byte blocks left
269 in the current read command. */
272 /* The base I/O address of the Sony Interface. This is a variable (not a
273 #define) so it can be easily changed via some future ioctl() */
274 static unsigned int cdu31a_port = 0;
275 MODULE_PARM(cdu31a_port, "i");
278 * The following are I/O addresses of the various registers for the drive. The
279 * comment for the base address also applies here.
281 static volatile unsigned short sony_cd_cmd_reg;
282 static volatile unsigned short sony_cd_param_reg;
283 static volatile unsigned short sony_cd_write_reg;
284 static volatile unsigned short sony_cd_control_reg;
285 static volatile unsigned short sony_cd_status_reg;
286 static volatile unsigned short sony_cd_result_reg;
287 static volatile unsigned short sony_cd_read_reg;
288 static volatile unsigned short sony_cd_fifost_reg;
291 static int sony_spun_up = 0; /* Has the drive been spun up? */
293 static int sony_speed = 0; /* Last wanted speed */
295 static int sony_xa_mode = 0; /* Is an XA disk in the drive
296 and the drive a CDU31A? */
298 static int sony_raw_data_mode = 1; /* 1 if data tracks, 0 if audio.
299 For raw data reads. */
301 static unsigned int sony_usage = 0; /* How many processes have the
302 drive open. */
304 static int sony_pas_init = 0; /* Initialize the Pro-Audio
305 Spectrum card? */
307 static struct s_sony_session_toc single_toc; /* Holds the
308 table of
309 contents. */
311 static struct s_all_sessions_toc sony_toc; /* entries gathered from all
312 sessions */
314 static int sony_toc_read = 0; /* Has the TOC been read for
315 the drive? */
317 static struct s_sony_subcode last_sony_subcode; /* Points to the last
318 subcode address read */
320 static volatile int sony_inuse = 0; /* Is the drive in use? Only one operation
321 at a time allowed */
323 static struct wait_queue * sony_wait = NULL; /* Things waiting for the drive */
325 static struct task_struct *has_cd_task = NULL; /* The task that is currently
326 using the CDROM drive, or
327 NULL if none. */
329 static int is_double_speed = 0; /* does the drive support double speed ? */
330 static int is_a_cdu31a = 1; /* Is the drive a CDU31A? */
332 static int is_auto_eject = 1; /* Door has been locked? 1=No/0=Yes */
335 * The audio status uses the values from read subchannel data as specified
336 * in include/linux/cdrom.h.
338 static volatile int sony_audio_status = CDROM_AUDIO_NO_STATUS;
341 * The following are a hack for pausing and resuming audio play. The drive
342 * does not work as I would expect it, if you stop it then start it again,
343 * the drive seeks back to the beginning and starts over. This holds the
344 * position during a pause so a resume can restart it. It uses the
345 * audio status variable above to tell if it is paused.
347 static unsigned volatile char cur_pos_msf[3] = { 0, 0, 0 };
348 static unsigned volatile char final_pos_msf[3] = { 0, 0, 0 };
350 /* What IRQ is the drive using? 0 if none. */
351 static int cdu31a_irq = 0;
352 MODULE_PARM(cdu31a_irq, "i");
354 /* The interrupt handler will wake this queue up when it gets an
355 interrupts. */
356 static struct wait_queue *cdu31a_irq_wait = NULL;
358 static int curr_control_reg = 0; /* Current value of the control register */
360 /* A disk changed variable. When a disk change is detected, it will
361 all be set to TRUE. As the upper layers ask for disk_changed status
362 it will be cleared. */
363 static char disk_changed;
365 /* Variable for using the readahead buffer. The readahead buffer
366 is used for raw sector reads and for blocksizes that are smaller
367 than 2048 bytes. */
368 static char readahead_buffer[CD_FRAMESIZE_RAW];
369 static int readahead_dataleft = 0;
370 static int readahead_bad = 0;
372 /* Used to time a short period to abort an operation after the
373 drive has been idle for a while. This keeps the light on
374 the drive from flashing for very long. */
375 static struct timer_list cdu31a_abort_timer;
377 /* Marks if the timeout has started an abort read. This is used
378 on entry to the drive to tell the code to read out the status
379 from the abort read. */
380 static int abort_read_started = 0;
384 * This routine returns 1 if the disk has been changed since the last
385 * check or 0 if it hasn't.
387 static int
388 scd_disk_change(kdev_t full_dev)
390 int retval;
392 retval = disk_changed;
393 disk_changed = 0;
395 return retval;
399 * Uniform cdrom interface function
400 * report back, if disc has changed from time of last request.
402 static int
403 scd_media_changed(struct cdrom_device_info *cdi, int disc_nr)
405 return scd_disk_change(cdi->dev);
409 * Uniform cdrom interface function
410 * report back, if drive is ready
412 static int scd_drive_status(struct cdrom_device_info *cdi, int slot_nr)
414 if (CDSL_CURRENT != slot_nr) {
415 /* we have no changer support */
416 return -EINVAL;
419 /*return sony_spun_up ? CDS_DISC_OK : CDS_DRIVE_NOT_READY;*/
420 return sony_spun_up ? CDS_DISC_OK : CDS_TRAY_OPEN;
423 static inline void
424 enable_interrupts(void)
426 curr_control_reg |= ( SONY_ATTN_INT_EN_BIT
427 | SONY_RES_RDY_INT_EN_BIT
428 | SONY_DATA_RDY_INT_EN_BIT);
429 outb(curr_control_reg, sony_cd_control_reg);
432 static inline void
433 disable_interrupts(void)
435 curr_control_reg &= ~( SONY_ATTN_INT_EN_BIT
436 | SONY_RES_RDY_INT_EN_BIT
437 | SONY_DATA_RDY_INT_EN_BIT);
438 outb(curr_control_reg, sony_cd_control_reg);
442 * Wait a little while (used for polling the drive). If in initialization,
443 * setting a timeout doesn't work, so just loop for a while.
445 static inline void
446 sony_sleep(void)
448 unsigned long flags;
450 if (cdu31a_irq <= 0)
452 current->state = TASK_INTERRUPTIBLE;
453 current->timeout = jiffies;
454 schedule();
456 else /* Interrupt driven */
458 save_flags(flags);
459 cli();
460 enable_interrupts();
461 interruptible_sleep_on(&cdu31a_irq_wait);
462 restore_flags(flags);
468 * The following are convenience routine to read various status and set
469 * various conditions in the drive.
471 static inline int
472 is_attention(void)
474 return((inb(sony_cd_status_reg) & SONY_ATTN_BIT) != 0);
477 static inline int
478 is_busy(void)
480 return((inb(sony_cd_status_reg) & SONY_BUSY_BIT) != 0);
483 static inline int
484 is_data_ready(void)
486 return((inb(sony_cd_status_reg) & SONY_DATA_RDY_BIT) != 0);
489 static inline int
490 is_data_requested(void)
492 return((inb(sony_cd_status_reg) & SONY_DATA_REQUEST_BIT) != 0);
495 static inline int
496 is_result_ready(void)
498 return((inb(sony_cd_status_reg) & SONY_RES_RDY_BIT) != 0);
501 static inline int
502 is_param_write_rdy(void)
504 return((inb(sony_cd_fifost_reg) & SONY_PARAM_WRITE_RDY_BIT) != 0);
507 static inline int
508 is_result_reg_not_empty(void)
510 return((inb(sony_cd_fifost_reg) & SONY_RES_REG_NOT_EMP_BIT) != 0);
513 static inline void
514 reset_drive(void)
516 curr_control_reg = 0;
517 readahead_dataleft = 0;
518 sony_toc_read = 0;
519 outb(SONY_DRIVE_RESET_BIT, sony_cd_control_reg);
523 * Uniform cdrom interface function
524 * reset drive and return when it is ready
526 static int scd_reset(struct cdrom_device_info * cdi)
528 int retry_count;
530 reset_drive();
532 retry_count = jiffies + SONY_RESET_TIMEOUT;
533 while ((retry_count > jiffies) && (!is_attention()))
535 sony_sleep();
538 return 0;
541 static inline void
542 clear_attention(void)
544 outb(curr_control_reg | SONY_ATTN_CLR_BIT, sony_cd_control_reg);
547 static inline void
548 clear_result_ready(void)
550 outb(curr_control_reg | SONY_RES_RDY_CLR_BIT, sony_cd_control_reg);
553 static inline void
554 clear_data_ready(void)
556 outb(curr_control_reg | SONY_DATA_RDY_CLR_BIT, sony_cd_control_reg);
559 static inline void
560 clear_param_reg(void)
562 outb(curr_control_reg | SONY_PARAM_CLR_BIT, sony_cd_control_reg);
565 static inline unsigned char
566 read_status_register(void)
568 return(inb(sony_cd_status_reg));
571 static inline unsigned char
572 read_result_register(void)
574 return(inb(sony_cd_result_reg));
577 static inline unsigned char
578 read_data_register(void)
580 return(inb(sony_cd_read_reg));
583 static inline void
584 write_param(unsigned char param)
586 outb(param, sony_cd_param_reg);
589 static inline void
590 write_cmd(unsigned char cmd)
592 outb(curr_control_reg | SONY_RES_RDY_INT_EN_BIT, sony_cd_control_reg);
593 outb(cmd, sony_cd_cmd_reg);
596 static void
597 cdu31a_interrupt(int irq, void *dev_id, struct pt_regs *regs)
599 unsigned char val;
601 if (abort_read_started)
603 /* We might be waiting for an abort to finish. Don't
604 disable interrupts yet, though, because we handle
605 this one here. */
606 /* Clear out the result registers. */
607 while (is_result_reg_not_empty())
609 val = read_result_register();
611 clear_data_ready();
612 clear_result_ready();
614 /* Clear out the data */
615 while (is_data_requested())
617 val = read_data_register();
619 abort_read_started = 0;
621 /* If something was waiting, wake it up now. */
622 if (cdu31a_irq_wait != NULL)
624 disable_interrupts();
625 wake_up(&cdu31a_irq_wait);
628 else if (cdu31a_irq_wait != NULL)
630 disable_interrupts();
631 wake_up(&cdu31a_irq_wait);
633 else
635 disable_interrupts();
636 printk("CDU31A: Got an interrupt but nothing was waiting\n");
641 * give more verbose error messages
643 static unsigned char *translate_error( unsigned char err_code )
645 static unsigned char errbuf[80];
647 switch (err_code) {
648 case 0x10: return "illegal command ";
649 case 0x11: return "illegal parameter ";
651 case 0x20: return "not loaded ";
652 case 0x21: return "no disc ";
653 case 0x22: return "not spinning ";
654 case 0x23: return "spinning ";
655 case 0x25: return "spindle servo ";
656 case 0x26: return "focus servo ";
657 case 0x29: return "eject mechanism ";
658 case 0x2a: return "audio playing ";
659 case 0x2c: return "emergency eject ";
661 case 0x30: return "focus ";
662 case 0x31: return "frame sync ";
663 case 0x32: return "subcode address ";
664 case 0x33: return "block sync ";
665 case 0x34: return "header address ";
667 case 0x40: return "illegal track read ";
668 case 0x41: return "mode 0 read ";
669 case 0x42: return "illegal mode read ";
670 case 0x43: return "illegal block size read ";
671 case 0x44: return "mode read ";
672 case 0x45: return "form read ";
673 case 0x46: return "leadout read ";
674 case 0x47: return "buffer overrun ";
676 case 0x53: return "unrecoverable CIRC ";
677 case 0x57: return "unrecoverable LECC ";
679 case 0x60: return "no TOC ";
680 case 0x61: return "invalid subcode data ";
681 case 0x63: return "focus on TOC read ";
682 case 0x64: return "frame sync on TOC read ";
683 case 0x65: return "TOC data ";
685 case 0x70: return "hardware failure ";
686 case 0x91: return "leadin ";
687 case 0x92: return "leadout ";
688 case 0x93: return "data track ";
690 sprintf(errbuf, "unknown 0x%02x ", err_code);
691 return errbuf;
695 * Set the drive parameters so the drive will auto-spin-up when a
696 * disk is inserted.
698 static void
699 set_drive_params(int want_doublespeed)
701 unsigned char res_reg[12];
702 unsigned int res_size;
703 unsigned char params[3];
706 params[0] = SONY_SD_AUTO_SPIN_DOWN_TIME;
707 params[1] = 0x00; /* Never spin down the drive. */
708 do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
709 params,
711 res_reg,
712 &res_size);
713 if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20))
715 printk(" Unable to set spin-down time: 0x%2.2x\n", res_reg[1]);
718 params[0] = SONY_SD_MECH_CONTROL;
719 params[1] = SONY_AUTO_SPIN_UP_BIT; /* Set auto spin up */
721 if (is_auto_eject) params[1] |= SONY_AUTO_EJECT_BIT;
723 if (is_double_speed && want_doublespeed)
725 params[1] |= SONY_DOUBLE_SPEED_BIT; /* Set the drive to double speed if
726 possible */
728 do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
729 params,
731 res_reg,
732 &res_size);
733 if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20))
735 printk(" Unable to set mechanical parameters: 0x%2.2x\n", res_reg[1]);
740 * Uniform cdrom interface function
741 * select reading speed for data access
743 static int scd_select_speed(struct cdrom_device_info *cdi, int speed)
745 if (speed == 0)
746 sony_speed = 1;
747 else
748 sony_speed = speed - 1;
750 set_drive_params(sony_speed);
751 return 0;
755 * Uniform cdrom interface function
756 * lock or unlock eject button
758 static int scd_lock_door(struct cdrom_device_info *cdi, int lock)
760 if (lock == 0 && sony_usage == 1)
762 /* Unlock the door, only if nobody is using the drive */
763 is_auto_eject = 1;
764 } else {
765 is_auto_eject = 0;
767 set_drive_params(sony_speed);
768 return 0;
772 * This code will reset the drive and attempt to restore sane parameters.
774 static void
775 restart_on_error(void)
777 unsigned char res_reg[12];
778 unsigned int res_size;
779 unsigned int retry_count;
782 printk("cdu31a: Resetting drive on error\n");
783 reset_drive();
784 retry_count = jiffies + SONY_RESET_TIMEOUT;
785 while ((retry_count > jiffies) && (!is_attention()))
787 sony_sleep();
789 set_drive_params(sony_speed);
790 do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg, &res_size);
791 if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20))
793 printk("cdu31a: Unable to spin up drive: 0x%2.2x\n", res_reg[1]);
796 current->state = TASK_INTERRUPTIBLE;
797 current->timeout = jiffies + 2*HZ;
798 schedule();
800 sony_get_toc();
804 * This routine writes data to the parameter register. Since this should
805 * happen fairly fast, it is polled with no OS waits between.
807 static int
808 write_params(unsigned char *params,
809 int num_params)
811 unsigned int retry_count;
814 retry_count = SONY_READY_RETRIES;
815 while ((retry_count > 0) && (!is_param_write_rdy()))
817 retry_count--;
819 if (!is_param_write_rdy())
821 return -EIO;
824 while (num_params > 0)
826 write_param(*params);
827 params++;
828 num_params--;
831 return 0;
836 * The following reads data from the command result register. It is a
837 * fairly complex routine, all status info flows back through this
838 * interface. The algorithm is stolen directly from the flowcharts in
839 * the drive manual.
841 static void
842 get_result(unsigned char *result_buffer,
843 unsigned int *result_size)
845 unsigned char a, b;
846 int i;
847 unsigned int retry_count;
850 while (handle_sony_cd_attention())
852 /* Wait for the result data to be ready */
853 retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
854 while ((retry_count > jiffies) && (is_busy() || (!(is_result_ready()))))
856 sony_sleep();
858 while (handle_sony_cd_attention())
861 if (is_busy() || (!(is_result_ready())))
863 #if DEBUG
864 printk("CDU31A timeout out %d\n", __LINE__);
865 #endif
866 result_buffer[0] = 0x20;
867 result_buffer[1] = SONY_TIMEOUT_OP_ERR;
868 *result_size = 2;
869 return;
873 * Get the first two bytes. This determines what else needs
874 * to be done.
876 clear_result_ready();
877 a = read_result_register();
878 *result_buffer = a;
879 result_buffer++;
881 /* Check for block error status result. */
882 if ((a & 0xf0) == 0x50)
884 *result_size = 1;
885 return;
888 b = read_result_register();
889 *result_buffer = b;
890 result_buffer++;
891 *result_size = 2;
894 * 0x20 means an error occurred. Byte 2 will have the error code.
895 * Otherwise, the command succeeded, byte 2 will have the count of
896 * how many more status bytes are coming.
898 * The result register can be read 10 bytes at a time, a wait for
899 * result ready to be asserted must be done between every 10 bytes.
901 if ((a & 0xf0) != 0x20)
903 if (b > 8)
905 for (i=0; i<8; i++)
907 *result_buffer = read_result_register();
908 result_buffer++;
909 (*result_size)++;
911 b = b - 8;
913 while (b > 10)
915 retry_count = SONY_READY_RETRIES;
916 while ((retry_count > 0) && (!is_result_ready()))
918 retry_count--;
920 if (!is_result_ready())
922 #if DEBUG
923 printk("CDU31A timeout out %d\n", __LINE__);
924 #endif
925 result_buffer[0] = 0x20;
926 result_buffer[1] = SONY_TIMEOUT_OP_ERR;
927 *result_size = 2;
928 return;
931 clear_result_ready();
933 for (i=0; i<10; i++)
935 *result_buffer = read_result_register();
936 result_buffer++;
937 (*result_size)++;
939 b = b - 10;
942 if (b > 0)
944 retry_count = SONY_READY_RETRIES;
945 while ((retry_count > 0) && (!is_result_ready()))
947 retry_count--;
949 if (!is_result_ready())
951 #if DEBUG
952 printk("CDU31A timeout out %d\n", __LINE__);
953 #endif
954 result_buffer[0] = 0x20;
955 result_buffer[1] = SONY_TIMEOUT_OP_ERR;
956 *result_size = 2;
957 return;
962 while (b > 0)
964 *result_buffer = read_result_register();
965 result_buffer++;
966 (*result_size)++;
967 b--;
973 * Do a command that does not involve data transfer. This routine must
974 * be re-entrant from the same task to support being called from the
975 * data operation code when an error occurs.
977 static void
978 do_sony_cd_cmd(unsigned char cmd,
979 unsigned char *params,
980 unsigned int num_params,
981 unsigned char *result_buffer,
982 unsigned int *result_size)
984 unsigned int retry_count;
985 int num_retries;
986 int recursive_call;
987 unsigned long flags;
990 save_flags(flags);
991 cli();
992 if (current != has_cd_task) /* Allow recursive calls to this routine */
994 while (sony_inuse)
996 interruptible_sleep_on(&sony_wait);
997 if (signal_pending(current))
999 result_buffer[0] = 0x20;
1000 result_buffer[1] = SONY_SIGNAL_OP_ERR;
1001 *result_size = 2;
1002 restore_flags(flags);
1003 return;
1006 sony_inuse = 1;
1007 has_cd_task = current;
1008 recursive_call = 0;
1010 else
1012 recursive_call = 1;
1015 num_retries = 0;
1016 retry_cd_operation:
1018 while (handle_sony_cd_attention())
1021 sti();
1023 retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
1024 while ((retry_count > jiffies) && (is_busy()))
1026 sony_sleep();
1028 while (handle_sony_cd_attention())
1031 if (is_busy())
1033 #if DEBUG
1034 printk("CDU31A timeout out %d\n", __LINE__);
1035 #endif
1036 result_buffer[0] = 0x20;
1037 result_buffer[1] = SONY_TIMEOUT_OP_ERR;
1038 *result_size = 2;
1040 else
1042 clear_result_ready();
1043 clear_param_reg();
1045 write_params(params, num_params);
1046 write_cmd(cmd);
1048 get_result(result_buffer, result_size);
1051 if ( ((result_buffer[0] & 0xf0) == 0x20)
1052 && (num_retries < MAX_CDU31A_RETRIES))
1054 num_retries++;
1055 current->state = TASK_INTERRUPTIBLE;
1056 current->timeout = jiffies + HZ/10; /* Wait .1 seconds on retries */
1057 schedule();
1058 goto retry_cd_operation;
1061 if (!recursive_call)
1063 has_cd_task = NULL;
1064 sony_inuse = 0;
1065 wake_up_interruptible(&sony_wait);
1068 restore_flags(flags);
1073 * Handle an attention from the drive. This will return 1 if it found one
1074 * or 0 if not (if one is found, the caller might want to call again).
1076 * This routine counts the number of consecutive times it is called
1077 * (since this is always called from a while loop until it returns
1078 * a 0), and returns a 0 if it happens too many times. This will help
1079 * prevent a lockup.
1081 static int
1082 handle_sony_cd_attention(void)
1084 unsigned char atten_code;
1085 static int num_consecutive_attentions = 0;
1086 volatile int val;
1089 #if 0*DEBUG
1090 printk("Entering handle_sony_cd_attention\n");
1091 #endif
1092 if (is_attention())
1094 if (num_consecutive_attentions > CDU31A_MAX_CONSECUTIVE_ATTENTIONS)
1096 printk("cdu31a: Too many consecutive attentions: %d\n",
1097 num_consecutive_attentions);
1098 num_consecutive_attentions = 0;
1099 #if DEBUG
1100 printk("Leaving handle_sony_cd_attention at %d\n", __LINE__);
1101 #endif
1102 return(0);
1105 clear_attention();
1106 atten_code = read_result_register();
1108 switch (atten_code)
1110 /* Someone changed the CD. Mark it as changed */
1111 case SONY_MECH_LOADED_ATTN:
1112 disk_changed = 1;
1113 sony_toc_read = 0;
1114 sony_audio_status = CDROM_AUDIO_NO_STATUS;
1115 sony_blocks_left = 0;
1116 break;
1118 case SONY_SPIN_DOWN_COMPLETE_ATTN:
1119 /* Mark the disk as spun down. */
1120 sony_spun_up = 0;
1121 break;
1123 case SONY_AUDIO_PLAY_DONE_ATTN:
1124 sony_audio_status = CDROM_AUDIO_COMPLETED;
1125 read_subcode();
1126 break;
1128 case SONY_EJECT_PUSHED_ATTN:
1129 if (is_auto_eject)
1131 sony_audio_status = CDROM_AUDIO_INVALID;
1133 break;
1135 case SONY_LEAD_IN_ERR_ATTN:
1136 case SONY_LEAD_OUT_ERR_ATTN:
1137 case SONY_DATA_TRACK_ERR_ATTN:
1138 case SONY_AUDIO_PLAYBACK_ERR_ATTN:
1139 sony_audio_status = CDROM_AUDIO_ERROR;
1140 break;
1143 num_consecutive_attentions++;
1144 #if DEBUG
1145 printk("Leaving handle_sony_cd_attention at %d\n", __LINE__);
1146 #endif
1147 return(1);
1149 else if (abort_read_started)
1151 while (is_result_reg_not_empty())
1153 val = read_result_register();
1155 clear_data_ready();
1156 clear_result_ready();
1157 /* Clear out the data */
1158 while (is_data_requested())
1160 val = read_data_register();
1162 abort_read_started = 0;
1163 #if DEBUG
1164 printk("Leaving handle_sony_cd_attention at %d\n", __LINE__);
1165 #endif
1166 return(1);
1169 num_consecutive_attentions = 0;
1170 #if 0*DEBUG
1171 printk("Leaving handle_sony_cd_attention at %d\n", __LINE__);
1172 #endif
1173 return(0);
1177 /* Convert from an integer 0-99 to BCD */
1178 static inline unsigned int
1179 int_to_bcd(unsigned int val)
1181 int retval;
1184 retval = (val / 10) << 4;
1185 retval = retval | val % 10;
1186 return(retval);
1190 /* Convert from BCD to an integer from 0-99 */
1191 static unsigned int
1192 bcd_to_int(unsigned int bcd)
1194 return((((bcd >> 4) & 0x0f) * 10) + (bcd & 0x0f));
1199 * Convert a logical sector value (like the OS would want to use for
1200 * a block device) to an MSF format.
1202 static void
1203 log_to_msf(unsigned int log, unsigned char *msf)
1205 log = log + LOG_START_OFFSET;
1206 msf[0] = int_to_bcd(log / 4500);
1207 log = log % 4500;
1208 msf[1] = int_to_bcd(log / 75);
1209 msf[2] = int_to_bcd(log % 75);
1214 * Convert an MSF format to a logical sector.
1216 static unsigned int
1217 msf_to_log(unsigned char *msf)
1219 unsigned int log;
1222 log = msf[2];
1223 log += msf[1] * 75;
1224 log += msf[0] * 4500;
1225 log = log - LOG_START_OFFSET;
1227 return log;
1232 * Take in integer size value and put it into a buffer like
1233 * the drive would want to see a number-of-sector value.
1235 static void
1236 size_to_buf(unsigned int size,
1237 unsigned char *buf)
1239 buf[0] = size / 65536;
1240 size = size % 65536;
1241 buf[1] = size / 256;
1242 buf[2] = size % 256;
1245 #if 0
1246 /* Uniform cdrom interface function.
1247 Return the status of the current disc:
1248 If it is recognized as CD-I -> return XA Mode 2 Form 2
1249 If it is recognized as XA -> return XA Mode 2 Form 1
1250 If there is at least one data track return Mode 1
1251 else return type AUDIO
1253 static int scd_disc_status(struct cdrom_device_info *cdi)
1255 if (sony_spun_up)
1257 int i;
1259 sony_get_toc();
1260 /* look into the TOC */
1261 if (sony_toc.disk_type == 0x10) /* this is a CD-I disc */
1262 return CDS_XA_2_2;
1263 if (sony_toc.disk_type == 0x20) /* this is a XA disc */
1264 return CDS_XA_2_1;
1265 for (i = 0; i < sony_toc.track_entries; i++)
1266 if (sony_toc.tracks[i].control & CDROM_DATA_TRACK)
1267 return CDS_DATA_1;
1268 return CDS_AUDIO;
1269 } else
1270 return CDS_NO_INFO;
1272 #endif
1274 /* Starts a read operation. Returns 0 on success and 1 on failure.
1275 The read operation used here allows multiple sequential sectors
1276 to be read and status returned for each sector. The driver will
1277 read the output one at a time as the requests come and abort the
1278 operation if the requested sector is not the next one from the
1279 drive. */
1280 static int
1281 start_request(unsigned int sector,
1282 unsigned int nsect,
1283 int read_nsect_only)
1285 unsigned char params[6];
1286 unsigned int read_size;
1287 unsigned int retry_count;
1290 #if DEBUG
1291 printk("Entering start_request\n");
1292 #endif
1293 log_to_msf(sector, params);
1294 /* If requested, read exactly what was asked. */
1295 if (read_nsect_only)
1297 read_size = nsect;
1300 * If the full read-ahead would go beyond the end of the media, trim
1301 * it back to read just till the end of the media.
1303 else if ((sector + nsect) >= sony_toc.lead_out_start_lba)
1305 read_size = sony_toc.lead_out_start_lba - sector;
1307 /* Read the full readahead amount. */
1308 else
1310 read_size = CDU31A_READAHEAD / 4;
1312 size_to_buf(read_size, &params[3]);
1315 * Clear any outstanding attentions and wait for the drive to
1316 * complete any pending operations.
1318 while (handle_sony_cd_attention())
1321 retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
1322 while ((retry_count > jiffies) && (is_busy()))
1324 sony_sleep();
1326 while (handle_sony_cd_attention())
1330 if (is_busy())
1332 printk("CDU31A: Timeout while waiting to issue command\n");
1333 #if DEBUG
1334 printk("Leaving start_request at %d\n", __LINE__);
1335 #endif
1336 return(1);
1338 else
1340 /* Issue the command */
1341 clear_result_ready();
1342 clear_param_reg();
1344 write_params(params, 6);
1345 write_cmd(SONY_READ_BLKERR_STAT_CMD);
1347 sony_blocks_left = read_size * 4;
1348 sony_next_block = sector * 4;
1349 readahead_dataleft = 0;
1350 readahead_bad = 0;
1351 #if DEBUG
1352 printk("Leaving start_request at %d\n", __LINE__);
1353 #endif
1354 return(0);
1356 #if DEBUG
1357 printk("Leaving start_request at %d\n", __LINE__);
1358 #endif
1361 /* Abort a pending read operation. Clear all the drive status and
1362 readahead variables. */
1363 static void
1364 abort_read(void)
1366 unsigned char result_reg[2];
1367 int result_size;
1368 volatile int val;
1371 do_sony_cd_cmd(SONY_ABORT_CMD, NULL, 0, result_reg, &result_size);
1372 if ((result_reg[0] & 0xf0) == 0x20)
1374 printk("CDU31A: Error aborting read, %s error\n",
1375 translate_error(
1376 result_reg[1]));
1379 while (is_result_reg_not_empty())
1381 val = read_result_register();
1383 clear_data_ready();
1384 clear_result_ready();
1385 /* Clear out the data */
1386 while (is_data_requested())
1388 val = read_data_register();
1391 sony_blocks_left = 0;
1392 readahead_dataleft = 0;
1393 readahead_bad = 0;
1396 /* Called when the timer times out. This will abort the
1397 pending read operation. */
1398 static void
1399 handle_abort_timeout(unsigned long data)
1401 unsigned long flags;
1403 #if DEBUG
1404 printk("Entering handle_abort_timeout\n");
1405 #endif
1406 save_flags(flags);
1407 cli();
1408 /* If it is in use, ignore it. */
1409 if (!sony_inuse)
1411 /* We can't use abort_read(), because it will sleep
1412 or schedule in the timer interrupt. Just start
1413 the operation, finish it on the next access to
1414 the drive. */
1415 clear_result_ready();
1416 clear_param_reg();
1417 write_cmd(SONY_ABORT_CMD);
1419 sony_blocks_left = 0;
1420 readahead_dataleft = 0;
1421 readahead_bad = 0;
1422 abort_read_started = 1;
1424 restore_flags(flags);
1425 #if DEBUG
1426 printk("Leaving handle_abort_timeout\n");
1427 #endif
1430 /* Actually get data and status from the drive. */
1431 static void
1432 input_data(char *buffer,
1433 unsigned int bytesleft,
1434 unsigned int nblocks,
1435 unsigned int offset,
1436 unsigned int skip)
1438 int i;
1439 volatile unsigned char val;
1442 #if DEBUG
1443 printk("Entering input_data\n");
1444 #endif
1445 /* If an XA disk on a CDU31A, skip the first 12 bytes of data from
1446 the disk. The real data is after that. */
1447 if (sony_xa_mode)
1449 for(i=0; i<CD_XA_HEAD; i++)
1451 val = read_data_register();
1455 clear_data_ready();
1457 if (bytesleft == 2048) /* 2048 byte direct buffer transfer */
1459 insb(sony_cd_read_reg, buffer, 2048);
1460 readahead_dataleft = 0;
1462 else
1464 /* If the input read did not align with the beginning of the block,
1465 skip the necessary bytes. */
1466 if (skip != 0)
1468 insb(sony_cd_read_reg, readahead_buffer, skip);
1471 /* Get the data into the buffer. */
1472 insb(sony_cd_read_reg, &buffer[offset], bytesleft);
1474 /* Get the rest of the data into the readahead buffer at the
1475 proper location. */
1476 readahead_dataleft = (2048 - skip) - bytesleft;
1477 insb(sony_cd_read_reg,
1478 readahead_buffer + bytesleft,
1479 readahead_dataleft);
1481 sony_blocks_left -= nblocks;
1482 sony_next_block += nblocks;
1484 /* If an XA disk, we have to clear out the rest of the unused
1485 error correction data. */
1486 if (sony_xa_mode)
1488 for(i=0; i<CD_XA_TAIL; i++)
1490 val = read_data_register();
1493 #if DEBUG
1494 printk("Leaving input_data at %d\n", __LINE__);
1495 #endif
1498 /* read data from the drive. Note the nsect must be <= 4. */
1499 static void
1500 read_data_block(char *buffer,
1501 unsigned int block,
1502 unsigned int nblocks,
1503 unsigned char res_reg[],
1504 int *res_size)
1506 unsigned int retry_count;
1507 unsigned int bytesleft;
1508 unsigned int offset;
1509 unsigned int skip;
1512 #if DEBUG
1513 printk("Entering read_data_block\n");
1514 #endif
1516 res_reg[0] = 0;
1517 res_reg[1] = 0;
1518 *res_size = 0;
1519 bytesleft = nblocks * 512;
1520 offset = 0;
1522 /* If the data in the read-ahead does not match the block offset,
1523 then fix things up. */
1524 if (((block % 4) * 512) != ((2048 - readahead_dataleft) % 2048))
1526 sony_next_block += block % 4;
1527 sony_blocks_left -= block % 4;
1528 skip = (block % 4) * 512;
1530 else
1532 skip = 0;
1535 /* We have readahead data in the buffer, get that first before we
1536 decide if a read is necessary. */
1537 if (readahead_dataleft != 0)
1539 if (bytesleft > readahead_dataleft)
1541 /* The readahead will not fill the requested buffer, but
1542 get the data out of the readahead into the buffer. */
1543 memcpy(buffer,
1544 readahead_buffer + (2048 - readahead_dataleft),
1545 readahead_dataleft);
1546 readahead_dataleft = 0;
1547 bytesleft -= readahead_dataleft;
1548 offset += readahead_dataleft;
1550 else
1552 /* The readahead will fill the whole buffer, get the data
1553 and return. */
1554 memcpy(buffer,
1555 readahead_buffer + (2048 - readahead_dataleft),
1556 bytesleft);
1557 readahead_dataleft -= bytesleft;
1558 bytesleft = 0;
1559 sony_blocks_left -= nblocks;
1560 sony_next_block += nblocks;
1562 /* If the data in the readahead is bad, return an error so the
1563 driver will abort the buffer. */
1564 if (readahead_bad)
1566 res_reg[0] = 0x20;
1567 res_reg[1] = SONY_BAD_DATA_ERR;
1568 *res_size = 2;
1571 if (readahead_dataleft == 0)
1573 readahead_bad = 0;
1576 /* Final transfer is done for read command, get final result. */
1577 if (sony_blocks_left == 0)
1579 get_result(res_reg, res_size);
1581 #if DEBUG
1582 printk("Leaving read_data_block at %d\n", __LINE__);
1583 #endif
1584 return;
1588 /* Wait for the drive to tell us we have something */
1589 retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
1590 while ((retry_count > jiffies) && !(is_data_ready()))
1592 while (handle_sony_cd_attention())
1595 sony_sleep();
1597 if (!(is_data_ready()))
1599 if (is_result_ready())
1601 get_result(res_reg, res_size);
1602 if ((res_reg[0] & 0xf0) != 0x20)
1604 printk("CDU31A: Got result that should have been error: %d\n",
1605 res_reg[0]);
1606 res_reg[0] = 0x20;
1607 res_reg[1] = SONY_BAD_DATA_ERR;
1608 *res_size = 2;
1610 abort_read();
1612 else
1614 #if DEBUG
1615 printk("CDU31A timeout out %d\n", __LINE__);
1616 #endif
1617 res_reg[0] = 0x20;
1618 res_reg[1] = SONY_TIMEOUT_OP_ERR;
1619 *res_size = 2;
1620 abort_read();
1623 else
1625 input_data(buffer, bytesleft, nblocks, offset, skip);
1627 /* Wait for the status from the drive. */
1628 retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
1629 while ((retry_count > jiffies) && !(is_result_ready()))
1631 while (handle_sony_cd_attention())
1634 sony_sleep();
1637 if (!is_result_ready())
1639 #if DEBUG
1640 printk("CDU31A timeout out %d\n", __LINE__);
1641 #endif
1642 res_reg[0] = 0x20;
1643 res_reg[1] = SONY_TIMEOUT_OP_ERR;
1644 *res_size = 2;
1645 abort_read();
1647 else
1649 get_result(res_reg, res_size);
1651 /* If we got a buffer status, handle that. */
1652 if ((res_reg[0] & 0xf0) == 0x50)
1655 if ( (res_reg[0] == SONY_NO_CIRC_ERR_BLK_STAT)
1656 || (res_reg[0] == SONY_NO_LECC_ERR_BLK_STAT)
1657 || (res_reg[0] == SONY_RECOV_LECC_ERR_BLK_STAT))
1659 /* The data was successful, but if data was read from
1660 the readahead and it was bad, set the whole
1661 buffer as bad. */
1662 if (readahead_bad)
1664 readahead_bad = 0;
1665 res_reg[0] = 0x20;
1666 res_reg[1] = SONY_BAD_DATA_ERR;
1667 *res_size = 2;
1670 else
1672 printk("CDU31A: Data block error: 0x%x\n", res_reg[0]);
1673 res_reg[0] = 0x20;
1674 res_reg[1] = SONY_BAD_DATA_ERR;
1675 *res_size = 2;
1677 /* Data is in the readahead buffer but an error was returned.
1678 Make sure future requests don't use the data. */
1679 if (bytesleft != 2048)
1681 readahead_bad = 1;
1685 /* Final transfer is done for read command, get final result. */
1686 if (sony_blocks_left == 0)
1688 get_result(res_reg, res_size);
1691 else if ((res_reg[0] & 0xf0) != 0x20)
1693 /* The drive gave me bad status, I don't know what to do.
1694 Reset the driver and return an error. */
1695 printk("CDU31A: Invalid block status: 0x%x\n", res_reg[0]);
1696 restart_on_error();
1697 res_reg[0] = 0x20;
1698 res_reg[1] = SONY_BAD_DATA_ERR;
1699 *res_size = 2;
1703 #if DEBUG
1704 printk("Leaving read_data_block at %d\n", __LINE__);
1705 #endif
1708 static int scd_spinup(void);
1711 * The OS calls this to perform a read or write operation to the drive.
1712 * Write obviously fail. Reads to a read ahead of sony_buffer_size
1713 * bytes to help speed operations. This especially helps since the OS
1714 * uses 1024 byte blocks and the drive uses 2048 byte blocks. Since most
1715 * data access on a CD is done sequentially, this saves a lot of operations.
1717 static void
1718 do_cdu31a_request(void)
1720 int block;
1721 int nblock;
1722 unsigned char res_reg[12];
1723 unsigned int res_size;
1724 int num_retries;
1725 unsigned long flags;
1728 #if DEBUG
1729 printk("Entering do_cdu31a_request\n");
1730 #endif
1733 * Make sure no one else is using the driver; wait for them
1734 * to finish if it is so.
1736 save_flags(flags);
1737 cli();
1738 while (sony_inuse)
1740 interruptible_sleep_on(&sony_wait);
1741 if (signal_pending(current))
1743 restore_flags(flags);
1744 if (CURRENT && CURRENT->rq_status != RQ_INACTIVE)
1746 end_request(0);
1748 restore_flags(flags);
1749 #if DEBUG
1750 printk("Leaving do_cdu31a_request at %d\n", __LINE__);
1751 #endif
1752 return;
1755 sony_inuse = 1;
1756 has_cd_task = current;
1758 /* Get drive status before doing anything. */
1759 while (handle_sony_cd_attention())
1762 /* Make sure we have a valid TOC. */
1763 sony_get_toc();
1765 sti();
1767 /* Make sure the timer is cancelled. */
1768 del_timer(&cdu31a_abort_timer);
1770 while (1)
1772 cdu31a_request_startover:
1774 * The beginning here is stolen from the hard disk driver. I hope
1775 * it's right.
1777 if (!(CURRENT) || CURRENT->rq_status == RQ_INACTIVE)
1779 goto end_do_cdu31a_request;
1782 if (!sony_spun_up)
1784 scd_spinup();
1787 /* I don't use INIT_REQUEST because it calls return, which would
1788 return without unlocking the device. It shouldn't matter,
1789 but just to be safe... */
1790 if (MAJOR(CURRENT->rq_dev) != MAJOR_NR)
1792 panic(DEVICE_NAME ": request list destroyed");
1794 if (CURRENT->bh)
1796 if (!buffer_locked(CURRENT->bh))
1798 panic(DEVICE_NAME ": block not locked");
1802 block = CURRENT->sector;
1803 nblock = CURRENT->nr_sectors;
1805 if (!sony_toc_read)
1807 printk("CDU31A: TOC not read\n");
1808 end_request(0);
1809 goto cdu31a_request_startover;
1812 switch(CURRENT->cmd)
1814 case READ:
1816 * If the block address is invalid or the request goes beyond the end of
1817 * the media, return an error.
1819 #if 0
1820 if ((block / 4) < sony_toc.start_track_lba)
1822 printk("CDU31A: Request before beginning of media\n");
1823 end_request(0);
1824 goto cdu31a_request_startover;
1826 #endif
1827 if ((block / 4) >= sony_toc.lead_out_start_lba)
1829 printk("CDU31A: Request past end of media\n");
1830 end_request(0);
1831 goto cdu31a_request_startover;
1833 if (((block + nblock) / 4) >= sony_toc.lead_out_start_lba)
1835 printk("CDU31A: Request past end of media\n");
1836 end_request(0);
1837 goto cdu31a_request_startover;
1840 num_retries = 0;
1842 try_read_again:
1843 while (handle_sony_cd_attention())
1846 if (!sony_toc_read)
1848 printk("CDU31A: TOC not read\n");
1849 end_request(0);
1850 goto cdu31a_request_startover;
1853 /* If no data is left to be read from the drive, start the
1854 next request. */
1855 if (sony_blocks_left == 0)
1857 if (start_request(block / 4, CDU31A_READAHEAD / 4, 0))
1859 end_request(0);
1860 goto cdu31a_request_startover;
1863 /* If the requested block is not the next one waiting in
1864 the driver, abort the current operation and start a
1865 new one. */
1866 else if (block != sony_next_block)
1868 #if DEBUG
1869 printk("CDU31A Warning: Read for block %d, expected %d\n",
1870 block,
1871 sony_next_block);
1872 #endif
1873 abort_read();
1874 if (!sony_toc_read)
1876 printk("CDU31A: TOC not read\n");
1877 end_request(0);
1878 goto cdu31a_request_startover;
1880 if (start_request(block / 4, CDU31A_READAHEAD / 4, 0))
1882 printk("CDU31a: start request failed\n");
1883 end_request(0);
1884 goto cdu31a_request_startover;
1888 read_data_block(CURRENT->buffer, block, nblock, res_reg, &res_size);
1889 if (res_reg[0] == 0x20)
1891 if (num_retries > MAX_CDU31A_RETRIES)
1893 end_request(0);
1894 goto cdu31a_request_startover;
1897 num_retries++;
1898 if (res_reg[1] == SONY_NOT_SPIN_ERR)
1900 do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg, &res_size);
1902 else
1904 printk("CDU31A: %s error for block %d, nblock %d\n", translate_error(res_reg[1]), block, nblock);
1906 goto try_read_again;
1908 else
1910 end_request(1);
1912 break;
1914 case WRITE:
1915 end_request(0);
1916 break;
1918 default:
1919 panic("CDU31A: Unknown cmd");
1923 end_do_cdu31a_request:
1924 cli();
1925 #if 0
1926 /* After finished, cancel any pending operations. */
1927 abort_read();
1928 #else
1929 /* Start a timer to time out after a while to disable
1930 the read. */
1931 cdu31a_abort_timer.expires = jiffies + 2*HZ; /* Wait 2 seconds */
1932 add_timer(&cdu31a_abort_timer);
1933 #endif
1935 has_cd_task = NULL;
1936 sony_inuse = 0;
1937 wake_up_interruptible(&sony_wait);
1938 restore_flags(flags);
1939 #if DEBUG
1940 printk("Leaving do_cdu31a_request at %d\n", __LINE__);
1941 #endif
1946 * Read the table of contents from the drive and set up TOC if
1947 * successful.
1949 static void
1950 sony_get_toc(void)
1952 unsigned char res_reg[2];
1953 unsigned int res_size;
1954 unsigned char parms[1];
1955 int session;
1956 int num_spin_ups;
1957 int totaltracks = 0;
1958 int mint = 99;
1959 int maxt = 0;
1961 #if DEBUG
1962 printk("Entering sony_get_toc\n");
1963 #endif
1965 num_spin_ups = 0;
1966 if (!sony_toc_read)
1968 respinup_on_gettoc:
1969 /* Ignore the result, since it might error if spinning already. */
1970 do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg, &res_size);
1972 do_sony_cd_cmd(SONY_READ_TOC_CMD, NULL, 0, res_reg, &res_size);
1974 /* The drive sometimes returns error 0. I don't know why, but ignore
1975 it. It seems to mean the drive has already done the operation. */
1976 if ((res_size < 2) || ((res_reg[0] != 0) && (res_reg[1] != 0)))
1978 /* If the drive is already playing, it's ok. */
1979 if ((res_reg[1] == SONY_AUDIO_PLAYING_ERR) || (res_reg[1] == 0))
1981 goto gettoc_drive_spinning;
1984 /* If the drive says it is not spun up (even though we just did it!)
1985 then retry the operation at least a few times. */
1986 if ( (res_reg[1] == SONY_NOT_SPIN_ERR)
1987 && (num_spin_ups < MAX_CDU31A_RETRIES))
1989 num_spin_ups++;
1990 goto respinup_on_gettoc;
1993 printk("cdu31a: Error reading TOC: %x %s\n",
1994 res_reg[0], translate_error(res_reg[1]));
1995 return;
1998 gettoc_drive_spinning:
2000 /* The idea here is we keep asking for sessions until the command
2001 fails. Then we know what the last valid session on the disk is.
2002 No need to check session 0, since session 0 is the same as session
2003 1; the command returns different information if you give it 0.
2005 #if DEBUG
2006 memset(&sony_toc, 0x0e, sizeof(sony_toc));
2007 memset(&single_toc, 0x0f, sizeof(single_toc));
2008 #endif
2009 session = 1;
2010 while (1)
2012 #if DEBUG
2013 printk("Trying session %d\n", session);
2014 #endif
2015 parms[0] = session;
2016 do_sony_cd_cmd(SONY_READ_TOC_SPEC_CMD,
2017 parms,
2019 res_reg,
2020 &res_size);
2022 #if DEBUG
2023 printk("%2.2x %2.2x\n", res_reg[0], res_reg[1]);
2024 #endif
2026 if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20))
2028 /* An error reading the TOC, this must be past the last session. */
2029 break;
2031 #if DEBUG
2032 printk("Reading session %d\n", session);
2033 #endif
2035 parms[0] = session;
2036 do_sony_cd_cmd(SONY_REQ_TOC_DATA_SPEC_CMD,
2037 parms,
2039 (unsigned char *) &single_toc,
2040 &res_size);
2041 if ((res_size < 2) || ((single_toc.exec_status[0] & 0xf0) == 0x20))
2043 printk("cdu31a: Error reading session %d: %x %s\n",
2044 session,
2045 single_toc.exec_status[0],
2046 translate_error(single_toc.exec_status[1]));
2047 /* An error reading the TOC. Return without sony_toc_read
2048 set. */
2049 return;
2051 #if DEBUG
2052 printk("add0 %01x, con0 %01x, poi0 %02x, 1st trk %d, dsktyp %x, dum0 %x\n",
2053 single_toc.address0, single_toc.control0, single_toc.point0,
2054 bcd_to_int(single_toc.first_track_num), single_toc.disk_type, single_toc.dummy0);
2055 printk("add1 %01x, con1 %01x, poi1 %02x, lst trk %d, dummy1 %x, dum2 %x\n",
2056 single_toc.address1, single_toc.control1, single_toc.point1,
2057 bcd_to_int(single_toc.last_track_num), single_toc.dummy1, single_toc.dummy2);
2058 printk("add2 %01x, con2 %01x, poi2 %02x leadout start min %d, sec %d, frame %d\n",
2059 single_toc.address2, single_toc.control2, single_toc.point2,
2060 bcd_to_int(single_toc.lead_out_start_msf[0]),
2061 bcd_to_int(single_toc.lead_out_start_msf[1]),
2062 bcd_to_int(single_toc.lead_out_start_msf[2]));
2063 if (res_size > 18 && single_toc.pointb0 > 0xaf)
2064 printk("addb0 %01x, conb0 %01x, poib0 %02x, nextsession min %d, sec %d, frame %d\n"
2065 "#mode5_ptrs %02d, max_start_outer_leadout_msf min %d, sec %d, frame %d\n",
2066 single_toc.addressb0, single_toc.controlb0, single_toc.pointb0,
2067 bcd_to_int(single_toc.next_poss_prog_area_msf[0]),
2068 bcd_to_int(single_toc.next_poss_prog_area_msf[1]),
2069 bcd_to_int(single_toc.next_poss_prog_area_msf[2]),
2070 single_toc.num_mode_5_pointers,
2071 bcd_to_int(single_toc.max_start_outer_leadout_msf[0]),
2072 bcd_to_int(single_toc.max_start_outer_leadout_msf[1]),
2073 bcd_to_int(single_toc.max_start_outer_leadout_msf[2]));
2074 if (res_size > 27 && single_toc.pointb1 > 0xaf)
2075 printk("addb1 %01x, conb1 %01x, poib1 %02x, %x %x %x %x #skipint_ptrs %d, #skiptrkassign %d %x\n",
2076 single_toc.addressb1, single_toc.controlb1, single_toc.pointb1,
2077 single_toc.dummyb0_1[0],
2078 single_toc.dummyb0_1[1],
2079 single_toc.dummyb0_1[2],
2080 single_toc.dummyb0_1[3],
2081 single_toc.num_skip_interval_pointers,
2082 single_toc.num_skip_track_assignments,
2083 single_toc.dummyb0_2);
2084 if (res_size > 36 && single_toc.pointb2 > 0xaf)
2085 printk("addb2 %01x, conb2 %01x, poib2 %02x, %02x %02x %02x %02x %02x %02x %02x\n",
2086 single_toc.addressb2, single_toc.controlb2, single_toc.pointb2,
2087 single_toc.tracksb2[0],
2088 single_toc.tracksb2[1],
2089 single_toc.tracksb2[2],
2090 single_toc.tracksb2[3],
2091 single_toc.tracksb2[4],
2092 single_toc.tracksb2[5],
2093 single_toc.tracksb2[6]);
2094 if (res_size > 45 && single_toc.pointb3 > 0xaf)
2095 printk("addb3 %01x, conb3 %01x, poib3 %02x, %02x %02x %02x %02x %02x %02x %02x\n",
2096 single_toc.addressb3, single_toc.controlb3, single_toc.pointb3,
2097 single_toc.tracksb3[0],
2098 single_toc.tracksb3[1],
2099 single_toc.tracksb3[2],
2100 single_toc.tracksb3[3],
2101 single_toc.tracksb3[4],
2102 single_toc.tracksb3[5],
2103 single_toc.tracksb3[6]);
2104 if (res_size > 54 && single_toc.pointb4 > 0xaf)
2105 printk("addb4 %01x, conb4 %01x, poib4 %02x, %02x %02x %02x %02x %02x %02x %02x\n",
2106 single_toc.addressb4, single_toc.controlb4, single_toc.pointb4,
2107 single_toc.tracksb4[0],
2108 single_toc.tracksb4[1],
2109 single_toc.tracksb4[2],
2110 single_toc.tracksb4[3],
2111 single_toc.tracksb4[4],
2112 single_toc.tracksb4[5],
2113 single_toc.tracksb4[6]);
2114 if (res_size > 63 && single_toc.pointc0 > 0xaf)
2115 printk("addc0 %01x, conc0 %01x, poic0 %02x, %02x %02x %02x %02x %02x %02x %02x\n",
2116 single_toc.addressc0, single_toc.controlc0, single_toc.pointc0,
2117 single_toc.dummyc0[0],
2118 single_toc.dummyc0[1],
2119 single_toc.dummyc0[2],
2120 single_toc.dummyc0[3],
2121 single_toc.dummyc0[4],
2122 single_toc.dummyc0[5],
2123 single_toc.dummyc0[6]);
2124 #endif
2125 #undef DEBUG
2126 #define DEBUG 0
2128 sony_toc.lead_out_start_msf[0] = bcd_to_int(single_toc.lead_out_start_msf[0]);
2129 sony_toc.lead_out_start_msf[1] = bcd_to_int(single_toc.lead_out_start_msf[1]);
2130 sony_toc.lead_out_start_msf[2] = bcd_to_int(single_toc.lead_out_start_msf[2]);
2131 sony_toc.lead_out_start_lba = single_toc.lead_out_start_lba =
2132 msf_to_log(sony_toc.lead_out_start_msf);
2134 /* For points that do not exist, move the data over them
2135 to the right location. */
2136 if (single_toc.pointb0 != 0xb0)
2138 memmove(((char *) &single_toc) + 27,
2139 ((char *) &single_toc) + 18,
2140 res_size - 18);
2141 res_size += 9;
2143 else if (res_size > 18) {
2144 sony_toc.lead_out_start_msf[0] = bcd_to_int(single_toc.max_start_outer_leadout_msf[0]);
2145 sony_toc.lead_out_start_msf[1] = bcd_to_int(single_toc.max_start_outer_leadout_msf[1]);
2146 sony_toc.lead_out_start_msf[2] = bcd_to_int(single_toc.max_start_outer_leadout_msf[2]);
2147 sony_toc.lead_out_start_lba = msf_to_log(sony_toc.lead_out_start_msf);
2149 if (single_toc.pointb1 != 0xb1)
2151 memmove(((char *) &single_toc) + 36,
2152 ((char *) &single_toc) + 27,
2153 res_size - 27);
2154 res_size += 9;
2156 if (single_toc.pointb2 != 0xb2)
2158 memmove(((char *) &single_toc) + 45,
2159 ((char *) &single_toc) + 36,
2160 res_size - 36);
2161 res_size += 9;
2163 if (single_toc.pointb3 != 0xb3)
2165 memmove(((char *) &single_toc) + 54,
2166 ((char *) &single_toc) + 45,
2167 res_size - 45);
2168 res_size += 9;
2170 if (single_toc.pointb4 != 0xb4)
2172 memmove(((char *) &single_toc) + 63,
2173 ((char *) &single_toc) + 54,
2174 res_size - 54);
2175 res_size += 9;
2177 if (single_toc.pointc0 != 0xc0)
2179 memmove(((char *) &single_toc) + 72,
2180 ((char *) &single_toc) + 63,
2181 res_size - 63);
2182 res_size += 9;
2185 #if DEBUG
2186 printk("start track lba %u, leadout start lba %u\n",
2187 single_toc.start_track_lba, single_toc.lead_out_start_lba);
2188 { int i;
2189 for (i = 0; i < 1 + bcd_to_int(single_toc.last_track_num)
2190 - bcd_to_int(single_toc.first_track_num); i++) {
2191 printk("trk %02d: add 0x%01x, con 0x%01x, track %02d, start min %02d, sec %02d, frame %02d\n",
2192 i, single_toc.tracks[i].address, single_toc.tracks[i].control,
2193 bcd_to_int(single_toc.tracks[i].track),
2194 bcd_to_int(single_toc.tracks[i].track_start_msf[0]),
2195 bcd_to_int(single_toc.tracks[i].track_start_msf[1]),
2196 bcd_to_int(single_toc.tracks[i].track_start_msf[2]));
2197 if (mint > bcd_to_int(single_toc.tracks[i].track))
2198 mint = bcd_to_int(single_toc.tracks[i].track);
2199 if (maxt < bcd_to_int(single_toc.tracks[i].track))
2200 maxt = bcd_to_int(single_toc.tracks[i].track);
2202 printk("min track number %d, max track number %d\n", mint, maxt);
2204 #endif
2206 /* prepare a special table of contents for a CD-I disc. They don't have one. */
2207 if (single_toc.disk_type == 0x10 &&
2208 single_toc.first_track_num == 2 &&
2209 single_toc.last_track_num == 2 /* CD-I */)
2211 sony_toc.tracks[totaltracks].address = 1;
2212 sony_toc.tracks[totaltracks].control = 4; /* force data tracks */
2213 sony_toc.tracks[totaltracks].track = 1;
2214 sony_toc.tracks[totaltracks].track_start_msf[0] = 0;
2215 sony_toc.tracks[totaltracks].track_start_msf[1] = 2;
2216 sony_toc.tracks[totaltracks].track_start_msf[2] = 0;
2217 mint = maxt = 1;
2218 totaltracks++;
2219 } else
2220 /* gather track entries from this session */
2221 { int i;
2222 for (i = 0; i < 1 + bcd_to_int(single_toc.last_track_num)
2223 - bcd_to_int(single_toc.first_track_num); i++, totaltracks++) {
2224 sony_toc.tracks[totaltracks].address = single_toc.tracks[i].address;
2225 sony_toc.tracks[totaltracks].control = single_toc.tracks[i].control;
2226 sony_toc.tracks[totaltracks].track = bcd_to_int(single_toc.tracks[i].track);
2227 sony_toc.tracks[totaltracks].track_start_msf[0] =
2228 bcd_to_int(single_toc.tracks[i].track_start_msf[0]);
2229 sony_toc.tracks[totaltracks].track_start_msf[1] =
2230 bcd_to_int(single_toc.tracks[i].track_start_msf[1]);
2231 sony_toc.tracks[totaltracks].track_start_msf[2] =
2232 bcd_to_int(single_toc.tracks[i].track_start_msf[2]);
2233 if (i == 0)
2234 single_toc.start_track_lba = msf_to_log(sony_toc.tracks[totaltracks].track_start_msf);
2235 if (mint > sony_toc.tracks[totaltracks].track)
2236 mint = sony_toc.tracks[totaltracks].track;
2237 if (maxt < sony_toc.tracks[totaltracks].track)
2238 maxt = sony_toc.tracks[totaltracks].track;
2241 sony_toc.first_track_num = mint;
2242 sony_toc.last_track_num = maxt;
2243 /* Disk type of last session wins. For example:
2244 CD-Extra has disk type 0 for the first session, so
2245 a dumb HiFi CD player thinks it is a plain audio CD.
2246 We are interested in the disk type of the last session,
2247 which is 0x20 (XA) for CD-Extra, so we can access the
2248 data track ... */
2249 sony_toc.disk_type = single_toc.disk_type;
2250 sony_toc.sessions = session;
2252 /* don't believe everything :-) */
2253 if (session == 1)
2254 single_toc.start_track_lba = 0;
2255 sony_toc.start_track_lba = single_toc.start_track_lba;
2257 if (session > 1 && single_toc.pointb0 == 0xb0 &&
2258 sony_toc.lead_out_start_lba == single_toc.lead_out_start_lba)
2260 break;
2263 /* Let's not get carried away... */
2264 if (session > 40)
2266 printk("cdu31a: too many sessions: %d\n", session);
2267 break;
2269 session++;
2271 sony_toc.track_entries = totaltracks;
2272 /* add one entry for the LAST track with track number CDROM_LEADOUT */
2273 sony_toc.tracks[totaltracks].address = single_toc.address2;
2274 sony_toc.tracks[totaltracks].control = single_toc.control2;
2275 sony_toc.tracks[totaltracks].track = CDROM_LEADOUT;
2276 sony_toc.tracks[totaltracks].track_start_msf[0] =
2277 sony_toc.lead_out_start_msf[0];
2278 sony_toc.tracks[totaltracks].track_start_msf[1] =
2279 sony_toc.lead_out_start_msf[1];
2280 sony_toc.tracks[totaltracks].track_start_msf[2] =
2281 sony_toc.lead_out_start_msf[2];
2283 sony_toc_read = 1;
2284 #undef DEBUG
2285 #if DEBUG
2286 printk("Disk session %d, start track: %d, stop track: %d\n",
2287 session,
2288 single_toc.start_track_lba,
2289 single_toc.lead_out_start_lba);
2290 #endif
2292 #if DEBUG
2293 printk("Leaving sony_get_toc\n");
2294 #endif
2299 * Uniform cdrom interface function
2300 * return multisession offset and sector information
2302 static int scd_get_last_session(struct cdrom_device_info *cdi,
2303 struct cdrom_multisession *ms_info)
2305 if (ms_info == NULL)
2306 return 1;
2308 if (!sony_toc_read)
2309 sony_get_toc();
2311 ms_info->addr_format = CDROM_LBA;
2312 ms_info->addr.lba = sony_toc.start_track_lba;
2313 ms_info->xa_flag = sony_toc.disk_type == SONY_XA_DISK_TYPE ||
2314 sony_toc.disk_type == 0x10 /* CDI */;
2316 return 0;
2320 * Search for a specific track in the table of contents.
2322 static int
2323 find_track(int track)
2325 int i;
2327 for (i = 0; i <= sony_toc.track_entries; i++)
2329 if (sony_toc.tracks[i].track == track)
2331 return i;
2335 return -1;
2340 * Read the subcode and put it in last_sony_subcode for future use.
2342 static int
2343 read_subcode(void)
2345 unsigned int res_size;
2348 do_sony_cd_cmd(SONY_REQ_SUBCODE_ADDRESS_CMD,
2349 NULL,
2351 (unsigned char *) &last_sony_subcode,
2352 &res_size);
2353 if ((res_size < 2) || ((last_sony_subcode.exec_status[0] & 0xf0) == 0x20))
2355 printk("Sony CDROM error %s (read_subcode)\n",
2356 translate_error(last_sony_subcode.exec_status[1]));
2357 return -EIO;
2360 last_sony_subcode.track_num = bcd_to_int(last_sony_subcode.track_num);
2361 last_sony_subcode.index_num = bcd_to_int(last_sony_subcode.index_num);
2362 last_sony_subcode.abs_msf[0] = bcd_to_int(last_sony_subcode.abs_msf[0]);
2363 last_sony_subcode.abs_msf[1] = bcd_to_int(last_sony_subcode.abs_msf[1]);
2364 last_sony_subcode.abs_msf[2] = bcd_to_int(last_sony_subcode.abs_msf[2]);
2366 last_sony_subcode.rel_msf[0] = bcd_to_int(last_sony_subcode.rel_msf[0]);
2367 last_sony_subcode.rel_msf[1] = bcd_to_int(last_sony_subcode.rel_msf[1]);
2368 last_sony_subcode.rel_msf[2] = bcd_to_int(last_sony_subcode.rel_msf[2]);
2369 return 0;
2373 * Uniform cdrom interface function
2374 * return the media catalog number found on some older audio cds
2376 static int
2377 scd_get_mcn(struct cdrom_device_info *cdi, struct cdrom_mcn *mcn)
2379 unsigned char resbuffer[2 + 14];
2380 unsigned char *mcnp = mcn->medium_catalog_number;
2381 unsigned char *resp = resbuffer + 3;
2382 unsigned int res_size;
2384 memset(mcn->medium_catalog_number, 0, 14);
2385 do_sony_cd_cmd(SONY_REQ_UPC_EAN_CMD,
2386 NULL,
2388 resbuffer,
2389 &res_size);
2390 if ((res_size < 2) || ((resbuffer[0] & 0xf0) == 0x20))
2392 else {
2393 /* packed bcd to single ASCII digits */
2394 *mcnp++ = (*resp >> 4) + '0';
2395 *mcnp++ = (*resp++ & 0x0f) + '0';
2396 *mcnp++ = (*resp >> 4) + '0';
2397 *mcnp++ = (*resp++ & 0x0f) + '0';
2398 *mcnp++ = (*resp >> 4) + '0';
2399 *mcnp++ = (*resp++ & 0x0f) + '0';
2400 *mcnp++ = (*resp >> 4) + '0';
2401 *mcnp++ = (*resp++ & 0x0f) + '0';
2402 *mcnp++ = (*resp >> 4) + '0';
2403 *mcnp++ = (*resp++ & 0x0f) + '0';
2404 *mcnp++ = (*resp >> 4) + '0';
2405 *mcnp++ = (*resp++ & 0x0f) + '0';
2406 *mcnp++ = (*resp >> 4) + '0';
2408 *mcnp = '\0';
2409 return 0;
2414 * Get the subchannel info like the CDROMSUBCHNL command wants to see it. If
2415 * the drive is playing, the subchannel needs to be read (since it would be
2416 * changing). If the drive is paused or completed, the subcode information has
2417 * already been stored, just use that. The ioctl call wants things in decimal
2418 * (not BCD), so all the conversions are done.
2420 static int
2421 sony_get_subchnl_info(struct cdrom_subchnl *schi)
2423 /* Get attention stuff */
2424 while (handle_sony_cd_attention())
2427 sony_get_toc();
2428 if (!sony_toc_read)
2430 return -EIO;
2433 switch (sony_audio_status)
2435 case CDROM_AUDIO_NO_STATUS:
2436 case CDROM_AUDIO_PLAY:
2437 if (read_subcode() < 0)
2439 return -EIO;
2441 break;
2443 case CDROM_AUDIO_PAUSED:
2444 case CDROM_AUDIO_COMPLETED:
2445 break;
2447 #if 0
2448 case CDROM_AUDIO_NO_STATUS:
2449 schi->cdsc_audiostatus = sony_audio_status;
2450 return 0;
2451 break;
2452 #endif
2453 case CDROM_AUDIO_INVALID:
2454 case CDROM_AUDIO_ERROR:
2455 default:
2456 return -EIO;
2459 schi->cdsc_audiostatus = sony_audio_status;
2460 schi->cdsc_adr = last_sony_subcode.address;
2461 schi->cdsc_ctrl = last_sony_subcode.control;
2462 schi->cdsc_trk = last_sony_subcode.track_num;
2463 schi->cdsc_ind = last_sony_subcode.index_num;
2464 if (schi->cdsc_format == CDROM_MSF)
2466 schi->cdsc_absaddr.msf.minute = last_sony_subcode.abs_msf[0];
2467 schi->cdsc_absaddr.msf.second = last_sony_subcode.abs_msf[1];
2468 schi->cdsc_absaddr.msf.frame = last_sony_subcode.abs_msf[2];
2470 schi->cdsc_reladdr.msf.minute = last_sony_subcode.rel_msf[0];
2471 schi->cdsc_reladdr.msf.second = last_sony_subcode.rel_msf[1];
2472 schi->cdsc_reladdr.msf.frame = last_sony_subcode.rel_msf[2];
2474 else if (schi->cdsc_format == CDROM_LBA)
2476 schi->cdsc_absaddr.lba = msf_to_log(last_sony_subcode.abs_msf);
2477 schi->cdsc_reladdr.lba = msf_to_log(last_sony_subcode.rel_msf);
2480 return 0;
2483 /* Get audio data from the drive. This is fairly complex because I
2484 am looking for status and data at the same time, but if I get status
2485 then I just look for data. I need to get the status immediately so
2486 the switch from audio to data tracks will happen quickly. */
2487 static void
2488 read_audio_data(char *buffer,
2489 unsigned char res_reg[],
2490 int *res_size)
2492 unsigned int retry_count;
2493 int result_read;
2496 res_reg[0] = 0;
2497 res_reg[1] = 0;
2498 *res_size = 0;
2499 result_read = 0;
2501 /* Wait for the drive to tell us we have something */
2502 retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
2503 continue_read_audio_wait:
2504 while ( (retry_count > jiffies)
2505 && !(is_data_ready())
2506 && !(is_result_ready() || result_read))
2508 while (handle_sony_cd_attention())
2511 sony_sleep();
2513 if (!(is_data_ready()))
2515 if (is_result_ready() && !result_read)
2517 get_result(res_reg, res_size);
2519 /* Read block status and continue waiting for data. */
2520 if ((res_reg[0] & 0xf0) == 0x50)
2522 result_read = 1;
2523 goto continue_read_audio_wait;
2525 /* Invalid data from the drive. Shut down the operation. */
2526 else if ((res_reg[0] & 0xf0) != 0x20)
2528 printk("CDU31A: Got result that should have been error: %d\n",
2529 res_reg[0]);
2530 res_reg[0] = 0x20;
2531 res_reg[1] = SONY_BAD_DATA_ERR;
2532 *res_size = 2;
2534 abort_read();
2536 else
2538 #if DEBUG
2539 printk("CDU31A timeout out %d\n", __LINE__);
2540 #endif
2541 res_reg[0] = 0x20;
2542 res_reg[1] = SONY_TIMEOUT_OP_ERR;
2543 *res_size = 2;
2544 abort_read();
2547 else
2549 clear_data_ready();
2551 /* If data block, then get 2340 bytes offset by 12. */
2552 if (sony_raw_data_mode)
2554 insb(sony_cd_read_reg, buffer + CD_XA_HEAD, CD_FRAMESIZE_RAW1);
2556 else
2558 /* Audio gets the whole 2352 bytes. */
2559 insb(sony_cd_read_reg, buffer, CD_FRAMESIZE_RAW);
2562 /* If I haven't already gotten the result, get it now. */
2563 if (!result_read)
2565 /* Wait for the drive to tell us we have something */
2566 retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
2567 while ((retry_count > jiffies) && !(is_result_ready()))
2569 while (handle_sony_cd_attention())
2572 sony_sleep();
2575 if (!is_result_ready())
2577 #if DEBUG
2578 printk("CDU31A timeout out %d\n", __LINE__);
2579 #endif
2580 res_reg[0] = 0x20;
2581 res_reg[1] = SONY_TIMEOUT_OP_ERR;
2582 *res_size = 2;
2583 abort_read();
2584 return;
2586 else
2588 get_result(res_reg, res_size);
2592 if ((res_reg[0] & 0xf0) == 0x50)
2594 if ( (res_reg[0] == SONY_NO_CIRC_ERR_BLK_STAT)
2595 || (res_reg[0] == SONY_NO_LECC_ERR_BLK_STAT)
2596 || (res_reg[0] == SONY_RECOV_LECC_ERR_BLK_STAT)
2597 || (res_reg[0] == SONY_NO_ERR_DETECTION_STAT))
2599 /* Ok, nothing to do. */
2601 else
2603 printk("CDU31A: Data block error: 0x%x\n", res_reg[0]);
2604 res_reg[0] = 0x20;
2605 res_reg[1] = SONY_BAD_DATA_ERR;
2606 *res_size = 2;
2609 else if ((res_reg[0] & 0xf0) != 0x20)
2611 /* The drive gave me bad status, I don't know what to do.
2612 Reset the driver and return an error. */
2613 printk("CDU31A: Invalid block status: 0x%x\n", res_reg[0]);
2614 restart_on_error();
2615 res_reg[0] = 0x20;
2616 res_reg[1] = SONY_BAD_DATA_ERR;
2617 *res_size = 2;
2622 /* Perform a raw data read. This will automatically detect the
2623 track type and read the proper data (audio or data). */
2624 static int
2625 read_audio(struct cdrom_read_audio *ra)
2627 int retval;
2628 unsigned char params[2];
2629 unsigned char res_reg[12];
2630 unsigned int res_size;
2631 unsigned int cframe;
2632 unsigned long flags;
2635 * Make sure no one else is using the driver; wait for them
2636 * to finish if it is so.
2638 save_flags(flags);
2639 cli();
2640 while (sony_inuse)
2642 interruptible_sleep_on(&sony_wait);
2643 if (signal_pending(current))
2645 restore_flags(flags);
2646 return -EAGAIN;
2649 sony_inuse = 1;
2650 has_cd_task = current;
2651 restore_flags(flags);
2653 if (!sony_spun_up)
2655 scd_spinup();
2658 /* Set the drive to do raw operations. */
2659 params[0] = SONY_SD_DECODE_PARAM;
2660 params[1] = 0x06 | sony_raw_data_mode;
2661 do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
2662 params,
2664 res_reg,
2665 &res_size);
2666 if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20))
2668 printk("CDU31A: Unable to set decode params: 0x%2.2x\n", res_reg[1]);
2669 return -EIO;
2672 /* From here down, we have to goto exit_read_audio instead of returning
2673 because the drive parameters have to be set back to data before
2674 return. */
2676 retval = 0;
2677 /* start_request clears out any readahead data, so it should be safe. */
2678 if (start_request(ra->addr.lba, ra->nframes, 1))
2680 retval = -EIO;
2681 goto exit_read_audio;
2684 /* For every requested frame. */
2685 cframe = 0;
2686 while (cframe < ra->nframes)
2688 read_audio_data(readahead_buffer, res_reg, &res_size);
2689 if ((res_reg[0] & 0xf0) == 0x20)
2691 if (res_reg[1] == SONY_BAD_DATA_ERR)
2693 printk("CDU31A: Data error on audio sector %d\n",
2694 ra->addr.lba + cframe);
2696 else if (res_reg[1] == SONY_ILL_TRACK_R_ERR)
2698 /* Illegal track type, change track types and start over. */
2699 sony_raw_data_mode = (sony_raw_data_mode) ? 0 : 1;
2701 /* Set the drive mode. */
2702 params[0] = SONY_SD_DECODE_PARAM;
2703 params[1] = 0x06 | sony_raw_data_mode;
2704 do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
2705 params,
2707 res_reg,
2708 &res_size);
2709 if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20))
2711 printk("CDU31A: Unable to set decode params: 0x%2.2x\n", res_reg[1]);
2712 retval = -EIO;
2713 goto exit_read_audio;
2716 /* Restart the request on the current frame. */
2717 if (start_request(ra->addr.lba + cframe, ra->nframes - cframe, 1))
2719 retval = -EIO;
2720 goto exit_read_audio;
2723 /* Don't go back to the top because don't want to get into
2724 and infinite loop. A lot of code gets duplicated, but
2725 that's no big deal, I don't guess. */
2726 read_audio_data(readahead_buffer, res_reg, &res_size);
2727 if ((res_reg[0] & 0xf0) == 0x20)
2729 if (res_reg[1] == SONY_BAD_DATA_ERR)
2731 printk("CDU31A: Data error on audio sector %d\n",
2732 ra->addr.lba + cframe);
2734 else
2736 printk("CDU31A: Error reading audio data on sector %d: %s\n",
2737 ra->addr.lba + cframe,
2738 translate_error(res_reg[1]));
2739 retval = -EIO;
2740 goto exit_read_audio;
2743 else
2745 copy_to_user((char *) (ra->buf + (CD_FRAMESIZE_RAW * cframe)),
2746 (char *) readahead_buffer,
2747 CD_FRAMESIZE_RAW);
2750 else
2752 printk("CDU31A: Error reading audio data on sector %d: %s\n",
2753 ra->addr.lba + cframe,
2754 translate_error(res_reg[1]));
2755 retval = -EIO;
2756 goto exit_read_audio;
2759 else
2761 copy_to_user((char *) (ra->buf + (CD_FRAMESIZE_RAW * cframe)),
2762 (char *) readahead_buffer,
2763 CD_FRAMESIZE_RAW);
2766 cframe++;
2769 get_result(res_reg, &res_size);
2770 if ((res_reg[0] & 0xf0) == 0x20)
2772 printk("CDU31A: Error return from audio read: %s\n",
2773 translate_error(res_reg[1]));
2774 retval = -EIO;
2775 goto exit_read_audio;
2778 exit_read_audio:
2780 /* Set the drive mode back to the proper one for the disk. */
2781 params[0] = SONY_SD_DECODE_PARAM;
2782 if (!sony_xa_mode)
2784 params[1] = 0x0f;
2786 else
2788 params[1] = 0x07;
2790 do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
2791 params,
2793 res_reg,
2794 &res_size);
2795 if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20))
2797 printk("CDU31A: Unable to reset decode params: 0x%2.2x\n", res_reg[1]);
2798 return -EIO;
2801 has_cd_task = NULL;
2802 sony_inuse = 0;
2803 wake_up_interruptible(&sony_wait);
2805 return(retval);
2808 static int
2809 do_sony_cd_cmd_chk(const char *name,
2810 unsigned char cmd,
2811 unsigned char *params,
2812 unsigned int num_params,
2813 unsigned char *result_buffer,
2814 unsigned int *result_size)
2816 do_sony_cd_cmd(cmd, params, num_params, result_buffer, result_size);
2817 if ((*result_size < 2) || ((result_buffer[0] & 0xf0) == 0x20))
2819 printk("Sony CDROM error %s (CDROM%s)\n", translate_error(result_buffer[1]), name);
2820 return -EIO;
2822 return 0;
2826 * Uniform cdrom interface function
2827 * open the tray
2829 static int scd_tray_move(struct cdrom_device_info *cdi, int position)
2831 if (position == 1 /* open tray */)
2833 unsigned char res_reg[12];
2834 unsigned int res_size;
2836 do_sony_cd_cmd(SONY_AUDIO_STOP_CMD, NULL, 0, res_reg, &res_size);
2837 do_sony_cd_cmd(SONY_SPIN_DOWN_CMD, NULL, 0, res_reg, &res_size);
2839 sony_audio_status = CDROM_AUDIO_INVALID;
2840 return do_sony_cd_cmd_chk("EJECT",SONY_EJECT_CMD, NULL, 0, res_reg, &res_size);
2841 } else {
2842 if (0 == scd_spinup())
2843 sony_spun_up = 1;
2844 return 0;
2849 * The big ugly ioctl handler.
2851 static int scd_audio_ioctl(struct cdrom_device_info *cdi,
2852 unsigned int cmd,
2853 void * arg)
2855 unsigned char res_reg[12];
2856 unsigned int res_size;
2857 unsigned char params[7];
2858 int i;
2861 switch (cmd)
2863 case CDROMSTART: /* Spin up the drive */
2864 return do_sony_cd_cmd_chk("START",SONY_SPIN_UP_CMD, NULL, 0, res_reg, &res_size);
2865 break;
2867 case CDROMSTOP: /* Spin down the drive */
2868 do_sony_cd_cmd(SONY_AUDIO_STOP_CMD, NULL, 0, res_reg, &res_size);
2871 * Spin the drive down, ignoring the error if the disk was
2872 * already not spinning.
2874 sony_audio_status = CDROM_AUDIO_NO_STATUS;
2875 return do_sony_cd_cmd_chk("STOP",SONY_SPIN_DOWN_CMD, NULL, 0, res_reg, &res_size);
2877 case CDROMPAUSE: /* Pause the drive */
2878 if(do_sony_cd_cmd_chk("PAUSE", SONY_AUDIO_STOP_CMD, NULL, 0, res_reg, &res_size))
2879 return -EIO;
2880 /* Get the current position and save it for resuming */
2881 if (read_subcode() < 0)
2883 return -EIO;
2885 cur_pos_msf[0] = last_sony_subcode.abs_msf[0];
2886 cur_pos_msf[1] = last_sony_subcode.abs_msf[1];
2887 cur_pos_msf[2] = last_sony_subcode.abs_msf[2];
2888 sony_audio_status = CDROM_AUDIO_PAUSED;
2889 return 0;
2890 break;
2892 case CDROMRESUME: /* Start the drive after being paused */
2893 if (sony_audio_status != CDROM_AUDIO_PAUSED)
2895 return -EINVAL;
2898 do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg, &res_size);
2900 /* Start the drive at the saved position. */
2901 params[1] = int_to_bcd(cur_pos_msf[0]);
2902 params[2] = int_to_bcd(cur_pos_msf[1]);
2903 params[3] = int_to_bcd(cur_pos_msf[2]);
2904 params[4] = int_to_bcd(final_pos_msf[0]);
2905 params[5] = int_to_bcd(final_pos_msf[1]);
2906 params[6] = int_to_bcd(final_pos_msf[2]);
2907 params[0] = 0x03;
2908 if(do_sony_cd_cmd_chk("RESUME",SONY_AUDIO_PLAYBACK_CMD, params, 7, res_reg, &res_size)<0)
2909 return -EIO;
2910 sony_audio_status = CDROM_AUDIO_PLAY;
2911 return 0;
2913 case CDROMPLAYMSF: /* Play starting at the given MSF address. */
2914 do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg, &res_size);
2916 /* The parameters are given in int, must be converted */
2917 for (i=1; i<7; i++)
2919 params[i] = int_to_bcd(((unsigned char *)arg)[i-1]);
2921 params[0] = 0x03;
2922 if(do_sony_cd_cmd_chk("PLAYMSF",SONY_AUDIO_PLAYBACK_CMD, params, 7, res_reg, &res_size)<0)
2923 return -EIO;
2925 /* Save the final position for pauses and resumes */
2926 final_pos_msf[0] = bcd_to_int(params[4]);
2927 final_pos_msf[1] = bcd_to_int(params[5]);
2928 final_pos_msf[2] = bcd_to_int(params[6]);
2929 sony_audio_status = CDROM_AUDIO_PLAY;
2930 return 0;
2932 case CDROMREADTOCHDR: /* Read the table of contents header */
2934 struct cdrom_tochdr *hdr;
2936 sony_get_toc();
2937 if (!sony_toc_read)
2939 return -EIO;
2942 hdr = (struct cdrom_tochdr *) arg;
2943 hdr->cdth_trk0 = sony_toc.first_track_num;
2944 hdr->cdth_trk1 = sony_toc.last_track_num;
2946 return 0;
2948 case CDROMREADTOCENTRY: /* Read a given table of contents entry */
2950 struct cdrom_tocentry *entry;
2951 int track_idx;
2952 unsigned char *msf_val = NULL;
2954 sony_get_toc();
2955 if (!sony_toc_read)
2957 return -EIO;
2960 entry = (struct cdrom_tocentry *) arg;
2962 track_idx = find_track(entry->cdte_track);
2963 if (track_idx < 0)
2965 return -EINVAL;
2968 entry->cdte_adr = sony_toc.tracks[track_idx].address;
2969 entry->cdte_ctrl = sony_toc.tracks[track_idx].control;
2970 msf_val = sony_toc.tracks[track_idx].track_start_msf;
2972 /* Logical buffer address or MSF format requested? */
2973 if (entry->cdte_format == CDROM_LBA)
2975 entry->cdte_addr.lba = msf_to_log(msf_val);
2977 else if (entry->cdte_format == CDROM_MSF)
2979 entry->cdte_addr.msf.minute = *msf_val;
2980 entry->cdte_addr.msf.second = *(msf_val+1);
2981 entry->cdte_addr.msf.frame = *(msf_val+2);
2984 return 0;
2985 break;
2987 case CDROMPLAYTRKIND: /* Play a track. This currently ignores index. */
2989 struct cdrom_ti *ti = (struct cdrom_ti *) arg;
2990 int track_idx;
2992 sony_get_toc();
2993 if (!sony_toc_read)
2995 return -EIO;
2998 if ( (ti->cdti_trk0 < sony_toc.first_track_num)
2999 || (ti->cdti_trk0 > sony_toc.last_track_num)
3000 || (ti->cdti_trk1 < ti->cdti_trk0))
3002 return -EINVAL;
3005 track_idx = find_track(ti->cdti_trk0);
3006 if (track_idx < 0)
3008 return -EINVAL;
3010 params[1] = int_to_bcd(sony_toc.tracks[track_idx].track_start_msf[0]);
3011 params[2] = int_to_bcd(sony_toc.tracks[track_idx].track_start_msf[1]);
3012 params[3] = int_to_bcd(sony_toc.tracks[track_idx].track_start_msf[2]);
3015 * If we want to stop after the last track, use the lead-out
3016 * MSF to do that.
3018 if (ti->cdti_trk1 >= sony_toc.last_track_num)
3020 track_idx = find_track(CDROM_LEADOUT);
3022 else
3024 track_idx = find_track(ti->cdti_trk1+1);
3026 if (track_idx < 0)
3028 return -EINVAL;
3030 params[4] = int_to_bcd(sony_toc.tracks[track_idx].track_start_msf[0]);
3031 params[5] = int_to_bcd(sony_toc.tracks[track_idx].track_start_msf[1]);
3032 params[6] = int_to_bcd(sony_toc.tracks[track_idx].track_start_msf[2]);
3033 params[0] = 0x03;
3035 do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg, &res_size);
3037 do_sony_cd_cmd(SONY_AUDIO_PLAYBACK_CMD, params, 7, res_reg, &res_size);
3039 if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20))
3041 printk("Params: %x %x %x %x %x %x %x\n", params[0], params[1],
3042 params[2], params[3], params[4], params[5], params[6]);
3043 printk("Sony CDROM error %s (CDROMPLAYTRKIND)\n", translate_error(res_reg[1]));
3044 return -EIO;
3047 /* Save the final position for pauses and resumes */
3048 final_pos_msf[0] = bcd_to_int(params[4]);
3049 final_pos_msf[1] = bcd_to_int(params[5]);
3050 final_pos_msf[2] = bcd_to_int(params[6]);
3051 sony_audio_status = CDROM_AUDIO_PLAY;
3052 return 0;
3055 case CDROMVOLCTRL: /* Volume control. What volume does this change, anyway? */
3057 struct cdrom_volctrl *volctrl = (struct cdrom_volctrl *) arg;
3059 params[0] = SONY_SD_AUDIO_VOLUME;
3060 params[1] = volctrl->channel0;
3061 params[2] = volctrl->channel1;
3062 return do_sony_cd_cmd_chk("VOLCTRL",SONY_SET_DRIVE_PARAM_CMD, params, 3, res_reg, &res_size);
3064 case CDROMSUBCHNL: /* Get subchannel info */
3065 return sony_get_subchnl_info((struct cdrom_subchnl *)arg);
3067 default:
3068 return -EINVAL;
3072 static int scd_dev_ioctl(struct cdrom_device_info *cdi,
3073 unsigned int cmd,
3074 unsigned long arg)
3076 int i;
3078 switch (cmd)
3080 case CDROMREADAUDIO: /* Read 2352 byte audio tracks and 2340 byte
3081 raw data tracks. */
3083 struct cdrom_read_audio ra;
3086 sony_get_toc();
3087 if (!sony_toc_read)
3089 return -EIO;
3092 if (ra.nframes == 0)
3094 return 0;
3097 i=verify_area(VERIFY_WRITE, ra.buf, CD_FRAMESIZE_RAW * ra.nframes);
3098 if(i<0)
3099 return i;
3100 copy_from_user(&ra, (char *) arg, sizeof(ra));
3102 if (ra.addr_format == CDROM_LBA)
3104 if ( (ra.addr.lba >= sony_toc.lead_out_start_lba)
3105 || (ra.addr.lba + ra.nframes >= sony_toc.lead_out_start_lba))
3107 return -EINVAL;
3110 else if (ra.addr_format == CDROM_MSF)
3112 if ( (ra.addr.msf.minute >= 75)
3113 || (ra.addr.msf.second >= 60)
3114 || (ra.addr.msf.frame >= 75))
3116 return -EINVAL;
3119 ra.addr.lba = ( (ra.addr.msf.minute * 4500)
3120 + (ra.addr.msf.second * 75)
3121 + ra.addr.msf.frame);
3122 if ( (ra.addr.lba >= sony_toc.lead_out_start_lba)
3123 || (ra.addr.lba + ra.nframes >= sony_toc.lead_out_start_lba))
3125 return -EINVAL;
3128 /* I know, this can go negative on an unsigned. However,
3129 the first thing done to the data is to add this value,
3130 so this should compensate and allow direct msf access. */
3131 ra.addr.lba -= LOG_START_OFFSET;
3133 else
3135 return -EINVAL;
3138 return(read_audio(&ra));
3140 return 0;
3141 break;
3143 default:
3144 return -EINVAL;
3148 static int scd_spinup(void)
3150 unsigned char res_reg[12];
3151 unsigned int res_size;
3152 int num_spin_ups;
3154 num_spin_ups = 0;
3156 respinup_on_open:
3157 do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg, &res_size);
3159 /* The drive sometimes returns error 0. I don't know why, but ignore
3160 it. It seems to mean the drive has already done the operation. */
3161 if ((res_size < 2) || ((res_reg[0] != 0) && (res_reg[1] != 0)))
3163 printk("Sony CDROM %s error (scd_open, spin up)\n", translate_error(res_reg[1]));
3164 return 1;
3167 do_sony_cd_cmd(SONY_READ_TOC_CMD, NULL, 0, res_reg, &res_size);
3169 /* The drive sometimes returns error 0. I don't know why, but ignore
3170 it. It seems to mean the drive has already done the operation. */
3171 if ((res_size < 2) || ((res_reg[0] != 0) && (res_reg[1] != 0)))
3173 /* If the drive is already playing, it's ok. */
3174 if ((res_reg[1] == SONY_AUDIO_PLAYING_ERR) || (res_reg[1] == 0))
3176 return 0;
3179 /* If the drive says it is not spun up (even though we just did it!)
3180 then retry the operation at least a few times. */
3181 if ( (res_reg[1] == SONY_NOT_SPIN_ERR)
3182 && (num_spin_ups < MAX_CDU31A_RETRIES))
3184 num_spin_ups++;
3185 goto respinup_on_open;
3188 printk("Sony CDROM error %s (scd_open, read toc)\n", translate_error(res_reg[1]));
3189 do_sony_cd_cmd(SONY_SPIN_DOWN_CMD, NULL, 0, res_reg, &res_size);
3190 return 1;
3192 return 0;
3196 * Open the drive for operations. Spin the drive up and read the table of
3197 * contents if these have not already been done.
3199 static int
3200 scd_open(struct cdrom_device_info *cdi, int openmode)
3202 unsigned char res_reg[12];
3203 unsigned int res_size;
3204 unsigned char params[2];
3206 MOD_INC_USE_COUNT;
3207 if (sony_usage == 0)
3209 if (scd_spinup() != 0) {
3210 MOD_DEC_USE_COUNT;
3211 return -EIO;
3213 sony_get_toc();
3214 if (!sony_toc_read)
3216 do_sony_cd_cmd(SONY_SPIN_DOWN_CMD, NULL, 0, res_reg, &res_size);
3217 MOD_DEC_USE_COUNT;
3218 return -EIO;
3221 /* For XA on the CDU31A only, we have to do special reads.
3222 The CDU33A handles XA automagically. */
3223 /* if ( (sony_toc.disk_type == SONY_XA_DISK_TYPE) */
3224 if ( (sony_toc.disk_type != 0x00)
3225 && (!is_double_speed))
3227 params[0] = SONY_SD_DECODE_PARAM;
3228 params[1] = 0x07;
3229 do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
3230 params,
3232 res_reg,
3233 &res_size);
3234 if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20))
3236 printk("CDU31A: Unable to set XA params: 0x%2.2x\n", res_reg[1]);
3238 sony_xa_mode = 1;
3240 /* A non-XA disk. Set the parms back if necessary. */
3241 else if (sony_xa_mode)
3243 params[0] = SONY_SD_DECODE_PARAM;
3244 params[1] = 0x0f;
3245 do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
3246 params,
3248 res_reg,
3249 &res_size);
3250 if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20))
3252 printk("CDU31A: Unable to reset XA params: 0x%2.2x\n", res_reg[1]);
3254 sony_xa_mode = 0;
3257 sony_spun_up = 1;
3260 sony_usage++;
3262 return 0;
3267 * Close the drive. Spin it down if no task is using it. The spin
3268 * down will fail if playing audio, so audio play is OK.
3270 static void
3271 scd_release(struct cdrom_device_info *cdi)
3273 if (sony_usage == 1)
3275 unsigned char res_reg[12];
3276 unsigned int res_size;
3278 do_sony_cd_cmd(SONY_SPIN_DOWN_CMD, NULL, 0, res_reg, &res_size);
3280 sony_spun_up = 0;
3282 sony_usage--;
3283 MOD_DEC_USE_COUNT;
3286 static struct cdrom_device_ops scd_dops = {
3287 scd_open, /* open */
3288 scd_release, /* release */
3289 scd_drive_status, /* drive status */
3290 scd_media_changed, /* media changed */
3291 scd_tray_move, /* tray move */
3292 scd_lock_door, /* lock door */
3293 scd_select_speed, /* select speed */
3294 NULL, /* select disc */
3295 scd_get_last_session, /* get last session */
3296 scd_get_mcn, /* get universal product code */
3297 scd_reset, /* hard reset */
3298 scd_audio_ioctl, /* audio ioctl */
3299 scd_dev_ioctl, /* device-specific ioctl */
3300 CDC_OPEN_TRAY | CDC_CLOSE_TRAY | CDC_LOCK | CDC_SELECT_SPEED | CDC_MULTI_SESSION |
3301 CDC_MULTI_SESSION | CDC_MCN | CDC_MEDIA_CHANGED | CDC_PLAY_AUDIO |
3302 CDC_RESET | CDC_IOCTLS | CDC_DRIVE_STATUS, /* capability */
3303 1, /* number of minor devices */
3306 static struct cdrom_device_info scd_info = {
3307 &scd_dops, /* device operations */
3308 NULL, /* link */
3309 NULL, /* handle */
3310 MKDEV(MAJOR_NR,0), /* dev */
3311 0, /* mask */
3312 2, /* maximum speed */
3313 1, /* number of discs */
3314 0, /* options, not owned */
3315 0, /* mc_flags, not owned */
3316 0 /* use count, not owned */
3319 /* The different types of disc loading mechanisms supported */
3320 static const char *load_mech[] __initdata = { "caddy", "tray", "pop-up", "unknown" };
3322 __initfunc(static void
3323 get_drive_configuration(unsigned short base_io,
3324 unsigned char res_reg[],
3325 unsigned int *res_size))
3327 int retry_count;
3330 /* Set the base address */
3331 cdu31a_port = base_io;
3333 /* Set up all the register locations */
3334 sony_cd_cmd_reg = cdu31a_port + SONY_CMD_REG_OFFSET;
3335 sony_cd_param_reg = cdu31a_port + SONY_PARAM_REG_OFFSET;
3336 sony_cd_write_reg = cdu31a_port + SONY_WRITE_REG_OFFSET;
3337 sony_cd_control_reg = cdu31a_port + SONY_CONTROL_REG_OFFSET;
3338 sony_cd_status_reg = cdu31a_port + SONY_STATUS_REG_OFFSET;
3339 sony_cd_result_reg = cdu31a_port + SONY_RESULT_REG_OFFSET;
3340 sony_cd_read_reg = cdu31a_port + SONY_READ_REG_OFFSET;
3341 sony_cd_fifost_reg = cdu31a_port + SONY_FIFOST_REG_OFFSET;
3344 * Check to see if anything exists at the status register location.
3345 * I don't know if this is a good way to check, but it seems to work
3346 * ok for me.
3348 if (read_status_register() != 0xff)
3351 * Reset the drive and wait for attention from it (to say it's reset).
3352 * If you don't wait, the next operation will probably fail.
3354 reset_drive();
3355 retry_count = jiffies + SONY_RESET_TIMEOUT;
3356 while ((retry_count > jiffies) && (!is_attention()))
3358 sony_sleep();
3361 #if 0
3362 /* If attention is never seen probably not a CDU31a present */
3363 if (!is_attention())
3365 res_reg[0] = 0x20;
3366 return;
3368 #endif
3371 * Get the drive configuration.
3373 do_sony_cd_cmd(SONY_REQ_DRIVE_CONFIG_CMD,
3374 NULL,
3376 (unsigned char *) res_reg,
3377 res_size);
3378 return;
3381 /* Return an error */
3382 res_reg[0] = 0x20;
3385 #ifndef MODULE
3387 * Set up base I/O and interrupts, called from main.c.
3389 __initfunc(void
3390 cdu31a_setup(char *strings,
3391 int *ints))
3393 if (ints[0] > 0)
3395 cdu31a_port = ints[1];
3397 if (ints[0] > 1)
3399 cdu31a_irq = ints[2];
3401 if ((strings != NULL) && (*strings != '\0'))
3403 if (strcmp(strings, "PAS") == 0)
3405 sony_pas_init = 1;
3407 else
3409 printk("CDU31A: Unknown interface type: %s\n", strings);
3413 #endif
3415 static int cdu31a_block_size;
3418 * Initialize the driver.
3420 __initfunc(int
3421 cdu31a_init(void))
3423 struct s_sony_drive_config drive_config;
3424 unsigned int res_size;
3425 char msg[255];
3426 char buf[40];
3427 int i;
3428 int drive_found;
3429 int tmp_irq;
3433 * According to Alex Freed (freed@europa.orion.adobe.com), this is
3434 * required for the Fusion CD-16 package. If the sound driver is
3435 * loaded, it should work fine, but just in case...
3437 * The following turn on the CD-ROM interface for a Fusion CD-16.
3439 if (sony_pas_init)
3441 outb(0xbc, 0x9a01);
3442 outb(0xe2, 0x9a01);
3445 drive_found = 0;
3447 /* Setting the base I/O address to 0xffff will disable it. */
3448 if (cdu31a_port == 0xffff)
3451 else if (cdu31a_port != 0)
3453 tmp_irq = cdu31a_irq; /* Need IRQ 0 because we can't sleep here. */
3454 cdu31a_irq = 0;
3456 get_drive_configuration(cdu31a_port,
3457 drive_config.exec_status,
3458 &res_size);
3459 if ((res_size > 2) && ((drive_config.exec_status[0] & 0xf0) == 0x00))
3461 drive_found = 1;
3464 cdu31a_irq = tmp_irq;
3466 else
3468 cdu31a_irq = 0;
3469 i = 0;
3470 while ( (cdu31a_addresses[i].base != 0)
3471 && (!drive_found))
3473 if (check_region(cdu31a_addresses[i].base, 4)) {
3474 i++;
3475 continue;
3477 get_drive_configuration(cdu31a_addresses[i].base,
3478 drive_config.exec_status,
3479 &res_size);
3480 if ((res_size > 2) && ((drive_config.exec_status[0] & 0xf0) == 0x00))
3482 drive_found = 1;
3483 cdu31a_irq = cdu31a_addresses[i].int_num;
3485 else
3487 i++;
3492 if (drive_found)
3494 int deficiency=0;
3496 request_region(cdu31a_port, 4,"cdu31a");
3498 if (register_blkdev(MAJOR_NR,"cdu31a",&cdrom_fops))
3500 printk("Unable to get major %d for CDU-31a\n", MAJOR_NR);
3501 goto errout2;
3504 if (SONY_HWC_DOUBLE_SPEED(drive_config))
3506 is_double_speed = 1;
3509 tmp_irq = cdu31a_irq; /* Need IRQ 0 because we can't sleep here. */
3510 cdu31a_irq = 0;
3512 set_drive_params(sony_speed);
3514 cdu31a_irq = tmp_irq;
3516 if (cdu31a_irq > 0)
3518 if (request_irq(cdu31a_irq, cdu31a_interrupt, SA_INTERRUPT, "cdu31a", NULL))
3520 printk("Unable to grab IRQ%d for the CDU31A driver\n", cdu31a_irq);
3521 cdu31a_irq = 0;
3525 sprintf(msg, "Sony I/F CDROM : %8.8s %16.16s %8.8s\n",
3526 drive_config.vendor_id,
3527 drive_config.product_id,
3528 drive_config.product_rev_level);
3529 sprintf(buf, " Capabilities: %s",
3530 load_mech[SONY_HWC_GET_LOAD_MECH(drive_config)]);
3531 strcat(msg, buf);
3532 if (SONY_HWC_AUDIO_PLAYBACK(drive_config))
3534 strcat(msg, ", audio");
3535 } else
3536 deficiency |= CDC_PLAY_AUDIO;
3537 if (SONY_HWC_EJECT(drive_config))
3539 strcat(msg, ", eject");
3540 } else
3541 deficiency |= CDC_OPEN_TRAY;
3542 if (SONY_HWC_LED_SUPPORT(drive_config))
3544 strcat(msg, ", LED");
3546 if (SONY_HWC_ELECTRIC_VOLUME(drive_config))
3548 strcat(msg, ", elec. Vol");
3550 if (SONY_HWC_ELECTRIC_VOLUME_CTL(drive_config))
3552 strcat(msg, ", sep. Vol");
3554 if (is_double_speed)
3556 strcat(msg, ", double speed");
3557 } else
3558 deficiency |= CDC_SELECT_SPEED;
3559 if (cdu31a_irq > 0)
3561 sprintf(buf, ", irq %d", cdu31a_irq);
3562 strcat(msg, buf);
3564 strcat(msg, "\n");
3566 is_a_cdu31a = strcmp("CD-ROM CDU31A", drive_config.product_id) == 0;
3568 blk_dev[MAJOR_NR].request_fn = DEVICE_REQUEST;
3569 read_ahead[MAJOR_NR] = CDU31A_READAHEAD;
3570 cdu31a_block_size = 1024; /* 1kB default block size */
3571 /* use 'mount -o block=2048' */
3572 blksize_size[MAJOR_NR] = &cdu31a_block_size;
3574 init_timer(&cdu31a_abort_timer);
3575 cdu31a_abort_timer.function = handle_abort_timeout;
3577 scd_info.mask = deficiency;
3578 strncpy(scd_info.name, "cdu31a", sizeof(scd_info.name));
3580 if (register_cdrom(&scd_info))
3582 goto errout0;
3587 disk_changed = 1;
3589 if (drive_found)
3591 return(0);
3593 else
3595 goto errout3;
3597 errout0:
3598 printk("Unable to register CDU-31a with Uniform cdrom driver\n");
3599 if (unregister_blkdev(MAJOR_NR, "cdu31a"))
3601 printk("Can't unregister block device for cdu31a\n");
3603 errout2:
3604 release_region(cdu31a_port,4);
3605 errout3:
3606 return -EIO;
3609 #ifdef MODULE
3612 init_module(void)
3614 return cdu31a_init();
3617 void
3618 cleanup_module(void)
3620 if (unregister_cdrom(&scd_info))
3622 printk("Can't unregister cdu31a from Uniform cdrom driver\n");
3623 return;
3625 if ((unregister_blkdev(MAJOR_NR, "cdu31a") == -EINVAL))
3627 printk("Can't unregister cdu31a\n");
3628 return;
3631 if (cdu31a_irq > 0)
3632 free_irq(cdu31a_irq, NULL);
3634 release_region(cdu31a_port,4);
3635 printk(KERN_INFO "cdu31a module released.\n");
3637 #endif MODULE