usb.c 29 KB

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