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 * -- Do resets with usb_device_reset (needs a thread context, use khubd)
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 * -- support pphaneuf's SDDR-75 with two LUNs (also broken capacity...)
15 * -- special case some senses, e.g. 3a/0 -> no media present, reduce retries
16 * -- verify the 13 conditions and do bulk resets
17 * -- normal pool of commands instead of cmdv[]?
18 * -- kill last_pipe and simply do two-state clearing on both pipes
19 * -- verify protocol (bulk) from USB descriptors (maybe...)
21 * -- move top_sense and work_bcs into separate allocations (if they survive)
22 * for cache purists and esoteric architectures.
23 * -- prune comments, they are too volumnous
24 * -- Exterminate P3 printks
26 * -- Redo "benh's retries", perhaps have spin-up code to handle them. V:D=?
28 #include <linux/kernel.h>
29 #include <linux/module.h>
30 #include <linux/usb.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 * Definitions which have to be scattered once we understand the layout better.
45 /* Transport (despite PR in the name) */
46 #define US_PR_BULK 0x50 /* bulk only */
49 #define US_SC_SCSI 0x06 /* Transparent */
53 #define UB_MINORS_PER_MAJOR 8
55 #define UB_MAX_CDB_SIZE 16 /* Corresponds to Bulk */
57 #define UB_SENSE_SIZE 18
62 /* command block wrapper */
64 __le32 Signature
; /* contains 'USBC' */
65 u32 Tag
; /* unique per command id */
66 __le32 DataTransferLength
; /* size of data */
67 u8 Flags
; /* direction in bit 0 */
68 u8 Lun
; /* LUN normally 0 */
69 u8 Length
; /* of of the CDB */
70 u8 CDB
[UB_MAX_CDB_SIZE
]; /* max command */
73 #define US_BULK_CB_WRAP_LEN 31
74 #define US_BULK_CB_SIGN 0x43425355 /*spells out USBC */
75 #define US_BULK_FLAG_IN 1
76 #define US_BULK_FLAG_OUT 0
78 /* command status wrapper */
80 __le32 Signature
; /* should = 'USBS' */
81 u32 Tag
; /* same as original command */
82 __le32 Residue
; /* amount not transferred */
83 u8 Status
; /* see below */
86 #define US_BULK_CS_WRAP_LEN 13
87 #define US_BULK_CS_SIGN 0x53425355 /* spells out 'USBS' */
88 /* This is for Olympus Camedia digital cameras */
89 #define US_BULK_CS_OLYMPUS_SIGN 0x55425355 /* spells out 'USBU' */
90 #define US_BULK_STAT_OK 0
91 #define US_BULK_STAT_FAIL 1
92 #define US_BULK_STAT_PHASE 2
94 /* bulk-only class specific requests */
95 #define US_BULK_RESET_REQUEST 0xff
96 #define US_BULK_GET_MAX_LUN 0xfe
102 #define UB_MAX_REQ_SG 1
103 #define UB_MAX_SECTORS 64
106 * A second is more than enough for a 32K transfer (UB_MAX_SECTORS)
107 * even if a webcam hogs the bus, but some devices need time to spin up.
109 #define UB_URB_TIMEOUT (HZ*2)
110 #define UB_DATA_TIMEOUT (HZ*5) /* ZIP does spin-ups in the data phase */
111 #define UB_STAT_TIMEOUT (HZ*5) /* Same spinups and eject for a dataless cmd. */
112 #define UB_CTRL_TIMEOUT (HZ/2) /* 500ms ought to be enough to clear a stall */
115 * An instance of a SCSI command in transit.
117 #define UB_DIR_NONE 0
118 #define UB_DIR_READ 1
119 #define UB_DIR_ILLEGAL2 2
120 #define UB_DIR_WRITE 3
122 #define UB_DIR_CHAR(c) (((c)==UB_DIR_WRITE)? 'w': \
123 (((c)==UB_DIR_READ)? 'r': 'n'))
125 enum ub_scsi_cmd_state
{
126 UB_CMDST_INIT
, /* Initial state */
127 UB_CMDST_CMD
, /* Command submitted */
128 UB_CMDST_DATA
, /* Data phase */
129 UB_CMDST_CLR2STS
, /* Clearing before requesting status */
130 UB_CMDST_STAT
, /* Status phase */
131 UB_CMDST_CLEAR
, /* Clearing a stall (halt, actually) */
132 UB_CMDST_SENSE
, /* Sending Request Sense */
133 UB_CMDST_DONE
/* Final state */
136 static char *ub_scsi_cmd_stname
[] = {
148 unsigned char cdb
[UB_MAX_CDB_SIZE
];
149 unsigned char cdb_len
;
151 unsigned char dir
; /* 0 - none, 1 - read, 3 - write. */
152 unsigned char trace_index
;
153 enum ub_scsi_cmd_state state
;
155 struct ub_scsi_cmd
*next
;
157 int error
; /* Return code - valid upon done */
158 unsigned int act_len
; /* Return size */
159 unsigned char key
, asc
, ascq
; /* May be valid if error==-EIO */
161 int stat_count
; /* Retries getting status. */
164 * We do not support transfers from highmem pages
165 * because the underlying USB framework does not do what we need.
167 char *data
; /* Requested buffer */
168 unsigned int len
; /* Requested length */
169 // struct scatterlist sgv[UB_MAX_REQ_SG];
171 void (*done
)(struct ub_dev
*, struct ub_scsi_cmd
*);
178 unsigned long nsec
; /* Linux size - 512 byte sectors */
179 unsigned int bsize
; /* Linux hardsect_size */
180 unsigned int bshift
; /* Shift between 512 and hard sects */
184 * The SCSI command tracing structure.
187 #define SCMD_ST_HIST_SZ 8
188 #define SCMD_TRACE_SZ 63 /* Less than 4KB of 61-byte lines */
190 struct ub_scsi_cmd_trace
{
193 unsigned int req_size
, act_size
;
196 unsigned char key
, asc
, ascq
;
197 char st_hst
[SCMD_ST_HIST_SZ
];
200 struct ub_scsi_trace
{
202 struct ub_scsi_cmd_trace vec
[SCMD_TRACE_SZ
];
206 * This is a direct take-off from linux/include/completion.h
207 * The difference is that I do not wait on this thing, just poll.
208 * When I want to wait (ub_probe), I just use the stock completion.
210 * Note that INIT_COMPLETION takes no lock. It is correct. But why
211 * in the bloody hell that thing takes struct instead of pointer to struct
212 * is quite beyond me. I just copied it from the stock completion.
214 struct ub_completion
{
219 static inline void ub_init_completion(struct ub_completion
*x
)
222 spin_lock_init(&x
->lock
);
225 #define UB_INIT_COMPLETION(x) ((x).done = 0)
227 static void ub_complete(struct ub_completion
*x
)
231 spin_lock_irqsave(&x
->lock
, flags
);
233 spin_unlock_irqrestore(&x
->lock
, flags
);
236 static int ub_is_completed(struct ub_completion
*x
)
241 spin_lock_irqsave(&x
->lock
, flags
);
243 spin_unlock_irqrestore(&x
->lock
, flags
);
249 struct ub_scsi_cmd_queue
{
251 struct ub_scsi_cmd
*head
, *tail
;
255 * The UB device instance.
259 int id
; /* Number among ub's */
260 atomic_t poison
; /* The USB device is disconnected */
261 int openc
; /* protected by ub_lock! */
262 /* kref is too implicit for our taste */
264 int changed
; /* Media was changed */
267 int first_open
; /* Kludge. See ub_bd_open. */
269 struct usb_device
*dev
;
270 struct usb_interface
*intf
;
272 struct ub_capacity capacity
;
273 struct gendisk
*disk
;
275 unsigned int send_bulk_pipe
; /* cached pipe values */
276 unsigned int recv_bulk_pipe
;
277 unsigned int send_ctrl_pipe
;
278 unsigned int recv_ctrl_pipe
;
280 struct tasklet_struct tasklet
;
282 /* XXX Use Ingo's mempool (once we have more than one) */
284 struct ub_scsi_cmd cmdv
[1];
286 struct ub_scsi_cmd_queue cmd_queue
;
287 struct ub_scsi_cmd top_rqs_cmd
; /* REQUEST SENSE */
288 unsigned char top_sense
[UB_SENSE_SIZE
];
290 struct ub_completion work_done
;
292 struct timer_list work_timer
;
293 int last_pipe
; /* What might need clearing */
294 struct bulk_cb_wrap work_bcb
;
295 struct bulk_cs_wrap work_bcs
;
296 struct usb_ctrlrequest work_cr
;
298 struct ub_scsi_trace tr
;
303 static void ub_cleanup(struct ub_dev
*sc
);
304 static int ub_bd_rq_fn_1(struct ub_dev
*sc
, struct request
*rq
);
305 static int ub_cmd_build_block(struct ub_dev
*sc
, struct ub_scsi_cmd
*cmd
,
307 static int ub_cmd_build_packet(struct ub_dev
*sc
, struct ub_scsi_cmd
*cmd
,
309 static void ub_rw_cmd_done(struct ub_dev
*sc
, struct ub_scsi_cmd
*cmd
);
310 static void ub_end_rq(struct request
*rq
, int uptodate
);
311 static int ub_submit_scsi(struct ub_dev
*sc
, struct ub_scsi_cmd
*cmd
);
312 static void ub_urb_complete(struct urb
*urb
, struct pt_regs
*pt
);
313 static void ub_scsi_action(unsigned long _dev
);
314 static void ub_scsi_dispatch(struct ub_dev
*sc
);
315 static void ub_scsi_urb_compl(struct ub_dev
*sc
, struct ub_scsi_cmd
*cmd
);
316 static void ub_state_done(struct ub_dev
*sc
, struct ub_scsi_cmd
*cmd
, int rc
);
317 static void __ub_state_stat(struct ub_dev
*sc
, struct ub_scsi_cmd
*cmd
);
318 static void ub_state_stat(struct ub_dev
*sc
, struct ub_scsi_cmd
*cmd
);
319 static void ub_state_sense(struct ub_dev
*sc
, struct ub_scsi_cmd
*cmd
);
320 static int ub_submit_clear_stall(struct ub_dev
*sc
, struct ub_scsi_cmd
*cmd
,
322 static void ub_top_sense_done(struct ub_dev
*sc
, struct ub_scsi_cmd
*scmd
);
323 static int ub_sync_tur(struct ub_dev
*sc
);
324 static int ub_sync_read_cap(struct ub_dev
*sc
, struct ub_capacity
*ret
);
328 static struct usb_device_id ub_usb_ids
[] = {
329 // { USB_DEVICE_VER(0x0781, 0x0002, 0x0009, 0x0009) }, /* SDDR-31 */
330 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE
, US_SC_SCSI
, US_PR_BULK
) },
334 MODULE_DEVICE_TABLE(usb
, ub_usb_ids
);
337 * Find me a way to identify "next free minor" for add_disk(),
338 * and the array disappears the next day. However, the number of
339 * hosts has something to do with the naming and /proc/partitions.
340 * This has to be thought out in detail before changing.
341 * If UB_MAX_HOST was 1000, we'd use a bitmap. Or a better data structure.
343 #define UB_MAX_HOSTS 26
344 static char ub_hostv
[UB_MAX_HOSTS
];
345 static DEFINE_SPINLOCK(ub_lock
); /* Locks globals and ->openc */
348 * The SCSI command tracing procedures.
351 static void ub_cmdtr_new(struct ub_dev
*sc
, struct ub_scsi_cmd
*cmd
)
354 struct ub_scsi_cmd_trace
*t
;
356 if ((n
= sc
->tr
.cur
+ 1) == SCMD_TRACE_SZ
) n
= 0;
359 memset(t
, 0, sizeof(struct ub_scsi_cmd_trace
));
363 t
->req_size
= cmd
->len
;
364 t
->st_hst
[0] = cmd
->state
;
367 cmd
->trace_index
= n
;
370 static void ub_cmdtr_state(struct ub_dev
*sc
, struct ub_scsi_cmd
*cmd
)
373 struct ub_scsi_cmd_trace
*t
;
375 t
= &sc
->tr
.vec
[cmd
->trace_index
];
376 if (t
->tag
== cmd
->tag
) {
377 if ((n
= t
->hcur
+ 1) == SCMD_ST_HIST_SZ
) n
= 0;
378 t
->st_hst
[n
] = cmd
->state
;
383 static void ub_cmdtr_act_len(struct ub_dev
*sc
, struct ub_scsi_cmd
*cmd
)
385 struct ub_scsi_cmd_trace
*t
;
387 t
= &sc
->tr
.vec
[cmd
->trace_index
];
388 if (t
->tag
== cmd
->tag
)
389 t
->act_size
= cmd
->act_len
;
392 static void ub_cmdtr_sense(struct ub_dev
*sc
, struct ub_scsi_cmd
*cmd
,
393 unsigned char *sense
)
395 struct ub_scsi_cmd_trace
*t
;
397 t
= &sc
->tr
.vec
[cmd
->trace_index
];
398 if (t
->tag
== cmd
->tag
) {
399 t
->key
= sense
[2] & 0x0F;
405 static ssize_t
ub_diag_show(struct device
*dev
, char *page
)
407 struct usb_interface
*intf
;
413 struct ub_scsi_cmd_trace
*t
;
415 intf
= to_usb_interface(dev
);
416 sc
= usb_get_intfdata(intf
);
421 spin_lock_irqsave(&sc
->lock
, flags
);
423 cnt
+= sprintf(page
+ cnt
,
424 "qlen %d qmax %d changed %d removable %d readonly %d\n",
425 sc
->cmd_queue
.qlen
, sc
->cmd_queue
.qmax
,
426 sc
->changed
, sc
->removable
, sc
->readonly
);
428 if ((nc
= sc
->tr
.cur
+ 1) == SCMD_TRACE_SZ
) nc
= 0;
429 for (j
= 0; j
< SCMD_TRACE_SZ
; j
++) {
432 cnt
+= sprintf(page
+ cnt
, "%08x %02x", t
->tag
, t
->op
);
433 if (t
->op
== REQUEST_SENSE
) {
434 cnt
+= sprintf(page
+ cnt
, " [sense %x %02x %02x]",
435 t
->key
, t
->asc
, t
->ascq
);
437 cnt
+= sprintf(page
+ cnt
, " %c", UB_DIR_CHAR(t
->dir
));
438 cnt
+= sprintf(page
+ cnt
, " [%5d %5d]",
439 t
->req_size
, t
->act_size
);
441 if ((nh
= t
->hcur
+ 1) == SCMD_ST_HIST_SZ
) nh
= 0;
442 for (i
= 0; i
< SCMD_ST_HIST_SZ
; i
++) {
443 cnt
+= sprintf(page
+ cnt
, " %s",
444 ub_scsi_cmd_stname
[(int)t
->st_hst
[nh
]]);
445 if (++nh
== SCMD_ST_HIST_SZ
) nh
= 0;
447 cnt
+= sprintf(page
+ cnt
, "\n");
449 if (++nc
== SCMD_TRACE_SZ
) nc
= 0;
452 spin_unlock_irqrestore(&sc
->lock
, flags
);
456 static DEVICE_ATTR(diag
, S_IRUGO
, ub_diag_show
, NULL
); /* N.B. World readable */
461 * This also stores the host for indexing by minor, which is somewhat dirty.
463 static int ub_id_get(void)
468 spin_lock_irqsave(&ub_lock
, flags
);
469 for (i
= 0; i
< UB_MAX_HOSTS
; i
++) {
470 if (ub_hostv
[i
] == 0) {
472 spin_unlock_irqrestore(&ub_lock
, flags
);
476 spin_unlock_irqrestore(&ub_lock
, flags
);
480 static void ub_id_put(int id
)
484 if (id
< 0 || id
>= UB_MAX_HOSTS
) {
485 printk(KERN_ERR DRV_NAME
": bad host ID %d\n", id
);
489 spin_lock_irqsave(&ub_lock
, flags
);
490 if (ub_hostv
[id
] == 0) {
491 spin_unlock_irqrestore(&ub_lock
, flags
);
492 printk(KERN_ERR DRV_NAME
": freeing free host ID %d\n", id
);
496 spin_unlock_irqrestore(&ub_lock
, flags
);
500 * Downcount for deallocation. This rides on two assumptions:
501 * - once something is poisoned, its refcount cannot grow
502 * - opens cannot happen at this time (del_gendisk was done)
503 * If the above is true, we can drop the lock, which we need for
504 * blk_cleanup_queue(): the silly thing may attempt to sleep.
505 * [Actually, it never needs to sleep for us, but it calls might_sleep()]
507 static void ub_put(struct ub_dev
*sc
)
511 spin_lock_irqsave(&ub_lock
, flags
);
513 if (sc
->openc
== 0 && atomic_read(&sc
->poison
)) {
514 spin_unlock_irqrestore(&ub_lock
, flags
);
517 spin_unlock_irqrestore(&ub_lock
, flags
);
522 * Final cleanup and deallocation.
524 static void ub_cleanup(struct ub_dev
*sc
)
528 /* I don't think queue can be NULL. But... Stolen from sx8.c */
529 if ((q
= sc
->disk
->queue
) != NULL
)
530 blk_cleanup_queue(q
);
533 * If we zero disk->private_data BEFORE put_disk, we have to check
534 * for NULL all over the place in open, release, check_media and
535 * revalidate, because the block level semaphore is well inside the
536 * put_disk. But we cannot zero after the call, because *disk is gone.
537 * The sd.c is blatantly racy in this area.
539 /* disk->private_data = NULL; */
548 * The "command allocator".
550 static struct ub_scsi_cmd
*ub_get_cmd(struct ub_dev
*sc
)
552 struct ub_scsi_cmd
*ret
;
561 static void ub_put_cmd(struct ub_dev
*sc
, struct ub_scsi_cmd
*cmd
)
563 if (cmd
!= &sc
->cmdv
[0]) {
564 printk(KERN_WARNING
"%s: releasing a foreign cmd %p\n",
569 printk(KERN_WARNING
"%s: releasing a free cmd\n", sc
->name
);
578 static void ub_cmdq_add(struct ub_dev
*sc
, struct ub_scsi_cmd
*cmd
)
580 struct ub_scsi_cmd_queue
*t
= &sc
->cmd_queue
;
582 if (t
->qlen
++ == 0) {
590 if (t
->qlen
> t
->qmax
)
594 static void ub_cmdq_insert(struct ub_dev
*sc
, struct ub_scsi_cmd
*cmd
)
596 struct ub_scsi_cmd_queue
*t
= &sc
->cmd_queue
;
598 if (t
->qlen
++ == 0) {
606 if (t
->qlen
> t
->qmax
)
610 static struct ub_scsi_cmd
*ub_cmdq_pop(struct ub_dev
*sc
)
612 struct ub_scsi_cmd_queue
*t
= &sc
->cmd_queue
;
613 struct ub_scsi_cmd
*cmd
;
625 #define ub_cmdq_peek(sc) ((sc)->cmd_queue.head)
628 * The request function is our main entry point
631 static void ub_bd_rq_fn(request_queue_t
*q
)
633 struct ub_dev
*sc
= q
->queuedata
;
636 while ((rq
= elv_next_request(q
)) != NULL
) {
637 if (ub_bd_rq_fn_1(sc
, rq
) != 0) {
644 static int ub_bd_rq_fn_1(struct ub_dev
*sc
, struct request
*rq
)
646 struct ub_scsi_cmd
*cmd
;
649 if (atomic_read(&sc
->poison
) || sc
->changed
) {
650 blkdev_dequeue_request(rq
);
655 if ((cmd
= ub_get_cmd(sc
)) == NULL
)
657 memset(cmd
, 0, sizeof(struct ub_scsi_cmd
));
659 blkdev_dequeue_request(rq
);
661 if (blk_pc_request(rq
)) {
662 rc
= ub_cmd_build_packet(sc
, cmd
, rq
);
664 rc
= ub_cmd_build_block(sc
, cmd
, rq
);
669 blk_start_queue(sc
->disk
->queue
);
673 cmd
->state
= UB_CMDST_INIT
;
674 cmd
->done
= ub_rw_cmd_done
;
677 cmd
->tag
= sc
->tagcnt
++;
678 if ((rc
= ub_submit_scsi(sc
, cmd
)) != 0) {
681 blk_start_queue(sc
->disk
->queue
);
688 static int ub_cmd_build_block(struct ub_dev
*sc
, struct ub_scsi_cmd
*cmd
,
692 #if 0 /* We use rq->buffer for now */
693 struct scatterlist
*sg
;
696 unsigned int block
, nblks
;
698 if (rq_data_dir(rq
) == WRITE
)
699 ub_dir
= UB_DIR_WRITE
;
701 ub_dir
= UB_DIR_READ
;
704 * get scatterlist from block layer
706 #if 0 /* We use rq->buffer for now */
708 n_elem
= blk_rq_map_sg(q
, rq
, sg
);
713 return 0; /* request with no s/g entries? */
716 if (n_elem
!= 1) { /* Paranoia */
717 printk(KERN_WARNING
"%s: request with %d segments\n",
727 * XXX Unfortunately, this check does not work. It is quite possible
728 * to get bogus non-null rq->buffer if you allow sg by mistake.
730 if (rq
->buffer
== NULL
) {
732 * This must not happen if we set the queue right.
733 * The block level must create bounce buffers for us.
735 static int do_print
= 1;
737 printk(KERN_WARNING
"%s: unmapped block request"
738 " flags 0x%lx sectors %lu\n",
739 sc
->name
, rq
->flags
, rq
->nr_sectors
);
748 * The call to blk_queue_hardsect_size() guarantees that request
749 * is aligned, but it is given in terms of 512 byte units, always.
751 block
= rq
->sector
>> sc
->capacity
.bshift
;
752 nblks
= rq
->nr_sectors
>> sc
->capacity
.bshift
;
754 cmd
->cdb
[0] = (ub_dir
== UB_DIR_READ
)? READ_10
: WRITE_10
;
755 /* 10-byte uses 4 bytes of LBA: 2147483648KB, 2097152MB, 2048GB */
756 cmd
->cdb
[2] = block
>> 24;
757 cmd
->cdb
[3] = block
>> 16;
758 cmd
->cdb
[4] = block
>> 8;
760 cmd
->cdb
[7] = nblks
>> 8;
765 cmd
->data
= rq
->buffer
;
766 cmd
->len
= rq
->nr_sectors
* 512;
771 static int ub_cmd_build_packet(struct ub_dev
*sc
, struct ub_scsi_cmd
*cmd
,
775 if (rq
->data_len
!= 0 && rq
->data
== NULL
) {
776 static int do_print
= 1;
778 printk(KERN_WARNING
"%s: unmapped packet request"
779 " flags 0x%lx length %d\n",
780 sc
->name
, rq
->flags
, rq
->data_len
);
786 memcpy(&cmd
->cdb
, rq
->cmd
, rq
->cmd_len
);
787 cmd
->cdb_len
= rq
->cmd_len
;
789 if (rq
->data_len
== 0) {
790 cmd
->dir
= UB_DIR_NONE
;
792 if (rq_data_dir(rq
) == WRITE
)
793 cmd
->dir
= UB_DIR_WRITE
;
795 cmd
->dir
= UB_DIR_READ
;
797 cmd
->data
= rq
->data
;
798 cmd
->len
= rq
->data_len
;
803 static void ub_rw_cmd_done(struct ub_dev
*sc
, struct ub_scsi_cmd
*cmd
)
805 struct request
*rq
= cmd
->back
;
806 struct gendisk
*disk
= sc
->disk
;
807 request_queue_t
*q
= disk
->queue
;
810 if (blk_pc_request(rq
)) {
811 /* UB_SENSE_SIZE is smaller than SCSI_SENSE_BUFFERSIZE */
812 memcpy(rq
->sense
, sc
->top_sense
, UB_SENSE_SIZE
);
813 rq
->sense_len
= UB_SENSE_SIZE
;
822 ub_end_rq(rq
, uptodate
);
826 static void ub_end_rq(struct request
*rq
, int uptodate
)
830 rc
= end_that_request_first(rq
, uptodate
, rq
->hard_nr_sectors
);
832 end_that_request_last(rq
);
836 * Submit a regular SCSI operation (not an auto-sense).
838 * The Iron Law of Good Submit Routine is:
839 * Zero return - callback is done, Nonzero return - callback is not done.
842 * Host is assumed locked.
844 * XXX We only support Bulk for the moment.
846 static int ub_submit_scsi(struct ub_dev
*sc
, struct ub_scsi_cmd
*cmd
)
849 if (cmd
->state
!= UB_CMDST_INIT
||
850 (cmd
->dir
!= UB_DIR_NONE
&& cmd
->len
== 0)) {
854 ub_cmdq_add(sc
, cmd
);
856 * We can call ub_scsi_dispatch(sc) right away here, but it's a little
857 * safer to jump to a tasklet, in case upper layers do something silly.
859 tasklet_schedule(&sc
->tasklet
);
864 * Submit the first URB for the queued command.
865 * This function does not deal with queueing in any way.
867 static int ub_scsi_cmd_start(struct ub_dev
*sc
, struct ub_scsi_cmd
*cmd
)
869 struct bulk_cb_wrap
*bcb
;
875 * ``If the allocation length is eighteen or greater, and a device
876 * server returns less than eithteen bytes of data, the application
877 * client should assume that the bytes not transferred would have been
878 * zeroes had the device server returned those bytes.''
880 * We zero sense for all commands so that when a packet request
881 * fails it does not return a stale sense.
883 memset(&sc
->top_sense
, 0, UB_SENSE_SIZE
);
885 /* set up the command wrapper */
886 bcb
->Signature
= cpu_to_le32(US_BULK_CB_SIGN
);
887 bcb
->Tag
= cmd
->tag
; /* Endianness is not important */
888 bcb
->DataTransferLength
= cpu_to_le32(cmd
->len
);
889 bcb
->Flags
= (cmd
->dir
== UB_DIR_READ
) ? 0x80 : 0;
890 bcb
->Lun
= 0; /* No multi-LUN yet */
891 bcb
->Length
= cmd
->cdb_len
;
893 /* copy the command payload */
894 memcpy(bcb
->CDB
, cmd
->cdb
, UB_MAX_CDB_SIZE
);
896 UB_INIT_COMPLETION(sc
->work_done
);
898 sc
->last_pipe
= sc
->send_bulk_pipe
;
899 usb_fill_bulk_urb(&sc
->work_urb
, sc
->dev
, sc
->send_bulk_pipe
,
900 bcb
, US_BULK_CB_WRAP_LEN
, ub_urb_complete
, sc
);
901 sc
->work_urb
.transfer_flags
= URB_ASYNC_UNLINK
;
903 /* Fill what we shouldn't be filling, because usb-storage did so. */
904 sc
->work_urb
.actual_length
= 0;
905 sc
->work_urb
.error_count
= 0;
906 sc
->work_urb
.status
= 0;
908 if ((rc
= usb_submit_urb(&sc
->work_urb
, GFP_ATOMIC
)) != 0) {
909 /* XXX Clear stalls */
910 printk("ub: cmd #%d start failed (%d)\n", cmd
->tag
, rc
); /* P3 */
911 ub_complete(&sc
->work_done
);
915 sc
->work_timer
.expires
= jiffies
+ UB_URB_TIMEOUT
;
916 add_timer(&sc
->work_timer
);
918 cmd
->state
= UB_CMDST_CMD
;
919 ub_cmdtr_state(sc
, cmd
);
926 static void ub_urb_timeout(unsigned long arg
)
928 struct ub_dev
*sc
= (struct ub_dev
*) arg
;
931 spin_lock_irqsave(&sc
->lock
, flags
);
932 usb_unlink_urb(&sc
->work_urb
);
933 spin_unlock_irqrestore(&sc
->lock
, flags
);
937 * Completion routine for the work URB.
939 * This can be called directly from usb_submit_urb (while we have
940 * the sc->lock taken) and from an interrupt (while we do NOT have
941 * the sc->lock taken). Therefore, bounce this off to a tasklet.
943 static void ub_urb_complete(struct urb
*urb
, struct pt_regs
*pt
)
945 struct ub_dev
*sc
= urb
->context
;
947 ub_complete(&sc
->work_done
);
948 tasklet_schedule(&sc
->tasklet
);
951 static void ub_scsi_action(unsigned long _dev
)
953 struct ub_dev
*sc
= (struct ub_dev
*) _dev
;
956 spin_lock_irqsave(&sc
->lock
, flags
);
957 del_timer(&sc
->work_timer
);
958 ub_scsi_dispatch(sc
);
959 spin_unlock_irqrestore(&sc
->lock
, flags
);
962 static void ub_scsi_dispatch(struct ub_dev
*sc
)
964 struct ub_scsi_cmd
*cmd
;
967 while ((cmd
= ub_cmdq_peek(sc
)) != NULL
) {
968 if (cmd
->state
== UB_CMDST_DONE
) {
970 (*cmd
->done
)(sc
, cmd
);
971 } else if (cmd
->state
== UB_CMDST_INIT
) {
972 ub_cmdtr_new(sc
, cmd
);
973 if ((rc
= ub_scsi_cmd_start(sc
, cmd
)) == 0)
976 cmd
->state
= UB_CMDST_DONE
;
977 ub_cmdtr_state(sc
, cmd
);
979 if (!ub_is_completed(&sc
->work_done
))
981 ub_scsi_urb_compl(sc
, cmd
);
986 static void ub_scsi_urb_compl(struct ub_dev
*sc
, struct ub_scsi_cmd
*cmd
)
988 struct urb
*urb
= &sc
->work_urb
;
989 struct bulk_cs_wrap
*bcs
;
993 if (atomic_read(&sc
->poison
)) {
994 /* A little too simplistic, I feel... */
998 if (cmd
->state
== UB_CMDST_CLEAR
) {
999 if (urb
->status
== -EPIPE
) {
1001 * STALL while clearning STALL.
1002 * The control pipe clears itself - nothing to do.
1003 * XXX Might try to reset the device here and retry.
1005 printk(KERN_NOTICE
"%s: "
1006 "stall on control pipe for device %u\n",
1007 sc
->name
, sc
->dev
->devnum
);
1012 * We ignore the result for the halt clear.
1015 /* reset the endpoint toggle */
1016 usb_settoggle(sc
->dev
, usb_pipeendpoint(sc
->last_pipe
),
1017 usb_pipeout(sc
->last_pipe
), 0);
1019 ub_state_sense(sc
, cmd
);
1021 } else if (cmd
->state
== UB_CMDST_CLR2STS
) {
1022 if (urb
->status
== -EPIPE
) {
1024 * STALL while clearning STALL.
1025 * The control pipe clears itself - nothing to do.
1026 * XXX Might try to reset the device here and retry.
1028 printk(KERN_NOTICE
"%s: "
1029 "stall on control pipe for device %u\n",
1030 sc
->name
, sc
->dev
->devnum
);
1035 * We ignore the result for the halt clear.
1038 /* reset the endpoint toggle */
1039 usb_settoggle(sc
->dev
, usb_pipeendpoint(sc
->last_pipe
),
1040 usb_pipeout(sc
->last_pipe
), 0);
1042 ub_state_stat(sc
, cmd
);
1044 } else if (cmd
->state
== UB_CMDST_CMD
) {
1045 if (urb
->status
== -EPIPE
) {
1046 rc
= ub_submit_clear_stall(sc
, cmd
, sc
->last_pipe
);
1048 printk(KERN_NOTICE
"%s: "
1049 "unable to submit clear for device %u"
1051 sc
->name
, sc
->dev
->devnum
, rc
);
1053 * This is typically ENOMEM or some other such shit.
1054 * Retrying is pointless. Just do Bad End on it...
1058 cmd
->state
= UB_CMDST_CLEAR
;
1059 ub_cmdtr_state(sc
, cmd
);
1062 if (urb
->status
!= 0) {
1063 printk("ub: cmd #%d cmd status (%d)\n", cmd
->tag
, urb
->status
); /* P3 */
1066 if (urb
->actual_length
!= US_BULK_CB_WRAP_LEN
) {
1067 printk("ub: cmd #%d xferred %d\n", cmd
->tag
, urb
->actual_length
); /* P3 */
1068 /* XXX Must do reset here to unconfuse the device */
1072 if (cmd
->dir
== UB_DIR_NONE
) {
1073 ub_state_stat(sc
, cmd
);
1077 UB_INIT_COMPLETION(sc
->work_done
);
1079 if (cmd
->dir
== UB_DIR_READ
)
1080 pipe
= sc
->recv_bulk_pipe
;
1082 pipe
= sc
->send_bulk_pipe
;
1083 sc
->last_pipe
= pipe
;
1084 usb_fill_bulk_urb(&sc
->work_urb
, sc
->dev
, pipe
,
1085 cmd
->data
, cmd
->len
, ub_urb_complete
, sc
);
1086 sc
->work_urb
.transfer_flags
= URB_ASYNC_UNLINK
;
1087 sc
->work_urb
.actual_length
= 0;
1088 sc
->work_urb
.error_count
= 0;
1089 sc
->work_urb
.status
= 0;
1091 if ((rc
= usb_submit_urb(&sc
->work_urb
, GFP_ATOMIC
)) != 0) {
1092 /* XXX Clear stalls */
1093 printk("ub: data #%d submit failed (%d)\n", cmd
->tag
, rc
); /* P3 */
1094 ub_complete(&sc
->work_done
);
1095 ub_state_done(sc
, cmd
, rc
);
1099 sc
->work_timer
.expires
= jiffies
+ UB_DATA_TIMEOUT
;
1100 add_timer(&sc
->work_timer
);
1102 cmd
->state
= UB_CMDST_DATA
;
1103 ub_cmdtr_state(sc
, cmd
);
1105 } else if (cmd
->state
== UB_CMDST_DATA
) {
1106 if (urb
->status
== -EPIPE
) {
1107 rc
= ub_submit_clear_stall(sc
, cmd
, sc
->last_pipe
);
1109 printk(KERN_NOTICE
"%s: "
1110 "unable to submit clear for device %u"
1112 sc
->name
, sc
->dev
->devnum
, rc
);
1114 * This is typically ENOMEM or some other such shit.
1115 * Retrying is pointless. Just do Bad End on it...
1119 cmd
->state
= UB_CMDST_CLR2STS
;
1120 ub_cmdtr_state(sc
, cmd
);
1123 if (urb
->status
== -EOVERFLOW
) {
1125 * A babble? Failure, but we must transfer CSW now.
1127 cmd
->error
= -EOVERFLOW
; /* A cheap trick... */
1129 if (urb
->status
!= 0)
1133 cmd
->act_len
= urb
->actual_length
;
1134 ub_cmdtr_act_len(sc
, cmd
);
1136 ub_state_stat(sc
, cmd
);
1138 } else if (cmd
->state
== UB_CMDST_STAT
) {
1139 if (urb
->status
== -EPIPE
) {
1140 rc
= ub_submit_clear_stall(sc
, cmd
, sc
->last_pipe
);
1142 printk(KERN_NOTICE
"%s: "
1143 "unable to submit clear for device %u"
1145 sc
->name
, sc
->dev
->devnum
, rc
);
1147 * This is typically ENOMEM or some other such shit.
1148 * Retrying is pointless. Just do Bad End on it...
1152 cmd
->state
= UB_CMDST_CLEAR
;
1153 ub_cmdtr_state(sc
, cmd
);
1156 if (urb
->status
!= 0)
1159 if (urb
->actual_length
== 0) {
1161 * Some broken devices add unnecessary zero-length
1162 * packets to the end of their data transfers.
1163 * Such packets show up as 0-length CSWs. If we
1164 * encounter such a thing, try to read the CSW again.
1166 if (++cmd
->stat_count
>= 4) {
1167 printk(KERN_NOTICE
"%s: "
1168 "unable to get CSW on device %u\n",
1169 sc
->name
, sc
->dev
->devnum
);
1172 __ub_state_stat(sc
, cmd
);
1177 * Check the returned Bulk protocol status.
1180 bcs
= &sc
->work_bcs
;
1181 rc
= le32_to_cpu(bcs
->Residue
);
1182 if (rc
!= cmd
->len
- cmd
->act_len
) {
1184 * It is all right to transfer less, the caller has
1185 * to check. But it's not all right if the device
1186 * counts disagree with our counts.
1188 /* P3 */ printk("%s: resid %d len %d act %d\n",
1189 sc
->name
, rc
, cmd
->len
, cmd
->act_len
);
1194 if (bcs
->Signature
!= cpu_to_le32(US_BULK_CS_SIGN
) &&
1195 bcs
->Signature
!= cpu_to_le32(US_BULK_CS_OLYMPUS_SIGN
)) {
1196 /* Windows ignores signatures, so do we. */
1200 if (bcs
->Tag
!= cmd
->tag
) {
1202 * This usually happens when we disagree with the
1203 * device's microcode about something. For instance,
1204 * a few of them throw this after timeouts. They buffer
1205 * commands and reply at commands we timed out before.
1206 * Without flushing these replies we loop forever.
1208 if (++cmd
->stat_count
>= 4) {
1209 printk(KERN_NOTICE
"%s: "
1210 "tag mismatch orig 0x%x reply 0x%x "
1212 sc
->name
, cmd
->tag
, bcs
->Tag
,
1216 __ub_state_stat(sc
, cmd
);
1220 switch (bcs
->Status
) {
1221 case US_BULK_STAT_OK
:
1223 case US_BULK_STAT_FAIL
:
1224 ub_state_sense(sc
, cmd
);
1226 case US_BULK_STAT_PHASE
:
1227 /* XXX We must reset the transport here */
1228 /* P3 */ printk("%s: status PHASE\n", sc
->name
);
1231 printk(KERN_INFO
"%s: unknown CSW status 0x%x\n",
1232 sc
->name
, bcs
->Status
);
1236 /* Not zeroing error to preserve a babble indicator */
1237 cmd
->state
= UB_CMDST_DONE
;
1238 ub_cmdtr_state(sc
, cmd
);
1240 (*cmd
->done
)(sc
, cmd
);
1242 } else if (cmd
->state
== UB_CMDST_SENSE
) {
1243 ub_state_done(sc
, cmd
, -EIO
);
1246 printk(KERN_WARNING
"%s: "
1247 "wrong command state %d on device %u\n",
1248 sc
->name
, cmd
->state
, sc
->dev
->devnum
);
1253 Bad_End
: /* Little Excel is dead */
1254 ub_state_done(sc
, cmd
, -EIO
);
1258 * Factorization helper for the command state machine:
1259 * Finish the command.
1261 static void ub_state_done(struct ub_dev
*sc
, struct ub_scsi_cmd
*cmd
, int rc
)
1265 cmd
->state
= UB_CMDST_DONE
;
1266 ub_cmdtr_state(sc
, cmd
);
1268 (*cmd
->done
)(sc
, cmd
);
1272 * Factorization helper for the command state machine:
1273 * Submit a CSW read.
1275 static void __ub_state_stat(struct ub_dev
*sc
, struct ub_scsi_cmd
*cmd
)
1279 UB_INIT_COMPLETION(sc
->work_done
);
1281 sc
->last_pipe
= sc
->recv_bulk_pipe
;
1282 usb_fill_bulk_urb(&sc
->work_urb
, sc
->dev
, sc
->recv_bulk_pipe
,
1283 &sc
->work_bcs
, US_BULK_CS_WRAP_LEN
, ub_urb_complete
, sc
);
1284 sc
->work_urb
.transfer_flags
= URB_ASYNC_UNLINK
;
1285 sc
->work_urb
.actual_length
= 0;
1286 sc
->work_urb
.error_count
= 0;
1287 sc
->work_urb
.status
= 0;
1289 if ((rc
= usb_submit_urb(&sc
->work_urb
, GFP_ATOMIC
)) != 0) {
1290 /* XXX Clear stalls */
1291 printk("%s: CSW #%d submit failed (%d)\n", sc
->name
, cmd
->tag
, rc
); /* P3 */
1292 ub_complete(&sc
->work_done
);
1293 ub_state_done(sc
, cmd
, rc
);
1297 sc
->work_timer
.expires
= jiffies
+ UB_STAT_TIMEOUT
;
1298 add_timer(&sc
->work_timer
);
1302 * Factorization helper for the command state machine:
1303 * Submit a CSW read and go to STAT state.
1305 static void ub_state_stat(struct ub_dev
*sc
, struct ub_scsi_cmd
*cmd
)
1307 __ub_state_stat(sc
, cmd
);
1309 cmd
->stat_count
= 0;
1310 cmd
->state
= UB_CMDST_STAT
;
1311 ub_cmdtr_state(sc
, cmd
);
1315 * Factorization helper for the command state machine:
1316 * Submit a REQUEST SENSE and go to SENSE state.
1318 static void ub_state_sense(struct ub_dev
*sc
, struct ub_scsi_cmd
*cmd
)
1320 struct ub_scsi_cmd
*scmd
;
1323 if (cmd
->cdb
[0] == REQUEST_SENSE
) {
1328 scmd
= &sc
->top_rqs_cmd
;
1329 scmd
->cdb
[0] = REQUEST_SENSE
;
1330 scmd
->cdb
[4] = UB_SENSE_SIZE
;
1332 scmd
->dir
= UB_DIR_READ
;
1333 scmd
->state
= UB_CMDST_INIT
;
1334 scmd
->data
= sc
->top_sense
;
1335 scmd
->len
= UB_SENSE_SIZE
;
1336 scmd
->done
= ub_top_sense_done
;
1339 scmd
->tag
= sc
->tagcnt
++;
1341 cmd
->state
= UB_CMDST_SENSE
;
1342 ub_cmdtr_state(sc
, cmd
);
1344 ub_cmdq_insert(sc
, scmd
);
1348 ub_state_done(sc
, cmd
, rc
);
1352 * A helper for the command's state machine:
1353 * Submit a stall clear.
1355 static int ub_submit_clear_stall(struct ub_dev
*sc
, struct ub_scsi_cmd
*cmd
,
1359 struct usb_ctrlrequest
*cr
;
1362 endp
= usb_pipeendpoint(stalled_pipe
);
1363 if (usb_pipein (stalled_pipe
))
1367 cr
->bRequestType
= USB_RECIP_ENDPOINT
;
1368 cr
->bRequest
= USB_REQ_CLEAR_FEATURE
;
1369 cr
->wValue
= cpu_to_le16(USB_ENDPOINT_HALT
);
1370 cr
->wIndex
= cpu_to_le16(endp
);
1371 cr
->wLength
= cpu_to_le16(0);
1373 UB_INIT_COMPLETION(sc
->work_done
);
1375 usb_fill_control_urb(&sc
->work_urb
, sc
->dev
, sc
->send_ctrl_pipe
,
1376 (unsigned char*) cr
, NULL
, 0, ub_urb_complete
, sc
);
1377 sc
->work_urb
.transfer_flags
= URB_ASYNC_UNLINK
;
1378 sc
->work_urb
.actual_length
= 0;
1379 sc
->work_urb
.error_count
= 0;
1380 sc
->work_urb
.status
= 0;
1382 if ((rc
= usb_submit_urb(&sc
->work_urb
, GFP_ATOMIC
)) != 0) {
1383 ub_complete(&sc
->work_done
);
1387 sc
->work_timer
.expires
= jiffies
+ UB_CTRL_TIMEOUT
;
1388 add_timer(&sc
->work_timer
);
1394 static void ub_top_sense_done(struct ub_dev
*sc
, struct ub_scsi_cmd
*scmd
)
1396 unsigned char *sense
= scmd
->data
;
1397 struct ub_scsi_cmd
*cmd
;
1400 * Ignoring scmd->act_len, because the buffer was pre-zeroed.
1402 ub_cmdtr_sense(sc
, scmd
, sense
);
1405 * Find the command which triggered the unit attention or a check,
1406 * save the sense into it, and advance its state machine.
1408 if ((cmd
= ub_cmdq_peek(sc
)) == NULL
) {
1409 printk(KERN_WARNING
"%s: sense done while idle\n", sc
->name
);
1412 if (cmd
!= scmd
->back
) {
1413 printk(KERN_WARNING
"%s: "
1414 "sense done for wrong command 0x%x on device %u\n",
1415 sc
->name
, cmd
->tag
, sc
->dev
->devnum
);
1418 if (cmd
->state
!= UB_CMDST_SENSE
) {
1419 printk(KERN_WARNING
"%s: "
1420 "sense done with bad cmd state %d on device %u\n",
1421 sc
->name
, cmd
->state
, sc
->dev
->devnum
);
1425 cmd
->key
= sense
[2] & 0x0F;
1426 cmd
->asc
= sense
[12];
1427 cmd
->ascq
= sense
[13];
1429 ub_scsi_urb_compl(sc
, cmd
);
1433 /* Determine what the maximum LUN supported is */
1434 int usb_stor_Bulk_max_lun(struct us_data
*us
)
1438 /* issue the command */
1439 result
= usb_stor_control_msg(us
, us
->recv_ctrl_pipe
,
1440 US_BULK_GET_MAX_LUN
,
1441 USB_DIR_IN
| USB_TYPE_CLASS
|
1442 USB_RECIP_INTERFACE
,
1443 0, us
->ifnum
, us
->iobuf
, 1, HZ
);
1446 * Some devices (i.e. Iomega Zip100) need this -- apparently
1447 * the bulk pipes get STALLed when the GetMaxLUN request is
1448 * processed. This is, in theory, harmless to all other devices
1449 * (regardless of if they stall or not).
1452 usb_stor_clear_halt(us
, us
->recv_bulk_pipe
);
1453 usb_stor_clear_halt(us
, us
->send_bulk_pipe
);
1456 US_DEBUGP("GetMaxLUN command result is %d, data is %d\n",
1457 result
, us
->iobuf
[0]);
1459 /* if we have a successful request, return the result */
1461 return us
->iobuf
[0];
1463 /* return the default -- no LUNs */
1469 * This is called from a process context.
1471 static void ub_revalidate(struct ub_dev
*sc
)
1474 sc
->readonly
= 0; /* XXX Query this from the device */
1476 sc
->capacity
.nsec
= 0;
1477 sc
->capacity
.bsize
= 512;
1478 sc
->capacity
.bshift
= 0;
1480 if (ub_sync_tur(sc
) != 0)
1481 return; /* Not ready */
1484 if (ub_sync_read_cap(sc
, &sc
->capacity
) != 0) {
1486 * The retry here means something is wrong, either with the
1487 * device, with the transport, or with our code.
1488 * We keep this because sd.c has retries for capacity.
1490 if (ub_sync_read_cap(sc
, &sc
->capacity
) != 0) {
1491 sc
->capacity
.nsec
= 0;
1492 sc
->capacity
.bsize
= 512;
1493 sc
->capacity
.bshift
= 0;
1500 * This is mostly needed to keep refcounting, but also to support
1501 * media checks on removable media drives.
1503 static int ub_bd_open(struct inode
*inode
, struct file
*filp
)
1505 struct gendisk
*disk
= inode
->i_bdev
->bd_disk
;
1507 unsigned long flags
;
1510 if ((sc
= disk
->private_data
) == NULL
)
1512 spin_lock_irqsave(&ub_lock
, flags
);
1513 if (atomic_read(&sc
->poison
)) {
1514 spin_unlock_irqrestore(&ub_lock
, flags
);
1518 spin_unlock_irqrestore(&ub_lock
, flags
);
1521 * This is a workaround for a specific problem in our block layer.
1522 * In 2.6.9, register_disk duplicates the code from rescan_partitions.
1523 * However, if we do add_disk with a device which persistently reports
1524 * a changed media, add_disk calls register_disk, which does do_open,
1525 * which will call rescan_paritions for changed media. After that,
1526 * register_disk attempts to do it all again and causes double kobject
1527 * registration and a eventually an oops on module removal.
1529 * The bottom line is, Al Viro says that we should not allow
1530 * bdev->bd_invalidated to be set when doing add_disk no matter what.
1532 if (sc
->first_open
) {
1540 if (sc
->removable
|| sc
->readonly
)
1541 check_disk_change(inode
->i_bdev
);
1544 * The sd.c considers ->media_present and ->changed not equivalent,
1545 * under some pretty murky conditions (a failure of READ CAPACITY).
1546 * We may need it one day.
1548 if (sc
->removable
&& sc
->changed
&& !(filp
->f_flags
& O_NDELAY
)) {
1553 if (sc
->readonly
&& (filp
->f_mode
& FMODE_WRITE
)) {
1567 static int ub_bd_release(struct inode
*inode
, struct file
*filp
)
1569 struct gendisk
*disk
= inode
->i_bdev
->bd_disk
;
1570 struct ub_dev
*sc
= disk
->private_data
;
1577 * The ioctl interface.
1579 static int ub_bd_ioctl(struct inode
*inode
, struct file
*filp
,
1580 unsigned int cmd
, unsigned long arg
)
1582 struct gendisk
*disk
= inode
->i_bdev
->bd_disk
;
1583 void __user
*usermem
= (void __user
*) arg
;
1585 return scsi_cmd_ioctl(filp
, disk
, cmd
, usermem
);
1589 * This is called once a new disk was seen by the block layer or by ub_probe().
1590 * The main onjective here is to discover the features of the media such as
1591 * the capacity, read-only status, etc. USB storage generally does not
1592 * need to be spun up, but if we needed it, this would be the place.
1594 * This call can sleep.
1596 * The return code is not used.
1598 static int ub_bd_revalidate(struct gendisk
*disk
)
1600 struct ub_dev
*sc
= disk
->private_data
;
1603 /* This is pretty much a long term P3 */
1604 if (!atomic_read(&sc
->poison
)) { /* Cover sc->dev */
1605 printk(KERN_INFO
"%s: device %u capacity nsec %ld bsize %u\n",
1606 sc
->name
, sc
->dev
->devnum
,
1607 sc
->capacity
.nsec
, sc
->capacity
.bsize
);
1610 /* XXX Support sector size switching like in sr.c */
1611 blk_queue_hardsect_size(disk
->queue
, sc
->capacity
.bsize
);
1612 set_capacity(disk
, sc
->capacity
.nsec
);
1613 // set_disk_ro(sdkp->disk, sc->readonly);
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_dev
*sc
= disk
->private_data
;
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(sc
) != 0) {
1651 static struct block_device_operations ub_bd_fops
= {
1652 .owner
= THIS_MODULE
,
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
;
1670 * Test if the device has a check condition on it, synchronously.
1672 static int ub_sync_tur(struct ub_dev
*sc
)
1674 struct ub_scsi_cmd
*cmd
;
1675 enum { ALLOC_SIZE
= sizeof(struct ub_scsi_cmd
) };
1676 unsigned long flags
;
1677 struct completion
compl;
1680 init_completion(&compl);
1683 if ((cmd
= kmalloc(ALLOC_SIZE
, GFP_KERNEL
)) == NULL
)
1685 memset(cmd
, 0, ALLOC_SIZE
);
1687 cmd
->cdb
[0] = TEST_UNIT_READY
;
1689 cmd
->dir
= UB_DIR_NONE
;
1690 cmd
->state
= UB_CMDST_INIT
;
1691 cmd
->done
= ub_probe_done
;
1694 spin_lock_irqsave(&sc
->lock
, flags
);
1695 cmd
->tag
= sc
->tagcnt
++;
1697 rc
= ub_submit_scsi(sc
, cmd
);
1698 spin_unlock_irqrestore(&sc
->lock
, flags
);
1701 printk("ub: testing ready: submit error (%d)\n", rc
); /* P3 */
1705 wait_for_completion(&compl);
1709 if (rc
== -EIO
&& cmd
->key
!= 0) /* Retries for benh's key */
1719 * Read the SCSI capacity synchronously (for probing).
1721 static int ub_sync_read_cap(struct ub_dev
*sc
, struct ub_capacity
*ret
)
1723 struct ub_scsi_cmd
*cmd
;
1725 enum { ALLOC_SIZE
= sizeof(struct ub_scsi_cmd
) + 8 };
1726 unsigned long flags
;
1727 unsigned int bsize
, shift
;
1729 struct completion
compl;
1732 init_completion(&compl);
1735 if ((cmd
= kmalloc(ALLOC_SIZE
, GFP_KERNEL
)) == NULL
)
1737 memset(cmd
, 0, ALLOC_SIZE
);
1738 p
= (char *)cmd
+ sizeof(struct ub_scsi_cmd
);
1742 cmd
->dir
= UB_DIR_READ
;
1743 cmd
->state
= UB_CMDST_INIT
;
1746 cmd
->done
= ub_probe_done
;
1749 spin_lock_irqsave(&sc
->lock
, flags
);
1750 cmd
->tag
= sc
->tagcnt
++;
1752 rc
= ub_submit_scsi(sc
, cmd
);
1753 spin_unlock_irqrestore(&sc
->lock
, flags
);
1756 printk("ub: reading capacity: submit error (%d)\n", rc
); /* P3 */
1760 wait_for_completion(&compl);
1762 if (cmd
->error
!= 0) {
1763 printk("ub: reading capacity: error %d\n", cmd
->error
); /* P3 */
1767 if (cmd
->act_len
!= 8) {
1768 printk("ub: reading capacity: size %d\n", cmd
->act_len
); /* P3 */
1773 /* sd.c special-cases sector size of 0 to mean 512. Needed? Safe? */
1774 nsec
= be32_to_cpu(*(__be32
*)p
) + 1;
1775 bsize
= be32_to_cpu(*(__be32
*)(p
+ 4));
1777 case 512: shift
= 0; break;
1778 case 1024: shift
= 1; break;
1779 case 2048: shift
= 2; break;
1780 case 4096: shift
= 3; break;
1782 printk("ub: Bad sector size %u\n", bsize
); /* P3 */
1788 ret
->bshift
= shift
;
1789 ret
->nsec
= nsec
<< shift
;
1802 static void ub_probe_urb_complete(struct urb
*urb
, struct pt_regs
*pt
)
1804 struct completion
*cop
= urb
->context
;
1808 static void ub_probe_timeout(unsigned long arg
)
1810 struct completion
*cop
= (struct completion
*) arg
;
1815 * Clear initial stalls.
1817 static int ub_probe_clear_stall(struct ub_dev
*sc
, int stalled_pipe
)
1820 struct usb_ctrlrequest
*cr
;
1821 struct completion
compl;
1822 struct timer_list timer
;
1825 init_completion(&compl);
1827 endp
= usb_pipeendpoint(stalled_pipe
);
1828 if (usb_pipein (stalled_pipe
))
1832 cr
->bRequestType
= USB_RECIP_ENDPOINT
;
1833 cr
->bRequest
= USB_REQ_CLEAR_FEATURE
;
1834 cr
->wValue
= cpu_to_le16(USB_ENDPOINT_HALT
);
1835 cr
->wIndex
= cpu_to_le16(endp
);
1836 cr
->wLength
= cpu_to_le16(0);
1838 usb_fill_control_urb(&sc
->work_urb
, sc
->dev
, sc
->send_ctrl_pipe
,
1839 (unsigned char*) cr
, NULL
, 0, ub_probe_urb_complete
, &compl);
1840 sc
->work_urb
.transfer_flags
= 0;
1841 sc
->work_urb
.actual_length
= 0;
1842 sc
->work_urb
.error_count
= 0;
1843 sc
->work_urb
.status
= 0;
1845 if ((rc
= usb_submit_urb(&sc
->work_urb
, GFP_KERNEL
)) != 0) {
1847 "%s: Unable to submit a probe clear (%d)\n", sc
->name
, rc
);
1852 timer
.function
= ub_probe_timeout
;
1853 timer
.data
= (unsigned long) &compl;
1854 timer
.expires
= jiffies
+ UB_CTRL_TIMEOUT
;
1857 wait_for_completion(&compl);
1859 del_timer_sync(&timer
);
1860 usb_kill_urb(&sc
->work_urb
);
1862 /* reset the endpoint toggle */
1863 usb_settoggle(sc
->dev
, endp
, usb_pipeout(sc
->last_pipe
), 0);
1869 * Get the pipe settings.
1871 static int ub_get_pipes(struct ub_dev
*sc
, struct usb_device
*dev
,
1872 struct usb_interface
*intf
)
1874 struct usb_host_interface
*altsetting
= intf
->cur_altsetting
;
1875 struct usb_endpoint_descriptor
*ep_in
= NULL
;
1876 struct usb_endpoint_descriptor
*ep_out
= NULL
;
1877 struct usb_endpoint_descriptor
*ep
;
1881 * Find the endpoints we need.
1882 * We are expecting a minimum of 2 endpoints - in and out (bulk).
1883 * We will ignore any others.
1885 for (i
= 0; i
< altsetting
->desc
.bNumEndpoints
; i
++) {
1886 ep
= &altsetting
->endpoint
[i
].desc
;
1888 /* Is it a BULK endpoint? */
1889 if ((ep
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
)
1890 == USB_ENDPOINT_XFER_BULK
) {
1891 /* BULK in or out? */
1892 if (ep
->bEndpointAddress
& USB_DIR_IN
)
1899 if (ep_in
== NULL
|| ep_out
== NULL
) {
1900 printk(KERN_NOTICE
"%s: device %u failed endpoint check\n",
1901 sc
->name
, sc
->dev
->devnum
);
1905 /* Calculate and store the pipe values */
1906 sc
->send_ctrl_pipe
= usb_sndctrlpipe(dev
, 0);
1907 sc
->recv_ctrl_pipe
= usb_rcvctrlpipe(dev
, 0);
1908 sc
->send_bulk_pipe
= usb_sndbulkpipe(dev
,
1909 ep_out
->bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
);
1910 sc
->recv_bulk_pipe
= usb_rcvbulkpipe(dev
,
1911 ep_in
->bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
);
1917 * Probing is done in the process context, which allows us to cheat
1918 * and not to build a state machine for the discovery.
1920 static int ub_probe(struct usb_interface
*intf
,
1921 const struct usb_device_id
*dev_id
)
1925 struct gendisk
*disk
;
1930 if ((sc
= kmalloc(sizeof(struct ub_dev
), GFP_KERNEL
)) == NULL
)
1932 memset(sc
, 0, sizeof(struct ub_dev
));
1933 spin_lock_init(&sc
->lock
);
1934 usb_init_urb(&sc
->work_urb
);
1935 tasklet_init(&sc
->tasklet
, ub_scsi_action
, (unsigned long)sc
);
1936 atomic_set(&sc
->poison
, 0);
1938 init_timer(&sc
->work_timer
);
1939 sc
->work_timer
.data
= (unsigned long) sc
;
1940 sc
->work_timer
.function
= ub_urb_timeout
;
1942 ub_init_completion(&sc
->work_done
);
1943 sc
->work_done
.done
= 1; /* A little yuk, but oh well... */
1946 if ((sc
->id
= ub_id_get()) == -1)
1948 snprintf(sc
->name
, 8, DRV_NAME
"%c", sc
->id
+ 'a');
1950 sc
->dev
= interface_to_usbdev(intf
);
1952 // sc->ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
1954 usb_set_intfdata(intf
, sc
);
1955 usb_get_dev(sc
->dev
);
1956 // usb_get_intf(sc->intf); /* Do we need this? */
1958 /* XXX Verify that we can handle the device (from descriptors) */
1960 ub_get_pipes(sc
, sc
->dev
, intf
);
1962 if (device_create_file(&sc
->intf
->dev
, &dev_attr_diag
) != 0)
1966 * At this point, all USB initialization is done, do upper layer.
1967 * We really hate halfway initialized structures, so from the
1968 * invariants perspective, this ub_dev is fully constructed at
1973 * This is needed to clear toggles. It is a problem only if we do
1974 * `rmmod ub && modprobe ub` without disconnects, but we like that.
1976 ub_probe_clear_stall(sc
, sc
->recv_bulk_pipe
);
1977 ub_probe_clear_stall(sc
, sc
->send_bulk_pipe
);
1980 * The way this is used by the startup code is a little specific.
1981 * A SCSI check causes a USB stall. Our common case code sees it
1982 * and clears the check, after which the device is ready for use.
1983 * But if a check was not present, any command other than
1984 * TEST_UNIT_READY ends with a lockup (including REQUEST_SENSE).
1986 * If we neglect to clear the SCSI check, the first real command fails
1987 * (which is the capacity readout). We clear that and retry, but why
1988 * causing spurious retries for no reason.
1990 * Revalidation may start with its own TEST_UNIT_READY, but that one
1991 * has to succeed, so we clear checks with an additional one here.
1992 * In any case it's not our business how revaliadation is implemented.
1994 for (i
= 0; i
< 3; i
++) { /* Retries for benh's key */
1995 if ((rc
= ub_sync_tur(sc
)) <= 0) break;
1996 if (rc
!= 0x6) break;
2000 sc
->removable
= 1; /* XXX Query this from the device */
2001 sc
->changed
= 1; /* ub_revalidate clears only */
2005 /* This is pretty much a long term P3 */
2006 printk(KERN_INFO
"%s: device %u capacity nsec %ld bsize %u\n",
2007 sc
->name
, sc
->dev
->devnum
, sc
->capacity
.nsec
, sc
->capacity
.bsize
);
2010 * Just one disk per sc currently, but maybe more.
2013 if ((disk
= alloc_disk(UB_MINORS_PER_MAJOR
)) == NULL
)
2017 sprintf(disk
->disk_name
, DRV_NAME
"%c", sc
->id
+ 'a');
2018 sprintf(disk
->devfs_name
, DEVFS_NAME
"/%c", sc
->id
+ 'a');
2019 disk
->major
= UB_MAJOR
;
2020 disk
->first_minor
= sc
->id
* UB_MINORS_PER_MAJOR
;
2021 disk
->fops
= &ub_bd_fops
;
2022 disk
->private_data
= sc
;
2023 disk
->driverfs_dev
= &intf
->dev
;
2026 if ((q
= blk_init_queue(ub_bd_rq_fn
, &sc
->lock
)) == NULL
)
2031 // blk_queue_bounce_limit(q, hba[i]->pdev->dma_mask);
2032 blk_queue_max_hw_segments(q
, UB_MAX_REQ_SG
);
2033 blk_queue_max_phys_segments(q
, UB_MAX_REQ_SG
);
2034 // blk_queue_segment_boundary(q, CARM_SG_BOUNDARY);
2035 blk_queue_max_sectors(q
, UB_MAX_SECTORS
);
2036 blk_queue_hardsect_size(q
, sc
->capacity
.bsize
);
2039 * This is a serious infraction, caused by a deficiency in the
2040 * USB sg interface (usb_sg_wait()). We plan to remove this once
2041 * we get mileage on the driver and can justify a change to USB API.
2042 * See blk_queue_bounce_limit() to understand this part.
2044 * XXX And I still need to be aware of the DMA mask in the HC.
2046 q
->bounce_pfn
= blk_max_low_pfn
;
2047 q
->bounce_gfp
= GFP_NOIO
;
2051 set_capacity(disk
, sc
->capacity
.nsec
);
2053 disk
->flags
|= GENHD_FL_REMOVABLE
;
2062 device_remove_file(&sc
->intf
->dev
, &dev_attr_diag
);
2064 usb_set_intfdata(intf
, NULL
);
2065 // usb_put_intf(sc->intf);
2066 usb_put_dev(sc
->dev
);
2074 static void ub_disconnect(struct usb_interface
*intf
)
2076 struct ub_dev
*sc
= usb_get_intfdata(intf
);
2077 struct gendisk
*disk
= sc
->disk
;
2078 unsigned long flags
;
2081 * Prevent ub_bd_release from pulling the rug from under us.
2082 * XXX This is starting to look like a kref.
2083 * XXX Why not to take this ref at probe time?
2085 spin_lock_irqsave(&ub_lock
, flags
);
2087 spin_unlock_irqrestore(&ub_lock
, flags
);
2090 * Fence stall clearnings, operations triggered by unlinkings and so on.
2091 * We do not attempt to unlink any URBs, because we do not trust the
2092 * unlink paths in HC drivers. Also, we get -84 upon disconnect anyway.
2094 atomic_set(&sc
->poison
, 1);
2097 * Blow away queued commands.
2099 * Actually, this never works, because before we get here
2100 * the HCD terminates outstanding URB(s). It causes our
2101 * SCSI command queue to advance, commands fail to submit,
2102 * and the whole queue drains. So, we just use this code to
2105 spin_lock_irqsave(&sc
->lock
, flags
);
2107 struct ub_scsi_cmd
*cmd
;
2109 while ((cmd
= ub_cmdq_pop(sc
)) != NULL
) {
2110 cmd
->error
= -ENOTCONN
;
2111 cmd
->state
= UB_CMDST_DONE
;
2112 ub_cmdtr_state(sc
, cmd
);
2114 (*cmd
->done
)(sc
, cmd
);
2118 printk(KERN_WARNING
"%s: "
2119 "%d was queued after shutdown\n", sc
->name
, cnt
);
2122 spin_unlock_irqrestore(&sc
->lock
, flags
);
2125 * Unregister the upper layer.
2127 if (disk
->flags
& GENHD_FL_UP
)
2130 * I wish I could do:
2131 * set_bit(QUEUE_FLAG_DEAD, &q->queue_flags);
2132 * As it is, we rely on our internal poisoning and let
2133 * the upper levels to spin furiously failing all the I/O.
2137 * Taking a lock on a structure which is about to be freed
2138 * is very nonsensual. Here it is largely a way to do a debug freeze,
2139 * and a bracket which shows where the nonsensual code segment ends.
2141 * Testing for -EINPROGRESS is always a bug, so we are bending
2142 * the rules a little.
2144 spin_lock_irqsave(&sc
->lock
, flags
);
2145 if (sc
->work_urb
.status
== -EINPROGRESS
) { /* janitors: ignore */
2146 printk(KERN_WARNING
"%s: "
2147 "URB is active after disconnect\n", sc
->name
);
2149 spin_unlock_irqrestore(&sc
->lock
, flags
);
2152 * There is virtually no chance that other CPU runs times so long
2153 * after ub_urb_complete should have called del_timer, but only if HCD
2154 * didn't forget to deliver a callback on unlink.
2156 del_timer_sync(&sc
->work_timer
);
2159 * At this point there must be no commands coming from anyone
2160 * and no URBs left in transit.
2163 device_remove_file(&sc
->intf
->dev
, &dev_attr_diag
);
2164 usb_set_intfdata(intf
, NULL
);
2165 // usb_put_intf(sc->intf);
2167 usb_put_dev(sc
->dev
);
2173 static struct usb_driver ub_driver
= {
2174 .owner
= THIS_MODULE
,
2177 .disconnect
= ub_disconnect
,
2178 .id_table
= ub_usb_ids
,
2181 static int __init
ub_init(void)
2185 /* P3 */ printk("ub: sizeof ub_scsi_cmd %zu ub_dev %zu\n",
2186 sizeof(struct ub_scsi_cmd
), sizeof(struct ub_dev
));
2188 if ((rc
= register_blkdev(UB_MAJOR
, DRV_NAME
)) != 0)
2190 devfs_mk_dir(DEVFS_NAME
);
2192 if ((rc
= usb_register(&ub_driver
)) != 0)
2198 devfs_remove(DEVFS_NAME
);
2199 unregister_blkdev(UB_MAJOR
, DRV_NAME
);
2204 static void __exit
ub_exit(void)
2206 usb_deregister(&ub_driver
);
2208 devfs_remove(DEVFS_NAME
);
2209 unregister_blkdev(UB_MAJOR
, DRV_NAME
);
2212 module_init(ub_init
);
2213 module_exit(ub_exit
);
2215 MODULE_LICENSE("GPL");