usb.c 29 KB

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