allow coexistance of N build and AC build.
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / drivers / block / ub.c
blob8bd33d107996489c50cd8ba4281c5e7a359b16bd
1 /*
2 * The low performance USB storage driver (ub).
4 * Copyright (c) 1999, 2000 Matthew Dharm (mdharm-usb@one-eyed-alien.net)
5 * Copyright (C) 2004 Pete Zaitcev (zaitcev@yahoo.com)
7 * This work is a part of Linux kernel, is derived from it,
8 * and is not licensed separately. See file COPYING for details.
10 * TODO (sorted by decreasing priority)
11 * -- set readonly flag for CDs, set removable flag for CF readers
12 * -- do inquiry and verify we got a disk and not a tape (for LUN mismatch)
13 * -- verify the 13 conditions and do bulk resets
14 * -- highmem
15 * -- move top_sense and work_bcs into separate allocations (if they survive)
16 * for cache purists and esoteric architectures.
17 * -- Allocate structure for LUN 0 before the first ub_sync_tur, avoid NULL. ?
18 * -- prune comments, they are too volumnous
19 * -- Resove XXX's
20 * -- CLEAR, CLR2STS, CLRRS seem to be ripe for refactoring.
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/usb.h>
25 #include <linux/usb_usual.h>
26 #include <linux/blkdev.h>
27 #include <linux/timer.h>
28 #include <scsi/scsi.h>
30 #define DRV_NAME "ub"
32 #define UB_MAJOR 180
35 * The command state machine is the key model for understanding of this driver.
37 * The general rule is that all transitions are done towards the bottom
38 * of the diagram, thus preventing any loops.
40 * An exception to that is how the STAT state is handled. A counter allows it
41 * to be re-entered along the path marked with [C].
43 * +--------+
44 * ! INIT !
45 * +--------+
46 * !
47 * ub_scsi_cmd_start fails ->--------------------------------------\
48 * ! !
49 * V !
50 * +--------+ !
51 * ! CMD ! !
52 * +--------+ !
53 * ! +--------+ !
54 * was -EPIPE -->-------------------------------->! CLEAR ! !
55 * ! +--------+ !
56 * ! ! !
57 * was error -->------------------------------------- ! --------->\
58 * ! ! !
59 * /--<-- cmd->dir == NONE ? ! !
60 * ! ! ! !
61 * ! V ! !
62 * ! +--------+ ! !
63 * ! ! DATA ! ! !
64 * ! +--------+ ! !
65 * ! ! +---------+ ! !
66 * ! was -EPIPE -->--------------->! CLR2STS ! ! !
67 * ! ! +---------+ ! !
68 * ! ! ! ! !
69 * ! ! was error -->---- ! --------->\
70 * ! was error -->--------------------- ! ------------- ! --------->\
71 * ! ! ! ! !
72 * ! V ! ! !
73 * \--->+--------+ ! ! !
74 * ! STAT !<--------------------------/ ! !
75 * /--->+--------+ ! !
76 * ! ! ! !
77 * [C] was -EPIPE -->-----------\ ! !
78 * ! ! ! ! !
79 * +<---- len == 0 ! ! !
80 * ! ! ! ! !
81 * ! was error -->--------------------------------------!---------->\
82 * ! ! ! ! !
83 * +<---- bad CSW ! ! !
84 * +<---- bad tag ! ! !
85 * ! ! V ! !
86 * ! ! +--------+ ! !
87 * ! ! ! CLRRS ! ! !
88 * ! ! +--------+ ! !
89 * ! ! ! ! !
90 * \------- ! --------------------[C]--------\ ! !
91 * ! ! ! !
92 * cmd->error---\ +--------+ ! !
93 * ! +--------------->! SENSE !<----------/ !
94 * STAT_FAIL----/ +--------+ !
95 * ! ! V
96 * ! V +--------+
97 * \--------------------------------\--------------------->! DONE !
98 * +--------+
102 * This many LUNs per USB device.
103 * Every one of them takes a host, see UB_MAX_HOSTS.
105 #define UB_MAX_LUNS 9
110 #define UB_PARTS_PER_LUN 8
112 #define UB_MAX_CDB_SIZE 16 /* Corresponds to Bulk */
114 #define UB_SENSE_SIZE 18
119 /* command block wrapper */
120 struct bulk_cb_wrap {
121 __le32 Signature; /* contains 'USBC' */
122 u32 Tag; /* unique per command id */
123 __le32 DataTransferLength; /* size of data */
124 u8 Flags; /* direction in bit 0 */
125 u8 Lun; /* LUN */
126 u8 Length; /* of of the CDB */
127 u8 CDB[UB_MAX_CDB_SIZE]; /* max command */
130 #define US_BULK_CB_WRAP_LEN 31
131 #define US_BULK_CB_SIGN 0x43425355 /*spells out USBC */
132 #define US_BULK_FLAG_IN 1
133 #define US_BULK_FLAG_OUT 0
135 /* command status wrapper */
136 struct bulk_cs_wrap {
137 __le32 Signature; /* should = 'USBS' */
138 u32 Tag; /* same as original command */
139 __le32 Residue; /* amount not transferred */
140 u8 Status; /* see below */
143 #define US_BULK_CS_WRAP_LEN 13
144 #define US_BULK_CS_SIGN 0x53425355 /* spells out 'USBS' */
145 #define US_BULK_STAT_OK 0
146 #define US_BULK_STAT_FAIL 1
147 #define US_BULK_STAT_PHASE 2
149 /* bulk-only class specific requests */
150 #define US_BULK_RESET_REQUEST 0xff
151 #define US_BULK_GET_MAX_LUN 0xfe
155 struct ub_dev;
157 #define UB_MAX_REQ_SG 9 /* cdrecord requires 32KB and maybe a header */
158 #define UB_MAX_SECTORS 64
161 * A second is more than enough for a 32K transfer (UB_MAX_SECTORS)
162 * even if a webcam hogs the bus, but some devices need time to spin up.
164 #define UB_URB_TIMEOUT (HZ*2)
165 #define UB_DATA_TIMEOUT (HZ*5) /* ZIP does spin-ups in the data phase */
166 #define UB_STAT_TIMEOUT (HZ*5) /* Same spinups and eject for a dataless cmd. */
167 #define UB_CTRL_TIMEOUT (HZ/2) /* 500ms ought to be enough to clear a stall */
170 * An instance of a SCSI command in transit.
172 #define UB_DIR_NONE 0
173 #define UB_DIR_READ 1
174 #define UB_DIR_ILLEGAL2 2
175 #define UB_DIR_WRITE 3
177 #define UB_DIR_CHAR(c) (((c)==UB_DIR_WRITE)? 'w': \
178 (((c)==UB_DIR_READ)? 'r': 'n'))
180 enum ub_scsi_cmd_state {
181 UB_CMDST_INIT, /* Initial state */
182 UB_CMDST_CMD, /* Command submitted */
183 UB_CMDST_DATA, /* Data phase */
184 UB_CMDST_CLR2STS, /* Clearing before requesting status */
185 UB_CMDST_STAT, /* Status phase */
186 UB_CMDST_CLEAR, /* Clearing a stall (halt, actually) */
187 UB_CMDST_CLRRS, /* Clearing before retrying status */
188 UB_CMDST_SENSE, /* Sending Request Sense */
189 UB_CMDST_DONE /* Final state */
192 struct ub_scsi_cmd {
193 unsigned char cdb[UB_MAX_CDB_SIZE];
194 unsigned char cdb_len;
196 unsigned char dir; /* 0 - none, 1 - read, 3 - write. */
197 enum ub_scsi_cmd_state state;
198 unsigned int tag;
199 struct ub_scsi_cmd *next;
201 int error; /* Return code - valid upon done */
202 unsigned int act_len; /* Return size */
203 unsigned char key, asc, ascq; /* May be valid if error==-EIO */
205 int stat_count; /* Retries getting status. */
207 unsigned int len; /* Requested length */
208 unsigned int current_sg;
209 unsigned int nsg; /* sgv[nsg] */
210 struct scatterlist sgv[UB_MAX_REQ_SG];
212 struct ub_lun *lun;
213 void (*done)(struct ub_dev *, struct ub_scsi_cmd *);
214 void *back;
217 struct ub_request {
218 struct request *rq;
219 unsigned int current_try;
220 unsigned int nsg; /* sgv[nsg] */
221 struct scatterlist sgv[UB_MAX_REQ_SG];
226 struct ub_capacity {
227 unsigned long nsec; /* Linux size - 512 byte sectors */
228 unsigned int bsize; /* Linux hardsect_size */
229 unsigned int bshift; /* Shift between 512 and hard sects */
233 * This is a direct take-off from linux/include/completion.h
234 * The difference is that I do not wait on this thing, just poll.
235 * When I want to wait (ub_probe), I just use the stock completion.
237 * Note that INIT_COMPLETION takes no lock. It is correct. But why
238 * in the bloody hell that thing takes struct instead of pointer to struct
239 * is quite beyond me. I just copied it from the stock completion.
241 struct ub_completion {
242 unsigned int done;
243 spinlock_t lock;
246 static inline void ub_init_completion(struct ub_completion *x)
248 x->done = 0;
249 spin_lock_init(&x->lock);
252 #define UB_INIT_COMPLETION(x) ((x).done = 0)
254 static void ub_complete(struct ub_completion *x)
256 unsigned long flags;
258 spin_lock_irqsave(&x->lock, flags);
259 x->done++;
260 spin_unlock_irqrestore(&x->lock, flags);
263 static int ub_is_completed(struct ub_completion *x)
265 unsigned long flags;
266 int ret;
268 spin_lock_irqsave(&x->lock, flags);
269 ret = x->done;
270 spin_unlock_irqrestore(&x->lock, flags);
271 return ret;
276 struct ub_scsi_cmd_queue {
277 int qlen, qmax;
278 struct ub_scsi_cmd *head, *tail;
282 * The block device instance (one per LUN).
284 struct ub_lun {
285 struct ub_dev *udev;
286 struct list_head link;
287 struct gendisk *disk;
288 int id; /* Host index */
289 int num; /* LUN number */
290 char name[16];
292 int changed; /* Media was changed */
293 int removable;
294 int readonly;
296 struct ub_request urq;
298 /* Use Ingo's mempool if or when we have more than one command. */
300 * Currently we never need more than one command for the whole device.
301 * However, giving every LUN a command is a cheap and automatic way
302 * to enforce fairness between them.
304 int cmda[1];
305 struct ub_scsi_cmd cmdv[1];
307 struct ub_capacity capacity;
311 * The USB device instance.
313 struct ub_dev {
314 spinlock_t *lock;
315 atomic_t poison; /* The USB device is disconnected */
316 int openc; /* protected by ub_lock! */
317 /* kref is too implicit for our taste */
318 int reset; /* Reset is running */
319 unsigned int tagcnt;
320 char name[12];
321 struct usb_device *dev;
322 struct usb_interface *intf;
324 struct list_head luns;
326 unsigned int send_bulk_pipe; /* cached pipe values */
327 unsigned int recv_bulk_pipe;
328 unsigned int send_ctrl_pipe;
329 unsigned int recv_ctrl_pipe;
331 struct tasklet_struct tasklet;
333 struct ub_scsi_cmd_queue cmd_queue;
334 struct ub_scsi_cmd top_rqs_cmd; /* REQUEST SENSE */
335 unsigned char top_sense[UB_SENSE_SIZE];
337 struct ub_completion work_done;
338 struct urb work_urb;
339 struct timer_list work_timer;
340 int last_pipe; /* What might need clearing */
341 __le32 signature; /* Learned signature */
342 struct bulk_cb_wrap work_bcb;
343 struct bulk_cs_wrap work_bcs;
344 struct usb_ctrlrequest work_cr;
346 struct work_struct reset_work;
347 wait_queue_head_t reset_wait;
349 int sg_stat[6];
354 static void ub_cleanup(struct ub_dev *sc);
355 static int ub_request_fn_1(struct ub_lun *lun, struct request *rq);
356 static void ub_cmd_build_block(struct ub_dev *sc, struct ub_lun *lun,
357 struct ub_scsi_cmd *cmd, struct ub_request *urq);
358 static void ub_cmd_build_packet(struct ub_dev *sc, struct ub_lun *lun,
359 struct ub_scsi_cmd *cmd, struct ub_request *urq);
360 static void ub_rw_cmd_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
361 static void ub_end_rq(struct request *rq, unsigned int status);
362 static int ub_rw_cmd_retry(struct ub_dev *sc, struct ub_lun *lun,
363 struct ub_request *urq, struct ub_scsi_cmd *cmd);
364 static int ub_submit_scsi(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
365 static void ub_urb_complete(struct urb *urb);
366 static void ub_scsi_action(unsigned long _dev);
367 static void ub_scsi_dispatch(struct ub_dev *sc);
368 static void ub_scsi_urb_compl(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
369 static void ub_data_start(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
370 static void ub_state_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd, int rc);
371 static int __ub_state_stat(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
372 static void ub_state_stat(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
373 static void ub_state_stat_counted(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
374 static void ub_state_sense(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
375 static int ub_submit_clear_stall(struct ub_dev *sc, struct ub_scsi_cmd *cmd,
376 int stalled_pipe);
377 static void ub_top_sense_done(struct ub_dev *sc, struct ub_scsi_cmd *scmd);
378 static void ub_reset_enter(struct ub_dev *sc, int try);
379 static void ub_reset_task(struct work_struct *work);
380 static int ub_sync_tur(struct ub_dev *sc, struct ub_lun *lun);
381 static int ub_sync_read_cap(struct ub_dev *sc, struct ub_lun *lun,
382 struct ub_capacity *ret);
383 static int ub_sync_reset(struct ub_dev *sc);
384 static int ub_probe_clear_stall(struct ub_dev *sc, int stalled_pipe);
385 static int ub_probe_lun(struct ub_dev *sc, int lnum);
389 #ifdef CONFIG_USB_LIBUSUAL
391 #define ub_usb_ids storage_usb_ids
392 #else
394 static struct usb_device_id ub_usb_ids[] = {
395 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_SCSI, US_PR_BULK) },
399 MODULE_DEVICE_TABLE(usb, ub_usb_ids);
400 #endif /* CONFIG_USB_LIBUSUAL */
403 * Find me a way to identify "next free minor" for add_disk(),
404 * and the array disappears the next day. However, the number of
405 * hosts has something to do with the naming and /proc/partitions.
406 * This has to be thought out in detail before changing.
407 * If UB_MAX_HOST was 1000, we'd use a bitmap. Or a better data structure.
409 #define UB_MAX_HOSTS 26
410 static char ub_hostv[UB_MAX_HOSTS];
412 #define UB_QLOCK_NUM 5
413 static spinlock_t ub_qlockv[UB_QLOCK_NUM];
414 static int ub_qlock_next = 0;
416 static DEFINE_SPINLOCK(ub_lock); /* Locks globals and ->openc */
419 * The id allocator.
421 * This also stores the host for indexing by minor, which is somewhat dirty.
423 static int ub_id_get(void)
425 unsigned long flags;
426 int i;
428 spin_lock_irqsave(&ub_lock, flags);
429 for (i = 0; i < UB_MAX_HOSTS; i++) {
430 if (ub_hostv[i] == 0) {
431 ub_hostv[i] = 1;
432 spin_unlock_irqrestore(&ub_lock, flags);
433 return i;
436 spin_unlock_irqrestore(&ub_lock, flags);
437 return -1;
440 static void ub_id_put(int id)
442 unsigned long flags;
444 if (id < 0 || id >= UB_MAX_HOSTS) {
445 printk(KERN_ERR DRV_NAME ": bad host ID %d\n", id);
446 return;
449 spin_lock_irqsave(&ub_lock, flags);
450 if (ub_hostv[id] == 0) {
451 spin_unlock_irqrestore(&ub_lock, flags);
452 printk(KERN_ERR DRV_NAME ": freeing free host ID %d\n", id);
453 return;
455 ub_hostv[id] = 0;
456 spin_unlock_irqrestore(&ub_lock, flags);
460 * This is necessitated by the fact that blk_cleanup_queue does not
461 * necesserily destroy the queue. Instead, it may merely decrease q->refcnt.
462 * Since our blk_init_queue() passes a spinlock common with ub_dev,
463 * we have life time issues when ub_cleanup frees ub_dev.
465 static spinlock_t *ub_next_lock(void)
467 unsigned long flags;
468 spinlock_t *ret;
470 spin_lock_irqsave(&ub_lock, flags);
471 ret = &ub_qlockv[ub_qlock_next];
472 ub_qlock_next = (ub_qlock_next + 1) % UB_QLOCK_NUM;
473 spin_unlock_irqrestore(&ub_lock, flags);
474 return ret;
478 * Downcount for deallocation. This rides on two assumptions:
479 * - once something is poisoned, its refcount cannot grow
480 * - opens cannot happen at this time (del_gendisk was done)
481 * If the above is true, we can drop the lock, which we need for
482 * blk_cleanup_queue(): the silly thing may attempt to sleep.
483 * [Actually, it never needs to sleep for us, but it calls might_sleep()]
485 static void ub_put(struct ub_dev *sc)
487 unsigned long flags;
489 spin_lock_irqsave(&ub_lock, flags);
490 --sc->openc;
491 if (sc->openc == 0 && atomic_read(&sc->poison)) {
492 spin_unlock_irqrestore(&ub_lock, flags);
493 ub_cleanup(sc);
494 } else {
495 spin_unlock_irqrestore(&ub_lock, flags);
500 * Final cleanup and deallocation.
502 static void ub_cleanup(struct ub_dev *sc)
504 struct list_head *p;
505 struct ub_lun *lun;
506 request_queue_t *q;
508 while (!list_empty(&sc->luns)) {
509 p = sc->luns.next;
510 lun = list_entry(p, struct ub_lun, link);
511 list_del(p);
513 /* I don't think queue can be NULL. But... Stolen from sx8.c */
514 if ((q = lun->disk->queue) != NULL)
515 blk_cleanup_queue(q);
517 * If we zero disk->private_data BEFORE put_disk, we have
518 * to check for NULL all over the place in open, release,
519 * check_media and revalidate, because the block level
520 * semaphore is well inside the put_disk.
521 * But we cannot zero after the call, because *disk is gone.
522 * The sd.c is blatantly racy in this area.
524 /* disk->private_data = NULL; */
525 put_disk(lun->disk);
526 lun->disk = NULL;
528 ub_id_put(lun->id);
529 kfree(lun);
532 usb_set_intfdata(sc->intf, NULL);
533 usb_put_intf(sc->intf);
534 usb_put_dev(sc->dev);
535 kfree(sc);
539 * The "command allocator".
541 static struct ub_scsi_cmd *ub_get_cmd(struct ub_lun *lun)
543 struct ub_scsi_cmd *ret;
545 if (lun->cmda[0])
546 return NULL;
547 ret = &lun->cmdv[0];
548 lun->cmda[0] = 1;
549 return ret;
552 static void ub_put_cmd(struct ub_lun *lun, struct ub_scsi_cmd *cmd)
554 if (cmd != &lun->cmdv[0]) {
555 printk(KERN_WARNING "%s: releasing a foreign cmd %p\n",
556 lun->name, cmd);
557 return;
559 if (!lun->cmda[0]) {
560 printk(KERN_WARNING "%s: releasing a free cmd\n", lun->name);
561 return;
563 lun->cmda[0] = 0;
567 * The command queue.
569 static void ub_cmdq_add(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
571 struct ub_scsi_cmd_queue *t = &sc->cmd_queue;
573 if (t->qlen++ == 0) {
574 t->head = cmd;
575 t->tail = cmd;
576 } else {
577 t->tail->next = cmd;
578 t->tail = cmd;
581 if (t->qlen > t->qmax)
582 t->qmax = t->qlen;
585 static void ub_cmdq_insert(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
587 struct ub_scsi_cmd_queue *t = &sc->cmd_queue;
589 if (t->qlen++ == 0) {
590 t->head = cmd;
591 t->tail = cmd;
592 } else {
593 cmd->next = t->head;
594 t->head = cmd;
597 if (t->qlen > t->qmax)
598 t->qmax = t->qlen;
601 static struct ub_scsi_cmd *ub_cmdq_pop(struct ub_dev *sc)
603 struct ub_scsi_cmd_queue *t = &sc->cmd_queue;
604 struct ub_scsi_cmd *cmd;
606 if (t->qlen == 0)
607 return NULL;
608 if (--t->qlen == 0)
609 t->tail = NULL;
610 cmd = t->head;
611 t->head = cmd->next;
612 cmd->next = NULL;
613 return cmd;
616 #define ub_cmdq_peek(sc) ((sc)->cmd_queue.head)
619 * The request function is our main entry point
622 static void ub_request_fn(request_queue_t *q)
624 struct ub_lun *lun = q->queuedata;
625 struct request *rq;
627 while ((rq = elv_next_request(q)) != NULL) {
628 if (ub_request_fn_1(lun, rq) != 0) {
629 blk_stop_queue(q);
630 break;
635 static int ub_request_fn_1(struct ub_lun *lun, struct request *rq)
637 struct ub_dev *sc = lun->udev;
638 struct ub_scsi_cmd *cmd;
639 struct ub_request *urq;
640 int n_elem;
642 if (atomic_read(&sc->poison)) {
643 blkdev_dequeue_request(rq);
644 ub_end_rq(rq, DID_NO_CONNECT << 16);
645 return 0;
648 if (lun->changed && !blk_pc_request(rq)) {
649 blkdev_dequeue_request(rq);
650 ub_end_rq(rq, SAM_STAT_CHECK_CONDITION);
651 return 0;
654 if (lun->urq.rq != NULL)
655 return -1;
656 if ((cmd = ub_get_cmd(lun)) == NULL)
657 return -1;
658 memset(cmd, 0, sizeof(struct ub_scsi_cmd));
660 blkdev_dequeue_request(rq);
662 urq = &lun->urq;
663 memset(urq, 0, sizeof(struct ub_request));
664 urq->rq = rq;
667 * get scatterlist from block layer
669 n_elem = blk_rq_map_sg(lun->disk->queue, rq, &urq->sgv[0]);
670 if (n_elem < 0) {
671 /* Impossible, because blk_rq_map_sg should not hit ENOMEM. */
672 printk(KERN_INFO "%s: failed request map (%d)\n",
673 lun->name, n_elem);
674 goto drop;
676 if (n_elem > UB_MAX_REQ_SG) { /* Paranoia */
677 printk(KERN_WARNING "%s: request with %d segments\n",
678 lun->name, n_elem);
679 goto drop;
681 urq->nsg = n_elem;
682 sc->sg_stat[n_elem < 5 ? n_elem : 5]++;
684 if (blk_pc_request(rq)) {
685 ub_cmd_build_packet(sc, lun, cmd, urq);
686 } else {
687 ub_cmd_build_block(sc, lun, cmd, urq);
689 cmd->state = UB_CMDST_INIT;
690 cmd->lun = lun;
691 cmd->done = ub_rw_cmd_done;
692 cmd->back = urq;
694 cmd->tag = sc->tagcnt++;
695 if (ub_submit_scsi(sc, cmd) != 0)
696 goto drop;
698 return 0;
700 drop:
701 ub_put_cmd(lun, cmd);
702 ub_end_rq(rq, DID_ERROR << 16);
703 return 0;
706 static void ub_cmd_build_block(struct ub_dev *sc, struct ub_lun *lun,
707 struct ub_scsi_cmd *cmd, struct ub_request *urq)
709 struct request *rq = urq->rq;
710 unsigned int block, nblks;
712 if (rq_data_dir(rq) == WRITE)
713 cmd->dir = UB_DIR_WRITE;
714 else
715 cmd->dir = UB_DIR_READ;
717 cmd->nsg = urq->nsg;
718 memcpy(cmd->sgv, urq->sgv, sizeof(struct scatterlist) * cmd->nsg);
721 * build the command
723 * The call to blk_queue_hardsect_size() guarantees that request
724 * is aligned, but it is given in terms of 512 byte units, always.
726 block = rq->sector >> lun->capacity.bshift;
727 nblks = rq->nr_sectors >> lun->capacity.bshift;
729 cmd->cdb[0] = (cmd->dir == UB_DIR_READ)? READ_10: WRITE_10;
730 /* 10-byte uses 4 bytes of LBA: 2147483648KB, 2097152MB, 2048GB */
731 cmd->cdb[2] = block >> 24;
732 cmd->cdb[3] = block >> 16;
733 cmd->cdb[4] = block >> 8;
734 cmd->cdb[5] = block;
735 cmd->cdb[7] = nblks >> 8;
736 cmd->cdb[8] = nblks;
737 cmd->cdb_len = 10;
739 cmd->len = rq->nr_sectors * 512;
742 static void ub_cmd_build_packet(struct ub_dev *sc, struct ub_lun *lun,
743 struct ub_scsi_cmd *cmd, struct ub_request *urq)
745 struct request *rq = urq->rq;
747 if (rq->data_len == 0) {
748 cmd->dir = UB_DIR_NONE;
749 } else {
750 if (rq_data_dir(rq) == WRITE)
751 cmd->dir = UB_DIR_WRITE;
752 else
753 cmd->dir = UB_DIR_READ;
756 cmd->nsg = urq->nsg;
757 memcpy(cmd->sgv, urq->sgv, sizeof(struct scatterlist) * cmd->nsg);
759 memcpy(&cmd->cdb, rq->cmd, rq->cmd_len);
760 cmd->cdb_len = rq->cmd_len;
762 cmd->len = rq->data_len;
765 static void ub_rw_cmd_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
767 struct ub_lun *lun = cmd->lun;
768 struct ub_request *urq = cmd->back;
769 struct request *rq;
770 unsigned int scsi_status;
772 rq = urq->rq;
774 if (cmd->error == 0) {
775 if (blk_pc_request(rq)) {
776 if (cmd->act_len >= rq->data_len)
777 rq->data_len = 0;
778 else
779 rq->data_len -= cmd->act_len;
781 scsi_status = 0;
782 } else {
783 if (blk_pc_request(rq)) {
784 /* UB_SENSE_SIZE is smaller than SCSI_SENSE_BUFFERSIZE */
785 memcpy(rq->sense, sc->top_sense, UB_SENSE_SIZE);
786 rq->sense_len = UB_SENSE_SIZE;
787 if (sc->top_sense[0] != 0)
788 scsi_status = SAM_STAT_CHECK_CONDITION;
789 else
790 scsi_status = DID_ERROR << 16;
791 } else {
792 if (cmd->error == -EIO) {
793 if (ub_rw_cmd_retry(sc, lun, urq, cmd) == 0)
794 return;
796 scsi_status = SAM_STAT_CHECK_CONDITION;
800 urq->rq = NULL;
802 ub_put_cmd(lun, cmd);
803 ub_end_rq(rq, scsi_status);
804 blk_start_queue(lun->disk->queue);
807 static void ub_end_rq(struct request *rq, unsigned int scsi_status)
809 int uptodate;
811 if (scsi_status == 0) {
812 uptodate = 1;
813 } else {
814 uptodate = 0;
815 rq->errors = scsi_status;
817 end_that_request_first(rq, uptodate, rq->hard_nr_sectors);
818 end_that_request_last(rq, uptodate);
821 static int ub_rw_cmd_retry(struct ub_dev *sc, struct ub_lun *lun,
822 struct ub_request *urq, struct ub_scsi_cmd *cmd)
825 if (atomic_read(&sc->poison))
826 return -ENXIO;
828 ub_reset_enter(sc, urq->current_try);
830 if (urq->current_try >= 3)
831 return -EIO;
832 urq->current_try++;
834 /* Remove this if anyone complains of flooding. */
835 printk(KERN_DEBUG "%s: dir %c len/act %d/%d "
836 "[sense %x %02x %02x] retry %d\n",
837 sc->name, UB_DIR_CHAR(cmd->dir), cmd->len, cmd->act_len,
838 cmd->key, cmd->asc, cmd->ascq, urq->current_try);
840 memset(cmd, 0, sizeof(struct ub_scsi_cmd));
841 ub_cmd_build_block(sc, lun, cmd, urq);
843 cmd->state = UB_CMDST_INIT;
844 cmd->lun = lun;
845 cmd->done = ub_rw_cmd_done;
846 cmd->back = urq;
848 cmd->tag = sc->tagcnt++;
850 #if 0 /* Wasteful */
851 return ub_submit_scsi(sc, cmd);
852 #else
853 ub_cmdq_add(sc, cmd);
854 return 0;
855 #endif
859 * Submit a regular SCSI operation (not an auto-sense).
861 * The Iron Law of Good Submit Routine is:
862 * Zero return - callback is done, Nonzero return - callback is not done.
863 * No exceptions.
865 * Host is assumed locked.
867 static int ub_submit_scsi(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
870 if (cmd->state != UB_CMDST_INIT ||
871 (cmd->dir != UB_DIR_NONE && cmd->len == 0)) {
872 return -EINVAL;
875 ub_cmdq_add(sc, cmd);
877 * We can call ub_scsi_dispatch(sc) right away here, but it's a little
878 * safer to jump to a tasklet, in case upper layers do something silly.
880 tasklet_schedule(&sc->tasklet);
881 return 0;
885 * Submit the first URB for the queued command.
886 * This function does not deal with queueing in any way.
888 static int ub_scsi_cmd_start(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
890 struct bulk_cb_wrap *bcb;
891 int rc;
893 bcb = &sc->work_bcb;
896 * ``If the allocation length is eighteen or greater, and a device
897 * server returns less than eithteen bytes of data, the application
898 * client should assume that the bytes not transferred would have been
899 * zeroes had the device server returned those bytes.''
901 * We zero sense for all commands so that when a packet request
902 * fails it does not return a stale sense.
904 memset(&sc->top_sense, 0, UB_SENSE_SIZE);
906 /* set up the command wrapper */
907 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
908 bcb->Tag = cmd->tag; /* Endianness is not important */
909 bcb->DataTransferLength = cpu_to_le32(cmd->len);
910 bcb->Flags = (cmd->dir == UB_DIR_READ) ? 0x80 : 0;
911 bcb->Lun = (cmd->lun != NULL) ? cmd->lun->num : 0;
912 bcb->Length = cmd->cdb_len;
914 /* copy the command payload */
915 memcpy(bcb->CDB, cmd->cdb, UB_MAX_CDB_SIZE);
917 UB_INIT_COMPLETION(sc->work_done);
919 sc->last_pipe = sc->send_bulk_pipe;
920 usb_fill_bulk_urb(&sc->work_urb, sc->dev, sc->send_bulk_pipe,
921 bcb, US_BULK_CB_WRAP_LEN, ub_urb_complete, sc);
923 /* Fill what we shouldn't be filling, because usb-storage did so. */
924 sc->work_urb.actual_length = 0;
925 sc->work_urb.error_count = 0;
926 sc->work_urb.status = 0;
928 if ((rc = usb_submit_urb(&sc->work_urb, GFP_ATOMIC)) != 0) {
929 /* XXX Clear stalls */
930 ub_complete(&sc->work_done);
931 return rc;
934 sc->work_timer.expires = jiffies + UB_URB_TIMEOUT;
935 add_timer(&sc->work_timer);
937 cmd->state = UB_CMDST_CMD;
938 return 0;
942 * Timeout handler.
944 static void ub_urb_timeout(unsigned long arg)
946 struct ub_dev *sc = (struct ub_dev *) arg;
947 unsigned long flags;
949 spin_lock_irqsave(sc->lock, flags);
950 if (!ub_is_completed(&sc->work_done))
951 usb_unlink_urb(&sc->work_urb);
952 spin_unlock_irqrestore(sc->lock, flags);
956 * Completion routine for the work URB.
958 * This can be called directly from usb_submit_urb (while we have
959 * the sc->lock taken) and from an interrupt (while we do NOT have
960 * the sc->lock taken). Therefore, bounce this off to a tasklet.
962 static void ub_urb_complete(struct urb *urb)
964 struct ub_dev *sc = urb->context;
966 ub_complete(&sc->work_done);
967 tasklet_schedule(&sc->tasklet);
970 static void ub_scsi_action(unsigned long _dev)
972 struct ub_dev *sc = (struct ub_dev *) _dev;
973 unsigned long flags;
975 spin_lock_irqsave(sc->lock, flags);
976 ub_scsi_dispatch(sc);
977 spin_unlock_irqrestore(sc->lock, flags);
980 static void ub_scsi_dispatch(struct ub_dev *sc)
982 struct ub_scsi_cmd *cmd;
983 int rc;
985 while (!sc->reset && (cmd = ub_cmdq_peek(sc)) != NULL) {
986 if (cmd->state == UB_CMDST_DONE) {
987 ub_cmdq_pop(sc);
988 (*cmd->done)(sc, cmd);
989 } else if (cmd->state == UB_CMDST_INIT) {
990 if ((rc = ub_scsi_cmd_start(sc, cmd)) == 0)
991 break;
992 cmd->error = rc;
993 cmd->state = UB_CMDST_DONE;
994 } else {
995 if (!ub_is_completed(&sc->work_done))
996 break;
997 del_timer(&sc->work_timer);
998 ub_scsi_urb_compl(sc, cmd);
1003 static void ub_scsi_urb_compl(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1005 struct urb *urb = &sc->work_urb;
1006 struct bulk_cs_wrap *bcs;
1007 int len;
1008 int rc;
1010 if (atomic_read(&sc->poison)) {
1011 ub_state_done(sc, cmd, -ENODEV);
1012 return;
1015 if (cmd->state == UB_CMDST_CLEAR) {
1016 if (urb->status == -EPIPE) {
1018 * STALL while clearning STALL.
1019 * The control pipe clears itself - nothing to do.
1021 printk(KERN_NOTICE "%s: stall on control pipe\n",
1022 sc->name);
1023 goto Bad_End;
1027 * We ignore the result for the halt clear.
1030 /* reset the endpoint toggle */
1031 usb_settoggle(sc->dev, usb_pipeendpoint(sc->last_pipe),
1032 usb_pipeout(sc->last_pipe), 0);
1034 ub_state_sense(sc, cmd);
1036 } else if (cmd->state == UB_CMDST_CLR2STS) {
1037 if (urb->status == -EPIPE) {
1038 printk(KERN_NOTICE "%s: stall on control pipe\n",
1039 sc->name);
1040 goto Bad_End;
1044 * We ignore the result for the halt clear.
1047 /* reset the endpoint toggle */
1048 usb_settoggle(sc->dev, usb_pipeendpoint(sc->last_pipe),
1049 usb_pipeout(sc->last_pipe), 0);
1051 ub_state_stat(sc, cmd);
1053 } else if (cmd->state == UB_CMDST_CLRRS) {
1054 if (urb->status == -EPIPE) {
1055 printk(KERN_NOTICE "%s: stall on control pipe\n",
1056 sc->name);
1057 goto Bad_End;
1061 * We ignore the result for the halt clear.
1064 /* reset the endpoint toggle */
1065 usb_settoggle(sc->dev, usb_pipeendpoint(sc->last_pipe),
1066 usb_pipeout(sc->last_pipe), 0);
1068 ub_state_stat_counted(sc, cmd);
1070 } else if (cmd->state == UB_CMDST_CMD) {
1071 switch (urb->status) {
1072 case 0:
1073 break;
1074 case -EOVERFLOW:
1075 goto Bad_End;
1076 case -EPIPE:
1077 rc = ub_submit_clear_stall(sc, cmd, sc->last_pipe);
1078 if (rc != 0) {
1079 printk(KERN_NOTICE "%s: "
1080 "unable to submit clear (%d)\n",
1081 sc->name, rc);
1083 * This is typically ENOMEM or some other such shit.
1084 * Retrying is pointless. Just do Bad End on it...
1086 ub_state_done(sc, cmd, rc);
1087 return;
1089 cmd->state = UB_CMDST_CLEAR;
1090 return;
1091 case -ESHUTDOWN: /* unplug */
1092 case -EILSEQ: /* unplug timeout on uhci */
1093 ub_state_done(sc, cmd, -ENODEV);
1094 return;
1095 default:
1096 goto Bad_End;
1098 if (urb->actual_length != US_BULK_CB_WRAP_LEN) {
1099 goto Bad_End;
1102 if (cmd->dir == UB_DIR_NONE || cmd->nsg < 1) {
1103 ub_state_stat(sc, cmd);
1104 return;
1107 // udelay(125); // usb-storage has this
1108 ub_data_start(sc, cmd);
1110 } else if (cmd->state == UB_CMDST_DATA) {
1111 if (urb->status == -EPIPE) {
1112 rc = ub_submit_clear_stall(sc, cmd, sc->last_pipe);
1113 if (rc != 0) {
1114 printk(KERN_NOTICE "%s: "
1115 "unable to submit clear (%d)\n",
1116 sc->name, rc);
1117 ub_state_done(sc, cmd, rc);
1118 return;
1120 cmd->state = UB_CMDST_CLR2STS;
1121 return;
1123 if (urb->status == -EOVERFLOW) {
1125 * A babble? Failure, but we must transfer CSW now.
1127 cmd->error = -EOVERFLOW; /* A cheap trick... */
1128 ub_state_stat(sc, cmd);
1129 return;
1132 if (cmd->dir == UB_DIR_WRITE) {
1134 * Do not continue writes in case of a failure.
1135 * Doing so would cause sectors to be mixed up,
1136 * which is worse than sectors lost.
1138 * We must try to read the CSW, or many devices
1139 * get confused.
1141 len = urb->actual_length;
1142 if (urb->status != 0 ||
1143 len != cmd->sgv[cmd->current_sg].length) {
1144 cmd->act_len += len;
1146 cmd->error = -EIO;
1147 ub_state_stat(sc, cmd);
1148 return;
1151 } else {
1153 * If an error occurs on read, we record it, and
1154 * continue to fetch data in order to avoid bubble.
1156 * As a small shortcut, we stop if we detect that
1157 * a CSW mixed into data.
1159 if (urb->status != 0)
1160 cmd->error = -EIO;
1162 len = urb->actual_length;
1163 if (urb->status != 0 ||
1164 len != cmd->sgv[cmd->current_sg].length) {
1165 if ((len & 0x1FF) == US_BULK_CS_WRAP_LEN)
1166 goto Bad_End;
1170 cmd->act_len += urb->actual_length;
1172 if (++cmd->current_sg < cmd->nsg) {
1173 ub_data_start(sc, cmd);
1174 return;
1176 ub_state_stat(sc, cmd);
1178 } else if (cmd->state == UB_CMDST_STAT) {
1179 if (urb->status == -EPIPE) {
1180 rc = ub_submit_clear_stall(sc, cmd, sc->last_pipe);
1181 if (rc != 0) {
1182 printk(KERN_NOTICE "%s: "
1183 "unable to submit clear (%d)\n",
1184 sc->name, rc);
1185 ub_state_done(sc, cmd, rc);
1186 return;
1190 * Having a stall when getting CSW is an error, so
1191 * make sure uppper levels are not oblivious to it.
1193 cmd->error = -EIO; /* A cheap trick... */
1195 cmd->state = UB_CMDST_CLRRS;
1196 return;
1199 /* Catch everything, including -EOVERFLOW and other nasties. */
1200 if (urb->status != 0)
1201 goto Bad_End;
1203 if (urb->actual_length == 0) {
1204 ub_state_stat_counted(sc, cmd);
1205 return;
1209 * Check the returned Bulk protocol status.
1210 * The status block has to be validated first.
1213 bcs = &sc->work_bcs;
1215 if (sc->signature == cpu_to_le32(0)) {
1217 * This is the first reply, so do not perform the check.
1218 * Instead, remember the signature the device uses
1219 * for future checks. But do not allow a nul.
1221 sc->signature = bcs->Signature;
1222 if (sc->signature == cpu_to_le32(0)) {
1223 ub_state_stat_counted(sc, cmd);
1224 return;
1226 } else {
1227 if (bcs->Signature != sc->signature) {
1228 ub_state_stat_counted(sc, cmd);
1229 return;
1233 if (bcs->Tag != cmd->tag) {
1235 * This usually happens when we disagree with the
1236 * device's microcode about something. For instance,
1237 * a few of them throw this after timeouts. They buffer
1238 * commands and reply at commands we timed out before.
1239 * Without flushing these replies we loop forever.
1241 ub_state_stat_counted(sc, cmd);
1242 return;
1245 len = le32_to_cpu(bcs->Residue);
1246 if (len != cmd->len - cmd->act_len) {
1248 * It is all right to transfer less, the caller has
1249 * to check. But it's not all right if the device
1250 * counts disagree with our counts.
1252 goto Bad_End;
1255 switch (bcs->Status) {
1256 case US_BULK_STAT_OK:
1257 break;
1258 case US_BULK_STAT_FAIL:
1259 ub_state_sense(sc, cmd);
1260 return;
1261 case US_BULK_STAT_PHASE:
1262 goto Bad_End;
1263 default:
1264 printk(KERN_INFO "%s: unknown CSW status 0x%x\n",
1265 sc->name, bcs->Status);
1266 ub_state_done(sc, cmd, -EINVAL);
1267 return;
1270 /* Not zeroing error to preserve a babble indicator */
1271 if (cmd->error != 0) {
1272 ub_state_sense(sc, cmd);
1273 return;
1275 cmd->state = UB_CMDST_DONE;
1276 ub_cmdq_pop(sc);
1277 (*cmd->done)(sc, cmd);
1279 } else if (cmd->state == UB_CMDST_SENSE) {
1280 ub_state_done(sc, cmd, -EIO);
1282 } else {
1283 printk(KERN_WARNING "%s: "
1284 "wrong command state %d\n",
1285 sc->name, cmd->state);
1286 ub_state_done(sc, cmd, -EINVAL);
1287 return;
1289 return;
1291 Bad_End: /* Little Excel is dead */
1292 ub_state_done(sc, cmd, -EIO);
1296 * Factorization helper for the command state machine:
1297 * Initiate a data segment transfer.
1299 static void ub_data_start(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1301 struct scatterlist *sg = &cmd->sgv[cmd->current_sg];
1302 int pipe;
1303 int rc;
1305 UB_INIT_COMPLETION(sc->work_done);
1307 if (cmd->dir == UB_DIR_READ)
1308 pipe = sc->recv_bulk_pipe;
1309 else
1310 pipe = sc->send_bulk_pipe;
1311 sc->last_pipe = pipe;
1312 usb_fill_bulk_urb(&sc->work_urb, sc->dev, pipe,
1313 page_address(sg->page) + sg->offset, sg->length,
1314 ub_urb_complete, sc);
1315 sc->work_urb.actual_length = 0;
1316 sc->work_urb.error_count = 0;
1317 sc->work_urb.status = 0;
1319 if ((rc = usb_submit_urb(&sc->work_urb, GFP_ATOMIC)) != 0) {
1320 /* XXX Clear stalls */
1321 ub_complete(&sc->work_done);
1322 ub_state_done(sc, cmd, rc);
1323 return;
1326 sc->work_timer.expires = jiffies + UB_DATA_TIMEOUT;
1327 add_timer(&sc->work_timer);
1329 cmd->state = UB_CMDST_DATA;
1333 * Factorization helper for the command state machine:
1334 * Finish the command.
1336 static void ub_state_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd, int rc)
1339 cmd->error = rc;
1340 cmd->state = UB_CMDST_DONE;
1341 ub_cmdq_pop(sc);
1342 (*cmd->done)(sc, cmd);
1346 * Factorization helper for the command state machine:
1347 * Submit a CSW read.
1349 static int __ub_state_stat(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1351 int rc;
1353 UB_INIT_COMPLETION(sc->work_done);
1355 sc->last_pipe = sc->recv_bulk_pipe;
1356 usb_fill_bulk_urb(&sc->work_urb, sc->dev, sc->recv_bulk_pipe,
1357 &sc->work_bcs, US_BULK_CS_WRAP_LEN, ub_urb_complete, sc);
1358 sc->work_urb.actual_length = 0;
1359 sc->work_urb.error_count = 0;
1360 sc->work_urb.status = 0;
1362 if ((rc = usb_submit_urb(&sc->work_urb, GFP_ATOMIC)) != 0) {
1363 /* XXX Clear stalls */
1364 ub_complete(&sc->work_done);
1365 ub_state_done(sc, cmd, rc);
1366 return -1;
1369 sc->work_timer.expires = jiffies + UB_STAT_TIMEOUT;
1370 add_timer(&sc->work_timer);
1371 return 0;
1375 * Factorization helper for the command state machine:
1376 * Submit a CSW read and go to STAT state.
1378 static void ub_state_stat(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1381 if (__ub_state_stat(sc, cmd) != 0)
1382 return;
1384 cmd->stat_count = 0;
1385 cmd->state = UB_CMDST_STAT;
1389 * Factorization helper for the command state machine:
1390 * Submit a CSW read and go to STAT state with counter (along [C] path).
1392 static void ub_state_stat_counted(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1395 if (++cmd->stat_count >= 4) {
1396 ub_state_sense(sc, cmd);
1397 return;
1400 if (__ub_state_stat(sc, cmd) != 0)
1401 return;
1403 cmd->state = UB_CMDST_STAT;
1407 * Factorization helper for the command state machine:
1408 * Submit a REQUEST SENSE and go to SENSE state.
1410 static void ub_state_sense(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1412 struct ub_scsi_cmd *scmd;
1413 struct scatterlist *sg;
1414 int rc;
1416 if (cmd->cdb[0] == REQUEST_SENSE) {
1417 rc = -EPIPE;
1418 goto error;
1421 scmd = &sc->top_rqs_cmd;
1422 memset(scmd, 0, sizeof(struct ub_scsi_cmd));
1423 scmd->cdb[0] = REQUEST_SENSE;
1424 scmd->cdb[4] = UB_SENSE_SIZE;
1425 scmd->cdb_len = 6;
1426 scmd->dir = UB_DIR_READ;
1427 scmd->state = UB_CMDST_INIT;
1428 scmd->nsg = 1;
1429 sg = &scmd->sgv[0];
1430 sg->page = virt_to_page(sc->top_sense);
1431 sg->offset = (unsigned long)sc->top_sense & (PAGE_SIZE-1);
1432 sg->length = UB_SENSE_SIZE;
1433 scmd->len = UB_SENSE_SIZE;
1434 scmd->lun = cmd->lun;
1435 scmd->done = ub_top_sense_done;
1436 scmd->back = cmd;
1438 scmd->tag = sc->tagcnt++;
1440 cmd->state = UB_CMDST_SENSE;
1442 ub_cmdq_insert(sc, scmd);
1443 return;
1445 error:
1446 ub_state_done(sc, cmd, rc);
1450 * A helper for the command's state machine:
1451 * Submit a stall clear.
1453 static int ub_submit_clear_stall(struct ub_dev *sc, struct ub_scsi_cmd *cmd,
1454 int stalled_pipe)
1456 int endp;
1457 struct usb_ctrlrequest *cr;
1458 int rc;
1460 endp = usb_pipeendpoint(stalled_pipe);
1461 if (usb_pipein (stalled_pipe))
1462 endp |= USB_DIR_IN;
1464 cr = &sc->work_cr;
1465 cr->bRequestType = USB_RECIP_ENDPOINT;
1466 cr->bRequest = USB_REQ_CLEAR_FEATURE;
1467 cr->wValue = cpu_to_le16(USB_ENDPOINT_HALT);
1468 cr->wIndex = cpu_to_le16(endp);
1469 cr->wLength = cpu_to_le16(0);
1471 UB_INIT_COMPLETION(sc->work_done);
1473 usb_fill_control_urb(&sc->work_urb, sc->dev, sc->send_ctrl_pipe,
1474 (unsigned char*) cr, NULL, 0, ub_urb_complete, sc);
1475 sc->work_urb.actual_length = 0;
1476 sc->work_urb.error_count = 0;
1477 sc->work_urb.status = 0;
1479 if ((rc = usb_submit_urb(&sc->work_urb, GFP_ATOMIC)) != 0) {
1480 ub_complete(&sc->work_done);
1481 return rc;
1484 sc->work_timer.expires = jiffies + UB_CTRL_TIMEOUT;
1485 add_timer(&sc->work_timer);
1486 return 0;
1491 static void ub_top_sense_done(struct ub_dev *sc, struct ub_scsi_cmd *scmd)
1493 unsigned char *sense = sc->top_sense;
1494 struct ub_scsi_cmd *cmd;
1497 * Find the command which triggered the unit attention or a check,
1498 * save the sense into it, and advance its state machine.
1500 if ((cmd = ub_cmdq_peek(sc)) == NULL) {
1501 printk(KERN_WARNING "%s: sense done while idle\n", sc->name);
1502 return;
1504 if (cmd != scmd->back) {
1505 printk(KERN_WARNING "%s: "
1506 "sense done for wrong command 0x%x\n",
1507 sc->name, cmd->tag);
1508 return;
1510 if (cmd->state != UB_CMDST_SENSE) {
1511 printk(KERN_WARNING "%s: "
1512 "sense done with bad cmd state %d\n",
1513 sc->name, cmd->state);
1514 return;
1518 * Ignoring scmd->act_len, because the buffer was pre-zeroed.
1520 cmd->key = sense[2] & 0x0F;
1521 cmd->asc = sense[12];
1522 cmd->ascq = sense[13];
1524 ub_scsi_urb_compl(sc, cmd);
1528 * Reset management
1529 * XXX Move usb_reset_device to khubd. Hogging kevent is not a good thing.
1530 * XXX Make usb_sync_reset asynchronous.
1533 static void ub_reset_enter(struct ub_dev *sc, int try)
1536 if (sc->reset) {
1537 /* This happens often on multi-LUN devices. */
1538 return;
1540 sc->reset = try + 1;
1542 #if 0 /* Not needed because the disconnect waits for us. */
1543 unsigned long flags;
1544 spin_lock_irqsave(&ub_lock, flags);
1545 sc->openc++;
1546 spin_unlock_irqrestore(&ub_lock, flags);
1547 #endif
1549 #if 0 /* We let them stop themselves. */
1550 struct list_head *p;
1551 struct ub_lun *lun;
1552 list_for_each(p, &sc->luns) {
1553 lun = list_entry(p, struct ub_lun, link);
1554 blk_stop_queue(lun->disk->queue);
1556 #endif
1558 schedule_work(&sc->reset_work);
1561 static void ub_reset_task(struct work_struct *work)
1563 struct ub_dev *sc = container_of(work, struct ub_dev, reset_work);
1564 unsigned long flags;
1565 struct list_head *p;
1566 struct ub_lun *lun;
1567 int rc;
1569 if (!sc->reset) {
1570 printk(KERN_WARNING "%s: Running reset unrequested\n",
1571 sc->name);
1572 return;
1575 if (atomic_read(&sc->poison)) {
1577 } else if ((sc->reset & 1) == 0) {
1578 ub_sync_reset(sc);
1579 msleep(700); /* usb-storage sleeps 6s (!) */
1580 ub_probe_clear_stall(sc, sc->recv_bulk_pipe);
1581 ub_probe_clear_stall(sc, sc->send_bulk_pipe);
1582 } else if (sc->dev->actconfig->desc.bNumInterfaces != 1) {
1584 } else {
1585 rc = usb_lock_device_for_reset(sc->dev, sc->intf);
1586 if (rc < 0) {
1587 printk(KERN_NOTICE
1588 "%s: usb_lock_device_for_reset failed (%d)\n",
1589 sc->name, rc);
1590 } else {
1591 rc = usb_reset_device(sc->dev);
1592 if (rc < 0) {
1593 printk(KERN_NOTICE "%s: "
1594 "usb_lock_device_for_reset failed (%d)\n",
1595 sc->name, rc);
1597 usb_unlock_device(sc->dev);
1602 * In theory, no commands can be running while reset is active,
1603 * so nobody can ask for another reset, and so we do not need any
1604 * queues of resets or anything. We do need a spinlock though,
1605 * to interact with block layer.
1607 spin_lock_irqsave(sc->lock, flags);
1608 sc->reset = 0;
1609 tasklet_schedule(&sc->tasklet);
1610 list_for_each(p, &sc->luns) {
1611 lun = list_entry(p, struct ub_lun, link);
1612 blk_start_queue(lun->disk->queue);
1614 wake_up(&sc->reset_wait);
1615 spin_unlock_irqrestore(sc->lock, flags);
1619 * This is called from a process context.
1621 static void ub_revalidate(struct ub_dev *sc, struct ub_lun *lun)
1624 lun->readonly = 0; /* XXX Query this from the device */
1626 lun->capacity.nsec = 0;
1627 lun->capacity.bsize = 512;
1628 lun->capacity.bshift = 0;
1630 if (ub_sync_tur(sc, lun) != 0)
1631 return; /* Not ready */
1632 lun->changed = 0;
1634 if (ub_sync_read_cap(sc, lun, &lun->capacity) != 0) {
1636 * The retry here means something is wrong, either with the
1637 * device, with the transport, or with our code.
1638 * We keep this because sd.c has retries for capacity.
1640 if (ub_sync_read_cap(sc, lun, &lun->capacity) != 0) {
1641 lun->capacity.nsec = 0;
1642 lun->capacity.bsize = 512;
1643 lun->capacity.bshift = 0;
1649 * The open funcion.
1650 * This is mostly needed to keep refcounting, but also to support
1651 * media checks on removable media drives.
1653 static int ub_bd_open(struct inode *inode, struct file *filp)
1655 struct gendisk *disk = inode->i_bdev->bd_disk;
1656 struct ub_lun *lun = disk->private_data;
1657 struct ub_dev *sc = lun->udev;
1658 unsigned long flags;
1659 int rc;
1661 spin_lock_irqsave(&ub_lock, flags);
1662 if (atomic_read(&sc->poison)) {
1663 spin_unlock_irqrestore(&ub_lock, flags);
1664 return -ENXIO;
1666 sc->openc++;
1667 spin_unlock_irqrestore(&ub_lock, flags);
1669 if (lun->removable || lun->readonly)
1670 check_disk_change(inode->i_bdev);
1673 * The sd.c considers ->media_present and ->changed not equivalent,
1674 * under some pretty murky conditions (a failure of READ CAPACITY).
1675 * We may need it one day.
1677 if (lun->removable && lun->changed && !(filp->f_flags & O_NDELAY)) {
1678 rc = -ENOMEDIUM;
1679 goto err_open;
1682 if (lun->readonly && (filp->f_mode & FMODE_WRITE)) {
1683 rc = -EROFS;
1684 goto err_open;
1687 return 0;
1689 err_open:
1690 ub_put(sc);
1691 return rc;
1696 static int ub_bd_release(struct inode *inode, struct file *filp)
1698 struct gendisk *disk = inode->i_bdev->bd_disk;
1699 struct ub_lun *lun = disk->private_data;
1700 struct ub_dev *sc = lun->udev;
1702 ub_put(sc);
1703 return 0;
1707 * The ioctl interface.
1709 static int ub_bd_ioctl(struct inode *inode, struct file *filp,
1710 unsigned int cmd, unsigned long arg)
1712 struct gendisk *disk = inode->i_bdev->bd_disk;
1713 void __user *usermem = (void __user *) arg;
1715 return scsi_cmd_ioctl(filp, disk, cmd, usermem);
1719 * This is called once a new disk was seen by the block layer or by ub_probe().
1720 * The main onjective here is to discover the features of the media such as
1721 * the capacity, read-only status, etc. USB storage generally does not
1722 * need to be spun up, but if we needed it, this would be the place.
1724 * This call can sleep.
1726 * The return code is not used.
1728 static int ub_bd_revalidate(struct gendisk *disk)
1730 struct ub_lun *lun = disk->private_data;
1732 ub_revalidate(lun->udev, lun);
1734 /* XXX Support sector size switching like in sr.c */
1735 blk_queue_hardsect_size(disk->queue, lun->capacity.bsize);
1736 set_capacity(disk, lun->capacity.nsec);
1737 // set_disk_ro(sdkp->disk, lun->readonly);
1739 return 0;
1743 * The check is called by the block layer to verify if the media
1744 * is still available. It is supposed to be harmless, lightweight and
1745 * non-intrusive in case the media was not changed.
1747 * This call can sleep.
1749 * The return code is bool!
1751 static int ub_bd_media_changed(struct gendisk *disk)
1753 struct ub_lun *lun = disk->private_data;
1755 if (!lun->removable)
1756 return 0;
1759 * We clean checks always after every command, so this is not
1760 * as dangerous as it looks. If the TEST_UNIT_READY fails here,
1761 * the device is actually not ready with operator or software
1762 * intervention required. One dangerous item might be a drive which
1763 * spins itself down, and come the time to write dirty pages, this
1764 * will fail, then block layer discards the data. Since we never
1765 * spin drives up, such devices simply cannot be used with ub anyway.
1767 if (ub_sync_tur(lun->udev, lun) != 0) {
1768 lun->changed = 1;
1769 return 1;
1772 return lun->changed;
1775 static struct block_device_operations ub_bd_fops = {
1776 .owner = THIS_MODULE,
1777 .open = ub_bd_open,
1778 .release = ub_bd_release,
1779 .ioctl = ub_bd_ioctl,
1780 .media_changed = ub_bd_media_changed,
1781 .revalidate_disk = ub_bd_revalidate,
1785 * Common ->done routine for commands executed synchronously.
1787 static void ub_probe_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1789 struct completion *cop = cmd->back;
1790 complete(cop);
1794 * Test if the device has a check condition on it, synchronously.
1796 static int ub_sync_tur(struct ub_dev *sc, struct ub_lun *lun)
1798 struct ub_scsi_cmd *cmd;
1799 enum { ALLOC_SIZE = sizeof(struct ub_scsi_cmd) };
1800 unsigned long flags;
1801 struct completion compl;
1802 int rc;
1804 init_completion(&compl);
1806 rc = -ENOMEM;
1807 if ((cmd = kzalloc(ALLOC_SIZE, GFP_KERNEL)) == NULL)
1808 goto err_alloc;
1810 cmd->cdb[0] = TEST_UNIT_READY;
1811 cmd->cdb_len = 6;
1812 cmd->dir = UB_DIR_NONE;
1813 cmd->state = UB_CMDST_INIT;
1814 cmd->lun = lun; /* This may be NULL, but that's ok */
1815 cmd->done = ub_probe_done;
1816 cmd->back = &compl;
1818 spin_lock_irqsave(sc->lock, flags);
1819 cmd->tag = sc->tagcnt++;
1821 rc = ub_submit_scsi(sc, cmd);
1822 spin_unlock_irqrestore(sc->lock, flags);
1824 if (rc != 0)
1825 goto err_submit;
1827 wait_for_completion(&compl);
1829 rc = cmd->error;
1831 if (rc == -EIO && cmd->key != 0) /* Retries for benh's key */
1832 rc = cmd->key;
1834 err_submit:
1835 kfree(cmd);
1836 err_alloc:
1837 return rc;
1841 * Read the SCSI capacity synchronously (for probing).
1843 static int ub_sync_read_cap(struct ub_dev *sc, struct ub_lun *lun,
1844 struct ub_capacity *ret)
1846 struct ub_scsi_cmd *cmd;
1847 struct scatterlist *sg;
1848 char *p;
1849 enum { ALLOC_SIZE = sizeof(struct ub_scsi_cmd) + 8 };
1850 unsigned long flags;
1851 unsigned int bsize, shift;
1852 unsigned long nsec;
1853 struct completion compl;
1854 int rc;
1856 init_completion(&compl);
1858 rc = -ENOMEM;
1859 if ((cmd = kzalloc(ALLOC_SIZE, GFP_KERNEL)) == NULL)
1860 goto err_alloc;
1861 p = (char *)cmd + sizeof(struct ub_scsi_cmd);
1863 cmd->cdb[0] = 0x25;
1864 cmd->cdb_len = 10;
1865 cmd->dir = UB_DIR_READ;
1866 cmd->state = UB_CMDST_INIT;
1867 cmd->nsg = 1;
1868 sg = &cmd->sgv[0];
1869 sg->page = virt_to_page(p);
1870 sg->offset = (unsigned long)p & (PAGE_SIZE-1);
1871 sg->length = 8;
1872 cmd->len = 8;
1873 cmd->lun = lun;
1874 cmd->done = ub_probe_done;
1875 cmd->back = &compl;
1877 spin_lock_irqsave(sc->lock, flags);
1878 cmd->tag = sc->tagcnt++;
1880 rc = ub_submit_scsi(sc, cmd);
1881 spin_unlock_irqrestore(sc->lock, flags);
1883 if (rc != 0)
1884 goto err_submit;
1886 wait_for_completion(&compl);
1888 if (cmd->error != 0) {
1889 rc = -EIO;
1890 goto err_read;
1892 if (cmd->act_len != 8) {
1893 rc = -EIO;
1894 goto err_read;
1897 /* sd.c special-cases sector size of 0 to mean 512. Needed? Safe? */
1898 nsec = be32_to_cpu(*(__be32 *)p) + 1;
1899 bsize = be32_to_cpu(*(__be32 *)(p + 4));
1900 switch (bsize) {
1901 case 512: shift = 0; break;
1902 case 1024: shift = 1; break;
1903 case 2048: shift = 2; break;
1904 case 4096: shift = 3; break;
1905 default:
1906 rc = -EDOM;
1907 goto err_inv_bsize;
1910 ret->bsize = bsize;
1911 ret->bshift = shift;
1912 ret->nsec = nsec << shift;
1913 rc = 0;
1915 err_inv_bsize:
1916 err_read:
1917 err_submit:
1918 kfree(cmd);
1919 err_alloc:
1920 return rc;
1925 static void ub_probe_urb_complete(struct urb *urb)
1927 struct completion *cop = urb->context;
1928 complete(cop);
1931 static void ub_probe_timeout(unsigned long arg)
1933 struct completion *cop = (struct completion *) arg;
1934 complete(cop);
1938 * Reset with a Bulk reset.
1940 static int ub_sync_reset(struct ub_dev *sc)
1942 int ifnum = sc->intf->cur_altsetting->desc.bInterfaceNumber;
1943 struct usb_ctrlrequest *cr;
1944 struct completion compl;
1945 struct timer_list timer;
1946 int rc;
1948 init_completion(&compl);
1950 cr = &sc->work_cr;
1951 cr->bRequestType = USB_TYPE_CLASS | USB_RECIP_INTERFACE;
1952 cr->bRequest = US_BULK_RESET_REQUEST;
1953 cr->wValue = cpu_to_le16(0);
1954 cr->wIndex = cpu_to_le16(ifnum);
1955 cr->wLength = cpu_to_le16(0);
1957 usb_fill_control_urb(&sc->work_urb, sc->dev, sc->send_ctrl_pipe,
1958 (unsigned char*) cr, NULL, 0, ub_probe_urb_complete, &compl);
1959 sc->work_urb.actual_length = 0;
1960 sc->work_urb.error_count = 0;
1961 sc->work_urb.status = 0;
1963 if ((rc = usb_submit_urb(&sc->work_urb, GFP_KERNEL)) != 0) {
1964 printk(KERN_WARNING
1965 "%s: Unable to submit a bulk reset (%d)\n", sc->name, rc);
1966 return rc;
1969 init_timer(&timer);
1970 timer.function = ub_probe_timeout;
1971 timer.data = (unsigned long) &compl;
1972 timer.expires = jiffies + UB_CTRL_TIMEOUT;
1973 add_timer(&timer);
1975 wait_for_completion(&compl);
1977 del_timer_sync(&timer);
1978 usb_kill_urb(&sc->work_urb);
1980 return sc->work_urb.status;
1984 * Get number of LUNs by the way of Bulk GetMaxLUN command.
1986 static int ub_sync_getmaxlun(struct ub_dev *sc)
1988 int ifnum = sc->intf->cur_altsetting->desc.bInterfaceNumber;
1989 unsigned char *p;
1990 enum { ALLOC_SIZE = 1 };
1991 struct usb_ctrlrequest *cr;
1992 struct completion compl;
1993 struct timer_list timer;
1994 int nluns;
1995 int rc;
1997 init_completion(&compl);
1999 rc = -ENOMEM;
2000 if ((p = kmalloc(ALLOC_SIZE, GFP_KERNEL)) == NULL)
2001 goto err_alloc;
2002 *p = 55;
2004 cr = &sc->work_cr;
2005 cr->bRequestType = USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
2006 cr->bRequest = US_BULK_GET_MAX_LUN;
2007 cr->wValue = cpu_to_le16(0);
2008 cr->wIndex = cpu_to_le16(ifnum);
2009 cr->wLength = cpu_to_le16(1);
2011 usb_fill_control_urb(&sc->work_urb, sc->dev, sc->recv_ctrl_pipe,
2012 (unsigned char*) cr, p, 1, ub_probe_urb_complete, &compl);
2013 sc->work_urb.actual_length = 0;
2014 sc->work_urb.error_count = 0;
2015 sc->work_urb.status = 0;
2017 if ((rc = usb_submit_urb(&sc->work_urb, GFP_KERNEL)) != 0)
2018 goto err_submit;
2020 init_timer(&timer);
2021 timer.function = ub_probe_timeout;
2022 timer.data = (unsigned long) &compl;
2023 timer.expires = jiffies + UB_CTRL_TIMEOUT;
2024 add_timer(&timer);
2026 wait_for_completion(&compl);
2028 del_timer_sync(&timer);
2029 usb_kill_urb(&sc->work_urb);
2031 if ((rc = sc->work_urb.status) < 0)
2032 goto err_io;
2034 if (sc->work_urb.actual_length != 1) {
2035 nluns = 0;
2036 } else {
2037 if ((nluns = *p) == 55) {
2038 nluns = 0;
2039 } else {
2040 /* GetMaxLUN returns the maximum LUN number */
2041 nluns += 1;
2042 if (nluns > UB_MAX_LUNS)
2043 nluns = UB_MAX_LUNS;
2047 kfree(p);
2048 return nluns;
2050 err_io:
2051 err_submit:
2052 kfree(p);
2053 err_alloc:
2054 return rc;
2058 * Clear initial stalls.
2060 static int ub_probe_clear_stall(struct ub_dev *sc, int stalled_pipe)
2062 int endp;
2063 struct usb_ctrlrequest *cr;
2064 struct completion compl;
2065 struct timer_list timer;
2066 int rc;
2068 init_completion(&compl);
2070 endp = usb_pipeendpoint(stalled_pipe);
2071 if (usb_pipein (stalled_pipe))
2072 endp |= USB_DIR_IN;
2074 cr = &sc->work_cr;
2075 cr->bRequestType = USB_RECIP_ENDPOINT;
2076 cr->bRequest = USB_REQ_CLEAR_FEATURE;
2077 cr->wValue = cpu_to_le16(USB_ENDPOINT_HALT);
2078 cr->wIndex = cpu_to_le16(endp);
2079 cr->wLength = cpu_to_le16(0);
2081 usb_fill_control_urb(&sc->work_urb, sc->dev, sc->send_ctrl_pipe,
2082 (unsigned char*) cr, NULL, 0, ub_probe_urb_complete, &compl);
2083 sc->work_urb.actual_length = 0;
2084 sc->work_urb.error_count = 0;
2085 sc->work_urb.status = 0;
2087 if ((rc = usb_submit_urb(&sc->work_urb, GFP_KERNEL)) != 0) {
2088 printk(KERN_WARNING
2089 "%s: Unable to submit a probe clear (%d)\n", sc->name, rc);
2090 return rc;
2093 init_timer(&timer);
2094 timer.function = ub_probe_timeout;
2095 timer.data = (unsigned long) &compl;
2096 timer.expires = jiffies + UB_CTRL_TIMEOUT;
2097 add_timer(&timer);
2099 wait_for_completion(&compl);
2101 del_timer_sync(&timer);
2102 usb_kill_urb(&sc->work_urb);
2104 /* reset the endpoint toggle */
2105 usb_settoggle(sc->dev, endp, usb_pipeout(sc->last_pipe), 0);
2107 return 0;
2111 * Get the pipe settings.
2113 static int ub_get_pipes(struct ub_dev *sc, struct usb_device *dev,
2114 struct usb_interface *intf)
2116 struct usb_host_interface *altsetting = intf->cur_altsetting;
2117 struct usb_endpoint_descriptor *ep_in = NULL;
2118 struct usb_endpoint_descriptor *ep_out = NULL;
2119 struct usb_endpoint_descriptor *ep;
2120 int i;
2123 * Find the endpoints we need.
2124 * We are expecting a minimum of 2 endpoints - in and out (bulk).
2125 * We will ignore any others.
2127 for (i = 0; i < altsetting->desc.bNumEndpoints; i++) {
2128 ep = &altsetting->endpoint[i].desc;
2130 /* Is it a BULK endpoint? */
2131 if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
2132 == USB_ENDPOINT_XFER_BULK) {
2133 /* BULK in or out? */
2134 if (ep->bEndpointAddress & USB_DIR_IN) {
2135 if (ep_in == NULL)
2136 ep_in = ep;
2137 } else {
2138 if (ep_out == NULL)
2139 ep_out = ep;
2144 if (ep_in == NULL || ep_out == NULL) {
2145 printk(KERN_NOTICE "%s: failed endpoint check\n",
2146 sc->name);
2147 return -ENODEV;
2150 /* Calculate and store the pipe values */
2151 sc->send_ctrl_pipe = usb_sndctrlpipe(dev, 0);
2152 sc->recv_ctrl_pipe = usb_rcvctrlpipe(dev, 0);
2153 sc->send_bulk_pipe = usb_sndbulkpipe(dev,
2154 ep_out->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
2155 sc->recv_bulk_pipe = usb_rcvbulkpipe(dev,
2156 ep_in->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
2158 return 0;
2162 * Probing is done in the process context, which allows us to cheat
2163 * and not to build a state machine for the discovery.
2165 static int ub_probe(struct usb_interface *intf,
2166 const struct usb_device_id *dev_id)
2168 struct ub_dev *sc;
2169 int nluns;
2170 int rc;
2171 int i;
2173 if (usb_usual_check_type(dev_id, USB_US_TYPE_UB))
2174 return -ENXIO;
2176 rc = -ENOMEM;
2177 if ((sc = kzalloc(sizeof(struct ub_dev), GFP_KERNEL)) == NULL)
2178 goto err_core;
2179 sc->lock = ub_next_lock();
2180 INIT_LIST_HEAD(&sc->luns);
2181 usb_init_urb(&sc->work_urb);
2182 tasklet_init(&sc->tasklet, ub_scsi_action, (unsigned long)sc);
2183 atomic_set(&sc->poison, 0);
2184 INIT_WORK(&sc->reset_work, ub_reset_task);
2185 init_waitqueue_head(&sc->reset_wait);
2187 init_timer(&sc->work_timer);
2188 sc->work_timer.data = (unsigned long) sc;
2189 sc->work_timer.function = ub_urb_timeout;
2191 ub_init_completion(&sc->work_done);
2192 sc->work_done.done = 1; /* A little yuk, but oh well... */
2194 sc->dev = interface_to_usbdev(intf);
2195 sc->intf = intf;
2196 // sc->ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
2197 usb_set_intfdata(intf, sc);
2198 usb_get_dev(sc->dev);
2200 * Since we give the interface struct to the block level through
2201 * disk->driverfs_dev, we have to pin it. Otherwise, block_uevent
2202 * oopses on close after a disconnect (kernels 2.6.16 and up).
2204 usb_get_intf(sc->intf);
2206 snprintf(sc->name, 12, DRV_NAME "(%d.%d)",
2207 sc->dev->bus->busnum, sc->dev->devnum);
2209 /* XXX Verify that we can handle the device (from descriptors) */
2211 if (ub_get_pipes(sc, sc->dev, intf) != 0)
2212 goto err_dev_desc;
2215 * At this point, all USB initialization is done, do upper layer.
2216 * We really hate halfway initialized structures, so from the
2217 * invariants perspective, this ub_dev is fully constructed at
2218 * this point.
2222 * This is needed to clear toggles. It is a problem only if we do
2223 * `rmmod ub && modprobe ub` without disconnects, but we like that.
2225 #if 0 /* iPod Mini fails if we do this (big white iPod works) */
2226 ub_probe_clear_stall(sc, sc->recv_bulk_pipe);
2227 ub_probe_clear_stall(sc, sc->send_bulk_pipe);
2228 #endif
2231 * The way this is used by the startup code is a little specific.
2232 * A SCSI check causes a USB stall. Our common case code sees it
2233 * and clears the check, after which the device is ready for use.
2234 * But if a check was not present, any command other than
2235 * TEST_UNIT_READY ends with a lockup (including REQUEST_SENSE).
2237 * If we neglect to clear the SCSI check, the first real command fails
2238 * (which is the capacity readout). We clear that and retry, but why
2239 * causing spurious retries for no reason.
2241 * Revalidation may start with its own TEST_UNIT_READY, but that one
2242 * has to succeed, so we clear checks with an additional one here.
2243 * In any case it's not our business how revaliadation is implemented.
2245 for (i = 0; i < 3; i++) { /* Retries for the schwag key from KS'04 */
2246 if ((rc = ub_sync_tur(sc, NULL)) <= 0) break;
2247 if (rc != 0x6) break;
2248 msleep(10);
2251 nluns = 1;
2252 for (i = 0; i < 3; i++) {
2253 if ((rc = ub_sync_getmaxlun(sc)) < 0)
2254 break;
2255 if (rc != 0) {
2256 nluns = rc;
2257 break;
2259 msleep(100);
2262 for (i = 0; i < nluns; i++) {
2263 ub_probe_lun(sc, i);
2265 return 0;
2267 err_dev_desc:
2268 usb_set_intfdata(intf, NULL);
2269 usb_put_intf(sc->intf);
2270 usb_put_dev(sc->dev);
2271 kfree(sc);
2272 err_core:
2273 return rc;
2276 static int ub_probe_lun(struct ub_dev *sc, int lnum)
2278 struct ub_lun *lun;
2279 request_queue_t *q;
2280 struct gendisk *disk;
2281 int rc;
2283 rc = -ENOMEM;
2284 if ((lun = kzalloc(sizeof(struct ub_lun), GFP_KERNEL)) == NULL)
2285 goto err_alloc;
2286 lun->num = lnum;
2288 rc = -ENOSR;
2289 if ((lun->id = ub_id_get()) == -1)
2290 goto err_id;
2292 lun->udev = sc;
2294 snprintf(lun->name, 16, DRV_NAME "%c(%d.%d.%d)",
2295 lun->id + 'a', sc->dev->bus->busnum, sc->dev->devnum, lun->num);
2297 lun->removable = 1; /* XXX Query this from the device */
2298 lun->changed = 1; /* ub_revalidate clears only */
2299 ub_revalidate(sc, lun);
2301 rc = -ENOMEM;
2302 if ((disk = alloc_disk(UB_PARTS_PER_LUN)) == NULL)
2303 goto err_diskalloc;
2305 sprintf(disk->disk_name, DRV_NAME "%c", lun->id + 'a');
2306 disk->major = UB_MAJOR;
2307 disk->first_minor = lun->id * UB_PARTS_PER_LUN;
2308 disk->fops = &ub_bd_fops;
2309 disk->private_data = lun;
2310 disk->driverfs_dev = &sc->intf->dev;
2312 rc = -ENOMEM;
2313 if ((q = blk_init_queue(ub_request_fn, sc->lock)) == NULL)
2314 goto err_blkqinit;
2316 disk->queue = q;
2318 blk_queue_bounce_limit(q, BLK_BOUNCE_HIGH);
2319 blk_queue_max_hw_segments(q, UB_MAX_REQ_SG);
2320 blk_queue_max_phys_segments(q, UB_MAX_REQ_SG);
2321 blk_queue_segment_boundary(q, 0xffffffff); /* Dubious. */
2322 blk_queue_max_sectors(q, UB_MAX_SECTORS);
2323 blk_queue_hardsect_size(q, lun->capacity.bsize);
2325 lun->disk = disk;
2326 q->queuedata = lun;
2327 list_add(&lun->link, &sc->luns);
2329 set_capacity(disk, lun->capacity.nsec);
2330 if (lun->removable)
2331 disk->flags |= GENHD_FL_REMOVABLE;
2333 add_disk(disk);
2335 return 0;
2337 err_blkqinit:
2338 put_disk(disk);
2339 err_diskalloc:
2340 ub_id_put(lun->id);
2341 err_id:
2342 kfree(lun);
2343 err_alloc:
2344 return rc;
2347 static void ub_disconnect(struct usb_interface *intf)
2349 struct ub_dev *sc = usb_get_intfdata(intf);
2350 struct list_head *p;
2351 struct ub_lun *lun;
2352 unsigned long flags;
2355 * Prevent ub_bd_release from pulling the rug from under us.
2356 * XXX This is starting to look like a kref.
2357 * XXX Why not to take this ref at probe time?
2359 spin_lock_irqsave(&ub_lock, flags);
2360 sc->openc++;
2361 spin_unlock_irqrestore(&ub_lock, flags);
2364 * Fence stall clearnings, operations triggered by unlinkings and so on.
2365 * We do not attempt to unlink any URBs, because we do not trust the
2366 * unlink paths in HC drivers. Also, we get -84 upon disconnect anyway.
2368 atomic_set(&sc->poison, 1);
2371 * Wait for reset to end, if any.
2373 wait_event(sc->reset_wait, !sc->reset);
2376 * Blow away queued commands.
2378 * Actually, this never works, because before we get here
2379 * the HCD terminates outstanding URB(s). It causes our
2380 * SCSI command queue to advance, commands fail to submit,
2381 * and the whole queue drains. So, we just use this code to
2382 * print warnings.
2384 spin_lock_irqsave(sc->lock, flags);
2386 struct ub_scsi_cmd *cmd;
2387 int cnt = 0;
2388 while ((cmd = ub_cmdq_peek(sc)) != NULL) {
2389 cmd->error = -ENOTCONN;
2390 cmd->state = UB_CMDST_DONE;
2391 ub_cmdq_pop(sc);
2392 (*cmd->done)(sc, cmd);
2393 cnt++;
2395 if (cnt != 0) {
2396 printk(KERN_WARNING "%s: "
2397 "%d was queued after shutdown\n", sc->name, cnt);
2400 spin_unlock_irqrestore(sc->lock, flags);
2403 * Unregister the upper layer.
2405 list_for_each (p, &sc->luns) {
2406 lun = list_entry(p, struct ub_lun, link);
2407 del_gendisk(lun->disk);
2409 * I wish I could do:
2410 * set_bit(QUEUE_FLAG_DEAD, &q->queue_flags);
2411 * As it is, we rely on our internal poisoning and let
2412 * the upper levels to spin furiously failing all the I/O.
2417 * Testing for -EINPROGRESS is always a bug, so we are bending
2418 * the rules a little.
2420 spin_lock_irqsave(sc->lock, flags);
2421 if (sc->work_urb.status == -EINPROGRESS) { /* janitors: ignore */
2422 printk(KERN_WARNING "%s: "
2423 "URB is active after disconnect\n", sc->name);
2425 spin_unlock_irqrestore(sc->lock, flags);
2428 * There is virtually no chance that other CPU runs times so long
2429 * after ub_urb_complete should have called del_timer, but only if HCD
2430 * didn't forget to deliver a callback on unlink.
2432 del_timer_sync(&sc->work_timer);
2435 * At this point there must be no commands coming from anyone
2436 * and no URBs left in transit.
2439 ub_put(sc);
2442 static struct usb_driver ub_driver = {
2443 .name = "ub",
2444 .probe = ub_probe,
2445 .disconnect = ub_disconnect,
2446 .id_table = ub_usb_ids,
2449 static int __init ub_init(void)
2451 int rc;
2452 int i;
2454 for (i = 0; i < UB_QLOCK_NUM; i++)
2455 spin_lock_init(&ub_qlockv[i]);
2457 if ((rc = register_blkdev(UB_MAJOR, DRV_NAME)) != 0)
2458 goto err_regblkdev;
2460 if ((rc = usb_register(&ub_driver)) != 0)
2461 goto err_register;
2463 usb_usual_set_present(USB_US_TYPE_UB);
2464 return 0;
2466 err_register:
2467 unregister_blkdev(UB_MAJOR, DRV_NAME);
2468 err_regblkdev:
2469 return rc;
2472 static void __exit ub_exit(void)
2474 usb_deregister(&ub_driver);
2476 unregister_blkdev(UB_MAJOR, DRV_NAME);
2477 usb_usual_clear_present(USB_US_TYPE_UB);
2480 module_init(ub_init);
2481 module_exit(ub_exit);
2483 MODULE_LICENSE("GPL");