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 * -- set readonly flag for CDs, set removable flag for CF readers
13 * -- do inquiry and verify we got a disk and not a tape (for LUN mismatch)
14 * -- special case some senses, e.g. 3a/0 -> no media present, reduce retries
15 * -- verify the 13 conditions and do bulk resets
16 * -- kill last_pipe and simply do two-state clearing on both pipes
18 * -- move top_sense and work_bcs into separate allocations (if they survive)
19 * for cache purists and esoteric architectures.
20 * -- Allocate structure for LUN 0 before the first ub_sync_tur, avoid NULL. ?
21 * -- prune comments, they are too volumnous
22 * -- Exterminate P3 printks
24 * -- Redo "benh's retries", perhaps have spin-up code to handle them. V:D=?
25 * -- CLEAR, CLR2STS, CLRRS seem to be ripe for refactoring.
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/usb.h>
30 #include <linux/usb_usual.h>
31 #include <linux/blkdev.h>
32 #include <linux/devfs_fs_kernel.h>
33 #include <linux/timer.h>
34 #include <scsi/scsi.h>
37 #define DEVFS_NAME DRV_NAME
42 * The command state machine is the key model for understanding of this driver.
44 * The general rule is that all transitions are done towards the bottom
45 * of the diagram, thus preventing any loops.
47 * An exception to that is how the STAT state is handled. A counter allows it
48 * to be re-entered along the path marked with [C].
54 * ub_scsi_cmd_start fails ->--------------------------------------\
61 * was -EPIPE -->-------------------------------->! CLEAR ! !
64 * was error -->------------------------------------- ! --------->\
66 * /--<-- cmd->dir == NONE ? ! !
73 * ! was -EPIPE -->--------------->! CLR2STS ! ! !
76 * ! ! was error -->---- ! --------->\
77 * ! was error -->--------------------- ! ------------- ! --------->\
80 * \--->+--------+ ! ! !
81 * ! STAT !<--------------------------/ ! !
84 * [C] was -EPIPE -->-----------\ ! !
86 * +<---- len == 0 ! ! !
88 * ! was error -->--------------------------------------!---------->\
90 * +<---- bad CSW ! ! !
91 * +<---- bad tag ! ! !
97 * \------- ! --------------------[C]--------\ ! !
99 * cmd->error---\ +--------+ ! !
100 * ! +--------------->! SENSE !<----------/ !
101 * STAT_FAIL----/ +--------+ !
104 * \--------------------------------\--------------------->! DONE !
109 * This many LUNs per USB device.
110 * Every one of them takes a host, see UB_MAX_HOSTS.
112 #define UB_MAX_LUNS 9
117 #define UB_PARTS_PER_LUN 8
119 #define UB_MAX_CDB_SIZE 16 /* Corresponds to Bulk */
121 #define UB_SENSE_SIZE 18
126 /* command block wrapper */
127 struct bulk_cb_wrap
{
128 __le32 Signature
; /* contains 'USBC' */
129 u32 Tag
; /* unique per command id */
130 __le32 DataTransferLength
; /* size of data */
131 u8 Flags
; /* direction in bit 0 */
133 u8 Length
; /* of of the CDB */
134 u8 CDB
[UB_MAX_CDB_SIZE
]; /* max command */
137 #define US_BULK_CB_WRAP_LEN 31
138 #define US_BULK_CB_SIGN 0x43425355 /*spells out USBC */
139 #define US_BULK_FLAG_IN 1
140 #define US_BULK_FLAG_OUT 0
142 /* command status wrapper */
143 struct bulk_cs_wrap
{
144 __le32 Signature
; /* should = 'USBS' */
145 u32 Tag
; /* same as original command */
146 __le32 Residue
; /* amount not transferred */
147 u8 Status
; /* see below */
150 #define US_BULK_CS_WRAP_LEN 13
151 #define US_BULK_CS_SIGN 0x53425355 /* spells out 'USBS' */
152 #define US_BULK_STAT_OK 0
153 #define US_BULK_STAT_FAIL 1
154 #define US_BULK_STAT_PHASE 2
156 /* bulk-only class specific requests */
157 #define US_BULK_RESET_REQUEST 0xff
158 #define US_BULK_GET_MAX_LUN 0xfe
164 #define UB_MAX_REQ_SG 9 /* cdrecord requires 32KB and maybe a header */
165 #define UB_MAX_SECTORS 64
168 * A second is more than enough for a 32K transfer (UB_MAX_SECTORS)
169 * even if a webcam hogs the bus, but some devices need time to spin up.
171 #define UB_URB_TIMEOUT (HZ*2)
172 #define UB_DATA_TIMEOUT (HZ*5) /* ZIP does spin-ups in the data phase */
173 #define UB_STAT_TIMEOUT (HZ*5) /* Same spinups and eject for a dataless cmd. */
174 #define UB_CTRL_TIMEOUT (HZ/2) /* 500ms ought to be enough to clear a stall */
177 * An instance of a SCSI command in transit.
179 #define UB_DIR_NONE 0
180 #define UB_DIR_READ 1
181 #define UB_DIR_ILLEGAL2 2
182 #define UB_DIR_WRITE 3
184 #define UB_DIR_CHAR(c) (((c)==UB_DIR_WRITE)? 'w': \
185 (((c)==UB_DIR_READ)? 'r': 'n'))
187 enum ub_scsi_cmd_state
{
188 UB_CMDST_INIT
, /* Initial state */
189 UB_CMDST_CMD
, /* Command submitted */
190 UB_CMDST_DATA
, /* Data phase */
191 UB_CMDST_CLR2STS
, /* Clearing before requesting status */
192 UB_CMDST_STAT
, /* Status phase */
193 UB_CMDST_CLEAR
, /* Clearing a stall (halt, actually) */
194 UB_CMDST_CLRRS
, /* Clearing before retrying status */
195 UB_CMDST_SENSE
, /* Sending Request Sense */
196 UB_CMDST_DONE
/* Final state */
199 static char *ub_scsi_cmd_stname
[] = {
212 unsigned char cdb
[UB_MAX_CDB_SIZE
];
213 unsigned char cdb_len
;
215 unsigned char dir
; /* 0 - none, 1 - read, 3 - write. */
216 unsigned char trace_index
;
217 enum ub_scsi_cmd_state state
;
219 struct ub_scsi_cmd
*next
;
221 int error
; /* Return code - valid upon done */
222 unsigned int act_len
; /* Return size */
223 unsigned char key
, asc
, ascq
; /* May be valid if error==-EIO */
225 int stat_count
; /* Retries getting status. */
227 unsigned int len
; /* Requested length */
228 unsigned int current_sg
;
229 unsigned int nsg
; /* sgv[nsg] */
230 struct scatterlist sgv
[UB_MAX_REQ_SG
];
233 void (*done
)(struct ub_dev
*, struct ub_scsi_cmd
*);
239 unsigned int current_try
;
240 unsigned int nsg
; /* sgv[nsg] */
241 struct scatterlist sgv
[UB_MAX_REQ_SG
];
247 unsigned long nsec
; /* Linux size - 512 byte sectors */
248 unsigned int bsize
; /* Linux hardsect_size */
249 unsigned int bshift
; /* Shift between 512 and hard sects */
253 * The SCSI command tracing structure.
256 #define SCMD_ST_HIST_SZ 8
257 #define SCMD_TRACE_SZ 63 /* Less than 4KB of 61-byte lines */
259 struct ub_scsi_cmd_trace
{
262 unsigned int req_size
, act_size
;
265 unsigned char key
, asc
, ascq
;
266 char st_hst
[SCMD_ST_HIST_SZ
];
269 struct ub_scsi_trace
{
271 struct ub_scsi_cmd_trace vec
[SCMD_TRACE_SZ
];
275 * This is a direct take-off from linux/include/completion.h
276 * The difference is that I do not wait on this thing, just poll.
277 * When I want to wait (ub_probe), I just use the stock completion.
279 * Note that INIT_COMPLETION takes no lock. It is correct. But why
280 * in the bloody hell that thing takes struct instead of pointer to struct
281 * is quite beyond me. I just copied it from the stock completion.
283 struct ub_completion
{
288 static inline void ub_init_completion(struct ub_completion
*x
)
291 spin_lock_init(&x
->lock
);
294 #define UB_INIT_COMPLETION(x) ((x).done = 0)
296 static void ub_complete(struct ub_completion
*x
)
300 spin_lock_irqsave(&x
->lock
, flags
);
302 spin_unlock_irqrestore(&x
->lock
, flags
);
305 static int ub_is_completed(struct ub_completion
*x
)
310 spin_lock_irqsave(&x
->lock
, flags
);
312 spin_unlock_irqrestore(&x
->lock
, flags
);
318 struct ub_scsi_cmd_queue
{
320 struct ub_scsi_cmd
*head
, *tail
;
324 * The block device instance (one per LUN).
328 struct list_head link
;
329 struct gendisk
*disk
;
330 int id
; /* Host index */
331 int num
; /* LUN number */
334 int changed
; /* Media was changed */
337 int first_open
; /* Kludge. See ub_bd_open. */
339 struct ub_request urq
;
341 /* Use Ingo's mempool if or when we have more than one command. */
343 * Currently we never need more than one command for the whole device.
344 * However, giving every LUN a command is a cheap and automatic way
345 * to enforce fairness between them.
348 struct ub_scsi_cmd cmdv
[1];
350 struct ub_capacity capacity
;
354 * The USB device instance.
358 atomic_t poison
; /* The USB device is disconnected */
359 int openc
; /* protected by ub_lock! */
360 /* kref is too implicit for our taste */
361 int reset
; /* Reset is running */
364 struct usb_device
*dev
;
365 struct usb_interface
*intf
;
367 struct list_head luns
;
369 unsigned int send_bulk_pipe
; /* cached pipe values */
370 unsigned int recv_bulk_pipe
;
371 unsigned int send_ctrl_pipe
;
372 unsigned int recv_ctrl_pipe
;
374 struct tasklet_struct tasklet
;
376 struct ub_scsi_cmd_queue cmd_queue
;
377 struct ub_scsi_cmd top_rqs_cmd
; /* REQUEST SENSE */
378 unsigned char top_sense
[UB_SENSE_SIZE
];
380 struct ub_completion work_done
;
382 struct timer_list work_timer
;
383 int last_pipe
; /* What might need clearing */
384 __le32 signature
; /* Learned signature */
385 struct bulk_cb_wrap work_bcb
;
386 struct bulk_cs_wrap work_bcs
;
387 struct usb_ctrlrequest work_cr
;
389 struct work_struct reset_work
;
390 wait_queue_head_t reset_wait
;
393 struct ub_scsi_trace tr
;
398 static void ub_cleanup(struct ub_dev
*sc
);
399 static int ub_request_fn_1(struct ub_lun
*lun
, struct request
*rq
);
400 static void ub_cmd_build_block(struct ub_dev
*sc
, struct ub_lun
*lun
,
401 struct ub_scsi_cmd
*cmd
, struct ub_request
*urq
);
402 static void ub_cmd_build_packet(struct ub_dev
*sc
, struct ub_lun
*lun
,
403 struct ub_scsi_cmd
*cmd
, struct ub_request
*urq
);
404 static void ub_rw_cmd_done(struct ub_dev
*sc
, struct ub_scsi_cmd
*cmd
);
405 static void ub_end_rq(struct request
*rq
, int uptodate
);
406 static int ub_rw_cmd_retry(struct ub_dev
*sc
, struct ub_lun
*lun
,
407 struct ub_request
*urq
, struct ub_scsi_cmd
*cmd
);
408 static int ub_submit_scsi(struct ub_dev
*sc
, struct ub_scsi_cmd
*cmd
);
409 static void ub_urb_complete(struct urb
*urb
, struct pt_regs
*pt
);
410 static void ub_scsi_action(unsigned long _dev
);
411 static void ub_scsi_dispatch(struct ub_dev
*sc
);
412 static void ub_scsi_urb_compl(struct ub_dev
*sc
, struct ub_scsi_cmd
*cmd
);
413 static void ub_data_start(struct ub_dev
*sc
, struct ub_scsi_cmd
*cmd
);
414 static void ub_state_done(struct ub_dev
*sc
, struct ub_scsi_cmd
*cmd
, int rc
);
415 static int __ub_state_stat(struct ub_dev
*sc
, struct ub_scsi_cmd
*cmd
);
416 static void ub_state_stat(struct ub_dev
*sc
, struct ub_scsi_cmd
*cmd
);
417 static void ub_state_stat_counted(struct ub_dev
*sc
, struct ub_scsi_cmd
*cmd
);
418 static void ub_state_sense(struct ub_dev
*sc
, struct ub_scsi_cmd
*cmd
);
419 static int ub_submit_clear_stall(struct ub_dev
*sc
, struct ub_scsi_cmd
*cmd
,
421 static void ub_top_sense_done(struct ub_dev
*sc
, struct ub_scsi_cmd
*scmd
);
422 static void ub_reset_enter(struct ub_dev
*sc
, int try);
423 static void ub_reset_task(void *arg
);
424 static int ub_sync_tur(struct ub_dev
*sc
, struct ub_lun
*lun
);
425 static int ub_sync_read_cap(struct ub_dev
*sc
, struct ub_lun
*lun
,
426 struct ub_capacity
*ret
);
427 static int ub_sync_reset(struct ub_dev
*sc
);
428 static int ub_probe_clear_stall(struct ub_dev
*sc
, int stalled_pipe
);
429 static int ub_probe_lun(struct ub_dev
*sc
, int lnum
);
433 #ifdef CONFIG_USB_LIBUSUAL
435 #define ub_usb_ids storage_usb_ids
438 static struct usb_device_id ub_usb_ids
[] = {
439 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE
, US_SC_SCSI
, US_PR_BULK
) },
443 MODULE_DEVICE_TABLE(usb
, ub_usb_ids
);
444 #endif /* CONFIG_USB_LIBUSUAL */
447 * Find me a way to identify "next free minor" for add_disk(),
448 * and the array disappears the next day. However, the number of
449 * hosts has something to do with the naming and /proc/partitions.
450 * This has to be thought out in detail before changing.
451 * If UB_MAX_HOST was 1000, we'd use a bitmap. Or a better data structure.
453 #define UB_MAX_HOSTS 26
454 static char ub_hostv
[UB_MAX_HOSTS
];
456 #define UB_QLOCK_NUM 5
457 static spinlock_t ub_qlockv
[UB_QLOCK_NUM
];
458 static int ub_qlock_next
= 0;
460 static DEFINE_SPINLOCK(ub_lock
); /* Locks globals and ->openc */
463 * The SCSI command tracing procedures.
466 static void ub_cmdtr_new(struct ub_dev
*sc
, struct ub_scsi_cmd
*cmd
)
469 struct ub_scsi_cmd_trace
*t
;
471 if ((n
= sc
->tr
.cur
+ 1) == SCMD_TRACE_SZ
) n
= 0;
474 memset(t
, 0, sizeof(struct ub_scsi_cmd_trace
));
478 t
->req_size
= cmd
->len
;
479 t
->st_hst
[0] = cmd
->state
;
482 cmd
->trace_index
= n
;
485 static void ub_cmdtr_state(struct ub_dev
*sc
, struct ub_scsi_cmd
*cmd
)
488 struct ub_scsi_cmd_trace
*t
;
490 t
= &sc
->tr
.vec
[cmd
->trace_index
];
491 if (t
->tag
== cmd
->tag
) {
492 if ((n
= t
->hcur
+ 1) == SCMD_ST_HIST_SZ
) n
= 0;
493 t
->st_hst
[n
] = cmd
->state
;
498 static void ub_cmdtr_act_len(struct ub_dev
*sc
, struct ub_scsi_cmd
*cmd
)
500 struct ub_scsi_cmd_trace
*t
;
502 t
= &sc
->tr
.vec
[cmd
->trace_index
];
503 if (t
->tag
== cmd
->tag
)
504 t
->act_size
= cmd
->act_len
;
507 static void ub_cmdtr_sense(struct ub_dev
*sc
, struct ub_scsi_cmd
*cmd
,
508 unsigned char *sense
)
510 struct ub_scsi_cmd_trace
*t
;
512 t
= &sc
->tr
.vec
[cmd
->trace_index
];
513 if (t
->tag
== cmd
->tag
) {
514 t
->key
= sense
[2] & 0x0F;
520 static ssize_t
ub_diag_show(struct device
*dev
, struct device_attribute
*attr
,
523 struct usb_interface
*intf
;
531 struct ub_scsi_cmd_trace
*t
;
533 intf
= to_usb_interface(dev
);
534 sc
= usb_get_intfdata(intf
);
539 spin_lock_irqsave(sc
->lock
, flags
);
541 cnt
+= sprintf(page
+ cnt
,
542 "poison %d reset %d\n",
543 atomic_read(&sc
->poison
), sc
->reset
);
544 cnt
+= sprintf(page
+ cnt
,
546 sc
->cmd_queue
.qlen
, sc
->cmd_queue
.qmax
);
547 cnt
+= sprintf(page
+ cnt
,
548 "sg %d %d %d %d %d .. %d\n",
556 list_for_each (p
, &sc
->luns
) {
557 lun
= list_entry(p
, struct ub_lun
, link
);
558 cnt
+= sprintf(page
+ cnt
,
559 "lun %u changed %d removable %d readonly %d\n",
560 lun
->num
, lun
->changed
, lun
->removable
, lun
->readonly
);
563 if ((nc
= sc
->tr
.cur
+ 1) == SCMD_TRACE_SZ
) nc
= 0;
564 for (j
= 0; j
< SCMD_TRACE_SZ
; j
++) {
567 cnt
+= sprintf(page
+ cnt
, "%08x %02x", t
->tag
, t
->op
);
568 if (t
->op
== REQUEST_SENSE
) {
569 cnt
+= sprintf(page
+ cnt
, " [sense %x %02x %02x]",
570 t
->key
, t
->asc
, t
->ascq
);
572 cnt
+= sprintf(page
+ cnt
, " %c", UB_DIR_CHAR(t
->dir
));
573 cnt
+= sprintf(page
+ cnt
, " [%5d %5d]",
574 t
->req_size
, t
->act_size
);
576 if ((nh
= t
->hcur
+ 1) == SCMD_ST_HIST_SZ
) nh
= 0;
577 for (i
= 0; i
< SCMD_ST_HIST_SZ
; i
++) {
578 cnt
+= sprintf(page
+ cnt
, " %s",
579 ub_scsi_cmd_stname
[(int)t
->st_hst
[nh
]]);
580 if (++nh
== SCMD_ST_HIST_SZ
) nh
= 0;
582 cnt
+= sprintf(page
+ cnt
, "\n");
584 if (++nc
== SCMD_TRACE_SZ
) nc
= 0;
587 spin_unlock_irqrestore(sc
->lock
, flags
);
591 static DEVICE_ATTR(diag
, S_IRUGO
, ub_diag_show
, NULL
); /* N.B. World readable */
596 * This also stores the host for indexing by minor, which is somewhat dirty.
598 static int ub_id_get(void)
603 spin_lock_irqsave(&ub_lock
, flags
);
604 for (i
= 0; i
< UB_MAX_HOSTS
; i
++) {
605 if (ub_hostv
[i
] == 0) {
607 spin_unlock_irqrestore(&ub_lock
, flags
);
611 spin_unlock_irqrestore(&ub_lock
, flags
);
615 static void ub_id_put(int id
)
619 if (id
< 0 || id
>= UB_MAX_HOSTS
) {
620 printk(KERN_ERR DRV_NAME
": bad host ID %d\n", id
);
624 spin_lock_irqsave(&ub_lock
, flags
);
625 if (ub_hostv
[id
] == 0) {
626 spin_unlock_irqrestore(&ub_lock
, flags
);
627 printk(KERN_ERR DRV_NAME
": freeing free host ID %d\n", id
);
631 spin_unlock_irqrestore(&ub_lock
, flags
);
635 * This is necessitated by the fact that blk_cleanup_queue does not
636 * necesserily destroy the queue. Instead, it may merely decrease q->refcnt.
637 * Since our blk_init_queue() passes a spinlock common with ub_dev,
638 * we have life time issues when ub_cleanup frees ub_dev.
640 static spinlock_t
*ub_next_lock(void)
645 spin_lock_irqsave(&ub_lock
, flags
);
646 ret
= &ub_qlockv
[ub_qlock_next
];
647 ub_qlock_next
= (ub_qlock_next
+ 1) % UB_QLOCK_NUM
;
648 spin_unlock_irqrestore(&ub_lock
, flags
);
653 * Downcount for deallocation. This rides on two assumptions:
654 * - once something is poisoned, its refcount cannot grow
655 * - opens cannot happen at this time (del_gendisk was done)
656 * If the above is true, we can drop the lock, which we need for
657 * blk_cleanup_queue(): the silly thing may attempt to sleep.
658 * [Actually, it never needs to sleep for us, but it calls might_sleep()]
660 static void ub_put(struct ub_dev
*sc
)
664 spin_lock_irqsave(&ub_lock
, flags
);
666 if (sc
->openc
== 0 && atomic_read(&sc
->poison
)) {
667 spin_unlock_irqrestore(&ub_lock
, flags
);
670 spin_unlock_irqrestore(&ub_lock
, flags
);
675 * Final cleanup and deallocation.
677 static void ub_cleanup(struct ub_dev
*sc
)
683 while (!list_empty(&sc
->luns
)) {
685 lun
= list_entry(p
, struct ub_lun
, link
);
688 /* I don't think queue can be NULL. But... Stolen from sx8.c */
689 if ((q
= lun
->disk
->queue
) != NULL
)
690 blk_cleanup_queue(q
);
692 * If we zero disk->private_data BEFORE put_disk, we have
693 * to check for NULL all over the place in open, release,
694 * check_media and revalidate, because the block level
695 * semaphore is well inside the put_disk.
696 * But we cannot zero after the call, because *disk is gone.
697 * The sd.c is blatantly racy in this area.
699 /* disk->private_data = NULL; */
711 * The "command allocator".
713 static struct ub_scsi_cmd
*ub_get_cmd(struct ub_lun
*lun
)
715 struct ub_scsi_cmd
*ret
;
724 static void ub_put_cmd(struct ub_lun
*lun
, struct ub_scsi_cmd
*cmd
)
726 if (cmd
!= &lun
->cmdv
[0]) {
727 printk(KERN_WARNING
"%s: releasing a foreign cmd %p\n",
732 printk(KERN_WARNING
"%s: releasing a free cmd\n", lun
->name
);
741 static void ub_cmdq_add(struct ub_dev
*sc
, struct ub_scsi_cmd
*cmd
)
743 struct ub_scsi_cmd_queue
*t
= &sc
->cmd_queue
;
745 if (t
->qlen
++ == 0) {
753 if (t
->qlen
> t
->qmax
)
757 static void ub_cmdq_insert(struct ub_dev
*sc
, struct ub_scsi_cmd
*cmd
)
759 struct ub_scsi_cmd_queue
*t
= &sc
->cmd_queue
;
761 if (t
->qlen
++ == 0) {
769 if (t
->qlen
> t
->qmax
)
773 static struct ub_scsi_cmd
*ub_cmdq_pop(struct ub_dev
*sc
)
775 struct ub_scsi_cmd_queue
*t
= &sc
->cmd_queue
;
776 struct ub_scsi_cmd
*cmd
;
788 #define ub_cmdq_peek(sc) ((sc)->cmd_queue.head)
791 * The request function is our main entry point
794 static void ub_request_fn(request_queue_t
*q
)
796 struct ub_lun
*lun
= q
->queuedata
;
799 while ((rq
= elv_next_request(q
)) != NULL
) {
800 if (ub_request_fn_1(lun
, rq
) != 0) {
807 static int ub_request_fn_1(struct ub_lun
*lun
, struct request
*rq
)
809 struct ub_dev
*sc
= lun
->udev
;
810 struct ub_scsi_cmd
*cmd
;
811 struct ub_request
*urq
;
814 if (atomic_read(&sc
->poison
) || lun
->changed
) {
815 blkdev_dequeue_request(rq
);
820 if (lun
->urq
.rq
!= NULL
)
822 if ((cmd
= ub_get_cmd(lun
)) == NULL
)
824 memset(cmd
, 0, sizeof(struct ub_scsi_cmd
));
826 blkdev_dequeue_request(rq
);
829 memset(urq
, 0, sizeof(struct ub_request
));
833 * get scatterlist from block layer
835 n_elem
= blk_rq_map_sg(lun
->disk
->queue
, rq
, &urq
->sgv
[0]);
837 printk(KERN_INFO
"%s: failed request map (%d)\n",
838 lun
->name
, n_elem
); /* P3 */
841 if (n_elem
> UB_MAX_REQ_SG
) { /* Paranoia */
842 printk(KERN_WARNING
"%s: request with %d segments\n",
847 sc
->sg_stat
[n_elem
< 5 ? n_elem
: 5]++;
849 if (blk_pc_request(rq
)) {
850 ub_cmd_build_packet(sc
, lun
, cmd
, urq
);
852 ub_cmd_build_block(sc
, lun
, cmd
, urq
);
854 cmd
->state
= UB_CMDST_INIT
;
856 cmd
->done
= ub_rw_cmd_done
;
859 cmd
->tag
= sc
->tagcnt
++;
860 if (ub_submit_scsi(sc
, cmd
) != 0)
866 ub_put_cmd(lun
, cmd
);
871 static void ub_cmd_build_block(struct ub_dev
*sc
, struct ub_lun
*lun
,
872 struct ub_scsi_cmd
*cmd
, struct ub_request
*urq
)
874 struct request
*rq
= urq
->rq
;
875 unsigned int block
, nblks
;
877 if (rq_data_dir(rq
) == WRITE
)
878 cmd
->dir
= UB_DIR_WRITE
;
880 cmd
->dir
= UB_DIR_READ
;
883 memcpy(cmd
->sgv
, urq
->sgv
, sizeof(struct scatterlist
) * cmd
->nsg
);
888 * The call to blk_queue_hardsect_size() guarantees that request
889 * is aligned, but it is given in terms of 512 byte units, always.
891 block
= rq
->sector
>> lun
->capacity
.bshift
;
892 nblks
= rq
->nr_sectors
>> lun
->capacity
.bshift
;
894 cmd
->cdb
[0] = (cmd
->dir
== UB_DIR_READ
)? READ_10
: WRITE_10
;
895 /* 10-byte uses 4 bytes of LBA: 2147483648KB, 2097152MB, 2048GB */
896 cmd
->cdb
[2] = block
>> 24;
897 cmd
->cdb
[3] = block
>> 16;
898 cmd
->cdb
[4] = block
>> 8;
900 cmd
->cdb
[7] = nblks
>> 8;
904 cmd
->len
= rq
->nr_sectors
* 512;
907 static void ub_cmd_build_packet(struct ub_dev
*sc
, struct ub_lun
*lun
,
908 struct ub_scsi_cmd
*cmd
, struct ub_request
*urq
)
910 struct request
*rq
= urq
->rq
;
912 if (rq
->data_len
== 0) {
913 cmd
->dir
= UB_DIR_NONE
;
915 if (rq_data_dir(rq
) == WRITE
)
916 cmd
->dir
= UB_DIR_WRITE
;
918 cmd
->dir
= UB_DIR_READ
;
922 memcpy(cmd
->sgv
, urq
->sgv
, sizeof(struct scatterlist
) * cmd
->nsg
);
924 memcpy(&cmd
->cdb
, rq
->cmd
, rq
->cmd_len
);
925 cmd
->cdb_len
= rq
->cmd_len
;
927 cmd
->len
= rq
->data_len
;
930 static void ub_rw_cmd_done(struct ub_dev
*sc
, struct ub_scsi_cmd
*cmd
)
932 struct ub_lun
*lun
= cmd
->lun
;
933 struct ub_request
*urq
= cmd
->back
;
939 if (cmd
->error
== 0) {
942 if (blk_pc_request(rq
)) {
943 if (cmd
->act_len
>= rq
->data_len
)
946 rq
->data_len
-= cmd
->act_len
;
951 if (blk_pc_request(rq
)) {
952 /* UB_SENSE_SIZE is smaller than SCSI_SENSE_BUFFERSIZE */
953 memcpy(rq
->sense
, sc
->top_sense
, UB_SENSE_SIZE
);
954 rq
->sense_len
= UB_SENSE_SIZE
;
955 if (sc
->top_sense
[0] != 0)
956 rq
->errors
= SAM_STAT_CHECK_CONDITION
;
958 rq
->errors
= DID_ERROR
<< 16;
960 if (cmd
->error
== -EIO
) {
961 if (ub_rw_cmd_retry(sc
, lun
, urq
, cmd
) == 0)
969 ub_put_cmd(lun
, cmd
);
970 ub_end_rq(rq
, uptodate
);
971 blk_start_queue(lun
->disk
->queue
);
974 static void ub_end_rq(struct request
*rq
, int uptodate
)
976 end_that_request_first(rq
, uptodate
, rq
->hard_nr_sectors
);
977 end_that_request_last(rq
, uptodate
);
980 static int ub_rw_cmd_retry(struct ub_dev
*sc
, struct ub_lun
*lun
,
981 struct ub_request
*urq
, struct ub_scsi_cmd
*cmd
)
984 if (atomic_read(&sc
->poison
))
987 ub_reset_enter(sc
, urq
->current_try
);
989 if (urq
->current_try
>= 3)
992 /* P3 */ printk("%s: dir %c len/act %d/%d "
993 "[sense %x %02x %02x] retry %d\n",
994 sc
->name
, UB_DIR_CHAR(cmd
->dir
), cmd
->len
, cmd
->act_len
,
995 cmd
->key
, cmd
->asc
, cmd
->ascq
, urq
->current_try
);
997 memset(cmd
, 0, sizeof(struct ub_scsi_cmd
));
998 ub_cmd_build_block(sc
, lun
, cmd
, urq
);
1000 cmd
->state
= UB_CMDST_INIT
;
1002 cmd
->done
= ub_rw_cmd_done
;
1005 cmd
->tag
= sc
->tagcnt
++;
1007 #if 0 /* Wasteful */
1008 return ub_submit_scsi(sc
, cmd
);
1010 ub_cmdq_add(sc
, cmd
);
1016 * Submit a regular SCSI operation (not an auto-sense).
1018 * The Iron Law of Good Submit Routine is:
1019 * Zero return - callback is done, Nonzero return - callback is not done.
1022 * Host is assumed locked.
1024 static int ub_submit_scsi(struct ub_dev
*sc
, struct ub_scsi_cmd
*cmd
)
1027 if (cmd
->state
!= UB_CMDST_INIT
||
1028 (cmd
->dir
!= UB_DIR_NONE
&& cmd
->len
== 0)) {
1032 ub_cmdq_add(sc
, cmd
);
1034 * We can call ub_scsi_dispatch(sc) right away here, but it's a little
1035 * safer to jump to a tasklet, in case upper layers do something silly.
1037 tasklet_schedule(&sc
->tasklet
);
1042 * Submit the first URB for the queued command.
1043 * This function does not deal with queueing in any way.
1045 static int ub_scsi_cmd_start(struct ub_dev
*sc
, struct ub_scsi_cmd
*cmd
)
1047 struct bulk_cb_wrap
*bcb
;
1050 bcb
= &sc
->work_bcb
;
1053 * ``If the allocation length is eighteen or greater, and a device
1054 * server returns less than eithteen bytes of data, the application
1055 * client should assume that the bytes not transferred would have been
1056 * zeroes had the device server returned those bytes.''
1058 * We zero sense for all commands so that when a packet request
1059 * fails it does not return a stale sense.
1061 memset(&sc
->top_sense
, 0, UB_SENSE_SIZE
);
1063 /* set up the command wrapper */
1064 bcb
->Signature
= cpu_to_le32(US_BULK_CB_SIGN
);
1065 bcb
->Tag
= cmd
->tag
; /* Endianness is not important */
1066 bcb
->DataTransferLength
= cpu_to_le32(cmd
->len
);
1067 bcb
->Flags
= (cmd
->dir
== UB_DIR_READ
) ? 0x80 : 0;
1068 bcb
->Lun
= (cmd
->lun
!= NULL
) ? cmd
->lun
->num
: 0;
1069 bcb
->Length
= cmd
->cdb_len
;
1071 /* copy the command payload */
1072 memcpy(bcb
->CDB
, cmd
->cdb
, UB_MAX_CDB_SIZE
);
1074 UB_INIT_COMPLETION(sc
->work_done
);
1076 sc
->last_pipe
= sc
->send_bulk_pipe
;
1077 usb_fill_bulk_urb(&sc
->work_urb
, sc
->dev
, sc
->send_bulk_pipe
,
1078 bcb
, US_BULK_CB_WRAP_LEN
, ub_urb_complete
, sc
);
1080 /* Fill what we shouldn't be filling, because usb-storage did so. */
1081 sc
->work_urb
.actual_length
= 0;
1082 sc
->work_urb
.error_count
= 0;
1083 sc
->work_urb
.status
= 0;
1085 if ((rc
= usb_submit_urb(&sc
->work_urb
, GFP_ATOMIC
)) != 0) {
1086 /* XXX Clear stalls */
1087 ub_complete(&sc
->work_done
);
1091 sc
->work_timer
.expires
= jiffies
+ UB_URB_TIMEOUT
;
1092 add_timer(&sc
->work_timer
);
1094 cmd
->state
= UB_CMDST_CMD
;
1095 ub_cmdtr_state(sc
, cmd
);
1102 static void ub_urb_timeout(unsigned long arg
)
1104 struct ub_dev
*sc
= (struct ub_dev
*) arg
;
1105 unsigned long flags
;
1107 spin_lock_irqsave(sc
->lock
, flags
);
1108 if (!ub_is_completed(&sc
->work_done
))
1109 usb_unlink_urb(&sc
->work_urb
);
1110 spin_unlock_irqrestore(sc
->lock
, flags
);
1114 * Completion routine for the work URB.
1116 * This can be called directly from usb_submit_urb (while we have
1117 * the sc->lock taken) and from an interrupt (while we do NOT have
1118 * the sc->lock taken). Therefore, bounce this off to a tasklet.
1120 static void ub_urb_complete(struct urb
*urb
, struct pt_regs
*pt
)
1122 struct ub_dev
*sc
= urb
->context
;
1124 ub_complete(&sc
->work_done
);
1125 tasklet_schedule(&sc
->tasklet
);
1128 static void ub_scsi_action(unsigned long _dev
)
1130 struct ub_dev
*sc
= (struct ub_dev
*) _dev
;
1131 unsigned long flags
;
1133 spin_lock_irqsave(sc
->lock
, flags
);
1134 ub_scsi_dispatch(sc
);
1135 spin_unlock_irqrestore(sc
->lock
, flags
);
1138 static void ub_scsi_dispatch(struct ub_dev
*sc
)
1140 struct ub_scsi_cmd
*cmd
;
1143 while (!sc
->reset
&& (cmd
= ub_cmdq_peek(sc
)) != NULL
) {
1144 if (cmd
->state
== UB_CMDST_DONE
) {
1146 (*cmd
->done
)(sc
, cmd
);
1147 } else if (cmd
->state
== UB_CMDST_INIT
) {
1148 ub_cmdtr_new(sc
, cmd
);
1149 if ((rc
= ub_scsi_cmd_start(sc
, cmd
)) == 0)
1152 cmd
->state
= UB_CMDST_DONE
;
1153 ub_cmdtr_state(sc
, cmd
);
1155 if (!ub_is_completed(&sc
->work_done
))
1157 del_timer(&sc
->work_timer
);
1158 ub_scsi_urb_compl(sc
, cmd
);
1163 static void ub_scsi_urb_compl(struct ub_dev
*sc
, struct ub_scsi_cmd
*cmd
)
1165 struct urb
*urb
= &sc
->work_urb
;
1166 struct bulk_cs_wrap
*bcs
;
1170 if (atomic_read(&sc
->poison
)) {
1171 ub_state_done(sc
, cmd
, -ENODEV
);
1175 if (cmd
->state
== UB_CMDST_CLEAR
) {
1176 if (urb
->status
== -EPIPE
) {
1178 * STALL while clearning STALL.
1179 * The control pipe clears itself - nothing to do.
1181 printk(KERN_NOTICE
"%s: stall on control pipe\n",
1187 * We ignore the result for the halt clear.
1190 /* reset the endpoint toggle */
1191 usb_settoggle(sc
->dev
, usb_pipeendpoint(sc
->last_pipe
),
1192 usb_pipeout(sc
->last_pipe
), 0);
1194 ub_state_sense(sc
, cmd
);
1196 } else if (cmd
->state
== UB_CMDST_CLR2STS
) {
1197 if (urb
->status
== -EPIPE
) {
1198 printk(KERN_NOTICE
"%s: stall on control pipe\n",
1204 * We ignore the result for the halt clear.
1207 /* reset the endpoint toggle */
1208 usb_settoggle(sc
->dev
, usb_pipeendpoint(sc
->last_pipe
),
1209 usb_pipeout(sc
->last_pipe
), 0);
1211 ub_state_stat(sc
, cmd
);
1213 } else if (cmd
->state
== UB_CMDST_CLRRS
) {
1214 if (urb
->status
== -EPIPE
) {
1215 printk(KERN_NOTICE
"%s: stall on control pipe\n",
1221 * We ignore the result for the halt clear.
1224 /* reset the endpoint toggle */
1225 usb_settoggle(sc
->dev
, usb_pipeendpoint(sc
->last_pipe
),
1226 usb_pipeout(sc
->last_pipe
), 0);
1228 ub_state_stat_counted(sc
, cmd
);
1230 } else if (cmd
->state
== UB_CMDST_CMD
) {
1231 switch (urb
->status
) {
1237 rc
= ub_submit_clear_stall(sc
, cmd
, sc
->last_pipe
);
1239 printk(KERN_NOTICE
"%s: "
1240 "unable to submit clear (%d)\n",
1243 * This is typically ENOMEM or some other such shit.
1244 * Retrying is pointless. Just do Bad End on it...
1246 ub_state_done(sc
, cmd
, rc
);
1249 cmd
->state
= UB_CMDST_CLEAR
;
1250 ub_cmdtr_state(sc
, cmd
);
1252 case -ESHUTDOWN
: /* unplug */
1253 case -EILSEQ
: /* unplug timeout on uhci */
1254 ub_state_done(sc
, cmd
, -ENODEV
);
1259 if (urb
->actual_length
!= US_BULK_CB_WRAP_LEN
) {
1263 if (cmd
->dir
== UB_DIR_NONE
|| cmd
->nsg
< 1) {
1264 ub_state_stat(sc
, cmd
);
1268 // udelay(125); // usb-storage has this
1269 ub_data_start(sc
, cmd
);
1271 } else if (cmd
->state
== UB_CMDST_DATA
) {
1272 if (urb
->status
== -EPIPE
) {
1273 rc
= ub_submit_clear_stall(sc
, cmd
, sc
->last_pipe
);
1275 printk(KERN_NOTICE
"%s: "
1276 "unable to submit clear (%d)\n",
1278 ub_state_done(sc
, cmd
, rc
);
1281 cmd
->state
= UB_CMDST_CLR2STS
;
1282 ub_cmdtr_state(sc
, cmd
);
1285 if (urb
->status
== -EOVERFLOW
) {
1287 * A babble? Failure, but we must transfer CSW now.
1289 cmd
->error
= -EOVERFLOW
; /* A cheap trick... */
1290 ub_state_stat(sc
, cmd
);
1294 if (cmd
->dir
== UB_DIR_WRITE
) {
1296 * Do not continue writes in case of a failure.
1297 * Doing so would cause sectors to be mixed up,
1298 * which is worse than sectors lost.
1300 * We must try to read the CSW, or many devices
1303 len
= urb
->actual_length
;
1304 if (urb
->status
!= 0 ||
1305 len
!= cmd
->sgv
[cmd
->current_sg
].length
) {
1306 cmd
->act_len
+= len
;
1307 ub_cmdtr_act_len(sc
, cmd
);
1310 ub_state_stat(sc
, cmd
);
1316 * If an error occurs on read, we record it, and
1317 * continue to fetch data in order to avoid bubble.
1319 * As a small shortcut, we stop if we detect that
1320 * a CSW mixed into data.
1322 if (urb
->status
!= 0)
1325 len
= urb
->actual_length
;
1326 if (urb
->status
!= 0 ||
1327 len
!= cmd
->sgv
[cmd
->current_sg
].length
) {
1328 if ((len
& 0x1FF) == US_BULK_CS_WRAP_LEN
)
1333 cmd
->act_len
+= urb
->actual_length
;
1334 ub_cmdtr_act_len(sc
, cmd
);
1336 if (++cmd
->current_sg
< cmd
->nsg
) {
1337 ub_data_start(sc
, cmd
);
1340 ub_state_stat(sc
, cmd
);
1342 } else if (cmd
->state
== UB_CMDST_STAT
) {
1343 if (urb
->status
== -EPIPE
) {
1344 rc
= ub_submit_clear_stall(sc
, cmd
, sc
->last_pipe
);
1346 printk(KERN_NOTICE
"%s: "
1347 "unable to submit clear (%d)\n",
1349 ub_state_done(sc
, cmd
, rc
);
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
);
1364 /* Catch everything, including -EOVERFLOW and other nasties. */
1365 if (urb
->status
!= 0)
1368 if (urb
->actual_length
== 0) {
1369 ub_state_stat_counted(sc
, cmd
);
1374 * Check the returned Bulk protocol status.
1375 * The status block has to be validated first.
1378 bcs
= &sc
->work_bcs
;
1380 if (sc
->signature
== cpu_to_le32(0)) {
1382 * This is the first reply, so do not perform the check.
1383 * Instead, remember the signature the device uses
1384 * for future checks. But do not allow a nul.
1386 sc
->signature
= bcs
->Signature
;
1387 if (sc
->signature
== cpu_to_le32(0)) {
1388 ub_state_stat_counted(sc
, cmd
);
1392 if (bcs
->Signature
!= sc
->signature
) {
1393 ub_state_stat_counted(sc
, cmd
);
1398 if (bcs
->Tag
!= cmd
->tag
) {
1400 * This usually happens when we disagree with the
1401 * device's microcode about something. For instance,
1402 * a few of them throw this after timeouts. They buffer
1403 * commands and reply at commands we timed out before.
1404 * Without flushing these replies we loop forever.
1406 ub_state_stat_counted(sc
, cmd
);
1410 len
= le32_to_cpu(bcs
->Residue
);
1411 if (len
!= cmd
->len
- cmd
->act_len
) {
1413 * It is all right to transfer less, the caller has
1414 * to check. But it's not all right if the device
1415 * counts disagree with our counts.
1417 /* P3 */ printk("%s: resid %d len %d act %d\n",
1418 sc
->name
, len
, cmd
->len
, cmd
->act_len
);
1422 switch (bcs
->Status
) {
1423 case US_BULK_STAT_OK
:
1425 case US_BULK_STAT_FAIL
:
1426 ub_state_sense(sc
, cmd
);
1428 case US_BULK_STAT_PHASE
:
1429 /* P3 */ printk("%s: status PHASE\n", sc
->name
);
1432 printk(KERN_INFO
"%s: unknown CSW status 0x%x\n",
1433 sc
->name
, bcs
->Status
);
1434 ub_state_done(sc
, cmd
, -EINVAL
);
1438 /* Not zeroing error to preserve a babble indicator */
1439 if (cmd
->error
!= 0) {
1440 ub_state_sense(sc
, cmd
);
1443 cmd
->state
= UB_CMDST_DONE
;
1444 ub_cmdtr_state(sc
, cmd
);
1446 (*cmd
->done
)(sc
, cmd
);
1448 } else if (cmd
->state
== UB_CMDST_SENSE
) {
1449 ub_state_done(sc
, cmd
, -EIO
);
1452 printk(KERN_WARNING
"%s: "
1453 "wrong command state %d\n",
1454 sc
->name
, cmd
->state
);
1455 ub_state_done(sc
, cmd
, -EINVAL
);
1460 Bad_End
: /* Little Excel is dead */
1461 ub_state_done(sc
, cmd
, -EIO
);
1465 * Factorization helper for the command state machine:
1466 * Initiate a data segment transfer.
1468 static void ub_data_start(struct ub_dev
*sc
, struct ub_scsi_cmd
*cmd
)
1470 struct scatterlist
*sg
= &cmd
->sgv
[cmd
->current_sg
];
1474 UB_INIT_COMPLETION(sc
->work_done
);
1476 if (cmd
->dir
== UB_DIR_READ
)
1477 pipe
= sc
->recv_bulk_pipe
;
1479 pipe
= sc
->send_bulk_pipe
;
1480 sc
->last_pipe
= pipe
;
1481 usb_fill_bulk_urb(&sc
->work_urb
, sc
->dev
, pipe
,
1482 page_address(sg
->page
) + sg
->offset
, sg
->length
,
1483 ub_urb_complete
, sc
);
1484 sc
->work_urb
.actual_length
= 0;
1485 sc
->work_urb
.error_count
= 0;
1486 sc
->work_urb
.status
= 0;
1488 if ((rc
= usb_submit_urb(&sc
->work_urb
, GFP_ATOMIC
)) != 0) {
1489 /* XXX Clear stalls */
1490 ub_complete(&sc
->work_done
);
1491 ub_state_done(sc
, cmd
, rc
);
1495 sc
->work_timer
.expires
= jiffies
+ UB_DATA_TIMEOUT
;
1496 add_timer(&sc
->work_timer
);
1498 cmd
->state
= UB_CMDST_DATA
;
1499 ub_cmdtr_state(sc
, cmd
);
1503 * Factorization helper for the command state machine:
1504 * Finish the command.
1506 static void ub_state_done(struct ub_dev
*sc
, struct ub_scsi_cmd
*cmd
, int rc
)
1510 cmd
->state
= UB_CMDST_DONE
;
1511 ub_cmdtr_state(sc
, cmd
);
1513 (*cmd
->done
)(sc
, cmd
);
1517 * Factorization helper for the command state machine:
1518 * Submit a CSW read.
1520 static int __ub_state_stat(struct ub_dev
*sc
, struct ub_scsi_cmd
*cmd
)
1524 UB_INIT_COMPLETION(sc
->work_done
);
1526 sc
->last_pipe
= sc
->recv_bulk_pipe
;
1527 usb_fill_bulk_urb(&sc
->work_urb
, sc
->dev
, sc
->recv_bulk_pipe
,
1528 &sc
->work_bcs
, US_BULK_CS_WRAP_LEN
, ub_urb_complete
, sc
);
1529 sc
->work_urb
.actual_length
= 0;
1530 sc
->work_urb
.error_count
= 0;
1531 sc
->work_urb
.status
= 0;
1533 if ((rc
= usb_submit_urb(&sc
->work_urb
, GFP_ATOMIC
)) != 0) {
1534 /* XXX Clear stalls */
1535 ub_complete(&sc
->work_done
);
1536 ub_state_done(sc
, cmd
, rc
);
1540 sc
->work_timer
.expires
= jiffies
+ UB_STAT_TIMEOUT
;
1541 add_timer(&sc
->work_timer
);
1546 * Factorization helper for the command state machine:
1547 * Submit a CSW read and go to STAT state.
1549 static void ub_state_stat(struct ub_dev
*sc
, struct ub_scsi_cmd
*cmd
)
1552 if (__ub_state_stat(sc
, cmd
) != 0)
1555 cmd
->stat_count
= 0;
1556 cmd
->state
= UB_CMDST_STAT
;
1557 ub_cmdtr_state(sc
, cmd
);
1561 * Factorization helper for the command state machine:
1562 * Submit a CSW read and go to STAT state with counter (along [C] path).
1564 static void ub_state_stat_counted(struct ub_dev
*sc
, struct ub_scsi_cmd
*cmd
)
1567 if (++cmd
->stat_count
>= 4) {
1568 ub_state_sense(sc
, cmd
);
1572 if (__ub_state_stat(sc
, cmd
) != 0)
1575 cmd
->state
= UB_CMDST_STAT
;
1576 ub_cmdtr_state(sc
, cmd
);
1580 * Factorization helper for the command state machine:
1581 * Submit a REQUEST SENSE and go to SENSE state.
1583 static void ub_state_sense(struct ub_dev
*sc
, struct ub_scsi_cmd
*cmd
)
1585 struct ub_scsi_cmd
*scmd
;
1586 struct scatterlist
*sg
;
1589 if (cmd
->cdb
[0] == REQUEST_SENSE
) {
1594 scmd
= &sc
->top_rqs_cmd
;
1595 memset(scmd
, 0, sizeof(struct ub_scsi_cmd
));
1596 scmd
->cdb
[0] = REQUEST_SENSE
;
1597 scmd
->cdb
[4] = UB_SENSE_SIZE
;
1599 scmd
->dir
= UB_DIR_READ
;
1600 scmd
->state
= UB_CMDST_INIT
;
1603 sg
->page
= virt_to_page(sc
->top_sense
);
1604 sg
->offset
= (unsigned long)sc
->top_sense
& (PAGE_SIZE
-1);
1605 sg
->length
= UB_SENSE_SIZE
;
1606 scmd
->len
= UB_SENSE_SIZE
;
1607 scmd
->lun
= cmd
->lun
;
1608 scmd
->done
= ub_top_sense_done
;
1611 scmd
->tag
= sc
->tagcnt
++;
1613 cmd
->state
= UB_CMDST_SENSE
;
1614 ub_cmdtr_state(sc
, cmd
);
1616 ub_cmdq_insert(sc
, scmd
);
1620 ub_state_done(sc
, cmd
, rc
);
1624 * A helper for the command's state machine:
1625 * Submit a stall clear.
1627 static int ub_submit_clear_stall(struct ub_dev
*sc
, struct ub_scsi_cmd
*cmd
,
1631 struct usb_ctrlrequest
*cr
;
1634 endp
= usb_pipeendpoint(stalled_pipe
);
1635 if (usb_pipein (stalled_pipe
))
1639 cr
->bRequestType
= USB_RECIP_ENDPOINT
;
1640 cr
->bRequest
= USB_REQ_CLEAR_FEATURE
;
1641 cr
->wValue
= cpu_to_le16(USB_ENDPOINT_HALT
);
1642 cr
->wIndex
= cpu_to_le16(endp
);
1643 cr
->wLength
= cpu_to_le16(0);
1645 UB_INIT_COMPLETION(sc
->work_done
);
1647 usb_fill_control_urb(&sc
->work_urb
, sc
->dev
, sc
->send_ctrl_pipe
,
1648 (unsigned char*) cr
, NULL
, 0, ub_urb_complete
, sc
);
1649 sc
->work_urb
.actual_length
= 0;
1650 sc
->work_urb
.error_count
= 0;
1651 sc
->work_urb
.status
= 0;
1653 if ((rc
= usb_submit_urb(&sc
->work_urb
, GFP_ATOMIC
)) != 0) {
1654 ub_complete(&sc
->work_done
);
1658 sc
->work_timer
.expires
= jiffies
+ UB_CTRL_TIMEOUT
;
1659 add_timer(&sc
->work_timer
);
1665 static void ub_top_sense_done(struct ub_dev
*sc
, struct ub_scsi_cmd
*scmd
)
1667 unsigned char *sense
= sc
->top_sense
;
1668 struct ub_scsi_cmd
*cmd
;
1671 * Ignoring scmd->act_len, because the buffer was pre-zeroed.
1673 ub_cmdtr_sense(sc
, scmd
, sense
);
1676 * Find the command which triggered the unit attention or a check,
1677 * save the sense into it, and advance its state machine.
1679 if ((cmd
= ub_cmdq_peek(sc
)) == NULL
) {
1680 printk(KERN_WARNING
"%s: sense done while idle\n", sc
->name
);
1683 if (cmd
!= scmd
->back
) {
1684 printk(KERN_WARNING
"%s: "
1685 "sense done for wrong command 0x%x\n",
1686 sc
->name
, cmd
->tag
);
1689 if (cmd
->state
!= UB_CMDST_SENSE
) {
1690 printk(KERN_WARNING
"%s: "
1691 "sense done with bad cmd state %d\n",
1692 sc
->name
, cmd
->state
);
1696 cmd
->key
= sense
[2] & 0x0F;
1697 cmd
->asc
= sense
[12];
1698 cmd
->ascq
= sense
[13];
1700 ub_scsi_urb_compl(sc
, cmd
);
1705 * XXX Move usb_reset_device to khubd. Hogging kevent is not a good thing.
1706 * XXX Make usb_sync_reset asynchronous.
1709 static void ub_reset_enter(struct ub_dev
*sc
, int try)
1713 /* This happens often on multi-LUN devices. */
1716 sc
->reset
= try + 1;
1718 #if 0 /* Not needed because the disconnect waits for us. */
1719 unsigned long flags
;
1720 spin_lock_irqsave(&ub_lock
, flags
);
1722 spin_unlock_irqrestore(&ub_lock
, flags
);
1725 #if 0 /* We let them stop themselves. */
1726 struct list_head
*p
;
1728 list_for_each(p
, &sc
->luns
) {
1729 lun
= list_entry(p
, struct ub_lun
, link
);
1730 blk_stop_queue(lun
->disk
->queue
);
1734 schedule_work(&sc
->reset_work
);
1737 static void ub_reset_task(void *arg
)
1739 struct ub_dev
*sc
= arg
;
1740 unsigned long flags
;
1741 struct list_head
*p
;
1746 printk(KERN_WARNING
"%s: Running reset unrequested\n",
1751 if (atomic_read(&sc
->poison
)) {
1752 printk(KERN_NOTICE
"%s: Not resetting disconnected device\n",
1753 sc
->name
); /* P3 This floods. Remove soon. XXX */
1754 } else if ((sc
->reset
& 1) == 0) {
1756 msleep(700); /* usb-storage sleeps 6s (!) */
1757 ub_probe_clear_stall(sc
, sc
->recv_bulk_pipe
);
1758 ub_probe_clear_stall(sc
, sc
->send_bulk_pipe
);
1759 } else if (sc
->dev
->actconfig
->desc
.bNumInterfaces
!= 1) {
1760 printk(KERN_NOTICE
"%s: Not resetting multi-interface device\n",
1761 sc
->name
); /* P3 This floods. Remove soon. XXX */
1763 if ((lkr
= usb_lock_device_for_reset(sc
->dev
, sc
->intf
)) < 0) {
1765 "%s: usb_lock_device_for_reset failed (%d)\n",
1768 rc
= usb_reset_device(sc
->dev
);
1770 printk(KERN_NOTICE
"%s: "
1771 "usb_lock_device_for_reset failed (%d)\n",
1776 usb_unlock_device(sc
->dev
);
1781 * In theory, no commands can be running while reset is active,
1782 * so nobody can ask for another reset, and so we do not need any
1783 * queues of resets or anything. We do need a spinlock though,
1784 * to interact with block layer.
1786 spin_lock_irqsave(sc
->lock
, flags
);
1788 tasklet_schedule(&sc
->tasklet
);
1789 list_for_each(p
, &sc
->luns
) {
1790 lun
= list_entry(p
, struct ub_lun
, link
);
1791 blk_start_queue(lun
->disk
->queue
);
1793 wake_up(&sc
->reset_wait
);
1794 spin_unlock_irqrestore(sc
->lock
, flags
);
1798 * This is called from a process context.
1800 static void ub_revalidate(struct ub_dev
*sc
, struct ub_lun
*lun
)
1803 lun
->readonly
= 0; /* XXX Query this from the device */
1805 lun
->capacity
.nsec
= 0;
1806 lun
->capacity
.bsize
= 512;
1807 lun
->capacity
.bshift
= 0;
1809 if (ub_sync_tur(sc
, lun
) != 0)
1810 return; /* Not ready */
1813 if (ub_sync_read_cap(sc
, lun
, &lun
->capacity
) != 0) {
1815 * The retry here means something is wrong, either with the
1816 * device, with the transport, or with our code.
1817 * We keep this because sd.c has retries for capacity.
1819 if (ub_sync_read_cap(sc
, lun
, &lun
->capacity
) != 0) {
1820 lun
->capacity
.nsec
= 0;
1821 lun
->capacity
.bsize
= 512;
1822 lun
->capacity
.bshift
= 0;
1829 * This is mostly needed to keep refcounting, but also to support
1830 * media checks on removable media drives.
1832 static int ub_bd_open(struct inode
*inode
, struct file
*filp
)
1834 struct gendisk
*disk
= inode
->i_bdev
->bd_disk
;
1837 unsigned long flags
;
1840 if ((lun
= disk
->private_data
) == NULL
)
1844 spin_lock_irqsave(&ub_lock
, flags
);
1845 if (atomic_read(&sc
->poison
)) {
1846 spin_unlock_irqrestore(&ub_lock
, flags
);
1850 spin_unlock_irqrestore(&ub_lock
, flags
);
1853 * This is a workaround for a specific problem in our block layer.
1854 * In 2.6.9, register_disk duplicates the code from rescan_partitions.
1855 * However, if we do add_disk with a device which persistently reports
1856 * a changed media, add_disk calls register_disk, which does do_open,
1857 * which will call rescan_paritions for changed media. After that,
1858 * register_disk attempts to do it all again and causes double kobject
1859 * registration and a eventually an oops on module removal.
1861 * The bottom line is, Al Viro says that we should not allow
1862 * bdev->bd_invalidated to be set when doing add_disk no matter what.
1864 if (lun
->first_open
) {
1865 lun
->first_open
= 0;
1872 if (lun
->removable
|| lun
->readonly
)
1873 check_disk_change(inode
->i_bdev
);
1876 * The sd.c considers ->media_present and ->changed not equivalent,
1877 * under some pretty murky conditions (a failure of READ CAPACITY).
1878 * We may need it one day.
1880 if (lun
->removable
&& lun
->changed
&& !(filp
->f_flags
& O_NDELAY
)) {
1885 if (lun
->readonly
&& (filp
->f_mode
& FMODE_WRITE
)) {
1899 static int ub_bd_release(struct inode
*inode
, struct file
*filp
)
1901 struct gendisk
*disk
= inode
->i_bdev
->bd_disk
;
1902 struct ub_lun
*lun
= disk
->private_data
;
1903 struct ub_dev
*sc
= lun
->udev
;
1910 * The ioctl interface.
1912 static int ub_bd_ioctl(struct inode
*inode
, struct file
*filp
,
1913 unsigned int cmd
, unsigned long arg
)
1915 struct gendisk
*disk
= inode
->i_bdev
->bd_disk
;
1916 void __user
*usermem
= (void __user
*) arg
;
1918 return scsi_cmd_ioctl(filp
, disk
, cmd
, usermem
);
1922 * This is called once a new disk was seen by the block layer or by ub_probe().
1923 * The main onjective here is to discover the features of the media such as
1924 * the capacity, read-only status, etc. USB storage generally does not
1925 * need to be spun up, but if we needed it, this would be the place.
1927 * This call can sleep.
1929 * The return code is not used.
1931 static int ub_bd_revalidate(struct gendisk
*disk
)
1933 struct ub_lun
*lun
= disk
->private_data
;
1935 ub_revalidate(lun
->udev
, lun
);
1937 /* XXX Support sector size switching like in sr.c */
1938 blk_queue_hardsect_size(disk
->queue
, lun
->capacity
.bsize
);
1939 set_capacity(disk
, lun
->capacity
.nsec
);
1940 // set_disk_ro(sdkp->disk, lun->readonly);
1946 * The check is called by the block layer to verify if the media
1947 * is still available. It is supposed to be harmless, lightweight and
1948 * non-intrusive in case the media was not changed.
1950 * This call can sleep.
1952 * The return code is bool!
1954 static int ub_bd_media_changed(struct gendisk
*disk
)
1956 struct ub_lun
*lun
= disk
->private_data
;
1958 if (!lun
->removable
)
1962 * We clean checks always after every command, so this is not
1963 * as dangerous as it looks. If the TEST_UNIT_READY fails here,
1964 * the device is actually not ready with operator or software
1965 * intervention required. One dangerous item might be a drive which
1966 * spins itself down, and come the time to write dirty pages, this
1967 * will fail, then block layer discards the data. Since we never
1968 * spin drives up, such devices simply cannot be used with ub anyway.
1970 if (ub_sync_tur(lun
->udev
, lun
) != 0) {
1975 return lun
->changed
;
1978 static struct block_device_operations ub_bd_fops
= {
1979 .owner
= THIS_MODULE
,
1981 .release
= ub_bd_release
,
1982 .ioctl
= ub_bd_ioctl
,
1983 .media_changed
= ub_bd_media_changed
,
1984 .revalidate_disk
= ub_bd_revalidate
,
1988 * Common ->done routine for commands executed synchronously.
1990 static void ub_probe_done(struct ub_dev
*sc
, struct ub_scsi_cmd
*cmd
)
1992 struct completion
*cop
= cmd
->back
;
1997 * Test if the device has a check condition on it, synchronously.
1999 static int ub_sync_tur(struct ub_dev
*sc
, struct ub_lun
*lun
)
2001 struct ub_scsi_cmd
*cmd
;
2002 enum { ALLOC_SIZE
= sizeof(struct ub_scsi_cmd
) };
2003 unsigned long flags
;
2004 struct completion
compl;
2007 init_completion(&compl);
2010 if ((cmd
= kmalloc(ALLOC_SIZE
, GFP_KERNEL
)) == NULL
)
2012 memset(cmd
, 0, ALLOC_SIZE
);
2014 cmd
->cdb
[0] = TEST_UNIT_READY
;
2016 cmd
->dir
= UB_DIR_NONE
;
2017 cmd
->state
= UB_CMDST_INIT
;
2018 cmd
->lun
= lun
; /* This may be NULL, but that's ok */
2019 cmd
->done
= ub_probe_done
;
2022 spin_lock_irqsave(sc
->lock
, flags
);
2023 cmd
->tag
= sc
->tagcnt
++;
2025 rc
= ub_submit_scsi(sc
, cmd
);
2026 spin_unlock_irqrestore(sc
->lock
, flags
);
2029 printk("ub: testing ready: submit error (%d)\n", rc
); /* P3 */
2033 wait_for_completion(&compl);
2037 if (rc
== -EIO
&& cmd
->key
!= 0) /* Retries for benh's key */
2047 * Read the SCSI capacity synchronously (for probing).
2049 static int ub_sync_read_cap(struct ub_dev
*sc
, struct ub_lun
*lun
,
2050 struct ub_capacity
*ret
)
2052 struct ub_scsi_cmd
*cmd
;
2053 struct scatterlist
*sg
;
2055 enum { ALLOC_SIZE
= sizeof(struct ub_scsi_cmd
) + 8 };
2056 unsigned long flags
;
2057 unsigned int bsize
, shift
;
2059 struct completion
compl;
2062 init_completion(&compl);
2065 if ((cmd
= kmalloc(ALLOC_SIZE
, GFP_KERNEL
)) == NULL
)
2067 memset(cmd
, 0, ALLOC_SIZE
);
2068 p
= (char *)cmd
+ sizeof(struct ub_scsi_cmd
);
2072 cmd
->dir
= UB_DIR_READ
;
2073 cmd
->state
= UB_CMDST_INIT
;
2076 sg
->page
= virt_to_page(p
);
2077 sg
->offset
= (unsigned long)p
& (PAGE_SIZE
-1);
2081 cmd
->done
= ub_probe_done
;
2084 spin_lock_irqsave(sc
->lock
, flags
);
2085 cmd
->tag
= sc
->tagcnt
++;
2087 rc
= ub_submit_scsi(sc
, cmd
);
2088 spin_unlock_irqrestore(sc
->lock
, flags
);
2091 printk("ub: reading capacity: submit error (%d)\n", rc
); /* P3 */
2095 wait_for_completion(&compl);
2097 if (cmd
->error
!= 0) {
2098 printk("ub: reading capacity: error %d\n", cmd
->error
); /* P3 */
2102 if (cmd
->act_len
!= 8) {
2103 printk("ub: reading capacity: size %d\n", cmd
->act_len
); /* P3 */
2108 /* sd.c special-cases sector size of 0 to mean 512. Needed? Safe? */
2109 nsec
= be32_to_cpu(*(__be32
*)p
) + 1;
2110 bsize
= be32_to_cpu(*(__be32
*)(p
+ 4));
2112 case 512: shift
= 0; break;
2113 case 1024: shift
= 1; break;
2114 case 2048: shift
= 2; break;
2115 case 4096: shift
= 3; break;
2117 printk("ub: Bad sector size %u\n", bsize
); /* P3 */
2123 ret
->bshift
= shift
;
2124 ret
->nsec
= nsec
<< shift
;
2137 static void ub_probe_urb_complete(struct urb
*urb
, struct pt_regs
*pt
)
2139 struct completion
*cop
= urb
->context
;
2143 static void ub_probe_timeout(unsigned long arg
)
2145 struct completion
*cop
= (struct completion
*) arg
;
2150 * Reset with a Bulk reset.
2152 static int ub_sync_reset(struct ub_dev
*sc
)
2154 int ifnum
= sc
->intf
->cur_altsetting
->desc
.bInterfaceNumber
;
2155 struct usb_ctrlrequest
*cr
;
2156 struct completion
compl;
2157 struct timer_list timer
;
2160 init_completion(&compl);
2163 cr
->bRequestType
= USB_TYPE_CLASS
| USB_RECIP_INTERFACE
;
2164 cr
->bRequest
= US_BULK_RESET_REQUEST
;
2165 cr
->wValue
= cpu_to_le16(0);
2166 cr
->wIndex
= cpu_to_le16(ifnum
);
2167 cr
->wLength
= cpu_to_le16(0);
2169 usb_fill_control_urb(&sc
->work_urb
, sc
->dev
, sc
->send_ctrl_pipe
,
2170 (unsigned char*) cr
, NULL
, 0, ub_probe_urb_complete
, &compl);
2171 sc
->work_urb
.actual_length
= 0;
2172 sc
->work_urb
.error_count
= 0;
2173 sc
->work_urb
.status
= 0;
2175 if ((rc
= usb_submit_urb(&sc
->work_urb
, GFP_KERNEL
)) != 0) {
2177 "%s: Unable to submit a bulk reset (%d)\n", sc
->name
, rc
);
2182 timer
.function
= ub_probe_timeout
;
2183 timer
.data
= (unsigned long) &compl;
2184 timer
.expires
= jiffies
+ UB_CTRL_TIMEOUT
;
2187 wait_for_completion(&compl);
2189 del_timer_sync(&timer
);
2190 usb_kill_urb(&sc
->work_urb
);
2192 return sc
->work_urb
.status
;
2196 * Get number of LUNs by the way of Bulk GetMaxLUN command.
2198 static int ub_sync_getmaxlun(struct ub_dev
*sc
)
2200 int ifnum
= sc
->intf
->cur_altsetting
->desc
.bInterfaceNumber
;
2202 enum { ALLOC_SIZE
= 1 };
2203 struct usb_ctrlrequest
*cr
;
2204 struct completion
compl;
2205 struct timer_list timer
;
2209 init_completion(&compl);
2212 if ((p
= kmalloc(ALLOC_SIZE
, GFP_KERNEL
)) == NULL
)
2217 cr
->bRequestType
= USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
;
2218 cr
->bRequest
= US_BULK_GET_MAX_LUN
;
2219 cr
->wValue
= cpu_to_le16(0);
2220 cr
->wIndex
= cpu_to_le16(ifnum
);
2221 cr
->wLength
= cpu_to_le16(1);
2223 usb_fill_control_urb(&sc
->work_urb
, sc
->dev
, sc
->recv_ctrl_pipe
,
2224 (unsigned char*) cr
, p
, 1, ub_probe_urb_complete
, &compl);
2225 sc
->work_urb
.actual_length
= 0;
2226 sc
->work_urb
.error_count
= 0;
2227 sc
->work_urb
.status
= 0;
2229 if ((rc
= usb_submit_urb(&sc
->work_urb
, GFP_KERNEL
)) != 0) {
2231 printk("%s: Stall submitting GetMaxLUN, using 1 LUN\n",
2235 "%s: Unable to submit GetMaxLUN (%d)\n",
2242 timer
.function
= ub_probe_timeout
;
2243 timer
.data
= (unsigned long) &compl;
2244 timer
.expires
= jiffies
+ UB_CTRL_TIMEOUT
;
2247 wait_for_completion(&compl);
2249 del_timer_sync(&timer
);
2250 usb_kill_urb(&sc
->work_urb
);
2252 if ((rc
= sc
->work_urb
.status
) < 0) {
2254 printk("%s: Stall at GetMaxLUN, using 1 LUN\n",
2258 "%s: Error at GetMaxLUN (%d)\n",
2264 if (sc
->work_urb
.actual_length
!= 1) {
2265 printk("%s: GetMaxLUN returned %d bytes\n", sc
->name
,
2266 sc
->work_urb
.actual_length
); /* P3 */
2269 if ((nluns
= *p
) == 55) {
2272 /* GetMaxLUN returns the maximum LUN number */
2274 if (nluns
> UB_MAX_LUNS
)
2275 nluns
= UB_MAX_LUNS
;
2277 printk("%s: GetMaxLUN returned %d, using %d LUNs\n", sc
->name
,
2278 *p
, nluns
); /* P3 */
2292 * Clear initial stalls.
2294 static int ub_probe_clear_stall(struct ub_dev
*sc
, int stalled_pipe
)
2297 struct usb_ctrlrequest
*cr
;
2298 struct completion
compl;
2299 struct timer_list timer
;
2302 init_completion(&compl);
2304 endp
= usb_pipeendpoint(stalled_pipe
);
2305 if (usb_pipein (stalled_pipe
))
2309 cr
->bRequestType
= USB_RECIP_ENDPOINT
;
2310 cr
->bRequest
= USB_REQ_CLEAR_FEATURE
;
2311 cr
->wValue
= cpu_to_le16(USB_ENDPOINT_HALT
);
2312 cr
->wIndex
= cpu_to_le16(endp
);
2313 cr
->wLength
= cpu_to_le16(0);
2315 usb_fill_control_urb(&sc
->work_urb
, sc
->dev
, sc
->send_ctrl_pipe
,
2316 (unsigned char*) cr
, NULL
, 0, ub_probe_urb_complete
, &compl);
2317 sc
->work_urb
.actual_length
= 0;
2318 sc
->work_urb
.error_count
= 0;
2319 sc
->work_urb
.status
= 0;
2321 if ((rc
= usb_submit_urb(&sc
->work_urb
, GFP_KERNEL
)) != 0) {
2323 "%s: Unable to submit a probe clear (%d)\n", sc
->name
, rc
);
2328 timer
.function
= ub_probe_timeout
;
2329 timer
.data
= (unsigned long) &compl;
2330 timer
.expires
= jiffies
+ UB_CTRL_TIMEOUT
;
2333 wait_for_completion(&compl);
2335 del_timer_sync(&timer
);
2336 usb_kill_urb(&sc
->work_urb
);
2338 /* reset the endpoint toggle */
2339 usb_settoggle(sc
->dev
, endp
, usb_pipeout(sc
->last_pipe
), 0);
2345 * Get the pipe settings.
2347 static int ub_get_pipes(struct ub_dev
*sc
, struct usb_device
*dev
,
2348 struct usb_interface
*intf
)
2350 struct usb_host_interface
*altsetting
= intf
->cur_altsetting
;
2351 struct usb_endpoint_descriptor
*ep_in
= NULL
;
2352 struct usb_endpoint_descriptor
*ep_out
= NULL
;
2353 struct usb_endpoint_descriptor
*ep
;
2357 * Find the endpoints we need.
2358 * We are expecting a minimum of 2 endpoints - in and out (bulk).
2359 * We will ignore any others.
2361 for (i
= 0; i
< altsetting
->desc
.bNumEndpoints
; i
++) {
2362 ep
= &altsetting
->endpoint
[i
].desc
;
2364 /* Is it a BULK endpoint? */
2365 if ((ep
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
)
2366 == USB_ENDPOINT_XFER_BULK
) {
2367 /* BULK in or out? */
2368 if (ep
->bEndpointAddress
& USB_DIR_IN
)
2375 if (ep_in
== NULL
|| ep_out
== NULL
) {
2376 printk(KERN_NOTICE
"%s: failed endpoint check\n",
2381 /* Calculate and store the pipe values */
2382 sc
->send_ctrl_pipe
= usb_sndctrlpipe(dev
, 0);
2383 sc
->recv_ctrl_pipe
= usb_rcvctrlpipe(dev
, 0);
2384 sc
->send_bulk_pipe
= usb_sndbulkpipe(dev
,
2385 ep_out
->bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
);
2386 sc
->recv_bulk_pipe
= usb_rcvbulkpipe(dev
,
2387 ep_in
->bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
);
2393 * Probing is done in the process context, which allows us to cheat
2394 * and not to build a state machine for the discovery.
2396 static int ub_probe(struct usb_interface
*intf
,
2397 const struct usb_device_id
*dev_id
)
2404 if (usb_usual_check_type(dev_id
, USB_US_TYPE_UB
))
2408 if ((sc
= kmalloc(sizeof(struct ub_dev
), GFP_KERNEL
)) == NULL
)
2410 memset(sc
, 0, sizeof(struct ub_dev
));
2411 sc
->lock
= ub_next_lock();
2412 INIT_LIST_HEAD(&sc
->luns
);
2413 usb_init_urb(&sc
->work_urb
);
2414 tasklet_init(&sc
->tasklet
, ub_scsi_action
, (unsigned long)sc
);
2415 atomic_set(&sc
->poison
, 0);
2416 INIT_WORK(&sc
->reset_work
, ub_reset_task
, sc
);
2417 init_waitqueue_head(&sc
->reset_wait
);
2419 init_timer(&sc
->work_timer
);
2420 sc
->work_timer
.data
= (unsigned long) sc
;
2421 sc
->work_timer
.function
= ub_urb_timeout
;
2423 ub_init_completion(&sc
->work_done
);
2424 sc
->work_done
.done
= 1; /* A little yuk, but oh well... */
2426 sc
->dev
= interface_to_usbdev(intf
);
2428 // sc->ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
2429 usb_set_intfdata(intf
, sc
);
2430 usb_get_dev(sc
->dev
);
2431 // usb_get_intf(sc->intf); /* Do we need this? */
2433 snprintf(sc
->name
, 12, DRV_NAME
"(%d.%d)",
2434 sc
->dev
->bus
->busnum
, sc
->dev
->devnum
);
2436 /* XXX Verify that we can handle the device (from descriptors) */
2438 if (ub_get_pipes(sc
, sc
->dev
, intf
) != 0)
2441 if (device_create_file(&sc
->intf
->dev
, &dev_attr_diag
) != 0)
2445 * At this point, all USB initialization is done, do upper layer.
2446 * We really hate halfway initialized structures, so from the
2447 * invariants perspective, this ub_dev is fully constructed at
2452 * This is needed to clear toggles. It is a problem only if we do
2453 * `rmmod ub && modprobe ub` without disconnects, but we like that.
2455 #if 0 /* iPod Mini fails if we do this (big white iPod works) */
2456 ub_probe_clear_stall(sc
, sc
->recv_bulk_pipe
);
2457 ub_probe_clear_stall(sc
, sc
->send_bulk_pipe
);
2461 * The way this is used by the startup code is a little specific.
2462 * A SCSI check causes a USB stall. Our common case code sees it
2463 * and clears the check, after which the device is ready for use.
2464 * But if a check was not present, any command other than
2465 * TEST_UNIT_READY ends with a lockup (including REQUEST_SENSE).
2467 * If we neglect to clear the SCSI check, the first real command fails
2468 * (which is the capacity readout). We clear that and retry, but why
2469 * causing spurious retries for no reason.
2471 * Revalidation may start with its own TEST_UNIT_READY, but that one
2472 * has to succeed, so we clear checks with an additional one here.
2473 * In any case it's not our business how revaliadation is implemented.
2475 for (i
= 0; i
< 3; i
++) { /* Retries for benh's key */
2476 if ((rc
= ub_sync_tur(sc
, NULL
)) <= 0) break;
2477 if (rc
!= 0x6) break;
2482 for (i
= 0; i
< 3; i
++) {
2483 if ((rc
= ub_sync_getmaxlun(sc
)) < 0) {
2485 * This segment is taken from usb-storage. They say
2486 * that ZIP-100 needs this, but my own ZIP-100 works
2487 * fine without this.
2488 * Still, it does not seem to hurt anything.
2491 ub_probe_clear_stall(sc
, sc
->recv_bulk_pipe
);
2492 ub_probe_clear_stall(sc
, sc
->send_bulk_pipe
);
2503 for (i
= 0; i
< nluns
; i
++) {
2504 ub_probe_lun(sc
, i
);
2508 /* device_remove_file(&sc->intf->dev, &dev_attr_diag); */
2511 usb_set_intfdata(intf
, NULL
);
2512 // usb_put_intf(sc->intf);
2513 usb_put_dev(sc
->dev
);
2519 static int ub_probe_lun(struct ub_dev
*sc
, int lnum
)
2523 struct gendisk
*disk
;
2527 if ((lun
= kmalloc(sizeof(struct ub_lun
), GFP_KERNEL
)) == NULL
)
2529 memset(lun
, 0, sizeof(struct ub_lun
));
2533 if ((lun
->id
= ub_id_get()) == -1)
2537 list_add(&lun
->link
, &sc
->luns
);
2539 snprintf(lun
->name
, 16, DRV_NAME
"%c(%d.%d.%d)",
2540 lun
->id
+ 'a', sc
->dev
->bus
->busnum
, sc
->dev
->devnum
, lun
->num
);
2542 lun
->removable
= 1; /* XXX Query this from the device */
2543 lun
->changed
= 1; /* ub_revalidate clears only */
2544 lun
->first_open
= 1;
2545 ub_revalidate(sc
, lun
);
2548 if ((disk
= alloc_disk(UB_PARTS_PER_LUN
)) == NULL
)
2552 sprintf(disk
->disk_name
, DRV_NAME
"%c", lun
->id
+ 'a');
2553 sprintf(disk
->devfs_name
, DEVFS_NAME
"/%c", lun
->id
+ 'a');
2554 disk
->major
= UB_MAJOR
;
2555 disk
->first_minor
= lun
->id
* UB_PARTS_PER_LUN
;
2556 disk
->fops
= &ub_bd_fops
;
2557 disk
->private_data
= lun
;
2558 disk
->driverfs_dev
= &sc
->intf
->dev
;
2561 if ((q
= blk_init_queue(ub_request_fn
, sc
->lock
)) == NULL
)
2566 blk_queue_bounce_limit(q
, BLK_BOUNCE_HIGH
);
2567 blk_queue_max_hw_segments(q
, UB_MAX_REQ_SG
);
2568 blk_queue_max_phys_segments(q
, UB_MAX_REQ_SG
);
2569 blk_queue_segment_boundary(q
, 0xffffffff); /* Dubious. */
2570 blk_queue_max_sectors(q
, UB_MAX_SECTORS
);
2571 blk_queue_hardsect_size(q
, lun
->capacity
.bsize
);
2575 set_capacity(disk
, lun
->capacity
.nsec
);
2577 disk
->flags
|= GENHD_FL_REMOVABLE
;
2586 list_del(&lun
->link
);
2594 static void ub_disconnect(struct usb_interface
*intf
)
2596 struct ub_dev
*sc
= usb_get_intfdata(intf
);
2597 struct list_head
*p
;
2599 struct gendisk
*disk
;
2600 unsigned long flags
;
2603 * Prevent ub_bd_release from pulling the rug from under us.
2604 * XXX This is starting to look like a kref.
2605 * XXX Why not to take this ref at probe time?
2607 spin_lock_irqsave(&ub_lock
, flags
);
2609 spin_unlock_irqrestore(&ub_lock
, flags
);
2612 * Fence stall clearnings, operations triggered by unlinkings and so on.
2613 * We do not attempt to unlink any URBs, because we do not trust the
2614 * unlink paths in HC drivers. Also, we get -84 upon disconnect anyway.
2616 atomic_set(&sc
->poison
, 1);
2619 * Wait for reset to end, if any.
2621 wait_event(sc
->reset_wait
, !sc
->reset
);
2624 * Blow away queued commands.
2626 * Actually, this never works, because before we get here
2627 * the HCD terminates outstanding URB(s). It causes our
2628 * SCSI command queue to advance, commands fail to submit,
2629 * and the whole queue drains. So, we just use this code to
2632 spin_lock_irqsave(sc
->lock
, flags
);
2634 struct ub_scsi_cmd
*cmd
;
2636 while ((cmd
= ub_cmdq_peek(sc
)) != NULL
) {
2637 cmd
->error
= -ENOTCONN
;
2638 cmd
->state
= UB_CMDST_DONE
;
2639 ub_cmdtr_state(sc
, cmd
);
2641 (*cmd
->done
)(sc
, cmd
);
2645 printk(KERN_WARNING
"%s: "
2646 "%d was queued after shutdown\n", sc
->name
, cnt
);
2649 spin_unlock_irqrestore(sc
->lock
, flags
);
2652 * Unregister the upper layer.
2654 list_for_each (p
, &sc
->luns
) {
2655 lun
= list_entry(p
, struct ub_lun
, link
);
2657 if (disk
->flags
& GENHD_FL_UP
)
2660 * I wish I could do:
2661 * set_bit(QUEUE_FLAG_DEAD, &q->queue_flags);
2662 * As it is, we rely on our internal poisoning and let
2663 * the upper levels to spin furiously failing all the I/O.
2668 * Testing for -EINPROGRESS is always a bug, so we are bending
2669 * the rules a little.
2671 spin_lock_irqsave(sc
->lock
, flags
);
2672 if (sc
->work_urb
.status
== -EINPROGRESS
) { /* janitors: ignore */
2673 printk(KERN_WARNING
"%s: "
2674 "URB is active after disconnect\n", sc
->name
);
2676 spin_unlock_irqrestore(sc
->lock
, flags
);
2679 * There is virtually no chance that other CPU runs times so long
2680 * after ub_urb_complete should have called del_timer, but only if HCD
2681 * didn't forget to deliver a callback on unlink.
2683 del_timer_sync(&sc
->work_timer
);
2686 * At this point there must be no commands coming from anyone
2687 * and no URBs left in transit.
2690 device_remove_file(&sc
->intf
->dev
, &dev_attr_diag
);
2691 usb_set_intfdata(intf
, NULL
);
2692 // usb_put_intf(sc->intf);
2694 usb_put_dev(sc
->dev
);
2700 static struct usb_driver ub_driver
= {
2703 .disconnect
= ub_disconnect
,
2704 .id_table
= ub_usb_ids
,
2707 static int __init
ub_init(void)
2712 for (i
= 0; i
< UB_QLOCK_NUM
; i
++)
2713 spin_lock_init(&ub_qlockv
[i
]);
2715 if ((rc
= register_blkdev(UB_MAJOR
, DRV_NAME
)) != 0)
2717 devfs_mk_dir(DEVFS_NAME
);
2719 if ((rc
= usb_register(&ub_driver
)) != 0)
2722 usb_usual_set_present(USB_US_TYPE_UB
);
2726 devfs_remove(DEVFS_NAME
);
2727 unregister_blkdev(UB_MAJOR
, DRV_NAME
);
2732 static void __exit
ub_exit(void)
2734 usb_deregister(&ub_driver
);
2736 devfs_remove(DEVFS_NAME
);
2737 unregister_blkdev(UB_MAJOR
, DRV_NAME
);
2738 usb_usual_clear_present(USB_US_TYPE_UB
);
2741 module_init(ub_init
);
2742 module_exit(ub_exit
);
2744 MODULE_LICENSE("GPL");