1 /* 2 * USB Attached SCSI 3 * Note that this is not the same as the USB Mass Storage driver 4 * 5 * Copyright Matthew Wilcox for Intel Corp, 2010 6 * Copyright Sarah Sharp for Intel Corp, 2010 7 * 8 * Distributed under the terms of the GNU GPL, version two. 9 */ 10 11 #include <linux/blkdev.h> 12 #include <linux/slab.h> 13 #include <linux/types.h> 14 #include <linux/module.h> 15 #include <linux/usb.h> 16 #include <linux/usb/storage.h> 17 18 #include <scsi/scsi.h> 19 #include <scsi/scsi_dbg.h> 20 #include <scsi/scsi_cmnd.h> 21 #include <scsi/scsi_device.h> 22 #include <scsi/scsi_host.h> 23 #include <scsi/scsi_tcq.h> 24 25 /* Common header for all IUs */ 26 struct iu { 27 __u8 iu_id; 28 __u8 rsvd1; 29 __be16 tag; 30 }; 31 32 enum { 33 IU_ID_COMMAND = 0x01, 34 IU_ID_STATUS = 0x03, 35 IU_ID_RESPONSE = 0x04, 36 IU_ID_TASK_MGMT = 0x05, 37 IU_ID_READ_READY = 0x06, 38 IU_ID_WRITE_READY = 0x07, 39 }; 40 41 struct command_iu { 42 __u8 iu_id; 43 __u8 rsvd1; 44 __be16 tag; 45 __u8 prio_attr; 46 __u8 rsvd5; 47 __u8 len; 48 __u8 rsvd7; 49 struct scsi_lun lun; 50 __u8 cdb[16]; /* XXX: Overflow-checking tools may misunderstand */ 51 }; 52 53 /* 54 * Also used for the Read Ready and Write Ready IUs since they have the 55 * same first four bytes 56 */ 57 struct sense_iu { 58 __u8 iu_id; 59 __u8 rsvd1; 60 __be16 tag; 61 __be16 status_qual; 62 __u8 status; 63 __u8 rsvd7[7]; 64 __be16 len; 65 __u8 sense[SCSI_SENSE_BUFFERSIZE]; 66 }; 67 68 /* 69 * The r00-r01c specs define this version of the SENSE IU data structure. 70 * It's still in use by several different firmware releases. 71 */ 72 struct sense_iu_old { 73 __u8 iu_id; 74 __u8 rsvd1; 75 __be16 tag; 76 __be16 len; 77 __u8 status; 78 __u8 service_response; 79 __u8 sense[SCSI_SENSE_BUFFERSIZE]; 80 }; 81 82 enum { 83 CMD_PIPE_ID = 1, 84 STATUS_PIPE_ID = 2, 85 DATA_IN_PIPE_ID = 3, 86 DATA_OUT_PIPE_ID = 4, 87 88 UAS_SIMPLE_TAG = 0, 89 UAS_HEAD_TAG = 1, 90 UAS_ORDERED_TAG = 2, 91 UAS_ACA = 4, 92 }; 93 94 struct uas_dev_info { 95 struct usb_interface *intf; 96 struct usb_device *udev; 97 int qdepth; 98 unsigned cmd_pipe, status_pipe, data_in_pipe, data_out_pipe; 99 unsigned use_streams:1; 100 unsigned uas_sense_old:1; 101 }; 102 103 enum { 104 ALLOC_STATUS_URB = (1 << 0), 105 SUBMIT_STATUS_URB = (1 << 1), 106 ALLOC_DATA_IN_URB = (1 << 2), 107 SUBMIT_DATA_IN_URB = (1 << 3), 108 ALLOC_DATA_OUT_URB = (1 << 4), 109 SUBMIT_DATA_OUT_URB = (1 << 5), 110 ALLOC_CMD_URB = (1 << 6), 111 SUBMIT_CMD_URB = (1 << 7), 112 }; 113 114 /* Overrides scsi_pointer */ 115 struct uas_cmd_info { 116 unsigned int state; 117 unsigned int stream; 118 struct urb *cmd_urb; 119 struct urb *status_urb; 120 struct urb *data_in_urb; 121 struct urb *data_out_urb; 122 struct list_head list; 123 }; 124 125 /* I hate forward declarations, but I actually have a loop */ 126 static int uas_submit_urbs(struct scsi_cmnd *cmnd, 127 struct uas_dev_info *devinfo, gfp_t gfp); 128 129 static DEFINE_SPINLOCK(uas_work_lock); 130 static LIST_HEAD(uas_work_list); 131 132 static void uas_do_work(struct work_struct *work) 133 { 134 struct uas_cmd_info *cmdinfo; 135 struct list_head list; 136 137 spin_lock_irq(&uas_work_lock); 138 list_replace_init(&uas_work_list, &list); 139 spin_unlock_irq(&uas_work_lock); 140 141 list_for_each_entry(cmdinfo, &list, list) { 142 struct scsi_pointer *scp = (void *)cmdinfo; 143 struct scsi_cmnd *cmnd = container_of(scp, 144 struct scsi_cmnd, SCp); 145 uas_submit_urbs(cmnd, cmnd->device->hostdata, GFP_NOIO); 146 } 147 } 148 149 static DECLARE_WORK(uas_work, uas_do_work); 150 151 static void uas_sense(struct urb *urb, struct scsi_cmnd *cmnd) 152 { 153 struct sense_iu *sense_iu = urb->transfer_buffer; 154 struct scsi_device *sdev = cmnd->device; 155 156 if (urb->actual_length > 16) { 157 unsigned len = be16_to_cpup(&sense_iu->len); 158 if (len + 16 != urb->actual_length) { 159 int newlen = min(len + 16, urb->actual_length) - 16; 160 if (newlen < 0) 161 newlen = 0; 162 sdev_printk(KERN_INFO, sdev, "%s: urb length %d " 163 "disagrees with IU sense data length %d, " 164 "using %d bytes of sense data\n", __func__, 165 urb->actual_length, len, newlen); 166 len = newlen; 167 } 168 memcpy(cmnd->sense_buffer, sense_iu->sense, len); 169 } 170 171 cmnd->result = sense_iu->status; 172 if (sdev->current_cmnd) 173 sdev->current_cmnd = NULL; 174 cmnd->scsi_done(cmnd); 175 usb_free_urb(urb); 176 } 177 178 static void uas_sense_old(struct urb *urb, struct scsi_cmnd *cmnd) 179 { 180 struct sense_iu_old *sense_iu = urb->transfer_buffer; 181 struct scsi_device *sdev = cmnd->device; 182 183 if (urb->actual_length > 8) { 184 unsigned len = be16_to_cpup(&sense_iu->len) - 2; 185 if (len + 8 != urb->actual_length) { 186 int newlen = min(len + 8, urb->actual_length) - 8; 187 if (newlen < 0) 188 newlen = 0; 189 sdev_printk(KERN_INFO, sdev, "%s: urb length %d " 190 "disagrees with IU sense data length %d, " 191 "using %d bytes of sense data\n", __func__, 192 urb->actual_length, len, newlen); 193 len = newlen; 194 } 195 memcpy(cmnd->sense_buffer, sense_iu->sense, len); 196 } 197 198 cmnd->result = sense_iu->status; 199 if (sdev->current_cmnd) 200 sdev->current_cmnd = NULL; 201 cmnd->scsi_done(cmnd); 202 usb_free_urb(urb); 203 } 204 205 static void uas_xfer_data(struct urb *urb, struct scsi_cmnd *cmnd, 206 unsigned direction) 207 { 208 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp; 209 int err; 210 211 cmdinfo->state = direction | SUBMIT_STATUS_URB; 212 err = uas_submit_urbs(cmnd, cmnd->device->hostdata, GFP_ATOMIC); 213 if (err) { 214 spin_lock(&uas_work_lock); 215 list_add_tail(&cmdinfo->list, &uas_work_list); 216 spin_unlock(&uas_work_lock); 217 schedule_work(&uas_work); 218 } 219 } 220 221 static void uas_stat_cmplt(struct urb *urb) 222 { 223 struct iu *iu = urb->transfer_buffer; 224 struct scsi_device *sdev = urb->context; 225 struct uas_dev_info *devinfo = sdev->hostdata; 226 struct scsi_cmnd *cmnd; 227 u16 tag; 228 229 if (urb->status) { 230 dev_err(&urb->dev->dev, "URB BAD STATUS %d\n", urb->status); 231 usb_free_urb(urb); 232 return; 233 } 234 235 tag = be16_to_cpup(&iu->tag) - 1; 236 if (sdev->current_cmnd) 237 cmnd = sdev->current_cmnd; 238 else 239 cmnd = scsi_find_tag(sdev, tag); 240 if (!cmnd) 241 return; 242 243 switch (iu->iu_id) { 244 case IU_ID_STATUS: 245 if (urb->actual_length < 16) 246 devinfo->uas_sense_old = 1; 247 if (devinfo->uas_sense_old) 248 uas_sense_old(urb, cmnd); 249 else 250 uas_sense(urb, cmnd); 251 break; 252 case IU_ID_READ_READY: 253 uas_xfer_data(urb, cmnd, SUBMIT_DATA_IN_URB); 254 break; 255 case IU_ID_WRITE_READY: 256 uas_xfer_data(urb, cmnd, SUBMIT_DATA_OUT_URB); 257 break; 258 default: 259 scmd_printk(KERN_ERR, cmnd, 260 "Bogus IU (%d) received on status pipe\n", iu->iu_id); 261 } 262 } 263 264 static void uas_data_cmplt(struct urb *urb) 265 { 266 struct scsi_data_buffer *sdb = urb->context; 267 sdb->resid = sdb->length - urb->actual_length; 268 usb_free_urb(urb); 269 } 270 271 static struct urb *uas_alloc_data_urb(struct uas_dev_info *devinfo, gfp_t gfp, 272 unsigned int pipe, u16 stream_id, 273 struct scsi_data_buffer *sdb, 274 enum dma_data_direction dir) 275 { 276 struct usb_device *udev = devinfo->udev; 277 struct urb *urb = usb_alloc_urb(0, gfp); 278 279 if (!urb) 280 goto out; 281 usb_fill_bulk_urb(urb, udev, pipe, NULL, sdb->length, uas_data_cmplt, 282 sdb); 283 if (devinfo->use_streams) 284 urb->stream_id = stream_id; 285 urb->num_sgs = udev->bus->sg_tablesize ? sdb->table.nents : 0; 286 urb->sg = sdb->table.sgl; 287 out: 288 return urb; 289 } 290 291 static struct urb *uas_alloc_sense_urb(struct uas_dev_info *devinfo, gfp_t gfp, 292 struct scsi_cmnd *cmnd, u16 stream_id) 293 { 294 struct usb_device *udev = devinfo->udev; 295 struct urb *urb = usb_alloc_urb(0, gfp); 296 struct sense_iu *iu; 297 298 if (!urb) 299 goto out; 300 301 iu = kzalloc(sizeof(*iu), gfp); 302 if (!iu) 303 goto free; 304 305 usb_fill_bulk_urb(urb, udev, devinfo->status_pipe, iu, sizeof(*iu), 306 uas_stat_cmplt, cmnd->device); 307 urb->stream_id = stream_id; 308 urb->transfer_flags |= URB_FREE_BUFFER; 309 out: 310 return urb; 311 free: 312 usb_free_urb(urb); 313 return NULL; 314 } 315 316 static struct urb *uas_alloc_cmd_urb(struct uas_dev_info *devinfo, gfp_t gfp, 317 struct scsi_cmnd *cmnd, u16 stream_id) 318 { 319 struct usb_device *udev = devinfo->udev; 320 struct scsi_device *sdev = cmnd->device; 321 struct urb *urb = usb_alloc_urb(0, gfp); 322 struct command_iu *iu; 323 int len; 324 325 if (!urb) 326 goto out; 327 328 len = cmnd->cmd_len - 16; 329 if (len < 0) 330 len = 0; 331 len = ALIGN(len, 4); 332 iu = kzalloc(sizeof(*iu) + len, gfp); 333 if (!iu) 334 goto free; 335 336 iu->iu_id = IU_ID_COMMAND; 337 iu->tag = cpu_to_be16(stream_id); 338 iu->prio_attr = UAS_SIMPLE_TAG; 339 iu->len = len; 340 int_to_scsilun(sdev->lun, &iu->lun); 341 memcpy(iu->cdb, cmnd->cmnd, cmnd->cmd_len); 342 343 usb_fill_bulk_urb(urb, udev, devinfo->cmd_pipe, iu, sizeof(*iu) + len, 344 usb_free_urb, NULL); 345 urb->transfer_flags |= URB_FREE_BUFFER; 346 out: 347 return urb; 348 free: 349 usb_free_urb(urb); 350 return NULL; 351 } 352 353 /* 354 * Why should I request the Status IU before sending the Command IU? Spec 355 * says to, but also says the device may receive them in any order. Seems 356 * daft to me. 357 */ 358 359 static int uas_submit_urbs(struct scsi_cmnd *cmnd, 360 struct uas_dev_info *devinfo, gfp_t gfp) 361 { 362 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp; 363 364 if (cmdinfo->state & ALLOC_STATUS_URB) { 365 cmdinfo->status_urb = uas_alloc_sense_urb(devinfo, gfp, cmnd, 366 cmdinfo->stream); 367 if (!cmdinfo->status_urb) 368 return SCSI_MLQUEUE_DEVICE_BUSY; 369 cmdinfo->state &= ~ALLOC_STATUS_URB; 370 } 371 372 if (cmdinfo->state & SUBMIT_STATUS_URB) { 373 if (usb_submit_urb(cmdinfo->status_urb, gfp)) { 374 scmd_printk(KERN_INFO, cmnd, 375 "sense urb submission failure\n"); 376 return SCSI_MLQUEUE_DEVICE_BUSY; 377 } 378 cmdinfo->state &= ~SUBMIT_STATUS_URB; 379 } 380 381 if (cmdinfo->state & ALLOC_DATA_IN_URB) { 382 cmdinfo->data_in_urb = uas_alloc_data_urb(devinfo, gfp, 383 devinfo->data_in_pipe, cmdinfo->stream, 384 scsi_in(cmnd), DMA_FROM_DEVICE); 385 if (!cmdinfo->data_in_urb) 386 return SCSI_MLQUEUE_DEVICE_BUSY; 387 cmdinfo->state &= ~ALLOC_DATA_IN_URB; 388 } 389 390 if (cmdinfo->state & SUBMIT_DATA_IN_URB) { 391 if (usb_submit_urb(cmdinfo->data_in_urb, gfp)) { 392 scmd_printk(KERN_INFO, cmnd, 393 "data in urb submission failure\n"); 394 return SCSI_MLQUEUE_DEVICE_BUSY; 395 } 396 cmdinfo->state &= ~SUBMIT_DATA_IN_URB; 397 } 398 399 if (cmdinfo->state & ALLOC_DATA_OUT_URB) { 400 cmdinfo->data_out_urb = uas_alloc_data_urb(devinfo, gfp, 401 devinfo->data_out_pipe, cmdinfo->stream, 402 scsi_out(cmnd), DMA_TO_DEVICE); 403 if (!cmdinfo->data_out_urb) 404 return SCSI_MLQUEUE_DEVICE_BUSY; 405 cmdinfo->state &= ~ALLOC_DATA_OUT_URB; 406 } 407 408 if (cmdinfo->state & SUBMIT_DATA_OUT_URB) { 409 if (usb_submit_urb(cmdinfo->data_out_urb, gfp)) { 410 scmd_printk(KERN_INFO, cmnd, 411 "data out urb submission failure\n"); 412 return SCSI_MLQUEUE_DEVICE_BUSY; 413 } 414 cmdinfo->state &= ~SUBMIT_DATA_OUT_URB; 415 } 416 417 if (cmdinfo->state & ALLOC_CMD_URB) { 418 cmdinfo->cmd_urb = uas_alloc_cmd_urb(devinfo, gfp, cmnd, 419 cmdinfo->stream); 420 if (!cmdinfo->cmd_urb) 421 return SCSI_MLQUEUE_DEVICE_BUSY; 422 cmdinfo->state &= ~ALLOC_CMD_URB; 423 } 424 425 if (cmdinfo->state & SUBMIT_CMD_URB) { 426 if (usb_submit_urb(cmdinfo->cmd_urb, gfp)) { 427 scmd_printk(KERN_INFO, cmnd, 428 "cmd urb submission failure\n"); 429 return SCSI_MLQUEUE_DEVICE_BUSY; 430 } 431 cmdinfo->state &= ~SUBMIT_CMD_URB; 432 } 433 434 return 0; 435 } 436 437 static int uas_queuecommand_lck(struct scsi_cmnd *cmnd, 438 void (*done)(struct scsi_cmnd *)) 439 { 440 struct scsi_device *sdev = cmnd->device; 441 struct uas_dev_info *devinfo = sdev->hostdata; 442 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp; 443 int err; 444 445 BUILD_BUG_ON(sizeof(struct uas_cmd_info) > sizeof(struct scsi_pointer)); 446 447 if (!cmdinfo->status_urb && sdev->current_cmnd) 448 return SCSI_MLQUEUE_DEVICE_BUSY; 449 450 if (blk_rq_tagged(cmnd->request)) { 451 cmdinfo->stream = cmnd->request->tag + 1; 452 } else { 453 sdev->current_cmnd = cmnd; 454 cmdinfo->stream = 1; 455 } 456 457 cmnd->scsi_done = done; 458 459 cmdinfo->state = ALLOC_STATUS_URB | SUBMIT_STATUS_URB | 460 ALLOC_CMD_URB | SUBMIT_CMD_URB; 461 462 switch (cmnd->sc_data_direction) { 463 case DMA_FROM_DEVICE: 464 cmdinfo->state |= ALLOC_DATA_IN_URB | SUBMIT_DATA_IN_URB; 465 break; 466 case DMA_BIDIRECTIONAL: 467 cmdinfo->state |= ALLOC_DATA_IN_URB | SUBMIT_DATA_IN_URB; 468 case DMA_TO_DEVICE: 469 cmdinfo->state |= ALLOC_DATA_OUT_URB | SUBMIT_DATA_OUT_URB; 470 case DMA_NONE: 471 break; 472 } 473 474 if (!devinfo->use_streams) { 475 cmdinfo->state &= ~(SUBMIT_DATA_IN_URB | SUBMIT_DATA_OUT_URB); 476 cmdinfo->stream = 0; 477 } 478 479 err = uas_submit_urbs(cmnd, devinfo, GFP_ATOMIC); 480 if (err) { 481 /* If we did nothing, give up now */ 482 if (cmdinfo->state & SUBMIT_STATUS_URB) { 483 usb_free_urb(cmdinfo->status_urb); 484 return SCSI_MLQUEUE_DEVICE_BUSY; 485 } 486 spin_lock(&uas_work_lock); 487 list_add_tail(&cmdinfo->list, &uas_work_list); 488 spin_unlock(&uas_work_lock); 489 schedule_work(&uas_work); 490 } 491 492 return 0; 493 } 494 495 static DEF_SCSI_QCMD(uas_queuecommand) 496 497 static int uas_eh_abort_handler(struct scsi_cmnd *cmnd) 498 { 499 struct scsi_device *sdev = cmnd->device; 500 sdev_printk(KERN_INFO, sdev, "%s tag %d\n", __func__, 501 cmnd->request->tag); 502 503 /* XXX: Send ABORT TASK Task Management command */ 504 return FAILED; 505 } 506 507 static int uas_eh_device_reset_handler(struct scsi_cmnd *cmnd) 508 { 509 struct scsi_device *sdev = cmnd->device; 510 sdev_printk(KERN_INFO, sdev, "%s tag %d\n", __func__, 511 cmnd->request->tag); 512 513 /* XXX: Send LOGICAL UNIT RESET Task Management command */ 514 return FAILED; 515 } 516 517 static int uas_eh_target_reset_handler(struct scsi_cmnd *cmnd) 518 { 519 struct scsi_device *sdev = cmnd->device; 520 sdev_printk(KERN_INFO, sdev, "%s tag %d\n", __func__, 521 cmnd->request->tag); 522 523 /* XXX: Can we reset just the one USB interface? 524 * Would calling usb_set_interface() have the right effect? 525 */ 526 return FAILED; 527 } 528 529 static int uas_eh_bus_reset_handler(struct scsi_cmnd *cmnd) 530 { 531 struct scsi_device *sdev = cmnd->device; 532 struct uas_dev_info *devinfo = sdev->hostdata; 533 struct usb_device *udev = devinfo->udev; 534 535 sdev_printk(KERN_INFO, sdev, "%s tag %d\n", __func__, 536 cmnd->request->tag); 537 538 if (usb_reset_device(udev)) 539 return SUCCESS; 540 541 return FAILED; 542 } 543 544 static int uas_slave_alloc(struct scsi_device *sdev) 545 { 546 sdev->hostdata = (void *)sdev->host->hostdata[0]; 547 return 0; 548 } 549 550 static int uas_slave_configure(struct scsi_device *sdev) 551 { 552 struct uas_dev_info *devinfo = sdev->hostdata; 553 scsi_set_tag_type(sdev, MSG_ORDERED_TAG); 554 scsi_activate_tcq(sdev, devinfo->qdepth - 1); 555 return 0; 556 } 557 558 static struct scsi_host_template uas_host_template = { 559 .module = THIS_MODULE, 560 .name = "uas", 561 .queuecommand = uas_queuecommand, 562 .slave_alloc = uas_slave_alloc, 563 .slave_configure = uas_slave_configure, 564 .eh_abort_handler = uas_eh_abort_handler, 565 .eh_device_reset_handler = uas_eh_device_reset_handler, 566 .eh_target_reset_handler = uas_eh_target_reset_handler, 567 .eh_bus_reset_handler = uas_eh_bus_reset_handler, 568 .can_queue = 65536, /* Is there a limit on the _host_ ? */ 569 .this_id = -1, 570 .sg_tablesize = SG_NONE, 571 .cmd_per_lun = 1, /* until we override it */ 572 .skip_settle_delay = 1, 573 .ordered_tag = 1, 574 }; 575 576 static struct usb_device_id uas_usb_ids[] = { 577 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_BULK) }, 578 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_UAS) }, 579 /* 0xaa is a prototype device I happen to have access to */ 580 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, 0xaa) }, 581 { } 582 }; 583 MODULE_DEVICE_TABLE(usb, uas_usb_ids); 584 585 static int uas_is_interface(struct usb_host_interface *intf) 586 { 587 return (intf->desc.bInterfaceClass == USB_CLASS_MASS_STORAGE && 588 intf->desc.bInterfaceSubClass == USB_SC_SCSI && 589 intf->desc.bInterfaceProtocol == USB_PR_UAS); 590 } 591 592 static int uas_switch_interface(struct usb_device *udev, 593 struct usb_interface *intf) 594 { 595 int i; 596 597 if (uas_is_interface(intf->cur_altsetting)) 598 return 0; 599 600 for (i = 0; i < intf->num_altsetting; i++) { 601 struct usb_host_interface *alt = &intf->altsetting[i]; 602 if (alt == intf->cur_altsetting) 603 continue; 604 if (uas_is_interface(alt)) 605 return usb_set_interface(udev, 606 alt->desc.bInterfaceNumber, 607 alt->desc.bAlternateSetting); 608 } 609 610 return -ENODEV; 611 } 612 613 static void uas_configure_endpoints(struct uas_dev_info *devinfo) 614 { 615 struct usb_host_endpoint *eps[4] = { }; 616 struct usb_interface *intf = devinfo->intf; 617 struct usb_device *udev = devinfo->udev; 618 struct usb_host_endpoint *endpoint = intf->cur_altsetting->endpoint; 619 unsigned i, n_endpoints = intf->cur_altsetting->desc.bNumEndpoints; 620 621 devinfo->uas_sense_old = 0; 622 623 for (i = 0; i < n_endpoints; i++) { 624 unsigned char *extra = endpoint[i].extra; 625 int len = endpoint[i].extralen; 626 while (len > 1) { 627 if (extra[1] == USB_DT_PIPE_USAGE) { 628 unsigned pipe_id = extra[2]; 629 if (pipe_id > 0 && pipe_id < 5) 630 eps[pipe_id - 1] = &endpoint[i]; 631 break; 632 } 633 len -= extra[0]; 634 extra += extra[0]; 635 } 636 } 637 638 /* 639 * Assume that if we didn't find a control pipe descriptor, we're 640 * using a device with old firmware that happens to be set up like 641 * this. 642 */ 643 if (!eps[0]) { 644 devinfo->cmd_pipe = usb_sndbulkpipe(udev, 1); 645 devinfo->status_pipe = usb_rcvbulkpipe(udev, 1); 646 devinfo->data_in_pipe = usb_rcvbulkpipe(udev, 2); 647 devinfo->data_out_pipe = usb_sndbulkpipe(udev, 2); 648 649 eps[1] = usb_pipe_endpoint(udev, devinfo->status_pipe); 650 eps[2] = usb_pipe_endpoint(udev, devinfo->data_in_pipe); 651 eps[3] = usb_pipe_endpoint(udev, devinfo->data_out_pipe); 652 } else { 653 devinfo->cmd_pipe = usb_sndbulkpipe(udev, 654 eps[0]->desc.bEndpointAddress); 655 devinfo->status_pipe = usb_rcvbulkpipe(udev, 656 eps[1]->desc.bEndpointAddress); 657 devinfo->data_in_pipe = usb_rcvbulkpipe(udev, 658 eps[2]->desc.bEndpointAddress); 659 devinfo->data_out_pipe = usb_sndbulkpipe(udev, 660 eps[3]->desc.bEndpointAddress); 661 } 662 663 devinfo->qdepth = usb_alloc_streams(devinfo->intf, eps + 1, 3, 256, 664 GFP_KERNEL); 665 if (devinfo->qdepth < 0) { 666 devinfo->qdepth = 256; 667 devinfo->use_streams = 0; 668 } else { 669 devinfo->use_streams = 1; 670 } 671 } 672 673 /* 674 * XXX: What I'd like to do here is register a SCSI host for each USB host in 675 * the system. Follow usb-storage's design of registering a SCSI host for 676 * each USB device for the moment. Can implement this by walking up the 677 * USB hierarchy until we find a USB host. 678 */ 679 static int uas_probe(struct usb_interface *intf, const struct usb_device_id *id) 680 { 681 int result; 682 struct Scsi_Host *shost; 683 struct uas_dev_info *devinfo; 684 struct usb_device *udev = interface_to_usbdev(intf); 685 686 if (uas_switch_interface(udev, intf)) 687 return -ENODEV; 688 689 devinfo = kmalloc(sizeof(struct uas_dev_info), GFP_KERNEL); 690 if (!devinfo) 691 return -ENOMEM; 692 693 result = -ENOMEM; 694 shost = scsi_host_alloc(&uas_host_template, sizeof(void *)); 695 if (!shost) 696 goto free; 697 698 shost->max_cmd_len = 16 + 252; 699 shost->max_id = 1; 700 shost->sg_tablesize = udev->bus->sg_tablesize; 701 702 result = scsi_add_host(shost, &intf->dev); 703 if (result) 704 goto free; 705 shost->hostdata[0] = (unsigned long)devinfo; 706 707 devinfo->intf = intf; 708 devinfo->udev = udev; 709 uas_configure_endpoints(devinfo); 710 711 scsi_scan_host(shost); 712 usb_set_intfdata(intf, shost); 713 return result; 714 free: 715 kfree(devinfo); 716 if (shost) 717 scsi_host_put(shost); 718 return result; 719 } 720 721 static int uas_pre_reset(struct usb_interface *intf) 722 { 723 /* XXX: Need to return 1 if it's not our device in error handling */ 724 return 0; 725 } 726 727 static int uas_post_reset(struct usb_interface *intf) 728 { 729 /* XXX: Need to return 1 if it's not our device in error handling */ 730 return 0; 731 } 732 733 static void uas_disconnect(struct usb_interface *intf) 734 { 735 struct usb_device *udev = interface_to_usbdev(intf); 736 struct usb_host_endpoint *eps[3]; 737 struct Scsi_Host *shost = usb_get_intfdata(intf); 738 struct uas_dev_info *devinfo = (void *)shost->hostdata[0]; 739 740 scsi_remove_host(shost); 741 742 eps[0] = usb_pipe_endpoint(udev, devinfo->status_pipe); 743 eps[1] = usb_pipe_endpoint(udev, devinfo->data_in_pipe); 744 eps[2] = usb_pipe_endpoint(udev, devinfo->data_out_pipe); 745 usb_free_streams(intf, eps, 3, GFP_KERNEL); 746 747 kfree(devinfo); 748 } 749 750 /* 751 * XXX: Should this plug into libusual so we can auto-upgrade devices from 752 * Bulk-Only to UAS? 753 */ 754 static struct usb_driver uas_driver = { 755 .name = "uas", 756 .probe = uas_probe, 757 .disconnect = uas_disconnect, 758 .pre_reset = uas_pre_reset, 759 .post_reset = uas_post_reset, 760 .id_table = uas_usb_ids, 761 }; 762 763 static int uas_init(void) 764 { 765 return usb_register(&uas_driver); 766 } 767 768 static void uas_exit(void) 769 { 770 usb_deregister(&uas_driver); 771 } 772 773 module_init(uas_init); 774 module_exit(uas_exit); 775 776 MODULE_LICENSE("GPL"); 777 MODULE_AUTHOR("Matthew Wilcox and Sarah Sharp"); 778