usb.c 30 KB

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