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

📄 jumpshot.c

📁 LINUX下USB驱动程序的开发
💻 C
📖 第 1 页 / 共 2 页
字号:
/* Driver for Lexar "Jumpshot" Compact Flash reader * * $Id: jumpshot.c,v 1.7 2002/02/25 00:40:13 mdharm Exp $ * * jumpshot driver v0.1: * * First release * * Current development and maintenance by: *   (c) 2000 Jimmie Mayfield (mayfield+usb@sackheads.org) * *   Many thanks to Robert Baruch for the SanDisk SmartMedia reader driver *   which I used as a template for this driver. * *   Some bugfixes and scatter-gather code by Gregory P. Smith  *   (greg-usb@electricrain.com) * *   Fix for media change by Joerg Schneider (js@joergschneider.com) * * Developed with the assistance of: * *   (C) 2002 Alan Stern <stern@rowland.org> * * 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. */  /*  * This driver attempts to support the Lexar Jumpshot USB CompactFlash   * reader.  Like many other USB CompactFlash readers, the Jumpshot contains  * a USB-to-ATA chip.   *  * This driver supports reading and writing.  If you're truly paranoid,  * however, you can force the driver into a write-protected state by setting  * the WP enable bits in jumpshot_handle_mode_sense.  Basically this means  * setting mode_param_header[3] = 0x80.    */#include "transport.h"#include "protocol.h"#include "usb.h"#include "debug.h"#include "jumpshot.h"#include <linux/sched.h>#include <linux/errno.h>#include <linux/slab.h>extern int usb_stor_control_msg(struct us_data *us, unsigned int pipe,				u8 request, u8 requesttype, u16 value,				u16 index, void *data, u16 size);extern int usb_stor_bulk_msg(struct us_data *us, void *data, int pipe,			     unsigned int len, unsigned int *act_len);#if 0static void jumpshot_dump_data(unsigned char *data, int len){	unsigned char buf[80];	int sofar = 0;	if (!data)		return;	memset(buf, 0, sizeof(buf));	for (sofar = 0; sofar < len; sofar++) {		sprintf(buf + strlen(buf), "%02x ",			((unsigned int) data[sofar]) & 0xFF);		if (sofar % 16 == 15) {			US_DEBUGP("jumpshot:  %s\n", buf);			memset(buf, 0, sizeof(buf));		}	}	if (strlen(buf) != 0)		US_DEBUGP("jumpshot:  %s\n", buf);}#endif/* * Send a control message and wait for the response. * * us - the pointer to the us_data structure for the device to use * * request - the URB Setup Packet's first 6 bytes. The first byte always *  corresponds to the request type, and the second byte always corresponds *  to the request.  The other 4 bytes do not correspond to value and index, *  since they are used in a custom way by the SCM protocol. * * xfer_data - a buffer from which to get, or to which to store, any data *  that gets send or received, respectively, with the URB. Even though *  it looks like we allocate a buffer in this code for the data, xfer_data *  must contain enough allocated space. * * xfer_len - the number of bytes to send or receive with the URB. * * This routine snarfed from the SanDisk SDDR-09 driver * */static int jumpshot_send_control(struct us_data  *us,				 int pipe,				 unsigned char request,				 unsigned char requesttype,				 unsigned short value,				 unsigned short index,				 unsigned char *xfer_data,				 unsigned int xfer_len){	int result;	// Send the URB to the device and wait for a response.	/* Why are request and request type reversed in this call? */	result = usb_stor_control_msg(us, pipe,				      request, requesttype,				      value, index, xfer_data, xfer_len);	// Check the return code for the command.	if (result < 0) {		/* if the command was aborted, indicate that */		if (result == -ECONNRESET)			return USB_STOR_TRANSPORT_ABORTED;		/* a stall is a fatal condition from the device */		if (result == -EPIPE) {			US_DEBUGP("jumpshot_send_control:  -- Stall on control pipe. Clearing\n");			result = usb_stor_clear_halt(us, pipe);			US_DEBUGP("jumpshot_send_control:  -- usb_stor_clear_halt() returns %d\n", result);			return USB_STOR_TRANSPORT_FAILED;		}		/* Uh oh... serious problem here */		return USB_STOR_TRANSPORT_ERROR;	}	return USB_STOR_TRANSPORT_GOOD;}static int jumpshot_raw_bulk(int direction,			     struct us_data *us,			     unsigned char *data, 		             unsigned int len){	int result;	int act_len;	int pipe;	if (direction == SCSI_DATA_READ)		pipe = usb_rcvbulkpipe(us->pusb_dev, us->ep_in);	else		pipe = usb_sndbulkpipe(us->pusb_dev, us->ep_out);	result = usb_stor_bulk_msg(us, data, pipe, len, &act_len);	// if we stall, we need to clear it before we go on	if (result == -EPIPE) {		US_DEBUGP("jumpshot_raw_bulk:  EPIPE. clearing endpoint halt for"			  " pipe 0x%x, stalled at %d bytes\n", pipe, act_len);		usb_stor_clear_halt(us, pipe);	}	if (result) {		// NAK - that means we've retried a few times already		if (result == -ETIMEDOUT) {			US_DEBUGP("jumpshot_raw_bulk:  device NAKed\n");			return US_BULK_TRANSFER_FAILED;		}		// -ECONNRESET -- we canceled this transfer		if (result == -ECONNRESET) {			US_DEBUGP("jumpshot_raw_bulk:  transfer aborted\n");			return US_BULK_TRANSFER_ABORTED;		}		if (result == -EPIPE) {			US_DEBUGP("jumpshot_raw_bulk:  output pipe stalled\n");			return USB_STOR_TRANSPORT_FAILED;		}		// the catch-all case		US_DEBUGP("jumpshot_raw_bulk:  unknown error\n");		return US_BULK_TRANSFER_FAILED;	}	if (act_len != len) {		US_DEBUGP("jumpshot_raw_bulk:  Warning. Transferred only %d bytes\n", act_len);		return US_BULK_TRANSFER_SHORT;	}	US_DEBUGP("jumpshot_raw_bulk:  Transfered %d of %d bytes\n", act_len, len);	return US_BULK_TRANSFER_GOOD;}static inline int jumpshot_bulk_read(struct us_data *us,			             unsigned char *data, 		                     unsigned int len){	if (len == 0)		return USB_STOR_TRANSPORT_GOOD;	US_DEBUGP("jumpshot_bulk_read:  len = %d\n", len);	return jumpshot_raw_bulk(SCSI_DATA_READ, us, data, len);}static inline int jumpshot_bulk_write(struct us_data *us,			              unsigned char *data, 		                      unsigned int len){	if (len == 0)		return USB_STOR_TRANSPORT_GOOD;	US_DEBUGP("jumpshot_bulk_write:  len = %d\n", len);	return jumpshot_raw_bulk(SCSI_DATA_WRITE, us, data, len);}static int jumpshot_get_status(struct us_data  *us){	unsigned char reply;	int rc;	if (!us)		return USB_STOR_TRANSPORT_ERROR;	// send the setup	rc = jumpshot_send_control(us,				   usb_rcvctrlpipe(us->pusb_dev, 0),				   0, 0xA0, 0, 7, &reply, 1);	if (rc != USB_STOR_TRANSPORT_GOOD)		return rc;	if (reply != 0x50) {		US_DEBUGP("jumpshot_get_status:  0x%2x\n",			  (unsigned short) (reply));		return USB_STOR_TRANSPORT_ERROR;	}	return USB_STOR_TRANSPORT_GOOD;}static int jumpshot_read_data(struct us_data *us,		              struct jumpshot_info *info,		              u32 sector,		              u32 sectors, 		              unsigned char *dest, 		              int use_sg){	unsigned char command[] = { 0, 0, 0, 0, 0, 0xe0, 0x20 };	unsigned char *buffer = NULL;	unsigned char *ptr;	unsigned char  thistime;	struct scatterlist *sg = NULL;        int totallen, len, result;        int sg_idx = 0, current_sg_offset = 0;        int transferred;        // we're working in LBA mode.  according to the ATA spec,         // we can support up to 28-bit addressing.  I don't know if Jumpshot        // supports beyond 24-bit addressing.  It's kind of hard to test         // since it requires > 8GB CF card.	//	if (sector > 0x0FFFFFFF)		return USB_STOR_TRANSPORT_ERROR;	// If we're using scatter-gather, we have to create a new	// buffer to read all of the data in first, since a	// scatter-gather buffer could in theory start in the middle	// of a page, which would be bad. A developer who wants a	// challenge might want to write a limited-buffer	// version of this code.	totallen = sectors * info->ssize;	do {               // loop, never allocate or transfer more than 64k at once (min(128k, 255*info->ssize) is the real limit)                len = min_t(int, totallen, 65536);                if (use_sg) {                        sg = (struct scatterlist *) dest;                        buffer = kmalloc(len, GFP_NOIO);                        if (buffer == NULL)                                return USB_STOR_TRANSPORT_ERROR;                        ptr = buffer;                } else {                        ptr = dest;                }                thistime = (len / info->ssize) & 0xff;		command[0] = 0;		command[1] = thistime;		command[2] = sector & 0xFF;		command[3] = (sector >>  8) & 0xFF;		command[4] = (sector >> 16) & 0xFF;		command[5] |= (sector >> 24) & 0x0F;		// send the setup + command		result = jumpshot_send_control(us,					       usb_sndctrlpipe(us->pusb_dev, 0),					       0, 0x20, 0, 1, command, 7);		if (result != USB_STOR_TRANSPORT_GOOD) {			if (use_sg)				kfree(buffer);			return result;		}		// read the result		result = jumpshot_bulk_read(us, ptr, len);		if (result != USB_STOR_TRANSPORT_GOOD) {			if (use_sg)				kfree(buffer);			return result;		}		US_DEBUGP("jumpshot_read_data:  %d bytes\n", len);		//jumpshot_dump_data(ptr, len);			sectors -= thistime;		sector  += thistime;                if (use_sg) {                        transferred = 0;                        while (sg_idx < use_sg && transferred < len) {                                if (len - transferred >= sg[sg_idx].length - current_sg_offset) {                                        US_DEBUGP("jumpshot_read_data:  adding %d bytes to %d byte sg buffer\n", sg[sg_idx].length - current_sg_offset, sg[sg_idx].length);                                        memcpy(sg[sg_idx].address + current_sg_offset,                                               buffer + transferred,                                               sg[sg_idx].length - current_sg_offset);                                        transferred += sg[sg_idx].length - current_sg_offset;                                        current_sg_offset = 0;                                        // on to the next sg buffer                                        ++sg_idx;                                } else {                                        US_DEBUGP("jumpshot_read_data:  adding %d bytes to %d byte sg buffer\n", len - transferred, sg[sg_idx].length);                                        memcpy(sg[sg_idx].address + current_sg_offset,                                               buffer + transferred,                                               len - transferred);                                        current_sg_offset += len - transferred;                                        // this sg buffer is only partially full and we're out of data to copy in                                        break;                                }                        }                        kfree(buffer);                } else {                        dest += len;                }                totallen -= len;        } while (totallen > 0);	return USB_STOR_TRANSPORT_GOOD;}static int jumpshot_write_data(struct us_data *us,		               struct jumpshot_info *info,			       u32 sector,			       u32 sectors, 		               unsigned char *src, 		               int use_sg){	unsigned char command[7] = { 0, 0, 0, 0, 0, 0xE0, 0x30 };	unsigned char *buffer = NULL;	unsigned char *ptr;	unsigned char  thistime;	struct scatterlist *sg = NULL;        int totallen, len, result, waitcount;        int sg_idx = 0, current_sg_offset = 0;        int transferred;        // we're working in LBA mode.  according to the ATA spec,         // we can support up to 28-bit addressing.  I don't know if Jumpshot        // supports beyond 24-bit addressing.  It's kind of hard to test         // since it requires > 8GB CF card.        //	if (sector > 0x0FFFFFFF)		return USB_STOR_TRANSPORT_ERROR;	// If we're using scatter-gather, we have to create a new	// buffer to read all of the data in first, since a	// scatter-gather buffer could in theory start in the middle	// of a page, which would be bad. A developer who wants a	// challenge might want to write a limited-buffer	// version of this code.	totallen = sectors * info->ssize;	do {                // loop, never allocate or transfer more than 64k at once (min(128k, 255*info->ssize) is the real limit)                len = min_t(int, totallen, 65536);                if (use_sg) {                        sg = (struct scatterlist *) src;                        buffer = kmalloc(len, GFP_NOIO);                        if (buffer == NULL)

⌨️ 快捷键说明

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