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

📄 ftl_cs.c

📁 pcmcia source code
💻 C
📖 第 1 页 / 共 3 页
字号:
/*======================================================================    A Flash Translation Layer memory card driver    This driver implements a disk-like block device driver with an    apparent block size of 512 bytes for flash memory cards.    ftl_cs.c 1.75 2002/06/29 06:27:37    The contents of this file are subject to the Mozilla Public    License Version 1.1 (the "License"); you may not use this file    except in compliance with the License. You may obtain a copy of    the License at http://www.mozilla.org/MPL/    Software distributed under the License is distributed on an "AS    IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or    implied. See the License for the specific language governing    rights and limitations under the License.    The initial developer of the original code is David A. Hinds    <dahinds@users.sourceforge.net>.  Portions created by David A. Hinds    are Copyright (C) 1999 David A. Hinds.  All Rights Reserved.    Alternatively, the contents of this file may be used under the    terms of the GNU General Public License version 2 (the "GPL"), in    which case the provisions of the GPL are applicable instead of the    above.  If you wish to allow the use of your version of this file    only under the terms of the GPL and not to allow others to use    your version of this file under the MPL, indicate your decision    by deleting the provisions above and replace them with the notice    and other provisions required by the GPL.  If you do not delete    the provisions above, a recipient may use your version of this    file under either the MPL or the GPL.    LEGAL NOTE: The FTL format is patented by M-Systems.  They have    granted a license for its use with PCMCIA devices:     "M-Systems grants a royalty-free, non-exclusive license under      any presently existing M-Systems intellectual property rights      necessary for the design and development of FTL-compatible      drivers, file systems and utilities using the data formats with      PCMCIA PC Cards as described in the PCMCIA Flash Translation      Layer (FTL) Specification."    Use of the FTL format for non-PCMCIA applications may be an    infringement of these patents.  For additional information,    contact M-Systems (http://www.m-sys.com) directly.      ======================================================================*//* #define PSYCHO_DEBUG */#include <linux/kernel.h>#include <linux/module.h>#include <linux/init.h>#include <linux/sched.h>#include <linux/ptrace.h>#include <linux/slab.h>#include <linux/string.h>#include <linux/timer.h>#include <linux/major.h>#include <linux/fs.h>#include <linux/ioctl.h>#include <linux/hdreg.h>#include <linux/vmalloc.h>#include <linux/blkpg.h>#include <asm/io.h>#include <asm/system.h>#include <asm/uaccess.h>#include <stdarg.h>#include <pcmcia/version.h>#include <pcmcia/cs_types.h>#include <pcmcia/cs.h>#include <pcmcia/bulkmem.h>#include <pcmcia/cistpl.h>#include <pcmcia/ds.h>#include <pcmcia/ftl.h>/*====================================================================*//* Module parameters */MODULE_AUTHOR("David Hinds <dahinds@users.sourceforge.net>");MODULE_DESCRIPTION("PCMCIA Flash Translation Layer driver");MODULE_LICENSE("Dual MPL/GPL");#define INT_MODULE_PARM(n, v) static int n = v; MODULE_PARM(n, "i")INT_MODULE_PARM(major_dev, 0);		/* major device # for FTL */INT_MODULE_PARM(shuffle_freq, 50);#ifdef PCMCIA_DEBUGINT_MODULE_PARM(pc_debug, PCMCIA_DEBUG);#define DEBUG(n, args...) if (pc_debug>(n)) printk(KERN_DEBUG args)static char *version ="ftl_cs.c 1.75 2002/06/29 06:27:37 (David Hinds)";#else#define DEBUG(n, args...)#endif/*====================================================================*//* Funky stuff for setting up a block device */#define MAJOR_NR		major_dev#define DEVICE_NAME		"ftl"#define DEVICE_REQUEST		do_ftl_request#define DEVICE_ON(device)#define DEVICE_OFF(device)#define DEVICE_NR(minor)	((minor)>>5)#define REGION_NR(minor)	(((minor)>>3)&3)#define PART_NR(minor)		((minor)&7)#define MINOR_NR(dev,reg,part)	(((dev)<<5)+((reg)<<3)+(part))#include <linux/blk.h>/*====================================================================*//* Maximum number of separate memory devices we'll allow */#define MAX_DEV		4/* Maximum number of regions per device */#define MAX_REGION	4/* Maximum number of partitions in an FTL region */#define PART_BITS	3#define MAX_PART	8/* Maximum number of outstanding erase requests per socket */#define MAX_ERASE	8/* Sector size -- shouldn't need to change */#define SECTOR_SIZE	512static void ftl_config(dev_link_t *link);static void ftl_release(u_long arg);static int ftl_event(event_t event, int priority,		     event_callback_args_t *args);static dev_link_t *ftl_attach(void);static void ftl_detach(dev_link_t *);/* Each memory region corresponds to a minor device */typedef struct partition_t {    dev_node_t		dev;    u_int		state;    u_int		*VirtualBlockMap;    u_int		*VirtualPageMap;    u_int		FreeTotal;    struct eun_info_t {	u_int			Offset;	u_int			EraseCount;	u_int			Free;	u_int			Deleted;    } *EUNInfo;    struct xfer_info_t {	u_int			Offset;	u_int			EraseCount;	u_short			state;    } *XferInfo;    u_short		bam_index;    u_int		*bam_cache;    u_short		DataUnits;    u_int		BlocksPerUnit;    erase_unit_header_t	header;    region_info_t	region;    memory_handle_t	handle;    int			open;    int			locked;} partition_t;/* Partition state flags */#define FTL_FORMATTED	0x01/* Transfer unit states */#define XFER_UNKNOWN	0x00#define XFER_ERASING	0x01#define XFER_ERASED	0x02#define XFER_PREPARED	0x03#define XFER_FAILED	0x04typedef struct ftl_dev_t {    dev_link_t		link;    eraseq_handle_t	eraseq_handle;    eraseq_entry_t	eraseq[MAX_ERASE];    wait_queue_head_t	erase_pending;    partition_t		minor[CISTPL_MAX_DEVICES];} ftl_dev_t;static dev_info_t dev_info = "ftl_cs";static dev_link_t *dev_table[MAX_DEV] = { NULL, /* ... */ };static struct hd_struct ftl_hd[MINOR_NR(MAX_DEV, 0, 0)];static int ftl_sizes[MINOR_NR(MAX_DEV, 0, 0)];static int ftl_blocksizes[MINOR_NR(MAX_DEV, 0, 0)];static wait_queue_head_t ftl_wait_open;static struct gendisk ftl_gendisk = {    major:		0,    major_name:		"ftl",    minor_shift:	PART_BITS,    max_p:		MAX_PART,#if (LINUX_VERSION_CODE < VERSION(2,3,40))    max_nr:		MAX_DEV*MAX_PART,#endif    part:		ftl_hd,    sizes:		ftl_sizes,    nr_real:		MAX_DEV*MAX_PART};/*====================================================================*/static int ftl_ioctl(struct inode *inode, struct file *file,		     u_int cmd, u_long arg);static int ftl_open(struct inode *inode, struct file *file);static FS_RELEASE_T ftl_close(struct inode *inode, struct file *file);static int ftl_reread_partitions(int minor);static struct block_device_operations ftl_blk_fops = {    open:	ftl_open,    release:	ftl_close,    ioctl:	ftl_ioctl,#ifdef block_device_operations    read:	block_read,    write:	block_write,    fsync:	block_fsync#endif};/*====================================================================*/static void cs_error(int func, int ret){    int i;    error_info_t err = { func, ret };        for (i = 0; i < MAX_DEV; i++)	if (dev_table[i] != NULL) break;    CardServices(ReportError, dev_table[i]->handle, &err);}/*======================================================================    ftl_attach() creates an "instance" of the driver, allocating    local data structures for one device.  The device is registered    with Card Services.======================================================================*/static dev_link_t *ftl_attach(void){    client_reg_t client_reg;    dev_link_t *link;    ftl_dev_t *dev;    eraseq_hdr_t eraseq_hdr;    int i, ret;        DEBUG(0, "ftl_cs: ftl_attach()\n");    for (i = 0; i < MAX_DEV; i++)	if (dev_table[i] == NULL) break;    if (i == MAX_DEV) {	printk(KERN_NOTICE "ftl_cs: no devices available\n");	return NULL;    }        /* Create new memory card device */    dev = kmalloc(sizeof(*dev), GFP_KERNEL);    if (!dev) return NULL;    memset(dev, 0, sizeof(*dev));    link = &dev->link; link->priv = dev;    link->release.function = &ftl_release;    link->release.data = (u_long)link;    dev_table[i] = link;    init_waitqueue_head(&dev->erase_pending);    /* Register with Card Services */    client_reg.dev_info = &dev_info;    client_reg.Attributes = INFO_IO_CLIENT | INFO_CARD_SHARE;    client_reg.EventMask =	CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET |	CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL |	CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME;    client_reg.event_handler = &ftl_event;    client_reg.Version = 0x0210;    client_reg.event_callback_args.client_data = link;    ret = CardServices(RegisterClient, &link->handle, &client_reg);    if (ret != CS_SUCCESS) {	cs_error(RegisterClient, ret);	ftl_detach(link);	return NULL;    }    for (i = 0; i < MAX_ERASE; i++)	dev->eraseq[i].State = ERASE_IDLE;    eraseq_hdr.QueueEntryCnt = MAX_ERASE;    eraseq_hdr.QueueEntryArray = dev->eraseq;    dev->eraseq_handle = (void *)link->handle;    ret = CardServices(RegisterEraseQueue, &dev->eraseq_handle, &eraseq_hdr);    if (ret != CS_SUCCESS) {	cs_error(RegisterEraseQueue, ret);	dev->eraseq_handle = NULL;	ftl_detach(link);	return NULL;    }        return link;} /* ftl_attach *//*======================================================================    This deletes a driver "instance".  The device is de-registered    with Card Services.  If it has been released, all local data    structures are freed.  Otherwise, the structures will be freed    when the device is released.======================================================================*/static void ftl_detach(dev_link_t *link){    ftl_dev_t *dev = link->priv;    int i;    DEBUG(0, "ftl_cs: ftl_detach(0x%p)\n", link);        /* Locate device structure */    for (i = 0; i < MAX_DEV; i++)	if (dev_table[i] == link) break;    if (i == MAX_DEV)	return;    del_timer(&link->release);    if (link->state & DEV_CONFIG) {	ftl_release((u_long)link);	if (link->state & DEV_STALE_CONFIG) {	    link->state |= DEV_STALE_LINK;	    return;	}    }    if (dev->eraseq_handle)	CardServices(DeregisterEraseQueue, dev->eraseq_handle);    if (link->handle)	CardServices(DeregisterClient, link->handle);        /* Unlink device structure, free bits */    dev_table[i] = NULL;    kfree(dev);    } /* ftl_detach *//*======================================================================    ftl_config() is scheduled to run after a CARD_INSERTION event    is received, to configure the PCMCIA socket, and to make the    ethernet device available to the system.    ======================================================================*/static void ftl_config(dev_link_t *link){    ftl_dev_t *dev = link->priv;    partition_t *minor;    region_info_t region;    dev_node_t **tail;    int i, ret, nr;    DEBUG(0, "ftl_cs: ftl_config(0x%p)\n", link);    /* Configure card */    link->state |= DEV_CONFIG;    for (i = 0; i < MAX_DEV; i++)	if (dev_table[i] == link) break;    tail = &link->dev;    minor = dev->minor;    nr = 0;    region.Attributes = REGION_TYPE_CM;    ret = CardServices(GetFirstRegion, link->handle, &region);    while (ret == CS_SUCCESS) {	minor->region = region;	sprintf(minor->dev.dev_name, "ftl%dc%d", i, nr);	minor->dev.major = major_dev;	minor->dev.minor = MINOR_NR(i, nr, 0);	*tail = &minor->dev; tail = &minor->dev.next;	minor++; nr++;	ret = CardServices(GetNextRegion, link->handle, &region);    }    *tail = NULL;        if (nr == 0)	printk(KERN_NOTICE "ftl_cs: no regions found!\n");    else {	printk(KERN_INFO "ftl_cs: ftl%d:", i);	minor = dev->minor;	for (i = 0; i < nr; i++) {	    if (minor[i].region.RegionSize & 0xfffff)		printk(" %u kb", minor[i].region.RegionSize >> 10);	    else		printk(" %u mb", minor[i].region.RegionSize >> 20);	}	printk("\n");    }    link->state &= ~DEV_CONFIG_PENDING;    } /* ftl_config *//*======================================================================    After a card is removed, ftl_release() will unregister the     device, and release the PCMCIA configuration.  If the device is    still open, this will be postponed until it is closed.    ======================================================================*/static void ftl_release(u_long arg){    dev_link_t *link = (dev_link_t *)arg;    int i;        DEBUG(0, "ftl_cs: ftl_release(0x%p)\n", link);    for (i = 0; i < MAX_DEV; i++)	if (dev_table[i] == link) break;    if (link->open) {	DEBUG(1, "ftl_cs: release postponed, ftl%d still open\n", i);	link->state |= DEV_STALE_CONFIG;	return;    }    link->dev = NULL;    if (link->win)	CardServices(ReleaseWindow, link->win);    link->state &= ~DEV_CONFIG;        if (link->state & DEV_STALE_LINK)	ftl_detach(link);    } /* ftl_release *//*======================================================================    The card status event handler.  Mostly, this schedules other    stuff to run after an event is received.    ======================================================================*/static void save_status(eraseq_entry_t *erase);static int ftl_event(event_t event, int priority,		     event_callback_args_t *args){    dev_link_t *link = args->client_data;    ftl_dev_t *dev = link->priv;    DEBUG(1, "ftl_cs: ftl_event()\n");        switch (event) {    case CS_EVENT_CARD_REMOVAL:	link->state &= ~DEV_PRESENT;	if (link->state & DEV_CONFIG)	    mod_timer(&link->release, jiffies + HZ/20);	break;    case CS_EVENT_CARD_INSERTION:	link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;	ftl_config(link);	break;    case CS_EVENT_ERASE_COMPLETE:	save_status((eraseq_entry_t *)(args->info));	wake_up(&dev->erase_pending);	break;    case CS_EVENT_PM_SUSPEND:	link->state |= DEV_SUSPEND;	/* Fall through... */    case CS_EVENT_RESET_PHYSICAL:	/* get_lock(link); */	break;    case CS_EVENT_PM_RESUME:	link->state &= ~DEV_SUSPEND;	/* Fall through... */    case CS_EVENT_CARD_RESET:	/* free_lock(link); */	break;    }    return 0;} /* ftl_event *//*======================================================================    Scan_header() checks to see if a memory region contains an FTL    partition.  build_maps() reads all the erase unit headers, builds    the erase unit map, and then builds the virtual page map.    ======================================================================*/static int scan_header(partition_t *part){    erase_unit_header_t header;    mem_op_t req;    int ret;    part->header.FormattedSize = 0;    /* Search first megabyte for a valid FTL header */    req.Attributes = MEM_OP_BUFFER_KERNEL;    req.Count = sizeof(header);    for (req.Offset = 0;	 req.Offset < 0x100000;	 req.Offset += part->region.BlockSize) {	ret = CardServices(ReadMemory, part->handle, &req, &header);	if (ret != CS_SUCCESS) {	    cs_error(ReadMemory, ret);	    return -1;	}	if (strcmp(header.DataOrgTuple+3, "FTL100") == 0) break;    }    if (req.Offset == 0x100000) {	printk(KERN_NOTICE "ftl_cs: FTL header not found.\n");	return -1;    }    if ((header.NumEraseUnits > 65536) || (header.BlockSize != 9) ||	(header.EraseUnitSize < 10) || (header.EraseUnitSize > 31) ||

⌨️ 快捷键说明

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