usb.c 31 KB

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