usb.c 31 KB

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