usb.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Driver for USB Mass Storage compliant devices
  4. *
  5. * Current development and maintenance by:
  6. * (c) 1999-2003 Matthew Dharm (mdharm-usb@one-eyed-alien.net)
  7. *
  8. * Developed with the assistance of:
  9. * (c) 2000 David L. Brown, Jr. (usb-storage@davidb.org)
  10. * (c) 2003-2009 Alan Stern (stern@rowland.harvard.edu)
  11. *
  12. * Initial work by:
  13. * (c) 1999 Michael Gee (michael@linuxspecific.com)
  14. *
  15. * usb_device_id support by Adam J. Richter (adam@yggdrasil.com):
  16. * (c) 2000 Yggdrasil Computing, Inc.
  17. *
  18. * This driver is based on the 'USB Mass Storage Class' document. This
  19. * describes in detail the protocol used to communicate with such
  20. * devices. Clearly, the designers had SCSI and ATAPI commands in
  21. * mind when they created this document. The commands are all very
  22. * similar to commands in the SCSI-II and ATAPI specifications.
  23. *
  24. * It is important to note that in a number of cases this class
  25. * exhibits class-specific exemptions from the USB specification.
  26. * Notably the usage of NAK, STALL and ACK differs from the norm, in
  27. * that they are used to communicate wait, failed and OK on commands.
  28. *
  29. * Also, for certain devices, the interrupt endpoint is used to convey
  30. * status of a command.
  31. */
  32. #ifdef CONFIG_USB_STORAGE_DEBUG
  33. #define DEBUG
  34. #endif
  35. #include <linux/sched.h>
  36. #include <linux/errno.h>
  37. #include <linux/module.h>
  38. #include <linux/slab.h>
  39. #include <linux/kthread.h>
  40. #include <linux/mutex.h>
  41. #include <linux/utsname.h>
  42. #include <scsi/scsi.h>
  43. #include <scsi/scsi_cmnd.h>
  44. #include <scsi/scsi_device.h>
  45. #include "usb.h"
  46. #include "scsiglue.h"
  47. #include "transport.h"
  48. #include "protocol.h"
  49. #include "debug.h"
  50. #include "initializers.h"
  51. #include "sierra_ms.h"
  52. #include "option_ms.h"
  53. #if IS_ENABLED(CONFIG_USB_UAS)
  54. #include "uas-detect.h"
  55. #endif
  56. #define DRV_NAME "usb-storage"
  57. /* Some informational data */
  58. MODULE_AUTHOR("Matthew Dharm <mdharm-usb@one-eyed-alien.net>");
  59. MODULE_DESCRIPTION("USB Mass Storage driver for Linux");
  60. MODULE_LICENSE("GPL");
  61. static unsigned int delay_use = 1;
  62. module_param(delay_use, uint, S_IRUGO | S_IWUSR);
  63. MODULE_PARM_DESC(delay_use, "seconds to delay before using a new device");
  64. static char quirks[128];
  65. module_param_string(quirks, quirks, sizeof(quirks), S_IRUGO | S_IWUSR);
  66. MODULE_PARM_DESC(quirks, "supplemental list of device IDs and their quirks");
  67. /*
  68. * The entries in this table correspond, line for line,
  69. * with the entries in usb_storage_usb_ids[], defined in usual-tables.c.
  70. */
  71. /*
  72. *The vendor name should be kept at eight characters or less, and
  73. * the product name should be kept at 16 characters or less. If a device
  74. * has the US_FL_FIX_INQUIRY flag, then the vendor and product names
  75. * normally generated by a device through the INQUIRY response will be
  76. * taken from this list, and this is the reason for the above size
  77. * restriction. However, if the flag is not present, then you
  78. * are free to use as many characters as you like.
  79. */
  80. #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
  81. vendor_name, product_name, use_protocol, use_transport, \
  82. init_function, Flags) \
  83. { \
  84. .vendorName = vendor_name, \
  85. .productName = product_name, \
  86. .useProtocol = use_protocol, \
  87. .useTransport = use_transport, \
  88. .initFunction = init_function, \
  89. }
  90. #define COMPLIANT_DEV UNUSUAL_DEV
  91. #define USUAL_DEV(use_protocol, use_transport) \
  92. { \
  93. .useProtocol = use_protocol, \
  94. .useTransport = use_transport, \
  95. }
  96. #define UNUSUAL_VENDOR_INTF(idVendor, cl, sc, pr, \
  97. vendor_name, product_name, use_protocol, use_transport, \
  98. init_function, Flags) \
  99. { \
  100. .vendorName = vendor_name, \
  101. .productName = product_name, \
  102. .useProtocol = use_protocol, \
  103. .useTransport = use_transport, \
  104. .initFunction = init_function, \
  105. }
  106. static const struct us_unusual_dev us_unusual_dev_list[] = {
  107. # include "unusual_devs.h"
  108. { } /* Terminating entry */
  109. };
  110. static const struct us_unusual_dev for_dynamic_ids =
  111. USUAL_DEV(USB_SC_SCSI, USB_PR_BULK);
  112. #undef UNUSUAL_DEV
  113. #undef COMPLIANT_DEV
  114. #undef USUAL_DEV
  115. #undef UNUSUAL_VENDOR_INTF
  116. #ifdef CONFIG_LOCKDEP
  117. static struct lock_class_key us_interface_key[USB_MAXINTERFACES];
  118. static void us_set_lock_class(struct mutex *mutex,
  119. struct usb_interface *intf)
  120. {
  121. struct usb_device *udev = interface_to_usbdev(intf);
  122. struct usb_host_config *config = udev->actconfig;
  123. int i;
  124. for (i = 0; i < config->desc.bNumInterfaces; i++) {
  125. if (config->interface[i] == intf)
  126. break;
  127. }
  128. BUG_ON(i == config->desc.bNumInterfaces);
  129. lockdep_set_class(mutex, &us_interface_key[i]);
  130. }
  131. #else
  132. static void us_set_lock_class(struct mutex *mutex,
  133. struct usb_interface *intf)
  134. {
  135. }
  136. #endif
  137. #ifdef CONFIG_PM /* Minimal support for suspend and resume */
  138. int usb_stor_suspend(struct usb_interface *iface, pm_message_t message)
  139. {
  140. struct us_data *us = usb_get_intfdata(iface);
  141. /* Wait until no command is running */
  142. mutex_lock(&us->dev_mutex);
  143. if (us->suspend_resume_hook)
  144. (us->suspend_resume_hook)(us, US_SUSPEND);
  145. /*
  146. * When runtime PM is working, we'll set a flag to indicate
  147. * whether we should autoresume when a SCSI request arrives.
  148. */
  149. mutex_unlock(&us->dev_mutex);
  150. return 0;
  151. }
  152. EXPORT_SYMBOL_GPL(usb_stor_suspend);
  153. int usb_stor_resume(struct usb_interface *iface)
  154. {
  155. struct us_data *us = usb_get_intfdata(iface);
  156. mutex_lock(&us->dev_mutex);
  157. if (us->suspend_resume_hook)
  158. (us->suspend_resume_hook)(us, US_RESUME);
  159. mutex_unlock(&us->dev_mutex);
  160. return 0;
  161. }
  162. EXPORT_SYMBOL_GPL(usb_stor_resume);
  163. int usb_stor_reset_resume(struct usb_interface *iface)
  164. {
  165. struct us_data *us = usb_get_intfdata(iface);
  166. /* Report the reset to the SCSI core */
  167. usb_stor_report_bus_reset(us);
  168. /*
  169. * If any of the subdrivers implemented a reinitialization scheme,
  170. * this is where the callback would be invoked.
  171. */
  172. return 0;
  173. }
  174. EXPORT_SYMBOL_GPL(usb_stor_reset_resume);
  175. #endif /* CONFIG_PM */
  176. /*
  177. * The next two routines get called just before and just after
  178. * a USB port reset, whether from this driver or a different one.
  179. */
  180. int usb_stor_pre_reset(struct usb_interface *iface)
  181. {
  182. struct us_data *us = usb_get_intfdata(iface);
  183. /* Make sure no command runs during the reset */
  184. mutex_lock(&us->dev_mutex);
  185. return 0;
  186. }
  187. EXPORT_SYMBOL_GPL(usb_stor_pre_reset);
  188. int usb_stor_post_reset(struct usb_interface *iface)
  189. {
  190. struct us_data *us = usb_get_intfdata(iface);
  191. /* Report the reset to the SCSI core */
  192. usb_stor_report_bus_reset(us);
  193. /*
  194. * If any of the subdrivers implemented a reinitialization scheme,
  195. * this is where the callback would be invoked.
  196. */
  197. mutex_unlock(&us->dev_mutex);
  198. return 0;
  199. }
  200. EXPORT_SYMBOL_GPL(usb_stor_post_reset);
  201. /*
  202. * fill_inquiry_response takes an unsigned char array (which must
  203. * be at least 36 characters) and populates the vendor name,
  204. * product name, and revision fields. Then the array is copied
  205. * into the SCSI command's response buffer (oddly enough
  206. * called request_buffer). data_len contains the length of the
  207. * data array, which again must be at least 36.
  208. */
  209. void fill_inquiry_response(struct us_data *us, unsigned char *data,
  210. unsigned int data_len)
  211. {
  212. if (data_len < 36) /* You lose. */
  213. return;
  214. memset(data+8, ' ', 28);
  215. if (data[0]&0x20) { /*
  216. * USB device currently not connected. Return
  217. * peripheral qualifier 001b ("...however, the
  218. * physical device is not currently connected
  219. * to this logical unit") and leave vendor and
  220. * product identification empty. ("If the target
  221. * does store some of the INQUIRY data on the
  222. * device, it may return zeros or ASCII spaces
  223. * (20h) in those fields until the data is
  224. * available from the device.").
  225. */
  226. } else {
  227. u16 bcdDevice = le16_to_cpu(us->pusb_dev->descriptor.bcdDevice);
  228. int n;
  229. n = strlen(us->unusual_dev->vendorName);
  230. memcpy(data+8, us->unusual_dev->vendorName, min(8, n));
  231. n = strlen(us->unusual_dev->productName);
  232. memcpy(data+16, us->unusual_dev->productName, min(16, n));
  233. data[32] = 0x30 + ((bcdDevice>>12) & 0x0F);
  234. data[33] = 0x30 + ((bcdDevice>>8) & 0x0F);
  235. data[34] = 0x30 + ((bcdDevice>>4) & 0x0F);
  236. data[35] = 0x30 + ((bcdDevice) & 0x0F);
  237. }
  238. usb_stor_set_xfer_buf(data, data_len, us->srb);
  239. }
  240. EXPORT_SYMBOL_GPL(fill_inquiry_response);
  241. static int usb_stor_control_thread(void * __us)
  242. {
  243. struct us_data *us = (struct us_data *)__us;
  244. struct Scsi_Host *host = us_to_host(us);
  245. struct scsi_cmnd *srb;
  246. for (;;) {
  247. usb_stor_dbg(us, "*** thread sleeping\n");
  248. if (wait_for_completion_interruptible(&us->cmnd_ready))
  249. break;
  250. usb_stor_dbg(us, "*** thread awakened\n");
  251. /* lock the device pointers */
  252. mutex_lock(&(us->dev_mutex));
  253. /* lock access to the state */
  254. scsi_lock(host);
  255. /* When we are called with no command pending, we're done */
  256. srb = us->srb;
  257. if (srb == NULL) {
  258. scsi_unlock(host);
  259. mutex_unlock(&us->dev_mutex);
  260. usb_stor_dbg(us, "-- exiting\n");
  261. break;
  262. }
  263. /* has the command timed out *already* ? */
  264. if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) {
  265. srb->result = DID_ABORT << 16;
  266. goto SkipForAbort;
  267. }
  268. scsi_unlock(host);
  269. /*
  270. * reject the command if the direction indicator
  271. * is UNKNOWN
  272. */
  273. if (srb->sc_data_direction == DMA_BIDIRECTIONAL) {
  274. usb_stor_dbg(us, "UNKNOWN data direction\n");
  275. srb->result = DID_ERROR << 16;
  276. }
  277. /*
  278. * reject if target != 0 or if LUN is higher than
  279. * the maximum known LUN
  280. */
  281. else if (srb->device->id &&
  282. !(us->fflags & US_FL_SCM_MULT_TARG)) {
  283. usb_stor_dbg(us, "Bad target number (%d:%llu)\n",
  284. srb->device->id,
  285. srb->device->lun);
  286. srb->result = DID_BAD_TARGET << 16;
  287. }
  288. else if (srb->device->lun > us->max_lun) {
  289. usb_stor_dbg(us, "Bad LUN (%d:%llu)\n",
  290. srb->device->id,
  291. srb->device->lun);
  292. srb->result = DID_BAD_TARGET << 16;
  293. }
  294. /*
  295. * Handle those devices which need us to fake
  296. * their inquiry data
  297. */
  298. else if ((srb->cmnd[0] == INQUIRY) &&
  299. (us->fflags & US_FL_FIX_INQUIRY)) {
  300. unsigned char data_ptr[36] = {
  301. 0x00, 0x80, 0x02, 0x02,
  302. 0x1F, 0x00, 0x00, 0x00};
  303. usb_stor_dbg(us, "Faking INQUIRY command\n");
  304. fill_inquiry_response(us, data_ptr, 36);
  305. srb->result = SAM_STAT_GOOD;
  306. }
  307. /* we've got a command, let's do it! */
  308. else {
  309. US_DEBUG(usb_stor_show_command(us, srb));
  310. us->proto_handler(srb, us);
  311. usb_mark_last_busy(us->pusb_dev);
  312. }
  313. /* lock access to the state */
  314. scsi_lock(host);
  315. /* was the command aborted? */
  316. if (srb->result == DID_ABORT << 16) {
  317. SkipForAbort:
  318. usb_stor_dbg(us, "scsi command aborted\n");
  319. srb = NULL; /* Don't call srb->scsi_done() */
  320. }
  321. /*
  322. * If an abort request was received we need to signal that
  323. * the abort has finished. The proper test for this is
  324. * the TIMED_OUT flag, not srb->result == DID_ABORT, because
  325. * the timeout might have occurred after the command had
  326. * already completed with a different result code.
  327. */
  328. if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) {
  329. complete(&(us->notify));
  330. /* Allow USB transfers to resume */
  331. clear_bit(US_FLIDX_ABORTING, &us->dflags);
  332. clear_bit(US_FLIDX_TIMED_OUT, &us->dflags);
  333. }
  334. /* finished working on this command */
  335. us->srb = NULL;
  336. scsi_unlock(host);
  337. /* unlock the device pointers */
  338. mutex_unlock(&us->dev_mutex);
  339. /* now that the locks are released, notify the SCSI core */
  340. if (srb) {
  341. usb_stor_dbg(us, "scsi cmd done, result=0x%x\n",
  342. srb->result);
  343. srb->scsi_done(srb);
  344. }
  345. } /* for (;;) */
  346. /* Wait until we are told to stop */
  347. for (;;) {
  348. set_current_state(TASK_INTERRUPTIBLE);
  349. if (kthread_should_stop())
  350. break;
  351. schedule();
  352. }
  353. __set_current_state(TASK_RUNNING);
  354. return 0;
  355. }
  356. /***********************************************************************
  357. * Device probing and disconnecting
  358. ***********************************************************************/
  359. /* Associate our private data with the USB device */
  360. static int associate_dev(struct us_data *us, struct usb_interface *intf)
  361. {
  362. /* Fill in the device-related fields */
  363. us->pusb_dev = interface_to_usbdev(intf);
  364. us->pusb_intf = intf;
  365. us->ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
  366. usb_stor_dbg(us, "Vendor: 0x%04x, Product: 0x%04x, Revision: 0x%04x\n",
  367. le16_to_cpu(us->pusb_dev->descriptor.idVendor),
  368. le16_to_cpu(us->pusb_dev->descriptor.idProduct),
  369. le16_to_cpu(us->pusb_dev->descriptor.bcdDevice));
  370. usb_stor_dbg(us, "Interface Subclass: 0x%02x, Protocol: 0x%02x\n",
  371. intf->cur_altsetting->desc.bInterfaceSubClass,
  372. intf->cur_altsetting->desc.bInterfaceProtocol);
  373. /* Store our private data in the interface */
  374. usb_set_intfdata(intf, us);
  375. /* Allocate the control/setup and DMA-mapped buffers */
  376. us->cr = kmalloc(sizeof(*us->cr), GFP_KERNEL);
  377. if (!us->cr)
  378. return -ENOMEM;
  379. us->iobuf = usb_alloc_coherent(us->pusb_dev, US_IOBUF_SIZE,
  380. GFP_KERNEL, &us->iobuf_dma);
  381. if (!us->iobuf) {
  382. usb_stor_dbg(us, "I/O buffer allocation failed\n");
  383. return -ENOMEM;
  384. }
  385. return 0;
  386. }
  387. /* Works only for digits and letters, but small and fast */
  388. #define TOLOWER(x) ((x) | 0x20)
  389. /* Adjust device flags based on the "quirks=" module parameter */
  390. void usb_stor_adjust_quirks(struct usb_device *udev, unsigned long *fflags)
  391. {
  392. char *p;
  393. u16 vid = le16_to_cpu(udev->descriptor.idVendor);
  394. u16 pid = le16_to_cpu(udev->descriptor.idProduct);
  395. unsigned f = 0;
  396. unsigned int mask = (US_FL_SANE_SENSE | US_FL_BAD_SENSE |
  397. US_FL_FIX_CAPACITY | US_FL_IGNORE_UAS |
  398. US_FL_CAPACITY_HEURISTICS | US_FL_IGNORE_DEVICE |
  399. US_FL_NOT_LOCKABLE | US_FL_MAX_SECTORS_64 |
  400. US_FL_CAPACITY_OK | US_FL_IGNORE_RESIDUE |
  401. US_FL_SINGLE_LUN | US_FL_NO_WP_DETECT |
  402. US_FL_NO_READ_DISC_INFO | US_FL_NO_READ_CAPACITY_16 |
  403. US_FL_INITIAL_READ10 | US_FL_WRITE_CACHE |
  404. US_FL_NO_ATA_1X | US_FL_NO_REPORT_OPCODES |
  405. US_FL_MAX_SECTORS_240 | US_FL_NO_REPORT_LUNS |
  406. US_FL_ALWAYS_SYNC);
  407. p = quirks;
  408. while (*p) {
  409. /* Each entry consists of VID:PID:flags */
  410. if (vid == simple_strtoul(p, &p, 16) &&
  411. *p == ':' &&
  412. pid == simple_strtoul(p+1, &p, 16) &&
  413. *p == ':')
  414. break;
  415. /* Move forward to the next entry */
  416. while (*p) {
  417. if (*p++ == ',')
  418. break;
  419. }
  420. }
  421. if (!*p) /* No match */
  422. return;
  423. /* Collect the flags */
  424. while (*++p && *p != ',') {
  425. switch (TOLOWER(*p)) {
  426. case 'a':
  427. f |= US_FL_SANE_SENSE;
  428. break;
  429. case 'b':
  430. f |= US_FL_BAD_SENSE;
  431. break;
  432. case 'c':
  433. f |= US_FL_FIX_CAPACITY;
  434. break;
  435. case 'd':
  436. f |= US_FL_NO_READ_DISC_INFO;
  437. break;
  438. case 'e':
  439. f |= US_FL_NO_READ_CAPACITY_16;
  440. break;
  441. case 'f':
  442. f |= US_FL_NO_REPORT_OPCODES;
  443. break;
  444. case 'g':
  445. f |= US_FL_MAX_SECTORS_240;
  446. break;
  447. case 'h':
  448. f |= US_FL_CAPACITY_HEURISTICS;
  449. break;
  450. case 'i':
  451. f |= US_FL_IGNORE_DEVICE;
  452. break;
  453. case 'j':
  454. f |= US_FL_NO_REPORT_LUNS;
  455. break;
  456. case 'k':
  457. f |= US_FL_NO_SAME;
  458. break;
  459. case 'l':
  460. f |= US_FL_NOT_LOCKABLE;
  461. break;
  462. case 'm':
  463. f |= US_FL_MAX_SECTORS_64;
  464. break;
  465. case 'n':
  466. f |= US_FL_INITIAL_READ10;
  467. break;
  468. case 'o':
  469. f |= US_FL_CAPACITY_OK;
  470. break;
  471. case 'p':
  472. f |= US_FL_WRITE_CACHE;
  473. break;
  474. case 'r':
  475. f |= US_FL_IGNORE_RESIDUE;
  476. break;
  477. case 's':
  478. f |= US_FL_SINGLE_LUN;
  479. break;
  480. case 't':
  481. f |= US_FL_NO_ATA_1X;
  482. break;
  483. case 'u':
  484. f |= US_FL_IGNORE_UAS;
  485. break;
  486. case 'w':
  487. f |= US_FL_NO_WP_DETECT;
  488. break;
  489. case 'y':
  490. f |= US_FL_ALWAYS_SYNC;
  491. break;
  492. /* Ignore unrecognized flag characters */
  493. }
  494. }
  495. *fflags = (*fflags & ~mask) | f;
  496. }
  497. EXPORT_SYMBOL_GPL(usb_stor_adjust_quirks);
  498. /* Get the unusual_devs entries and the string descriptors */
  499. static int get_device_info(struct us_data *us, const struct usb_device_id *id,
  500. const struct us_unusual_dev *unusual_dev)
  501. {
  502. struct usb_device *dev = us->pusb_dev;
  503. struct usb_interface_descriptor *idesc =
  504. &us->pusb_intf->cur_altsetting->desc;
  505. struct device *pdev = &us->pusb_intf->dev;
  506. /* Store the entries */
  507. us->unusual_dev = unusual_dev;
  508. us->subclass = (unusual_dev->useProtocol == USB_SC_DEVICE) ?
  509. idesc->bInterfaceSubClass :
  510. unusual_dev->useProtocol;
  511. us->protocol = (unusual_dev->useTransport == USB_PR_DEVICE) ?
  512. idesc->bInterfaceProtocol :
  513. unusual_dev->useTransport;
  514. us->fflags = id->driver_info;
  515. usb_stor_adjust_quirks(us->pusb_dev, &us->fflags);
  516. if (us->fflags & US_FL_IGNORE_DEVICE) {
  517. dev_info(pdev, "device ignored\n");
  518. return -ENODEV;
  519. }
  520. /*
  521. * This flag is only needed when we're in high-speed, so let's
  522. * disable it if we're in full-speed
  523. */
  524. if (dev->speed != USB_SPEED_HIGH)
  525. us->fflags &= ~US_FL_GO_SLOW;
  526. if (us->fflags)
  527. dev_info(pdev, "Quirks match for vid %04x pid %04x: %lx\n",
  528. le16_to_cpu(dev->descriptor.idVendor),
  529. le16_to_cpu(dev->descriptor.idProduct),
  530. us->fflags);
  531. /*
  532. * Log a message if a non-generic unusual_dev entry contains an
  533. * unnecessary subclass or protocol override. This may stimulate
  534. * reports from users that will help us remove unneeded entries
  535. * from the unusual_devs.h table.
  536. */
  537. if (id->idVendor || id->idProduct) {
  538. static const char *msgs[3] = {
  539. "an unneeded SubClass entry",
  540. "an unneeded Protocol entry",
  541. "unneeded SubClass and Protocol entries"};
  542. struct usb_device_descriptor *ddesc = &dev->descriptor;
  543. int msg = -1;
  544. if (unusual_dev->useProtocol != USB_SC_DEVICE &&
  545. us->subclass == idesc->bInterfaceSubClass)
  546. msg += 1;
  547. if (unusual_dev->useTransport != USB_PR_DEVICE &&
  548. us->protocol == idesc->bInterfaceProtocol)
  549. msg += 2;
  550. if (msg >= 0 && !(us->fflags & US_FL_NEED_OVERRIDE))
  551. dev_notice(pdev, "This device "
  552. "(%04x,%04x,%04x S %02x P %02x)"
  553. " has %s in unusual_devs.h (kernel"
  554. " %s)\n"
  555. " Please send a copy of this message to "
  556. "<linux-usb@vger.kernel.org> and "
  557. "<usb-storage@lists.one-eyed-alien.net>\n",
  558. le16_to_cpu(ddesc->idVendor),
  559. le16_to_cpu(ddesc->idProduct),
  560. le16_to_cpu(ddesc->bcdDevice),
  561. idesc->bInterfaceSubClass,
  562. idesc->bInterfaceProtocol,
  563. msgs[msg],
  564. utsname()->release);
  565. }
  566. return 0;
  567. }
  568. /* Get the transport settings */
  569. static void get_transport(struct us_data *us)
  570. {
  571. switch (us->protocol) {
  572. case USB_PR_CB:
  573. us->transport_name = "Control/Bulk";
  574. us->transport = usb_stor_CB_transport;
  575. us->transport_reset = usb_stor_CB_reset;
  576. us->max_lun = 7;
  577. break;
  578. case USB_PR_CBI:
  579. us->transport_name = "Control/Bulk/Interrupt";
  580. us->transport = usb_stor_CB_transport;
  581. us->transport_reset = usb_stor_CB_reset;
  582. us->max_lun = 7;
  583. break;
  584. case USB_PR_BULK:
  585. us->transport_name = "Bulk";
  586. us->transport = usb_stor_Bulk_transport;
  587. us->transport_reset = usb_stor_Bulk_reset;
  588. break;
  589. }
  590. }
  591. /* Get the protocol settings */
  592. static void get_protocol(struct us_data *us)
  593. {
  594. switch (us->subclass) {
  595. case USB_SC_RBC:
  596. us->protocol_name = "Reduced Block Commands (RBC)";
  597. us->proto_handler = usb_stor_transparent_scsi_command;
  598. break;
  599. case USB_SC_8020:
  600. us->protocol_name = "8020i";
  601. us->proto_handler = usb_stor_pad12_command;
  602. us->max_lun = 0;
  603. break;
  604. case USB_SC_QIC:
  605. us->protocol_name = "QIC-157";
  606. us->proto_handler = usb_stor_pad12_command;
  607. us->max_lun = 0;
  608. break;
  609. case USB_SC_8070:
  610. us->protocol_name = "8070i";
  611. us->proto_handler = usb_stor_pad12_command;
  612. us->max_lun = 0;
  613. break;
  614. case USB_SC_SCSI:
  615. us->protocol_name = "Transparent SCSI";
  616. us->proto_handler = usb_stor_transparent_scsi_command;
  617. break;
  618. case USB_SC_UFI:
  619. us->protocol_name = "Uniform Floppy Interface (UFI)";
  620. us->proto_handler = usb_stor_ufi_command;
  621. break;
  622. }
  623. }
  624. /* Get the pipe settings */
  625. static int get_pipes(struct us_data *us)
  626. {
  627. struct usb_host_interface *alt = us->pusb_intf->cur_altsetting;
  628. struct usb_endpoint_descriptor *ep_in;
  629. struct usb_endpoint_descriptor *ep_out;
  630. struct usb_endpoint_descriptor *ep_int;
  631. int res;
  632. /*
  633. * Find the first endpoint of each type we need.
  634. * We are expecting a minimum of 2 endpoints - in and out (bulk).
  635. * An optional interrupt-in is OK (necessary for CBI protocol).
  636. * We will ignore any others.
  637. */
  638. res = usb_find_common_endpoints(alt, &ep_in, &ep_out, NULL, NULL);
  639. if (res) {
  640. usb_stor_dbg(us, "bulk endpoints not found\n");
  641. return res;
  642. }
  643. res = usb_find_int_in_endpoint(alt, &ep_int);
  644. if (res && us->protocol == USB_PR_CBI) {
  645. usb_stor_dbg(us, "interrupt endpoint not found\n");
  646. return res;
  647. }
  648. /* Calculate and store the pipe values */
  649. us->send_ctrl_pipe = usb_sndctrlpipe(us->pusb_dev, 0);
  650. us->recv_ctrl_pipe = usb_rcvctrlpipe(us->pusb_dev, 0);
  651. us->send_bulk_pipe = usb_sndbulkpipe(us->pusb_dev,
  652. usb_endpoint_num(ep_out));
  653. us->recv_bulk_pipe = usb_rcvbulkpipe(us->pusb_dev,
  654. usb_endpoint_num(ep_in));
  655. if (ep_int) {
  656. us->recv_intr_pipe = usb_rcvintpipe(us->pusb_dev,
  657. usb_endpoint_num(ep_int));
  658. us->ep_bInterval = ep_int->bInterval;
  659. }
  660. return 0;
  661. }
  662. /* Initialize all the dynamic resources we need */
  663. static int usb_stor_acquire_resources(struct us_data *us)
  664. {
  665. int p;
  666. struct task_struct *th;
  667. us->current_urb = usb_alloc_urb(0, GFP_KERNEL);
  668. if (!us->current_urb)
  669. return -ENOMEM;
  670. /*
  671. * Just before we start our control thread, initialize
  672. * the device if it needs initialization
  673. */
  674. if (us->unusual_dev->initFunction) {
  675. p = us->unusual_dev->initFunction(us);
  676. if (p)
  677. return p;
  678. }
  679. /* Start up our control thread */
  680. th = kthread_run(usb_stor_control_thread, us, "usb-storage");
  681. if (IS_ERR(th)) {
  682. dev_warn(&us->pusb_intf->dev,
  683. "Unable to start control thread\n");
  684. return PTR_ERR(th);
  685. }
  686. us->ctl_thread = th;
  687. return 0;
  688. }
  689. /* Release all our dynamic resources */
  690. static void usb_stor_release_resources(struct us_data *us)
  691. {
  692. /*
  693. * Tell the control thread to exit. The SCSI host must
  694. * already have been removed and the DISCONNECTING flag set
  695. * so that we won't accept any more commands.
  696. */
  697. usb_stor_dbg(us, "-- sending exit command to thread\n");
  698. complete(&us->cmnd_ready);
  699. if (us->ctl_thread)
  700. kthread_stop(us->ctl_thread);
  701. /* Call the destructor routine, if it exists */
  702. if (us->extra_destructor) {
  703. usb_stor_dbg(us, "-- calling extra_destructor()\n");
  704. us->extra_destructor(us->extra);
  705. }
  706. /* Free the extra data and the URB */
  707. kfree(us->extra);
  708. usb_free_urb(us->current_urb);
  709. }
  710. /* Dissociate from the USB device */
  711. static void dissociate_dev(struct us_data *us)
  712. {
  713. /* Free the buffers */
  714. kfree(us->cr);
  715. usb_free_coherent(us->pusb_dev, US_IOBUF_SIZE, us->iobuf, us->iobuf_dma);
  716. /* Remove our private data from the interface */
  717. usb_set_intfdata(us->pusb_intf, NULL);
  718. }
  719. /*
  720. * First stage of disconnect processing: stop SCSI scanning,
  721. * remove the host, and stop accepting new commands
  722. */
  723. static void quiesce_and_remove_host(struct us_data *us)
  724. {
  725. struct Scsi_Host *host = us_to_host(us);
  726. /* If the device is really gone, cut short reset delays */
  727. if (us->pusb_dev->state == USB_STATE_NOTATTACHED) {
  728. set_bit(US_FLIDX_DISCONNECTING, &us->dflags);
  729. wake_up(&us->delay_wait);
  730. }
  731. /*
  732. * Prevent SCSI scanning (if it hasn't started yet)
  733. * or wait for the SCSI-scanning routine to stop.
  734. */
  735. cancel_delayed_work_sync(&us->scan_dwork);
  736. /* Balance autopm calls if scanning was cancelled */
  737. if (test_bit(US_FLIDX_SCAN_PENDING, &us->dflags))
  738. usb_autopm_put_interface_no_suspend(us->pusb_intf);
  739. /*
  740. * Removing the host will perform an orderly shutdown: caches
  741. * synchronized, disks spun down, etc.
  742. */
  743. scsi_remove_host(host);
  744. /*
  745. * Prevent any new commands from being accepted and cut short
  746. * reset delays.
  747. */
  748. scsi_lock(host);
  749. set_bit(US_FLIDX_DISCONNECTING, &us->dflags);
  750. scsi_unlock(host);
  751. wake_up(&us->delay_wait);
  752. }
  753. /* Second stage of disconnect processing: deallocate all resources */
  754. static void release_everything(struct us_data *us)
  755. {
  756. usb_stor_release_resources(us);
  757. dissociate_dev(us);
  758. /*
  759. * Drop our reference to the host; the SCSI core will free it
  760. * (and "us" along with it) when the refcount becomes 0.
  761. */
  762. scsi_host_put(us_to_host(us));
  763. }
  764. /* Delayed-work routine to carry out SCSI-device scanning */
  765. static void usb_stor_scan_dwork(struct work_struct *work)
  766. {
  767. struct us_data *us = container_of(work, struct us_data,
  768. scan_dwork.work);
  769. struct device *dev = &us->pusb_intf->dev;
  770. dev_dbg(dev, "starting scan\n");
  771. /* For bulk-only devices, determine the max LUN value */
  772. if (us->protocol == USB_PR_BULK &&
  773. !(us->fflags & US_FL_SINGLE_LUN) &&
  774. !(us->fflags & US_FL_SCM_MULT_TARG)) {
  775. mutex_lock(&us->dev_mutex);
  776. us->max_lun = usb_stor_Bulk_max_lun(us);
  777. /*
  778. * Allow proper scanning of devices that present more than 8 LUNs
  779. * While not affecting other devices that may need the previous
  780. * behavior
  781. */
  782. if (us->max_lun >= 8)
  783. us_to_host(us)->max_lun = us->max_lun+1;
  784. mutex_unlock(&us->dev_mutex);
  785. }
  786. scsi_scan_host(us_to_host(us));
  787. dev_dbg(dev, "scan complete\n");
  788. /* Should we unbind if no devices were detected? */
  789. usb_autopm_put_interface(us->pusb_intf);
  790. clear_bit(US_FLIDX_SCAN_PENDING, &us->dflags);
  791. }
  792. static unsigned int usb_stor_sg_tablesize(struct usb_interface *intf)
  793. {
  794. struct usb_device *usb_dev = interface_to_usbdev(intf);
  795. if (usb_dev->bus->sg_tablesize) {
  796. return usb_dev->bus->sg_tablesize;
  797. }
  798. return SG_ALL;
  799. }
  800. /* First part of general USB mass-storage probing */
  801. int usb_stor_probe1(struct us_data **pus,
  802. struct usb_interface *intf,
  803. const struct usb_device_id *id,
  804. const struct us_unusual_dev *unusual_dev,
  805. struct scsi_host_template *sht)
  806. {
  807. struct Scsi_Host *host;
  808. struct us_data *us;
  809. int result;
  810. dev_info(&intf->dev, "USB Mass Storage device detected\n");
  811. /*
  812. * Ask the SCSI layer to allocate a host structure, with extra
  813. * space at the end for our private us_data structure.
  814. */
  815. host = scsi_host_alloc(sht, sizeof(*us));
  816. if (!host) {
  817. dev_warn(&intf->dev, "Unable to allocate the scsi host\n");
  818. return -ENOMEM;
  819. }
  820. /*
  821. * Allow 16-byte CDBs and thus > 2TB
  822. */
  823. host->max_cmd_len = 16;
  824. host->sg_tablesize = usb_stor_sg_tablesize(intf);
  825. *pus = us = host_to_us(host);
  826. mutex_init(&(us->dev_mutex));
  827. us_set_lock_class(&us->dev_mutex, intf);
  828. init_completion(&us->cmnd_ready);
  829. init_completion(&(us->notify));
  830. init_waitqueue_head(&us->delay_wait);
  831. INIT_DELAYED_WORK(&us->scan_dwork, usb_stor_scan_dwork);
  832. /* Associate the us_data structure with the USB device */
  833. result = associate_dev(us, intf);
  834. if (result)
  835. goto BadDevice;
  836. /* Get the unusual_devs entries and the descriptors */
  837. result = get_device_info(us, id, unusual_dev);
  838. if (result)
  839. goto BadDevice;
  840. /* Get standard transport and protocol settings */
  841. get_transport(us);
  842. get_protocol(us);
  843. /*
  844. * Give the caller a chance to fill in specialized transport
  845. * or protocol settings.
  846. */
  847. return 0;
  848. BadDevice:
  849. usb_stor_dbg(us, "storage_probe() failed\n");
  850. release_everything(us);
  851. return result;
  852. }
  853. EXPORT_SYMBOL_GPL(usb_stor_probe1);
  854. /* Second part of general USB mass-storage probing */
  855. int usb_stor_probe2(struct us_data *us)
  856. {
  857. int result;
  858. struct device *dev = &us->pusb_intf->dev;
  859. /* Make sure the transport and protocol have both been set */
  860. if (!us->transport || !us->proto_handler) {
  861. result = -ENXIO;
  862. goto BadDevice;
  863. }
  864. usb_stor_dbg(us, "Transport: %s\n", us->transport_name);
  865. usb_stor_dbg(us, "Protocol: %s\n", us->protocol_name);
  866. if (us->fflags & US_FL_SCM_MULT_TARG) {
  867. /*
  868. * SCM eUSCSI bridge devices can have different numbers
  869. * of LUNs on different targets; allow all to be probed.
  870. */
  871. us->max_lun = 7;
  872. /* The eUSCSI itself has ID 7, so avoid scanning that */
  873. us_to_host(us)->this_id = 7;
  874. /* max_id is 8 initially, so no need to set it here */
  875. } else {
  876. /* In the normal case there is only a single target */
  877. us_to_host(us)->max_id = 1;
  878. /*
  879. * Like Windows, we won't store the LUN bits in CDB[1] for
  880. * SCSI-2 devices using the Bulk-Only transport (even though
  881. * this violates the SCSI spec).
  882. */
  883. if (us->transport == usb_stor_Bulk_transport)
  884. us_to_host(us)->no_scsi2_lun_in_cdb = 1;
  885. }
  886. /* fix for single-lun devices */
  887. if (us->fflags & US_FL_SINGLE_LUN)
  888. us->max_lun = 0;
  889. /* Find the endpoints and calculate pipe values */
  890. result = get_pipes(us);
  891. if (result)
  892. goto BadDevice;
  893. /*
  894. * If the device returns invalid data for the first READ(10)
  895. * command, indicate the command should be retried.
  896. */
  897. if (us->fflags & US_FL_INITIAL_READ10)
  898. set_bit(US_FLIDX_REDO_READ10, &us->dflags);
  899. /* Acquire all the other resources and add the host */
  900. result = usb_stor_acquire_resources(us);
  901. if (result)
  902. goto BadDevice;
  903. usb_autopm_get_interface_no_resume(us->pusb_intf);
  904. snprintf(us->scsi_name, sizeof(us->scsi_name), "usb-storage %s",
  905. dev_name(&us->pusb_intf->dev));
  906. result = scsi_add_host(us_to_host(us), dev);
  907. if (result) {
  908. dev_warn(dev,
  909. "Unable to add the scsi host\n");
  910. goto HostAddErr;
  911. }
  912. /* Submit the delayed_work for SCSI-device scanning */
  913. set_bit(US_FLIDX_SCAN_PENDING, &us->dflags);
  914. if (delay_use > 0)
  915. dev_dbg(dev, "waiting for device to settle before scanning\n");
  916. queue_delayed_work(system_freezable_wq, &us->scan_dwork,
  917. delay_use * HZ);
  918. return 0;
  919. /* We come here if there are any problems */
  920. HostAddErr:
  921. usb_autopm_put_interface_no_suspend(us->pusb_intf);
  922. BadDevice:
  923. usb_stor_dbg(us, "storage_probe() failed\n");
  924. release_everything(us);
  925. return result;
  926. }
  927. EXPORT_SYMBOL_GPL(usb_stor_probe2);
  928. /* Handle a USB mass-storage disconnect */
  929. void usb_stor_disconnect(struct usb_interface *intf)
  930. {
  931. struct us_data *us = usb_get_intfdata(intf);
  932. quiesce_and_remove_host(us);
  933. release_everything(us);
  934. }
  935. EXPORT_SYMBOL_GPL(usb_stor_disconnect);
  936. static struct scsi_host_template usb_stor_host_template;
  937. /* The main probe routine for standard devices */
  938. static int storage_probe(struct usb_interface *intf,
  939. const struct usb_device_id *id)
  940. {
  941. const struct us_unusual_dev *unusual_dev;
  942. struct us_data *us;
  943. int result;
  944. int size;
  945. /* If uas is enabled and this device can do uas then ignore it. */
  946. #if IS_ENABLED(CONFIG_USB_UAS)
  947. if (uas_use_uas_driver(intf, id, NULL))
  948. return -ENXIO;
  949. #endif
  950. /*
  951. * If the device isn't standard (is handled by a subdriver
  952. * module) then don't accept it.
  953. */
  954. if (usb_usual_ignore_device(intf))
  955. return -ENXIO;
  956. /*
  957. * Call the general probe procedures.
  958. *
  959. * The unusual_dev_list array is parallel to the usb_storage_usb_ids
  960. * table, so we use the index of the id entry to find the
  961. * corresponding unusual_devs entry.
  962. */
  963. size = ARRAY_SIZE(us_unusual_dev_list);
  964. if (id >= usb_storage_usb_ids && id < usb_storage_usb_ids + size) {
  965. unusual_dev = (id - usb_storage_usb_ids) + us_unusual_dev_list;
  966. } else {
  967. unusual_dev = &for_dynamic_ids;
  968. dev_dbg(&intf->dev, "Use Bulk-Only transport with the Transparent SCSI protocol for dynamic id: 0x%04x 0x%04x\n",
  969. id->idVendor, id->idProduct);
  970. }
  971. result = usb_stor_probe1(&us, intf, id, unusual_dev,
  972. &usb_stor_host_template);
  973. if (result)
  974. return result;
  975. /* No special transport or protocol settings in the main module */
  976. result = usb_stor_probe2(us);
  977. return result;
  978. }
  979. static struct usb_driver usb_storage_driver = {
  980. .name = DRV_NAME,
  981. .probe = storage_probe,
  982. .disconnect = usb_stor_disconnect,
  983. .suspend = usb_stor_suspend,
  984. .resume = usb_stor_resume,
  985. .reset_resume = usb_stor_reset_resume,
  986. .pre_reset = usb_stor_pre_reset,
  987. .post_reset = usb_stor_post_reset,
  988. .id_table = usb_storage_usb_ids,
  989. .supports_autosuspend = 1,
  990. .soft_unbind = 1,
  991. };
  992. module_usb_stor_driver(usb_storage_driver, usb_stor_host_template, DRV_NAME);