usb.c 31 KB

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