aboutsummaryrefslogtreecommitdiff
path: root/drivers/cdrom/cdu31a.c
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 15:20:36 -0700
committerLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 15:20:36 -0700
commit1da177e4c3f41524e886b7f1b8a0c1fc7321cac2 (patch)
tree0bba044c4ce775e45a88a51686b5d9f90697ea9d /drivers/cdrom/cdu31a.c
Linux-2.6.12-rc2v2.6.12-rc2
Initial git repository build. I'm not bothering with the full history, even though we have it. We can create a separate "historical" git archive of that later if we want to, and in the meantime it's about 3.2GB when imported into git - space that would just make the early git days unnecessarily complicated, when we don't have a lot of good infrastructure for it. Let it rip!
Diffstat (limited to 'drivers/cdrom/cdu31a.c')
-rw-r--r--drivers/cdrom/cdu31a.c3248
1 files changed, 3248 insertions, 0 deletions
diff --git a/drivers/cdrom/cdu31a.c b/drivers/cdrom/cdu31a.c
new file mode 100644
index 00000000000..647a71b12a2
--- /dev/null
+++ b/drivers/cdrom/cdu31a.c
@@ -0,0 +1,3248 @@
+/*
+* Sony CDU-31A CDROM interface device driver.
+*
+* Corey Minyard (minyard@wf-rch.cirr.com)
+*
+* Colossians 3:17
+*
+* See Documentation/cdrom/cdu31a for additional details about this driver.
+*
+* The Sony interface device driver handles Sony interface CDROM
+* drives and provides a complete block-level interface as well as an
+* ioctl() interface compatible with the Sun (as specified in
+* include/linux/cdrom.h). With this interface, CDROMs can be
+* accessed and standard audio CDs can be played back normally.
+*
+* WARNING - All autoprobes have been removed from the driver.
+* You MUST configure the CDU31A via a LILO config
+* at boot time or in lilo.conf. I have the
+* following in my lilo.conf:
+*
+* append="cdu31a=0x1f88,0,PAS"
+*
+* The first number is the I/O base address of the
+* card. The second is the interrupt (0 means none).
+ * The third should be "PAS" if on a Pro-Audio
+ * spectrum, or nothing if on something else.
+ *
+ * This interface is (unfortunately) a polled interface. This is
+ * because most Sony interfaces are set up with DMA and interrupts
+ * disables. Some (like mine) do not even have the capability to
+ * handle interrupts or DMA. For this reason you will see a lot of
+ * the following:
+ *
+ * retry_count = jiffies+ SONY_JIFFIES_TIMEOUT;
+ * while (time_before(jiffies, retry_count) && (! <some condition to wait for))
+ * {
+ * while (handle_sony_cd_attention())
+ * ;
+ *
+ * sony_sleep();
+ * }
+ * if (the condition not met)
+ * {
+ * return an error;
+ * }
+ *
+ * This ugly hack waits for something to happen, sleeping a little
+ * between every try. it also handles attentions, which are
+ * asynchronous events from the drive informing the driver that a disk
+ * has been inserted, removed, etc.
+ *
+ * NEWS FLASH - The driver now supports interrupts but they are
+ * turned off by default. Use of interrupts is highly encouraged, it
+ * cuts CPU usage down to a reasonable level. I had DMA in for a while
+ * but PC DMA is just too slow. Better to just insb() it.
+ *
+ * One thing about these drives: They talk in MSF (Minute Second Frame) format.
+ * There are 75 frames a second, 60 seconds a minute, and up to 75 minutes on a
+ * disk. The funny thing is that these are sent to the drive in BCD, but the
+ * interface wants to see them in decimal. A lot of conversion goes on.
+ *
+ * DRIVER SPECIAL FEATURES
+ * -----------------------
+ *
+ * This section describes features beyond the normal audio and CD-ROM
+ * functions of the drive.
+ *
+ * XA compatibility
+ *
+ * The driver should support XA disks for both the CDU31A and CDU33A.
+ * It does this transparently, the using program doesn't need to set it.
+ *
+ * Multi-Session
+ *
+ * A multi-session disk looks just like a normal disk to the user.
+ * Just mount one normally, and all the data should be there.
+ * A special thanks to Koen for help with this!
+ *
+ * Raw sector I/O
+ *
+ * Using the CDROMREADAUDIO it is possible to read raw audio and data
+ * tracks. Both operations return 2352 bytes per sector. On the data
+ * tracks, the first 12 bytes is not returned by the drive and the value
+ * of that data is indeterminate.
+ *
+ *
+ * Copyright (C) 1993 Corey Minyard
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * TODO:
+ * CDs with form1 and form2 sectors cause problems
+ * with current read-ahead strategy.
+ *
+ * Credits:
+ * Heiko Eissfeldt <heiko@colossus.escape.de>
+ * For finding abug in the return of the track numbers.
+ * TOC processing redone for proper multisession support.
+ *
+ *
+ * It probably a little late to be adding a history, but I guess I
+ * will start.
+ *
+ * 10/24/95 - Added support for disabling the eject button when the
+ * drive is open. Note that there is a small problem
+ * still here, if the eject button is pushed while the
+ * drive light is flashing, the drive will return a bad
+ * status and be reset. It recovers, though.
+ *
+ * 03/07/97 - Fixed a problem with timers.
+ *
+ *
+ * 18 Spetember 1997 -- Ported to Uniform CD-ROM driver by
+ * Heiko Eissfeldt <heiko@colossus.escape.de> with additional
+ * changes by Erik Andersen <andersee@debian.org>
+ *
+ * 24 January 1998 -- Removed the scd_disc_status() function, which was now
+ * just dead code left over from the port.
+ * Erik Andersen <andersee@debian.org>
+ *
+ * 16 July 1998 -- Drive donated to Erik Andersen by John Kodis
+ * <kodis@jagunet.com>. Work begun on fixing driver to
+ * work under 2.1.X. Added temporary extra printks
+ * which seem to slow it down enough to work.
+ *
+ * 9 November 1999 -- Make kernel-parameter implementation work with 2.3.x
+ * Removed init_module & cleanup_module in favor of
+ * module_init & module_exit.
+ * Torben Mathiasen <tmm@image.dk>
+ *
+ * 22 October 2004 -- Make the driver work in 2.6.X
+ * Added workaround to fix hard lockups on eject
+ * Fixed door locking problem after mounting empty drive
+ * Set double-speed drives to double speed by default
+ * Removed all readahead things - not needed anymore
+ * Ondrej Zary <rainbow@rainbow-software.org>
+*/
+
+#define DEBUG 1
+
+#include <linux/major.h>
+#include <linux/module.h>
+#include <linux/errno.h>
+#include <linux/signal.h>
+#include <linux/sched.h>
+#include <linux/timer.h>
+#include <linux/fs.h>
+#include <linux/kernel.h>
+#include <linux/hdreg.h>
+#include <linux/genhd.h>
+#include <linux/ioport.h>
+#include <linux/devfs_fs_kernel.h>
+#include <linux/string.h>
+#include <linux/slab.h>
+#include <linux/init.h>
+#include <linux/interrupt.h>
+#include <linux/cdrom.h>
+
+#include <asm/system.h>
+#include <asm/io.h>
+#include <asm/uaccess.h>
+#include <asm/dma.h>
+
+#include "cdu31a.h"
+
+#define MAJOR_NR CDU31A_CDROM_MAJOR
+#include <linux/blkdev.h>
+
+#define CDU31A_MAX_CONSECUTIVE_ATTENTIONS 10
+
+#define PFX "CDU31A: "
+
+/*
+** Edit the following data to change interrupts, DMA channels, etc.
+** Default is polled and no DMA. DMA is not recommended for double-speed
+** drives.
+*/
+static struct {
+ unsigned short base; /* I/O Base Address */
+ short int_num; /* Interrupt Number (-1 means scan for it,
+ 0 means don't use) */
+} cdu31a_addresses[] __initdata = {
+ {0}
+};
+
+static int handle_sony_cd_attention(void);
+static int read_subcode(void);
+static void sony_get_toc(void);
+static int scd_spinup(void);
+/*static int scd_open(struct inode *inode, struct file *filp);*/
+static int scd_open(struct cdrom_device_info *, int);
+static void do_sony_cd_cmd(unsigned char cmd,
+ unsigned char *params,
+ unsigned int num_params,
+ unsigned char *result_buffer,
+ unsigned int *result_size);
+static void size_to_buf(unsigned int size, unsigned char *buf);
+
+/* Parameters for the read-ahead. */
+static unsigned int sony_next_block; /* Next 512 byte block offset */
+static unsigned int sony_blocks_left = 0; /* Number of 512 byte blocks left
+ in the current read command. */
+
+
+/* The base I/O address of the Sony Interface. This is a variable (not a
+ #define) so it can be easily changed via some future ioctl() */
+static unsigned int cdu31a_port = 0;
+module_param(cdu31a_port, uint, 0);
+
+/*
+ * The following are I/O addresses of the various registers for the drive. The
+ * comment for the base address also applies here.
+ */
+static volatile unsigned short sony_cd_cmd_reg;
+static volatile unsigned short sony_cd_param_reg;
+static volatile unsigned short sony_cd_write_reg;
+static volatile unsigned short sony_cd_control_reg;
+static volatile unsigned short sony_cd_status_reg;
+static volatile unsigned short sony_cd_result_reg;
+static volatile unsigned short sony_cd_read_reg;
+static volatile unsigned short sony_cd_fifost_reg;
+
+static struct request_queue *cdu31a_queue;
+static DEFINE_SPINLOCK(cdu31a_lock); /* queue lock */
+
+static int sony_spun_up = 0; /* Has the drive been spun up? */
+
+static int sony_speed = 0; /* Last wanted speed */
+
+static int sony_xa_mode = 0; /* Is an XA disk in the drive
+ and the drive a CDU31A? */
+
+static int sony_raw_data_mode = 1; /* 1 if data tracks, 0 if audio.
+ For raw data reads. */
+
+static unsigned int sony_usage = 0; /* How many processes have the
+ drive open. */
+
+static int sony_pas_init = 0; /* Initialize the Pro-Audio
+ Spectrum card? */
+
+static struct s_sony_session_toc single_toc; /* Holds the
+ table of
+ contents. */
+
+static struct s_all_sessions_toc sony_toc; /* entries gathered from all
+ sessions */
+
+static int sony_toc_read = 0; /* Has the TOC been read for
+ the drive? */
+
+static struct s_sony_subcode last_sony_subcode; /* Points to the last
+ subcode address read */
+
+static DECLARE_MUTEX(sony_sem); /* Semaphore for drive hardware access */
+
+static int is_double_speed = 0; /* does the drive support double speed ? */
+
+static int is_auto_eject = 1; /* Door has been locked? 1=No/0=Yes */
+
+/*
+ * The audio status uses the values from read subchannel data as specified
+ * in include/linux/cdrom.h.
+ */
+static volatile int sony_audio_status = CDROM_AUDIO_NO_STATUS;
+
+/*
+ * The following are a hack for pausing and resuming audio play. The drive
+ * does not work as I would expect it, if you stop it then start it again,
+ * the drive seeks back to the beginning and starts over. This holds the
+ * position during a pause so a resume can restart it. It uses the
+ * audio status variable above to tell if it is paused.
+ */
+static unsigned volatile char cur_pos_msf[3] = { 0, 0, 0 };
+static unsigned volatile char final_pos_msf[3] = { 0, 0, 0 };
+
+/* What IRQ is the drive using? 0 if none. */
+static int cdu31a_irq = 0;
+module_param(cdu31a_irq, int, 0);
+
+/* The interrupt handler will wake this queue up when it gets an
+ interrupts. */
+DECLARE_WAIT_QUEUE_HEAD(cdu31a_irq_wait);
+static int irq_flag = 0;
+
+static int curr_control_reg = 0; /* Current value of the control register */
+
+/* A disk changed variable. When a disk change is detected, it will
+ all be set to TRUE. As the upper layers ask for disk_changed status
+ it will be cleared. */
+static char disk_changed;
+
+/* This was readahead_buffer once... Now it's used only for audio reads */
+static char audio_buffer[CD_FRAMESIZE_RAW];
+
+/* Used to time a short period to abort an operation after the
+ drive has been idle for a while. This keeps the light on
+ the drive from flashing for very long. */
+static struct timer_list cdu31a_abort_timer;
+
+/* Marks if the timeout has started an abort read. This is used
+ on entry to the drive to tell the code to read out the status
+ from the abort read. */
+static int abort_read_started = 0;
+
+/*
+ * Uniform cdrom interface function
+ * report back, if disc has changed from time of last request.
+ */
+static int scd_media_changed(struct cdrom_device_info *cdi, int disc_nr)
+{
+ int retval;
+
+ retval = disk_changed;
+ disk_changed = 0;
+
+ return retval;
+}
+
+/*
+ * Uniform cdrom interface function
+ * report back, if drive is ready
+ */
+static int scd_drive_status(struct cdrom_device_info *cdi, int slot_nr)
+{
+ if (CDSL_CURRENT != slot_nr)
+ /* we have no changer support */
+ return -EINVAL;
+ if (sony_spun_up)
+ return CDS_DISC_OK;
+ if (down_interruptible(&sony_sem))
+ return -ERESTARTSYS;
+ if (scd_spinup() == 0)
+ sony_spun_up = 1;
+ up(&sony_sem);
+ return sony_spun_up ? CDS_DISC_OK : CDS_DRIVE_NOT_READY;
+}
+
+static inline void enable_interrupts(void)
+{
+ curr_control_reg |= (SONY_ATTN_INT_EN_BIT
+ | SONY_RES_RDY_INT_EN_BIT
+ | SONY_DATA_RDY_INT_EN_BIT);
+ outb(curr_control_reg, sony_cd_control_reg);
+}
+
+static inline void disable_interrupts(void)
+{
+ curr_control_reg &= ~(SONY_ATTN_INT_EN_BIT
+ | SONY_RES_RDY_INT_EN_BIT
+ | SONY_DATA_RDY_INT_EN_BIT);
+ outb(curr_control_reg, sony_cd_control_reg);
+}
+
+/*
+ * Wait a little while (used for polling the drive). If in initialization,
+ * setting a timeout doesn't work, so just loop for a while.
+ */
+static inline void sony_sleep(void)
+{
+ if (cdu31a_irq <= 0) {
+ yield();
+ } else { /* Interrupt driven */
+ DEFINE_WAIT(w);
+ int first = 1;
+
+ while (1) {
+ prepare_to_wait(&cdu31a_irq_wait, &w,
+ TASK_INTERRUPTIBLE);
+ if (first) {
+ enable_interrupts();
+ first = 0;
+ }
+
+ if (irq_flag != 0)
+ break;
+ if (!signal_pending(current)) {
+ schedule();
+ continue;
+ } else
+ disable_interrupts();
+ break;
+ }
+ finish_wait(&cdu31a_irq_wait, &w);
+ irq_flag = 0;
+ }
+}
+
+
+/*
+ * The following are convenience routine to read various status and set
+ * various conditions in the drive.
+ */
+static inline int is_attention(void)
+{
+ return (inb(sony_cd_status_reg) & SONY_ATTN_BIT) != 0;
+}
+
+static inline int is_busy(void)
+{
+ return (inb(sony_cd_status_reg) & SONY_BUSY_BIT) != 0;
+}
+
+static inline int is_data_ready(void)
+{
+ return (inb(sony_cd_status_reg) & SONY_DATA_RDY_BIT) != 0;
+}
+
+static inline int is_data_requested(void)
+{
+ return (inb(sony_cd_status_reg) & SONY_DATA_REQUEST_BIT) != 0;
+}
+
+static inline int is_result_ready(void)
+{
+ return (inb(sony_cd_status_reg) & SONY_RES_RDY_BIT) != 0;
+}
+
+static inline int is_param_write_rdy(void)
+{
+ return (inb(sony_cd_fifost_reg) & SONY_PARAM_WRITE_RDY_BIT) != 0;
+}
+
+static inline int is_result_reg_not_empty(void)
+{
+ return (inb(sony_cd_fifost_reg) & SONY_RES_REG_NOT_EMP_BIT) != 0;
+}
+
+static inline void reset_drive(void)
+{
+ curr_control_reg = 0;
+ sony_toc_read = 0;
+ outb(SONY_DRIVE_RESET_BIT, sony_cd_control_reg);
+}
+
+/*
+ * Uniform cdrom interface function
+ * reset drive and return when it is ready
+ */
+static int scd_reset(struct cdrom_device_info *cdi)
+{
+ unsigned long retry_count;
+
+ if (down_interruptible(&sony_sem))
+ return -ERESTARTSYS;
+ reset_drive();
+
+ retry_count = jiffies + SONY_RESET_TIMEOUT;
+ while (time_before(jiffies, retry_count) && (!is_attention())) {
+ sony_sleep();
+ }
+
+ up(&sony_sem);
+ return 0;
+}
+
+static inline void clear_attention(void)
+{
+ outb(curr_control_reg | SONY_ATTN_CLR_BIT, sony_cd_control_reg);
+}
+
+static inline void clear_result_ready(void)
+{
+ outb(curr_control_reg | SONY_RES_RDY_CLR_BIT, sony_cd_control_reg);
+}
+
+static inline void clear_data_ready(void)
+{
+ outb(curr_control_reg | SONY_DATA_RDY_CLR_BIT,
+ sony_cd_control_reg);
+}
+
+static inline void clear_param_reg(void)
+{
+ outb(curr_control_reg | SONY_PARAM_CLR_BIT, sony_cd_control_reg);
+}
+
+static inline unsigned char read_status_register(void)
+{
+ return inb(sony_cd_status_reg);
+}
+
+static inline unsigned char read_result_register(void)
+{
+ return inb(sony_cd_result_reg);
+}
+
+static inline unsigned char read_data_register(void)
+{
+ return inb(sony_cd_read_reg);
+}
+
+static inline void write_param(unsigned char param)
+{
+ outb(param, sony_cd_param_reg);
+}
+
+static inline void write_cmd(unsigned char cmd)
+{
+ outb(curr_control_reg | SONY_RES_RDY_INT_EN_BIT,
+ sony_cd_control_reg);
+ outb(cmd, sony_cd_cmd_reg);
+}
+
+static irqreturn_t cdu31a_interrupt(int irq, void *dev_id, struct pt_regs *regs)
+{
+ unsigned char val;
+
+ if (abort_read_started) {
+ /* We might be waiting for an abort to finish. Don't
+ disable interrupts yet, though, because we handle
+ this one here. */
+ /* Clear out the result registers. */
+ while (is_result_reg_not_empty()) {
+ val = read_result_register();
+ }
+ clear_data_ready();
+ clear_result_ready();
+
+ /* Clear out the data */
+ while (is_data_requested()) {
+ val = read_data_register();
+ }
+ abort_read_started = 0;
+
+ /* If something was waiting, wake it up now. */
+ if (waitqueue_active(&cdu31a_irq_wait)) {
+ disable_interrupts();
+ irq_flag = 1;
+ wake_up_interruptible(&cdu31a_irq_wait);
+ }
+ } else if (waitqueue_active(&cdu31a_irq_wait)) {
+ disable_interrupts();
+ irq_flag = 1;
+ wake_up_interruptible(&cdu31a_irq_wait);
+ } else {
+ disable_interrupts();
+ printk(KERN_NOTICE PFX
+ "Got an interrupt but nothing was waiting\n");
+ }
+ return IRQ_HANDLED;
+}
+
+/*
+ * give more verbose error messages
+ */
+static unsigned char *translate_error(unsigned char err_code)
+{
+ static unsigned char errbuf[80];
+
+ switch (err_code) {
+ case 0x10: return "illegal command ";
+ case 0x11: return "illegal parameter ";
+
+ case 0x20: return "not loaded ";
+ case 0x21: return "no disc ";
+ case 0x22: return "not spinning ";
+ case 0x23: return "spinning ";
+ case 0x25: return "spindle servo ";
+ case 0x26: return "focus servo ";
+ case 0x29: return "eject mechanism ";
+ case 0x2a: return "audio playing ";
+ case 0x2c: return "emergency eject ";
+
+ case 0x30: return "focus ";
+ case 0x31: return "frame sync ";
+ case 0x32: return "subcode address ";
+ case 0x33: return "block sync ";
+ case 0x34: return "header address ";
+
+ case 0x40: return "illegal track read ";
+ case 0x41: return "mode 0 read ";
+ case 0x42: return "illegal mode read ";
+ case 0x43: return "illegal block size read ";
+ case 0x44: return "mode read ";
+ case 0x45: return "form read ";
+ case 0x46: return "leadout read ";
+ case 0x47: return "buffer overrun ";
+
+ case 0x53: return "unrecoverable CIRC ";
+ case 0x57: return "unrecoverable LECC ";
+
+ case 0x60: return "no TOC ";
+ case 0x61: return "invalid subcode data ";
+ case 0x63: return "focus on TOC read ";
+ case 0x64: return "frame sync on TOC read ";
+ case 0x65: return "TOC data ";
+
+ case 0x70: return "hardware failure ";
+ case 0x91: return "leadin ";
+ case 0x92: return "leadout ";
+ case 0x93: return "data track ";
+ }
+ sprintf(errbuf, "unknown 0x%02x ", err_code);
+ return errbuf;
+}
+
+/*
+ * Set the drive parameters so the drive will auto-spin-up when a
+ * disk is inserted.
+ */
+static void set_drive_params(int want_doublespeed)
+{
+ unsigned char res_reg[12];
+ unsigned int res_size;
+ unsigned char params[3];
+
+
+ params[0] = SONY_SD_AUTO_SPIN_DOWN_TIME;
+ params[1] = 0x00; /* Never spin down the drive. */
+ do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
+ params, 2, res_reg, &res_size);
+ if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20)) {
+ printk(KERN_NOTICE PFX
+ "Unable to set spin-down time: 0x%2.2x\n", res_reg[1]);
+ }
+
+ params[0] = SONY_SD_MECH_CONTROL;
+ params[1] = SONY_AUTO_SPIN_UP_BIT; /* Set auto spin up */
+
+ if (is_auto_eject)
+ params[1] |= SONY_AUTO_EJECT_BIT;
+
+ if (is_double_speed && want_doublespeed) {
+ params[1] |= SONY_DOUBLE_SPEED_BIT; /* Set the drive to double speed if
+ possible */
+ }
+ do_sony_cd_cmd(SONY_SET_DRIVE_PARAM_CMD,
+ params, 2, res_reg, &res_size);
+ if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20)) {
+ printk(KERN_NOTICE PFX "Unable to set mechanical "
+ "parameters: 0x%2.2x\n", res_reg[1]);
+ }
+}
+
+/*
+ * Uniform cdrom interface function
+ * select reading speed for data access
+ */
+static int scd_select_speed(struct cdrom_device_info *cdi, int speed)
+{
+ if (speed == 0)
+ sony_speed = 1;
+ else
+ sony_speed = speed - 1;
+
+ if (down_interruptible(&sony_sem))
+ return -ERESTARTSYS;
+ set_drive_params(sony_speed);
+ up(&sony_sem);
+ return 0;
+}
+
+/*
+ * Uniform cdrom interface function
+ * lock or unlock eject button
+ */
+static int scd_lock_door(struct cdrom_device_info *cdi, int lock)
+{
+ if (lock == 0) {
+ is_auto_eject = 1;
+ } else {
+ is_auto_eject = 0;
+ }
+ if (down_interruptible(&sony_sem))
+ return -ERESTARTSYS;
+ set_drive_params(sony_speed);
+ up(&sony_sem);
+ return 0;
+}
+
+/*
+ * This code will reset the drive and attempt to restore sane parameters.
+ */
+static void restart_on_error(void)
+{
+ unsigned char res_reg[12];
+ unsigned int res_size;
+ unsigned long retry_count;
+
+
+ printk(KERN_NOTICE PFX "Resetting drive on error\n");
+ reset_drive();
+ retry_count = jiffies + SONY_RESET_TIMEOUT;
+ while (time_before(jiffies, retry_count) && (!is_attention())) {
+ sony_sleep();
+ }
+ set_drive_params(sony_speed);
+ do_sony_cd_cmd(SONY_SPIN_UP_CMD, NULL, 0, res_reg, &res_size);
+ if ((res_size < 2) || ((res_reg[0] & 0xf0) == 0x20)) {
+ printk(KERN_NOTICE PFX "Unable to spin up drive: 0x%2.2x\n",
+ res_reg[1]);
+ }
+
+ msleep(2000);
+
+ sony_get_toc();
+}
+
+/*
+ * This routine writes data to the parameter register. Since this should
+ * happen fairly fast, it is polled with no OS waits between.
+ */
+static int write_params(unsigned char *params, int num_params)
+{
+ unsigned int retry_count;
+
+
+ retry_count = SONY_READY_RETRIES;
+ while ((retry_count > 0) && (!is_param_write_rdy())) {
+ retry_count--;
+ }
+ if (!is_param_write_rdy()) {
+ return -EIO;
+ }
+
+ while (num_params > 0) {
+ write_param(*params);
+ params++;
+ num_params--;
+ }
+
+ return 0;
+}
+
+
+/*
+ * The following reads data from the command result register. It is a
+ * fairly complex routine, all status info flows back through this
+ * interface. The algorithm is stolen directly from the flowcharts in
+ * the drive manual.
+ */
+static void
+get_result(unsigned char *result_buffer, unsigned int *result_size)
+{
+ unsigned char a, b;
+ int i;
+ unsigned long retry_count;
+
+
+ while (handle_sony_cd_attention());
+ /* Wait for the result data to be ready */
+ retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
+ while (time_before(jiffies, retry_count)
+ && (is_busy() || (!(is_result_ready())))) {
+ sony_sleep();
+
+ while (handle_sony_cd_attention());
+ }
+ if (is_busy() || (!(is_result_ready()))) {
+ pr_debug(PFX "timeout out %d\n", __LINE__);
+ result_buffer[0] = 0x20;
+ result_buffer[1] = SONY_TIMEOUT_OP_ERR;
+ *result_size = 2;
+ return;
+ }
+
+ /*
+ * Get the first two bytes. This determines what else needs
+ * to be done.
+ */
+ clear_result_ready();
+ a = read_result_register();
+ *result_buffer = a;
+ result_buffer++;
+
+ /* Check for block error status result. */
+ if ((a & 0xf0) == 0x50) {
+ *result_size = 1;
+ return;
+ }
+
+ b = read_result_register();
+ *result_buffer = b;
+ result_buffer++;
+ *result_size = 2;
+
+ /*
+ * 0x20 means an error occurred. Byte 2 will have the error code.
+ * Otherwise, the command succeeded, byte 2 will have the count of
+ * how many more status bytes are coming.
+ *
+ * The result register can be read 10 bytes at a time, a wait for
+ * result ready to be asserted must be done between every 10 bytes.
+ */
+ if ((a & 0xf0) != 0x20) {
+ if (b > 8) {
+ for (i = 0; i < 8; i++) {
+ *result_buffer = read_result_register();
+ result_buffer++;
+ (*result_size)++;
+ }
+ b = b - 8;
+
+ while (b > 10) {
+ retry_count = SONY_READY_RETRIES;
+ while ((retry_count > 0)
+ && (!is_result_ready())) {
+ retry_count--;
+ }
+ if (!is_result_ready()) {
+ pr_debug(PFX "timeout out %d\n",
+ __LINE__);
+ result_buffer[0] = 0x20;
+ result_buffer[1] =
+ SONY_TIMEOUT_OP_ERR;
+ *result_size = 2;
+ return;
+ }
+
+ clear_result_ready();
+
+ for (i = 0; i < 10; i++) {
+ *result_buffer =
+ read_result_register();
+ result_buffer++;
+ (*result_size)++;
+ }
+ b = b - 10;
+ }
+
+ if (b > 0) {
+ retry_count = SONY_READY_RETRIES;
+ while ((retry_count > 0)
+ && (!is_result_ready())) {
+ retry_count--;
+ }
+ if (!is_result_ready()) {
+ pr_debug(PFX "timeout out %d\n",
+ __LINE__);
+ result_buffer[0] = 0x20;
+ result_buffer[1] =
+ SONY_TIMEOUT_OP_ERR;
+ *result_size = 2;
+ return;
+ }
+ }
+ }
+
+ while (b > 0) {
+ *result_buffer = read_result_register();
+ result_buffer++;
+ (*result_size)++;
+ b--;
+ }
+ }
+}
+
+/*
+ * Do a command that does not involve data transfer. This routine must
+ * be re-entrant from the same task to support being called from the
+ * data operation code when an error occurs.
+ */
+static void
+do_sony_cd_cmd(unsigned char cmd,
+ unsigned char *params,
+ unsigned int num_params,
+ unsigned char *result_buffer, unsigned int *result_size)
+{
+ unsigned long retry_count;
+ int num_retries = 0;
+
+retry_cd_operation:
+
+ while (handle_sony_cd_attention());
+
+ retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
+ while (time_before(jiffies, retry_count) && (is_busy())) {
+ sony_sleep();
+
+ while (handle_sony_cd_attention());
+ }
+ if (is_busy()) {
+ pr_debug(PFX "timeout out %d\n", __LINE__);
+ result_buffer[0] = 0x20;
+ result_buffer[1] = SONY_TIMEOUT_OP_ERR;
+ *result_size = 2;
+ } else {
+ clear_result_ready();
+ clear_param_reg();
+
+ write_params(params, num_params);
+ write_cmd(cmd);
+
+ get_result(result_buffer, result_size);
+ }
+
+ if (((result_buffer[0] & 0xf0) == 0x20)
+ && (num_retries < MAX_CDU31A_RETRIES)) {
+ num_retries++;
+ msleep(100);
+ goto retry_cd_operation;
+ }
+}
+
+
+/*
+ * Handle an attention from the drive. This will return 1 if it found one
+ * or 0 if not (if one is found, the caller might want to call again).
+ *
+ * This routine counts the number of consecutive times it is called
+ * (since this is always called from a while loop until it returns
+ * a 0), and returns a 0 if it happens too many times. This will help
+ * prevent a lockup.
+ */
+static int handle_sony_cd_attention(void)
+{
+ unsigned char atten_code;
+ static int num_consecutive_attentions = 0;
+ volatile int val;
+
+
+#if 0
+ pr_debug(PFX "Entering %s\n", __FUNCTION__);
+#endif
+ if (is_attention()) {
+ if (num_consecutive_attentions >
+ CDU31A_MAX_CONSECUTIVE_ATTENTIONS) {
+ printk(KERN_NOTICE PFX "Too many consecutive "
+ "attentions: %d\n", num_consecutive_attentions);
+ num_consecutive_attentions = 0;
+ pr_debug(PFX "Leaving %s at %d\n", __FUNCTION__,
+ __LINE__);
+ return 0;
+ }
+
+ clear_attention();
+ atten_code = read_result_register();
+
+ switch (atten_code) {
+ /* Someone changed the CD. Mark it as changed */
+ case SONY_MECH_LOADED_ATTN:
+ disk_changed = 1;
+ sony_toc_read = 0;
+ sony_audio_status = CDROM_AUDIO_NO_STATUS;
+ sony_blocks_left = 0;
+ break;
+
+ case SONY_SPIN_DOWN_COMPLETE_ATTN:
+ /* Mark the disk as spun down. */
+ sony_spun_up = 0;
+ break;
+
+ case SONY_AUDIO_PLAY_DONE_ATTN:
+ sony_audio_status = CDROM_AUDIO_COMPLETED;
+ read_subcode();
+ break;
+
+ case SONY_EJECT_PUSHED_ATTN:
+ if (is_auto_eject) {
+ sony_audio_status = CDROM_AUDIO_INVALID;
+ }
+ break;
+
+ case SONY_LEAD_IN_ERR_ATTN:
+ case SONY_LEAD_OUT_ERR_ATTN:
+ case SONY_DATA_TRACK_ERR_ATTN:
+ case SONY_AUDIO_PLAYBACK_ERR_ATTN:
+ sony_audio_status = CDROM_AUDIO_ERROR;
+ break;
+ }
+
+ num_consecutive_attentions++;
+ pr_debug(PFX "Leaving %s at %d\n", __FUNCTION__, __LINE__);
+ return 1;
+ } else if (abort_read_started) {
+ while (is_result_reg_not_empty()) {
+ val = read_result_register();
+ }
+ clear_data_ready();
+ clear_result_ready();
+ /* Clear out the data */
+ while (is_data_requested()) {
+ val = read_data_register();
+ }
+ abort_read_started = 0;
+ pr_debug(PFX "Leaving %s at %d\n", __FUNCTION__, __LINE__);
+ return 1;
+ }
+
+ num_consecutive_attentions = 0;
+#if 0
+ pr_debug(PFX "Leaving %s at %d\n", __FUNCTION__, __LINE__);
+#endif
+ return 0;
+}
+
+
+/* Convert from an integer 0-99 to BCD */
+static inline unsigned int int_to_bcd(unsigned int val)
+{
+ int retval;
+
+
+ retval = (val / 10) << 4;
+ retval = retval | val % 10;
+ return retval;
+}
+
+
+/* Convert from BCD to an integer from 0-99 */
+static unsigned int bcd_to_int(unsigned int bcd)
+{
+ return (((bcd >> 4) & 0x0f) * 10) + (bcd & 0x0f);
+}
+
+
+/*
+ * Convert a logical sector value (like the OS would want to use for
+ * a block device) to an MSF format.
+ */
+static void log_to_msf(unsigned int log, unsigned char *msf)
+{
+ log = log + LOG_START_OFFSET;
+ msf[0] = int_to_bcd(log / 4500);
+ log = log % 4500;
+ msf[1] = int_to_bcd(log / 75);
+ msf[2] = int_to_bcd(log % 75);
+}
+
+
+/*
+ * Convert an MSF format to a logical sector.
+ */
+static unsigned int msf_to_log(unsigned char *msf)
+{
+ unsigned int log;
+
+
+ log = msf[2];
+ log += msf[1] * 75;
+ log += msf[0] * 4500;
+ log = log - LOG_START_OFFSET;
+
+ return log;
+}
+
+
+/*
+ * Take in integer size value and put it into a buffer like
+ * the drive would want to see a number-of-sector value.
+ */
+static void size_to_buf(unsigned int size, unsigned char *buf)
+{
+ buf[0] = size / 65536;
+ size = size % 65536;
+ buf[1] = size / 256;
+ buf[2] = size % 256;
+}
+
+/* Starts a read operation. Returns 0 on success and 1 on failure.
+ The read operation used here allows multiple sequential sectors
+ to be read and status returned for each sector. The driver will
+ read the output one at a time as the requests come and abort the
+ operation if the requested sector is not the next one from the
+ drive. */
+static int
+start_request(unsigned int sector, unsigned int nsect)
+{
+ unsigned char params[6];
+ unsigned long retry_count;
+
+
+ pr_debug(PFX "Entering %s\n", __FUNCTION__);
+ log_to_msf(sector, params);
+ size_to_buf(nsect, &params[3]);
+
+ /*
+ * Clear any outstanding attentions and wait for the drive to
+ * complete any pending operations.
+ */
+ while (handle_sony_cd_attention());
+
+ retry_count = jiffies + SONY_JIFFIES_TIMEOUT;
+ while (time_before(jiffies, retry_count) && (is_busy())) {
+ sony_sleep();
+
+ while (handle_sony_cd_attention());
+ }
+
+ if (is_busy()) {
+ printk(KERN_NOTICE PFX "Timeout while waiting "
+ "to issue command\n");
+ pr_debug(PFX "Leaving %s at %d\n", __FUNCTION__, __LINE__);
+ return 1;
+ } else {
+ /* Issue the command */
+ clear_result_ready();
+ clear_param_reg();
+
+ write_params(params, 6);
+ write_cmd(SONY_READ_BLKERR_STAT_CMD);
+
+ sony_blocks_left = nsect * 4;
+ sony_next_block = sector * 4;
+ pr_debug(PFX "Leaving %s at %d\n", __FUNCTION__, __LINE__);
+ return 0;
+ }
+ pr_debug(PFX "Leaving %s at %d\n", __FUNCTION__, __LINE__);
+}
+
+/* Abort a pending read operation. Clear all the drive status variables. */
+static void abort_read(void)
+{
+ unsigned char result_reg[2];
+ int result_size;
+ volatile int val;
+
+
+ do_sony_cd_cmd(SONY_ABORT_CMD, NULL, 0, result_reg, &result_size);
+ if ((result_reg[0] & 0xf0) == 0x20) {
+ printk(KERN_ERR PFX "Aborting read, %s error\n",
+ translate_error(result_reg[1]));
+ }
+
+ while (is_result_reg_not_empty()) {
+ val = read_result_register();
+ }
+ clear_data_ready();
+ clear_result_ready();
+ /* Clear out the data */
+ while (is_data_requested()) {
+ val = read_data_register();
+ }
+
+ sony_blocks_left = 0;
+}
+
+/* Called when the timer times out. This will abort the
+ pending read operation. */
+static void handle_abort_timeout(unsigned long data)
+{
+ pr_debug(PFX "Entering %s\n", __FUNCTION__);
+ /* If it is in use, ignore it. */
+ if (down_trylock(&sony_sem) == 0) {
+ /* We can't use abort_read(), because it will sleep
+ or schedule in the timer interrupt. Just start
+ the operation, finish it on the next access to
+ the drive. */
+ clear_result_ready();
+ clear_param_reg();
+ write_cmd(SONY_ABORT_CMD);
+
+ sony_blocks_left = 0;
+ abort_read_started = 1;
+ up(&sony_sem);
+ }
+ pr_debug(PFX "Leaving %s\n", __FUNCTION__);
+}
+
+/* Actually get one sector of data from the drive. */
+static void
+input_data_sector(char *buffer)