usb.c 31.4 KB
Newer Older
1
2
/*
 * Driver for USB Mass Storage compliant devices
Linus Torvalds's avatar
Linus Torvalds committed
3
4
5
6
7
8
 *
 * 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)
9
 *   (c) 2003-2009 Alan Stern (stern@rowland.harvard.edu)
Linus Torvalds's avatar
Linus Torvalds committed
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
 *
 * 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.
 */

49
50
51
52
#ifdef CONFIG_USB_STORAGE_DEBUG
#define DEBUG
#endif

Linus Torvalds's avatar
Linus Torvalds committed
53
54
#include <linux/sched.h>
#include <linux/errno.h>
55
#include <linux/freezer.h>
Linus Torvalds's avatar
Linus Torvalds committed
56
57
#include <linux/module.h>
#include <linux/slab.h>
58
#include <linux/kthread.h>
59
#include <linux/mutex.h>
60
#include <linux/utsname.h>
Linus Torvalds's avatar
Linus Torvalds committed
61
62
63
64
65
66
67
68
69
70
71
72

#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"

73
#include "sierra_ms.h"
74
#include "option_ms.h"
Linus Torvalds's avatar
Linus Torvalds committed
75

76
77
78
79
#if IS_ENABLED(CONFIG_USB_UAS)
#include "uas-detect.h"
#endif

80
81
#define DRV_NAME "usb-storage"

Linus Torvalds's avatar
Linus Torvalds committed
82
83
84
85
86
/* Some informational data */
MODULE_AUTHOR("Matthew Dharm <mdharm-usb@one-eyed-alien.net>");
MODULE_DESCRIPTION("USB Mass Storage driver for Linux");
MODULE_LICENSE("GPL");

87
static unsigned int delay_use = 1;
Linus Torvalds's avatar
Linus Torvalds committed
88
89
90
module_param(delay_use, uint, S_IRUGO | S_IWUSR);
MODULE_PARM_DESC(delay_use, "seconds to delay before using a new device");

91
92
static char quirks[128];
module_param_string(quirks, quirks, sizeof(quirks), S_IRUGO | S_IWUSR);
93
94
MODULE_PARM_DESC(quirks, "supplemental list of device IDs and their quirks");

Linus Torvalds's avatar
Linus Torvalds committed
95

96
97
/*
 * The entries in this table correspond, line for line,
98
 * with the entries in usb_storage_usb_ids[], defined in usual-tables.c.
Linus Torvalds's avatar
Linus Torvalds committed
99
100
 */

101
102
/*
 *The vendor name should be kept at eight characters or less, and
Linus Torvalds's avatar
Linus Torvalds committed
103
104
 * 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
Mickael Maison's avatar
Mickael Maison committed
105
 * normally generated by a device through the INQUIRY response will be
Linus Torvalds's avatar
Linus Torvalds committed
106
107
108
109
110
111
112
113
114
115
116
117
118
119
 * 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,	\
120
121
}

122
123
#define COMPLIANT_DEV	UNUSUAL_DEV

124
#define USUAL_DEV(use_protocol, use_transport) \
125
126
127
{ \
	.useProtocol = use_protocol,	\
	.useTransport = use_transport,	\
Linus Torvalds's avatar
Linus Torvalds committed
128
129
}

130
131
132
133
134
135
136
137
138
139
140
#define UNUSUAL_VENDOR_INTF(idVendor, cl, sc, pr, \
		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,	\
}

Linus Torvalds's avatar
Linus Torvalds committed
141
static struct us_unusual_dev us_unusual_dev_list[] = {
142
#	include "unusual_devs.h"
143
	{ }		/* Terminating entry */
Linus Torvalds's avatar
Linus Torvalds committed
144
145
};

146
static struct us_unusual_dev for_dynamic_ids =
147
		USUAL_DEV(USB_SC_SCSI, USB_PR_BULK);
148

149
150
151
#undef UNUSUAL_DEV
#undef COMPLIANT_DEV
#undef USUAL_DEV
152
#undef UNUSUAL_VENDOR_INTF
153

154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#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
183
184
185

#ifdef CONFIG_PM	/* Minimal support for suspend and resume */

186
int usb_stor_suspend(struct usb_interface *iface, pm_message_t message)
187
188
189
190
{
	struct us_data *us = usb_get_intfdata(iface);

	/* Wait until no command is running */
191
	mutex_lock(&us->dev_mutex);
192

193
194
	if (us->suspend_resume_hook)
		(us->suspend_resume_hook)(us, US_SUSPEND);
195

196
197
198
199
	/*
	 * When runtime PM is working, we'll set a flag to indicate
	 * whether we should autoresume when a SCSI request arrives.
	 */
200

201
	mutex_unlock(&us->dev_mutex);
202
203
	return 0;
}
204
EXPORT_SYMBOL_GPL(usb_stor_suspend);
205

206
int usb_stor_resume(struct usb_interface *iface)
207
208
209
{
	struct us_data *us = usb_get_intfdata(iface);

210
	mutex_lock(&us->dev_mutex);
211

212
213
	if (us->suspend_resume_hook)
		(us->suspend_resume_hook)(us, US_RESUME);
214

215
	mutex_unlock(&us->dev_mutex);
216
217
	return 0;
}
218
EXPORT_SYMBOL_GPL(usb_stor_resume);
219

220
int usb_stor_reset_resume(struct usb_interface *iface)
Alan Stern's avatar
Alan Stern committed
221
222
223
224
225
226
{
	struct us_data *us = usb_get_intfdata(iface);

	/* Report the reset to the SCSI core */
	usb_stor_report_bus_reset(us);

227
228
229
230
	/*
	 * FIXME: Notify the subdrivers that they need to reinitialize
	 * the device
	 */
Alan Stern's avatar
Alan Stern committed
231
232
	return 0;
}
233
EXPORT_SYMBOL_GPL(usb_stor_reset_resume);
Alan Stern's avatar
Alan Stern committed
234

235
#endif /* CONFIG_PM */
Linus Torvalds's avatar
Linus Torvalds committed
236

237
238
239
240
241
/*
 * The next two routines get called just before and just after
 * a USB port reset, whether from this driver or a different one.
 */

242
int usb_stor_pre_reset(struct usb_interface *iface)
243
244
245
246
247
{
	struct us_data *us = usb_get_intfdata(iface);

	/* Make sure no command runs during the reset */
	mutex_lock(&us->dev_mutex);
Alan Stern's avatar
Alan Stern committed
248
	return 0;
249
}
250
EXPORT_SYMBOL_GPL(usb_stor_pre_reset);
251

252
int usb_stor_post_reset(struct usb_interface *iface)
253
254
255
256
257
258
{
	struct us_data *us = usb_get_intfdata(iface);

	/* Report the reset to the SCSI core */
	usb_stor_report_bus_reset(us);

259
260
261
262
	/*
	 * FIXME: Notify the subdrivers that they need to reinitialize
	 * the device
	 */
Alan Stern's avatar
Alan Stern committed
263

Alan Stern's avatar
Alan Stern committed
264
265
	mutex_unlock(&us->dev_mutex);
	return 0;
266
}
267
EXPORT_SYMBOL_GPL(usb_stor_post_reset);
268

Linus Torvalds's avatar
Linus Torvalds committed
269
270
271
272
273
274
275
276
277
278
279
280
/*
 * 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)
{
281
	if (data_len < 36) /* You lose. */
Linus Torvalds's avatar
Linus Torvalds committed
282
283
		return;

284
	memset(data+8, ' ', 28);
285
286
287
288
289
290
291
292
293
294
295
	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.").
			     */
Linus Torvalds's avatar
Linus Torvalds committed
296
297
	} else {
		u16 bcdDevice = le16_to_cpu(us->pusb_dev->descriptor.bcdDevice);
298
299
300
301
302
303
304
		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));

Linus Torvalds's avatar
Linus Torvalds committed
305
306
307
308
309
310
311
312
		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);
}
313
EXPORT_SYMBOL_GPL(fill_inquiry_response);
Linus Torvalds's avatar
Linus Torvalds committed
314
315
316
317
318
319

static int usb_stor_control_thread(void * __us)
{
	struct us_data *us = (struct us_data *)__us;
	struct Scsi_Host *host = us_to_host(us);

320
	for (;;) {
321
		usb_stor_dbg(us, "*** thread sleeping\n");
322
		if (wait_for_completion_interruptible(&us->cmnd_ready))
Linus Torvalds's avatar
Linus Torvalds committed
323
			break;
324

325
		usb_stor_dbg(us, "*** thread awakened\n");
Linus Torvalds's avatar
Linus Torvalds committed
326
327

		/* lock the device pointers */
328
		mutex_lock(&(us->dev_mutex));
Linus Torvalds's avatar
Linus Torvalds committed
329

330
331
332
333
334
335
		/* lock access to the state */
		scsi_lock(host);

		/* When we are called with no command pending, we're done */
		if (us->srb == NULL) {
			scsi_unlock(host);
336
			mutex_unlock(&us->dev_mutex);
337
			usb_stor_dbg(us, "-- exiting\n");
Linus Torvalds's avatar
Linus Torvalds committed
338
339
340
341
			break;
		}

		/* has the command timed out *already* ? */
342
		if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) {
Linus Torvalds's avatar
Linus Torvalds committed
343
344
345
346
347
348
			us->srb->result = DID_ABORT << 16;
			goto SkipForAbort;
		}

		scsi_unlock(host);

349
350
		/*
		 * reject the command if the direction indicator
Linus Torvalds's avatar
Linus Torvalds committed
351
352
353
		 * is UNKNOWN
		 */
		if (us->srb->sc_data_direction == DMA_BIDIRECTIONAL) {
354
			usb_stor_dbg(us, "UNKNOWN data direction\n");
Linus Torvalds's avatar
Linus Torvalds committed
355
356
357
			us->srb->result = DID_ERROR << 16;
		}

358
359
		/*
		 * reject if target != 0 or if LUN is higher than
Linus Torvalds's avatar
Linus Torvalds committed
360
361
		 * the maximum known LUN
		 */
362
		else if (us->srb->device->id &&
363
				!(us->fflags & US_FL_SCM_MULT_TARG)) {
Hannes Reinecke's avatar
Hannes Reinecke committed
364
365
366
			usb_stor_dbg(us, "Bad target number (%d:%llu)\n",
				     us->srb->device->id,
				     us->srb->device->lun);
Linus Torvalds's avatar
Linus Torvalds committed
367
368
369
370
			us->srb->result = DID_BAD_TARGET << 16;
		}

		else if (us->srb->device->lun > us->max_lun) {
Hannes Reinecke's avatar
Hannes Reinecke committed
371
372
373
			usb_stor_dbg(us, "Bad LUN (%d:%llu)\n",
				     us->srb->device->id,
				     us->srb->device->lun);
Linus Torvalds's avatar
Linus Torvalds committed
374
375
376
			us->srb->result = DID_BAD_TARGET << 16;
		}

377
378
379
380
		/*
		 * Handle those devices which need us to fake
		 * their inquiry data
		 */
Linus Torvalds's avatar
Linus Torvalds committed
381
		else if ((us->srb->cmnd[0] == INQUIRY) &&
382
			    (us->fflags & US_FL_FIX_INQUIRY)) {
Linus Torvalds's avatar
Linus Torvalds committed
383
384
385
386
			unsigned char data_ptr[36] = {
			    0x00, 0x80, 0x02, 0x02,
			    0x1F, 0x00, 0x00, 0x00};

387
			usb_stor_dbg(us, "Faking INQUIRY command\n");
Linus Torvalds's avatar
Linus Torvalds committed
388
389
390
391
392
393
			fill_inquiry_response(us, data_ptr, 36);
			us->srb->result = SAM_STAT_GOOD;
		}

		/* we've got a command, let's do it! */
		else {
394
			US_DEBUG(usb_stor_show_command(us, us->srb));
Linus Torvalds's avatar
Linus Torvalds committed
395
			us->proto_handler(us->srb, us);
396
			usb_mark_last_busy(us->pusb_dev);
Linus Torvalds's avatar
Linus Torvalds committed
397
398
399
400
401
402
		}

		/* lock access to the state */
		scsi_lock(host);

		/* indicate that the command is done */
403
		if (us->srb->result != DID_ABORT << 16) {
404
405
			usb_stor_dbg(us, "scsi cmd done, result=0x%x\n",
				     us->srb->result);
Linus Torvalds's avatar
Linus Torvalds committed
406
407
408
			us->srb->scsi_done(us->srb);
		} else {
SkipForAbort:
409
			usb_stor_dbg(us, "scsi command aborted\n");
Linus Torvalds's avatar
Linus Torvalds committed
410
411
		}

412
413
		/*
		 * If an abort request was received we need to signal that
Linus Torvalds's avatar
Linus Torvalds committed
414
415
		 * the abort has finished.  The proper test for this is
		 * the TIMED_OUT flag, not srb->result == DID_ABORT, because
416
		 * the timeout might have occurred after the command had
417
418
		 * already completed with a different result code.
		 */
419
		if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) {
Linus Torvalds's avatar
Linus Torvalds committed
420
421
			complete(&(us->notify));

422
			/* Allow USB transfers to resume */
423
424
			clear_bit(US_FLIDX_ABORTING, &us->dflags);
			clear_bit(US_FLIDX_TIMED_OUT, &us->dflags);
425
426
		}

Linus Torvalds's avatar
Linus Torvalds committed
427
428
429
430
431
		/* finished working on this command */
		us->srb = NULL;
		scsi_unlock(host);

		/* unlock the device pointers */
432
		mutex_unlock(&us->dev_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
433
434
	} /* for (;;) */

435
436
437
438
439
440
441
442
443
	/* Wait until we are told to stop */
	for (;;) {
		set_current_state(TASK_INTERRUPTIBLE);
		if (kthread_should_stop())
			break;
		schedule();
	}
	__set_current_state(TASK_RUNNING);
	return 0;
444
}
Linus Torvalds's avatar
Linus Torvalds committed
445
446
447
448
449
450
451
452
453
454
455
456

/***********************************************************************
 * Device probing and disconnecting
 ***********************************************************************/

/* Associate our private data with the USB device */
static int associate_dev(struct us_data *us, struct usb_interface *intf)
{
	/* Fill in the device-related fields */
	us->pusb_dev = interface_to_usbdev(intf);
	us->pusb_intf = intf;
	us->ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
457
458
459
460
461
462
463
	usb_stor_dbg(us, "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));
	usb_stor_dbg(us, "Interface Subclass: 0x%02x, Protocol: 0x%02x\n",
		     intf->cur_altsetting->desc.bInterfaceSubClass,
		     intf->cur_altsetting->desc.bInterfaceProtocol);
Linus Torvalds's avatar
Linus Torvalds committed
464
465
466
467

	/* Store our private data in the interface */
	usb_set_intfdata(intf, us);

468
469
	/* Allocate the control/setup and DMA-mapped buffers */
	us->cr = kmalloc(sizeof(*us->cr), GFP_KERNEL);
470
	if (!us->cr)
Linus Torvalds's avatar
Linus Torvalds committed
471
472
		return -ENOMEM;

473
	us->iobuf = usb_alloc_coherent(us->pusb_dev, US_IOBUF_SIZE,
Linus Torvalds's avatar
Linus Torvalds committed
474
475
			GFP_KERNEL, &us->iobuf_dma);
	if (!us->iobuf) {
476
		usb_stor_dbg(us, "I/O buffer allocation failed\n");
Linus Torvalds's avatar
Linus Torvalds committed
477
478
479
480
481
		return -ENOMEM;
	}
	return 0;
}

482
483
484
/* Works only for digits and letters, but small and fast */
#define TOLOWER(x) ((x) | 0x20)

485
/* Adjust device flags based on the "quirks=" module parameter */
486
void usb_stor_adjust_quirks(struct usb_device *udev, unsigned long *fflags)
487
{
488
	char *p;
489
490
	u16 vid = le16_to_cpu(udev->descriptor.idVendor);
	u16 pid = le16_to_cpu(udev->descriptor.idProduct);
491
	unsigned f = 0;
Alan Stern's avatar
Alan Stern committed
492
	unsigned int mask = (US_FL_SANE_SENSE | US_FL_BAD_SENSE |
493
			US_FL_FIX_CAPACITY | US_FL_IGNORE_UAS |
494
			US_FL_CAPACITY_HEURISTICS | US_FL_IGNORE_DEVICE |
495
			US_FL_NOT_LOCKABLE | US_FL_MAX_SECTORS_64 |
496
			US_FL_CAPACITY_OK | US_FL_IGNORE_RESIDUE |
497
			US_FL_SINGLE_LUN | US_FL_NO_WP_DETECT |
Alan Stern's avatar
Alan Stern committed
498
			US_FL_NO_READ_DISC_INFO | US_FL_NO_READ_CAPACITY_16 |
499
			US_FL_INITIAL_READ10 | US_FL_WRITE_CACHE |
500
			US_FL_NO_ATA_1X | US_FL_NO_REPORT_OPCODES |
501
502
			US_FL_MAX_SECTORS_240 | US_FL_NO_REPORT_LUNS |
			US_FL_ALWAYS_SYNC);
503
504
505
506
507
508
509
510

	p = quirks;
	while (*p) {
		/* Each entry consists of VID:PID:flags */
		if (vid == simple_strtoul(p, &p, 16) &&
				*p == ':' &&
				pid == simple_strtoul(p+1, &p, 16) &&
				*p == ':')
511
			break;
512
513
514
515
516

		/* Move forward to the next entry */
		while (*p) {
			if (*p++ == ',')
				break;
517
518
		}
	}
519
520
521
522
523
524
525
526
527
	if (!*p)	/* No match */
		return;

	/* Collect the flags */
	while (*++p && *p != ',') {
		switch (TOLOWER(*p)) {
		case 'a':
			f |= US_FL_SANE_SENSE;
			break;
528
529
530
		case 'b':
			f |= US_FL_BAD_SENSE;
			break;
531
532
533
		case 'c':
			f |= US_FL_FIX_CAPACITY;
			break;
534
535
536
537
538
539
		case 'd':
			f |= US_FL_NO_READ_DISC_INFO;
			break;
		case 'e':
			f |= US_FL_NO_READ_CAPACITY_16;
			break;
540
541
542
		case 'f':
			f |= US_FL_NO_REPORT_OPCODES;
			break;
543
544
545
		case 'g':
			f |= US_FL_MAX_SECTORS_240;
			break;
546
547
548
549
550
551
		case 'h':
			f |= US_FL_CAPACITY_HEURISTICS;
			break;
		case 'i':
			f |= US_FL_IGNORE_DEVICE;
			break;
552
553
554
		case 'j':
			f |= US_FL_NO_REPORT_LUNS;
			break;
555
556
557
558
559
560
		case 'l':
			f |= US_FL_NOT_LOCKABLE;
			break;
		case 'm':
			f |= US_FL_MAX_SECTORS_64;
			break;
Alan Stern's avatar
Alan Stern committed
561
562
563
		case 'n':
			f |= US_FL_INITIAL_READ10;
			break;
564
565
566
		case 'o':
			f |= US_FL_CAPACITY_OK;
			break;
567
568
569
		case 'p':
			f |= US_FL_WRITE_CACHE;
			break;
570
571
572
573
574
575
		case 'r':
			f |= US_FL_IGNORE_RESIDUE;
			break;
		case 's':
			f |= US_FL_SINGLE_LUN;
			break;
576
577
578
		case 't':
			f |= US_FL_NO_ATA_1X;
			break;
579
580
581
		case 'u':
			f |= US_FL_IGNORE_UAS;
			break;
582
583
584
		case 'w':
			f |= US_FL_NO_WP_DETECT;
			break;
585
586
587
		case 'y':
			f |= US_FL_ALWAYS_SYNC;
			break;
588
589
590
		/* Ignore unrecognized flag characters */
		}
	}
591
	*fflags = (*fflags & ~mask) | f;
592
}
593
EXPORT_SYMBOL_GPL(usb_stor_adjust_quirks);
594

Linus Torvalds's avatar
Linus Torvalds committed
595
/* Get the unusual_devs entries and the string descriptors */
596
597
static int get_device_info(struct us_data *us, const struct usb_device_id *id,
		struct us_unusual_dev *unusual_dev)
Linus Torvalds's avatar
Linus Torvalds committed
598
599
600
601
{
	struct usb_device *dev = us->pusb_dev;
	struct usb_interface_descriptor *idesc =
		&us->pusb_intf->cur_altsetting->desc;
602
	struct device *pdev = &us->pusb_intf->dev;
Linus Torvalds's avatar
Linus Torvalds committed
603
604
605

	/* Store the entries */
	us->unusual_dev = unusual_dev;
606
	us->subclass = (unusual_dev->useProtocol == USB_SC_DEVICE) ?
Linus Torvalds's avatar
Linus Torvalds committed
607
608
			idesc->bInterfaceSubClass :
			unusual_dev->useProtocol;
609
	us->protocol = (unusual_dev->useTransport == USB_PR_DEVICE) ?
Linus Torvalds's avatar
Linus Torvalds committed
610
611
			idesc->bInterfaceProtocol :
			unusual_dev->useTransport;
612
	us->fflags = id->driver_info;
613
	usb_stor_adjust_quirks(us->pusb_dev, &us->fflags);
Linus Torvalds's avatar
Linus Torvalds committed
614

615
	if (us->fflags & US_FL_IGNORE_DEVICE) {
616
		dev_info(pdev, "device ignored\n");
617
618
619
		return -ENODEV;
	}

Linus Torvalds's avatar
Linus Torvalds committed
620
621
622
623
624
	/*
	 * 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)
625
		us->fflags &= ~US_FL_GO_SLOW;
Linus Torvalds's avatar
Linus Torvalds committed
626

627
628
629
630
631
632
	if (us->fflags)
		dev_info(pdev, "Quirks match for vid %04x pid %04x: %lx\n",
				le16_to_cpu(dev->descriptor.idVendor),
				le16_to_cpu(dev->descriptor.idProduct),
				us->fflags);

633
634
	/*
	 * Log a message if a non-generic unusual_dev entry contains an
Linus Torvalds's avatar
Linus Torvalds committed
635
636
637
638
639
	 * 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) {
640
		static const char *msgs[3] = {
Linus Torvalds's avatar
Linus Torvalds committed
641
642
643
644
645
646
			"an unneeded SubClass entry",
			"an unneeded Protocol entry",
			"unneeded SubClass and Protocol entries"};
		struct usb_device_descriptor *ddesc = &dev->descriptor;
		int msg = -1;

647
		if (unusual_dev->useProtocol != USB_SC_DEVICE &&
Linus Torvalds's avatar
Linus Torvalds committed
648
649
			us->subclass == idesc->bInterfaceSubClass)
			msg += 1;
650
		if (unusual_dev->useTransport != USB_PR_DEVICE &&
Linus Torvalds's avatar
Linus Torvalds committed
651
652
			us->protocol == idesc->bInterfaceProtocol)
			msg += 2;
653
		if (msg >= 0 && !(us->fflags & US_FL_NEED_OVERRIDE))
654
655
656
657
658
659
660
661
662
663
664
665
666
667
			dev_notice(pdev, "This device "
					"(%04x,%04x,%04x S %02x P %02x)"
					" has %s in unusual_devs.h (kernel"
					" %s)\n"
					"   Please send a copy of this message to "
					"<linux-usb@vger.kernel.org> and "
					"<usb-storage@lists.one-eyed-alien.net>\n",
					le16_to_cpu(ddesc->idVendor),
					le16_to_cpu(ddesc->idProduct),
					le16_to_cpu(ddesc->bcdDevice),
					idesc->bInterfaceSubClass,
					idesc->bInterfaceProtocol,
					msgs[msg],
					utsname()->release);
Linus Torvalds's avatar
Linus Torvalds committed
668
	}
669
670

	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
671
672
673
}

/* Get the transport settings */
674
static void get_transport(struct us_data *us)
Linus Torvalds's avatar
Linus Torvalds committed
675
676
{
	switch (us->protocol) {
677
	case USB_PR_CB:
Linus Torvalds's avatar
Linus Torvalds committed
678
679
680
681
682
683
		us->transport_name = "Control/Bulk";
		us->transport = usb_stor_CB_transport;
		us->transport_reset = usb_stor_CB_reset;
		us->max_lun = 7;
		break;

684
	case USB_PR_CBI:
Linus Torvalds's avatar
Linus Torvalds committed
685
		us->transport_name = "Control/Bulk/Interrupt";
686
		us->transport = usb_stor_CB_transport;
Linus Torvalds's avatar
Linus Torvalds committed
687
688
689
690
		us->transport_reset = usb_stor_CB_reset;
		us->max_lun = 7;
		break;

691
	case USB_PR_BULK:
Linus Torvalds's avatar
Linus Torvalds committed
692
693
694
695
696
697
698
699
		us->transport_name = "Bulk";
		us->transport = usb_stor_Bulk_transport;
		us->transport_reset = usb_stor_Bulk_reset;
		break;
	}
}

/* Get the protocol settings */
700
static void get_protocol(struct us_data *us)
Linus Torvalds's avatar
Linus Torvalds committed
701
702
{
	switch (us->subclass) {
703
	case USB_SC_RBC:
Linus Torvalds's avatar
Linus Torvalds committed
704
705
706
707
		us->protocol_name = "Reduced Block Commands (RBC)";
		us->proto_handler = usb_stor_transparent_scsi_command;
		break;

708
	case USB_SC_8020:
Linus Torvalds's avatar
Linus Torvalds committed
709
		us->protocol_name = "8020i";
710
		us->proto_handler = usb_stor_pad12_command;
Linus Torvalds's avatar
Linus Torvalds committed
711
712
713
		us->max_lun = 0;
		break;

714
	case USB_SC_QIC:
Linus Torvalds's avatar
Linus Torvalds committed
715
		us->protocol_name = "QIC-157";
716
		us->proto_handler = usb_stor_pad12_command;
Linus Torvalds's avatar
Linus Torvalds committed
717
718
719
		us->max_lun = 0;
		break;

720
	case USB_SC_8070:
Linus Torvalds's avatar
Linus Torvalds committed
721
		us->protocol_name = "8070i";
722
		us->proto_handler = usb_stor_pad12_command;
Linus Torvalds's avatar
Linus Torvalds committed
723
724
725
		us->max_lun = 0;
		break;

726
	case USB_SC_SCSI:
Linus Torvalds's avatar
Linus Torvalds committed
727
728
729
730
		us->protocol_name = "Transparent SCSI";
		us->proto_handler = usb_stor_transparent_scsi_command;
		break;

731
	case USB_SC_UFI:
Linus Torvalds's avatar
Linus Torvalds committed
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
		us->protocol_name = "Uniform Floppy Interface (UFI)";
		us->proto_handler = usb_stor_ufi_command;
		break;
	}
}

/* Get the pipe settings */
static int get_pipes(struct us_data *us)
{
	struct usb_host_interface *altsetting =
		us->pusb_intf->cur_altsetting;
	int i;
	struct usb_endpoint_descriptor *ep;
	struct usb_endpoint_descriptor *ep_in = NULL;
	struct usb_endpoint_descriptor *ep_out = NULL;
	struct usb_endpoint_descriptor *ep_int = NULL;

	/*
750
	 * Find the first endpoint of each type we need.
Linus Torvalds's avatar
Linus Torvalds committed
751
	 * We are expecting a minimum of 2 endpoints - in and out (bulk).
752
	 * An optional interrupt-in is OK (necessary for CBI protocol).
Linus Torvalds's avatar
Linus Torvalds committed
753
754
755
756
757
	 * We will ignore any others.
	 */
	for (i = 0; i < altsetting->desc.bNumEndpoints; i++) {
		ep = &altsetting->endpoint[i].desc;

758
		if (usb_endpoint_xfer_bulk(ep)) {
759
760
761
762
763
764
765
			if (usb_endpoint_dir_in(ep)) {
				if (!ep_in)
					ep_in = ep;
			} else {
				if (!ep_out)
					ep_out = ep;
			}
Linus Torvalds's avatar
Linus Torvalds committed
766
767
		}

768
769
770
		else if (usb_endpoint_is_int_in(ep)) {
			if (!ep_int)
				ep_int = ep;
Linus Torvalds's avatar
Linus Torvalds committed
771
772
773
		}
	}

774
	if (!ep_in || !ep_out || (us->protocol == USB_PR_CBI && !ep_int)) {
775
		usb_stor_dbg(us, "Endpoint sanity check failed! Rejecting dev.\n");
Linus Torvalds's avatar
Linus Torvalds committed
776
777
778
779
780
781
782
		return -EIO;
	}

	/* Calculate and store the pipe values */
	us->send_ctrl_pipe = usb_sndctrlpipe(us->pusb_dev, 0);
	us->recv_ctrl_pipe = usb_rcvctrlpipe(us->pusb_dev, 0);
	us->send_bulk_pipe = usb_sndbulkpipe(us->pusb_dev,
783
		usb_endpoint_num(ep_out));
784
	us->recv_bulk_pipe = usb_rcvbulkpipe(us->pusb_dev,
785
		usb_endpoint_num(ep_in));
Linus Torvalds's avatar
Linus Torvalds committed
786
787
	if (ep_int) {
		us->recv_intr_pipe = usb_rcvintpipe(us->pusb_dev,
788
			usb_endpoint_num(ep_int));
Linus Torvalds's avatar
Linus Torvalds committed
789
790
791
792
793
794
795
796
797
		us->ep_bInterval = ep_int->bInterval;
	}
	return 0;
}

/* Initialize all the dynamic resources we need */
static int usb_stor_acquire_resources(struct us_data *us)
{
	int p;
798
	struct task_struct *th;
Linus Torvalds's avatar
Linus Torvalds committed
799
800

	us->current_urb = usb_alloc_urb(0, GFP_KERNEL);
801
	if (!us->current_urb)
Linus Torvalds's avatar
Linus Torvalds committed
802
803
		return -ENOMEM;

804
805
806
807
	/*
	 * Just before we start our control thread, initialize
	 * the device if it needs initialization
	 */
808
809
810
811
812
	if (us->unusual_dev->initFunction) {
		p = us->unusual_dev->initFunction(us);
		if (p)
			return p;
	}
Linus Torvalds's avatar
Linus Torvalds committed
813
814

	/* Start up our control thread */
815
	th = kthread_run(usb_stor_control_thread, us, "usb-storage");
816
	if (IS_ERR(th)) {
817
818
		dev_warn(&us->pusb_intf->dev,
				"Unable to start control thread\n");
819
		return PTR_ERR(th);
Linus Torvalds's avatar
Linus Torvalds committed
820
	}
821
	us->ctl_thread = th;
Linus Torvalds's avatar
Linus Torvalds committed
822
823
824
825
826
827
828

	return 0;
}

/* Release all our dynamic resources */
static void usb_stor_release_resources(struct us_data *us)
{
829
830
	/*
	 * Tell the control thread to exit.  The SCSI host must
831
832
	 * already have been removed and the DISCONNECTING flag set
	 * so that we won't accept any more commands.
Linus Torvalds's avatar
Linus Torvalds committed
833
	 */
834
	usb_stor_dbg(us, "-- sending exit command to thread\n");
835
	complete(&us->cmnd_ready);
836
837
	if (us->ctl_thread)
		kthread_stop(us->ctl_thread);
Linus Torvalds's avatar
Linus Torvalds committed
838
839
840

	/* Call the destructor routine, if it exists */
	if (us->extra_destructor) {
841
		usb_stor_dbg(us, "-- calling extra_destructor()\n");
Linus Torvalds's avatar
Linus Torvalds committed
842
843
844
845
846
847
848
849
850
851
852
		us->extra_destructor(us->extra);
	}

	/* Free the extra data and the URB */
	kfree(us->extra);
	usb_free_urb(us->current_urb);
}

/* Dissociate from the USB device */
static void dissociate_dev(struct us_data *us)
{
853
854
	/* Free the buffers */
	kfree(us->cr);
855
	usb_free_coherent(us->pusb_dev, US_IOBUF_SIZE, us->iobuf, us->iobuf_dma);
Linus Torvalds's avatar
Linus Torvalds committed
856
857
858
859
860

	/* Remove our private data from the interface */
	usb_set_intfdata(us->pusb_intf, NULL);
}

861
862
/*
 * First stage of disconnect processing: stop SCSI scanning,
863
864
 * remove the host, and stop accepting new commands
 */
865
866
static void quiesce_and_remove_host(struct us_data *us)
{
867
868
	struct Scsi_Host *host = us_to_host(us);

869
	/* If the device is really gone, cut short reset delays */
870
	if (us->pusb_dev->state == USB_STATE_NOTATTACHED) {
871
		set_bit(US_FLIDX_DISCONNECTING, &us->dflags);
872
873
		wake_up(&us->delay_wait);
	}
874

875
876
	/*
	 * Prevent SCSI scanning (if it hasn't started yet)
877
	 * or wait for the SCSI-scanning routine to stop.
878
	 */
879
880
881
882
883
	cancel_delayed_work_sync(&us->scan_dwork);

	/* Balance autopm calls if scanning was cancelled */
	if (test_bit(US_FLIDX_SCAN_PENDING, &us->dflags))
		usb_autopm_put_interface_no_suspend(us->pusb_intf);
884

885
886
	/*
	 * Removing the host will perform an orderly shutdown: caches
887
888
	 * synchronized, disks spun down, etc.
	 */
889
	scsi_remove_host(host);
890

891
892
	/*
	 * Prevent any new commands from being accepted and cut short
893
894
895
896
897
898
	 * reset delays.
	 */
	scsi_lock(host);
	set_bit(US_FLIDX_DISCONNECTING, &us->dflags);
	scsi_unlock(host);
	wake_up(&us->delay_wait);
899
900
901
902
903
904
905
906
}

/* Second stage of disconnect processing: deallocate all resources */
static void release_everything(struct us_data *us)
{
	usb_stor_release_resources(us);
	dissociate_dev(us);

907
908
909
910
	/*
	 * Drop our reference to the host; the SCSI core will free it
	 * (and "us" along with it) when the refcount becomes 0.
	 */
911
912
913
	scsi_host_put(us_to_host(us));
}

914
915
/* Delayed-work routine to carry out SCSI-device scanning */
static void usb_stor_scan_dwork(struct work_struct *work)
Linus Torvalds's avatar
Linus Torvalds committed
916
{
917
918
	struct us_data *us = container_of(work, struct us_data,
			scan_dwork.work);
919
	struct device *dev = &us->pusb_intf->dev;
Linus Torvalds's avatar
Linus Torvalds committed
920

921
	dev_dbg(dev, "starting scan\n");
922

923
	/* For bulk-only devices, determine the max LUN value */
924
925
926
	if (us->protocol == USB_PR_BULK &&
	    !(us->fflags & US_FL_SINGLE_LUN) &&
	    !(us->fflags & US_FL_SCM_MULT_TARG)) {
927
928
		mutex_lock(&us->dev_mutex);
		us->max_lun = usb_stor_Bulk_max_lun(us);
929
930
		/*
		 * Allow proper scanning of devices that present more than 8 LUNs
931
932
		 * While not affecting other devices that may need the previous
		 * behavior
933
934
935
		 */
		if (us->max_lun >= 8)
			us_to_host(us)->max_lun = us->max_lun+1;
936
		mutex_unlock(&us->dev_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
937
	}
938
939
	scsi_scan_host(us_to_host(us));
	dev_dbg(dev, "scan complete\n");
Linus Torvalds's avatar
Linus Torvalds committed
940

941
	/* Should we unbind if no devices were detected? */
Linus Torvalds's avatar
Linus Torvalds committed
942

943
	usb_autopm_put_interface(us->pusb_intf);
944
	clear_bit(US_FLIDX_SCAN_PENDING, &us->dflags);
Linus Torvalds's avatar
Linus Torvalds committed
945
946
}

947
948
949
950
951
952
953
954
955
static unsigned int usb_stor_sg_tablesize(struct usb_interface *intf)
{
	struct usb_device *usb_dev = interface_to_usbdev(intf);

	if (usb_dev->bus->sg_tablesize) {
		return usb_dev->bus->sg_tablesize;
	}
	return SG_ALL;
}
Linus Torvalds's avatar
Linus Torvalds committed
956

957
958
959
960
/* First part of general USB mass-storage probing */
int usb_stor_probe1(struct us_data **pus,
		struct usb_interface *intf,
		const struct usb_device_id *id,
961
962
		struct us_unusual_dev *unusual_dev,
		struct scsi_host_template *sht)
Linus Torvalds's avatar
Linus Torvalds committed
963
964
965
966
{
	struct Scsi_Host *host;
	struct us_data *us;
	int result;
967

968
	dev_info(&intf->dev, "USB Mass Storage device detected\n");
Linus Torvalds's avatar
Linus Torvalds committed
969
970
971
972
973

	/*
	 * Ask the SCSI layer to allocate a host structure, with extra
	 * space at the end for our private us_data structure.
	 */
974
	host = scsi_host_alloc(sht, sizeof(*us));
Linus Torvalds's avatar
Linus Torvalds committed
975
	if (!host) {
976
		dev_warn(&intf->dev, "Unable to allocate the scsi host\n");
Linus Torvalds's avatar
Linus Torvalds committed
977
978
979
		return -ENOMEM;
	}

980
981
982
983
	/*
	 * Allow 16-byte CDBs and thus > 2TB
	 */
	host->max_cmd_len = 16;
984
	host->sg_tablesize = usb_stor_sg_tablesize(intf);
985
	*pus = us = host_to_us(host);
986
	mutex_init(&(us->dev_mutex));
987
	us_set_lock_class(&us->dev_mutex, intf);
988
	init_completion(&us->cmnd_ready);
Linus Torvalds's avatar
Linus Torvalds committed
989
990
	init_completion(&(us->notify));
	init_waitqueue_head(&us->delay_wait);
991
	INIT_DELAYED_WORK(&us->scan_dwork, usb_stor_scan_dwork);
Linus Torvalds's avatar
Linus Torvalds committed
992
993
994
995
996
997

	/* Associate the us_data structure with the USB device */
	result = associate_dev(us, intf);
	if (result)
		goto BadDevice;

998
999
	/* Get the unusual_devs entries and the descriptors */
	result = get_device_info(us, id, unusual_dev);
1000
1001
	if (result)
		goto BadDevice;
Linus Torvalds's avatar
Linus Torvalds committed
1002

1003
1004
1005
1006
	/* Get standard transport and protocol settings */
	get_transport(us);
	get_protocol(us);

1007
1008
	/*
	 * Give the caller a chance to fill in specialized transport
1009
1010
1011
1012
1013
	 * or protocol settings.
	 */
	return 0;

BadDevice:
1014
	usb_stor_dbg(us, "storage_probe() failed\n");
1015
1016
1017
1018
1019
1020
1021
1022
1023
	release_everything(us);
	return result;
}
EXPORT_SYMBOL_GPL(usb_stor_probe1);

/* Second part of general USB mass-storage probing */
int usb_stor_probe2(struct us_data *us)
{
	int result;
1024
	struct device *dev = &us->pusb_intf->dev;
1025
1026
1027
1028

	/* Make sure the transport and protocol have both been set */
	if (!us->transport || !us->proto_handler) {
		result = -ENXIO;
Linus Torvalds's avatar
Linus Torvalds committed
1029
		goto BadDevice;
1030
	}
1031
1032
	usb_stor_dbg(us, "Transport: %s\n", us->transport_name);
	usb_stor_dbg(us, "Protocol: %s\n", us->protocol_name);
1033

1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
	if (us->fflags & US_FL_SCM_MULT_TARG) {
		/*
		 * SCM eUSCSI bridge devices can have different numbers
		 * of LUNs on different targets; allow all to be probed.
		 */
		us->max_lun = 7;
		/* The eUSCSI itself has ID 7, so avoid scanning that */
		us_to_host(us)->this_id = 7;
		/* max_id is 8 initially, so no need to set it here */
	} else {
		/* In the normal case there is only a single target */
		us_to_host(us)->max_id = 1;
		/*
		 * Like Windows, we won't store the LUN bits in CDB[1] for
		 * SCSI-2 devices using the Bulk-Only transport (even though
		 * this violates the SCSI spec).
		 */
		if (us->transport == usb_stor_Bulk_transport)
			us_to_host(us)->no_scsi2_lun_in_cdb = 1;
	}

1055
1056
1057
1058
1059
	/* fix for single-lun devices */
	if (us->fflags & US_FL_SINGLE_LUN)
		us->max_lun = 0;

	/* Find the endpoints and calculate pipe values */
Linus Torvalds's avatar
Linus Torvalds committed
1060
1061
1062
1063
	result = get_pipes(us);
	if (result)
		goto BadDevice;

Alan Stern's avatar
Alan Stern committed
1064
1065
1066
1067
1068
1069
1070
	/*
	 * If the device returns invalid data for the first READ(10)
	 * command, indicate the command should be retried.
	 */
	if (us->fflags & US_FL_INITIAL_READ10)
		set_bit(US_FLIDX_REDO_READ10, &us->dflags);

Linus Torvalds's avatar
Linus Torvalds committed
1071
1072
1073
1074
	/* Acquire all the other resources and add the host */
	result = usb_stor_acquire_resources(us);
	if (result)
		goto BadDevice;
1075
	usb_autopm_get_interface_no_resume(us->pusb_intf);
1076
1077
	snprintf(us->scsi_name, sizeof(us->scsi_name), "usb-storage %s",
					dev_name(&us->pusb_intf->dev));
1078
	result = scsi_add_host(us_to_host(us), dev);
Linus Torvalds's avatar
Linus Torvalds committed
1079
	if (result) {
1080
1081
		dev_warn(dev,
				"Unable to add the scsi host\n");
1082
		goto HostAddErr;
Linus Torvalds's avatar
Linus Torvalds committed
1083
1084
	}

1085
1086
	/* Submit the delayed_work for SCSI-device scanning */
	set_bit(US_FLIDX_SCAN_PENDING, &us->dflags);
Linus Torvalds's avatar
Linus Torvalds committed
1087

1088
1089
1090
1091
	if (delay_use > 0)
		dev_dbg(dev, "waiting for device to settle before scanning\n");
	queue_delayed_work(system_freezable_wq, &us->scan_dwork,
			delay_use * HZ);
Linus Torvalds's avatar
Linus Torvalds committed
1092
1093
1094
	return 0;

	/* We come here if there are any problems */
1095
1096
HostAddErr:
	usb_autopm_put_interface_no_suspend(us->pusb_intf);
Linus Torvalds's avatar
Linus Torvalds committed
1097
BadDevice:
1098
	usb_stor_dbg(us, "storage_probe() failed\n");
1099
	release_everything(us);
Linus Torvalds's avatar
Linus Torvalds committed
1100
1101
	return result;
}
1102
EXPORT_SYMBOL_GPL(usb_stor_probe2);
Linus Torvalds's avatar
Linus Torvalds committed
1103

1104
1105
/* Handle a USB mass-storage disconnect */
void usb_stor_disconnect(struct usb_interface *intf)
Linus Torvalds's avatar
Linus Torvalds committed
1106
1107
1108
{
	struct us_data *us = usb_get_intfdata(intf);

1109
1110
	quiesce_and_remove_host(us);
	release_everything(us);
Linus Torvalds's avatar
Linus Torvalds committed
1111
}
1112
1113
EXPORT_SYMBOL_GPL(usb_stor_disconnect);

1114
1115
static struct scsi_host_template usb_stor_host_template;

1116
1117
1118
1119
/* The main probe routine for standard devices */
static int storage_probe(struct usb_interface *intf,
			 const struct usb_device_id *id)
{
1120
	struct us_unusual_dev *unusual_dev;
1121
1122
	struct us_data *us;
	int result;
1123
	int size;
1124

1125
1126
	/* If uas is enabled and this device can do uas then ignore it. */
#if IS_ENABLED(CONFIG_USB_UAS)
1127
	if (uas_use_uas_driver(intf, id, NULL))
1128
1129
1130
		return -ENXIO;
#endif

1131
1132
1133
1134
	/*
	 * If the device isn't standard (is handled by a subdriver
	 * module) then don't accept it.
	 */
1135
	if (usb_usual_ignore_device(intf))
1136
1137
1138
1139
1140
1141
1142
1143
1144
		return -ENXIO;

	/*
	 * Call the general probe procedures.
	 *
	 * The unusual_dev_list array is parallel to the usb_storage_usb_ids
	 * table, so we use the index of the id entry to find the
	 * corresponding unusual_devs entry.
	 */