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