[PATCH] USB: fix ub issues
[linux-2.6/x86.git] / drivers / block / ub.c
blob19c5e59bcfa826966d433c2edd6f921592a7f40f
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 * -- Kill first_open (Al Viro fixed the block layer now)
12 * -- Do resets with usb_device_reset (needs a thread context, use khubd)
13 * -- set readonly flag for CDs, set removable flag for CF readers
14 * -- do inquiry and verify we got a disk and not a tape (for LUN mismatch)
15 * -- special case some senses, e.g. 3a/0 -> no media present, reduce retries
16 * -- verify the 13 conditions and do bulk resets
17 * -- kill last_pipe and simply do two-state clearing on both pipes
18 * -- verify protocol (bulk) from USB descriptors (maybe...)
19 * -- highmem and sg
20 * -- move top_sense and work_bcs into separate allocations (if they survive)
21 * for cache purists and esoteric architectures.
22 * -- prune comments, they are too volumnous
23 * -- Exterminate P3 printks
24 * -- Resove XXX's
25 * -- Redo "benh's retries", perhaps have spin-up code to handle them. V:D=?
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/usb.h>
30 #include <linux/blkdev.h>
31 #include <linux/devfs_fs_kernel.h>
32 #include <linux/timer.h>
33 #include <scsi/scsi.h>
35 #define DRV_NAME "ub"
36 #define DEVFS_NAME DRV_NAME
38 #define UB_MAJOR 180
41 * Definitions which have to be scattered once we understand the layout better.
44 /* Transport (despite PR in the name) */
45 #define US_PR_BULK 0x50 /* bulk only */
47 /* Protocol */
48 #define US_SC_SCSI 0x06 /* Transparent */
51 * This many LUNs per USB device.
52 * Every one of them takes a host, see UB_MAX_HOSTS.
54 #define UB_MAX_LUNS 9
59 #define UB_MINORS_PER_MAJOR 8
61 #define UB_MAX_CDB_SIZE 16 /* Corresponds to Bulk */
63 #define UB_SENSE_SIZE 18
68 /* command block wrapper */
69 struct bulk_cb_wrap {
70 __le32 Signature; /* contains 'USBC' */
71 u32 Tag; /* unique per command id */
72 __le32 DataTransferLength; /* size of data */
73 u8 Flags; /* direction in bit 0 */
74 u8 Lun; /* LUN */
75 u8 Length; /* of of the CDB */
76 u8 CDB[UB_MAX_CDB_SIZE]; /* max command */
79 #define US_BULK_CB_WRAP_LEN 31
80 #define US_BULK_CB_SIGN 0x43425355 /*spells out USBC */
81 #define US_BULK_FLAG_IN 1
82 #define US_BULK_FLAG_OUT 0
84 /* command status wrapper */
85 struct bulk_cs_wrap {
86 __le32 Signature; /* should = 'USBS' */
87 u32 Tag; /* same as original command */
88 __le32 Residue; /* amount not transferred */
89 u8 Status; /* see below */
92 #define US_BULK_CS_WRAP_LEN 13
93 #define US_BULK_CS_SIGN 0x53425355 /* spells out 'USBS' */
94 /* This is for Olympus Camedia digital cameras */
95 #define US_BULK_CS_OLYMPUS_SIGN 0x55425355 /* spells out 'USBU' */
96 #define US_BULK_STAT_OK 0
97 #define US_BULK_STAT_FAIL 1
98 #define US_BULK_STAT_PHASE 2
100 /* bulk-only class specific requests */
101 #define US_BULK_RESET_REQUEST 0xff
102 #define US_BULK_GET_MAX_LUN 0xfe
106 struct ub_dev;
108 #define UB_MAX_REQ_SG 1
109 #define UB_MAX_SECTORS 64
112 * A second is more than enough for a 32K transfer (UB_MAX_SECTORS)
113 * even if a webcam hogs the bus, but some devices need time to spin up.
115 #define UB_URB_TIMEOUT (HZ*2)
116 #define UB_DATA_TIMEOUT (HZ*5) /* ZIP does spin-ups in the data phase */
117 #define UB_STAT_TIMEOUT (HZ*5) /* Same spinups and eject for a dataless cmd. */
118 #define UB_CTRL_TIMEOUT (HZ/2) /* 500ms ought to be enough to clear a stall */
121 * An instance of a SCSI command in transit.
123 #define UB_DIR_NONE 0
124 #define UB_DIR_READ 1
125 #define UB_DIR_ILLEGAL2 2
126 #define UB_DIR_WRITE 3
128 #define UB_DIR_CHAR(c) (((c)==UB_DIR_WRITE)? 'w': \
129 (((c)==UB_DIR_READ)? 'r': 'n'))
131 enum ub_scsi_cmd_state {
132 UB_CMDST_INIT, /* Initial state */
133 UB_CMDST_CMD, /* Command submitted */
134 UB_CMDST_DATA, /* Data phase */
135 UB_CMDST_CLR2STS, /* Clearing before requesting status */
136 UB_CMDST_STAT, /* Status phase */
137 UB_CMDST_CLEAR, /* Clearing a stall (halt, actually) */
138 UB_CMDST_SENSE, /* Sending Request Sense */
139 UB_CMDST_DONE /* Final state */
142 static char *ub_scsi_cmd_stname[] = {
143 ". ",
144 "Cmd",
145 "dat",
146 "c2s",
147 "sts",
148 "clr",
149 "Sen",
150 "fin"
153 struct ub_scsi_cmd {
154 unsigned char cdb[UB_MAX_CDB_SIZE];
155 unsigned char cdb_len;
157 unsigned char dir; /* 0 - none, 1 - read, 3 - write. */
158 unsigned char trace_index;
159 enum ub_scsi_cmd_state state;
160 unsigned int tag;
161 struct ub_scsi_cmd *next;
163 int error; /* Return code - valid upon done */
164 unsigned int act_len; /* Return size */
165 unsigned char key, asc, ascq; /* May be valid if error==-EIO */
167 int stat_count; /* Retries getting status. */
170 * We do not support transfers from highmem pages
171 * because the underlying USB framework does not do what we need.
173 char *data; /* Requested buffer */
174 unsigned int len; /* Requested length */
175 // struct scatterlist sgv[UB_MAX_REQ_SG];
177 struct ub_lun *lun;
178 void (*done)(struct ub_dev *, struct ub_scsi_cmd *);
179 void *back;
184 struct ub_capacity {
185 unsigned long nsec; /* Linux size - 512 byte sectors */
186 unsigned int bsize; /* Linux hardsect_size */
187 unsigned int bshift; /* Shift between 512 and hard sects */
191 * The SCSI command tracing structure.
194 #define SCMD_ST_HIST_SZ 8
195 #define SCMD_TRACE_SZ 63 /* Less than 4KB of 61-byte lines */
197 struct ub_scsi_cmd_trace {
198 int hcur;
199 unsigned int tag;
200 unsigned int req_size, act_size;
201 unsigned char op;
202 unsigned char dir;
203 unsigned char key, asc, ascq;
204 char st_hst[SCMD_ST_HIST_SZ];
207 struct ub_scsi_trace {
208 int cur;
209 struct ub_scsi_cmd_trace vec[SCMD_TRACE_SZ];
213 * This is a direct take-off from linux/include/completion.h
214 * The difference is that I do not wait on this thing, just poll.
215 * When I want to wait (ub_probe), I just use the stock completion.
217 * Note that INIT_COMPLETION takes no lock. It is correct. But why
218 * in the bloody hell that thing takes struct instead of pointer to struct
219 * is quite beyond me. I just copied it from the stock completion.
221 struct ub_completion {
222 unsigned int done;
223 spinlock_t lock;
226 static inline void ub_init_completion(struct ub_completion *x)
228 x->done = 0;
229 spin_lock_init(&x->lock);
232 #define UB_INIT_COMPLETION(x) ((x).done = 0)
234 static void ub_complete(struct ub_completion *x)
236 unsigned long flags;
238 spin_lock_irqsave(&x->lock, flags);
239 x->done++;
240 spin_unlock_irqrestore(&x->lock, flags);
243 static int ub_is_completed(struct ub_completion *x)
245 unsigned long flags;
246 int ret;
248 spin_lock_irqsave(&x->lock, flags);
249 ret = x->done;
250 spin_unlock_irqrestore(&x->lock, flags);
251 return ret;
256 struct ub_scsi_cmd_queue {
257 int qlen, qmax;
258 struct ub_scsi_cmd *head, *tail;
262 * The block device instance (one per LUN).
264 struct ub_lun {
265 struct ub_dev *udev;
266 struct list_head link;
267 struct gendisk *disk;
268 int id; /* Host index */
269 int num; /* LUN number */
270 char name[16];
272 int changed; /* Media was changed */
273 int removable;
274 int readonly;
275 int first_open; /* Kludge. See ub_bd_open. */
277 /* Use Ingo's mempool if or when we have more than one command. */
279 * Currently we never need more than one command for the whole device.
280 * However, giving every LUN a command is a cheap and automatic way
281 * to enforce fairness between them.
283 int cmda[1];
284 struct ub_scsi_cmd cmdv[1];
286 struct ub_capacity capacity;
290 * The USB device instance.
292 struct ub_dev {
293 spinlock_t lock;
294 atomic_t poison; /* The USB device is disconnected */
295 int openc; /* protected by ub_lock! */
296 /* kref is too implicit for our taste */
297 unsigned int tagcnt;
298 char name[12];
299 struct usb_device *dev;
300 struct usb_interface *intf;
302 struct list_head luns;
304 unsigned int send_bulk_pipe; /* cached pipe values */
305 unsigned int recv_bulk_pipe;
306 unsigned int send_ctrl_pipe;
307 unsigned int recv_ctrl_pipe;
309 struct tasklet_struct tasklet;
311 struct ub_scsi_cmd_queue cmd_queue;
312 struct ub_scsi_cmd top_rqs_cmd; /* REQUEST SENSE */
313 unsigned char top_sense[UB_SENSE_SIZE];
315 struct ub_completion work_done;
316 struct urb work_urb;
317 struct timer_list work_timer;
318 int last_pipe; /* What might need clearing */
319 struct bulk_cb_wrap work_bcb;
320 struct bulk_cs_wrap work_bcs;
321 struct usb_ctrlrequest work_cr;
323 struct ub_scsi_trace tr;
328 static void ub_cleanup(struct ub_dev *sc);
329 static int ub_bd_rq_fn_1(struct ub_lun *lun, struct request *rq);
330 static int ub_cmd_build_block(struct ub_dev *sc, struct ub_lun *lun,
331 struct ub_scsi_cmd *cmd, struct request *rq);
332 static int ub_cmd_build_packet(struct ub_dev *sc, struct ub_scsi_cmd *cmd,
333 struct request *rq);
334 static void ub_rw_cmd_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
335 static void ub_end_rq(struct request *rq, int uptodate);
336 static int ub_submit_scsi(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
337 static void ub_urb_complete(struct urb *urb, struct pt_regs *pt);
338 static void ub_scsi_action(unsigned long _dev);
339 static void ub_scsi_dispatch(struct ub_dev *sc);
340 static void ub_scsi_urb_compl(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
341 static void ub_state_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd, int rc);
342 static void __ub_state_stat(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
343 static void ub_state_stat(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
344 static void ub_state_sense(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
345 static int ub_submit_clear_stall(struct ub_dev *sc, struct ub_scsi_cmd *cmd,
346 int stalled_pipe);
347 static void ub_top_sense_done(struct ub_dev *sc, struct ub_scsi_cmd *scmd);
348 static int ub_sync_tur(struct ub_dev *sc, struct ub_lun *lun);
349 static int ub_sync_read_cap(struct ub_dev *sc, struct ub_lun *lun,
350 struct ub_capacity *ret);
351 static int ub_probe_lun(struct ub_dev *sc, int lnum);
355 static struct usb_device_id ub_usb_ids[] = {
356 // { USB_DEVICE_VER(0x0781, 0x0002, 0x0009, 0x0009) }, /* SDDR-31 */
357 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_SCSI, US_PR_BULK) },
361 MODULE_DEVICE_TABLE(usb, ub_usb_ids);
364 * Find me a way to identify "next free minor" for add_disk(),
365 * and the array disappears the next day. However, the number of
366 * hosts has something to do with the naming and /proc/partitions.
367 * This has to be thought out in detail before changing.
368 * If UB_MAX_HOST was 1000, we'd use a bitmap. Or a better data structure.
370 #define UB_MAX_HOSTS 26
371 static char ub_hostv[UB_MAX_HOSTS];
373 static DEFINE_SPINLOCK(ub_lock); /* Locks globals and ->openc */
376 * The SCSI command tracing procedures.
379 static void ub_cmdtr_new(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
381 int n;
382 struct ub_scsi_cmd_trace *t;
384 if ((n = sc->tr.cur + 1) == SCMD_TRACE_SZ) n = 0;
385 t = &sc->tr.vec[n];
387 memset(t, 0, sizeof(struct ub_scsi_cmd_trace));
388 t->tag = cmd->tag;
389 t->op = cmd->cdb[0];
390 t->dir = cmd->dir;
391 t->req_size = cmd->len;
392 t->st_hst[0] = cmd->state;
394 sc->tr.cur = n;
395 cmd->trace_index = n;
398 static void ub_cmdtr_state(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
400 int n;
401 struct ub_scsi_cmd_trace *t;
403 t = &sc->tr.vec[cmd->trace_index];
404 if (t->tag == cmd->tag) {
405 if ((n = t->hcur + 1) == SCMD_ST_HIST_SZ) n = 0;
406 t->st_hst[n] = cmd->state;
407 t->hcur = n;
411 static void ub_cmdtr_act_len(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
413 struct ub_scsi_cmd_trace *t;
415 t = &sc->tr.vec[cmd->trace_index];
416 if (t->tag == cmd->tag)
417 t->act_size = cmd->act_len;
420 static void ub_cmdtr_sense(struct ub_dev *sc, struct ub_scsi_cmd *cmd,
421 unsigned char *sense)
423 struct ub_scsi_cmd_trace *t;
425 t = &sc->tr.vec[cmd->trace_index];
426 if (t->tag == cmd->tag) {
427 t->key = sense[2] & 0x0F;
428 t->asc = sense[12];
429 t->ascq = sense[13];
433 static ssize_t ub_diag_show(struct device *dev, char *page)
435 struct usb_interface *intf;
436 struct ub_dev *sc;
437 struct list_head *p;
438 struct ub_lun *lun;
439 int cnt;
440 unsigned long flags;
441 int nc, nh;
442 int i, j;
443 struct ub_scsi_cmd_trace *t;
445 intf = to_usb_interface(dev);
446 sc = usb_get_intfdata(intf);
447 if (sc == NULL)
448 return 0;
450 cnt = 0;
451 spin_lock_irqsave(&sc->lock, flags);
453 cnt += sprintf(page + cnt,
454 "qlen %d qmax %d\n",
455 sc->cmd_queue.qlen, sc->cmd_queue.qmax);
457 list_for_each (p, &sc->luns) {
458 lun = list_entry(p, struct ub_lun, link);
459 cnt += sprintf(page + cnt,
460 "lun %u changed %d removable %d readonly %d\n",
461 lun->num, lun->changed, lun->removable, lun->readonly);
464 if ((nc = sc->tr.cur + 1) == SCMD_TRACE_SZ) nc = 0;
465 for (j = 0; j < SCMD_TRACE_SZ; j++) {
466 t = &sc->tr.vec[nc];
468 cnt += sprintf(page + cnt, "%08x %02x", t->tag, t->op);
469 if (t->op == REQUEST_SENSE) {
470 cnt += sprintf(page + cnt, " [sense %x %02x %02x]",
471 t->key, t->asc, t->ascq);
472 } else {
473 cnt += sprintf(page + cnt, " %c", UB_DIR_CHAR(t->dir));
474 cnt += sprintf(page + cnt, " [%5d %5d]",
475 t->req_size, t->act_size);
477 if ((nh = t->hcur + 1) == SCMD_ST_HIST_SZ) nh = 0;
478 for (i = 0; i < SCMD_ST_HIST_SZ; i++) {
479 cnt += sprintf(page + cnt, " %s",
480 ub_scsi_cmd_stname[(int)t->st_hst[nh]]);
481 if (++nh == SCMD_ST_HIST_SZ) nh = 0;
483 cnt += sprintf(page + cnt, "\n");
485 if (++nc == SCMD_TRACE_SZ) nc = 0;
488 spin_unlock_irqrestore(&sc->lock, flags);
489 return cnt;
492 static DEVICE_ATTR(diag, S_IRUGO, ub_diag_show, NULL); /* N.B. World readable */
495 * The id allocator.
497 * This also stores the host for indexing by minor, which is somewhat dirty.
499 static int ub_id_get(void)
501 unsigned long flags;
502 int i;
504 spin_lock_irqsave(&ub_lock, flags);
505 for (i = 0; i < UB_MAX_HOSTS; i++) {
506 if (ub_hostv[i] == 0) {
507 ub_hostv[i] = 1;
508 spin_unlock_irqrestore(&ub_lock, flags);
509 return i;
512 spin_unlock_irqrestore(&ub_lock, flags);
513 return -1;
516 static void ub_id_put(int id)
518 unsigned long flags;
520 if (id < 0 || id >= UB_MAX_HOSTS) {
521 printk(KERN_ERR DRV_NAME ": bad host ID %d\n", id);
522 return;
525 spin_lock_irqsave(&ub_lock, flags);
526 if (ub_hostv[id] == 0) {
527 spin_unlock_irqrestore(&ub_lock, flags);
528 printk(KERN_ERR DRV_NAME ": freeing free host ID %d\n", id);
529 return;
531 ub_hostv[id] = 0;
532 spin_unlock_irqrestore(&ub_lock, flags);
536 * Downcount for deallocation. This rides on two assumptions:
537 * - once something is poisoned, its refcount cannot grow
538 * - opens cannot happen at this time (del_gendisk was done)
539 * If the above is true, we can drop the lock, which we need for
540 * blk_cleanup_queue(): the silly thing may attempt to sleep.
541 * [Actually, it never needs to sleep for us, but it calls might_sleep()]
543 static void ub_put(struct ub_dev *sc)
545 unsigned long flags;
547 spin_lock_irqsave(&ub_lock, flags);
548 --sc->openc;
549 if (sc->openc == 0 && atomic_read(&sc->poison)) {
550 spin_unlock_irqrestore(&ub_lock, flags);
551 ub_cleanup(sc);
552 } else {
553 spin_unlock_irqrestore(&ub_lock, flags);
558 * Final cleanup and deallocation.
560 static void ub_cleanup(struct ub_dev *sc)
562 struct list_head *p;
563 struct ub_lun *lun;
564 request_queue_t *q;
566 while (!list_empty(&sc->luns)) {
567 p = sc->luns.next;
568 lun = list_entry(p, struct ub_lun, link);
569 list_del(p);
571 /* I don't think queue can be NULL. But... Stolen from sx8.c */
572 if ((q = lun->disk->queue) != NULL)
573 blk_cleanup_queue(q);
575 * If we zero disk->private_data BEFORE put_disk, we have
576 * to check for NULL all over the place in open, release,
577 * check_media and revalidate, because the block level
578 * semaphore is well inside the put_disk.
579 * But we cannot zero after the call, because *disk is gone.
580 * The sd.c is blatantly racy in this area.
582 /* disk->private_data = NULL; */
583 put_disk(lun->disk);
584 lun->disk = NULL;
586 ub_id_put(lun->id);
587 kfree(lun);
590 kfree(sc);
594 * The "command allocator".
596 static struct ub_scsi_cmd *ub_get_cmd(struct ub_lun *lun)
598 struct ub_scsi_cmd *ret;
600 if (lun->cmda[0])
601 return NULL;
602 ret = &lun->cmdv[0];
603 lun->cmda[0] = 1;
604 return ret;
607 static void ub_put_cmd(struct ub_lun *lun, struct ub_scsi_cmd *cmd)
609 if (cmd != &lun->cmdv[0]) {
610 printk(KERN_WARNING "%s: releasing a foreign cmd %p\n",
611 lun->name, cmd);
612 return;
614 if (!lun->cmda[0]) {
615 printk(KERN_WARNING "%s: releasing a free cmd\n", lun->name);
616 return;
618 lun->cmda[0] = 0;
622 * The command queue.
624 static void ub_cmdq_add(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
626 struct ub_scsi_cmd_queue *t = &sc->cmd_queue;
628 if (t->qlen++ == 0) {
629 t->head = cmd;
630 t->tail = cmd;
631 } else {
632 t->tail->next = cmd;
633 t->tail = cmd;
636 if (t->qlen > t->qmax)
637 t->qmax = t->qlen;
640 static void ub_cmdq_insert(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
642 struct ub_scsi_cmd_queue *t = &sc->cmd_queue;
644 if (t->qlen++ == 0) {
645 t->head = cmd;
646 t->tail = cmd;
647 } else {
648 cmd->next = t->head;
649 t->head = cmd;
652 if (t->qlen > t->qmax)
653 t->qmax = t->qlen;
656 static struct ub_scsi_cmd *ub_cmdq_pop(struct ub_dev *sc)
658 struct ub_scsi_cmd_queue *t = &sc->cmd_queue;
659 struct ub_scsi_cmd *cmd;
661 if (t->qlen == 0)
662 return NULL;
663 if (--t->qlen == 0)
664 t->tail = NULL;
665 cmd = t->head;
666 t->head = cmd->next;
667 cmd->next = NULL;
668 return cmd;
671 #define ub_cmdq_peek(sc) ((sc)->cmd_queue.head)
674 * The request function is our main entry point
677 static void ub_bd_rq_fn(request_queue_t *q)
679 struct ub_lun *lun = q->queuedata;
680 struct request *rq;
682 while ((rq = elv_next_request(q)) != NULL) {
683 if (ub_bd_rq_fn_1(lun, rq) != 0) {
684 blk_stop_queue(q);
685 break;
690 static int ub_bd_rq_fn_1(struct ub_lun *lun, struct request *rq)
692 struct ub_dev *sc = lun->udev;
693 struct ub_scsi_cmd *cmd;
694 int rc;
696 if (atomic_read(&sc->poison) || lun->changed) {
697 blkdev_dequeue_request(rq);
698 ub_end_rq(rq, 0);
699 return 0;
702 if ((cmd = ub_get_cmd(lun)) == NULL)
703 return -1;
704 memset(cmd, 0, sizeof(struct ub_scsi_cmd));
706 blkdev_dequeue_request(rq);
708 if (blk_pc_request(rq)) {
709 rc = ub_cmd_build_packet(sc, cmd, rq);
710 } else {
711 rc = ub_cmd_build_block(sc, lun, cmd, rq);
713 if (rc != 0) {
714 ub_put_cmd(lun, cmd);
715 ub_end_rq(rq, 0);
716 return 0;
718 cmd->state = UB_CMDST_INIT;
719 cmd->lun = lun;
720 cmd->done = ub_rw_cmd_done;
721 cmd->back = rq;
723 cmd->tag = sc->tagcnt++;
724 if ((rc = ub_submit_scsi(sc, cmd)) != 0) {
725 ub_put_cmd(lun, cmd);
726 ub_end_rq(rq, 0);
727 return 0;
730 return 0;
733 static int ub_cmd_build_block(struct ub_dev *sc, struct ub_lun *lun,
734 struct ub_scsi_cmd *cmd, struct request *rq)
736 int ub_dir;
737 #if 0 /* We use rq->buffer for now */
738 struct scatterlist *sg;
739 int n_elem;
740 #endif
741 unsigned int block, nblks;
743 if (rq_data_dir(rq) == WRITE)
744 ub_dir = UB_DIR_WRITE;
745 else
746 ub_dir = UB_DIR_READ;
749 * get scatterlist from block layer
751 #if 0 /* We use rq->buffer for now */
752 sg = &cmd->sgv[0];
753 n_elem = blk_rq_map_sg(q, rq, sg);
754 if (n_elem <= 0) {
755 ub_put_cmd(lun, cmd);
756 ub_end_rq(rq, 0);
757 blk_start_queue(q);
758 return 0; /* request with no s/g entries? */
761 if (n_elem != 1) { /* Paranoia */
762 printk(KERN_WARNING "%s: request with %d segments\n",
763 sc->name, n_elem);
764 ub_put_cmd(lun, cmd);
765 ub_end_rq(rq, 0);
766 blk_start_queue(q);
767 return 0;
769 #endif
772 * XXX Unfortunately, this check does not work. It is quite possible
773 * to get bogus non-null rq->buffer if you allow sg by mistake.
775 if (rq->buffer == NULL) {
777 * This must not happen if we set the queue right.
778 * The block level must create bounce buffers for us.
780 static int do_print = 1;
781 if (do_print) {
782 printk(KERN_WARNING "%s: unmapped block request"
783 " flags 0x%lx sectors %lu\n",
784 sc->name, rq->flags, rq->nr_sectors);
785 do_print = 0;
787 return -1;
791 * build the command
793 * The call to blk_queue_hardsect_size() guarantees that request
794 * is aligned, but it is given in terms of 512 byte units, always.
796 block = rq->sector >> lun->capacity.bshift;
797 nblks = rq->nr_sectors >> lun->capacity.bshift;
799 cmd->cdb[0] = (ub_dir == UB_DIR_READ)? READ_10: WRITE_10;
800 /* 10-byte uses 4 bytes of LBA: 2147483648KB, 2097152MB, 2048GB */
801 cmd->cdb[2] = block >> 24;
802 cmd->cdb[3] = block >> 16;
803 cmd->cdb[4] = block >> 8;
804 cmd->cdb[5] = block;
805 cmd->cdb[7] = nblks >> 8;
806 cmd->cdb[8] = nblks;
807 cmd->cdb_len = 10;
809 cmd->dir = ub_dir;
810 cmd->data = rq->buffer;
811 cmd->len = rq->nr_sectors * 512;
813 return 0;
816 static int ub_cmd_build_packet(struct ub_dev *sc, struct ub_scsi_cmd *cmd,
817 struct request *rq)
820 if (rq->data_len != 0 && rq->data == NULL) {
821 static int do_print = 1;
822 if (do_print) {
823 printk(KERN_WARNING "%s: unmapped packet request"
824 " flags 0x%lx length %d\n",
825 sc->name, rq->flags, rq->data_len);
826 do_print = 0;
828 return -1;
831 memcpy(&cmd->cdb, rq->cmd, rq->cmd_len);
832 cmd->cdb_len = rq->cmd_len;
834 if (rq->data_len == 0) {
835 cmd->dir = UB_DIR_NONE;
836 } else {
837 if (rq_data_dir(rq) == WRITE)
838 cmd->dir = UB_DIR_WRITE;
839 else
840 cmd->dir = UB_DIR_READ;
842 cmd->data = rq->data;
843 cmd->len = rq->data_len;
845 return 0;
848 static void ub_rw_cmd_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
850 struct request *rq = cmd->back;
851 struct ub_lun *lun = cmd->lun;
852 struct gendisk *disk = lun->disk;
853 request_queue_t *q = disk->queue;
854 int uptodate;
856 if (blk_pc_request(rq)) {
857 /* UB_SENSE_SIZE is smaller than SCSI_SENSE_BUFFERSIZE */
858 memcpy(rq->sense, sc->top_sense, UB_SENSE_SIZE);
859 rq->sense_len = UB_SENSE_SIZE;
862 if (cmd->error == 0)
863 uptodate = 1;
864 else
865 uptodate = 0;
867 ub_put_cmd(lun, cmd);
868 ub_end_rq(rq, uptodate);
869 blk_start_queue(q);
872 static void ub_end_rq(struct request *rq, int uptodate)
874 int rc;
876 rc = end_that_request_first(rq, uptodate, rq->hard_nr_sectors);
877 // assert(rc == 0);
878 end_that_request_last(rq);
882 * Submit a regular SCSI operation (not an auto-sense).
884 * The Iron Law of Good Submit Routine is:
885 * Zero return - callback is done, Nonzero return - callback is not done.
886 * No exceptions.
888 * Host is assumed locked.
890 * XXX We only support Bulk for the moment.
892 static int ub_submit_scsi(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
895 if (cmd->state != UB_CMDST_INIT ||
896 (cmd->dir != UB_DIR_NONE && cmd->len == 0)) {
897 return -EINVAL;
900 ub_cmdq_add(sc, cmd);
902 * We can call ub_scsi_dispatch(sc) right away here, but it's a little
903 * safer to jump to a tasklet, in case upper layers do something silly.
905 tasklet_schedule(&sc->tasklet);
906 return 0;
910 * Submit the first URB for the queued command.
911 * This function does not deal with queueing in any way.
913 static int ub_scsi_cmd_start(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
915 struct bulk_cb_wrap *bcb;
916 int rc;
918 bcb = &sc->work_bcb;
921 * ``If the allocation length is eighteen or greater, and a device
922 * server returns less than eithteen bytes of data, the application
923 * client should assume that the bytes not transferred would have been
924 * zeroes had the device server returned those bytes.''
926 * We zero sense for all commands so that when a packet request
927 * fails it does not return a stale sense.
929 memset(&sc->top_sense, 0, UB_SENSE_SIZE);
931 /* set up the command wrapper */
932 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
933 bcb->Tag = cmd->tag; /* Endianness is not important */
934 bcb->DataTransferLength = cpu_to_le32(cmd->len);
935 bcb->Flags = (cmd->dir == UB_DIR_READ) ? 0x80 : 0;
936 bcb->Lun = (cmd->lun != NULL) ? cmd->lun->num : 0;
937 bcb->Length = cmd->cdb_len;
939 /* copy the command payload */
940 memcpy(bcb->CDB, cmd->cdb, UB_MAX_CDB_SIZE);
942 UB_INIT_COMPLETION(sc->work_done);
944 sc->last_pipe = sc->send_bulk_pipe;
945 usb_fill_bulk_urb(&sc->work_urb, sc->dev, sc->send_bulk_pipe,
946 bcb, US_BULK_CB_WRAP_LEN, ub_urb_complete, sc);
947 sc->work_urb.transfer_flags = URB_ASYNC_UNLINK;
949 /* Fill what we shouldn't be filling, because usb-storage did so. */
950 sc->work_urb.actual_length = 0;
951 sc->work_urb.error_count = 0;
952 sc->work_urb.status = 0;
954 if ((rc = usb_submit_urb(&sc->work_urb, GFP_ATOMIC)) != 0) {
955 /* XXX Clear stalls */
956 printk("ub: cmd #%d start failed (%d)\n", cmd->tag, rc); /* P3 */
957 ub_complete(&sc->work_done);
958 return rc;
961 sc->work_timer.expires = jiffies + UB_URB_TIMEOUT;
962 add_timer(&sc->work_timer);
964 cmd->state = UB_CMDST_CMD;
965 ub_cmdtr_state(sc, cmd);
966 return 0;
970 * Timeout handler.
972 static void ub_urb_timeout(unsigned long arg)
974 struct ub_dev *sc = (struct ub_dev *) arg;
975 unsigned long flags;
977 spin_lock_irqsave(&sc->lock, flags);
978 usb_unlink_urb(&sc->work_urb);
979 spin_unlock_irqrestore(&sc->lock, flags);
983 * Completion routine for the work URB.
985 * This can be called directly from usb_submit_urb (while we have
986 * the sc->lock taken) and from an interrupt (while we do NOT have
987 * the sc->lock taken). Therefore, bounce this off to a tasklet.
989 static void ub_urb_complete(struct urb *urb, struct pt_regs *pt)
991 struct ub_dev *sc = urb->context;
993 ub_complete(&sc->work_done);
994 tasklet_schedule(&sc->tasklet);
997 static void ub_scsi_action(unsigned long _dev)
999 struct ub_dev *sc = (struct ub_dev *) _dev;
1000 unsigned long flags;
1002 spin_lock_irqsave(&sc->lock, flags);
1003 del_timer(&sc->work_timer);
1004 ub_scsi_dispatch(sc);
1005 spin_unlock_irqrestore(&sc->lock, flags);
1008 static void ub_scsi_dispatch(struct ub_dev *sc)
1010 struct ub_scsi_cmd *cmd;
1011 int rc;
1013 while ((cmd = ub_cmdq_peek(sc)) != NULL) {
1014 if (cmd->state == UB_CMDST_DONE) {
1015 ub_cmdq_pop(sc);
1016 (*cmd->done)(sc, cmd);
1017 } else if (cmd->state == UB_CMDST_INIT) {
1018 ub_cmdtr_new(sc, cmd);
1019 if ((rc = ub_scsi_cmd_start(sc, cmd)) == 0)
1020 break;
1021 cmd->error = rc;
1022 cmd->state = UB_CMDST_DONE;
1023 ub_cmdtr_state(sc, cmd);
1024 } else {
1025 if (!ub_is_completed(&sc->work_done))
1026 break;
1027 ub_scsi_urb_compl(sc, cmd);
1032 static void ub_scsi_urb_compl(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1034 struct urb *urb = &sc->work_urb;
1035 struct bulk_cs_wrap *bcs;
1036 int pipe;
1037 int rc;
1039 if (atomic_read(&sc->poison)) {
1040 /* A little too simplistic, I feel... */
1041 goto Bad_End;
1044 if (cmd->state == UB_CMDST_CLEAR) {
1045 if (urb->status == -EPIPE) {
1047 * STALL while clearning STALL.
1048 * The control pipe clears itself - nothing to do.
1049 * XXX Might try to reset the device here and retry.
1051 printk(KERN_NOTICE "%s: stall on control pipe\n",
1052 sc->name);
1053 goto Bad_End;
1057 * We ignore the result for the halt clear.
1060 /* reset the endpoint toggle */
1061 usb_settoggle(sc->dev, usb_pipeendpoint(sc->last_pipe),
1062 usb_pipeout(sc->last_pipe), 0);
1064 ub_state_sense(sc, cmd);
1066 } else if (cmd->state == UB_CMDST_CLR2STS) {
1067 if (urb->status == -EPIPE) {
1069 * STALL while clearning STALL.
1070 * The control pipe clears itself - nothing to do.
1071 * XXX Might try to reset the device here and retry.
1073 printk(KERN_NOTICE "%s: stall on control pipe\n",
1074 sc->name);
1075 goto Bad_End;
1079 * We ignore the result for the halt clear.
1082 /* reset the endpoint toggle */
1083 usb_settoggle(sc->dev, usb_pipeendpoint(sc->last_pipe),
1084 usb_pipeout(sc->last_pipe), 0);
1086 ub_state_stat(sc, cmd);
1088 } else if (cmd->state == UB_CMDST_CMD) {
1089 if (urb->status == -EPIPE) {
1090 rc = ub_submit_clear_stall(sc, cmd, sc->last_pipe);
1091 if (rc != 0) {
1092 printk(KERN_NOTICE "%s: "
1093 "unable to submit clear (%d)\n",
1094 sc->name, rc);
1096 * This is typically ENOMEM or some other such shit.
1097 * Retrying is pointless. Just do Bad End on it...
1099 goto Bad_End;
1101 cmd->state = UB_CMDST_CLEAR;
1102 ub_cmdtr_state(sc, cmd);
1103 return;
1105 if (urb->status != 0) {
1106 printk("ub: cmd #%d cmd status (%d)\n", cmd->tag, urb->status); /* P3 */
1107 goto Bad_End;
1109 if (urb->actual_length != US_BULK_CB_WRAP_LEN) {
1110 printk("ub: cmd #%d xferred %d\n", cmd->tag, urb->actual_length); /* P3 */
1111 /* XXX Must do reset here to unconfuse the device */
1112 goto Bad_End;
1115 if (cmd->dir == UB_DIR_NONE) {
1116 ub_state_stat(sc, cmd);
1117 return;
1120 UB_INIT_COMPLETION(sc->work_done);
1122 if (cmd->dir == UB_DIR_READ)
1123 pipe = sc->recv_bulk_pipe;
1124 else
1125 pipe = sc->send_bulk_pipe;
1126 sc->last_pipe = pipe;
1127 usb_fill_bulk_urb(&sc->work_urb, sc->dev, pipe,
1128 cmd->data, cmd->len, ub_urb_complete, sc);
1129 sc->work_urb.transfer_flags = URB_ASYNC_UNLINK;
1130 sc->work_urb.actual_length = 0;
1131 sc->work_urb.error_count = 0;
1132 sc->work_urb.status = 0;
1134 if ((rc = usb_submit_urb(&sc->work_urb, GFP_ATOMIC)) != 0) {
1135 /* XXX Clear stalls */
1136 printk("ub: data #%d submit failed (%d)\n", cmd->tag, rc); /* P3 */
1137 ub_complete(&sc->work_done);
1138 ub_state_done(sc, cmd, rc);
1139 return;
1142 sc->work_timer.expires = jiffies + UB_DATA_TIMEOUT;
1143 add_timer(&sc->work_timer);
1145 cmd->state = UB_CMDST_DATA;
1146 ub_cmdtr_state(sc, cmd);
1148 } else if (cmd->state == UB_CMDST_DATA) {
1149 if (urb->status == -EPIPE) {
1150 rc = ub_submit_clear_stall(sc, cmd, sc->last_pipe);
1151 if (rc != 0) {
1152 printk(KERN_NOTICE "%s: "
1153 "unable to submit clear (%d)\n",
1154 sc->name, rc);
1156 * This is typically ENOMEM or some other such shit.
1157 * Retrying is pointless. Just do Bad End on it...
1159 goto Bad_End;
1161 cmd->state = UB_CMDST_CLR2STS;
1162 ub_cmdtr_state(sc, cmd);
1163 return;
1165 if (urb->status == -EOVERFLOW) {
1167 * A babble? Failure, but we must transfer CSW now.
1169 cmd->error = -EOVERFLOW; /* A cheap trick... */
1170 } else {
1171 if (urb->status != 0)
1172 goto Bad_End;
1175 cmd->act_len = urb->actual_length;
1176 ub_cmdtr_act_len(sc, cmd);
1178 ub_state_stat(sc, cmd);
1180 } else if (cmd->state == UB_CMDST_STAT) {
1181 if (urb->status == -EPIPE) {
1182 rc = ub_submit_clear_stall(sc, cmd, sc->last_pipe);
1183 if (rc != 0) {
1184 printk(KERN_NOTICE "%s: "
1185 "unable to submit clear (%d)\n",
1186 sc->name, rc);
1188 * This is typically ENOMEM or some other such shit.
1189 * Retrying is pointless. Just do Bad End on it...
1191 goto Bad_End;
1193 cmd->state = UB_CMDST_CLEAR;
1194 ub_cmdtr_state(sc, cmd);
1195 return;
1197 if (urb->status != 0)
1198 goto Bad_End;
1200 if (urb->actual_length == 0) {
1202 * Some broken devices add unnecessary zero-length
1203 * packets to the end of their data transfers.
1204 * Such packets show up as 0-length CSWs. If we
1205 * encounter such a thing, try to read the CSW again.
1207 if (++cmd->stat_count >= 4) {
1208 printk(KERN_NOTICE "%s: unable to get CSW\n",
1209 sc->name);
1210 goto Bad_End;
1212 __ub_state_stat(sc, cmd);
1213 return;
1217 * Check the returned Bulk protocol status.
1220 bcs = &sc->work_bcs;
1221 rc = le32_to_cpu(bcs->Residue);
1222 if (rc != cmd->len - cmd->act_len) {
1224 * It is all right to transfer less, the caller has
1225 * to check. But it's not all right if the device
1226 * counts disagree with our counts.
1228 /* P3 */ printk("%s: resid %d len %d act %d\n",
1229 sc->name, rc, cmd->len, cmd->act_len);
1230 goto Bad_End;
1233 #if 0
1234 if (bcs->Signature != cpu_to_le32(US_BULK_CS_SIGN) &&
1235 bcs->Signature != cpu_to_le32(US_BULK_CS_OLYMPUS_SIGN)) {
1236 /* Windows ignores signatures, so do we. */
1238 #endif
1240 if (bcs->Tag != cmd->tag) {
1242 * This usually happens when we disagree with the
1243 * device's microcode about something. For instance,
1244 * a few of them throw this after timeouts. They buffer
1245 * commands and reply at commands we timed out before.
1246 * Without flushing these replies we loop forever.
1248 if (++cmd->stat_count >= 4) {
1249 printk(KERN_NOTICE "%s: "
1250 "tag mismatch orig 0x%x reply 0x%x\n",
1251 sc->name, cmd->tag, bcs->Tag);
1252 goto Bad_End;
1254 __ub_state_stat(sc, cmd);
1255 return;
1258 switch (bcs->Status) {
1259 case US_BULK_STAT_OK:
1260 break;
1261 case US_BULK_STAT_FAIL:
1262 ub_state_sense(sc, cmd);
1263 return;
1264 case US_BULK_STAT_PHASE:
1265 /* XXX We must reset the transport here */
1266 /* P3 */ printk("%s: status PHASE\n", sc->name);
1267 goto Bad_End;
1268 default:
1269 printk(KERN_INFO "%s: unknown CSW status 0x%x\n",
1270 sc->name, bcs->Status);
1271 goto Bad_End;
1274 /* Not zeroing error to preserve a babble indicator */
1275 cmd->state = UB_CMDST_DONE;
1276 ub_cmdtr_state(sc, cmd);
1277 ub_cmdq_pop(sc);
1278 (*cmd->done)(sc, cmd);
1280 } else if (cmd->state == UB_CMDST_SENSE) {
1281 ub_state_done(sc, cmd, -EIO);
1283 } else {
1284 printk(KERN_WARNING "%s: "
1285 "wrong command state %d\n",
1286 sc->name, cmd->state);
1287 goto Bad_End;
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 * Finish the command.
1299 static void ub_state_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd, int rc)
1302 cmd->error = rc;
1303 cmd->state = UB_CMDST_DONE;
1304 ub_cmdtr_state(sc, cmd);
1305 ub_cmdq_pop(sc);
1306 (*cmd->done)(sc, cmd);
1310 * Factorization helper for the command state machine:
1311 * Submit a CSW read.
1313 static void __ub_state_stat(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1315 int rc;
1317 UB_INIT_COMPLETION(sc->work_done);
1319 sc->last_pipe = sc->recv_bulk_pipe;
1320 usb_fill_bulk_urb(&sc->work_urb, sc->dev, sc->recv_bulk_pipe,
1321 &sc->work_bcs, US_BULK_CS_WRAP_LEN, ub_urb_complete, sc);
1322 sc->work_urb.transfer_flags = URB_ASYNC_UNLINK;
1323 sc->work_urb.actual_length = 0;
1324 sc->work_urb.error_count = 0;
1325 sc->work_urb.status = 0;
1327 if ((rc = usb_submit_urb(&sc->work_urb, GFP_ATOMIC)) != 0) {
1328 /* XXX Clear stalls */
1329 ub_complete(&sc->work_done);
1330 ub_state_done(sc, cmd, rc);
1331 return;
1334 sc->work_timer.expires = jiffies + UB_STAT_TIMEOUT;
1335 add_timer(&sc->work_timer);
1339 * Factorization helper for the command state machine:
1340 * Submit a CSW read and go to STAT state.
1342 static void ub_state_stat(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1344 __ub_state_stat(sc, cmd);
1346 cmd->stat_count = 0;
1347 cmd->state = UB_CMDST_STAT;
1348 ub_cmdtr_state(sc, cmd);
1352 * Factorization helper for the command state machine:
1353 * Submit a REQUEST SENSE and go to SENSE state.
1355 static void ub_state_sense(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1357 struct ub_scsi_cmd *scmd;
1358 int rc;
1360 if (cmd->cdb[0] == REQUEST_SENSE) {
1361 rc = -EPIPE;
1362 goto error;
1365 scmd = &sc->top_rqs_cmd;
1366 scmd->cdb[0] = REQUEST_SENSE;
1367 scmd->cdb[4] = UB_SENSE_SIZE;
1368 scmd->cdb_len = 6;
1369 scmd->dir = UB_DIR_READ;
1370 scmd->state = UB_CMDST_INIT;
1371 scmd->data = sc->top_sense;
1372 scmd->len = UB_SENSE_SIZE;
1373 scmd->lun = cmd->lun;
1374 scmd->done = ub_top_sense_done;
1375 scmd->back = cmd;
1377 scmd->tag = sc->tagcnt++;
1379 cmd->state = UB_CMDST_SENSE;
1380 ub_cmdtr_state(sc, cmd);
1382 ub_cmdq_insert(sc, scmd);
1383 return;
1385 error:
1386 ub_state_done(sc, cmd, rc);
1390 * A helper for the command's state machine:
1391 * Submit a stall clear.
1393 static int ub_submit_clear_stall(struct ub_dev *sc, struct ub_scsi_cmd *cmd,
1394 int stalled_pipe)
1396 int endp;
1397 struct usb_ctrlrequest *cr;
1398 int rc;
1400 endp = usb_pipeendpoint(stalled_pipe);
1401 if (usb_pipein (stalled_pipe))
1402 endp |= USB_DIR_IN;
1404 cr = &sc->work_cr;
1405 cr->bRequestType = USB_RECIP_ENDPOINT;
1406 cr->bRequest = USB_REQ_CLEAR_FEATURE;
1407 cr->wValue = cpu_to_le16(USB_ENDPOINT_HALT);
1408 cr->wIndex = cpu_to_le16(endp);
1409 cr->wLength = cpu_to_le16(0);
1411 UB_INIT_COMPLETION(sc->work_done);
1413 usb_fill_control_urb(&sc->work_urb, sc->dev, sc->send_ctrl_pipe,
1414 (unsigned char*) cr, NULL, 0, ub_urb_complete, sc);
1415 sc->work_urb.transfer_flags = URB_ASYNC_UNLINK;
1416 sc->work_urb.actual_length = 0;
1417 sc->work_urb.error_count = 0;
1418 sc->work_urb.status = 0;
1420 if ((rc = usb_submit_urb(&sc->work_urb, GFP_ATOMIC)) != 0) {
1421 ub_complete(&sc->work_done);
1422 return rc;
1425 sc->work_timer.expires = jiffies + UB_CTRL_TIMEOUT;
1426 add_timer(&sc->work_timer);
1427 return 0;
1432 static void ub_top_sense_done(struct ub_dev *sc, struct ub_scsi_cmd *scmd)
1434 unsigned char *sense = scmd->data;
1435 struct ub_scsi_cmd *cmd;
1438 * Ignoring scmd->act_len, because the buffer was pre-zeroed.
1440 ub_cmdtr_sense(sc, scmd, sense);
1443 * Find the command which triggered the unit attention or a check,
1444 * save the sense into it, and advance its state machine.
1446 if ((cmd = ub_cmdq_peek(sc)) == NULL) {
1447 printk(KERN_WARNING "%s: sense done while idle\n", sc->name);
1448 return;
1450 if (cmd != scmd->back) {
1451 printk(KERN_WARNING "%s: "
1452 "sense done for wrong command 0x%x\n",
1453 sc->name, cmd->tag);
1454 return;
1456 if (cmd->state != UB_CMDST_SENSE) {
1457 printk(KERN_WARNING "%s: "
1458 "sense done with bad cmd state %d\n",
1459 sc->name, cmd->state);
1460 return;
1463 cmd->key = sense[2] & 0x0F;
1464 cmd->asc = sense[12];
1465 cmd->ascq = sense[13];
1467 ub_scsi_urb_compl(sc, cmd);
1471 * This is called from a process context.
1473 static void ub_revalidate(struct ub_dev *sc, struct ub_lun *lun)
1476 lun->readonly = 0; /* XXX Query this from the device */
1478 lun->capacity.nsec = 0;
1479 lun->capacity.bsize = 512;
1480 lun->capacity.bshift = 0;
1482 if (ub_sync_tur(sc, lun) != 0)
1483 return; /* Not ready */
1484 lun->changed = 0;
1486 if (ub_sync_read_cap(sc, lun, &lun->capacity) != 0) {
1488 * The retry here means something is wrong, either with the
1489 * device, with the transport, or with our code.
1490 * We keep this because sd.c has retries for capacity.
1492 if (ub_sync_read_cap(sc, lun, &lun->capacity) != 0) {
1493 lun->capacity.nsec = 0;
1494 lun->capacity.bsize = 512;
1495 lun->capacity.bshift = 0;
1501 * The open funcion.
1502 * This is mostly needed to keep refcounting, but also to support
1503 * media checks on removable media drives.
1505 static int ub_bd_open(struct inode *inode, struct file *filp)
1507 struct gendisk *disk = inode->i_bdev->bd_disk;
1508 struct ub_lun *lun;
1509 struct ub_dev *sc;
1510 unsigned long flags;
1511 int rc;
1513 if ((lun = disk->private_data) == NULL)
1514 return -ENXIO;
1515 sc = lun->udev;
1517 spin_lock_irqsave(&ub_lock, flags);
1518 if (atomic_read(&sc->poison)) {
1519 spin_unlock_irqrestore(&ub_lock, flags);
1520 return -ENXIO;
1522 sc->openc++;
1523 spin_unlock_irqrestore(&ub_lock, flags);
1526 * This is a workaround for a specific problem in our block layer.
1527 * In 2.6.9, register_disk duplicates the code from rescan_partitions.
1528 * However, if we do add_disk with a device which persistently reports
1529 * a changed media, add_disk calls register_disk, which does do_open,
1530 * which will call rescan_paritions for changed media. After that,
1531 * register_disk attempts to do it all again and causes double kobject
1532 * registration and a eventually an oops on module removal.
1534 * The bottom line is, Al Viro says that we should not allow
1535 * bdev->bd_invalidated to be set when doing add_disk no matter what.
1537 if (lun->first_open) {
1538 lun->first_open = 0;
1539 if (lun->changed) {
1540 rc = -ENOMEDIUM;
1541 goto err_open;
1545 if (lun->removable || lun->readonly)
1546 check_disk_change(inode->i_bdev);
1549 * The sd.c considers ->media_present and ->changed not equivalent,
1550 * under some pretty murky conditions (a failure of READ CAPACITY).
1551 * We may need it one day.
1553 if (lun->removable && lun->changed && !(filp->f_flags & O_NDELAY)) {
1554 rc = -ENOMEDIUM;
1555 goto err_open;
1558 if (lun->readonly && (filp->f_mode & FMODE_WRITE)) {
1559 rc = -EROFS;
1560 goto err_open;
1563 return 0;
1565 err_open:
1566 ub_put(sc);
1567 return rc;
1572 static int ub_bd_release(struct inode *inode, struct file *filp)
1574 struct gendisk *disk = inode->i_bdev->bd_disk;
1575 struct ub_lun *lun = disk->private_data;
1576 struct ub_dev *sc = lun->udev;
1578 ub_put(sc);
1579 return 0;
1583 * The ioctl interface.
1585 static int ub_bd_ioctl(struct inode *inode, struct file *filp,
1586 unsigned int cmd, unsigned long arg)
1588 struct gendisk *disk = inode->i_bdev->bd_disk;
1589 void __user *usermem = (void __user *) arg;
1591 return scsi_cmd_ioctl(filp, disk, cmd, usermem);
1595 * This is called once a new disk was seen by the block layer or by ub_probe().
1596 * The main onjective here is to discover the features of the media such as
1597 * the capacity, read-only status, etc. USB storage generally does not
1598 * need to be spun up, but if we needed it, this would be the place.
1600 * This call can sleep.
1602 * The return code is not used.
1604 static int ub_bd_revalidate(struct gendisk *disk)
1606 struct ub_lun *lun = disk->private_data;
1608 ub_revalidate(lun->udev, lun);
1610 /* XXX Support sector size switching like in sr.c */
1611 blk_queue_hardsect_size(disk->queue, lun->capacity.bsize);
1612 set_capacity(disk, lun->capacity.nsec);
1613 // set_disk_ro(sdkp->disk, lun->readonly);
1615 return 0;
1619 * The check is called by the block layer to verify if the media
1620 * is still available. It is supposed to be harmless, lightweight and
1621 * non-intrusive in case the media was not changed.
1623 * This call can sleep.
1625 * The return code is bool!
1627 static int ub_bd_media_changed(struct gendisk *disk)
1629 struct ub_lun *lun = disk->private_data;
1631 if (!lun->removable)
1632 return 0;
1635 * We clean checks always after every command, so this is not
1636 * as dangerous as it looks. If the TEST_UNIT_READY fails here,
1637 * the device is actually not ready with operator or software
1638 * intervention required. One dangerous item might be a drive which
1639 * spins itself down, and come the time to write dirty pages, this
1640 * will fail, then block layer discards the data. Since we never
1641 * spin drives up, such devices simply cannot be used with ub anyway.
1643 if (ub_sync_tur(lun->udev, lun) != 0) {
1644 lun->changed = 1;
1645 return 1;
1648 return lun->changed;
1651 static struct block_device_operations ub_bd_fops = {
1652 .owner = THIS_MODULE,
1653 .open = ub_bd_open,
1654 .release = ub_bd_release,
1655 .ioctl = ub_bd_ioctl,
1656 .media_changed = ub_bd_media_changed,
1657 .revalidate_disk = ub_bd_revalidate,
1661 * Common ->done routine for commands executed synchronously.
1663 static void ub_probe_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1665 struct completion *cop = cmd->back;
1666 complete(cop);
1670 * Test if the device has a check condition on it, synchronously.
1672 static int ub_sync_tur(struct ub_dev *sc, struct ub_lun *lun)
1674 struct ub_scsi_cmd *cmd;
1675 enum { ALLOC_SIZE = sizeof(struct ub_scsi_cmd) };
1676 unsigned long flags;
1677 struct completion compl;
1678 int rc;
1680 init_completion(&compl);
1682 rc = -ENOMEM;
1683 if ((cmd = kmalloc(ALLOC_SIZE, GFP_KERNEL)) == NULL)
1684 goto err_alloc;
1685 memset(cmd, 0, ALLOC_SIZE);
1687 cmd->cdb[0] = TEST_UNIT_READY;
1688 cmd->cdb_len = 6;
1689 cmd->dir = UB_DIR_NONE;
1690 cmd->state = UB_CMDST_INIT;
1691 cmd->lun = lun; /* This may be NULL, but that's ok */
1692 cmd->done = ub_probe_done;
1693 cmd->back = &compl;
1695 spin_lock_irqsave(&sc->lock, flags);
1696 cmd->tag = sc->tagcnt++;
1698 rc = ub_submit_scsi(sc, cmd);
1699 spin_unlock_irqrestore(&sc->lock, flags);
1701 if (rc != 0) {
1702 printk("ub: testing ready: submit error (%d)\n", rc); /* P3 */
1703 goto err_submit;
1706 wait_for_completion(&compl);
1708 rc = cmd->error;
1710 if (rc == -EIO && cmd->key != 0) /* Retries for benh's key */
1711 rc = cmd->key;
1713 err_submit:
1714 kfree(cmd);
1715 err_alloc:
1716 return rc;
1720 * Read the SCSI capacity synchronously (for probing).
1722 static int ub_sync_read_cap(struct ub_dev *sc, struct ub_lun *lun,
1723 struct ub_capacity *ret)
1725 struct ub_scsi_cmd *cmd;
1726 char *p;
1727 enum { ALLOC_SIZE = sizeof(struct ub_scsi_cmd) + 8 };
1728 unsigned long flags;
1729 unsigned int bsize, shift;
1730 unsigned long nsec;
1731 struct completion compl;
1732 int rc;
1734 init_completion(&compl);
1736 rc = -ENOMEM;
1737 if ((cmd = kmalloc(ALLOC_SIZE, GFP_KERNEL)) == NULL)
1738 goto err_alloc;
1739 memset(cmd, 0, ALLOC_SIZE);
1740 p = (char *)cmd + sizeof(struct ub_scsi_cmd);
1742 cmd->cdb[0] = 0x25;
1743 cmd->cdb_len = 10;
1744 cmd->dir = UB_DIR_READ;
1745 cmd->state = UB_CMDST_INIT;
1746 cmd->data = p;
1747 cmd->len = 8;
1748 cmd->lun = lun;
1749 cmd->done = ub_probe_done;
1750 cmd->back = &compl;
1752 spin_lock_irqsave(&sc->lock, flags);
1753 cmd->tag = sc->tagcnt++;
1755 rc = ub_submit_scsi(sc, cmd);
1756 spin_unlock_irqrestore(&sc->lock, flags);
1758 if (rc != 0) {
1759 printk("ub: reading capacity: submit error (%d)\n", rc); /* P3 */
1760 goto err_submit;
1763 wait_for_completion(&compl);
1765 if (cmd->error != 0) {
1766 printk("ub: reading capacity: error %d\n", cmd->error); /* P3 */
1767 rc = -EIO;
1768 goto err_read;
1770 if (cmd->act_len != 8) {
1771 printk("ub: reading capacity: size %d\n", cmd->act_len); /* P3 */
1772 rc = -EIO;
1773 goto err_read;
1776 /* sd.c special-cases sector size of 0 to mean 512. Needed? Safe? */
1777 nsec = be32_to_cpu(*(__be32 *)p) + 1;
1778 bsize = be32_to_cpu(*(__be32 *)(p + 4));
1779 switch (bsize) {
1780 case 512: shift = 0; break;
1781 case 1024: shift = 1; break;
1782 case 2048: shift = 2; break;
1783 case 4096: shift = 3; break;
1784 default:
1785 printk("ub: Bad sector size %u\n", bsize); /* P3 */
1786 rc = -EDOM;
1787 goto err_inv_bsize;
1790 ret->bsize = bsize;
1791 ret->bshift = shift;
1792 ret->nsec = nsec << shift;
1793 rc = 0;
1795 err_inv_bsize:
1796 err_read:
1797 err_submit:
1798 kfree(cmd);
1799 err_alloc:
1800 return rc;
1805 static void ub_probe_urb_complete(struct urb *urb, struct pt_regs *pt)
1807 struct completion *cop = urb->context;
1808 complete(cop);
1811 static void ub_probe_timeout(unsigned long arg)
1813 struct completion *cop = (struct completion *) arg;
1814 complete(cop);
1818 * Get number of LUNs by the way of Bulk GetMaxLUN command.
1820 static int ub_sync_getmaxlun(struct ub_dev *sc)
1822 int ifnum = sc->intf->cur_altsetting->desc.bInterfaceNumber;
1823 unsigned char *p;
1824 enum { ALLOC_SIZE = 1 };
1825 struct usb_ctrlrequest *cr;
1826 struct completion compl;
1827 struct timer_list timer;
1828 int nluns;
1829 int rc;
1831 init_completion(&compl);
1833 rc = -ENOMEM;
1834 if ((p = kmalloc(ALLOC_SIZE, GFP_KERNEL)) == NULL)
1835 goto err_alloc;
1836 *p = 55;
1838 cr = &sc->work_cr;
1839 cr->bRequestType = USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
1840 cr->bRequest = US_BULK_GET_MAX_LUN;
1841 cr->wValue = cpu_to_le16(0);
1842 cr->wIndex = cpu_to_le16(ifnum);
1843 cr->wLength = cpu_to_le16(1);
1845 usb_fill_control_urb(&sc->work_urb, sc->dev, sc->recv_ctrl_pipe,
1846 (unsigned char*) cr, p, 1, ub_probe_urb_complete, &compl);
1847 sc->work_urb.transfer_flags = 0;
1848 sc->work_urb.actual_length = 0;
1849 sc->work_urb.error_count = 0;
1850 sc->work_urb.status = 0;
1852 if ((rc = usb_submit_urb(&sc->work_urb, GFP_KERNEL)) != 0) {
1853 if (rc == -EPIPE) {
1854 printk("%s: Stall at GetMaxLUN, using 1 LUN\n",
1855 sc->name); /* P3 */
1856 } else {
1857 printk(KERN_WARNING
1858 "%s: Unable to submit GetMaxLUN (%d)\n",
1859 sc->name, rc);
1861 goto err_submit;
1864 init_timer(&timer);
1865 timer.function = ub_probe_timeout;
1866 timer.data = (unsigned long) &compl;
1867 timer.expires = jiffies + UB_CTRL_TIMEOUT;
1868 add_timer(&timer);
1870 wait_for_completion(&compl);
1872 del_timer_sync(&timer);
1873 usb_kill_urb(&sc->work_urb);
1875 if (sc->work_urb.actual_length != 1) {
1876 printk("%s: GetMaxLUN returned %d bytes\n", sc->name,
1877 sc->work_urb.actual_length); /* P3 */
1878 nluns = 0;
1879 } else {
1880 if ((nluns = *p) == 55) {
1881 nluns = 0;
1882 } else {
1883 /* GetMaxLUN returns the maximum LUN number */
1884 nluns += 1;
1885 if (nluns > UB_MAX_LUNS)
1886 nluns = UB_MAX_LUNS;
1888 printk("%s: GetMaxLUN returned %d, using %d LUNs\n", sc->name,
1889 *p, nluns); /* P3 */
1892 kfree(p);
1893 return nluns;
1895 err_submit:
1896 kfree(p);
1897 err_alloc:
1898 return rc;
1902 * Clear initial stalls.
1904 static int ub_probe_clear_stall(struct ub_dev *sc, int stalled_pipe)
1906 int endp;
1907 struct usb_ctrlrequest *cr;
1908 struct completion compl;
1909 struct timer_list timer;
1910 int rc;
1912 init_completion(&compl);
1914 endp = usb_pipeendpoint(stalled_pipe);
1915 if (usb_pipein (stalled_pipe))
1916 endp |= USB_DIR_IN;
1918 cr = &sc->work_cr;
1919 cr->bRequestType = USB_RECIP_ENDPOINT;
1920 cr->bRequest = USB_REQ_CLEAR_FEATURE;
1921 cr->wValue = cpu_to_le16(USB_ENDPOINT_HALT);
1922 cr->wIndex = cpu_to_le16(endp);
1923 cr->wLength = cpu_to_le16(0);
1925 usb_fill_control_urb(&sc->work_urb, sc->dev, sc->send_ctrl_pipe,
1926 (unsigned char*) cr, NULL, 0, ub_probe_urb_complete, &compl);
1927 sc->work_urb.transfer_flags = 0;
1928 sc->work_urb.actual_length = 0;
1929 sc->work_urb.error_count = 0;
1930 sc->work_urb.status = 0;
1932 if ((rc = usb_submit_urb(&sc->work_urb, GFP_KERNEL)) != 0) {
1933 printk(KERN_WARNING
1934 "%s: Unable to submit a probe clear (%d)\n", sc->name, rc);
1935 return rc;
1938 init_timer(&timer);
1939 timer.function = ub_probe_timeout;
1940 timer.data = (unsigned long) &compl;
1941 timer.expires = jiffies + UB_CTRL_TIMEOUT;
1942 add_timer(&timer);
1944 wait_for_completion(&compl);
1946 del_timer_sync(&timer);
1947 usb_kill_urb(&sc->work_urb);
1949 /* reset the endpoint toggle */
1950 usb_settoggle(sc->dev, endp, usb_pipeout(sc->last_pipe), 0);
1952 return 0;
1956 * Get the pipe settings.
1958 static int ub_get_pipes(struct ub_dev *sc, struct usb_device *dev,
1959 struct usb_interface *intf)
1961 struct usb_host_interface *altsetting = intf->cur_altsetting;
1962 struct usb_endpoint_descriptor *ep_in = NULL;
1963 struct usb_endpoint_descriptor *ep_out = NULL;
1964 struct usb_endpoint_descriptor *ep;
1965 int i;
1968 * Find the endpoints we need.
1969 * We are expecting a minimum of 2 endpoints - in and out (bulk).
1970 * We will ignore any others.
1972 for (i = 0; i < altsetting->desc.bNumEndpoints; i++) {
1973 ep = &altsetting->endpoint[i].desc;
1975 /* Is it a BULK endpoint? */
1976 if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1977 == USB_ENDPOINT_XFER_BULK) {
1978 /* BULK in or out? */
1979 if (ep->bEndpointAddress & USB_DIR_IN)
1980 ep_in = ep;
1981 else
1982 ep_out = ep;
1986 if (ep_in == NULL || ep_out == NULL) {
1987 printk(KERN_NOTICE "%s: failed endpoint check\n",
1988 sc->name);
1989 return -EIO;
1992 /* Calculate and store the pipe values */
1993 sc->send_ctrl_pipe = usb_sndctrlpipe(dev, 0);
1994 sc->recv_ctrl_pipe = usb_rcvctrlpipe(dev, 0);
1995 sc->send_bulk_pipe = usb_sndbulkpipe(dev,
1996 ep_out->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
1997 sc->recv_bulk_pipe = usb_rcvbulkpipe(dev,
1998 ep_in->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
2000 return 0;
2004 * Probing is done in the process context, which allows us to cheat
2005 * and not to build a state machine for the discovery.
2007 static int ub_probe(struct usb_interface *intf,
2008 const struct usb_device_id *dev_id)
2010 struct ub_dev *sc;
2011 int nluns;
2012 int rc;
2013 int i;
2015 rc = -ENOMEM;
2016 if ((sc = kmalloc(sizeof(struct ub_dev), GFP_KERNEL)) == NULL)
2017 goto err_core;
2018 memset(sc, 0, sizeof(struct ub_dev));
2019 spin_lock_init(&sc->lock);
2020 INIT_LIST_HEAD(&sc->luns);
2021 usb_init_urb(&sc->work_urb);
2022 tasklet_init(&sc->tasklet, ub_scsi_action, (unsigned long)sc);
2023 atomic_set(&sc->poison, 0);
2025 init_timer(&sc->work_timer);
2026 sc->work_timer.data = (unsigned long) sc;
2027 sc->work_timer.function = ub_urb_timeout;
2029 ub_init_completion(&sc->work_done);
2030 sc->work_done.done = 1; /* A little yuk, but oh well... */
2032 sc->dev = interface_to_usbdev(intf);
2033 sc->intf = intf;
2034 // sc->ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
2035 usb_set_intfdata(intf, sc);
2036 usb_get_dev(sc->dev);
2037 // usb_get_intf(sc->intf); /* Do we need this? */
2039 snprintf(sc->name, 12, DRV_NAME "(%d.%d)",
2040 sc->dev->bus->busnum, sc->dev->devnum);
2042 /* XXX Verify that we can handle the device (from descriptors) */
2044 ub_get_pipes(sc, sc->dev, intf);
2046 if (device_create_file(&sc->intf->dev, &dev_attr_diag) != 0)
2047 goto err_diag;
2050 * At this point, all USB initialization is done, do upper layer.
2051 * We really hate halfway initialized structures, so from the
2052 * invariants perspective, this ub_dev is fully constructed at
2053 * this point.
2057 * This is needed to clear toggles. It is a problem only if we do
2058 * `rmmod ub && modprobe ub` without disconnects, but we like that.
2060 ub_probe_clear_stall(sc, sc->recv_bulk_pipe);
2061 ub_probe_clear_stall(sc, sc->send_bulk_pipe);
2064 * The way this is used by the startup code is a little specific.
2065 * A SCSI check causes a USB stall. Our common case code sees it
2066 * and clears the check, after which the device is ready for use.
2067 * But if a check was not present, any command other than
2068 * TEST_UNIT_READY ends with a lockup (including REQUEST_SENSE).
2070 * If we neglect to clear the SCSI check, the first real command fails
2071 * (which is the capacity readout). We clear that and retry, but why
2072 * causing spurious retries for no reason.
2074 * Revalidation may start with its own TEST_UNIT_READY, but that one
2075 * has to succeed, so we clear checks with an additional one here.
2076 * In any case it's not our business how revaliadation is implemented.
2078 for (i = 0; i < 3; i++) { /* Retries for benh's key */
2079 if ((rc = ub_sync_tur(sc, NULL)) <= 0) break;
2080 if (rc != 0x6) break;
2081 msleep(10);
2084 nluns = 1;
2085 for (i = 0; i < 3; i++) {
2086 if ((rc = ub_sync_getmaxlun(sc)) < 0) {
2088 * Some devices (i.e. Iomega Zip100) need this --
2089 * apparently the bulk pipes get STALLed when the
2090 * GetMaxLUN request is processed.
2091 * XXX I have a ZIP-100, verify it does this.
2093 if (rc == -EPIPE) {
2094 ub_probe_clear_stall(sc, sc->recv_bulk_pipe);
2095 ub_probe_clear_stall(sc, sc->send_bulk_pipe);
2097 break;
2099 if (rc != 0) {
2100 nluns = rc;
2101 break;
2103 msleep(100);
2106 for (i = 0; i < nluns; i++) {
2107 ub_probe_lun(sc, i);
2109 return 0;
2111 /* device_remove_file(&sc->intf->dev, &dev_attr_diag); */
2112 err_diag:
2113 usb_set_intfdata(intf, NULL);
2114 // usb_put_intf(sc->intf);
2115 usb_put_dev(sc->dev);
2116 kfree(sc);
2117 err_core:
2118 return rc;
2121 static int ub_probe_lun(struct ub_dev *sc, int lnum)
2123 struct ub_lun *lun;
2124 request_queue_t *q;
2125 struct gendisk *disk;
2126 int rc;
2128 rc = -ENOMEM;
2129 if ((lun = kmalloc(sizeof(struct ub_lun), GFP_KERNEL)) == NULL)
2130 goto err_alloc;
2131 memset(lun, 0, sizeof(struct ub_lun));
2132 lun->num = lnum;
2134 rc = -ENOSR;
2135 if ((lun->id = ub_id_get()) == -1)
2136 goto err_id;
2138 lun->udev = sc;
2139 list_add(&lun->link, &sc->luns);
2141 snprintf(lun->name, 16, DRV_NAME "%c(%d.%d.%d)",
2142 lun->id + 'a', sc->dev->bus->busnum, sc->dev->devnum, lun->num);
2144 lun->removable = 1; /* XXX Query this from the device */
2145 lun->changed = 1; /* ub_revalidate clears only */
2146 lun->first_open = 1;
2147 ub_revalidate(sc, lun);
2149 rc = -ENOMEM;
2150 if ((disk = alloc_disk(UB_MINORS_PER_MAJOR)) == NULL)
2151 goto err_diskalloc;
2153 lun->disk = disk;
2154 sprintf(disk->disk_name, DRV_NAME "%c", lun->id + 'a');
2155 sprintf(disk->devfs_name, DEVFS_NAME "/%c", lun->id + 'a');
2156 disk->major = UB_MAJOR;
2157 disk->first_minor = lun->id * UB_MINORS_PER_MAJOR;
2158 disk->fops = &ub_bd_fops;
2159 disk->private_data = lun;
2160 disk->driverfs_dev = &sc->intf->dev; /* XXX Many to one ok? */
2162 rc = -ENOMEM;
2163 if ((q = blk_init_queue(ub_bd_rq_fn, &sc->lock)) == NULL)
2164 goto err_blkqinit;
2166 disk->queue = q;
2168 blk_queue_bounce_limit(q, BLK_BOUNCE_HIGH);
2169 blk_queue_max_hw_segments(q, UB_MAX_REQ_SG);
2170 blk_queue_max_phys_segments(q, UB_MAX_REQ_SG);
2171 blk_queue_segment_boundary(q, 0xffffffff); /* Dubious. */
2172 blk_queue_max_sectors(q, UB_MAX_SECTORS);
2173 blk_queue_hardsect_size(q, lun->capacity.bsize);
2175 q->queuedata = lun;
2177 set_capacity(disk, lun->capacity.nsec);
2178 if (lun->removable)
2179 disk->flags |= GENHD_FL_REMOVABLE;
2181 add_disk(disk);
2183 return 0;
2185 err_blkqinit:
2186 put_disk(disk);
2187 err_diskalloc:
2188 list_del(&lun->link);
2189 ub_id_put(lun->id);
2190 err_id:
2191 kfree(lun);
2192 err_alloc:
2193 return rc;
2196 static void ub_disconnect(struct usb_interface *intf)
2198 struct ub_dev *sc = usb_get_intfdata(intf);
2199 struct list_head *p;
2200 struct ub_lun *lun;
2201 struct gendisk *disk;
2202 unsigned long flags;
2205 * Prevent ub_bd_release from pulling the rug from under us.
2206 * XXX This is starting to look like a kref.
2207 * XXX Why not to take this ref at probe time?
2209 spin_lock_irqsave(&ub_lock, flags);
2210 sc->openc++;
2211 spin_unlock_irqrestore(&ub_lock, flags);
2214 * Fence stall clearnings, operations triggered by unlinkings and so on.
2215 * We do not attempt to unlink any URBs, because we do not trust the
2216 * unlink paths in HC drivers. Also, we get -84 upon disconnect anyway.
2218 atomic_set(&sc->poison, 1);
2221 * Blow away queued commands.
2223 * Actually, this never works, because before we get here
2224 * the HCD terminates outstanding URB(s). It causes our
2225 * SCSI command queue to advance, commands fail to submit,
2226 * and the whole queue drains. So, we just use this code to
2227 * print warnings.
2229 spin_lock_irqsave(&sc->lock, flags);
2231 struct ub_scsi_cmd *cmd;
2232 int cnt = 0;
2233 while ((cmd = ub_cmdq_pop(sc)) != NULL) {
2234 cmd->error = -ENOTCONN;
2235 cmd->state = UB_CMDST_DONE;
2236 ub_cmdtr_state(sc, cmd);
2237 ub_cmdq_pop(sc);
2238 (*cmd->done)(sc, cmd);
2239 cnt++;
2241 if (cnt != 0) {
2242 printk(KERN_WARNING "%s: "
2243 "%d was queued after shutdown\n", sc->name, cnt);
2246 spin_unlock_irqrestore(&sc->lock, flags);
2249 * Unregister the upper layer.
2251 list_for_each (p, &sc->luns) {
2252 lun = list_entry(p, struct ub_lun, link);
2253 disk = lun->disk;
2254 if (disk->flags & GENHD_FL_UP)
2255 del_gendisk(disk);
2257 * I wish I could do:
2258 * set_bit(QUEUE_FLAG_DEAD, &q->queue_flags);
2259 * As it is, we rely on our internal poisoning and let
2260 * the upper levels to spin furiously failing all the I/O.
2265 * Taking a lock on a structure which is about to be freed
2266 * is very nonsensual. Here it is largely a way to do a debug freeze,
2267 * and a bracket which shows where the nonsensual code segment ends.
2269 * Testing for -EINPROGRESS is always a bug, so we are bending
2270 * the rules a little.
2272 spin_lock_irqsave(&sc->lock, flags);
2273 if (sc->work_urb.status == -EINPROGRESS) { /* janitors: ignore */
2274 printk(KERN_WARNING "%s: "
2275 "URB is active after disconnect\n", sc->name);
2277 spin_unlock_irqrestore(&sc->lock, flags);
2280 * There is virtually no chance that other CPU runs times so long
2281 * after ub_urb_complete should have called del_timer, but only if HCD
2282 * didn't forget to deliver a callback on unlink.
2284 del_timer_sync(&sc->work_timer);
2287 * At this point there must be no commands coming from anyone
2288 * and no URBs left in transit.
2291 device_remove_file(&sc->intf->dev, &dev_attr_diag);
2292 usb_set_intfdata(intf, NULL);
2293 // usb_put_intf(sc->intf);
2294 sc->intf = NULL;
2295 usb_put_dev(sc->dev);
2296 sc->dev = NULL;
2298 ub_put(sc);
2301 static struct usb_driver ub_driver = {
2302 .owner = THIS_MODULE,
2303 .name = "ub",
2304 .probe = ub_probe,
2305 .disconnect = ub_disconnect,
2306 .id_table = ub_usb_ids,
2309 static int __init ub_init(void)
2311 int rc;
2313 /* P3 */ printk("ub: sizeof ub_scsi_cmd %zu ub_dev %zu ub_lun %zu\n",
2314 sizeof(struct ub_scsi_cmd), sizeof(struct ub_dev), sizeof(struct ub_lun));
2316 if ((rc = register_blkdev(UB_MAJOR, DRV_NAME)) != 0)
2317 goto err_regblkdev;
2318 devfs_mk_dir(DEVFS_NAME);
2320 if ((rc = usb_register(&ub_driver)) != 0)
2321 goto err_register;
2323 return 0;
2325 err_register:
2326 devfs_remove(DEVFS_NAME);
2327 unregister_blkdev(UB_MAJOR, DRV_NAME);
2328 err_regblkdev:
2329 return rc;
2332 static void __exit ub_exit(void)
2334 usb_deregister(&ub_driver);
2336 devfs_remove(DEVFS_NAME);
2337 unregister_blkdev(UB_MAJOR, DRV_NAME);
2340 module_init(ub_init);
2341 module_exit(ub_exit);
2343 MODULE_LICENSE("GPL");