usb.c 31 KB

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