1258 lines · 33.2 KB · C
RawDownload
// SPDX-License-Identifier: GPL-2.0+/* * Driver for USB Mass Storage compliant devices * * 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-2009 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. */ #ifdef CONFIG_USB_STORAGE_DEBUG#define DEBUG#endif #include <linux/sched.h>#include <linux/errno.h>#include <linux/module.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 <linux/usb/hcd.h>#include "scsiglue.h"#include "transport.h"#include "protocol.h"#include "debug.h"#include "initializers.h" #include "sierra_ms.h"#include "option_ms.h" #if IS_ENABLED(CONFIG_USB_UAS)#include "uas-detect.h"#endif #define DRV_NAME "usb-storage" /* 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 = 1 * MSEC_PER_SEC; /** * parse_delay_str - parse an unsigned decimal integer delay * @str: String to parse. * @ndecimals: Number of decimal to scale up. * @suffix: Suffix string to parse. * @val: Where to store the parsed value. * * Parse an unsigned decimal value in @str, optionally end with @suffix. * Stores the parsed value in @val just as it is if @str ends with @suffix. * Otherwise store the value scale up by 10^(@ndecimal). * * Returns 0 on success, a negative error code otherwise. */static int parse_delay_str(const char *str, int ndecimals, const char *suffix,			unsigned int *val){	int n, n2, l;	char buf[16]; 	l = strlen(suffix);	n = strlen(str);	if (n > 0 && str[n - 1] == '\n')		--n;	if (n >= l && !strncmp(&str[n - l], suffix, l)) {		n -= l;		n2 = 0;	} else		n2 = ndecimals; 	if (n + n2 > sizeof(buf) - 1)		return -EINVAL; 	memcpy(buf, str, n);	while (n2-- > 0)		buf[n++] = '0';	buf[n] = 0; 	return kstrtouint(buf, 10, val);} /** * format_delay_ms - format an integer value into a delay string * @val: The integer value to format, scaled by 10^(@ndecimals). * @ndecimals: Number of decimal to scale down. * @suffix: Suffix string to format. * @str: Where to store the formatted string. * @size: The size of buffer for @str. * * Format an integer value in @val scale down by 10^(@ndecimals) without @suffix * if @val is divisible by 10^(@ndecimals). * Otherwise format a value in @val just as it is with @suffix * * Returns the number of characters written into @str. */static int format_delay_ms(unsigned int val, int ndecimals, const char *suffix,			char *str, int size){	u64 delay_ms = val;	unsigned int rem = do_div(delay_ms, int_pow(10, ndecimals));	int ret; 	if (rem)		ret = scnprintf(str, size, "%u%s\n", val, suffix);	else		ret = scnprintf(str, size, "%u\n", (unsigned int)delay_ms);	return ret;} static int delay_use_set(const char *s, const struct kernel_param *kp){	unsigned int delay_ms;	int ret; 	ret = parse_delay_str(skip_spaces(s), 3, "ms", &delay_ms);	if (ret < 0)		return ret; 	*((unsigned int *)kp->arg) = delay_ms;	return 0;} static int delay_use_get(char *s, const struct kernel_param *kp){	unsigned int delay_ms = *((unsigned int *)kp->arg); 	return format_delay_ms(delay_ms, 3, "ms", s, PAGE_SIZE);} static const struct kernel_param_ops delay_use_ops = {	.set = delay_use_set,	.get = delay_use_get,};module_param_cb(delay_use, &delay_use_ops, &delay_use, 0644);MODULE_PARM_DESC(delay_use, "time to delay before using a new device"); static char quirks[128];module_param_string(quirks, quirks, sizeof(quirks), S_IRUGO | S_IWUSR);MODULE_PARM_DESC(quirks, "supplemental list of device IDs and their quirks");  /* * The entries in this table correspond, line for line, * with the entries in usb_storage_usb_ids[], defined in usual-tables.c. */ /* *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 through 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 COMPLIANT_DEV	UNUSUAL_DEV #define USUAL_DEV(use_protocol, use_transport) \{ \	.useProtocol = use_protocol,	\	.useTransport = use_transport,	\} static const struct us_unusual_dev us_unusual_dev_list[] = {#	include "unusual_devs.h"	{ }		/* Terminating entry */}; static const struct us_unusual_dev for_dynamic_ids =		USUAL_DEV(USB_SC_SCSI, USB_PR_BULK); #undef UNUSUAL_DEV#undef COMPLIANT_DEV#undef USUAL_DEV #ifdef CONFIG_LOCKDEP static struct lock_class_key us_interface_key[USB_MAXINTERFACES]; static void us_set_lock_class(struct mutex *mutex,		struct usb_interface *intf){	struct usb_device *udev = interface_to_usbdev(intf);	struct usb_host_config *config = udev->actconfig;	int i; 	for (i = 0; i < config->desc.bNumInterfaces; i++) {		if (config->interface[i] == intf)			break;	} 	BUG_ON(i == config->desc.bNumInterfaces); 	lockdep_set_class(mutex, &us_interface_key[i]);} #else static void us_set_lock_class(struct mutex *mutex,		struct usb_interface *intf){} #endif #ifdef CONFIG_PM	/* Minimal support for suspend and resume */ int usb_stor_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); 	if (us->suspend_resume_hook)		(us->suspend_resume_hook)(us, US_SUSPEND); 	/*	 * 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;}EXPORT_SYMBOL_GPL(usb_stor_suspend); int usb_stor_resume(struct usb_interface *iface){	struct us_data *us = usb_get_intfdata(iface); 	mutex_lock(&us->dev_mutex); 	if (us->suspend_resume_hook)		(us->suspend_resume_hook)(us, US_RESUME); 	mutex_unlock(&us->dev_mutex);	return 0;}EXPORT_SYMBOL_GPL(usb_stor_resume); int usb_stor_reset_resume(struct usb_interface *iface){	struct us_data *us = usb_get_intfdata(iface); 	/* Report the reset to the SCSI core */	usb_stor_report_bus_reset(us); 	/*	 * If any of the subdrivers implemented a reinitialization scheme,	 * this is where the callback would be invoked.	 */	return 0;}EXPORT_SYMBOL_GPL(usb_stor_reset_resume); #endif /* CONFIG_PM */ /* * The next two routines get called just before and just after * a USB port reset, whether from this driver or a different one. */ int usb_stor_pre_reset(struct usb_interface *iface){	struct us_data *us = usb_get_intfdata(iface); 	/* Make sure no command runs during the reset */	mutex_lock(&us->dev_mutex);	return 0;}EXPORT_SYMBOL_GPL(usb_stor_pre_reset); int usb_stor_post_reset(struct usb_interface *iface){	struct us_data *us = usb_get_intfdata(iface); 	/* Report the reset to the SCSI core */	usb_stor_report_bus_reset(us); 	/*	 * If any of the subdrivers implemented a reinitialization scheme,	 * this is where the callback would be invoked.	 */ 	mutex_unlock(&us->dev_mutex);	return 0;}EXPORT_SYMBOL_GPL(usb_stor_post_reset); /* * 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; 	memset(data+8, ' ', 28);	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.").			     */	} else {		u16 bcdDevice = le16_to_cpu(us->pusb_dev->descriptor.bcdDevice);		int n; 		n = strlen(us->unusual_dev->vendorName);		memcpy(data+8, us->unusual_dev->vendorName, min(8, n));		n = strlen(us->unusual_dev->productName);		memcpy(data+16, us->unusual_dev->productName, min(16, n)); 		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);}EXPORT_SYMBOL_GPL(fill_inquiry_response); static int usb_stor_control_thread(void * __us){	struct us_data *us = (struct us_data *)__us;	struct Scsi_Host *host = us_to_host(us);	struct scsi_cmnd *srb; 	for (;;) {		usb_stor_dbg(us, "*** thread sleeping\n");		if (wait_for_completion_interruptible(&us->cmnd_ready))			break; 		usb_stor_dbg(us, "*** thread awakened\n"); 		/* lock the device pointers */		mutex_lock(&(us->dev_mutex)); 		/* lock access to the state */		scsi_lock(host); 		/* When we are called with no command pending, we're done */		srb = us->srb;		if (srb == NULL) {			scsi_unlock(host);			mutex_unlock(&us->dev_mutex);			usb_stor_dbg(us, "-- exiting\n");			break;		} 		/* has the command timed out *already* ? */		if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) {			srb->result = DID_ABORT << 16;			goto SkipForAbort;		} 		scsi_unlock(host); 		/*		 * reject the command if the direction indicator		 * is UNKNOWN		 */		if (srb->sc_data_direction == DMA_BIDIRECTIONAL) {			usb_stor_dbg(us, "UNKNOWN data direction\n");			srb->result = DID_ERROR << 16;		} 		/*		 * reject if target != 0 or if LUN is higher than		 * the maximum known LUN		 */		else if (srb->device->id &&				!(us->fflags & US_FL_SCM_MULT_TARG)) {			usb_stor_dbg(us, "Bad target number (%d:%llu)\n",				     srb->device->id,				     srb->device->lun);			srb->result = DID_BAD_TARGET << 16;		} 		else if (srb->device->lun > us->max_lun) {			usb_stor_dbg(us, "Bad LUN (%d:%llu)\n",				     srb->device->id,				     srb->device->lun);			srb->result = DID_BAD_TARGET << 16;		} 		/*		 * Handle those devices which need us to fake		 * their inquiry data		 */		else if ((srb->cmnd[0] == INQUIRY) &&			    (us->fflags & US_FL_FIX_INQUIRY)) {			unsigned char data_ptr[36] = {			    0x00, 0x80, 0x02, 0x02,			    0x1F, 0x00, 0x00, 0x00}; 			usb_stor_dbg(us, "Faking INQUIRY command\n");			fill_inquiry_response(us, data_ptr, 36);			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(srb, us);			usb_mark_last_busy(us->pusb_dev);		} 		/* lock access to the state */		scsi_lock(host); 		/* was the command aborted? */		if (srb->result == DID_ABORT << 16) {SkipForAbort:			usb_stor_dbg(us, "scsi command aborted\n");			srb = NULL;	/* Don't call scsi_done() */		} 		/*		 * 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->dflags)) {			complete(&(us->notify)); 			/* Allow USB transfers to resume */			clear_bit(US_FLIDX_ABORTING, &us->dflags);			clear_bit(US_FLIDX_TIMED_OUT, &us->dflags);		} 		/* finished working on this command */		us->srb = NULL;		scsi_unlock(host); 		/* unlock the device pointers */		mutex_unlock(&us->dev_mutex); 		/* now that the locks are released, notify the SCSI core */		if (srb) {			usb_stor_dbg(us, "scsi cmd done, result=0x%x\n",

Lines 1–500 of 1258.