3 * Note that this is not the same as the USB Mass Storage driver
5 * Copyright Matthew Wilcox for Intel Corp, 2010
6 * Copyright Sarah Sharp for Intel Corp, 2010
8 * Distributed under the terms of the GNU GPL, version two.
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/hcd.h>
17 #include <linux/usb/storage.h>
18 #include <linux/usb/uas.h>
20 #include <scsi/scsi.h>
21 #include <scsi/scsi_dbg.h>
22 #include <scsi/scsi_cmnd.h>
23 #include <scsi/scsi_device.h>
24 #include <scsi/scsi_host.h>
25 #include <scsi/scsi_tcq.h>
28 * The r00-r01c specs define this version of the SENSE IU data structure.
29 * It's still in use by several different firmware releases.
37 __u8 service_response;
38 __u8 sense[SCSI_SENSE_BUFFERSIZE];
42 struct usb_interface *intf;
43 struct usb_device *udev;
44 struct usb_anchor sense_urbs;
45 struct usb_anchor data_urbs;
46 int qdepth, resetting;
47 struct response_ui response;
48 unsigned cmd_pipe, status_pipe, data_in_pipe, data_out_pipe;
49 unsigned use_streams:1;
50 unsigned uas_sense_old:1;
51 struct scsi_cmnd *cmnd;
55 SUBMIT_STATUS_URB = (1 << 1),
56 ALLOC_DATA_IN_URB = (1 << 2),
57 SUBMIT_DATA_IN_URB = (1 << 3),
58 ALLOC_DATA_OUT_URB = (1 << 4),
59 SUBMIT_DATA_OUT_URB = (1 << 5),
60 ALLOC_CMD_URB = (1 << 6),
61 SUBMIT_CMD_URB = (1 << 7),
62 COMMAND_INFLIGHT = (1 << 8),
63 DATA_IN_URB_INFLIGHT = (1 << 9),
64 DATA_OUT_URB_INFLIGHT = (1 << 10),
65 COMMAND_COMPLETED = (1 << 11),
68 /* Overrides scsi_pointer */
74 struct urb *data_in_urb;
75 struct urb *data_out_urb;
76 struct list_head list;
79 /* I hate forward declarations, but I actually have a loop */
80 static int uas_submit_urbs(struct scsi_cmnd *cmnd,
81 struct uas_dev_info *devinfo, gfp_t gfp);
82 static void uas_do_work(struct work_struct *work);
84 static DECLARE_WORK(uas_work, uas_do_work);
85 static DEFINE_SPINLOCK(uas_work_lock);
86 static LIST_HEAD(uas_work_list);
88 static void uas_do_work(struct work_struct *work)
90 struct uas_cmd_info *cmdinfo;
91 struct uas_cmd_info *temp;
92 struct list_head list;
95 spin_lock_irq(&uas_work_lock);
96 list_replace_init(&uas_work_list, &list);
97 spin_unlock_irq(&uas_work_lock);
99 list_for_each_entry_safe(cmdinfo, temp, &list, list) {
100 struct scsi_pointer *scp = (void *)cmdinfo;
101 struct scsi_cmnd *cmnd = container_of(scp,
102 struct scsi_cmnd, SCp);
103 err = uas_submit_urbs(cmnd, cmnd->device->hostdata, GFP_NOIO);
105 list_del(&cmdinfo->list);
106 spin_lock_irq(&uas_work_lock);
107 list_add_tail(&cmdinfo->list, &uas_work_list);
108 spin_unlock_irq(&uas_work_lock);
109 schedule_work(&uas_work);
114 static void uas_sense(struct urb *urb, struct scsi_cmnd *cmnd)
116 struct sense_iu *sense_iu = urb->transfer_buffer;
117 struct scsi_device *sdev = cmnd->device;
119 if (urb->actual_length > 16) {
120 unsigned len = be16_to_cpup(&sense_iu->len);
121 if (len + 16 != urb->actual_length) {
122 int newlen = min(len + 16, urb->actual_length) - 16;
125 sdev_printk(KERN_INFO, sdev, "%s: urb length %d "
126 "disagrees with IU sense data length %d, "
127 "using %d bytes of sense data\n", __func__,
128 urb->actual_length, len, newlen);
131 memcpy(cmnd->sense_buffer, sense_iu->sense, len);
134 cmnd->result = sense_iu->status;
137 static void uas_sense_old(struct urb *urb, struct scsi_cmnd *cmnd)
139 struct sense_iu_old *sense_iu = urb->transfer_buffer;
140 struct scsi_device *sdev = cmnd->device;
142 if (urb->actual_length > 8) {
143 unsigned len = be16_to_cpup(&sense_iu->len) - 2;
144 if (len + 8 != urb->actual_length) {
145 int newlen = min(len + 8, urb->actual_length) - 8;
148 sdev_printk(KERN_INFO, sdev, "%s: urb length %d "
149 "disagrees with IU sense data length %d, "
150 "using %d bytes of sense data\n", __func__,
151 urb->actual_length, len, newlen);
154 memcpy(cmnd->sense_buffer, sense_iu->sense, len);
157 cmnd->result = sense_iu->status;
160 static void uas_log_cmd_state(struct scsi_cmnd *cmnd, const char *caller)
162 struct uas_cmd_info *ci = (void *)&cmnd->SCp;
164 scmd_printk(KERN_INFO, cmnd, "%s %p tag %d, inflight:"
165 "%s%s%s%s%s%s%s%s%s%s%s\n",
166 caller, cmnd, cmnd->request->tag,
167 (ci->state & SUBMIT_STATUS_URB) ? " s-st" : "",
168 (ci->state & ALLOC_DATA_IN_URB) ? " a-in" : "",
169 (ci->state & SUBMIT_DATA_IN_URB) ? " s-in" : "",
170 (ci->state & ALLOC_DATA_OUT_URB) ? " a-out" : "",
171 (ci->state & SUBMIT_DATA_OUT_URB) ? " s-out" : "",
172 (ci->state & ALLOC_CMD_URB) ? " a-cmd" : "",
173 (ci->state & SUBMIT_CMD_URB) ? " s-cmd" : "",
174 (ci->state & COMMAND_INFLIGHT) ? " CMD" : "",
175 (ci->state & DATA_IN_URB_INFLIGHT) ? " IN" : "",
176 (ci->state & DATA_OUT_URB_INFLIGHT) ? " OUT" : "",
177 (ci->state & COMMAND_COMPLETED) ? " done" : "");
180 static int uas_try_complete(struct scsi_cmnd *cmnd, const char *caller)
182 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
184 if (cmdinfo->state & (COMMAND_INFLIGHT |
185 DATA_IN_URB_INFLIGHT |
186 DATA_OUT_URB_INFLIGHT))
188 BUG_ON(cmdinfo->state & COMMAND_COMPLETED);
189 cmdinfo->state |= COMMAND_COMPLETED;
190 usb_free_urb(cmdinfo->data_in_urb);
191 usb_free_urb(cmdinfo->data_out_urb);
192 cmnd->scsi_done(cmnd);
196 static void uas_xfer_data(struct urb *urb, struct scsi_cmnd *cmnd,
199 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
202 cmdinfo->state |= direction | SUBMIT_STATUS_URB;
203 err = uas_submit_urbs(cmnd, cmnd->device->hostdata, GFP_ATOMIC);
205 spin_lock(&uas_work_lock);
206 list_add_tail(&cmdinfo->list, &uas_work_list);
207 spin_unlock(&uas_work_lock);
208 schedule_work(&uas_work);
212 static void uas_stat_cmplt(struct urb *urb)
214 struct iu *iu = urb->transfer_buffer;
215 struct Scsi_Host *shost = urb->context;
216 struct uas_dev_info *devinfo = (void *)shost->hostdata[0];
217 struct scsi_cmnd *cmnd;
218 struct uas_cmd_info *cmdinfo;
222 dev_err(&urb->dev->dev, "URB BAD STATUS %d\n", urb->status);
227 if (devinfo->resetting) {
232 tag = be16_to_cpup(&iu->tag) - 1;
234 cmnd = devinfo->cmnd;
236 cmnd = scsi_host_find_tag(shost, tag - 1);
238 if (iu->iu_id != IU_ID_RESPONSE) {
243 cmdinfo = (void *)&cmnd->SCp;
248 if (devinfo->cmnd == cmnd)
249 devinfo->cmnd = NULL;
251 if (urb->actual_length < 16)
252 devinfo->uas_sense_old = 1;
253 if (devinfo->uas_sense_old)
254 uas_sense_old(urb, cmnd);
256 uas_sense(urb, cmnd);
257 if (cmnd->result != 0) {
258 /* cancel data transfers on error */
259 if (cmdinfo->state & DATA_IN_URB_INFLIGHT)
260 usb_unlink_urb(cmdinfo->data_in_urb);
261 if (cmdinfo->state & DATA_OUT_URB_INFLIGHT)
262 usb_unlink_urb(cmdinfo->data_out_urb);
264 cmdinfo->state &= ~COMMAND_INFLIGHT;
265 uas_try_complete(cmnd, __func__);
267 case IU_ID_READ_READY:
268 uas_xfer_data(urb, cmnd, SUBMIT_DATA_IN_URB);
270 case IU_ID_WRITE_READY:
271 uas_xfer_data(urb, cmnd, SUBMIT_DATA_OUT_URB);
274 /* store results for uas_eh_task_mgmt() */
275 memcpy(&devinfo->response, iu, sizeof(devinfo->response));
278 scmd_printk(KERN_ERR, cmnd,
279 "Bogus IU (%d) received on status pipe\n", iu->iu_id);
284 static void uas_data_cmplt(struct urb *urb)
286 struct scsi_cmnd *cmnd = urb->context;
287 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
288 struct scsi_data_buffer *sdb = NULL;
290 if (cmdinfo->data_in_urb == urb) {
292 cmdinfo->state &= ~DATA_IN_URB_INFLIGHT;
293 } else if (cmdinfo->data_out_urb == urb) {
294 sdb = scsi_out(cmnd);
295 cmdinfo->state &= ~DATA_OUT_URB_INFLIGHT;
299 /* error: no data transfered */
300 sdb->resid = sdb->length;
302 sdb->resid = sdb->length - urb->actual_length;
304 if (cmdinfo->aborted) {
307 uas_try_complete(cmnd, __func__);
310 static struct urb *uas_alloc_data_urb(struct uas_dev_info *devinfo, gfp_t gfp,
311 unsigned int pipe, u16 stream_id,
312 struct scsi_cmnd *cmnd,
313 enum dma_data_direction dir)
315 struct usb_device *udev = devinfo->udev;
316 struct urb *urb = usb_alloc_urb(0, gfp);
317 struct scsi_data_buffer *sdb = (dir == DMA_FROM_DEVICE)
318 ? scsi_in(cmnd) : scsi_out(cmnd);
322 usb_fill_bulk_urb(urb, udev, pipe, NULL, sdb->length,
323 uas_data_cmplt, cmnd);
324 if (devinfo->use_streams)
325 urb->stream_id = stream_id;
326 urb->num_sgs = udev->bus->sg_tablesize ? sdb->table.nents : 0;
327 urb->sg = sdb->table.sgl;
332 static struct urb *uas_alloc_sense_urb(struct uas_dev_info *devinfo, gfp_t gfp,
333 struct Scsi_Host *shost, u16 stream_id)
335 struct usb_device *udev = devinfo->udev;
336 struct urb *urb = usb_alloc_urb(0, gfp);
342 iu = kzalloc(sizeof(*iu), gfp);
346 usb_fill_bulk_urb(urb, udev, devinfo->status_pipe, iu, sizeof(*iu),
347 uas_stat_cmplt, shost);
348 urb->stream_id = stream_id;
349 urb->transfer_flags |= URB_FREE_BUFFER;
357 static struct urb *uas_alloc_cmd_urb(struct uas_dev_info *devinfo, gfp_t gfp,
358 struct scsi_cmnd *cmnd, u16 stream_id)
360 struct usb_device *udev = devinfo->udev;
361 struct scsi_device *sdev = cmnd->device;
362 struct urb *urb = usb_alloc_urb(0, gfp);
363 struct command_iu *iu;
369 len = cmnd->cmd_len - 16;
373 iu = kzalloc(sizeof(*iu) + len, gfp);
377 iu->iu_id = IU_ID_COMMAND;
378 if (blk_rq_tagged(cmnd->request))
379 iu->tag = cpu_to_be16(cmnd->request->tag + 2);
381 iu->tag = cpu_to_be16(1);
382 iu->prio_attr = UAS_SIMPLE_TAG;
384 int_to_scsilun(sdev->lun, &iu->lun);
385 memcpy(iu->cdb, cmnd->cmnd, cmnd->cmd_len);
387 usb_fill_bulk_urb(urb, udev, devinfo->cmd_pipe, iu, sizeof(*iu) + len,
389 urb->transfer_flags |= URB_FREE_BUFFER;
397 static int uas_submit_task_urb(struct scsi_cmnd *cmnd, gfp_t gfp,
398 u8 function, u16 stream_id)
400 struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata;
401 struct usb_device *udev = devinfo->udev;
402 struct urb *urb = usb_alloc_urb(0, gfp);
403 struct task_mgmt_iu *iu;
409 iu = kzalloc(sizeof(*iu), gfp);
413 iu->iu_id = IU_ID_TASK_MGMT;
414 iu->tag = cpu_to_be16(stream_id);
415 int_to_scsilun(cmnd->device->lun, &iu->lun);
417 iu->function = function;
420 if (blk_rq_tagged(cmnd->request))
421 iu->task_tag = cpu_to_be16(cmnd->request->tag + 2);
423 iu->task_tag = cpu_to_be16(1);
427 usb_fill_bulk_urb(urb, udev, devinfo->cmd_pipe, iu, sizeof(*iu),
429 urb->transfer_flags |= URB_FREE_BUFFER;
431 err = usb_submit_urb(urb, gfp);
443 * Why should I request the Status IU before sending the Command IU? Spec
444 * says to, but also says the device may receive them in any order. Seems
448 static int uas_submit_sense_urb(struct Scsi_Host *shost,
449 gfp_t gfp, unsigned int stream)
451 struct uas_dev_info *devinfo = (void *)shost->hostdata[0];
454 urb = uas_alloc_sense_urb(devinfo, gfp, shost, stream);
456 return SCSI_MLQUEUE_DEVICE_BUSY;
457 if (usb_submit_urb(urb, gfp)) {
458 shost_printk(KERN_INFO, shost,
459 "sense urb submission failure\n");
461 return SCSI_MLQUEUE_DEVICE_BUSY;
463 usb_anchor_urb(urb, &devinfo->sense_urbs);
467 static int uas_submit_urbs(struct scsi_cmnd *cmnd,
468 struct uas_dev_info *devinfo, gfp_t gfp)
470 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
473 if (cmdinfo->state & SUBMIT_STATUS_URB) {
474 err = uas_submit_sense_urb(cmnd->device->host, gfp,
479 cmdinfo->state &= ~SUBMIT_STATUS_URB;
482 if (cmdinfo->state & ALLOC_DATA_IN_URB) {
483 cmdinfo->data_in_urb = uas_alloc_data_urb(devinfo, gfp,
484 devinfo->data_in_pipe, cmdinfo->stream,
485 cmnd, DMA_FROM_DEVICE);
486 if (!cmdinfo->data_in_urb)
487 return SCSI_MLQUEUE_DEVICE_BUSY;
488 cmdinfo->state &= ~ALLOC_DATA_IN_URB;
491 if (cmdinfo->state & SUBMIT_DATA_IN_URB) {
492 if (usb_submit_urb(cmdinfo->data_in_urb, gfp)) {
493 scmd_printk(KERN_INFO, cmnd,
494 "data in urb submission failure\n");
495 return SCSI_MLQUEUE_DEVICE_BUSY;
497 cmdinfo->state &= ~SUBMIT_DATA_IN_URB;
498 cmdinfo->state |= DATA_IN_URB_INFLIGHT;
499 usb_anchor_urb(cmdinfo->data_in_urb, &devinfo->data_urbs);
502 if (cmdinfo->state & ALLOC_DATA_OUT_URB) {
503 cmdinfo->data_out_urb = uas_alloc_data_urb(devinfo, gfp,
504 devinfo->data_out_pipe, cmdinfo->stream,
505 cmnd, DMA_TO_DEVICE);
506 if (!cmdinfo->data_out_urb)
507 return SCSI_MLQUEUE_DEVICE_BUSY;
508 cmdinfo->state &= ~ALLOC_DATA_OUT_URB;
511 if (cmdinfo->state & SUBMIT_DATA_OUT_URB) {
512 if (usb_submit_urb(cmdinfo->data_out_urb, gfp)) {
513 scmd_printk(KERN_INFO, cmnd,
514 "data out urb submission failure\n");
515 return SCSI_MLQUEUE_DEVICE_BUSY;
517 cmdinfo->state &= ~SUBMIT_DATA_OUT_URB;
518 cmdinfo->state |= DATA_OUT_URB_INFLIGHT;
519 usb_anchor_urb(cmdinfo->data_out_urb, &devinfo->data_urbs);
522 if (cmdinfo->state & ALLOC_CMD_URB) {
523 cmdinfo->cmd_urb = uas_alloc_cmd_urb(devinfo, gfp, cmnd,
525 if (!cmdinfo->cmd_urb)
526 return SCSI_MLQUEUE_DEVICE_BUSY;
527 cmdinfo->state &= ~ALLOC_CMD_URB;
530 if (cmdinfo->state & SUBMIT_CMD_URB) {
531 if (usb_submit_urb(cmdinfo->cmd_urb, gfp)) {
532 scmd_printk(KERN_INFO, cmnd,
533 "cmd urb submission failure\n");
534 return SCSI_MLQUEUE_DEVICE_BUSY;
536 cmdinfo->state &= ~SUBMIT_CMD_URB;
537 cmdinfo->state |= COMMAND_INFLIGHT;
543 static int uas_queuecommand_lck(struct scsi_cmnd *cmnd,
544 void (*done)(struct scsi_cmnd *))
546 struct scsi_device *sdev = cmnd->device;
547 struct uas_dev_info *devinfo = sdev->hostdata;
548 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
551 BUILD_BUG_ON(sizeof(struct uas_cmd_info) > sizeof(struct scsi_pointer));
554 return SCSI_MLQUEUE_DEVICE_BUSY;
556 if (blk_rq_tagged(cmnd->request)) {
557 cmdinfo->stream = cmnd->request->tag + 2;
559 devinfo->cmnd = cmnd;
563 cmnd->scsi_done = done;
565 cmdinfo->state = SUBMIT_STATUS_URB |
566 ALLOC_CMD_URB | SUBMIT_CMD_URB;
567 cmdinfo->aborted = 0;
569 switch (cmnd->sc_data_direction) {
570 case DMA_FROM_DEVICE:
571 cmdinfo->state |= ALLOC_DATA_IN_URB | SUBMIT_DATA_IN_URB;
573 case DMA_BIDIRECTIONAL:
574 cmdinfo->state |= ALLOC_DATA_IN_URB | SUBMIT_DATA_IN_URB;
576 cmdinfo->state |= ALLOC_DATA_OUT_URB | SUBMIT_DATA_OUT_URB;
581 if (!devinfo->use_streams) {
582 cmdinfo->state &= ~(SUBMIT_DATA_IN_URB | SUBMIT_DATA_OUT_URB);
586 err = uas_submit_urbs(cmnd, devinfo, GFP_ATOMIC);
588 /* If we did nothing, give up now */
589 if (cmdinfo->state & SUBMIT_STATUS_URB) {
590 return SCSI_MLQUEUE_DEVICE_BUSY;
592 spin_lock(&uas_work_lock);
593 list_add_tail(&cmdinfo->list, &uas_work_list);
594 spin_unlock(&uas_work_lock);
595 schedule_work(&uas_work);
601 static DEF_SCSI_QCMD(uas_queuecommand)
603 static int uas_eh_task_mgmt(struct scsi_cmnd *cmnd,
604 const char *fname, u8 function)
606 struct Scsi_Host *shost = cmnd->device->host;
607 struct uas_dev_info *devinfo = (void *)shost->hostdata[0];
608 u16 tag = 9999; /* FIXME */
610 memset(&devinfo->response, 0, sizeof(devinfo->response));
611 if (uas_submit_sense_urb(shost, GFP_NOIO, tag)) {
612 shost_printk(KERN_INFO, shost,
613 "%s: %s: submit sense urb failed\n",
617 if (uas_submit_task_urb(cmnd, GFP_NOIO, function, tag)) {
618 shost_printk(KERN_INFO, shost,
619 "%s: %s: submit task mgmt urb failed\n",
623 if (0 == usb_wait_anchor_empty_timeout(&devinfo->sense_urbs, 3000)) {
624 shost_printk(KERN_INFO, shost,
625 "%s: %s timed out\n", __func__, fname);
628 if (be16_to_cpu(devinfo->response.tag) != tag) {
629 shost_printk(KERN_INFO, shost,
630 "%s: %s failed (wrong tag %d/%d)\n", __func__,
631 fname, be16_to_cpu(devinfo->response.tag), tag);
634 if (devinfo->response.response_code != RC_TMF_COMPLETE) {
635 shost_printk(KERN_INFO, shost,
636 "%s: %s failed (rc 0x%x)\n", __func__,
637 fname, devinfo->response.response_code);
643 static int uas_eh_abort_handler(struct scsi_cmnd *cmnd)
645 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
648 uas_log_cmd_state(cmnd, __func__);
649 cmdinfo->aborted = 1;
650 ret = uas_eh_task_mgmt(cmnd, "ABORT TASK", TMF_ABORT_TASK);
651 if (cmdinfo->state & DATA_IN_URB_INFLIGHT)
652 usb_kill_urb(cmdinfo->data_in_urb);
653 if (cmdinfo->state & DATA_OUT_URB_INFLIGHT)
654 usb_kill_urb(cmdinfo->data_out_urb);
658 static int uas_eh_device_reset_handler(struct scsi_cmnd *cmnd)
660 sdev_printk(KERN_INFO, cmnd->device, "%s\n", __func__);
661 return uas_eh_task_mgmt(cmnd, "LOGICAL UNIT RESET",
662 TMF_LOGICAL_UNIT_RESET);
665 static int uas_eh_bus_reset_handler(struct scsi_cmnd *cmnd)
667 struct scsi_device *sdev = cmnd->device;
668 struct uas_dev_info *devinfo = sdev->hostdata;
669 struct usb_device *udev = devinfo->udev;
672 devinfo->resetting = 1;
673 usb_kill_anchored_urbs(&devinfo->sense_urbs);
674 usb_kill_anchored_urbs(&devinfo->data_urbs);
675 err = usb_reset_device(udev);
676 devinfo->resetting = 0;
679 shost_printk(KERN_INFO, sdev->host, "%s FAILED\n", __func__);
683 shost_printk(KERN_INFO, sdev->host, "%s success\n", __func__);
687 static int uas_slave_alloc(struct scsi_device *sdev)
689 sdev->hostdata = (void *)sdev->host->hostdata[0];
693 static int uas_slave_configure(struct scsi_device *sdev)
695 struct uas_dev_info *devinfo = sdev->hostdata;
696 scsi_set_tag_type(sdev, MSG_ORDERED_TAG);
697 scsi_activate_tcq(sdev, devinfo->qdepth - 2);
701 static struct scsi_host_template uas_host_template = {
702 .module = THIS_MODULE,
704 .queuecommand = uas_queuecommand,
705 .slave_alloc = uas_slave_alloc,
706 .slave_configure = uas_slave_configure,
707 .eh_abort_handler = uas_eh_abort_handler,
708 .eh_device_reset_handler = uas_eh_device_reset_handler,
709 .eh_bus_reset_handler = uas_eh_bus_reset_handler,
710 .can_queue = 65536, /* Is there a limit on the _host_ ? */
712 .sg_tablesize = SG_NONE,
713 .cmd_per_lun = 1, /* until we override it */
714 .skip_settle_delay = 1,
718 static struct usb_device_id uas_usb_ids[] = {
719 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_BULK) },
720 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_UAS) },
721 /* 0xaa is a prototype device I happen to have access to */
722 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, 0xaa) },
725 MODULE_DEVICE_TABLE(usb, uas_usb_ids);
727 static int uas_is_interface(struct usb_host_interface *intf)
729 return (intf->desc.bInterfaceClass == USB_CLASS_MASS_STORAGE &&
730 intf->desc.bInterfaceSubClass == USB_SC_SCSI &&
731 intf->desc.bInterfaceProtocol == USB_PR_UAS);
734 static int uas_isnt_supported(struct usb_device *udev)
736 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
738 dev_warn(&udev->dev, "The driver for the USB controller %s does not "
739 "support scatter-gather which is\n",
740 hcd->driver->description);
741 dev_warn(&udev->dev, "required by the UAS driver. Please try an"
742 "alternative USB controller if you wish to use UAS.\n");
746 static int uas_switch_interface(struct usb_device *udev,
747 struct usb_interface *intf)
750 int sg_supported = udev->bus->sg_tablesize != 0;
752 for (i = 0; i < intf->num_altsetting; i++) {
753 struct usb_host_interface *alt = &intf->altsetting[i];
755 if (uas_is_interface(alt)) {
757 return uas_isnt_supported(udev);
758 return usb_set_interface(udev,
759 alt->desc.bInterfaceNumber,
760 alt->desc.bAlternateSetting);
767 static void uas_configure_endpoints(struct uas_dev_info *devinfo)
769 struct usb_host_endpoint *eps[4] = { };
770 struct usb_interface *intf = devinfo->intf;
771 struct usb_device *udev = devinfo->udev;
772 struct usb_host_endpoint *endpoint = intf->cur_altsetting->endpoint;
773 unsigned i, n_endpoints = intf->cur_altsetting->desc.bNumEndpoints;
775 devinfo->uas_sense_old = 0;
776 devinfo->cmnd = NULL;
778 for (i = 0; i < n_endpoints; i++) {
779 unsigned char *extra = endpoint[i].extra;
780 int len = endpoint[i].extralen;
782 if (extra[1] == USB_DT_PIPE_USAGE) {
783 unsigned pipe_id = extra[2];
784 if (pipe_id > 0 && pipe_id < 5)
785 eps[pipe_id - 1] = &endpoint[i];
794 * Assume that if we didn't find a control pipe descriptor, we're
795 * using a device with old firmware that happens to be set up like
799 devinfo->cmd_pipe = usb_sndbulkpipe(udev, 1);
800 devinfo->status_pipe = usb_rcvbulkpipe(udev, 1);
801 devinfo->data_in_pipe = usb_rcvbulkpipe(udev, 2);
802 devinfo->data_out_pipe = usb_sndbulkpipe(udev, 2);
804 eps[1] = usb_pipe_endpoint(udev, devinfo->status_pipe);
805 eps[2] = usb_pipe_endpoint(udev, devinfo->data_in_pipe);
806 eps[3] = usb_pipe_endpoint(udev, devinfo->data_out_pipe);
808 devinfo->cmd_pipe = usb_sndbulkpipe(udev,
809 eps[0]->desc.bEndpointAddress);
810 devinfo->status_pipe = usb_rcvbulkpipe(udev,
811 eps[1]->desc.bEndpointAddress);
812 devinfo->data_in_pipe = usb_rcvbulkpipe(udev,
813 eps[2]->desc.bEndpointAddress);
814 devinfo->data_out_pipe = usb_sndbulkpipe(udev,
815 eps[3]->desc.bEndpointAddress);
818 devinfo->qdepth = usb_alloc_streams(devinfo->intf, eps + 1, 3, 256,
820 if (devinfo->qdepth < 0) {
821 devinfo->qdepth = 256;
822 devinfo->use_streams = 0;
824 devinfo->use_streams = 1;
828 static void uas_free_streams(struct uas_dev_info *devinfo)
830 struct usb_device *udev = devinfo->udev;
831 struct usb_host_endpoint *eps[3];
833 eps[0] = usb_pipe_endpoint(udev, devinfo->status_pipe);
834 eps[1] = usb_pipe_endpoint(udev, devinfo->data_in_pipe);
835 eps[2] = usb_pipe_endpoint(udev, devinfo->data_out_pipe);
836 usb_free_streams(devinfo->intf, eps, 3, GFP_KERNEL);
840 * XXX: What I'd like to do here is register a SCSI host for each USB host in
841 * the system. Follow usb-storage's design of registering a SCSI host for
842 * each USB device for the moment. Can implement this by walking up the
843 * USB hierarchy until we find a USB host.
845 static int uas_probe(struct usb_interface *intf, const struct usb_device_id *id)
848 struct Scsi_Host *shost;
849 struct uas_dev_info *devinfo;
850 struct usb_device *udev = interface_to_usbdev(intf);
852 if (uas_switch_interface(udev, intf))
855 devinfo = kmalloc(sizeof(struct uas_dev_info), GFP_KERNEL);
860 shost = scsi_host_alloc(&uas_host_template, sizeof(void *));
864 shost->max_cmd_len = 16 + 252;
866 shost->sg_tablesize = udev->bus->sg_tablesize;
868 devinfo->intf = intf;
869 devinfo->udev = udev;
870 devinfo->resetting = 0;
871 init_usb_anchor(&devinfo->sense_urbs);
872 init_usb_anchor(&devinfo->data_urbs);
873 uas_configure_endpoints(devinfo);
875 result = scsi_init_shared_tag_map(shost, devinfo->qdepth - 2);
879 result = scsi_add_host(shost, &intf->dev);
883 shost->hostdata[0] = (unsigned long)devinfo;
885 scsi_scan_host(shost);
886 usb_set_intfdata(intf, shost);
890 uas_free_streams(devinfo);
894 scsi_host_put(shost);
898 static int uas_pre_reset(struct usb_interface *intf)
900 /* XXX: Need to return 1 if it's not our device in error handling */
904 static int uas_post_reset(struct usb_interface *intf)
906 /* XXX: Need to return 1 if it's not our device in error handling */
910 static void uas_disconnect(struct usb_interface *intf)
912 struct Scsi_Host *shost = usb_get_intfdata(intf);
913 struct uas_dev_info *devinfo = (void *)shost->hostdata[0];
915 scsi_remove_host(shost);
916 usb_kill_anchored_urbs(&devinfo->sense_urbs);
917 usb_kill_anchored_urbs(&devinfo->data_urbs);
918 uas_free_streams(devinfo);
923 * XXX: Should this plug into libusual so we can auto-upgrade devices from
926 static struct usb_driver uas_driver = {
929 .disconnect = uas_disconnect,
930 .pre_reset = uas_pre_reset,
931 .post_reset = uas_post_reset,
932 .id_table = uas_usb_ids,
935 module_usb_driver(uas_driver);
937 MODULE_LICENSE("GPL");
938 MODULE_AUTHOR("Matthew Wilcox and Sarah Sharp");