blob: db4943c53d36bd035a9ef14d6abea2984973a794 [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)
Pete Zaitcevf4800072005-05-01 16:05:40 -070011 * -- Kill first_open (Al Viro fixed the block layer now)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * -- set readonly flag for CDs, set removable flag for CF readers
13 * -- do inquiry and verify we got a disk and not a tape (for LUN mismatch)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 * -- special case some senses, e.g. 3a/0 -> no media present, reduce retries
15 * -- verify the 13 conditions and do bulk resets
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 * -- kill last_pipe and simply do two-state clearing on both pipes
17 * -- verify protocol (bulk) from USB descriptors (maybe...)
Pete Zaitcevba6abf12005-07-30 22:38:30 -070018 * -- highmem
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * -- move top_sense and work_bcs into separate allocations (if they survive)
20 * for cache purists and esoteric architectures.
Pete Zaitcevba6abf12005-07-30 22:38:30 -070021 * -- Allocate structure for LUN 0 before the first ub_sync_tur, avoid NULL. ?
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 * -- prune comments, they are too volumnous
23 * -- Exterminate P3 printks
24 * -- Resove XXX's
25 * -- Redo "benh's retries", perhaps have spin-up code to handle them. V:D=?
Pete Zaitcev1872bce2005-07-27 11:43:51 -070026 * -- CLEAR, CLR2STS, CLRRS seem to be ripe for refactoring.
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 */
28#include <linux/kernel.h>
29#include <linux/module.h>
30#include <linux/usb.h>
Pete Zaitceva00828e2005-10-22 20:15:09 -070031#include <linux/usb_usual.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/blkdev.h>
33#include <linux/devfs_fs_kernel.h>
34#include <linux/timer.h>
35#include <scsi/scsi.h>
36
37#define DRV_NAME "ub"
38#define DEVFS_NAME DRV_NAME
39
40#define UB_MAJOR 180
41
42/*
Pete Zaitcev1872bce2005-07-27 11:43:51 -070043 * The command state machine is the key model for understanding of this driver.
44 *
45 * The general rule is that all transitions are done towards the bottom
46 * of the diagram, thus preventing any loops.
47 *
48 * An exception to that is how the STAT state is handled. A counter allows it
49 * to be re-entered along the path marked with [C].
50 *
51 * +--------+
52 * ! INIT !
53 * +--------+
54 * !
55 * ub_scsi_cmd_start fails ->--------------------------------------\
56 * ! !
57 * V !
58 * +--------+ !
59 * ! CMD ! !
60 * +--------+ !
61 * ! +--------+ !
62 * was -EPIPE -->-------------------------------->! CLEAR ! !
63 * ! +--------+ !
64 * ! ! !
65 * was error -->------------------------------------- ! --------->\
66 * ! ! !
67 * /--<-- cmd->dir == NONE ? ! !
68 * ! ! ! !
69 * ! V ! !
70 * ! +--------+ ! !
71 * ! ! DATA ! ! !
72 * ! +--------+ ! !
73 * ! ! +---------+ ! !
74 * ! was -EPIPE -->--------------->! CLR2STS ! ! !
75 * ! ! +---------+ ! !
76 * ! ! ! ! !
77 * ! ! was error -->---- ! --------->\
78 * ! was error -->--------------------- ! ------------- ! --------->\
79 * ! ! ! ! !
80 * ! V ! ! !
81 * \--->+--------+ ! ! !
82 * ! STAT !<--------------------------/ ! !
83 * /--->+--------+ ! !
84 * ! ! ! !
85 * [C] was -EPIPE -->-----------\ ! !
86 * ! ! ! ! !
87 * +<---- len == 0 ! ! !
88 * ! ! ! ! !
89 * ! was error -->--------------------------------------!---------->\
90 * ! ! ! ! !
91 * +<---- bad CSW ! ! !
92 * +<---- bad tag ! ! !
93 * ! ! V ! !
94 * ! ! +--------+ ! !
95 * ! ! ! CLRRS ! ! !
96 * ! ! +--------+ ! !
97 * ! ! ! ! !
98 * \------- ! --------------------[C]--------\ ! !
99 * ! ! ! !
100 * cmd->error---\ +--------+ ! !
101 * ! +--------------->! SENSE !<----------/ !
102 * STAT_FAIL----/ +--------+ !
103 * ! ! V
104 * ! V +--------+
105 * \--------------------------------\--------------------->! DONE !
106 * +--------+
107 */
108
109/*
Pete Zaitcevf4800072005-05-01 16:05:40 -0700110 * This many LUNs per USB device.
111 * Every one of them takes a host, see UB_MAX_HOSTS.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 */
Pete Zaitcev9f793d22005-06-06 13:54:59 -0700113#define UB_MAX_LUNS 9
Pete Zaitcevf4800072005-05-01 16:05:40 -0700114
115/*
116 */
117
Pete Zaitcev4fb729f2005-12-17 02:34:12 -0800118#define UB_PARTS_PER_LUN 8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120#define UB_MAX_CDB_SIZE 16 /* Corresponds to Bulk */
121
122#define UB_SENSE_SIZE 18
123
124/*
125 */
126
127/* command block wrapper */
128struct bulk_cb_wrap {
129 __le32 Signature; /* contains 'USBC' */
130 u32 Tag; /* unique per command id */
131 __le32 DataTransferLength; /* size of data */
132 u8 Flags; /* direction in bit 0 */
Pete Zaitcevf4800072005-05-01 16:05:40 -0700133 u8 Lun; /* LUN */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 u8 Length; /* of of the CDB */
135 u8 CDB[UB_MAX_CDB_SIZE]; /* max command */
136};
137
138#define US_BULK_CB_WRAP_LEN 31
139#define US_BULK_CB_SIGN 0x43425355 /*spells out USBC */
140#define US_BULK_FLAG_IN 1
141#define US_BULK_FLAG_OUT 0
142
143/* command status wrapper */
144struct bulk_cs_wrap {
145 __le32 Signature; /* should = 'USBS' */
146 u32 Tag; /* same as original command */
147 __le32 Residue; /* amount not transferred */
148 u8 Status; /* see below */
149};
150
151#define US_BULK_CS_WRAP_LEN 13
152#define US_BULK_CS_SIGN 0x53425355 /* spells out 'USBS' */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153#define US_BULK_STAT_OK 0
154#define US_BULK_STAT_FAIL 1
155#define US_BULK_STAT_PHASE 2
156
157/* bulk-only class specific requests */
158#define US_BULK_RESET_REQUEST 0xff
159#define US_BULK_GET_MAX_LUN 0xfe
160
161/*
162 */
163struct ub_dev;
164
Pete Zaitcev64bd8452005-09-22 00:48:29 -0700165#define UB_MAX_REQ_SG 9 /* cdrecord requires 32KB and maybe a header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166#define UB_MAX_SECTORS 64
167
168/*
169 * A second is more than enough for a 32K transfer (UB_MAX_SECTORS)
170 * even if a webcam hogs the bus, but some devices need time to spin up.
171 */
172#define UB_URB_TIMEOUT (HZ*2)
173#define UB_DATA_TIMEOUT (HZ*5) /* ZIP does spin-ups in the data phase */
174#define UB_STAT_TIMEOUT (HZ*5) /* Same spinups and eject for a dataless cmd. */
175#define UB_CTRL_TIMEOUT (HZ/2) /* 500ms ought to be enough to clear a stall */
176
177/*
178 * An instance of a SCSI command in transit.
179 */
180#define UB_DIR_NONE 0
181#define UB_DIR_READ 1
182#define UB_DIR_ILLEGAL2 2
183#define UB_DIR_WRITE 3
184
185#define UB_DIR_CHAR(c) (((c)==UB_DIR_WRITE)? 'w': \
186 (((c)==UB_DIR_READ)? 'r': 'n'))
187
188enum ub_scsi_cmd_state {
189 UB_CMDST_INIT, /* Initial state */
190 UB_CMDST_CMD, /* Command submitted */
191 UB_CMDST_DATA, /* Data phase */
192 UB_CMDST_CLR2STS, /* Clearing before requesting status */
193 UB_CMDST_STAT, /* Status phase */
194 UB_CMDST_CLEAR, /* Clearing a stall (halt, actually) */
Pete Zaitcev1872bce2005-07-27 11:43:51 -0700195 UB_CMDST_CLRRS, /* Clearing before retrying status */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 UB_CMDST_SENSE, /* Sending Request Sense */
197 UB_CMDST_DONE /* Final state */
198};
199
200static char *ub_scsi_cmd_stname[] = {
201 ". ",
202 "Cmd",
203 "dat",
204 "c2s",
205 "sts",
206 "clr",
Pete Zaitcev1872bce2005-07-27 11:43:51 -0700207 "crs",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 "Sen",
209 "fin"
210};
211
212struct ub_scsi_cmd {
213 unsigned char cdb[UB_MAX_CDB_SIZE];
214 unsigned char cdb_len;
215
216 unsigned char dir; /* 0 - none, 1 - read, 3 - write. */
217 unsigned char trace_index;
218 enum ub_scsi_cmd_state state;
219 unsigned int tag;
220 struct ub_scsi_cmd *next;
221
222 int error; /* Return code - valid upon done */
223 unsigned int act_len; /* Return size */
224 unsigned char key, asc, ascq; /* May be valid if error==-EIO */
225
226 int stat_count; /* Retries getting status. */
227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 unsigned int len; /* Requested length */
Pete Zaitceva1cf96e2005-08-14 21:16:03 -0700229 unsigned int current_sg;
230 unsigned int nsg; /* sgv[nsg] */
231 struct scatterlist sgv[UB_MAX_REQ_SG];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Pete Zaitcevf4800072005-05-01 16:05:40 -0700233 struct ub_lun *lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 void (*done)(struct ub_dev *, struct ub_scsi_cmd *);
235 void *back;
236};
237
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800238struct ub_request {
239 struct request *rq;
240 unsigned int current_try;
241 unsigned int nsg; /* sgv[nsg] */
242 struct scatterlist sgv[UB_MAX_REQ_SG];
243};
244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245/*
246 */
247struct ub_capacity {
248 unsigned long nsec; /* Linux size - 512 byte sectors */
249 unsigned int bsize; /* Linux hardsect_size */
250 unsigned int bshift; /* Shift between 512 and hard sects */
251};
252
253/*
254 * The SCSI command tracing structure.
255 */
256
257#define SCMD_ST_HIST_SZ 8
258#define SCMD_TRACE_SZ 63 /* Less than 4KB of 61-byte lines */
259
260struct ub_scsi_cmd_trace {
261 int hcur;
262 unsigned int tag;
263 unsigned int req_size, act_size;
264 unsigned char op;
265 unsigned char dir;
266 unsigned char key, asc, ascq;
267 char st_hst[SCMD_ST_HIST_SZ];
268};
269
270struct ub_scsi_trace {
271 int cur;
272 struct ub_scsi_cmd_trace vec[SCMD_TRACE_SZ];
273};
274
275/*
276 * This is a direct take-off from linux/include/completion.h
277 * The difference is that I do not wait on this thing, just poll.
278 * When I want to wait (ub_probe), I just use the stock completion.
279 *
280 * Note that INIT_COMPLETION takes no lock. It is correct. But why
281 * in the bloody hell that thing takes struct instead of pointer to struct
282 * is quite beyond me. I just copied it from the stock completion.
283 */
284struct ub_completion {
285 unsigned int done;
286 spinlock_t lock;
287};
288
289static inline void ub_init_completion(struct ub_completion *x)
290{
291 x->done = 0;
292 spin_lock_init(&x->lock);
293}
294
295#define UB_INIT_COMPLETION(x) ((x).done = 0)
296
297static void ub_complete(struct ub_completion *x)
298{
299 unsigned long flags;
300
301 spin_lock_irqsave(&x->lock, flags);
302 x->done++;
303 spin_unlock_irqrestore(&x->lock, flags);
304}
305
306static int ub_is_completed(struct ub_completion *x)
307{
308 unsigned long flags;
309 int ret;
310
311 spin_lock_irqsave(&x->lock, flags);
312 ret = x->done;
313 spin_unlock_irqrestore(&x->lock, flags);
314 return ret;
315}
316
317/*
318 */
319struct ub_scsi_cmd_queue {
320 int qlen, qmax;
321 struct ub_scsi_cmd *head, *tail;
322};
323
324/*
Pete Zaitcevf4800072005-05-01 16:05:40 -0700325 * The block device instance (one per LUN).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 */
Pete Zaitcevf4800072005-05-01 16:05:40 -0700327struct ub_lun {
328 struct ub_dev *udev;
329 struct list_head link;
330 struct gendisk *disk;
331 int id; /* Host index */
332 int num; /* LUN number */
333 char name[16];
334
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 int changed; /* Media was changed */
336 int removable;
337 int readonly;
338 int first_open; /* Kludge. See ub_bd_open. */
Pete Zaitcevf4800072005-05-01 16:05:40 -0700339
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800340 struct ub_request urq;
341
Pete Zaitcevf4800072005-05-01 16:05:40 -0700342 /* Use Ingo's mempool if or when we have more than one command. */
343 /*
344 * Currently we never need more than one command for the whole device.
345 * However, giving every LUN a command is a cheap and automatic way
346 * to enforce fairness between them.
347 */
348 int cmda[1];
349 struct ub_scsi_cmd cmdv[1];
350
351 struct ub_capacity capacity;
352};
353
354/*
355 * The USB device instance.
356 */
357struct ub_dev {
Pete Zaitcev65b4fe52005-12-28 14:22:17 -0800358 spinlock_t *lock;
Pete Zaitcevf4800072005-05-01 16:05:40 -0700359 atomic_t poison; /* The USB device is disconnected */
360 int openc; /* protected by ub_lock! */
361 /* kref is too implicit for our taste */
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800362 int reset; /* Reset is running */
Pete Zaitcevf4800072005-05-01 16:05:40 -0700363 unsigned int tagcnt;
364 char name[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 struct usb_device *dev;
366 struct usb_interface *intf;
367
Pete Zaitcevf4800072005-05-01 16:05:40 -0700368 struct list_head luns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
370 unsigned int send_bulk_pipe; /* cached pipe values */
371 unsigned int recv_bulk_pipe;
372 unsigned int send_ctrl_pipe;
373 unsigned int recv_ctrl_pipe;
374
375 struct tasklet_struct tasklet;
376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 struct ub_scsi_cmd_queue cmd_queue;
378 struct ub_scsi_cmd top_rqs_cmd; /* REQUEST SENSE */
379 unsigned char top_sense[UB_SENSE_SIZE];
380
381 struct ub_completion work_done;
382 struct urb work_urb;
383 struct timer_list work_timer;
384 int last_pipe; /* What might need clearing */
Pete Zaitcev1872bce2005-07-27 11:43:51 -0700385 __le32 signature; /* Learned signature */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 struct bulk_cb_wrap work_bcb;
387 struct bulk_cs_wrap work_bcs;
388 struct usb_ctrlrequest work_cr;
389
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800390 struct work_struct reset_work;
391 wait_queue_head_t reset_wait;
392
Pete Zaitcev64bd8452005-09-22 00:48:29 -0700393 int sg_stat[6];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 struct ub_scsi_trace tr;
395};
396
397/*
398 */
399static void ub_cleanup(struct ub_dev *sc);
Pete Zaitcev6c1eb8c2005-07-30 22:51:52 -0700400static int ub_request_fn_1(struct ub_lun *lun, struct request *rq);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800401static void ub_cmd_build_block(struct ub_dev *sc, struct ub_lun *lun,
402 struct ub_scsi_cmd *cmd, struct ub_request *urq);
403static void ub_cmd_build_packet(struct ub_dev *sc, struct ub_lun *lun,
404 struct ub_scsi_cmd *cmd, struct ub_request *urq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405static void ub_rw_cmd_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
406static void ub_end_rq(struct request *rq, int uptodate);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800407static int ub_rw_cmd_retry(struct ub_dev *sc, struct ub_lun *lun,
408 struct ub_request *urq, struct ub_scsi_cmd *cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409static int ub_submit_scsi(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
410static void ub_urb_complete(struct urb *urb, struct pt_regs *pt);
411static void ub_scsi_action(unsigned long _dev);
412static void ub_scsi_dispatch(struct ub_dev *sc);
413static void ub_scsi_urb_compl(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
Pete Zaitceva1cf96e2005-08-14 21:16:03 -0700414static void ub_data_start(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415static void ub_state_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd, int rc);
Pete Zaitcev1872bce2005-07-27 11:43:51 -0700416static int __ub_state_stat(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417static void ub_state_stat(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
Pete Zaitcev1872bce2005-07-27 11:43:51 -0700418static void ub_state_stat_counted(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419static void ub_state_sense(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
420static int ub_submit_clear_stall(struct ub_dev *sc, struct ub_scsi_cmd *cmd,
421 int stalled_pipe);
422static void ub_top_sense_done(struct ub_dev *sc, struct ub_scsi_cmd *scmd);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800423static void ub_reset_enter(struct ub_dev *sc);
424static void ub_reset_task(void *arg);
Pete Zaitcevf4800072005-05-01 16:05:40 -0700425static int ub_sync_tur(struct ub_dev *sc, struct ub_lun *lun);
426static int ub_sync_read_cap(struct ub_dev *sc, struct ub_lun *lun,
427 struct ub_capacity *ret);
428static int ub_probe_lun(struct ub_dev *sc, int lnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430/*
431 */
Pete Zaitceva00828e2005-10-22 20:15:09 -0700432#ifdef CONFIG_USB_LIBUSUAL
433
434#define ub_usb_ids storage_usb_ids
435#else
436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437static struct usb_device_id ub_usb_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_SCSI, US_PR_BULK) },
439 { }
440};
441
442MODULE_DEVICE_TABLE(usb, ub_usb_ids);
Pete Zaitceva00828e2005-10-22 20:15:09 -0700443#endif /* CONFIG_USB_LIBUSUAL */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445/*
446 * Find me a way to identify "next free minor" for add_disk(),
447 * and the array disappears the next day. However, the number of
448 * hosts has something to do with the naming and /proc/partitions.
449 * This has to be thought out in detail before changing.
450 * If UB_MAX_HOST was 1000, we'd use a bitmap. Or a better data structure.
451 */
452#define UB_MAX_HOSTS 26
453static char ub_hostv[UB_MAX_HOSTS];
Pete Zaitcevf4800072005-05-01 16:05:40 -0700454
Pete Zaitcev65b4fe52005-12-28 14:22:17 -0800455#define UB_QLOCK_NUM 5
456static spinlock_t ub_qlockv[UB_QLOCK_NUM];
457static int ub_qlock_next = 0;
458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459static DEFINE_SPINLOCK(ub_lock); /* Locks globals and ->openc */
460
461/*
462 * The SCSI command tracing procedures.
463 */
464
465static void ub_cmdtr_new(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
466{
467 int n;
468 struct ub_scsi_cmd_trace *t;
469
470 if ((n = sc->tr.cur + 1) == SCMD_TRACE_SZ) n = 0;
471 t = &sc->tr.vec[n];
472
473 memset(t, 0, sizeof(struct ub_scsi_cmd_trace));
474 t->tag = cmd->tag;
475 t->op = cmd->cdb[0];
476 t->dir = cmd->dir;
477 t->req_size = cmd->len;
478 t->st_hst[0] = cmd->state;
479
480 sc->tr.cur = n;
481 cmd->trace_index = n;
482}
483
484static void ub_cmdtr_state(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
485{
486 int n;
487 struct ub_scsi_cmd_trace *t;
488
489 t = &sc->tr.vec[cmd->trace_index];
490 if (t->tag == cmd->tag) {
491 if ((n = t->hcur + 1) == SCMD_ST_HIST_SZ) n = 0;
492 t->st_hst[n] = cmd->state;
493 t->hcur = n;
494 }
495}
496
497static void ub_cmdtr_act_len(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
498{
499 struct ub_scsi_cmd_trace *t;
500
501 t = &sc->tr.vec[cmd->trace_index];
502 if (t->tag == cmd->tag)
503 t->act_size = cmd->act_len;
504}
505
506static void ub_cmdtr_sense(struct ub_dev *sc, struct ub_scsi_cmd *cmd,
507 unsigned char *sense)
508{
509 struct ub_scsi_cmd_trace *t;
510
511 t = &sc->tr.vec[cmd->trace_index];
512 if (t->tag == cmd->tag) {
513 t->key = sense[2] & 0x0F;
514 t->asc = sense[12];
515 t->ascq = sense[13];
516 }
517}
518
Pete Zaitcev07d4fd22005-07-30 22:51:45 -0700519static ssize_t ub_diag_show(struct device *dev, struct device_attribute *attr,
520 char *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521{
522 struct usb_interface *intf;
523 struct ub_dev *sc;
Pete Zaitcevf4800072005-05-01 16:05:40 -0700524 struct list_head *p;
525 struct ub_lun *lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 int cnt;
527 unsigned long flags;
528 int nc, nh;
529 int i, j;
530 struct ub_scsi_cmd_trace *t;
531
532 intf = to_usb_interface(dev);
533 sc = usb_get_intfdata(intf);
534 if (sc == NULL)
535 return 0;
536
537 cnt = 0;
Pete Zaitcev65b4fe52005-12-28 14:22:17 -0800538 spin_lock_irqsave(sc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
540 cnt += sprintf(page + cnt,
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800541 "poison %d reset %d\n",
542 atomic_read(&sc->poison), sc->reset);
543 cnt += sprintf(page + cnt,
Pete Zaitcevf4800072005-05-01 16:05:40 -0700544 "qlen %d qmax %d\n",
545 sc->cmd_queue.qlen, sc->cmd_queue.qmax);
Pete Zaitcevba6abf12005-07-30 22:38:30 -0700546 cnt += sprintf(page + cnt,
Pete Zaitcev64bd8452005-09-22 00:48:29 -0700547 "sg %d %d %d %d %d .. %d\n",
Pete Zaitcevba6abf12005-07-30 22:38:30 -0700548 sc->sg_stat[0],
549 sc->sg_stat[1],
550 sc->sg_stat[2],
551 sc->sg_stat[3],
Pete Zaitcev64bd8452005-09-22 00:48:29 -0700552 sc->sg_stat[4],
553 sc->sg_stat[5]);
Pete Zaitcevf4800072005-05-01 16:05:40 -0700554
555 list_for_each (p, &sc->luns) {
556 lun = list_entry(p, struct ub_lun, link);
557 cnt += sprintf(page + cnt,
558 "lun %u changed %d removable %d readonly %d\n",
559 lun->num, lun->changed, lun->removable, lun->readonly);
560 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
562 if ((nc = sc->tr.cur + 1) == SCMD_TRACE_SZ) nc = 0;
563 for (j = 0; j < SCMD_TRACE_SZ; j++) {
564 t = &sc->tr.vec[nc];
565
566 cnt += sprintf(page + cnt, "%08x %02x", t->tag, t->op);
567 if (t->op == REQUEST_SENSE) {
568 cnt += sprintf(page + cnt, " [sense %x %02x %02x]",
569 t->key, t->asc, t->ascq);
570 } else {
571 cnt += sprintf(page + cnt, " %c", UB_DIR_CHAR(t->dir));
572 cnt += sprintf(page + cnt, " [%5d %5d]",
573 t->req_size, t->act_size);
574 }
575 if ((nh = t->hcur + 1) == SCMD_ST_HIST_SZ) nh = 0;
576 for (i = 0; i < SCMD_ST_HIST_SZ; i++) {
577 cnt += sprintf(page + cnt, " %s",
578 ub_scsi_cmd_stname[(int)t->st_hst[nh]]);
579 if (++nh == SCMD_ST_HIST_SZ) nh = 0;
580 }
581 cnt += sprintf(page + cnt, "\n");
582
583 if (++nc == SCMD_TRACE_SZ) nc = 0;
584 }
585
Pete Zaitcev65b4fe52005-12-28 14:22:17 -0800586 spin_unlock_irqrestore(sc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 return cnt;
588}
589
590static DEVICE_ATTR(diag, S_IRUGO, ub_diag_show, NULL); /* N.B. World readable */
591
592/*
593 * The id allocator.
594 *
595 * This also stores the host for indexing by minor, which is somewhat dirty.
596 */
597static int ub_id_get(void)
598{
599 unsigned long flags;
600 int i;
601
602 spin_lock_irqsave(&ub_lock, flags);
603 for (i = 0; i < UB_MAX_HOSTS; i++) {
604 if (ub_hostv[i] == 0) {
605 ub_hostv[i] = 1;
606 spin_unlock_irqrestore(&ub_lock, flags);
607 return i;
608 }
609 }
610 spin_unlock_irqrestore(&ub_lock, flags);
611 return -1;
612}
613
614static void ub_id_put(int id)
615{
616 unsigned long flags;
617
618 if (id < 0 || id >= UB_MAX_HOSTS) {
619 printk(KERN_ERR DRV_NAME ": bad host ID %d\n", id);
620 return;
621 }
622
623 spin_lock_irqsave(&ub_lock, flags);
624 if (ub_hostv[id] == 0) {
625 spin_unlock_irqrestore(&ub_lock, flags);
626 printk(KERN_ERR DRV_NAME ": freeing free host ID %d\n", id);
627 return;
628 }
629 ub_hostv[id] = 0;
630 spin_unlock_irqrestore(&ub_lock, flags);
631}
632
633/*
Pete Zaitcev65b4fe52005-12-28 14:22:17 -0800634 * This is necessitated by the fact that blk_cleanup_queue does not
635 * necesserily destroy the queue. Instead, it may merely decrease q->refcnt.
636 * Since our blk_init_queue() passes a spinlock common with ub_dev,
637 * we have life time issues when ub_cleanup frees ub_dev.
638 */
639static spinlock_t *ub_next_lock(void)
640{
641 unsigned long flags;
642 spinlock_t *ret;
643
644 spin_lock_irqsave(&ub_lock, flags);
645 ret = &ub_qlockv[ub_qlock_next];
646 ub_qlock_next = (ub_qlock_next + 1) % UB_QLOCK_NUM;
647 spin_unlock_irqrestore(&ub_lock, flags);
648 return ret;
649}
650
651/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 * Downcount for deallocation. This rides on two assumptions:
653 * - once something is poisoned, its refcount cannot grow
654 * - opens cannot happen at this time (del_gendisk was done)
655 * If the above is true, we can drop the lock, which we need for
656 * blk_cleanup_queue(): the silly thing may attempt to sleep.
657 * [Actually, it never needs to sleep for us, but it calls might_sleep()]
658 */
659static void ub_put(struct ub_dev *sc)
660{
661 unsigned long flags;
662
663 spin_lock_irqsave(&ub_lock, flags);
664 --sc->openc;
665 if (sc->openc == 0 && atomic_read(&sc->poison)) {
666 spin_unlock_irqrestore(&ub_lock, flags);
667 ub_cleanup(sc);
668 } else {
669 spin_unlock_irqrestore(&ub_lock, flags);
670 }
671}
672
673/*
674 * Final cleanup and deallocation.
675 */
676static void ub_cleanup(struct ub_dev *sc)
677{
Pete Zaitcevf4800072005-05-01 16:05:40 -0700678 struct list_head *p;
679 struct ub_lun *lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 request_queue_t *q;
681
Pete Zaitcevf4800072005-05-01 16:05:40 -0700682 while (!list_empty(&sc->luns)) {
683 p = sc->luns.next;
684 lun = list_entry(p, struct ub_lun, link);
685 list_del(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Pete Zaitcevf4800072005-05-01 16:05:40 -0700687 /* I don't think queue can be NULL. But... Stolen from sx8.c */
688 if ((q = lun->disk->queue) != NULL)
689 blk_cleanup_queue(q);
690 /*
691 * If we zero disk->private_data BEFORE put_disk, we have
692 * to check for NULL all over the place in open, release,
693 * check_media and revalidate, because the block level
694 * semaphore is well inside the put_disk.
695 * But we cannot zero after the call, because *disk is gone.
696 * The sd.c is blatantly racy in this area.
697 */
698 /* disk->private_data = NULL; */
699 put_disk(lun->disk);
700 lun->disk = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Pete Zaitcevf4800072005-05-01 16:05:40 -0700702 ub_id_put(lun->id);
703 kfree(lun);
704 }
705
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 kfree(sc);
707}
708
709/*
710 * The "command allocator".
711 */
Pete Zaitcevf4800072005-05-01 16:05:40 -0700712static struct ub_scsi_cmd *ub_get_cmd(struct ub_lun *lun)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713{
714 struct ub_scsi_cmd *ret;
715
Pete Zaitcevf4800072005-05-01 16:05:40 -0700716 if (lun->cmda[0])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 return NULL;
Pete Zaitcevf4800072005-05-01 16:05:40 -0700718 ret = &lun->cmdv[0];
719 lun->cmda[0] = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 return ret;
721}
722
Pete Zaitcevf4800072005-05-01 16:05:40 -0700723static void ub_put_cmd(struct ub_lun *lun, struct ub_scsi_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724{
Pete Zaitcevf4800072005-05-01 16:05:40 -0700725 if (cmd != &lun->cmdv[0]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 printk(KERN_WARNING "%s: releasing a foreign cmd %p\n",
Pete Zaitcevf4800072005-05-01 16:05:40 -0700727 lun->name, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 return;
729 }
Pete Zaitcevf4800072005-05-01 16:05:40 -0700730 if (!lun->cmda[0]) {
731 printk(KERN_WARNING "%s: releasing a free cmd\n", lun->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 return;
733 }
Pete Zaitcevf4800072005-05-01 16:05:40 -0700734 lun->cmda[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735}
736
737/*
738 * The command queue.
739 */
740static void ub_cmdq_add(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
741{
742 struct ub_scsi_cmd_queue *t = &sc->cmd_queue;
743
744 if (t->qlen++ == 0) {
745 t->head = cmd;
746 t->tail = cmd;
747 } else {
748 t->tail->next = cmd;
749 t->tail = cmd;
750 }
751
752 if (t->qlen > t->qmax)
753 t->qmax = t->qlen;
754}
755
756static void ub_cmdq_insert(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
757{
758 struct ub_scsi_cmd_queue *t = &sc->cmd_queue;
759
760 if (t->qlen++ == 0) {
761 t->head = cmd;
762 t->tail = cmd;
763 } else {
764 cmd->next = t->head;
765 t->head = cmd;
766 }
767
768 if (t->qlen > t->qmax)
769 t->qmax = t->qlen;
770}
771
772static struct ub_scsi_cmd *ub_cmdq_pop(struct ub_dev *sc)
773{
774 struct ub_scsi_cmd_queue *t = &sc->cmd_queue;
775 struct ub_scsi_cmd *cmd;
776
777 if (t->qlen == 0)
778 return NULL;
779 if (--t->qlen == 0)
780 t->tail = NULL;
781 cmd = t->head;
782 t->head = cmd->next;
783 cmd->next = NULL;
784 return cmd;
785}
786
787#define ub_cmdq_peek(sc) ((sc)->cmd_queue.head)
788
789/*
790 * The request function is our main entry point
791 */
792
Pete Zaitcev6c1eb8c2005-07-30 22:51:52 -0700793static void ub_request_fn(request_queue_t *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794{
Pete Zaitcevf4800072005-05-01 16:05:40 -0700795 struct ub_lun *lun = q->queuedata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 struct request *rq;
797
798 while ((rq = elv_next_request(q)) != NULL) {
Pete Zaitcev6c1eb8c2005-07-30 22:51:52 -0700799 if (ub_request_fn_1(lun, rq) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 blk_stop_queue(q);
801 break;
802 }
803 }
804}
805
Pete Zaitcev6c1eb8c2005-07-30 22:51:52 -0700806static int ub_request_fn_1(struct ub_lun *lun, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
Pete Zaitcevf4800072005-05-01 16:05:40 -0700808 struct ub_dev *sc = lun->udev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 struct ub_scsi_cmd *cmd;
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800810 struct ub_request *urq;
811 int n_elem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Pete Zaitcevf4800072005-05-01 16:05:40 -0700813 if (atomic_read(&sc->poison) || lun->changed) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 blkdev_dequeue_request(rq);
815 ub_end_rq(rq, 0);
816 return 0;
817 }
818
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800819 if (lun->urq.rq != NULL)
820 return -1;
Pete Zaitcevf4800072005-05-01 16:05:40 -0700821 if ((cmd = ub_get_cmd(lun)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 return -1;
823 memset(cmd, 0, sizeof(struct ub_scsi_cmd));
824
825 blkdev_dequeue_request(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800827 urq = &lun->urq;
828 memset(urq, 0, sizeof(struct ub_request));
829 urq->rq = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
831 /*
832 * get scatterlist from block layer
833 */
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800834 n_elem = blk_rq_map_sg(lun->disk->queue, rq, &urq->sgv[0]);
835 if (n_elem < 0) {
Pete Zaitcevba6abf12005-07-30 22:38:30 -0700836 printk(KERN_INFO "%s: failed request map (%d)\n",
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800837 lun->name, n_elem); /* P3 */
838 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 }
Pete Zaitcevba6abf12005-07-30 22:38:30 -0700840 if (n_elem > UB_MAX_REQ_SG) { /* Paranoia */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 printk(KERN_WARNING "%s: request with %d segments\n",
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800842 lun->name, n_elem);
843 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 }
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800845 urq->nsg = n_elem;
Pete Zaitcev64bd8452005-09-22 00:48:29 -0700846 sc->sg_stat[n_elem < 5 ? n_elem : 5]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800848 if (blk_pc_request(rq)) {
849 ub_cmd_build_packet(sc, lun, cmd, urq);
850 } else {
851 ub_cmd_build_block(sc, lun, cmd, urq);
852 }
853 cmd->state = UB_CMDST_INIT;
854 cmd->lun = lun;
855 cmd->done = ub_rw_cmd_done;
856 cmd->back = urq;
857
858 cmd->tag = sc->tagcnt++;
859 if (ub_submit_scsi(sc, cmd) != 0)
860 goto drop;
861
862 return 0;
863
864drop:
865 ub_put_cmd(lun, cmd);
866 ub_end_rq(rq, 0);
867 return 0;
868}
869
870static void ub_cmd_build_block(struct ub_dev *sc, struct ub_lun *lun,
871 struct ub_scsi_cmd *cmd, struct ub_request *urq)
872{
873 struct request *rq = urq->rq;
874 unsigned int block, nblks;
875
876 if (rq_data_dir(rq) == WRITE)
877 cmd->dir = UB_DIR_WRITE;
878 else
879 cmd->dir = UB_DIR_READ;
880
881 cmd->nsg = urq->nsg;
882 memcpy(cmd->sgv, urq->sgv, sizeof(struct scatterlist) * cmd->nsg);
883
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 /*
885 * build the command
886 *
887 * The call to blk_queue_hardsect_size() guarantees that request
888 * is aligned, but it is given in terms of 512 byte units, always.
889 */
Pete Zaitceva1cf96e2005-08-14 21:16:03 -0700890 block = rq->sector >> lun->capacity.bshift;
891 nblks = rq->nr_sectors >> lun->capacity.bshift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800893 cmd->cdb[0] = (cmd->dir == UB_DIR_READ)? READ_10: WRITE_10;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 /* 10-byte uses 4 bytes of LBA: 2147483648KB, 2097152MB, 2048GB */
895 cmd->cdb[2] = block >> 24;
896 cmd->cdb[3] = block >> 16;
897 cmd->cdb[4] = block >> 8;
898 cmd->cdb[5] = block;
899 cmd->cdb[7] = nblks >> 8;
900 cmd->cdb[8] = nblks;
901 cmd->cdb_len = 10;
902
Pete Zaitceva1cf96e2005-08-14 21:16:03 -0700903 cmd->len = rq->nr_sectors * 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904}
905
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800906static void ub_cmd_build_packet(struct ub_dev *sc, struct ub_lun *lun,
907 struct ub_scsi_cmd *cmd, struct ub_request *urq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908{
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800909 struct request *rq = urq->rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
911 if (rq->data_len == 0) {
912 cmd->dir = UB_DIR_NONE;
913 } else {
914 if (rq_data_dir(rq) == WRITE)
915 cmd->dir = UB_DIR_WRITE;
916 else
917 cmd->dir = UB_DIR_READ;
918 }
Pete Zaitceva1cf96e2005-08-14 21:16:03 -0700919
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800920 cmd->nsg = urq->nsg;
921 memcpy(cmd->sgv, urq->sgv, sizeof(struct scatterlist) * cmd->nsg);
Pete Zaitceva1cf96e2005-08-14 21:16:03 -0700922
923 memcpy(&cmd->cdb, rq->cmd, rq->cmd_len);
924 cmd->cdb_len = rq->cmd_len;
925
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 cmd->len = rq->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927}
928
929static void ub_rw_cmd_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
930{
Pete Zaitcevf4800072005-05-01 16:05:40 -0700931 struct ub_lun *lun = cmd->lun;
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800932 struct ub_request *urq = cmd->back;
933 struct request *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 int uptodate;
935
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800936 rq = urq->rq;
937
Pete Zaitceva1cf96e2005-08-14 21:16:03 -0700938 if (cmd->error == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 uptodate = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Pete Zaitceva1cf96e2005-08-14 21:16:03 -0700941 if (blk_pc_request(rq)) {
942 if (cmd->act_len >= rq->data_len)
943 rq->data_len = 0;
944 else
945 rq->data_len -= cmd->act_len;
Pete Zaitcevba6abf12005-07-30 22:38:30 -0700946 }
Pete Zaitceva1cf96e2005-08-14 21:16:03 -0700947 } else {
Pete Zaitcevba6abf12005-07-30 22:38:30 -0700948 uptodate = 0;
Pete Zaitcevba6abf12005-07-30 22:38:30 -0700949
Pete Zaitceva1cf96e2005-08-14 21:16:03 -0700950 if (blk_pc_request(rq)) {
951 /* UB_SENSE_SIZE is smaller than SCSI_SENSE_BUFFERSIZE */
952 memcpy(rq->sense, sc->top_sense, UB_SENSE_SIZE);
953 rq->sense_len = UB_SENSE_SIZE;
954 if (sc->top_sense[0] != 0)
955 rq->errors = SAM_STAT_CHECK_CONDITION;
956 else
957 rq->errors = DID_ERROR << 16;
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800958 } else {
959 if (cmd->error == -EIO) {
960 if (ub_rw_cmd_retry(sc, lun, urq, cmd) == 0)
961 return;
962 }
Pete Zaitceva1cf96e2005-08-14 21:16:03 -0700963 }
964 }
Pete Zaitcevba6abf12005-07-30 22:38:30 -0700965
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800966 urq->rq = NULL;
967
Pete Zaitcevf4800072005-05-01 16:05:40 -0700968 ub_put_cmd(lun, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 ub_end_rq(rq, uptodate);
Pete Zaitcevba6abf12005-07-30 22:38:30 -0700970 blk_start_queue(lun->disk->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971}
972
973static void ub_end_rq(struct request *rq, int uptodate)
974{
Daniel Marjamakiab930912005-12-17 02:38:46 -0800975 end_that_request_first(rq, uptodate, rq->hard_nr_sectors);
Tejun Heo8ffdc652006-01-06 09:49:03 +0100976 end_that_request_last(rq, uptodate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977}
978
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800979static int ub_rw_cmd_retry(struct ub_dev *sc, struct ub_lun *lun,
980 struct ub_request *urq, struct ub_scsi_cmd *cmd)
981{
982
983 if (atomic_read(&sc->poison))
984 return -ENXIO;
985
986 ub_reset_enter(sc);
987
988 if (urq->current_try >= 3)
989 return -EIO;
990 urq->current_try++;
991 /* P3 */ printk("%s: dir %c len/act %d/%d "
992 "[sense %x %02x %02x] retry %d\n",
993 sc->name, UB_DIR_CHAR(cmd->dir), cmd->len, cmd->act_len,
994 cmd->key, cmd->asc, cmd->ascq, urq->current_try);
995
996 memset(cmd, 0, sizeof(struct ub_scsi_cmd));
997 ub_cmd_build_block(sc, lun, cmd, urq);
998
999 cmd->state = UB_CMDST_INIT;
1000 cmd->lun = lun;
1001 cmd->done = ub_rw_cmd_done;
1002 cmd->back = urq;
1003
1004 cmd->tag = sc->tagcnt++;
1005
1006#if 0 /* Wasteful */
1007 return ub_submit_scsi(sc, cmd);
1008#else
1009 ub_cmdq_add(sc, cmd);
1010 return 0;
1011#endif
1012}
1013
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014/*
1015 * Submit a regular SCSI operation (not an auto-sense).
1016 *
1017 * The Iron Law of Good Submit Routine is:
1018 * Zero return - callback is done, Nonzero return - callback is not done.
1019 * No exceptions.
1020 *
1021 * Host is assumed locked.
1022 *
1023 * XXX We only support Bulk for the moment.
1024 */
1025static int ub_submit_scsi(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1026{
1027
1028 if (cmd->state != UB_CMDST_INIT ||
1029 (cmd->dir != UB_DIR_NONE && cmd->len == 0)) {
1030 return -EINVAL;
1031 }
1032
1033 ub_cmdq_add(sc, cmd);
1034 /*
1035 * We can call ub_scsi_dispatch(sc) right away here, but it's a little
1036 * safer to jump to a tasklet, in case upper layers do something silly.
1037 */
1038 tasklet_schedule(&sc->tasklet);
1039 return 0;
1040}
1041
1042/*
1043 * Submit the first URB for the queued command.
1044 * This function does not deal with queueing in any way.
1045 */
1046static int ub_scsi_cmd_start(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1047{
1048 struct bulk_cb_wrap *bcb;
1049 int rc;
1050
1051 bcb = &sc->work_bcb;
1052
1053 /*
1054 * ``If the allocation length is eighteen or greater, and a device
1055 * server returns less than eithteen bytes of data, the application
1056 * client should assume that the bytes not transferred would have been
1057 * zeroes had the device server returned those bytes.''
1058 *
1059 * We zero sense for all commands so that when a packet request
1060 * fails it does not return a stale sense.
1061 */
1062 memset(&sc->top_sense, 0, UB_SENSE_SIZE);
1063
1064 /* set up the command wrapper */
1065 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
1066 bcb->Tag = cmd->tag; /* Endianness is not important */
1067 bcb->DataTransferLength = cpu_to_le32(cmd->len);
1068 bcb->Flags = (cmd->dir == UB_DIR_READ) ? 0x80 : 0;
Pete Zaitcevf4800072005-05-01 16:05:40 -07001069 bcb->Lun = (cmd->lun != NULL) ? cmd->lun->num : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 bcb->Length = cmd->cdb_len;
1071
1072 /* copy the command payload */
1073 memcpy(bcb->CDB, cmd->cdb, UB_MAX_CDB_SIZE);
1074
1075 UB_INIT_COMPLETION(sc->work_done);
1076
1077 sc->last_pipe = sc->send_bulk_pipe;
1078 usb_fill_bulk_urb(&sc->work_urb, sc->dev, sc->send_bulk_pipe,
1079 bcb, US_BULK_CB_WRAP_LEN, ub_urb_complete, sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
1081 /* Fill what we shouldn't be filling, because usb-storage did so. */
1082 sc->work_urb.actual_length = 0;
1083 sc->work_urb.error_count = 0;
1084 sc->work_urb.status = 0;
1085
1086 if ((rc = usb_submit_urb(&sc->work_urb, GFP_ATOMIC)) != 0) {
1087 /* XXX Clear stalls */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 ub_complete(&sc->work_done);
1089 return rc;
1090 }
1091
1092 sc->work_timer.expires = jiffies + UB_URB_TIMEOUT;
1093 add_timer(&sc->work_timer);
1094
1095 cmd->state = UB_CMDST_CMD;
1096 ub_cmdtr_state(sc, cmd);
1097 return 0;
1098}
1099
1100/*
1101 * Timeout handler.
1102 */
1103static void ub_urb_timeout(unsigned long arg)
1104{
1105 struct ub_dev *sc = (struct ub_dev *) arg;
1106 unsigned long flags;
1107
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08001108 spin_lock_irqsave(sc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 usb_unlink_urb(&sc->work_urb);
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08001110 spin_unlock_irqrestore(sc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111}
1112
1113/*
1114 * Completion routine for the work URB.
1115 *
1116 * This can be called directly from usb_submit_urb (while we have
1117 * the sc->lock taken) and from an interrupt (while we do NOT have
1118 * the sc->lock taken). Therefore, bounce this off to a tasklet.
1119 */
1120static void ub_urb_complete(struct urb *urb, struct pt_regs *pt)
1121{
1122 struct ub_dev *sc = urb->context;
1123
1124 ub_complete(&sc->work_done);
1125 tasklet_schedule(&sc->tasklet);
1126}
1127
1128static void ub_scsi_action(unsigned long _dev)
1129{
1130 struct ub_dev *sc = (struct ub_dev *) _dev;
1131 unsigned long flags;
1132
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08001133 spin_lock_irqsave(sc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 del_timer(&sc->work_timer);
1135 ub_scsi_dispatch(sc);
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08001136 spin_unlock_irqrestore(sc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137}
1138
1139static void ub_scsi_dispatch(struct ub_dev *sc)
1140{
1141 struct ub_scsi_cmd *cmd;
1142 int rc;
1143
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001144 while (!sc->reset && (cmd = ub_cmdq_peek(sc)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 if (cmd->state == UB_CMDST_DONE) {
1146 ub_cmdq_pop(sc);
1147 (*cmd->done)(sc, cmd);
1148 } else if (cmd->state == UB_CMDST_INIT) {
1149 ub_cmdtr_new(sc, cmd);
1150 if ((rc = ub_scsi_cmd_start(sc, cmd)) == 0)
1151 break;
1152 cmd->error = rc;
1153 cmd->state = UB_CMDST_DONE;
1154 ub_cmdtr_state(sc, cmd);
1155 } else {
1156 if (!ub_is_completed(&sc->work_done))
1157 break;
1158 ub_scsi_urb_compl(sc, cmd);
1159 }
1160 }
1161}
1162
1163static void ub_scsi_urb_compl(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1164{
1165 struct urb *urb = &sc->work_urb;
1166 struct bulk_cs_wrap *bcs;
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001167 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 int rc;
1169
1170 if (atomic_read(&sc->poison)) {
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001171 ub_state_done(sc, cmd, -ENODEV);
1172 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 }
1174
1175 if (cmd->state == UB_CMDST_CLEAR) {
1176 if (urb->status == -EPIPE) {
1177 /*
1178 * STALL while clearning STALL.
1179 * The control pipe clears itself - nothing to do.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 */
Pete Zaitcevf4800072005-05-01 16:05:40 -07001181 printk(KERN_NOTICE "%s: stall on control pipe\n",
1182 sc->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 goto Bad_End;
1184 }
1185
1186 /*
1187 * We ignore the result for the halt clear.
1188 */
1189
1190 /* reset the endpoint toggle */
1191 usb_settoggle(sc->dev, usb_pipeendpoint(sc->last_pipe),
1192 usb_pipeout(sc->last_pipe), 0);
1193
1194 ub_state_sense(sc, cmd);
1195
1196 } else if (cmd->state == UB_CMDST_CLR2STS) {
1197 if (urb->status == -EPIPE) {
Pete Zaitcevf4800072005-05-01 16:05:40 -07001198 printk(KERN_NOTICE "%s: stall on control pipe\n",
1199 sc->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 goto Bad_End;
1201 }
1202
1203 /*
1204 * We ignore the result for the halt clear.
1205 */
1206
1207 /* reset the endpoint toggle */
1208 usb_settoggle(sc->dev, usb_pipeendpoint(sc->last_pipe),
1209 usb_pipeout(sc->last_pipe), 0);
1210
1211 ub_state_stat(sc, cmd);
1212
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001213 } else if (cmd->state == UB_CMDST_CLRRS) {
1214 if (urb->status == -EPIPE) {
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001215 printk(KERN_NOTICE "%s: stall on control pipe\n",
1216 sc->name);
1217 goto Bad_End;
1218 }
1219
1220 /*
1221 * We ignore the result for the halt clear.
1222 */
1223
1224 /* reset the endpoint toggle */
1225 usb_settoggle(sc->dev, usb_pipeendpoint(sc->last_pipe),
1226 usb_pipeout(sc->last_pipe), 0);
1227
1228 ub_state_stat_counted(sc, cmd);
1229
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 } else if (cmd->state == UB_CMDST_CMD) {
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001231 switch (urb->status) {
1232 case 0:
1233 break;
1234 case -EOVERFLOW:
1235 goto Bad_End;
1236 case -EPIPE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 rc = ub_submit_clear_stall(sc, cmd, sc->last_pipe);
1238 if (rc != 0) {
1239 printk(KERN_NOTICE "%s: "
Pete Zaitcevf4800072005-05-01 16:05:40 -07001240 "unable to submit clear (%d)\n",
1241 sc->name, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 /*
1243 * This is typically ENOMEM or some other such shit.
1244 * Retrying is pointless. Just do Bad End on it...
1245 */
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001246 ub_state_done(sc, cmd, rc);
1247 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 }
1249 cmd->state = UB_CMDST_CLEAR;
1250 ub_cmdtr_state(sc, cmd);
1251 return;
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001252 case -ESHUTDOWN: /* unplug */
1253 case -EILSEQ: /* unplug timeout on uhci */
1254 ub_state_done(sc, cmd, -ENODEV);
1255 return;
1256 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 goto Bad_End;
1258 }
1259 if (urb->actual_length != US_BULK_CB_WRAP_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 goto Bad_End;
1261 }
1262
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001263 if (cmd->dir == UB_DIR_NONE || cmd->nsg < 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 ub_state_stat(sc, cmd);
1265 return;
1266 }
1267
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001268 // udelay(125); // usb-storage has this
1269 ub_data_start(sc, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
1271 } else if (cmd->state == UB_CMDST_DATA) {
1272 if (urb->status == -EPIPE) {
1273 rc = ub_submit_clear_stall(sc, cmd, sc->last_pipe);
1274 if (rc != 0) {
1275 printk(KERN_NOTICE "%s: "
Pete Zaitcevf4800072005-05-01 16:05:40 -07001276 "unable to submit clear (%d)\n",
1277 sc->name, rc);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001278 ub_state_done(sc, cmd, rc);
1279 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 }
1281 cmd->state = UB_CMDST_CLR2STS;
1282 ub_cmdtr_state(sc, cmd);
1283 return;
1284 }
1285 if (urb->status == -EOVERFLOW) {
1286 /*
1287 * A babble? Failure, but we must transfer CSW now.
1288 */
1289 cmd->error = -EOVERFLOW; /* A cheap trick... */
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001290 ub_state_stat(sc, cmd);
1291 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 }
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001293
1294 if (cmd->dir == UB_DIR_WRITE) {
1295 /*
1296 * Do not continue writes in case of a failure.
1297 * Doing so would cause sectors to be mixed up,
1298 * which is worse than sectors lost.
1299 *
1300 * We must try to read the CSW, or many devices
1301 * get confused.
1302 */
1303 len = urb->actual_length;
1304 if (urb->status != 0 ||
1305 len != cmd->sgv[cmd->current_sg].length) {
1306 cmd->act_len += len;
1307 ub_cmdtr_act_len(sc, cmd);
1308
1309 cmd->error = -EIO;
1310 ub_state_stat(sc, cmd);
1311 return;
1312 }
1313
1314 } else {
1315 /*
1316 * If an error occurs on read, we record it, and
1317 * continue to fetch data in order to avoid bubble.
1318 *
1319 * As a small shortcut, we stop if we detect that
1320 * a CSW mixed into data.
1321 */
1322 if (urb->status != 0)
1323 cmd->error = -EIO;
1324
1325 len = urb->actual_length;
1326 if (urb->status != 0 ||
1327 len != cmd->sgv[cmd->current_sg].length) {
1328 if ((len & 0x1FF) == US_BULK_CS_WRAP_LEN)
1329 goto Bad_End;
1330 }
1331 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001333 cmd->act_len += urb->actual_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 ub_cmdtr_act_len(sc, cmd);
1335
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001336 if (++cmd->current_sg < cmd->nsg) {
1337 ub_data_start(sc, cmd);
1338 return;
1339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 ub_state_stat(sc, cmd);
1341
1342 } else if (cmd->state == UB_CMDST_STAT) {
1343 if (urb->status == -EPIPE) {
1344 rc = ub_submit_clear_stall(sc, cmd, sc->last_pipe);
1345 if (rc != 0) {
1346 printk(KERN_NOTICE "%s: "
Pete Zaitcevf4800072005-05-01 16:05:40 -07001347 "unable to submit clear (%d)\n",
1348 sc->name, rc);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001349 ub_state_done(sc, cmd, rc);
1350 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 }
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001352
1353 /*
1354 * Having a stall when getting CSW is an error, so
1355 * make sure uppper levels are not oblivious to it.
1356 */
1357 cmd->error = -EIO; /* A cheap trick... */
1358
1359 cmd->state = UB_CMDST_CLRRS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 ub_cmdtr_state(sc, cmd);
1361 return;
1362 }
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001363
1364 /* Catch everything, including -EOVERFLOW and other nasties. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 if (urb->status != 0)
1366 goto Bad_End;
1367
1368 if (urb->actual_length == 0) {
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001369 ub_state_stat_counted(sc, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 return;
1371 }
1372
1373 /*
1374 * Check the returned Bulk protocol status.
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001375 * The status block has to be validated first.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 */
1377
1378 bcs = &sc->work_bcs;
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001379
1380 if (sc->signature == cpu_to_le32(0)) {
1381 /*
1382 * This is the first reply, so do not perform the check.
1383 * Instead, remember the signature the device uses
1384 * for future checks. But do not allow a nul.
1385 */
1386 sc->signature = bcs->Signature;
1387 if (sc->signature == cpu_to_le32(0)) {
1388 ub_state_stat_counted(sc, cmd);
1389 return;
1390 }
1391 } else {
1392 if (bcs->Signature != sc->signature) {
1393 ub_state_stat_counted(sc, cmd);
1394 return;
1395 }
1396 }
1397
1398 if (bcs->Tag != cmd->tag) {
1399 /*
1400 * This usually happens when we disagree with the
1401 * device's microcode about something. For instance,
1402 * a few of them throw this after timeouts. They buffer
1403 * commands and reply at commands we timed out before.
1404 * Without flushing these replies we loop forever.
1405 */
1406 ub_state_stat_counted(sc, cmd);
1407 return;
1408 }
1409
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001410 len = le32_to_cpu(bcs->Residue);
1411 if (len != cmd->len - cmd->act_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 /*
1413 * It is all right to transfer less, the caller has
1414 * to check. But it's not all right if the device
1415 * counts disagree with our counts.
1416 */
1417 /* P3 */ printk("%s: resid %d len %d act %d\n",
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001418 sc->name, len, cmd->len, cmd->act_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 goto Bad_End;
1420 }
1421
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 switch (bcs->Status) {
1423 case US_BULK_STAT_OK:
1424 break;
1425 case US_BULK_STAT_FAIL:
1426 ub_state_sense(sc, cmd);
1427 return;
1428 case US_BULK_STAT_PHASE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 /* P3 */ printk("%s: status PHASE\n", sc->name);
1430 goto Bad_End;
1431 default:
1432 printk(KERN_INFO "%s: unknown CSW status 0x%x\n",
1433 sc->name, bcs->Status);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001434 ub_state_done(sc, cmd, -EINVAL);
1435 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 }
1437
1438 /* Not zeroing error to preserve a babble indicator */
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001439 if (cmd->error != 0) {
1440 ub_state_sense(sc, cmd);
1441 return;
1442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 cmd->state = UB_CMDST_DONE;
1444 ub_cmdtr_state(sc, cmd);
1445 ub_cmdq_pop(sc);
1446 (*cmd->done)(sc, cmd);
1447
1448 } else if (cmd->state == UB_CMDST_SENSE) {
1449 ub_state_done(sc, cmd, -EIO);
1450
1451 } else {
1452 printk(KERN_WARNING "%s: "
Pete Zaitcevf4800072005-05-01 16:05:40 -07001453 "wrong command state %d\n",
1454 sc->name, cmd->state);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001455 ub_state_done(sc, cmd, -EINVAL);
1456 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 }
1458 return;
1459
1460Bad_End: /* Little Excel is dead */
1461 ub_state_done(sc, cmd, -EIO);
1462}
1463
1464/*
1465 * Factorization helper for the command state machine:
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001466 * Initiate a data segment transfer.
1467 */
1468static void ub_data_start(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1469{
1470 struct scatterlist *sg = &cmd->sgv[cmd->current_sg];
1471 int pipe;
1472 int rc;
1473
1474 UB_INIT_COMPLETION(sc->work_done);
1475
1476 if (cmd->dir == UB_DIR_READ)
1477 pipe = sc->recv_bulk_pipe;
1478 else
1479 pipe = sc->send_bulk_pipe;
1480 sc->last_pipe = pipe;
1481 usb_fill_bulk_urb(&sc->work_urb, sc->dev, pipe,
1482 page_address(sg->page) + sg->offset, sg->length,
1483 ub_urb_complete, sc);
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001484 sc->work_urb.actual_length = 0;
1485 sc->work_urb.error_count = 0;
1486 sc->work_urb.status = 0;
1487
1488 if ((rc = usb_submit_urb(&sc->work_urb, GFP_ATOMIC)) != 0) {
1489 /* XXX Clear stalls */
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001490 ub_complete(&sc->work_done);
1491 ub_state_done(sc, cmd, rc);
1492 return;
1493 }
1494
1495 sc->work_timer.expires = jiffies + UB_DATA_TIMEOUT;
1496 add_timer(&sc->work_timer);
1497
1498 cmd->state = UB_CMDST_DATA;
1499 ub_cmdtr_state(sc, cmd);
1500}
1501
1502/*
1503 * Factorization helper for the command state machine:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 * Finish the command.
1505 */
1506static void ub_state_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd, int rc)
1507{
1508
1509 cmd->error = rc;
1510 cmd->state = UB_CMDST_DONE;
1511 ub_cmdtr_state(sc, cmd);
1512 ub_cmdq_pop(sc);
1513 (*cmd->done)(sc, cmd);
1514}
1515
1516/*
1517 * Factorization helper for the command state machine:
1518 * Submit a CSW read.
1519 */
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001520static int __ub_state_stat(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521{
1522 int rc;
1523
1524 UB_INIT_COMPLETION(sc->work_done);
1525
1526 sc->last_pipe = sc->recv_bulk_pipe;
1527 usb_fill_bulk_urb(&sc->work_urb, sc->dev, sc->recv_bulk_pipe,
1528 &sc->work_bcs, US_BULK_CS_WRAP_LEN, ub_urb_complete, sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 sc->work_urb.actual_length = 0;
1530 sc->work_urb.error_count = 0;
1531 sc->work_urb.status = 0;
1532
1533 if ((rc = usb_submit_urb(&sc->work_urb, GFP_ATOMIC)) != 0) {
1534 /* XXX Clear stalls */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 ub_complete(&sc->work_done);
1536 ub_state_done(sc, cmd, rc);
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001537 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 }
1539
1540 sc->work_timer.expires = jiffies + UB_STAT_TIMEOUT;
1541 add_timer(&sc->work_timer);
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001542 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543}
1544
1545/*
1546 * Factorization helper for the command state machine:
1547 * Submit a CSW read and go to STAT state.
1548 */
1549static void ub_state_stat(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1550{
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001551
1552 if (__ub_state_stat(sc, cmd) != 0)
1553 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
1555 cmd->stat_count = 0;
1556 cmd->state = UB_CMDST_STAT;
1557 ub_cmdtr_state(sc, cmd);
1558}
1559
1560/*
1561 * Factorization helper for the command state machine:
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001562 * Submit a CSW read and go to STAT state with counter (along [C] path).
1563 */
1564static void ub_state_stat_counted(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1565{
1566
1567 if (++cmd->stat_count >= 4) {
1568 ub_state_sense(sc, cmd);
1569 return;
1570 }
1571
1572 if (__ub_state_stat(sc, cmd) != 0)
1573 return;
1574
1575 cmd->state = UB_CMDST_STAT;
1576 ub_cmdtr_state(sc, cmd);
1577}
1578
1579/*
1580 * Factorization helper for the command state machine:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 * Submit a REQUEST SENSE and go to SENSE state.
1582 */
1583static void ub_state_sense(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1584{
1585 struct ub_scsi_cmd *scmd;
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001586 struct scatterlist *sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 int rc;
1588
1589 if (cmd->cdb[0] == REQUEST_SENSE) {
1590 rc = -EPIPE;
1591 goto error;
1592 }
1593
1594 scmd = &sc->top_rqs_cmd;
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001595 memset(scmd, 0, sizeof(struct ub_scsi_cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 scmd->cdb[0] = REQUEST_SENSE;
1597 scmd->cdb[4] = UB_SENSE_SIZE;
1598 scmd->cdb_len = 6;
1599 scmd->dir = UB_DIR_READ;
1600 scmd->state = UB_CMDST_INIT;
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001601 scmd->nsg = 1;
1602 sg = &scmd->sgv[0];
1603 sg->page = virt_to_page(sc->top_sense);
Pete Zaitcev38ffdd62005-09-24 13:10:16 -07001604 sg->offset = (unsigned long)sc->top_sense & (PAGE_SIZE-1);
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001605 sg->length = UB_SENSE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 scmd->len = UB_SENSE_SIZE;
Pete Zaitcevf4800072005-05-01 16:05:40 -07001607 scmd->lun = cmd->lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 scmd->done = ub_top_sense_done;
1609 scmd->back = cmd;
1610
1611 scmd->tag = sc->tagcnt++;
1612
1613 cmd->state = UB_CMDST_SENSE;
1614 ub_cmdtr_state(sc, cmd);
1615
1616 ub_cmdq_insert(sc, scmd);
1617 return;
1618
1619error:
1620 ub_state_done(sc, cmd, rc);
1621}
1622
1623/*
1624 * A helper for the command's state machine:
1625 * Submit a stall clear.
1626 */
1627static int ub_submit_clear_stall(struct ub_dev *sc, struct ub_scsi_cmd *cmd,
1628 int stalled_pipe)
1629{
1630 int endp;
1631 struct usb_ctrlrequest *cr;
1632 int rc;
1633
1634 endp = usb_pipeendpoint(stalled_pipe);
1635 if (usb_pipein (stalled_pipe))
1636 endp |= USB_DIR_IN;
1637
1638 cr = &sc->work_cr;
1639 cr->bRequestType = USB_RECIP_ENDPOINT;
1640 cr->bRequest = USB_REQ_CLEAR_FEATURE;
1641 cr->wValue = cpu_to_le16(USB_ENDPOINT_HALT);
1642 cr->wIndex = cpu_to_le16(endp);
1643 cr->wLength = cpu_to_le16(0);
1644
1645 UB_INIT_COMPLETION(sc->work_done);
1646
1647 usb_fill_control_urb(&sc->work_urb, sc->dev, sc->send_ctrl_pipe,
1648 (unsigned char*) cr, NULL, 0, ub_urb_complete, sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 sc->work_urb.actual_length = 0;
1650 sc->work_urb.error_count = 0;
1651 sc->work_urb.status = 0;
1652
1653 if ((rc = usb_submit_urb(&sc->work_urb, GFP_ATOMIC)) != 0) {
1654 ub_complete(&sc->work_done);
1655 return rc;
1656 }
1657
1658 sc->work_timer.expires = jiffies + UB_CTRL_TIMEOUT;
1659 add_timer(&sc->work_timer);
1660 return 0;
1661}
1662
1663/*
1664 */
1665static void ub_top_sense_done(struct ub_dev *sc, struct ub_scsi_cmd *scmd)
1666{
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001667 unsigned char *sense = sc->top_sense;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 struct ub_scsi_cmd *cmd;
1669
1670 /*
1671 * Ignoring scmd->act_len, because the buffer was pre-zeroed.
1672 */
1673 ub_cmdtr_sense(sc, scmd, sense);
1674
1675 /*
1676 * Find the command which triggered the unit attention or a check,
1677 * save the sense into it, and advance its state machine.
1678 */
1679 if ((cmd = ub_cmdq_peek(sc)) == NULL) {
1680 printk(KERN_WARNING "%s: sense done while idle\n", sc->name);
1681 return;
1682 }
1683 if (cmd != scmd->back) {
1684 printk(KERN_WARNING "%s: "
Pete Zaitcevf4800072005-05-01 16:05:40 -07001685 "sense done for wrong command 0x%x\n",
1686 sc->name, cmd->tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 return;
1688 }
1689 if (cmd->state != UB_CMDST_SENSE) {
1690 printk(KERN_WARNING "%s: "
Pete Zaitcevf4800072005-05-01 16:05:40 -07001691 "sense done with bad cmd state %d\n",
1692 sc->name, cmd->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 return;
1694 }
1695
1696 cmd->key = sense[2] & 0x0F;
1697 cmd->asc = sense[12];
1698 cmd->ascq = sense[13];
1699
1700 ub_scsi_urb_compl(sc, cmd);
1701}
1702
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703/*
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001704 * Reset management
1705 */
1706
1707static void ub_reset_enter(struct ub_dev *sc)
1708{
1709
1710 if (sc->reset) {
1711 /* This happens often on multi-LUN devices. */
1712 return;
1713 }
1714 sc->reset = 1;
1715
1716#if 0 /* Not needed because the disconnect waits for us. */
1717 unsigned long flags;
1718 spin_lock_irqsave(&ub_lock, flags);
1719 sc->openc++;
1720 spin_unlock_irqrestore(&ub_lock, flags);
1721#endif
1722
1723#if 0 /* We let them stop themselves. */
1724 struct list_head *p;
1725 struct ub_lun *lun;
1726 list_for_each(p, &sc->luns) {
1727 lun = list_entry(p, struct ub_lun, link);
1728 blk_stop_queue(lun->disk->queue);
1729 }
1730#endif
1731
1732 schedule_work(&sc->reset_work);
1733}
1734
1735static void ub_reset_task(void *arg)
1736{
1737 struct ub_dev *sc = arg;
1738 unsigned long flags;
1739 struct list_head *p;
1740 struct ub_lun *lun;
1741 int lkr, rc;
1742
1743 if (!sc->reset) {
1744 printk(KERN_WARNING "%s: Running reset unrequested\n",
1745 sc->name);
1746 return;
1747 }
1748
1749 if (atomic_read(&sc->poison)) {
1750 printk(KERN_NOTICE "%s: Not resetting disconnected device\n",
1751 sc->name); /* P3 This floods. Remove soon. XXX */
1752 } else if (sc->dev->actconfig->desc.bNumInterfaces != 1) {
1753 printk(KERN_NOTICE "%s: Not resetting multi-interface device\n",
1754 sc->name); /* P3 This floods. Remove soon. XXX */
1755 } else {
1756 if ((lkr = usb_lock_device_for_reset(sc->dev, sc->intf)) < 0) {
1757 printk(KERN_NOTICE
1758 "%s: usb_lock_device_for_reset failed (%d)\n",
1759 sc->name, lkr);
1760 } else {
1761 rc = usb_reset_device(sc->dev);
1762 if (rc < 0) {
1763 printk(KERN_NOTICE "%s: "
1764 "usb_lock_device_for_reset failed (%d)\n",
1765 sc->name, rc);
1766 }
1767
1768 if (lkr)
1769 usb_unlock_device(sc->dev);
1770 }
1771 }
1772
1773 /*
1774 * In theory, no commands can be running while reset is active,
1775 * so nobody can ask for another reset, and so we do not need any
1776 * queues of resets or anything. We do need a spinlock though,
1777 * to interact with block layer.
1778 */
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08001779 spin_lock_irqsave(sc->lock, flags);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001780 sc->reset = 0;
1781 tasklet_schedule(&sc->tasklet);
1782 list_for_each(p, &sc->luns) {
1783 lun = list_entry(p, struct ub_lun, link);
1784 blk_start_queue(lun->disk->queue);
1785 }
1786 wake_up(&sc->reset_wait);
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08001787 spin_unlock_irqrestore(sc->lock, flags);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001788}
1789
1790/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 * This is called from a process context.
1792 */
Pete Zaitcevf4800072005-05-01 16:05:40 -07001793static void ub_revalidate(struct ub_dev *sc, struct ub_lun *lun)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794{
1795
Pete Zaitcevf4800072005-05-01 16:05:40 -07001796 lun->readonly = 0; /* XXX Query this from the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
Pete Zaitcevf4800072005-05-01 16:05:40 -07001798 lun->capacity.nsec = 0;
1799 lun->capacity.bsize = 512;
1800 lun->capacity.bshift = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801
Pete Zaitcevf4800072005-05-01 16:05:40 -07001802 if (ub_sync_tur(sc, lun) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 return; /* Not ready */
Pete Zaitcevf4800072005-05-01 16:05:40 -07001804 lun->changed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
Pete Zaitcevf4800072005-05-01 16:05:40 -07001806 if (ub_sync_read_cap(sc, lun, &lun->capacity) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 /*
1808 * The retry here means something is wrong, either with the
1809 * device, with the transport, or with our code.
1810 * We keep this because sd.c has retries for capacity.
1811 */
Pete Zaitcevf4800072005-05-01 16:05:40 -07001812 if (ub_sync_read_cap(sc, lun, &lun->capacity) != 0) {
1813 lun->capacity.nsec = 0;
1814 lun->capacity.bsize = 512;
1815 lun->capacity.bshift = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 }
1817 }
1818}
1819
1820/*
1821 * The open funcion.
1822 * This is mostly needed to keep refcounting, but also to support
1823 * media checks on removable media drives.
1824 */
1825static int ub_bd_open(struct inode *inode, struct file *filp)
1826{
1827 struct gendisk *disk = inode->i_bdev->bd_disk;
Pete Zaitcevf4800072005-05-01 16:05:40 -07001828 struct ub_lun *lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 struct ub_dev *sc;
1830 unsigned long flags;
1831 int rc;
1832
Pete Zaitcevf4800072005-05-01 16:05:40 -07001833 if ((lun = disk->private_data) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 return -ENXIO;
Pete Zaitcevf4800072005-05-01 16:05:40 -07001835 sc = lun->udev;
1836
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 spin_lock_irqsave(&ub_lock, flags);
1838 if (atomic_read(&sc->poison)) {
1839 spin_unlock_irqrestore(&ub_lock, flags);
1840 return -ENXIO;
1841 }
1842 sc->openc++;
1843 spin_unlock_irqrestore(&ub_lock, flags);
1844
1845 /*
1846 * This is a workaround for a specific problem in our block layer.
1847 * In 2.6.9, register_disk duplicates the code from rescan_partitions.
1848 * However, if we do add_disk with a device which persistently reports
1849 * a changed media, add_disk calls register_disk, which does do_open,
1850 * which will call rescan_paritions for changed media. After that,
1851 * register_disk attempts to do it all again and causes double kobject
1852 * registration and a eventually an oops on module removal.
1853 *
1854 * The bottom line is, Al Viro says that we should not allow
1855 * bdev->bd_invalidated to be set when doing add_disk no matter what.
1856 */
Pete Zaitcevf4800072005-05-01 16:05:40 -07001857 if (lun->first_open) {
1858 lun->first_open = 0;
1859 if (lun->changed) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 rc = -ENOMEDIUM;
1861 goto err_open;
1862 }
1863 }
1864
Pete Zaitcevf4800072005-05-01 16:05:40 -07001865 if (lun->removable || lun->readonly)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 check_disk_change(inode->i_bdev);
1867
1868 /*
1869 * The sd.c considers ->media_present and ->changed not equivalent,
1870 * under some pretty murky conditions (a failure of READ CAPACITY).
1871 * We may need it one day.
1872 */
Pete Zaitcevf4800072005-05-01 16:05:40 -07001873 if (lun->removable && lun->changed && !(filp->f_flags & O_NDELAY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 rc = -ENOMEDIUM;
1875 goto err_open;
1876 }
1877
Pete Zaitcevf4800072005-05-01 16:05:40 -07001878 if (lun->readonly && (filp->f_mode & FMODE_WRITE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 rc = -EROFS;
1880 goto err_open;
1881 }
1882
1883 return 0;
1884
1885err_open:
1886 ub_put(sc);
1887 return rc;
1888}
1889
1890/*
1891 */
1892static int ub_bd_release(struct inode *inode, struct file *filp)
1893{
1894 struct gendisk *disk = inode->i_bdev->bd_disk;
Pete Zaitcevf4800072005-05-01 16:05:40 -07001895 struct ub_lun *lun = disk->private_data;
1896 struct ub_dev *sc = lun->udev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897
1898 ub_put(sc);
1899 return 0;
1900}
1901
1902/*
1903 * The ioctl interface.
1904 */
1905static int ub_bd_ioctl(struct inode *inode, struct file *filp,
1906 unsigned int cmd, unsigned long arg)
1907{
1908 struct gendisk *disk = inode->i_bdev->bd_disk;
1909 void __user *usermem = (void __user *) arg;
1910
1911 return scsi_cmd_ioctl(filp, disk, cmd, usermem);
1912}
1913
1914/*
1915 * This is called once a new disk was seen by the block layer or by ub_probe().
1916 * The main onjective here is to discover the features of the media such as
1917 * the capacity, read-only status, etc. USB storage generally does not
1918 * need to be spun up, but if we needed it, this would be the place.
1919 *
1920 * This call can sleep.
1921 *
1922 * The return code is not used.
1923 */
1924static int ub_bd_revalidate(struct gendisk *disk)
1925{
Pete Zaitcevf4800072005-05-01 16:05:40 -07001926 struct ub_lun *lun = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927
Pete Zaitcevf4800072005-05-01 16:05:40 -07001928 ub_revalidate(lun->udev, lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929
1930 /* XXX Support sector size switching like in sr.c */
Pete Zaitcevf4800072005-05-01 16:05:40 -07001931 blk_queue_hardsect_size(disk->queue, lun->capacity.bsize);
1932 set_capacity(disk, lun->capacity.nsec);
1933 // set_disk_ro(sdkp->disk, lun->readonly);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
1935 return 0;
1936}
1937
1938/*
1939 * The check is called by the block layer to verify if the media
1940 * is still available. It is supposed to be harmless, lightweight and
1941 * non-intrusive in case the media was not changed.
1942 *
1943 * This call can sleep.
1944 *
1945 * The return code is bool!
1946 */
1947static int ub_bd_media_changed(struct gendisk *disk)
1948{
Pete Zaitcevf4800072005-05-01 16:05:40 -07001949 struct ub_lun *lun = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950
Pete Zaitcevf4800072005-05-01 16:05:40 -07001951 if (!lun->removable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 return 0;
1953
1954 /*
1955 * We clean checks always after every command, so this is not
1956 * as dangerous as it looks. If the TEST_UNIT_READY fails here,
1957 * the device is actually not ready with operator or software
1958 * intervention required. One dangerous item might be a drive which
1959 * spins itself down, and come the time to write dirty pages, this
1960 * will fail, then block layer discards the data. Since we never
1961 * spin drives up, such devices simply cannot be used with ub anyway.
1962 */
Pete Zaitcevf4800072005-05-01 16:05:40 -07001963 if (ub_sync_tur(lun->udev, lun) != 0) {
1964 lun->changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 return 1;
1966 }
1967
Pete Zaitcevf4800072005-05-01 16:05:40 -07001968 return lun->changed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969}
1970
1971static struct block_device_operations ub_bd_fops = {
1972 .owner = THIS_MODULE,
1973 .open = ub_bd_open,
1974 .release = ub_bd_release,
1975 .ioctl = ub_bd_ioctl,
1976 .media_changed = ub_bd_media_changed,
1977 .revalidate_disk = ub_bd_revalidate,
1978};
1979
1980/*
1981 * Common ->done routine for commands executed synchronously.
1982 */
1983static void ub_probe_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1984{
1985 struct completion *cop = cmd->back;
1986 complete(cop);
1987}
1988
1989/*
1990 * Test if the device has a check condition on it, synchronously.
1991 */
Pete Zaitcevf4800072005-05-01 16:05:40 -07001992static int ub_sync_tur(struct ub_dev *sc, struct ub_lun *lun)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993{
1994 struct ub_scsi_cmd *cmd;
1995 enum { ALLOC_SIZE = sizeof(struct ub_scsi_cmd) };
1996 unsigned long flags;
1997 struct completion compl;
1998 int rc;
1999
2000 init_completion(&compl);
2001
2002 rc = -ENOMEM;
2003 if ((cmd = kmalloc(ALLOC_SIZE, GFP_KERNEL)) == NULL)
2004 goto err_alloc;
2005 memset(cmd, 0, ALLOC_SIZE);
2006
2007 cmd->cdb[0] = TEST_UNIT_READY;
2008 cmd->cdb_len = 6;
2009 cmd->dir = UB_DIR_NONE;
2010 cmd->state = UB_CMDST_INIT;
Pete Zaitcevf4800072005-05-01 16:05:40 -07002011 cmd->lun = lun; /* This may be NULL, but that's ok */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 cmd->done = ub_probe_done;
2013 cmd->back = &compl;
2014
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08002015 spin_lock_irqsave(sc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 cmd->tag = sc->tagcnt++;
2017
2018 rc = ub_submit_scsi(sc, cmd);
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08002019 spin_unlock_irqrestore(sc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020
2021 if (rc != 0) {
2022 printk("ub: testing ready: submit error (%d)\n", rc); /* P3 */
2023 goto err_submit;
2024 }
2025
2026 wait_for_completion(&compl);
2027
2028 rc = cmd->error;
2029
2030 if (rc == -EIO && cmd->key != 0) /* Retries for benh's key */
2031 rc = cmd->key;
2032
2033err_submit:
2034 kfree(cmd);
2035err_alloc:
2036 return rc;
2037}
2038
2039/*
2040 * Read the SCSI capacity synchronously (for probing).
2041 */
Pete Zaitcevf4800072005-05-01 16:05:40 -07002042static int ub_sync_read_cap(struct ub_dev *sc, struct ub_lun *lun,
2043 struct ub_capacity *ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044{
2045 struct ub_scsi_cmd *cmd;
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07002046 struct scatterlist *sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 char *p;
2048 enum { ALLOC_SIZE = sizeof(struct ub_scsi_cmd) + 8 };
2049 unsigned long flags;
2050 unsigned int bsize, shift;
2051 unsigned long nsec;
2052 struct completion compl;
2053 int rc;
2054
2055 init_completion(&compl);
2056
2057 rc = -ENOMEM;
2058 if ((cmd = kmalloc(ALLOC_SIZE, GFP_KERNEL)) == NULL)
2059 goto err_alloc;
2060 memset(cmd, 0, ALLOC_SIZE);
2061 p = (char *)cmd + sizeof(struct ub_scsi_cmd);
2062
2063 cmd->cdb[0] = 0x25;
2064 cmd->cdb_len = 10;
2065 cmd->dir = UB_DIR_READ;
2066 cmd->state = UB_CMDST_INIT;
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07002067 cmd->nsg = 1;
2068 sg = &cmd->sgv[0];
2069 sg->page = virt_to_page(p);
Pete Zaitcev38ffdd62005-09-24 13:10:16 -07002070 sg->offset = (unsigned long)p & (PAGE_SIZE-1);
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07002071 sg->length = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 cmd->len = 8;
Pete Zaitcevf4800072005-05-01 16:05:40 -07002073 cmd->lun = lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 cmd->done = ub_probe_done;
2075 cmd->back = &compl;
2076
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08002077 spin_lock_irqsave(sc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 cmd->tag = sc->tagcnt++;
2079
2080 rc = ub_submit_scsi(sc, cmd);
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08002081 spin_unlock_irqrestore(sc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082
2083 if (rc != 0) {
2084 printk("ub: reading capacity: submit error (%d)\n", rc); /* P3 */
2085 goto err_submit;
2086 }
2087
2088 wait_for_completion(&compl);
2089
2090 if (cmd->error != 0) {
2091 printk("ub: reading capacity: error %d\n", cmd->error); /* P3 */
2092 rc = -EIO;
2093 goto err_read;
2094 }
2095 if (cmd->act_len != 8) {
2096 printk("ub: reading capacity: size %d\n", cmd->act_len); /* P3 */
2097 rc = -EIO;
2098 goto err_read;
2099 }
2100
2101 /* sd.c special-cases sector size of 0 to mean 512. Needed? Safe? */
2102 nsec = be32_to_cpu(*(__be32 *)p) + 1;
2103 bsize = be32_to_cpu(*(__be32 *)(p + 4));
2104 switch (bsize) {
2105 case 512: shift = 0; break;
2106 case 1024: shift = 1; break;
2107 case 2048: shift = 2; break;
2108 case 4096: shift = 3; break;
2109 default:
2110 printk("ub: Bad sector size %u\n", bsize); /* P3 */
2111 rc = -EDOM;
2112 goto err_inv_bsize;
2113 }
2114
2115 ret->bsize = bsize;
2116 ret->bshift = shift;
2117 ret->nsec = nsec << shift;
2118 rc = 0;
2119
2120err_inv_bsize:
2121err_read:
2122err_submit:
2123 kfree(cmd);
2124err_alloc:
2125 return rc;
2126}
2127
2128/*
2129 */
2130static void ub_probe_urb_complete(struct urb *urb, struct pt_regs *pt)
2131{
2132 struct completion *cop = urb->context;
2133 complete(cop);
2134}
2135
2136static void ub_probe_timeout(unsigned long arg)
2137{
2138 struct completion *cop = (struct completion *) arg;
2139 complete(cop);
2140}
2141
2142/*
Pete Zaitcevf4800072005-05-01 16:05:40 -07002143 * Get number of LUNs by the way of Bulk GetMaxLUN command.
2144 */
2145static int ub_sync_getmaxlun(struct ub_dev *sc)
2146{
2147 int ifnum = sc->intf->cur_altsetting->desc.bInterfaceNumber;
2148 unsigned char *p;
2149 enum { ALLOC_SIZE = 1 };
2150 struct usb_ctrlrequest *cr;
2151 struct completion compl;
2152 struct timer_list timer;
2153 int nluns;
2154 int rc;
2155
2156 init_completion(&compl);
2157
2158 rc = -ENOMEM;
2159 if ((p = kmalloc(ALLOC_SIZE, GFP_KERNEL)) == NULL)
2160 goto err_alloc;
2161 *p = 55;
2162
2163 cr = &sc->work_cr;
2164 cr->bRequestType = USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
2165 cr->bRequest = US_BULK_GET_MAX_LUN;
2166 cr->wValue = cpu_to_le16(0);
2167 cr->wIndex = cpu_to_le16(ifnum);
2168 cr->wLength = cpu_to_le16(1);
2169
2170 usb_fill_control_urb(&sc->work_urb, sc->dev, sc->recv_ctrl_pipe,
2171 (unsigned char*) cr, p, 1, ub_probe_urb_complete, &compl);
Pete Zaitcevf4800072005-05-01 16:05:40 -07002172 sc->work_urb.actual_length = 0;
2173 sc->work_urb.error_count = 0;
2174 sc->work_urb.status = 0;
2175
2176 if ((rc = usb_submit_urb(&sc->work_urb, GFP_KERNEL)) != 0) {
2177 if (rc == -EPIPE) {
Pete Zaitcev64bd8452005-09-22 00:48:29 -07002178 printk("%s: Stall submitting GetMaxLUN, using 1 LUN\n",
Pete Zaitcevf4800072005-05-01 16:05:40 -07002179 sc->name); /* P3 */
2180 } else {
Pete Zaitcev64bd8452005-09-22 00:48:29 -07002181 printk(KERN_NOTICE
Pete Zaitcevf4800072005-05-01 16:05:40 -07002182 "%s: Unable to submit GetMaxLUN (%d)\n",
2183 sc->name, rc);
2184 }
2185 goto err_submit;
2186 }
2187
2188 init_timer(&timer);
2189 timer.function = ub_probe_timeout;
2190 timer.data = (unsigned long) &compl;
2191 timer.expires = jiffies + UB_CTRL_TIMEOUT;
2192 add_timer(&timer);
2193
2194 wait_for_completion(&compl);
2195
2196 del_timer_sync(&timer);
2197 usb_kill_urb(&sc->work_urb);
2198
Pete Zaitcev64bd8452005-09-22 00:48:29 -07002199 if ((rc = sc->work_urb.status) < 0) {
2200 if (rc == -EPIPE) {
2201 printk("%s: Stall at GetMaxLUN, using 1 LUN\n",
2202 sc->name); /* P3 */
2203 } else {
2204 printk(KERN_NOTICE
2205 "%s: Error at GetMaxLUN (%d)\n",
2206 sc->name, rc);
2207 }
2208 goto err_io;
2209 }
2210
Pete Zaitcevf4800072005-05-01 16:05:40 -07002211 if (sc->work_urb.actual_length != 1) {
2212 printk("%s: GetMaxLUN returned %d bytes\n", sc->name,
2213 sc->work_urb.actual_length); /* P3 */
2214 nluns = 0;
2215 } else {
2216 if ((nluns = *p) == 55) {
2217 nluns = 0;
2218 } else {
2219 /* GetMaxLUN returns the maximum LUN number */
2220 nluns += 1;
2221 if (nluns > UB_MAX_LUNS)
2222 nluns = UB_MAX_LUNS;
2223 }
2224 printk("%s: GetMaxLUN returned %d, using %d LUNs\n", sc->name,
2225 *p, nluns); /* P3 */
2226 }
2227
2228 kfree(p);
2229 return nluns;
2230
Pete Zaitcev64bd8452005-09-22 00:48:29 -07002231err_io:
Pete Zaitcevf4800072005-05-01 16:05:40 -07002232err_submit:
2233 kfree(p);
2234err_alloc:
2235 return rc;
2236}
2237
2238/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 * Clear initial stalls.
2240 */
2241static int ub_probe_clear_stall(struct ub_dev *sc, int stalled_pipe)
2242{
2243 int endp;
2244 struct usb_ctrlrequest *cr;
2245 struct completion compl;
2246 struct timer_list timer;
2247 int rc;
2248
2249 init_completion(&compl);
2250
2251 endp = usb_pipeendpoint(stalled_pipe);
2252 if (usb_pipein (stalled_pipe))
2253 endp |= USB_DIR_IN;
2254
2255 cr = &sc->work_cr;
2256 cr->bRequestType = USB_RECIP_ENDPOINT;
2257 cr->bRequest = USB_REQ_CLEAR_FEATURE;
2258 cr->wValue = cpu_to_le16(USB_ENDPOINT_HALT);
2259 cr->wIndex = cpu_to_le16(endp);
2260 cr->wLength = cpu_to_le16(0);
2261
2262 usb_fill_control_urb(&sc->work_urb, sc->dev, sc->send_ctrl_pipe,
2263 (unsigned char*) cr, NULL, 0, ub_probe_urb_complete, &compl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 sc->work_urb.actual_length = 0;
2265 sc->work_urb.error_count = 0;
2266 sc->work_urb.status = 0;
2267
2268 if ((rc = usb_submit_urb(&sc->work_urb, GFP_KERNEL)) != 0) {
2269 printk(KERN_WARNING
2270 "%s: Unable to submit a probe clear (%d)\n", sc->name, rc);
2271 return rc;
2272 }
2273
2274 init_timer(&timer);
2275 timer.function = ub_probe_timeout;
2276 timer.data = (unsigned long) &compl;
2277 timer.expires = jiffies + UB_CTRL_TIMEOUT;
2278 add_timer(&timer);
2279
2280 wait_for_completion(&compl);
2281
2282 del_timer_sync(&timer);
2283 usb_kill_urb(&sc->work_urb);
2284
2285 /* reset the endpoint toggle */
2286 usb_settoggle(sc->dev, endp, usb_pipeout(sc->last_pipe), 0);
2287
2288 return 0;
2289}
2290
2291/*
2292 * Get the pipe settings.
2293 */
2294static int ub_get_pipes(struct ub_dev *sc, struct usb_device *dev,
2295 struct usb_interface *intf)
2296{
2297 struct usb_host_interface *altsetting = intf->cur_altsetting;
2298 struct usb_endpoint_descriptor *ep_in = NULL;
2299 struct usb_endpoint_descriptor *ep_out = NULL;
2300 struct usb_endpoint_descriptor *ep;
2301 int i;
2302
2303 /*
2304 * Find the endpoints we need.
2305 * We are expecting a minimum of 2 endpoints - in and out (bulk).
2306 * We will ignore any others.
2307 */
2308 for (i = 0; i < altsetting->desc.bNumEndpoints; i++) {
2309 ep = &altsetting->endpoint[i].desc;
2310
2311 /* Is it a BULK endpoint? */
2312 if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
2313 == USB_ENDPOINT_XFER_BULK) {
2314 /* BULK in or out? */
2315 if (ep->bEndpointAddress & USB_DIR_IN)
2316 ep_in = ep;
2317 else
2318 ep_out = ep;
2319 }
2320 }
2321
2322 if (ep_in == NULL || ep_out == NULL) {
Pete Zaitcevf4800072005-05-01 16:05:40 -07002323 printk(KERN_NOTICE "%s: failed endpoint check\n",
2324 sc->name);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08002325 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 }
2327
2328 /* Calculate and store the pipe values */
2329 sc->send_ctrl_pipe = usb_sndctrlpipe(dev, 0);
2330 sc->recv_ctrl_pipe = usb_rcvctrlpipe(dev, 0);
2331 sc->send_bulk_pipe = usb_sndbulkpipe(dev,
2332 ep_out->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
2333 sc->recv_bulk_pipe = usb_rcvbulkpipe(dev,
2334 ep_in->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
2335
2336 return 0;
2337}
2338
2339/*
2340 * Probing is done in the process context, which allows us to cheat
2341 * and not to build a state machine for the discovery.
2342 */
2343static int ub_probe(struct usb_interface *intf,
2344 const struct usb_device_id *dev_id)
2345{
2346 struct ub_dev *sc;
Pete Zaitcevf4800072005-05-01 16:05:40 -07002347 int nluns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 int rc;
2349 int i;
2350
Pete Zaitceva00828e2005-10-22 20:15:09 -07002351 if (usb_usual_check_type(dev_id, USB_US_TYPE_UB))
2352 return -ENXIO;
2353
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 rc = -ENOMEM;
2355 if ((sc = kmalloc(sizeof(struct ub_dev), GFP_KERNEL)) == NULL)
2356 goto err_core;
2357 memset(sc, 0, sizeof(struct ub_dev));
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08002358 sc->lock = ub_next_lock();
Pete Zaitcevf4800072005-05-01 16:05:40 -07002359 INIT_LIST_HEAD(&sc->luns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 usb_init_urb(&sc->work_urb);
2361 tasklet_init(&sc->tasklet, ub_scsi_action, (unsigned long)sc);
2362 atomic_set(&sc->poison, 0);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08002363 INIT_WORK(&sc->reset_work, ub_reset_task, sc);
2364 init_waitqueue_head(&sc->reset_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365
2366 init_timer(&sc->work_timer);
2367 sc->work_timer.data = (unsigned long) sc;
2368 sc->work_timer.function = ub_urb_timeout;
2369
2370 ub_init_completion(&sc->work_done);
2371 sc->work_done.done = 1; /* A little yuk, but oh well... */
2372
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 sc->dev = interface_to_usbdev(intf);
2374 sc->intf = intf;
2375 // sc->ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 usb_set_intfdata(intf, sc);
2377 usb_get_dev(sc->dev);
2378 // usb_get_intf(sc->intf); /* Do we need this? */
2379
Pete Zaitcevf4800072005-05-01 16:05:40 -07002380 snprintf(sc->name, 12, DRV_NAME "(%d.%d)",
2381 sc->dev->bus->busnum, sc->dev->devnum);
2382
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 /* XXX Verify that we can handle the device (from descriptors) */
2384
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08002385 if (ub_get_pipes(sc, sc->dev, intf) != 0)
2386 goto err_dev_desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387
2388 if (device_create_file(&sc->intf->dev, &dev_attr_diag) != 0)
2389 goto err_diag;
2390
2391 /*
2392 * At this point, all USB initialization is done, do upper layer.
2393 * We really hate halfway initialized structures, so from the
2394 * invariants perspective, this ub_dev is fully constructed at
2395 * this point.
2396 */
2397
2398 /*
2399 * This is needed to clear toggles. It is a problem only if we do
2400 * `rmmod ub && modprobe ub` without disconnects, but we like that.
2401 */
Pete Zaitcevc6c88832005-09-22 00:49:45 -07002402#if 0 /* iPod Mini fails if we do this (big white iPod works) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 ub_probe_clear_stall(sc, sc->recv_bulk_pipe);
2404 ub_probe_clear_stall(sc, sc->send_bulk_pipe);
Pete Zaitcevc6c88832005-09-22 00:49:45 -07002405#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406
2407 /*
2408 * The way this is used by the startup code is a little specific.
2409 * A SCSI check causes a USB stall. Our common case code sees it
2410 * and clears the check, after which the device is ready for use.
2411 * But if a check was not present, any command other than
2412 * TEST_UNIT_READY ends with a lockup (including REQUEST_SENSE).
2413 *
2414 * If we neglect to clear the SCSI check, the first real command fails
2415 * (which is the capacity readout). We clear that and retry, but why
2416 * causing spurious retries for no reason.
2417 *
2418 * Revalidation may start with its own TEST_UNIT_READY, but that one
2419 * has to succeed, so we clear checks with an additional one here.
2420 * In any case it's not our business how revaliadation is implemented.
2421 */
2422 for (i = 0; i < 3; i++) { /* Retries for benh's key */
Pete Zaitcevf4800072005-05-01 16:05:40 -07002423 if ((rc = ub_sync_tur(sc, NULL)) <= 0) break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 if (rc != 0x6) break;
2425 msleep(10);
2426 }
2427
Pete Zaitcevf4800072005-05-01 16:05:40 -07002428 nluns = 1;
2429 for (i = 0; i < 3; i++) {
2430 if ((rc = ub_sync_getmaxlun(sc)) < 0) {
2431 /*
Pete Zaitcev64bd8452005-09-22 00:48:29 -07002432 * This segment is taken from usb-storage. They say
2433 * that ZIP-100 needs this, but my own ZIP-100 works
2434 * fine without this.
2435 * Still, it does not seem to hurt anything.
Pete Zaitcevf4800072005-05-01 16:05:40 -07002436 */
2437 if (rc == -EPIPE) {
2438 ub_probe_clear_stall(sc, sc->recv_bulk_pipe);
2439 ub_probe_clear_stall(sc, sc->send_bulk_pipe);
2440 }
2441 break;
2442 }
2443 if (rc != 0) {
2444 nluns = rc;
2445 break;
2446 }
Pete Zaitcev9f793d22005-06-06 13:54:59 -07002447 msleep(100);
Pete Zaitcevf4800072005-05-01 16:05:40 -07002448 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449
Pete Zaitcevf4800072005-05-01 16:05:40 -07002450 for (i = 0; i < nluns; i++) {
2451 ub_probe_lun(sc, i);
2452 }
2453 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454
Pete Zaitcevf4800072005-05-01 16:05:40 -07002455 /* device_remove_file(&sc->intf->dev, &dev_attr_diag); */
2456err_diag:
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08002457err_dev_desc:
Pete Zaitcevf4800072005-05-01 16:05:40 -07002458 usb_set_intfdata(intf, NULL);
2459 // usb_put_intf(sc->intf);
2460 usb_put_dev(sc->dev);
2461 kfree(sc);
2462err_core:
2463 return rc;
2464}
2465
2466static int ub_probe_lun(struct ub_dev *sc, int lnum)
2467{
2468 struct ub_lun *lun;
2469 request_queue_t *q;
2470 struct gendisk *disk;
2471 int rc;
2472
2473 rc = -ENOMEM;
2474 if ((lun = kmalloc(sizeof(struct ub_lun), GFP_KERNEL)) == NULL)
2475 goto err_alloc;
2476 memset(lun, 0, sizeof(struct ub_lun));
2477 lun->num = lnum;
2478
2479 rc = -ENOSR;
2480 if ((lun->id = ub_id_get()) == -1)
2481 goto err_id;
2482
2483 lun->udev = sc;
2484 list_add(&lun->link, &sc->luns);
2485
2486 snprintf(lun->name, 16, DRV_NAME "%c(%d.%d.%d)",
2487 lun->id + 'a', sc->dev->bus->busnum, sc->dev->devnum, lun->num);
2488
2489 lun->removable = 1; /* XXX Query this from the device */
2490 lun->changed = 1; /* ub_revalidate clears only */
2491 lun->first_open = 1;
2492 ub_revalidate(sc, lun);
2493
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 rc = -ENOMEM;
Pete Zaitcev4fb729f2005-12-17 02:34:12 -08002495 if ((disk = alloc_disk(UB_PARTS_PER_LUN)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 goto err_diskalloc;
2497
Pete Zaitcevf4800072005-05-01 16:05:40 -07002498 lun->disk = disk;
2499 sprintf(disk->disk_name, DRV_NAME "%c", lun->id + 'a');
2500 sprintf(disk->devfs_name, DEVFS_NAME "/%c", lun->id + 'a');
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 disk->major = UB_MAJOR;
Pete Zaitcev4fb729f2005-12-17 02:34:12 -08002502 disk->first_minor = lun->id * UB_PARTS_PER_LUN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 disk->fops = &ub_bd_fops;
Pete Zaitcevf4800072005-05-01 16:05:40 -07002504 disk->private_data = lun;
Pete Zaitcev64bd8452005-09-22 00:48:29 -07002505 disk->driverfs_dev = &sc->intf->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506
2507 rc = -ENOMEM;
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08002508 if ((q = blk_init_queue(ub_request_fn, sc->lock)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 goto err_blkqinit;
2510
2511 disk->queue = q;
2512
Pete Zaitcevf4800072005-05-01 16:05:40 -07002513 blk_queue_bounce_limit(q, BLK_BOUNCE_HIGH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 blk_queue_max_hw_segments(q, UB_MAX_REQ_SG);
2515 blk_queue_max_phys_segments(q, UB_MAX_REQ_SG);
Pete Zaitcevf4800072005-05-01 16:05:40 -07002516 blk_queue_segment_boundary(q, 0xffffffff); /* Dubious. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 blk_queue_max_sectors(q, UB_MAX_SECTORS);
Pete Zaitcevf4800072005-05-01 16:05:40 -07002518 blk_queue_hardsect_size(q, lun->capacity.bsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519
Pete Zaitcevf4800072005-05-01 16:05:40 -07002520 q->queuedata = lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521
Pete Zaitcevf4800072005-05-01 16:05:40 -07002522 set_capacity(disk, lun->capacity.nsec);
2523 if (lun->removable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 disk->flags |= GENHD_FL_REMOVABLE;
2525
2526 add_disk(disk);
2527
2528 return 0;
2529
2530err_blkqinit:
2531 put_disk(disk);
2532err_diskalloc:
Pete Zaitcevf4800072005-05-01 16:05:40 -07002533 list_del(&lun->link);
2534 ub_id_put(lun->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535err_id:
Pete Zaitcevf4800072005-05-01 16:05:40 -07002536 kfree(lun);
2537err_alloc:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 return rc;
2539}
2540
2541static void ub_disconnect(struct usb_interface *intf)
2542{
2543 struct ub_dev *sc = usb_get_intfdata(intf);
Pete Zaitcevf4800072005-05-01 16:05:40 -07002544 struct list_head *p;
2545 struct ub_lun *lun;
2546 struct gendisk *disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 unsigned long flags;
2548
2549 /*
2550 * Prevent ub_bd_release from pulling the rug from under us.
2551 * XXX This is starting to look like a kref.
2552 * XXX Why not to take this ref at probe time?
2553 */
2554 spin_lock_irqsave(&ub_lock, flags);
2555 sc->openc++;
2556 spin_unlock_irqrestore(&ub_lock, flags);
2557
2558 /*
2559 * Fence stall clearnings, operations triggered by unlinkings and so on.
2560 * We do not attempt to unlink any URBs, because we do not trust the
2561 * unlink paths in HC drivers. Also, we get -84 upon disconnect anyway.
2562 */
2563 atomic_set(&sc->poison, 1);
2564
2565 /*
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08002566 * Wait for reset to end, if any.
2567 */
2568 wait_event(sc->reset_wait, !sc->reset);
2569
2570 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 * Blow away queued commands.
2572 *
2573 * Actually, this never works, because before we get here
2574 * the HCD terminates outstanding URB(s). It causes our
2575 * SCSI command queue to advance, commands fail to submit,
2576 * and the whole queue drains. So, we just use this code to
2577 * print warnings.
2578 */
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08002579 spin_lock_irqsave(sc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 {
2581 struct ub_scsi_cmd *cmd;
2582 int cnt = 0;
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08002583 while ((cmd = ub_cmdq_peek(sc)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 cmd->error = -ENOTCONN;
2585 cmd->state = UB_CMDST_DONE;
2586 ub_cmdtr_state(sc, cmd);
2587 ub_cmdq_pop(sc);
2588 (*cmd->done)(sc, cmd);
2589 cnt++;
2590 }
2591 if (cnt != 0) {
2592 printk(KERN_WARNING "%s: "
2593 "%d was queued after shutdown\n", sc->name, cnt);
2594 }
2595 }
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08002596 spin_unlock_irqrestore(sc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597
2598 /*
2599 * Unregister the upper layer.
2600 */
Pete Zaitcevf4800072005-05-01 16:05:40 -07002601 list_for_each (p, &sc->luns) {
2602 lun = list_entry(p, struct ub_lun, link);
2603 disk = lun->disk;
2604 if (disk->flags & GENHD_FL_UP)
2605 del_gendisk(disk);
2606 /*
2607 * I wish I could do:
2608 * set_bit(QUEUE_FLAG_DEAD, &q->queue_flags);
2609 * As it is, we rely on our internal poisoning and let
2610 * the upper levels to spin furiously failing all the I/O.
2611 */
2612 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613
2614 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 * Testing for -EINPROGRESS is always a bug, so we are bending
2616 * the rules a little.
2617 */
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08002618 spin_lock_irqsave(sc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 if (sc->work_urb.status == -EINPROGRESS) { /* janitors: ignore */
2620 printk(KERN_WARNING "%s: "
2621 "URB is active after disconnect\n", sc->name);
2622 }
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08002623 spin_unlock_irqrestore(sc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624
2625 /*
2626 * There is virtually no chance that other CPU runs times so long
2627 * after ub_urb_complete should have called del_timer, but only if HCD
2628 * didn't forget to deliver a callback on unlink.
2629 */
2630 del_timer_sync(&sc->work_timer);
2631
2632 /*
2633 * At this point there must be no commands coming from anyone
2634 * and no URBs left in transit.
2635 */
2636
2637 device_remove_file(&sc->intf->dev, &dev_attr_diag);
2638 usb_set_intfdata(intf, NULL);
2639 // usb_put_intf(sc->intf);
2640 sc->intf = NULL;
2641 usb_put_dev(sc->dev);
2642 sc->dev = NULL;
2643
2644 ub_put(sc);
2645}
2646
2647static struct usb_driver ub_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 .name = "ub",
2649 .probe = ub_probe,
2650 .disconnect = ub_disconnect,
2651 .id_table = ub_usb_ids,
2652};
2653
2654static int __init ub_init(void)
2655{
2656 int rc;
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08002657 int i;
2658
2659 for (i = 0; i < UB_QLOCK_NUM; i++)
2660 spin_lock_init(&ub_qlockv[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 if ((rc = register_blkdev(UB_MAJOR, DRV_NAME)) != 0)
2663 goto err_regblkdev;
2664 devfs_mk_dir(DEVFS_NAME);
2665
2666 if ((rc = usb_register(&ub_driver)) != 0)
2667 goto err_register;
2668
Pete Zaitceva00828e2005-10-22 20:15:09 -07002669 usb_usual_set_present(USB_US_TYPE_UB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 return 0;
2671
2672err_register:
2673 devfs_remove(DEVFS_NAME);
2674 unregister_blkdev(UB_MAJOR, DRV_NAME);
2675err_regblkdev:
2676 return rc;
2677}
2678
2679static void __exit ub_exit(void)
2680{
2681 usb_deregister(&ub_driver);
2682
2683 devfs_remove(DEVFS_NAME);
2684 unregister_blkdev(UB_MAJOR, DRV_NAME);
Pete Zaitceva00828e2005-10-22 20:15:09 -07002685 usb_usual_clear_present(USB_US_TYPE_UB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686}
2687
2688module_init(ub_init);
2689module_exit(ub_exit);
2690
2691MODULE_LICENSE("GPL");