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