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 * -- Kill first_open (Al Viro fixed the block layer now)
12 * -- Do resets with usb_device_reset (needs a thread context, use khubd)
13 * -- set readonly flag for CDs, set removable flag for CF readers
14 * -- do inquiry and verify we got a disk and not a tape (for LUN mismatch)
15 * -- special case some senses, e.g. 3a/0 -> no media present, reduce retries
16 * -- verify the 13 conditions and do bulk resets
17 * -- kill last_pipe and simply do two-state clearing on both pipes
18 * -- verify protocol (bulk) from USB descriptors (maybe...)
20 * -- move top_sense and work_bcs into separate allocations (if they survive)
21 * for cache purists and esoteric architectures.
22 * -- Allocate structure for LUN 0 before the first ub_sync_tur, avoid NULL. ?
23 * -- prune comments, they are too volumnous
24 * -- Exterminate P3 printks
26 * -- Redo "benh's retries", perhaps have spin-up code to handle them. V:D=?
27 * -- CLEAR, CLR2STS, CLRRS seem to be ripe for refactoring.
29 #include <linux/kernel.h>
30 #include <linux/module.h>
31 #include <linux/usb.h>
32 #include <linux/blkdev.h>
33 #include <linux/devfs_fs_kernel.h>
34 #include <linux/timer.h>
35 #include <scsi/scsi.h>
38 #define DEVFS_NAME DRV_NAME
43 * The command state machine is the key model for understanding of this driver.
45 * The general rule is that all transitions are done towards the bottom
46 * of the diagram, thus preventing any loops.
48 * An exception to that is how the STAT state is handled. A counter allows it
49 * to be re-entered along the path marked with [C].
55 * ub_scsi_cmd_start fails ->--------------------------------------\
62 * was -EPIPE -->-------------------------------->! CLEAR ! !
65 * was error -->------------------------------------- ! --------->\
67 * /--<-- cmd->dir == NONE ? ! !
74 * ! was -EPIPE -->--------------->! CLR2STS ! ! !
77 * ! ! was error -->---- ! --------->\
78 * ! was error -->--------------------- ! ------------- ! --------->\
81 * \--->+--------+ ! ! !
82 * ! STAT !<--------------------------/ ! !
85 * [C] was -EPIPE -->-----------\ ! !
87 * +<---- len == 0 ! ! !
89 * ! was error -->--------------------------------------!---------->\
91 * +<---- bad CSW ! ! !
92 * +<---- bad tag ! ! !
98 * \------- ! --------------------[C]--------\ ! !
100 * cmd->error---\ +--------+ ! !
101 * ! +--------------->! SENSE !<----------/ !
102 * STAT_FAIL----/ +--------+ !
105 * \--------------------------------\--------------------->! DONE !
110 * Definitions which have to be scattered once we understand the layout better.
113 /* Transport (despite PR in the name) */
114 #define US_PR_BULK 0x50 /* bulk only */
117 #define US_SC_SCSI 0x06 /* Transparent */
120 * This many LUNs per USB device.
121 * Every one of them takes a host, see UB_MAX_HOSTS.
123 #define UB_MAX_LUNS 9
128 #define UB_MINORS_PER_MAJOR 8
130 #define UB_MAX_CDB_SIZE 16 /* Corresponds to Bulk */
132 #define UB_SENSE_SIZE 18
137 /* command block wrapper */
138 struct bulk_cb_wrap {
139 __le32 Signature; /* contains 'USBC' */
140 u32 Tag; /* unique per command id */
141 __le32 DataTransferLength; /* size of data */
142 u8 Flags; /* direction in bit 0 */
144 u8 Length; /* of of the CDB */
145 u8 CDB[UB_MAX_CDB_SIZE]; /* max command */
148 #define US_BULK_CB_WRAP_LEN 31
149 #define US_BULK_CB_SIGN 0x43425355 /*spells out USBC */
150 #define US_BULK_FLAG_IN 1
151 #define US_BULK_FLAG_OUT 0
153 /* command status wrapper */
154 struct bulk_cs_wrap {
155 __le32 Signature; /* should = 'USBS' */
156 u32 Tag; /* same as original command */
157 __le32 Residue; /* amount not transferred */
158 u8 Status; /* see below */
161 #define US_BULK_CS_WRAP_LEN 13
162 #define US_BULK_CS_SIGN 0x53425355 /* spells out 'USBS' */
163 #define US_BULK_STAT_OK 0
164 #define US_BULK_STAT_FAIL 1
165 #define US_BULK_STAT_PHASE 2
167 /* bulk-only class specific requests */
168 #define US_BULK_RESET_REQUEST 0xff
169 #define US_BULK_GET_MAX_LUN 0xfe
175 #define UB_MAX_REQ_SG 4
176 #define UB_MAX_SECTORS 64
179 * A second is more than enough for a 32K transfer (UB_MAX_SECTORS)
180 * even if a webcam hogs the bus, but some devices need time to spin up.
182 #define UB_URB_TIMEOUT (HZ*2)
183 #define UB_DATA_TIMEOUT (HZ*5) /* ZIP does spin-ups in the data phase */
184 #define UB_STAT_TIMEOUT (HZ*5) /* Same spinups and eject for a dataless cmd. */
185 #define UB_CTRL_TIMEOUT (HZ/2) /* 500ms ought to be enough to clear a stall */
188 * An instance of a SCSI command in transit.
190 #define UB_DIR_NONE 0
191 #define UB_DIR_READ 1
192 #define UB_DIR_ILLEGAL2 2
193 #define UB_DIR_WRITE 3
195 #define UB_DIR_CHAR(c) (((c)==UB_DIR_WRITE)? 'w': \
196 (((c)==UB_DIR_READ)? 'r': 'n'))
198 enum ub_scsi_cmd_state {
199 UB_CMDST_INIT, /* Initial state */
200 UB_CMDST_CMD, /* Command submitted */
201 UB_CMDST_DATA, /* Data phase */
202 UB_CMDST_CLR2STS, /* Clearing before requesting status */
203 UB_CMDST_STAT, /* Status phase */
204 UB_CMDST_CLEAR, /* Clearing a stall (halt, actually) */
205 UB_CMDST_CLRRS, /* Clearing before retrying status */
206 UB_CMDST_SENSE, /* Sending Request Sense */
207 UB_CMDST_DONE /* Final state */
210 static char *ub_scsi_cmd_stname[] = {
223 unsigned char cdb[UB_MAX_CDB_SIZE];
224 unsigned char cdb_len;
226 unsigned char dir; /* 0 - none, 1 - read, 3 - write. */
227 unsigned char trace_index;
228 enum ub_scsi_cmd_state state;
230 struct ub_scsi_cmd *next;
232 int error; /* Return code - valid upon done */
233 unsigned int act_len; /* Return size */
234 unsigned char key, asc, ascq; /* May be valid if error==-EIO */
236 int stat_count; /* Retries getting status. */
239 * We do not support transfers from highmem pages
240 * because the underlying USB framework does not do what we need.
242 char *data; /* Requested buffer */
243 unsigned int len; /* Requested length */
246 void (*done)(struct ub_dev *, struct ub_scsi_cmd *);
253 unsigned int current_block;
254 unsigned int current_sg;
255 unsigned int nsg; /* sgv[nsg] */
256 struct scatterlist sgv[UB_MAX_REQ_SG];
262 unsigned long nsec; /* Linux size - 512 byte sectors */
263 unsigned int bsize; /* Linux hardsect_size */
264 unsigned int bshift; /* Shift between 512 and hard sects */
268 * The SCSI command tracing structure.
271 #define SCMD_ST_HIST_SZ 8
272 #define SCMD_TRACE_SZ 63 /* Less than 4KB of 61-byte lines */
274 struct ub_scsi_cmd_trace {
277 unsigned int req_size, act_size;
280 unsigned char key, asc, ascq;
281 char st_hst[SCMD_ST_HIST_SZ];
284 struct ub_scsi_trace {
286 struct ub_scsi_cmd_trace vec[SCMD_TRACE_SZ];
290 * This is a direct take-off from linux/include/completion.h
291 * The difference is that I do not wait on this thing, just poll.
292 * When I want to wait (ub_probe), I just use the stock completion.
294 * Note that INIT_COMPLETION takes no lock. It is correct. But why
295 * in the bloody hell that thing takes struct instead of pointer to struct
296 * is quite beyond me. I just copied it from the stock completion.
298 struct ub_completion {
303 static inline void ub_init_completion(struct ub_completion *x)
306 spin_lock_init(&x->lock);
309 #define UB_INIT_COMPLETION(x) ((x).done = 0)
311 static void ub_complete(struct ub_completion *x)
315 spin_lock_irqsave(&x->lock, flags);
317 spin_unlock_irqrestore(&x->lock, flags);
320 static int ub_is_completed(struct ub_completion *x)
325 spin_lock_irqsave(&x->lock, flags);
327 spin_unlock_irqrestore(&x->lock, flags);
333 struct ub_scsi_cmd_queue {
335 struct ub_scsi_cmd *head, *tail;
339 * The block device instance (one per LUN).
343 struct list_head link;
344 struct gendisk *disk;
345 int id; /* Host index */
346 int num; /* LUN number */
349 int changed; /* Media was changed */
352 int first_open; /* Kludge. See ub_bd_open. */
354 struct ub_request urq;
356 /* Use Ingo's mempool if or when we have more than one command. */
358 * Currently we never need more than one command for the whole device.
359 * However, giving every LUN a command is a cheap and automatic way
360 * to enforce fairness between them.
363 struct ub_scsi_cmd cmdv[1];
365 struct ub_capacity capacity;
369 * The USB device instance.
373 atomic_t poison; /* The USB device is disconnected */
374 int openc; /* protected by ub_lock! */
375 /* kref is too implicit for our taste */
378 struct usb_device *dev;
379 struct usb_interface *intf;
381 struct list_head luns;
383 unsigned int send_bulk_pipe; /* cached pipe values */
384 unsigned int recv_bulk_pipe;
385 unsigned int send_ctrl_pipe;
386 unsigned int recv_ctrl_pipe;
388 struct tasklet_struct tasklet;
390 struct ub_scsi_cmd_queue cmd_queue;
391 struct ub_scsi_cmd top_rqs_cmd; /* REQUEST SENSE */
392 unsigned char top_sense[UB_SENSE_SIZE];
394 struct ub_completion work_done;
396 struct timer_list work_timer;
397 int last_pipe; /* What might need clearing */
398 __le32 signature; /* Learned signature */
399 struct bulk_cb_wrap work_bcb;
400 struct bulk_cs_wrap work_bcs;
401 struct usb_ctrlrequest work_cr;
403 int sg_stat[UB_MAX_REQ_SG+1];
404 struct ub_scsi_trace tr;
409 static void ub_cleanup(struct ub_dev *sc);
410 static int ub_bd_rq_fn_1(struct ub_lun *lun, struct request *rq);
411 static int ub_cmd_build_block(struct ub_dev *sc, struct ub_lun *lun,
412 struct ub_scsi_cmd *cmd, struct request *rq);
413 static void ub_scsi_build_block(struct ub_lun *lun,
414 struct ub_scsi_cmd *cmd, struct ub_request *urq);
415 static int ub_cmd_build_packet(struct ub_dev *sc, struct ub_lun *lun,
416 struct ub_scsi_cmd *cmd, struct request *rq);
417 static void ub_rw_cmd_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
418 static void ub_end_rq(struct request *rq, int uptodate);
419 static int ub_request_advance(struct ub_dev *sc, struct ub_lun *lun,
420 struct ub_request *urq, struct ub_scsi_cmd *cmd);
421 static int ub_submit_scsi(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
422 static void ub_urb_complete(struct urb *urb, struct pt_regs *pt);
423 static void ub_scsi_action(unsigned long _dev);
424 static void ub_scsi_dispatch(struct ub_dev *sc);
425 static void ub_scsi_urb_compl(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
426 static void ub_state_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd, int rc);
427 static int __ub_state_stat(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
428 static void ub_state_stat(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
429 static void ub_state_stat_counted(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
430 static void ub_state_sense(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
431 static int ub_submit_clear_stall(struct ub_dev *sc, struct ub_scsi_cmd *cmd,
433 static void ub_top_sense_done(struct ub_dev *sc, struct ub_scsi_cmd *scmd);
434 static int ub_sync_tur(struct ub_dev *sc, struct ub_lun *lun);
435 static int ub_sync_read_cap(struct ub_dev *sc, struct ub_lun *lun,
436 struct ub_capacity *ret);
437 static int ub_probe_lun(struct ub_dev *sc, int lnum);
441 static struct usb_device_id ub_usb_ids[] = {
442 // { USB_DEVICE_VER(0x0781, 0x0002, 0x0009, 0x0009) }, /* SDDR-31 */
443 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_SCSI, US_PR_BULK) },
447 MODULE_DEVICE_TABLE(usb, ub_usb_ids);
450 * Find me a way to identify "next free minor" for add_disk(),
451 * and the array disappears the next day. However, the number of
452 * hosts has something to do with the naming and /proc/partitions.
453 * This has to be thought out in detail before changing.
454 * If UB_MAX_HOST was 1000, we'd use a bitmap. Or a better data structure.
456 #define UB_MAX_HOSTS 26
457 static char ub_hostv[UB_MAX_HOSTS];
459 static DEFINE_SPINLOCK(ub_lock); /* Locks globals and ->openc */
462 * The SCSI command tracing procedures.
465 static void ub_cmdtr_new(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
468 struct ub_scsi_cmd_trace *t;
470 if ((n = sc->tr.cur + 1) == SCMD_TRACE_SZ) n = 0;
473 memset(t, 0, sizeof(struct ub_scsi_cmd_trace));
477 t->req_size = cmd->len;
478 t->st_hst[0] = cmd->state;
481 cmd->trace_index = n;
484 static void ub_cmdtr_state(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
487 struct ub_scsi_cmd_trace *t;
489 t = &sc->tr.vec[cmd->trace_index];
490 if (t->tag == cmd->tag) {
491 if ((n = t->hcur + 1) == SCMD_ST_HIST_SZ) n = 0;
492 t->st_hst[n] = cmd->state;
497 static void ub_cmdtr_act_len(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
499 struct ub_scsi_cmd_trace *t;
501 t = &sc->tr.vec[cmd->trace_index];
502 if (t->tag == cmd->tag)
503 t->act_size = cmd->act_len;
506 static void ub_cmdtr_sense(struct ub_dev *sc, struct ub_scsi_cmd *cmd,
507 unsigned char *sense)
509 struct ub_scsi_cmd_trace *t;
511 t = &sc->tr.vec[cmd->trace_index];
512 if (t->tag == cmd->tag) {
513 t->key = sense[2] & 0x0F;
519 static ssize_t ub_diag_show(struct device *dev, struct device_attribute *attr, char *page)
521 struct usb_interface *intf;
529 struct ub_scsi_cmd_trace *t;
531 intf = to_usb_interface(dev);
532 sc = usb_get_intfdata(intf);
537 spin_lock_irqsave(&sc->lock, flags);
539 cnt += sprintf(page + cnt,
541 sc->cmd_queue.qlen, sc->cmd_queue.qmax);
542 cnt += sprintf(page + cnt,
543 "sg %d %d %d %d %d\n",
550 list_for_each (p, &sc->luns) {
551 lun = list_entry(p, struct ub_lun, link);
552 cnt += sprintf(page + cnt,
553 "lun %u changed %d removable %d readonly %d\n",
554 lun->num, lun->changed, lun->removable, lun->readonly);
557 if ((nc = sc->tr.cur + 1) == SCMD_TRACE_SZ) nc = 0;
558 for (j = 0; j < SCMD_TRACE_SZ; j++) {
561 cnt += sprintf(page + cnt, "%08x %02x", t->tag, t->op);
562 if (t->op == REQUEST_SENSE) {
563 cnt += sprintf(page + cnt, " [sense %x %02x %02x]",
564 t->key, t->asc, t->ascq);
566 cnt += sprintf(page + cnt, " %c", UB_DIR_CHAR(t->dir));
567 cnt += sprintf(page + cnt, " [%5d %5d]",
568 t->req_size, t->act_size);
570 if ((nh = t->hcur + 1) == SCMD_ST_HIST_SZ) nh = 0;
571 for (i = 0; i < SCMD_ST_HIST_SZ; i++) {
572 cnt += sprintf(page + cnt, " %s",
573 ub_scsi_cmd_stname[(int)t->st_hst[nh]]);
574 if (++nh == SCMD_ST_HIST_SZ) nh = 0;
576 cnt += sprintf(page + cnt, "\n");
578 if (++nc == SCMD_TRACE_SZ) nc = 0;
581 spin_unlock_irqrestore(&sc->lock, flags);
585 static DEVICE_ATTR(diag, S_IRUGO, ub_diag_show, NULL); /* N.B. World readable */
590 * This also stores the host for indexing by minor, which is somewhat dirty.
592 static int ub_id_get(void)
597 spin_lock_irqsave(&ub_lock, flags);
598 for (i = 0; i < UB_MAX_HOSTS; i++) {
599 if (ub_hostv[i] == 0) {
601 spin_unlock_irqrestore(&ub_lock, flags);
605 spin_unlock_irqrestore(&ub_lock, flags);
609 static void ub_id_put(int id)
613 if (id < 0 || id >= UB_MAX_HOSTS) {
614 printk(KERN_ERR DRV_NAME ": bad host ID %d\n", id);
618 spin_lock_irqsave(&ub_lock, flags);
619 if (ub_hostv[id] == 0) {
620 spin_unlock_irqrestore(&ub_lock, flags);
621 printk(KERN_ERR DRV_NAME ": freeing free host ID %d\n", id);
625 spin_unlock_irqrestore(&ub_lock, flags);
629 * Downcount for deallocation. This rides on two assumptions:
630 * - once something is poisoned, its refcount cannot grow
631 * - opens cannot happen at this time (del_gendisk was done)
632 * If the above is true, we can drop the lock, which we need for
633 * blk_cleanup_queue(): the silly thing may attempt to sleep.
634 * [Actually, it never needs to sleep for us, but it calls might_sleep()]
636 static void ub_put(struct ub_dev *sc)
640 spin_lock_irqsave(&ub_lock, flags);
642 if (sc->openc == 0 && atomic_read(&sc->poison)) {
643 spin_unlock_irqrestore(&ub_lock, flags);
646 spin_unlock_irqrestore(&ub_lock, flags);
651 * Final cleanup and deallocation.
653 static void ub_cleanup(struct ub_dev *sc)
659 while (!list_empty(&sc->luns)) {
661 lun = list_entry(p, struct ub_lun, link);
664 /* I don't think queue can be NULL. But... Stolen from sx8.c */
665 if ((q = lun->disk->queue) != NULL)
666 blk_cleanup_queue(q);
668 * If we zero disk->private_data BEFORE put_disk, we have
669 * to check for NULL all over the place in open, release,
670 * check_media and revalidate, because the block level
671 * semaphore is well inside the put_disk.
672 * But we cannot zero after the call, because *disk is gone.
673 * The sd.c is blatantly racy in this area.
675 /* disk->private_data = NULL; */
687 * The "command allocator".
689 static struct ub_scsi_cmd *ub_get_cmd(struct ub_lun *lun)
691 struct ub_scsi_cmd *ret;
700 static void ub_put_cmd(struct ub_lun *lun, struct ub_scsi_cmd *cmd)
702 if (cmd != &lun->cmdv[0]) {
703 printk(KERN_WARNING "%s: releasing a foreign cmd %p\n",
708 printk(KERN_WARNING "%s: releasing a free cmd\n", lun->name);
717 static void ub_cmdq_add(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
719 struct ub_scsi_cmd_queue *t = &sc->cmd_queue;
721 if (t->qlen++ == 0) {
729 if (t->qlen > t->qmax)
733 static void ub_cmdq_insert(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
735 struct ub_scsi_cmd_queue *t = &sc->cmd_queue;
737 if (t->qlen++ == 0) {
745 if (t->qlen > t->qmax)
749 static struct ub_scsi_cmd *ub_cmdq_pop(struct ub_dev *sc)
751 struct ub_scsi_cmd_queue *t = &sc->cmd_queue;
752 struct ub_scsi_cmd *cmd;
764 #define ub_cmdq_peek(sc) ((sc)->cmd_queue.head)
767 * The request function is our main entry point
770 static void ub_bd_rq_fn(request_queue_t *q)
772 struct ub_lun *lun = q->queuedata;
775 while ((rq = elv_next_request(q)) != NULL) {
776 if (ub_bd_rq_fn_1(lun, rq) != 0) {
783 static int ub_bd_rq_fn_1(struct ub_lun *lun, struct request *rq)
785 struct ub_dev *sc = lun->udev;
786 struct ub_scsi_cmd *cmd;
789 if (atomic_read(&sc->poison) || lun->changed) {
790 blkdev_dequeue_request(rq);
795 if (lun->urq.rq != NULL)
797 if ((cmd = ub_get_cmd(lun)) == NULL)
799 memset(cmd, 0, sizeof(struct ub_scsi_cmd));
801 blkdev_dequeue_request(rq);
802 if (blk_pc_request(rq)) {
803 rc = ub_cmd_build_packet(sc, lun, cmd, rq);
805 rc = ub_cmd_build_block(sc, lun, cmd, rq);
808 ub_put_cmd(lun, cmd);
812 cmd->state = UB_CMDST_INIT;
814 cmd->done = ub_rw_cmd_done;
815 cmd->back = &lun->urq;
817 cmd->tag = sc->tagcnt++;
818 if (ub_submit_scsi(sc, cmd) != 0) {
819 ub_put_cmd(lun, cmd);
827 static int ub_cmd_build_block(struct ub_dev *sc, struct ub_lun *lun,
828 struct ub_scsi_cmd *cmd, struct request *rq)
830 struct ub_request *urq;
835 memset(urq, 0, sizeof(struct ub_request));
837 if (rq_data_dir(rq) == WRITE)
838 ub_dir = UB_DIR_WRITE;
840 ub_dir = UB_DIR_READ;
843 * get scatterlist from block layer
845 n_elem = blk_rq_map_sg(lun->disk->queue, rq, &urq->sgv[0]);
847 printk(KERN_INFO "%s: failed request map (%d)\n",
848 sc->name, n_elem); /* P3 */
849 return -1; /* request with no s/g entries? */
851 if (n_elem > UB_MAX_REQ_SG) { /* Paranoia */
852 printk(KERN_WARNING "%s: request with %d segments\n",
857 sc->sg_stat[n_elem]++;
862 * The call to blk_queue_hardsect_size() guarantees that request
863 * is aligned, but it is given in terms of 512 byte units, always.
865 urq->current_block = rq->sector >> lun->capacity.bshift;
866 // nblks = rq->nr_sectors >> lun->capacity.bshift;
872 ub_scsi_build_block(lun, cmd, urq);
876 static void ub_scsi_build_block(struct ub_lun *lun,
877 struct ub_scsi_cmd *cmd, struct ub_request *urq)
879 struct scatterlist *sg;
880 unsigned int block, nblks;
882 sg = &urq->sgv[urq->current_sg];
884 block = urq->current_block;
885 nblks = sg->length >> (lun->capacity.bshift + 9);
887 cmd->cdb[0] = (urq->dir == UB_DIR_READ)? READ_10: WRITE_10;
888 /* 10-byte uses 4 bytes of LBA: 2147483648KB, 2097152MB, 2048GB */
889 cmd->cdb[2] = block >> 24;
890 cmd->cdb[3] = block >> 16;
891 cmd->cdb[4] = block >> 8;
893 cmd->cdb[7] = nblks >> 8;
898 cmd->data = page_address(sg->page) + sg->offset;
899 cmd->len = sg->length;
902 static int ub_cmd_build_packet(struct ub_dev *sc, struct ub_lun *lun,
903 struct ub_scsi_cmd *cmd, struct request *rq)
905 struct ub_request *urq;
908 memset(urq, 0, sizeof(struct ub_request));
912 if (rq->data_len != 0 && rq->data == NULL) {
913 static int do_print = 1;
915 printk(KERN_WARNING "%s: unmapped packet request"
916 " flags 0x%lx length %d\n",
917 sc->name, rq->flags, rq->data_len);
923 memcpy(&cmd->cdb, rq->cmd, rq->cmd_len);
924 cmd->cdb_len = rq->cmd_len;
926 if (rq->data_len == 0) {
927 cmd->dir = UB_DIR_NONE;
929 if (rq_data_dir(rq) == WRITE)
930 cmd->dir = UB_DIR_WRITE;
932 cmd->dir = UB_DIR_READ;
934 cmd->data = rq->data;
935 cmd->len = rq->data_len;
940 static void ub_rw_cmd_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
942 struct ub_lun *lun = cmd->lun;
943 struct ub_request *urq = cmd->back;
949 if (blk_pc_request(rq)) {
950 /* UB_SENSE_SIZE is smaller than SCSI_SENSE_BUFFERSIZE */
951 memcpy(rq->sense, sc->top_sense, UB_SENSE_SIZE);
952 rq->sense_len = UB_SENSE_SIZE;
960 if (cmd->error == 0 && urq->current_sg+1 < urq->nsg) {
961 if (ub_request_advance(sc, lun, urq, cmd) == 0) {
962 /* Stay on target... */
970 ub_put_cmd(lun, cmd);
971 ub_end_rq(rq, uptodate);
972 blk_start_queue(lun->disk->queue);
975 static void ub_end_rq(struct request *rq, int uptodate)
979 rc = end_that_request_first(rq, uptodate, rq->hard_nr_sectors);
981 end_that_request_last(rq);
984 static int ub_request_advance(struct ub_dev *sc, struct ub_lun *lun,
985 struct ub_request *urq, struct ub_scsi_cmd *cmd)
987 struct scatterlist *sg;
990 /* XXX This is temporary, until we sort out S/G in packet requests. */
991 if (blk_pc_request(urq->rq)) {
993 "2-segment packet request completed\n"); /* P3 */
997 sg = &urq->sgv[urq->current_sg];
998 nblks = sg->length >> (lun->capacity.bshift + 9);
999 urq->current_block += nblks;
1003 memset(cmd, 0, sizeof(struct ub_scsi_cmd));
1004 ub_scsi_build_block(lun, cmd, urq);
1005 cmd->state = UB_CMDST_INIT;
1007 cmd->done = ub_rw_cmd_done;
1008 cmd->back = &lun->urq;
1010 cmd->tag = sc->tagcnt++;
1011 if (ub_submit_scsi(sc, cmd) != 0) {
1019 * Submit a regular SCSI operation (not an auto-sense).
1021 * The Iron Law of Good Submit Routine is:
1022 * Zero return - callback is done, Nonzero return - callback is not done.
1025 * Host is assumed locked.
1027 * XXX We only support Bulk for the moment.
1029 static int ub_submit_scsi(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1032 if (cmd->state != UB_CMDST_INIT ||
1033 (cmd->dir != UB_DIR_NONE && cmd->len == 0)) {
1037 ub_cmdq_add(sc, cmd);
1039 * We can call ub_scsi_dispatch(sc) right away here, but it's a little
1040 * safer to jump to a tasklet, in case upper layers do something silly.
1042 tasklet_schedule(&sc->tasklet);
1047 * Submit the first URB for the queued command.
1048 * This function does not deal with queueing in any way.
1050 static int ub_scsi_cmd_start(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1052 struct bulk_cb_wrap *bcb;
1055 bcb = &sc->work_bcb;
1058 * ``If the allocation length is eighteen or greater, and a device
1059 * server returns less than eithteen bytes of data, the application
1060 * client should assume that the bytes not transferred would have been
1061 * zeroes had the device server returned those bytes.''
1063 * We zero sense for all commands so that when a packet request
1064 * fails it does not return a stale sense.
1066 memset(&sc->top_sense, 0, UB_SENSE_SIZE);
1068 /* set up the command wrapper */
1069 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
1070 bcb->Tag = cmd->tag; /* Endianness is not important */
1071 bcb->DataTransferLength = cpu_to_le32(cmd->len);
1072 bcb->Flags = (cmd->dir == UB_DIR_READ) ? 0x80 : 0;
1073 bcb->Lun = (cmd->lun != NULL) ? cmd->lun->num : 0;
1074 bcb->Length = cmd->cdb_len;
1076 /* copy the command payload */
1077 memcpy(bcb->CDB, cmd->cdb, UB_MAX_CDB_SIZE);
1079 UB_INIT_COMPLETION(sc->work_done);
1081 sc->last_pipe = sc->send_bulk_pipe;
1082 usb_fill_bulk_urb(&sc->work_urb, sc->dev, sc->send_bulk_pipe,
1083 bcb, US_BULK_CB_WRAP_LEN, ub_urb_complete, sc);
1084 sc->work_urb.transfer_flags = URB_ASYNC_UNLINK;
1086 /* Fill what we shouldn't be filling, because usb-storage did so. */
1087 sc->work_urb.actual_length = 0;
1088 sc->work_urb.error_count = 0;
1089 sc->work_urb.status = 0;
1091 if ((rc = usb_submit_urb(&sc->work_urb, GFP_ATOMIC)) != 0) {
1092 /* XXX Clear stalls */
1093 printk("ub: cmd #%d start failed (%d)\n", cmd->tag, rc); /* P3 */
1094 ub_complete(&sc->work_done);
1098 sc->work_timer.expires = jiffies + UB_URB_TIMEOUT;
1099 add_timer(&sc->work_timer);
1101 cmd->state = UB_CMDST_CMD;
1102 ub_cmdtr_state(sc, cmd);
1109 static void ub_urb_timeout(unsigned long arg)
1111 struct ub_dev *sc = (struct ub_dev *) arg;
1112 unsigned long flags;
1114 spin_lock_irqsave(&sc->lock, flags);
1115 usb_unlink_urb(&sc->work_urb);
1116 spin_unlock_irqrestore(&sc->lock, flags);
1120 * Completion routine for the work URB.
1122 * This can be called directly from usb_submit_urb (while we have
1123 * the sc->lock taken) and from an interrupt (while we do NOT have
1124 * the sc->lock taken). Therefore, bounce this off to a tasklet.
1126 static void ub_urb_complete(struct urb *urb, struct pt_regs *pt)
1128 struct ub_dev *sc = urb->context;
1130 ub_complete(&sc->work_done);
1131 tasklet_schedule(&sc->tasklet);
1134 static void ub_scsi_action(unsigned long _dev)
1136 struct ub_dev *sc = (struct ub_dev *) _dev;
1137 unsigned long flags;
1139 spin_lock_irqsave(&sc->lock, flags);
1140 del_timer(&sc->work_timer);
1141 ub_scsi_dispatch(sc);
1142 spin_unlock_irqrestore(&sc->lock, flags);
1145 static void ub_scsi_dispatch(struct ub_dev *sc)
1147 struct ub_scsi_cmd *cmd;
1150 while ((cmd = ub_cmdq_peek(sc)) != NULL) {
1151 if (cmd->state == UB_CMDST_DONE) {
1153 (*cmd->done)(sc, cmd);
1154 } else if (cmd->state == UB_CMDST_INIT) {
1155 ub_cmdtr_new(sc, cmd);
1156 if ((rc = ub_scsi_cmd_start(sc, cmd)) == 0)
1159 cmd->state = UB_CMDST_DONE;
1160 ub_cmdtr_state(sc, cmd);
1162 if (!ub_is_completed(&sc->work_done))
1164 ub_scsi_urb_compl(sc, cmd);
1169 static void ub_scsi_urb_compl(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1171 struct urb *urb = &sc->work_urb;
1172 struct bulk_cs_wrap *bcs;
1176 if (atomic_read(&sc->poison)) {
1177 /* A little too simplistic, I feel... */
1181 if (cmd->state == UB_CMDST_CLEAR) {
1182 if (urb->status == -EPIPE) {
1184 * STALL while clearning STALL.
1185 * The control pipe clears itself - nothing to do.
1186 * XXX Might try to reset the device here and retry.
1188 printk(KERN_NOTICE "%s: stall on control pipe\n",
1194 * We ignore the result for the halt clear.
1197 /* reset the endpoint toggle */
1198 usb_settoggle(sc->dev, usb_pipeendpoint(sc->last_pipe),
1199 usb_pipeout(sc->last_pipe), 0);
1201 ub_state_sense(sc, cmd);
1203 } else if (cmd->state == UB_CMDST_CLR2STS) {
1204 if (urb->status == -EPIPE) {
1206 * STALL while clearning STALL.
1207 * The control pipe clears itself - nothing to do.
1208 * XXX Might try to reset the device here and retry.
1210 printk(KERN_NOTICE "%s: stall on control pipe\n",
1216 * We ignore the result for the halt clear.
1219 /* reset the endpoint toggle */
1220 usb_settoggle(sc->dev, usb_pipeendpoint(sc->last_pipe),
1221 usb_pipeout(sc->last_pipe), 0);
1223 ub_state_stat(sc, cmd);
1225 } else if (cmd->state == UB_CMDST_CLRRS) {
1226 if (urb->status == -EPIPE) {
1228 * STALL while clearning STALL.
1229 * The control pipe clears itself - nothing to do.
1230 * XXX Might try to reset the device here and retry.
1232 printk(KERN_NOTICE "%s: stall on control pipe\n",
1238 * We ignore the result for the halt clear.
1241 /* reset the endpoint toggle */
1242 usb_settoggle(sc->dev, usb_pipeendpoint(sc->last_pipe),
1243 usb_pipeout(sc->last_pipe), 0);
1245 ub_state_stat_counted(sc, cmd);
1247 } else if (cmd->state == UB_CMDST_CMD) {
1248 if (urb->status == -EPIPE) {
1249 rc = ub_submit_clear_stall(sc, cmd, sc->last_pipe);
1251 printk(KERN_NOTICE "%s: "
1252 "unable to submit clear (%d)\n",
1255 * This is typically ENOMEM or some other such shit.
1256 * Retrying is pointless. Just do Bad End on it...
1260 cmd->state = UB_CMDST_CLEAR;
1261 ub_cmdtr_state(sc, cmd);
1264 if (urb->status != 0) {
1265 printk("ub: cmd #%d cmd status (%d)\n", cmd->tag, urb->status); /* P3 */
1268 if (urb->actual_length != US_BULK_CB_WRAP_LEN) {
1269 printk("ub: cmd #%d xferred %d\n", cmd->tag, urb->actual_length); /* P3 */
1270 /* XXX Must do reset here to unconfuse the device */
1274 if (cmd->dir == UB_DIR_NONE) {
1275 ub_state_stat(sc, cmd);
1279 UB_INIT_COMPLETION(sc->work_done);
1281 if (cmd->dir == UB_DIR_READ)
1282 pipe = sc->recv_bulk_pipe;
1284 pipe = sc->send_bulk_pipe;
1285 sc->last_pipe = pipe;
1286 usb_fill_bulk_urb(&sc->work_urb, sc->dev, pipe,
1287 cmd->data, cmd->len, ub_urb_complete, sc);
1288 sc->work_urb.transfer_flags = URB_ASYNC_UNLINK;
1289 sc->work_urb.actual_length = 0;
1290 sc->work_urb.error_count = 0;
1291 sc->work_urb.status = 0;
1293 if ((rc = usb_submit_urb(&sc->work_urb, GFP_ATOMIC)) != 0) {
1294 /* XXX Clear stalls */
1295 printk("ub: data #%d submit failed (%d)\n", cmd->tag, rc); /* P3 */
1296 ub_complete(&sc->work_done);
1297 ub_state_done(sc, cmd, rc);
1301 sc->work_timer.expires = jiffies + UB_DATA_TIMEOUT;
1302 add_timer(&sc->work_timer);
1304 cmd->state = UB_CMDST_DATA;
1305 ub_cmdtr_state(sc, cmd);
1307 } else if (cmd->state == UB_CMDST_DATA) {
1308 if (urb->status == -EPIPE) {
1309 rc = ub_submit_clear_stall(sc, cmd, sc->last_pipe);
1311 printk(KERN_NOTICE "%s: "
1312 "unable to submit clear (%d)\n",
1315 * This is typically ENOMEM or some other such shit.
1316 * Retrying is pointless. Just do Bad End on it...
1320 cmd->state = UB_CMDST_CLR2STS;
1321 ub_cmdtr_state(sc, cmd);
1324 if (urb->status == -EOVERFLOW) {
1326 * A babble? Failure, but we must transfer CSW now.
1328 cmd->error = -EOVERFLOW; /* A cheap trick... */
1330 if (urb->status != 0)
1334 cmd->act_len = urb->actual_length;
1335 ub_cmdtr_act_len(sc, cmd);
1337 ub_state_stat(sc, cmd);
1339 } else if (cmd->state == UB_CMDST_STAT) {
1340 if (urb->status == -EPIPE) {
1341 rc = ub_submit_clear_stall(sc, cmd, sc->last_pipe);
1343 printk(KERN_NOTICE "%s: "
1344 "unable to submit clear (%d)\n",
1347 * This is typically ENOMEM or some other such shit.
1348 * Retrying is pointless. Just do Bad End on it...
1354 * Having a stall when getting CSW is an error, so
1355 * make sure uppper levels are not oblivious to it.
1357 cmd->error = -EIO; /* A cheap trick... */
1359 cmd->state = UB_CMDST_CLRRS;
1360 ub_cmdtr_state(sc, cmd);
1363 if (urb->status == -EOVERFLOW) {
1365 * XXX We are screwed here. Retrying is pointless,
1366 * because the pipelined data will not get in until
1367 * we read with a big enough buffer. We must reset XXX.
1371 if (urb->status != 0)
1374 if (urb->actual_length == 0) {
1375 ub_state_stat_counted(sc, cmd);
1380 * Check the returned Bulk protocol status.
1381 * The status block has to be validated first.
1384 bcs = &sc->work_bcs;
1386 if (sc->signature == cpu_to_le32(0)) {
1388 * This is the first reply, so do not perform the check.
1389 * Instead, remember the signature the device uses
1390 * for future checks. But do not allow a nul.
1392 sc->signature = bcs->Signature;
1393 if (sc->signature == cpu_to_le32(0)) {
1394 ub_state_stat_counted(sc, cmd);
1398 if (bcs->Signature != sc->signature) {
1399 ub_state_stat_counted(sc, cmd);
1404 if (bcs->Tag != cmd->tag) {
1406 * This usually happens when we disagree with the
1407 * device's microcode about something. For instance,
1408 * a few of them throw this after timeouts. They buffer
1409 * commands and reply at commands we timed out before.
1410 * Without flushing these replies we loop forever.
1412 ub_state_stat_counted(sc, cmd);
1416 rc = le32_to_cpu(bcs->Residue);
1417 if (rc != cmd->len - cmd->act_len) {
1419 * It is all right to transfer less, the caller has
1420 * to check. But it's not all right if the device
1421 * counts disagree with our counts.
1423 /* P3 */ printk("%s: resid %d len %d act %d\n",
1424 sc->name, rc, cmd->len, cmd->act_len);
1428 switch (bcs->Status) {
1429 case US_BULK_STAT_OK:
1431 case US_BULK_STAT_FAIL:
1432 ub_state_sense(sc, cmd);
1434 case US_BULK_STAT_PHASE:
1435 /* XXX We must reset the transport here */
1436 /* P3 */ printk("%s: status PHASE\n", sc->name);
1439 printk(KERN_INFO "%s: unknown CSW status 0x%x\n",
1440 sc->name, bcs->Status);
1444 /* Not zeroing error to preserve a babble indicator */
1445 if (cmd->error != 0) {
1446 ub_state_sense(sc, cmd);
1449 cmd->state = UB_CMDST_DONE;
1450 ub_cmdtr_state(sc, cmd);
1452 (*cmd->done)(sc, cmd);
1454 } else if (cmd->state == UB_CMDST_SENSE) {
1455 ub_state_done(sc, cmd, -EIO);
1458 printk(KERN_WARNING "%s: "
1459 "wrong command state %d\n",
1460 sc->name, cmd->state);
1465 Bad_End: /* Little Excel is dead */
1466 ub_state_done(sc, cmd, -EIO);
1470 * Factorization helper for the command state machine:
1471 * Finish the command.
1473 static void ub_state_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd, int rc)
1477 cmd->state = UB_CMDST_DONE;
1478 ub_cmdtr_state(sc, cmd);
1480 (*cmd->done)(sc, cmd);
1484 * Factorization helper for the command state machine:
1485 * Submit a CSW read.
1487 static int __ub_state_stat(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1491 UB_INIT_COMPLETION(sc->work_done);
1493 sc->last_pipe = sc->recv_bulk_pipe;
1494 usb_fill_bulk_urb(&sc->work_urb, sc->dev, sc->recv_bulk_pipe,
1495 &sc->work_bcs, US_BULK_CS_WRAP_LEN, ub_urb_complete, sc);
1496 sc->work_urb.transfer_flags = URB_ASYNC_UNLINK;
1497 sc->work_urb.actual_length = 0;
1498 sc->work_urb.error_count = 0;
1499 sc->work_urb.status = 0;
1501 if ((rc = usb_submit_urb(&sc->work_urb, GFP_ATOMIC)) != 0) {
1502 /* XXX Clear stalls */
1503 ub_complete(&sc->work_done);
1504 ub_state_done(sc, cmd, rc);
1508 sc->work_timer.expires = jiffies + UB_STAT_TIMEOUT;
1509 add_timer(&sc->work_timer);
1514 * Factorization helper for the command state machine:
1515 * Submit a CSW read and go to STAT state.
1517 static void ub_state_stat(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1520 if (__ub_state_stat(sc, cmd) != 0)
1523 cmd->stat_count = 0;
1524 cmd->state = UB_CMDST_STAT;
1525 ub_cmdtr_state(sc, cmd);
1529 * Factorization helper for the command state machine:
1530 * Submit a CSW read and go to STAT state with counter (along [C] path).
1532 static void ub_state_stat_counted(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1535 if (++cmd->stat_count >= 4) {
1536 ub_state_sense(sc, cmd);
1540 if (__ub_state_stat(sc, cmd) != 0)
1543 cmd->state = UB_CMDST_STAT;
1544 ub_cmdtr_state(sc, cmd);
1548 * Factorization helper for the command state machine:
1549 * Submit a REQUEST SENSE and go to SENSE state.
1551 static void ub_state_sense(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1553 struct ub_scsi_cmd *scmd;
1556 if (cmd->cdb[0] == REQUEST_SENSE) {
1561 scmd = &sc->top_rqs_cmd;
1562 scmd->cdb[0] = REQUEST_SENSE;
1563 scmd->cdb[4] = UB_SENSE_SIZE;
1565 scmd->dir = UB_DIR_READ;
1566 scmd->state = UB_CMDST_INIT;
1567 scmd->data = sc->top_sense;
1568 scmd->len = UB_SENSE_SIZE;
1569 scmd->lun = cmd->lun;
1570 scmd->done = ub_top_sense_done;
1573 scmd->tag = sc->tagcnt++;
1575 cmd->state = UB_CMDST_SENSE;
1576 ub_cmdtr_state(sc, cmd);
1578 ub_cmdq_insert(sc, scmd);
1582 ub_state_done(sc, cmd, rc);
1586 * A helper for the command's state machine:
1587 * Submit a stall clear.
1589 static int ub_submit_clear_stall(struct ub_dev *sc, struct ub_scsi_cmd *cmd,
1593 struct usb_ctrlrequest *cr;
1596 endp = usb_pipeendpoint(stalled_pipe);
1597 if (usb_pipein (stalled_pipe))
1601 cr->bRequestType = USB_RECIP_ENDPOINT;
1602 cr->bRequest = USB_REQ_CLEAR_FEATURE;
1603 cr->wValue = cpu_to_le16(USB_ENDPOINT_HALT);
1604 cr->wIndex = cpu_to_le16(endp);
1605 cr->wLength = cpu_to_le16(0);
1607 UB_INIT_COMPLETION(sc->work_done);
1609 usb_fill_control_urb(&sc->work_urb, sc->dev, sc->send_ctrl_pipe,
1610 (unsigned char*) cr, NULL, 0, ub_urb_complete, sc);
1611 sc->work_urb.transfer_flags = URB_ASYNC_UNLINK;
1612 sc->work_urb.actual_length = 0;
1613 sc->work_urb.error_count = 0;
1614 sc->work_urb.status = 0;
1616 if ((rc = usb_submit_urb(&sc->work_urb, GFP_ATOMIC)) != 0) {
1617 ub_complete(&sc->work_done);
1621 sc->work_timer.expires = jiffies + UB_CTRL_TIMEOUT;
1622 add_timer(&sc->work_timer);
1628 static void ub_top_sense_done(struct ub_dev *sc, struct ub_scsi_cmd *scmd)
1630 unsigned char *sense = scmd->data;
1631 struct ub_scsi_cmd *cmd;
1634 * Ignoring scmd->act_len, because the buffer was pre-zeroed.
1636 ub_cmdtr_sense(sc, scmd, sense);
1639 * Find the command which triggered the unit attention or a check,
1640 * save the sense into it, and advance its state machine.
1642 if ((cmd = ub_cmdq_peek(sc)) == NULL) {
1643 printk(KERN_WARNING "%s: sense done while idle\n", sc->name);
1646 if (cmd != scmd->back) {
1647 printk(KERN_WARNING "%s: "
1648 "sense done for wrong command 0x%x\n",
1649 sc->name, cmd->tag);
1652 if (cmd->state != UB_CMDST_SENSE) {
1653 printk(KERN_WARNING "%s: "
1654 "sense done with bad cmd state %d\n",
1655 sc->name, cmd->state);
1659 cmd->key = sense[2] & 0x0F;
1660 cmd->asc = sense[12];
1661 cmd->ascq = sense[13];
1663 ub_scsi_urb_compl(sc, cmd);
1667 * This is called from a process context.
1669 static void ub_revalidate(struct ub_dev *sc, struct ub_lun *lun)
1672 lun->readonly = 0; /* XXX Query this from the device */
1674 lun->capacity.nsec = 0;
1675 lun->capacity.bsize = 512;
1676 lun->capacity.bshift = 0;
1678 if (ub_sync_tur(sc, lun) != 0)
1679 return; /* Not ready */
1682 if (ub_sync_read_cap(sc, lun, &lun->capacity) != 0) {
1684 * The retry here means something is wrong, either with the
1685 * device, with the transport, or with our code.
1686 * We keep this because sd.c has retries for capacity.
1688 if (ub_sync_read_cap(sc, lun, &lun->capacity) != 0) {
1689 lun->capacity.nsec = 0;
1690 lun->capacity.bsize = 512;
1691 lun->capacity.bshift = 0;
1698 * This is mostly needed to keep refcounting, but also to support
1699 * media checks on removable media drives.
1701 static int ub_bd_open(struct inode *inode, struct file *filp)
1703 struct gendisk *disk = inode->i_bdev->bd_disk;
1706 unsigned long flags;
1709 if ((lun = disk->private_data) == NULL)
1713 spin_lock_irqsave(&ub_lock, flags);
1714 if (atomic_read(&sc->poison)) {
1715 spin_unlock_irqrestore(&ub_lock, flags);
1719 spin_unlock_irqrestore(&ub_lock, flags);
1722 * This is a workaround for a specific problem in our block layer.
1723 * In 2.6.9, register_disk duplicates the code from rescan_partitions.
1724 * However, if we do add_disk with a device which persistently reports
1725 * a changed media, add_disk calls register_disk, which does do_open,
1726 * which will call rescan_paritions for changed media. After that,
1727 * register_disk attempts to do it all again and causes double kobject
1728 * registration and a eventually an oops on module removal.
1730 * The bottom line is, Al Viro says that we should not allow
1731 * bdev->bd_invalidated to be set when doing add_disk no matter what.
1733 if (lun->first_open) {
1734 lun->first_open = 0;
1741 if (lun->removable || lun->readonly)
1742 check_disk_change(inode->i_bdev);
1745 * The sd.c considers ->media_present and ->changed not equivalent,
1746 * under some pretty murky conditions (a failure of READ CAPACITY).
1747 * We may need it one day.
1749 if (lun->removable && lun->changed && !(filp->f_flags & O_NDELAY)) {
1754 if (lun->readonly && (filp->f_mode & FMODE_WRITE)) {
1768 static int ub_bd_release(struct inode *inode, struct file *filp)
1770 struct gendisk *disk = inode->i_bdev->bd_disk;
1771 struct ub_lun *lun = disk->private_data;
1772 struct ub_dev *sc = lun->udev;
1779 * The ioctl interface.
1781 static int ub_bd_ioctl(struct inode *inode, struct file *filp,
1782 unsigned int cmd, unsigned long arg)
1784 struct gendisk *disk = inode->i_bdev->bd_disk;
1785 void __user *usermem = (void __user *) arg;
1787 return scsi_cmd_ioctl(filp, disk, cmd, usermem);
1791 * This is called once a new disk was seen by the block layer or by ub_probe().
1792 * The main onjective here is to discover the features of the media such as
1793 * the capacity, read-only status, etc. USB storage generally does not
1794 * need to be spun up, but if we needed it, this would be the place.
1796 * This call can sleep.
1798 * The return code is not used.
1800 static int ub_bd_revalidate(struct gendisk *disk)
1802 struct ub_lun *lun = disk->private_data;
1804 ub_revalidate(lun->udev, lun);
1806 /* XXX Support sector size switching like in sr.c */
1807 blk_queue_hardsect_size(disk->queue, lun->capacity.bsize);
1808 set_capacity(disk, lun->capacity.nsec);
1809 // set_disk_ro(sdkp->disk, lun->readonly);
1815 * The check is called by the block layer to verify if the media
1816 * is still available. It is supposed to be harmless, lightweight and
1817 * non-intrusive in case the media was not changed.
1819 * This call can sleep.
1821 * The return code is bool!
1823 static int ub_bd_media_changed(struct gendisk *disk)
1825 struct ub_lun *lun = disk->private_data;
1827 if (!lun->removable)
1831 * We clean checks always after every command, so this is not
1832 * as dangerous as it looks. If the TEST_UNIT_READY fails here,
1833 * the device is actually not ready with operator or software
1834 * intervention required. One dangerous item might be a drive which
1835 * spins itself down, and come the time to write dirty pages, this
1836 * will fail, then block layer discards the data. Since we never
1837 * spin drives up, such devices simply cannot be used with ub anyway.
1839 if (ub_sync_tur(lun->udev, lun) != 0) {
1844 return lun->changed;
1847 static struct block_device_operations ub_bd_fops = {
1848 .owner = THIS_MODULE,
1850 .release = ub_bd_release,
1851 .ioctl = ub_bd_ioctl,
1852 .media_changed = ub_bd_media_changed,
1853 .revalidate_disk = ub_bd_revalidate,
1857 * Common ->done routine for commands executed synchronously.
1859 static void ub_probe_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1861 struct completion *cop = cmd->back;
1866 * Test if the device has a check condition on it, synchronously.
1868 static int ub_sync_tur(struct ub_dev *sc, struct ub_lun *lun)
1870 struct ub_scsi_cmd *cmd;
1871 enum { ALLOC_SIZE = sizeof(struct ub_scsi_cmd) };
1872 unsigned long flags;
1873 struct completion compl;
1876 init_completion(&compl);
1879 if ((cmd = kmalloc(ALLOC_SIZE, GFP_KERNEL)) == NULL)
1881 memset(cmd, 0, ALLOC_SIZE);
1883 cmd->cdb[0] = TEST_UNIT_READY;
1885 cmd->dir = UB_DIR_NONE;
1886 cmd->state = UB_CMDST_INIT;
1887 cmd->lun = lun; /* This may be NULL, but that's ok */
1888 cmd->done = ub_probe_done;
1891 spin_lock_irqsave(&sc->lock, flags);
1892 cmd->tag = sc->tagcnt++;
1894 rc = ub_submit_scsi(sc, cmd);
1895 spin_unlock_irqrestore(&sc->lock, flags);
1898 printk("ub: testing ready: submit error (%d)\n", rc); /* P3 */
1902 wait_for_completion(&compl);
1906 if (rc == -EIO && cmd->key != 0) /* Retries for benh's key */
1916 * Read the SCSI capacity synchronously (for probing).
1918 static int ub_sync_read_cap(struct ub_dev *sc, struct ub_lun *lun,
1919 struct ub_capacity *ret)
1921 struct ub_scsi_cmd *cmd;
1923 enum { ALLOC_SIZE = sizeof(struct ub_scsi_cmd) + 8 };
1924 unsigned long flags;
1925 unsigned int bsize, shift;
1927 struct completion compl;
1930 init_completion(&compl);
1933 if ((cmd = kmalloc(ALLOC_SIZE, GFP_KERNEL)) == NULL)
1935 memset(cmd, 0, ALLOC_SIZE);
1936 p = (char *)cmd + sizeof(struct ub_scsi_cmd);
1940 cmd->dir = UB_DIR_READ;
1941 cmd->state = UB_CMDST_INIT;
1945 cmd->done = ub_probe_done;
1948 spin_lock_irqsave(&sc->lock, flags);
1949 cmd->tag = sc->tagcnt++;
1951 rc = ub_submit_scsi(sc, cmd);
1952 spin_unlock_irqrestore(&sc->lock, flags);
1955 printk("ub: reading capacity: submit error (%d)\n", rc); /* P3 */
1959 wait_for_completion(&compl);
1961 if (cmd->error != 0) {
1962 printk("ub: reading capacity: error %d\n", cmd->error); /* P3 */
1966 if (cmd->act_len != 8) {
1967 printk("ub: reading capacity: size %d\n", cmd->act_len); /* P3 */
1972 /* sd.c special-cases sector size of 0 to mean 512. Needed? Safe? */
1973 nsec = be32_to_cpu(*(__be32 *)p) + 1;
1974 bsize = be32_to_cpu(*(__be32 *)(p + 4));
1976 case 512: shift = 0; break;
1977 case 1024: shift = 1; break;
1978 case 2048: shift = 2; break;
1979 case 4096: shift = 3; break;
1981 printk("ub: Bad sector size %u\n", bsize); /* P3 */
1987 ret->bshift = shift;
1988 ret->nsec = nsec << shift;
2001 static void ub_probe_urb_complete(struct urb *urb, struct pt_regs *pt)
2003 struct completion *cop = urb->context;
2007 static void ub_probe_timeout(unsigned long arg)
2009 struct completion *cop = (struct completion *) arg;
2014 * Get number of LUNs by the way of Bulk GetMaxLUN command.
2016 static int ub_sync_getmaxlun(struct ub_dev *sc)
2018 int ifnum = sc->intf->cur_altsetting->desc.bInterfaceNumber;
2020 enum { ALLOC_SIZE = 1 };
2021 struct usb_ctrlrequest *cr;
2022 struct completion compl;
2023 struct timer_list timer;
2027 init_completion(&compl);
2030 if ((p = kmalloc(ALLOC_SIZE, GFP_KERNEL)) == NULL)
2035 cr->bRequestType = USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
2036 cr->bRequest = US_BULK_GET_MAX_LUN;
2037 cr->wValue = cpu_to_le16(0);
2038 cr->wIndex = cpu_to_le16(ifnum);
2039 cr->wLength = cpu_to_le16(1);
2041 usb_fill_control_urb(&sc->work_urb, sc->dev, sc->recv_ctrl_pipe,
2042 (unsigned char*) cr, p, 1, ub_probe_urb_complete, &compl);
2043 sc->work_urb.transfer_flags = 0;
2044 sc->work_urb.actual_length = 0;
2045 sc->work_urb.error_count = 0;
2046 sc->work_urb.status = 0;
2048 if ((rc = usb_submit_urb(&sc->work_urb, GFP_KERNEL)) != 0) {
2050 printk("%s: Stall at GetMaxLUN, using 1 LUN\n",
2054 "%s: Unable to submit GetMaxLUN (%d)\n",
2061 timer.function = ub_probe_timeout;
2062 timer.data = (unsigned long) &compl;
2063 timer.expires = jiffies + UB_CTRL_TIMEOUT;
2066 wait_for_completion(&compl);
2068 del_timer_sync(&timer);
2069 usb_kill_urb(&sc->work_urb);
2071 if (sc->work_urb.actual_length != 1) {
2072 printk("%s: GetMaxLUN returned %d bytes\n", sc->name,
2073 sc->work_urb.actual_length); /* P3 */
2076 if ((nluns = *p) == 55) {
2079 /* GetMaxLUN returns the maximum LUN number */
2081 if (nluns > UB_MAX_LUNS)
2082 nluns = UB_MAX_LUNS;
2084 printk("%s: GetMaxLUN returned %d, using %d LUNs\n", sc->name,
2085 *p, nluns); /* P3 */
2098 * Clear initial stalls.
2100 static int ub_probe_clear_stall(struct ub_dev *sc, int stalled_pipe)
2103 struct usb_ctrlrequest *cr;
2104 struct completion compl;
2105 struct timer_list timer;
2108 init_completion(&compl);
2110 endp = usb_pipeendpoint(stalled_pipe);
2111 if (usb_pipein (stalled_pipe))
2115 cr->bRequestType = USB_RECIP_ENDPOINT;
2116 cr->bRequest = USB_REQ_CLEAR_FEATURE;
2117 cr->wValue = cpu_to_le16(USB_ENDPOINT_HALT);
2118 cr->wIndex = cpu_to_le16(endp);
2119 cr->wLength = cpu_to_le16(0);
2121 usb_fill_control_urb(&sc->work_urb, sc->dev, sc->send_ctrl_pipe,
2122 (unsigned char*) cr, NULL, 0, ub_probe_urb_complete, &compl);
2123 sc->work_urb.transfer_flags = 0;
2124 sc->work_urb.actual_length = 0;
2125 sc->work_urb.error_count = 0;
2126 sc->work_urb.status = 0;
2128 if ((rc = usb_submit_urb(&sc->work_urb, GFP_KERNEL)) != 0) {
2130 "%s: Unable to submit a probe clear (%d)\n", sc->name, rc);
2135 timer.function = ub_probe_timeout;
2136 timer.data = (unsigned long) &compl;
2137 timer.expires = jiffies + UB_CTRL_TIMEOUT;
2140 wait_for_completion(&compl);
2142 del_timer_sync(&timer);
2143 usb_kill_urb(&sc->work_urb);
2145 /* reset the endpoint toggle */
2146 usb_settoggle(sc->dev, endp, usb_pipeout(sc->last_pipe), 0);
2152 * Get the pipe settings.
2154 static int ub_get_pipes(struct ub_dev *sc, struct usb_device *dev,
2155 struct usb_interface *intf)
2157 struct usb_host_interface *altsetting = intf->cur_altsetting;
2158 struct usb_endpoint_descriptor *ep_in = NULL;
2159 struct usb_endpoint_descriptor *ep_out = NULL;
2160 struct usb_endpoint_descriptor *ep;
2164 * Find the endpoints we need.
2165 * We are expecting a minimum of 2 endpoints - in and out (bulk).
2166 * We will ignore any others.
2168 for (i = 0; i < altsetting->desc.bNumEndpoints; i++) {
2169 ep = &altsetting->endpoint[i].desc;
2171 /* Is it a BULK endpoint? */
2172 if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
2173 == USB_ENDPOINT_XFER_BULK) {
2174 /* BULK in or out? */
2175 if (ep->bEndpointAddress & USB_DIR_IN)
2182 if (ep_in == NULL || ep_out == NULL) {
2183 printk(KERN_NOTICE "%s: failed endpoint check\n",
2188 /* Calculate and store the pipe values */
2189 sc->send_ctrl_pipe = usb_sndctrlpipe(dev, 0);
2190 sc->recv_ctrl_pipe = usb_rcvctrlpipe(dev, 0);
2191 sc->send_bulk_pipe = usb_sndbulkpipe(dev,
2192 ep_out->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
2193 sc->recv_bulk_pipe = usb_rcvbulkpipe(dev,
2194 ep_in->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
2200 * Probing is done in the process context, which allows us to cheat
2201 * and not to build a state machine for the discovery.
2203 static int ub_probe(struct usb_interface *intf,
2204 const struct usb_device_id *dev_id)
2212 if ((sc = kmalloc(sizeof(struct ub_dev), GFP_KERNEL)) == NULL)
2214 memset(sc, 0, sizeof(struct ub_dev));
2215 spin_lock_init(&sc->lock);
2216 INIT_LIST_HEAD(&sc->luns);
2217 usb_init_urb(&sc->work_urb);
2218 tasklet_init(&sc->tasklet, ub_scsi_action, (unsigned long)sc);
2219 atomic_set(&sc->poison, 0);
2221 init_timer(&sc->work_timer);
2222 sc->work_timer.data = (unsigned long) sc;
2223 sc->work_timer.function = ub_urb_timeout;
2225 ub_init_completion(&sc->work_done);
2226 sc->work_done.done = 1; /* A little yuk, but oh well... */
2228 sc->dev = interface_to_usbdev(intf);
2230 // sc->ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
2231 usb_set_intfdata(intf, sc);
2232 usb_get_dev(sc->dev);
2233 // usb_get_intf(sc->intf); /* Do we need this? */
2235 snprintf(sc->name, 12, DRV_NAME "(%d.%d)",
2236 sc->dev->bus->busnum, sc->dev->devnum);
2238 /* XXX Verify that we can handle the device (from descriptors) */
2240 ub_get_pipes(sc, sc->dev, intf);
2242 if (device_create_file(&sc->intf->dev, &dev_attr_diag) != 0)
2246 * At this point, all USB initialization is done, do upper layer.
2247 * We really hate halfway initialized structures, so from the
2248 * invariants perspective, this ub_dev is fully constructed at
2253 * This is needed to clear toggles. It is a problem only if we do
2254 * `rmmod ub && modprobe ub` without disconnects, but we like that.
2256 ub_probe_clear_stall(sc, sc->recv_bulk_pipe);
2257 ub_probe_clear_stall(sc, sc->send_bulk_pipe);
2260 * The way this is used by the startup code is a little specific.
2261 * A SCSI check causes a USB stall. Our common case code sees it
2262 * and clears the check, after which the device is ready for use.
2263 * But if a check was not present, any command other than
2264 * TEST_UNIT_READY ends with a lockup (including REQUEST_SENSE).
2266 * If we neglect to clear the SCSI check, the first real command fails
2267 * (which is the capacity readout). We clear that and retry, but why
2268 * causing spurious retries for no reason.
2270 * Revalidation may start with its own TEST_UNIT_READY, but that one
2271 * has to succeed, so we clear checks with an additional one here.
2272 * In any case it's not our business how revaliadation is implemented.
2274 for (i = 0; i < 3; i++) { /* Retries for benh's key */
2275 if ((rc = ub_sync_tur(sc, NULL)) <= 0) break;
2276 if (rc != 0x6) break;
2281 for (i = 0; i < 3; i++) {
2282 if ((rc = ub_sync_getmaxlun(sc)) < 0) {
2284 * Some devices (i.e. Iomega Zip100) need this --
2285 * apparently the bulk pipes get STALLed when the
2286 * GetMaxLUN request is processed.
2287 * XXX I have a ZIP-100, verify it does this.
2290 ub_probe_clear_stall(sc, sc->recv_bulk_pipe);
2291 ub_probe_clear_stall(sc, sc->send_bulk_pipe);
2302 for (i = 0; i < nluns; i++) {
2303 ub_probe_lun(sc, i);
2307 /* device_remove_file(&sc->intf->dev, &dev_attr_diag); */
2309 usb_set_intfdata(intf, NULL);
2310 // usb_put_intf(sc->intf);
2311 usb_put_dev(sc->dev);
2317 static int ub_probe_lun(struct ub_dev *sc, int lnum)
2321 struct gendisk *disk;
2325 if ((lun = kmalloc(sizeof(struct ub_lun), GFP_KERNEL)) == NULL)
2327 memset(lun, 0, sizeof(struct ub_lun));
2331 if ((lun->id = ub_id_get()) == -1)
2335 list_add(&lun->link, &sc->luns);
2337 snprintf(lun->name, 16, DRV_NAME "%c(%d.%d.%d)",
2338 lun->id + 'a', sc->dev->bus->busnum, sc->dev->devnum, lun->num);
2340 lun->removable = 1; /* XXX Query this from the device */
2341 lun->changed = 1; /* ub_revalidate clears only */
2342 lun->first_open = 1;
2343 ub_revalidate(sc, lun);
2346 if ((disk = alloc_disk(UB_MINORS_PER_MAJOR)) == NULL)
2350 sprintf(disk->disk_name, DRV_NAME "%c", lun->id + 'a');
2351 sprintf(disk->devfs_name, DEVFS_NAME "/%c", lun->id + 'a');
2352 disk->major = UB_MAJOR;
2353 disk->first_minor = lun->id * UB_MINORS_PER_MAJOR;
2354 disk->fops = &ub_bd_fops;
2355 disk->private_data = lun;
2356 disk->driverfs_dev = &sc->intf->dev; /* XXX Many to one ok? */
2359 if ((q = blk_init_queue(ub_bd_rq_fn, &sc->lock)) == NULL)
2364 blk_queue_bounce_limit(q, BLK_BOUNCE_HIGH);
2365 blk_queue_max_hw_segments(q, UB_MAX_REQ_SG);
2366 blk_queue_max_phys_segments(q, UB_MAX_REQ_SG);
2367 blk_queue_segment_boundary(q, 0xffffffff); /* Dubious. */
2368 blk_queue_max_sectors(q, UB_MAX_SECTORS);
2369 blk_queue_hardsect_size(q, lun->capacity.bsize);
2373 set_capacity(disk, lun->capacity.nsec);
2375 disk->flags |= GENHD_FL_REMOVABLE;
2384 list_del(&lun->link);
2392 static void ub_disconnect(struct usb_interface *intf)
2394 struct ub_dev *sc = usb_get_intfdata(intf);
2395 struct list_head *p;
2397 struct gendisk *disk;
2398 unsigned long flags;
2401 * Prevent ub_bd_release from pulling the rug from under us.
2402 * XXX This is starting to look like a kref.
2403 * XXX Why not to take this ref at probe time?
2405 spin_lock_irqsave(&ub_lock, flags);
2407 spin_unlock_irqrestore(&ub_lock, flags);
2410 * Fence stall clearnings, operations triggered by unlinkings and so on.
2411 * We do not attempt to unlink any URBs, because we do not trust the
2412 * unlink paths in HC drivers. Also, we get -84 upon disconnect anyway.
2414 atomic_set(&sc->poison, 1);
2417 * Blow away queued commands.
2419 * Actually, this never works, because before we get here
2420 * the HCD terminates outstanding URB(s). It causes our
2421 * SCSI command queue to advance, commands fail to submit,
2422 * and the whole queue drains. So, we just use this code to
2425 spin_lock_irqsave(&sc->lock, flags);
2427 struct ub_scsi_cmd *cmd;
2429 while ((cmd = ub_cmdq_pop(sc)) != NULL) {
2430 cmd->error = -ENOTCONN;
2431 cmd->state = UB_CMDST_DONE;
2432 ub_cmdtr_state(sc, cmd);
2434 (*cmd->done)(sc, cmd);
2438 printk(KERN_WARNING "%s: "
2439 "%d was queued after shutdown\n", sc->name, cnt);
2442 spin_unlock_irqrestore(&sc->lock, flags);
2445 * Unregister the upper layer.
2447 list_for_each (p, &sc->luns) {
2448 lun = list_entry(p, struct ub_lun, link);
2450 if (disk->flags & GENHD_FL_UP)
2453 * I wish I could do:
2454 * set_bit(QUEUE_FLAG_DEAD, &q->queue_flags);
2455 * As it is, we rely on our internal poisoning and let
2456 * the upper levels to spin furiously failing all the I/O.
2461 * Taking a lock on a structure which is about to be freed
2462 * is very nonsensual. Here it is largely a way to do a debug freeze,
2463 * and a bracket which shows where the nonsensual code segment ends.
2465 * Testing for -EINPROGRESS is always a bug, so we are bending
2466 * the rules a little.
2468 spin_lock_irqsave(&sc->lock, flags);
2469 if (sc->work_urb.status == -EINPROGRESS) { /* janitors: ignore */
2470 printk(KERN_WARNING "%s: "
2471 "URB is active after disconnect\n", sc->name);
2473 spin_unlock_irqrestore(&sc->lock, flags);
2476 * There is virtually no chance that other CPU runs times so long
2477 * after ub_urb_complete should have called del_timer, but only if HCD
2478 * didn't forget to deliver a callback on unlink.
2480 del_timer_sync(&sc->work_timer);
2483 * At this point there must be no commands coming from anyone
2484 * and no URBs left in transit.
2487 device_remove_file(&sc->intf->dev, &dev_attr_diag);
2488 usb_set_intfdata(intf, NULL);
2489 // usb_put_intf(sc->intf);
2491 usb_put_dev(sc->dev);
2497 static struct usb_driver ub_driver = {
2498 .owner = THIS_MODULE,
2501 .disconnect = ub_disconnect,
2502 .id_table = ub_usb_ids,
2505 static int __init ub_init(void)
2509 /* P3 */ printk("ub: sizeof ub_scsi_cmd %zu ub_dev %zu ub_lun %zu\n",
2510 sizeof(struct ub_scsi_cmd), sizeof(struct ub_dev), sizeof(struct ub_lun));
2512 if ((rc = register_blkdev(UB_MAJOR, DRV_NAME)) != 0)
2514 devfs_mk_dir(DEVFS_NAME);
2516 if ((rc = usb_register(&ub_driver)) != 0)
2522 devfs_remove(DEVFS_NAME);
2523 unregister_blkdev(UB_MAJOR, DRV_NAME);
2528 static void __exit ub_exit(void)
2530 usb_deregister(&ub_driver);
2532 devfs_remove(DEVFS_NAME);
2533 unregister_blkdev(UB_MAJOR, DRV_NAME);
2536 module_init(ub_init);
2537 module_exit(ub_exit);
2539 MODULE_LICENSE("GPL");