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

📄 usb.c

📁 omap3 linux 2.6 用nocc去除了冗余代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/* Driver for USB Mass Storage compliant devices * * $Id: usb.c,v 1.75 2002/04/22 03:39:43 mdharm Exp $ * * Current development and maintenance by: *   (c) 1999-2003 Matthew Dharm (mdharm-usb@one-eyed-alien.net) * * Developed with the assistance of: *   (c) 2000 David L. Brown, Jr. (usb-storage@davidb.org) *   (c) 2003 Alan Stern (stern@rowland.harvard.edu) * * Initial work by: *   (c) 1999 Michael Gee (michael@linuxspecific.com) * * usb_device_id support by Adam J. Richter (adam@yggdrasil.com): *   (c) 2000 Yggdrasil Computing, Inc. * * This driver is based on the 'USB Mass Storage Class' document. This * describes in detail the protocol used to communicate with such * devices.  Clearly, the designers had SCSI and ATAPI commands in * mind when they created this document.  The commands are all very * similar to commands in the SCSI-II and ATAPI specifications. * * It is important to note that in a number of cases this class * exhibits class-specific exemptions from the USB specification. * Notably the usage of NAK, STALL and ACK differs from the norm, in * that they are used to communicate wait, failed and OK on commands. * * Also, for certain devices, the interrupt endpoint is used to convey * status of a command. * * Please see http://www.one-eyed-alien.net/~mdharm/linux-usb for more * information about this driver. * * 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 <linux/sched.h>#include <linux/errno.h>#include <linux/freezer.h>#include <linux/module.h>#include <linux/init.h>#include <linux/slab.h>#include <linux/kthread.h>#include <linux/mutex.h>#include <linux/utsname.h>#include <scsi/scsi.h>#include <scsi/scsi_cmnd.h>#include <scsi/scsi_device.h>#include "usb.h"#include "scsiglue.h"#include "transport.h"#include "protocol.h"#include "debug.h"#include "initializers.h"/* Some informational data */MODULE_AUTHOR("Matthew Dharm <mdharm-usb@one-eyed-alien.net>");MODULE_DESCRIPTION("USB Mass Storage driver for Linux");MODULE_LICENSE("GPL");static unsigned int delay_use = 5;module_param(delay_use, uint, S_IRUGO | S_IWUSR);MODULE_PARM_DESC(delay_use, "seconds to delay before using a new device");/* These are used to make sure the module doesn't unload before all the * threads have exited. */static atomic_t total_threads = ATOMIC_INIT(0);static DECLARE_COMPLETION(threads_gone);/* * The entries in this table correspond, line for line, * with the entries of us_unusual_dev_list[]. */#define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \		    vendorName, productName,useProtocol, useTransport, \		    initFunction, flags) \{ USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin,bcdDeviceMax), \  .driver_info = (flags)|(USB_US_TYPE_STOR<<24) }#define USUAL_DEV(useProto, useTrans, useType) \{ USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, useProto, useTrans), \  .driver_info = (USB_US_TYPE_STOR<<24) }static struct usb_device_id storage_usb_ids [] = {#	include "unusual_devs.h"#undef UNUSUAL_DEV#undef USUAL_DEV	/* Terminating entry */	{ }};MODULE_DEVICE_TABLE (usb, storage_usb_ids);/* This is the list of devices we recognize, along with their flag data *//* The vendor name should be kept at eight characters or less, and * the product name should be kept at 16 characters or less. If a device * has the US_FL_FIX_INQUIRY flag, then the vendor and product names * normally generated by a device thorugh the INQUIRY response will be * taken from this list, and this is the reason for the above size * restriction. However, if the flag is not present, then you * are free to use as many characters as you like. */#define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \		    vendor_name, product_name, use_protocol, use_transport, \		    init_function, Flags) \{ \	.vendorName = vendor_name,	\	.productName = product_name,	\	.useProtocol = use_protocol,	\	.useTransport = use_transport,	\	.initFunction = init_function,	\}#define USUAL_DEV(use_protocol, use_transport, use_type) \{ \	.useProtocol = use_protocol,	\	.useTransport = use_transport,	\}static struct us_unusual_dev us_unusual_dev_list[] = {#	include "unusual_devs.h" #	undef UNUSUAL_DEV#	undef USUAL_DEV	/* Terminating entry */	{ NULL }};static int storage_suspend(struct usb_interface *iface, pm_message_t message){	struct us_data *us = usb_get_intfdata(iface);	/* Wait until no command is running */	mutex_lock(&us->dev_mutex);	US_DEBUGP("%s\n", __FUNCTION__);	if (us->suspend_resume_hook)		(us->suspend_resume_hook)(us, US_SUSPEND);	iface->dev.power.power_state.event = message.event;	/* When runtime PM is working, we'll set a flag to indicate	 * whether we should autoresume when a SCSI request arrives. */	mutex_unlock(&us->dev_mutex);	return 0;}static int storage_resume(struct usb_interface *iface){	struct us_data *us = usb_get_intfdata(iface);	mutex_lock(&us->dev_mutex);	US_DEBUGP("%s\n", __FUNCTION__);	if (us->suspend_resume_hook)		(us->suspend_resume_hook)(us, US_RESUME);	iface->dev.power.power_state.event = PM_EVENT_ON;	mutex_unlock(&us->dev_mutex);	return 0;}/* * The next two routines get called just before and just after * a USB port reset, whether from this driver or a different one. */static void storage_pre_reset(struct usb_interface *iface){	struct us_data *us = usb_get_intfdata(iface);	US_DEBUGP("%s\n", __FUNCTION__);	/* Make sure no command runs during the reset */	mutex_lock(&us->dev_mutex);}static void storage_post_reset(struct usb_interface *iface){	struct us_data *us = usb_get_intfdata(iface);	US_DEBUGP("%s\n", __FUNCTION__);	/* Report the reset to the SCSI core */	scsi_lock(us_to_host(us));	usb_stor_report_bus_reset(us);	scsi_unlock(us_to_host(us));	/* FIXME: Notify the subdrivers that they need to reinitialize	 * the device */	mutex_unlock(&us->dev_mutex);}/* * fill_inquiry_response takes an unsigned char array (which must * be at least 36 characters) and populates the vendor name, * product name, and revision fields. Then the array is copied * into the SCSI command's response buffer (oddly enough * called request_buffer). data_len contains the length of the * data array, which again must be at least 36. */void fill_inquiry_response(struct us_data *us, unsigned char *data,		unsigned int data_len){	if (data_len<36) // You lose.		return;	if(data[0]&0x20) { /* USB device currently not connected. Return			      peripheral qualifier 001b ("...however, the			      physical device is not currently connected			      to this logical unit") and leave vendor and			      product identification empty. ("If the target			      does store some of the INQUIRY data on the			      device, it may return zeros or ASCII spaces 			      (20h) in those fields until the data is			      available from the device."). */		memset(data+8,0,28);	} else {		u16 bcdDevice = le16_to_cpu(us->pusb_dev->descriptor.bcdDevice);		memcpy(data+8, us->unusual_dev->vendorName, 			strlen(us->unusual_dev->vendorName) > 8 ? 8 :			strlen(us->unusual_dev->vendorName));		memcpy(data+16, us->unusual_dev->productName, 			strlen(us->unusual_dev->productName) > 16 ? 16 :			strlen(us->unusual_dev->productName));		data[32] = 0x30 + ((bcdDevice>>12) & 0x0F);		data[33] = 0x30 + ((bcdDevice>>8) & 0x0F);		data[34] = 0x30 + ((bcdDevice>>4) & 0x0F);		data[35] = 0x30 + ((bcdDevice) & 0x0F);	}	usb_stor_set_xfer_buf(data, data_len, us->srb);}static int usb_stor_control_thread(void * __us){	struct us_data *us = (struct us_data *)__us;	struct Scsi_Host *host = us_to_host(us);	current->flags |= PF_NOFREEZE;	for(;;) {		US_DEBUGP("*** thread sleeping.\n");		if(down_interruptible(&us->sema))			break;					US_DEBUGP("*** thread awakened.\n");		/* lock the device pointers */		mutex_lock(&(us->dev_mutex));		/* if the device has disconnected, we are free to exit */		if (test_bit(US_FLIDX_DISCONNECTING, &us->flags)) {			US_DEBUGP("-- exiting\n");			mutex_unlock(&us->dev_mutex);			break;		}		/* lock access to the state */		scsi_lock(host);		/* has the command timed out *already* ? */		if (test_bit(US_FLIDX_TIMED_OUT, &us->flags)) {			us->srb->result = DID_ABORT << 16;			goto SkipForAbort;		}		scsi_unlock(host);		/* reject the command if the direction indicator 		 * is UNKNOWN		 */		if (us->srb->sc_data_direction == DMA_BIDIRECTIONAL) {			US_DEBUGP("UNKNOWN data direction\n");			us->srb->result = DID_ERROR << 16;		}		/* reject if target != 0 or if LUN is higher than		 * the maximum known LUN		 */		else if (us->srb->device->id && 				!(us->flags & US_FL_SCM_MULT_TARG)) {			US_DEBUGP("Bad target number (%d:%d)\n",				  us->srb->device->id, us->srb->device->lun);			us->srb->result = DID_BAD_TARGET << 16;		}		else if (us->srb->device->lun > us->max_lun) {			US_DEBUGP("Bad LUN (%d:%d)\n",				  us->srb->device->id, us->srb->device->lun);			us->srb->result = DID_BAD_TARGET << 16;		}		/* Handle those devices which need us to fake 		 * their inquiry data */		else if ((us->srb->cmnd[0] == INQUIRY) &&			    (us->flags & US_FL_FIX_INQUIRY)) {			unsigned char data_ptr[36] = {			    0x00, 0x80, 0x02, 0x02,			    0x1F, 0x00, 0x00, 0x00};			US_DEBUGP("Faking INQUIRY command\n");			fill_inquiry_response(us, data_ptr, 36);			us->srb->result = SAM_STAT_GOOD;		}		/* we've got a command, let's do it! */		else {			US_DEBUG(usb_stor_show_command(us->srb));			us->proto_handler(us->srb, us);		}		/* lock access to the state */		scsi_lock(host);		/* did the command already complete because of a disconnect? */		if (!us->srb)			;		/* nothing to do */		/* indicate that the command is done */		else if (us->srb->result != DID_ABORT << 16) {			US_DEBUGP("scsi cmd done, result=0x%x\n", 				   us->srb->result);			us->srb->scsi_done(us->srb);		} else {SkipForAbort:			US_DEBUGP("scsi command aborted\n");		}		/* If an abort request was received we need to signal that		 * the abort has finished.  The proper test for this is		 * the TIMED_OUT flag, not srb->result == DID_ABORT, because		 * the timeout might have occurred after the command had		 * already completed with a different result code. */		if (test_bit(US_FLIDX_TIMED_OUT, &us->flags)) {			complete(&(us->notify));			/* Allow USB transfers to resume */			clear_bit(US_FLIDX_ABORTING, &us->flags);			clear_bit(US_FLIDX_TIMED_OUT, &us->flags);		}		/* finished working on this command */		us->srb = NULL;		scsi_unlock(host);		/* unlock the device pointers */		mutex_unlock(&us->dev_mutex);	} /* for (;;) */	scsi_host_put(host);	/* notify the exit routine that we're actually exiting now 	 *	 * complete()/wait_for_completion() is similar to up()/down(),	 * except that complete() is safe in the case where the structure	 * is getting deleted in a parallel mode of execution (i.e. just	 * after the down() -- that's necessary for the thread-shutdown	 * case.	 *	 * complete_and_exit() goes even further than this -- it is safe in	 * the case that the thread of the caller is going away (not just	 * the structure) -- this is necessary for the module-remove case.	 * This is important in preemption kernels, which transfer the flow	 * of execution immediately upon a complete().	 */	complete_and_exit(&threads_gone, 0);}	/*********************************************************************** * Device probing and disconnecting ***********************************************************************//* Associate our private data with the USB device */static int associate_dev(struct us_data *us, struct usb_interface *intf){	US_DEBUGP("-- %s\n", __FUNCTION__);	/* Fill in the device-related fields */	us->pusb_dev = interface_to_usbdev(intf);	us->pusb_intf = intf;	us->ifnum = intf->cur_altsetting->desc.bInterfaceNumber;	US_DEBUGP("Vendor: 0x%04x, Product: 0x%04x, Revision: 0x%04x\n",			le16_to_cpu(us->pusb_dev->descriptor.idVendor),			le16_to_cpu(us->pusb_dev->descriptor.idProduct),			le16_to_cpu(us->pusb_dev->descriptor.bcdDevice));	US_DEBUGP("Interface Subclass: 0x%02x, Protocol: 0x%02x\n",			intf->cur_altsetting->desc.bInterfaceSubClass,			intf->cur_altsetting->desc.bInterfaceProtocol);	/* Store our private data in the interface */	usb_set_intfdata(intf, us);	/* Allocate the device-related DMA-mapped buffers */	us->cr = usb_buffer_alloc(us->pusb_dev, sizeof(*us->cr),			GFP_KERNEL, &us->cr_dma);	if (!us->cr) {		US_DEBUGP("usb_ctrlrequest allocation failed\n");		return -ENOMEM;	}	us->iobuf = usb_buffer_alloc(us->pusb_dev, US_IOBUF_SIZE,			GFP_KERNEL, &us->iobuf_dma);	if (!us->iobuf) {		US_DEBUGP("I/O buffer allocation failed\n");		return -ENOMEM;	}	us->sensebuf = kmalloc(US_SENSE_SIZE, GFP_KERNEL);	if (!us->sensebuf) {		US_DEBUGP("Sense buffer allocation failed\n");		return -ENOMEM;	}	return 0;}/* Find an unusual_dev descriptor (always succeeds in the current code) */static struct us_unusual_dev *find_unusual(const struct usb_device_id *id){	const int id_index = id - storage_usb_ids;	return &us_unusual_dev_list[id_index];}/* Get the unusual_devs entries and the string descriptors */static int get_device_info(struct us_data *us, const struct usb_device_id *id){	struct usb_device *dev = us->pusb_dev;	struct usb_interface_descriptor *idesc =		&us->pusb_intf->cur_altsetting->desc;	struct us_unusual_dev *unusual_dev = find_unusual(id);	/* Store the entries */	us->unusual_dev = unusual_dev;	us->subclass = (unusual_dev->useProtocol == US_SC_DEVICE) ?			idesc->bInterfaceSubClass :			unusual_dev->useProtocol;	us->protocol = (unusual_dev->useTransport == US_PR_DEVICE) ?			idesc->bInterfaceProtocol :			unusual_dev->useTransport;	us->flags = USB_US_ORIG_FLAGS(id->driver_info);	if (us->flags & US_FL_IGNORE_DEVICE) {		printk(KERN_INFO USB_STORAGE "device ignored\n");		return -ENODEV;	}	/*	 * This flag is only needed when we're in high-speed, so let's	 * disable it if we're in full-speed	 */	if (dev->speed != USB_SPEED_HIGH)		us->flags &= ~US_FL_GO_SLOW;	/* Log a message if a non-generic unusual_dev entry contains an	 * unnecessary subclass or protocol override.  This may stimulate	 * reports from users that will help us remove unneeded entries	 * from the unusual_devs.h table.	 */	if (id->idVendor || id->idProduct) {		static const char *msgs[3] = {			"an unneeded SubClass entry",			"an unneeded Protocol entry",			"unneeded SubClass and Protocol entries"};		struct usb_device_descriptor *ddesc = &dev->descriptor;		int msg = -1;		if (unusual_dev->useProtocol != US_SC_DEVICE &&			us->subclass == idesc->bInterfaceSubClass)			msg += 1;

⌨️ 快捷键说明

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