]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/block/ub.c
[PATCH] USB: ub 1/3: Axboe's quasi-S/G
[linux-2.6-omap-h63xx.git] / drivers / block / ub.c
1 /*
2  * The low performance USB storage driver (ub).
3  *
4  * Copyright (c) 1999, 2000 Matthew Dharm (mdharm-usb@one-eyed-alien.net)
5  * Copyright (C) 2004 Pete Zaitcev (zaitcev@yahoo.com)
6  *
7  * This work is a part of Linux kernel, is derived from it,
8  * and is not licensed separately. See file COPYING for details.
9  *
10  * TODO (sorted by decreasing priority)
11  *  -- Kill first_open (Al Viro fixed the block layer now)
12  *  -- Do resets with usb_device_reset (needs a thread context, use khubd)
13  *  -- set readonly flag for CDs, set removable flag for CF readers
14  *  -- do inquiry and verify we got a disk and not a tape (for LUN mismatch)
15  *  -- special case some senses, e.g. 3a/0 -> no media present, reduce retries
16  *  -- verify the 13 conditions and do bulk resets
17  *  -- kill last_pipe and simply do two-state clearing on both pipes
18  *  -- verify protocol (bulk) from USB descriptors (maybe...)
19  *  -- highmem
20  *  -- move top_sense and work_bcs into separate allocations (if they survive)
21  *     for cache purists and esoteric architectures.
22  *  -- Allocate structure for LUN 0 before the first ub_sync_tur, avoid NULL. ?
23  *  -- prune comments, they are too volumnous
24  *  -- Exterminate P3 printks
25  *  -- Resove XXX's
26  *  -- Redo "benh's retries", perhaps have spin-up code to handle them. V:D=?
27  *  -- CLEAR, CLR2STS, CLRRS seem to be ripe for refactoring.
28  */
29 #include <linux/kernel.h>
30 #include <linux/module.h>
31 #include <linux/usb.h>
32 #include <linux/blkdev.h>
33 #include <linux/devfs_fs_kernel.h>
34 #include <linux/timer.h>
35 #include <scsi/scsi.h>
36
37 #define DRV_NAME "ub"
38 #define DEVFS_NAME DRV_NAME
39
40 #define UB_MAJOR 180
41
42 /*
43  * The command state machine is the key model for understanding of this driver.
44  *
45  * The general rule is that all transitions are done towards the bottom
46  * of the diagram, thus preventing any loops.
47  *
48  * An exception to that is how the STAT state is handled. A counter allows it
49  * to be re-entered along the path marked with [C].
50  *
51  *       +--------+
52  *       ! INIT   !
53  *       +--------+
54  *           !
55  *        ub_scsi_cmd_start fails ->--------------------------------------\
56  *           !                                                            !
57  *           V                                                            !
58  *       +--------+                                                       !
59  *       ! CMD    !                                                       !
60  *       +--------+                                                       !
61  *           !                                            +--------+      !
62  *         was -EPIPE -->-------------------------------->! CLEAR  !      !
63  *           !                                            +--------+      !
64  *           !                                                !           !
65  *         was error -->------------------------------------- ! --------->\
66  *           !                                                !           !
67  *  /--<-- cmd->dir == NONE ?                                 !           !
68  *  !        !                                                !           !
69  *  !        V                                                !           !
70  *  !    +--------+                                           !           !
71  *  !    ! DATA   !                                           !           !
72  *  !    +--------+                                           !           !
73  *  !        !                           +---------+          !           !
74  *  !      was -EPIPE -->--------------->! CLR2STS !          !           !
75  *  !        !                           +---------+          !           !
76  *  !        !                                !               !           !
77  *  !        !                              was error -->---- ! --------->\
78  *  !      was error -->--------------------- ! ------------- ! --------->\
79  *  !        !                                !               !           !
80  *  !        V                                !               !           !
81  *  \--->+--------+                           !               !           !
82  *       ! STAT   !<--------------------------/               !           !
83  *  /--->+--------+                                           !           !
84  *  !        !                                                !           !
85  * [C]     was -EPIPE -->-----------\                         !           !
86  *  !        !                      !                         !           !
87  *  +<---- len == 0                 !                         !           !
88  *  !        !                      !                         !           !
89  *  !      was error -->--------------------------------------!---------->\
90  *  !        !                      !                         !           !
91  *  +<---- bad CSW                  !                         !           !
92  *  +<---- bad tag                  !                         !           !
93  *  !        !                      V                         !           !
94  *  !        !                 +--------+                     !           !
95  *  !        !                 ! CLRRS  !                     !           !
96  *  !        !                 +--------+                     !           !
97  *  !        !                      !                         !           !
98  *  \------- ! --------------------[C]--------\               !           !
99  *           !                                !               !           !
100  *         cmd->error---\                +--------+           !           !
101  *           !          +--------------->! SENSE  !<----------/           !
102  *         STAT_FAIL----/                +--------+                       !
103  *           !                                !                           V
104  *           !                                V                      +--------+
105  *           \--------------------------------\--------------------->! DONE   !
106  *                                                                   +--------+
107  */
108
109 /*
110  * Definitions which have to be scattered once we understand the layout better.
111  */
112
113 /* Transport (despite PR in the name) */
114 #define US_PR_BULK      0x50            /* bulk only */
115
116 /* Protocol */
117 #define US_SC_SCSI      0x06            /* Transparent */
118
119 /*
120  * This many LUNs per USB device.
121  * Every one of them takes a host, see UB_MAX_HOSTS.
122  */
123 #define UB_MAX_LUNS   9
124
125 /*
126  */
127
128 #define UB_MINORS_PER_MAJOR     8
129
130 #define UB_MAX_CDB_SIZE      16         /* Corresponds to Bulk */
131
132 #define UB_SENSE_SIZE  18
133
134 /*
135  */
136
137 /* command block wrapper */
138 struct bulk_cb_wrap {
139         __le32  Signature;              /* contains 'USBC' */
140         u32     Tag;                    /* unique per command id */
141         __le32  DataTransferLength;     /* size of data */
142         u8      Flags;                  /* direction in bit 0 */
143         u8      Lun;                    /* LUN */
144         u8      Length;                 /* of of the CDB */
145         u8      CDB[UB_MAX_CDB_SIZE];   /* max command */
146 };
147
148 #define US_BULK_CB_WRAP_LEN     31
149 #define US_BULK_CB_SIGN         0x43425355      /*spells out USBC */
150 #define US_BULK_FLAG_IN         1
151 #define US_BULK_FLAG_OUT        0
152
153 /* command status wrapper */
154 struct bulk_cs_wrap {
155         __le32  Signature;              /* should = 'USBS' */
156         u32     Tag;                    /* same as original command */
157         __le32  Residue;                /* amount not transferred */
158         u8      Status;                 /* see below */
159 };
160
161 #define US_BULK_CS_WRAP_LEN     13
162 #define US_BULK_CS_SIGN         0x53425355      /* spells out 'USBS' */
163 #define US_BULK_STAT_OK         0
164 #define US_BULK_STAT_FAIL       1
165 #define US_BULK_STAT_PHASE      2
166
167 /* bulk-only class specific requests */
168 #define US_BULK_RESET_REQUEST   0xff
169 #define US_BULK_GET_MAX_LUN     0xfe
170
171 /*
172  */
173 struct ub_dev;
174
175 #define UB_MAX_REQ_SG   4
176 #define UB_MAX_SECTORS 64
177
178 /*
179  * A second is more than enough for a 32K transfer (UB_MAX_SECTORS)
180  * even if a webcam hogs the bus, but some devices need time to spin up.
181  */
182 #define UB_URB_TIMEOUT  (HZ*2)
183 #define UB_DATA_TIMEOUT (HZ*5)  /* ZIP does spin-ups in the data phase */
184 #define UB_STAT_TIMEOUT (HZ*5)  /* Same spinups and eject for a dataless cmd. */
185 #define UB_CTRL_TIMEOUT (HZ/2)  /* 500ms ought to be enough to clear a stall */
186
187 /*
188  * An instance of a SCSI command in transit.
189  */
190 #define UB_DIR_NONE     0
191 #define UB_DIR_READ     1
192 #define UB_DIR_ILLEGAL2 2
193 #define UB_DIR_WRITE    3
194
195 #define UB_DIR_CHAR(c)  (((c)==UB_DIR_WRITE)? 'w': \
196                          (((c)==UB_DIR_READ)? 'r': 'n'))
197
198 enum ub_scsi_cmd_state {
199         UB_CMDST_INIT,                  /* Initial state */
200         UB_CMDST_CMD,                   /* Command submitted */
201         UB_CMDST_DATA,                  /* Data phase */
202         UB_CMDST_CLR2STS,               /* Clearing before requesting status */
203         UB_CMDST_STAT,                  /* Status phase */
204         UB_CMDST_CLEAR,                 /* Clearing a stall (halt, actually) */
205         UB_CMDST_CLRRS,                 /* Clearing before retrying status */
206         UB_CMDST_SENSE,                 /* Sending Request Sense */
207         UB_CMDST_DONE                   /* Final state */
208 };
209
210 static char *ub_scsi_cmd_stname[] = {
211         ".  ",
212         "Cmd",
213         "dat",
214         "c2s",
215         "sts",
216         "clr",
217         "crs",
218         "Sen",
219         "fin"
220 };
221
222 struct ub_scsi_cmd {
223         unsigned char cdb[UB_MAX_CDB_SIZE];
224         unsigned char cdb_len;
225
226         unsigned char dir;              /* 0 - none, 1 - read, 3 - write. */
227         unsigned char trace_index;
228         enum ub_scsi_cmd_state state;
229         unsigned int tag;
230         struct ub_scsi_cmd *next;
231
232         int error;                      /* Return code - valid upon done */
233         unsigned int act_len;           /* Return size */
234         unsigned char key, asc, ascq;   /* May be valid if error==-EIO */
235
236         int stat_count;                 /* Retries getting status. */
237
238         /*
239          * We do not support transfers from highmem pages
240          * because the underlying USB framework does not do what we need.
241          */
242         char *data;                     /* Requested buffer */
243         unsigned int len;               /* Requested length */
244
245         struct ub_lun *lun;
246         void (*done)(struct ub_dev *, struct ub_scsi_cmd *);
247         void *back;
248 };
249
250 struct ub_request {
251         struct request *rq;
252         unsigned char dir;
253         unsigned int current_block;
254         unsigned int current_sg;
255         unsigned int nsg;               /* sgv[nsg] */
256         struct scatterlist sgv[UB_MAX_REQ_SG];
257 };
258
259 /*
260  */
261 struct ub_capacity {
262         unsigned long nsec;             /* Linux size - 512 byte sectors */
263         unsigned int bsize;             /* Linux hardsect_size */
264         unsigned int bshift;            /* Shift between 512 and hard sects */
265 };
266
267 /*
268  * The SCSI command tracing structure.
269  */
270
271 #define SCMD_ST_HIST_SZ   8
272 #define SCMD_TRACE_SZ    63             /* Less than 4KB of 61-byte lines */
273
274 struct ub_scsi_cmd_trace {
275         int hcur;
276         unsigned int tag;
277         unsigned int req_size, act_size;
278         unsigned char op;
279         unsigned char dir;
280         unsigned char key, asc, ascq;
281         char st_hst[SCMD_ST_HIST_SZ];   
282 };
283
284 struct ub_scsi_trace {
285         int cur;
286         struct ub_scsi_cmd_trace vec[SCMD_TRACE_SZ];
287 };
288
289 /*
290  * This is a direct take-off from linux/include/completion.h
291  * The difference is that I do not wait on this thing, just poll.
292  * When I want to wait (ub_probe), I just use the stock completion.
293  *
294  * Note that INIT_COMPLETION takes no lock. It is correct. But why
295  * in the bloody hell that thing takes struct instead of pointer to struct
296  * is quite beyond me. I just copied it from the stock completion.
297  */
298 struct ub_completion {
299         unsigned int done;
300         spinlock_t lock;
301 };
302
303 static inline void ub_init_completion(struct ub_completion *x)
304 {
305         x->done = 0;
306         spin_lock_init(&x->lock);
307 }
308
309 #define UB_INIT_COMPLETION(x)   ((x).done = 0)
310
311 static void ub_complete(struct ub_completion *x)
312 {
313         unsigned long flags;
314
315         spin_lock_irqsave(&x->lock, flags);
316         x->done++;
317         spin_unlock_irqrestore(&x->lock, flags);
318 }
319
320 static int ub_is_completed(struct ub_completion *x)
321 {
322         unsigned long flags;
323         int ret;
324
325         spin_lock_irqsave(&x->lock, flags);
326         ret = x->done;
327         spin_unlock_irqrestore(&x->lock, flags);
328         return ret;
329 }
330
331 /*
332  */
333 struct ub_scsi_cmd_queue {
334         int qlen, qmax;
335         struct ub_scsi_cmd *head, *tail;
336 };
337
338 /*
339  * The block device instance (one per LUN).
340  */
341 struct ub_lun {
342         struct ub_dev *udev;
343         struct list_head link;
344         struct gendisk *disk;
345         int id;                         /* Host index */
346         int num;                        /* LUN number */
347         char name[16];
348
349         int changed;                    /* Media was changed */
350         int removable;
351         int readonly;
352         int first_open;                 /* Kludge. See ub_bd_open. */
353
354         struct ub_request urq;
355
356         /* Use Ingo's mempool if or when we have more than one command. */
357         /*
358          * Currently we never need more than one command for the whole device.
359          * However, giving every LUN a command is a cheap and automatic way
360          * to enforce fairness between them.
361          */
362         int cmda[1];
363         struct ub_scsi_cmd cmdv[1];
364
365         struct ub_capacity capacity; 
366 };
367
368 /*
369  * The USB device instance.
370  */
371 struct ub_dev {
372         spinlock_t lock;
373         atomic_t poison;                /* The USB device is disconnected */
374         int openc;                      /* protected by ub_lock! */
375                                         /* kref is too implicit for our taste */
376         unsigned int tagcnt;
377         char name[12];
378         struct usb_device *dev;
379         struct usb_interface *intf;
380
381         struct list_head luns;
382
383         unsigned int send_bulk_pipe;    /* cached pipe values */
384         unsigned int recv_bulk_pipe;
385         unsigned int send_ctrl_pipe;
386         unsigned int recv_ctrl_pipe;
387
388         struct tasklet_struct tasklet;
389
390         struct ub_scsi_cmd_queue cmd_queue;
391         struct ub_scsi_cmd top_rqs_cmd; /* REQUEST SENSE */
392         unsigned char top_sense[UB_SENSE_SIZE];
393
394         struct ub_completion work_done;
395         struct urb work_urb;
396         struct timer_list work_timer;
397         int last_pipe;                  /* What might need clearing */
398         __le32 signature;               /* Learned signature */
399         struct bulk_cb_wrap work_bcb;
400         struct bulk_cs_wrap work_bcs;
401         struct usb_ctrlrequest work_cr;
402
403         int sg_stat[UB_MAX_REQ_SG+1];
404         struct ub_scsi_trace tr;
405 };
406
407 /*
408  */
409 static void ub_cleanup(struct ub_dev *sc);
410 static int ub_bd_rq_fn_1(struct ub_lun *lun, struct request *rq);
411 static int ub_cmd_build_block(struct ub_dev *sc, struct ub_lun *lun,
412     struct ub_scsi_cmd *cmd, struct request *rq);
413 static void ub_scsi_build_block(struct ub_lun *lun,
414     struct ub_scsi_cmd *cmd, struct ub_request *urq);
415 static int ub_cmd_build_packet(struct ub_dev *sc, struct ub_lun *lun,
416     struct ub_scsi_cmd *cmd, struct request *rq);
417 static void ub_rw_cmd_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
418 static void ub_end_rq(struct request *rq, int uptodate);
419 static int ub_request_advance(struct ub_dev *sc, struct ub_lun *lun,
420     struct ub_request *urq, struct ub_scsi_cmd *cmd);
421 static int ub_submit_scsi(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
422 static void ub_urb_complete(struct urb *urb, struct pt_regs *pt);
423 static void ub_scsi_action(unsigned long _dev);
424 static void ub_scsi_dispatch(struct ub_dev *sc);
425 static void ub_scsi_urb_compl(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
426 static void ub_state_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd, int rc);
427 static int __ub_state_stat(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
428 static void ub_state_stat(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
429 static void ub_state_stat_counted(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
430 static void ub_state_sense(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
431 static int ub_submit_clear_stall(struct ub_dev *sc, struct ub_scsi_cmd *cmd,
432     int stalled_pipe);
433 static void ub_top_sense_done(struct ub_dev *sc, struct ub_scsi_cmd *scmd);
434 static int ub_sync_tur(struct ub_dev *sc, struct ub_lun *lun);
435 static int ub_sync_read_cap(struct ub_dev *sc, struct ub_lun *lun,
436     struct ub_capacity *ret);
437 static int ub_probe_lun(struct ub_dev *sc, int lnum);
438
439 /*
440  */
441 static struct usb_device_id ub_usb_ids[] = {
442         // { USB_DEVICE_VER(0x0781, 0x0002, 0x0009, 0x0009) },  /* SDDR-31 */
443         { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_SCSI, US_PR_BULK) },
444         { }
445 };
446
447 MODULE_DEVICE_TABLE(usb, ub_usb_ids);
448
449 /*
450  * Find me a way to identify "next free minor" for add_disk(),
451  * and the array disappears the next day. However, the number of
452  * hosts has something to do with the naming and /proc/partitions.
453  * This has to be thought out in detail before changing.
454  * If UB_MAX_HOST was 1000, we'd use a bitmap. Or a better data structure.
455  */
456 #define UB_MAX_HOSTS  26
457 static char ub_hostv[UB_MAX_HOSTS];
458
459 static DEFINE_SPINLOCK(ub_lock);        /* Locks globals and ->openc */
460
461 /*
462  * The SCSI command tracing procedures.
463  */
464
465 static void ub_cmdtr_new(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
466 {
467         int n;
468         struct ub_scsi_cmd_trace *t;
469
470         if ((n = sc->tr.cur + 1) == SCMD_TRACE_SZ) n = 0;
471         t = &sc->tr.vec[n];
472
473         memset(t, 0, sizeof(struct ub_scsi_cmd_trace));
474         t->tag = cmd->tag;
475         t->op = cmd->cdb[0];
476         t->dir = cmd->dir;
477         t->req_size = cmd->len;
478         t->st_hst[0] = cmd->state;
479
480         sc->tr.cur = n;
481         cmd->trace_index = n;
482 }
483
484 static void ub_cmdtr_state(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
485 {
486         int n;
487         struct ub_scsi_cmd_trace *t;
488
489         t = &sc->tr.vec[cmd->trace_index];
490         if (t->tag == cmd->tag) {
491                 if ((n = t->hcur + 1) == SCMD_ST_HIST_SZ) n = 0;
492                 t->st_hst[n] = cmd->state;
493                 t->hcur = n;
494         }
495 }
496
497 static void ub_cmdtr_act_len(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
498 {
499         struct ub_scsi_cmd_trace *t;
500
501         t = &sc->tr.vec[cmd->trace_index];
502         if (t->tag == cmd->tag)
503                 t->act_size = cmd->act_len;
504 }
505
506 static void ub_cmdtr_sense(struct ub_dev *sc, struct ub_scsi_cmd *cmd,
507     unsigned char *sense)
508 {
509         struct ub_scsi_cmd_trace *t;
510
511         t = &sc->tr.vec[cmd->trace_index];
512         if (t->tag == cmd->tag) {
513                 t->key = sense[2] & 0x0F;
514                 t->asc = sense[12];
515                 t->ascq = sense[13];
516         }
517 }
518
519 static ssize_t ub_diag_show(struct device *dev, struct device_attribute *attr, char *page)
520 {
521         struct usb_interface *intf;
522         struct ub_dev *sc;
523         struct list_head *p;
524         struct ub_lun *lun;
525         int cnt;
526         unsigned long flags;
527         int nc, nh;
528         int i, j;
529         struct ub_scsi_cmd_trace *t;
530
531         intf = to_usb_interface(dev);
532         sc = usb_get_intfdata(intf);
533         if (sc == NULL)
534                 return 0;
535
536         cnt = 0;
537         spin_lock_irqsave(&sc->lock, flags);
538
539         cnt += sprintf(page + cnt,
540             "qlen %d qmax %d\n",
541             sc->cmd_queue.qlen, sc->cmd_queue.qmax);
542         cnt += sprintf(page + cnt,
543             "sg %d %d %d %d %d\n",
544             sc->sg_stat[0],
545             sc->sg_stat[1],
546             sc->sg_stat[2],
547             sc->sg_stat[3],
548             sc->sg_stat[4]);
549
550         list_for_each (p, &sc->luns) {
551                 lun = list_entry(p, struct ub_lun, link);
552                 cnt += sprintf(page + cnt,
553                     "lun %u changed %d removable %d readonly %d\n",
554                     lun->num, lun->changed, lun->removable, lun->readonly);
555         }
556
557         if ((nc = sc->tr.cur + 1) == SCMD_TRACE_SZ) nc = 0;
558         for (j = 0; j < SCMD_TRACE_SZ; j++) {
559                 t = &sc->tr.vec[nc];
560
561                 cnt += sprintf(page + cnt, "%08x %02x", t->tag, t->op);
562                 if (t->op == REQUEST_SENSE) {
563                         cnt += sprintf(page + cnt, " [sense %x %02x %02x]",
564                                         t->key, t->asc, t->ascq);
565                 } else {
566                         cnt += sprintf(page + cnt, " %c", UB_DIR_CHAR(t->dir));
567                         cnt += sprintf(page + cnt, " [%5d %5d]",
568                                         t->req_size, t->act_size);
569                 }
570                 if ((nh = t->hcur + 1) == SCMD_ST_HIST_SZ) nh = 0;
571                 for (i = 0; i < SCMD_ST_HIST_SZ; i++) {
572                         cnt += sprintf(page + cnt, " %s",
573                                         ub_scsi_cmd_stname[(int)t->st_hst[nh]]);
574                         if (++nh == SCMD_ST_HIST_SZ) nh = 0;
575                 }
576                 cnt += sprintf(page + cnt, "\n");
577
578                 if (++nc == SCMD_TRACE_SZ) nc = 0;
579         }
580
581         spin_unlock_irqrestore(&sc->lock, flags);
582         return cnt;
583 }
584
585 static DEVICE_ATTR(diag, S_IRUGO, ub_diag_show, NULL); /* N.B. World readable */
586
587 /*
588  * The id allocator.
589  *
590  * This also stores the host for indexing by minor, which is somewhat dirty.
591  */
592 static int ub_id_get(void)
593 {
594         unsigned long flags;
595         int i;
596
597         spin_lock_irqsave(&ub_lock, flags);
598         for (i = 0; i < UB_MAX_HOSTS; i++) {
599                 if (ub_hostv[i] == 0) {
600                         ub_hostv[i] = 1;
601                         spin_unlock_irqrestore(&ub_lock, flags);
602                         return i;
603                 }
604         }
605         spin_unlock_irqrestore(&ub_lock, flags);
606         return -1;
607 }
608
609 static void ub_id_put(int id)
610 {
611         unsigned long flags;
612
613         if (id < 0 || id >= UB_MAX_HOSTS) {
614                 printk(KERN_ERR DRV_NAME ": bad host ID %d\n", id);
615                 return;
616         }
617
618         spin_lock_irqsave(&ub_lock, flags);
619         if (ub_hostv[id] == 0) {
620                 spin_unlock_irqrestore(&ub_lock, flags);
621                 printk(KERN_ERR DRV_NAME ": freeing free host ID %d\n", id);
622                 return;
623         }
624         ub_hostv[id] = 0;
625         spin_unlock_irqrestore(&ub_lock, flags);
626 }
627
628 /*
629  * Downcount for deallocation. This rides on two assumptions:
630  *  - once something is poisoned, its refcount cannot grow
631  *  - opens cannot happen at this time (del_gendisk was done)
632  * If the above is true, we can drop the lock, which we need for
633  * blk_cleanup_queue(): the silly thing may attempt to sleep.
634  * [Actually, it never needs to sleep for us, but it calls might_sleep()]
635  */
636 static void ub_put(struct ub_dev *sc)
637 {
638         unsigned long flags;
639
640         spin_lock_irqsave(&ub_lock, flags);
641         --sc->openc;
642         if (sc->openc == 0 && atomic_read(&sc->poison)) {
643                 spin_unlock_irqrestore(&ub_lock, flags);
644                 ub_cleanup(sc);
645         } else {
646                 spin_unlock_irqrestore(&ub_lock, flags);
647         }
648 }
649
650 /*
651  * Final cleanup and deallocation.
652  */
653 static void ub_cleanup(struct ub_dev *sc)
654 {
655         struct list_head *p;
656         struct ub_lun *lun;
657         request_queue_t *q;
658
659         while (!list_empty(&sc->luns)) {
660                 p = sc->luns.next;
661                 lun = list_entry(p, struct ub_lun, link);
662                 list_del(p);
663
664                 /* I don't think queue can be NULL. But... Stolen from sx8.c */
665                 if ((q = lun->disk->queue) != NULL)
666                         blk_cleanup_queue(q);
667                 /*
668                  * If we zero disk->private_data BEFORE put_disk, we have
669                  * to check for NULL all over the place in open, release,
670                  * check_media and revalidate, because the block level
671                  * semaphore is well inside the put_disk.
672                  * But we cannot zero after the call, because *disk is gone.
673                  * The sd.c is blatantly racy in this area.
674                  */
675                 /* disk->private_data = NULL; */
676                 put_disk(lun->disk);
677                 lun->disk = NULL;
678
679                 ub_id_put(lun->id);
680                 kfree(lun);
681         }
682
683         kfree(sc);
684 }
685
686 /*
687  * The "command allocator".
688  */
689 static struct ub_scsi_cmd *ub_get_cmd(struct ub_lun *lun)
690 {
691         struct ub_scsi_cmd *ret;
692
693         if (lun->cmda[0])
694                 return NULL;
695         ret = &lun->cmdv[0];
696         lun->cmda[0] = 1;
697         return ret;
698 }
699
700 static void ub_put_cmd(struct ub_lun *lun, struct ub_scsi_cmd *cmd)
701 {
702         if (cmd != &lun->cmdv[0]) {
703                 printk(KERN_WARNING "%s: releasing a foreign cmd %p\n",
704                     lun->name, cmd);
705                 return;
706         }
707         if (!lun->cmda[0]) {
708                 printk(KERN_WARNING "%s: releasing a free cmd\n", lun->name);
709                 return;
710         }
711         lun->cmda[0] = 0;
712 }
713
714 /*
715  * The command queue.
716  */
717 static void ub_cmdq_add(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
718 {
719         struct ub_scsi_cmd_queue *t = &sc->cmd_queue;
720
721         if (t->qlen++ == 0) {
722                 t->head = cmd;
723                 t->tail = cmd;
724         } else {
725                 t->tail->next = cmd;
726                 t->tail = cmd;
727         }
728
729         if (t->qlen > t->qmax)
730                 t->qmax = t->qlen;
731 }
732
733 static void ub_cmdq_insert(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
734 {
735         struct ub_scsi_cmd_queue *t = &sc->cmd_queue;
736
737         if (t->qlen++ == 0) {
738                 t->head = cmd;
739                 t->tail = cmd;
740         } else {
741                 cmd->next = t->head;
742                 t->head = cmd;
743         }
744
745         if (t->qlen > t->qmax)
746                 t->qmax = t->qlen;
747 }
748
749 static struct ub_scsi_cmd *ub_cmdq_pop(struct ub_dev *sc)
750 {
751         struct ub_scsi_cmd_queue *t = &sc->cmd_queue;
752         struct ub_scsi_cmd *cmd;
753
754         if (t->qlen == 0)
755                 return NULL;
756         if (--t->qlen == 0)
757                 t->tail = NULL;
758         cmd = t->head;
759         t->head = cmd->next;
760         cmd->next = NULL;
761         return cmd;
762 }
763
764 #define ub_cmdq_peek(sc)  ((sc)->cmd_queue.head)
765
766 /*
767  * The request function is our main entry point
768  */
769
770 static void ub_bd_rq_fn(request_queue_t *q)
771 {
772         struct ub_lun *lun = q->queuedata;
773         struct request *rq;
774
775         while ((rq = elv_next_request(q)) != NULL) {
776                 if (ub_bd_rq_fn_1(lun, rq) != 0) {
777                         blk_stop_queue(q);
778                         break;
779                 }
780         }
781 }
782
783 static int ub_bd_rq_fn_1(struct ub_lun *lun, struct request *rq)
784 {
785         struct ub_dev *sc = lun->udev;
786         struct ub_scsi_cmd *cmd;
787         int rc;
788
789         if (atomic_read(&sc->poison) || lun->changed) {
790                 blkdev_dequeue_request(rq);
791                 ub_end_rq(rq, 0);
792                 return 0;
793         }
794
795         if (lun->urq.rq != NULL)
796                 return -1;
797         if ((cmd = ub_get_cmd(lun)) == NULL)
798                 return -1;
799         memset(cmd, 0, sizeof(struct ub_scsi_cmd));
800
801         blkdev_dequeue_request(rq);
802         if (blk_pc_request(rq)) {
803                 rc = ub_cmd_build_packet(sc, lun, cmd, rq);
804         } else {
805                 rc = ub_cmd_build_block(sc, lun, cmd, rq);
806         }
807         if (rc != 0) {
808                 ub_put_cmd(lun, cmd);
809                 ub_end_rq(rq, 0);
810                 return 0;
811         }
812         cmd->state = UB_CMDST_INIT;
813         cmd->lun = lun;
814         cmd->done = ub_rw_cmd_done;
815         cmd->back = &lun->urq;
816
817         cmd->tag = sc->tagcnt++;
818         if (ub_submit_scsi(sc, cmd) != 0) {
819                 ub_put_cmd(lun, cmd);
820                 ub_end_rq(rq, 0);
821                 return 0;
822         }
823
824         return 0;
825 }
826
827 static int ub_cmd_build_block(struct ub_dev *sc, struct ub_lun *lun,
828     struct ub_scsi_cmd *cmd, struct request *rq)
829 {
830         struct ub_request *urq;
831         int ub_dir;
832         int n_elem;
833
834         urq = &lun->urq;
835         memset(urq, 0, sizeof(struct ub_request));
836
837         if (rq_data_dir(rq) == WRITE)
838                 ub_dir = UB_DIR_WRITE;
839         else
840                 ub_dir = UB_DIR_READ;
841
842         /*
843          * get scatterlist from block layer
844          */
845         n_elem = blk_rq_map_sg(lun->disk->queue, rq, &urq->sgv[0]);
846         if (n_elem <= 0) {
847                 printk(KERN_INFO "%s: failed request map (%d)\n",
848                     sc->name, n_elem); /* P3 */
849                 return -1;              /* request with no s/g entries? */
850         }
851         if (n_elem > UB_MAX_REQ_SG) {   /* Paranoia */
852                 printk(KERN_WARNING "%s: request with %d segments\n",
853                     sc->name, n_elem);
854                 return -1;
855         }
856         urq->nsg = n_elem;
857         sc->sg_stat[n_elem]++;
858
859         /*
860          * build the command
861          *
862          * The call to blk_queue_hardsect_size() guarantees that request
863          * is aligned, but it is given in terms of 512 byte units, always.
864          */
865         urq->current_block = rq->sector >> lun->capacity.bshift;
866         // nblks = rq->nr_sectors >> lun->capacity.bshift;
867
868         urq->rq = rq;
869         urq->current_sg = 0;
870         urq->dir = ub_dir;
871
872         ub_scsi_build_block(lun, cmd, urq);
873         return 0;
874 }
875
876 static void ub_scsi_build_block(struct ub_lun *lun,
877     struct ub_scsi_cmd *cmd, struct ub_request *urq)
878 {
879         struct scatterlist *sg;
880         unsigned int block, nblks;
881
882         sg = &urq->sgv[urq->current_sg];
883
884         block = urq->current_block;
885         nblks = sg->length >> (lun->capacity.bshift + 9);
886
887         cmd->cdb[0] = (urq->dir == UB_DIR_READ)? READ_10: WRITE_10;
888         /* 10-byte uses 4 bytes of LBA: 2147483648KB, 2097152MB, 2048GB */
889         cmd->cdb[2] = block >> 24;
890         cmd->cdb[3] = block >> 16;
891         cmd->cdb[4] = block >> 8;
892         cmd->cdb[5] = block;
893         cmd->cdb[7] = nblks >> 8;
894         cmd->cdb[8] = nblks;
895         cmd->cdb_len = 10;
896
897         cmd->dir = urq->dir;
898         cmd->data = page_address(sg->page) + sg->offset;
899         cmd->len = sg->length;
900 }
901
902 static int ub_cmd_build_packet(struct ub_dev *sc, struct ub_lun *lun,
903     struct ub_scsi_cmd *cmd, struct request *rq)
904 {
905         struct ub_request *urq;
906
907         urq = &lun->urq;
908         memset(urq, 0, sizeof(struct ub_request));
909         urq->rq = rq;
910         sc->sg_stat[0]++;
911
912         if (rq->data_len != 0 && rq->data == NULL) {
913                 static int do_print = 1;
914                 if (do_print) {
915                         printk(KERN_WARNING "%s: unmapped packet request"
916                             " flags 0x%lx length %d\n",
917                             sc->name, rq->flags, rq->data_len);
918                         do_print = 0;
919                 }
920                 return -1;
921         }
922
923         memcpy(&cmd->cdb, rq->cmd, rq->cmd_len);
924         cmd->cdb_len = rq->cmd_len;
925
926         if (rq->data_len == 0) {
927                 cmd->dir = UB_DIR_NONE;
928         } else {
929                 if (rq_data_dir(rq) == WRITE)
930                         cmd->dir = UB_DIR_WRITE;
931                 else
932                         cmd->dir = UB_DIR_READ;
933         }
934         cmd->data = rq->data;
935         cmd->len = rq->data_len;
936
937         return 0;
938 }
939
940 static void ub_rw_cmd_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
941 {
942         struct ub_lun *lun = cmd->lun;
943         struct ub_request *urq = cmd->back;
944         struct request *rq;
945         int uptodate;
946
947         rq = urq->rq;
948
949         if (blk_pc_request(rq)) {
950                 /* UB_SENSE_SIZE is smaller than SCSI_SENSE_BUFFERSIZE */
951                 memcpy(rq->sense, sc->top_sense, UB_SENSE_SIZE);
952                 rq->sense_len = UB_SENSE_SIZE;
953         }
954
955         if (cmd->error == 0)
956                 uptodate = 1;
957         else
958                 uptodate = 0;
959
960         if (cmd->error == 0 && urq->current_sg+1 < urq->nsg) {
961                 if (ub_request_advance(sc, lun, urq, cmd) == 0) {
962                         /* Stay on target... */
963                         return;
964                 }
965                 uptodate = 0;
966         }
967
968         urq->rq = NULL;
969
970         ub_put_cmd(lun, cmd);
971         ub_end_rq(rq, uptodate);
972         blk_start_queue(lun->disk->queue);
973 }
974
975 static void ub_end_rq(struct request *rq, int uptodate)
976 {
977         int rc;
978
979         rc = end_that_request_first(rq, uptodate, rq->hard_nr_sectors);
980         // assert(rc == 0);
981         end_that_request_last(rq);
982 }
983
984 static int ub_request_advance(struct ub_dev *sc, struct ub_lun *lun,
985     struct ub_request *urq, struct ub_scsi_cmd *cmd)
986 {
987         struct scatterlist *sg;
988         unsigned int nblks;
989
990         /* XXX This is temporary, until we sort out S/G in packet requests. */
991         if (blk_pc_request(urq->rq)) {
992                 printk(KERN_WARNING
993                     "2-segment packet request completed\n"); /* P3 */
994                 return -1;
995         }
996
997         sg = &urq->sgv[urq->current_sg];
998         nblks = sg->length >> (lun->capacity.bshift + 9);
999         urq->current_block += nblks;
1000         urq->current_sg++;
1001         sg++;
1002
1003         memset(cmd, 0, sizeof(struct ub_scsi_cmd));
1004         ub_scsi_build_block(lun, cmd, urq);
1005         cmd->state = UB_CMDST_INIT;
1006         cmd->lun = lun;
1007         cmd->done = ub_rw_cmd_done;
1008         cmd->back = &lun->urq;
1009
1010         cmd->tag = sc->tagcnt++;
1011         if (ub_submit_scsi(sc, cmd) != 0) {
1012                 return -1;
1013         }
1014
1015         return 0;
1016 }
1017
1018 /*
1019  * Submit a regular SCSI operation (not an auto-sense).
1020  *
1021  * The Iron Law of Good Submit Routine is:
1022  * Zero return - callback is done, Nonzero return - callback is not done.
1023  * No exceptions.
1024  *
1025  * Host is assumed locked.
1026  *
1027  * XXX We only support Bulk for the moment.
1028  */
1029 static int ub_submit_scsi(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1030 {
1031
1032         if (cmd->state != UB_CMDST_INIT ||
1033             (cmd->dir != UB_DIR_NONE && cmd->len == 0)) {
1034                 return -EINVAL;
1035         }
1036
1037         ub_cmdq_add(sc, cmd);
1038         /*
1039          * We can call ub_scsi_dispatch(sc) right away here, but it's a little
1040          * safer to jump to a tasklet, in case upper layers do something silly.
1041          */
1042         tasklet_schedule(&sc->tasklet);
1043         return 0;
1044 }
1045
1046 /*
1047  * Submit the first URB for the queued command.
1048  * This function does not deal with queueing in any way.
1049  */
1050 static int ub_scsi_cmd_start(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1051 {
1052         struct bulk_cb_wrap *bcb;
1053         int rc;
1054
1055         bcb = &sc->work_bcb;
1056
1057         /*
1058          * ``If the allocation length is eighteen or greater, and a device
1059          * server returns less than eithteen bytes of data, the application
1060          * client should assume that the bytes not transferred would have been
1061          * zeroes had the device server returned those bytes.''
1062          *
1063          * We zero sense for all commands so that when a packet request
1064          * fails it does not return a stale sense.
1065          */
1066         memset(&sc->top_sense, 0, UB_SENSE_SIZE);
1067
1068         /* set up the command wrapper */
1069         bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
1070         bcb->Tag = cmd->tag;            /* Endianness is not important */
1071         bcb->DataTransferLength = cpu_to_le32(cmd->len);
1072         bcb->Flags = (cmd->dir == UB_DIR_READ) ? 0x80 : 0;
1073         bcb->Lun = (cmd->lun != NULL) ? cmd->lun->num : 0;
1074         bcb->Length = cmd->cdb_len;
1075
1076         /* copy the command payload */
1077         memcpy(bcb->CDB, cmd->cdb, UB_MAX_CDB_SIZE);
1078
1079         UB_INIT_COMPLETION(sc->work_done);
1080
1081         sc->last_pipe = sc->send_bulk_pipe;
1082         usb_fill_bulk_urb(&sc->work_urb, sc->dev, sc->send_bulk_pipe,
1083             bcb, US_BULK_CB_WRAP_LEN, ub_urb_complete, sc);
1084         sc->work_urb.transfer_flags = URB_ASYNC_UNLINK;
1085
1086         /* Fill what we shouldn't be filling, because usb-storage did so. */
1087         sc->work_urb.actual_length = 0;
1088         sc->work_urb.error_count = 0;
1089         sc->work_urb.status = 0;
1090
1091         if ((rc = usb_submit_urb(&sc->work_urb, GFP_ATOMIC)) != 0) {
1092                 /* XXX Clear stalls */
1093                 printk("ub: cmd #%d start failed (%d)\n", cmd->tag, rc); /* P3 */
1094                 ub_complete(&sc->work_done);
1095                 return rc;
1096         }
1097
1098         sc->work_timer.expires = jiffies + UB_URB_TIMEOUT;
1099         add_timer(&sc->work_timer);
1100
1101         cmd->state = UB_CMDST_CMD;
1102         ub_cmdtr_state(sc, cmd);
1103         return 0;
1104 }
1105
1106 /*
1107  * Timeout handler.
1108  */
1109 static void ub_urb_timeout(unsigned long arg)
1110 {
1111         struct ub_dev *sc = (struct ub_dev *) arg;
1112         unsigned long flags;
1113
1114         spin_lock_irqsave(&sc->lock, flags);
1115         usb_unlink_urb(&sc->work_urb);
1116         spin_unlock_irqrestore(&sc->lock, flags);
1117 }
1118
1119 /*
1120  * Completion routine for the work URB.
1121  *
1122  * This can be called directly from usb_submit_urb (while we have
1123  * the sc->lock taken) and from an interrupt (while we do NOT have
1124  * the sc->lock taken). Therefore, bounce this off to a tasklet.
1125  */
1126 static void ub_urb_complete(struct urb *urb, struct pt_regs *pt)
1127 {
1128         struct ub_dev *sc = urb->context;
1129
1130         ub_complete(&sc->work_done);
1131         tasklet_schedule(&sc->tasklet);
1132 }
1133
1134 static void ub_scsi_action(unsigned long _dev)
1135 {
1136         struct ub_dev *sc = (struct ub_dev *) _dev;
1137         unsigned long flags;
1138
1139         spin_lock_irqsave(&sc->lock, flags);
1140         del_timer(&sc->work_timer);
1141         ub_scsi_dispatch(sc);
1142         spin_unlock_irqrestore(&sc->lock, flags);
1143 }
1144
1145 static void ub_scsi_dispatch(struct ub_dev *sc)
1146 {
1147         struct ub_scsi_cmd *cmd;
1148         int rc;
1149
1150         while ((cmd = ub_cmdq_peek(sc)) != NULL) {
1151                 if (cmd->state == UB_CMDST_DONE) {
1152                         ub_cmdq_pop(sc);
1153                         (*cmd->done)(sc, cmd);
1154                 } else if (cmd->state == UB_CMDST_INIT) {
1155                         ub_cmdtr_new(sc, cmd);
1156                         if ((rc = ub_scsi_cmd_start(sc, cmd)) == 0)
1157                                 break;
1158                         cmd->error = rc;
1159                         cmd->state = UB_CMDST_DONE;
1160                         ub_cmdtr_state(sc, cmd);
1161                 } else {
1162                         if (!ub_is_completed(&sc->work_done))
1163                                 break;
1164                         ub_scsi_urb_compl(sc, cmd);
1165                 }
1166         }
1167 }
1168
1169 static void ub_scsi_urb_compl(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1170 {
1171         struct urb *urb = &sc->work_urb;
1172         struct bulk_cs_wrap *bcs;
1173         int pipe;
1174         int rc;
1175
1176         if (atomic_read(&sc->poison)) {
1177                 /* A little too simplistic, I feel... */
1178                 goto Bad_End;
1179         }
1180
1181         if (cmd->state == UB_CMDST_CLEAR) {
1182                 if (urb->status == -EPIPE) {
1183                         /*
1184                          * STALL while clearning STALL.
1185                          * The control pipe clears itself - nothing to do.
1186                          * XXX Might try to reset the device here and retry.
1187                          */
1188                         printk(KERN_NOTICE "%s: stall on control pipe\n",
1189                             sc->name);
1190                         goto Bad_End;
1191                 }
1192
1193                 /*
1194                  * We ignore the result for the halt clear.
1195                  */
1196
1197                 /* reset the endpoint toggle */
1198                 usb_settoggle(sc->dev, usb_pipeendpoint(sc->last_pipe),
1199                         usb_pipeout(sc->last_pipe), 0);
1200
1201                 ub_state_sense(sc, cmd);
1202
1203         } else if (cmd->state == UB_CMDST_CLR2STS) {
1204                 if (urb->status == -EPIPE) {
1205                         /*
1206                          * STALL while clearning STALL.
1207                          * The control pipe clears itself - nothing to do.
1208                          * XXX Might try to reset the device here and retry.
1209                          */
1210                         printk(KERN_NOTICE "%s: stall on control pipe\n",
1211                             sc->name);
1212                         goto Bad_End;
1213                 }
1214
1215                 /*
1216                  * We ignore the result for the halt clear.
1217                  */
1218
1219                 /* reset the endpoint toggle */
1220                 usb_settoggle(sc->dev, usb_pipeendpoint(sc->last_pipe),
1221                         usb_pipeout(sc->last_pipe), 0);
1222
1223                 ub_state_stat(sc, cmd);
1224
1225         } else if (cmd->state == UB_CMDST_CLRRS) {
1226                 if (urb->status == -EPIPE) {
1227                         /*
1228                          * STALL while clearning STALL.
1229                          * The control pipe clears itself - nothing to do.
1230                          * XXX Might try to reset the device here and retry.
1231                          */
1232                         printk(KERN_NOTICE "%s: stall on control pipe\n",
1233                             sc->name);
1234                         goto Bad_End;
1235                 }
1236
1237                 /*
1238                  * We ignore the result for the halt clear.
1239                  */
1240
1241                 /* reset the endpoint toggle */
1242                 usb_settoggle(sc->dev, usb_pipeendpoint(sc->last_pipe),
1243                         usb_pipeout(sc->last_pipe), 0);
1244
1245                 ub_state_stat_counted(sc, cmd);
1246
1247         } else if (cmd->state == UB_CMDST_CMD) {
1248                 if (urb->status == -EPIPE) {
1249                         rc = ub_submit_clear_stall(sc, cmd, sc->last_pipe);
1250                         if (rc != 0) {
1251                                 printk(KERN_NOTICE "%s: "
1252                                     "unable to submit clear (%d)\n",
1253                                     sc->name, rc);
1254                                 /*
1255                                  * This is typically ENOMEM or some other such shit.
1256                                  * Retrying is pointless. Just do Bad End on it...
1257                                  */
1258                                 goto Bad_End;
1259                         }
1260                         cmd->state = UB_CMDST_CLEAR;
1261                         ub_cmdtr_state(sc, cmd);
1262                         return;
1263                 }
1264                 if (urb->status != 0) {
1265                         printk("ub: cmd #%d cmd status (%d)\n", cmd->tag, urb->status); /* P3 */
1266                         goto Bad_End;
1267                 }
1268                 if (urb->actual_length != US_BULK_CB_WRAP_LEN) {
1269                         printk("ub: cmd #%d xferred %d\n", cmd->tag, urb->actual_length); /* P3 */
1270                         /* XXX Must do reset here to unconfuse the device */
1271                         goto Bad_End;
1272                 }
1273
1274                 if (cmd->dir == UB_DIR_NONE) {
1275                         ub_state_stat(sc, cmd);
1276                         return;
1277                 }
1278
1279                 UB_INIT_COMPLETION(sc->work_done);
1280
1281                 if (cmd->dir == UB_DIR_READ)
1282                         pipe = sc->recv_bulk_pipe;
1283                 else
1284                         pipe = sc->send_bulk_pipe;
1285                 sc->last_pipe = pipe;
1286                 usb_fill_bulk_urb(&sc->work_urb, sc->dev, pipe,
1287                     cmd->data, cmd->len, ub_urb_complete, sc);
1288                 sc->work_urb.transfer_flags = URB_ASYNC_UNLINK;
1289                 sc->work_urb.actual_length = 0;
1290                 sc->work_urb.error_count = 0;
1291                 sc->work_urb.status = 0;
1292
1293                 if ((rc = usb_submit_urb(&sc->work_urb, GFP_ATOMIC)) != 0) {
1294                         /* XXX Clear stalls */
1295                         printk("ub: data #%d submit failed (%d)\n", cmd->tag, rc); /* P3 */
1296                         ub_complete(&sc->work_done);
1297                         ub_state_done(sc, cmd, rc);
1298                         return;
1299                 }
1300
1301                 sc->work_timer.expires = jiffies + UB_DATA_TIMEOUT;
1302                 add_timer(&sc->work_timer);
1303
1304                 cmd->state = UB_CMDST_DATA;
1305                 ub_cmdtr_state(sc, cmd);
1306
1307         } else if (cmd->state == UB_CMDST_DATA) {
1308                 if (urb->status == -EPIPE) {
1309                         rc = ub_submit_clear_stall(sc, cmd, sc->last_pipe);
1310                         if (rc != 0) {
1311                                 printk(KERN_NOTICE "%s: "
1312                                     "unable to submit clear (%d)\n",
1313                                     sc->name, rc);
1314                                 /*
1315                                  * This is typically ENOMEM or some other such shit.
1316                                  * Retrying is pointless. Just do Bad End on it...
1317                                  */
1318                                 goto Bad_End;
1319                         }
1320                         cmd->state = UB_CMDST_CLR2STS;
1321                         ub_cmdtr_state(sc, cmd);
1322                         return;
1323                 }
1324                 if (urb->status == -EOVERFLOW) {
1325                         /*
1326                          * A babble? Failure, but we must transfer CSW now.
1327                          */
1328                         cmd->error = -EOVERFLOW;        /* A cheap trick... */
1329                 } else {
1330                         if (urb->status != 0)
1331                                 goto Bad_End;
1332                 }
1333
1334                 cmd->act_len = urb->actual_length;
1335                 ub_cmdtr_act_len(sc, cmd);
1336
1337                 ub_state_stat(sc, cmd);
1338
1339         } else if (cmd->state == UB_CMDST_STAT) {
1340                 if (urb->status == -EPIPE) {
1341                         rc = ub_submit_clear_stall(sc, cmd, sc->last_pipe);
1342                         if (rc != 0) {
1343                                 printk(KERN_NOTICE "%s: "
1344                                     "unable to submit clear (%d)\n",
1345                                     sc->name, rc);
1346                                 /*
1347                                  * This is typically ENOMEM or some other such shit.
1348                                  * Retrying is pointless. Just do Bad End on it...
1349                                  */
1350                                 goto Bad_End;
1351                         }
1352
1353                         /*
1354                          * Having a stall when getting CSW is an error, so
1355                          * make sure uppper levels are not oblivious to it.
1356                          */
1357                         cmd->error = -EIO;              /* A cheap trick... */
1358
1359                         cmd->state = UB_CMDST_CLRRS;
1360                         ub_cmdtr_state(sc, cmd);
1361                         return;
1362                 }
1363                 if (urb->status == -EOVERFLOW) {
1364                         /*
1365                          * XXX We are screwed here. Retrying is pointless,
1366                          * because the pipelined data will not get in until
1367                          * we read with a big enough buffer. We must reset XXX.
1368                          */
1369                         goto Bad_End;
1370                 }
1371                 if (urb->status != 0)
1372                         goto Bad_End;
1373
1374                 if (urb->actual_length == 0) {
1375                         ub_state_stat_counted(sc, cmd);
1376                         return;
1377                 }
1378
1379                 /*
1380                  * Check the returned Bulk protocol status.
1381                  * The status block has to be validated first.
1382                  */
1383
1384                 bcs = &sc->work_bcs;
1385
1386                 if (sc->signature == cpu_to_le32(0)) {
1387                         /*
1388                          * This is the first reply, so do not perform the check.
1389                          * Instead, remember the signature the device uses
1390                          * for future checks. But do not allow a nul.
1391                          */
1392                         sc->signature = bcs->Signature;
1393                         if (sc->signature == cpu_to_le32(0)) {
1394                                 ub_state_stat_counted(sc, cmd);
1395                                 return;
1396                         }
1397                 } else {
1398                         if (bcs->Signature != sc->signature) {
1399                                 ub_state_stat_counted(sc, cmd);
1400                                 return;
1401                         }
1402                 }
1403
1404                 if (bcs->Tag != cmd->tag) {
1405                         /*
1406                          * This usually happens when we disagree with the
1407                          * device's microcode about something. For instance,
1408                          * a few of them throw this after timeouts. They buffer
1409                          * commands and reply at commands we timed out before.
1410                          * Without flushing these replies we loop forever.
1411                          */
1412                         ub_state_stat_counted(sc, cmd);
1413                         return;
1414                 }
1415
1416                 rc = le32_to_cpu(bcs->Residue);
1417                 if (rc != cmd->len - cmd->act_len) {
1418                         /*
1419                          * It is all right to transfer less, the caller has
1420                          * to check. But it's not all right if the device
1421                          * counts disagree with our counts.
1422                          */
1423                         /* P3 */ printk("%s: resid %d len %d act %d\n",
1424                             sc->name, rc, cmd->len, cmd->act_len);
1425                         goto Bad_End;
1426                 }
1427
1428                 switch (bcs->Status) {
1429                 case US_BULK_STAT_OK:
1430                         break;
1431                 case US_BULK_STAT_FAIL:
1432                         ub_state_sense(sc, cmd);
1433                         return;
1434                 case US_BULK_STAT_PHASE:
1435                         /* XXX We must reset the transport here */
1436                         /* P3 */ printk("%s: status PHASE\n", sc->name);
1437                         goto Bad_End;
1438                 default:
1439                         printk(KERN_INFO "%s: unknown CSW status 0x%x\n",
1440                             sc->name, bcs->Status);
1441                         goto Bad_End;
1442                 }
1443
1444                 /* Not zeroing error to preserve a babble indicator */
1445                 if (cmd->error != 0) {
1446                         ub_state_sense(sc, cmd);
1447                         return;
1448                 }
1449                 cmd->state = UB_CMDST_DONE;
1450                 ub_cmdtr_state(sc, cmd);
1451                 ub_cmdq_pop(sc);
1452                 (*cmd->done)(sc, cmd);
1453
1454         } else if (cmd->state == UB_CMDST_SENSE) {
1455                 ub_state_done(sc, cmd, -EIO);
1456
1457         } else {
1458                 printk(KERN_WARNING "%s: "
1459                     "wrong command state %d\n",
1460                     sc->name, cmd->state);
1461                 goto Bad_End;
1462         }
1463         return;
1464
1465 Bad_End: /* Little Excel is dead */
1466         ub_state_done(sc, cmd, -EIO);
1467 }
1468
1469 /*
1470  * Factorization helper for the command state machine:
1471  * Finish the command.
1472  */
1473 static void ub_state_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd, int rc)
1474 {
1475
1476         cmd->error = rc;
1477         cmd->state = UB_CMDST_DONE;
1478         ub_cmdtr_state(sc, cmd);
1479         ub_cmdq_pop(sc);
1480         (*cmd->done)(sc, cmd);
1481 }
1482
1483 /*
1484  * Factorization helper for the command state machine:
1485  * Submit a CSW read.
1486  */
1487 static int __ub_state_stat(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1488 {
1489         int rc;
1490
1491         UB_INIT_COMPLETION(sc->work_done);
1492
1493         sc->last_pipe = sc->recv_bulk_pipe;
1494         usb_fill_bulk_urb(&sc->work_urb, sc->dev, sc->recv_bulk_pipe,
1495             &sc->work_bcs, US_BULK_CS_WRAP_LEN, ub_urb_complete, sc);
1496         sc->work_urb.transfer_flags = URB_ASYNC_UNLINK;
1497         sc->work_urb.actual_length = 0;
1498         sc->work_urb.error_count = 0;
1499         sc->work_urb.status = 0;
1500
1501         if ((rc = usb_submit_urb(&sc->work_urb, GFP_ATOMIC)) != 0) {
1502                 /* XXX Clear stalls */
1503                 ub_complete(&sc->work_done);
1504                 ub_state_done(sc, cmd, rc);
1505                 return -1;
1506         }
1507
1508         sc->work_timer.expires = jiffies + UB_STAT_TIMEOUT;
1509         add_timer(&sc->work_timer);
1510         return 0;
1511 }
1512
1513 /*
1514  * Factorization helper for the command state machine:
1515  * Submit a CSW read and go to STAT state.
1516  */
1517 static void ub_state_stat(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1518 {
1519
1520         if (__ub_state_stat(sc, cmd) != 0)
1521                 return;
1522
1523         cmd->stat_count = 0;
1524         cmd->state = UB_CMDST_STAT;
1525         ub_cmdtr_state(sc, cmd);
1526 }
1527
1528 /*
1529  * Factorization helper for the command state machine:
1530  * Submit a CSW read and go to STAT state with counter (along [C] path).
1531  */
1532 static void ub_state_stat_counted(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1533 {
1534
1535         if (++cmd->stat_count >= 4) {
1536                 ub_state_sense(sc, cmd);
1537                 return;
1538         }
1539
1540         if (__ub_state_stat(sc, cmd) != 0)
1541                 return;
1542
1543         cmd->state = UB_CMDST_STAT;
1544         ub_cmdtr_state(sc, cmd);
1545 }
1546
1547 /*
1548  * Factorization helper for the command state machine:
1549  * Submit a REQUEST SENSE and go to SENSE state.
1550  */
1551 static void ub_state_sense(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1552 {
1553         struct ub_scsi_cmd *scmd;
1554         int rc;
1555
1556         if (cmd->cdb[0] == REQUEST_SENSE) {
1557                 rc = -EPIPE;
1558                 goto error;
1559         }
1560
1561         scmd = &sc->top_rqs_cmd;
1562         scmd->cdb[0] = REQUEST_SENSE;
1563         scmd->cdb[4] = UB_SENSE_SIZE;
1564         scmd->cdb_len = 6;
1565         scmd->dir = UB_DIR_READ;
1566         scmd->state = UB_CMDST_INIT;
1567         scmd->data = sc->top_sense;
1568         scmd->len = UB_SENSE_SIZE;
1569         scmd->lun = cmd->lun;
1570         scmd->done = ub_top_sense_done;
1571         scmd->back = cmd;
1572
1573         scmd->tag = sc->tagcnt++;
1574
1575         cmd->state = UB_CMDST_SENSE;
1576         ub_cmdtr_state(sc, cmd);
1577
1578         ub_cmdq_insert(sc, scmd);
1579         return;
1580
1581 error:
1582         ub_state_done(sc, cmd, rc);
1583 }
1584
1585 /*
1586  * A helper for the command's state machine:
1587  * Submit a stall clear.
1588  */
1589 static int ub_submit_clear_stall(struct ub_dev *sc, struct ub_scsi_cmd *cmd,
1590     int stalled_pipe)
1591 {
1592         int endp;
1593         struct usb_ctrlrequest *cr;
1594         int rc;
1595
1596         endp = usb_pipeendpoint(stalled_pipe);
1597         if (usb_pipein (stalled_pipe))
1598                 endp |= USB_DIR_IN;
1599
1600         cr = &sc->work_cr;
1601         cr->bRequestType = USB_RECIP_ENDPOINT;
1602         cr->bRequest = USB_REQ_CLEAR_FEATURE;
1603         cr->wValue = cpu_to_le16(USB_ENDPOINT_HALT);
1604         cr->wIndex = cpu_to_le16(endp);
1605         cr->wLength = cpu_to_le16(0);
1606
1607         UB_INIT_COMPLETION(sc->work_done);
1608
1609         usb_fill_control_urb(&sc->work_urb, sc->dev, sc->send_ctrl_pipe,
1610             (unsigned char*) cr, NULL, 0, ub_urb_complete, sc);
1611         sc->work_urb.transfer_flags = URB_ASYNC_UNLINK;
1612         sc->work_urb.actual_length = 0;
1613         sc->work_urb.error_count = 0;
1614         sc->work_urb.status = 0;
1615
1616         if ((rc = usb_submit_urb(&sc->work_urb, GFP_ATOMIC)) != 0) {
1617                 ub_complete(&sc->work_done);
1618                 return rc;
1619         }
1620
1621         sc->work_timer.expires = jiffies + UB_CTRL_TIMEOUT;
1622         add_timer(&sc->work_timer);
1623         return 0;
1624 }
1625
1626 /*
1627  */
1628 static void ub_top_sense_done(struct ub_dev *sc, struct ub_scsi_cmd *scmd)
1629 {
1630         unsigned char *sense = scmd->data;
1631         struct ub_scsi_cmd *cmd;
1632
1633         /*
1634          * Ignoring scmd->act_len, because the buffer was pre-zeroed.
1635          */
1636         ub_cmdtr_sense(sc, scmd, sense);
1637
1638         /*
1639          * Find the command which triggered the unit attention or a check,
1640          * save the sense into it, and advance its state machine.
1641          */
1642         if ((cmd = ub_cmdq_peek(sc)) == NULL) {
1643                 printk(KERN_WARNING "%s: sense done while idle\n", sc->name);
1644                 return;
1645         }
1646         if (cmd != scmd->back) {
1647                 printk(KERN_WARNING "%s: "
1648                     "sense done for wrong command 0x%x\n",
1649                     sc->name, cmd->tag);
1650                 return;
1651         }
1652         if (cmd->state != UB_CMDST_SENSE) {
1653                 printk(KERN_WARNING "%s: "
1654                     "sense done with bad cmd state %d\n",
1655                     sc->name, cmd->state);
1656                 return;
1657         }
1658
1659         cmd->key = sense[2] & 0x0F;
1660         cmd->asc = sense[12];
1661         cmd->ascq = sense[13];
1662
1663         ub_scsi_urb_compl(sc, cmd);
1664 }
1665
1666 /*
1667  * This is called from a process context.
1668  */
1669 static void ub_revalidate(struct ub_dev *sc, struct ub_lun *lun)
1670 {
1671
1672         lun->readonly = 0;      /* XXX Query this from the device */
1673
1674         lun->capacity.nsec = 0;
1675         lun->capacity.bsize = 512;
1676         lun->capacity.bshift = 0;
1677
1678         if (ub_sync_tur(sc, lun) != 0)
1679                 return;                 /* Not ready */
1680         lun->changed = 0;
1681
1682         if (ub_sync_read_cap(sc, lun, &lun->capacity) != 0) {
1683                 /*
1684                  * The retry here means something is wrong, either with the
1685                  * device, with the transport, or with our code.
1686                  * We keep this because sd.c has retries for capacity.
1687                  */
1688                 if (ub_sync_read_cap(sc, lun, &lun->capacity) != 0) {
1689                         lun->capacity.nsec = 0;
1690                         lun->capacity.bsize = 512;
1691                         lun->capacity.bshift = 0;
1692                 }
1693         }
1694 }
1695
1696 /*
1697  * The open funcion.
1698  * This is mostly needed to keep refcounting, but also to support
1699  * media checks on removable media drives.
1700  */
1701 static int ub_bd_open(struct inode *inode, struct file *filp)
1702 {
1703         struct gendisk *disk = inode->i_bdev->bd_disk;
1704         struct ub_lun *lun;
1705         struct ub_dev *sc;
1706         unsigned long flags;
1707         int rc;
1708
1709         if ((lun = disk->private_data) == NULL)
1710                 return -ENXIO;
1711         sc = lun->udev;
1712
1713         spin_lock_irqsave(&ub_lock, flags);
1714         if (atomic_read(&sc->poison)) {
1715                 spin_unlock_irqrestore(&ub_lock, flags);
1716                 return -ENXIO;
1717         }
1718         sc->openc++;
1719         spin_unlock_irqrestore(&ub_lock, flags);
1720
1721         /*
1722          * This is a workaround for a specific problem in our block layer.
1723          * In 2.6.9, register_disk duplicates the code from rescan_partitions.
1724          * However, if we do add_disk with a device which persistently reports
1725          * a changed media, add_disk calls register_disk, which does do_open,
1726          * which will call rescan_paritions for changed media. After that,
1727          * register_disk attempts to do it all again and causes double kobject
1728          * registration and a eventually an oops on module removal.
1729          *
1730          * The bottom line is, Al Viro says that we should not allow
1731          * bdev->bd_invalidated to be set when doing add_disk no matter what.
1732          */
1733         if (lun->first_open) {
1734                 lun->first_open = 0;
1735                 if (lun->changed) {
1736                         rc = -ENOMEDIUM;
1737                         goto err_open;
1738                 }
1739         }
1740
1741         if (lun->removable || lun->readonly)
1742                 check_disk_change(inode->i_bdev);
1743
1744         /*
1745          * The sd.c considers ->media_present and ->changed not equivalent,
1746          * under some pretty murky conditions (a failure of READ CAPACITY).
1747          * We may need it one day.
1748          */
1749         if (lun->removable && lun->changed && !(filp->f_flags & O_NDELAY)) {
1750                 rc = -ENOMEDIUM;
1751                 goto err_open;
1752         }
1753
1754         if (lun->readonly && (filp->f_mode & FMODE_WRITE)) {
1755                 rc = -EROFS;
1756                 goto err_open;
1757         }
1758
1759         return 0;
1760
1761 err_open:
1762         ub_put(sc);
1763         return rc;
1764 }
1765
1766 /*
1767  */
1768 static int ub_bd_release(struct inode *inode, struct file *filp)
1769 {
1770         struct gendisk *disk = inode->i_bdev->bd_disk;
1771         struct ub_lun *lun = disk->private_data;
1772         struct ub_dev *sc = lun->udev;
1773
1774         ub_put(sc);
1775         return 0;
1776 }
1777
1778 /*
1779  * The ioctl interface.
1780  */
1781 static int ub_bd_ioctl(struct inode *inode, struct file *filp,
1782     unsigned int cmd, unsigned long arg)
1783 {
1784         struct gendisk *disk = inode->i_bdev->bd_disk;
1785         void __user *usermem = (void __user *) arg;
1786
1787         return scsi_cmd_ioctl(filp, disk, cmd, usermem);
1788 }
1789
1790 /*
1791  * This is called once a new disk was seen by the block layer or by ub_probe().
1792  * The main onjective here is to discover the features of the media such as
1793  * the capacity, read-only status, etc. USB storage generally does not
1794  * need to be spun up, but if we needed it, this would be the place.
1795  *
1796  * This call can sleep.
1797  *
1798  * The return code is not used.
1799  */
1800 static int ub_bd_revalidate(struct gendisk *disk)
1801 {
1802         struct ub_lun *lun = disk->private_data;
1803
1804         ub_revalidate(lun->udev, lun);
1805
1806         /* XXX Support sector size switching like in sr.c */
1807         blk_queue_hardsect_size(disk->queue, lun->capacity.bsize);
1808         set_capacity(disk, lun->capacity.nsec);
1809         // set_disk_ro(sdkp->disk, lun->readonly);
1810
1811         return 0;
1812 }
1813
1814 /*
1815  * The check is called by the block layer to verify if the media
1816  * is still available. It is supposed to be harmless, lightweight and
1817  * non-intrusive in case the media was not changed.
1818  *
1819  * This call can sleep.
1820  *
1821  * The return code is bool!
1822  */
1823 static int ub_bd_media_changed(struct gendisk *disk)
1824 {
1825         struct ub_lun *lun = disk->private_data;
1826
1827         if (!lun->removable)
1828                 return 0;
1829
1830         /*
1831          * We clean checks always after every command, so this is not
1832          * as dangerous as it looks. If the TEST_UNIT_READY fails here,
1833          * the device is actually not ready with operator or software
1834          * intervention required. One dangerous item might be a drive which
1835          * spins itself down, and come the time to write dirty pages, this
1836          * will fail, then block layer discards the data. Since we never
1837          * spin drives up, such devices simply cannot be used with ub anyway.
1838          */
1839         if (ub_sync_tur(lun->udev, lun) != 0) {
1840                 lun->changed = 1;
1841                 return 1;
1842         }
1843
1844         return lun->changed;
1845 }
1846
1847 static struct block_device_operations ub_bd_fops = {
1848         .owner          = THIS_MODULE,
1849         .open           = ub_bd_open,
1850         .release        = ub_bd_release,
1851         .ioctl          = ub_bd_ioctl,
1852         .media_changed  = ub_bd_media_changed,
1853         .revalidate_disk = ub_bd_revalidate,
1854 };
1855
1856 /*
1857  * Common ->done routine for commands executed synchronously.
1858  */
1859 static void ub_probe_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1860 {
1861         struct completion *cop = cmd->back;
1862         complete(cop);
1863 }
1864
1865 /*
1866  * Test if the device has a check condition on it, synchronously.
1867  */
1868 static int ub_sync_tur(struct ub_dev *sc, struct ub_lun *lun)
1869 {
1870         struct ub_scsi_cmd *cmd;
1871         enum { ALLOC_SIZE = sizeof(struct ub_scsi_cmd) };
1872         unsigned long flags;
1873         struct completion compl;
1874         int rc;
1875
1876         init_completion(&compl);
1877
1878         rc = -ENOMEM;
1879         if ((cmd = kmalloc(ALLOC_SIZE, GFP_KERNEL)) == NULL)
1880                 goto err_alloc;
1881         memset(cmd, 0, ALLOC_SIZE);
1882
1883         cmd->cdb[0] = TEST_UNIT_READY;
1884         cmd->cdb_len = 6;
1885         cmd->dir = UB_DIR_NONE;
1886         cmd->state = UB_CMDST_INIT;
1887         cmd->lun = lun;                 /* This may be NULL, but that's ok */
1888         cmd->done = ub_probe_done;
1889         cmd->back = &compl;
1890
1891         spin_lock_irqsave(&sc->lock, flags);
1892         cmd->tag = sc->tagcnt++;
1893
1894         rc = ub_submit_scsi(sc, cmd);
1895         spin_unlock_irqrestore(&sc->lock, flags);
1896
1897         if (rc != 0) {
1898                 printk("ub: testing ready: submit error (%d)\n", rc); /* P3 */
1899                 goto err_submit;
1900         }
1901
1902         wait_for_completion(&compl);
1903
1904         rc = cmd->error;
1905
1906         if (rc == -EIO && cmd->key != 0)        /* Retries for benh's key */
1907                 rc = cmd->key;
1908
1909 err_submit:
1910         kfree(cmd);
1911 err_alloc:
1912         return rc;
1913 }
1914
1915 /*
1916  * Read the SCSI capacity synchronously (for probing).
1917  */
1918 static int ub_sync_read_cap(struct ub_dev *sc, struct ub_lun *lun,
1919     struct ub_capacity *ret)
1920 {
1921         struct ub_scsi_cmd *cmd;
1922         char *p;
1923         enum { ALLOC_SIZE = sizeof(struct ub_scsi_cmd) + 8 };
1924         unsigned long flags;
1925         unsigned int bsize, shift;
1926         unsigned long nsec;
1927         struct completion compl;
1928         int rc;
1929
1930         init_completion(&compl);
1931
1932         rc = -ENOMEM;
1933         if ((cmd = kmalloc(ALLOC_SIZE, GFP_KERNEL)) == NULL)
1934                 goto err_alloc;
1935         memset(cmd, 0, ALLOC_SIZE);
1936         p = (char *)cmd + sizeof(struct ub_scsi_cmd);
1937
1938         cmd->cdb[0] = 0x25;
1939         cmd->cdb_len = 10;
1940         cmd->dir = UB_DIR_READ;
1941         cmd->state = UB_CMDST_INIT;
1942         cmd->data = p;
1943         cmd->len = 8;
1944         cmd->lun = lun;
1945         cmd->done = ub_probe_done;
1946         cmd->back = &compl;
1947
1948         spin_lock_irqsave(&sc->lock, flags);
1949         cmd->tag = sc->tagcnt++;
1950
1951         rc = ub_submit_scsi(sc, cmd);
1952         spin_unlock_irqrestore(&sc->lock, flags);
1953
1954         if (rc != 0) {
1955                 printk("ub: reading capacity: submit error (%d)\n", rc); /* P3 */
1956                 goto err_submit;
1957         }
1958
1959         wait_for_completion(&compl);
1960
1961         if (cmd->error != 0) {
1962                 printk("ub: reading capacity: error %d\n", cmd->error); /* P3 */
1963                 rc = -EIO;
1964                 goto err_read;
1965         }
1966         if (cmd->act_len != 8) {
1967                 printk("ub: reading capacity: size %d\n", cmd->act_len); /* P3 */
1968                 rc = -EIO;
1969                 goto err_read;
1970         }
1971
1972         /* sd.c special-cases sector size of 0 to mean 512. Needed? Safe? */
1973         nsec = be32_to_cpu(*(__be32 *)p) + 1;
1974         bsize = be32_to_cpu(*(__be32 *)(p + 4));
1975         switch (bsize) {
1976         case 512:       shift = 0;      break;
1977         case 1024:      shift = 1;      break;
1978         case 2048:      shift = 2;      break;
1979         case 4096:      shift = 3;      break;
1980         default:
1981                 printk("ub: Bad sector size %u\n", bsize); /* P3 */
1982                 rc = -EDOM;
1983                 goto err_inv_bsize;
1984         }
1985
1986         ret->bsize = bsize;
1987         ret->bshift = shift;
1988         ret->nsec = nsec << shift;
1989         rc = 0;
1990
1991 err_inv_bsize:
1992 err_read:
1993 err_submit:
1994         kfree(cmd);
1995 err_alloc:
1996         return rc;
1997 }
1998
1999 /*
2000  */
2001 static void ub_probe_urb_complete(struct urb *urb, struct pt_regs *pt)
2002 {
2003         struct completion *cop = urb->context;
2004         complete(cop);
2005 }
2006
2007 static void ub_probe_timeout(unsigned long arg)
2008 {
2009         struct completion *cop = (struct completion *) arg;
2010         complete(cop);
2011 }
2012
2013 /*
2014  * Get number of LUNs by the way of Bulk GetMaxLUN command.
2015  */
2016 static int ub_sync_getmaxlun(struct ub_dev *sc)
2017 {
2018         int ifnum = sc->intf->cur_altsetting->desc.bInterfaceNumber;
2019         unsigned char *p;
2020         enum { ALLOC_SIZE = 1 };
2021         struct usb_ctrlrequest *cr;
2022         struct completion compl;
2023         struct timer_list timer;
2024         int nluns;
2025         int rc;
2026
2027         init_completion(&compl);
2028
2029         rc = -ENOMEM;
2030         if ((p = kmalloc(ALLOC_SIZE, GFP_KERNEL)) == NULL)
2031                 goto err_alloc;
2032         *p = 55;
2033
2034         cr = &sc->work_cr;
2035         cr->bRequestType = USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
2036         cr->bRequest = US_BULK_GET_MAX_LUN;
2037         cr->wValue = cpu_to_le16(0);
2038         cr->wIndex = cpu_to_le16(ifnum);
2039         cr->wLength = cpu_to_le16(1);
2040
2041         usb_fill_control_urb(&sc->work_urb, sc->dev, sc->recv_ctrl_pipe,
2042             (unsigned char*) cr, p, 1, ub_probe_urb_complete, &compl);
2043         sc->work_urb.transfer_flags = 0;
2044         sc->work_urb.actual_length = 0;
2045         sc->work_urb.error_count = 0;
2046         sc->work_urb.status = 0;
2047
2048         if ((rc = usb_submit_urb(&sc->work_urb, GFP_KERNEL)) != 0) {
2049                 if (rc == -EPIPE) {
2050                         printk("%s: Stall at GetMaxLUN, using 1 LUN\n",
2051                              sc->name); /* P3 */
2052                 } else {
2053                         printk(KERN_WARNING
2054                              "%s: Unable to submit GetMaxLUN (%d)\n",
2055                              sc->name, rc);
2056                 }
2057                 goto err_submit;
2058         }
2059
2060         init_timer(&timer);
2061         timer.function = ub_probe_timeout;
2062         timer.data = (unsigned long) &compl;
2063         timer.expires = jiffies + UB_CTRL_TIMEOUT;
2064         add_timer(&timer);
2065
2066         wait_for_completion(&compl);
2067
2068         del_timer_sync(&timer);
2069         usb_kill_urb(&sc->work_urb);
2070
2071         if (sc->work_urb.actual_length != 1) {
2072                 printk("%s: GetMaxLUN returned %d bytes\n", sc->name,
2073                     sc->work_urb.actual_length); /* P3 */
2074                 nluns = 0;
2075         } else {
2076                 if ((nluns = *p) == 55) {
2077                         nluns = 0;
2078                 } else {
2079                         /* GetMaxLUN returns the maximum LUN number */
2080                         nluns += 1;
2081                         if (nluns > UB_MAX_LUNS)
2082                                 nluns = UB_MAX_LUNS;
2083                 }
2084                 printk("%s: GetMaxLUN returned %d, using %d LUNs\n", sc->name,
2085                     *p, nluns); /* P3 */
2086         }
2087
2088         kfree(p);
2089         return nluns;
2090
2091 err_submit:
2092         kfree(p);
2093 err_alloc:
2094         return rc;
2095 }
2096
2097 /*
2098  * Clear initial stalls.
2099  */
2100 static int ub_probe_clear_stall(struct ub_dev *sc, int stalled_pipe)
2101 {
2102         int endp;
2103         struct usb_ctrlrequest *cr;
2104         struct completion compl;
2105         struct timer_list timer;
2106         int rc;
2107
2108         init_completion(&compl);
2109
2110         endp = usb_pipeendpoint(stalled_pipe);
2111         if (usb_pipein (stalled_pipe))
2112                 endp |= USB_DIR_IN;
2113
2114         cr = &sc->work_cr;
2115         cr->bRequestType = USB_RECIP_ENDPOINT;
2116         cr->bRequest = USB_REQ_CLEAR_FEATURE;
2117         cr->wValue = cpu_to_le16(USB_ENDPOINT_HALT);
2118         cr->wIndex = cpu_to_le16(endp);
2119         cr->wLength = cpu_to_le16(0);
2120
2121         usb_fill_control_urb(&sc->work_urb, sc->dev, sc->send_ctrl_pipe,
2122             (unsigned char*) cr, NULL, 0, ub_probe_urb_complete, &compl);
2123         sc->work_urb.transfer_flags = 0;
2124         sc->work_urb.actual_length = 0;
2125         sc->work_urb.error_count = 0;
2126         sc->work_urb.status = 0;
2127
2128         if ((rc = usb_submit_urb(&sc->work_urb, GFP_KERNEL)) != 0) {
2129                 printk(KERN_WARNING
2130                      "%s: Unable to submit a probe clear (%d)\n", sc->name, rc);
2131                 return rc;
2132         }
2133
2134         init_timer(&timer);
2135         timer.function = ub_probe_timeout;
2136         timer.data = (unsigned long) &compl;
2137         timer.expires = jiffies + UB_CTRL_TIMEOUT;
2138         add_timer(&timer);
2139
2140         wait_for_completion(&compl);
2141
2142         del_timer_sync(&timer);
2143         usb_kill_urb(&sc->work_urb);
2144
2145         /* reset the endpoint toggle */
2146         usb_settoggle(sc->dev, endp, usb_pipeout(sc->last_pipe), 0);
2147
2148         return 0;
2149 }
2150
2151 /*
2152  * Get the pipe settings.
2153  */
2154 static int ub_get_pipes(struct ub_dev *sc, struct usb_device *dev,
2155     struct usb_interface *intf)
2156 {
2157         struct usb_host_interface *altsetting = intf->cur_altsetting;
2158         struct usb_endpoint_descriptor *ep_in = NULL;
2159         struct usb_endpoint_descriptor *ep_out = NULL;
2160         struct usb_endpoint_descriptor *ep;
2161         int i;
2162
2163         /*
2164          * Find the endpoints we need.
2165          * We are expecting a minimum of 2 endpoints - in and out (bulk).
2166          * We will ignore any others.
2167          */
2168         for (i = 0; i < altsetting->desc.bNumEndpoints; i++) {
2169                 ep = &altsetting->endpoint[i].desc;
2170
2171                 /* Is it a BULK endpoint? */
2172                 if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
2173                                 == USB_ENDPOINT_XFER_BULK) {
2174                         /* BULK in or out? */
2175                         if (ep->bEndpointAddress & USB_DIR_IN)
2176                                 ep_in = ep;
2177                         else
2178                                 ep_out = ep;
2179                 }
2180         }
2181
2182         if (ep_in == NULL || ep_out == NULL) {
2183                 printk(KERN_NOTICE "%s: failed endpoint check\n",
2184                     sc->name);
2185                 return -EIO;
2186         }
2187
2188         /* Calculate and store the pipe values */
2189         sc->send_ctrl_pipe = usb_sndctrlpipe(dev, 0);
2190         sc->recv_ctrl_pipe = usb_rcvctrlpipe(dev, 0);
2191         sc->send_bulk_pipe = usb_sndbulkpipe(dev,
2192                 ep_out->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
2193         sc->recv_bulk_pipe = usb_rcvbulkpipe(dev, 
2194                 ep_in->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
2195
2196         return 0;
2197 }
2198
2199 /*
2200  * Probing is done in the process context, which allows us to cheat
2201  * and not to build a state machine for the discovery.
2202  */
2203 static int ub_probe(struct usb_interface *intf,
2204     const struct usb_device_id *dev_id)
2205 {
2206         struct ub_dev *sc;
2207         int nluns;
2208         int rc;
2209         int i;
2210
2211         rc = -ENOMEM;
2212         if ((sc = kmalloc(sizeof(struct ub_dev), GFP_KERNEL)) == NULL)
2213                 goto err_core;
2214         memset(sc, 0, sizeof(struct ub_dev));
2215         spin_lock_init(&sc->lock);
2216         INIT_LIST_HEAD(&sc->luns);
2217         usb_init_urb(&sc->work_urb);
2218         tasklet_init(&sc->tasklet, ub_scsi_action, (unsigned long)sc);
2219         atomic_set(&sc->poison, 0);
2220
2221         init_timer(&sc->work_timer);
2222         sc->work_timer.data = (unsigned long) sc;
2223         sc->work_timer.function = ub_urb_timeout;
2224
2225         ub_init_completion(&sc->work_done);
2226         sc->work_done.done = 1;         /* A little yuk, but oh well... */
2227
2228         sc->dev = interface_to_usbdev(intf);
2229         sc->intf = intf;
2230         // sc->ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
2231         usb_set_intfdata(intf, sc);
2232         usb_get_dev(sc->dev);
2233         // usb_get_intf(sc->intf);      /* Do we need this? */
2234
2235         snprintf(sc->name, 12, DRV_NAME "(%d.%d)",
2236             sc->dev->bus->busnum, sc->dev->devnum);
2237
2238         /* XXX Verify that we can handle the device (from descriptors) */
2239
2240         ub_get_pipes(sc, sc->dev, intf);
2241
2242         if (device_create_file(&sc->intf->dev, &dev_attr_diag) != 0)
2243                 goto err_diag;
2244
2245         /*
2246          * At this point, all USB initialization is done, do upper layer.
2247          * We really hate halfway initialized structures, so from the
2248          * invariants perspective, this ub_dev is fully constructed at
2249          * this point.
2250          */
2251
2252         /*
2253          * This is needed to clear toggles. It is a problem only if we do
2254          * `rmmod ub && modprobe ub` without disconnects, but we like that.
2255          */
2256         ub_probe_clear_stall(sc, sc->recv_bulk_pipe);
2257         ub_probe_clear_stall(sc, sc->send_bulk_pipe);
2258
2259         /*
2260          * The way this is used by the startup code is a little specific.
2261          * A SCSI check causes a USB stall. Our common case code sees it
2262          * and clears the check, after which the device is ready for use.
2263          * But if a check was not present, any command other than
2264          * TEST_UNIT_READY ends with a lockup (including REQUEST_SENSE).
2265          *
2266          * If we neglect to clear the SCSI check, the first real command fails
2267          * (which is the capacity readout). We clear that and retry, but why
2268          * causing spurious retries for no reason.
2269          *
2270          * Revalidation may start with its own TEST_UNIT_READY, but that one
2271          * has to succeed, so we clear checks with an additional one here.
2272          * In any case it's not our business how revaliadation is implemented.
2273          */
2274         for (i = 0; i < 3; i++) {       /* Retries for benh's key */
2275                 if ((rc = ub_sync_tur(sc, NULL)) <= 0) break;
2276                 if (rc != 0x6) break;
2277                 msleep(10);
2278         }
2279
2280         nluns = 1;
2281         for (i = 0; i < 3; i++) {
2282                 if ((rc = ub_sync_getmaxlun(sc)) < 0) {
2283                         /* 
2284                          * Some devices (i.e. Iomega Zip100) need this --
2285                          * apparently the bulk pipes get STALLed when the
2286                          * GetMaxLUN request is processed.
2287                          * XXX I have a ZIP-100, verify it does this.
2288                          */
2289                         if (rc == -EPIPE) {
2290                                 ub_probe_clear_stall(sc, sc->recv_bulk_pipe);
2291                                 ub_probe_clear_stall(sc, sc->send_bulk_pipe);
2292                         }
2293                         break;
2294                 }
2295                 if (rc != 0) {
2296                         nluns = rc;
2297                         break;
2298                 }
2299                 msleep(100);
2300         }
2301
2302         for (i = 0; i < nluns; i++) {
2303                 ub_probe_lun(sc, i);
2304         }
2305         return 0;
2306
2307         /* device_remove_file(&sc->intf->dev, &dev_attr_diag); */
2308 err_diag:
2309         usb_set_intfdata(intf, NULL);
2310         // usb_put_intf(sc->intf);
2311         usb_put_dev(sc->dev);
2312         kfree(sc);
2313 err_core:
2314         return rc;
2315 }
2316
2317 static int ub_probe_lun(struct ub_dev *sc, int lnum)
2318 {
2319         struct ub_lun *lun;
2320         request_queue_t *q;
2321         struct gendisk *disk;
2322         int rc;
2323
2324         rc = -ENOMEM;
2325         if ((lun = kmalloc(sizeof(struct ub_lun), GFP_KERNEL)) == NULL)
2326                 goto err_alloc;
2327         memset(lun, 0, sizeof(struct ub_lun));
2328         lun->num = lnum;
2329
2330         rc = -ENOSR;
2331         if ((lun->id = ub_id_get()) == -1)
2332                 goto err_id;
2333
2334         lun->udev = sc;
2335         list_add(&lun->link, &sc->luns);
2336
2337         snprintf(lun->name, 16, DRV_NAME "%c(%d.%d.%d)",
2338             lun->id + 'a', sc->dev->bus->busnum, sc->dev->devnum, lun->num);
2339
2340         lun->removable = 1;             /* XXX Query this from the device */
2341         lun->changed = 1;               /* ub_revalidate clears only */
2342         lun->first_open = 1;
2343         ub_revalidate(sc, lun);
2344
2345         rc = -ENOMEM;
2346         if ((disk = alloc_disk(UB_MINORS_PER_MAJOR)) == NULL)
2347                 goto err_diskalloc;
2348
2349         lun->disk = disk;
2350         sprintf(disk->disk_name, DRV_NAME "%c", lun->id + 'a');
2351         sprintf(disk->devfs_name, DEVFS_NAME "/%c", lun->id + 'a');
2352         disk->major = UB_MAJOR;
2353         disk->first_minor = lun->id * UB_MINORS_PER_MAJOR;
2354         disk->fops = &ub_bd_fops;
2355         disk->private_data = lun;
2356         disk->driverfs_dev = &sc->intf->dev;    /* XXX Many to one ok? */
2357
2358         rc = -ENOMEM;
2359         if ((q = blk_init_queue(ub_bd_rq_fn, &sc->lock)) == NULL)
2360                 goto err_blkqinit;
2361
2362         disk->queue = q;
2363
2364         blk_queue_bounce_limit(q, BLK_BOUNCE_HIGH);
2365         blk_queue_max_hw_segments(q, UB_MAX_REQ_SG);
2366         blk_queue_max_phys_segments(q, UB_MAX_REQ_SG);
2367         blk_queue_segment_boundary(q, 0xffffffff);      /* Dubious. */
2368         blk_queue_max_sectors(q, UB_MAX_SECTORS);
2369         blk_queue_hardsect_size(q, lun->capacity.bsize);
2370
2371         q->queuedata = lun;
2372
2373         set_capacity(disk, lun->capacity.nsec);
2374         if (lun->removable)
2375                 disk->flags |= GENHD_FL_REMOVABLE;
2376
2377         add_disk(disk);
2378
2379         return 0;
2380
2381 err_blkqinit:
2382         put_disk(disk);
2383 err_diskalloc:
2384         list_del(&lun->link);
2385         ub_id_put(lun->id);
2386 err_id:
2387         kfree(lun);
2388 err_alloc:
2389         return rc;
2390 }
2391
2392 static void ub_disconnect(struct usb_interface *intf)
2393 {
2394         struct ub_dev *sc = usb_get_intfdata(intf);
2395         struct list_head *p;
2396         struct ub_lun *lun;
2397         struct gendisk *disk;
2398         unsigned long flags;
2399
2400         /*
2401          * Prevent ub_bd_release from pulling the rug from under us.
2402          * XXX This is starting to look like a kref.
2403          * XXX Why not to take this ref at probe time?
2404          */
2405         spin_lock_irqsave(&ub_lock, flags);
2406         sc->openc++;
2407         spin_unlock_irqrestore(&ub_lock, flags);
2408
2409         /*
2410          * Fence stall clearnings, operations triggered by unlinkings and so on.
2411          * We do not attempt to unlink any URBs, because we do not trust the
2412          * unlink paths in HC drivers. Also, we get -84 upon disconnect anyway.
2413          */
2414         atomic_set(&sc->poison, 1);
2415
2416         /*
2417          * Blow away queued commands.
2418          *
2419          * Actually, this never works, because before we get here
2420          * the HCD terminates outstanding URB(s). It causes our
2421          * SCSI command queue to advance, commands fail to submit,
2422          * and the whole queue drains. So, we just use this code to
2423          * print warnings.
2424          */
2425         spin_lock_irqsave(&sc->lock, flags);
2426         {
2427                 struct ub_scsi_cmd *cmd;
2428                 int cnt = 0;
2429                 while ((cmd = ub_cmdq_pop(sc)) != NULL) {
2430                         cmd->error = -ENOTCONN;
2431                         cmd->state = UB_CMDST_DONE;
2432                         ub_cmdtr_state(sc, cmd);
2433                         ub_cmdq_pop(sc);
2434                         (*cmd->done)(sc, cmd);
2435                         cnt++;
2436                 }
2437                 if (cnt != 0) {
2438                         printk(KERN_WARNING "%s: "
2439                             "%d was queued after shutdown\n", sc->name, cnt);
2440                 }
2441         }
2442         spin_unlock_irqrestore(&sc->lock, flags);
2443
2444         /*
2445          * Unregister the upper layer.
2446          */
2447         list_for_each (p, &sc->luns) {
2448                 lun = list_entry(p, struct ub_lun, link);
2449                 disk = lun->disk;
2450                 if (disk->flags & GENHD_FL_UP)
2451                         del_gendisk(disk);
2452                 /*
2453                  * I wish I could do:
2454                  *    set_bit(QUEUE_FLAG_DEAD, &q->queue_flags);
2455                  * As it is, we rely on our internal poisoning and let
2456                  * the upper levels to spin furiously failing all the I/O.
2457                  */
2458         }
2459
2460         /*
2461          * Taking a lock on a structure which is about to be freed
2462          * is very nonsensual. Here it is largely a way to do a debug freeze,
2463          * and a bracket which shows where the nonsensual code segment ends.
2464          *
2465          * Testing for -EINPROGRESS is always a bug, so we are bending
2466          * the rules a little.
2467          */
2468         spin_lock_irqsave(&sc->lock, flags);
2469         if (sc->work_urb.status == -EINPROGRESS) {      /* janitors: ignore */
2470                 printk(KERN_WARNING "%s: "
2471                     "URB is active after disconnect\n", sc->name);
2472         }
2473         spin_unlock_irqrestore(&sc->lock, flags);
2474
2475         /*
2476          * There is virtually no chance that other CPU runs times so long
2477          * after ub_urb_complete should have called del_timer, but only if HCD
2478          * didn't forget to deliver a callback on unlink.
2479          */
2480         del_timer_sync(&sc->work_timer);
2481
2482         /*
2483          * At this point there must be no commands coming from anyone
2484          * and no URBs left in transit.
2485          */
2486
2487         device_remove_file(&sc->intf->dev, &dev_attr_diag);
2488         usb_set_intfdata(intf, NULL);
2489         // usb_put_intf(sc->intf);
2490         sc->intf = NULL;
2491         usb_put_dev(sc->dev);
2492         sc->dev = NULL;
2493
2494         ub_put(sc);
2495 }
2496
2497 static struct usb_driver ub_driver = {
2498         .owner =        THIS_MODULE,
2499         .name =         "ub",
2500         .probe =        ub_probe,
2501         .disconnect =   ub_disconnect,
2502         .id_table =     ub_usb_ids,
2503 };
2504
2505 static int __init ub_init(void)
2506 {
2507         int rc;
2508
2509         /* P3 */ printk("ub: sizeof ub_scsi_cmd %zu ub_dev %zu ub_lun %zu\n",
2510                         sizeof(struct ub_scsi_cmd), sizeof(struct ub_dev), sizeof(struct ub_lun));
2511
2512         if ((rc = register_blkdev(UB_MAJOR, DRV_NAME)) != 0)
2513                 goto err_regblkdev;
2514         devfs_mk_dir(DEVFS_NAME);
2515
2516         if ((rc = usb_register(&ub_driver)) != 0)
2517                 goto err_register;
2518
2519         return 0;
2520
2521 err_register:
2522         devfs_remove(DEVFS_NAME);
2523         unregister_blkdev(UB_MAJOR, DRV_NAME);
2524 err_regblkdev:
2525         return rc;
2526 }
2527
2528 static void __exit ub_exit(void)
2529 {
2530         usb_deregister(&ub_driver);
2531
2532         devfs_remove(DEVFS_NAME);
2533         unregister_blkdev(UB_MAJOR, DRV_NAME);
2534 }
2535
2536 module_init(ub_init);
2537 module_exit(ub_exit);
2538
2539 MODULE_LICENSE("GPL");