blob: 8eb44a55b9b8d5e6799d16efece43810763d8e2f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * The low performance USB storage driver (ub).
3 *
4 * Copyright (c) 1999, 2000 Matthew Dharm (mdharm-usb@one-eyed-alien.net)
5 * Copyright (C) 2004 Pete Zaitcev (zaitcev@yahoo.com)
6 *
7 * This work is a part of Linux kernel, is derived from it,
8 * and is not licensed separately. See file COPYING for details.
9 *
10 * TODO (sorted by decreasing priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * -- 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)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * -- special case some senses, e.g. 3a/0 -> no media present, reduce retries
14 * -- verify the 13 conditions and do bulk resets
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * -- kill last_pipe and simply do two-state clearing on both pipes
Pete Zaitcevba6abf12005-07-30 22:38:30 -070016 * -- highmem
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 * -- move top_sense and work_bcs into separate allocations (if they survive)
18 * for cache purists and esoteric architectures.
Pete Zaitcevba6abf12005-07-30 22:38:30 -070019 * -- Allocate structure for LUN 0 before the first ub_sync_tur, avoid NULL. ?
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 * -- prune comments, they are too volumnous
21 * -- Exterminate P3 printks
22 * -- Resove XXX's
23 * -- Redo "benh's retries", perhaps have spin-up code to handle them. V:D=?
Pete Zaitcev1872bce2005-07-27 11:43:51 -070024 * -- CLEAR, CLR2STS, CLRRS seem to be ripe for refactoring.
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 */
26#include <linux/kernel.h>
27#include <linux/module.h>
28#include <linux/usb.h>
Pete Zaitceva00828e2005-10-22 20:15:09 -070029#include <linux/usb_usual.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/blkdev.h>
31#include <linux/devfs_fs_kernel.h>
32#include <linux/timer.h>
33#include <scsi/scsi.h>
34
35#define DRV_NAME "ub"
36#define DEVFS_NAME DRV_NAME
37
38#define UB_MAJOR 180
39
40/*
Pete Zaitcev1872bce2005-07-27 11:43:51 -070041 * The command state machine is the key model for understanding of this driver.
42 *
43 * The general rule is that all transitions are done towards the bottom
44 * of the diagram, thus preventing any loops.
45 *
46 * An exception to that is how the STAT state is handled. A counter allows it
47 * to be re-entered along the path marked with [C].
48 *
49 * +--------+
50 * ! INIT !
51 * +--------+
52 * !
53 * ub_scsi_cmd_start fails ->--------------------------------------\
54 * ! !
55 * V !
56 * +--------+ !
57 * ! CMD ! !
58 * +--------+ !
59 * ! +--------+ !
60 * was -EPIPE -->-------------------------------->! CLEAR ! !
61 * ! +--------+ !
62 * ! ! !
63 * was error -->------------------------------------- ! --------->\
64 * ! ! !
65 * /--<-- cmd->dir == NONE ? ! !
66 * ! ! ! !
67 * ! V ! !
68 * ! +--------+ ! !
69 * ! ! DATA ! ! !
70 * ! +--------+ ! !
71 * ! ! +---------+ ! !
72 * ! was -EPIPE -->--------------->! CLR2STS ! ! !
73 * ! ! +---------+ ! !
74 * ! ! ! ! !
75 * ! ! was error -->---- ! --------->\
76 * ! was error -->--------------------- ! ------------- ! --------->\
77 * ! ! ! ! !
78 * ! V ! ! !
79 * \--->+--------+ ! ! !
80 * ! STAT !<--------------------------/ ! !
81 * /--->+--------+ ! !
82 * ! ! ! !
83 * [C] was -EPIPE -->-----------\ ! !
84 * ! ! ! ! !
85 * +<---- len == 0 ! ! !
86 * ! ! ! ! !
87 * ! was error -->--------------------------------------!---------->\
88 * ! ! ! ! !
89 * +<---- bad CSW ! ! !
90 * +<---- bad tag ! ! !
91 * ! ! V ! !
92 * ! ! +--------+ ! !
93 * ! ! ! CLRRS ! ! !
94 * ! ! +--------+ ! !
95 * ! ! ! ! !
96 * \------- ! --------------------[C]--------\ ! !
97 * ! ! ! !
98 * cmd->error---\ +--------+ ! !
99 * ! +--------------->! SENSE !<----------/ !
100 * STAT_FAIL----/ +--------+ !
101 * ! ! V
102 * ! V +--------+
103 * \--------------------------------\--------------------->! DONE !
104 * +--------+
105 */
106
107/*
Pete Zaitcevf4800072005-05-01 16:05:40 -0700108 * This many LUNs per USB device.
109 * Every one of them takes a host, see UB_MAX_HOSTS.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 */
Pete Zaitcev9f793d22005-06-06 13:54:59 -0700111#define UB_MAX_LUNS 9
Pete Zaitcevf4800072005-05-01 16:05:40 -0700112
113/*
114 */
115
Pete Zaitcev4fb729f2005-12-17 02:34:12 -0800116#define UB_PARTS_PER_LUN 8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118#define UB_MAX_CDB_SIZE 16 /* Corresponds to Bulk */
119
120#define UB_SENSE_SIZE 18
121
122/*
123 */
124
125/* command block wrapper */
126struct bulk_cb_wrap {
127 __le32 Signature; /* contains 'USBC' */
128 u32 Tag; /* unique per command id */
129 __le32 DataTransferLength; /* size of data */
130 u8 Flags; /* direction in bit 0 */
Pete Zaitcevf4800072005-05-01 16:05:40 -0700131 u8 Lun; /* LUN */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 u8 Length; /* of of the CDB */
133 u8 CDB[UB_MAX_CDB_SIZE]; /* max command */
134};
135
136#define US_BULK_CB_WRAP_LEN 31
137#define US_BULK_CB_SIGN 0x43425355 /*spells out USBC */
138#define US_BULK_FLAG_IN 1
139#define US_BULK_FLAG_OUT 0
140
141/* command status wrapper */
142struct bulk_cs_wrap {
143 __le32 Signature; /* should = 'USBS' */
144 u32 Tag; /* same as original command */
145 __le32 Residue; /* amount not transferred */
146 u8 Status; /* see below */
147};
148
149#define US_BULK_CS_WRAP_LEN 13
150#define US_BULK_CS_SIGN 0x53425355 /* spells out 'USBS' */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151#define US_BULK_STAT_OK 0
152#define US_BULK_STAT_FAIL 1
153#define US_BULK_STAT_PHASE 2
154
155/* bulk-only class specific requests */
156#define US_BULK_RESET_REQUEST 0xff
157#define US_BULK_GET_MAX_LUN 0xfe
158
159/*
160 */
161struct ub_dev;
162
Pete Zaitcev64bd8452005-09-22 00:48:29 -0700163#define UB_MAX_REQ_SG 9 /* cdrecord requires 32KB and maybe a header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164#define UB_MAX_SECTORS 64
165
166/*
167 * A second is more than enough for a 32K transfer (UB_MAX_SECTORS)
168 * even if a webcam hogs the bus, but some devices need time to spin up.
169 */
170#define UB_URB_TIMEOUT (HZ*2)
171#define UB_DATA_TIMEOUT (HZ*5) /* ZIP does spin-ups in the data phase */
172#define UB_STAT_TIMEOUT (HZ*5) /* Same spinups and eject for a dataless cmd. */
173#define UB_CTRL_TIMEOUT (HZ/2) /* 500ms ought to be enough to clear a stall */
174
175/*
176 * An instance of a SCSI command in transit.
177 */
178#define UB_DIR_NONE 0
179#define UB_DIR_READ 1
180#define UB_DIR_ILLEGAL2 2
181#define UB_DIR_WRITE 3
182
Pete Zaitcev952ba222006-03-02 16:42:59 -0800183/* P3 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184#define UB_DIR_CHAR(c) (((c)==UB_DIR_WRITE)? 'w': \
185 (((c)==UB_DIR_READ)? 'r': 'n'))
186
187enum ub_scsi_cmd_state {
188 UB_CMDST_INIT, /* Initial state */
189 UB_CMDST_CMD, /* Command submitted */
190 UB_CMDST_DATA, /* Data phase */
191 UB_CMDST_CLR2STS, /* Clearing before requesting status */
192 UB_CMDST_STAT, /* Status phase */
193 UB_CMDST_CLEAR, /* Clearing a stall (halt, actually) */
Pete Zaitcev1872bce2005-07-27 11:43:51 -0700194 UB_CMDST_CLRRS, /* Clearing before retrying status */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 UB_CMDST_SENSE, /* Sending Request Sense */
196 UB_CMDST_DONE /* Final state */
197};
198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199struct ub_scsi_cmd {
200 unsigned char cdb[UB_MAX_CDB_SIZE];
201 unsigned char cdb_len;
202
203 unsigned char dir; /* 0 - none, 1 - read, 3 - write. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 enum ub_scsi_cmd_state state;
205 unsigned int tag;
206 struct ub_scsi_cmd *next;
207
208 int error; /* Return code - valid upon done */
209 unsigned int act_len; /* Return size */
210 unsigned char key, asc, ascq; /* May be valid if error==-EIO */
211
212 int stat_count; /* Retries getting status. */
213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 unsigned int len; /* Requested length */
Pete Zaitceva1cf96e2005-08-14 21:16:03 -0700215 unsigned int current_sg;
216 unsigned int nsg; /* sgv[nsg] */
217 struct scatterlist sgv[UB_MAX_REQ_SG];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Pete Zaitcevf4800072005-05-01 16:05:40 -0700219 struct ub_lun *lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 void (*done)(struct ub_dev *, struct ub_scsi_cmd *);
221 void *back;
222};
223
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800224struct ub_request {
225 struct request *rq;
226 unsigned int current_try;
227 unsigned int nsg; /* sgv[nsg] */
228 struct scatterlist sgv[UB_MAX_REQ_SG];
229};
230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231/*
232 */
233struct ub_capacity {
234 unsigned long nsec; /* Linux size - 512 byte sectors */
235 unsigned int bsize; /* Linux hardsect_size */
236 unsigned int bshift; /* Shift between 512 and hard sects */
237};
238
239/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 * This is a direct take-off from linux/include/completion.h
241 * The difference is that I do not wait on this thing, just poll.
242 * When I want to wait (ub_probe), I just use the stock completion.
243 *
244 * Note that INIT_COMPLETION takes no lock. It is correct. But why
245 * in the bloody hell that thing takes struct instead of pointer to struct
246 * is quite beyond me. I just copied it from the stock completion.
247 */
248struct ub_completion {
249 unsigned int done;
250 spinlock_t lock;
251};
252
253static inline void ub_init_completion(struct ub_completion *x)
254{
255 x->done = 0;
256 spin_lock_init(&x->lock);
257}
258
259#define UB_INIT_COMPLETION(x) ((x).done = 0)
260
261static void ub_complete(struct ub_completion *x)
262{
263 unsigned long flags;
264
265 spin_lock_irqsave(&x->lock, flags);
266 x->done++;
267 spin_unlock_irqrestore(&x->lock, flags);
268}
269
270static int ub_is_completed(struct ub_completion *x)
271{
272 unsigned long flags;
273 int ret;
274
275 spin_lock_irqsave(&x->lock, flags);
276 ret = x->done;
277 spin_unlock_irqrestore(&x->lock, flags);
278 return ret;
279}
280
281/*
282 */
283struct ub_scsi_cmd_queue {
284 int qlen, qmax;
285 struct ub_scsi_cmd *head, *tail;
286};
287
288/*
Pete Zaitcevf4800072005-05-01 16:05:40 -0700289 * The block device instance (one per LUN).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 */
Pete Zaitcevf4800072005-05-01 16:05:40 -0700291struct ub_lun {
292 struct ub_dev *udev;
293 struct list_head link;
294 struct gendisk *disk;
295 int id; /* Host index */
296 int num; /* LUN number */
297 char name[16];
298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 int changed; /* Media was changed */
300 int removable;
301 int readonly;
Pete Zaitcevf4800072005-05-01 16:05:40 -0700302
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800303 struct ub_request urq;
304
Pete Zaitcevf4800072005-05-01 16:05:40 -0700305 /* Use Ingo's mempool if or when we have more than one command. */
306 /*
307 * Currently we never need more than one command for the whole device.
308 * However, giving every LUN a command is a cheap and automatic way
309 * to enforce fairness between them.
310 */
311 int cmda[1];
312 struct ub_scsi_cmd cmdv[1];
313
314 struct ub_capacity capacity;
315};
316
317/*
318 * The USB device instance.
319 */
320struct ub_dev {
Pete Zaitcev65b4fe52005-12-28 14:22:17 -0800321 spinlock_t *lock;
Pete Zaitcevf4800072005-05-01 16:05:40 -0700322 atomic_t poison; /* The USB device is disconnected */
323 int openc; /* protected by ub_lock! */
324 /* kref is too implicit for our taste */
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800325 int reset; /* Reset is running */
Pete Zaitcevf4800072005-05-01 16:05:40 -0700326 unsigned int tagcnt;
327 char name[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 struct usb_device *dev;
329 struct usb_interface *intf;
330
Pete Zaitcevf4800072005-05-01 16:05:40 -0700331 struct list_head luns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
333 unsigned int send_bulk_pipe; /* cached pipe values */
334 unsigned int recv_bulk_pipe;
335 unsigned int send_ctrl_pipe;
336 unsigned int recv_ctrl_pipe;
337
338 struct tasklet_struct tasklet;
339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 struct ub_scsi_cmd_queue cmd_queue;
341 struct ub_scsi_cmd top_rqs_cmd; /* REQUEST SENSE */
342 unsigned char top_sense[UB_SENSE_SIZE];
343
344 struct ub_completion work_done;
345 struct urb work_urb;
346 struct timer_list work_timer;
347 int last_pipe; /* What might need clearing */
Pete Zaitcev1872bce2005-07-27 11:43:51 -0700348 __le32 signature; /* Learned signature */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 struct bulk_cb_wrap work_bcb;
350 struct bulk_cs_wrap work_bcs;
351 struct usb_ctrlrequest work_cr;
352
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800353 struct work_struct reset_work;
354 wait_queue_head_t reset_wait;
355
Pete Zaitcev64bd8452005-09-22 00:48:29 -0700356 int sg_stat[6];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357};
358
359/*
360 */
361static void ub_cleanup(struct ub_dev *sc);
Pete Zaitcev6c1eb8c2005-07-30 22:51:52 -0700362static int ub_request_fn_1(struct ub_lun *lun, struct request *rq);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800363static void ub_cmd_build_block(struct ub_dev *sc, struct ub_lun *lun,
364 struct ub_scsi_cmd *cmd, struct ub_request *urq);
365static void ub_cmd_build_packet(struct ub_dev *sc, struct ub_lun *lun,
366 struct ub_scsi_cmd *cmd, struct ub_request *urq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367static void ub_rw_cmd_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
368static void ub_end_rq(struct request *rq, int uptodate);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800369static int ub_rw_cmd_retry(struct ub_dev *sc, struct ub_lun *lun,
370 struct ub_request *urq, struct ub_scsi_cmd *cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371static int ub_submit_scsi(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
372static void ub_urb_complete(struct urb *urb, struct pt_regs *pt);
373static void ub_scsi_action(unsigned long _dev);
374static void ub_scsi_dispatch(struct ub_dev *sc);
375static void ub_scsi_urb_compl(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
Pete Zaitceva1cf96e2005-08-14 21:16:03 -0700376static void ub_data_start(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377static void ub_state_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd, int rc);
Pete Zaitcev1872bce2005-07-27 11:43:51 -0700378static int __ub_state_stat(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379static void ub_state_stat(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
Pete Zaitcev1872bce2005-07-27 11:43:51 -0700380static void ub_state_stat_counted(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381static void ub_state_sense(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
382static int ub_submit_clear_stall(struct ub_dev *sc, struct ub_scsi_cmd *cmd,
383 int stalled_pipe);
384static void ub_top_sense_done(struct ub_dev *sc, struct ub_scsi_cmd *scmd);
Pete Zaitcev2c2e4a22006-01-05 00:26:30 -0800385static void ub_reset_enter(struct ub_dev *sc, int try);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800386static void ub_reset_task(void *arg);
Pete Zaitcevf4800072005-05-01 16:05:40 -0700387static int ub_sync_tur(struct ub_dev *sc, struct ub_lun *lun);
388static int ub_sync_read_cap(struct ub_dev *sc, struct ub_lun *lun,
389 struct ub_capacity *ret);
Pete Zaitcev2c2e4a22006-01-05 00:26:30 -0800390static int ub_sync_reset(struct ub_dev *sc);
391static int ub_probe_clear_stall(struct ub_dev *sc, int stalled_pipe);
Pete Zaitcevf4800072005-05-01 16:05:40 -0700392static int ub_probe_lun(struct ub_dev *sc, int lnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
394/*
395 */
Pete Zaitceva00828e2005-10-22 20:15:09 -0700396#ifdef CONFIG_USB_LIBUSUAL
397
398#define ub_usb_ids storage_usb_ids
399#else
400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401static struct usb_device_id ub_usb_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_SCSI, US_PR_BULK) },
403 { }
404};
405
406MODULE_DEVICE_TABLE(usb, ub_usb_ids);
Pete Zaitceva00828e2005-10-22 20:15:09 -0700407#endif /* CONFIG_USB_LIBUSUAL */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409/*
410 * Find me a way to identify "next free minor" for add_disk(),
411 * and the array disappears the next day. However, the number of
412 * hosts has something to do with the naming and /proc/partitions.
413 * This has to be thought out in detail before changing.
414 * If UB_MAX_HOST was 1000, we'd use a bitmap. Or a better data structure.
415 */
416#define UB_MAX_HOSTS 26
417static char ub_hostv[UB_MAX_HOSTS];
Pete Zaitcevf4800072005-05-01 16:05:40 -0700418
Pete Zaitcev65b4fe52005-12-28 14:22:17 -0800419#define UB_QLOCK_NUM 5
420static spinlock_t ub_qlockv[UB_QLOCK_NUM];
421static int ub_qlock_next = 0;
422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423static DEFINE_SPINLOCK(ub_lock); /* Locks globals and ->openc */
424
425/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 * The id allocator.
427 *
428 * This also stores the host for indexing by minor, which is somewhat dirty.
429 */
430static int ub_id_get(void)
431{
432 unsigned long flags;
433 int i;
434
435 spin_lock_irqsave(&ub_lock, flags);
436 for (i = 0; i < UB_MAX_HOSTS; i++) {
437 if (ub_hostv[i] == 0) {
438 ub_hostv[i] = 1;
439 spin_unlock_irqrestore(&ub_lock, flags);
440 return i;
441 }
442 }
443 spin_unlock_irqrestore(&ub_lock, flags);
444 return -1;
445}
446
447static void ub_id_put(int id)
448{
449 unsigned long flags;
450
451 if (id < 0 || id >= UB_MAX_HOSTS) {
452 printk(KERN_ERR DRV_NAME ": bad host ID %d\n", id);
453 return;
454 }
455
456 spin_lock_irqsave(&ub_lock, flags);
457 if (ub_hostv[id] == 0) {
458 spin_unlock_irqrestore(&ub_lock, flags);
459 printk(KERN_ERR DRV_NAME ": freeing free host ID %d\n", id);
460 return;
461 }
462 ub_hostv[id] = 0;
463 spin_unlock_irqrestore(&ub_lock, flags);
464}
465
466/*
Pete Zaitcev65b4fe52005-12-28 14:22:17 -0800467 * This is necessitated by the fact that blk_cleanup_queue does not
468 * necesserily destroy the queue. Instead, it may merely decrease q->refcnt.
469 * Since our blk_init_queue() passes a spinlock common with ub_dev,
470 * we have life time issues when ub_cleanup frees ub_dev.
471 */
472static spinlock_t *ub_next_lock(void)
473{
474 unsigned long flags;
475 spinlock_t *ret;
476
477 spin_lock_irqsave(&ub_lock, flags);
478 ret = &ub_qlockv[ub_qlock_next];
479 ub_qlock_next = (ub_qlock_next + 1) % UB_QLOCK_NUM;
480 spin_unlock_irqrestore(&ub_lock, flags);
481 return ret;
482}
483
484/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 * Downcount for deallocation. This rides on two assumptions:
486 * - once something is poisoned, its refcount cannot grow
487 * - opens cannot happen at this time (del_gendisk was done)
488 * If the above is true, we can drop the lock, which we need for
489 * blk_cleanup_queue(): the silly thing may attempt to sleep.
490 * [Actually, it never needs to sleep for us, but it calls might_sleep()]
491 */
492static void ub_put(struct ub_dev *sc)
493{
494 unsigned long flags;
495
496 spin_lock_irqsave(&ub_lock, flags);
497 --sc->openc;
498 if (sc->openc == 0 && atomic_read(&sc->poison)) {
499 spin_unlock_irqrestore(&ub_lock, flags);
500 ub_cleanup(sc);
501 } else {
502 spin_unlock_irqrestore(&ub_lock, flags);
503 }
504}
505
506/*
507 * Final cleanup and deallocation.
508 */
509static void ub_cleanup(struct ub_dev *sc)
510{
Pete Zaitcevf4800072005-05-01 16:05:40 -0700511 struct list_head *p;
512 struct ub_lun *lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 request_queue_t *q;
514
Pete Zaitcevf4800072005-05-01 16:05:40 -0700515 while (!list_empty(&sc->luns)) {
516 p = sc->luns.next;
517 lun = list_entry(p, struct ub_lun, link);
518 list_del(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
Pete Zaitcevf4800072005-05-01 16:05:40 -0700520 /* I don't think queue can be NULL. But... Stolen from sx8.c */
521 if ((q = lun->disk->queue) != NULL)
522 blk_cleanup_queue(q);
523 /*
524 * If we zero disk->private_data BEFORE put_disk, we have
525 * to check for NULL all over the place in open, release,
526 * check_media and revalidate, because the block level
527 * semaphore is well inside the put_disk.
528 * But we cannot zero after the call, because *disk is gone.
529 * The sd.c is blatantly racy in this area.
530 */
531 /* disk->private_data = NULL; */
532 put_disk(lun->disk);
533 lun->disk = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
Pete Zaitcevf4800072005-05-01 16:05:40 -0700535 ub_id_put(lun->id);
536 kfree(lun);
537 }
538
Pete Zaitcev77ef6c42006-05-03 00:16:00 -0700539 usb_set_intfdata(sc->intf, NULL);
540 usb_put_intf(sc->intf);
541 usb_put_dev(sc->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 kfree(sc);
543}
544
545/*
546 * The "command allocator".
547 */
Pete Zaitcevf4800072005-05-01 16:05:40 -0700548static struct ub_scsi_cmd *ub_get_cmd(struct ub_lun *lun)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
550 struct ub_scsi_cmd *ret;
551
Pete Zaitcevf4800072005-05-01 16:05:40 -0700552 if (lun->cmda[0])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 return NULL;
Pete Zaitcevf4800072005-05-01 16:05:40 -0700554 ret = &lun->cmdv[0];
555 lun->cmda[0] = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 return ret;
557}
558
Pete Zaitcevf4800072005-05-01 16:05:40 -0700559static void ub_put_cmd(struct ub_lun *lun, struct ub_scsi_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560{
Pete Zaitcevf4800072005-05-01 16:05:40 -0700561 if (cmd != &lun->cmdv[0]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 printk(KERN_WARNING "%s: releasing a foreign cmd %p\n",
Pete Zaitcevf4800072005-05-01 16:05:40 -0700563 lun->name, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 return;
565 }
Pete Zaitcevf4800072005-05-01 16:05:40 -0700566 if (!lun->cmda[0]) {
567 printk(KERN_WARNING "%s: releasing a free cmd\n", lun->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 return;
569 }
Pete Zaitcevf4800072005-05-01 16:05:40 -0700570 lun->cmda[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571}
572
573/*
574 * The command queue.
575 */
576static void ub_cmdq_add(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
577{
578 struct ub_scsi_cmd_queue *t = &sc->cmd_queue;
579
580 if (t->qlen++ == 0) {
581 t->head = cmd;
582 t->tail = cmd;
583 } else {
584 t->tail->next = cmd;
585 t->tail = cmd;
586 }
587
588 if (t->qlen > t->qmax)
589 t->qmax = t->qlen;
590}
591
592static void ub_cmdq_insert(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
593{
594 struct ub_scsi_cmd_queue *t = &sc->cmd_queue;
595
596 if (t->qlen++ == 0) {
597 t->head = cmd;
598 t->tail = cmd;
599 } else {
600 cmd->next = t->head;
601 t->head = cmd;
602 }
603
604 if (t->qlen > t->qmax)
605 t->qmax = t->qlen;
606}
607
608static struct ub_scsi_cmd *ub_cmdq_pop(struct ub_dev *sc)
609{
610 struct ub_scsi_cmd_queue *t = &sc->cmd_queue;
611 struct ub_scsi_cmd *cmd;
612
613 if (t->qlen == 0)
614 return NULL;
615 if (--t->qlen == 0)
616 t->tail = NULL;
617 cmd = t->head;
618 t->head = cmd->next;
619 cmd->next = NULL;
620 return cmd;
621}
622
623#define ub_cmdq_peek(sc) ((sc)->cmd_queue.head)
624
625/*
626 * The request function is our main entry point
627 */
628
Pete Zaitcev6c1eb8c2005-07-30 22:51:52 -0700629static void ub_request_fn(request_queue_t *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
Pete Zaitcevf4800072005-05-01 16:05:40 -0700631 struct ub_lun *lun = q->queuedata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 struct request *rq;
633
634 while ((rq = elv_next_request(q)) != NULL) {
Pete Zaitcev6c1eb8c2005-07-30 22:51:52 -0700635 if (ub_request_fn_1(lun, rq) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 blk_stop_queue(q);
637 break;
638 }
639 }
640}
641
Pete Zaitcev6c1eb8c2005-07-30 22:51:52 -0700642static int ub_request_fn_1(struct ub_lun *lun, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643{
Pete Zaitcevf4800072005-05-01 16:05:40 -0700644 struct ub_dev *sc = lun->udev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 struct ub_scsi_cmd *cmd;
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800646 struct ub_request *urq;
647 int n_elem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Pete Zaitcevf4800072005-05-01 16:05:40 -0700649 if (atomic_read(&sc->poison) || lun->changed) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 blkdev_dequeue_request(rq);
651 ub_end_rq(rq, 0);
652 return 0;
653 }
654
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800655 if (lun->urq.rq != NULL)
656 return -1;
Pete Zaitcevf4800072005-05-01 16:05:40 -0700657 if ((cmd = ub_get_cmd(lun)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 return -1;
659 memset(cmd, 0, sizeof(struct ub_scsi_cmd));
660
661 blkdev_dequeue_request(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800663 urq = &lun->urq;
664 memset(urq, 0, sizeof(struct ub_request));
665 urq->rq = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667 /*
668 * get scatterlist from block layer
669 */
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800670 n_elem = blk_rq_map_sg(lun->disk->queue, rq, &urq->sgv[0]);
671 if (n_elem < 0) {
Pete Zaitcevba6abf12005-07-30 22:38:30 -0700672 printk(KERN_INFO "%s: failed request map (%d)\n",
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800673 lun->name, n_elem); /* P3 */
674 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 }
Pete Zaitcevba6abf12005-07-30 22:38:30 -0700676 if (n_elem > UB_MAX_REQ_SG) { /* Paranoia */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 printk(KERN_WARNING "%s: request with %d segments\n",
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800678 lun->name, n_elem);
679 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 }
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800681 urq->nsg = n_elem;
Pete Zaitcev64bd8452005-09-22 00:48:29 -0700682 sc->sg_stat[n_elem < 5 ? n_elem : 5]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800684 if (blk_pc_request(rq)) {
685 ub_cmd_build_packet(sc, lun, cmd, urq);
686 } else {
687 ub_cmd_build_block(sc, lun, cmd, urq);
688 }
689 cmd->state = UB_CMDST_INIT;
690 cmd->lun = lun;
691 cmd->done = ub_rw_cmd_done;
692 cmd->back = urq;
693
694 cmd->tag = sc->tagcnt++;
695 if (ub_submit_scsi(sc, cmd) != 0)
696 goto drop;
697
698 return 0;
699
700drop:
701 ub_put_cmd(lun, cmd);
702 ub_end_rq(rq, 0);
703 return 0;
704}
705
706static void ub_cmd_build_block(struct ub_dev *sc, struct ub_lun *lun,
707 struct ub_scsi_cmd *cmd, struct ub_request *urq)
708{
709 struct request *rq = urq->rq;
710 unsigned int block, nblks;
711
712 if (rq_data_dir(rq) == WRITE)
713 cmd->dir = UB_DIR_WRITE;
714 else
715 cmd->dir = UB_DIR_READ;
716
717 cmd->nsg = urq->nsg;
718 memcpy(cmd->sgv, urq->sgv, sizeof(struct scatterlist) * cmd->nsg);
719
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 /*
721 * build the command
722 *
723 * The call to blk_queue_hardsect_size() guarantees that request
724 * is aligned, but it is given in terms of 512 byte units, always.
725 */
Pete Zaitceva1cf96e2005-08-14 21:16:03 -0700726 block = rq->sector >> lun->capacity.bshift;
727 nblks = rq->nr_sectors >> lun->capacity.bshift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800729 cmd->cdb[0] = (cmd->dir == UB_DIR_READ)? READ_10: WRITE_10;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 /* 10-byte uses 4 bytes of LBA: 2147483648KB, 2097152MB, 2048GB */
731 cmd->cdb[2] = block >> 24;
732 cmd->cdb[3] = block >> 16;
733 cmd->cdb[4] = block >> 8;
734 cmd->cdb[5] = block;
735 cmd->cdb[7] = nblks >> 8;
736 cmd->cdb[8] = nblks;
737 cmd->cdb_len = 10;
738
Pete Zaitceva1cf96e2005-08-14 21:16:03 -0700739 cmd->len = rq->nr_sectors * 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740}
741
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800742static void ub_cmd_build_packet(struct ub_dev *sc, struct ub_lun *lun,
743 struct ub_scsi_cmd *cmd, struct ub_request *urq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744{
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800745 struct request *rq = urq->rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
747 if (rq->data_len == 0) {
748 cmd->dir = UB_DIR_NONE;
749 } else {
750 if (rq_data_dir(rq) == WRITE)
751 cmd->dir = UB_DIR_WRITE;
752 else
753 cmd->dir = UB_DIR_READ;
754 }
Pete Zaitceva1cf96e2005-08-14 21:16:03 -0700755
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800756 cmd->nsg = urq->nsg;
757 memcpy(cmd->sgv, urq->sgv, sizeof(struct scatterlist) * cmd->nsg);
Pete Zaitceva1cf96e2005-08-14 21:16:03 -0700758
759 memcpy(&cmd->cdb, rq->cmd, rq->cmd_len);
760 cmd->cdb_len = rq->cmd_len;
761
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 cmd->len = rq->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763}
764
765static void ub_rw_cmd_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
766{
Pete Zaitcevf4800072005-05-01 16:05:40 -0700767 struct ub_lun *lun = cmd->lun;
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800768 struct ub_request *urq = cmd->back;
769 struct request *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 int uptodate;
771
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800772 rq = urq->rq;
773
Pete Zaitceva1cf96e2005-08-14 21:16:03 -0700774 if (cmd->error == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 uptodate = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
Pete Zaitceva1cf96e2005-08-14 21:16:03 -0700777 if (blk_pc_request(rq)) {
778 if (cmd->act_len >= rq->data_len)
779 rq->data_len = 0;
780 else
781 rq->data_len -= cmd->act_len;
Pete Zaitcevba6abf12005-07-30 22:38:30 -0700782 }
Pete Zaitceva1cf96e2005-08-14 21:16:03 -0700783 } else {
Pete Zaitcevba6abf12005-07-30 22:38:30 -0700784 uptodate = 0;
Pete Zaitcevba6abf12005-07-30 22:38:30 -0700785
Pete Zaitceva1cf96e2005-08-14 21:16:03 -0700786 if (blk_pc_request(rq)) {
787 /* UB_SENSE_SIZE is smaller than SCSI_SENSE_BUFFERSIZE */
788 memcpy(rq->sense, sc->top_sense, UB_SENSE_SIZE);
789 rq->sense_len = UB_SENSE_SIZE;
790 if (sc->top_sense[0] != 0)
791 rq->errors = SAM_STAT_CHECK_CONDITION;
792 else
793 rq->errors = DID_ERROR << 16;
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800794 } else {
795 if (cmd->error == -EIO) {
796 if (ub_rw_cmd_retry(sc, lun, urq, cmd) == 0)
797 return;
798 }
Pete Zaitceva1cf96e2005-08-14 21:16:03 -0700799 }
800 }
Pete Zaitcevba6abf12005-07-30 22:38:30 -0700801
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800802 urq->rq = NULL;
803
Pete Zaitcevf4800072005-05-01 16:05:40 -0700804 ub_put_cmd(lun, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 ub_end_rq(rq, uptodate);
Pete Zaitcevba6abf12005-07-30 22:38:30 -0700806 blk_start_queue(lun->disk->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807}
808
809static void ub_end_rq(struct request *rq, int uptodate)
810{
Daniel Marjamakiab930912005-12-17 02:38:46 -0800811 end_that_request_first(rq, uptodate, rq->hard_nr_sectors);
Tejun Heo8ffdc652006-01-06 09:49:03 +0100812 end_that_request_last(rq, uptodate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813}
814
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800815static int ub_rw_cmd_retry(struct ub_dev *sc, struct ub_lun *lun,
816 struct ub_request *urq, struct ub_scsi_cmd *cmd)
817{
818
819 if (atomic_read(&sc->poison))
820 return -ENXIO;
821
Pete Zaitcev2c2e4a22006-01-05 00:26:30 -0800822 ub_reset_enter(sc, urq->current_try);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800823
824 if (urq->current_try >= 3)
825 return -EIO;
826 urq->current_try++;
827 /* P3 */ printk("%s: dir %c len/act %d/%d "
828 "[sense %x %02x %02x] retry %d\n",
829 sc->name, UB_DIR_CHAR(cmd->dir), cmd->len, cmd->act_len,
830 cmd->key, cmd->asc, cmd->ascq, urq->current_try);
831
832 memset(cmd, 0, sizeof(struct ub_scsi_cmd));
833 ub_cmd_build_block(sc, lun, cmd, urq);
834
835 cmd->state = UB_CMDST_INIT;
836 cmd->lun = lun;
837 cmd->done = ub_rw_cmd_done;
838 cmd->back = urq;
839
840 cmd->tag = sc->tagcnt++;
841
842#if 0 /* Wasteful */
843 return ub_submit_scsi(sc, cmd);
844#else
845 ub_cmdq_add(sc, cmd);
846 return 0;
847#endif
848}
849
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850/*
851 * Submit a regular SCSI operation (not an auto-sense).
852 *
853 * The Iron Law of Good Submit Routine is:
854 * Zero return - callback is done, Nonzero return - callback is not done.
855 * No exceptions.
856 *
857 * Host is assumed locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 */
859static int ub_submit_scsi(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
860{
861
862 if (cmd->state != UB_CMDST_INIT ||
863 (cmd->dir != UB_DIR_NONE && cmd->len == 0)) {
864 return -EINVAL;
865 }
866
867 ub_cmdq_add(sc, cmd);
868 /*
869 * We can call ub_scsi_dispatch(sc) right away here, but it's a little
870 * safer to jump to a tasklet, in case upper layers do something silly.
871 */
872 tasklet_schedule(&sc->tasklet);
873 return 0;
874}
875
876/*
877 * Submit the first URB for the queued command.
878 * This function does not deal with queueing in any way.
879 */
880static int ub_scsi_cmd_start(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
881{
882 struct bulk_cb_wrap *bcb;
883 int rc;
884
885 bcb = &sc->work_bcb;
886
887 /*
888 * ``If the allocation length is eighteen or greater, and a device
889 * server returns less than eithteen bytes of data, the application
890 * client should assume that the bytes not transferred would have been
891 * zeroes had the device server returned those bytes.''
892 *
893 * We zero sense for all commands so that when a packet request
894 * fails it does not return a stale sense.
895 */
896 memset(&sc->top_sense, 0, UB_SENSE_SIZE);
897
898 /* set up the command wrapper */
899 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
900 bcb->Tag = cmd->tag; /* Endianness is not important */
901 bcb->DataTransferLength = cpu_to_le32(cmd->len);
902 bcb->Flags = (cmd->dir == UB_DIR_READ) ? 0x80 : 0;
Pete Zaitcevf4800072005-05-01 16:05:40 -0700903 bcb->Lun = (cmd->lun != NULL) ? cmd->lun->num : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 bcb->Length = cmd->cdb_len;
905
906 /* copy the command payload */
907 memcpy(bcb->CDB, cmd->cdb, UB_MAX_CDB_SIZE);
908
909 UB_INIT_COMPLETION(sc->work_done);
910
911 sc->last_pipe = sc->send_bulk_pipe;
912 usb_fill_bulk_urb(&sc->work_urb, sc->dev, sc->send_bulk_pipe,
913 bcb, US_BULK_CB_WRAP_LEN, ub_urb_complete, sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
915 /* Fill what we shouldn't be filling, because usb-storage did so. */
916 sc->work_urb.actual_length = 0;
917 sc->work_urb.error_count = 0;
918 sc->work_urb.status = 0;
919
920 if ((rc = usb_submit_urb(&sc->work_urb, GFP_ATOMIC)) != 0) {
921 /* XXX Clear stalls */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 ub_complete(&sc->work_done);
923 return rc;
924 }
925
926 sc->work_timer.expires = jiffies + UB_URB_TIMEOUT;
927 add_timer(&sc->work_timer);
928
929 cmd->state = UB_CMDST_CMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 return 0;
931}
932
933/*
934 * Timeout handler.
935 */
936static void ub_urb_timeout(unsigned long arg)
937{
938 struct ub_dev *sc = (struct ub_dev *) arg;
939 unsigned long flags;
940
Pete Zaitcev65b4fe52005-12-28 14:22:17 -0800941 spin_lock_irqsave(sc->lock, flags);
Pete Zaitcevb31f8212006-01-05 00:14:02 -0800942 if (!ub_is_completed(&sc->work_done))
943 usb_unlink_urb(&sc->work_urb);
Pete Zaitcev65b4fe52005-12-28 14:22:17 -0800944 spin_unlock_irqrestore(sc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945}
946
947/*
948 * Completion routine for the work URB.
949 *
950 * This can be called directly from usb_submit_urb (while we have
951 * the sc->lock taken) and from an interrupt (while we do NOT have
952 * the sc->lock taken). Therefore, bounce this off to a tasklet.
953 */
954static void ub_urb_complete(struct urb *urb, struct pt_regs *pt)
955{
956 struct ub_dev *sc = urb->context;
957
958 ub_complete(&sc->work_done);
959 tasklet_schedule(&sc->tasklet);
960}
961
962static void ub_scsi_action(unsigned long _dev)
963{
964 struct ub_dev *sc = (struct ub_dev *) _dev;
965 unsigned long flags;
966
Pete Zaitcev65b4fe52005-12-28 14:22:17 -0800967 spin_lock_irqsave(sc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 ub_scsi_dispatch(sc);
Pete Zaitcev65b4fe52005-12-28 14:22:17 -0800969 spin_unlock_irqrestore(sc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970}
971
972static void ub_scsi_dispatch(struct ub_dev *sc)
973{
974 struct ub_scsi_cmd *cmd;
975 int rc;
976
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800977 while (!sc->reset && (cmd = ub_cmdq_peek(sc)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 if (cmd->state == UB_CMDST_DONE) {
979 ub_cmdq_pop(sc);
980 (*cmd->done)(sc, cmd);
981 } else if (cmd->state == UB_CMDST_INIT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 if ((rc = ub_scsi_cmd_start(sc, cmd)) == 0)
983 break;
984 cmd->error = rc;
985 cmd->state = UB_CMDST_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 } else {
987 if (!ub_is_completed(&sc->work_done))
988 break;
Pete Zaitcevb31f8212006-01-05 00:14:02 -0800989 del_timer(&sc->work_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 ub_scsi_urb_compl(sc, cmd);
991 }
992 }
993}
994
995static void ub_scsi_urb_compl(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
996{
997 struct urb *urb = &sc->work_urb;
998 struct bulk_cs_wrap *bcs;
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800999 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 int rc;
1001
1002 if (atomic_read(&sc->poison)) {
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001003 ub_state_done(sc, cmd, -ENODEV);
1004 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 }
1006
1007 if (cmd->state == UB_CMDST_CLEAR) {
1008 if (urb->status == -EPIPE) {
1009 /*
1010 * STALL while clearning STALL.
1011 * The control pipe clears itself - nothing to do.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 */
Pete Zaitcevf4800072005-05-01 16:05:40 -07001013 printk(KERN_NOTICE "%s: stall on control pipe\n",
1014 sc->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 goto Bad_End;
1016 }
1017
1018 /*
1019 * We ignore the result for the halt clear.
1020 */
1021
1022 /* reset the endpoint toggle */
1023 usb_settoggle(sc->dev, usb_pipeendpoint(sc->last_pipe),
1024 usb_pipeout(sc->last_pipe), 0);
1025
1026 ub_state_sense(sc, cmd);
1027
1028 } else if (cmd->state == UB_CMDST_CLR2STS) {
1029 if (urb->status == -EPIPE) {
Pete Zaitcevf4800072005-05-01 16:05:40 -07001030 printk(KERN_NOTICE "%s: stall on control pipe\n",
1031 sc->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 goto Bad_End;
1033 }
1034
1035 /*
1036 * We ignore the result for the halt clear.
1037 */
1038
1039 /* reset the endpoint toggle */
1040 usb_settoggle(sc->dev, usb_pipeendpoint(sc->last_pipe),
1041 usb_pipeout(sc->last_pipe), 0);
1042
1043 ub_state_stat(sc, cmd);
1044
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001045 } else if (cmd->state == UB_CMDST_CLRRS) {
1046 if (urb->status == -EPIPE) {
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001047 printk(KERN_NOTICE "%s: stall on control pipe\n",
1048 sc->name);
1049 goto Bad_End;
1050 }
1051
1052 /*
1053 * We ignore the result for the halt clear.
1054 */
1055
1056 /* reset the endpoint toggle */
1057 usb_settoggle(sc->dev, usb_pipeendpoint(sc->last_pipe),
1058 usb_pipeout(sc->last_pipe), 0);
1059
1060 ub_state_stat_counted(sc, cmd);
1061
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 } else if (cmd->state == UB_CMDST_CMD) {
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001063 switch (urb->status) {
1064 case 0:
1065 break;
1066 case -EOVERFLOW:
1067 goto Bad_End;
1068 case -EPIPE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 rc = ub_submit_clear_stall(sc, cmd, sc->last_pipe);
1070 if (rc != 0) {
1071 printk(KERN_NOTICE "%s: "
Pete Zaitcevf4800072005-05-01 16:05:40 -07001072 "unable to submit clear (%d)\n",
1073 sc->name, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 /*
1075 * This is typically ENOMEM or some other such shit.
1076 * Retrying is pointless. Just do Bad End on it...
1077 */
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001078 ub_state_done(sc, cmd, rc);
1079 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 }
1081 cmd->state = UB_CMDST_CLEAR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 return;
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001083 case -ESHUTDOWN: /* unplug */
1084 case -EILSEQ: /* unplug timeout on uhci */
1085 ub_state_done(sc, cmd, -ENODEV);
1086 return;
1087 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 goto Bad_End;
1089 }
1090 if (urb->actual_length != US_BULK_CB_WRAP_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 goto Bad_End;
1092 }
1093
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001094 if (cmd->dir == UB_DIR_NONE || cmd->nsg < 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 ub_state_stat(sc, cmd);
1096 return;
1097 }
1098
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001099 // udelay(125); // usb-storage has this
1100 ub_data_start(sc, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
1102 } else if (cmd->state == UB_CMDST_DATA) {
1103 if (urb->status == -EPIPE) {
1104 rc = ub_submit_clear_stall(sc, cmd, sc->last_pipe);
1105 if (rc != 0) {
1106 printk(KERN_NOTICE "%s: "
Pete Zaitcevf4800072005-05-01 16:05:40 -07001107 "unable to submit clear (%d)\n",
1108 sc->name, rc);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001109 ub_state_done(sc, cmd, rc);
1110 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 }
1112 cmd->state = UB_CMDST_CLR2STS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 return;
1114 }
1115 if (urb->status == -EOVERFLOW) {
1116 /*
1117 * A babble? Failure, but we must transfer CSW now.
1118 */
1119 cmd->error = -EOVERFLOW; /* A cheap trick... */
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001120 ub_state_stat(sc, cmd);
1121 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 }
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001123
1124 if (cmd->dir == UB_DIR_WRITE) {
1125 /*
1126 * Do not continue writes in case of a failure.
1127 * Doing so would cause sectors to be mixed up,
1128 * which is worse than sectors lost.
1129 *
1130 * We must try to read the CSW, or many devices
1131 * get confused.
1132 */
1133 len = urb->actual_length;
1134 if (urb->status != 0 ||
1135 len != cmd->sgv[cmd->current_sg].length) {
1136 cmd->act_len += len;
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001137
1138 cmd->error = -EIO;
1139 ub_state_stat(sc, cmd);
1140 return;
1141 }
1142
1143 } else {
1144 /*
1145 * If an error occurs on read, we record it, and
1146 * continue to fetch data in order to avoid bubble.
1147 *
1148 * As a small shortcut, we stop if we detect that
1149 * a CSW mixed into data.
1150 */
1151 if (urb->status != 0)
1152 cmd->error = -EIO;
1153
1154 len = urb->actual_length;
1155 if (urb->status != 0 ||
1156 len != cmd->sgv[cmd->current_sg].length) {
1157 if ((len & 0x1FF) == US_BULK_CS_WRAP_LEN)
1158 goto Bad_End;
1159 }
1160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001162 cmd->act_len += urb->actual_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001164 if (++cmd->current_sg < cmd->nsg) {
1165 ub_data_start(sc, cmd);
1166 return;
1167 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 ub_state_stat(sc, cmd);
1169
1170 } else if (cmd->state == UB_CMDST_STAT) {
1171 if (urb->status == -EPIPE) {
1172 rc = ub_submit_clear_stall(sc, cmd, sc->last_pipe);
1173 if (rc != 0) {
1174 printk(KERN_NOTICE "%s: "
Pete Zaitcevf4800072005-05-01 16:05:40 -07001175 "unable to submit clear (%d)\n",
1176 sc->name, rc);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001177 ub_state_done(sc, cmd, rc);
1178 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 }
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001180
1181 /*
1182 * Having a stall when getting CSW is an error, so
1183 * make sure uppper levels are not oblivious to it.
1184 */
1185 cmd->error = -EIO; /* A cheap trick... */
1186
1187 cmd->state = UB_CMDST_CLRRS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 return;
1189 }
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001190
1191 /* Catch everything, including -EOVERFLOW and other nasties. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 if (urb->status != 0)
1193 goto Bad_End;
1194
1195 if (urb->actual_length == 0) {
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001196 ub_state_stat_counted(sc, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 return;
1198 }
1199
1200 /*
1201 * Check the returned Bulk protocol status.
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001202 * The status block has to be validated first.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 */
1204
1205 bcs = &sc->work_bcs;
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001206
1207 if (sc->signature == cpu_to_le32(0)) {
1208 /*
1209 * This is the first reply, so do not perform the check.
1210 * Instead, remember the signature the device uses
1211 * for future checks. But do not allow a nul.
1212 */
1213 sc->signature = bcs->Signature;
1214 if (sc->signature == cpu_to_le32(0)) {
1215 ub_state_stat_counted(sc, cmd);
1216 return;
1217 }
1218 } else {
1219 if (bcs->Signature != sc->signature) {
1220 ub_state_stat_counted(sc, cmd);
1221 return;
1222 }
1223 }
1224
1225 if (bcs->Tag != cmd->tag) {
1226 /*
1227 * This usually happens when we disagree with the
1228 * device's microcode about something. For instance,
1229 * a few of them throw this after timeouts. They buffer
1230 * commands and reply at commands we timed out before.
1231 * Without flushing these replies we loop forever.
1232 */
1233 ub_state_stat_counted(sc, cmd);
1234 return;
1235 }
1236
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001237 len = le32_to_cpu(bcs->Residue);
1238 if (len != cmd->len - cmd->act_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 /*
1240 * It is all right to transfer less, the caller has
1241 * to check. But it's not all right if the device
1242 * counts disagree with our counts.
1243 */
1244 /* P3 */ printk("%s: resid %d len %d act %d\n",
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001245 sc->name, len, cmd->len, cmd->act_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 goto Bad_End;
1247 }
1248
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 switch (bcs->Status) {
1250 case US_BULK_STAT_OK:
1251 break;
1252 case US_BULK_STAT_FAIL:
1253 ub_state_sense(sc, cmd);
1254 return;
1255 case US_BULK_STAT_PHASE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 /* P3 */ printk("%s: status PHASE\n", sc->name);
1257 goto Bad_End;
1258 default:
1259 printk(KERN_INFO "%s: unknown CSW status 0x%x\n",
1260 sc->name, bcs->Status);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001261 ub_state_done(sc, cmd, -EINVAL);
1262 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 }
1264
1265 /* Not zeroing error to preserve a babble indicator */
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001266 if (cmd->error != 0) {
1267 ub_state_sense(sc, cmd);
1268 return;
1269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 cmd->state = UB_CMDST_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 ub_cmdq_pop(sc);
1272 (*cmd->done)(sc, cmd);
1273
1274 } else if (cmd->state == UB_CMDST_SENSE) {
1275 ub_state_done(sc, cmd, -EIO);
1276
1277 } else {
1278 printk(KERN_WARNING "%s: "
Pete Zaitcevf4800072005-05-01 16:05:40 -07001279 "wrong command state %d\n",
1280 sc->name, cmd->state);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001281 ub_state_done(sc, cmd, -EINVAL);
1282 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 }
1284 return;
1285
1286Bad_End: /* Little Excel is dead */
1287 ub_state_done(sc, cmd, -EIO);
1288}
1289
1290/*
1291 * Factorization helper for the command state machine:
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001292 * Initiate a data segment transfer.
1293 */
1294static void ub_data_start(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1295{
1296 struct scatterlist *sg = &cmd->sgv[cmd->current_sg];
1297 int pipe;
1298 int rc;
1299
1300 UB_INIT_COMPLETION(sc->work_done);
1301
1302 if (cmd->dir == UB_DIR_READ)
1303 pipe = sc->recv_bulk_pipe;
1304 else
1305 pipe = sc->send_bulk_pipe;
1306 sc->last_pipe = pipe;
1307 usb_fill_bulk_urb(&sc->work_urb, sc->dev, pipe,
1308 page_address(sg->page) + sg->offset, sg->length,
1309 ub_urb_complete, sc);
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001310 sc->work_urb.actual_length = 0;
1311 sc->work_urb.error_count = 0;
1312 sc->work_urb.status = 0;
1313
1314 if ((rc = usb_submit_urb(&sc->work_urb, GFP_ATOMIC)) != 0) {
1315 /* XXX Clear stalls */
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001316 ub_complete(&sc->work_done);
1317 ub_state_done(sc, cmd, rc);
1318 return;
1319 }
1320
1321 sc->work_timer.expires = jiffies + UB_DATA_TIMEOUT;
1322 add_timer(&sc->work_timer);
1323
1324 cmd->state = UB_CMDST_DATA;
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001325}
1326
1327/*
1328 * Factorization helper for the command state machine:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 * Finish the command.
1330 */
1331static void ub_state_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd, int rc)
1332{
1333
1334 cmd->error = rc;
1335 cmd->state = UB_CMDST_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 ub_cmdq_pop(sc);
1337 (*cmd->done)(sc, cmd);
1338}
1339
1340/*
1341 * Factorization helper for the command state machine:
1342 * Submit a CSW read.
1343 */
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001344static int __ub_state_stat(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345{
1346 int rc;
1347
1348 UB_INIT_COMPLETION(sc->work_done);
1349
1350 sc->last_pipe = sc->recv_bulk_pipe;
1351 usb_fill_bulk_urb(&sc->work_urb, sc->dev, sc->recv_bulk_pipe,
1352 &sc->work_bcs, US_BULK_CS_WRAP_LEN, ub_urb_complete, sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 sc->work_urb.actual_length = 0;
1354 sc->work_urb.error_count = 0;
1355 sc->work_urb.status = 0;
1356
1357 if ((rc = usb_submit_urb(&sc->work_urb, GFP_ATOMIC)) != 0) {
1358 /* XXX Clear stalls */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 ub_complete(&sc->work_done);
1360 ub_state_done(sc, cmd, rc);
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001361 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 }
1363
1364 sc->work_timer.expires = jiffies + UB_STAT_TIMEOUT;
1365 add_timer(&sc->work_timer);
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001366 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367}
1368
1369/*
1370 * Factorization helper for the command state machine:
1371 * Submit a CSW read and go to STAT state.
1372 */
1373static void ub_state_stat(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1374{
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001375
1376 if (__ub_state_stat(sc, cmd) != 0)
1377 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
1379 cmd->stat_count = 0;
1380 cmd->state = UB_CMDST_STAT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381}
1382
1383/*
1384 * Factorization helper for the command state machine:
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001385 * Submit a CSW read and go to STAT state with counter (along [C] path).
1386 */
1387static void ub_state_stat_counted(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1388{
1389
1390 if (++cmd->stat_count >= 4) {
1391 ub_state_sense(sc, cmd);
1392 return;
1393 }
1394
1395 if (__ub_state_stat(sc, cmd) != 0)
1396 return;
1397
1398 cmd->state = UB_CMDST_STAT;
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001399}
1400
1401/*
1402 * Factorization helper for the command state machine:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 * Submit a REQUEST SENSE and go to SENSE state.
1404 */
1405static void ub_state_sense(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1406{
1407 struct ub_scsi_cmd *scmd;
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001408 struct scatterlist *sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 int rc;
1410
1411 if (cmd->cdb[0] == REQUEST_SENSE) {
1412 rc = -EPIPE;
1413 goto error;
1414 }
1415
1416 scmd = &sc->top_rqs_cmd;
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001417 memset(scmd, 0, sizeof(struct ub_scsi_cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 scmd->cdb[0] = REQUEST_SENSE;
1419 scmd->cdb[4] = UB_SENSE_SIZE;
1420 scmd->cdb_len = 6;
1421 scmd->dir = UB_DIR_READ;
1422 scmd->state = UB_CMDST_INIT;
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001423 scmd->nsg = 1;
1424 sg = &scmd->sgv[0];
1425 sg->page = virt_to_page(sc->top_sense);
Pete Zaitcev38ffdd62005-09-24 13:10:16 -07001426 sg->offset = (unsigned long)sc->top_sense & (PAGE_SIZE-1);
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001427 sg->length = UB_SENSE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 scmd->len = UB_SENSE_SIZE;
Pete Zaitcevf4800072005-05-01 16:05:40 -07001429 scmd->lun = cmd->lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 scmd->done = ub_top_sense_done;
1431 scmd->back = cmd;
1432
1433 scmd->tag = sc->tagcnt++;
1434
1435 cmd->state = UB_CMDST_SENSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
1437 ub_cmdq_insert(sc, scmd);
1438 return;
1439
1440error:
1441 ub_state_done(sc, cmd, rc);
1442}
1443
1444/*
1445 * A helper for the command's state machine:
1446 * Submit a stall clear.
1447 */
1448static int ub_submit_clear_stall(struct ub_dev *sc, struct ub_scsi_cmd *cmd,
1449 int stalled_pipe)
1450{
1451 int endp;
1452 struct usb_ctrlrequest *cr;
1453 int rc;
1454
1455 endp = usb_pipeendpoint(stalled_pipe);
1456 if (usb_pipein (stalled_pipe))
1457 endp |= USB_DIR_IN;
1458
1459 cr = &sc->work_cr;
1460 cr->bRequestType = USB_RECIP_ENDPOINT;
1461 cr->bRequest = USB_REQ_CLEAR_FEATURE;
1462 cr->wValue = cpu_to_le16(USB_ENDPOINT_HALT);
1463 cr->wIndex = cpu_to_le16(endp);
1464 cr->wLength = cpu_to_le16(0);
1465
1466 UB_INIT_COMPLETION(sc->work_done);
1467
1468 usb_fill_control_urb(&sc->work_urb, sc->dev, sc->send_ctrl_pipe,
1469 (unsigned char*) cr, NULL, 0, ub_urb_complete, sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 sc->work_urb.actual_length = 0;
1471 sc->work_urb.error_count = 0;
1472 sc->work_urb.status = 0;
1473
1474 if ((rc = usb_submit_urb(&sc->work_urb, GFP_ATOMIC)) != 0) {
1475 ub_complete(&sc->work_done);
1476 return rc;
1477 }
1478
1479 sc->work_timer.expires = jiffies + UB_CTRL_TIMEOUT;
1480 add_timer(&sc->work_timer);
1481 return 0;
1482}
1483
1484/*
1485 */
1486static void ub_top_sense_done(struct ub_dev *sc, struct ub_scsi_cmd *scmd)
1487{
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001488 unsigned char *sense = sc->top_sense;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 struct ub_scsi_cmd *cmd;
1490
1491 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 * Find the command which triggered the unit attention or a check,
1493 * save the sense into it, and advance its state machine.
1494 */
1495 if ((cmd = ub_cmdq_peek(sc)) == NULL) {
1496 printk(KERN_WARNING "%s: sense done while idle\n", sc->name);
1497 return;
1498 }
1499 if (cmd != scmd->back) {
1500 printk(KERN_WARNING "%s: "
Pete Zaitcevf4800072005-05-01 16:05:40 -07001501 "sense done for wrong command 0x%x\n",
1502 sc->name, cmd->tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 return;
1504 }
1505 if (cmd->state != UB_CMDST_SENSE) {
1506 printk(KERN_WARNING "%s: "
Pete Zaitcevf4800072005-05-01 16:05:40 -07001507 "sense done with bad cmd state %d\n",
1508 sc->name, cmd->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 return;
1510 }
1511
Pete Zaitcev952ba222006-03-02 16:42:59 -08001512 /*
1513 * Ignoring scmd->act_len, because the buffer was pre-zeroed.
1514 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 cmd->key = sense[2] & 0x0F;
1516 cmd->asc = sense[12];
1517 cmd->ascq = sense[13];
1518
1519 ub_scsi_urb_compl(sc, cmd);
1520}
1521
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522/*
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001523 * Reset management
Pete Zaitcev2c2e4a22006-01-05 00:26:30 -08001524 * XXX Move usb_reset_device to khubd. Hogging kevent is not a good thing.
1525 * XXX Make usb_sync_reset asynchronous.
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001526 */
1527
Pete Zaitcev2c2e4a22006-01-05 00:26:30 -08001528static void ub_reset_enter(struct ub_dev *sc, int try)
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001529{
1530
1531 if (sc->reset) {
1532 /* This happens often on multi-LUN devices. */
1533 return;
1534 }
Pete Zaitcev2c2e4a22006-01-05 00:26:30 -08001535 sc->reset = try + 1;
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001536
1537#if 0 /* Not needed because the disconnect waits for us. */
1538 unsigned long flags;
1539 spin_lock_irqsave(&ub_lock, flags);
1540 sc->openc++;
1541 spin_unlock_irqrestore(&ub_lock, flags);
1542#endif
1543
1544#if 0 /* We let them stop themselves. */
1545 struct list_head *p;
1546 struct ub_lun *lun;
1547 list_for_each(p, &sc->luns) {
1548 lun = list_entry(p, struct ub_lun, link);
1549 blk_stop_queue(lun->disk->queue);
1550 }
1551#endif
1552
1553 schedule_work(&sc->reset_work);
1554}
1555
1556static void ub_reset_task(void *arg)
1557{
1558 struct ub_dev *sc = arg;
1559 unsigned long flags;
1560 struct list_head *p;
1561 struct ub_lun *lun;
1562 int lkr, rc;
1563
1564 if (!sc->reset) {
1565 printk(KERN_WARNING "%s: Running reset unrequested\n",
1566 sc->name);
1567 return;
1568 }
1569
1570 if (atomic_read(&sc->poison)) {
1571 printk(KERN_NOTICE "%s: Not resetting disconnected device\n",
1572 sc->name); /* P3 This floods. Remove soon. XXX */
Pete Zaitcev2c2e4a22006-01-05 00:26:30 -08001573 } else if ((sc->reset & 1) == 0) {
1574 ub_sync_reset(sc);
1575 msleep(700); /* usb-storage sleeps 6s (!) */
1576 ub_probe_clear_stall(sc, sc->recv_bulk_pipe);
1577 ub_probe_clear_stall(sc, sc->send_bulk_pipe);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001578 } else if (sc->dev->actconfig->desc.bNumInterfaces != 1) {
1579 printk(KERN_NOTICE "%s: Not resetting multi-interface device\n",
1580 sc->name); /* P3 This floods. Remove soon. XXX */
1581 } else {
1582 if ((lkr = usb_lock_device_for_reset(sc->dev, sc->intf)) < 0) {
1583 printk(KERN_NOTICE
1584 "%s: usb_lock_device_for_reset failed (%d)\n",
1585 sc->name, lkr);
1586 } else {
1587 rc = usb_reset_device(sc->dev);
1588 if (rc < 0) {
1589 printk(KERN_NOTICE "%s: "
1590 "usb_lock_device_for_reset failed (%d)\n",
1591 sc->name, rc);
1592 }
1593
1594 if (lkr)
1595 usb_unlock_device(sc->dev);
1596 }
1597 }
1598
1599 /*
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.
1604 */
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08001605 spin_lock_irqsave(sc->lock, flags);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001606 sc->reset = 0;
1607 tasklet_schedule(&sc->tasklet);
1608 list_for_each(p, &sc->luns) {
1609 lun = list_entry(p, struct ub_lun, link);
1610 blk_start_queue(lun->disk->queue);
1611 }
1612 wake_up(&sc->reset_wait);
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08001613 spin_unlock_irqrestore(sc->lock, flags);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001614}
1615
1616/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 * This is called from a process context.
1618 */
Pete Zaitcevf4800072005-05-01 16:05:40 -07001619static void ub_revalidate(struct ub_dev *sc, struct ub_lun *lun)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620{
1621
Pete Zaitcevf4800072005-05-01 16:05:40 -07001622 lun->readonly = 0; /* XXX Query this from the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
Pete Zaitcevf4800072005-05-01 16:05:40 -07001624 lun->capacity.nsec = 0;
1625 lun->capacity.bsize = 512;
1626 lun->capacity.bshift = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627
Pete Zaitcevf4800072005-05-01 16:05:40 -07001628 if (ub_sync_tur(sc, lun) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 return; /* Not ready */
Pete Zaitcevf4800072005-05-01 16:05:40 -07001630 lun->changed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631
Pete Zaitcevf4800072005-05-01 16:05:40 -07001632 if (ub_sync_read_cap(sc, lun, &lun->capacity) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 /*
1634 * The retry here means something is wrong, either with the
1635 * device, with the transport, or with our code.
1636 * We keep this because sd.c has retries for capacity.
1637 */
Pete Zaitcevf4800072005-05-01 16:05:40 -07001638 if (ub_sync_read_cap(sc, lun, &lun->capacity) != 0) {
1639 lun->capacity.nsec = 0;
1640 lun->capacity.bsize = 512;
1641 lun->capacity.bshift = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 }
1643 }
1644}
1645
1646/*
1647 * The open funcion.
1648 * This is mostly needed to keep refcounting, but also to support
1649 * media checks on removable media drives.
1650 */
1651static int ub_bd_open(struct inode *inode, struct file *filp)
1652{
1653 struct gendisk *disk = inode->i_bdev->bd_disk;
Pete Zaitcev41fea552006-04-28 20:45:49 -07001654 struct ub_lun *lun = disk->private_data;
1655 struct ub_dev *sc = lun->udev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 unsigned long flags;
1657 int rc;
1658
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 spin_lock_irqsave(&ub_lock, flags);
1660 if (atomic_read(&sc->poison)) {
1661 spin_unlock_irqrestore(&ub_lock, flags);
1662 return -ENXIO;
1663 }
1664 sc->openc++;
1665 spin_unlock_irqrestore(&ub_lock, flags);
1666
Pete Zaitcevf4800072005-05-01 16:05:40 -07001667 if (lun->removable || lun->readonly)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 check_disk_change(inode->i_bdev);
1669
1670 /*
1671 * The sd.c considers ->media_present and ->changed not equivalent,
1672 * under some pretty murky conditions (a failure of READ CAPACITY).
1673 * We may need it one day.
1674 */
Pete Zaitcevf4800072005-05-01 16:05:40 -07001675 if (lun->removable && lun->changed && !(filp->f_flags & O_NDELAY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 rc = -ENOMEDIUM;
1677 goto err_open;
1678 }
1679
Pete Zaitcevf4800072005-05-01 16:05:40 -07001680 if (lun->readonly && (filp->f_mode & FMODE_WRITE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 rc = -EROFS;
1682 goto err_open;
1683 }
1684
1685 return 0;
1686
1687err_open:
1688 ub_put(sc);
1689 return rc;
1690}
1691
1692/*
1693 */
1694static int ub_bd_release(struct inode *inode, struct file *filp)
1695{
1696 struct gendisk *disk = inode->i_bdev->bd_disk;
Pete Zaitcevf4800072005-05-01 16:05:40 -07001697 struct ub_lun *lun = disk->private_data;
1698 struct ub_dev *sc = lun->udev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699
1700 ub_put(sc);
1701 return 0;
1702}
1703
1704/*
1705 * The ioctl interface.
1706 */
1707static int ub_bd_ioctl(struct inode *inode, struct file *filp,
1708 unsigned int cmd, unsigned long arg)
1709{
1710 struct gendisk *disk = inode->i_bdev->bd_disk;
1711 void __user *usermem = (void __user *) arg;
1712
1713 return scsi_cmd_ioctl(filp, disk, cmd, usermem);
1714}
1715
1716/*
1717 * This is called once a new disk was seen by the block layer or by ub_probe().
1718 * The main onjective here is to discover the features of the media such as
1719 * the capacity, read-only status, etc. USB storage generally does not
1720 * need to be spun up, but if we needed it, this would be the place.
1721 *
1722 * This call can sleep.
1723 *
1724 * The return code is not used.
1725 */
1726static int ub_bd_revalidate(struct gendisk *disk)
1727{
Pete Zaitcevf4800072005-05-01 16:05:40 -07001728 struct ub_lun *lun = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
Pete Zaitcevf4800072005-05-01 16:05:40 -07001730 ub_revalidate(lun->udev, lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731
1732 /* XXX Support sector size switching like in sr.c */
Pete Zaitcevf4800072005-05-01 16:05:40 -07001733 blk_queue_hardsect_size(disk->queue, lun->capacity.bsize);
1734 set_capacity(disk, lun->capacity.nsec);
1735 // set_disk_ro(sdkp->disk, lun->readonly);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
1737 return 0;
1738}
1739
1740/*
1741 * The check is called by the block layer to verify if the media
1742 * is still available. It is supposed to be harmless, lightweight and
1743 * non-intrusive in case the media was not changed.
1744 *
1745 * This call can sleep.
1746 *
1747 * The return code is bool!
1748 */
1749static int ub_bd_media_changed(struct gendisk *disk)
1750{
Pete Zaitcevf4800072005-05-01 16:05:40 -07001751 struct ub_lun *lun = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
Pete Zaitcevf4800072005-05-01 16:05:40 -07001753 if (!lun->removable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 return 0;
1755
1756 /*
1757 * We clean checks always after every command, so this is not
1758 * as dangerous as it looks. If the TEST_UNIT_READY fails here,
1759 * the device is actually not ready with operator or software
1760 * intervention required. One dangerous item might be a drive which
1761 * spins itself down, and come the time to write dirty pages, this
1762 * will fail, then block layer discards the data. Since we never
1763 * spin drives up, such devices simply cannot be used with ub anyway.
1764 */
Pete Zaitcevf4800072005-05-01 16:05:40 -07001765 if (ub_sync_tur(lun->udev, lun) != 0) {
1766 lun->changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 return 1;
1768 }
1769
Pete Zaitcevf4800072005-05-01 16:05:40 -07001770 return lun->changed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771}
1772
1773static struct block_device_operations ub_bd_fops = {
1774 .owner = THIS_MODULE,
1775 .open = ub_bd_open,
1776 .release = ub_bd_release,
1777 .ioctl = ub_bd_ioctl,
1778 .media_changed = ub_bd_media_changed,
1779 .revalidate_disk = ub_bd_revalidate,
1780};
1781
1782/*
1783 * Common ->done routine for commands executed synchronously.
1784 */
1785static void ub_probe_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1786{
1787 struct completion *cop = cmd->back;
1788 complete(cop);
1789}
1790
1791/*
1792 * Test if the device has a check condition on it, synchronously.
1793 */
Pete Zaitcevf4800072005-05-01 16:05:40 -07001794static int ub_sync_tur(struct ub_dev *sc, struct ub_lun *lun)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795{
1796 struct ub_scsi_cmd *cmd;
1797 enum { ALLOC_SIZE = sizeof(struct ub_scsi_cmd) };
1798 unsigned long flags;
1799 struct completion compl;
1800 int rc;
1801
1802 init_completion(&compl);
1803
1804 rc = -ENOMEM;
Pete Zaitcev29da7932006-02-13 20:35:57 -08001805 if ((cmd = kzalloc(ALLOC_SIZE, GFP_KERNEL)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 goto err_alloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
1808 cmd->cdb[0] = TEST_UNIT_READY;
1809 cmd->cdb_len = 6;
1810 cmd->dir = UB_DIR_NONE;
1811 cmd->state = UB_CMDST_INIT;
Pete Zaitcevf4800072005-05-01 16:05:40 -07001812 cmd->lun = lun; /* This may be NULL, but that's ok */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 cmd->done = ub_probe_done;
1814 cmd->back = &compl;
1815
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08001816 spin_lock_irqsave(sc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 cmd->tag = sc->tagcnt++;
1818
1819 rc = ub_submit_scsi(sc, cmd);
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08001820 spin_unlock_irqrestore(sc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821
1822 if (rc != 0) {
1823 printk("ub: testing ready: submit error (%d)\n", rc); /* P3 */
1824 goto err_submit;
1825 }
1826
1827 wait_for_completion(&compl);
1828
1829 rc = cmd->error;
1830
1831 if (rc == -EIO && cmd->key != 0) /* Retries for benh's key */
1832 rc = cmd->key;
1833
1834err_submit:
1835 kfree(cmd);
1836err_alloc:
1837 return rc;
1838}
1839
1840/*
1841 * Read the SCSI capacity synchronously (for probing).
1842 */
Pete Zaitcevf4800072005-05-01 16:05:40 -07001843static int ub_sync_read_cap(struct ub_dev *sc, struct ub_lun *lun,
1844 struct ub_capacity *ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845{
1846 struct ub_scsi_cmd *cmd;
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001847 struct scatterlist *sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 char *p;
1849 enum { ALLOC_SIZE = sizeof(struct ub_scsi_cmd) + 8 };
1850 unsigned long flags;
1851 unsigned int bsize, shift;
1852 unsigned long nsec;
1853 struct completion compl;
1854 int rc;
1855
1856 init_completion(&compl);
1857
1858 rc = -ENOMEM;
Pete Zaitcev29da7932006-02-13 20:35:57 -08001859 if ((cmd = kzalloc(ALLOC_SIZE, GFP_KERNEL)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 goto err_alloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 p = (char *)cmd + sizeof(struct ub_scsi_cmd);
1862
1863 cmd->cdb[0] = 0x25;
1864 cmd->cdb_len = 10;
1865 cmd->dir = UB_DIR_READ;
1866 cmd->state = UB_CMDST_INIT;
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001867 cmd->nsg = 1;
1868 sg = &cmd->sgv[0];
1869 sg->page = virt_to_page(p);
Pete Zaitcev38ffdd62005-09-24 13:10:16 -07001870 sg->offset = (unsigned long)p & (PAGE_SIZE-1);
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001871 sg->length = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 cmd->len = 8;
Pete Zaitcevf4800072005-05-01 16:05:40 -07001873 cmd->lun = lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 cmd->done = ub_probe_done;
1875 cmd->back = &compl;
1876
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08001877 spin_lock_irqsave(sc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 cmd->tag = sc->tagcnt++;
1879
1880 rc = ub_submit_scsi(sc, cmd);
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08001881 spin_unlock_irqrestore(sc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882
1883 if (rc != 0) {
1884 printk("ub: reading capacity: submit error (%d)\n", rc); /* P3 */
1885 goto err_submit;
1886 }
1887
1888 wait_for_completion(&compl);
1889
1890 if (cmd->error != 0) {
1891 printk("ub: reading capacity: error %d\n", cmd->error); /* P3 */
1892 rc = -EIO;
1893 goto err_read;
1894 }
1895 if (cmd->act_len != 8) {
1896 printk("ub: reading capacity: size %d\n", cmd->act_len); /* P3 */
1897 rc = -EIO;
1898 goto err_read;
1899 }
1900
1901 /* sd.c special-cases sector size of 0 to mean 512. Needed? Safe? */
1902 nsec = be32_to_cpu(*(__be32 *)p) + 1;
1903 bsize = be32_to_cpu(*(__be32 *)(p + 4));
1904 switch (bsize) {
1905 case 512: shift = 0; break;
1906 case 1024: shift = 1; break;
1907 case 2048: shift = 2; break;
1908 case 4096: shift = 3; break;
1909 default:
1910 printk("ub: Bad sector size %u\n", bsize); /* P3 */
1911 rc = -EDOM;
1912 goto err_inv_bsize;
1913 }
1914
1915 ret->bsize = bsize;
1916 ret->bshift = shift;
1917 ret->nsec = nsec << shift;
1918 rc = 0;
1919
1920err_inv_bsize:
1921err_read:
1922err_submit:
1923 kfree(cmd);
1924err_alloc:
1925 return rc;
1926}
1927
1928/*
1929 */
1930static void ub_probe_urb_complete(struct urb *urb, struct pt_regs *pt)
1931{
1932 struct completion *cop = urb->context;
1933 complete(cop);
1934}
1935
1936static void ub_probe_timeout(unsigned long arg)
1937{
1938 struct completion *cop = (struct completion *) arg;
1939 complete(cop);
1940}
1941
1942/*
Pete Zaitcev2c2e4a22006-01-05 00:26:30 -08001943 * Reset with a Bulk reset.
1944 */
1945static int ub_sync_reset(struct ub_dev *sc)
1946{
1947 int ifnum = sc->intf->cur_altsetting->desc.bInterfaceNumber;
1948 struct usb_ctrlrequest *cr;
1949 struct completion compl;
1950 struct timer_list timer;
1951 int rc;
1952
1953 init_completion(&compl);
1954
1955 cr = &sc->work_cr;
1956 cr->bRequestType = USB_TYPE_CLASS | USB_RECIP_INTERFACE;
1957 cr->bRequest = US_BULK_RESET_REQUEST;
1958 cr->wValue = cpu_to_le16(0);
1959 cr->wIndex = cpu_to_le16(ifnum);
1960 cr->wLength = cpu_to_le16(0);
1961
1962 usb_fill_control_urb(&sc->work_urb, sc->dev, sc->send_ctrl_pipe,
1963 (unsigned char*) cr, NULL, 0, ub_probe_urb_complete, &compl);
1964 sc->work_urb.actual_length = 0;
1965 sc->work_urb.error_count = 0;
1966 sc->work_urb.status = 0;
1967
1968 if ((rc = usb_submit_urb(&sc->work_urb, GFP_KERNEL)) != 0) {
1969 printk(KERN_WARNING
1970 "%s: Unable to submit a bulk reset (%d)\n", sc->name, rc);
1971 return rc;
1972 }
1973
1974 init_timer(&timer);
1975 timer.function = ub_probe_timeout;
1976 timer.data = (unsigned long) &compl;
1977 timer.expires = jiffies + UB_CTRL_TIMEOUT;
1978 add_timer(&timer);
1979
1980 wait_for_completion(&compl);
1981
1982 del_timer_sync(&timer);
1983 usb_kill_urb(&sc->work_urb);
1984
1985 return sc->work_urb.status;
1986}
1987
1988/*
Pete Zaitcevf4800072005-05-01 16:05:40 -07001989 * Get number of LUNs by the way of Bulk GetMaxLUN command.
1990 */
1991static int ub_sync_getmaxlun(struct ub_dev *sc)
1992{
1993 int ifnum = sc->intf->cur_altsetting->desc.bInterfaceNumber;
1994 unsigned char *p;
1995 enum { ALLOC_SIZE = 1 };
1996 struct usb_ctrlrequest *cr;
1997 struct completion compl;
1998 struct timer_list timer;
1999 int nluns;
2000 int rc;
2001
2002 init_completion(&compl);
2003
2004 rc = -ENOMEM;
2005 if ((p = kmalloc(ALLOC_SIZE, GFP_KERNEL)) == NULL)
2006 goto err_alloc;
2007 *p = 55;
2008
2009 cr = &sc->work_cr;
2010 cr->bRequestType = USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
2011 cr->bRequest = US_BULK_GET_MAX_LUN;
2012 cr->wValue = cpu_to_le16(0);
2013 cr->wIndex = cpu_to_le16(ifnum);
2014 cr->wLength = cpu_to_le16(1);
2015
2016 usb_fill_control_urb(&sc->work_urb, sc->dev, sc->recv_ctrl_pipe,
2017 (unsigned char*) cr, p, 1, ub_probe_urb_complete, &compl);
Pete Zaitcevf4800072005-05-01 16:05:40 -07002018 sc->work_urb.actual_length = 0;
2019 sc->work_urb.error_count = 0;
2020 sc->work_urb.status = 0;
2021
2022 if ((rc = usb_submit_urb(&sc->work_urb, GFP_KERNEL)) != 0) {
2023 if (rc == -EPIPE) {
Pete Zaitcev64bd8452005-09-22 00:48:29 -07002024 printk("%s: Stall submitting GetMaxLUN, using 1 LUN\n",
Pete Zaitcevf4800072005-05-01 16:05:40 -07002025 sc->name); /* P3 */
2026 } else {
Pete Zaitcev64bd8452005-09-22 00:48:29 -07002027 printk(KERN_NOTICE
Pete Zaitcevf4800072005-05-01 16:05:40 -07002028 "%s: Unable to submit GetMaxLUN (%d)\n",
2029 sc->name, rc);
2030 }
2031 goto err_submit;
2032 }
2033
2034 init_timer(&timer);
2035 timer.function = ub_probe_timeout;
2036 timer.data = (unsigned long) &compl;
2037 timer.expires = jiffies + UB_CTRL_TIMEOUT;
2038 add_timer(&timer);
2039
2040 wait_for_completion(&compl);
2041
2042 del_timer_sync(&timer);
2043 usb_kill_urb(&sc->work_urb);
2044
Pete Zaitcev64bd8452005-09-22 00:48:29 -07002045 if ((rc = sc->work_urb.status) < 0) {
2046 if (rc == -EPIPE) {
2047 printk("%s: Stall at GetMaxLUN, using 1 LUN\n",
2048 sc->name); /* P3 */
2049 } else {
2050 printk(KERN_NOTICE
2051 "%s: Error at GetMaxLUN (%d)\n",
2052 sc->name, rc);
2053 }
2054 goto err_io;
2055 }
2056
Pete Zaitcevf4800072005-05-01 16:05:40 -07002057 if (sc->work_urb.actual_length != 1) {
2058 printk("%s: GetMaxLUN returned %d bytes\n", sc->name,
2059 sc->work_urb.actual_length); /* P3 */
2060 nluns = 0;
2061 } else {
2062 if ((nluns = *p) == 55) {
2063 nluns = 0;
2064 } else {
2065 /* GetMaxLUN returns the maximum LUN number */
2066 nluns += 1;
2067 if (nluns > UB_MAX_LUNS)
2068 nluns = UB_MAX_LUNS;
2069 }
2070 printk("%s: GetMaxLUN returned %d, using %d LUNs\n", sc->name,
2071 *p, nluns); /* P3 */
2072 }
2073
2074 kfree(p);
2075 return nluns;
2076
Pete Zaitcev64bd8452005-09-22 00:48:29 -07002077err_io:
Pete Zaitcevf4800072005-05-01 16:05:40 -07002078err_submit:
2079 kfree(p);
2080err_alloc:
2081 return rc;
2082}
2083
2084/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 * Clear initial stalls.
2086 */
2087static int ub_probe_clear_stall(struct ub_dev *sc, int stalled_pipe)
2088{
2089 int endp;
2090 struct usb_ctrlrequest *cr;
2091 struct completion compl;
2092 struct timer_list timer;
2093 int rc;
2094
2095 init_completion(&compl);
2096
2097 endp = usb_pipeendpoint(stalled_pipe);
2098 if (usb_pipein (stalled_pipe))
2099 endp |= USB_DIR_IN;
2100
2101 cr = &sc->work_cr;
2102 cr->bRequestType = USB_RECIP_ENDPOINT;
2103 cr->bRequest = USB_REQ_CLEAR_FEATURE;
2104 cr->wValue = cpu_to_le16(USB_ENDPOINT_HALT);
2105 cr->wIndex = cpu_to_le16(endp);
2106 cr->wLength = cpu_to_le16(0);
2107
2108 usb_fill_control_urb(&sc->work_urb, sc->dev, sc->send_ctrl_pipe,
2109 (unsigned char*) cr, NULL, 0, ub_probe_urb_complete, &compl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 sc->work_urb.actual_length = 0;
2111 sc->work_urb.error_count = 0;
2112 sc->work_urb.status = 0;
2113
2114 if ((rc = usb_submit_urb(&sc->work_urb, GFP_KERNEL)) != 0) {
2115 printk(KERN_WARNING
2116 "%s: Unable to submit a probe clear (%d)\n", sc->name, rc);
2117 return rc;
2118 }
2119
2120 init_timer(&timer);
2121 timer.function = ub_probe_timeout;
2122 timer.data = (unsigned long) &compl;
2123 timer.expires = jiffies + UB_CTRL_TIMEOUT;
2124 add_timer(&timer);
2125
2126 wait_for_completion(&compl);
2127
2128 del_timer_sync(&timer);
2129 usb_kill_urb(&sc->work_urb);
2130
2131 /* reset the endpoint toggle */
2132 usb_settoggle(sc->dev, endp, usb_pipeout(sc->last_pipe), 0);
2133
2134 return 0;
2135}
2136
2137/*
2138 * Get the pipe settings.
2139 */
2140static int ub_get_pipes(struct ub_dev *sc, struct usb_device *dev,
2141 struct usb_interface *intf)
2142{
2143 struct usb_host_interface *altsetting = intf->cur_altsetting;
2144 struct usb_endpoint_descriptor *ep_in = NULL;
2145 struct usb_endpoint_descriptor *ep_out = NULL;
2146 struct usb_endpoint_descriptor *ep;
2147 int i;
2148
2149 /*
2150 * Find the endpoints we need.
2151 * We are expecting a minimum of 2 endpoints - in and out (bulk).
2152 * We will ignore any others.
2153 */
2154 for (i = 0; i < altsetting->desc.bNumEndpoints; i++) {
2155 ep = &altsetting->endpoint[i].desc;
2156
2157 /* Is it a BULK endpoint? */
2158 if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
2159 == USB_ENDPOINT_XFER_BULK) {
2160 /* BULK in or out? */
2161 if (ep->bEndpointAddress & USB_DIR_IN)
2162 ep_in = ep;
2163 else
2164 ep_out = ep;
2165 }
2166 }
2167
2168 if (ep_in == NULL || ep_out == NULL) {
Pete Zaitcevf4800072005-05-01 16:05:40 -07002169 printk(KERN_NOTICE "%s: failed endpoint check\n",
2170 sc->name);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08002171 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 }
2173
2174 /* Calculate and store the pipe values */
2175 sc->send_ctrl_pipe = usb_sndctrlpipe(dev, 0);
2176 sc->recv_ctrl_pipe = usb_rcvctrlpipe(dev, 0);
2177 sc->send_bulk_pipe = usb_sndbulkpipe(dev,
2178 ep_out->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
2179 sc->recv_bulk_pipe = usb_rcvbulkpipe(dev,
2180 ep_in->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
2181
2182 return 0;
2183}
2184
2185/*
2186 * Probing is done in the process context, which allows us to cheat
2187 * and not to build a state machine for the discovery.
2188 */
2189static int ub_probe(struct usb_interface *intf,
2190 const struct usb_device_id *dev_id)
2191{
2192 struct ub_dev *sc;
Pete Zaitcevf4800072005-05-01 16:05:40 -07002193 int nluns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 int rc;
2195 int i;
2196
Pete Zaitceva00828e2005-10-22 20:15:09 -07002197 if (usb_usual_check_type(dev_id, USB_US_TYPE_UB))
2198 return -ENXIO;
2199
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 rc = -ENOMEM;
Pete Zaitcev29da7932006-02-13 20:35:57 -08002201 if ((sc = kzalloc(sizeof(struct ub_dev), GFP_KERNEL)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 goto err_core;
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08002203 sc->lock = ub_next_lock();
Pete Zaitcevf4800072005-05-01 16:05:40 -07002204 INIT_LIST_HEAD(&sc->luns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 usb_init_urb(&sc->work_urb);
2206 tasklet_init(&sc->tasklet, ub_scsi_action, (unsigned long)sc);
2207 atomic_set(&sc->poison, 0);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08002208 INIT_WORK(&sc->reset_work, ub_reset_task, sc);
2209 init_waitqueue_head(&sc->reset_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210
2211 init_timer(&sc->work_timer);
2212 sc->work_timer.data = (unsigned long) sc;
2213 sc->work_timer.function = ub_urb_timeout;
2214
2215 ub_init_completion(&sc->work_done);
2216 sc->work_done.done = 1; /* A little yuk, but oh well... */
2217
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 sc->dev = interface_to_usbdev(intf);
2219 sc->intf = intf;
2220 // sc->ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 usb_set_intfdata(intf, sc);
2222 usb_get_dev(sc->dev);
Pete Zaitcev77ef6c42006-05-03 00:16:00 -07002223 /*
2224 * Since we give the interface struct to the block level through
2225 * disk->driverfs_dev, we have to pin it. Otherwise, block_uevent
2226 * oopses on close after a disconnect (kernels 2.6.16 and up).
2227 */
2228 usb_get_intf(sc->intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229
Pete Zaitcevf4800072005-05-01 16:05:40 -07002230 snprintf(sc->name, 12, DRV_NAME "(%d.%d)",
2231 sc->dev->bus->busnum, sc->dev->devnum);
2232
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 /* XXX Verify that we can handle the device (from descriptors) */
2234
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08002235 if (ub_get_pipes(sc, sc->dev, intf) != 0)
2236 goto err_dev_desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 /*
2239 * At this point, all USB initialization is done, do upper layer.
2240 * We really hate halfway initialized structures, so from the
2241 * invariants perspective, this ub_dev is fully constructed at
2242 * this point.
2243 */
2244
2245 /*
2246 * This is needed to clear toggles. It is a problem only if we do
2247 * `rmmod ub && modprobe ub` without disconnects, but we like that.
2248 */
Pete Zaitcevc6c88832005-09-22 00:49:45 -07002249#if 0 /* iPod Mini fails if we do this (big white iPod works) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 ub_probe_clear_stall(sc, sc->recv_bulk_pipe);
2251 ub_probe_clear_stall(sc, sc->send_bulk_pipe);
Pete Zaitcevc6c88832005-09-22 00:49:45 -07002252#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253
2254 /*
2255 * The way this is used by the startup code is a little specific.
2256 * A SCSI check causes a USB stall. Our common case code sees it
2257 * and clears the check, after which the device is ready for use.
2258 * But if a check was not present, any command other than
2259 * TEST_UNIT_READY ends with a lockup (including REQUEST_SENSE).
2260 *
2261 * If we neglect to clear the SCSI check, the first real command fails
2262 * (which is the capacity readout). We clear that and retry, but why
2263 * causing spurious retries for no reason.
2264 *
2265 * Revalidation may start with its own TEST_UNIT_READY, but that one
2266 * has to succeed, so we clear checks with an additional one here.
2267 * In any case it's not our business how revaliadation is implemented.
2268 */
2269 for (i = 0; i < 3; i++) { /* Retries for benh's key */
Pete Zaitcevf4800072005-05-01 16:05:40 -07002270 if ((rc = ub_sync_tur(sc, NULL)) <= 0) break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 if (rc != 0x6) break;
2272 msleep(10);
2273 }
2274
Pete Zaitcevf4800072005-05-01 16:05:40 -07002275 nluns = 1;
2276 for (i = 0; i < 3; i++) {
Pete Zaitcev11a223a2006-03-02 16:53:00 -08002277 if ((rc = ub_sync_getmaxlun(sc)) < 0)
Pete Zaitcevf4800072005-05-01 16:05:40 -07002278 break;
Pete Zaitcevf4800072005-05-01 16:05:40 -07002279 if (rc != 0) {
2280 nluns = rc;
2281 break;
2282 }
Pete Zaitcev9f793d22005-06-06 13:54:59 -07002283 msleep(100);
Pete Zaitcevf4800072005-05-01 16:05:40 -07002284 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285
Pete Zaitcevf4800072005-05-01 16:05:40 -07002286 for (i = 0; i < nluns; i++) {
2287 ub_probe_lun(sc, i);
2288 }
2289 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08002291err_dev_desc:
Pete Zaitcevf4800072005-05-01 16:05:40 -07002292 usb_set_intfdata(intf, NULL);
Pete Zaitcev77ef6c42006-05-03 00:16:00 -07002293 usb_put_intf(sc->intf);
Pete Zaitcevf4800072005-05-01 16:05:40 -07002294 usb_put_dev(sc->dev);
2295 kfree(sc);
2296err_core:
2297 return rc;
2298}
2299
2300static int ub_probe_lun(struct ub_dev *sc, int lnum)
2301{
2302 struct ub_lun *lun;
2303 request_queue_t *q;
2304 struct gendisk *disk;
2305 int rc;
2306
2307 rc = -ENOMEM;
Pete Zaitcev29da7932006-02-13 20:35:57 -08002308 if ((lun = kzalloc(sizeof(struct ub_lun), GFP_KERNEL)) == NULL)
Pete Zaitcevf4800072005-05-01 16:05:40 -07002309 goto err_alloc;
Pete Zaitcevf4800072005-05-01 16:05:40 -07002310 lun->num = lnum;
2311
2312 rc = -ENOSR;
2313 if ((lun->id = ub_id_get()) == -1)
2314 goto err_id;
2315
2316 lun->udev = sc;
2317 list_add(&lun->link, &sc->luns);
2318
2319 snprintf(lun->name, 16, DRV_NAME "%c(%d.%d.%d)",
2320 lun->id + 'a', sc->dev->bus->busnum, sc->dev->devnum, lun->num);
2321
2322 lun->removable = 1; /* XXX Query this from the device */
2323 lun->changed = 1; /* ub_revalidate clears only */
Pete Zaitcevf4800072005-05-01 16:05:40 -07002324 ub_revalidate(sc, lun);
2325
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 rc = -ENOMEM;
Pete Zaitcev4fb729f2005-12-17 02:34:12 -08002327 if ((disk = alloc_disk(UB_PARTS_PER_LUN)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 goto err_diskalloc;
2329
Pete Zaitcevf4800072005-05-01 16:05:40 -07002330 lun->disk = disk;
2331 sprintf(disk->disk_name, DRV_NAME "%c", lun->id + 'a');
2332 sprintf(disk->devfs_name, DEVFS_NAME "/%c", lun->id + 'a');
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 disk->major = UB_MAJOR;
Pete Zaitcev4fb729f2005-12-17 02:34:12 -08002334 disk->first_minor = lun->id * UB_PARTS_PER_LUN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 disk->fops = &ub_bd_fops;
Pete Zaitcevf4800072005-05-01 16:05:40 -07002336 disk->private_data = lun;
Pete Zaitcev64bd8452005-09-22 00:48:29 -07002337 disk->driverfs_dev = &sc->intf->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338
2339 rc = -ENOMEM;
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08002340 if ((q = blk_init_queue(ub_request_fn, sc->lock)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 goto err_blkqinit;
2342
2343 disk->queue = q;
2344
Pete Zaitcevf4800072005-05-01 16:05:40 -07002345 blk_queue_bounce_limit(q, BLK_BOUNCE_HIGH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 blk_queue_max_hw_segments(q, UB_MAX_REQ_SG);
2347 blk_queue_max_phys_segments(q, UB_MAX_REQ_SG);
Pete Zaitcevf4800072005-05-01 16:05:40 -07002348 blk_queue_segment_boundary(q, 0xffffffff); /* Dubious. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 blk_queue_max_sectors(q, UB_MAX_SECTORS);
Pete Zaitcevf4800072005-05-01 16:05:40 -07002350 blk_queue_hardsect_size(q, lun->capacity.bsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351
Pete Zaitcevf4800072005-05-01 16:05:40 -07002352 q->queuedata = lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353
Pete Zaitcevf4800072005-05-01 16:05:40 -07002354 set_capacity(disk, lun->capacity.nsec);
2355 if (lun->removable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 disk->flags |= GENHD_FL_REMOVABLE;
2357
2358 add_disk(disk);
2359
2360 return 0;
2361
2362err_blkqinit:
2363 put_disk(disk);
2364err_diskalloc:
Pete Zaitcevf4800072005-05-01 16:05:40 -07002365 list_del(&lun->link);
2366 ub_id_put(lun->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367err_id:
Pete Zaitcevf4800072005-05-01 16:05:40 -07002368 kfree(lun);
2369err_alloc:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 return rc;
2371}
2372
2373static void ub_disconnect(struct usb_interface *intf)
2374{
2375 struct ub_dev *sc = usb_get_intfdata(intf);
Pete Zaitcevf4800072005-05-01 16:05:40 -07002376 struct list_head *p;
2377 struct ub_lun *lun;
2378 struct gendisk *disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 unsigned long flags;
2380
2381 /*
2382 * Prevent ub_bd_release from pulling the rug from under us.
2383 * XXX This is starting to look like a kref.
2384 * XXX Why not to take this ref at probe time?
2385 */
2386 spin_lock_irqsave(&ub_lock, flags);
2387 sc->openc++;
2388 spin_unlock_irqrestore(&ub_lock, flags);
2389
2390 /*
2391 * Fence stall clearnings, operations triggered by unlinkings and so on.
2392 * We do not attempt to unlink any URBs, because we do not trust the
2393 * unlink paths in HC drivers. Also, we get -84 upon disconnect anyway.
2394 */
2395 atomic_set(&sc->poison, 1);
2396
2397 /*
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08002398 * Wait for reset to end, if any.
2399 */
2400 wait_event(sc->reset_wait, !sc->reset);
2401
2402 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 * Blow away queued commands.
2404 *
2405 * Actually, this never works, because before we get here
2406 * the HCD terminates outstanding URB(s). It causes our
2407 * SCSI command queue to advance, commands fail to submit,
2408 * and the whole queue drains. So, we just use this code to
2409 * print warnings.
2410 */
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08002411 spin_lock_irqsave(sc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 {
2413 struct ub_scsi_cmd *cmd;
2414 int cnt = 0;
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08002415 while ((cmd = ub_cmdq_peek(sc)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 cmd->error = -ENOTCONN;
2417 cmd->state = UB_CMDST_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 ub_cmdq_pop(sc);
2419 (*cmd->done)(sc, cmd);
2420 cnt++;
2421 }
2422 if (cnt != 0) {
2423 printk(KERN_WARNING "%s: "
2424 "%d was queued after shutdown\n", sc->name, cnt);
2425 }
2426 }
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08002427 spin_unlock_irqrestore(sc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428
2429 /*
2430 * Unregister the upper layer.
2431 */
Pete Zaitcevf4800072005-05-01 16:05:40 -07002432 list_for_each (p, &sc->luns) {
2433 lun = list_entry(p, struct ub_lun, link);
2434 disk = lun->disk;
2435 if (disk->flags & GENHD_FL_UP)
2436 del_gendisk(disk);
2437 /*
2438 * I wish I could do:
2439 * set_bit(QUEUE_FLAG_DEAD, &q->queue_flags);
2440 * As it is, we rely on our internal poisoning and let
2441 * the upper levels to spin furiously failing all the I/O.
2442 */
2443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444
2445 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 * Testing for -EINPROGRESS is always a bug, so we are bending
2447 * the rules a little.
2448 */
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08002449 spin_lock_irqsave(sc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 if (sc->work_urb.status == -EINPROGRESS) { /* janitors: ignore */
2451 printk(KERN_WARNING "%s: "
2452 "URB is active after disconnect\n", sc->name);
2453 }
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08002454 spin_unlock_irqrestore(sc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455
2456 /*
2457 * There is virtually no chance that other CPU runs times so long
2458 * after ub_urb_complete should have called del_timer, but only if HCD
2459 * didn't forget to deliver a callback on unlink.
2460 */
2461 del_timer_sync(&sc->work_timer);
2462
2463 /*
2464 * At this point there must be no commands coming from anyone
2465 * and no URBs left in transit.
2466 */
2467
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 ub_put(sc);
2469}
2470
2471static struct usb_driver ub_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 .name = "ub",
2473 .probe = ub_probe,
2474 .disconnect = ub_disconnect,
2475 .id_table = ub_usb_ids,
2476};
2477
2478static int __init ub_init(void)
2479{
2480 int rc;
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08002481 int i;
2482
2483 for (i = 0; i < UB_QLOCK_NUM; i++)
2484 spin_lock_init(&ub_qlockv[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 if ((rc = register_blkdev(UB_MAJOR, DRV_NAME)) != 0)
2487 goto err_regblkdev;
2488 devfs_mk_dir(DEVFS_NAME);
2489
2490 if ((rc = usb_register(&ub_driver)) != 0)
2491 goto err_register;
2492
Pete Zaitceva00828e2005-10-22 20:15:09 -07002493 usb_usual_set_present(USB_US_TYPE_UB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 return 0;
2495
2496err_register:
2497 devfs_remove(DEVFS_NAME);
2498 unregister_blkdev(UB_MAJOR, DRV_NAME);
2499err_regblkdev:
2500 return rc;
2501}
2502
2503static void __exit ub_exit(void)
2504{
2505 usb_deregister(&ub_driver);
2506
2507 devfs_remove(DEVFS_NAME);
2508 unregister_blkdev(UB_MAJOR, DRV_NAME);
Pete Zaitceva00828e2005-10-22 20:15:09 -07002509 usb_usual_clear_present(USB_US_TYPE_UB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510}
2511
2512module_init(ub_init);
2513module_exit(ub_exit);
2514
2515MODULE_LICENSE("GPL");