usb.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195
  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. /* Some informational data */
  100. MODULE_AUTHOR("Matthew Dharm <mdharm-usb@one-eyed-alien.net>");
  101. MODULE_DESCRIPTION("USB Mass Storage driver for Linux");
  102. MODULE_LICENSE("GPL");
  103. static unsigned int delay_use = 5;
  104. module_param(delay_use, uint, S_IRUGO | S_IWUSR);
  105. MODULE_PARM_DESC(delay_use, "seconds to delay before using a new device");
  106. static char *quirks;
  107. module_param(quirks, charp, S_IRUGO);
  108. MODULE_PARM_DESC(quirks, "supplemental list of device IDs and their quirks");
  109. struct quirks_entry {
  110. u16 vid, pid;
  111. u32 fflags;
  112. };
  113. static struct quirks_entry *quirks_list, *quirks_end;
  114. /*
  115. * The entries in this table correspond, line for line,
  116. * with the entries of us_unusual_dev_list[].
  117. */
  118. #ifndef CONFIG_USB_LIBUSUAL
  119. #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
  120. vendorName, productName,useProtocol, useTransport, \
  121. initFunction, flags) \
  122. { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin,bcdDeviceMax), \
  123. .driver_info = (flags)|(USB_US_TYPE_STOR<<24) }
  124. #define USUAL_DEV(useProto, useTrans, useType) \
  125. { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, useProto, useTrans), \
  126. .driver_info = (USB_US_TYPE_STOR<<24) }
  127. static struct usb_device_id storage_usb_ids [] = {
  128. # include "unusual_devs.h"
  129. #undef UNUSUAL_DEV
  130. #undef USUAL_DEV
  131. /* Terminating entry */
  132. { }
  133. };
  134. MODULE_DEVICE_TABLE (usb, storage_usb_ids);
  135. #endif /* CONFIG_USB_LIBUSUAL */
  136. /* This is the list of devices we recognize, along with their flag data */
  137. /* The vendor name should be kept at eight characters or less, and
  138. * the product name should be kept at 16 characters or less. If a device
  139. * has the US_FL_FIX_INQUIRY flag, then the vendor and product names
  140. * normally generated by a device thorugh the INQUIRY response will be
  141. * taken from this list, and this is the reason for the above size
  142. * restriction. However, if the flag is not present, then you
  143. * are free to use as many characters as you like.
  144. */
  145. #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
  146. vendor_name, product_name, use_protocol, use_transport, \
  147. init_function, Flags) \
  148. { \
  149. .vendorName = vendor_name, \
  150. .productName = product_name, \
  151. .useProtocol = use_protocol, \
  152. .useTransport = use_transport, \
  153. .initFunction = init_function, \
  154. }
  155. #define USUAL_DEV(use_protocol, use_transport, use_type) \
  156. { \
  157. .useProtocol = use_protocol, \
  158. .useTransport = use_transport, \
  159. }
  160. static struct us_unusual_dev us_unusual_dev_list[] = {
  161. # include "unusual_devs.h"
  162. # undef UNUSUAL_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. /* Adjust device flags based on the "quirks=" module parameter */
  400. static void adjust_quirks(struct us_data *us)
  401. {
  402. u16 vid, pid;
  403. struct quirks_entry *q;
  404. unsigned int mask = (US_FL_FIX_CAPACITY | US_FL_IGNORE_DEVICE |
  405. US_FL_NOT_LOCKABLE | US_FL_MAX_SECTORS_64 |
  406. US_FL_IGNORE_RESIDUE | US_FL_SINGLE_LUN |
  407. US_FL_NO_WP_DETECT);
  408. vid = le16_to_cpu(us->pusb_dev->descriptor.idVendor);
  409. pid = le16_to_cpu(us->pusb_dev->descriptor.idProduct);
  410. for (q = quirks_list; q != quirks_end; ++q) {
  411. if (q->vid == vid && q->pid == pid) {
  412. us->fflags = (us->fflags & ~mask) | q->fflags;
  413. dev_info(&us->pusb_intf->dev, "Quirks match for "
  414. "vid %04x pid %04x: %x\n",
  415. vid, pid, q->fflags);
  416. break;
  417. }
  418. }
  419. }
  420. /* Find an unusual_dev descriptor (always succeeds in the current code) */
  421. static struct us_unusual_dev *find_unusual(const struct usb_device_id *id)
  422. {
  423. const int id_index = id - storage_usb_ids;
  424. return &us_unusual_dev_list[id_index];
  425. }
  426. /* Get the unusual_devs entries and the string descriptors */
  427. static int get_device_info(struct us_data *us, const struct usb_device_id *id)
  428. {
  429. struct usb_device *dev = us->pusb_dev;
  430. struct usb_interface_descriptor *idesc =
  431. &us->pusb_intf->cur_altsetting->desc;
  432. struct us_unusual_dev *unusual_dev = find_unusual(id);
  433. /* Store the entries */
  434. us->unusual_dev = unusual_dev;
  435. us->subclass = (unusual_dev->useProtocol == US_SC_DEVICE) ?
  436. idesc->bInterfaceSubClass :
  437. unusual_dev->useProtocol;
  438. us->protocol = (unusual_dev->useTransport == US_PR_DEVICE) ?
  439. idesc->bInterfaceProtocol :
  440. unusual_dev->useTransport;
  441. us->fflags = USB_US_ORIG_FLAGS(id->driver_info);
  442. adjust_quirks(us);
  443. if (us->fflags & US_FL_IGNORE_DEVICE) {
  444. printk(KERN_INFO USB_STORAGE "device ignored\n");
  445. return -ENODEV;
  446. }
  447. /*
  448. * This flag is only needed when we're in high-speed, so let's
  449. * disable it if we're in full-speed
  450. */
  451. if (dev->speed != USB_SPEED_HIGH)
  452. us->fflags &= ~US_FL_GO_SLOW;
  453. /* Log a message if a non-generic unusual_dev entry contains an
  454. * unnecessary subclass or protocol override. This may stimulate
  455. * reports from users that will help us remove unneeded entries
  456. * from the unusual_devs.h table.
  457. */
  458. if (id->idVendor || id->idProduct) {
  459. static const char *msgs[3] = {
  460. "an unneeded SubClass entry",
  461. "an unneeded Protocol entry",
  462. "unneeded SubClass and Protocol entries"};
  463. struct usb_device_descriptor *ddesc = &dev->descriptor;
  464. int msg = -1;
  465. if (unusual_dev->useProtocol != US_SC_DEVICE &&
  466. us->subclass == idesc->bInterfaceSubClass)
  467. msg += 1;
  468. if (unusual_dev->useTransport != US_PR_DEVICE &&
  469. us->protocol == idesc->bInterfaceProtocol)
  470. msg += 2;
  471. if (msg >= 0 && !(us->fflags & US_FL_NEED_OVERRIDE))
  472. printk(KERN_NOTICE USB_STORAGE "This device "
  473. "(%04x,%04x,%04x S %02x P %02x)"
  474. " has %s in unusual_devs.h (kernel"
  475. " %s)\n"
  476. " Please send a copy of this message to "
  477. "<linux-usb@vger.kernel.org> and "
  478. "<usb-storage@lists.one-eyed-alien.net>\n",
  479. le16_to_cpu(ddesc->idVendor),
  480. le16_to_cpu(ddesc->idProduct),
  481. le16_to_cpu(ddesc->bcdDevice),
  482. idesc->bInterfaceSubClass,
  483. idesc->bInterfaceProtocol,
  484. msgs[msg],
  485. utsname()->release);
  486. }
  487. return 0;
  488. }
  489. /* Get the transport settings */
  490. static int get_transport(struct us_data *us)
  491. {
  492. switch (us->protocol) {
  493. case US_PR_CB:
  494. us->transport_name = "Control/Bulk";
  495. us->transport = usb_stor_CB_transport;
  496. us->transport_reset = usb_stor_CB_reset;
  497. us->max_lun = 7;
  498. break;
  499. case US_PR_CBI:
  500. us->transport_name = "Control/Bulk/Interrupt";
  501. us->transport = usb_stor_CB_transport;
  502. us->transport_reset = usb_stor_CB_reset;
  503. us->max_lun = 7;
  504. break;
  505. case US_PR_BULK:
  506. us->transport_name = "Bulk";
  507. us->transport = usb_stor_Bulk_transport;
  508. us->transport_reset = usb_stor_Bulk_reset;
  509. break;
  510. #ifdef CONFIG_USB_STORAGE_USBAT
  511. case US_PR_USBAT:
  512. us->transport_name = "Shuttle USBAT";
  513. us->transport = usbat_transport;
  514. us->transport_reset = usb_stor_CB_reset;
  515. us->max_lun = 1;
  516. break;
  517. #endif
  518. #ifdef CONFIG_USB_STORAGE_SDDR09
  519. case US_PR_EUSB_SDDR09:
  520. us->transport_name = "EUSB/SDDR09";
  521. us->transport = sddr09_transport;
  522. us->transport_reset = usb_stor_CB_reset;
  523. us->max_lun = 0;
  524. break;
  525. #endif
  526. #ifdef CONFIG_USB_STORAGE_SDDR55
  527. case US_PR_SDDR55:
  528. us->transport_name = "SDDR55";
  529. us->transport = sddr55_transport;
  530. us->transport_reset = sddr55_reset;
  531. us->max_lun = 0;
  532. break;
  533. #endif
  534. #ifdef CONFIG_USB_STORAGE_DPCM
  535. case US_PR_DPCM_USB:
  536. us->transport_name = "Control/Bulk-EUSB/SDDR09";
  537. us->transport = dpcm_transport;
  538. us->transport_reset = usb_stor_CB_reset;
  539. us->max_lun = 1;
  540. break;
  541. #endif
  542. #ifdef CONFIG_USB_STORAGE_FREECOM
  543. case US_PR_FREECOM:
  544. us->transport_name = "Freecom";
  545. us->transport = freecom_transport;
  546. us->transport_reset = usb_stor_freecom_reset;
  547. us->max_lun = 0;
  548. break;
  549. #endif
  550. #ifdef CONFIG_USB_STORAGE_DATAFAB
  551. case US_PR_DATAFAB:
  552. us->transport_name = "Datafab Bulk-Only";
  553. us->transport = datafab_transport;
  554. us->transport_reset = usb_stor_Bulk_reset;
  555. us->max_lun = 1;
  556. break;
  557. #endif
  558. #ifdef CONFIG_USB_STORAGE_JUMPSHOT
  559. case US_PR_JUMPSHOT:
  560. us->transport_name = "Lexar Jumpshot Control/Bulk";
  561. us->transport = jumpshot_transport;
  562. us->transport_reset = usb_stor_Bulk_reset;
  563. us->max_lun = 1;
  564. break;
  565. #endif
  566. #ifdef CONFIG_USB_STORAGE_ALAUDA
  567. case US_PR_ALAUDA:
  568. us->transport_name = "Alauda Control/Bulk";
  569. us->transport = alauda_transport;
  570. us->transport_reset = usb_stor_Bulk_reset;
  571. us->max_lun = 1;
  572. break;
  573. #endif
  574. #ifdef CONFIG_USB_STORAGE_KARMA
  575. case US_PR_KARMA:
  576. us->transport_name = "Rio Karma/Bulk";
  577. us->transport = rio_karma_transport;
  578. us->transport_reset = usb_stor_Bulk_reset;
  579. break;
  580. #endif
  581. default:
  582. return -EIO;
  583. }
  584. US_DEBUGP("Transport: %s\n", us->transport_name);
  585. /* fix for single-lun devices */
  586. if (us->fflags & US_FL_SINGLE_LUN)
  587. us->max_lun = 0;
  588. return 0;
  589. }
  590. /* Get the protocol settings */
  591. static int get_protocol(struct us_data *us)
  592. {
  593. switch (us->subclass) {
  594. case US_SC_RBC:
  595. us->protocol_name = "Reduced Block Commands (RBC)";
  596. us->proto_handler = usb_stor_transparent_scsi_command;
  597. break;
  598. case US_SC_8020:
  599. us->protocol_name = "8020i";
  600. us->proto_handler = usb_stor_pad12_command;
  601. us->max_lun = 0;
  602. break;
  603. case US_SC_QIC:
  604. us->protocol_name = "QIC-157";
  605. us->proto_handler = usb_stor_pad12_command;
  606. us->max_lun = 0;
  607. break;
  608. case US_SC_8070:
  609. us->protocol_name = "8070i";
  610. us->proto_handler = usb_stor_pad12_command;
  611. us->max_lun = 0;
  612. break;
  613. case US_SC_SCSI:
  614. us->protocol_name = "Transparent SCSI";
  615. us->proto_handler = usb_stor_transparent_scsi_command;
  616. break;
  617. case US_SC_UFI:
  618. us->protocol_name = "Uniform Floppy Interface (UFI)";
  619. us->proto_handler = usb_stor_ufi_command;
  620. break;
  621. #ifdef CONFIG_USB_STORAGE_ISD200
  622. case US_SC_ISD200:
  623. us->protocol_name = "ISD200 ATA/ATAPI";
  624. us->proto_handler = isd200_ata_command;
  625. break;
  626. #endif
  627. #ifdef CONFIG_USB_STORAGE_CYPRESS_ATACB
  628. case US_SC_CYP_ATACB:
  629. us->protocol_name = "Transparent SCSI with Cypress ATACB";
  630. us->proto_handler = cypress_atacb_passthrough;
  631. break;
  632. #endif
  633. default:
  634. return -EIO;
  635. }
  636. US_DEBUGP("Protocol: %s\n", us->protocol_name);
  637. return 0;
  638. }
  639. /* Get the pipe settings */
  640. static int get_pipes(struct us_data *us)
  641. {
  642. struct usb_host_interface *altsetting =
  643. us->pusb_intf->cur_altsetting;
  644. int i;
  645. struct usb_endpoint_descriptor *ep;
  646. struct usb_endpoint_descriptor *ep_in = NULL;
  647. struct usb_endpoint_descriptor *ep_out = NULL;
  648. struct usb_endpoint_descriptor *ep_int = NULL;
  649. /*
  650. * Find the first endpoint of each type we need.
  651. * We are expecting a minimum of 2 endpoints - in and out (bulk).
  652. * An optional interrupt-in is OK (necessary for CBI protocol).
  653. * We will ignore any others.
  654. */
  655. for (i = 0; i < altsetting->desc.bNumEndpoints; i++) {
  656. ep = &altsetting->endpoint[i].desc;
  657. if (usb_endpoint_xfer_bulk(ep)) {
  658. if (usb_endpoint_dir_in(ep)) {
  659. if (!ep_in)
  660. ep_in = ep;
  661. } else {
  662. if (!ep_out)
  663. ep_out = ep;
  664. }
  665. }
  666. else if (usb_endpoint_is_int_in(ep)) {
  667. if (!ep_int)
  668. ep_int = ep;
  669. }
  670. }
  671. if (!ep_in || !ep_out || (us->protocol == US_PR_CBI && !ep_int)) {
  672. US_DEBUGP("Endpoint sanity check failed! Rejecting dev.\n");
  673. return -EIO;
  674. }
  675. /* Calculate and store the pipe values */
  676. us->send_ctrl_pipe = usb_sndctrlpipe(us->pusb_dev, 0);
  677. us->recv_ctrl_pipe = usb_rcvctrlpipe(us->pusb_dev, 0);
  678. us->send_bulk_pipe = usb_sndbulkpipe(us->pusb_dev,
  679. ep_out->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
  680. us->recv_bulk_pipe = usb_rcvbulkpipe(us->pusb_dev,
  681. ep_in->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
  682. if (ep_int) {
  683. us->recv_intr_pipe = usb_rcvintpipe(us->pusb_dev,
  684. ep_int->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
  685. us->ep_bInterval = ep_int->bInterval;
  686. }
  687. return 0;
  688. }
  689. /* Initialize all the dynamic resources we need */
  690. static int usb_stor_acquire_resources(struct us_data *us)
  691. {
  692. int p;
  693. struct task_struct *th;
  694. us->current_urb = usb_alloc_urb(0, GFP_KERNEL);
  695. if (!us->current_urb) {
  696. US_DEBUGP("URB allocation failed\n");
  697. return -ENOMEM;
  698. }
  699. /* Just before we start our control thread, initialize
  700. * the device if it needs initialization */
  701. if (us->unusual_dev->initFunction) {
  702. p = us->unusual_dev->initFunction(us);
  703. if (p)
  704. return p;
  705. }
  706. /* Start up our control thread */
  707. th = kthread_run(usb_stor_control_thread, us, "usb-storage");
  708. if (IS_ERR(th)) {
  709. printk(KERN_WARNING USB_STORAGE
  710. "Unable to start control thread\n");
  711. return PTR_ERR(th);
  712. }
  713. us->ctl_thread = th;
  714. return 0;
  715. }
  716. /* Release all our dynamic resources */
  717. static void usb_stor_release_resources(struct us_data *us)
  718. {
  719. US_DEBUGP("-- %s\n", __func__);
  720. /* Tell the control thread to exit. The SCSI host must
  721. * already have been removed and the DISCONNECTING flag set
  722. * so that we won't accept any more commands.
  723. */
  724. US_DEBUGP("-- sending exit command to thread\n");
  725. complete(&us->cmnd_ready);
  726. if (us->ctl_thread)
  727. kthread_stop(us->ctl_thread);
  728. /* Call the destructor routine, if it exists */
  729. if (us->extra_destructor) {
  730. US_DEBUGP("-- calling extra_destructor()\n");
  731. us->extra_destructor(us->extra);
  732. }
  733. /* Free the extra data and the URB */
  734. kfree(us->extra);
  735. usb_free_urb(us->current_urb);
  736. }
  737. /* Dissociate from the USB device */
  738. static void dissociate_dev(struct us_data *us)
  739. {
  740. US_DEBUGP("-- %s\n", __func__);
  741. /* Free the device-related DMA-mapped buffers */
  742. if (us->cr)
  743. usb_buffer_free(us->pusb_dev, sizeof(*us->cr), us->cr,
  744. us->cr_dma);
  745. if (us->iobuf)
  746. usb_buffer_free(us->pusb_dev, US_IOBUF_SIZE, us->iobuf,
  747. us->iobuf_dma);
  748. /* Remove our private data from the interface */
  749. usb_set_intfdata(us->pusb_intf, NULL);
  750. }
  751. /* First stage of disconnect processing: stop SCSI scanning,
  752. * remove the host, and stop accepting new commands
  753. */
  754. static void quiesce_and_remove_host(struct us_data *us)
  755. {
  756. struct Scsi_Host *host = us_to_host(us);
  757. /* If the device is really gone, cut short reset delays */
  758. if (us->pusb_dev->state == USB_STATE_NOTATTACHED)
  759. set_bit(US_FLIDX_DISCONNECTING, &us->dflags);
  760. /* Prevent SCSI-scanning (if it hasn't started yet)
  761. * and wait for the SCSI-scanning thread to stop.
  762. */
  763. set_bit(US_FLIDX_DONT_SCAN, &us->dflags);
  764. wake_up(&us->delay_wait);
  765. wait_for_completion(&us->scanning_done);
  766. /* Removing the host will perform an orderly shutdown: caches
  767. * synchronized, disks spun down, etc.
  768. */
  769. scsi_remove_host(host);
  770. /* Prevent any new commands from being accepted and cut short
  771. * reset delays.
  772. */
  773. scsi_lock(host);
  774. set_bit(US_FLIDX_DISCONNECTING, &us->dflags);
  775. scsi_unlock(host);
  776. wake_up(&us->delay_wait);
  777. }
  778. /* Second stage of disconnect processing: deallocate all resources */
  779. static void release_everything(struct us_data *us)
  780. {
  781. usb_stor_release_resources(us);
  782. dissociate_dev(us);
  783. /* Drop our reference to the host; the SCSI core will free it
  784. * (and "us" along with it) when the refcount becomes 0. */
  785. scsi_host_put(us_to_host(us));
  786. }
  787. /* Thread to carry out delayed SCSI-device scanning */
  788. static int usb_stor_scan_thread(void * __us)
  789. {
  790. struct us_data *us = (struct us_data *)__us;
  791. printk(KERN_DEBUG
  792. "usb-storage: device found at %d\n", us->pusb_dev->devnum);
  793. set_freezable();
  794. /* Wait for the timeout to expire or for a disconnect */
  795. if (delay_use > 0) {
  796. printk(KERN_DEBUG "usb-storage: waiting for device "
  797. "to settle before scanning\n");
  798. wait_event_freezable_timeout(us->delay_wait,
  799. test_bit(US_FLIDX_DONT_SCAN, &us->dflags),
  800. delay_use * HZ);
  801. }
  802. /* If the device is still connected, perform the scanning */
  803. if (!test_bit(US_FLIDX_DONT_SCAN, &us->dflags)) {
  804. /* For bulk-only devices, determine the max LUN value */
  805. if (us->protocol == US_PR_BULK &&
  806. !(us->fflags & US_FL_SINGLE_LUN)) {
  807. mutex_lock(&us->dev_mutex);
  808. us->max_lun = usb_stor_Bulk_max_lun(us);
  809. mutex_unlock(&us->dev_mutex);
  810. }
  811. scsi_scan_host(us_to_host(us));
  812. printk(KERN_DEBUG "usb-storage: device scan complete\n");
  813. /* Should we unbind if no devices were detected? */
  814. }
  815. complete_and_exit(&us->scanning_done, 0);
  816. }
  817. /* Probe to see if we can drive a newly-connected USB device */
  818. static int storage_probe(struct usb_interface *intf,
  819. const struct usb_device_id *id)
  820. {
  821. struct Scsi_Host *host;
  822. struct us_data *us;
  823. int result;
  824. struct task_struct *th;
  825. if (usb_usual_check_type(id, USB_US_TYPE_STOR))
  826. return -ENXIO;
  827. US_DEBUGP("USB Mass Storage device detected\n");
  828. /*
  829. * Ask the SCSI layer to allocate a host structure, with extra
  830. * space at the end for our private us_data structure.
  831. */
  832. host = scsi_host_alloc(&usb_stor_host_template, sizeof(*us));
  833. if (!host) {
  834. printk(KERN_WARNING USB_STORAGE
  835. "Unable to allocate the scsi host\n");
  836. return -ENOMEM;
  837. }
  838. /*
  839. * Allow 16-byte CDBs and thus > 2TB
  840. */
  841. host->max_cmd_len = 16;
  842. us = host_to_us(host);
  843. memset(us, 0, sizeof(struct us_data));
  844. mutex_init(&(us->dev_mutex));
  845. init_completion(&us->cmnd_ready);
  846. init_completion(&(us->notify));
  847. init_waitqueue_head(&us->delay_wait);
  848. init_completion(&us->scanning_done);
  849. /* Associate the us_data structure with the USB device */
  850. result = associate_dev(us, intf);
  851. if (result)
  852. goto BadDevice;
  853. /*
  854. * Get the unusual_devs entries and the descriptors
  855. *
  856. * id_index is calculated in the declaration to be the index number
  857. * of the match from the usb_device_id table, so we can find the
  858. * corresponding entry in the private table.
  859. */
  860. result = get_device_info(us, id);
  861. if (result)
  862. goto BadDevice;
  863. /* Get the transport, protocol, and pipe settings */
  864. result = get_transport(us);
  865. if (result)
  866. goto BadDevice;
  867. result = get_protocol(us);
  868. if (result)
  869. goto BadDevice;
  870. result = get_pipes(us);
  871. if (result)
  872. goto BadDevice;
  873. /* Acquire all the other resources and add the host */
  874. result = usb_stor_acquire_resources(us);
  875. if (result)
  876. goto BadDevice;
  877. result = scsi_add_host(host, &intf->dev);
  878. if (result) {
  879. printk(KERN_WARNING USB_STORAGE
  880. "Unable to add the scsi host\n");
  881. goto BadDevice;
  882. }
  883. /* Start up the thread for delayed SCSI-device scanning */
  884. th = kthread_create(usb_stor_scan_thread, us, "usb-stor-scan");
  885. if (IS_ERR(th)) {
  886. printk(KERN_WARNING USB_STORAGE
  887. "Unable to start the device-scanning thread\n");
  888. complete(&us->scanning_done);
  889. quiesce_and_remove_host(us);
  890. result = PTR_ERR(th);
  891. goto BadDevice;
  892. }
  893. wake_up_process(th);
  894. return 0;
  895. /* We come here if there are any problems */
  896. BadDevice:
  897. US_DEBUGP("storage_probe() failed\n");
  898. release_everything(us);
  899. return result;
  900. }
  901. /* Handle a disconnect event from the USB core */
  902. static void storage_disconnect(struct usb_interface *intf)
  903. {
  904. struct us_data *us = usb_get_intfdata(intf);
  905. US_DEBUGP("storage_disconnect() called\n");
  906. quiesce_and_remove_host(us);
  907. release_everything(us);
  908. }
  909. /***********************************************************************
  910. * Initialization and registration
  911. ***********************************************************************/
  912. static struct usb_driver usb_storage_driver = {
  913. .name = "usb-storage",
  914. .probe = storage_probe,
  915. .disconnect = storage_disconnect,
  916. #ifdef CONFIG_PM
  917. .suspend = storage_suspend,
  918. .resume = storage_resume,
  919. .reset_resume = storage_reset_resume,
  920. #endif
  921. .pre_reset = storage_pre_reset,
  922. .post_reset = storage_post_reset,
  923. .id_table = storage_usb_ids,
  924. .soft_unbind = 1,
  925. };
  926. /* Works only for digits and letters, but small and fast */
  927. #define TOLOWER(x) ((x) | 0x20)
  928. static void __init parse_quirks(void)
  929. {
  930. int n, i;
  931. char *p;
  932. if (!quirks)
  933. return;
  934. /* Count the ':' characters to get 2 * the number of entries */
  935. n = 0;
  936. for (p = quirks; *p; ++p) {
  937. if (*p == ':')
  938. ++n;
  939. }
  940. n /= 2;
  941. if (n == 0)
  942. return; /* Don't allocate 0 bytes */
  943. quirks_list = kmalloc(n * sizeof(*quirks_list), GFP_KERNEL);
  944. if (!quirks_list)
  945. return;
  946. p = quirks;
  947. quirks_end = quirks_list;
  948. for (i = 0; i < n && *p; ++i) {
  949. unsigned f = 0;
  950. /* Each entry consists of VID:PID:flags */
  951. quirks_end->vid = simple_strtoul(p, &p, 16);
  952. if (*p != ':')
  953. goto skip_to_next;
  954. quirks_end->pid = simple_strtoul(p+1, &p, 16);
  955. if (*p != ':')
  956. goto skip_to_next;
  957. while (*++p && *p != ',') {
  958. switch (TOLOWER(*p)) {
  959. case 'c':
  960. f |= US_FL_FIX_CAPACITY;
  961. break;
  962. case 'i':
  963. f |= US_FL_IGNORE_DEVICE;
  964. break;
  965. case 'l':
  966. f |= US_FL_NOT_LOCKABLE;
  967. break;
  968. case 'm':
  969. f |= US_FL_MAX_SECTORS_64;
  970. break;
  971. case 'r':
  972. f |= US_FL_IGNORE_RESIDUE;
  973. break;
  974. case 's':
  975. f |= US_FL_SINGLE_LUN;
  976. break;
  977. case 'w':
  978. f |= US_FL_NO_WP_DETECT;
  979. break;
  980. /* Ignore unrecognized flag characters */
  981. }
  982. }
  983. quirks_end->fflags = f;
  984. ++quirks_end;
  985. skip_to_next:
  986. /* Entries are separated by commas */
  987. while (*p) {
  988. if (*p++ == ',')
  989. break;
  990. }
  991. } /* for (i = 0; ...) */
  992. }
  993. static int __init usb_stor_init(void)
  994. {
  995. int retval;
  996. printk(KERN_INFO "Initializing USB Mass Storage driver...\n");
  997. parse_quirks();
  998. /* register the driver, return usb_register return code if error */
  999. retval = usb_register(&usb_storage_driver);
  1000. if (retval == 0) {
  1001. printk(KERN_INFO "USB Mass Storage support registered.\n");
  1002. usb_usual_set_present(USB_US_TYPE_STOR);
  1003. }
  1004. return retval;
  1005. }
  1006. static void __exit usb_stor_exit(void)
  1007. {
  1008. US_DEBUGP("usb_stor_exit() called\n");
  1009. /* Deregister the driver
  1010. * This will cause disconnect() to be called for each
  1011. * attached unit
  1012. */
  1013. US_DEBUGP("-- calling usb_deregister()\n");
  1014. usb_deregister(&usb_storage_driver) ;
  1015. usb_usual_clear_present(USB_US_TYPE_STOR);
  1016. }
  1017. module_init(usb_stor_init);
  1018. module_exit(usb_stor_exit);