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