usb.c 31 KB

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