1/* Driver for USB Mass Storage compliant devices 2 * 3 * $Id: usb.c,v 1.1.1.1 2008/10/15 03:27:04 james26_jang Exp $ 4 * 5 * Current development and maintenance by: 6 * (c) 1999-2002 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 * 11 * Initial work by: 12 * (c) 1999 Michael Gee (michael@linuxspecific.com) 13 * 14 * usb_device_id support by Adam J. Richter (adam@yggdrasil.com): 15 * (c) 2000 Yggdrasil Computing, Inc. 16 * 17 * This driver is based on the 'USB Mass Storage Class' document. This 18 * describes in detail the protocol used to communicate with such 19 * devices. Clearly, the designers had SCSI and ATAPI commands in 20 * mind when they created this document. The commands are all very 21 * similar to commands in the SCSI-II and ATAPI specifications. 22 * 23 * It is important to note that in a number of cases this class 24 * exhibits class-specific exemptions from the USB specification. 25 * Notably the usage of NAK, STALL and ACK differs from the norm, in 26 * that they are used to communicate wait, failed and OK on commands. 27 * 28 * Also, for certain devices, the interrupt endpoint is used to convey 29 * status of a command. 30 * 31 * Please see http://www.one-eyed-alien.net/~mdharm/linux-usb for more 32 * information about this driver. 33 * 34 * This program is free software; you can redistribute it and/or modify it 35 * under the terms of the GNU General Public License as published by the 36 * Free Software Foundation; either version 2, or (at your option) any 37 * later version. 38 * 39 * This program is distributed in the hope that it will be useful, but 40 * WITHOUT ANY WARRANTY; without even the implied warranty of 41 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 42 * General Public License for more details. 43 * 44 * You should have received a copy of the GNU General Public License along 45 * with this program; if not, write to the Free Software Foundation, Inc., 46 * 675 Mass Ave, Cambridge, MA 02139, USA. 47 */ 48 49#include <linux/config.h> 50#include "usb.h" 51#include "scsiglue.h" 52#include "transport.h" 53#include "protocol.h" 54#include "debug.h" 55#include "initializers.h" 56 57#ifdef CONFIG_USB_STORAGE_HP8200e 58#include "shuttle_usbat.h" 59#endif 60#ifdef CONFIG_USB_STORAGE_SDDR09 61#include "sddr09.h" 62#endif 63#ifdef CONFIG_USB_STORAGE_SDDR55 64#include "sddr55.h" 65#endif 66#ifdef CONFIG_USB_STORAGE_DPCM 67#include "dpcm.h" 68#endif 69#ifdef CONFIG_USB_STORAGE_FREECOM 70#include "freecom.h" 71#endif 72#ifdef CONFIG_USB_STORAGE_ISD200 73#include "isd200.h" 74#endif 75#ifdef CONFIG_USB_STORAGE_DATAFAB 76#include "datafab.h" 77#endif 78#ifdef CONFIG_USB_STORAGE_JUMPSHOT 79#include "jumpshot.h" 80#endif 81 82 83#include <linux/module.h> 84#include <linux/sched.h> 85#include <linux/errno.h> 86#include <linux/init.h> 87#include <linux/slab.h> 88 89/* Some informational data */ 90MODULE_AUTHOR("Matthew Dharm <mdharm-usb@one-eyed-alien.net>"); 91MODULE_DESCRIPTION("USB Mass Storage driver for Linux"); 92MODULE_LICENSE("GPL"); 93 94/* 95 * Per device data 96 */ 97 98static int my_host_number; 99 100/* 101 * kernel thread actions 102 */ 103 104#define US_ACT_COMMAND 1 105#define US_ACT_DEVICE_RESET 2 106#define US_ACT_BUS_RESET 3 107#define US_ACT_HOST_RESET 4 108#define US_ACT_EXIT 5 109#define US_ACT_CONNECT 6 110#define US_ACT_DISCONNECT 7 111 112/* The list of structures and the protective lock for them */ 113struct us_data *us_list; 114struct semaphore us_list_semaphore; 115 116static void * storage_probe(struct usb_device *dev, unsigned int ifnum, 117 const struct usb_device_id *id); 118 119static void storage_disconnect(struct usb_device *dev, void *ptr); 120 121/* The entries in this table, except for final ones here 122 * (USB_MASS_STORAGE_CLASS and the empty entry), correspond, 123 * line for line with the entries of us_unsuaul_dev_list[]. 124 * For now, we duplicate idVendor and idProduct in us_unsual_dev_list, 125 * just to avoid alignment bugs. 126 */ 127 128#define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \ 129 vendorName, productName,useProtocol, useTransport, \ 130 initFunction, flags) \ 131{ USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin,bcdDeviceMax) } 132 133static struct usb_device_id storage_usb_ids [] = { 134 135# include "unusual_devs.h" 136#undef UNUSUAL_DEV 137 /* Control/Bulk transport for all SubClass values */ 138 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_RBC, US_PR_CB) }, 139 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_8020, US_PR_CB) }, 140 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_QIC, US_PR_CB) }, 141 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_UFI, US_PR_CB) }, 142 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_8070, US_PR_CB) }, 143 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_SCSI, US_PR_CB) }, 144 145 /* Control/Bulk/Interrupt transport for all SubClass values */ 146 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_RBC, US_PR_CBI) }, 147 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_8020, US_PR_CBI) }, 148 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_QIC, US_PR_CBI) }, 149 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_UFI, US_PR_CBI) }, 150 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_8070, US_PR_CBI) }, 151 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_SCSI, US_PR_CBI) }, 152 153 /* Bulk-only transport for all SubClass values */ 154 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_RBC, US_PR_BULK) }, 155 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_8020, US_PR_BULK) }, 156 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_QIC, US_PR_BULK) }, 157 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_UFI, US_PR_BULK) }, 158 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_8070, US_PR_BULK) }, 159 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_SCSI, US_PR_BULK) }, 160 161 /* Terminating entry */ 162 { } 163}; 164 165MODULE_DEVICE_TABLE (usb, storage_usb_ids); 166 167/* This is the list of devices we recognize, along with their flag data */ 168 169/* The vendor name should be kept at eight characters or less, and 170 * the product name should be kept at 16 characters or less. If a device 171 * has the US_FL_FIX_INQUIRY flag, then the vendor and product names 172 * normally generated by a device thorugh the INQUIRY response will be 173 * taken from this list, and this is the reason for the above size 174 * restriction. However, if the flag is not present, then you 175 * are free to use as many characters as you like. 176 */ 177 178#undef UNUSUAL_DEV 179#define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \ 180 vendor_name, product_name, use_protocol, use_transport, \ 181 init_function, Flags) \ 182{ \ 183 vendorName: vendor_name, \ 184 productName: product_name, \ 185 useProtocol: use_protocol, \ 186 useTransport: use_transport, \ 187 initFunction : init_function, \ 188 flags: Flags, \ 189} 190 191static struct us_unusual_dev us_unusual_dev_list[] = { 192# include "unusual_devs.h" 193# undef UNUSUAL_DEV 194 /* Control/Bulk transport for all SubClass values */ 195 { useProtocol: US_SC_RBC, 196 useTransport: US_PR_CB}, 197 { useProtocol: US_SC_8020, 198 useTransport: US_PR_CB}, 199 { useProtocol: US_SC_QIC, 200 useTransport: US_PR_CB}, 201 { useProtocol: US_SC_UFI, 202 useTransport: US_PR_CB}, 203 { useProtocol: US_SC_8070, 204 useTransport: US_PR_CB}, 205 { useProtocol: US_SC_SCSI, 206 useTransport: US_PR_CB}, 207 208 /* Control/Bulk/Interrupt transport for all SubClass values */ 209 { useProtocol: US_SC_RBC, 210 useTransport: US_PR_CBI}, 211 { useProtocol: US_SC_8020, 212 useTransport: US_PR_CBI}, 213 { useProtocol: US_SC_QIC, 214 useTransport: US_PR_CBI}, 215 { useProtocol: US_SC_UFI, 216 useTransport: US_PR_CBI}, 217 { useProtocol: US_SC_8070, 218 useTransport: US_PR_CBI}, 219 { useProtocol: US_SC_SCSI, 220 useTransport: US_PR_CBI}, 221 222 /* Bulk-only transport for all SubClass values */ 223 { useProtocol: US_SC_RBC, 224 useTransport: US_PR_BULK}, 225 { useProtocol: US_SC_8020, 226 useTransport: US_PR_BULK}, 227 { useProtocol: US_SC_QIC, 228 useTransport: US_PR_BULK}, 229 { useProtocol: US_SC_UFI, 230 useTransport: US_PR_BULK}, 231 { useProtocol: US_SC_8070, 232 useTransport: US_PR_BULK}, 233 { useProtocol: US_SC_SCSI, 234 useTransport: US_PR_BULK}, 235 236 /* Terminating entry */ 237 { 0 } 238}; 239 240struct usb_driver usb_storage_driver = { 241 name: "usb-storage", 242 probe: storage_probe, 243 disconnect: storage_disconnect, 244 id_table: storage_usb_ids, 245}; 246 247/* 248 * fill_inquiry_response takes an unsigned char array (which must 249 * be at least 36 characters) and populates the vendor name, 250 * product name, and revision fields. Then the array is copied 251 * into the SCSI command's response buffer (oddly enough 252 * called request_buffer). data_len contains the length of the 253 * data array, which again must be at least 36. 254 */ 255 256void fill_inquiry_response(struct us_data *us, unsigned char *data, 257 unsigned int data_len) { 258 259 int i; 260 struct scatterlist *sg; 261 int len = 262 us->srb->request_bufflen > data_len ? data_len : 263 us->srb->request_bufflen; 264 int transferred; 265 int amt; 266 267 if (data_len<36) // You lose. 268 return; 269 270 if(data[0]&0x20) { /* USB device currently not connected. Return 271 peripheral qualifier 001b ("...however, the 272 physical device is not currently connected 273 to this logical unit") and leave vendor and 274 product identification empty. ("If the target 275 does store some of the INQUIRY data on the 276 device, it may return zeros or ASCII spaces 277 (20h) in those fields until the data is 278 available from the device."). */ 279 memset(data+8,0,28); 280 } else { 281 memcpy(data+8, us->unusual_dev->vendorName, 282 strlen(us->unusual_dev->vendorName) > 8 ? 8 : 283 strlen(us->unusual_dev->vendorName)); 284 memcpy(data+16, us->unusual_dev->productName, 285 strlen(us->unusual_dev->productName) > 16 ? 16 : 286 strlen(us->unusual_dev->productName)); 287 data[32] = 0x30 + ((us->pusb_dev->descriptor.bcdDevice>>12) & 0x0F); 288 data[33] = 0x30 + ((us->pusb_dev->descriptor.bcdDevice>>8) & 0x0F); 289 data[34] = 0x30 + ((us->pusb_dev->descriptor.bcdDevice>>4) & 0x0F); 290 data[35] = 0x30 + ((us->pusb_dev->descriptor.bcdDevice) & 0x0F); 291 } 292 293 if (us->srb->use_sg) { 294 sg = (struct scatterlist *)us->srb->request_buffer; 295 for (i=0; i<us->srb->use_sg; i++) 296 memset(sg[i].address, 0, sg[i].length); 297 for (i=0, transferred=0; 298 i<us->srb->use_sg && transferred < len; 299 i++) { 300 amt = sg[i].length > len-transferred ? 301 len-transferred : sg[i].length; 302 memcpy(sg[i].address, data+transferred, amt); 303 transferred -= amt; 304 } 305 } else { 306 memset(us->srb->request_buffer, 0, us->srb->request_bufflen); 307 memcpy(us->srb->request_buffer, data, len); 308 } 309} 310 311static int usb_stor_control_thread(void * __us) 312{ 313 struct us_data *us = (struct us_data *)__us; 314 int action; 315 316 lock_kernel(); 317 318 /* 319 * This thread doesn't need any user-level access, 320 * so get rid of all our resources.. 321 */ 322 exit_files(current); 323 current->files = init_task.files; 324 atomic_inc(¤t->files->count); 325 daemonize(); 326 reparent_to_init(); 327 328 /* avoid getting signals */ 329 spin_lock_irq(¤t->sigmask_lock); 330 flush_signals(current); 331 sigfillset(¤t->blocked); 332 recalc_sigpending(current); 333 spin_unlock_irq(¤t->sigmask_lock); 334 335 /* set our name for identification purposes */ 336 sprintf(current->comm, "usb-storage-%d", us->host_number); 337 338 current->flags |= PF_MEMALLOC; 339 340 unlock_kernel(); 341 342 /* set up for wakeups by new commands */ 343 init_MUTEX_LOCKED(&us->sema); 344 345 /* signal that we've started the thread */ 346 complete(&(us->notify)); 347 set_current_state(TASK_INTERRUPTIBLE); 348 349 for(;;) { 350 unsigned long flags; 351 US_DEBUGP("*** thread sleeping.\n"); 352 if(down_interruptible(&us->sema)) 353 break; 354 355 US_DEBUGP("*** thread awakened.\n"); 356 357 /* lock access to the queue element */ 358 spin_lock_irqsave(&(us->queue_exclusion), flags); 359 360 /* take the command off the queue */ 361 action = us->action; 362 us->action = 0; 363 us->srb = us->queue_srb; 364 365 /* release the queue lock as fast as possible */ 366 spin_unlock_irqrestore(&(us->queue_exclusion), flags); 367 368 switch (action) { 369 case US_ACT_COMMAND: 370 /* reject the command if the direction indicator 371 * is UNKNOWN 372 */ 373 if (us->srb->sc_data_direction == SCSI_DATA_UNKNOWN) { 374 US_DEBUGP("UNKNOWN data direction\n"); 375 us->srb->result = DID_ERROR << 16; 376 set_current_state(TASK_INTERRUPTIBLE); 377 us->srb->scsi_done(us->srb); 378 us->srb = NULL; 379 break; 380 } 381 382 /* reject if target != 0 or if LUN is higher than 383 * the maximum known LUN 384 */ 385 if (us->srb->target && 386 !(us->flags & US_FL_SCM_MULT_TARG)) { 387 US_DEBUGP("Bad target number (%d/%d)\n", 388 us->srb->target, us->srb->lun); 389 us->srb->result = DID_BAD_TARGET << 16; 390 391 set_current_state(TASK_INTERRUPTIBLE); 392 us->srb->scsi_done(us->srb); 393 us->srb = NULL; 394 break; 395 } 396 397 if (us->srb->lun > us->max_lun) { 398 US_DEBUGP("Bad LUN (%d/%d)\n", 399 us->srb->target, us->srb->lun); 400 us->srb->result = DID_BAD_TARGET << 16; 401 402 set_current_state(TASK_INTERRUPTIBLE); 403 us->srb->scsi_done(us->srb); 404 us->srb = NULL; 405 break; 406 } 407 408 /* lock the device pointers */ 409 down(&(us->dev_semaphore)); 410 411 /* our device has gone - pretend not ready */ 412 if (!us->pusb_dev) { 413 US_DEBUGP("Request is for removed device\n"); 414 /* For REQUEST_SENSE, it's the data. But 415 * for anything else, it should look like 416 * we auto-sensed for it. 417 */ 418 if (us->srb->cmnd[0] == REQUEST_SENSE) { 419 memcpy(us->srb->request_buffer, 420 usb_stor_sense_notready, 421 sizeof(usb_stor_sense_notready)); 422 us->srb->result = GOOD << 1; 423 } else if(us->srb->cmnd[0] == INQUIRY) { 424 unsigned char data_ptr[36] = { 425 0x20, 0x80, 0x02, 0x02, 426 0x1F, 0x00, 0x00, 0x00}; 427 US_DEBUGP("Faking INQUIRY command for disconnected device\n"); 428 fill_inquiry_response(us, data_ptr, 36); 429 us->srb->result = GOOD << 1; 430 } else { 431 memcpy(us->srb->sense_buffer, 432 usb_stor_sense_notready, 433 sizeof(usb_stor_sense_notready)); 434 us->srb->result = CHECK_CONDITION << 1; 435 } 436 } else { /* !us->pusb_dev */ 437 438 /* Handle those devices which need us to fake 439 * their inquiry data */ 440 if ((us->srb->cmnd[0] == INQUIRY) && 441 (us->flags & US_FL_FIX_INQUIRY)) { 442 unsigned char data_ptr[36] = { 443 0x00, 0x80, 0x02, 0x02, 444 0x1F, 0x00, 0x00, 0x00}; 445 446 US_DEBUGP("Faking INQUIRY command\n"); 447 fill_inquiry_response(us, data_ptr, 36); 448 us->srb->result = GOOD << 1; 449 } else { 450 /* we've got a command, let's do it! */ 451 US_DEBUG(usb_stor_show_command(us->srb)); 452 us->proto_handler(us->srb, us); 453 } 454 } 455 456 /* unlock the device pointers */ 457 up(&(us->dev_semaphore)); 458 459 /* indicate that the command is done */ 460 if (us->srb->result != DID_ABORT << 16) { 461 US_DEBUGP("scsi cmd done, result=0x%x\n", 462 us->srb->result); 463 set_current_state(TASK_INTERRUPTIBLE); 464 us->srb->scsi_done(us->srb); 465 }; 466 if (atomic_read(&us->abortcnt) != 0) { 467 US_DEBUGP("scsi command aborted\n"); 468 set_current_state(TASK_INTERRUPTIBLE); 469 complete(&(us->notify)); 470 } 471 us->srb = NULL; 472 break; 473 474 case US_ACT_DEVICE_RESET: 475 break; 476 477 case US_ACT_BUS_RESET: 478 break; 479 480 case US_ACT_HOST_RESET: 481 break; 482 483 } /* end switch on action */ 484 485 /* exit if we get a signal to exit */ 486 if (action == US_ACT_EXIT) { 487 US_DEBUGP("-- US_ACT_EXIT command received\n"); 488 break; 489 } 490 } /* for (;;) */ 491 492 /* clean up after ourselves */ 493 set_current_state(TASK_INTERRUPTIBLE); 494 495 /* notify the exit routine that we're actually exiting now */ 496 complete(&(us->notify)); 497 498 return 0; 499} 500 501/* Set up the IRQ pipe and handler 502 * Note that this function assumes that all the data in the us_data 503 * strucuture is current. This includes the ep_int field, which gives us 504 * the endpoint for the interrupt. 505 * Returns non-zero on failure, zero on success 506 */ 507static int usb_stor_allocate_irq(struct us_data *ss) 508{ 509 unsigned int pipe; 510 int maxp; 511 int result; 512 513 US_DEBUGP("Allocating IRQ for CBI transport\n"); 514 515 /* lock access to the data structure */ 516 down(&(ss->irq_urb_sem)); 517 518 /* allocate the URB */ 519 ss->irq_urb = usb_alloc_urb(0); 520 if (!ss->irq_urb) { 521 up(&(ss->irq_urb_sem)); 522 US_DEBUGP("couldn't allocate interrupt URB"); 523 return 1; 524 } 525 526 /* calculate the pipe and max packet size */ 527 pipe = usb_rcvintpipe(ss->pusb_dev, ss->ep_int->bEndpointAddress & 528 USB_ENDPOINT_NUMBER_MASK); 529 maxp = usb_maxpacket(ss->pusb_dev, pipe, usb_pipeout(pipe)); 530 if (maxp > sizeof(ss->irqbuf)) 531 maxp = sizeof(ss->irqbuf); 532 533 /* fill in the URB with our data */ 534 FILL_INT_URB(ss->irq_urb, ss->pusb_dev, pipe, ss->irqbuf, maxp, 535 usb_stor_CBI_irq, ss, ss->ep_int->bInterval); 536 537 /* submit the URB for processing */ 538 result = usb_submit_urb(ss->irq_urb); 539 US_DEBUGP("usb_submit_urb() returns %d\n", result); 540 if (result) { 541 usb_free_urb(ss->irq_urb); 542 up(&(ss->irq_urb_sem)); 543 return 2; 544 } 545 546 /* unlock the data structure and return success */ 547 up(&(ss->irq_urb_sem)); 548 return 0; 549} 550 551/* Probe to see if a new device is actually a SCSI device */ 552static void * storage_probe(struct usb_device *dev, unsigned int ifnum, 553 const struct usb_device_id *id) 554{ 555 int i; 556 const int id_index = id - storage_usb_ids; 557 char mf[USB_STOR_STRING_LEN]; /* manufacturer */ 558 char prod[USB_STOR_STRING_LEN]; /* product */ 559 char serial[USB_STOR_STRING_LEN]; /* serial number */ 560 GUID(guid); /* Global Unique Identifier */ 561 unsigned int flags; 562 struct us_unusual_dev *unusual_dev; 563 struct us_data *ss = NULL; 564#ifdef CONFIG_USB_STORAGE_SDDR09 565 int result; 566#endif 567 568 /* these are temporary copies -- we test on these, then put them 569 * in the us-data structure 570 */ 571 struct usb_endpoint_descriptor *ep_in = NULL; 572 struct usb_endpoint_descriptor *ep_out = NULL; 573 struct usb_endpoint_descriptor *ep_int = NULL; 574 u8 subclass = 0; 575 u8 protocol = 0; 576 577 /* the altsettting on the interface we're probing that matched our 578 * usb_match_id table 579 */ 580 struct usb_interface *intf = dev->actconfig->interface; 581 struct usb_interface_descriptor *altsetting = 582 intf[ifnum].altsetting + intf[ifnum].act_altsetting; 583 US_DEBUGP("act_altsettting is %d\n", intf[ifnum].act_altsetting); 584 585 if (dev->actconfig->bNumInterfaces > 1) // patch for U2EC 586 { 587 printk("We only support single-interface storage device!\n"); 588 return NULL; 589 } 590 591 /* clear the temporary strings */ 592 memset(mf, 0, sizeof(mf)); 593 memset(prod, 0, sizeof(prod)); 594 memset(serial, 0, sizeof(serial)); 595 596 /* 597 * Can we support this device, either because we know about it 598 * from our unusual device list, or because it advertises that it's 599 * compliant to the specification? 600 * 601 * id_index is calculated in the declaration to be the index number 602 * of the match from the usb_device_id table, so we can find the 603 * corresponding entry in the private table. 604 */ 605 US_DEBUGP("id_index calculated to be: %d\n", id_index); 606 US_DEBUGP("Array length appears to be: %d\n", sizeof(us_unusual_dev_list) / sizeof(us_unusual_dev_list[0])); 607 if (id_index < 608 sizeof(us_unusual_dev_list) / sizeof(us_unusual_dev_list[0])) { 609 unusual_dev = &us_unusual_dev_list[id_index]; 610 if (unusual_dev->vendorName) 611 US_DEBUGP("Vendor: %s\n", unusual_dev->vendorName); 612 if (unusual_dev->productName) 613 US_DEBUGP("Product: %s\n", unusual_dev->productName); 614 } else 615 /* no, we can't support it */ 616 return NULL; 617 618 /* At this point, we know we've got a live one */ 619 US_DEBUGP("USB Mass Storage device detected\n"); 620 621 /* Determine subclass and protocol, or copy from the interface */ 622 subclass = (unusual_dev->useProtocol == US_SC_DEVICE) ? 623 altsetting->bInterfaceSubClass : 624 unusual_dev->useProtocol; 625 protocol = (unusual_dev->useTransport == US_PR_DEVICE) ? 626 altsetting->bInterfaceProtocol : 627 unusual_dev->useTransport; 628 flags = unusual_dev->flags; 629 630 if (flags & US_FL_IGNORE_DEVICE) { 631 printk(KERN_WARNING USB_STORAGE "device ignored\n"); 632 return NULL; 633 } 634 635 /* 636 * Find the endpoints we need 637 * We are expecting a minimum of 2 endpoints - in and out (bulk). 638 * An optional interrupt is OK (necessary for CBI protocol). 639 * We will ignore any others. 640 */ 641 for (i = 0; i < altsetting->bNumEndpoints; i++) { 642 /* is it an BULK endpoint? */ 643 if ((altsetting->endpoint[i].bmAttributes & 644 USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_BULK) { 645 /* BULK in or out? */ 646 if (altsetting->endpoint[i].bEndpointAddress & 647 USB_DIR_IN) 648 ep_in = &altsetting->endpoint[i]; 649 else 650 ep_out = &altsetting->endpoint[i]; 651 } 652 653 /* is it an interrupt endpoint? */ 654 if ((altsetting->endpoint[i].bmAttributes & 655 USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT) { 656 ep_int = &altsetting->endpoint[i]; 657 } 658 } 659 US_DEBUGP("Endpoints: In: 0x%p Out: 0x%p Int: 0x%p (Period %d)\n", 660 ep_in, ep_out, ep_int, ep_int ? ep_int->bInterval : 0); 661 662#ifdef CONFIG_USB_STORAGE_SDDR09 663 if (protocol == US_PR_EUSB_SDDR09 || protocol == US_PR_DPCM_USB) { 664 /* set the configuration -- STALL is an acceptable response here */ 665 result = usb_set_configuration(dev, 1); 666 667 US_DEBUGP("Result from usb_set_configuration is %d\n", result); 668 if (result == -EPIPE) { 669 US_DEBUGP("-- clearing stall on control interface\n"); 670 usb_clear_halt(dev, usb_sndctrlpipe(dev, 0)); 671 } else if (result != 0) { 672 /* it's not a stall, but another error -- time to bail */ 673 US_DEBUGP("-- Unknown error. Rejecting device\n"); 674 return NULL; 675 } 676 } 677#endif 678 679 /* Do some basic sanity checks, and bail if we find a problem */ 680 if (!ep_in || !ep_out || (protocol == US_PR_CBI && !ep_int)) { 681 US_DEBUGP("Endpoint sanity check failed! Rejecting dev.\n"); 682 return NULL; 683 } 684 685 /* At this point, we're committed to using the device */ 686 usb_inc_dev_use(dev); 687 688 /* clear the GUID and fetch the strings */ 689 GUID_CLEAR(guid); 690 if (dev->descriptor.iManufacturer) 691 usb_string(dev, dev->descriptor.iManufacturer, 692 mf, sizeof(mf)); 693 if (dev->descriptor.iProduct) 694 usb_string(dev, dev->descriptor.iProduct, 695 prod, sizeof(prod)); 696 if (dev->descriptor.iSerialNumber && !(flags & US_FL_IGNORE_SER)) 697 usb_string(dev, dev->descriptor.iSerialNumber, 698 serial, sizeof(serial)); 699 700 /* Create a GUID for this device */ 701 if (dev->descriptor.iSerialNumber && serial[0]) { 702 /* If we have a serial number, and it's a non-NULL string */ 703 make_guid(guid, dev->descriptor.idVendor, 704 dev->descriptor.idProduct, serial); 705 } else { 706 /* We don't have a serial number, so we use 0 */ 707 make_guid(guid, dev->descriptor.idVendor, 708 dev->descriptor.idProduct, "0"); 709 } 710 711 /* 712 * Now check if we have seen this GUID before 713 * We're looking for a device with a matching GUID that isn't 714 * already on the system 715 */ 716 ss = us_list; 717 while ((ss != NULL) && 718 ((ss->pusb_dev) || !GUID_EQUAL(guid, ss->guid))) 719 ss = ss->next; 720 721 if (ss != NULL) { 722 /* Existing device -- re-connect */ 723 US_DEBUGP("Found existing GUID " GUID_FORMAT "\n", 724 GUID_ARGS(guid)); 725 726 /* lock the device pointers */ 727 down(&(ss->dev_semaphore)); 728 729 /* establish the connection to the new device upon reconnect */ 730 ss->ifnum = ifnum; 731 ss->pusb_dev = dev; 732 733 /* copy over the endpoint data */ 734 if (ep_in) 735 ss->ep_in = ep_in->bEndpointAddress & 736 USB_ENDPOINT_NUMBER_MASK; 737 if (ep_out) 738 ss->ep_out = ep_out->bEndpointAddress & 739 USB_ENDPOINT_NUMBER_MASK; 740 ss->ep_int = ep_int; 741 742 /* allocate an IRQ callback if one is needed */ 743 if ((ss->protocol == US_PR_CBI) && usb_stor_allocate_irq(ss)) { 744 up(&(ss->dev_semaphore)); 745 usb_dec_dev_use(dev); 746 return NULL; 747 } 748 749 /* allocate the URB we're going to use */ 750 ss->current_urb = usb_alloc_urb(0); 751 if (!ss->current_urb) { 752 up(&(ss->dev_semaphore)); 753 usb_dec_dev_use(dev); 754 return NULL; 755 } 756 757 /* Re-Initialize the device if it needs it */ 758 if (unusual_dev && unusual_dev->initFunction) 759 (unusual_dev->initFunction)(ss); 760 761 /* Initialize the host template based on the default one */ 762 memcpy(&(ss->htmplt), &usb_stor_host_template, 763 sizeof(usb_stor_host_template)); 764 765 /* We abuse this pointer so we can pass the ss pointer to 766 * the host controller thread in us_detect. But how else are 767 * we to do it? 768 */ 769 (struct us_data *)ss->htmplt.proc_dir = ss; 770 771 /* start up our control thread */ 772 ss->pid = kernel_thread(usb_stor_control_thread, ss, 773 CLONE_VM); 774 if (ss->pid < 0) { 775 printk(KERN_WARNING USB_STORAGE 776 "Unable to start control thread\n"); 777 kfree(ss->current_urb); 778 ss->current_urb = NULL; 779 usb_dec_dev_use(dev); 780 return NULL; 781 } 782 783 /* wait for the thread to start */ 784 wait_for_completion(&(ss->notify)); 785 786 /* now register - our detect function will be called */ 787 ss->htmplt.module = THIS_MODULE; 788 789 /* unlock the device pointers */ 790 up(&(ss->dev_semaphore)); 791 792 scsi_register_module(MODULE_SCSI_HA, &(ss->htmplt)); 793 794 } else { 795 /* New device -- allocate memory and initialize */ 796 US_DEBUGP("New GUID " GUID_FORMAT "\n", GUID_ARGS(guid)); 797 798 if ((ss = (struct us_data *)kmalloc(sizeof(struct us_data), 799 GFP_KERNEL)) == NULL) { 800 printk(KERN_WARNING USB_STORAGE "Out of memory\n"); 801 usb_dec_dev_use(dev); 802 return NULL; 803 } 804 memset(ss, 0, sizeof(struct us_data)); 805 806 /* allocate the URB we're going to use */ 807 ss->current_urb = usb_alloc_urb(0); 808 if (!ss->current_urb) { 809 kfree(ss); 810 usb_dec_dev_use(dev); 811 return NULL; 812 } 813 814 /* Initialize the mutexes only when the struct is new */ 815 init_completion(&(ss->notify)); 816 init_MUTEX_LOCKED(&(ss->ip_waitq)); 817 spin_lock_init(&(ss->queue_exclusion)); 818 init_MUTEX(&(ss->irq_urb_sem)); 819 init_MUTEX(&(ss->current_urb_sem)); 820 init_MUTEX(&(ss->dev_semaphore)); 821 822 /* copy over the subclass and protocol data */ 823 ss->subclass = subclass; 824 ss->protocol = protocol; 825 ss->flags = flags; 826 ss->unusual_dev = unusual_dev; 827 828 /* copy over the endpoint data */ 829 if (ep_in) 830 ss->ep_in = ep_in->bEndpointAddress & 831 USB_ENDPOINT_NUMBER_MASK; 832 if (ep_out) 833 ss->ep_out = ep_out->bEndpointAddress & 834 USB_ENDPOINT_NUMBER_MASK; 835 ss->ep_int = ep_int; 836 837 /* establish the connection to the new device */ 838 ss->ifnum = ifnum; 839 ss->pusb_dev = dev; 840 841 /* copy over the identifiying strings */ 842 strncpy(ss->vendor, mf, USB_STOR_STRING_LEN); 843 strncpy(ss->product, prod, USB_STOR_STRING_LEN); 844 strncpy(ss->serial, serial, USB_STOR_STRING_LEN); 845 if (strlen(ss->vendor) == 0) { 846 if (unusual_dev->vendorName) 847 strncpy(ss->vendor, unusual_dev->vendorName, 848 USB_STOR_STRING_LEN); 849 else 850 strncpy(ss->vendor, "Unknown", 851 USB_STOR_STRING_LEN); 852 } 853 if (strlen(ss->product) == 0) { 854 if (unusual_dev->productName) 855 strncpy(ss->product, unusual_dev->productName, 856 USB_STOR_STRING_LEN); 857 else 858 strncpy(ss->product, "Unknown", 859 USB_STOR_STRING_LEN); 860 } 861 if (strlen(ss->serial) == 0) 862 strncpy(ss->serial, "None", USB_STOR_STRING_LEN); 863 864 /* copy the GUID we created before */ 865 memcpy(ss->guid, guid, sizeof(guid)); 866 867 /* 868 * Set the handler pointers based on the protocol 869 * Again, this data is persistant across reattachments 870 */ 871 switch (ss->protocol) { 872 case US_PR_CB: 873 ss->transport_name = "Control/Bulk"; 874 ss->transport = usb_stor_CB_transport; 875 ss->transport_reset = usb_stor_CB_reset; 876 ss->max_lun = 7; 877 break; 878 879 case US_PR_CBI: 880 ss->transport_name = "Control/Bulk/Interrupt"; 881 ss->transport = usb_stor_CBI_transport; 882 ss->transport_reset = usb_stor_CB_reset; 883 ss->max_lun = 7; 884 break; 885 886 case US_PR_BULK: 887 ss->transport_name = "Bulk"; 888 ss->transport = usb_stor_Bulk_transport; 889 ss->transport_reset = usb_stor_Bulk_reset; 890 ss->max_lun = usb_stor_Bulk_max_lun(ss); 891 break; 892 893#ifdef CONFIG_USB_STORAGE_HP8200e 894 case US_PR_SCM_ATAPI: 895 ss->transport_name = "SCM/ATAPI"; 896 ss->transport = hp8200e_transport; 897 ss->transport_reset = usb_stor_CB_reset; 898 ss->max_lun = 1; 899 break; 900#endif 901 902#ifdef CONFIG_USB_STORAGE_SDDR09 903 case US_PR_EUSB_SDDR09: 904 ss->transport_name = "EUSB/SDDR09"; 905 ss->transport = sddr09_transport; 906 ss->transport_reset = usb_stor_CB_reset; 907 ss->max_lun = 0; 908 break; 909#endif 910 911#ifdef CONFIG_USB_STORAGE_SDDR55 912 case US_PR_SDDR55: 913 ss->transport_name = "SDDR55"; 914 ss->transport = sddr55_transport; 915 ss->transport_reset = sddr55_reset; 916 ss->max_lun = 0; 917 break; 918#endif 919 920#ifdef CONFIG_USB_STORAGE_DPCM 921 case US_PR_DPCM_USB: 922 ss->transport_name = "Control/Bulk-EUSB/SDDR09"; 923 ss->transport = dpcm_transport; 924 ss->transport_reset = usb_stor_CB_reset; 925 ss->max_lun = 1; 926 break; 927#endif 928 929#ifdef CONFIG_USB_STORAGE_FREECOM 930 case US_PR_FREECOM: 931 ss->transport_name = "Freecom"; 932 ss->transport = freecom_transport; 933 ss->transport_reset = usb_stor_freecom_reset; 934 ss->max_lun = 0; 935 break; 936#endif 937 938#ifdef CONFIG_USB_STORAGE_DATAFAB 939 case US_PR_DATAFAB: 940 ss->transport_name = "Datafab Bulk-Only"; 941 ss->transport = datafab_transport; 942 ss->transport_reset = usb_stor_Bulk_reset; 943 ss->max_lun = 1; 944 break; 945#endif 946 947#ifdef CONFIG_USB_STORAGE_JUMPSHOT 948 case US_PR_JUMPSHOT: 949 ss->transport_name = "Lexar Jumpshot Control/Bulk"; 950 ss->transport = jumpshot_transport; 951 ss->transport_reset = usb_stor_Bulk_reset; 952 ss->max_lun = 1; 953 break; 954#endif 955 956 default: 957 ss->transport_name = "Unknown"; 958 kfree(ss->current_urb); 959 kfree(ss); 960 usb_dec_dev_use(dev); 961 return NULL; 962 break; 963 } 964 US_DEBUGP("Transport: %s\n", ss->transport_name); 965 966 /* fix for single-lun devices */ 967 if (ss->flags & US_FL_SINGLE_LUN) 968 ss->max_lun = 0; 969 970 switch (ss->subclass) { 971 case US_SC_RBC: 972 ss->protocol_name = "Reduced Block Commands (RBC)"; 973 ss->proto_handler = usb_stor_transparent_scsi_command; 974 break; 975 976 case US_SC_8020: 977 ss->protocol_name = "8020i"; 978 ss->proto_handler = usb_stor_ATAPI_command; 979 ss->max_lun = 0; 980 break; 981 982 case US_SC_QIC: 983 ss->protocol_name = "QIC-157"; 984 ss->proto_handler = usb_stor_qic157_command; 985 ss->max_lun = 0; 986 break; 987 988 case US_SC_8070: 989 ss->protocol_name = "8070i"; 990 ss->proto_handler = usb_stor_ATAPI_command; 991 ss->max_lun = 0; 992 break; 993 994 case US_SC_SCSI: 995 ss->protocol_name = "Transparent SCSI"; 996 ss->proto_handler = usb_stor_transparent_scsi_command; 997 break; 998 999 case US_SC_UFI: 1000 ss->protocol_name = "Uniform Floppy Interface (UFI)"; 1001 ss->proto_handler = usb_stor_ufi_command; 1002 break; 1003 1004#ifdef CONFIG_USB_STORAGE_ISD200 1005 case US_SC_ISD200: 1006 ss->protocol_name = "ISD200 ATA/ATAPI"; 1007 ss->proto_handler = isd200_ata_command; 1008 break; 1009#endif 1010 1011 default: 1012 ss->protocol_name = "Unknown"; 1013 kfree(ss->current_urb); 1014 kfree(ss); 1015 usb_dec_dev_use(dev); 1016 return NULL; 1017 break; 1018 } 1019 US_DEBUGP("Protocol: %s\n", ss->protocol_name); 1020 1021 /* allocate an IRQ callback if one is needed */ 1022 if ((ss->protocol == US_PR_CBI) && usb_stor_allocate_irq(ss)) { 1023 kfree(ss->current_urb); 1024 kfree(ss); 1025 usb_dec_dev_use(dev); 1026 return NULL; 1027 } 1028 1029 /* 1030 * Since this is a new device, we need to generate a scsi 1031 * host definition, and register with the higher SCSI layers 1032 */ 1033 1034 /* Initialize the host template based on the default one */ 1035 memcpy(&(ss->htmplt), &usb_stor_host_template, 1036 sizeof(usb_stor_host_template)); 1037 1038 /* Grab the next host number */ 1039 ss->host_number = my_host_number++; 1040 1041 /* We abuse this pointer so we can pass the ss pointer to 1042 * the host controller thread in us_detect. But how else are 1043 * we to do it? 1044 */ 1045 (struct us_data *)ss->htmplt.proc_dir = ss; 1046 1047 /* Just before we start our control thread, initialize 1048 * the device if it needs initialization */ 1049 if (unusual_dev && unusual_dev->initFunction) 1050 unusual_dev->initFunction(ss); 1051 1052 /* start up our control thread */ 1053 ss->pid = kernel_thread(usb_stor_control_thread, ss, 1054 CLONE_VM); 1055 if (ss->pid < 0) { 1056 printk(KERN_WARNING USB_STORAGE 1057 "Unable to start control thread\n"); 1058 kfree(ss->current_urb); 1059 kfree(ss); 1060 usb_dec_dev_use(dev); 1061 return NULL; 1062 } 1063 1064 /* wait for the thread to start */ 1065 wait_for_completion(&(ss->notify)); 1066 1067 /* now register - our detect function will be called */ 1068 ss->htmplt.module = THIS_MODULE; 1069 scsi_register_module(MODULE_SCSI_HA, &(ss->htmplt)); 1070 1071 /* lock access to the data structures */ 1072 down(&us_list_semaphore); 1073 1074 /* put us in the list */ 1075 ss->next = us_list; 1076 us_list = ss; 1077 1078 /* release the data structure lock */ 1079 up(&us_list_semaphore); 1080 } 1081 1082 printk(KERN_DEBUG 1083 "WARNING: USB Mass Storage data integrity not assured\n"); 1084 printk(KERN_DEBUG 1085 "USB Mass Storage device found at %d\n", dev->devnum); 1086 1087 /* return a pointer for the disconnect function */ 1088 return ss; 1089} 1090 1091/* Handle a disconnect event from the USB core */ 1092static void storage_disconnect(struct usb_device *dev, void *ptr) 1093{ 1094 struct us_data *ss = ptr; 1095 int result; 1096 1097 US_DEBUGP("storage_disconnect() called\n"); 1098 1099 /* this is the odd case -- we disconnected but weren't using it */ 1100 if (!ss) { 1101 US_DEBUGP("-- device was not in use\n"); 1102 return; 1103 } 1104 1105 /* lock access to the device data structure */ 1106 down(&(ss->dev_semaphore)); 1107 1108 /* release the IRQ, if we have one */ 1109 down(&(ss->irq_urb_sem)); 1110 if (ss->irq_urb) { 1111 US_DEBUGP("-- releasing irq URB\n"); 1112 result = usb_unlink_urb(ss->irq_urb); 1113 US_DEBUGP("-- usb_unlink_urb() returned %d\n", result); 1114 usb_free_urb(ss->irq_urb); 1115 ss->irq_urb = NULL; 1116 } 1117 up(&(ss->irq_urb_sem)); 1118 1119 /* free up the main URB for this device */ 1120 US_DEBUGP("-- releasing main URB\n"); 1121 result = usb_unlink_urb(ss->current_urb); 1122 US_DEBUGP("-- usb_unlink_urb() returned %d\n", result); 1123 usb_free_urb(ss->current_urb); 1124 ss->current_urb = NULL; 1125 1126 /* mark the device as gone */ 1127 usb_dec_dev_use(ss->pusb_dev); 1128 ss->pusb_dev = NULL; 1129 1130 scsi_unregister_module(MODULE_SCSI_HA, &(ss->htmplt)); 1131 1132 /* unlock access to the device data structure */ 1133 up(&(ss->dev_semaphore)); 1134} 1135 1136/*********************************************************************** 1137 * Initialization and registration 1138 ***********************************************************************/ 1139 1140int __init usb_stor_init(void) 1141{ 1142 printk(KERN_INFO "Initializing USB Mass Storage driver...\n"); 1143 1144 /* initialize internal global data elements */ 1145 us_list = NULL; 1146 init_MUTEX(&us_list_semaphore); 1147 my_host_number = 0; 1148 1149 /* register the driver, return -1 if error */ 1150 if (usb_register(&usb_storage_driver) < 0) 1151 return -1; 1152 1153 /* we're all set */ 1154 printk(KERN_INFO "USB Mass Storage support registered.\n"); 1155 return 0; 1156} 1157 1158void __exit usb_stor_exit(void) 1159{ 1160 struct us_data *next; 1161 1162 US_DEBUGP("usb_stor_exit() called\n"); 1163 1164 /* Deregister the driver 1165 * This eliminates races with probes and disconnects 1166 */ 1167 US_DEBUGP("-- calling usb_deregister()\n"); 1168 usb_deregister(&usb_storage_driver) ; 1169 1170 /* While there are still virtual hosts, unregister them 1171 * Note that it's important to do this completely before removing 1172 * the structures because of possible races with the /proc 1173 * interface 1174 */ 1175 for (next = us_list; next; next = next->next) { 1176 US_DEBUGP("-- calling scsi_unregister_module()\n"); 1177 scsi_unregister_module(MODULE_SCSI_HA, &(next->htmplt)); 1178 } 1179 1180 /* While there are still structures, free them. Note that we are 1181 * now race-free, since these structures can no longer be accessed 1182 * from either the SCSI command layer or the /proc interface 1183 */ 1184 while (us_list) { 1185 /* keep track of where the next one is */ 1186 next = us_list->next; 1187 1188 /* If there's extra data in the us_data structure then 1189 * free that first */ 1190 if (us_list->extra) { 1191 /* call the destructor routine, if it exists */ 1192 if (us_list->extra_destructor) { 1193 US_DEBUGP("-- calling extra_destructor()\n"); 1194 us_list->extra_destructor(us_list->extra); 1195 } 1196 1197 /* destroy the extra data */ 1198 US_DEBUGP("-- freeing the data structure\n"); 1199 kfree(us_list->extra); 1200 } 1201 1202 /* free the structure itself */ 1203 kfree (us_list); 1204 1205 /* advance the list pointer */ 1206 us_list = next; 1207 } 1208} 1209 1210module_init(usb_stor_init); 1211module_exit(usb_stor_exit); 1212