blob: 5e740e14ab351de7deddda3f2fd2308b4bb32b02 [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 * -- verify the 13 conditions and do bulk resets
Pete Zaitcevba6abf12005-07-30 22:38:30 -070014 * -- highmem
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * -- move top_sense and work_bcs into separate allocations (if they survive)
16 * for cache purists and esoteric architectures.
Pete Zaitcevba6abf12005-07-30 22:38:30 -070017 * -- Allocate structure for LUN 0 before the first ub_sync_tur, avoid NULL. ?
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 * -- prune comments, they are too volumnous
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * -- Resove XXX's
Pete Zaitcev1872bce2005-07-27 11:43:51 -070020 * -- CLEAR, CLR2STS, CLRRS seem to be ripe for refactoring.
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 */
22#include <linux/kernel.h>
23#include <linux/module.h>
24#include <linux/usb.h>
Pete Zaitceva00828e2005-10-22 20:15:09 -070025#include <linux/usb_usual.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/blkdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/timer.h>
Jens Axboe45711f12007-10-22 21:19:53 +020028#include <linux/scatterlist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <scsi/scsi.h>
30
31#define DRV_NAME "ub"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33#define UB_MAJOR 180
34
35/*
Pete Zaitcev1872bce2005-07-27 11:43:51 -070036 * The command state machine is the key model for understanding of this driver.
37 *
38 * The general rule is that all transitions are done towards the bottom
39 * of the diagram, thus preventing any loops.
40 *
41 * An exception to that is how the STAT state is handled. A counter allows it
42 * to be re-entered along the path marked with [C].
43 *
44 * +--------+
45 * ! INIT !
46 * +--------+
47 * !
48 * ub_scsi_cmd_start fails ->--------------------------------------\
49 * ! !
50 * V !
51 * +--------+ !
52 * ! CMD ! !
53 * +--------+ !
54 * ! +--------+ !
55 * was -EPIPE -->-------------------------------->! CLEAR ! !
56 * ! +--------+ !
57 * ! ! !
58 * was error -->------------------------------------- ! --------->\
59 * ! ! !
60 * /--<-- cmd->dir == NONE ? ! !
61 * ! ! ! !
62 * ! V ! !
63 * ! +--------+ ! !
64 * ! ! DATA ! ! !
65 * ! +--------+ ! !
66 * ! ! +---------+ ! !
67 * ! was -EPIPE -->--------------->! CLR2STS ! ! !
68 * ! ! +---------+ ! !
69 * ! ! ! ! !
70 * ! ! was error -->---- ! --------->\
71 * ! was error -->--------------------- ! ------------- ! --------->\
72 * ! ! ! ! !
73 * ! V ! ! !
74 * \--->+--------+ ! ! !
75 * ! STAT !<--------------------------/ ! !
76 * /--->+--------+ ! !
77 * ! ! ! !
78 * [C] was -EPIPE -->-----------\ ! !
79 * ! ! ! ! !
80 * +<---- len == 0 ! ! !
81 * ! ! ! ! !
82 * ! was error -->--------------------------------------!---------->\
83 * ! ! ! ! !
84 * +<---- bad CSW ! ! !
85 * +<---- bad tag ! ! !
86 * ! ! V ! !
87 * ! ! +--------+ ! !
88 * ! ! ! CLRRS ! ! !
89 * ! ! +--------+ ! !
90 * ! ! ! ! !
91 * \------- ! --------------------[C]--------\ ! !
92 * ! ! ! !
93 * cmd->error---\ +--------+ ! !
94 * ! +--------------->! SENSE !<----------/ !
95 * STAT_FAIL----/ +--------+ !
96 * ! ! V
97 * ! V +--------+
98 * \--------------------------------\--------------------->! DONE !
99 * +--------+
100 */
101
102/*
Pete Zaitcevf4800072005-05-01 16:05:40 -0700103 * This many LUNs per USB device.
104 * Every one of them takes a host, see UB_MAX_HOSTS.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 */
Pete Zaitcev9f793d22005-06-06 13:54:59 -0700106#define UB_MAX_LUNS 9
Pete Zaitcevf4800072005-05-01 16:05:40 -0700107
108/*
109 */
110
Pete Zaitcev4fb729f2005-12-17 02:34:12 -0800111#define UB_PARTS_PER_LUN 8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113#define UB_MAX_CDB_SIZE 16 /* Corresponds to Bulk */
114
115#define UB_SENSE_SIZE 18
116
117/*
118 */
119
120/* command block wrapper */
121struct bulk_cb_wrap {
122 __le32 Signature; /* contains 'USBC' */
123 u32 Tag; /* unique per command id */
124 __le32 DataTransferLength; /* size of data */
125 u8 Flags; /* direction in bit 0 */
Pete Zaitcevf4800072005-05-01 16:05:40 -0700126 u8 Lun; /* LUN */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 u8 Length; /* of of the CDB */
128 u8 CDB[UB_MAX_CDB_SIZE]; /* max command */
129};
130
131#define US_BULK_CB_WRAP_LEN 31
132#define US_BULK_CB_SIGN 0x43425355 /*spells out USBC */
133#define US_BULK_FLAG_IN 1
134#define US_BULK_FLAG_OUT 0
135
136/* command status wrapper */
137struct bulk_cs_wrap {
138 __le32 Signature; /* should = 'USBS' */
139 u32 Tag; /* same as original command */
140 __le32 Residue; /* amount not transferred */
141 u8 Status; /* see below */
142};
143
144#define US_BULK_CS_WRAP_LEN 13
145#define US_BULK_CS_SIGN 0x53425355 /* spells out 'USBS' */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146#define US_BULK_STAT_OK 0
147#define US_BULK_STAT_FAIL 1
148#define US_BULK_STAT_PHASE 2
149
150/* bulk-only class specific requests */
151#define US_BULK_RESET_REQUEST 0xff
152#define US_BULK_GET_MAX_LUN 0xfe
153
154/*
155 */
156struct ub_dev;
157
Pete Zaitcev64bd8452005-09-22 00:48:29 -0700158#define UB_MAX_REQ_SG 9 /* cdrecord requires 32KB and maybe a header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159#define UB_MAX_SECTORS 64
160
161/*
162 * A second is more than enough for a 32K transfer (UB_MAX_SECTORS)
163 * even if a webcam hogs the bus, but some devices need time to spin up.
164 */
165#define UB_URB_TIMEOUT (HZ*2)
166#define UB_DATA_TIMEOUT (HZ*5) /* ZIP does spin-ups in the data phase */
167#define UB_STAT_TIMEOUT (HZ*5) /* Same spinups and eject for a dataless cmd. */
168#define UB_CTRL_TIMEOUT (HZ/2) /* 500ms ought to be enough to clear a stall */
169
170/*
171 * An instance of a SCSI command in transit.
172 */
173#define UB_DIR_NONE 0
174#define UB_DIR_READ 1
175#define UB_DIR_ILLEGAL2 2
176#define UB_DIR_WRITE 3
177
178#define UB_DIR_CHAR(c) (((c)==UB_DIR_WRITE)? 'w': \
179 (((c)==UB_DIR_READ)? 'r': 'n'))
180
181enum ub_scsi_cmd_state {
182 UB_CMDST_INIT, /* Initial state */
183 UB_CMDST_CMD, /* Command submitted */
184 UB_CMDST_DATA, /* Data phase */
185 UB_CMDST_CLR2STS, /* Clearing before requesting status */
186 UB_CMDST_STAT, /* Status phase */
187 UB_CMDST_CLEAR, /* Clearing a stall (halt, actually) */
Pete Zaitcev1872bce2005-07-27 11:43:51 -0700188 UB_CMDST_CLRRS, /* Clearing before retrying status */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 UB_CMDST_SENSE, /* Sending Request Sense */
190 UB_CMDST_DONE /* Final state */
191};
192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193struct ub_scsi_cmd {
194 unsigned char cdb[UB_MAX_CDB_SIZE];
195 unsigned char cdb_len;
196
197 unsigned char dir; /* 0 - none, 1 - read, 3 - write. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 enum ub_scsi_cmd_state state;
199 unsigned int tag;
200 struct ub_scsi_cmd *next;
201
202 int error; /* Return code - valid upon done */
203 unsigned int act_len; /* Return size */
204 unsigned char key, asc, ascq; /* May be valid if error==-EIO */
205
206 int stat_count; /* Retries getting status. */
207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 unsigned int len; /* Requested length */
Pete Zaitceva1cf96e2005-08-14 21:16:03 -0700209 unsigned int current_sg;
210 unsigned int nsg; /* sgv[nsg] */
211 struct scatterlist sgv[UB_MAX_REQ_SG];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Pete Zaitcevf4800072005-05-01 16:05:40 -0700213 struct ub_lun *lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 void (*done)(struct ub_dev *, struct ub_scsi_cmd *);
215 void *back;
216};
217
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800218struct ub_request {
219 struct request *rq;
220 unsigned int current_try;
221 unsigned int nsg; /* sgv[nsg] */
222 struct scatterlist sgv[UB_MAX_REQ_SG];
223};
224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225/*
226 */
227struct ub_capacity {
228 unsigned long nsec; /* Linux size - 512 byte sectors */
229 unsigned int bsize; /* Linux hardsect_size */
230 unsigned int bshift; /* Shift between 512 and hard sects */
231};
232
233/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 * This is a direct take-off from linux/include/completion.h
235 * The difference is that I do not wait on this thing, just poll.
236 * When I want to wait (ub_probe), I just use the stock completion.
237 *
238 * Note that INIT_COMPLETION takes no lock. It is correct. But why
239 * in the bloody hell that thing takes struct instead of pointer to struct
240 * is quite beyond me. I just copied it from the stock completion.
241 */
242struct ub_completion {
243 unsigned int done;
244 spinlock_t lock;
245};
246
247static inline void ub_init_completion(struct ub_completion *x)
248{
249 x->done = 0;
250 spin_lock_init(&x->lock);
251}
252
253#define UB_INIT_COMPLETION(x) ((x).done = 0)
254
255static void ub_complete(struct ub_completion *x)
256{
257 unsigned long flags;
258
259 spin_lock_irqsave(&x->lock, flags);
260 x->done++;
261 spin_unlock_irqrestore(&x->lock, flags);
262}
263
264static int ub_is_completed(struct ub_completion *x)
265{
266 unsigned long flags;
267 int ret;
268
269 spin_lock_irqsave(&x->lock, flags);
270 ret = x->done;
271 spin_unlock_irqrestore(&x->lock, flags);
272 return ret;
273}
274
275/*
276 */
277struct ub_scsi_cmd_queue {
278 int qlen, qmax;
279 struct ub_scsi_cmd *head, *tail;
280};
281
282/*
Pete Zaitcevf4800072005-05-01 16:05:40 -0700283 * The block device instance (one per LUN).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 */
Pete Zaitcevf4800072005-05-01 16:05:40 -0700285struct ub_lun {
286 struct ub_dev *udev;
287 struct list_head link;
288 struct gendisk *disk;
289 int id; /* Host index */
290 int num; /* LUN number */
291 char name[16];
292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 int changed; /* Media was changed */
294 int removable;
295 int readonly;
Pete Zaitcevf4800072005-05-01 16:05:40 -0700296
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800297 struct ub_request urq;
298
Pete Zaitcevf4800072005-05-01 16:05:40 -0700299 /* Use Ingo's mempool if or when we have more than one command. */
300 /*
301 * Currently we never need more than one command for the whole device.
302 * However, giving every LUN a command is a cheap and automatic way
303 * to enforce fairness between them.
304 */
305 int cmda[1];
306 struct ub_scsi_cmd cmdv[1];
307
308 struct ub_capacity capacity;
309};
310
311/*
312 * The USB device instance.
313 */
314struct ub_dev {
Pete Zaitcev65b4fe52005-12-28 14:22:17 -0800315 spinlock_t *lock;
Pete Zaitcevf4800072005-05-01 16:05:40 -0700316 atomic_t poison; /* The USB device is disconnected */
317 int openc; /* protected by ub_lock! */
318 /* kref is too implicit for our taste */
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800319 int reset; /* Reset is running */
Pete Zaitcevf4800072005-05-01 16:05:40 -0700320 unsigned int tagcnt;
321 char name[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 struct usb_device *dev;
323 struct usb_interface *intf;
324
Pete Zaitcevf4800072005-05-01 16:05:40 -0700325 struct list_head luns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
327 unsigned int send_bulk_pipe; /* cached pipe values */
328 unsigned int recv_bulk_pipe;
329 unsigned int send_ctrl_pipe;
330 unsigned int recv_ctrl_pipe;
331
332 struct tasklet_struct tasklet;
333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 struct ub_scsi_cmd_queue cmd_queue;
335 struct ub_scsi_cmd top_rqs_cmd; /* REQUEST SENSE */
336 unsigned char top_sense[UB_SENSE_SIZE];
337
338 struct ub_completion work_done;
339 struct urb work_urb;
340 struct timer_list work_timer;
341 int last_pipe; /* What might need clearing */
Pete Zaitcev1872bce2005-07-27 11:43:51 -0700342 __le32 signature; /* Learned signature */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 struct bulk_cb_wrap work_bcb;
344 struct bulk_cs_wrap work_bcs;
345 struct usb_ctrlrequest work_cr;
346
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800347 struct work_struct reset_work;
348 wait_queue_head_t reset_wait;
349
Pete Zaitcev64bd8452005-09-22 00:48:29 -0700350 int sg_stat[6];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351};
352
353/*
354 */
355static void ub_cleanup(struct ub_dev *sc);
Pete Zaitcev6c1eb8c2005-07-30 22:51:52 -0700356static int ub_request_fn_1(struct ub_lun *lun, struct request *rq);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800357static void ub_cmd_build_block(struct ub_dev *sc, struct ub_lun *lun,
358 struct ub_scsi_cmd *cmd, struct ub_request *urq);
359static void ub_cmd_build_packet(struct ub_dev *sc, struct ub_lun *lun,
360 struct ub_scsi_cmd *cmd, struct ub_request *urq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361static void ub_rw_cmd_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
Pete Zaitcevd1ad4ea2006-09-07 16:54:22 -0700362static void ub_end_rq(struct request *rq, unsigned int status);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800363static int ub_rw_cmd_retry(struct ub_dev *sc, struct ub_lun *lun,
364 struct ub_request *urq, struct ub_scsi_cmd *cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365static int ub_submit_scsi(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
David Howells7d12e782006-10-05 14:55:46 +0100366static void ub_urb_complete(struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367static void ub_scsi_action(unsigned long _dev);
368static void ub_scsi_dispatch(struct ub_dev *sc);
369static void ub_scsi_urb_compl(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
Pete Zaitceva1cf96e2005-08-14 21:16:03 -0700370static void ub_data_start(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371static void ub_state_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd, int rc);
Pete Zaitcev1872bce2005-07-27 11:43:51 -0700372static int __ub_state_stat(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373static void ub_state_stat(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
Pete Zaitcev1872bce2005-07-27 11:43:51 -0700374static void ub_state_stat_counted(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375static void ub_state_sense(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
376static int ub_submit_clear_stall(struct ub_dev *sc, struct ub_scsi_cmd *cmd,
377 int stalled_pipe);
378static void ub_top_sense_done(struct ub_dev *sc, struct ub_scsi_cmd *scmd);
Pete Zaitcev2c2e4a22006-01-05 00:26:30 -0800379static void ub_reset_enter(struct ub_dev *sc, int try);
David Howellsc4028952006-11-22 14:57:56 +0000380static void ub_reset_task(struct work_struct *work);
Pete Zaitcevf4800072005-05-01 16:05:40 -0700381static int ub_sync_tur(struct ub_dev *sc, struct ub_lun *lun);
382static int ub_sync_read_cap(struct ub_dev *sc, struct ub_lun *lun,
383 struct ub_capacity *ret);
Pete Zaitcev2c2e4a22006-01-05 00:26:30 -0800384static int ub_sync_reset(struct ub_dev *sc);
385static int ub_probe_clear_stall(struct ub_dev *sc, int stalled_pipe);
Pete Zaitcevf4800072005-05-01 16:05:40 -0700386static int ub_probe_lun(struct ub_dev *sc, int lnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
388/*
389 */
Pete Zaitceva00828e2005-10-22 20:15:09 -0700390#ifdef CONFIG_USB_LIBUSUAL
391
392#define ub_usb_ids storage_usb_ids
393#else
394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395static struct usb_device_id ub_usb_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_SCSI, US_PR_BULK) },
397 { }
398};
399
400MODULE_DEVICE_TABLE(usb, ub_usb_ids);
Pete Zaitceva00828e2005-10-22 20:15:09 -0700401#endif /* CONFIG_USB_LIBUSUAL */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403/*
404 * Find me a way to identify "next free minor" for add_disk(),
405 * and the array disappears the next day. However, the number of
406 * hosts has something to do with the naming and /proc/partitions.
407 * This has to be thought out in detail before changing.
408 * If UB_MAX_HOST was 1000, we'd use a bitmap. Or a better data structure.
409 */
410#define UB_MAX_HOSTS 26
411static char ub_hostv[UB_MAX_HOSTS];
Pete Zaitcevf4800072005-05-01 16:05:40 -0700412
Pete Zaitcev65b4fe52005-12-28 14:22:17 -0800413#define UB_QLOCK_NUM 5
414static spinlock_t ub_qlockv[UB_QLOCK_NUM];
415static int ub_qlock_next = 0;
416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417static DEFINE_SPINLOCK(ub_lock); /* Locks globals and ->openc */
418
419/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 * The id allocator.
421 *
422 * This also stores the host for indexing by minor, which is somewhat dirty.
423 */
424static int ub_id_get(void)
425{
426 unsigned long flags;
427 int i;
428
429 spin_lock_irqsave(&ub_lock, flags);
430 for (i = 0; i < UB_MAX_HOSTS; i++) {
431 if (ub_hostv[i] == 0) {
432 ub_hostv[i] = 1;
433 spin_unlock_irqrestore(&ub_lock, flags);
434 return i;
435 }
436 }
437 spin_unlock_irqrestore(&ub_lock, flags);
438 return -1;
439}
440
441static void ub_id_put(int id)
442{
443 unsigned long flags;
444
445 if (id < 0 || id >= UB_MAX_HOSTS) {
446 printk(KERN_ERR DRV_NAME ": bad host ID %d\n", id);
447 return;
448 }
449
450 spin_lock_irqsave(&ub_lock, flags);
451 if (ub_hostv[id] == 0) {
452 spin_unlock_irqrestore(&ub_lock, flags);
453 printk(KERN_ERR DRV_NAME ": freeing free host ID %d\n", id);
454 return;
455 }
456 ub_hostv[id] = 0;
457 spin_unlock_irqrestore(&ub_lock, flags);
458}
459
460/*
Pete Zaitcev65b4fe52005-12-28 14:22:17 -0800461 * This is necessitated by the fact that blk_cleanup_queue does not
462 * necesserily destroy the queue. Instead, it may merely decrease q->refcnt.
463 * Since our blk_init_queue() passes a spinlock common with ub_dev,
464 * we have life time issues when ub_cleanup frees ub_dev.
465 */
466static spinlock_t *ub_next_lock(void)
467{
468 unsigned long flags;
469 spinlock_t *ret;
470
471 spin_lock_irqsave(&ub_lock, flags);
472 ret = &ub_qlockv[ub_qlock_next];
473 ub_qlock_next = (ub_qlock_next + 1) % UB_QLOCK_NUM;
474 spin_unlock_irqrestore(&ub_lock, flags);
475 return ret;
476}
477
478/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 * Downcount for deallocation. This rides on two assumptions:
480 * - once something is poisoned, its refcount cannot grow
481 * - opens cannot happen at this time (del_gendisk was done)
482 * If the above is true, we can drop the lock, which we need for
483 * blk_cleanup_queue(): the silly thing may attempt to sleep.
484 * [Actually, it never needs to sleep for us, but it calls might_sleep()]
485 */
486static void ub_put(struct ub_dev *sc)
487{
488 unsigned long flags;
489
490 spin_lock_irqsave(&ub_lock, flags);
491 --sc->openc;
492 if (sc->openc == 0 && atomic_read(&sc->poison)) {
493 spin_unlock_irqrestore(&ub_lock, flags);
494 ub_cleanup(sc);
495 } else {
496 spin_unlock_irqrestore(&ub_lock, flags);
497 }
498}
499
500/*
501 * Final cleanup and deallocation.
502 */
503static void ub_cleanup(struct ub_dev *sc)
504{
Pete Zaitcevf4800072005-05-01 16:05:40 -0700505 struct list_head *p;
506 struct ub_lun *lun;
Jens Axboe165125e2007-07-24 09:28:11 +0200507 struct request_queue *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Pete Zaitcevf4800072005-05-01 16:05:40 -0700509 while (!list_empty(&sc->luns)) {
510 p = sc->luns.next;
511 lun = list_entry(p, struct ub_lun, link);
512 list_del(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Pete Zaitcevf4800072005-05-01 16:05:40 -0700514 /* I don't think queue can be NULL. But... Stolen from sx8.c */
515 if ((q = lun->disk->queue) != NULL)
516 blk_cleanup_queue(q);
517 /*
518 * If we zero disk->private_data BEFORE put_disk, we have
519 * to check for NULL all over the place in open, release,
520 * check_media and revalidate, because the block level
521 * semaphore is well inside the put_disk.
522 * But we cannot zero after the call, because *disk is gone.
523 * The sd.c is blatantly racy in this area.
524 */
525 /* disk->private_data = NULL; */
526 put_disk(lun->disk);
527 lun->disk = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Pete Zaitcevf4800072005-05-01 16:05:40 -0700529 ub_id_put(lun->id);
530 kfree(lun);
531 }
532
Pete Zaitcev77ef6c42006-05-03 00:16:00 -0700533 usb_set_intfdata(sc->intf, NULL);
534 usb_put_intf(sc->intf);
535 usb_put_dev(sc->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 kfree(sc);
537}
538
539/*
540 * The "command allocator".
541 */
Pete Zaitcevf4800072005-05-01 16:05:40 -0700542static struct ub_scsi_cmd *ub_get_cmd(struct ub_lun *lun)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
544 struct ub_scsi_cmd *ret;
545
Pete Zaitcevf4800072005-05-01 16:05:40 -0700546 if (lun->cmda[0])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 return NULL;
Pete Zaitcevf4800072005-05-01 16:05:40 -0700548 ret = &lun->cmdv[0];
549 lun->cmda[0] = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 return ret;
551}
552
Pete Zaitcevf4800072005-05-01 16:05:40 -0700553static void ub_put_cmd(struct ub_lun *lun, struct ub_scsi_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554{
Pete Zaitcevf4800072005-05-01 16:05:40 -0700555 if (cmd != &lun->cmdv[0]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 printk(KERN_WARNING "%s: releasing a foreign cmd %p\n",
Pete Zaitcevf4800072005-05-01 16:05:40 -0700557 lun->name, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 return;
559 }
Pete Zaitcevf4800072005-05-01 16:05:40 -0700560 if (!lun->cmda[0]) {
561 printk(KERN_WARNING "%s: releasing a free cmd\n", lun->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 return;
563 }
Pete Zaitcevf4800072005-05-01 16:05:40 -0700564 lun->cmda[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565}
566
567/*
568 * The command queue.
569 */
570static void ub_cmdq_add(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
571{
572 struct ub_scsi_cmd_queue *t = &sc->cmd_queue;
573
574 if (t->qlen++ == 0) {
575 t->head = cmd;
576 t->tail = cmd;
577 } else {
578 t->tail->next = cmd;
579 t->tail = cmd;
580 }
581
582 if (t->qlen > t->qmax)
583 t->qmax = t->qlen;
584}
585
586static void ub_cmdq_insert(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
587{
588 struct ub_scsi_cmd_queue *t = &sc->cmd_queue;
589
590 if (t->qlen++ == 0) {
591 t->head = cmd;
592 t->tail = cmd;
593 } else {
594 cmd->next = t->head;
595 t->head = cmd;
596 }
597
598 if (t->qlen > t->qmax)
599 t->qmax = t->qlen;
600}
601
602static struct ub_scsi_cmd *ub_cmdq_pop(struct ub_dev *sc)
603{
604 struct ub_scsi_cmd_queue *t = &sc->cmd_queue;
605 struct ub_scsi_cmd *cmd;
606
607 if (t->qlen == 0)
608 return NULL;
609 if (--t->qlen == 0)
610 t->tail = NULL;
611 cmd = t->head;
612 t->head = cmd->next;
613 cmd->next = NULL;
614 return cmd;
615}
616
617#define ub_cmdq_peek(sc) ((sc)->cmd_queue.head)
618
619/*
620 * The request function is our main entry point
621 */
622
Jens Axboe165125e2007-07-24 09:28:11 +0200623static void ub_request_fn(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624{
Pete Zaitcevf4800072005-05-01 16:05:40 -0700625 struct ub_lun *lun = q->queuedata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 struct request *rq;
627
628 while ((rq = elv_next_request(q)) != NULL) {
Pete Zaitcev6c1eb8c2005-07-30 22:51:52 -0700629 if (ub_request_fn_1(lun, rq) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 blk_stop_queue(q);
631 break;
632 }
633 }
634}
635
Pete Zaitcev6c1eb8c2005-07-30 22:51:52 -0700636static int ub_request_fn_1(struct ub_lun *lun, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
Pete Zaitcevf4800072005-05-01 16:05:40 -0700638 struct ub_dev *sc = lun->udev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 struct ub_scsi_cmd *cmd;
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800640 struct ub_request *urq;
641 int n_elem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Pete Zaitcevd1ad4ea2006-09-07 16:54:22 -0700643 if (atomic_read(&sc->poison)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 blkdev_dequeue_request(rq);
Pete Zaitcevd1ad4ea2006-09-07 16:54:22 -0700645 ub_end_rq(rq, DID_NO_CONNECT << 16);
646 return 0;
647 }
648
649 if (lun->changed && !blk_pc_request(rq)) {
650 blkdev_dequeue_request(rq);
651 ub_end_rq(rq, SAM_STAT_CHECK_CONDITION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 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));
Jens Axboe45711f12007-10-22 21:19:53 +0200660 sg_init_table(cmd->sgv, UB_MAX_REQ_SG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
662 blkdev_dequeue_request(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800664 urq = &lun->urq;
665 memset(urq, 0, sizeof(struct ub_request));
666 urq->rq = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
668 /*
669 * get scatterlist from block layer
670 */
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800671 n_elem = blk_rq_map_sg(lun->disk->queue, rq, &urq->sgv[0]);
672 if (n_elem < 0) {
Pete Zaitcevb5600332006-05-25 20:08:50 -0700673 /* Impossible, because blk_rq_map_sg should not hit ENOMEM. */
Pete Zaitcevba6abf12005-07-30 22:38:30 -0700674 printk(KERN_INFO "%s: failed request map (%d)\n",
Pete Zaitcevb5600332006-05-25 20:08:50 -0700675 lun->name, n_elem);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800676 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 }
Pete Zaitcevba6abf12005-07-30 22:38:30 -0700678 if (n_elem > UB_MAX_REQ_SG) { /* Paranoia */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 printk(KERN_WARNING "%s: request with %d segments\n",
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800680 lun->name, n_elem);
681 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 }
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800683 urq->nsg = n_elem;
Pete Zaitcev64bd8452005-09-22 00:48:29 -0700684 sc->sg_stat[n_elem < 5 ? n_elem : 5]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800686 if (blk_pc_request(rq)) {
687 ub_cmd_build_packet(sc, lun, cmd, urq);
688 } else {
689 ub_cmd_build_block(sc, lun, cmd, urq);
690 }
691 cmd->state = UB_CMDST_INIT;
692 cmd->lun = lun;
693 cmd->done = ub_rw_cmd_done;
694 cmd->back = urq;
695
696 cmd->tag = sc->tagcnt++;
697 if (ub_submit_scsi(sc, cmd) != 0)
698 goto drop;
699
700 return 0;
701
702drop:
703 ub_put_cmd(lun, cmd);
Pete Zaitcevd1ad4ea2006-09-07 16:54:22 -0700704 ub_end_rq(rq, DID_ERROR << 16);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800705 return 0;
706}
707
708static void ub_cmd_build_block(struct ub_dev *sc, struct ub_lun *lun,
709 struct ub_scsi_cmd *cmd, struct ub_request *urq)
710{
711 struct request *rq = urq->rq;
712 unsigned int block, nblks;
713
714 if (rq_data_dir(rq) == WRITE)
715 cmd->dir = UB_DIR_WRITE;
716 else
717 cmd->dir = UB_DIR_READ;
718
719 cmd->nsg = urq->nsg;
720 memcpy(cmd->sgv, urq->sgv, sizeof(struct scatterlist) * cmd->nsg);
721
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 /*
723 * build the command
724 *
725 * The call to blk_queue_hardsect_size() guarantees that request
726 * is aligned, but it is given in terms of 512 byte units, always.
727 */
Pete Zaitceva1cf96e2005-08-14 21:16:03 -0700728 block = rq->sector >> lun->capacity.bshift;
729 nblks = rq->nr_sectors >> lun->capacity.bshift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800731 cmd->cdb[0] = (cmd->dir == UB_DIR_READ)? READ_10: WRITE_10;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 /* 10-byte uses 4 bytes of LBA: 2147483648KB, 2097152MB, 2048GB */
733 cmd->cdb[2] = block >> 24;
734 cmd->cdb[3] = block >> 16;
735 cmd->cdb[4] = block >> 8;
736 cmd->cdb[5] = block;
737 cmd->cdb[7] = nblks >> 8;
738 cmd->cdb[8] = nblks;
739 cmd->cdb_len = 10;
740
Pete Zaitceva1cf96e2005-08-14 21:16:03 -0700741 cmd->len = rq->nr_sectors * 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742}
743
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800744static void ub_cmd_build_packet(struct ub_dev *sc, struct ub_lun *lun,
745 struct ub_scsi_cmd *cmd, struct ub_request *urq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746{
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800747 struct request *rq = urq->rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
749 if (rq->data_len == 0) {
750 cmd->dir = UB_DIR_NONE;
751 } else {
752 if (rq_data_dir(rq) == WRITE)
753 cmd->dir = UB_DIR_WRITE;
754 else
755 cmd->dir = UB_DIR_READ;
756 }
Pete Zaitceva1cf96e2005-08-14 21:16:03 -0700757
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800758 cmd->nsg = urq->nsg;
759 memcpy(cmd->sgv, urq->sgv, sizeof(struct scatterlist) * cmd->nsg);
Pete Zaitceva1cf96e2005-08-14 21:16:03 -0700760
761 memcpy(&cmd->cdb, rq->cmd, rq->cmd_len);
762 cmd->cdb_len = rq->cmd_len;
763
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 cmd->len = rq->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765}
766
767static void ub_rw_cmd_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
768{
Pete Zaitcevf4800072005-05-01 16:05:40 -0700769 struct ub_lun *lun = cmd->lun;
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800770 struct ub_request *urq = cmd->back;
771 struct request *rq;
Pete Zaitcevd1ad4ea2006-09-07 16:54:22 -0700772 unsigned int scsi_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800774 rq = urq->rq;
775
Pete Zaitceva1cf96e2005-08-14 21:16:03 -0700776 if (cmd->error == 0) {
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 Zaitcevd1ad4ea2006-09-07 16:54:22 -0700783 scsi_status = 0;
Pete Zaitceva1cf96e2005-08-14 21:16:03 -0700784 } else {
Pete Zaitceva1cf96e2005-08-14 21:16:03 -0700785 if (blk_pc_request(rq)) {
786 /* UB_SENSE_SIZE is smaller than SCSI_SENSE_BUFFERSIZE */
787 memcpy(rq->sense, sc->top_sense, UB_SENSE_SIZE);
788 rq->sense_len = UB_SENSE_SIZE;
789 if (sc->top_sense[0] != 0)
Pete Zaitcevd1ad4ea2006-09-07 16:54:22 -0700790 scsi_status = SAM_STAT_CHECK_CONDITION;
Pete Zaitceva1cf96e2005-08-14 21:16:03 -0700791 else
Pete Zaitcevd1ad4ea2006-09-07 16:54:22 -0700792 scsi_status = DID_ERROR << 16;
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800793 } else {
794 if (cmd->error == -EIO) {
795 if (ub_rw_cmd_retry(sc, lun, urq, cmd) == 0)
796 return;
797 }
Pete Zaitcevd1ad4ea2006-09-07 16:54:22 -0700798 scsi_status = SAM_STAT_CHECK_CONDITION;
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);
Pete Zaitcevd1ad4ea2006-09-07 16:54:22 -0700805 ub_end_rq(rq, scsi_status);
Pete Zaitcevba6abf12005-07-30 22:38:30 -0700806 blk_start_queue(lun->disk->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807}
808
Pete Zaitcevd1ad4ea2006-09-07 16:54:22 -0700809static void ub_end_rq(struct request *rq, unsigned int scsi_status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810{
Pete Zaitcevd1ad4ea2006-09-07 16:54:22 -0700811 int uptodate;
812
813 if (scsi_status == 0) {
814 uptodate = 1;
815 } else {
816 uptodate = 0;
817 rq->errors = scsi_status;
818 }
Daniel Marjamakiab930912005-12-17 02:38:46 -0800819 end_that_request_first(rq, uptodate, rq->hard_nr_sectors);
Tejun Heo8ffdc652006-01-06 09:49:03 +0100820 end_that_request_last(rq, uptodate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821}
822
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800823static int ub_rw_cmd_retry(struct ub_dev *sc, struct ub_lun *lun,
824 struct ub_request *urq, struct ub_scsi_cmd *cmd)
825{
826
827 if (atomic_read(&sc->poison))
828 return -ENXIO;
829
Pete Zaitcev2c2e4a22006-01-05 00:26:30 -0800830 ub_reset_enter(sc, urq->current_try);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800831
832 if (urq->current_try >= 3)
833 return -EIO;
834 urq->current_try++;
Pete Zaitcevb5600332006-05-25 20:08:50 -0700835
836 /* Remove this if anyone complains of flooding. */
837 printk(KERN_DEBUG "%s: dir %c len/act %d/%d "
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800838 "[sense %x %02x %02x] retry %d\n",
839 sc->name, UB_DIR_CHAR(cmd->dir), cmd->len, cmd->act_len,
840 cmd->key, cmd->asc, cmd->ascq, urq->current_try);
841
842 memset(cmd, 0, sizeof(struct ub_scsi_cmd));
843 ub_cmd_build_block(sc, lun, cmd, urq);
844
845 cmd->state = UB_CMDST_INIT;
846 cmd->lun = lun;
847 cmd->done = ub_rw_cmd_done;
848 cmd->back = urq;
849
850 cmd->tag = sc->tagcnt++;
851
852#if 0 /* Wasteful */
853 return ub_submit_scsi(sc, cmd);
854#else
855 ub_cmdq_add(sc, cmd);
856 return 0;
857#endif
858}
859
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860/*
861 * Submit a regular SCSI operation (not an auto-sense).
862 *
863 * The Iron Law of Good Submit Routine is:
864 * Zero return - callback is done, Nonzero return - callback is not done.
865 * No exceptions.
866 *
867 * Host is assumed locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 */
869static int ub_submit_scsi(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
870{
871
872 if (cmd->state != UB_CMDST_INIT ||
873 (cmd->dir != UB_DIR_NONE && cmd->len == 0)) {
874 return -EINVAL;
875 }
876
877 ub_cmdq_add(sc, cmd);
878 /*
879 * We can call ub_scsi_dispatch(sc) right away here, but it's a little
880 * safer to jump to a tasklet, in case upper layers do something silly.
881 */
882 tasklet_schedule(&sc->tasklet);
883 return 0;
884}
885
886/*
887 * Submit the first URB for the queued command.
888 * This function does not deal with queueing in any way.
889 */
890static int ub_scsi_cmd_start(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
891{
892 struct bulk_cb_wrap *bcb;
893 int rc;
894
895 bcb = &sc->work_bcb;
896
897 /*
898 * ``If the allocation length is eighteen or greater, and a device
899 * server returns less than eithteen bytes of data, the application
900 * client should assume that the bytes not transferred would have been
901 * zeroes had the device server returned those bytes.''
902 *
903 * We zero sense for all commands so that when a packet request
904 * fails it does not return a stale sense.
905 */
906 memset(&sc->top_sense, 0, UB_SENSE_SIZE);
907
908 /* set up the command wrapper */
909 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
910 bcb->Tag = cmd->tag; /* Endianness is not important */
911 bcb->DataTransferLength = cpu_to_le32(cmd->len);
912 bcb->Flags = (cmd->dir == UB_DIR_READ) ? 0x80 : 0;
Pete Zaitcevf4800072005-05-01 16:05:40 -0700913 bcb->Lun = (cmd->lun != NULL) ? cmd->lun->num : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 bcb->Length = cmd->cdb_len;
915
916 /* copy the command payload */
917 memcpy(bcb->CDB, cmd->cdb, UB_MAX_CDB_SIZE);
918
919 UB_INIT_COMPLETION(sc->work_done);
920
921 sc->last_pipe = sc->send_bulk_pipe;
922 usb_fill_bulk_urb(&sc->work_urb, sc->dev, sc->send_bulk_pipe,
923 bcb, US_BULK_CB_WRAP_LEN, ub_urb_complete, sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
925 /* Fill what we shouldn't be filling, because usb-storage did so. */
926 sc->work_urb.actual_length = 0;
927 sc->work_urb.error_count = 0;
928 sc->work_urb.status = 0;
929
930 if ((rc = usb_submit_urb(&sc->work_urb, GFP_ATOMIC)) != 0) {
931 /* XXX Clear stalls */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 ub_complete(&sc->work_done);
933 return rc;
934 }
935
936 sc->work_timer.expires = jiffies + UB_URB_TIMEOUT;
937 add_timer(&sc->work_timer);
938
939 cmd->state = UB_CMDST_CMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 return 0;
941}
942
943/*
944 * Timeout handler.
945 */
946static void ub_urb_timeout(unsigned long arg)
947{
948 struct ub_dev *sc = (struct ub_dev *) arg;
949 unsigned long flags;
950
Pete Zaitcev65b4fe52005-12-28 14:22:17 -0800951 spin_lock_irqsave(sc->lock, flags);
Pete Zaitcevb31f8212006-01-05 00:14:02 -0800952 if (!ub_is_completed(&sc->work_done))
953 usb_unlink_urb(&sc->work_urb);
Pete Zaitcev65b4fe52005-12-28 14:22:17 -0800954 spin_unlock_irqrestore(sc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955}
956
957/*
958 * Completion routine for the work URB.
959 *
960 * This can be called directly from usb_submit_urb (while we have
961 * the sc->lock taken) and from an interrupt (while we do NOT have
962 * the sc->lock taken). Therefore, bounce this off to a tasklet.
963 */
David Howells7d12e782006-10-05 14:55:46 +0100964static void ub_urb_complete(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965{
966 struct ub_dev *sc = urb->context;
967
968 ub_complete(&sc->work_done);
969 tasklet_schedule(&sc->tasklet);
970}
971
972static void ub_scsi_action(unsigned long _dev)
973{
974 struct ub_dev *sc = (struct ub_dev *) _dev;
975 unsigned long flags;
976
Pete Zaitcev65b4fe52005-12-28 14:22:17 -0800977 spin_lock_irqsave(sc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 ub_scsi_dispatch(sc);
Pete Zaitcev65b4fe52005-12-28 14:22:17 -0800979 spin_unlock_irqrestore(sc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980}
981
982static void ub_scsi_dispatch(struct ub_dev *sc)
983{
984 struct ub_scsi_cmd *cmd;
985 int rc;
986
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800987 while (!sc->reset && (cmd = ub_cmdq_peek(sc)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 if (cmd->state == UB_CMDST_DONE) {
989 ub_cmdq_pop(sc);
990 (*cmd->done)(sc, cmd);
991 } else if (cmd->state == UB_CMDST_INIT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 if ((rc = ub_scsi_cmd_start(sc, cmd)) == 0)
993 break;
994 cmd->error = rc;
995 cmd->state = UB_CMDST_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 } else {
997 if (!ub_is_completed(&sc->work_done))
998 break;
Pete Zaitcevb31f8212006-01-05 00:14:02 -0800999 del_timer(&sc->work_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 ub_scsi_urb_compl(sc, cmd);
1001 }
1002 }
1003}
1004
1005static void ub_scsi_urb_compl(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1006{
1007 struct urb *urb = &sc->work_urb;
1008 struct bulk_cs_wrap *bcs;
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001009 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 int rc;
1011
1012 if (atomic_read(&sc->poison)) {
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001013 ub_state_done(sc, cmd, -ENODEV);
1014 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 }
1016
1017 if (cmd->state == UB_CMDST_CLEAR) {
1018 if (urb->status == -EPIPE) {
1019 /*
1020 * STALL while clearning STALL.
1021 * The control pipe clears itself - nothing to do.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 */
Pete Zaitcevf4800072005-05-01 16:05:40 -07001023 printk(KERN_NOTICE "%s: stall on control pipe\n",
1024 sc->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 goto Bad_End;
1026 }
1027
1028 /*
1029 * We ignore the result for the halt clear.
1030 */
1031
1032 /* reset the endpoint toggle */
1033 usb_settoggle(sc->dev, usb_pipeendpoint(sc->last_pipe),
1034 usb_pipeout(sc->last_pipe), 0);
1035
1036 ub_state_sense(sc, cmd);
1037
1038 } else if (cmd->state == UB_CMDST_CLR2STS) {
1039 if (urb->status == -EPIPE) {
Pete Zaitcevf4800072005-05-01 16:05:40 -07001040 printk(KERN_NOTICE "%s: stall on control pipe\n",
1041 sc->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 goto Bad_End;
1043 }
1044
1045 /*
1046 * We ignore the result for the halt clear.
1047 */
1048
1049 /* reset the endpoint toggle */
1050 usb_settoggle(sc->dev, usb_pipeendpoint(sc->last_pipe),
1051 usb_pipeout(sc->last_pipe), 0);
1052
1053 ub_state_stat(sc, cmd);
1054
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001055 } else if (cmd->state == UB_CMDST_CLRRS) {
1056 if (urb->status == -EPIPE) {
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001057 printk(KERN_NOTICE "%s: stall on control pipe\n",
1058 sc->name);
1059 goto Bad_End;
1060 }
1061
1062 /*
1063 * We ignore the result for the halt clear.
1064 */
1065
1066 /* reset the endpoint toggle */
1067 usb_settoggle(sc->dev, usb_pipeendpoint(sc->last_pipe),
1068 usb_pipeout(sc->last_pipe), 0);
1069
1070 ub_state_stat_counted(sc, cmd);
1071
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 } else if (cmd->state == UB_CMDST_CMD) {
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001073 switch (urb->status) {
1074 case 0:
1075 break;
1076 case -EOVERFLOW:
1077 goto Bad_End;
1078 case -EPIPE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 rc = ub_submit_clear_stall(sc, cmd, sc->last_pipe);
1080 if (rc != 0) {
1081 printk(KERN_NOTICE "%s: "
Pete Zaitcevf4800072005-05-01 16:05:40 -07001082 "unable to submit clear (%d)\n",
1083 sc->name, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 /*
1085 * This is typically ENOMEM or some other such shit.
1086 * Retrying is pointless. Just do Bad End on it...
1087 */
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001088 ub_state_done(sc, cmd, rc);
1089 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 }
1091 cmd->state = UB_CMDST_CLEAR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 return;
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001093 case -ESHUTDOWN: /* unplug */
1094 case -EILSEQ: /* unplug timeout on uhci */
1095 ub_state_done(sc, cmd, -ENODEV);
1096 return;
1097 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 goto Bad_End;
1099 }
1100 if (urb->actual_length != US_BULK_CB_WRAP_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 goto Bad_End;
1102 }
1103
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001104 if (cmd->dir == UB_DIR_NONE || cmd->nsg < 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 ub_state_stat(sc, cmd);
1106 return;
1107 }
1108
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001109 // udelay(125); // usb-storage has this
1110 ub_data_start(sc, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
1112 } else if (cmd->state == UB_CMDST_DATA) {
1113 if (urb->status == -EPIPE) {
1114 rc = ub_submit_clear_stall(sc, cmd, sc->last_pipe);
1115 if (rc != 0) {
1116 printk(KERN_NOTICE "%s: "
Pete Zaitcevf4800072005-05-01 16:05:40 -07001117 "unable to submit clear (%d)\n",
1118 sc->name, rc);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001119 ub_state_done(sc, cmd, rc);
1120 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 }
1122 cmd->state = UB_CMDST_CLR2STS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 return;
1124 }
1125 if (urb->status == -EOVERFLOW) {
1126 /*
1127 * A babble? Failure, but we must transfer CSW now.
1128 */
1129 cmd->error = -EOVERFLOW; /* A cheap trick... */
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001130 ub_state_stat(sc, cmd);
1131 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 }
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001133
1134 if (cmd->dir == UB_DIR_WRITE) {
1135 /*
1136 * Do not continue writes in case of a failure.
1137 * Doing so would cause sectors to be mixed up,
1138 * which is worse than sectors lost.
1139 *
1140 * We must try to read the CSW, or many devices
1141 * get confused.
1142 */
1143 len = urb->actual_length;
1144 if (urb->status != 0 ||
1145 len != cmd->sgv[cmd->current_sg].length) {
1146 cmd->act_len += len;
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001147
1148 cmd->error = -EIO;
1149 ub_state_stat(sc, cmd);
1150 return;
1151 }
1152
1153 } else {
1154 /*
1155 * If an error occurs on read, we record it, and
1156 * continue to fetch data in order to avoid bubble.
1157 *
1158 * As a small shortcut, we stop if we detect that
1159 * a CSW mixed into data.
1160 */
1161 if (urb->status != 0)
1162 cmd->error = -EIO;
1163
1164 len = urb->actual_length;
1165 if (urb->status != 0 ||
1166 len != cmd->sgv[cmd->current_sg].length) {
1167 if ((len & 0x1FF) == US_BULK_CS_WRAP_LEN)
1168 goto Bad_End;
1169 }
1170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001172 cmd->act_len += urb->actual_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001174 if (++cmd->current_sg < cmd->nsg) {
1175 ub_data_start(sc, cmd);
1176 return;
1177 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 ub_state_stat(sc, cmd);
1179
1180 } else if (cmd->state == UB_CMDST_STAT) {
1181 if (urb->status == -EPIPE) {
1182 rc = ub_submit_clear_stall(sc, cmd, sc->last_pipe);
1183 if (rc != 0) {
1184 printk(KERN_NOTICE "%s: "
Pete Zaitcevf4800072005-05-01 16:05:40 -07001185 "unable to submit clear (%d)\n",
1186 sc->name, rc);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001187 ub_state_done(sc, cmd, rc);
1188 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 }
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001190
1191 /*
1192 * Having a stall when getting CSW is an error, so
1193 * make sure uppper levels are not oblivious to it.
1194 */
1195 cmd->error = -EIO; /* A cheap trick... */
1196
1197 cmd->state = UB_CMDST_CLRRS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 return;
1199 }
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001200
1201 /* Catch everything, including -EOVERFLOW and other nasties. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 if (urb->status != 0)
1203 goto Bad_End;
1204
1205 if (urb->actual_length == 0) {
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001206 ub_state_stat_counted(sc, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 return;
1208 }
1209
1210 /*
1211 * Check the returned Bulk protocol status.
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001212 * The status block has to be validated first.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 */
1214
1215 bcs = &sc->work_bcs;
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001216
1217 if (sc->signature == cpu_to_le32(0)) {
1218 /*
1219 * This is the first reply, so do not perform the check.
1220 * Instead, remember the signature the device uses
1221 * for future checks. But do not allow a nul.
1222 */
1223 sc->signature = bcs->Signature;
1224 if (sc->signature == cpu_to_le32(0)) {
1225 ub_state_stat_counted(sc, cmd);
1226 return;
1227 }
1228 } else {
1229 if (bcs->Signature != sc->signature) {
1230 ub_state_stat_counted(sc, cmd);
1231 return;
1232 }
1233 }
1234
1235 if (bcs->Tag != cmd->tag) {
1236 /*
1237 * This usually happens when we disagree with the
1238 * device's microcode about something. For instance,
1239 * a few of them throw this after timeouts. They buffer
1240 * commands and reply at commands we timed out before.
1241 * Without flushing these replies we loop forever.
1242 */
1243 ub_state_stat_counted(sc, cmd);
1244 return;
1245 }
1246
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001247 len = le32_to_cpu(bcs->Residue);
1248 if (len != cmd->len - cmd->act_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 /*
1250 * It is all right to transfer less, the caller has
1251 * to check. But it's not all right if the device
1252 * counts disagree with our counts.
1253 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 goto Bad_End;
1255 }
1256
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 switch (bcs->Status) {
1258 case US_BULK_STAT_OK:
1259 break;
1260 case US_BULK_STAT_FAIL:
1261 ub_state_sense(sc, cmd);
1262 return;
1263 case US_BULK_STAT_PHASE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 goto Bad_End;
1265 default:
1266 printk(KERN_INFO "%s: unknown CSW status 0x%x\n",
1267 sc->name, bcs->Status);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001268 ub_state_done(sc, cmd, -EINVAL);
1269 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 }
1271
1272 /* Not zeroing error to preserve a babble indicator */
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001273 if (cmd->error != 0) {
1274 ub_state_sense(sc, cmd);
1275 return;
1276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 cmd->state = UB_CMDST_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 ub_cmdq_pop(sc);
1279 (*cmd->done)(sc, cmd);
1280
1281 } else if (cmd->state == UB_CMDST_SENSE) {
1282 ub_state_done(sc, cmd, -EIO);
1283
1284 } else {
1285 printk(KERN_WARNING "%s: "
Pete Zaitcevf4800072005-05-01 16:05:40 -07001286 "wrong command state %d\n",
1287 sc->name, cmd->state);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001288 ub_state_done(sc, cmd, -EINVAL);
1289 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 }
1291 return;
1292
1293Bad_End: /* Little Excel is dead */
1294 ub_state_done(sc, cmd, -EIO);
1295}
1296
1297/*
1298 * Factorization helper for the command state machine:
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001299 * Initiate a data segment transfer.
1300 */
1301static void ub_data_start(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1302{
1303 struct scatterlist *sg = &cmd->sgv[cmd->current_sg];
1304 int pipe;
1305 int rc;
1306
1307 UB_INIT_COMPLETION(sc->work_done);
1308
1309 if (cmd->dir == UB_DIR_READ)
1310 pipe = sc->recv_bulk_pipe;
1311 else
1312 pipe = sc->send_bulk_pipe;
1313 sc->last_pipe = pipe;
Jens Axboe45711f12007-10-22 21:19:53 +02001314 usb_fill_bulk_urb(&sc->work_urb, sc->dev, pipe, sg_virt(sg),
1315 sg->length, ub_urb_complete, sc);
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001316 sc->work_urb.actual_length = 0;
1317 sc->work_urb.error_count = 0;
1318 sc->work_urb.status = 0;
1319
1320 if ((rc = usb_submit_urb(&sc->work_urb, GFP_ATOMIC)) != 0) {
1321 /* XXX Clear stalls */
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001322 ub_complete(&sc->work_done);
1323 ub_state_done(sc, cmd, rc);
1324 return;
1325 }
1326
1327 sc->work_timer.expires = jiffies + UB_DATA_TIMEOUT;
1328 add_timer(&sc->work_timer);
1329
1330 cmd->state = UB_CMDST_DATA;
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001331}
1332
1333/*
1334 * Factorization helper for the command state machine:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 * Finish the command.
1336 */
1337static void ub_state_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd, int rc)
1338{
1339
1340 cmd->error = rc;
1341 cmd->state = UB_CMDST_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 ub_cmdq_pop(sc);
1343 (*cmd->done)(sc, cmd);
1344}
1345
1346/*
1347 * Factorization helper for the command state machine:
1348 * Submit a CSW read.
1349 */
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001350static int __ub_state_stat(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351{
1352 int rc;
1353
1354 UB_INIT_COMPLETION(sc->work_done);
1355
1356 sc->last_pipe = sc->recv_bulk_pipe;
1357 usb_fill_bulk_urb(&sc->work_urb, sc->dev, sc->recv_bulk_pipe,
1358 &sc->work_bcs, US_BULK_CS_WRAP_LEN, ub_urb_complete, sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 sc->work_urb.actual_length = 0;
1360 sc->work_urb.error_count = 0;
1361 sc->work_urb.status = 0;
1362
1363 if ((rc = usb_submit_urb(&sc->work_urb, GFP_ATOMIC)) != 0) {
1364 /* XXX Clear stalls */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 ub_complete(&sc->work_done);
1366 ub_state_done(sc, cmd, rc);
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001367 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 }
1369
1370 sc->work_timer.expires = jiffies + UB_STAT_TIMEOUT;
1371 add_timer(&sc->work_timer);
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001372 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373}
1374
1375/*
1376 * Factorization helper for the command state machine:
1377 * Submit a CSW read and go to STAT state.
1378 */
1379static void ub_state_stat(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1380{
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001381
1382 if (__ub_state_stat(sc, cmd) != 0)
1383 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
1385 cmd->stat_count = 0;
1386 cmd->state = UB_CMDST_STAT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387}
1388
1389/*
1390 * Factorization helper for the command state machine:
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001391 * Submit a CSW read and go to STAT state with counter (along [C] path).
1392 */
1393static void ub_state_stat_counted(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1394{
1395
1396 if (++cmd->stat_count >= 4) {
1397 ub_state_sense(sc, cmd);
1398 return;
1399 }
1400
1401 if (__ub_state_stat(sc, cmd) != 0)
1402 return;
1403
1404 cmd->state = UB_CMDST_STAT;
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001405}
1406
1407/*
1408 * Factorization helper for the command state machine:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 * Submit a REQUEST SENSE and go to SENSE state.
1410 */
1411static void ub_state_sense(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1412{
1413 struct ub_scsi_cmd *scmd;
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001414 struct scatterlist *sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 int rc;
1416
1417 if (cmd->cdb[0] == REQUEST_SENSE) {
1418 rc = -EPIPE;
1419 goto error;
1420 }
1421
1422 scmd = &sc->top_rqs_cmd;
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001423 memset(scmd, 0, sizeof(struct ub_scsi_cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 scmd->cdb[0] = REQUEST_SENSE;
1425 scmd->cdb[4] = UB_SENSE_SIZE;
1426 scmd->cdb_len = 6;
1427 scmd->dir = UB_DIR_READ;
1428 scmd->state = UB_CMDST_INIT;
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001429 scmd->nsg = 1;
1430 sg = &scmd->sgv[0];
Jens Axboe642f1492007-10-24 11:20:47 +02001431 sg_set_page(sg, virt_to_page(sc->top_sense), UB_SENSE_SIZE,
1432 (unsigned long)sc->top_sense & (PAGE_SIZE-1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 scmd->len = UB_SENSE_SIZE;
Pete Zaitcevf4800072005-05-01 16:05:40 -07001434 scmd->lun = cmd->lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 scmd->done = ub_top_sense_done;
1436 scmd->back = cmd;
1437
1438 scmd->tag = sc->tagcnt++;
1439
1440 cmd->state = UB_CMDST_SENSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
1442 ub_cmdq_insert(sc, scmd);
1443 return;
1444
1445error:
1446 ub_state_done(sc, cmd, rc);
1447}
1448
1449/*
1450 * A helper for the command's state machine:
1451 * Submit a stall clear.
1452 */
1453static int ub_submit_clear_stall(struct ub_dev *sc, struct ub_scsi_cmd *cmd,
1454 int stalled_pipe)
1455{
1456 int endp;
1457 struct usb_ctrlrequest *cr;
1458 int rc;
1459
1460 endp = usb_pipeendpoint(stalled_pipe);
1461 if (usb_pipein (stalled_pipe))
1462 endp |= USB_DIR_IN;
1463
1464 cr = &sc->work_cr;
1465 cr->bRequestType = USB_RECIP_ENDPOINT;
1466 cr->bRequest = USB_REQ_CLEAR_FEATURE;
1467 cr->wValue = cpu_to_le16(USB_ENDPOINT_HALT);
1468 cr->wIndex = cpu_to_le16(endp);
1469 cr->wLength = cpu_to_le16(0);
1470
1471 UB_INIT_COMPLETION(sc->work_done);
1472
1473 usb_fill_control_urb(&sc->work_urb, sc->dev, sc->send_ctrl_pipe,
1474 (unsigned char*) cr, NULL, 0, ub_urb_complete, sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 sc->work_urb.actual_length = 0;
1476 sc->work_urb.error_count = 0;
1477 sc->work_urb.status = 0;
1478
1479 if ((rc = usb_submit_urb(&sc->work_urb, GFP_ATOMIC)) != 0) {
1480 ub_complete(&sc->work_done);
1481 return rc;
1482 }
1483
1484 sc->work_timer.expires = jiffies + UB_CTRL_TIMEOUT;
1485 add_timer(&sc->work_timer);
1486 return 0;
1487}
1488
1489/*
1490 */
1491static void ub_top_sense_done(struct ub_dev *sc, struct ub_scsi_cmd *scmd)
1492{
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001493 unsigned char *sense = sc->top_sense;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 struct ub_scsi_cmd *cmd;
1495
1496 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 * Find the command which triggered the unit attention or a check,
1498 * save the sense into it, and advance its state machine.
1499 */
1500 if ((cmd = ub_cmdq_peek(sc)) == NULL) {
1501 printk(KERN_WARNING "%s: sense done while idle\n", sc->name);
1502 return;
1503 }
1504 if (cmd != scmd->back) {
1505 printk(KERN_WARNING "%s: "
Pete Zaitcevf4800072005-05-01 16:05:40 -07001506 "sense done for wrong command 0x%x\n",
1507 sc->name, cmd->tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 return;
1509 }
1510 if (cmd->state != UB_CMDST_SENSE) {
1511 printk(KERN_WARNING "%s: "
Pete Zaitcevf4800072005-05-01 16:05:40 -07001512 "sense done with bad cmd state %d\n",
1513 sc->name, cmd->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 return;
1515 }
1516
Pete Zaitcev952ba222006-03-02 16:42:59 -08001517 /*
1518 * Ignoring scmd->act_len, because the buffer was pre-zeroed.
1519 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 cmd->key = sense[2] & 0x0F;
1521 cmd->asc = sense[12];
1522 cmd->ascq = sense[13];
1523
1524 ub_scsi_urb_compl(sc, cmd);
1525}
1526
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527/*
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001528 * Reset management
Pete Zaitcev2c2e4a22006-01-05 00:26:30 -08001529 * XXX Move usb_reset_device to khubd. Hogging kevent is not a good thing.
1530 * XXX Make usb_sync_reset asynchronous.
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001531 */
1532
Pete Zaitcev2c2e4a22006-01-05 00:26:30 -08001533static void ub_reset_enter(struct ub_dev *sc, int try)
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001534{
1535
1536 if (sc->reset) {
1537 /* This happens often on multi-LUN devices. */
1538 return;
1539 }
Pete Zaitcev2c2e4a22006-01-05 00:26:30 -08001540 sc->reset = try + 1;
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001541
1542#if 0 /* Not needed because the disconnect waits for us. */
1543 unsigned long flags;
1544 spin_lock_irqsave(&ub_lock, flags);
1545 sc->openc++;
1546 spin_unlock_irqrestore(&ub_lock, flags);
1547#endif
1548
1549#if 0 /* We let them stop themselves. */
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001550 struct ub_lun *lun;
Matthias Kaehlckea69228d2007-07-09 12:03:07 -07001551 list_for_each_entry(lun, &sc->luns, link) {
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001552 blk_stop_queue(lun->disk->queue);
1553 }
1554#endif
1555
1556 schedule_work(&sc->reset_work);
1557}
1558
David Howellsc4028952006-11-22 14:57:56 +00001559static void ub_reset_task(struct work_struct *work)
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001560{
David Howellsc4028952006-11-22 14:57:56 +00001561 struct ub_dev *sc = container_of(work, struct ub_dev, reset_work);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001562 unsigned long flags;
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001563 struct ub_lun *lun;
1564 int lkr, rc;
1565
1566 if (!sc->reset) {
1567 printk(KERN_WARNING "%s: Running reset unrequested\n",
1568 sc->name);
1569 return;
1570 }
1571
1572 if (atomic_read(&sc->poison)) {
Pete Zaitcevb5600332006-05-25 20:08:50 -07001573 ;
Pete Zaitcev2c2e4a22006-01-05 00:26:30 -08001574 } else if ((sc->reset & 1) == 0) {
1575 ub_sync_reset(sc);
1576 msleep(700); /* usb-storage sleeps 6s (!) */
1577 ub_probe_clear_stall(sc, sc->recv_bulk_pipe);
1578 ub_probe_clear_stall(sc, sc->send_bulk_pipe);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001579 } else if (sc->dev->actconfig->desc.bNumInterfaces != 1) {
Pete Zaitcevb5600332006-05-25 20:08:50 -07001580 ;
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001581 } 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);
Matthias Kaehlckea69228d2007-07-09 12:03:07 -07001608 list_for_each_entry(lun, &sc->luns, link) {
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001609 blk_start_queue(lun->disk->queue);
1610 }
1611 wake_up(&sc->reset_wait);
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08001612 spin_unlock_irqrestore(sc->lock, flags);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001613}
1614
1615/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 * This is called from a process context.
1617 */
Pete Zaitcevf4800072005-05-01 16:05:40 -07001618static void ub_revalidate(struct ub_dev *sc, struct ub_lun *lun)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619{
1620
Pete Zaitcevf4800072005-05-01 16:05:40 -07001621 lun->readonly = 0; /* XXX Query this from the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
Pete Zaitcevf4800072005-05-01 16:05:40 -07001623 lun->capacity.nsec = 0;
1624 lun->capacity.bsize = 512;
1625 lun->capacity.bshift = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626
Pete Zaitcevf4800072005-05-01 16:05:40 -07001627 if (ub_sync_tur(sc, lun) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 return; /* Not ready */
Pete Zaitcevf4800072005-05-01 16:05:40 -07001629 lun->changed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
Pete Zaitcevf4800072005-05-01 16:05:40 -07001631 if (ub_sync_read_cap(sc, lun, &lun->capacity) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 /*
1633 * The retry here means something is wrong, either with the
1634 * device, with the transport, or with our code.
1635 * We keep this because sd.c has retries for capacity.
1636 */
Pete Zaitcevf4800072005-05-01 16:05:40 -07001637 if (ub_sync_read_cap(sc, lun, &lun->capacity) != 0) {
1638 lun->capacity.nsec = 0;
1639 lun->capacity.bsize = 512;
1640 lun->capacity.bshift = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 }
1642 }
1643}
1644
1645/*
1646 * The open funcion.
1647 * This is mostly needed to keep refcounting, but also to support
1648 * media checks on removable media drives.
1649 */
1650static int ub_bd_open(struct inode *inode, struct file *filp)
1651{
1652 struct gendisk *disk = inode->i_bdev->bd_disk;
Pete Zaitcev41fea55e2006-04-28 20:45:49 -07001653 struct ub_lun *lun = disk->private_data;
1654 struct ub_dev *sc = lun->udev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 unsigned long flags;
1656 int rc;
1657
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 spin_lock_irqsave(&ub_lock, flags);
1659 if (atomic_read(&sc->poison)) {
1660 spin_unlock_irqrestore(&ub_lock, flags);
1661 return -ENXIO;
1662 }
1663 sc->openc++;
1664 spin_unlock_irqrestore(&ub_lock, flags);
1665
Pete Zaitcevf4800072005-05-01 16:05:40 -07001666 if (lun->removable || lun->readonly)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 check_disk_change(inode->i_bdev);
1668
1669 /*
1670 * The sd.c considers ->media_present and ->changed not equivalent,
1671 * under some pretty murky conditions (a failure of READ CAPACITY).
1672 * We may need it one day.
1673 */
Pete Zaitcevf4800072005-05-01 16:05:40 -07001674 if (lun->removable && lun->changed && !(filp->f_flags & O_NDELAY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 rc = -ENOMEDIUM;
1676 goto err_open;
1677 }
1678
Pete Zaitcevf4800072005-05-01 16:05:40 -07001679 if (lun->readonly && (filp->f_mode & FMODE_WRITE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 rc = -EROFS;
1681 goto err_open;
1682 }
1683
1684 return 0;
1685
1686err_open:
1687 ub_put(sc);
1688 return rc;
1689}
1690
1691/*
1692 */
1693static int ub_bd_release(struct inode *inode, struct file *filp)
1694{
1695 struct gendisk *disk = inode->i_bdev->bd_disk;
Pete Zaitcevf4800072005-05-01 16:05:40 -07001696 struct ub_lun *lun = disk->private_data;
1697 struct ub_dev *sc = lun->udev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
1699 ub_put(sc);
1700 return 0;
1701}
1702
1703/*
1704 * The ioctl interface.
1705 */
1706static int ub_bd_ioctl(struct inode *inode, struct file *filp,
1707 unsigned int cmd, unsigned long arg)
1708{
1709 struct gendisk *disk = inode->i_bdev->bd_disk;
1710 void __user *usermem = (void __user *) arg;
1711
FUJITA Tomonori45e79a32007-07-09 12:39:20 +02001712 return scsi_cmd_ioctl(filp, disk->queue, disk, cmd, usermem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713}
1714
1715/*
1716 * This is called once a new disk was seen by the block layer or by ub_probe().
1717 * The main onjective here is to discover the features of the media such as
1718 * the capacity, read-only status, etc. USB storage generally does not
1719 * need to be spun up, but if we needed it, this would be the place.
1720 *
1721 * This call can sleep.
1722 *
1723 * The return code is not used.
1724 */
1725static int ub_bd_revalidate(struct gendisk *disk)
1726{
Pete Zaitcevf4800072005-05-01 16:05:40 -07001727 struct ub_lun *lun = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728
Pete Zaitcevf4800072005-05-01 16:05:40 -07001729 ub_revalidate(lun->udev, lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
1731 /* XXX Support sector size switching like in sr.c */
Pete Zaitcevf4800072005-05-01 16:05:40 -07001732 blk_queue_hardsect_size(disk->queue, lun->capacity.bsize);
1733 set_capacity(disk, lun->capacity.nsec);
1734 // set_disk_ro(sdkp->disk, lun->readonly);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
1736 return 0;
1737}
1738
1739/*
1740 * The check is called by the block layer to verify if the media
1741 * is still available. It is supposed to be harmless, lightweight and
1742 * non-intrusive in case the media was not changed.
1743 *
1744 * This call can sleep.
1745 *
1746 * The return code is bool!
1747 */
1748static int ub_bd_media_changed(struct gendisk *disk)
1749{
Pete Zaitcevf4800072005-05-01 16:05:40 -07001750 struct ub_lun *lun = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751
Pete Zaitcevf4800072005-05-01 16:05:40 -07001752 if (!lun->removable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 return 0;
1754
1755 /*
1756 * We clean checks always after every command, so this is not
1757 * as dangerous as it looks. If the TEST_UNIT_READY fails here,
1758 * the device is actually not ready with operator or software
1759 * intervention required. One dangerous item might be a drive which
1760 * spins itself down, and come the time to write dirty pages, this
1761 * will fail, then block layer discards the data. Since we never
1762 * spin drives up, such devices simply cannot be used with ub anyway.
1763 */
Pete Zaitcevf4800072005-05-01 16:05:40 -07001764 if (ub_sync_tur(lun->udev, lun) != 0) {
1765 lun->changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 return 1;
1767 }
1768
Pete Zaitcevf4800072005-05-01 16:05:40 -07001769 return lun->changed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770}
1771
1772static struct block_device_operations ub_bd_fops = {
1773 .owner = THIS_MODULE,
1774 .open = ub_bd_open,
1775 .release = ub_bd_release,
1776 .ioctl = ub_bd_ioctl,
1777 .media_changed = ub_bd_media_changed,
1778 .revalidate_disk = ub_bd_revalidate,
1779};
1780
1781/*
1782 * Common ->done routine for commands executed synchronously.
1783 */
1784static void ub_probe_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1785{
1786 struct completion *cop = cmd->back;
1787 complete(cop);
1788}
1789
1790/*
1791 * Test if the device has a check condition on it, synchronously.
1792 */
Pete Zaitcevf4800072005-05-01 16:05:40 -07001793static int ub_sync_tur(struct ub_dev *sc, struct ub_lun *lun)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794{
1795 struct ub_scsi_cmd *cmd;
1796 enum { ALLOC_SIZE = sizeof(struct ub_scsi_cmd) };
1797 unsigned long flags;
1798 struct completion compl;
1799 int rc;
1800
1801 init_completion(&compl);
1802
1803 rc = -ENOMEM;
Pete Zaitcev29da7932006-02-13 20:35:57 -08001804 if ((cmd = kzalloc(ALLOC_SIZE, GFP_KERNEL)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 goto err_alloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806
1807 cmd->cdb[0] = TEST_UNIT_READY;
1808 cmd->cdb_len = 6;
1809 cmd->dir = UB_DIR_NONE;
1810 cmd->state = UB_CMDST_INIT;
Pete Zaitcevf4800072005-05-01 16:05:40 -07001811 cmd->lun = lun; /* This may be NULL, but that's ok */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 cmd->done = ub_probe_done;
1813 cmd->back = &compl;
1814
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08001815 spin_lock_irqsave(sc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 cmd->tag = sc->tagcnt++;
1817
1818 rc = ub_submit_scsi(sc, cmd);
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08001819 spin_unlock_irqrestore(sc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820
Pete Zaitcevb5600332006-05-25 20:08:50 -07001821 if (rc != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 goto err_submit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823
1824 wait_for_completion(&compl);
1825
1826 rc = cmd->error;
1827
1828 if (rc == -EIO && cmd->key != 0) /* Retries for benh's key */
1829 rc = cmd->key;
1830
1831err_submit:
1832 kfree(cmd);
1833err_alloc:
1834 return rc;
1835}
1836
1837/*
1838 * Read the SCSI capacity synchronously (for probing).
1839 */
Pete Zaitcevf4800072005-05-01 16:05:40 -07001840static int ub_sync_read_cap(struct ub_dev *sc, struct ub_lun *lun,
1841 struct ub_capacity *ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842{
1843 struct ub_scsi_cmd *cmd;
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001844 struct scatterlist *sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 char *p;
1846 enum { ALLOC_SIZE = sizeof(struct ub_scsi_cmd) + 8 };
1847 unsigned long flags;
1848 unsigned int bsize, shift;
1849 unsigned long nsec;
1850 struct completion compl;
1851 int rc;
1852
1853 init_completion(&compl);
1854
1855 rc = -ENOMEM;
Pete Zaitcev29da7932006-02-13 20:35:57 -08001856 if ((cmd = kzalloc(ALLOC_SIZE, GFP_KERNEL)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 goto err_alloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 p = (char *)cmd + sizeof(struct ub_scsi_cmd);
1859
1860 cmd->cdb[0] = 0x25;
1861 cmd->cdb_len = 10;
1862 cmd->dir = UB_DIR_READ;
1863 cmd->state = UB_CMDST_INIT;
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001864 cmd->nsg = 1;
1865 sg = &cmd->sgv[0];
Jens Axboe642f1492007-10-24 11:20:47 +02001866 sg_set_page(sg, virt_to_page(p), 8, (unsigned long)p & (PAGE_SIZE-1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 cmd->len = 8;
Pete Zaitcevf4800072005-05-01 16:05:40 -07001868 cmd->lun = lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 cmd->done = ub_probe_done;
1870 cmd->back = &compl;
1871
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08001872 spin_lock_irqsave(sc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 cmd->tag = sc->tagcnt++;
1874
1875 rc = ub_submit_scsi(sc, cmd);
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08001876 spin_unlock_irqrestore(sc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877
Pete Zaitcevb5600332006-05-25 20:08:50 -07001878 if (rc != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 goto err_submit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
1881 wait_for_completion(&compl);
1882
1883 if (cmd->error != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 rc = -EIO;
1885 goto err_read;
1886 }
1887 if (cmd->act_len != 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 rc = -EIO;
1889 goto err_read;
1890 }
1891
1892 /* sd.c special-cases sector size of 0 to mean 512. Needed? Safe? */
1893 nsec = be32_to_cpu(*(__be32 *)p) + 1;
1894 bsize = be32_to_cpu(*(__be32 *)(p + 4));
1895 switch (bsize) {
1896 case 512: shift = 0; break;
1897 case 1024: shift = 1; break;
1898 case 2048: shift = 2; break;
1899 case 4096: shift = 3; break;
1900 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 rc = -EDOM;
1902 goto err_inv_bsize;
1903 }
1904
1905 ret->bsize = bsize;
1906 ret->bshift = shift;
1907 ret->nsec = nsec << shift;
1908 rc = 0;
1909
1910err_inv_bsize:
1911err_read:
1912err_submit:
1913 kfree(cmd);
1914err_alloc:
1915 return rc;
1916}
1917
1918/*
1919 */
David Howells7d12e782006-10-05 14:55:46 +01001920static void ub_probe_urb_complete(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921{
1922 struct completion *cop = urb->context;
1923 complete(cop);
1924}
1925
1926static void ub_probe_timeout(unsigned long arg)
1927{
1928 struct completion *cop = (struct completion *) arg;
1929 complete(cop);
1930}
1931
1932/*
Pete Zaitcev2c2e4a22006-01-05 00:26:30 -08001933 * Reset with a Bulk reset.
1934 */
1935static int ub_sync_reset(struct ub_dev *sc)
1936{
1937 int ifnum = sc->intf->cur_altsetting->desc.bInterfaceNumber;
1938 struct usb_ctrlrequest *cr;
1939 struct completion compl;
1940 struct timer_list timer;
1941 int rc;
1942
1943 init_completion(&compl);
1944
1945 cr = &sc->work_cr;
1946 cr->bRequestType = USB_TYPE_CLASS | USB_RECIP_INTERFACE;
1947 cr->bRequest = US_BULK_RESET_REQUEST;
1948 cr->wValue = cpu_to_le16(0);
1949 cr->wIndex = cpu_to_le16(ifnum);
1950 cr->wLength = cpu_to_le16(0);
1951
1952 usb_fill_control_urb(&sc->work_urb, sc->dev, sc->send_ctrl_pipe,
1953 (unsigned char*) cr, NULL, 0, ub_probe_urb_complete, &compl);
1954 sc->work_urb.actual_length = 0;
1955 sc->work_urb.error_count = 0;
1956 sc->work_urb.status = 0;
1957
1958 if ((rc = usb_submit_urb(&sc->work_urb, GFP_KERNEL)) != 0) {
1959 printk(KERN_WARNING
1960 "%s: Unable to submit a bulk reset (%d)\n", sc->name, rc);
1961 return rc;
1962 }
1963
1964 init_timer(&timer);
1965 timer.function = ub_probe_timeout;
1966 timer.data = (unsigned long) &compl;
1967 timer.expires = jiffies + UB_CTRL_TIMEOUT;
1968 add_timer(&timer);
1969
1970 wait_for_completion(&compl);
1971
1972 del_timer_sync(&timer);
1973 usb_kill_urb(&sc->work_urb);
1974
1975 return sc->work_urb.status;
1976}
1977
1978/*
Pete Zaitcevf4800072005-05-01 16:05:40 -07001979 * Get number of LUNs by the way of Bulk GetMaxLUN command.
1980 */
1981static int ub_sync_getmaxlun(struct ub_dev *sc)
1982{
1983 int ifnum = sc->intf->cur_altsetting->desc.bInterfaceNumber;
1984 unsigned char *p;
1985 enum { ALLOC_SIZE = 1 };
1986 struct usb_ctrlrequest *cr;
1987 struct completion compl;
1988 struct timer_list timer;
1989 int nluns;
1990 int rc;
1991
1992 init_completion(&compl);
1993
1994 rc = -ENOMEM;
1995 if ((p = kmalloc(ALLOC_SIZE, GFP_KERNEL)) == NULL)
1996 goto err_alloc;
1997 *p = 55;
1998
1999 cr = &sc->work_cr;
2000 cr->bRequestType = USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
2001 cr->bRequest = US_BULK_GET_MAX_LUN;
2002 cr->wValue = cpu_to_le16(0);
2003 cr->wIndex = cpu_to_le16(ifnum);
2004 cr->wLength = cpu_to_le16(1);
2005
2006 usb_fill_control_urb(&sc->work_urb, sc->dev, sc->recv_ctrl_pipe,
2007 (unsigned char*) cr, p, 1, ub_probe_urb_complete, &compl);
Pete Zaitcevf4800072005-05-01 16:05:40 -07002008 sc->work_urb.actual_length = 0;
2009 sc->work_urb.error_count = 0;
2010 sc->work_urb.status = 0;
2011
Pete Zaitcevb5600332006-05-25 20:08:50 -07002012 if ((rc = usb_submit_urb(&sc->work_urb, GFP_KERNEL)) != 0)
Pete Zaitcevf4800072005-05-01 16:05:40 -07002013 goto err_submit;
Pete Zaitcevf4800072005-05-01 16:05:40 -07002014
2015 init_timer(&timer);
2016 timer.function = ub_probe_timeout;
2017 timer.data = (unsigned long) &compl;
2018 timer.expires = jiffies + UB_CTRL_TIMEOUT;
2019 add_timer(&timer);
2020
2021 wait_for_completion(&compl);
2022
2023 del_timer_sync(&timer);
2024 usb_kill_urb(&sc->work_urb);
2025
Pete Zaitcevb5600332006-05-25 20:08:50 -07002026 if ((rc = sc->work_urb.status) < 0)
Pete Zaitcev64bd8452005-09-22 00:48:29 -07002027 goto err_io;
Pete Zaitcev64bd8452005-09-22 00:48:29 -07002028
Pete Zaitcevf4800072005-05-01 16:05:40 -07002029 if (sc->work_urb.actual_length != 1) {
Pete Zaitcevf4800072005-05-01 16:05:40 -07002030 nluns = 0;
2031 } else {
2032 if ((nluns = *p) == 55) {
2033 nluns = 0;
2034 } else {
2035 /* GetMaxLUN returns the maximum LUN number */
2036 nluns += 1;
2037 if (nluns > UB_MAX_LUNS)
2038 nluns = UB_MAX_LUNS;
2039 }
Pete Zaitcevf4800072005-05-01 16:05:40 -07002040 }
2041
2042 kfree(p);
2043 return nluns;
2044
Pete Zaitcev64bd8452005-09-22 00:48:29 -07002045err_io:
Pete Zaitcevf4800072005-05-01 16:05:40 -07002046err_submit:
2047 kfree(p);
2048err_alloc:
2049 return rc;
2050}
2051
2052/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 * Clear initial stalls.
2054 */
2055static int ub_probe_clear_stall(struct ub_dev *sc, int stalled_pipe)
2056{
2057 int endp;
2058 struct usb_ctrlrequest *cr;
2059 struct completion compl;
2060 struct timer_list timer;
2061 int rc;
2062
2063 init_completion(&compl);
2064
2065 endp = usb_pipeendpoint(stalled_pipe);
2066 if (usb_pipein (stalled_pipe))
2067 endp |= USB_DIR_IN;
2068
2069 cr = &sc->work_cr;
2070 cr->bRequestType = USB_RECIP_ENDPOINT;
2071 cr->bRequest = USB_REQ_CLEAR_FEATURE;
2072 cr->wValue = cpu_to_le16(USB_ENDPOINT_HALT);
2073 cr->wIndex = cpu_to_le16(endp);
2074 cr->wLength = cpu_to_le16(0);
2075
2076 usb_fill_control_urb(&sc->work_urb, sc->dev, sc->send_ctrl_pipe,
2077 (unsigned char*) cr, NULL, 0, ub_probe_urb_complete, &compl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 sc->work_urb.actual_length = 0;
2079 sc->work_urb.error_count = 0;
2080 sc->work_urb.status = 0;
2081
2082 if ((rc = usb_submit_urb(&sc->work_urb, GFP_KERNEL)) != 0) {
2083 printk(KERN_WARNING
2084 "%s: Unable to submit a probe clear (%d)\n", sc->name, rc);
2085 return rc;
2086 }
2087
2088 init_timer(&timer);
2089 timer.function = ub_probe_timeout;
2090 timer.data = (unsigned long) &compl;
2091 timer.expires = jiffies + UB_CTRL_TIMEOUT;
2092 add_timer(&timer);
2093
2094 wait_for_completion(&compl);
2095
2096 del_timer_sync(&timer);
2097 usb_kill_urb(&sc->work_urb);
2098
2099 /* reset the endpoint toggle */
2100 usb_settoggle(sc->dev, endp, usb_pipeout(sc->last_pipe), 0);
2101
2102 return 0;
2103}
2104
2105/*
2106 * Get the pipe settings.
2107 */
2108static int ub_get_pipes(struct ub_dev *sc, struct usb_device *dev,
2109 struct usb_interface *intf)
2110{
2111 struct usb_host_interface *altsetting = intf->cur_altsetting;
2112 struct usb_endpoint_descriptor *ep_in = NULL;
2113 struct usb_endpoint_descriptor *ep_out = NULL;
2114 struct usb_endpoint_descriptor *ep;
2115 int i;
2116
2117 /*
2118 * Find the endpoints we need.
2119 * We are expecting a minimum of 2 endpoints - in and out (bulk).
2120 * We will ignore any others.
2121 */
2122 for (i = 0; i < altsetting->desc.bNumEndpoints; i++) {
2123 ep = &altsetting->endpoint[i].desc;
2124
2125 /* Is it a BULK endpoint? */
2126 if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
2127 == USB_ENDPOINT_XFER_BULK) {
2128 /* BULK in or out? */
Pete Zaitcev643616e2007-03-08 19:56:23 -08002129 if (ep->bEndpointAddress & USB_DIR_IN) {
2130 if (ep_in == NULL)
2131 ep_in = ep;
2132 } else {
2133 if (ep_out == NULL)
2134 ep_out = ep;
2135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 }
2137 }
2138
2139 if (ep_in == NULL || ep_out == NULL) {
Pete Zaitcevf4800072005-05-01 16:05:40 -07002140 printk(KERN_NOTICE "%s: failed endpoint check\n",
2141 sc->name);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08002142 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 }
2144
2145 /* Calculate and store the pipe values */
2146 sc->send_ctrl_pipe = usb_sndctrlpipe(dev, 0);
2147 sc->recv_ctrl_pipe = usb_rcvctrlpipe(dev, 0);
2148 sc->send_bulk_pipe = usb_sndbulkpipe(dev,
2149 ep_out->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
2150 sc->recv_bulk_pipe = usb_rcvbulkpipe(dev,
2151 ep_in->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
2152
2153 return 0;
2154}
2155
2156/*
2157 * Probing is done in the process context, which allows us to cheat
2158 * and not to build a state machine for the discovery.
2159 */
2160static int ub_probe(struct usb_interface *intf,
2161 const struct usb_device_id *dev_id)
2162{
2163 struct ub_dev *sc;
Pete Zaitcevf4800072005-05-01 16:05:40 -07002164 int nluns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 int rc;
2166 int i;
2167
Pete Zaitceva00828e2005-10-22 20:15:09 -07002168 if (usb_usual_check_type(dev_id, USB_US_TYPE_UB))
2169 return -ENXIO;
2170
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 rc = -ENOMEM;
Pete Zaitcev29da7932006-02-13 20:35:57 -08002172 if ((sc = kzalloc(sizeof(struct ub_dev), GFP_KERNEL)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 goto err_core;
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08002174 sc->lock = ub_next_lock();
Pete Zaitcevf4800072005-05-01 16:05:40 -07002175 INIT_LIST_HEAD(&sc->luns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 usb_init_urb(&sc->work_urb);
2177 tasklet_init(&sc->tasklet, ub_scsi_action, (unsigned long)sc);
2178 atomic_set(&sc->poison, 0);
David Howellsc4028952006-11-22 14:57:56 +00002179 INIT_WORK(&sc->reset_work, ub_reset_task);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08002180 init_waitqueue_head(&sc->reset_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181
2182 init_timer(&sc->work_timer);
2183 sc->work_timer.data = (unsigned long) sc;
2184 sc->work_timer.function = ub_urb_timeout;
2185
2186 ub_init_completion(&sc->work_done);
2187 sc->work_done.done = 1; /* A little yuk, but oh well... */
2188
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 sc->dev = interface_to_usbdev(intf);
2190 sc->intf = intf;
2191 // sc->ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 usb_set_intfdata(intf, sc);
2193 usb_get_dev(sc->dev);
Pete Zaitcev77ef6c42006-05-03 00:16:00 -07002194 /*
2195 * Since we give the interface struct to the block level through
2196 * disk->driverfs_dev, we have to pin it. Otherwise, block_uevent
2197 * oopses on close after a disconnect (kernels 2.6.16 and up).
2198 */
2199 usb_get_intf(sc->intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200
Pete Zaitcevf4800072005-05-01 16:05:40 -07002201 snprintf(sc->name, 12, DRV_NAME "(%d.%d)",
2202 sc->dev->bus->busnum, sc->dev->devnum);
2203
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 /* XXX Verify that we can handle the device (from descriptors) */
2205
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08002206 if (ub_get_pipes(sc, sc->dev, intf) != 0)
2207 goto err_dev_desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 /*
2210 * At this point, all USB initialization is done, do upper layer.
2211 * We really hate halfway initialized structures, so from the
2212 * invariants perspective, this ub_dev is fully constructed at
2213 * this point.
2214 */
2215
2216 /*
2217 * This is needed to clear toggles. It is a problem only if we do
2218 * `rmmod ub && modprobe ub` without disconnects, but we like that.
2219 */
Pete Zaitcevc6c88832005-09-22 00:49:45 -07002220#if 0 /* iPod Mini fails if we do this (big white iPod works) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 ub_probe_clear_stall(sc, sc->recv_bulk_pipe);
2222 ub_probe_clear_stall(sc, sc->send_bulk_pipe);
Pete Zaitcevc6c88832005-09-22 00:49:45 -07002223#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224
2225 /*
2226 * The way this is used by the startup code is a little specific.
2227 * A SCSI check causes a USB stall. Our common case code sees it
2228 * and clears the check, after which the device is ready for use.
2229 * But if a check was not present, any command other than
2230 * TEST_UNIT_READY ends with a lockup (including REQUEST_SENSE).
2231 *
2232 * If we neglect to clear the SCSI check, the first real command fails
2233 * (which is the capacity readout). We clear that and retry, but why
2234 * causing spurious retries for no reason.
2235 *
2236 * Revalidation may start with its own TEST_UNIT_READY, but that one
2237 * has to succeed, so we clear checks with an additional one here.
2238 * In any case it's not our business how revaliadation is implemented.
2239 */
Pete Zaitcevb5600332006-05-25 20:08:50 -07002240 for (i = 0; i < 3; i++) { /* Retries for the schwag key from KS'04 */
Pete Zaitcevf4800072005-05-01 16:05:40 -07002241 if ((rc = ub_sync_tur(sc, NULL)) <= 0) break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 if (rc != 0x6) break;
2243 msleep(10);
2244 }
2245
Pete Zaitcevf4800072005-05-01 16:05:40 -07002246 nluns = 1;
2247 for (i = 0; i < 3; i++) {
Pete Zaitcev11a223a2006-03-02 16:53:00 -08002248 if ((rc = ub_sync_getmaxlun(sc)) < 0)
Pete Zaitcevf4800072005-05-01 16:05:40 -07002249 break;
Pete Zaitcevf4800072005-05-01 16:05:40 -07002250 if (rc != 0) {
2251 nluns = rc;
2252 break;
2253 }
Pete Zaitcev9f793d22005-06-06 13:54:59 -07002254 msleep(100);
Pete Zaitcevf4800072005-05-01 16:05:40 -07002255 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256
Pete Zaitcevf4800072005-05-01 16:05:40 -07002257 for (i = 0; i < nluns; i++) {
2258 ub_probe_lun(sc, i);
2259 }
2260 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08002262err_dev_desc:
Pete Zaitcevf4800072005-05-01 16:05:40 -07002263 usb_set_intfdata(intf, NULL);
Pete Zaitcev77ef6c42006-05-03 00:16:00 -07002264 usb_put_intf(sc->intf);
Pete Zaitcevf4800072005-05-01 16:05:40 -07002265 usb_put_dev(sc->dev);
2266 kfree(sc);
2267err_core:
2268 return rc;
2269}
2270
2271static int ub_probe_lun(struct ub_dev *sc, int lnum)
2272{
2273 struct ub_lun *lun;
Jens Axboe165125e2007-07-24 09:28:11 +02002274 struct request_queue *q;
Pete Zaitcevf4800072005-05-01 16:05:40 -07002275 struct gendisk *disk;
2276 int rc;
2277
2278 rc = -ENOMEM;
Pete Zaitcev29da7932006-02-13 20:35:57 -08002279 if ((lun = kzalloc(sizeof(struct ub_lun), GFP_KERNEL)) == NULL)
Pete Zaitcevf4800072005-05-01 16:05:40 -07002280 goto err_alloc;
Pete Zaitcevf4800072005-05-01 16:05:40 -07002281 lun->num = lnum;
2282
2283 rc = -ENOSR;
2284 if ((lun->id = ub_id_get()) == -1)
2285 goto err_id;
2286
2287 lun->udev = sc;
Pete Zaitcevf4800072005-05-01 16:05:40 -07002288
2289 snprintf(lun->name, 16, DRV_NAME "%c(%d.%d.%d)",
2290 lun->id + 'a', sc->dev->bus->busnum, sc->dev->devnum, lun->num);
2291
2292 lun->removable = 1; /* XXX Query this from the device */
2293 lun->changed = 1; /* ub_revalidate clears only */
Pete Zaitcevf4800072005-05-01 16:05:40 -07002294 ub_revalidate(sc, lun);
2295
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 rc = -ENOMEM;
Pete Zaitcev4fb729f2005-12-17 02:34:12 -08002297 if ((disk = alloc_disk(UB_PARTS_PER_LUN)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 goto err_diskalloc;
2299
Pete Zaitcevf4800072005-05-01 16:05:40 -07002300 sprintf(disk->disk_name, DRV_NAME "%c", lun->id + 'a');
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 disk->major = UB_MAJOR;
Pete Zaitcev4fb729f2005-12-17 02:34:12 -08002302 disk->first_minor = lun->id * UB_PARTS_PER_LUN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 disk->fops = &ub_bd_fops;
Pete Zaitcevf4800072005-05-01 16:05:40 -07002304 disk->private_data = lun;
Pete Zaitcev64bd8452005-09-22 00:48:29 -07002305 disk->driverfs_dev = &sc->intf->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306
2307 rc = -ENOMEM;
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08002308 if ((q = blk_init_queue(ub_request_fn, sc->lock)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 goto err_blkqinit;
2310
2311 disk->queue = q;
2312
Pete Zaitcevf4800072005-05-01 16:05:40 -07002313 blk_queue_bounce_limit(q, BLK_BOUNCE_HIGH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 blk_queue_max_hw_segments(q, UB_MAX_REQ_SG);
2315 blk_queue_max_phys_segments(q, UB_MAX_REQ_SG);
Pete Zaitcevf4800072005-05-01 16:05:40 -07002316 blk_queue_segment_boundary(q, 0xffffffff); /* Dubious. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 blk_queue_max_sectors(q, UB_MAX_SECTORS);
Pete Zaitcevf4800072005-05-01 16:05:40 -07002318 blk_queue_hardsect_size(q, lun->capacity.bsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319
Pete Zaitcev688e9fb2006-05-25 20:04:54 -07002320 lun->disk = disk;
Pete Zaitcevf4800072005-05-01 16:05:40 -07002321 q->queuedata = lun;
Pete Zaitcev688e9fb2006-05-25 20:04:54 -07002322 list_add(&lun->link, &sc->luns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323
Pete Zaitcevf4800072005-05-01 16:05:40 -07002324 set_capacity(disk, lun->capacity.nsec);
2325 if (lun->removable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 disk->flags |= GENHD_FL_REMOVABLE;
2327
2328 add_disk(disk);
2329
2330 return 0;
2331
2332err_blkqinit:
2333 put_disk(disk);
2334err_diskalloc:
Pete Zaitcevf4800072005-05-01 16:05:40 -07002335 ub_id_put(lun->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336err_id:
Pete Zaitcevf4800072005-05-01 16:05:40 -07002337 kfree(lun);
2338err_alloc:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 return rc;
2340}
2341
2342static void ub_disconnect(struct usb_interface *intf)
2343{
2344 struct ub_dev *sc = usb_get_intfdata(intf);
Pete Zaitcevf4800072005-05-01 16:05:40 -07002345 struct ub_lun *lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 unsigned long flags;
2347
2348 /*
2349 * Prevent ub_bd_release from pulling the rug from under us.
2350 * XXX This is starting to look like a kref.
2351 * XXX Why not to take this ref at probe time?
2352 */
2353 spin_lock_irqsave(&ub_lock, flags);
2354 sc->openc++;
2355 spin_unlock_irqrestore(&ub_lock, flags);
2356
2357 /*
2358 * Fence stall clearnings, operations triggered by unlinkings and so on.
2359 * We do not attempt to unlink any URBs, because we do not trust the
2360 * unlink paths in HC drivers. Also, we get -84 upon disconnect anyway.
2361 */
2362 atomic_set(&sc->poison, 1);
2363
2364 /*
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08002365 * Wait for reset to end, if any.
2366 */
2367 wait_event(sc->reset_wait, !sc->reset);
2368
2369 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 * Blow away queued commands.
2371 *
2372 * Actually, this never works, because before we get here
2373 * the HCD terminates outstanding URB(s). It causes our
2374 * SCSI command queue to advance, commands fail to submit,
2375 * and the whole queue drains. So, we just use this code to
2376 * print warnings.
2377 */
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08002378 spin_lock_irqsave(sc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 {
2380 struct ub_scsi_cmd *cmd;
2381 int cnt = 0;
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08002382 while ((cmd = ub_cmdq_peek(sc)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 cmd->error = -ENOTCONN;
2384 cmd->state = UB_CMDST_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 ub_cmdq_pop(sc);
2386 (*cmd->done)(sc, cmd);
2387 cnt++;
2388 }
2389 if (cnt != 0) {
2390 printk(KERN_WARNING "%s: "
2391 "%d was queued after shutdown\n", sc->name, cnt);
2392 }
2393 }
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08002394 spin_unlock_irqrestore(sc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395
2396 /*
2397 * Unregister the upper layer.
2398 */
Matthias Kaehlckea69228d2007-07-09 12:03:07 -07002399 list_for_each_entry(lun, &sc->luns, link) {
Pete Zaitcev688e9fb2006-05-25 20:04:54 -07002400 del_gendisk(lun->disk);
Pete Zaitcevf4800072005-05-01 16:05:40 -07002401 /*
2402 * I wish I could do:
2403 * set_bit(QUEUE_FLAG_DEAD, &q->queue_flags);
2404 * As it is, we rely on our internal poisoning and let
2405 * the upper levels to spin furiously failing all the I/O.
2406 */
2407 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408
2409 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 * Testing for -EINPROGRESS is always a bug, so we are bending
2411 * the rules a little.
2412 */
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08002413 spin_lock_irqsave(sc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 if (sc->work_urb.status == -EINPROGRESS) { /* janitors: ignore */
2415 printk(KERN_WARNING "%s: "
2416 "URB is active after disconnect\n", sc->name);
2417 }
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08002418 spin_unlock_irqrestore(sc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419
2420 /*
2421 * There is virtually no chance that other CPU runs times so long
2422 * after ub_urb_complete should have called del_timer, but only if HCD
2423 * didn't forget to deliver a callback on unlink.
2424 */
2425 del_timer_sync(&sc->work_timer);
2426
2427 /*
2428 * At this point there must be no commands coming from anyone
2429 * and no URBs left in transit.
2430 */
2431
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 ub_put(sc);
2433}
2434
2435static struct usb_driver ub_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 .name = "ub",
2437 .probe = ub_probe,
2438 .disconnect = ub_disconnect,
2439 .id_table = ub_usb_ids,
2440};
2441
2442static int __init ub_init(void)
2443{
2444 int rc;
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08002445 int i;
2446
2447 for (i = 0; i < UB_QLOCK_NUM; i++)
2448 spin_lock_init(&ub_qlockv[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 if ((rc = register_blkdev(UB_MAJOR, DRV_NAME)) != 0)
2451 goto err_regblkdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452
2453 if ((rc = usb_register(&ub_driver)) != 0)
2454 goto err_register;
2455
Pete Zaitceva00828e2005-10-22 20:15:09 -07002456 usb_usual_set_present(USB_US_TYPE_UB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 return 0;
2458
2459err_register:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 unregister_blkdev(UB_MAJOR, DRV_NAME);
2461err_regblkdev:
2462 return rc;
2463}
2464
2465static void __exit ub_exit(void)
2466{
2467 usb_deregister(&ub_driver);
2468
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 unregister_blkdev(UB_MAJOR, DRV_NAME);
Pete Zaitceva00828e2005-10-22 20:15:09 -07002470 usb_usual_clear_present(USB_US_TYPE_UB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471}
2472
2473module_init(ub_init);
2474module_exit(ub_exit);
2475
2476MODULE_LICENSE("GPL");