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

📄 jumpshot.c

📁 usb 子设备程序 支持sd卡 mouse keyboard 的最单单的驱动程序 gcc编译 支持
💻 C
📖 第 1 页 / 共 2 页
字号:
/* Driver for Lexar "Jumpshot" Compact Flash reader * * $Id: jumpshot.c,v 1.1.1.1 2007/06/12 07:27:09 eyryu 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.  See the comments  * in that routine.  */#include <linux/errno.h>#include <linux/slab.h>#include <scsi/scsi.h>#include <scsi/scsi_cmnd.h>#include "usb.h"#include "transport.h"#include "protocol.h"#include "debug.h"#include "jumpshot.h"static inline int jumpshot_bulk_read(struct us_data *us,				     unsigned char *data, 				     unsigned int len){	if (len == 0)		return USB_STOR_XFER_GOOD;	US_DEBUGP("jumpshot_bulk_read:  len = %d\n", len);	return usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,			data, len, NULL);}static inline int jumpshot_bulk_write(struct us_data *us,				      unsigned char *data, 				      unsigned int len){	if (len == 0)		return USB_STOR_XFER_GOOD;	US_DEBUGP("jumpshot_bulk_write:  len = %d\n", len);	return usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,			data, len, NULL);}static int jumpshot_get_status(struct us_data  *us){	int rc;	if (!us)		return USB_STOR_TRANSPORT_ERROR;	// send the setup	rc = usb_stor_ctrl_transfer(us, us->recv_ctrl_pipe,				   0, 0xA0, 0, 7, us->iobuf, 1);	if (rc != USB_STOR_XFER_GOOD)		return USB_STOR_TRANSPORT_ERROR;	if (us->iobuf[0] != 0x50) {		US_DEBUGP("jumpshot_get_status:  0x%2x\n",			  us->iobuf[0]);		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 *command = us->iobuf;	unsigned char *buffer;	unsigned char  thistime;	unsigned int totallen, alloclen;	int len, result;	unsigned int sg_idx = 0, sg_offset = 0;	// 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;	totallen = sectors * info->ssize;	// Since we don't read more than 64 KB 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.	alloclen = min(totallen, 65536u);	buffer = kmalloc(alloclen, GFP_NOIO);	if (buffer == NULL)		return USB_STOR_TRANSPORT_ERROR;	do {		// loop, never allocate or transfer more than 64k at once		// (min(128k, 255*info->ssize) is the real limit)		len = min(totallen, alloclen);		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] = 0xE0 | ((sector >> 24) & 0x0F);		command[6] = 0x20;		// send the setup + command		result = usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,					       0, 0x20, 0, 1, command, 7);		if (result != USB_STOR_XFER_GOOD)			goto leave;		// read the result		result = jumpshot_bulk_read(us, buffer, len);		if (result != USB_STOR_XFER_GOOD)			goto leave;		US_DEBUGP("jumpshot_read_data:  %d bytes\n", len);		// Store the data in the transfer buffer		usb_stor_access_xfer_buf(buffer, len, us->srb,				 &sg_idx, &sg_offset, TO_XFER_BUF);		sector += thistime;		totallen -= len;	} while (totallen > 0);	kfree(buffer);	return USB_STOR_TRANSPORT_GOOD; leave:	kfree(buffer);	return USB_STOR_TRANSPORT_ERROR;}static int jumpshot_write_data(struct us_data *us,			       struct jumpshot_info *info,			       u32 sector,			       u32 sectors){	unsigned char *command = us->iobuf;	unsigned char *buffer;	unsigned char  thistime;	unsigned int totallen, alloclen;	int len, result, waitcount;	unsigned int sg_idx = 0, sg_offset = 0;	// 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;	totallen = sectors * info->ssize;	// Since we don't write more than 64 KB 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.	alloclen = min(totallen, 65536u);	buffer = kmalloc(alloclen, GFP_NOIO);	if (buffer == NULL)		return USB_STOR_TRANSPORT_ERROR;	do {		// loop, never allocate or transfer more than 64k at once		// (min(128k, 255*info->ssize) is the real limit)		len = min(totallen, alloclen);		thistime = (len / info->ssize) & 0xff;		// Get the data from the transfer buffer		usb_stor_access_xfer_buf(buffer, len, us->srb,				&sg_idx, &sg_offset, FROM_XFER_BUF);		command[0] = 0;		command[1] = thistime;		command[2] = sector & 0xFF;		command[3] = (sector >>  8) & 0xFF;		command[4] = (sector >> 16) & 0xFF;		command[5] = 0xE0 | ((sector >> 24) & 0x0F);		command[6] = 0x30;		// send the setup + command		result = usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,			0, 0x20, 0, 1, command, 7);		if (result != USB_STOR_XFER_GOOD)			goto leave;		// send the data		result = jumpshot_bulk_write(us, buffer, len);		if (result != USB_STOR_XFER_GOOD)			goto leave;		// read the result.  apparently the bulk write can complete		// before the jumpshot drive is finished writing.  so we loop		// here until we get a good return code		waitcount = 0;		do {			result = jumpshot_get_status(us);			if (result != USB_STOR_TRANSPORT_GOOD) {				// I have not experimented to find the smallest value.				//				msleep(50); 			}		} while ((result != USB_STOR_TRANSPORT_GOOD) && (waitcount < 10));		if (result != USB_STOR_TRANSPORT_GOOD)			US_DEBUGP("jumpshot_write_data:  Gah!  Waitcount = 10.  Bad write!?\n");		sector += thistime;		totallen -= len;	} while (totallen > 0);	kfree(buffer);	return result; leave:	kfree(buffer);	return USB_STOR_TRANSPORT_ERROR;}static int jumpshot_id_device(struct us_data *us,			      struct jumpshot_info *info){	unsigned char *command = us->iobuf;	unsigned char *reply;	int 	 rc;	if (!us || !info)		return USB_STOR_TRANSPORT_ERROR;	command[0] = 0xE0;	command[1] = 0xEC;	reply = kmalloc(512, GFP_NOIO);	if (!reply)		return USB_STOR_TRANSPORT_ERROR;	// send the setup	rc = usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,				   0, 0x20, 0, 6, command, 2);	if (rc != USB_STOR_XFER_GOOD) {

⌨️ 快捷键说明

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