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

📄 sddr55.c

📁 优龙2410linux2.6.8内核源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/* Driver for SanDisk SDDR-55 SmartMedia reader * * $Id:$ * * SDDR55 driver v0.1: * * First release * * Current development and maintenance by: *   (c) 2002 Simon Munton * * 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. */#include "transport.h"#include "protocol.h"#include "usb.h"#include "debug.h"#include "sddr55.h"#include <linux/jiffies.h>#include <linux/errno.h>#include <linux/slab.h>#define short_pack(lsb,msb) ( ((u16)(lsb)) | ( ((u16)(msb))<<8 ) )#define LSB_of(s) ((s)&0xFF)#define MSB_of(s) ((s)>>8)#define PAGESIZE  512#define set_sense_info(sk, asc, ascq)	\    do {				\	info->sense_data[2] = sk;	\	info->sense_data[12] = asc;	\	info->sense_data[13] = ascq;	\	} while (0)struct sddr55_card_info {	unsigned long	capacity;	/* Size of card in bytes */	int		max_log_blks;	/* maximum number of logical blocks */	int		pageshift;	/* log2 of pagesize */	int		smallpageshift;	/* 1 if pagesize == 256 */	int		blocksize;	/* Size of block in pages */	int		blockshift;	/* log2 of blocksize */	int		blockmask;	/* 2^blockshift - 1 */	int		read_only;	/* non zero if card is write protected */	int		force_read_only;	/* non zero if we find a map error*/	int		*lba_to_pba;	/* logical to physical map */	int		*pba_to_lba;	/* physical to logical map */	int		fatal_error;	/* set if we detect something nasty */	unsigned long 	last_access;	/* number of jiffies since we last talked to device */	unsigned char   sense_data[18];};#define NOT_ALLOCATED		0xffffffff#define BAD_BLOCK		0xffff#define CIS_BLOCK		0x400#define UNUSED_BLOCK		0x3ffstatic intsddr55_bulk_transport(struct us_data *us, int direction,		      unsigned char *data, unsigned int len) {	struct sddr55_card_info *info = (struct sddr55_card_info *)us->extra;	unsigned int pipe = (direction == SCSI_DATA_READ) ?			us->recv_bulk_pipe : us->send_bulk_pipe;	if (!len)		return USB_STOR_XFER_GOOD;	info->last_access = jiffies;	return usb_stor_bulk_transfer_buf(us, pipe, data, len, NULL);}/* check if card inserted, if there is, update read_only status * return non zero if no card */static int sddr55_status(struct us_data *us){	int result;	unsigned char *command = us->iobuf;	unsigned char *status = us->iobuf;	struct sddr55_card_info *info = (struct sddr55_card_info *)us->extra;	/* send command */	memset(command, 0, 8);	command[5] = 0xB0;	command[7] = 0x80;	result = sddr55_bulk_transport(us,		SCSI_DATA_WRITE, command, 8);	US_DEBUGP("Result for send_command in status %d\n",		result);	if (result != USB_STOR_XFER_GOOD) {		set_sense_info (4, 0, 0);	/* hardware error */		return USB_STOR_TRANSPORT_ERROR;	}	result = sddr55_bulk_transport(us,		SCSI_DATA_READ, status,	4);	/* expect to get short transfer if no card fitted */	if (result == USB_STOR_XFER_SHORT || result == USB_STOR_XFER_STALLED) {		/* had a short transfer, no card inserted, free map memory */		if (info->lba_to_pba)			kfree(info->lba_to_pba);		if (info->pba_to_lba)			kfree(info->pba_to_lba);		info->lba_to_pba = NULL;		info->pba_to_lba = NULL;		info->fatal_error = 0;		info->force_read_only = 0;		set_sense_info (2, 0x3a, 0);	/* not ready, medium not present */		return USB_STOR_TRANSPORT_FAILED;	}	if (result != USB_STOR_XFER_GOOD) {		set_sense_info (4, 0, 0);	/* hardware error */		return USB_STOR_TRANSPORT_FAILED;	}		/* check write protect status */	info->read_only = (status[0] & 0x20);	/* now read status */	result = sddr55_bulk_transport(us,		SCSI_DATA_READ, status,	2);	if (result != USB_STOR_XFER_GOOD) {		set_sense_info (4, 0, 0);	/* hardware error */	}	return (result == USB_STOR_XFER_GOOD ?			USB_STOR_TRANSPORT_GOOD : USB_STOR_TRANSPORT_FAILED);}static int sddr55_read_data(struct us_data *us,		unsigned int lba,		unsigned int page,		unsigned short sectors) {	int result = USB_STOR_TRANSPORT_GOOD;	unsigned char *command = us->iobuf;	unsigned char *status = us->iobuf;	struct sddr55_card_info *info = (struct sddr55_card_info *)us->extra;	unsigned char *buffer;	unsigned int pba;	unsigned long address;	unsigned short pages;	unsigned int len, index, offset;	// Since we only read in one block at a time, we have to create	// a bounce buffer and move the data a piece at a time between the	// bounce buffer and the actual transfer buffer.	len = min((unsigned int) sectors, (unsigned int) info->blocksize >>			info->smallpageshift) * PAGESIZE;	buffer = kmalloc(len, GFP_NOIO);	if (buffer == NULL)		return USB_STOR_TRANSPORT_ERROR; /* out of memory */	index = offset = 0;	while (sectors>0) {		/* have we got to end? */		if (lba >= info->max_log_blks)			break;		pba = info->lba_to_pba[lba];		// Read as many sectors as possible in this block		pages = min((unsigned int) sectors << info->smallpageshift,				info->blocksize - page);		len = pages << info->pageshift;		US_DEBUGP("Read %02X pages, from PBA %04X"			" (LBA %04X) page %02X\n",			pages, pba, lba, page);		if (pba == NOT_ALLOCATED) {			/* no pba for this lba, fill with zeroes */			memset (buffer, 0, len);		} else {			address = (pba << info->blockshift) + page;			command[0] = 0;			command[1] = LSB_of(address>>16);			command[2] = LSB_of(address>>8);			command[3] = LSB_of(address);			command[4] = 0;			command[5] = 0xB0;			command[6] = LSB_of(pages << (1 - info->smallpageshift));			command[7] = 0x85;			/* send command */			result = sddr55_bulk_transport(us,				SCSI_DATA_WRITE, command, 8);			US_DEBUGP("Result for send_command in read_data %d\n",				result);			if (result != USB_STOR_XFER_GOOD) {				result = USB_STOR_TRANSPORT_ERROR;				goto leave;			}			/* read data */			result = sddr55_bulk_transport(us,				SCSI_DATA_READ, buffer, len);			if (result != USB_STOR_XFER_GOOD) {				result = USB_STOR_TRANSPORT_ERROR;				goto leave;			}			/* now read status */			result = sddr55_bulk_transport(us,				SCSI_DATA_READ, status, 2);			if (result != USB_STOR_XFER_GOOD) {				result = USB_STOR_TRANSPORT_ERROR;				goto leave;			}			/* check status for error */			if (status[0] == 0xff && status[1] == 0x4) {				set_sense_info (3, 0x11, 0);				result = USB_STOR_TRANSPORT_FAILED;				goto leave;			}		}		// Store the data in the transfer buffer		usb_stor_access_xfer_buf(buffer, len, us->srb,				&index, &offset, TO_XFER_BUF);		page = 0;		lba++;		sectors -= pages >> info->smallpageshift;	}	result = USB_STOR_TRANSPORT_GOOD;leave:	kfree(buffer);	return result;}static int sddr55_write_data(struct us_data *us,		unsigned int lba,		unsigned int page,		unsigned short sectors) {	int result = USB_STOR_TRANSPORT_GOOD;	unsigned char *command = us->iobuf;	unsigned char *status = us->iobuf;	struct sddr55_card_info *info = (struct sddr55_card_info *)us->extra;	unsigned char *buffer;	unsigned int pba;	unsigned int new_pba;	unsigned long address;	unsigned short pages;	int i;	unsigned int len, index, offset;	/* check if we are allowed to write */	if (info->read_only || info->force_read_only) {		set_sense_info (7, 0x27, 0);	/* read only */		return USB_STOR_TRANSPORT_FAILED;	}	// Since we only write one block at a time, we have to create	// a bounce buffer and move the data a piece at a time between the	// bounce buffer and the actual transfer buffer.	len = min((unsigned int) sectors, (unsigned int) info->blocksize >>			info->smallpageshift) * PAGESIZE;	buffer = kmalloc(len, GFP_NOIO);	if (buffer == NULL)		return USB_STOR_TRANSPORT_ERROR;	index = offset = 0;	while (sectors > 0) {		/* have we got to end? */		if (lba >= info->max_log_blks)			break;		pba = info->lba_to_pba[lba];		// Write as many sectors as possible in this block		pages = min((unsigned int) sectors << info->smallpageshift,				info->blocksize - page);		len = pages << info->pageshift;		// Get the data from the transfer buffer		usb_stor_access_xfer_buf(buffer, len, us->srb,				&index, &offset, FROM_XFER_BUF);		US_DEBUGP("Write %02X pages, to PBA %04X"			" (LBA %04X) page %02X\n",			pages, pba, lba, page);					command[4] = 0;		if (pba == NOT_ALLOCATED) {			/* no pba allocated for this lba, find a free pba to use */			int max_pba = (info->max_log_blks / 250 ) * 256;			int found_count = 0;			int found_pba = -1;			/* set pba to first block in zone lba is in */			pba = (lba / 1000) * 1024;			US_DEBUGP("No PBA for LBA %04X\n",lba);			if (max_pba > 1024)				max_pba = 1024;			/*			 * Scan through the map looking for an unused block			 * leave 16 unused blocks at start (or as many as			 * possible) since the sddr55 seems to reuse a used			 * block when it shouldn't if we don't leave space.			 */			for (i = 0; i < max_pba; i++, pba++) {				if (info->pba_to_lba[pba] == UNUSED_BLOCK) {					found_pba = pba;					if (found_count++ > 16)						break;				}			}			pba = found_pba;			if (pba == -1) {				/* oh dear */				US_DEBUGP("Couldn't find unallocated block\n");				set_sense_info (3, 0x31, 0);	/* medium error */				result = USB_STOR_TRANSPORT_FAILED;				goto leave;			}			US_DEBUGP("Allocating PBA %04X for LBA %04X\n", pba, lba);			/* set writing to unallocated block flag */			command[4] = 0x40;		}		address = (pba << info->blockshift) + page;		command[1] = LSB_of(address>>16);		command[2] = LSB_of(address>>8); 		command[3] = LSB_of(address);		/* set the lba into the command, modulo 1000 */		command[0] = LSB_of(lba % 1000);		command[6] = MSB_of(lba % 1000);		command[4] |= LSB_of(pages >> info->smallpageshift);		command[5] = 0xB0;		command[7] = 0x86;		/* send command */		result = sddr55_bulk_transport(us,			SCSI_DATA_WRITE, command, 8);		if (result != USB_STOR_XFER_GOOD) {			US_DEBUGP("Result for send_command in write_data %d\n",			result);			/* set_sense_info is superfluous here? */			set_sense_info (3, 0x3, 0);/* peripheral write error */			result = USB_STOR_TRANSPORT_FAILED;			goto leave;		}		/* send the data */		result = sddr55_bulk_transport(us,			SCSI_DATA_WRITE, buffer, len);		if (result != USB_STOR_XFER_GOOD) {			US_DEBUGP("Result for send_data in write_data %d\n",				  result);			/* set_sense_info is superfluous here? */			set_sense_info (3, 0x3, 0);/* peripheral write error */			result = USB_STOR_TRANSPORT_FAILED;			goto leave;		}		/* now read status */		result = sddr55_bulk_transport(us, SCSI_DATA_READ, status, 6);		if (result != USB_STOR_XFER_GOOD) {			US_DEBUGP("Result for get_status in write_data %d\n",				  result);			/* set_sense_info is superfluous here? */			set_sense_info (3, 0x3, 0);/* peripheral write error */			result = USB_STOR_TRANSPORT_FAILED;			goto leave;		}		new_pba = (status[3] + (status[4] << 8) + (status[5] << 16))						  >> info->blockshift;		/* check status for error */		if (status[0] == 0xff && status[1] == 0x4) {			info->pba_to_lba[new_pba] = BAD_BLOCK;			set_sense_info (3, 0x0c, 0);			result = USB_STOR_TRANSPORT_FAILED;			goto leave;		}		US_DEBUGP("Updating maps for LBA %04X: old PBA %04X, new PBA %04X\n",			lba, pba, new_pba);		/* update the lba<->pba maps, note new_pba might be the same as pba */		info->lba_to_pba[lba] = new_pba;		info->pba_to_lba[pba] = UNUSED_BLOCK;		/* check that new_pba wasn't already being used */		if (info->pba_to_lba[new_pba] != UNUSED_BLOCK) {			printk(KERN_ERR "sddr55 error: new PBA %04X already in use for LBA %04X\n",				new_pba, info->pba_to_lba[new_pba]);			info->fatal_error = 1;			set_sense_info (3, 0x31, 0);			result = USB_STOR_TRANSPORT_FAILED;			goto leave;		}		/* update the pba<->lba maps for new_pba */		info->pba_to_lba[new_pba] = lba % 1000;		page = 0;		lba++;		sectors -= pages >> info->smallpageshift;	}	result = USB_STOR_TRANSPORT_GOOD;

⌨️ 快捷键说明

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