usb.c 29 KB

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