usb.c 31 KB

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