usb.c 30 KB

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