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;
47 unsigned cmd_pipe, status_pipe, data_in_pipe, data_out_pipe;
48 unsigned use_streams:1;
49 unsigned uas_sense_old:1;
50 struct scsi_cmnd *cmnd;
54 SUBMIT_STATUS_URB = (1 << 1),
55 ALLOC_DATA_IN_URB = (1 << 2),
56 SUBMIT_DATA_IN_URB = (1 << 3),
57 ALLOC_DATA_OUT_URB = (1 << 4),
58 SUBMIT_DATA_OUT_URB = (1 << 5),
59 ALLOC_CMD_URB = (1 << 6),
60 SUBMIT_CMD_URB = (1 << 7),
61 COMMAND_INFLIGHT = (1 << 8),
62 DATA_IN_URB_INFLIGHT = (1 << 9),
63 DATA_OUT_URB_INFLIGHT = (1 << 10),
64 COMMAND_COMPLETED = (1 << 11),
67 /* Overrides scsi_pointer */
72 struct urb *data_in_urb;
73 struct urb *data_out_urb;
74 struct list_head list;
77 /* I hate forward declarations, but I actually have a loop */
78 static int uas_submit_urbs(struct scsi_cmnd *cmnd,
79 struct uas_dev_info *devinfo, gfp_t gfp);
80 static void uas_do_work(struct work_struct *work);
82 static DECLARE_WORK(uas_work, uas_do_work);
83 static DEFINE_SPINLOCK(uas_work_lock);
84 static LIST_HEAD(uas_work_list);
86 static void uas_do_work(struct work_struct *work)
88 struct uas_cmd_info *cmdinfo;
89 struct uas_cmd_info *temp;
90 struct list_head list;
93 spin_lock_irq(&uas_work_lock);
94 list_replace_init(&uas_work_list, &list);
95 spin_unlock_irq(&uas_work_lock);
97 list_for_each_entry_safe(cmdinfo, temp, &list, list) {
98 struct scsi_pointer *scp = (void *)cmdinfo;
99 struct scsi_cmnd *cmnd = container_of(scp,
100 struct scsi_cmnd, SCp);
101 err = uas_submit_urbs(cmnd, cmnd->device->hostdata, GFP_NOIO);
103 list_del(&cmdinfo->list);
104 spin_lock_irq(&uas_work_lock);
105 list_add_tail(&cmdinfo->list, &uas_work_list);
106 spin_unlock_irq(&uas_work_lock);
107 schedule_work(&uas_work);
112 static void uas_sense(struct urb *urb, struct scsi_cmnd *cmnd)
114 struct sense_iu *sense_iu = urb->transfer_buffer;
115 struct scsi_device *sdev = cmnd->device;
117 if (urb->actual_length > 16) {
118 unsigned len = be16_to_cpup(&sense_iu->len);
119 if (len + 16 != urb->actual_length) {
120 int newlen = min(len + 16, urb->actual_length) - 16;
123 sdev_printk(KERN_INFO, sdev, "%s: urb length %d "
124 "disagrees with IU sense data length %d, "
125 "using %d bytes of sense data\n", __func__,
126 urb->actual_length, len, newlen);
129 memcpy(cmnd->sense_buffer, sense_iu->sense, len);
132 cmnd->result = sense_iu->status;
135 static void uas_sense_old(struct urb *urb, struct scsi_cmnd *cmnd)
137 struct sense_iu_old *sense_iu = urb->transfer_buffer;
138 struct scsi_device *sdev = cmnd->device;
140 if (urb->actual_length > 8) {
141 unsigned len = be16_to_cpup(&sense_iu->len) - 2;
142 if (len + 8 != urb->actual_length) {
143 int newlen = min(len + 8, urb->actual_length) - 8;
146 sdev_printk(KERN_INFO, sdev, "%s: urb length %d "
147 "disagrees with IU sense data length %d, "
148 "using %d bytes of sense data\n", __func__,
149 urb->actual_length, len, newlen);
152 memcpy(cmnd->sense_buffer, sense_iu->sense, len);
155 cmnd->result = sense_iu->status;
158 static void uas_log_cmd_state(struct scsi_cmnd *cmnd, const char *caller)
160 struct uas_cmd_info *ci = (void *)&cmnd->SCp;
162 scmd_printk(KERN_INFO, cmnd, "%s %p tag %d, inflight:"
163 "%s%s%s%s%s%s%s%s%s%s%s\n",
164 caller, cmnd, cmnd->request->tag,
165 (ci->state & SUBMIT_STATUS_URB) ? " s-st" : "",
166 (ci->state & ALLOC_DATA_IN_URB) ? " a-in" : "",
167 (ci->state & SUBMIT_DATA_IN_URB) ? " s-in" : "",
168 (ci->state & ALLOC_DATA_OUT_URB) ? " a-out" : "",
169 (ci->state & SUBMIT_DATA_OUT_URB) ? " s-out" : "",
170 (ci->state & ALLOC_CMD_URB) ? " a-cmd" : "",
171 (ci->state & SUBMIT_CMD_URB) ? " s-cmd" : "",
172 (ci->state & COMMAND_INFLIGHT) ? " CMD" : "",
173 (ci->state & DATA_IN_URB_INFLIGHT) ? " IN" : "",
174 (ci->state & DATA_OUT_URB_INFLIGHT) ? " OUT" : "",
175 (ci->state & COMMAND_COMPLETED) ? " done" : "");
178 static int uas_try_complete(struct scsi_cmnd *cmnd, const char *caller)
180 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
182 if (cmdinfo->state & (COMMAND_INFLIGHT |
183 DATA_IN_URB_INFLIGHT |
184 DATA_OUT_URB_INFLIGHT))
186 BUG_ON(cmdinfo->state & COMMAND_COMPLETED);
187 cmdinfo->state |= COMMAND_COMPLETED;
188 usb_free_urb(cmdinfo->data_in_urb);
189 usb_free_urb(cmdinfo->data_out_urb);
190 cmnd->scsi_done(cmnd);
194 static void uas_xfer_data(struct urb *urb, struct scsi_cmnd *cmnd,
197 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
200 cmdinfo->state |= direction | SUBMIT_STATUS_URB;
201 err = uas_submit_urbs(cmnd, cmnd->device->hostdata, GFP_ATOMIC);
203 spin_lock(&uas_work_lock);
204 list_add_tail(&cmdinfo->list, &uas_work_list);
205 spin_unlock(&uas_work_lock);
206 schedule_work(&uas_work);
210 static void uas_stat_cmplt(struct urb *urb)
212 struct iu *iu = urb->transfer_buffer;
213 struct Scsi_Host *shost = urb->context;
214 struct uas_dev_info *devinfo = (void *)shost->hostdata[0];
215 struct scsi_cmnd *cmnd;
216 struct uas_cmd_info *cmdinfo;
220 dev_err(&urb->dev->dev, "URB BAD STATUS %d\n", urb->status);
225 tag = be16_to_cpup(&iu->tag) - 1;
227 cmnd = devinfo->cmnd;
229 cmnd = scsi_host_find_tag(shost, tag - 1);
234 cmdinfo = (void *)&cmnd->SCp;
238 if (devinfo->cmnd == cmnd)
239 devinfo->cmnd = NULL;
241 if (urb->actual_length < 16)
242 devinfo->uas_sense_old = 1;
243 if (devinfo->uas_sense_old)
244 uas_sense_old(urb, cmnd);
246 uas_sense(urb, cmnd);
247 if (cmnd->result != 0) {
248 /* cancel data transfers on error */
249 if (cmdinfo->state & DATA_IN_URB_INFLIGHT)
250 usb_unlink_urb(cmdinfo->data_in_urb);
251 if (cmdinfo->state & DATA_OUT_URB_INFLIGHT)
252 usb_unlink_urb(cmdinfo->data_out_urb);
254 cmdinfo->state &= ~COMMAND_INFLIGHT;
255 uas_try_complete(cmnd, __func__);
257 case IU_ID_READ_READY:
258 uas_xfer_data(urb, cmnd, SUBMIT_DATA_IN_URB);
260 case IU_ID_WRITE_READY:
261 uas_xfer_data(urb, cmnd, SUBMIT_DATA_OUT_URB);
264 scmd_printk(KERN_ERR, cmnd,
265 "Bogus IU (%d) received on status pipe\n", iu->iu_id);
270 static void uas_data_cmplt(struct urb *urb)
272 struct scsi_cmnd *cmnd = urb->context;
273 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
274 struct scsi_data_buffer *sdb = NULL;
276 if (cmdinfo->data_in_urb == urb) {
278 cmdinfo->state &= ~DATA_IN_URB_INFLIGHT;
279 } else if (cmdinfo->data_out_urb == urb) {
280 sdb = scsi_out(cmnd);
281 cmdinfo->state &= ~DATA_OUT_URB_INFLIGHT;
285 /* error: no data transfered */
286 sdb->resid = sdb->length;
288 sdb->resid = sdb->length - urb->actual_length;
290 uas_try_complete(cmnd, __func__);
293 static struct urb *uas_alloc_data_urb(struct uas_dev_info *devinfo, gfp_t gfp,
294 unsigned int pipe, u16 stream_id,
295 struct scsi_cmnd *cmnd,
296 enum dma_data_direction dir)
298 struct usb_device *udev = devinfo->udev;
299 struct urb *urb = usb_alloc_urb(0, gfp);
300 struct scsi_data_buffer *sdb = (dir == DMA_FROM_DEVICE)
301 ? scsi_in(cmnd) : scsi_out(cmnd);
305 usb_fill_bulk_urb(urb, udev, pipe, NULL, sdb->length,
306 uas_data_cmplt, cmnd);
307 if (devinfo->use_streams)
308 urb->stream_id = stream_id;
309 urb->num_sgs = udev->bus->sg_tablesize ? sdb->table.nents : 0;
310 urb->sg = sdb->table.sgl;
315 static struct urb *uas_alloc_sense_urb(struct uas_dev_info *devinfo, gfp_t gfp,
316 struct Scsi_Host *shost, u16 stream_id)
318 struct usb_device *udev = devinfo->udev;
319 struct urb *urb = usb_alloc_urb(0, gfp);
325 iu = kzalloc(sizeof(*iu), gfp);
329 usb_fill_bulk_urb(urb, udev, devinfo->status_pipe, iu, sizeof(*iu),
330 uas_stat_cmplt, shost);
331 urb->stream_id = stream_id;
332 urb->transfer_flags |= URB_FREE_BUFFER;
340 static struct urb *uas_alloc_cmd_urb(struct uas_dev_info *devinfo, gfp_t gfp,
341 struct scsi_cmnd *cmnd, u16 stream_id)
343 struct usb_device *udev = devinfo->udev;
344 struct scsi_device *sdev = cmnd->device;
345 struct urb *urb = usb_alloc_urb(0, gfp);
346 struct command_iu *iu;
352 len = cmnd->cmd_len - 16;
356 iu = kzalloc(sizeof(*iu) + len, gfp);
360 iu->iu_id = IU_ID_COMMAND;
361 if (blk_rq_tagged(cmnd->request))
362 iu->tag = cpu_to_be16(cmnd->request->tag + 2);
364 iu->tag = cpu_to_be16(1);
365 iu->prio_attr = UAS_SIMPLE_TAG;
367 int_to_scsilun(sdev->lun, &iu->lun);
368 memcpy(iu->cdb, cmnd->cmnd, cmnd->cmd_len);
370 usb_fill_bulk_urb(urb, udev, devinfo->cmd_pipe, iu, sizeof(*iu) + len,
372 urb->transfer_flags |= URB_FREE_BUFFER;
381 * Why should I request the Status IU before sending the Command IU? Spec
382 * says to, but also says the device may receive them in any order. Seems
386 static int uas_submit_sense_urb(struct Scsi_Host *shost,
387 gfp_t gfp, unsigned int stream)
389 struct uas_dev_info *devinfo = (void *)shost->hostdata[0];
392 urb = uas_alloc_sense_urb(devinfo, gfp, shost, stream);
394 return SCSI_MLQUEUE_DEVICE_BUSY;
395 if (usb_submit_urb(urb, gfp)) {
396 shost_printk(KERN_INFO, shost,
397 "sense urb submission failure\n");
399 return SCSI_MLQUEUE_DEVICE_BUSY;
401 usb_anchor_urb(urb, &devinfo->sense_urbs);
405 static int uas_submit_urbs(struct scsi_cmnd *cmnd,
406 struct uas_dev_info *devinfo, gfp_t gfp)
408 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
411 if (cmdinfo->state & SUBMIT_STATUS_URB) {
412 err = uas_submit_sense_urb(cmnd->device->host, gfp,
417 cmdinfo->state &= ~SUBMIT_STATUS_URB;
420 if (cmdinfo->state & ALLOC_DATA_IN_URB) {
421 cmdinfo->data_in_urb = uas_alloc_data_urb(devinfo, gfp,
422 devinfo->data_in_pipe, cmdinfo->stream,
423 cmnd, DMA_FROM_DEVICE);
424 if (!cmdinfo->data_in_urb)
425 return SCSI_MLQUEUE_DEVICE_BUSY;
426 cmdinfo->state &= ~ALLOC_DATA_IN_URB;
429 if (cmdinfo->state & SUBMIT_DATA_IN_URB) {
430 if (usb_submit_urb(cmdinfo->data_in_urb, gfp)) {
431 scmd_printk(KERN_INFO, cmnd,
432 "data in urb submission failure\n");
433 return SCSI_MLQUEUE_DEVICE_BUSY;
435 cmdinfo->state &= ~SUBMIT_DATA_IN_URB;
436 cmdinfo->state |= DATA_IN_URB_INFLIGHT;
437 usb_anchor_urb(cmdinfo->data_in_urb, &devinfo->data_urbs);
440 if (cmdinfo->state & ALLOC_DATA_OUT_URB) {
441 cmdinfo->data_out_urb = uas_alloc_data_urb(devinfo, gfp,
442 devinfo->data_out_pipe, cmdinfo->stream,
443 cmnd, DMA_TO_DEVICE);
444 if (!cmdinfo->data_out_urb)
445 return SCSI_MLQUEUE_DEVICE_BUSY;
446 cmdinfo->state &= ~ALLOC_DATA_OUT_URB;
449 if (cmdinfo->state & SUBMIT_DATA_OUT_URB) {
450 if (usb_submit_urb(cmdinfo->data_out_urb, gfp)) {
451 scmd_printk(KERN_INFO, cmnd,
452 "data out urb submission failure\n");
453 return SCSI_MLQUEUE_DEVICE_BUSY;
455 cmdinfo->state &= ~SUBMIT_DATA_OUT_URB;
456 cmdinfo->state |= DATA_OUT_URB_INFLIGHT;
457 usb_anchor_urb(cmdinfo->data_out_urb, &devinfo->data_urbs);
460 if (cmdinfo->state & ALLOC_CMD_URB) {
461 cmdinfo->cmd_urb = uas_alloc_cmd_urb(devinfo, gfp, cmnd,
463 if (!cmdinfo->cmd_urb)
464 return SCSI_MLQUEUE_DEVICE_BUSY;
465 cmdinfo->state &= ~ALLOC_CMD_URB;
468 if (cmdinfo->state & SUBMIT_CMD_URB) {
469 if (usb_submit_urb(cmdinfo->cmd_urb, gfp)) {
470 scmd_printk(KERN_INFO, cmnd,
471 "cmd urb submission failure\n");
472 return SCSI_MLQUEUE_DEVICE_BUSY;
474 cmdinfo->state &= ~SUBMIT_CMD_URB;
475 cmdinfo->state |= COMMAND_INFLIGHT;
481 static int uas_queuecommand_lck(struct scsi_cmnd *cmnd,
482 void (*done)(struct scsi_cmnd *))
484 struct scsi_device *sdev = cmnd->device;
485 struct uas_dev_info *devinfo = sdev->hostdata;
486 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
489 BUILD_BUG_ON(sizeof(struct uas_cmd_info) > sizeof(struct scsi_pointer));
492 return SCSI_MLQUEUE_DEVICE_BUSY;
494 if (blk_rq_tagged(cmnd->request)) {
495 cmdinfo->stream = cmnd->request->tag + 2;
497 devinfo->cmnd = cmnd;
501 cmnd->scsi_done = done;
503 cmdinfo->state = SUBMIT_STATUS_URB |
504 ALLOC_CMD_URB | SUBMIT_CMD_URB;
506 switch (cmnd->sc_data_direction) {
507 case DMA_FROM_DEVICE:
508 cmdinfo->state |= ALLOC_DATA_IN_URB | SUBMIT_DATA_IN_URB;
510 case DMA_BIDIRECTIONAL:
511 cmdinfo->state |= ALLOC_DATA_IN_URB | SUBMIT_DATA_IN_URB;
513 cmdinfo->state |= ALLOC_DATA_OUT_URB | SUBMIT_DATA_OUT_URB;
518 if (!devinfo->use_streams) {
519 cmdinfo->state &= ~(SUBMIT_DATA_IN_URB | SUBMIT_DATA_OUT_URB);
523 err = uas_submit_urbs(cmnd, devinfo, GFP_ATOMIC);
525 /* If we did nothing, give up now */
526 if (cmdinfo->state & SUBMIT_STATUS_URB) {
527 return SCSI_MLQUEUE_DEVICE_BUSY;
529 spin_lock(&uas_work_lock);
530 list_add_tail(&cmdinfo->list, &uas_work_list);
531 spin_unlock(&uas_work_lock);
532 schedule_work(&uas_work);
538 static DEF_SCSI_QCMD(uas_queuecommand)
540 static int uas_eh_abort_handler(struct scsi_cmnd *cmnd)
542 uas_log_cmd_state(cmnd, __func__);
544 /* XXX: Send ABORT TASK Task Management command */
548 static int uas_eh_device_reset_handler(struct scsi_cmnd *cmnd)
550 struct scsi_device *sdev = cmnd->device;
551 sdev_printk(KERN_INFO, sdev, "%s tag %d\n", __func__,
554 /* XXX: Send LOGICAL UNIT RESET Task Management command */
558 static int uas_eh_target_reset_handler(struct scsi_cmnd *cmnd)
560 struct scsi_device *sdev = cmnd->device;
561 sdev_printk(KERN_INFO, sdev, "%s tag %d\n", __func__,
564 /* XXX: Can we reset just the one USB interface?
565 * Would calling usb_set_interface() have the right effect?
570 static int uas_eh_bus_reset_handler(struct scsi_cmnd *cmnd)
572 struct scsi_device *sdev = cmnd->device;
573 struct uas_dev_info *devinfo = sdev->hostdata;
574 struct usb_device *udev = devinfo->udev;
576 sdev_printk(KERN_INFO, sdev, "%s tag %d\n", __func__,
579 if (usb_reset_device(udev))
585 static int uas_slave_alloc(struct scsi_device *sdev)
587 sdev->hostdata = (void *)sdev->host->hostdata[0];
591 static int uas_slave_configure(struct scsi_device *sdev)
593 struct uas_dev_info *devinfo = sdev->hostdata;
594 scsi_set_tag_type(sdev, MSG_ORDERED_TAG);
595 scsi_activate_tcq(sdev, devinfo->qdepth - 2);
599 static struct scsi_host_template uas_host_template = {
600 .module = THIS_MODULE,
602 .queuecommand = uas_queuecommand,
603 .slave_alloc = uas_slave_alloc,
604 .slave_configure = uas_slave_configure,
605 .eh_abort_handler = uas_eh_abort_handler,
606 .eh_device_reset_handler = uas_eh_device_reset_handler,
607 .eh_target_reset_handler = uas_eh_target_reset_handler,
608 .eh_bus_reset_handler = uas_eh_bus_reset_handler,
609 .can_queue = 65536, /* Is there a limit on the _host_ ? */
611 .sg_tablesize = SG_NONE,
612 .cmd_per_lun = 1, /* until we override it */
613 .skip_settle_delay = 1,
617 static struct usb_device_id uas_usb_ids[] = {
618 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_BULK) },
619 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_UAS) },
620 /* 0xaa is a prototype device I happen to have access to */
621 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, 0xaa) },
624 MODULE_DEVICE_TABLE(usb, uas_usb_ids);
626 static int uas_is_interface(struct usb_host_interface *intf)
628 return (intf->desc.bInterfaceClass == USB_CLASS_MASS_STORAGE &&
629 intf->desc.bInterfaceSubClass == USB_SC_SCSI &&
630 intf->desc.bInterfaceProtocol == USB_PR_UAS);
633 static int uas_isnt_supported(struct usb_device *udev)
635 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
637 dev_warn(&udev->dev, "The driver for the USB controller %s does not "
638 "support scatter-gather which is\n",
639 hcd->driver->description);
640 dev_warn(&udev->dev, "required by the UAS driver. Please try an"
641 "alternative USB controller if you wish to use UAS.\n");
645 static int uas_switch_interface(struct usb_device *udev,
646 struct usb_interface *intf)
649 int sg_supported = udev->bus->sg_tablesize != 0;
651 for (i = 0; i < intf->num_altsetting; i++) {
652 struct usb_host_interface *alt = &intf->altsetting[i];
654 if (uas_is_interface(alt)) {
656 return uas_isnt_supported(udev);
657 return usb_set_interface(udev,
658 alt->desc.bInterfaceNumber,
659 alt->desc.bAlternateSetting);
666 static void uas_configure_endpoints(struct uas_dev_info *devinfo)
668 struct usb_host_endpoint *eps[4] = { };
669 struct usb_interface *intf = devinfo->intf;
670 struct usb_device *udev = devinfo->udev;
671 struct usb_host_endpoint *endpoint = intf->cur_altsetting->endpoint;
672 unsigned i, n_endpoints = intf->cur_altsetting->desc.bNumEndpoints;
674 devinfo->uas_sense_old = 0;
675 devinfo->cmnd = NULL;
677 for (i = 0; i < n_endpoints; i++) {
678 unsigned char *extra = endpoint[i].extra;
679 int len = endpoint[i].extralen;
681 if (extra[1] == USB_DT_PIPE_USAGE) {
682 unsigned pipe_id = extra[2];
683 if (pipe_id > 0 && pipe_id < 5)
684 eps[pipe_id - 1] = &endpoint[i];
693 * Assume that if we didn't find a control pipe descriptor, we're
694 * using a device with old firmware that happens to be set up like
698 devinfo->cmd_pipe = usb_sndbulkpipe(udev, 1);
699 devinfo->status_pipe = usb_rcvbulkpipe(udev, 1);
700 devinfo->data_in_pipe = usb_rcvbulkpipe(udev, 2);
701 devinfo->data_out_pipe = usb_sndbulkpipe(udev, 2);
703 eps[1] = usb_pipe_endpoint(udev, devinfo->status_pipe);
704 eps[2] = usb_pipe_endpoint(udev, devinfo->data_in_pipe);
705 eps[3] = usb_pipe_endpoint(udev, devinfo->data_out_pipe);
707 devinfo->cmd_pipe = usb_sndbulkpipe(udev,
708 eps[0]->desc.bEndpointAddress);
709 devinfo->status_pipe = usb_rcvbulkpipe(udev,
710 eps[1]->desc.bEndpointAddress);
711 devinfo->data_in_pipe = usb_rcvbulkpipe(udev,
712 eps[2]->desc.bEndpointAddress);
713 devinfo->data_out_pipe = usb_sndbulkpipe(udev,
714 eps[3]->desc.bEndpointAddress);
717 devinfo->qdepth = usb_alloc_streams(devinfo->intf, eps + 1, 3, 256,
719 if (devinfo->qdepth < 0) {
720 devinfo->qdepth = 256;
721 devinfo->use_streams = 0;
723 devinfo->use_streams = 1;
727 static void uas_free_streams(struct uas_dev_info *devinfo)
729 struct usb_device *udev = devinfo->udev;
730 struct usb_host_endpoint *eps[3];
732 eps[0] = usb_pipe_endpoint(udev, devinfo->status_pipe);
733 eps[1] = usb_pipe_endpoint(udev, devinfo->data_in_pipe);
734 eps[2] = usb_pipe_endpoint(udev, devinfo->data_out_pipe);
735 usb_free_streams(devinfo->intf, eps, 3, GFP_KERNEL);
739 * XXX: What I'd like to do here is register a SCSI host for each USB host in
740 * the system. Follow usb-storage's design of registering a SCSI host for
741 * each USB device for the moment. Can implement this by walking up the
742 * USB hierarchy until we find a USB host.
744 static int uas_probe(struct usb_interface *intf, const struct usb_device_id *id)
747 struct Scsi_Host *shost;
748 struct uas_dev_info *devinfo;
749 struct usb_device *udev = interface_to_usbdev(intf);
751 if (uas_switch_interface(udev, intf))
754 devinfo = kmalloc(sizeof(struct uas_dev_info), GFP_KERNEL);
759 shost = scsi_host_alloc(&uas_host_template, sizeof(void *));
763 shost->max_cmd_len = 16 + 252;
765 shost->sg_tablesize = udev->bus->sg_tablesize;
767 devinfo->intf = intf;
768 devinfo->udev = udev;
769 init_usb_anchor(&devinfo->sense_urbs);
770 init_usb_anchor(&devinfo->data_urbs);
771 uas_configure_endpoints(devinfo);
773 result = scsi_init_shared_tag_map(shost, devinfo->qdepth - 2);
777 result = scsi_add_host(shost, &intf->dev);
781 shost->hostdata[0] = (unsigned long)devinfo;
783 scsi_scan_host(shost);
784 usb_set_intfdata(intf, shost);
788 uas_free_streams(devinfo);
792 scsi_host_put(shost);
796 static int uas_pre_reset(struct usb_interface *intf)
798 /* XXX: Need to return 1 if it's not our device in error handling */
802 static int uas_post_reset(struct usb_interface *intf)
804 /* XXX: Need to return 1 if it's not our device in error handling */
808 static void uas_disconnect(struct usb_interface *intf)
810 struct Scsi_Host *shost = usb_get_intfdata(intf);
811 struct uas_dev_info *devinfo = (void *)shost->hostdata[0];
813 scsi_remove_host(shost);
814 usb_kill_anchored_urbs(&devinfo->sense_urbs);
815 usb_kill_anchored_urbs(&devinfo->data_urbs);
816 uas_free_streams(devinfo);
821 * XXX: Should this plug into libusual so we can auto-upgrade devices from
824 static struct usb_driver uas_driver = {
827 .disconnect = uas_disconnect,
828 .pre_reset = uas_pre_reset,
829 .post_reset = uas_post_reset,
830 .id_table = uas_usb_ids,
833 module_usb_driver(uas_driver);
835 MODULE_LICENSE("GPL");
836 MODULE_AUTHOR("Matthew Wilcox and Sarah Sharp");