usb.c 31 KB

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