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

📄 microtek.c

📁 基于S3CEB2410平台LINUX操作系统下 USB驱动源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/* Driver for Microtek Scanmaker X6 USB scanner, and possibly others. * * (C) Copyright 2000 John Fremlin <vii@penguinpowered.com> * (C) Copyright 2000 Oliver Neukum <Oliver.Neukum@lrz.uni-muenchen.de> * * Parts shamelessly stolen from usb-storage and copyright by their * authors. Thanks to Matt Dharm for giving us permission! * * This driver implements a SCSI host controller driver and a USB * device driver. To avoid confusion, all the USB related stuff is * prefixed by mts_usb_ and all the SCSI stuff by mts_scsi_. * * Microtek (www.microtek.com) did not release the specifications for * their USB protocol to us, so we had to reverse engineer them. We * don't know for which models they are valid. * * The X6 USB has three bulk endpoints, one output (0x1) down which * commands and outgoing data are sent, and two input: 0x82 from which * normal data is read from the scanner (in packets of maximum 32 * bytes) and from which the status byte is read, and 0x83 from which * the results of a scan (or preview) are read in up to 64 * 1024 byte * chunks by the Windows driver. We don't know how much it is possible * to read at a time from 0x83. * * It seems possible to read (with URB transfers) everything from 0x82 * in one go, without bothering to read in 32 byte chunks. * * There seems to be an optimisation of a further READ implicit if * you simply read from 0x83. * * Guessed protocol: * *	Send raw SCSI command to EP 0x1 * *	If there is data to receive: *		If the command was READ datatype=image: *			Read a lot of data from EP 0x83 *		Else: *			Read data from EP 0x82 *	Else: *		If there is data to transmit: *			Write it to EP 0x1 * *	Read status byte from EP 0x82 * * References: * * The SCSI command set for the scanner is available from *	ftp://ftp.microtek.com/microtek/devpack/ * * Microtek NV sent us a more up to date version of the document. If * you want it, just send mail. * * Status: * *	Untested with multiple scanners. *	Untested on SMP. *	Untested on a bigendian machine. * * History: * *	20000417 starting history *	20000417 fixed load oops *	20000417 fixed unload oops *	20000419 fixed READ IMAGE detection *	20000424 started conversion to use URBs *	20000502 handled short transfers as errors *	20000513 rename and organisation of functions (john) *	20000513 added IDs for all products supported by Windows driver (john) *	20000514 Rewrote mts_scsi_queuecommand to use URBs (john) *	20000514 Version 0.0.8j *      20000514 Fix reporting of non-existant devices to SCSI layer (john) *	20000514 Added MTS_DEBUG_INT (john) *	20000514 Changed "usb-microtek" to "microtek" for consistency (john) *	20000514 Stupid bug fixes (john) *	20000514 Version 0.0.9j *	20000515 Put transfer context and URB in mts_desc (john) *	20000515 Added prelim turn off debugging support (john) *	20000515 Version 0.0.10j *      20000515 Fixed up URB allocation (clear URB on alloc) (john) *      20000515 Version 0.0.11j *	20000516 Removed unnecessary spinlock in mts_transfer_context (john) *	20000516 Removed unnecessary up on instance lock in mts_remove_nolock (john) *	20000516 Implemented (badly) scsi_abort (john) *	20000516 Version 0.0.12j *      20000517 Hopefully removed mts_remove_nolock quasideadlock (john) *      20000517 Added mts_debug_dump to print ll USB info (john) *	20000518 Tweaks and documentation updates (john) *	20000518 Version 0.0.13j *	20000518 Cleaned up abort handling (john) *	20000523 Removed scsi_command and various scsi_..._resets (john) *	20000523 Added unlink URB on scsi_abort, now OHCI supports it (john) *	20000523 Fixed last tiresome compile warning (john) *	20000523 Version 0.0.14j (though version 0.1 has come out?) *	20000602 Added primitive reset *	20000602 Version 0.2.0 *	20000603 various cosmetic changes *	20000603 Version 0.2.1 *	20000620 minor cosmetic changes *	20000620 Version 0.2.2 *	20000822 Hopefully fixed deadlock in mts_remove_nolock() *	20000822 Fixed minor race in mts_transfer_cleanup() *	20000822 Fixed deadlock on submission error in queuecommand *	20000822 Version 0.2.3 *	20000913 Reduced module size if debugging is off *	20000913 Version 0.2.4 *      20010210 New abort logic *      20010210 Version 0.3.0 *	20010217 Merged scatter/gather *	20010218 Version 0.4.0 *	20010218 Cosmetic fixes *	20010218 Version 0.4.1 *      20010306 Abort while using scatter/gather *      20010306 Version 0.4.2 *      20010311 Remove all timeouts and tidy up generally (john) *	20010320 check return value of scsi_register() *	20010320 Version 0.4.3 *	20010408 Identify version on module load. *	20011003 Fix multiple requests */#include <linux/module.h>#include <linux/kernel.h>#include <linux/sched.h>#include <linux/signal.h>#include <linux/errno.h>#include <linux/random.h>#include <linux/poll.h>#include <linux/init.h>#include <linux/slab.h>#include <linux/spinlock.h>#include <linux/smp_lock.h>#include <linux/usb.h>#include <linux/proc_fs.h>#include <asm/atomic.h>#include <linux/blk.h>#include "../scsi/scsi.h"#include "../scsi/hosts.h"#include "../scsi/sd.h"#include "microtek.h"/* * Version Information */#define DRIVER_VERSION "v0.4.3"#define DRIVER_AUTHOR "John Fremlin <vii@penguinpowered.com>, Oliver Neukum <Oliver.Neukum@lrz.uni-muenchen.de>"#define DRIVER_DESC "Microtek Scanmaker X6 USB scanner driver"/* Should we do debugging? *///#define MTS_DO_DEBUG/* USB layer driver interface */static void *mts_usb_probe(struct usb_device *dev, unsigned int interface,			 const struct usb_device_id *id);static void mts_usb_disconnect(struct usb_device *dev, void *ptr);static struct usb_device_id mts_usb_ids [];static struct usb_driver mts_usb_driver = {	name:		"microtekX6",	probe:		mts_usb_probe,	disconnect:	mts_usb_disconnect,	id_table:	mts_usb_ids,};/* Internal driver stuff */#define MTS_VERSION	"0.4.3"#define MTS_NAME	"microtek usb (rev " MTS_VERSION "): "#define MTS_WARNING(x...) \	printk( KERN_WARNING MTS_NAME x )#define MTS_ERROR(x...) \	printk( KERN_ERR MTS_NAME x )#define MTS_INT_ERROR(x...) \	MTS_ERROR(x)#define MTS_MESSAGE(x...) \	printk( KERN_INFO MTS_NAME x )#if defined MTS_DO_DEBUG#define MTS_DEBUG(x...) \	printk( KERN_DEBUG MTS_NAME x )#define MTS_DEBUG_GOT_HERE() \	MTS_DEBUG("got to %s:%d (%s)\n", __FILE__, (int)__LINE__, __PRETTY_FUNCTION__ )#define MTS_DEBUG_INT() \	do { MTS_DEBUG_GOT_HERE(); \	     MTS_DEBUG("transfer = 0x%x context = 0x%x\n",(int)transfer,(int)context ); \	     MTS_DEBUG("status = 0x%x data-length = 0x%x sent = 0x%x\n",(int)transfer->status,(int)context->data_length, (int)transfer->actual_length ); \             mts_debug_dump(context->instance);\	   } while(0)#else#define MTS_NUL_STATEMENT do { } while(0)#define MTS_DEBUG(x...)	MTS_NUL_STATEMENT#define MTS_DEBUG_GOT_HERE() MTS_NUL_STATEMENT#define MTS_DEBUG_INT() MTS_NUL_STATEMENT#endif#define MTS_INT_INIT()\	struct mts_transfer_context* context = (struct mts_transfer_context*)transfer->context; \	MTS_DEBUG_INT();\#ifdef MTS_DO_DEBUGstatic inline void mts_debug_dump(struct mts_desc* desc) {	MTS_DEBUG("desc at 0x%x: halted = %02x%02x, toggle = %02x%02x\n",		  (int)desc,(int)desc->usb_dev->halted[1],(int)desc->usb_dev->halted[0],		  (int)desc->usb_dev->toggle[1],(int)desc->usb_dev->toggle[0]		);	MTS_DEBUG("ep_out=%x ep_response=%x ep_image=%x\n",		  usb_sndbulkpipe(desc->usb_dev,desc->ep_out),		  usb_rcvbulkpipe(desc->usb_dev,desc->ep_response),		  usb_rcvbulkpipe(desc->usb_dev,desc->ep_image)		);}static inline void mts_show_command(Scsi_Cmnd *srb){	char *what = NULL;	switch (srb->cmnd[0]) {	case TEST_UNIT_READY: what = "TEST_UNIT_READY"; break;	case REZERO_UNIT: what = "REZERO_UNIT"; break;	case REQUEST_SENSE: what = "REQUEST_SENSE"; break;	case FORMAT_UNIT: what = "FORMAT_UNIT"; break;	case READ_BLOCK_LIMITS: what = "READ_BLOCK_LIMITS"; break;	case REASSIGN_BLOCKS: what = "REASSIGN_BLOCKS"; break;	case READ_6: what = "READ_6"; break;	case WRITE_6: what = "WRITE_6"; break;	case SEEK_6: what = "SEEK_6"; break;	case READ_REVERSE: what = "READ_REVERSE"; break;	case WRITE_FILEMARKS: what = "WRITE_FILEMARKS"; break;	case SPACE: what = "SPACE"; break;	case INQUIRY: what = "INQUIRY"; break;	case RECOVER_BUFFERED_DATA: what = "RECOVER_BUFFERED_DATA"; break;	case MODE_SELECT: what = "MODE_SELECT"; break;	case RESERVE: what = "RESERVE"; break;	case RELEASE: what = "RELEASE"; break;	case COPY: what = "COPY"; break;	case ERASE: what = "ERASE"; break;	case MODE_SENSE: what = "MODE_SENSE"; break;	case START_STOP: what = "START_STOP"; break;	case RECEIVE_DIAGNOSTIC: what = "RECEIVE_DIAGNOSTIC"; break;	case SEND_DIAGNOSTIC: what = "SEND_DIAGNOSTIC"; break;	case ALLOW_MEDIUM_REMOVAL: what = "ALLOW_MEDIUM_REMOVAL"; break;	case SET_WINDOW: what = "SET_WINDOW"; break;	case READ_CAPACITY: what = "READ_CAPACITY"; break;	case READ_10: what = "READ_10"; break;	case WRITE_10: what = "WRITE_10"; break;	case SEEK_10: what = "SEEK_10"; break;	case WRITE_VERIFY: what = "WRITE_VERIFY"; break;	case VERIFY: what = "VERIFY"; break;	case SEARCH_HIGH: what = "SEARCH_HIGH"; break;	case SEARCH_EQUAL: what = "SEARCH_EQUAL"; break;	case SEARCH_LOW: what = "SEARCH_LOW"; break;	case SET_LIMITS: what = "SET_LIMITS"; break;	case READ_POSITION: what = "READ_POSITION"; break;	case SYNCHRONIZE_CACHE: what = "SYNCHRONIZE_CACHE"; break;	case LOCK_UNLOCK_CACHE: what = "LOCK_UNLOCK_CACHE"; break;	case READ_DEFECT_DATA: what = "READ_DEFECT_DATA"; break;	case MEDIUM_SCAN: what = "MEDIUM_SCAN"; break;	case COMPARE: what = "COMPARE"; break;	case COPY_VERIFY: what = "COPY_VERIFY"; break;	case WRITE_BUFFER: what = "WRITE_BUFFER"; break;	case READ_BUFFER: what = "READ_BUFFER"; break;	case UPDATE_BLOCK: what = "UPDATE_BLOCK"; break;	case READ_LONG: what = "READ_LONG"; break;	case WRITE_LONG: what = "WRITE_LONG"; break;	case CHANGE_DEFINITION: what = "CHANGE_DEFINITION"; break;	case WRITE_SAME: what = "WRITE_SAME"; break;	case READ_TOC: what = "READ_TOC"; break;	case LOG_SELECT: what = "LOG_SELECT"; break;	case LOG_SENSE: what = "LOG_SENSE"; break;	case MODE_SELECT_10: what = "MODE_SELECT_10"; break;	case MODE_SENSE_10: what = "MODE_SENSE_10"; break;	case MOVE_MEDIUM: what = "MOVE_MEDIUM"; break;	case READ_12: what = "READ_12"; break;	case WRITE_12: what = "WRITE_12"; break;	case WRITE_VERIFY_12: what = "WRITE_VERIFY_12"; break;	case SEARCH_HIGH_12: what = "SEARCH_HIGH_12"; break;	case SEARCH_EQUAL_12: what = "SEARCH_EQUAL_12"; break;	case SEARCH_LOW_12: what = "SEARCH_LOW_12"; break;	case READ_ELEMENT_STATUS: what = "READ_ELEMENT_STATUS"; break;	case SEND_VOLUME_TAG: what = "SEND_VOLUME_TAG"; break;	case WRITE_LONG_2: what = "WRITE_LONG_2"; break;	default:		MTS_DEBUG("can't decode command\n");		goto out;		break;	}	MTS_DEBUG( "Command %s (%d bytes)\n", what, srb->cmd_len); out:	MTS_DEBUG( "  %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",	       srb->cmnd[0], srb->cmnd[1], srb->cmnd[2], srb->cmnd[3], srb->cmnd[4], srb->cmnd[5],	       srb->cmnd[6], srb->cmnd[7], srb->cmnd[8], srb->cmnd[9]);}#elsestatic inline void mts_show_command(Scsi_Cmnd * dummy){}static inline void mts_debug_dump(struct mts_desc* dummy){}#endif/* static inline int mts_is_aborting(struct mts_desc* desc) {	return (atomic_read(&desc->context.do_abort));}  */static inline void mts_urb_abort(struct mts_desc* desc) {	MTS_DEBUG_GOT_HERE();	mts_debug_dump(desc);	usb_unlink_urb( &desc->urb );}static struct mts_desc * mts_list; /* list of active scanners */struct semaphore mts_list_semaphore;/* Internal list operations */staticvoid mts_remove_nolock( struct mts_desc* to_remove ){	MTS_DEBUG( "removing 0x%x from list\n",		   (int)to_remove );	lock_kernel();	mts_urb_abort(to_remove);	MTS_DEBUG_GOT_HERE();	if ( to_remove != mts_list ) {		MTS_DEBUG_GOT_HERE();		if (to_remove->prev && to_remove->next)			to_remove->prev->next = to_remove->next;	} else {		MTS_DEBUG_GOT_HERE();		mts_list = to_remove->next;		if (mts_list) {			MTS_DEBUG_GOT_HERE();			mts_list->prev = 0;		}	}	if ( to_remove->next ) {		MTS_DEBUG_GOT_HERE();		to_remove->next->prev = to_remove->prev;	}	MTS_DEBUG_GOT_HERE();	scsi_unregister_module(MODULE_SCSI_HA, &(to_remove->ctempl));	unlock_kernel();	kfree( to_remove );}staticvoid mts_add_nolock( struct mts_desc* to_add ){	MTS_DEBUG( "adding 0x%x to list\n", (int)to_add );	to_add->prev = 0;	to_add->next = mts_list;	if ( mts_list ) {		mts_list->prev = to_add;	}	mts_list = to_add;}/* SCSI driver interface *//* scsi related functions - dummies for now mostly */static int mts_scsi_release(struct Scsi_Host *psh){	MTS_DEBUG_GOT_HERE();	return 0;}static int mts_scsi_abort (Scsi_Cmnd *srb){	struct mts_desc* desc = (struct mts_desc*)(srb->host->hostdata[0]);	MTS_DEBUG_GOT_HERE();	mts_urb_abort(desc);	return SCSI_ABORT_PENDING;}static int mts_scsi_host_reset (Scsi_Cmnd *srb){	struct mts_desc* desc = (struct mts_desc*)(srb->host->hostdata[0]);	MTS_DEBUG_GOT_HERE();	mts_debug_dump(desc);	usb_reset_device(desc->usb_dev); /*FIXME: untested on new reset code */	return 0;  /* RANT why here 0 and not SUCCESS */}/* the core of the scsi part *//* faking a detection - which can't fail :-) */static int mts_scsi_detect (struct SHT * sht){	/* Whole function stolen from usb-storage */	struct mts_desc * desc = (struct mts_desc *)sht->proc_dir;	/* What a hideous hack! */	char local_name[48];	MTS_DEBUG_GOT_HERE();	/* set up the name of our subdirectory under /proc/scsi/ */	sprintf(local_name, "microtek-%d", desc->host_number);	sht->proc_name = kmalloc (strlen(local_name) + 1, GFP_KERNEL);	/* FIXME: where is this freed ? */	if (!sht->proc_name) {		MTS_ERROR( "unable to allocate memory for proc interface!!\n" );		return 0;	}	strcpy(sht->proc_name, local_name); 	sht->proc_dir = NULL;	/* In host->hostdata we store a pointer to desc */	desc->host = scsi_register(sht, sizeof(desc));	if (desc->host == NULL) {		MTS_ERROR("Cannot register due to low memory");		kfree(sht->proc_name);		return 0;	}	desc->host->hostdata[0] = (unsigned long)desc;/* FIXME: what if sizeof(void*) != sizeof(unsigned long)? */	return 1;}/* Main entrypoint: SCSI commands are dispatched to here */staticint mts_scsi_queuecommand (Scsi_Cmnd *srb, mts_scsi_cmnd_callback callback );static void mts_transfer_cleanup( struct urb *transfer );static void mts_do_sg(struct urb * transfer);inline staticvoid mts_int_submit_urb (struct urb* transfer,			int pipe,			void* data,			unsigned length,			mts_usb_urb_callback callback )/* Interrupt context! *//* Holding transfer->context->lock! */{	int res;	MTS_INT_INIT();	FILL_BULK_URB(transfer,		      context->instance->usb_dev,		      pipe,		      data,		      length,		      callback,		      context		);	transfer->status = 0;	res = usb_submit_urb( transfer );	if ( res ) {		MTS_INT_ERROR( "could not submit URB! Error was %d\n",(int)res );		context->srb->result = DID_ERROR << 16;		mts_transfer_cleanup(transfer);	}	return;}static void mts_transfer_cleanup( struct urb *transfer )/* Interrupt context! */{	MTS_INT_INIT();	if ( context->final_callback )		context->final_callback(context->srb);}static void mts_transfer_done( struct urb *transfer ){	MTS_INT_INIT();	context->srb->result &= MTS_SCSI_ERR_MASK;	context->srb->result |= (unsigned)context->status<<1;	mts_transfer_cleanup(transfer);

⌨️ 快捷键说明

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