📄 optcd.c
字号:
/* linux/drivers/cdrom/optcd.c - Optics Storage 8000 AT CDROM driver $Id: optcd.c,v 1.11 1997/01/26 07:13:00 davem Exp $ Copyright (C) 1995 Leo Spiekman (spiekman@dutette.et.tudelft.nl) Based on Aztech CD268 CDROM driver by Werner Zimmermann and preworks by Eberhard Moenkeberg (emoenke@gwdg.de). 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, 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.*//* Revision history 14-5-95 v0.0 Plays sound tracks. No reading of data CDs yet. Detection of disk change doesn't work. 21-5-95 v0.1 First ALPHA version. CD can be mounted. The device major nr is borrowed from the Aztech driver. Speed is around 240 kb/s, as measured with "time dd if=/dev/cdrom of=/dev/null \ bs=2048 count=4096". 24-6-95 v0.2 Reworked the #defines for the command codes and the like, as well as the structure of the hardware communication protocol, to reflect the "official" documentation, kindly supplied by C.K. Tan, Optics Storage Pte. Ltd. Also tidied up the state machine somewhat. 28-6-95 v0.3 Removed the ISP-16 interface code, as this should go into its own driver. The driver now has its own major nr. Disk change detection now seems to work, too. This version became part of the standard kernel as of version 1.3.7 24-9-95 v0.4 Re-inserted ISP-16 interface code which I copied from sjcd.c, with a few changes. Updated README.optcd. Submitted for inclusion in 1.3.21 29-9-95 v0.4a Fixed bug that prevented compilation as module 25-10-95 v0.5 Started multisession code. Implementation copied from Werner Zimmermann, who copied it from Heiko Schlittermann's mcdx. 17-1-96 v0.6 Multisession works; some cleanup too. 18-4-96 v0.7 Increased some timing constants; thanks to Luke McFarlane. Also tidied up some printk behaviour. ISP16 initialization is now handled by a separate driver. 09-11-99 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>*//* Includes */#include <linux/module.h>#include <linux/mm.h>#include <linux/ioport.h>#include <linux/init.h>#include <linux/devfs_fs_kernel.h>#include <asm/io.h>#define MAJOR_NR OPTICS_CDROM_MAJOR#include <linux/blk.h>#include <linux/cdrom.h>#include "optcd.h"#include <asm/uaccess.h>/* Debug support *//* Don't forget to add new debug flags here. */#if DEBUG_DRIVE_IF | DEBUG_VFS | DEBUG_CONV | DEBUG_TOC | \ DEBUG_BUFFERS | DEBUG_REQUEST | DEBUG_STATE | DEBUG_MULTIS#define DEBUG(x) debug xstatic void debug(int debug_this, const char* fmt, ...){ char s[1024]; va_list args; if (!debug_this) return; va_start(args, fmt); vsprintf(s, fmt, args); printk(KERN_DEBUG "optcd: %s\n", s); va_end(args);}#else#define DEBUG(x)#endifstatic int blksize = 2048;static int hsecsize = 2048;/* Drive hardware/firmware characteristics Identifiers in accordance with Optics Storage documentation */#define optcd_port optcd /* Needed for the modutils. */static short optcd_port = OPTCD_PORTBASE; /* I/O base of drive. */MODULE_PARM(optcd_port, "h");/* Drive registers, read */#define DATA_PORT optcd_port /* Read data/status */#define STATUS_PORT optcd_port+1 /* Indicate data/status availability *//* Drive registers, write */#define COMIN_PORT optcd_port /* For passing command/parameter */#define RESET_PORT optcd_port+1 /* Write anything and wait 0.5 sec */#define HCON_PORT optcd_port+2 /* Host Xfer Configuration *//* Command completion/status read from DATA register */#define ST_DRVERR 0x80#define ST_DOOR_OPEN 0x40#define ST_MIXEDMODE_DISK 0x20#define ST_MODE_BITS 0x1c#define ST_M_STOP 0x00#define ST_M_READ 0x04#define ST_M_AUDIO 0x04#define ST_M_PAUSE 0x08#define ST_M_INITIAL 0x0c#define ST_M_ERROR 0x10#define ST_M_OTHERS 0x14#define ST_MODE2TRACK 0x02#define ST_DSK_CHG 0x01#define ST_L_LOCK 0x01#define ST_CMD_OK 0x00#define ST_OP_OK 0x01#define ST_PA_OK 0x02#define ST_OP_ERROR 0x05#define ST_PA_ERROR 0x06/* Error codes (appear as command completion code from DATA register) *//* Player related errors */#define ERR_ILLCMD 0x11 /* Illegal command to player module */#define ERR_ILLPARM 0x12 /* Illegal parameter to player module */#define ERR_SLEDGE 0x13#define ERR_FOCUS 0x14#define ERR_MOTOR 0x15#define ERR_RADIAL 0x16#define ERR_PLL 0x17 /* PLL lock error */#define ERR_SUB_TIM 0x18 /* Subcode timeout error */#define ERR_SUB_NF 0x19 /* Subcode not found error */#define ERR_TRAY 0x1a#define ERR_TOC 0x1b /* Table of Contents read error */#define ERR_JUMP 0x1c/* Data errors */#define ERR_MODE 0x21#define ERR_FORM 0x22#define ERR_HEADADDR 0x23 /* Header Address not found */#define ERR_CRC 0x24#define ERR_ECC 0x25 /* Uncorrectable ECC error */#define ERR_CRC_UNC 0x26 /* CRC error and uncorrectable error */#define ERR_ILLBSYNC 0x27 /* Illegal block sync error */#define ERR_VDST 0x28 /* VDST not found *//* Timeout errors */#define ERR_READ_TIM 0x31 /* Read timeout error */#define ERR_DEC_STP 0x32 /* Decoder stopped */#define ERR_DEC_TIM 0x33 /* Decoder interrupt timeout error *//* Function abort codes */#define ERR_KEY 0x41 /* Key -Detected abort */#define ERR_READ_FINISH 0x42 /* Read Finish *//* Second Byte diagnostic codes */#define ERR_NOBSYNC 0x01 /* No block sync */#define ERR_SHORTB 0x02 /* Short block */#define ERR_LONGB 0x03 /* Long block */#define ERR_SHORTDSP 0x04 /* Short DSP word */#define ERR_LONGDSP 0x05 /* Long DSP word *//* Status availability flags read from STATUS register */#define FL_EJECT 0x20#define FL_WAIT 0x10 /* active low */#define FL_EOP 0x08 /* active low */#define FL_STEN 0x04 /* Status available when low */#define FL_DTEN 0x02 /* Data available when low */#define FL_DRQ 0x01 /* active low */#define FL_RESET 0xde /* These bits are high after a reset */#define FL_STDT (FL_STEN|FL_DTEN)/* Transfer mode, written to HCON register */#define HCON_DTS 0x08#define HCON_SDRQB 0x04#define HCON_LOHI 0x02#define HCON_DMA16 0x01/* Drive command set, written to COMIN register *//* Quick response commands */#define COMDRVST 0x20 /* Drive Status Read */#define COMERRST 0x21 /* Error Status Read */#define COMIOCTLISTAT 0x22 /* Status Read; reset disk changed bit */#define COMINITSINGLE 0x28 /* Initialize Single Speed */#define COMINITDOUBLE 0x29 /* Initialize Double Speed */#define COMUNLOCK 0x30 /* Unlock */#define COMLOCK 0x31 /* Lock */#define COMLOCKST 0x32 /* Lock/Unlock Status */#define COMVERSION 0x40 /* Get Firmware Revision */#define COMVOIDREADMODE 0x50 /* Void Data Read Mode *//* Read commands */#define COMFETCH 0x60 /* Prefetch Data */#define COMREAD 0x61 /* Read */#define COMREADRAW 0x62 /* Read Raw Data */#define COMREADALL 0x63 /* Read All 2646 Bytes *//* Player control commands */#define COMLEADIN 0x70 /* Seek To Lead-in */#define COMSEEK 0x71 /* Seek */#define COMPAUSEON 0x80 /* Pause On */#define COMPAUSEOFF 0x81 /* Pause Off */#define COMSTOP 0x82 /* Stop */#define COMOPEN 0x90 /* Open Tray Door */#define COMCLOSE 0x91 /* Close Tray Door */#define COMPLAY 0xa0 /* Audio Play */#define COMPLAY_TNO 0xa2 /* Audio Play By Track Number */#define COMSUBQ 0xb0 /* Read Sub-q Code */#define COMLOCATION 0xb1 /* Read Head Position *//* Audio control commands */#define COMCHCTRL 0xc0 /* Audio Channel Control *//* Miscellaneous (test) commands */#define COMDRVTEST 0xd0 /* Write Test Bytes */#define COMTEST 0xd1 /* Diagnostic Test *//* Low level drive interface. Only here we do actual I/O Waiting for status / data available *//* Busy wait until FLAG goes low. Return 0 on timeout. */inline static int flag_low(int flag, unsigned long timeout){ int flag_high; unsigned long count = 0; while ((flag_high = (inb(STATUS_PORT) & flag))) if (++count >= timeout) break; DEBUG((DEBUG_DRIVE_IF, "flag_low 0x%x count %ld%s", flag, count, flag_high ? " timeout" : "")); return !flag_high;}/* Timed waiting for status or data */static int sleep_timeout; /* max # of ticks to sleep */static DECLARE_WAIT_QUEUE_HEAD(waitq);static void sleep_timer(unsigned long data);static struct timer_list delay_timer = {function: sleep_timer};/* Timer routine: wake up when desired flag goes low, or when timeout expires. */static void sleep_timer(unsigned long data){ int flags = inb(STATUS_PORT) & FL_STDT; if (flags == FL_STDT && --sleep_timeout > 0) { mod_timer(&delay_timer, jiffies + HZ/100); /* multi-statement macro */ } else wake_up(&waitq);}/* Sleep until FLAG goes low. Return 0 on timeout or wrong flag low. */static int sleep_flag_low(int flag, unsigned long timeout){ int flag_high; DEBUG((DEBUG_DRIVE_IF, "sleep_flag_low")); sleep_timeout = timeout; flag_high = inb(STATUS_PORT) & flag; if (flag_high && sleep_timeout > 0) { mod_timer(&delay_timer, jiffies + HZ/100); sleep_on(&waitq); flag_high = inb(STATUS_PORT) & flag; } DEBUG((DEBUG_DRIVE_IF, "flag 0x%x count %ld%s", flag, timeout, flag_high ? " timeout" : "")); return !flag_high;}/* Low level drive interface. Only here we do actual I/O Sending commands and parameters *//* Errors in the command protocol */#define ERR_IF_CMD_TIMEOUT 0x100#define ERR_IF_ERR_TIMEOUT 0x101#define ERR_IF_RESP_TIMEOUT 0x102#define ERR_IF_DATA_TIMEOUT 0x103#define ERR_IF_NOSTAT 0x104/* Send command code. Return <0 indicates error */static int send_cmd(int cmd){ unsigned char ack; DEBUG((DEBUG_DRIVE_IF, "sending command 0x%02x\n", cmd)); outb(HCON_DTS, HCON_PORT); /* Enable Suspend Data Transfer */ outb(cmd, COMIN_PORT); /* Send command code */ if (!flag_low(FL_STEN, BUSY_TIMEOUT)) /* Wait for status */ return -ERR_IF_CMD_TIMEOUT; ack = inb(DATA_PORT); /* read command acknowledge */ outb(HCON_SDRQB, HCON_PORT); /* Disable Suspend Data Transfer */ return ack==ST_OP_OK ? 0 : -ack;}/* Send command parameters. Return <0 indicates error */static int send_params(struct cdrom_msf *params){ unsigned char ack; DEBUG((DEBUG_DRIVE_IF, "sending parameters" " %02x:%02x:%02x" " %02x:%02x:%02x", params->cdmsf_min0, params->cdmsf_sec0, params->cdmsf_frame0, params->cdmsf_min1, params->cdmsf_sec1, params->cdmsf_frame1)); outb(params->cdmsf_min0, COMIN_PORT); outb(params->cdmsf_sec0, COMIN_PORT); outb(params->cdmsf_frame0, COMIN_PORT); outb(params->cdmsf_min1, COMIN_PORT); outb(params->cdmsf_sec1, COMIN_PORT); outb(params->cdmsf_frame1, COMIN_PORT); if (!flag_low(FL_STEN, BUSY_TIMEOUT)) /* Wait for status */ return -ERR_IF_CMD_TIMEOUT; ack = inb(DATA_PORT); /* read command acknowledge */ return ack==ST_PA_OK ? 0 : -ack;}/* Send parameters for SEEK command. Return <0 indicates error */static int send_seek_params(struct cdrom_msf *params){ unsigned char ack; DEBUG((DEBUG_DRIVE_IF, "sending seek parameters" " %02x:%02x:%02x", params->cdmsf_min0, params->cdmsf_sec0, params->cdmsf_frame0)); outb(params->cdmsf_min0, COMIN_PORT); outb(params->cdmsf_sec0, COMIN_PORT); outb(params->cdmsf_frame0, COMIN_PORT); if (!flag_low(FL_STEN, BUSY_TIMEOUT)) /* Wait for status */ return -ERR_IF_CMD_TIMEOUT; ack = inb(DATA_PORT); /* read command acknowledge */ return ack==ST_PA_OK ? 0 : -ack;}/* Wait for command execution status. Choice between busy waiting and sleeping. Return value <0 indicates timeout. */inline static int get_exec_status(int busy_waiting){ unsigned char exec_status; if (busy_waiting ? !flag_low(FL_STEN, BUSY_TIMEOUT) : !sleep_flag_low(FL_STEN, SLEEP_TIMEOUT)) return -ERR_IF_CMD_TIMEOUT; exec_status = inb(DATA_PORT); DEBUG((DEBUG_DRIVE_IF, "returned exec status 0x%02x", exec_status)); return exec_status;}/* Wait busy for extra byte of data that a command returns. Return value <0 indicates timeout. */inline static int get_data(int short_timeout){ unsigned char data; if (!flag_low(FL_STEN, short_timeout ? FAST_TIMEOUT : BUSY_TIMEOUT)) return -ERR_IF_DATA_TIMEOUT; data = inb(DATA_PORT); DEBUG((DEBUG_DRIVE_IF, "returned data 0x%02x", data)); return data;}/* Returns 0 if failed */static int reset_drive(void){ unsigned long count = 0; int flags; DEBUG((DEBUG_DRIVE_IF, "reset drive")); outb(0, RESET_PORT); while (++count < RESET_WAIT) inb(DATA_PORT); count = 0; while ((flags = (inb(STATUS_PORT) & FL_RESET)) != FL_RESET) if (++count >= BUSY_TIMEOUT) break; DEBUG((DEBUG_DRIVE_IF, "reset %s", flags == FL_RESET ? "succeeded" : "failed")); if (flags != FL_RESET) return 0; /* Reset failed */ outb(HCON_SDRQB, HCON_PORT); /* Disable Suspend Data Transfer */ return 1; /* Reset succeeded */}/* Facilities for asynchronous operation *//* Read status/data availability flags FL_STEN and FL_DTEN */inline static int stdt_flags(void){ return inb(STATUS_PORT) & FL_STDT;}/* Fetch status that has previously been waited for. <0 means not available */inline static int fetch_status(void){ unsigned char status; if (inb(STATUS_PORT) & FL_STEN) return -ERR_IF_NOSTAT; status = inb(DATA_PORT); DEBUG((DEBUG_DRIVE_IF, "fetched exec status 0x%02x", status)); return status;}/* Fetch data that has previously been waited for. */inline static void fetch_data(char *buf, int n){ insb(DATA_PORT, buf, n); DEBUG((DEBUG_DRIVE_IF, "fetched 0x%x bytes", n));}/* Flush status and data fifos */inline static void flush_data(void){ while ((inb(STATUS_PORT) & FL_STDT) != FL_STDT) inb(DATA_PORT); DEBUG((DEBUG_DRIVE_IF, "flushed fifos"));}/* Command protocol *//* Send a simple command and wait for response. Command codes < COMFETCH are quick response commands */inline static int exec_cmd(int cmd){ int ack = send_cmd(cmd); if (ack < 0) return ack; return get_exec_status(cmd < COMFETCH);}/* Send a command with parameters. Don't wait for the response, * which consists of data blocks read from the CD. */inline static int exec_read_cmd(int cmd, struct cdrom_msf *params){ int ack = send_cmd(cmd); if (ack < 0) return ack; return send_params(params);}/* Send a seek command with parameters and wait for response */inline static int exec_seek_cmd(int cmd, struct cdrom_msf *params){ int ack = send_cmd(cmd); if (ack < 0) return ack; ack = send_seek_params(params); if (ack < 0) return ack; return 0;}/* Send a command with parameters and wait for response */inline static int exec_long_cmd(int cmd, struct cdrom_msf *params){ int ack = exec_read_cmd(cmd, params); if (ack < 0) return ack; return get_exec_status(0);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -