usb.c 31 KB

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