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
;
45 unsigned cmd_pipe
, status_pipe
, data_in_pipe
, data_out_pipe
;
46 unsigned use_streams
:1;
47 unsigned uas_sense_old
:1;
48 struct scsi_cmnd
*cmnd
;
49 struct urb
*status_urb
; /* used only if stream support is available */
53 ALLOC_STATUS_URB
= (1 << 0),
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 COMPLETED_DATA_IN
= (1 << 8),
62 COMPLETED_DATA_OUT
= (1 << 9),
63 DATA_COMPLETES_CMD
= (1 << 10),
66 /* Overrides scsi_pointer */
71 /* status_urb is used only if stream support isn't available */
72 struct urb
*status_urb
;
73 struct urb
*data_in_urb
;
74 struct urb
*data_out_urb
;
75 struct list_head list
;
78 /* I hate forward declarations, but I actually have a loop */
79 static int uas_submit_urbs(struct scsi_cmnd
*cmnd
,
80 struct uas_dev_info
*devinfo
, gfp_t gfp
);
81 static void uas_do_work(struct work_struct
*work
);
83 static DECLARE_WORK(uas_work
, uas_do_work
);
84 static DEFINE_SPINLOCK(uas_work_lock
);
85 static LIST_HEAD(uas_work_list
);
87 static void uas_do_work(struct work_struct
*work
)
89 struct uas_cmd_info
*cmdinfo
;
90 struct uas_cmd_info
*temp
;
91 struct list_head list
;
94 spin_lock_irq(&uas_work_lock
);
95 list_replace_init(&uas_work_list
, &list
);
96 spin_unlock_irq(&uas_work_lock
);
98 list_for_each_entry_safe(cmdinfo
, temp
, &list
, list
) {
99 struct scsi_pointer
*scp
= (void *)cmdinfo
;
100 struct scsi_cmnd
*cmnd
= container_of(scp
,
101 struct scsi_cmnd
, SCp
);
102 err
= uas_submit_urbs(cmnd
, cmnd
->device
->hostdata
, GFP_NOIO
);
104 list_del(&cmdinfo
->list
);
105 spin_lock_irq(&uas_work_lock
);
106 list_add_tail(&cmdinfo
->list
, &uas_work_list
);
107 spin_unlock_irq(&uas_work_lock
);
108 schedule_work(&uas_work
);
113 static void uas_sense(struct urb
*urb
, struct scsi_cmnd
*cmnd
)
115 struct sense_iu
*sense_iu
= urb
->transfer_buffer
;
116 struct scsi_device
*sdev
= cmnd
->device
;
117 struct uas_cmd_info
*cmdinfo
= (void *)&cmnd
->SCp
;
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
;
135 if (!(cmdinfo
->state
& DATA_COMPLETES_CMD
))
136 cmnd
->scsi_done(cmnd
);
139 static void uas_sense_old(struct urb
*urb
, struct scsi_cmnd
*cmnd
)
141 struct sense_iu_old
*sense_iu
= urb
->transfer_buffer
;
142 struct scsi_device
*sdev
= cmnd
->device
;
143 struct uas_cmd_info
*cmdinfo
= (void *)&cmnd
->SCp
;
145 if (urb
->actual_length
> 8) {
146 unsigned len
= be16_to_cpup(&sense_iu
->len
) - 2;
147 if (len
+ 8 != urb
->actual_length
) {
148 int newlen
= min(len
+ 8, urb
->actual_length
) - 8;
151 sdev_printk(KERN_INFO
, sdev
, "%s: urb length %d "
152 "disagrees with IU sense data length %d, "
153 "using %d bytes of sense data\n", __func__
,
154 urb
->actual_length
, len
, newlen
);
157 memcpy(cmnd
->sense_buffer
, sense_iu
->sense
, len
);
160 cmnd
->result
= sense_iu
->status
;
161 if (!(cmdinfo
->state
& DATA_COMPLETES_CMD
))
162 cmnd
->scsi_done(cmnd
);
165 static void uas_xfer_data(struct urb
*urb
, struct scsi_cmnd
*cmnd
,
168 struct uas_cmd_info
*cmdinfo
= (void *)&cmnd
->SCp
;
171 cmdinfo
->state
= direction
;
172 err
= uas_submit_urbs(cmnd
, cmnd
->device
->hostdata
, GFP_ATOMIC
);
174 spin_lock(&uas_work_lock
);
175 list_add_tail(&cmdinfo
->list
, &uas_work_list
);
176 spin_unlock(&uas_work_lock
);
177 schedule_work(&uas_work
);
181 static void uas_stat_cmplt(struct urb
*urb
)
183 struct iu
*iu
= urb
->transfer_buffer
;
184 struct Scsi_Host
*shost
= urb
->context
;
185 struct uas_dev_info
*devinfo
= (void *)shost
->hostdata
[0];
186 struct scsi_cmnd
*cmnd
;
187 struct uas_cmd_info
*cmdinfo
;
192 dev_err(&urb
->dev
->dev
, "URB BAD STATUS %d\n", urb
->status
);
193 if (devinfo
->use_streams
)
198 tag
= be16_to_cpup(&iu
->tag
) - 1;
200 cmnd
= devinfo
->cmnd
;
202 cmnd
= scsi_host_find_tag(shost
, tag
- 1);
204 if (devinfo
->use_streams
) {
208 ret
= usb_submit_urb(urb
, GFP_ATOMIC
);
210 dev_err(&urb
->dev
->dev
, "failed submit status urb\n");
213 cmdinfo
= (void *)&cmnd
->SCp
;
217 if (devinfo
->cmnd
== cmnd
)
218 devinfo
->cmnd
= NULL
;
220 if (!(cmdinfo
->state
& COMPLETED_DATA_IN
) &&
221 cmdinfo
->data_in_urb
) {
222 if (devinfo
->use_streams
) {
223 cmdinfo
->state
|= DATA_COMPLETES_CMD
;
224 usb_unlink_urb(cmdinfo
->data_in_urb
);
226 usb_free_urb(cmdinfo
->data_in_urb
);
229 if (!(cmdinfo
->state
& COMPLETED_DATA_OUT
) &&
230 cmdinfo
->data_out_urb
) {
231 if (devinfo
->use_streams
) {
232 cmdinfo
->state
|= DATA_COMPLETES_CMD
;
233 usb_unlink_urb(cmdinfo
->data_in_urb
);
235 usb_free_urb(cmdinfo
->data_out_urb
);
239 if (urb
->actual_length
< 16)
240 devinfo
->uas_sense_old
= 1;
241 if (devinfo
->uas_sense_old
)
242 uas_sense_old(urb
, cmnd
);
244 uas_sense(urb
, cmnd
);
246 case IU_ID_READ_READY
:
247 uas_xfer_data(urb
, cmnd
, SUBMIT_DATA_IN_URB
);
249 case IU_ID_WRITE_READY
:
250 uas_xfer_data(urb
, cmnd
, SUBMIT_DATA_OUT_URB
);
253 scmd_printk(KERN_ERR
, cmnd
,
254 "Bogus IU (%d) received on status pipe\n", iu
->iu_id
);
257 if (devinfo
->use_streams
) {
262 ret
= usb_submit_urb(urb
, GFP_ATOMIC
);
264 dev_err(&urb
->dev
->dev
, "failed submit status urb\n");
267 static void uas_data_out_cmplt(struct urb
*urb
)
269 struct scsi_cmnd
*cmnd
= urb
->context
;
270 struct scsi_data_buffer
*sdb
= scsi_out(cmnd
);
271 struct uas_cmd_info
*cmdinfo
= (void *)&cmnd
->SCp
;
273 cmdinfo
->state
|= COMPLETED_DATA_OUT
;
275 sdb
->resid
= sdb
->length
- urb
->actual_length
;
278 if (cmdinfo
->state
& DATA_COMPLETES_CMD
)
279 cmnd
->scsi_done(cmnd
);
282 static void uas_data_in_cmplt(struct urb
*urb
)
284 struct scsi_cmnd
*cmnd
= urb
->context
;
285 struct scsi_data_buffer
*sdb
= scsi_in(cmnd
);
286 struct uas_cmd_info
*cmdinfo
= (void *)&cmnd
->SCp
;
288 cmdinfo
->state
|= COMPLETED_DATA_IN
;
290 sdb
->resid
= sdb
->length
- urb
->actual_length
;
293 if (cmdinfo
->state
& DATA_COMPLETES_CMD
)
294 cmnd
->scsi_done(cmnd
);
297 static struct urb
*uas_alloc_data_urb(struct uas_dev_info
*devinfo
, gfp_t gfp
,
298 unsigned int pipe
, struct scsi_cmnd
*cmnd
,
299 enum dma_data_direction dir
)
301 struct uas_cmd_info
*cmdinfo
= (void *)&cmnd
->SCp
;
302 struct usb_device
*udev
= devinfo
->udev
;
303 struct urb
*urb
= usb_alloc_urb(0, gfp
);
304 struct scsi_data_buffer
*sdb
;
305 usb_complete_t complete_fn
;
306 u16 stream_id
= cmdinfo
->stream
;
310 if (dir
== DMA_FROM_DEVICE
) {
312 complete_fn
= uas_data_in_cmplt
;
314 sdb
= scsi_out(cmnd
);
315 complete_fn
= uas_data_out_cmplt
;
317 usb_fill_bulk_urb(urb
, udev
, pipe
, NULL
, sdb
->length
,
319 urb
->stream_id
= stream_id
;
320 urb
->num_sgs
= udev
->bus
->sg_tablesize
? sdb
->table
.nents
: 0;
321 urb
->sg
= sdb
->table
.sgl
;
326 static struct urb
*uas_alloc_sense_urb(struct uas_dev_info
*devinfo
, gfp_t gfp
,
327 struct Scsi_Host
*shost
, u16 stream_id
)
329 struct usb_device
*udev
= devinfo
->udev
;
330 struct urb
*urb
= usb_alloc_urb(0, gfp
);
336 iu
= kzalloc(sizeof(*iu
), gfp
);
340 usb_fill_bulk_urb(urb
, udev
, devinfo
->status_pipe
, iu
, sizeof(*iu
),
341 uas_stat_cmplt
, shost
);
342 urb
->stream_id
= stream_id
;
343 urb
->transfer_flags
|= URB_FREE_BUFFER
;
351 static struct urb
*uas_alloc_cmd_urb(struct uas_dev_info
*devinfo
, gfp_t gfp
,
352 struct scsi_cmnd
*cmnd
, u16 stream_id
)
354 struct usb_device
*udev
= devinfo
->udev
;
355 struct scsi_device
*sdev
= cmnd
->device
;
356 struct urb
*urb
= usb_alloc_urb(0, gfp
);
357 struct command_iu
*iu
;
363 len
= cmnd
->cmd_len
- 16;
367 iu
= kzalloc(sizeof(*iu
) + len
, gfp
);
371 iu
->iu_id
= IU_ID_COMMAND
;
372 if (blk_rq_tagged(cmnd
->request
))
373 iu
->tag
= cpu_to_be16(cmnd
->request
->tag
+ 2);
375 iu
->tag
= cpu_to_be16(1);
376 iu
->prio_attr
= UAS_SIMPLE_TAG
;
378 int_to_scsilun(sdev
->lun
, &iu
->lun
);
379 memcpy(iu
->cdb
, cmnd
->cmnd
, cmnd
->cmd_len
);
381 usb_fill_bulk_urb(urb
, udev
, devinfo
->cmd_pipe
, iu
, sizeof(*iu
) + len
,
383 urb
->transfer_flags
|= URB_FREE_BUFFER
;
392 * Why should I request the Status IU before sending the Command IU? Spec
393 * says to, but also says the device may receive them in any order. Seems
397 static int uas_submit_urbs(struct scsi_cmnd
*cmnd
,
398 struct uas_dev_info
*devinfo
, gfp_t gfp
)
400 struct uas_cmd_info
*cmdinfo
= (void *)&cmnd
->SCp
;
402 if (cmdinfo
->state
& ALLOC_STATUS_URB
) {
403 cmdinfo
->status_urb
= uas_alloc_sense_urb(devinfo
, gfp
,
404 cmnd
->device
->host
, cmdinfo
->stream
);
405 if (!cmdinfo
->status_urb
)
406 return SCSI_MLQUEUE_DEVICE_BUSY
;
407 cmdinfo
->state
&= ~ALLOC_STATUS_URB
;
410 if (cmdinfo
->state
& SUBMIT_STATUS_URB
) {
411 if (usb_submit_urb(cmdinfo
->status_urb
, gfp
)) {
412 scmd_printk(KERN_INFO
, cmnd
,
413 "sense urb submission failure\n");
414 return SCSI_MLQUEUE_DEVICE_BUSY
;
416 cmdinfo
->state
&= ~SUBMIT_STATUS_URB
;
419 if (cmdinfo
->state
& ALLOC_DATA_IN_URB
) {
420 cmdinfo
->data_in_urb
= uas_alloc_data_urb(devinfo
, gfp
,
421 devinfo
->data_in_pipe
, cmnd
,
423 if (!cmdinfo
->data_in_urb
)
424 return SCSI_MLQUEUE_DEVICE_BUSY
;
425 cmdinfo
->state
&= ~ALLOC_DATA_IN_URB
;
428 if (cmdinfo
->state
& SUBMIT_DATA_IN_URB
) {
429 if (usb_submit_urb(cmdinfo
->data_in_urb
, gfp
)) {
430 scmd_printk(KERN_INFO
, cmnd
,
431 "data in urb submission failure\n");
432 return SCSI_MLQUEUE_DEVICE_BUSY
;
434 cmdinfo
->state
&= ~SUBMIT_DATA_IN_URB
;
437 if (cmdinfo
->state
& ALLOC_DATA_OUT_URB
) {
438 cmdinfo
->data_out_urb
= uas_alloc_data_urb(devinfo
, gfp
,
439 devinfo
->data_out_pipe
, cmnd
,
441 if (!cmdinfo
->data_out_urb
)
442 return SCSI_MLQUEUE_DEVICE_BUSY
;
443 cmdinfo
->state
&= ~ALLOC_DATA_OUT_URB
;
446 if (cmdinfo
->state
& SUBMIT_DATA_OUT_URB
) {
447 if (usb_submit_urb(cmdinfo
->data_out_urb
, gfp
)) {
448 scmd_printk(KERN_INFO
, cmnd
,
449 "data out urb submission failure\n");
450 return SCSI_MLQUEUE_DEVICE_BUSY
;
452 cmdinfo
->state
&= ~SUBMIT_DATA_OUT_URB
;
455 if (cmdinfo
->state
& ALLOC_CMD_URB
) {
456 cmdinfo
->cmd_urb
= uas_alloc_cmd_urb(devinfo
, gfp
, cmnd
,
458 if (!cmdinfo
->cmd_urb
)
459 return SCSI_MLQUEUE_DEVICE_BUSY
;
460 cmdinfo
->state
&= ~ALLOC_CMD_URB
;
463 if (cmdinfo
->state
& SUBMIT_CMD_URB
) {
464 if (usb_submit_urb(cmdinfo
->cmd_urb
, gfp
)) {
465 scmd_printk(KERN_INFO
, cmnd
,
466 "cmd urb submission failure\n");
467 return SCSI_MLQUEUE_DEVICE_BUSY
;
469 cmdinfo
->state
&= ~SUBMIT_CMD_URB
;
475 static int uas_queuecommand_lck(struct scsi_cmnd
*cmnd
,
476 void (*done
)(struct scsi_cmnd
*))
478 struct scsi_device
*sdev
= cmnd
->device
;
479 struct uas_dev_info
*devinfo
= sdev
->hostdata
;
480 struct uas_cmd_info
*cmdinfo
= (void *)&cmnd
->SCp
;
483 BUILD_BUG_ON(sizeof(struct uas_cmd_info
) > sizeof(struct scsi_pointer
));
486 return SCSI_MLQUEUE_DEVICE_BUSY
;
488 if (blk_rq_tagged(cmnd
->request
)) {
489 cmdinfo
->stream
= cmnd
->request
->tag
+ 2;
491 devinfo
->cmnd
= cmnd
;
495 cmnd
->scsi_done
= done
;
497 cmdinfo
->state
= ALLOC_STATUS_URB
| SUBMIT_STATUS_URB
|
498 ALLOC_CMD_URB
| SUBMIT_CMD_URB
;
500 switch (cmnd
->sc_data_direction
) {
501 case DMA_FROM_DEVICE
:
502 cmdinfo
->state
|= ALLOC_DATA_IN_URB
| SUBMIT_DATA_IN_URB
;
504 case DMA_BIDIRECTIONAL
:
505 cmdinfo
->state
|= ALLOC_DATA_IN_URB
| SUBMIT_DATA_IN_URB
;
507 cmdinfo
->state
|= ALLOC_DATA_OUT_URB
| SUBMIT_DATA_OUT_URB
;
512 if (!devinfo
->use_streams
) {
513 cmdinfo
->state
&= ~(SUBMIT_DATA_IN_URB
| SUBMIT_DATA_OUT_URB
|
514 ALLOC_STATUS_URB
| SUBMIT_STATUS_URB
);
518 err
= uas_submit_urbs(cmnd
, devinfo
, GFP_ATOMIC
);
520 /* If we did nothing, give up now */
521 if (cmdinfo
->state
& SUBMIT_STATUS_URB
) {
522 usb_free_urb(cmdinfo
->status_urb
);
523 return SCSI_MLQUEUE_DEVICE_BUSY
;
525 spin_lock(&uas_work_lock
);
526 list_add_tail(&cmdinfo
->list
, &uas_work_list
);
527 spin_unlock(&uas_work_lock
);
528 schedule_work(&uas_work
);
534 static DEF_SCSI_QCMD(uas_queuecommand
)
536 static int uas_eh_abort_handler(struct scsi_cmnd
*cmnd
)
538 struct scsi_device
*sdev
= cmnd
->device
;
539 sdev_printk(KERN_INFO
, sdev
, "%s tag %d\n", __func__
,
542 /* XXX: Send ABORT TASK Task Management command */
546 static int uas_eh_device_reset_handler(struct scsi_cmnd
*cmnd
)
548 struct scsi_device
*sdev
= cmnd
->device
;
549 sdev_printk(KERN_INFO
, sdev
, "%s tag %d\n", __func__
,
552 /* XXX: Send LOGICAL UNIT RESET Task Management command */
556 static int uas_eh_target_reset_handler(struct scsi_cmnd
*cmnd
)
558 struct scsi_device
*sdev
= cmnd
->device
;
559 sdev_printk(KERN_INFO
, sdev
, "%s tag %d\n", __func__
,
562 /* XXX: Can we reset just the one USB interface?
563 * Would calling usb_set_interface() have the right effect?
568 static int uas_eh_bus_reset_handler(struct scsi_cmnd
*cmnd
)
570 struct scsi_device
*sdev
= cmnd
->device
;
571 struct uas_dev_info
*devinfo
= sdev
->hostdata
;
572 struct usb_device
*udev
= devinfo
->udev
;
574 sdev_printk(KERN_INFO
, sdev
, "%s tag %d\n", __func__
,
577 if (usb_reset_device(udev
))
583 static int uas_slave_alloc(struct scsi_device
*sdev
)
585 sdev
->hostdata
= (void *)sdev
->host
->hostdata
[0];
589 static int uas_slave_configure(struct scsi_device
*sdev
)
591 struct uas_dev_info
*devinfo
= sdev
->hostdata
;
592 scsi_set_tag_type(sdev
, MSG_ORDERED_TAG
);
593 scsi_activate_tcq(sdev
, devinfo
->qdepth
- 2);
597 static struct scsi_host_template uas_host_template
= {
598 .module
= THIS_MODULE
,
600 .queuecommand
= uas_queuecommand
,
601 .slave_alloc
= uas_slave_alloc
,
602 .slave_configure
= uas_slave_configure
,
603 .eh_abort_handler
= uas_eh_abort_handler
,
604 .eh_device_reset_handler
= uas_eh_device_reset_handler
,
605 .eh_target_reset_handler
= uas_eh_target_reset_handler
,
606 .eh_bus_reset_handler
= uas_eh_bus_reset_handler
,
607 .can_queue
= 65536, /* Is there a limit on the _host_ ? */
609 .sg_tablesize
= SG_NONE
,
610 .cmd_per_lun
= 1, /* until we override it */
611 .skip_settle_delay
= 1,
615 static struct usb_device_id uas_usb_ids
[] = {
616 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE
, USB_SC_SCSI
, USB_PR_BULK
) },
617 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE
, USB_SC_SCSI
, USB_PR_UAS
) },
618 /* 0xaa is a prototype device I happen to have access to */
619 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE
, USB_SC_SCSI
, 0xaa) },
622 MODULE_DEVICE_TABLE(usb
, uas_usb_ids
);
624 static int uas_is_interface(struct usb_host_interface
*intf
)
626 return (intf
->desc
.bInterfaceClass
== USB_CLASS_MASS_STORAGE
&&
627 intf
->desc
.bInterfaceSubClass
== USB_SC_SCSI
&&
628 intf
->desc
.bInterfaceProtocol
== USB_PR_UAS
);
631 static int uas_isnt_supported(struct usb_device
*udev
)
633 struct usb_hcd
*hcd
= bus_to_hcd(udev
->bus
);
635 dev_warn(&udev
->dev
, "The driver for the USB controller %s does not "
636 "support scatter-gather which is\n",
637 hcd
->driver
->description
);
638 dev_warn(&udev
->dev
, "required by the UAS driver. Please try an"
639 "alternative USB controller if you wish to use UAS.\n");
643 static int uas_switch_interface(struct usb_device
*udev
,
644 struct usb_interface
*intf
)
647 int sg_supported
= udev
->bus
->sg_tablesize
!= 0;
649 for (i
= 0; i
< intf
->num_altsetting
; i
++) {
650 struct usb_host_interface
*alt
= &intf
->altsetting
[i
];
652 if (uas_is_interface(alt
)) {
654 return uas_isnt_supported(udev
);
655 return usb_set_interface(udev
,
656 alt
->desc
.bInterfaceNumber
,
657 alt
->desc
.bAlternateSetting
);
664 static void uas_configure_endpoints(struct uas_dev_info
*devinfo
)
666 struct usb_host_endpoint
*eps
[4] = { };
667 struct usb_interface
*intf
= devinfo
->intf
;
668 struct usb_device
*udev
= devinfo
->udev
;
669 struct usb_host_endpoint
*endpoint
= intf
->cur_altsetting
->endpoint
;
670 unsigned i
, n_endpoints
= intf
->cur_altsetting
->desc
.bNumEndpoints
;
672 devinfo
->uas_sense_old
= 0;
673 devinfo
->cmnd
= NULL
;
675 for (i
= 0; i
< n_endpoints
; i
++) {
676 unsigned char *extra
= endpoint
[i
].extra
;
677 int len
= endpoint
[i
].extralen
;
679 if (extra
[1] == USB_DT_PIPE_USAGE
) {
680 unsigned pipe_id
= extra
[2];
681 if (pipe_id
> 0 && pipe_id
< 5)
682 eps
[pipe_id
- 1] = &endpoint
[i
];
691 * Assume that if we didn't find a control pipe descriptor, we're
692 * using a device with old firmware that happens to be set up like
696 devinfo
->cmd_pipe
= usb_sndbulkpipe(udev
, 1);
697 devinfo
->status_pipe
= usb_rcvbulkpipe(udev
, 1);
698 devinfo
->data_in_pipe
= usb_rcvbulkpipe(udev
, 2);
699 devinfo
->data_out_pipe
= usb_sndbulkpipe(udev
, 2);
701 eps
[1] = usb_pipe_endpoint(udev
, devinfo
->status_pipe
);
702 eps
[2] = usb_pipe_endpoint(udev
, devinfo
->data_in_pipe
);
703 eps
[3] = usb_pipe_endpoint(udev
, devinfo
->data_out_pipe
);
705 devinfo
->cmd_pipe
= usb_sndbulkpipe(udev
,
706 eps
[0]->desc
.bEndpointAddress
);
707 devinfo
->status_pipe
= usb_rcvbulkpipe(udev
,
708 eps
[1]->desc
.bEndpointAddress
);
709 devinfo
->data_in_pipe
= usb_rcvbulkpipe(udev
,
710 eps
[2]->desc
.bEndpointAddress
);
711 devinfo
->data_out_pipe
= usb_sndbulkpipe(udev
,
712 eps
[3]->desc
.bEndpointAddress
);
715 devinfo
->qdepth
= usb_alloc_streams(devinfo
->intf
, eps
+ 1, 3, 256,
717 if (devinfo
->qdepth
< 0) {
718 devinfo
->qdepth
= 256;
719 devinfo
->use_streams
= 0;
721 devinfo
->use_streams
= 1;
725 static int uas_alloc_status_urb(struct uas_dev_info
*devinfo
,
726 struct Scsi_Host
*shost
)
728 if (devinfo
->use_streams
) {
729 devinfo
->status_urb
= NULL
;
733 devinfo
->status_urb
= uas_alloc_sense_urb(devinfo
, GFP_KERNEL
,
735 if (!devinfo
->status_urb
)
738 if (usb_submit_urb(devinfo
->status_urb
, GFP_KERNEL
))
743 usb_free_urb(devinfo
->status_urb
);
748 static void uas_free_streams(struct uas_dev_info
*devinfo
)
750 struct usb_device
*udev
= devinfo
->udev
;
751 struct usb_host_endpoint
*eps
[3];
753 eps
[0] = usb_pipe_endpoint(udev
, devinfo
->status_pipe
);
754 eps
[1] = usb_pipe_endpoint(udev
, devinfo
->data_in_pipe
);
755 eps
[2] = usb_pipe_endpoint(udev
, devinfo
->data_out_pipe
);
756 usb_free_streams(devinfo
->intf
, eps
, 3, GFP_KERNEL
);
760 * XXX: What I'd like to do here is register a SCSI host for each USB host in
761 * the system. Follow usb-storage's design of registering a SCSI host for
762 * each USB device for the moment. Can implement this by walking up the
763 * USB hierarchy until we find a USB host.
765 static int uas_probe(struct usb_interface
*intf
, const struct usb_device_id
*id
)
768 struct Scsi_Host
*shost
;
769 struct uas_dev_info
*devinfo
;
770 struct usb_device
*udev
= interface_to_usbdev(intf
);
772 if (uas_switch_interface(udev
, intf
))
775 devinfo
= kmalloc(sizeof(struct uas_dev_info
), GFP_KERNEL
);
780 shost
= scsi_host_alloc(&uas_host_template
, sizeof(void *));
784 shost
->max_cmd_len
= 16 + 252;
786 shost
->sg_tablesize
= udev
->bus
->sg_tablesize
;
788 devinfo
->intf
= intf
;
789 devinfo
->udev
= udev
;
790 uas_configure_endpoints(devinfo
);
792 result
= scsi_init_shared_tag_map(shost
, devinfo
->qdepth
- 2);
796 result
= scsi_add_host(shost
, &intf
->dev
);
800 shost
->hostdata
[0] = (unsigned long)devinfo
;
802 result
= uas_alloc_status_urb(devinfo
, shost
);
804 goto err_alloc_status
;
806 scsi_scan_host(shost
);
807 usb_set_intfdata(intf
, shost
);
811 scsi_remove_host(shost
);
814 uas_free_streams(devinfo
);
818 scsi_host_put(shost
);
822 static int uas_pre_reset(struct usb_interface
*intf
)
824 /* XXX: Need to return 1 if it's not our device in error handling */
828 static int uas_post_reset(struct usb_interface
*intf
)
830 /* XXX: Need to return 1 if it's not our device in error handling */
834 static void uas_disconnect(struct usb_interface
*intf
)
836 struct Scsi_Host
*shost
= usb_get_intfdata(intf
);
837 struct uas_dev_info
*devinfo
= (void *)shost
->hostdata
[0];
839 scsi_remove_host(shost
);
840 usb_kill_urb(devinfo
->status_urb
);
841 usb_free_urb(devinfo
->status_urb
);
842 uas_free_streams(devinfo
);
847 * XXX: Should this plug into libusual so we can auto-upgrade devices from
850 static struct usb_driver uas_driver
= {
853 .disconnect
= uas_disconnect
,
854 .pre_reset
= uas_pre_reset
,
855 .post_reset
= uas_post_reset
,
856 .id_table
= uas_usb_ids
,
859 module_usb_driver(uas_driver
);
861 MODULE_LICENSE("GPL");
862 MODULE_AUTHOR("Matthew Wilcox and Sarah Sharp");