⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sjcd.c

📁 cdrom device drive for linux.
💻 C
📖 第 1 页 / 共 3 页
字号:
/* -- sjcd.c
 *
 *   Sanyo CD-ROM device driver implementation, Version 1.6
 *   Copyright (C) 1995  Vadim V. Model
 *
 *   model@cecmow.enet.dec.com
 *   vadim@rbrf.ru
 *   vadim@ipsun.ras.ru
 *
 *
 *  This driver is based on pre-works by Eberhard Moenkeberg (emoenke@gwdg.de);
 *  it was developed under use of mcd.c from Martin Harriss, with help of
 *  Eric van der Maarel (H.T.M.v.d.Maarel@marin.nl).
 *
 *  It is planned to include these routines into sbpcd.c later - to make
 *  a "mixed use" on one cable possible for all kinds of drives which use
 *  the SoundBlaster/Panasonic style CDROM interface. But today, the
 *  ability to install directly from CDROM is more important than flexibility.
 *
 *  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.
 *
 *  History:
 *  1.1 First public release with kernel version 1.3.7.
 *      Written by Vadim Model.
 *  1.2 Added detection and configuration of cdrom interface
 *      on ISP16 soundcard.
 *      Allow for command line options: sjcd=<io_base>,<irq>,<dma>
 *  1.3 Some minor changes to README.sjcd.
 *  1.4 MSS Sound support!! Listen to a CD through the speakers.
 *  1.5 Module support and bugfixes.
 *      Tray locking.
 *  1.6 Removed ISP16 code from this driver.
 *      Allow only to set io base address on command line: sjcd=<io_base>
 *      Changes to Documentation/cdrom/sjcd
 *      Added cleanup after any error in the initialisation.
 *  1.7 Added code to set the sector size tables to prevent the bug present in 
 *      the previous version of this driver.  Coded added by Anthony Barbachan 
 *      from bugfix tip originally suggested by Alan Cox.
 *
 *  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>
 */

#define SJCD_VERSION_MAJOR 1
#define SJCD_VERSION_MINOR 7

#include <linux/module.h>
#include <linux/errno.h>
#include <linux/sched.h>
#include <linux/mm.h>
#include <linux/timer.h>
#include <linux/fs.h>
#include <linux/kernel.h>
#include <linux/cdrom.h>
#include <linux/ioport.h>
#include <linux/string.h>
#include <linux/major.h>
#include <linux/init.h>
#include <linux/devfs_fs_kernel.h>

#include <asm/system.h>
#include <asm/io.h>
#include <asm/uaccess.h>

#define MAJOR_NR SANYO_CDROM_MAJOR
#include <linux/blk.h>
#include "sjcd.h"

static int sjcd_present = 0;

#define SJCD_BUF_SIZ 32		/* cdr-h94a has internal 64K buffer */

/*
 * buffer for block size conversion
 */
static char sjcd_buf[2048 * SJCD_BUF_SIZ];
static volatile int sjcd_buf_bn[SJCD_BUF_SIZ], sjcd_next_bn;
static volatile int sjcd_buf_in, sjcd_buf_out = -1;

/*
 * Status.
 */
static unsigned short sjcd_status_valid = 0;
static unsigned short sjcd_door_closed;
static unsigned short sjcd_door_was_open;
static unsigned short sjcd_media_is_available;
static unsigned short sjcd_media_is_changed;
static unsigned short sjcd_toc_uptodate = 0;
static unsigned short sjcd_command_failed;
static volatile unsigned char sjcd_completion_status = 0;
static volatile unsigned char sjcd_completion_error = 0;
static unsigned short sjcd_command_is_in_progress = 0;
static unsigned short sjcd_error_reported = 0;

static int sjcd_open_count;

static int sjcd_audio_status;
static struct sjcd_play_msf sjcd_playing;

static int sjcd_base = SJCD_BASE_ADDR;

MODULE_PARM(sjcd_base, "i");

static DECLARE_WAIT_QUEUE_HEAD(sjcd_waitq);

/*
 * Data transfer.
 */
static volatile unsigned short sjcd_transfer_is_active = 0;

enum sjcd_transfer_state {
	SJCD_S_IDLE = 0,
	SJCD_S_START = 1,
	SJCD_S_MODE = 2,
	SJCD_S_READ = 3,
	SJCD_S_DATA = 4,
	SJCD_S_STOP = 5,
	SJCD_S_STOPPING = 6
};
static enum sjcd_transfer_state sjcd_transfer_state = SJCD_S_IDLE;
static long sjcd_transfer_timeout = 0;
static int sjcd_read_count = 0;
static unsigned char sjcd_mode = 0;

#define SJCD_READ_TIMEOUT 5000

#if defined( SJCD_GATHER_STAT )
/*
 * Statistic.
 */
static struct sjcd_stat statistic;
#endif

/*
 * Timer.
 */
static struct timer_list sjcd_delay_timer;

#define SJCD_SET_TIMER( func, tmout )           \
    ( sjcd_delay_timer.expires = jiffies+tmout,         \
      sjcd_delay_timer.function = ( void * )func, \
      add_timer( &sjcd_delay_timer ) )

#define CLEAR_TIMER del_timer( &sjcd_delay_timer )

static int sjcd_cleanup(void);

/*
 * Set up device, i.e., use command line data to set
 * base address.
 */
#ifndef MODULE
static int __init sjcd_setup(char *str)
{
	int ints[2];
	(void) get_options(str, ARRAY_SIZE(ints), ints);
	if (ints[0] > 0)
		sjcd_base = ints[1];

	return 1;
}

__setup("sjcd=", sjcd_setup);

#endif

/*
 * Special converters.
 */
static unsigned char bin2bcd(int bin)
{
	int u, v;

	u = bin % 10;
	v = bin / 10;
	return (u | (v << 4));
}

static int bcd2bin(unsigned char bcd)
{
	return ((bcd >> 4) * 10 + (bcd & 0x0F));
}

static long msf2hsg(struct msf *mp)
{
	return (bcd2bin(mp->frame) + bcd2bin(mp->sec) * 75
		+ bcd2bin(mp->min) * 4500 - 150);
}

static void hsg2msf(long hsg, struct msf *msf)
{
	hsg += 150;
	msf->min = hsg / 4500;
	hsg %= 4500;
	msf->sec = hsg / 75;
	msf->frame = hsg % 75;
	msf->min = bin2bcd(msf->min);	/* convert to BCD */
	msf->sec = bin2bcd(msf->sec);
	msf->frame = bin2bcd(msf->frame);
}

/*
 * Send a command to cdrom. Invalidate status.
 */
static void sjcd_send_cmd(unsigned char cmd)
{
#if defined( SJCD_TRACE )
	printk("SJCD: send_cmd( 0x%x )\n", cmd);
#endif
	outb(cmd, SJCDPORT(0));
	sjcd_command_is_in_progress = 1;
	sjcd_status_valid = 0;
	sjcd_command_failed = 0;
}

/*
 * Send a command with one arg to cdrom. Invalidate status.
 */
static void sjcd_send_1_cmd(unsigned char cmd, unsigned char a)
{
#if defined( SJCD_TRACE )
	printk("SJCD: send_1_cmd( 0x%x, 0x%x )\n", cmd, a);
#endif
	outb(cmd, SJCDPORT(0));
	outb(a, SJCDPORT(0));
	sjcd_command_is_in_progress = 1;
	sjcd_status_valid = 0;
	sjcd_command_failed = 0;
}

/*
 * Send a command with four args to cdrom. Invalidate status.
 */
static void sjcd_send_4_cmd(unsigned char cmd, unsigned char a,
			    unsigned char b, unsigned char c,
			    unsigned char d)
{
#if defined( SJCD_TRACE )
	printk("SJCD: send_4_cmd( 0x%x )\n", cmd);
#endif
	outb(cmd, SJCDPORT(0));
	outb(a, SJCDPORT(0));
	outb(b, SJCDPORT(0));
	outb(c, SJCDPORT(0));
	outb(d, SJCDPORT(0));
	sjcd_command_is_in_progress = 1;
	sjcd_status_valid = 0;
	sjcd_command_failed = 0;
}

/*
 * Send a play or read command to cdrom. Invalidate Status.
 */
static void sjcd_send_6_cmd(unsigned char cmd, struct sjcd_play_msf *pms)
{
#if defined( SJCD_TRACE )
	printk("SJCD: send_long_cmd( 0x%x )\n", cmd);
#endif
	outb(cmd, SJCDPORT(0));
	outb(pms->start.min, SJCDPORT(0));
	outb(pms->start.sec, SJCDPORT(0));
	outb(pms->start.frame, SJCDPORT(0));
	outb(pms->end.min, SJCDPORT(0));
	outb(pms->end.sec, SJCDPORT(0));
	outb(pms->end.frame, SJCDPORT(0));
	sjcd_command_is_in_progress = 1;
	sjcd_status_valid = 0;
	sjcd_command_failed = 0;
}

/*
 * Get a value from the data port. Should not block, so we use a little
 * wait for a while. Returns 0 if OK.
 */
static int sjcd_load_response(void *buf, int len)
{
	unsigned char *resp = (unsigned char *) buf;

	for (; len; --len) {
		int i;
		for (i = 200;
		     i-- && !SJCD_STATUS_AVAILABLE(inb(SJCDPORT(1))););
		if (i > 0)
			*resp++ = (unsigned char) inb(SJCDPORT(0));
		else
			break;
	}
	return (len);
}

/*
 * Load and parse command completion status (drive info byte and maybe error).
 * Sorry, no error classification yet.
 */
static void sjcd_load_status(void)
{
	sjcd_media_is_changed = 0;
	sjcd_completion_error = 0;
	sjcd_completion_status = inb(SJCDPORT(0));
	if (sjcd_completion_status & SST_DOOR_OPENED) {
		sjcd_door_closed = sjcd_media_is_available = 0;
	} else {
		sjcd_door_closed = 1;
		if (sjcd_completion_status & SST_MEDIA_CHANGED)
			sjcd_media_is_available = sjcd_media_is_changed =
			    1;
		else if (sjcd_completion_status & 0x0F) {
			/*
			 * OK, we seem to catch an error ...
			 */
			while (!SJCD_STATUS_AVAILABLE(inb(SJCDPORT(1))));
			sjcd_completion_error = inb(SJCDPORT(0));
			if ((sjcd_completion_status & 0x08) &&
			    (sjcd_completion_error & 0x40))
				sjcd_media_is_available = 0;
			else
				sjcd_command_failed = 1;
		} else
			sjcd_media_is_available = 1;
	}
	/*
	 * Ok, status loaded successfully.
	 */
	sjcd_status_valid = 1, sjcd_error_reported = 0;
	sjcd_command_is_in_progress = 0;

	/*
	 * If the disk is changed, the TOC is not valid.
	 */
	if (sjcd_media_is_changed)
		sjcd_toc_uptodate = 0;
#if defined( SJCD_TRACE )
	printk("SJCD: status %02x.%02x loaded.\n",
	       (int) sjcd_completion_status, (int) sjcd_completion_error);
#endif
}

/*
 * Read status from cdrom. Check to see if the status is available.
 */
static int sjcd_check_status(void)
{
	/*
	 * Try to load the response from cdrom into buffer.
	 */
	if (SJCD_STATUS_AVAILABLE(inb(SJCDPORT(1)))) {
		sjcd_load_status();
		return (1);
	} else {
		/*
		 * No status is available.
		 */
		return (0);
	}
}

/*
 * This is just timeout counter, and nothing more. Surprised ? :-)
 */
static volatile long sjcd_status_timeout;

/*
 * We need about 10 seconds to wait. The longest command takes about 5 seconds
 * to probe the disk (usually after tray closed or drive reset). Other values
 * should be thought of for other commands.
 */
#define SJCD_WAIT_FOR_STATUS_TIMEOUT 1000

static void sjcd_status_timer(void)
{
	if (sjcd_check_status()) {
		/*
		 * The command completed and status is loaded, stop waiting.
		 */
		wake_up(&sjcd_waitq);
	} else if (--sjcd_status_timeout <= 0) {
		/*
		 * We are timed out. 
		 */
		wake_up(&sjcd_waitq);
	} else {
		/*
		 * We have still some time to wait. Try again.
		 */
		SJCD_SET_TIMER(sjcd_status_timer, 1);
	}
}

/*
 * Wait for status for 10 sec approx. Returns non-positive when timed out.
 * Should not be used while reading data CDs.
 */
static int sjcd_wait_for_status(void)
{
	sjcd_status_timeout = SJCD_WAIT_FOR_STATUS_TIMEOUT;
	SJCD_SET_TIMER(sjcd_status_timer, 1);
	sleep_on(&sjcd_waitq);
#if defined( SJCD_DIAGNOSTIC ) || defined ( SJCD_TRACE )
	if (sjcd_status_timeout <= 0)
		printk("SJCD: Error Wait For Status.\n");
#endif
	return (sjcd_status_timeout);
}

static int sjcd_receive_status(void)
{
	int i;
#if defined( SJCD_TRACE )
	printk("SJCD: receive_status\n");
#endif
	/*
	 * Wait a bit for status available.
	 */
	for (i = 200; i-- && (sjcd_check_status() == 0););
	if (i < 0) {
#if defined( SJCD_TRACE )
		printk("SJCD: long wait for status\n");
#endif
		if (sjcd_wait_for_status() <= 0)
			printk("SJCD: Timeout when read status.\n");
		else
			i = 0;
	}
	return (i);
}

/*
 * Load the status. Issue get status command and wait for status available.
 */
static void sjcd_get_status(void)
{
#if defined( SJCD_TRACE )
	printk("SJCD: get_status\n");
#endif
	sjcd_send_cmd(SCMD_GET_STATUS);
	sjcd_receive_status();
}

/*
 * Check the drive if the disk is changed. Should be revised.
 */
static int sjcd_disk_change(kdev_t full_dev)
{
#if 0
	printk("SJCD: sjcd_disk_change( 0x%x )\n", full_dev);
#endif
	if (MINOR(full_dev) > 0) {
		printk("SJCD: request error: invalid device minor.\n");
		return 0;
	}
	if (!sjcd_command_is_in_progress)
		sjcd_get_status();
	return (sjcd_status_valid ? sjcd_media_is_changed : 0);
}

/*
 * Read the table of contents (TOC) and TOC header if necessary.
 * We assume that the drive contains no more than 99 toc entries.
 */
static struct sjcd_hw_disk_info sjcd_table_of_contents[SJCD_MAX_TRACKS];
static unsigned char sjcd_first_track_no, sjcd_last_track_no;
#define sjcd_disk_length  sjcd_table_of_contents[0].un.track_msf

static int sjcd_update_toc(void)
{
	struct sjcd_hw_disk_info info;
	int i;
#if defined( SJCD_TRACE )
	printk("SJCD: update toc:\n");
#endif
	/*
	 * check to see if we need to do anything
	 */
	if (sjcd_toc_uptodate)
		return (0);

	/*
	 * Get the TOC start information.
	 */
	sjcd_send_1_cmd(SCMD_GET_DISK_INFO, SCMD_GET_1_TRACK);
	sjcd_receive_status();

	if (!sjcd_status_valid) {
		printk("SJCD: cannot load status.\n");
		return (-1);
	}

	if (!sjcd_media_is_available) {
		printk("SJCD: no disk in drive\n");
		return (-1);
	}

	if (!sjcd_command_failed) {
		if (sjcd_load_response(&info, sizeof(info)) != 0) {
			printk
			    ("SJCD: cannot load response about TOC start.\n");
			return (-1);
		}
		sjcd_first_track_no = bcd2bin(info.un.track_no);
	} else {
		printk("SJCD: get first failed\n");
		return (-1);
	}
#if defined( SJCD_TRACE )
	printk("SJCD: TOC start 0x%02x ", sjcd_first_track_no);
#endif
	/*
	 * Get the TOC finish information.
	 */
	sjcd_send_1_cmd(SCMD_GET_DISK_INFO, SCMD_GET_L_TRACK);
	sjcd_receive_status();

	if (!sjcd_status_valid) {
		printk("SJCD: cannot load status.\n");
		return (-1);
	}

	if (!sjcd_media_is_available) {
		printk("SJCD: no disk in drive\n");
		return (-1);
	}

	if (!sjcd_command_failed) {
		if (sjcd_load_response(&info, sizeof(info)) != 0) {
			printk
			    ("SJCD: cannot load response about TOC finish.\n");
			return (-1);
		}
		sjcd_last_track_no = bcd2bin(info.un.track_no);
	} else {
		printk("SJCD: get last failed\n");
		return (-1);
	}
#if defined( SJCD_TRACE )
	printk("SJCD: TOC finish 0x%02x ", sjcd_last_track_no);
#endif
	for (i = sjcd_first_track_no; i <= sjcd_last_track_no; i++) {
		/*
		 * Get the first track information.
		 */
		sjcd_send_1_cmd(SCMD_GET_DISK_INFO, bin2bcd(i));
		sjcd_receive_status();

		if (!sjcd_status_valid) {
			printk("SJCD: cannot load status.\n");
			return (-1);
		}

		if (!sjcd_media_is_available) {
			printk("SJCD: no disk in drive\n");
			return (-1);
		}

		if (!sjcd_command_failed) {
			if (sjcd_load_response(&sjcd_table_of_contents[i],
					       sizeof(struct
						      sjcd_hw_disk_info))
			    != 0) {
				printk
				    ("SJCD: cannot load info for %d track\n",
				     i);
				return (-1);
			}
		} else {
			printk("SJCD: get info %d failed\n", i);
			return (-1);
		}
	}

	/*
	 * Get the disk length info.
	 */
	sjcd_send_1_cmd(SCMD_GET_DISK_INFO, SCMD_GET_D_SIZE);
	sjcd_receive_status();

	if (!sjcd_status_valid) {
		printk("SJCD: cannot load status.\n");
		return (-1);
	}

	if (!sjcd_media_is_available) {
		printk("SJCD: no disk in drive\n");
		return (-1);
	}

	if (!sjcd_command_failed) {
		if (sjcd_load_response(&info, sizeof(info)) != 0) {
			printk
			    ("SJCD: cannot load response about disk size.\n");
			return (-1);
		}
		sjcd_disk_length.min = info.un.track_msf.min;
		sjcd_disk_length.sec = info.un.track_msf.sec;
		sjcd_disk_length.frame = info.un.track_msf.frame;
	} else {
		printk("SJCD: get size failed\n");
		return (1);
	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -