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