usb.c 31 KB

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