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

📄 xd.c

📁 Linux内核源代码 为压缩文件 是<<Linux内核>>一书中的源代码
💻 C
📖 第 1 页 / 共 3 页
字号:
/* * This file contains the driver for an XT hard disk controller * (at least the DTC 5150X) for Linux. * * Author: Pat Mackinlay, pat@it.com.au * Date: 29/09/92 *  * Revised: 01/01/93, ... * * Ref: DTC 5150X Controller Specification (thanks to Kevin Fowler, *   kevinf@agora.rain.com) * Also thanks to: Salvador Abreu, Dave Thaler, Risto Kankkunen and *   Wim Van Dorst. * * Revised: 04/04/94 by Risto Kankkunen *   Moved the detection code from xd_init() to xd_geninit() as it needed *   interrupts enabled and Linus didn't want to enable them in that first *   phase. xd_geninit() is the place to do these kinds of things anyway, *   he says. * * Modularized: 04/10/96 by Todd Fries, tfries@umr.edu * * Revised: 13/12/97 by Andrzej Krzysztofowicz, ankry@mif.pg.gda.pl *   Fixed some problems with disk initialization and module initiation. *   Added support for manual geometry setting (except Seagate controllers) *   in form: *      xd_geo=<cyl_xda>,<head_xda>,<sec_xda>[,<cyl_xdb>,<head_xdb>,<sec_xdb>] *   Recovered DMA access. Abridged messages. Added support for DTC5051CX, *   WD1002-27X & XEBEC controllers. Driver uses now some jumper settings. *   Extended ioctl() support. */#include <linux/module.h>#include <linux/errno.h>#include <linux/sched.h>#include <linux/mm.h>#include <linux/fs.h>#include <linux/kernel.h>#include <linux/timer.h>#include <linux/genhd.h>#include <linux/hdreg.h>#include <linux/ioport.h>#include <linux/init.h>#include <linux/devfs_fs_kernel.h>#include <asm/system.h>#include <asm/io.h>#include <asm/uaccess.h>#include <asm/dma.h>#define MAJOR_NR XT_DISK_MAJOR#include <linux/blk.h>#include <linux/blkpg.h>#include "xd.h"#define XD_DONT_USE_DMA		0  /* Initial value. may be overriden using				      "nodma" module option */#define XD_INIT_DISK_DELAY	(30*HZ/1000)  /* 30 ms delay during disk initialization *//* Above may need to be increased if a problem with the 2nd drive detection   (ST11M controller) or resetting a controler (WD) appears */XD_INFO xd_info[XD_MAXDRIVES];/* If you try this driver and find that your card is not detected by the driver at bootup, you need to add your BIOS   signature and details to the following list of signatures. A BIOS signature is a string embedded into the first   few bytes of your controller's on-board ROM BIOS. To find out what yours is, use something like MS-DOS's DEBUG   command. Run DEBUG, and then you can examine your BIOS signature with:	d xxxx:0000   where xxxx is the segment of your controller (like C800 or D000 or something). On the ASCII dump at the right, you should   be able to see a string mentioning the manufacturer's copyright etc. Add this string into the table below. The parameters   in the table are, in order:	offset			; this is the offset (in bytes) from the start of your ROM where the signature starts	signature		; this is the actual text of the signature	xd_?_init_controller	; this is the controller init routine used by your controller	xd_?_init_drive		; this is the drive init routine used by your controller   The controllers directly supported at the moment are: DTC 5150x, WD 1004A27X, ST11M/R and override. If your controller is   made by the same manufacturer as one of these, try using the same init routines as they do. If that doesn't work, your   best bet is to use the "override" routines. These routines use a "portable" method of getting the disk's geometry, and   may work with your card. If none of these seem to work, try sending me some email and I'll see what I can do <grin>.   NOTE: You can now specify your XT controller's parameters from the command line in the form xd=TYPE,IRQ,IO,DMA. The driver   should be able to detect your drive's geometry from this info. (eg: xd=0,5,0x320,3 is the "standard"). */#include <asm/page.h>#define xd_dma_mem_alloc(size) __get_dma_pages(GFP_KERNEL,get_order(size))#define xd_dma_mem_free(addr, size) free_pages(addr, get_order(size))static char *xd_dma_buffer = 0;static XD_SIGNATURE xd_sigs[] __initdata = {	{ 0x0000,"Override geometry handler",NULL,xd_override_init_drive,"n unknown" }, /* Pat Mackinlay, pat@it.com.au */	{ 0x0008,"[BXD06 (C) DTC 17-MAY-1985]",xd_dtc_init_controller,xd_dtc5150cx_init_drive," DTC 5150CX" }, /* Andrzej Krzysztofowicz, ankry@mif.pg.gda.pl */	{ 0x000B,"CRD18A   Not an IBM rom. (C) Copyright Data Technology Corp. 05/31/88",xd_dtc_init_controller,xd_dtc_init_drive," DTC 5150X" }, /* Todd Fries, tfries@umr.edu */	{ 0x000B,"CXD23A Not an IBM ROM (C)Copyright Data Technology Corp 12/03/88",xd_dtc_init_controller,xd_dtc_init_drive," DTC 5150X" }, /* Pat Mackinlay, pat@it.com.au */	{ 0x0008,"07/15/86(C) Copyright 1986 Western Digital Corp.",xd_wd_init_controller,xd_wd_init_drive," Western Dig. 1002-27X" }, /* Andrzej Krzysztofowicz, ankry@mif.pg.gda.pl */	{ 0x0008,"06/24/88(C) Copyright 1988 Western Digital Corp.",xd_wd_init_controller,xd_wd_init_drive," Western Dig. WDXT-GEN2" }, /* Dan Newcombe, newcombe@aa.csc.peachnet.edu */	{ 0x0015,"SEAGATE ST11 BIOS REVISION",xd_seagate_init_controller,xd_seagate_init_drive," Seagate ST11M/R" }, /* Salvador Abreu, spa@fct.unl.pt */	{ 0x0010,"ST11R BIOS",xd_seagate_init_controller,xd_seagate_init_drive," Seagate ST11M/R" }, /* Risto Kankkunen, risto.kankkunen@cs.helsinki.fi */	{ 0x0010,"ST11 BIOS v1.7",xd_seagate_init_controller,xd_seagate_init_drive," Seagate ST11R" }, /* Alan Hourihane, alanh@fairlite.demon.co.uk */	{ 0x1000,"(c)Copyright 1987 SMS",xd_omti_init_controller,xd_omti_init_drive,"n OMTI 5520" }, /* Dirk Melchers, dirk@merlin.nbg.sub.org */	{ 0x0006,"COPYRIGHT XEBEC (C) 1984",xd_xebec_init_controller,xd_xebec_init_drive," XEBEC" }, /* Andrzej Krzysztofowicz, ankry@mif.pg.gda.pl */};static unsigned int xd_bases[] __initdata ={	0xC8000, 0xCA000, 0xCC000,	0xCE000, 0xD0000, 0xD2000,	0xD4000, 0xD6000, 0xD8000,	0xDA000, 0xDC000, 0xDE000,	0xE0000};static struct hd_struct xd_struct[XD_MAXDRIVES << 6];static int xd_sizes[XD_MAXDRIVES << 6], xd_access[XD_MAXDRIVES];static int xd_blocksizes[XD_MAXDRIVES << 6];extern struct block_device_operations xd_fops;static struct gendisk xd_gendisk = {	MAJOR_NR,	/* Major number */	"xd",		/* Major name */	6,		/* Bits to shift to get real from partition */	1 << 6,		/* Number of partitions per real */	xd_struct,	/* hd struct */	xd_sizes,	/* block sizes */	0,		/* number */	(void *) xd_info,	/* internal */	NULL,		/* next */	&xd_fops,	/* file operations */};static struct block_device_operations xd_fops = {	open:		xd_open,	release:	xd_release,	ioctl:		xd_ioctl,};static DECLARE_WAIT_QUEUE_HEAD(xd_wait_int);static DECLARE_WAIT_QUEUE_HEAD(xd_wait_open);static u_char xd_valid[XD_MAXDRIVES] = { 0,0 };static u_char xd_drives, xd_irq = 5, xd_dma = 3, xd_maxsectors;static u_char xd_override __initdata = 0, xd_type __initdata = 0;static u_short xd_iobase = 0x320;static int xd_geo[XD_MAXDRIVES*3] __initdata = { 0, };static volatile int xdc_busy;static DECLARE_WAIT_QUEUE_HEAD(xdc_wait);static struct timer_list xd_timer, xd_watchdog_int;static volatile u_char xd_error;static int nodma = XD_DONT_USE_DMA;static devfs_handle_t devfs_handle = NULL;/* xd_init: register the block device number and set up pointer tables */int __init xd_init (void){	init_timer (&xd_timer); xd_timer.function = xd_wakeup;	init_timer (&xd_watchdog_int); xd_watchdog_int.function = xd_watchdog;	if (devfs_register_blkdev(MAJOR_NR,"xd",&xd_fops)) {		printk("xd: Unable to get major number %d\n",MAJOR_NR);		return -1;	}	devfs_handle = devfs_mk_dir (NULL, xd_gendisk.major_name, NULL);	blk_init_queue(BLK_DEFAULT_QUEUE(MAJOR_NR), DEVICE_REQUEST);	read_ahead[MAJOR_NR] = 8;	/* 8 sector (4kB) read ahead */	xd_gendisk.next = gendisk_head;	gendisk_head = &xd_gendisk;	xd_geninit();	return 0;}/* xd_detect: scan the possible BIOS ROM locations for the signature strings */static u_char __init xd_detect (u_char *controller, unsigned int *address){	u_char i,j,found = 0;	if (xd_override)	{		*controller = xd_type;		*address = 0;		return(1);	}	for (i = 0; i < (sizeof(xd_bases) / sizeof(xd_bases[0])) && !found; i++)		for (j = 1; j < (sizeof(xd_sigs) / sizeof(xd_sigs[0])) && !found; j++)			if (isa_check_signature(xd_bases[i] + xd_sigs[j].offset,xd_sigs[j].string,strlen(xd_sigs[j].string))) {				*controller = j;				xd_type = j;				*address = xd_bases[i];				found++;			}	return (found);}/* xd_geninit: grab the IRQ and DMA channel, initialise the drives *//* and set up the "raw" device entries in the table */static void __init xd_geninit (void){	u_char i,controller;	unsigned int address;	for(i=0;i<(XD_MAXDRIVES << 6);i++) xd_blocksizes[i] = 1024;	blksize_size[MAJOR_NR] = xd_blocksizes;	if (xd_detect(&controller,&address)) {		printk("Detected a%s controller (type %d) at address %06x\n",			xd_sigs[controller].name,controller,address);		if (check_region(xd_iobase,4)) {			printk("xd: Ports at 0x%x are not available\n",				xd_iobase);			return;		}		request_region(xd_iobase,4,"xd");		if (controller)			xd_sigs[controller].init_controller(address);		xd_drives = xd_initdrives(xd_sigs[controller].init_drive);				printk("Detected %d hard drive%s (using IRQ%d & DMA%d)\n",			xd_drives,xd_drives == 1 ? "" : "s",xd_irq,xd_dma);		for (i = 0; i < xd_drives; i++)			printk(" xd%c: CHS=%d/%d/%d\n",'a'+i,				xd_info[i].cylinders,xd_info[i].heads,				xd_info[i].sectors);	}	if (xd_drives) {		if (!request_irq(xd_irq,xd_interrupt_handler, 0, "XT hard disk", NULL)) {			if (request_dma(xd_dma,"xd")) {				printk("xd: unable to get DMA%d\n",xd_dma);				free_irq(xd_irq, NULL);			}		}		else			printk("xd: unable to get IRQ%d\n",xd_irq);	}	for (i = 0; i < xd_drives; i++) {		xd_valid[i] = 1;		register_disk(&xd_gendisk, MKDEV(MAJOR_NR,i<<6), 1<<6, &xd_fops,				xd_info[i].heads * xd_info[i].cylinders *				xd_info[i].sectors);	}	xd_gendisk.nr_real = xd_drives;}/* xd_open: open a device */static int xd_open (struct inode *inode,struct file *file){	int dev = DEVICE_NR(inode->i_rdev);	MOD_INC_USE_COUNT;	if (dev < xd_drives) {		while (!xd_valid[dev])			sleep_on(&xd_wait_open);		xd_access[dev]++;		return (0);	}	MOD_DEC_USE_COUNT;	return -ENXIO;}/* do_xd_request: handle an incoming request */static void do_xd_request (request_queue_t * q){	u_int block,count,retry;	int code;	sti();	if (xdc_busy)		return;	while (code = 0, !QUEUE_EMPTY) {		INIT_REQUEST;	/* do some checking on the request structure */		if (CURRENT_DEV < xd_drives		    && CURRENT->sector + CURRENT->nr_sectors		         <= xd_struct[MINOR(CURRENT->rq_dev)].nr_sects) {			block = CURRENT->sector + xd_struct[MINOR(CURRENT->rq_dev)].start_sect;			count = CURRENT->nr_sectors;			switch (CURRENT->cmd) {				case READ:				case WRITE:					for (retry = 0; (retry < XD_RETRIES) && !code; retry++)						code = xd_readwrite(CURRENT->cmd,CURRENT_DEV,CURRENT->buffer,block,count);					break;				default:					printk("do_xd_request: unknown request\n");					break;			}		}		end_request(code);	/* wrap up, 0 = fail, 1 = success */	}}/* xd_ioctl: handle device ioctl's */static int xd_ioctl (struct inode *inode,struct file *file,u_int cmd,u_long arg){	int dev;	if ((!inode) || !(inode->i_rdev))		return -EINVAL; 	dev = DEVICE_NR(inode->i_rdev);	if (dev >= xd_drives) return -EINVAL;	switch (cmd) {		case HDIO_GETGEO:		{			struct hd_geometry g;			struct hd_geometry *geometry = (struct hd_geometry *) arg;			if (!geometry) return -EINVAL;			g.heads = xd_info[dev].heads;			g.sectors = xd_info[dev].sectors;			g.cylinders = xd_info[dev].cylinders;			g.start = xd_struct[MINOR(inode->i_rdev)].start_sect;			return copy_to_user(geometry, &g, sizeof g) ? -EFAULT : 0;		}		case BLKGETSIZE:			if (!arg) return -EINVAL;			return put_user(xd_struct[MINOR(inode->i_rdev)].nr_sects,(long *) arg);		case HDIO_SET_DMA:			if (!capable(CAP_SYS_ADMIN)) return -EACCES;			if (xdc_busy) return -EBUSY;			nodma = !arg;			if (nodma && xd_dma_buffer) {				xd_dma_mem_free((unsigned long)xd_dma_buffer, xd_maxsectors * 0x200);				xd_dma_buffer = 0;			}			return 0;		case HDIO_GET_DMA:			return put_user(!nodma, (long *) arg);		case HDIO_GET_MULTCOUNT:			return put_user(xd_maxsectors, (long *) arg);		case BLKRRPART:			if (!capable(CAP_SYS_ADMIN)) 				return -EACCES;			return xd_reread_partitions(inode->i_rdev);		case BLKFLSBUF:		case BLKROSET:		case BLKROGET:		case BLKRASET:		case BLKRAGET:		case BLKPG:			return blk_ioctl(inode->i_rdev, cmd, arg);		default:			return -EINVAL;	}}/* xd_release: release the device */static int xd_release (struct inode *inode, struct file *file){	int target = DEVICE_NR(inode->i_rdev);	if (target < xd_drives) {		xd_access[target]--;#ifdef MODULE		MOD_DEC_USE_COUNT;#endif /* MODULE */	}	return 0;}/* xd_reread_partitions: rereads the partition table from a drive */static int xd_reread_partitions(kdev_t dev){	int target;	int start;	int partition;		target = DEVICE_NR(dev); 	start = target << xd_gendisk.minor_shift;	cli();	xd_valid[target] = (xd_access[target] != 1);        sti();	if (xd_valid[target])		return -EBUSY;	for (partition = xd_gendisk.max_p - 1; partition >= 0; partition--) {		int minor = (start | partition);		kdev_t devp = MKDEV(MAJOR_NR, minor);		struct super_block * sb = get_super(devp);				sync_dev(devp);		if (sb)			invalidate_inodes(sb);		invalidate_buffers(devp);		xd_gendisk.part[minor].start_sect = 0;

⌨️ 快捷键说明

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