usb.c 33 KB

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