2 * The low performance USB storage driver (ub).
4 * Copyright (c) 1999, 2000 Matthew Dharm (mdharm-usb@one-eyed-alien.net)
5 * Copyright (C) 2004 Pete Zaitcev (zaitcev@yahoo.com)
7 * This work is a part of Linux kernel, is derived from it,
8 * and is not licensed separately. See file COPYING for details.
10 * TODO (sorted by decreasing priority)
11 * -- set readonly flag for CDs, set removable flag for CF readers
12 * -- do inquiry and verify we got a disk and not a tape (for LUN mismatch)
13 * -- verify the 13 conditions and do bulk resets
15 * -- move top_sense and work_bcs into separate allocations (if they survive)
16 * for cache purists and esoteric architectures.
17 * -- Allocate structure for LUN 0 before the first ub_sync_tur, avoid NULL. ?
18 * -- prune comments, they are too volumnous
20 * -- CLEAR, CLR2STS, CLRRS seem to be ripe for refactoring.
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/usb.h>
25 #include <linux/usb_usual.h>
26 #include <linux/blkdev.h>
27 #include <linux/timer.h>
28 #include <linux/scatterlist.h>
29 #include <scsi/scsi.h>
36 * The command state machine is the key model for understanding of this driver.
38 * The general rule is that all transitions are done towards the bottom
39 * of the diagram, thus preventing any loops.
41 * An exception to that is how the STAT state is handled. A counter allows it
42 * to be re-entered along the path marked with [C].
48 * ub_scsi_cmd_start fails ->--------------------------------------\
55 * was -EPIPE -->-------------------------------->! CLEAR ! !
58 * was error -->------------------------------------- ! --------->\
60 * /--<-- cmd->dir == NONE ? ! !
67 * ! was -EPIPE -->--------------->! CLR2STS ! ! !
70 * ! ! was error -->---- ! --------->\
71 * ! was error -->--------------------- ! ------------- ! --------->\
74 * \--->+--------+ ! ! !
75 * ! STAT !<--------------------------/ ! !
78 * [C] was -EPIPE -->-----------\ ! !
80 * +<---- len == 0 ! ! !
82 * ! was error -->--------------------------------------!---------->\
84 * +<---- bad CSW ! ! !
85 * +<---- bad tag ! ! !
91 * \------- ! --------------------[C]--------\ ! !
93 * cmd->error---\ +--------+ ! !
94 * ! +--------------->! SENSE !<----------/ !
95 * STAT_FAIL----/ +--------+ !
98 * \--------------------------------\--------------------->! DONE !
103 * This many LUNs per USB device.
104 * Every one of them takes a host, see UB_MAX_HOSTS.
106 #define UB_MAX_LUNS 9
111 #define UB_PARTS_PER_LUN 8
113 #define UB_MAX_CDB_SIZE 16 /* Corresponds to Bulk */
115 #define UB_SENSE_SIZE 18
120 /* command block wrapper */
121 struct bulk_cb_wrap
{
122 __le32 Signature
; /* contains 'USBC' */
123 u32 Tag
; /* unique per command id */
124 __le32 DataTransferLength
; /* size of data */
125 u8 Flags
; /* direction in bit 0 */
127 u8 Length
; /* of of the CDB */
128 u8 CDB
[UB_MAX_CDB_SIZE
]; /* max command */
131 #define US_BULK_CB_WRAP_LEN 31
132 #define US_BULK_CB_SIGN 0x43425355 /*spells out USBC */
133 #define US_BULK_FLAG_IN 1
134 #define US_BULK_FLAG_OUT 0
136 /* command status wrapper */
137 struct bulk_cs_wrap
{
138 __le32 Signature
; /* should = 'USBS' */
139 u32 Tag
; /* same as original command */
140 __le32 Residue
; /* amount not transferred */
141 u8 Status
; /* see below */
144 #define US_BULK_CS_WRAP_LEN 13
145 #define US_BULK_CS_SIGN 0x53425355 /* spells out 'USBS' */
146 #define US_BULK_STAT_OK 0
147 #define US_BULK_STAT_FAIL 1
148 #define US_BULK_STAT_PHASE 2
150 /* bulk-only class specific requests */
151 #define US_BULK_RESET_REQUEST 0xff
152 #define US_BULK_GET_MAX_LUN 0xfe
158 #define UB_MAX_REQ_SG 9 /* cdrecord requires 32KB and maybe a header */
159 #define UB_MAX_SECTORS 64
162 * A second is more than enough for a 32K transfer (UB_MAX_SECTORS)
163 * even if a webcam hogs the bus, but some devices need time to spin up.
165 #define UB_URB_TIMEOUT (HZ*2)
166 #define UB_DATA_TIMEOUT (HZ*5) /* ZIP does spin-ups in the data phase */
167 #define UB_STAT_TIMEOUT (HZ*5) /* Same spinups and eject for a dataless cmd. */
168 #define UB_CTRL_TIMEOUT (HZ/2) /* 500ms ought to be enough to clear a stall */
171 * An instance of a SCSI command in transit.
173 #define UB_DIR_NONE 0
174 #define UB_DIR_READ 1
175 #define UB_DIR_ILLEGAL2 2
176 #define UB_DIR_WRITE 3
178 #define UB_DIR_CHAR(c) (((c)==UB_DIR_WRITE)? 'w': \
179 (((c)==UB_DIR_READ)? 'r': 'n'))
181 enum ub_scsi_cmd_state
{
182 UB_CMDST_INIT
, /* Initial state */
183 UB_CMDST_CMD
, /* Command submitted */
184 UB_CMDST_DATA
, /* Data phase */
185 UB_CMDST_CLR2STS
, /* Clearing before requesting status */
186 UB_CMDST_STAT
, /* Status phase */
187 UB_CMDST_CLEAR
, /* Clearing a stall (halt, actually) */
188 UB_CMDST_CLRRS
, /* Clearing before retrying status */
189 UB_CMDST_SENSE
, /* Sending Request Sense */
190 UB_CMDST_DONE
/* Final state */
194 unsigned char cdb
[UB_MAX_CDB_SIZE
];
195 unsigned char cdb_len
;
197 unsigned char dir
; /* 0 - none, 1 - read, 3 - write. */
198 enum ub_scsi_cmd_state state
;
200 struct ub_scsi_cmd
*next
;
202 int error
; /* Return code - valid upon done */
203 unsigned int act_len
; /* Return size */
204 unsigned char key
, asc
, ascq
; /* May be valid if error==-EIO */
206 int stat_count
; /* Retries getting status. */
208 unsigned int len
; /* Requested length */
209 unsigned int current_sg
;
210 unsigned int nsg
; /* sgv[nsg] */
211 struct scatterlist sgv
[UB_MAX_REQ_SG
];
214 void (*done
)(struct ub_dev
*, struct ub_scsi_cmd
*);
220 unsigned int current_try
;
221 unsigned int nsg
; /* sgv[nsg] */
222 struct scatterlist sgv
[UB_MAX_REQ_SG
];
228 unsigned long nsec
; /* Linux size - 512 byte sectors */
229 unsigned int bsize
; /* Linux hardsect_size */
230 unsigned int bshift
; /* Shift between 512 and hard sects */
234 * This is a direct take-off from linux/include/completion.h
235 * The difference is that I do not wait on this thing, just poll.
236 * When I want to wait (ub_probe), I just use the stock completion.
238 * Note that INIT_COMPLETION takes no lock. It is correct. But why
239 * in the bloody hell that thing takes struct instead of pointer to struct
240 * is quite beyond me. I just copied it from the stock completion.
242 struct ub_completion
{
247 static inline void ub_init_completion(struct ub_completion
*x
)
250 spin_lock_init(&x
->lock
);
253 #define UB_INIT_COMPLETION(x) ((x).done = 0)
255 static void ub_complete(struct ub_completion
*x
)
259 spin_lock_irqsave(&x
->lock
, flags
);
261 spin_unlock_irqrestore(&x
->lock
, flags
);
264 static int ub_is_completed(struct ub_completion
*x
)
269 spin_lock_irqsave(&x
->lock
, flags
);
271 spin_unlock_irqrestore(&x
->lock
, flags
);
277 struct ub_scsi_cmd_queue
{
279 struct ub_scsi_cmd
*head
, *tail
;
283 * The block device instance (one per LUN).
287 struct list_head link
;
288 struct gendisk
*disk
;
289 int id
; /* Host index */
290 int num
; /* LUN number */
293 int changed
; /* Media was changed */
297 struct ub_request urq
;
299 /* Use Ingo's mempool if or when we have more than one command. */
301 * Currently we never need more than one command for the whole device.
302 * However, giving every LUN a command is a cheap and automatic way
303 * to enforce fairness between them.
306 struct ub_scsi_cmd cmdv
[1];
308 struct ub_capacity capacity
;
312 * The USB device instance.
316 atomic_t poison
; /* The USB device is disconnected */
317 int openc
; /* protected by ub_lock! */
318 /* kref is too implicit for our taste */
319 int reset
; /* Reset is running */
322 struct usb_device
*dev
;
323 struct usb_interface
*intf
;
325 struct list_head luns
;
327 unsigned int send_bulk_pipe
; /* cached pipe values */
328 unsigned int recv_bulk_pipe
;
329 unsigned int send_ctrl_pipe
;
330 unsigned int recv_ctrl_pipe
;
332 struct tasklet_struct tasklet
;
334 struct ub_scsi_cmd_queue cmd_queue
;
335 struct ub_scsi_cmd top_rqs_cmd
; /* REQUEST SENSE */
336 unsigned char top_sense
[UB_SENSE_SIZE
];
338 struct ub_completion work_done
;
340 struct timer_list work_timer
;
341 int last_pipe
; /* What might need clearing */
342 __le32 signature
; /* Learned signature */
343 struct bulk_cb_wrap work_bcb
;
344 struct bulk_cs_wrap work_bcs
;
345 struct usb_ctrlrequest work_cr
;
347 struct work_struct reset_work
;
348 wait_queue_head_t reset_wait
;
355 static void ub_cleanup(struct ub_dev
*sc
);
356 static int ub_request_fn_1(struct ub_lun
*lun
, struct request
*rq
);
357 static void ub_cmd_build_block(struct ub_dev
*sc
, struct ub_lun
*lun
,
358 struct ub_scsi_cmd
*cmd
, struct ub_request
*urq
);
359 static void ub_cmd_build_packet(struct ub_dev
*sc
, struct ub_lun
*lun
,
360 struct ub_scsi_cmd
*cmd
, struct ub_request
*urq
);
361 static void ub_rw_cmd_done(struct ub_dev
*sc
, struct ub_scsi_cmd
*cmd
);
362 static void ub_end_rq(struct request
*rq
, unsigned int status
);
363 static int ub_rw_cmd_retry(struct ub_dev
*sc
, struct ub_lun
*lun
,
364 struct ub_request
*urq
, struct ub_scsi_cmd
*cmd
);
365 static int ub_submit_scsi(struct ub_dev
*sc
, struct ub_scsi_cmd
*cmd
);
366 static void ub_urb_complete(struct urb
*urb
);
367 static void ub_scsi_action(unsigned long _dev
);
368 static void ub_scsi_dispatch(struct ub_dev
*sc
);
369 static void ub_scsi_urb_compl(struct ub_dev
*sc
, struct ub_scsi_cmd
*cmd
);
370 static void ub_data_start(struct ub_dev
*sc
, struct ub_scsi_cmd
*cmd
);
371 static void ub_state_done(struct ub_dev
*sc
, struct ub_scsi_cmd
*cmd
, int rc
);
372 static int __ub_state_stat(struct ub_dev
*sc
, struct ub_scsi_cmd
*cmd
);
373 static void ub_state_stat(struct ub_dev
*sc
, struct ub_scsi_cmd
*cmd
);
374 static void ub_state_stat_counted(struct ub_dev
*sc
, struct ub_scsi_cmd
*cmd
);
375 static void ub_state_sense(struct ub_dev
*sc
, struct ub_scsi_cmd
*cmd
);
376 static int ub_submit_clear_stall(struct ub_dev
*sc
, struct ub_scsi_cmd
*cmd
,
378 static void ub_top_sense_done(struct ub_dev
*sc
, struct ub_scsi_cmd
*scmd
);
379 static void ub_reset_enter(struct ub_dev
*sc
, int try);
380 static void ub_reset_task(struct work_struct
*work
);
381 static int ub_sync_tur(struct ub_dev
*sc
, struct ub_lun
*lun
);
382 static int ub_sync_read_cap(struct ub_dev
*sc
, struct ub_lun
*lun
,
383 struct ub_capacity
*ret
);
384 static int ub_sync_reset(struct ub_dev
*sc
);
385 static int ub_probe_clear_stall(struct ub_dev
*sc
, int stalled_pipe
);
386 static int ub_probe_lun(struct ub_dev
*sc
, int lnum
);
390 #ifdef CONFIG_USB_LIBUSUAL
392 #define ub_usb_ids storage_usb_ids
395 static struct usb_device_id ub_usb_ids
[] = {
396 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE
, US_SC_SCSI
, US_PR_BULK
) },
400 MODULE_DEVICE_TABLE(usb
, ub_usb_ids
);
401 #endif /* CONFIG_USB_LIBUSUAL */
404 * Find me a way to identify "next free minor" for add_disk(),
405 * and the array disappears the next day. However, the number of
406 * hosts has something to do with the naming and /proc/partitions.
407 * This has to be thought out in detail before changing.
408 * If UB_MAX_HOST was 1000, we'd use a bitmap. Or a better data structure.
410 #define UB_MAX_HOSTS 26
411 static char ub_hostv
[UB_MAX_HOSTS
];
413 #define UB_QLOCK_NUM 5
414 static spinlock_t ub_qlockv
[UB_QLOCK_NUM
];
415 static int ub_qlock_next
= 0;
417 static DEFINE_SPINLOCK(ub_lock
); /* Locks globals and ->openc */
422 * This also stores the host for indexing by minor, which is somewhat dirty.
424 static int ub_id_get(void)
429 spin_lock_irqsave(&ub_lock
, flags
);
430 for (i
= 0; i
< UB_MAX_HOSTS
; i
++) {
431 if (ub_hostv
[i
] == 0) {
433 spin_unlock_irqrestore(&ub_lock
, flags
);
437 spin_unlock_irqrestore(&ub_lock
, flags
);
441 static void ub_id_put(int id
)
445 if (id
< 0 || id
>= UB_MAX_HOSTS
) {
446 printk(KERN_ERR DRV_NAME
": bad host ID %d\n", id
);
450 spin_lock_irqsave(&ub_lock
, flags
);
451 if (ub_hostv
[id
] == 0) {
452 spin_unlock_irqrestore(&ub_lock
, flags
);
453 printk(KERN_ERR DRV_NAME
": freeing free host ID %d\n", id
);
457 spin_unlock_irqrestore(&ub_lock
, flags
);
461 * This is necessitated by the fact that blk_cleanup_queue does not
462 * necesserily destroy the queue. Instead, it may merely decrease q->refcnt.
463 * Since our blk_init_queue() passes a spinlock common with ub_dev,
464 * we have life time issues when ub_cleanup frees ub_dev.
466 static spinlock_t
*ub_next_lock(void)
471 spin_lock_irqsave(&ub_lock
, flags
);
472 ret
= &ub_qlockv
[ub_qlock_next
];
473 ub_qlock_next
= (ub_qlock_next
+ 1) % UB_QLOCK_NUM
;
474 spin_unlock_irqrestore(&ub_lock
, flags
);
479 * Downcount for deallocation. This rides on two assumptions:
480 * - once something is poisoned, its refcount cannot grow
481 * - opens cannot happen at this time (del_gendisk was done)
482 * If the above is true, we can drop the lock, which we need for
483 * blk_cleanup_queue(): the silly thing may attempt to sleep.
484 * [Actually, it never needs to sleep for us, but it calls might_sleep()]
486 static void ub_put(struct ub_dev
*sc
)
490 spin_lock_irqsave(&ub_lock
, flags
);
492 if (sc
->openc
== 0 && atomic_read(&sc
->poison
)) {
493 spin_unlock_irqrestore(&ub_lock
, flags
);
496 spin_unlock_irqrestore(&ub_lock
, flags
);
501 * Final cleanup and deallocation.
503 static void ub_cleanup(struct ub_dev
*sc
)
507 struct request_queue
*q
;
509 while (!list_empty(&sc
->luns
)) {
511 lun
= list_entry(p
, struct ub_lun
, link
);
514 /* I don't think queue can be NULL. But... Stolen from sx8.c */
515 if ((q
= lun
->disk
->queue
) != NULL
)
516 blk_cleanup_queue(q
);
518 * If we zero disk->private_data BEFORE put_disk, we have
519 * to check for NULL all over the place in open, release,
520 * check_media and revalidate, because the block level
521 * semaphore is well inside the put_disk.
522 * But we cannot zero after the call, because *disk is gone.
523 * The sd.c is blatantly racy in this area.
525 /* disk->private_data = NULL; */
533 usb_set_intfdata(sc
->intf
, NULL
);
534 usb_put_intf(sc
->intf
);
535 usb_put_dev(sc
->dev
);
540 * The "command allocator".
542 static struct ub_scsi_cmd
*ub_get_cmd(struct ub_lun
*lun
)
544 struct ub_scsi_cmd
*ret
;
553 static void ub_put_cmd(struct ub_lun
*lun
, struct ub_scsi_cmd
*cmd
)
555 if (cmd
!= &lun
->cmdv
[0]) {
556 printk(KERN_WARNING
"%s: releasing a foreign cmd %p\n",
561 printk(KERN_WARNING
"%s: releasing a free cmd\n", lun
->name
);
570 static void ub_cmdq_add(struct ub_dev
*sc
, struct ub_scsi_cmd
*cmd
)
572 struct ub_scsi_cmd_queue
*t
= &sc
->cmd_queue
;
574 if (t
->qlen
++ == 0) {
582 if (t
->qlen
> t
->qmax
)
586 static void ub_cmdq_insert(struct ub_dev
*sc
, struct ub_scsi_cmd
*cmd
)
588 struct ub_scsi_cmd_queue
*t
= &sc
->cmd_queue
;
590 if (t
->qlen
++ == 0) {
598 if (t
->qlen
> t
->qmax
)
602 static struct ub_scsi_cmd
*ub_cmdq_pop(struct ub_dev
*sc
)
604 struct ub_scsi_cmd_queue
*t
= &sc
->cmd_queue
;
605 struct ub_scsi_cmd
*cmd
;
617 #define ub_cmdq_peek(sc) ((sc)->cmd_queue.head)
620 * The request function is our main entry point
623 static void ub_request_fn(struct request_queue
*q
)
625 struct ub_lun
*lun
= q
->queuedata
;
628 while ((rq
= elv_next_request(q
)) != NULL
) {
629 if (ub_request_fn_1(lun
, rq
) != 0) {
636 static int ub_request_fn_1(struct ub_lun
*lun
, struct request
*rq
)
638 struct ub_dev
*sc
= lun
->udev
;
639 struct ub_scsi_cmd
*cmd
;
640 struct ub_request
*urq
;
643 if (atomic_read(&sc
->poison
)) {
644 blkdev_dequeue_request(rq
);
645 ub_end_rq(rq
, DID_NO_CONNECT
<< 16);
649 if (lun
->changed
&& !blk_pc_request(rq
)) {
650 blkdev_dequeue_request(rq
);
651 ub_end_rq(rq
, SAM_STAT_CHECK_CONDITION
);
655 if (lun
->urq
.rq
!= NULL
)
657 if ((cmd
= ub_get_cmd(lun
)) == NULL
)
659 memset(cmd
, 0, sizeof(struct ub_scsi_cmd
));
660 sg_init_table(cmd
->sgv
, UB_MAX_REQ_SG
);
662 blkdev_dequeue_request(rq
);
665 memset(urq
, 0, sizeof(struct ub_request
));
669 * get scatterlist from block layer
671 n_elem
= blk_rq_map_sg(lun
->disk
->queue
, rq
, &urq
->sgv
[0]);
673 /* Impossible, because blk_rq_map_sg should not hit ENOMEM. */
674 printk(KERN_INFO
"%s: failed request map (%d)\n",
678 if (n_elem
> UB_MAX_REQ_SG
) { /* Paranoia */
679 printk(KERN_WARNING
"%s: request with %d segments\n",
684 sc
->sg_stat
[n_elem
< 5 ? n_elem
: 5]++;
686 if (blk_pc_request(rq
)) {
687 ub_cmd_build_packet(sc
, lun
, cmd
, urq
);
689 ub_cmd_build_block(sc
, lun
, cmd
, urq
);
691 cmd
->state
= UB_CMDST_INIT
;
693 cmd
->done
= ub_rw_cmd_done
;
696 cmd
->tag
= sc
->tagcnt
++;
697 if (ub_submit_scsi(sc
, cmd
) != 0)
703 ub_put_cmd(lun
, cmd
);
704 ub_end_rq(rq
, DID_ERROR
<< 16);
708 static void ub_cmd_build_block(struct ub_dev
*sc
, struct ub_lun
*lun
,
709 struct ub_scsi_cmd
*cmd
, struct ub_request
*urq
)
711 struct request
*rq
= urq
->rq
;
712 unsigned int block
, nblks
;
714 if (rq_data_dir(rq
) == WRITE
)
715 cmd
->dir
= UB_DIR_WRITE
;
717 cmd
->dir
= UB_DIR_READ
;
720 memcpy(cmd
->sgv
, urq
->sgv
, sizeof(struct scatterlist
) * cmd
->nsg
);
725 * The call to blk_queue_hardsect_size() guarantees that request
726 * is aligned, but it is given in terms of 512 byte units, always.
728 block
= rq
->sector
>> lun
->capacity
.bshift
;
729 nblks
= rq
->nr_sectors
>> lun
->capacity
.bshift
;
731 cmd
->cdb
[0] = (cmd
->dir
== UB_DIR_READ
)? READ_10
: WRITE_10
;
732 /* 10-byte uses 4 bytes of LBA: 2147483648KB, 2097152MB, 2048GB */
733 cmd
->cdb
[2] = block
>> 24;
734 cmd
->cdb
[3] = block
>> 16;
735 cmd
->cdb
[4] = block
>> 8;
737 cmd
->cdb
[7] = nblks
>> 8;
741 cmd
->len
= rq
->nr_sectors
* 512;
744 static void ub_cmd_build_packet(struct ub_dev
*sc
, struct ub_lun
*lun
,
745 struct ub_scsi_cmd
*cmd
, struct ub_request
*urq
)
747 struct request
*rq
= urq
->rq
;
749 if (rq
->data_len
== 0) {
750 cmd
->dir
= UB_DIR_NONE
;
752 if (rq_data_dir(rq
) == WRITE
)
753 cmd
->dir
= UB_DIR_WRITE
;
755 cmd
->dir
= UB_DIR_READ
;
759 memcpy(cmd
->sgv
, urq
->sgv
, sizeof(struct scatterlist
) * cmd
->nsg
);
761 memcpy(&cmd
->cdb
, rq
->cmd
, rq
->cmd_len
);
762 cmd
->cdb_len
= rq
->cmd_len
;
764 cmd
->len
= rq
->data_len
;
767 static void ub_rw_cmd_done(struct ub_dev
*sc
, struct ub_scsi_cmd
*cmd
)
769 struct ub_lun
*lun
= cmd
->lun
;
770 struct ub_request
*urq
= cmd
->back
;
772 unsigned int scsi_status
;
776 if (cmd
->error
== 0) {
777 if (blk_pc_request(rq
)) {
778 if (cmd
->act_len
>= rq
->data_len
)
781 rq
->data_len
-= cmd
->act_len
;
785 if (blk_pc_request(rq
)) {
786 /* UB_SENSE_SIZE is smaller than SCSI_SENSE_BUFFERSIZE */
787 memcpy(rq
->sense
, sc
->top_sense
, UB_SENSE_SIZE
);
788 rq
->sense_len
= UB_SENSE_SIZE
;
789 if (sc
->top_sense
[0] != 0)
790 scsi_status
= SAM_STAT_CHECK_CONDITION
;
792 scsi_status
= DID_ERROR
<< 16;
794 if (cmd
->error
== -EIO
) {
795 if (ub_rw_cmd_retry(sc
, lun
, urq
, cmd
) == 0)
798 scsi_status
= SAM_STAT_CHECK_CONDITION
;
804 ub_put_cmd(lun
, cmd
);
805 ub_end_rq(rq
, scsi_status
);
806 blk_start_queue(lun
->disk
->queue
);
809 static void ub_end_rq(struct request
*rq
, unsigned int scsi_status
)
813 if (scsi_status
== 0) {
817 rq
->errors
= scsi_status
;
819 end_that_request_first(rq
, uptodate
, rq
->hard_nr_sectors
);
820 end_that_request_last(rq
, uptodate
);
823 static int ub_rw_cmd_retry(struct ub_dev
*sc
, struct ub_lun
*lun
,
824 struct ub_request
*urq
, struct ub_scsi_cmd
*cmd
)
827 if (atomic_read(&sc
->poison
))
830 ub_reset_enter(sc
, urq
->current_try
);
832 if (urq
->current_try
>= 3)
836 /* Remove this if anyone complains of flooding. */
837 printk(KERN_DEBUG
"%s: dir %c len/act %d/%d "
838 "[sense %x %02x %02x] retry %d\n",
839 sc
->name
, UB_DIR_CHAR(cmd
->dir
), cmd
->len
, cmd
->act_len
,
840 cmd
->key
, cmd
->asc
, cmd
->ascq
, urq
->current_try
);
842 memset(cmd
, 0, sizeof(struct ub_scsi_cmd
));
843 ub_cmd_build_block(sc
, lun
, cmd
, urq
);
845 cmd
->state
= UB_CMDST_INIT
;
847 cmd
->done
= ub_rw_cmd_done
;
850 cmd
->tag
= sc
->tagcnt
++;
853 return ub_submit_scsi(sc
, cmd
);
855 ub_cmdq_add(sc
, cmd
);
861 * Submit a regular SCSI operation (not an auto-sense).
863 * The Iron Law of Good Submit Routine is:
864 * Zero return - callback is done, Nonzero return - callback is not done.
867 * Host is assumed locked.
869 static int ub_submit_scsi(struct ub_dev
*sc
, struct ub_scsi_cmd
*cmd
)
872 if (cmd
->state
!= UB_CMDST_INIT
||
873 (cmd
->dir
!= UB_DIR_NONE
&& cmd
->len
== 0)) {
877 ub_cmdq_add(sc
, cmd
);
879 * We can call ub_scsi_dispatch(sc) right away here, but it's a little
880 * safer to jump to a tasklet, in case upper layers do something silly.
882 tasklet_schedule(&sc
->tasklet
);
887 * Submit the first URB for the queued command.
888 * This function does not deal with queueing in any way.
890 static int ub_scsi_cmd_start(struct ub_dev
*sc
, struct ub_scsi_cmd
*cmd
)
892 struct bulk_cb_wrap
*bcb
;
898 * ``If the allocation length is eighteen or greater, and a device
899 * server returns less than eithteen bytes of data, the application
900 * client should assume that the bytes not transferred would have been
901 * zeroes had the device server returned those bytes.''
903 * We zero sense for all commands so that when a packet request
904 * fails it does not return a stale sense.
906 memset(&sc
->top_sense
, 0, UB_SENSE_SIZE
);
908 /* set up the command wrapper */
909 bcb
->Signature
= cpu_to_le32(US_BULK_CB_SIGN
);
910 bcb
->Tag
= cmd
->tag
; /* Endianness is not important */
911 bcb
->DataTransferLength
= cpu_to_le32(cmd
->len
);
912 bcb
->Flags
= (cmd
->dir
== UB_DIR_READ
) ? 0x80 : 0;
913 bcb
->Lun
= (cmd
->lun
!= NULL
) ? cmd
->lun
->num
: 0;
914 bcb
->Length
= cmd
->cdb_len
;
916 /* copy the command payload */
917 memcpy(bcb
->CDB
, cmd
->cdb
, UB_MAX_CDB_SIZE
);
919 UB_INIT_COMPLETION(sc
->work_done
);
921 sc
->last_pipe
= sc
->send_bulk_pipe
;
922 usb_fill_bulk_urb(&sc
->work_urb
, sc
->dev
, sc
->send_bulk_pipe
,
923 bcb
, US_BULK_CB_WRAP_LEN
, ub_urb_complete
, sc
);
925 /* Fill what we shouldn't be filling, because usb-storage did so. */
926 sc
->work_urb
.actual_length
= 0;
927 sc
->work_urb
.error_count
= 0;
928 sc
->work_urb
.status
= 0;
930 if ((rc
= usb_submit_urb(&sc
->work_urb
, GFP_ATOMIC
)) != 0) {
931 /* XXX Clear stalls */
932 ub_complete(&sc
->work_done
);
936 sc
->work_timer
.expires
= jiffies
+ UB_URB_TIMEOUT
;
937 add_timer(&sc
->work_timer
);
939 cmd
->state
= UB_CMDST_CMD
;
946 static void ub_urb_timeout(unsigned long arg
)
948 struct ub_dev
*sc
= (struct ub_dev
*) arg
;
951 spin_lock_irqsave(sc
->lock
, flags
);
952 if (!ub_is_completed(&sc
->work_done
))
953 usb_unlink_urb(&sc
->work_urb
);
954 spin_unlock_irqrestore(sc
->lock
, flags
);
958 * Completion routine for the work URB.
960 * This can be called directly from usb_submit_urb (while we have
961 * the sc->lock taken) and from an interrupt (while we do NOT have
962 * the sc->lock taken). Therefore, bounce this off to a tasklet.
964 static void ub_urb_complete(struct urb
*urb
)
966 struct ub_dev
*sc
= urb
->context
;
968 ub_complete(&sc
->work_done
);
969 tasklet_schedule(&sc
->tasklet
);
972 static void ub_scsi_action(unsigned long _dev
)
974 struct ub_dev
*sc
= (struct ub_dev
*) _dev
;
977 spin_lock_irqsave(sc
->lock
, flags
);
978 ub_scsi_dispatch(sc
);
979 spin_unlock_irqrestore(sc
->lock
, flags
);
982 static void ub_scsi_dispatch(struct ub_dev
*sc
)
984 struct ub_scsi_cmd
*cmd
;
987 while (!sc
->reset
&& (cmd
= ub_cmdq_peek(sc
)) != NULL
) {
988 if (cmd
->state
== UB_CMDST_DONE
) {
990 (*cmd
->done
)(sc
, cmd
);
991 } else if (cmd
->state
== UB_CMDST_INIT
) {
992 if ((rc
= ub_scsi_cmd_start(sc
, cmd
)) == 0)
995 cmd
->state
= UB_CMDST_DONE
;
997 if (!ub_is_completed(&sc
->work_done
))
999 del_timer(&sc
->work_timer
);
1000 ub_scsi_urb_compl(sc
, cmd
);
1005 static void ub_scsi_urb_compl(struct ub_dev
*sc
, struct ub_scsi_cmd
*cmd
)
1007 struct urb
*urb
= &sc
->work_urb
;
1008 struct bulk_cs_wrap
*bcs
;
1012 if (atomic_read(&sc
->poison
)) {
1013 ub_state_done(sc
, cmd
, -ENODEV
);
1017 if (cmd
->state
== UB_CMDST_CLEAR
) {
1018 if (urb
->status
== -EPIPE
) {
1020 * STALL while clearning STALL.
1021 * The control pipe clears itself - nothing to do.
1023 printk(KERN_NOTICE
"%s: stall on control pipe\n",
1029 * We ignore the result for the halt clear.
1032 /* reset the endpoint toggle */
1033 usb_settoggle(sc
->dev
, usb_pipeendpoint(sc
->last_pipe
),
1034 usb_pipeout(sc
->last_pipe
), 0);
1036 ub_state_sense(sc
, cmd
);
1038 } else if (cmd
->state
== UB_CMDST_CLR2STS
) {
1039 if (urb
->status
== -EPIPE
) {
1040 printk(KERN_NOTICE
"%s: stall on control pipe\n",
1046 * We ignore the result for the halt clear.
1049 /* reset the endpoint toggle */
1050 usb_settoggle(sc
->dev
, usb_pipeendpoint(sc
->last_pipe
),
1051 usb_pipeout(sc
->last_pipe
), 0);
1053 ub_state_stat(sc
, cmd
);
1055 } else if (cmd
->state
== UB_CMDST_CLRRS
) {
1056 if (urb
->status
== -EPIPE
) {
1057 printk(KERN_NOTICE
"%s: stall on control pipe\n",
1063 * We ignore the result for the halt clear.
1066 /* reset the endpoint toggle */
1067 usb_settoggle(sc
->dev
, usb_pipeendpoint(sc
->last_pipe
),
1068 usb_pipeout(sc
->last_pipe
), 0);
1070 ub_state_stat_counted(sc
, cmd
);
1072 } else if (cmd
->state
== UB_CMDST_CMD
) {
1073 switch (urb
->status
) {
1079 rc
= ub_submit_clear_stall(sc
, cmd
, sc
->last_pipe
);
1081 printk(KERN_NOTICE
"%s: "
1082 "unable to submit clear (%d)\n",
1085 * This is typically ENOMEM or some other such shit.
1086 * Retrying is pointless. Just do Bad End on it...
1088 ub_state_done(sc
, cmd
, rc
);
1091 cmd
->state
= UB_CMDST_CLEAR
;
1093 case -ESHUTDOWN
: /* unplug */
1094 case -EILSEQ
: /* unplug timeout on uhci */
1095 ub_state_done(sc
, cmd
, -ENODEV
);
1100 if (urb
->actual_length
!= US_BULK_CB_WRAP_LEN
) {
1104 if (cmd
->dir
== UB_DIR_NONE
|| cmd
->nsg
< 1) {
1105 ub_state_stat(sc
, cmd
);
1109 // udelay(125); // usb-storage has this
1110 ub_data_start(sc
, cmd
);
1112 } else if (cmd
->state
== UB_CMDST_DATA
) {
1113 if (urb
->status
== -EPIPE
) {
1114 rc
= ub_submit_clear_stall(sc
, cmd
, sc
->last_pipe
);
1116 printk(KERN_NOTICE
"%s: "
1117 "unable to submit clear (%d)\n",
1119 ub_state_done(sc
, cmd
, rc
);
1122 cmd
->state
= UB_CMDST_CLR2STS
;
1125 if (urb
->status
== -EOVERFLOW
) {
1127 * A babble? Failure, but we must transfer CSW now.
1129 cmd
->error
= -EOVERFLOW
; /* A cheap trick... */
1130 ub_state_stat(sc
, cmd
);
1134 if (cmd
->dir
== UB_DIR_WRITE
) {
1136 * Do not continue writes in case of a failure.
1137 * Doing so would cause sectors to be mixed up,
1138 * which is worse than sectors lost.
1140 * We must try to read the CSW, or many devices
1143 len
= urb
->actual_length
;
1144 if (urb
->status
!= 0 ||
1145 len
!= cmd
->sgv
[cmd
->current_sg
].length
) {
1146 cmd
->act_len
+= len
;
1149 ub_state_stat(sc
, cmd
);
1155 * If an error occurs on read, we record it, and
1156 * continue to fetch data in order to avoid bubble.
1158 * As a small shortcut, we stop if we detect that
1159 * a CSW mixed into data.
1161 if (urb
->status
!= 0)
1164 len
= urb
->actual_length
;
1165 if (urb
->status
!= 0 ||
1166 len
!= cmd
->sgv
[cmd
->current_sg
].length
) {
1167 if ((len
& 0x1FF) == US_BULK_CS_WRAP_LEN
)
1172 cmd
->act_len
+= urb
->actual_length
;
1174 if (++cmd
->current_sg
< cmd
->nsg
) {
1175 ub_data_start(sc
, cmd
);
1178 ub_state_stat(sc
, cmd
);
1180 } else if (cmd
->state
== UB_CMDST_STAT
) {
1181 if (urb
->status
== -EPIPE
) {
1182 rc
= ub_submit_clear_stall(sc
, cmd
, sc
->last_pipe
);
1184 printk(KERN_NOTICE
"%s: "
1185 "unable to submit clear (%d)\n",
1187 ub_state_done(sc
, cmd
, rc
);
1192 * Having a stall when getting CSW is an error, so
1193 * make sure uppper levels are not oblivious to it.
1195 cmd
->error
= -EIO
; /* A cheap trick... */
1197 cmd
->state
= UB_CMDST_CLRRS
;
1201 /* Catch everything, including -EOVERFLOW and other nasties. */
1202 if (urb
->status
!= 0)
1205 if (urb
->actual_length
== 0) {
1206 ub_state_stat_counted(sc
, cmd
);
1211 * Check the returned Bulk protocol status.
1212 * The status block has to be validated first.
1215 bcs
= &sc
->work_bcs
;
1217 if (sc
->signature
== cpu_to_le32(0)) {
1219 * This is the first reply, so do not perform the check.
1220 * Instead, remember the signature the device uses
1221 * for future checks. But do not allow a nul.
1223 sc
->signature
= bcs
->Signature
;
1224 if (sc
->signature
== cpu_to_le32(0)) {
1225 ub_state_stat_counted(sc
, cmd
);
1229 if (bcs
->Signature
!= sc
->signature
) {
1230 ub_state_stat_counted(sc
, cmd
);
1235 if (bcs
->Tag
!= cmd
->tag
) {
1237 * This usually happens when we disagree with the
1238 * device's microcode about something. For instance,
1239 * a few of them throw this after timeouts. They buffer
1240 * commands and reply at commands we timed out before.
1241 * Without flushing these replies we loop forever.
1243 ub_state_stat_counted(sc
, cmd
);
1247 len
= le32_to_cpu(bcs
->Residue
);
1248 if (len
!= cmd
->len
- cmd
->act_len
) {
1250 * It is all right to transfer less, the caller has
1251 * to check. But it's not all right if the device
1252 * counts disagree with our counts.
1257 switch (bcs
->Status
) {
1258 case US_BULK_STAT_OK
:
1260 case US_BULK_STAT_FAIL
:
1261 ub_state_sense(sc
, cmd
);
1263 case US_BULK_STAT_PHASE
:
1266 printk(KERN_INFO
"%s: unknown CSW status 0x%x\n",
1267 sc
->name
, bcs
->Status
);
1268 ub_state_done(sc
, cmd
, -EINVAL
);
1272 /* Not zeroing error to preserve a babble indicator */
1273 if (cmd
->error
!= 0) {
1274 ub_state_sense(sc
, cmd
);
1277 cmd
->state
= UB_CMDST_DONE
;
1279 (*cmd
->done
)(sc
, cmd
);
1281 } else if (cmd
->state
== UB_CMDST_SENSE
) {
1282 ub_state_done(sc
, cmd
, -EIO
);
1285 printk(KERN_WARNING
"%s: "
1286 "wrong command state %d\n",
1287 sc
->name
, cmd
->state
);
1288 ub_state_done(sc
, cmd
, -EINVAL
);
1293 Bad_End
: /* Little Excel is dead */
1294 ub_state_done(sc
, cmd
, -EIO
);
1298 * Factorization helper for the command state machine:
1299 * Initiate a data segment transfer.
1301 static void ub_data_start(struct ub_dev
*sc
, struct ub_scsi_cmd
*cmd
)
1303 struct scatterlist
*sg
= &cmd
->sgv
[cmd
->current_sg
];
1307 UB_INIT_COMPLETION(sc
->work_done
);
1309 if (cmd
->dir
== UB_DIR_READ
)
1310 pipe
= sc
->recv_bulk_pipe
;
1312 pipe
= sc
->send_bulk_pipe
;
1313 sc
->last_pipe
= pipe
;
1314 usb_fill_bulk_urb(&sc
->work_urb
, sc
->dev
, pipe
, sg_virt(sg
),
1315 sg
->length
, ub_urb_complete
, sc
);
1316 sc
->work_urb
.actual_length
= 0;
1317 sc
->work_urb
.error_count
= 0;
1318 sc
->work_urb
.status
= 0;
1320 if ((rc
= usb_submit_urb(&sc
->work_urb
, GFP_ATOMIC
)) != 0) {
1321 /* XXX Clear stalls */
1322 ub_complete(&sc
->work_done
);
1323 ub_state_done(sc
, cmd
, rc
);
1327 sc
->work_timer
.expires
= jiffies
+ UB_DATA_TIMEOUT
;
1328 add_timer(&sc
->work_timer
);
1330 cmd
->state
= UB_CMDST_DATA
;
1334 * Factorization helper for the command state machine:
1335 * Finish the command.
1337 static void ub_state_done(struct ub_dev
*sc
, struct ub_scsi_cmd
*cmd
, int rc
)
1341 cmd
->state
= UB_CMDST_DONE
;
1343 (*cmd
->done
)(sc
, cmd
);
1347 * Factorization helper for the command state machine:
1348 * Submit a CSW read.
1350 static int __ub_state_stat(struct ub_dev
*sc
, struct ub_scsi_cmd
*cmd
)
1354 UB_INIT_COMPLETION(sc
->work_done
);
1356 sc
->last_pipe
= sc
->recv_bulk_pipe
;
1357 usb_fill_bulk_urb(&sc
->work_urb
, sc
->dev
, sc
->recv_bulk_pipe
,
1358 &sc
->work_bcs
, US_BULK_CS_WRAP_LEN
, ub_urb_complete
, sc
);
1359 sc
->work_urb
.actual_length
= 0;
1360 sc
->work_urb
.error_count
= 0;
1361 sc
->work_urb
.status
= 0;
1363 if ((rc
= usb_submit_urb(&sc
->work_urb
, GFP_ATOMIC
)) != 0) {
1364 /* XXX Clear stalls */
1365 ub_complete(&sc
->work_done
);
1366 ub_state_done(sc
, cmd
, rc
);
1370 sc
->work_timer
.expires
= jiffies
+ UB_STAT_TIMEOUT
;
1371 add_timer(&sc
->work_timer
);
1376 * Factorization helper for the command state machine:
1377 * Submit a CSW read and go to STAT state.
1379 static void ub_state_stat(struct ub_dev
*sc
, struct ub_scsi_cmd
*cmd
)
1382 if (__ub_state_stat(sc
, cmd
) != 0)
1385 cmd
->stat_count
= 0;
1386 cmd
->state
= UB_CMDST_STAT
;
1390 * Factorization helper for the command state machine:
1391 * Submit a CSW read and go to STAT state with counter (along [C] path).
1393 static void ub_state_stat_counted(struct ub_dev
*sc
, struct ub_scsi_cmd
*cmd
)
1396 if (++cmd
->stat_count
>= 4) {
1397 ub_state_sense(sc
, cmd
);
1401 if (__ub_state_stat(sc
, cmd
) != 0)
1404 cmd
->state
= UB_CMDST_STAT
;
1408 * Factorization helper for the command state machine:
1409 * Submit a REQUEST SENSE and go to SENSE state.
1411 static void ub_state_sense(struct ub_dev
*sc
, struct ub_scsi_cmd
*cmd
)
1413 struct ub_scsi_cmd
*scmd
;
1414 struct scatterlist
*sg
;
1417 if (cmd
->cdb
[0] == REQUEST_SENSE
) {
1422 scmd
= &sc
->top_rqs_cmd
;
1423 memset(scmd
, 0, sizeof(struct ub_scsi_cmd
));
1424 scmd
->cdb
[0] = REQUEST_SENSE
;
1425 scmd
->cdb
[4] = UB_SENSE_SIZE
;
1427 scmd
->dir
= UB_DIR_READ
;
1428 scmd
->state
= UB_CMDST_INIT
;
1431 sg_set_page(sg
, virt_to_page(sc
->top_sense
), UB_SENSE_SIZE
,
1432 (unsigned long)sc
->top_sense
& (PAGE_SIZE
-1));
1433 scmd
->len
= UB_SENSE_SIZE
;
1434 scmd
->lun
= cmd
->lun
;
1435 scmd
->done
= ub_top_sense_done
;
1438 scmd
->tag
= sc
->tagcnt
++;
1440 cmd
->state
= UB_CMDST_SENSE
;
1442 ub_cmdq_insert(sc
, scmd
);
1446 ub_state_done(sc
, cmd
, rc
);
1450 * A helper for the command's state machine:
1451 * Submit a stall clear.
1453 static int ub_submit_clear_stall(struct ub_dev
*sc
, struct ub_scsi_cmd
*cmd
,
1457 struct usb_ctrlrequest
*cr
;
1460 endp
= usb_pipeendpoint(stalled_pipe
);
1461 if (usb_pipein (stalled_pipe
))
1465 cr
->bRequestType
= USB_RECIP_ENDPOINT
;
1466 cr
->bRequest
= USB_REQ_CLEAR_FEATURE
;
1467 cr
->wValue
= cpu_to_le16(USB_ENDPOINT_HALT
);
1468 cr
->wIndex
= cpu_to_le16(endp
);
1469 cr
->wLength
= cpu_to_le16(0);
1471 UB_INIT_COMPLETION(sc
->work_done
);
1473 usb_fill_control_urb(&sc
->work_urb
, sc
->dev
, sc
->send_ctrl_pipe
,
1474 (unsigned char*) cr
, NULL
, 0, ub_urb_complete
, sc
);
1475 sc
->work_urb
.actual_length
= 0;
1476 sc
->work_urb
.error_count
= 0;
1477 sc
->work_urb
.status
= 0;
1479 if ((rc
= usb_submit_urb(&sc
->work_urb
, GFP_ATOMIC
)) != 0) {
1480 ub_complete(&sc
->work_done
);
1484 sc
->work_timer
.expires
= jiffies
+ UB_CTRL_TIMEOUT
;
1485 add_timer(&sc
->work_timer
);
1491 static void ub_top_sense_done(struct ub_dev
*sc
, struct ub_scsi_cmd
*scmd
)
1493 unsigned char *sense
= sc
->top_sense
;
1494 struct ub_scsi_cmd
*cmd
;
1497 * Find the command which triggered the unit attention or a check,
1498 * save the sense into it, and advance its state machine.
1500 if ((cmd
= ub_cmdq_peek(sc
)) == NULL
) {
1501 printk(KERN_WARNING
"%s: sense done while idle\n", sc
->name
);
1504 if (cmd
!= scmd
->back
) {
1505 printk(KERN_WARNING
"%s: "
1506 "sense done for wrong command 0x%x\n",
1507 sc
->name
, cmd
->tag
);
1510 if (cmd
->state
!= UB_CMDST_SENSE
) {
1511 printk(KERN_WARNING
"%s: "
1512 "sense done with bad cmd state %d\n",
1513 sc
->name
, cmd
->state
);
1518 * Ignoring scmd->act_len, because the buffer was pre-zeroed.
1520 cmd
->key
= sense
[2] & 0x0F;
1521 cmd
->asc
= sense
[12];
1522 cmd
->ascq
= sense
[13];
1524 ub_scsi_urb_compl(sc
, cmd
);
1529 * XXX Move usb_reset_device to khubd. Hogging kevent is not a good thing.
1530 * XXX Make usb_sync_reset asynchronous.
1533 static void ub_reset_enter(struct ub_dev
*sc
, int try)
1537 /* This happens often on multi-LUN devices. */
1540 sc
->reset
= try + 1;
1542 #if 0 /* Not needed because the disconnect waits for us. */
1543 unsigned long flags
;
1544 spin_lock_irqsave(&ub_lock
, flags
);
1546 spin_unlock_irqrestore(&ub_lock
, flags
);
1549 #if 0 /* We let them stop themselves. */
1551 list_for_each_entry(lun
, &sc
->luns
, link
) {
1552 blk_stop_queue(lun
->disk
->queue
);
1556 schedule_work(&sc
->reset_work
);
1559 static void ub_reset_task(struct work_struct
*work
)
1561 struct ub_dev
*sc
= container_of(work
, struct ub_dev
, reset_work
);
1562 unsigned long flags
;
1567 printk(KERN_WARNING
"%s: Running reset unrequested\n",
1572 if (atomic_read(&sc
->poison
)) {
1574 } else if ((sc
->reset
& 1) == 0) {
1576 msleep(700); /* usb-storage sleeps 6s (!) */
1577 ub_probe_clear_stall(sc
, sc
->recv_bulk_pipe
);
1578 ub_probe_clear_stall(sc
, sc
->send_bulk_pipe
);
1579 } else if (sc
->dev
->actconfig
->desc
.bNumInterfaces
!= 1) {
1582 if ((lkr
= usb_lock_device_for_reset(sc
->dev
, sc
->intf
)) < 0) {
1584 "%s: usb_lock_device_for_reset failed (%d)\n",
1587 rc
= usb_reset_device(sc
->dev
);
1589 printk(KERN_NOTICE
"%s: "
1590 "usb_lock_device_for_reset failed (%d)\n",
1595 usb_unlock_device(sc
->dev
);
1600 * In theory, no commands can be running while reset is active,
1601 * so nobody can ask for another reset, and so we do not need any
1602 * queues of resets or anything. We do need a spinlock though,
1603 * to interact with block layer.
1605 spin_lock_irqsave(sc
->lock
, flags
);
1607 tasklet_schedule(&sc
->tasklet
);
1608 list_for_each_entry(lun
, &sc
->luns
, link
) {
1609 blk_start_queue(lun
->disk
->queue
);
1611 wake_up(&sc
->reset_wait
);
1612 spin_unlock_irqrestore(sc
->lock
, flags
);
1616 * This is called from a process context.
1618 static void ub_revalidate(struct ub_dev
*sc
, struct ub_lun
*lun
)
1621 lun
->readonly
= 0; /* XXX Query this from the device */
1623 lun
->capacity
.nsec
= 0;
1624 lun
->capacity
.bsize
= 512;
1625 lun
->capacity
.bshift
= 0;
1627 if (ub_sync_tur(sc
, lun
) != 0)
1628 return; /* Not ready */
1631 if (ub_sync_read_cap(sc
, lun
, &lun
->capacity
) != 0) {
1633 * The retry here means something is wrong, either with the
1634 * device, with the transport, or with our code.
1635 * We keep this because sd.c has retries for capacity.
1637 if (ub_sync_read_cap(sc
, lun
, &lun
->capacity
) != 0) {
1638 lun
->capacity
.nsec
= 0;
1639 lun
->capacity
.bsize
= 512;
1640 lun
->capacity
.bshift
= 0;
1647 * This is mostly needed to keep refcounting, but also to support
1648 * media checks on removable media drives.
1650 static int ub_bd_open(struct inode
*inode
, struct file
*filp
)
1652 struct gendisk
*disk
= inode
->i_bdev
->bd_disk
;
1653 struct ub_lun
*lun
= disk
->private_data
;
1654 struct ub_dev
*sc
= lun
->udev
;
1655 unsigned long flags
;
1658 spin_lock_irqsave(&ub_lock
, flags
);
1659 if (atomic_read(&sc
->poison
)) {
1660 spin_unlock_irqrestore(&ub_lock
, flags
);
1664 spin_unlock_irqrestore(&ub_lock
, flags
);
1666 if (lun
->removable
|| lun
->readonly
)
1667 check_disk_change(inode
->i_bdev
);
1670 * The sd.c considers ->media_present and ->changed not equivalent,
1671 * under some pretty murky conditions (a failure of READ CAPACITY).
1672 * We may need it one day.
1674 if (lun
->removable
&& lun
->changed
&& !(filp
->f_flags
& O_NDELAY
)) {
1679 if (lun
->readonly
&& (filp
->f_mode
& FMODE_WRITE
)) {
1693 static int ub_bd_release(struct inode
*inode
, struct file
*filp
)
1695 struct gendisk
*disk
= inode
->i_bdev
->bd_disk
;
1696 struct ub_lun
*lun
= disk
->private_data
;
1697 struct ub_dev
*sc
= lun
->udev
;
1704 * The ioctl interface.
1706 static int ub_bd_ioctl(struct inode
*inode
, struct file
*filp
,
1707 unsigned int cmd
, unsigned long arg
)
1709 struct gendisk
*disk
= inode
->i_bdev
->bd_disk
;
1710 void __user
*usermem
= (void __user
*) arg
;
1712 return scsi_cmd_ioctl(filp
, disk
->queue
, disk
, cmd
, usermem
);
1716 * This is called once a new disk was seen by the block layer or by ub_probe().
1717 * The main onjective here is to discover the features of the media such as
1718 * the capacity, read-only status, etc. USB storage generally does not
1719 * need to be spun up, but if we needed it, this would be the place.
1721 * This call can sleep.
1723 * The return code is not used.
1725 static int ub_bd_revalidate(struct gendisk
*disk
)
1727 struct ub_lun
*lun
= disk
->private_data
;
1729 ub_revalidate(lun
->udev
, lun
);
1731 /* XXX Support sector size switching like in sr.c */
1732 blk_queue_hardsect_size(disk
->queue
, lun
->capacity
.bsize
);
1733 set_capacity(disk
, lun
->capacity
.nsec
);
1734 // set_disk_ro(sdkp->disk, lun->readonly);
1740 * The check is called by the block layer to verify if the media
1741 * is still available. It is supposed to be harmless, lightweight and
1742 * non-intrusive in case the media was not changed.
1744 * This call can sleep.
1746 * The return code is bool!
1748 static int ub_bd_media_changed(struct gendisk
*disk
)
1750 struct ub_lun
*lun
= disk
->private_data
;
1752 if (!lun
->removable
)
1756 * We clean checks always after every command, so this is not
1757 * as dangerous as it looks. If the TEST_UNIT_READY fails here,
1758 * the device is actually not ready with operator or software
1759 * intervention required. One dangerous item might be a drive which
1760 * spins itself down, and come the time to write dirty pages, this
1761 * will fail, then block layer discards the data. Since we never
1762 * spin drives up, such devices simply cannot be used with ub anyway.
1764 if (ub_sync_tur(lun
->udev
, lun
) != 0) {
1769 return lun
->changed
;
1772 static struct block_device_operations ub_bd_fops
= {
1773 .owner
= THIS_MODULE
,
1775 .release
= ub_bd_release
,
1776 .ioctl
= ub_bd_ioctl
,
1777 .media_changed
= ub_bd_media_changed
,
1778 .revalidate_disk
= ub_bd_revalidate
,
1782 * Common ->done routine for commands executed synchronously.
1784 static void ub_probe_done(struct ub_dev
*sc
, struct ub_scsi_cmd
*cmd
)
1786 struct completion
*cop
= cmd
->back
;
1791 * Test if the device has a check condition on it, synchronously.
1793 static int ub_sync_tur(struct ub_dev
*sc
, struct ub_lun
*lun
)
1795 struct ub_scsi_cmd
*cmd
;
1796 enum { ALLOC_SIZE
= sizeof(struct ub_scsi_cmd
) };
1797 unsigned long flags
;
1798 struct completion
compl;
1801 init_completion(&compl);
1804 if ((cmd
= kzalloc(ALLOC_SIZE
, GFP_KERNEL
)) == NULL
)
1807 cmd
->cdb
[0] = TEST_UNIT_READY
;
1809 cmd
->dir
= UB_DIR_NONE
;
1810 cmd
->state
= UB_CMDST_INIT
;
1811 cmd
->lun
= lun
; /* This may be NULL, but that's ok */
1812 cmd
->done
= ub_probe_done
;
1815 spin_lock_irqsave(sc
->lock
, flags
);
1816 cmd
->tag
= sc
->tagcnt
++;
1818 rc
= ub_submit_scsi(sc
, cmd
);
1819 spin_unlock_irqrestore(sc
->lock
, flags
);
1824 wait_for_completion(&compl);
1828 if (rc
== -EIO
&& cmd
->key
!= 0) /* Retries for benh's key */
1838 * Read the SCSI capacity synchronously (for probing).
1840 static int ub_sync_read_cap(struct ub_dev
*sc
, struct ub_lun
*lun
,
1841 struct ub_capacity
*ret
)
1843 struct ub_scsi_cmd
*cmd
;
1844 struct scatterlist
*sg
;
1846 enum { ALLOC_SIZE
= sizeof(struct ub_scsi_cmd
) + 8 };
1847 unsigned long flags
;
1848 unsigned int bsize
, shift
;
1850 struct completion
compl;
1853 init_completion(&compl);
1856 if ((cmd
= kzalloc(ALLOC_SIZE
, GFP_KERNEL
)) == NULL
)
1858 p
= (char *)cmd
+ sizeof(struct ub_scsi_cmd
);
1862 cmd
->dir
= UB_DIR_READ
;
1863 cmd
->state
= UB_CMDST_INIT
;
1866 sg_set_page(sg
, virt_to_page(p
), 8, (unsigned long)p
& (PAGE_SIZE
-1));
1869 cmd
->done
= ub_probe_done
;
1872 spin_lock_irqsave(sc
->lock
, flags
);
1873 cmd
->tag
= sc
->tagcnt
++;
1875 rc
= ub_submit_scsi(sc
, cmd
);
1876 spin_unlock_irqrestore(sc
->lock
, flags
);
1881 wait_for_completion(&compl);
1883 if (cmd
->error
!= 0) {
1887 if (cmd
->act_len
!= 8) {
1892 /* sd.c special-cases sector size of 0 to mean 512. Needed? Safe? */
1893 nsec
= be32_to_cpu(*(__be32
*)p
) + 1;
1894 bsize
= be32_to_cpu(*(__be32
*)(p
+ 4));
1896 case 512: shift
= 0; break;
1897 case 1024: shift
= 1; break;
1898 case 2048: shift
= 2; break;
1899 case 4096: shift
= 3; break;
1906 ret
->bshift
= shift
;
1907 ret
->nsec
= nsec
<< shift
;
1920 static void ub_probe_urb_complete(struct urb
*urb
)
1922 struct completion
*cop
= urb
->context
;
1926 static void ub_probe_timeout(unsigned long arg
)
1928 struct completion
*cop
= (struct completion
*) arg
;
1933 * Reset with a Bulk reset.
1935 static int ub_sync_reset(struct ub_dev
*sc
)
1937 int ifnum
= sc
->intf
->cur_altsetting
->desc
.bInterfaceNumber
;
1938 struct usb_ctrlrequest
*cr
;
1939 struct completion
compl;
1940 struct timer_list timer
;
1943 init_completion(&compl);
1946 cr
->bRequestType
= USB_TYPE_CLASS
| USB_RECIP_INTERFACE
;
1947 cr
->bRequest
= US_BULK_RESET_REQUEST
;
1948 cr
->wValue
= cpu_to_le16(0);
1949 cr
->wIndex
= cpu_to_le16(ifnum
);
1950 cr
->wLength
= cpu_to_le16(0);
1952 usb_fill_control_urb(&sc
->work_urb
, sc
->dev
, sc
->send_ctrl_pipe
,
1953 (unsigned char*) cr
, NULL
, 0, ub_probe_urb_complete
, &compl);
1954 sc
->work_urb
.actual_length
= 0;
1955 sc
->work_urb
.error_count
= 0;
1956 sc
->work_urb
.status
= 0;
1958 if ((rc
= usb_submit_urb(&sc
->work_urb
, GFP_KERNEL
)) != 0) {
1960 "%s: Unable to submit a bulk reset (%d)\n", sc
->name
, rc
);
1965 timer
.function
= ub_probe_timeout
;
1966 timer
.data
= (unsigned long) &compl;
1967 timer
.expires
= jiffies
+ UB_CTRL_TIMEOUT
;
1970 wait_for_completion(&compl);
1972 del_timer_sync(&timer
);
1973 usb_kill_urb(&sc
->work_urb
);
1975 return sc
->work_urb
.status
;
1979 * Get number of LUNs by the way of Bulk GetMaxLUN command.
1981 static int ub_sync_getmaxlun(struct ub_dev
*sc
)
1983 int ifnum
= sc
->intf
->cur_altsetting
->desc
.bInterfaceNumber
;
1985 enum { ALLOC_SIZE
= 1 };
1986 struct usb_ctrlrequest
*cr
;
1987 struct completion
compl;
1988 struct timer_list timer
;
1992 init_completion(&compl);
1995 if ((p
= kmalloc(ALLOC_SIZE
, GFP_KERNEL
)) == NULL
)
2000 cr
->bRequestType
= USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
;
2001 cr
->bRequest
= US_BULK_GET_MAX_LUN
;
2002 cr
->wValue
= cpu_to_le16(0);
2003 cr
->wIndex
= cpu_to_le16(ifnum
);
2004 cr
->wLength
= cpu_to_le16(1);
2006 usb_fill_control_urb(&sc
->work_urb
, sc
->dev
, sc
->recv_ctrl_pipe
,
2007 (unsigned char*) cr
, p
, 1, ub_probe_urb_complete
, &compl);
2008 sc
->work_urb
.actual_length
= 0;
2009 sc
->work_urb
.error_count
= 0;
2010 sc
->work_urb
.status
= 0;
2012 if ((rc
= usb_submit_urb(&sc
->work_urb
, GFP_KERNEL
)) != 0)
2016 timer
.function
= ub_probe_timeout
;
2017 timer
.data
= (unsigned long) &compl;
2018 timer
.expires
= jiffies
+ UB_CTRL_TIMEOUT
;
2021 wait_for_completion(&compl);
2023 del_timer_sync(&timer
);
2024 usb_kill_urb(&sc
->work_urb
);
2026 if ((rc
= sc
->work_urb
.status
) < 0)
2029 if (sc
->work_urb
.actual_length
!= 1) {
2032 if ((nluns
= *p
) == 55) {
2035 /* GetMaxLUN returns the maximum LUN number */
2037 if (nluns
> UB_MAX_LUNS
)
2038 nluns
= UB_MAX_LUNS
;
2053 * Clear initial stalls.
2055 static int ub_probe_clear_stall(struct ub_dev
*sc
, int stalled_pipe
)
2058 struct usb_ctrlrequest
*cr
;
2059 struct completion
compl;
2060 struct timer_list timer
;
2063 init_completion(&compl);
2065 endp
= usb_pipeendpoint(stalled_pipe
);
2066 if (usb_pipein (stalled_pipe
))
2070 cr
->bRequestType
= USB_RECIP_ENDPOINT
;
2071 cr
->bRequest
= USB_REQ_CLEAR_FEATURE
;
2072 cr
->wValue
= cpu_to_le16(USB_ENDPOINT_HALT
);
2073 cr
->wIndex
= cpu_to_le16(endp
);
2074 cr
->wLength
= cpu_to_le16(0);
2076 usb_fill_control_urb(&sc
->work_urb
, sc
->dev
, sc
->send_ctrl_pipe
,
2077 (unsigned char*) cr
, NULL
, 0, ub_probe_urb_complete
, &compl);
2078 sc
->work_urb
.actual_length
= 0;
2079 sc
->work_urb
.error_count
= 0;
2080 sc
->work_urb
.status
= 0;
2082 if ((rc
= usb_submit_urb(&sc
->work_urb
, GFP_KERNEL
)) != 0) {
2084 "%s: Unable to submit a probe clear (%d)\n", sc
->name
, rc
);
2089 timer
.function
= ub_probe_timeout
;
2090 timer
.data
= (unsigned long) &compl;
2091 timer
.expires
= jiffies
+ UB_CTRL_TIMEOUT
;
2094 wait_for_completion(&compl);
2096 del_timer_sync(&timer
);
2097 usb_kill_urb(&sc
->work_urb
);
2099 /* reset the endpoint toggle */
2100 usb_settoggle(sc
->dev
, endp
, usb_pipeout(sc
->last_pipe
), 0);
2106 * Get the pipe settings.
2108 static int ub_get_pipes(struct ub_dev
*sc
, struct usb_device
*dev
,
2109 struct usb_interface
*intf
)
2111 struct usb_host_interface
*altsetting
= intf
->cur_altsetting
;
2112 struct usb_endpoint_descriptor
*ep_in
= NULL
;
2113 struct usb_endpoint_descriptor
*ep_out
= NULL
;
2114 struct usb_endpoint_descriptor
*ep
;
2118 * Find the endpoints we need.
2119 * We are expecting a minimum of 2 endpoints - in and out (bulk).
2120 * We will ignore any others.
2122 for (i
= 0; i
< altsetting
->desc
.bNumEndpoints
; i
++) {
2123 ep
= &altsetting
->endpoint
[i
].desc
;
2125 /* Is it a BULK endpoint? */
2126 if ((ep
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
)
2127 == USB_ENDPOINT_XFER_BULK
) {
2128 /* BULK in or out? */
2129 if (ep
->bEndpointAddress
& USB_DIR_IN
) {
2139 if (ep_in
== NULL
|| ep_out
== NULL
) {
2140 printk(KERN_NOTICE
"%s: failed endpoint check\n",
2145 /* Calculate and store the pipe values */
2146 sc
->send_ctrl_pipe
= usb_sndctrlpipe(dev
, 0);
2147 sc
->recv_ctrl_pipe
= usb_rcvctrlpipe(dev
, 0);
2148 sc
->send_bulk_pipe
= usb_sndbulkpipe(dev
,
2149 ep_out
->bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
);
2150 sc
->recv_bulk_pipe
= usb_rcvbulkpipe(dev
,
2151 ep_in
->bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
);
2157 * Probing is done in the process context, which allows us to cheat
2158 * and not to build a state machine for the discovery.
2160 static int ub_probe(struct usb_interface
*intf
,
2161 const struct usb_device_id
*dev_id
)
2168 if (usb_usual_check_type(dev_id
, USB_US_TYPE_UB
))
2172 if ((sc
= kzalloc(sizeof(struct ub_dev
), GFP_KERNEL
)) == NULL
)
2174 sc
->lock
= ub_next_lock();
2175 INIT_LIST_HEAD(&sc
->luns
);
2176 usb_init_urb(&sc
->work_urb
);
2177 tasklet_init(&sc
->tasklet
, ub_scsi_action
, (unsigned long)sc
);
2178 atomic_set(&sc
->poison
, 0);
2179 INIT_WORK(&sc
->reset_work
, ub_reset_task
);
2180 init_waitqueue_head(&sc
->reset_wait
);
2182 init_timer(&sc
->work_timer
);
2183 sc
->work_timer
.data
= (unsigned long) sc
;
2184 sc
->work_timer
.function
= ub_urb_timeout
;
2186 ub_init_completion(&sc
->work_done
);
2187 sc
->work_done
.done
= 1; /* A little yuk, but oh well... */
2189 sc
->dev
= interface_to_usbdev(intf
);
2191 // sc->ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
2192 usb_set_intfdata(intf
, sc
);
2193 usb_get_dev(sc
->dev
);
2195 * Since we give the interface struct to the block level through
2196 * disk->driverfs_dev, we have to pin it. Otherwise, block_uevent
2197 * oopses on close after a disconnect (kernels 2.6.16 and up).
2199 usb_get_intf(sc
->intf
);
2201 snprintf(sc
->name
, 12, DRV_NAME
"(%d.%d)",
2202 sc
->dev
->bus
->busnum
, sc
->dev
->devnum
);
2204 /* XXX Verify that we can handle the device (from descriptors) */
2206 if (ub_get_pipes(sc
, sc
->dev
, intf
) != 0)
2210 * At this point, all USB initialization is done, do upper layer.
2211 * We really hate halfway initialized structures, so from the
2212 * invariants perspective, this ub_dev is fully constructed at
2217 * This is needed to clear toggles. It is a problem only if we do
2218 * `rmmod ub && modprobe ub` without disconnects, but we like that.
2220 #if 0 /* iPod Mini fails if we do this (big white iPod works) */
2221 ub_probe_clear_stall(sc
, sc
->recv_bulk_pipe
);
2222 ub_probe_clear_stall(sc
, sc
->send_bulk_pipe
);
2226 * The way this is used by the startup code is a little specific.
2227 * A SCSI check causes a USB stall. Our common case code sees it
2228 * and clears the check, after which the device is ready for use.
2229 * But if a check was not present, any command other than
2230 * TEST_UNIT_READY ends with a lockup (including REQUEST_SENSE).
2232 * If we neglect to clear the SCSI check, the first real command fails
2233 * (which is the capacity readout). We clear that and retry, but why
2234 * causing spurious retries for no reason.
2236 * Revalidation may start with its own TEST_UNIT_READY, but that one
2237 * has to succeed, so we clear checks with an additional one here.
2238 * In any case it's not our business how revaliadation is implemented.
2240 for (i
= 0; i
< 3; i
++) { /* Retries for the schwag key from KS'04 */
2241 if ((rc
= ub_sync_tur(sc
, NULL
)) <= 0) break;
2242 if (rc
!= 0x6) break;
2247 for (i
= 0; i
< 3; i
++) {
2248 if ((rc
= ub_sync_getmaxlun(sc
)) < 0)
2257 for (i
= 0; i
< nluns
; i
++) {
2258 ub_probe_lun(sc
, i
);
2263 usb_set_intfdata(intf
, NULL
);
2264 usb_put_intf(sc
->intf
);
2265 usb_put_dev(sc
->dev
);
2271 static int ub_probe_lun(struct ub_dev
*sc
, int lnum
)
2274 struct request_queue
*q
;
2275 struct gendisk
*disk
;
2279 if ((lun
= kzalloc(sizeof(struct ub_lun
), GFP_KERNEL
)) == NULL
)
2284 if ((lun
->id
= ub_id_get()) == -1)
2289 snprintf(lun
->name
, 16, DRV_NAME
"%c(%d.%d.%d)",
2290 lun
->id
+ 'a', sc
->dev
->bus
->busnum
, sc
->dev
->devnum
, lun
->num
);
2292 lun
->removable
= 1; /* XXX Query this from the device */
2293 lun
->changed
= 1; /* ub_revalidate clears only */
2294 ub_revalidate(sc
, lun
);
2297 if ((disk
= alloc_disk(UB_PARTS_PER_LUN
)) == NULL
)
2300 sprintf(disk
->disk_name
, DRV_NAME
"%c", lun
->id
+ 'a');
2301 disk
->major
= UB_MAJOR
;
2302 disk
->first_minor
= lun
->id
* UB_PARTS_PER_LUN
;
2303 disk
->fops
= &ub_bd_fops
;
2304 disk
->private_data
= lun
;
2305 disk
->driverfs_dev
= &sc
->intf
->dev
;
2308 if ((q
= blk_init_queue(ub_request_fn
, sc
->lock
)) == NULL
)
2313 blk_queue_bounce_limit(q
, BLK_BOUNCE_HIGH
);
2314 blk_queue_max_hw_segments(q
, UB_MAX_REQ_SG
);
2315 blk_queue_max_phys_segments(q
, UB_MAX_REQ_SG
);
2316 blk_queue_segment_boundary(q
, 0xffffffff); /* Dubious. */
2317 blk_queue_max_sectors(q
, UB_MAX_SECTORS
);
2318 blk_queue_hardsect_size(q
, lun
->capacity
.bsize
);
2322 list_add(&lun
->link
, &sc
->luns
);
2324 set_capacity(disk
, lun
->capacity
.nsec
);
2326 disk
->flags
|= GENHD_FL_REMOVABLE
;
2342 static void ub_disconnect(struct usb_interface
*intf
)
2344 struct ub_dev
*sc
= usb_get_intfdata(intf
);
2346 unsigned long flags
;
2349 * Prevent ub_bd_release from pulling the rug from under us.
2350 * XXX This is starting to look like a kref.
2351 * XXX Why not to take this ref at probe time?
2353 spin_lock_irqsave(&ub_lock
, flags
);
2355 spin_unlock_irqrestore(&ub_lock
, flags
);
2358 * Fence stall clearnings, operations triggered by unlinkings and so on.
2359 * We do not attempt to unlink any URBs, because we do not trust the
2360 * unlink paths in HC drivers. Also, we get -84 upon disconnect anyway.
2362 atomic_set(&sc
->poison
, 1);
2365 * Wait for reset to end, if any.
2367 wait_event(sc
->reset_wait
, !sc
->reset
);
2370 * Blow away queued commands.
2372 * Actually, this never works, because before we get here
2373 * the HCD terminates outstanding URB(s). It causes our
2374 * SCSI command queue to advance, commands fail to submit,
2375 * and the whole queue drains. So, we just use this code to
2378 spin_lock_irqsave(sc
->lock
, flags
);
2380 struct ub_scsi_cmd
*cmd
;
2382 while ((cmd
= ub_cmdq_peek(sc
)) != NULL
) {
2383 cmd
->error
= -ENOTCONN
;
2384 cmd
->state
= UB_CMDST_DONE
;
2386 (*cmd
->done
)(sc
, cmd
);
2390 printk(KERN_WARNING
"%s: "
2391 "%d was queued after shutdown\n", sc
->name
, cnt
);
2394 spin_unlock_irqrestore(sc
->lock
, flags
);
2397 * Unregister the upper layer.
2399 list_for_each_entry(lun
, &sc
->luns
, link
) {
2400 del_gendisk(lun
->disk
);
2402 * I wish I could do:
2403 * set_bit(QUEUE_FLAG_DEAD, &q->queue_flags);
2404 * As it is, we rely on our internal poisoning and let
2405 * the upper levels to spin furiously failing all the I/O.
2410 * Testing for -EINPROGRESS is always a bug, so we are bending
2411 * the rules a little.
2413 spin_lock_irqsave(sc
->lock
, flags
);
2414 if (sc
->work_urb
.status
== -EINPROGRESS
) { /* janitors: ignore */
2415 printk(KERN_WARNING
"%s: "
2416 "URB is active after disconnect\n", sc
->name
);
2418 spin_unlock_irqrestore(sc
->lock
, flags
);
2421 * There is virtually no chance that other CPU runs times so long
2422 * after ub_urb_complete should have called del_timer, but only if HCD
2423 * didn't forget to deliver a callback on unlink.
2425 del_timer_sync(&sc
->work_timer
);
2428 * At this point there must be no commands coming from anyone
2429 * and no URBs left in transit.
2435 static struct usb_driver ub_driver
= {
2438 .disconnect
= ub_disconnect
,
2439 .id_table
= ub_usb_ids
,
2442 static int __init
ub_init(void)
2447 for (i
= 0; i
< UB_QLOCK_NUM
; i
++)
2448 spin_lock_init(&ub_qlockv
[i
]);
2450 if ((rc
= register_blkdev(UB_MAJOR
, DRV_NAME
)) != 0)
2453 if ((rc
= usb_register(&ub_driver
)) != 0)
2456 usb_usual_set_present(USB_US_TYPE_UB
);
2460 unregister_blkdev(UB_MAJOR
, DRV_NAME
);
2465 static void __exit
ub_exit(void)
2467 usb_deregister(&ub_driver
);
2469 unregister_blkdev(UB_MAJOR
, DRV_NAME
);
2470 usb_usual_clear_present(USB_US_TYPE_UB
);
2473 module_init(ub_init
);
2474 module_exit(ub_exit
);
2476 MODULE_LICENSE("GPL");