2 * Sony CDU-31A CDROM interface device driver.
4 * Corey Minyard (minyard@wf-rch.cirr.com)
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
32 * retry_count = jiffies+ SONY_JIFFIES_TIMEOUT;
33 * while ((retry_count > jiffies) && (! <some condition to wait for))
35 * while (handle_sony_cd_attention())
40 * if (the condition not met)
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.
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.
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!
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.
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.
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 +-----------------+ |
142 * +------------------------------------------+
144 * JP1 sets the Base Address, using the following settings:
146 * Address Pin 1 Pin 2
147 * ------- ----- -----
153 * JP2 and JP3 configure the DMA channel; they must be set the same.
155 * DMA Pin 1 Pin 2 Pin 3
156 * --- ----- ----- -----
161 * JP4 Configures the IRQ:
163 * IRQ Pin 1 Pin 2 Pin 3 Pin 4
164 * --- ----- ----- ----- -----
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
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>
210 #include <asm/uaccess.h>
213 #include <linux/cdrom.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
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
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
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 */
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
,
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
304 static int sony_pas_init
= 0; /* Initialize the Pro-Audio
307 static struct s_sony_session_toc single_toc
; /* Holds the
311 static struct s_all_sessions_toc sony_toc
; /* entries gathered from all
314 static int sony_toc_read
= 0; /* Has the TOC been read for
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
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
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
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
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.
388 scd_disk_change(kdev_t full_dev
)
392 retval
= disk_changed
;
399 * Uniform cdrom interface function
400 * report back, if disc has changed from time of last request.
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 */
419 /*return sony_spun_up ? CDS_DISC_OK : CDS_DRIVE_NOT_READY;*/
420 return sony_spun_up
? CDS_DISC_OK
: CDS_TRAY_OPEN
;
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
);
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.
452 current
->state
= TASK_INTERRUPTIBLE
;
453 current
->timeout
= jiffies
;
456 else /* Interrupt driven */
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.
474 return((inb(sony_cd_status_reg
) & SONY_ATTN_BIT
) != 0);
480 return((inb(sony_cd_status_reg
) & SONY_BUSY_BIT
) != 0);
486 return((inb(sony_cd_status_reg
) & SONY_DATA_RDY_BIT
) != 0);
490 is_data_requested(void)
492 return((inb(sony_cd_status_reg
) & SONY_DATA_REQUEST_BIT
) != 0);
496 is_result_ready(void)
498 return((inb(sony_cd_status_reg
) & SONY_RES_RDY_BIT
) != 0);
502 is_param_write_rdy(void)
504 return((inb(sony_cd_fifost_reg
) & SONY_PARAM_WRITE_RDY_BIT
) != 0);
508 is_result_reg_not_empty(void)
510 return((inb(sony_cd_fifost_reg
) & SONY_RES_REG_NOT_EMP_BIT
) != 0);
516 curr_control_reg
= 0;
517 readahead_dataleft
= 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
)
532 retry_count
= jiffies
+ SONY_RESET_TIMEOUT
;
533 while ((retry_count
> jiffies
) && (!is_attention()))
542 clear_attention(void)
544 outb(curr_control_reg
| SONY_ATTN_CLR_BIT
, sony_cd_control_reg
);
548 clear_result_ready(void)
550 outb(curr_control_reg
| SONY_RES_RDY_CLR_BIT
, sony_cd_control_reg
);
554 clear_data_ready(void)
556 outb(curr_control_reg
| SONY_DATA_RDY_CLR_BIT
, sony_cd_control_reg
);
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
));
584 write_param(unsigned char param
)
586 outb(param
, sony_cd_param_reg
);
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
);
597 cdu31a_interrupt(int irq
, void *dev_id
, struct pt_regs
*regs
)
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
606 /* Clear out the result registers. */
607 while (is_result_reg_not_empty())
609 val
= read_result_register();
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
);
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];
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
);
695 * Set the drive parameters so the drive will auto-spin-up when a
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
,
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
728 do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD
,
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
)
748 sony_speed
= speed
- 1;
750 set_drive_params(sony_speed
);
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 */
767 set_drive_params(sony_speed
);
772 * This code will reset the drive and attempt to restore sane parameters.
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");
784 retry_count
= jiffies
+ SONY_RESET_TIMEOUT
;
785 while ((retry_count
> jiffies
) && (!is_attention()))
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
;
804 * This routine writes data to the parameter register. Since this should
805 * happen fairly fast, it is polled with no OS waits between.
808 write_params(unsigned char *params
,
811 unsigned int retry_count
;
814 retry_count
= SONY_READY_RETRIES
;
815 while ((retry_count
> 0) && (!is_param_write_rdy()))
819 if (!is_param_write_rdy())
824 while (num_params
> 0)
826 write_param(*params
);
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
842 get_result(unsigned char *result_buffer
,
843 unsigned int *result_size
)
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()))))
858 while (handle_sony_cd_attention())
861 if (is_busy() || (!(is_result_ready())))
864 printk("CDU31A timeout out %d\n", __LINE__
);
866 result_buffer
[0] = 0x20;
867 result_buffer
[1] = SONY_TIMEOUT_OP_ERR
;
873 * Get the first two bytes. This determines what else needs
876 clear_result_ready();
877 a
= read_result_register();
881 /* Check for block error status result. */
882 if ((a
& 0xf0) == 0x50)
888 b
= read_result_register();
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)
907 *result_buffer
= read_result_register();
915 retry_count
= SONY_READY_RETRIES
;
916 while ((retry_count
> 0) && (!is_result_ready()))
920 if (!is_result_ready())
923 printk("CDU31A timeout out %d\n", __LINE__
);
925 result_buffer
[0] = 0x20;
926 result_buffer
[1] = SONY_TIMEOUT_OP_ERR
;
931 clear_result_ready();
935 *result_buffer
= read_result_register();
944 retry_count
= SONY_READY_RETRIES
;
945 while ((retry_count
> 0) && (!is_result_ready()))
949 if (!is_result_ready())
952 printk("CDU31A timeout out %d\n", __LINE__
);
954 result_buffer
[0] = 0x20;
955 result_buffer
[1] = SONY_TIMEOUT_OP_ERR
;
964 *result_buffer
= read_result_register();
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.
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
;
992 if (current
!= has_cd_task
) /* Allow recursive calls to this routine */
996 interruptible_sleep_on(&sony_wait
);
997 if (signal_pending(current
))
999 result_buffer
[0] = 0x20;
1000 result_buffer
[1] = SONY_SIGNAL_OP_ERR
;
1002 restore_flags(flags
);
1007 has_cd_task
= current
;
1018 while (handle_sony_cd_attention())
1023 retry_count
= jiffies
+ SONY_JIFFIES_TIMEOUT
;
1024 while ((retry_count
> jiffies
) && (is_busy()))
1028 while (handle_sony_cd_attention())
1034 printk("CDU31A timeout out %d\n", __LINE__
);
1036 result_buffer
[0] = 0x20;
1037 result_buffer
[1] = SONY_TIMEOUT_OP_ERR
;
1042 clear_result_ready();
1045 write_params(params
, num_params
);
1048 get_result(result_buffer
, result_size
);
1051 if ( ((result_buffer
[0] & 0xf0) == 0x20)
1052 && (num_retries
< MAX_CDU31A_RETRIES
))
1055 current
->state
= TASK_INTERRUPTIBLE
;
1056 current
->timeout
= jiffies
+ HZ
/10; /* Wait .1 seconds on retries */
1058 goto retry_cd_operation
;
1061 if (!recursive_call
)
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
1082 handle_sony_cd_attention(void)
1084 unsigned char atten_code
;
1085 static int num_consecutive_attentions
= 0;
1090 printk("Entering handle_sony_cd_attention\n");
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;
1100 printk("Leaving handle_sony_cd_attention at %d\n", __LINE__
);
1106 atten_code
= read_result_register();
1110 /* Someone changed the CD. Mark it as changed */
1111 case SONY_MECH_LOADED_ATTN
:
1114 sony_audio_status
= CDROM_AUDIO_NO_STATUS
;
1115 sony_blocks_left
= 0;
1118 case SONY_SPIN_DOWN_COMPLETE_ATTN
:
1119 /* Mark the disk as spun down. */
1123 case SONY_AUDIO_PLAY_DONE_ATTN
:
1124 sony_audio_status
= CDROM_AUDIO_COMPLETED
;
1128 case SONY_EJECT_PUSHED_ATTN
:
1131 sony_audio_status
= CDROM_AUDIO_INVALID
;
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
;
1143 num_consecutive_attentions
++;
1145 printk("Leaving handle_sony_cd_attention at %d\n", __LINE__
);
1149 else if (abort_read_started
)
1151 while (is_result_reg_not_empty())
1153 val
= read_result_register();
1156 clear_result_ready();
1157 /* Clear out the data */
1158 while (is_data_requested())
1160 val
= read_data_register();
1162 abort_read_started
= 0;
1164 printk("Leaving handle_sony_cd_attention at %d\n", __LINE__
);
1169 num_consecutive_attentions
= 0;
1171 printk("Leaving handle_sony_cd_attention at %d\n", __LINE__
);
1177 /* Convert from an integer 0-99 to BCD */
1178 static inline unsigned int
1179 int_to_bcd(unsigned int val
)
1184 retval
= (val
/ 10) << 4;
1185 retval
= retval
| val
% 10;
1190 /* Convert from BCD to an integer from 0-99 */
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.
1203 log_to_msf(unsigned int log
, unsigned char *msf
)
1205 log
= log
+ LOG_START_OFFSET
;
1206 msf
[0] = int_to_bcd(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.
1217 msf_to_log(unsigned char *msf
)
1224 log
+= msf
[0] * 4500;
1225 log
= log
- LOG_START_OFFSET
;
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.
1236 size_to_buf(unsigned int size
,
1239 buf
[0] = size
/ 65536;
1240 size
= size
% 65536;
1241 buf
[1] = size
/ 256;
1242 buf
[2] = size
% 256;
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
)
1260 /* look into the TOC */
1261 if (sony_toc
.disk_type
== 0x10) /* this is a CD-I disc */
1263 if (sony_toc
.disk_type
== 0x20) /* this is a XA disc */
1265 for (i
= 0; i
< sony_toc
.track_entries
; i
++)
1266 if (sony_toc
.tracks
[i
].control
& CDROM_DATA_TRACK
)
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
1281 start_request(unsigned int sector
,
1283 int read_nsect_only
)
1285 unsigned char params
[6];
1286 unsigned int read_size
;
1287 unsigned int retry_count
;
1291 printk("Entering start_request\n");
1293 log_to_msf(sector
, params
);
1294 /* If requested, read exactly what was asked. */
1295 if (read_nsect_only
)
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. */
1310 read_size
= CDU31A_READAHEAD
/ 4;
1312 size_to_buf(read_size
, ¶ms
[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()))
1326 while (handle_sony_cd_attention())
1332 printk("CDU31A: Timeout while waiting to issue command\n");
1334 printk("Leaving start_request at %d\n", __LINE__
);
1340 /* Issue the command */
1341 clear_result_ready();
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;
1352 printk("Leaving start_request at %d\n", __LINE__
);
1357 printk("Leaving start_request at %d\n", __LINE__
);
1361 /* Abort a pending read operation. Clear all the drive status and
1362 readahead variables. */
1366 unsigned char result_reg
[2];
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",
1379 while (is_result_reg_not_empty())
1381 val
= read_result_register();
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;
1396 /* Called when the timer times out. This will abort the
1397 pending read operation. */
1399 handle_abort_timeout(unsigned long data
)
1401 unsigned long flags
;
1404 printk("Entering handle_abort_timeout\n");
1408 /* If it is in use, ignore it. */
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
1415 clear_result_ready();
1417 write_cmd(SONY_ABORT_CMD
);
1419 sony_blocks_left
= 0;
1420 readahead_dataleft
= 0;
1422 abort_read_started
= 1;
1424 restore_flags(flags
);
1426 printk("Leaving handle_abort_timeout\n");
1430 /* Actually get data and status from the drive. */
1432 input_data(char *buffer
,
1433 unsigned int bytesleft
,
1434 unsigned int nblocks
,
1435 unsigned int offset
,
1439 volatile unsigned char val
;
1443 printk("Entering input_data\n");
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. */
1449 for(i
=0; i
<CD_XA_HEAD
; i
++)
1451 val
= read_data_register();
1457 if (bytesleft
== 2048) /* 2048 byte direct buffer transfer */
1459 insb(sony_cd_read_reg
, buffer
, 2048);
1460 readahead_dataleft
= 0;
1464 /* If the input read did not align with the beginning of the block,
1465 skip the necessary bytes. */
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
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. */
1488 for(i
=0; i
<CD_XA_TAIL
; i
++)
1490 val
= read_data_register();
1494 printk("Leaving input_data at %d\n", __LINE__
);
1498 /* read data from the drive. Note the nsect must be <= 4. */
1500 read_data_block(char *buffer
,
1502 unsigned int nblocks
,
1503 unsigned char res_reg
[],
1506 unsigned int retry_count
;
1507 unsigned int bytesleft
;
1508 unsigned int offset
;
1513 printk("Entering read_data_block\n");
1519 bytesleft
= nblocks
* 512;
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;
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. */
1544 readahead_buffer
+ (2048 - readahead_dataleft
),
1545 readahead_dataleft
);
1546 readahead_dataleft
= 0;
1547 bytesleft
-= readahead_dataleft
;
1548 offset
+= readahead_dataleft
;
1552 /* The readahead will fill the whole buffer, get the data
1555 readahead_buffer
+ (2048 - readahead_dataleft
),
1557 readahead_dataleft
-= bytesleft
;
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. */
1567 res_reg
[1] = SONY_BAD_DATA_ERR
;
1571 if (readahead_dataleft
== 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
);
1582 printk("Leaving read_data_block at %d\n", __LINE__
);
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())
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",
1607 res_reg
[1] = SONY_BAD_DATA_ERR
;
1615 printk("CDU31A timeout out %d\n", __LINE__
);
1618 res_reg
[1] = SONY_TIMEOUT_OP_ERR
;
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())
1637 if (!is_result_ready())
1640 printk("CDU31A timeout out %d\n", __LINE__
);
1643 res_reg
[1] = SONY_TIMEOUT_OP_ERR
;
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
1666 res_reg
[1] = SONY_BAD_DATA_ERR
;
1672 printk("CDU31A: Data block error: 0x%x\n", res_reg
[0]);
1674 res_reg
[1] = SONY_BAD_DATA_ERR
;
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)
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]);
1698 res_reg
[1] = SONY_BAD_DATA_ERR
;
1704 printk("Leaving read_data_block at %d\n", __LINE__
);
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.
1718 do_cdu31a_request(void)
1722 unsigned char res_reg
[12];
1723 unsigned int res_size
;
1725 unsigned long flags
;
1729 printk("Entering do_cdu31a_request\n");
1733 * Make sure no one else is using the driver; wait for them
1734 * to finish if it is so.
1740 interruptible_sleep_on(&sony_wait
);
1741 if (signal_pending(current
))
1743 restore_flags(flags
);
1744 if (CURRENT
&& CURRENT
->rq_status
!= RQ_INACTIVE
)
1748 restore_flags(flags
);
1750 printk("Leaving do_cdu31a_request at %d\n", __LINE__
);
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. */
1767 /* Make sure the timer is cancelled. */
1768 del_timer(&cdu31a_abort_timer
);
1772 cdu31a_request_startover
:
1774 * The beginning here is stolen from the hard disk driver. I hope
1777 if (!(CURRENT
) || CURRENT
->rq_status
== RQ_INACTIVE
)
1779 goto end_do_cdu31a_request
;
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");
1796 if (!buffer_locked(CURRENT
->bh
))
1798 panic(DEVICE_NAME
": block not locked");
1802 block
= CURRENT
->sector
;
1803 nblock
= CURRENT
->nr_sectors
;
1807 printk("CDU31A: TOC not read\n");
1809 goto cdu31a_request_startover
;
1812 switch(CURRENT
->cmd
)
1816 * If the block address is invalid or the request goes beyond the end of
1817 * the media, return an error.
1820 if ((block
/ 4) < sony_toc
.start_track_lba
)
1822 printk("CDU31A: Request before beginning of media\n");
1824 goto cdu31a_request_startover
;
1827 if ((block
/ 4) >= sony_toc
.lead_out_start_lba
)
1829 printk("CDU31A: Request past end of media\n");
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");
1837 goto cdu31a_request_startover
;
1843 while (handle_sony_cd_attention())
1848 printk("CDU31A: TOC not read\n");
1850 goto cdu31a_request_startover
;
1853 /* If no data is left to be read from the drive, start the
1855 if (sony_blocks_left
== 0)
1857 if (start_request(block
/ 4, CDU31A_READAHEAD
/ 4, 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
1866 else if (block
!= sony_next_block
)
1869 printk("CDU31A Warning: Read for block %d, expected %d\n",
1876 printk("CDU31A: TOC not read\n");
1878 goto cdu31a_request_startover
;
1880 if (start_request(block
/ 4, CDU31A_READAHEAD
/ 4, 0))
1882 printk("CDU31a: start request failed\n");
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
)
1894 goto cdu31a_request_startover
;
1898 if (res_reg
[1] == SONY_NOT_SPIN_ERR
)
1900 do_sony_cd_cmd(SONY_SPIN_UP_CMD
, NULL
, 0, res_reg
, &res_size
);
1904 printk("CDU31A: %s error for block %d, nblock %d\n", translate_error(res_reg
[1]), block
, nblock
);
1906 goto try_read_again
;
1919 panic("CDU31A: Unknown cmd");
1923 end_do_cdu31a_request
:
1926 /* After finished, cancel any pending operations. */
1929 /* Start a timer to time out after a while to disable
1931 cdu31a_abort_timer
.expires
= jiffies
+ 2*HZ
; /* Wait 2 seconds */
1932 add_timer(&cdu31a_abort_timer
);
1937 wake_up_interruptible(&sony_wait
);
1938 restore_flags(flags
);
1940 printk("Leaving do_cdu31a_request at %d\n", __LINE__
);
1946 * Read the table of contents from the drive and set up TOC if
1952 unsigned char res_reg
[2];
1953 unsigned int res_size
;
1954 unsigned char parms
[1];
1957 int totaltracks
= 0;
1962 printk("Entering sony_get_toc\n");
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
))
1990 goto respinup_on_gettoc
;
1993 printk("cdu31a: Error reading TOC: %x %s\n",
1994 res_reg
[0], translate_error(res_reg
[1]));
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.
2006 memset(&sony_toc
, 0x0e, sizeof(sony_toc
));
2007 memset(&single_toc
, 0x0f, sizeof(single_toc
));
2013 printk("Trying session %d\n", session
);
2016 do_sony_cd_cmd(SONY_READ_TOC_SPEC_CMD
,
2023 printk("%2.2x %2.2x\n", res_reg
[0], res_reg
[1]);
2026 if ((res_size
< 2) || ((res_reg
[0] & 0xf0) == 0x20))
2028 /* An error reading the TOC, this must be past the last session. */
2032 printk("Reading session %d\n", session
);
2036 do_sony_cd_cmd(SONY_REQ_TOC_DATA_SPEC_CMD
,
2039 (unsigned char *) &single_toc
,
2041 if ((res_size
< 2) || ((single_toc
.exec_status
[0] & 0xf0) == 0x20))
2043 printk("cdu31a: Error reading session %d: %x %s\n",
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
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]);
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,
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,
2156 if (single_toc
.pointb2
!= 0xb2)
2158 memmove(((char *) &single_toc
) + 45,
2159 ((char *) &single_toc
) + 36,
2163 if (single_toc
.pointb3
!= 0xb3)
2165 memmove(((char *) &single_toc
) + 54,
2166 ((char *) &single_toc
) + 45,
2170 if (single_toc
.pointb4
!= 0xb4)
2172 memmove(((char *) &single_toc
) + 63,
2173 ((char *) &single_toc
) + 54,
2177 if (single_toc
.pointc0
!= 0xc0)
2179 memmove(((char *) &single_toc
) + 72,
2180 ((char *) &single_toc
) + 63,
2186 printk("start track lba %u, leadout start lba %u\n",
2187 single_toc
.start_track_lba
, single_toc
.lead_out_start_lba
);
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
);
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;
2220 /* gather track entries from this session */
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]);
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
2249 sony_toc
.disk_type
= single_toc
.disk_type
;
2250 sony_toc
.sessions
= session
;
2252 /* don't believe everything :-) */
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
)
2263 /* Let's not get carried away... */
2266 printk("cdu31a: too many sessions: %d\n", 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];
2286 printk("Disk session %d, start track: %d, stop track: %d\n",
2288 single_toc
.start_track_lba
,
2289 single_toc
.lead_out_start_lba
);
2293 printk("Leaving sony_get_toc\n");
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
)
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 */;
2320 * Search for a specific track in the table of contents.
2323 find_track(int track
)
2327 for (i
= 0; i
<= sony_toc
.track_entries
; i
++)
2329 if (sony_toc
.tracks
[i
].track
== track
)
2340 * Read the subcode and put it in last_sony_subcode for future use.
2345 unsigned int res_size
;
2348 do_sony_cd_cmd(SONY_REQ_SUBCODE_ADDRESS_CMD
,
2351 (unsigned char *) &last_sony_subcode
,
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]));
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]);
2373 * Uniform cdrom interface function
2374 * return the media catalog number found on some older audio cds
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
,
2390 if ((res_size
< 2) || ((resbuffer
[0] & 0xf0) == 0x20))
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';
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.
2421 sony_get_subchnl_info(struct cdrom_subchnl
*schi
)
2423 /* Get attention stuff */
2424 while (handle_sony_cd_attention())
2433 switch (sony_audio_status
)
2435 case CDROM_AUDIO_NO_STATUS
:
2436 case CDROM_AUDIO_PLAY
:
2437 if (read_subcode() < 0)
2443 case CDROM_AUDIO_PAUSED
:
2444 case CDROM_AUDIO_COMPLETED
:
2448 case CDROM_AUDIO_NO_STATUS
:
2449 schi
->cdsc_audiostatus
= sony_audio_status
;
2453 case CDROM_AUDIO_INVALID
:
2454 case CDROM_AUDIO_ERROR
:
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
);
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. */
2488 read_audio_data(char *buffer
,
2489 unsigned char res_reg
[],
2492 unsigned int retry_count
;
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())
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)
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",
2531 res_reg
[1] = SONY_BAD_DATA_ERR
;
2539 printk("CDU31A timeout out %d\n", __LINE__
);
2542 res_reg
[1] = SONY_TIMEOUT_OP_ERR
;
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
);
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. */
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())
2575 if (!is_result_ready())
2578 printk("CDU31A timeout out %d\n", __LINE__
);
2581 res_reg
[1] = SONY_TIMEOUT_OP_ERR
;
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. */
2603 printk("CDU31A: Data block error: 0x%x\n", res_reg
[0]);
2605 res_reg
[1] = SONY_BAD_DATA_ERR
;
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]);
2616 res_reg
[1] = SONY_BAD_DATA_ERR
;
2622 /* Perform a raw data read. This will automatically detect the
2623 track type and read the proper data (audio or data). */
2625 read_audio(struct cdrom_read_audio
*ra
)
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.
2642 interruptible_sleep_on(&sony_wait
);
2643 if (signal_pending(current
))
2645 restore_flags(flags
);
2650 has_cd_task
= current
;
2651 restore_flags(flags
);
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
,
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]);
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
2677 /* start_request clears out any readahead data, so it should be safe. */
2678 if (start_request(ra
->addr
.lba
, ra
->nframes
, 1))
2681 goto exit_read_audio
;
2684 /* For every requested frame. */
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
,
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]);
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))
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
);
2736 printk("CDU31A: Error reading audio data on sector %d: %s\n",
2737 ra
->addr
.lba
+ cframe
,
2738 translate_error(res_reg
[1]));
2740 goto exit_read_audio
;
2745 copy_to_user((char *) (ra
->buf
+ (CD_FRAMESIZE_RAW
* cframe
)),
2746 (char *) readahead_buffer
,
2752 printk("CDU31A: Error reading audio data on sector %d: %s\n",
2753 ra
->addr
.lba
+ cframe
,
2754 translate_error(res_reg
[1]));
2756 goto exit_read_audio
;
2761 copy_to_user((char *) (ra
->buf
+ (CD_FRAMESIZE_RAW
* cframe
)),
2762 (char *) readahead_buffer
,
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]));
2775 goto exit_read_audio
;
2780 /* Set the drive mode back to the proper one for the disk. */
2781 params
[0] = SONY_SD_DECODE_PARAM
;
2790 do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD
,
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]);
2803 wake_up_interruptible(&sony_wait
);
2809 do_sony_cd_cmd_chk(const char *name
,
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
);
2826 * Uniform cdrom interface function
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
);
2842 if (0 == scd_spinup())
2849 * The big ugly ioctl handler.
2851 static int scd_audio_ioctl(struct cdrom_device_info
*cdi
,
2855 unsigned char res_reg
[12];
2856 unsigned int res_size
;
2857 unsigned char params
[7];
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
);
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
))
2880 /* Get the current position and save it for resuming */
2881 if (read_subcode() < 0)
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
;
2892 case CDROMRESUME
: /* Start the drive after being paused */
2893 if (sony_audio_status
!= CDROM_AUDIO_PAUSED
)
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]);
2908 if(do_sony_cd_cmd_chk("RESUME",SONY_AUDIO_PLAYBACK_CMD
, params
, 7, res_reg
, &res_size
)<0)
2910 sony_audio_status
= CDROM_AUDIO_PLAY
;
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 */
2919 params
[i
] = int_to_bcd(((unsigned char *)arg
)[i
-1]);
2922 if(do_sony_cd_cmd_chk("PLAYMSF",SONY_AUDIO_PLAYBACK_CMD
, params
, 7, res_reg
, &res_size
)<0)
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
;
2932 case CDROMREADTOCHDR
: /* Read the table of contents header */
2934 struct cdrom_tochdr
*hdr
;
2942 hdr
= (struct cdrom_tochdr
*) arg
;
2943 hdr
->cdth_trk0
= sony_toc
.first_track_num
;
2944 hdr
->cdth_trk1
= sony_toc
.last_track_num
;
2948 case CDROMREADTOCENTRY
: /* Read a given table of contents entry */
2950 struct cdrom_tocentry
*entry
;
2952 unsigned char *msf_val
= NULL
;
2960 entry
= (struct cdrom_tocentry
*) arg
;
2962 track_idx
= find_track(entry
->cdte_track
);
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);
2987 case CDROMPLAYTRKIND
: /* Play a track. This currently ignores index. */
2989 struct cdrom_ti
*ti
= (struct cdrom_ti
*) arg
;
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
))
3005 track_idx
= find_track(ti
->cdti_trk0
);
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
3018 if (ti
->cdti_trk1
>= sony_toc
.last_track_num
)
3020 track_idx
= find_track(CDROM_LEADOUT
);
3024 track_idx
= find_track(ti
->cdti_trk1
+1);
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]);
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]));
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
;
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
);
3072 static int scd_dev_ioctl(struct cdrom_device_info
*cdi
,
3080 case CDROMREADAUDIO
: /* Read 2352 byte audio tracks and 2340 byte
3083 struct cdrom_read_audio ra
;
3092 if (ra
.nframes
== 0)
3097 i
=verify_area(VERIFY_WRITE
, ra
.buf
, CD_FRAMESIZE_RAW
* ra
.nframes
);
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
))
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))
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
))
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
;
3138 return(read_audio(&ra
));
3148 static int scd_spinup(void)
3150 unsigned char res_reg
[12];
3151 unsigned int res_size
;
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]));
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))
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
))
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
);
3196 * Open the drive for operations. Spin the drive up and read the table of
3197 * contents if these have not already been done.
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];
3207 if (sony_usage
== 0)
3209 if (scd_spinup() != 0) {
3216 do_sony_cd_cmd(SONY_SPIN_DOWN_CMD
, NULL
, 0, res_reg
, &res_size
);
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
;
3229 do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD
,
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]);
3240 /* A non-XA disk. Set the parms back if necessary. */
3241 else if (sony_xa_mode
)
3243 params
[0] = SONY_SD_DECODE_PARAM
;
3245 do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD
,
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]);
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.
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
);
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 */
3310 MKDEV(MAJOR_NR
,0), /* dev */
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
))
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
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.
3355 retry_count
= jiffies
+ SONY_RESET_TIMEOUT
;
3356 while ((retry_count
> jiffies
) && (!is_attention()))
3362 /* If attention is never seen probably not a CDU31a present */
3363 if (!is_attention())
3371 * Get the drive configuration.
3373 do_sony_cd_cmd(SONY_REQ_DRIVE_CONFIG_CMD
,
3376 (unsigned char *) res_reg
,
3381 /* Return an error */
3387 * Set up base I/O and interrupts, called from main.c.
3390 cdu31a_setup(char *strings
,
3395 cdu31a_port
= ints
[1];
3399 cdu31a_irq
= ints
[2];
3401 if ((strings
!= NULL
) && (*strings
!= '\0'))
3403 if (strcmp(strings
, "PAS") == 0)
3409 printk("CDU31A: Unknown interface type: %s\n", strings
);
3415 static int cdu31a_block_size
;
3418 * Initialize the driver.
3423 struct s_sony_drive_config drive_config
;
3424 unsigned int res_size
;
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.
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. */
3456 get_drive_configuration(cdu31a_port
,
3457 drive_config
.exec_status
,
3459 if ((res_size
> 2) && ((drive_config
.exec_status
[0] & 0xf0) == 0x00))
3464 cdu31a_irq
= tmp_irq
;
3470 while ( (cdu31a_addresses
[i
].base
!= 0)
3473 if (check_region(cdu31a_addresses
[i
].base
, 4)) {
3477 get_drive_configuration(cdu31a_addresses
[i
].base
,
3478 drive_config
.exec_status
,
3480 if ((res_size
> 2) && ((drive_config
.exec_status
[0] & 0xf0) == 0x00))
3483 cdu31a_irq
= cdu31a_addresses
[i
].int_num
;
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
);
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. */
3512 set_drive_params(sony_speed
);
3514 cdu31a_irq
= tmp_irq
;
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
);
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
)]);
3532 if (SONY_HWC_AUDIO_PLAYBACK(drive_config
))
3534 strcat(msg
, ", audio");
3536 deficiency
|= CDC_PLAY_AUDIO
;
3537 if (SONY_HWC_EJECT(drive_config
))
3539 strcat(msg
, ", eject");
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");
3558 deficiency
|= CDC_SELECT_SPEED
;
3561 sprintf(buf
, ", irq %d", cdu31a_irq
);
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
))
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");
3604 release_region(cdu31a_port
,4);
3614 return cdu31a_init();
3618 cleanup_module(void)
3620 if (unregister_cdrom(&scd_info
))
3622 printk("Can't unregister cdu31a from Uniform cdrom driver\n");
3625 if ((unregister_blkdev(MAJOR_NR
, "cdu31a") == -EINVAL
))
3627 printk("Can't unregister cdu31a\n");
3632 free_irq(cdu31a_irq
, NULL
);
3634 release_region(cdu31a_port
,4);
3635 printk(KERN_INFO
"cdu31a module released.\n");