[PATCH] USB: ub 04 Loss of timer and a hang
[linux-2.6-block.git] / drivers / block / ub.c
CommitLineData
1da177e4
LT
1/*
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)
f4800078 11 * -- Kill first_open (Al Viro fixed the block layer now)
1da177e4
LT
12 * -- set readonly flag for CDs, set removable flag for CF readers
13 * -- do inquiry and verify we got a disk and not a tape (for LUN mismatch)
1da177e4
LT
14 * -- special case some senses, e.g. 3a/0 -> no media present, reduce retries
15 * -- verify the 13 conditions and do bulk resets
1da177e4
LT
16 * -- kill last_pipe and simply do two-state clearing on both pipes
17 * -- verify protocol (bulk) from USB descriptors (maybe...)
ba6abf13 18 * -- highmem
1da177e4
LT
19 * -- move top_sense and work_bcs into separate allocations (if they survive)
20 * for cache purists and esoteric architectures.
ba6abf13 21 * -- Allocate structure for LUN 0 before the first ub_sync_tur, avoid NULL. ?
1da177e4
LT
22 * -- 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=?
1872bceb 26 * -- CLEAR, CLR2STS, CLRRS seem to be ripe for refactoring.
1da177e4
LT
27 */
28#include <linux/kernel.h>
29#include <linux/module.h>
30#include <linux/usb.h>
a00828e9 31#include <linux/usb_usual.h>
1da177e4
LT
32#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
1872bceb
PZ
42/*
43 * 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
1da177e4 109/*
f4800078
PZ
110 * This many LUNs per USB device.
111 * Every one of them takes a host, see UB_MAX_HOSTS.
1da177e4 112 */
9f793d2c 113#define UB_MAX_LUNS 9
f4800078
PZ
114
115/*
116 */
117
4fb729f5 118#define UB_PARTS_PER_LUN 8
1da177e4
LT
119
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 */
f4800078 133 u8 Lun; /* LUN */
1da177e4
LT
134 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' */
1da177e4
LT
153#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
64bd8453 165#define UB_MAX_REQ_SG 9 /* cdrecord requires 32KB and maybe a header */
1da177e4
LT
166#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) */
1872bceb 195 UB_CMDST_CLRRS, /* Clearing before retrying status */
1da177e4
LT
196 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",
1872bceb 207 "crs",
1da177e4
LT
208 "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
1da177e4 228 unsigned int len; /* Requested length */
a1cf96ef
PZ
229 unsigned int current_sg;
230 unsigned int nsg; /* sgv[nsg] */
231 struct scatterlist sgv[UB_MAX_REQ_SG];
1da177e4 232
f4800078 233 struct ub_lun *lun;
1da177e4
LT
234 void (*done)(struct ub_dev *, struct ub_scsi_cmd *);
235 void *back;
236};
237
2c26c9e6
PZ
238struct 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
1da177e4
LT
245/*
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/*
f4800078
PZ
325 * The block device instance (one per LUN).
326 */
327struct 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
335 int changed; /* Media was changed */
336 int removable;
337 int readonly;
338 int first_open; /* Kludge. See ub_bd_open. */
339
2c26c9e6
PZ
340 struct ub_request urq;
341
f4800078
PZ
342 /* 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.
1da177e4
LT
356 */
357struct ub_dev {
65b4fe55 358 spinlock_t *lock;
1da177e4
LT
359 atomic_t poison; /* The USB device is disconnected */
360 int openc; /* protected by ub_lock! */
361 /* kref is too implicit for our taste */
2c26c9e6 362 int reset; /* Reset is running */
1da177e4 363 unsigned int tagcnt;
f4800078 364 char name[12];
1da177e4
LT
365 struct usb_device *dev;
366 struct usb_interface *intf;
367
f4800078 368 struct list_head luns;
1da177e4
LT
369
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
1da177e4
LT
377 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 */
1872bceb 385 __le32 signature; /* Learned signature */
1da177e4
LT
386 struct bulk_cb_wrap work_bcb;
387 struct bulk_cs_wrap work_bcs;
388 struct usb_ctrlrequest work_cr;
389
2c26c9e6
PZ
390 struct work_struct reset_work;
391 wait_queue_head_t reset_wait;
392
64bd8453 393 int sg_stat[6];
1da177e4
LT
394 struct ub_scsi_trace tr;
395};
396
397/*
398 */
399static void ub_cleanup(struct ub_dev *sc);
6c1eb8c1 400static int ub_request_fn_1(struct ub_lun *lun, struct request *rq);
2c26c9e6
PZ
401static 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);
1da177e4
LT
405static void ub_rw_cmd_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
406static void ub_end_rq(struct request *rq, int uptodate);
2c26c9e6
PZ
407static int ub_rw_cmd_retry(struct ub_dev *sc, struct ub_lun *lun,
408 struct ub_request *urq, struct ub_scsi_cmd *cmd);
1da177e4
LT
409static 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);
a1cf96ef 414static void ub_data_start(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
1da177e4 415static void ub_state_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd, int rc);
1872bceb 416static int __ub_state_stat(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
1da177e4 417static void ub_state_stat(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
1872bceb 418static void ub_state_stat_counted(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
1da177e4
LT
419static 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);
2c26c9e6
PZ
423static void ub_reset_enter(struct ub_dev *sc);
424static void ub_reset_task(void *arg);
f4800078
PZ
425static 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);
1da177e4
LT
429
430/*
431 */
a00828e9
PZ
432#ifdef CONFIG_USB_LIBUSUAL
433
434#define ub_usb_ids storage_usb_ids
435#else
436
1da177e4 437static struct usb_device_id ub_usb_ids[] = {
1da177e4
LT
438 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_SCSI, US_PR_BULK) },
439 { }
440};
441
442MODULE_DEVICE_TABLE(usb, ub_usb_ids);
a00828e9 443#endif /* CONFIG_USB_LIBUSUAL */
1da177e4
LT
444
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];
f4800078 454
65b4fe55
PZ
455#define UB_QLOCK_NUM 5
456static spinlock_t ub_qlockv[UB_QLOCK_NUM];
457static int ub_qlock_next = 0;
458
1da177e4
LT
459static 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
07d4fd25
PZ
519static ssize_t ub_diag_show(struct device *dev, struct device_attribute *attr,
520 char *page)
1da177e4
LT
521{
522 struct usb_interface *intf;
523 struct ub_dev *sc;
f4800078
PZ
524 struct list_head *p;
525 struct ub_lun *lun;
1da177e4
LT
526 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;
65b4fe55 538 spin_lock_irqsave(sc->lock, flags);
1da177e4 539
2c26c9e6
PZ
540 cnt += sprintf(page + cnt,
541 "poison %d reset %d\n",
542 atomic_read(&sc->poison), sc->reset);
1da177e4 543 cnt += sprintf(page + cnt,
f4800078
PZ
544 "qlen %d qmax %d\n",
545 sc->cmd_queue.qlen, sc->cmd_queue.qmax);
ba6abf13 546 cnt += sprintf(page + cnt,
64bd8453 547 "sg %d %d %d %d %d .. %d\n",
ba6abf13
PZ
548 sc->sg_stat[0],
549 sc->sg_stat[1],
550 sc->sg_stat[2],
551 sc->sg_stat[3],
64bd8453
PZ
552 sc->sg_stat[4],
553 sc->sg_stat[5]);
f4800078
PZ
554
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 }
1da177e4
LT
561
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
65b4fe55 586 spin_unlock_irqrestore(sc->lock, flags);
1da177e4
LT
587 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
65b4fe55
PZ
633/*
634 * 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
1da177e4
LT
651/*
652 * 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{
f4800078
PZ
678 struct list_head *p;
679 struct ub_lun *lun;
1da177e4
LT
680 request_queue_t *q;
681
f4800078
PZ
682 while (!list_empty(&sc->luns)) {
683 p = sc->luns.next;
684 lun = list_entry(p, struct ub_lun, link);
685 list_del(p);
1da177e4 686
f4800078
PZ
687 /* 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;
701
702 ub_id_put(lun->id);
703 kfree(lun);
704 }
1da177e4 705
1da177e4
LT
706 kfree(sc);
707}
708
709/*
710 * The "command allocator".
711 */
f4800078 712static struct ub_scsi_cmd *ub_get_cmd(struct ub_lun *lun)
1da177e4
LT
713{
714 struct ub_scsi_cmd *ret;
715
f4800078 716 if (lun->cmda[0])
1da177e4 717 return NULL;
f4800078
PZ
718 ret = &lun->cmdv[0];
719 lun->cmda[0] = 1;
1da177e4
LT
720 return ret;
721}
722
f4800078 723static void ub_put_cmd(struct ub_lun *lun, struct ub_scsi_cmd *cmd)
1da177e4 724{
f4800078 725 if (cmd != &lun->cmdv[0]) {
1da177e4 726 printk(KERN_WARNING "%s: releasing a foreign cmd %p\n",
f4800078 727 lun->name, cmd);
1da177e4
LT
728 return;
729 }
f4800078
PZ
730 if (!lun->cmda[0]) {
731 printk(KERN_WARNING "%s: releasing a free cmd\n", lun->name);
1da177e4
LT
732 return;
733 }
f4800078 734 lun->cmda[0] = 0;
1da177e4
LT
735}
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
6c1eb8c1 793static void ub_request_fn(request_queue_t *q)
1da177e4 794{
f4800078 795 struct ub_lun *lun = q->queuedata;
1da177e4
LT
796 struct request *rq;
797
798 while ((rq = elv_next_request(q)) != NULL) {
6c1eb8c1 799 if (ub_request_fn_1(lun, rq) != 0) {
1da177e4
LT
800 blk_stop_queue(q);
801 break;
802 }
803 }
804}
805
6c1eb8c1 806static int ub_request_fn_1(struct ub_lun *lun, struct request *rq)
1da177e4 807{
f4800078 808 struct ub_dev *sc = lun->udev;
1da177e4 809 struct ub_scsi_cmd *cmd;
2c26c9e6
PZ
810 struct ub_request *urq;
811 int n_elem;
1da177e4 812
f4800078 813 if (atomic_read(&sc->poison) || lun->changed) {
1da177e4
LT
814 blkdev_dequeue_request(rq);
815 ub_end_rq(rq, 0);
816 return 0;
817 }
818
2c26c9e6
PZ
819 if (lun->urq.rq != NULL)
820 return -1;
f4800078 821 if ((cmd = ub_get_cmd(lun)) == NULL)
1da177e4
LT
822 return -1;
823 memset(cmd, 0, sizeof(struct ub_scsi_cmd));
824
825 blkdev_dequeue_request(rq);
2c26c9e6
PZ
826
827 urq = &lun->urq;
828 memset(urq, 0, sizeof(struct ub_request));
829 urq->rq = rq;
830
831 /*
832 * get scatterlist from block layer
833 */
834 n_elem = blk_rq_map_sg(lun->disk->queue, rq, &urq->sgv[0]);
835 if (n_elem < 0) {
836 printk(KERN_INFO "%s: failed request map (%d)\n",
837 lun->name, n_elem); /* P3 */
838 goto drop;
839 }
840 if (n_elem > UB_MAX_REQ_SG) { /* Paranoia */
841 printk(KERN_WARNING "%s: request with %d segments\n",
842 lun->name, n_elem);
843 goto drop;
844 }
845 urq->nsg = n_elem;
846 sc->sg_stat[n_elem < 5 ? n_elem : 5]++;
847
1da177e4 848 if (blk_pc_request(rq)) {
2c26c9e6 849 ub_cmd_build_packet(sc, lun, cmd, urq);
1da177e4 850 } else {
2c26c9e6 851 ub_cmd_build_block(sc, lun, cmd, urq);
1da177e4 852 }
1da177e4 853 cmd->state = UB_CMDST_INIT;
f4800078 854 cmd->lun = lun;
1da177e4 855 cmd->done = ub_rw_cmd_done;
2c26c9e6 856 cmd->back = urq;
1da177e4
LT
857
858 cmd->tag = sc->tagcnt++;
2c26c9e6
PZ
859 if (ub_submit_scsi(sc, cmd) != 0)
860 goto drop;
861
862 return 0;
1da177e4 863
2c26c9e6
PZ
864drop:
865 ub_put_cmd(lun, cmd);
866 ub_end_rq(rq, 0);
1da177e4
LT
867 return 0;
868}
869
2c26c9e6
PZ
870static void ub_cmd_build_block(struct ub_dev *sc, struct ub_lun *lun,
871 struct ub_scsi_cmd *cmd, struct ub_request *urq)
1da177e4 872{
2c26c9e6 873 struct request *rq = urq->rq;
a1cf96ef 874 unsigned int block, nblks;
1da177e4
LT
875
876 if (rq_data_dir(rq) == WRITE)
2c26c9e6 877 cmd->dir = UB_DIR_WRITE;
1da177e4 878 else
2c26c9e6 879 cmd->dir = UB_DIR_READ;
1da177e4 880
2c26c9e6
PZ
881 cmd->nsg = urq->nsg;
882 memcpy(cmd->sgv, urq->sgv, sizeof(struct scatterlist) * cmd->nsg);
1da177e4
LT
883
884 /*
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 */
a1cf96ef
PZ
890 block = rq->sector >> lun->capacity.bshift;
891 nblks = rq->nr_sectors >> lun->capacity.bshift;
ba6abf13 892
2c26c9e6 893 cmd->cdb[0] = (cmd->dir == UB_DIR_READ)? READ_10: WRITE_10;
1da177e4
LT
894 /* 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
a1cf96ef 903 cmd->len = rq->nr_sectors * 512;
1da177e4
LT
904}
905
2c26c9e6
PZ
906static void ub_cmd_build_packet(struct ub_dev *sc, struct ub_lun *lun,
907 struct ub_scsi_cmd *cmd, struct ub_request *urq)
1da177e4 908{
2c26c9e6 909 struct request *rq = urq->rq;
1da177e4
LT
910
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 }
a1cf96ef 919
2c26c9e6
PZ
920 cmd->nsg = urq->nsg;
921 memcpy(cmd->sgv, urq->sgv, sizeof(struct scatterlist) * cmd->nsg);
a1cf96ef
PZ
922
923 memcpy(&cmd->cdb, rq->cmd, rq->cmd_len);
924 cmd->cdb_len = rq->cmd_len;
925
1da177e4 926 cmd->len = rq->data_len;
1da177e4
LT
927}
928
929static void ub_rw_cmd_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
930{
f4800078 931 struct ub_lun *lun = cmd->lun;
2c26c9e6
PZ
932 struct ub_request *urq = cmd->back;
933 struct request *rq;
1da177e4
LT
934 int uptodate;
935
2c26c9e6
PZ
936 rq = urq->rq;
937
a1cf96ef 938 if (cmd->error == 0) {
1da177e4 939 uptodate = 1;
1da177e4 940
a1cf96ef
PZ
941 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;
ba6abf13 946 }
a1cf96ef 947 } else {
ba6abf13 948 uptodate = 0;
ba6abf13 949
a1cf96ef
PZ
950 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;
2c26c9e6
PZ
958 } else {
959 if (cmd->error == -EIO) {
960 if (ub_rw_cmd_retry(sc, lun, urq, cmd) == 0)
961 return;
962 }
a1cf96ef
PZ
963 }
964 }
ba6abf13 965
2c26c9e6
PZ
966 urq->rq = NULL;
967
f4800078 968 ub_put_cmd(lun, cmd);
1da177e4 969 ub_end_rq(rq, uptodate);
ba6abf13 970 blk_start_queue(lun->disk->queue);
1da177e4
LT
971}
972
973static void ub_end_rq(struct request *rq, int uptodate)
974{
ab93091d 975 end_that_request_first(rq, uptodate, rq->hard_nr_sectors);
8ffdc655 976 end_that_request_last(rq, uptodate);
1da177e4
LT
977}
978
2c26c9e6
PZ
979static 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
1da177e4
LT
1014/*
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;
f4800078 1069 bcb->Lun = (cmd->lun != NULL) ? cmd->lun->num : 0;
1da177e4
LT
1070 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);
1da177e4
LT
1080
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 */
1da177e4
LT
1088 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
65b4fe55 1108 spin_lock_irqsave(sc->lock, flags);
b31f821c
PZ
1109 if (!ub_is_completed(&sc->work_done))
1110 usb_unlink_urb(&sc->work_urb);
65b4fe55 1111 spin_unlock_irqrestore(sc->lock, flags);
1da177e4
LT
1112}
1113
1114/*
1115 * Completion routine for the work URB.
1116 *
1117 * This can be called directly from usb_submit_urb (while we have
1118 * the sc->lock taken) and from an interrupt (while we do NOT have
1119 * the sc->lock taken). Therefore, bounce this off to a tasklet.
1120 */
1121static void ub_urb_complete(struct urb *urb, struct pt_regs *pt)
1122{
1123 struct ub_dev *sc = urb->context;
1124
1125 ub_complete(&sc->work_done);
1126 tasklet_schedule(&sc->tasklet);
1127}
1128
1129static void ub_scsi_action(unsigned long _dev)
1130{
1131 struct ub_dev *sc = (struct ub_dev *) _dev;
1132 unsigned long flags;
1133
65b4fe55 1134 spin_lock_irqsave(sc->lock, flags);
1da177e4 1135 ub_scsi_dispatch(sc);
65b4fe55 1136 spin_unlock_irqrestore(sc->lock, flags);
1da177e4
LT
1137}
1138
1139static void ub_scsi_dispatch(struct ub_dev *sc)
1140{
1141 struct ub_scsi_cmd *cmd;
1142 int rc;
1143
2c26c9e6 1144 while (!sc->reset && (cmd = ub_cmdq_peek(sc)) != NULL) {
1da177e4
LT
1145 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;
b31f821c 1158 del_timer(&sc->work_timer);
1da177e4
LT
1159 ub_scsi_urb_compl(sc, cmd);
1160 }
1161 }
1162}
1163
1164static void ub_scsi_urb_compl(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1165{
1166 struct urb *urb = &sc->work_urb;
1167 struct bulk_cs_wrap *bcs;
2c26c9e6 1168 int len;
1da177e4
LT
1169 int rc;
1170
1171 if (atomic_read(&sc->poison)) {
2c26c9e6
PZ
1172 ub_state_done(sc, cmd, -ENODEV);
1173 return;
1da177e4
LT
1174 }
1175
1176 if (cmd->state == UB_CMDST_CLEAR) {
1177 if (urb->status == -EPIPE) {
1178 /*
1179 * STALL while clearning STALL.
1180 * The control pipe clears itself - nothing to do.
1da177e4 1181 */
f4800078
PZ
1182 printk(KERN_NOTICE "%s: stall on control pipe\n",
1183 sc->name);
1da177e4
LT
1184 goto Bad_End;
1185 }
1186
1187 /*
1188 * We ignore the result for the halt clear.
1189 */
1190
1191 /* reset the endpoint toggle */
1192 usb_settoggle(sc->dev, usb_pipeendpoint(sc->last_pipe),
1193 usb_pipeout(sc->last_pipe), 0);
1194
1195 ub_state_sense(sc, cmd);
1196
1197 } else if (cmd->state == UB_CMDST_CLR2STS) {
1198 if (urb->status == -EPIPE) {
f4800078
PZ
1199 printk(KERN_NOTICE "%s: stall on control pipe\n",
1200 sc->name);
1da177e4
LT
1201 goto Bad_End;
1202 }
1203
1204 /*
1205 * We ignore the result for the halt clear.
1206 */
1207
1208 /* reset the endpoint toggle */
1209 usb_settoggle(sc->dev, usb_pipeendpoint(sc->last_pipe),
1210 usb_pipeout(sc->last_pipe), 0);
1211
1212 ub_state_stat(sc, cmd);
1213
1872bceb
PZ
1214 } else if (cmd->state == UB_CMDST_CLRRS) {
1215 if (urb->status == -EPIPE) {
1872bceb
PZ
1216 printk(KERN_NOTICE "%s: stall on control pipe\n",
1217 sc->name);
1218 goto Bad_End;
1219 }
1220
1221 /*
1222 * We ignore the result for the halt clear.
1223 */
1224
1225 /* reset the endpoint toggle */
1226 usb_settoggle(sc->dev, usb_pipeendpoint(sc->last_pipe),
1227 usb_pipeout(sc->last_pipe), 0);
1228
1229 ub_state_stat_counted(sc, cmd);
1230
1da177e4 1231 } else if (cmd->state == UB_CMDST_CMD) {
2c26c9e6
PZ
1232 switch (urb->status) {
1233 case 0:
1234 break;
1235 case -EOVERFLOW:
1236 goto Bad_End;
1237 case -EPIPE:
1da177e4
LT
1238 rc = ub_submit_clear_stall(sc, cmd, sc->last_pipe);
1239 if (rc != 0) {
1240 printk(KERN_NOTICE "%s: "
f4800078
PZ
1241 "unable to submit clear (%d)\n",
1242 sc->name, rc);
1da177e4
LT
1243 /*
1244 * This is typically ENOMEM or some other such shit.
1245 * Retrying is pointless. Just do Bad End on it...
1246 */
2c26c9e6
PZ
1247 ub_state_done(sc, cmd, rc);
1248 return;
1da177e4
LT
1249 }
1250 cmd->state = UB_CMDST_CLEAR;
1251 ub_cmdtr_state(sc, cmd);
1252 return;
2c26c9e6
PZ
1253 case -ESHUTDOWN: /* unplug */
1254 case -EILSEQ: /* unplug timeout on uhci */
1255 ub_state_done(sc, cmd, -ENODEV);
1256 return;
1257 default:
1da177e4
LT
1258 goto Bad_End;
1259 }
1260 if (urb->actual_length != US_BULK_CB_WRAP_LEN) {
1da177e4
LT
1261 goto Bad_End;
1262 }
1263
a1cf96ef 1264 if (cmd->dir == UB_DIR_NONE || cmd->nsg < 1) {
1da177e4
LT
1265 ub_state_stat(sc, cmd);
1266 return;
1267 }
1268
a1cf96ef
PZ
1269 // udelay(125); // usb-storage has this
1270 ub_data_start(sc, cmd);
1da177e4
LT
1271
1272 } else if (cmd->state == UB_CMDST_DATA) {
1273 if (urb->status == -EPIPE) {
1274 rc = ub_submit_clear_stall(sc, cmd, sc->last_pipe);
1275 if (rc != 0) {
1276 printk(KERN_NOTICE "%s: "
f4800078
PZ
1277 "unable to submit clear (%d)\n",
1278 sc->name, rc);
2c26c9e6
PZ
1279 ub_state_done(sc, cmd, rc);
1280 return;
1da177e4
LT
1281 }
1282 cmd->state = UB_CMDST_CLR2STS;
1283 ub_cmdtr_state(sc, cmd);
1284 return;
1285 }
1286 if (urb->status == -EOVERFLOW) {
1287 /*
1288 * A babble? Failure, but we must transfer CSW now.
1289 */
1290 cmd->error = -EOVERFLOW; /* A cheap trick... */
a1cf96ef
PZ
1291 ub_state_stat(sc, cmd);
1292 return;
1da177e4 1293 }
2c26c9e6
PZ
1294
1295 if (cmd->dir == UB_DIR_WRITE) {
1296 /*
1297 * Do not continue writes in case of a failure.
1298 * Doing so would cause sectors to be mixed up,
1299 * which is worse than sectors lost.
1300 *
1301 * We must try to read the CSW, or many devices
1302 * get confused.
1303 */
1304 len = urb->actual_length;
1305 if (urb->status != 0 ||
1306 len != cmd->sgv[cmd->current_sg].length) {
1307 cmd->act_len += len;
1308 ub_cmdtr_act_len(sc, cmd);
1309
1310 cmd->error = -EIO;
1311 ub_state_stat(sc, cmd);
1312 return;
1313 }
1314
1315 } else {
1316 /*
1317 * If an error occurs on read, we record it, and
1318 * continue to fetch data in order to avoid bubble.
1319 *
1320 * As a small shortcut, we stop if we detect that
1321 * a CSW mixed into data.
1322 */
1323 if (urb->status != 0)
1324 cmd->error = -EIO;
1325
1326 len = urb->actual_length;
1327 if (urb->status != 0 ||
1328 len != cmd->sgv[cmd->current_sg].length) {
1329 if ((len & 0x1FF) == US_BULK_CS_WRAP_LEN)
1330 goto Bad_End;
1331 }
1332 }
1da177e4 1333
a1cf96ef 1334 cmd->act_len += urb->actual_length;
1da177e4
LT
1335 ub_cmdtr_act_len(sc, cmd);
1336
a1cf96ef
PZ
1337 if (++cmd->current_sg < cmd->nsg) {
1338 ub_data_start(sc, cmd);
1339 return;
1340 }
1da177e4
LT
1341 ub_state_stat(sc, cmd);
1342
1343 } else if (cmd->state == UB_CMDST_STAT) {
1344 if (urb->status == -EPIPE) {
1345 rc = ub_submit_clear_stall(sc, cmd, sc->last_pipe);
1346 if (rc != 0) {
1347 printk(KERN_NOTICE "%s: "
f4800078
PZ
1348 "unable to submit clear (%d)\n",
1349 sc->name, rc);
2c26c9e6
PZ
1350 ub_state_done(sc, cmd, rc);
1351 return;
1da177e4 1352 }
1872bceb
PZ
1353
1354 /*
1355 * Having a stall when getting CSW is an error, so
1356 * make sure uppper levels are not oblivious to it.
1357 */
1358 cmd->error = -EIO; /* A cheap trick... */
1359
1360 cmd->state = UB_CMDST_CLRRS;
1da177e4
LT
1361 ub_cmdtr_state(sc, cmd);
1362 return;
1363 }
2c26c9e6
PZ
1364
1365 /* Catch everything, including -EOVERFLOW and other nasties. */
1da177e4
LT
1366 if (urb->status != 0)
1367 goto Bad_End;
1368
1369 if (urb->actual_length == 0) {
1872bceb 1370 ub_state_stat_counted(sc, cmd);
1da177e4
LT
1371 return;
1372 }
1373
1374 /*
1375 * Check the returned Bulk protocol status.
1872bceb 1376 * The status block has to be validated first.
1da177e4
LT
1377 */
1378
1379 bcs = &sc->work_bcs;
1872bceb
PZ
1380
1381 if (sc->signature == cpu_to_le32(0)) {
1da177e4 1382 /*
1872bceb
PZ
1383 * This is the first reply, so do not perform the check.
1384 * Instead, remember the signature the device uses
1385 * for future checks. But do not allow a nul.
1da177e4 1386 */
1872bceb
PZ
1387 sc->signature = bcs->Signature;
1388 if (sc->signature == cpu_to_le32(0)) {
1389 ub_state_stat_counted(sc, cmd);
1390 return;
1391 }
1392 } else {
1393 if (bcs->Signature != sc->signature) {
1394 ub_state_stat_counted(sc, cmd);
1395 return;
1396 }
1da177e4 1397 }
1da177e4
LT
1398
1399 if (bcs->Tag != cmd->tag) {
1400 /*
1401 * This usually happens when we disagree with the
1402 * device's microcode about something. For instance,
1403 * a few of them throw this after timeouts. They buffer
1404 * commands and reply at commands we timed out before.
1405 * Without flushing these replies we loop forever.
1406 */
1872bceb 1407 ub_state_stat_counted(sc, cmd);
1da177e4
LT
1408 return;
1409 }
1410
2c26c9e6
PZ
1411 len = le32_to_cpu(bcs->Residue);
1412 if (len != cmd->len - cmd->act_len) {
1872bceb
PZ
1413 /*
1414 * It is all right to transfer less, the caller has
1415 * to check. But it's not all right if the device
1416 * counts disagree with our counts.
1417 */
1418 /* P3 */ printk("%s: resid %d len %d act %d\n",
2c26c9e6 1419 sc->name, len, cmd->len, cmd->act_len);
1872bceb
PZ
1420 goto Bad_End;
1421 }
1422
1da177e4
LT
1423 switch (bcs->Status) {
1424 case US_BULK_STAT_OK:
1425 break;
1426 case US_BULK_STAT_FAIL:
1427 ub_state_sense(sc, cmd);
1428 return;
1429 case US_BULK_STAT_PHASE:
1da177e4
LT
1430 /* P3 */ printk("%s: status PHASE\n", sc->name);
1431 goto Bad_End;
1432 default:
1433 printk(KERN_INFO "%s: unknown CSW status 0x%x\n",
1434 sc->name, bcs->Status);
2c26c9e6
PZ
1435 ub_state_done(sc, cmd, -EINVAL);
1436 return;
1da177e4
LT
1437 }
1438
1439 /* Not zeroing error to preserve a babble indicator */
1872bceb
PZ
1440 if (cmd->error != 0) {
1441 ub_state_sense(sc, cmd);
1442 return;
1443 }
1da177e4
LT
1444 cmd->state = UB_CMDST_DONE;
1445 ub_cmdtr_state(sc, cmd);
1446 ub_cmdq_pop(sc);
1447 (*cmd->done)(sc, cmd);
1448
1449 } else if (cmd->state == UB_CMDST_SENSE) {
1450 ub_state_done(sc, cmd, -EIO);
1451
1452 } else {
1453 printk(KERN_WARNING "%s: "
f4800078
PZ
1454 "wrong command state %d\n",
1455 sc->name, cmd->state);
2c26c9e6
PZ
1456 ub_state_done(sc, cmd, -EINVAL);
1457 return;
1da177e4
LT
1458 }
1459 return;
1460
1461Bad_End: /* Little Excel is dead */
1462 ub_state_done(sc, cmd, -EIO);
1463}
1464
a1cf96ef
PZ
1465/*
1466 * Factorization helper for the command state machine:
1467 * Initiate a data segment transfer.
1468 */
1469static void ub_data_start(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1470{
1471 struct scatterlist *sg = &cmd->sgv[cmd->current_sg];
1472 int pipe;
1473 int rc;
1474
1475 UB_INIT_COMPLETION(sc->work_done);
1476
1477 if (cmd->dir == UB_DIR_READ)
1478 pipe = sc->recv_bulk_pipe;
1479 else
1480 pipe = sc->send_bulk_pipe;
1481 sc->last_pipe = pipe;
1482 usb_fill_bulk_urb(&sc->work_urb, sc->dev, pipe,
1483 page_address(sg->page) + sg->offset, sg->length,
1484 ub_urb_complete, sc);
a1cf96ef
PZ
1485 sc->work_urb.actual_length = 0;
1486 sc->work_urb.error_count = 0;
1487 sc->work_urb.status = 0;
1488
1489 if ((rc = usb_submit_urb(&sc->work_urb, GFP_ATOMIC)) != 0) {
1490 /* XXX Clear stalls */
a1cf96ef
PZ
1491 ub_complete(&sc->work_done);
1492 ub_state_done(sc, cmd, rc);
1493 return;
1494 }
1495
1496 sc->work_timer.expires = jiffies + UB_DATA_TIMEOUT;
1497 add_timer(&sc->work_timer);
1498
1499 cmd->state = UB_CMDST_DATA;
1500 ub_cmdtr_state(sc, cmd);
1501}
1502
1da177e4
LT
1503/*
1504 * Factorization helper for the command state machine:
1505 * Finish the command.
1506 */
1507static void ub_state_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd, int rc)
1508{
1509
1510 cmd->error = rc;
1511 cmd->state = UB_CMDST_DONE;
1512 ub_cmdtr_state(sc, cmd);
1513 ub_cmdq_pop(sc);
1514 (*cmd->done)(sc, cmd);
1515}
1516
1517/*
1518 * Factorization helper for the command state machine:
1519 * Submit a CSW read.
1520 */
1872bceb 1521static int __ub_state_stat(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1da177e4
LT
1522{
1523 int rc;
1524
1525 UB_INIT_COMPLETION(sc->work_done);
1526
1527 sc->last_pipe = sc->recv_bulk_pipe;
1528 usb_fill_bulk_urb(&sc->work_urb, sc->dev, sc->recv_bulk_pipe,
1529 &sc->work_bcs, US_BULK_CS_WRAP_LEN, ub_urb_complete, sc);
1da177e4
LT
1530 sc->work_urb.actual_length = 0;
1531 sc->work_urb.error_count = 0;
1532 sc->work_urb.status = 0;
1533
1534 if ((rc = usb_submit_urb(&sc->work_urb, GFP_ATOMIC)) != 0) {
1535 /* XXX Clear stalls */
1da177e4
LT
1536 ub_complete(&sc->work_done);
1537 ub_state_done(sc, cmd, rc);
1872bceb 1538 return -1;
1da177e4
LT
1539 }
1540
1541 sc->work_timer.expires = jiffies + UB_STAT_TIMEOUT;
1542 add_timer(&sc->work_timer);
1872bceb 1543 return 0;
1da177e4
LT
1544}
1545
1546/*
1547 * Factorization helper for the command state machine:
1548 * Submit a CSW read and go to STAT state.
1549 */
1550static void ub_state_stat(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1551{
1872bceb
PZ
1552
1553 if (__ub_state_stat(sc, cmd) != 0)
1554 return;
1da177e4
LT
1555
1556 cmd->stat_count = 0;
1557 cmd->state = UB_CMDST_STAT;
1872bceb
PZ
1558 ub_cmdtr_state(sc, cmd);
1559}
1560
1561/*
1562 * Factorization helper for the command state machine:
1563 * Submit a CSW read and go to STAT state with counter (along [C] path).
1564 */
1565static void ub_state_stat_counted(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1566{
1567
1568 if (++cmd->stat_count >= 4) {
1569 ub_state_sense(sc, cmd);
1570 return;
1571 }
1572
1573 if (__ub_state_stat(sc, cmd) != 0)
1574 return;
1575
1576 cmd->state = UB_CMDST_STAT;
1da177e4
LT
1577 ub_cmdtr_state(sc, cmd);
1578}
1579
1580/*
1581 * Factorization helper for the command state machine:
1582 * Submit a REQUEST SENSE and go to SENSE state.
1583 */
1584static void ub_state_sense(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1585{
1586 struct ub_scsi_cmd *scmd;
a1cf96ef 1587 struct scatterlist *sg;
1da177e4
LT
1588 int rc;
1589
1590 if (cmd->cdb[0] == REQUEST_SENSE) {
1591 rc = -EPIPE;
1592 goto error;
1593 }
1594
1595 scmd = &sc->top_rqs_cmd;
a1cf96ef 1596 memset(scmd, 0, sizeof(struct ub_scsi_cmd));
1da177e4
LT
1597 scmd->cdb[0] = REQUEST_SENSE;
1598 scmd->cdb[4] = UB_SENSE_SIZE;
1599 scmd->cdb_len = 6;
1600 scmd->dir = UB_DIR_READ;
1601 scmd->state = UB_CMDST_INIT;
a1cf96ef
PZ
1602 scmd->nsg = 1;
1603 sg = &scmd->sgv[0];
1604 sg->page = virt_to_page(sc->top_sense);
38ffdd62 1605 sg->offset = (unsigned long)sc->top_sense & (PAGE_SIZE-1);
a1cf96ef 1606 sg->length = UB_SENSE_SIZE;
1da177e4 1607 scmd->len = UB_SENSE_SIZE;
f4800078 1608 scmd->lun = cmd->lun;
1da177e4
LT
1609 scmd->done = ub_top_sense_done;
1610 scmd->back = cmd;
1611
1612 scmd->tag = sc->tagcnt++;
1613
1614 cmd->state = UB_CMDST_SENSE;
1615 ub_cmdtr_state(sc, cmd);
1616
1617 ub_cmdq_insert(sc, scmd);
1618 return;
1619
1620error:
1621 ub_state_done(sc, cmd, rc);
1622}
1623
1624/*
1625 * A helper for the command's state machine:
1626 * Submit a stall clear.
1627 */
1628static int ub_submit_clear_stall(struct ub_dev *sc, struct ub_scsi_cmd *cmd,
1629 int stalled_pipe)
1630{
1631 int endp;
1632 struct usb_ctrlrequest *cr;
1633 int rc;
1634
1635 endp = usb_pipeendpoint(stalled_pipe);
1636 if (usb_pipein (stalled_pipe))
1637 endp |= USB_DIR_IN;
1638
1639 cr = &sc->work_cr;
1640 cr->bRequestType = USB_RECIP_ENDPOINT;
1641 cr->bRequest = USB_REQ_CLEAR_FEATURE;
1642 cr->wValue = cpu_to_le16(USB_ENDPOINT_HALT);
1643 cr->wIndex = cpu_to_le16(endp);
1644 cr->wLength = cpu_to_le16(0);
1645
1646 UB_INIT_COMPLETION(sc->work_done);
1647
1648 usb_fill_control_urb(&sc->work_urb, sc->dev, sc->send_ctrl_pipe,
1649 (unsigned char*) cr, NULL, 0, ub_urb_complete, sc);
1da177e4
LT
1650 sc->work_urb.actual_length = 0;
1651 sc->work_urb.error_count = 0;
1652 sc->work_urb.status = 0;
1653
1654 if ((rc = usb_submit_urb(&sc->work_urb, GFP_ATOMIC)) != 0) {
1655 ub_complete(&sc->work_done);
1656 return rc;
1657 }
1658
1659 sc->work_timer.expires = jiffies + UB_CTRL_TIMEOUT;
1660 add_timer(&sc->work_timer);
1661 return 0;
1662}
1663
1664/*
1665 */
1666static void ub_top_sense_done(struct ub_dev *sc, struct ub_scsi_cmd *scmd)
1667{
a1cf96ef 1668 unsigned char *sense = sc->top_sense;
1da177e4
LT
1669 struct ub_scsi_cmd *cmd;
1670
1671 /*
1672 * Ignoring scmd->act_len, because the buffer was pre-zeroed.
1673 */
1674 ub_cmdtr_sense(sc, scmd, sense);
1675
1676 /*
1677 * Find the command which triggered the unit attention or a check,
1678 * save the sense into it, and advance its state machine.
1679 */
1680 if ((cmd = ub_cmdq_peek(sc)) == NULL) {
1681 printk(KERN_WARNING "%s: sense done while idle\n", sc->name);
1682 return;
1683 }
1684 if (cmd != scmd->back) {
1685 printk(KERN_WARNING "%s: "
f4800078
PZ
1686 "sense done for wrong command 0x%x\n",
1687 sc->name, cmd->tag);
1da177e4
LT
1688 return;
1689 }
1690 if (cmd->state != UB_CMDST_SENSE) {
1691 printk(KERN_WARNING "%s: "
f4800078
PZ
1692 "sense done with bad cmd state %d\n",
1693 sc->name, cmd->state);
1da177e4
LT
1694 return;
1695 }
1696
1697 cmd->key = sense[2] & 0x0F;
1698 cmd->asc = sense[12];
1699 cmd->ascq = sense[13];
1700
1701 ub_scsi_urb_compl(sc, cmd);
1702}
1703
2c26c9e6
PZ
1704/*
1705 * Reset management
1706 */
1707
1708static void ub_reset_enter(struct ub_dev *sc)
1709{
1710
1711 if (sc->reset) {
1712 /* This happens often on multi-LUN devices. */
1713 return;
1714 }
1715 sc->reset = 1;
1716
1717#if 0 /* Not needed because the disconnect waits for us. */
1718 unsigned long flags;
1719 spin_lock_irqsave(&ub_lock, flags);
1720 sc->openc++;
1721 spin_unlock_irqrestore(&ub_lock, flags);
1722#endif
1723
1724#if 0 /* We let them stop themselves. */
1725 struct list_head *p;
1726 struct ub_lun *lun;
1727 list_for_each(p, &sc->luns) {
1728 lun = list_entry(p, struct ub_lun, link);
1729 blk_stop_queue(lun->disk->queue);
1730 }
1731#endif
1732
1733 schedule_work(&sc->reset_work);
1734}
1735
1736static void ub_reset_task(void *arg)
1737{
1738 struct ub_dev *sc = arg;
1739 unsigned long flags;
1740 struct list_head *p;
1741 struct ub_lun *lun;
1742 int lkr, rc;
1743
1744 if (!sc->reset) {
1745 printk(KERN_WARNING "%s: Running reset unrequested\n",
1746 sc->name);
1747 return;
1748 }
1749
1750 if (atomic_read(&sc->poison)) {
1751 printk(KERN_NOTICE "%s: Not resetting disconnected device\n",
1752 sc->name); /* P3 This floods. Remove soon. XXX */
1753 } else if (sc->dev->actconfig->desc.bNumInterfaces != 1) {
1754 printk(KERN_NOTICE "%s: Not resetting multi-interface device\n",
1755 sc->name); /* P3 This floods. Remove soon. XXX */
1756 } else {
1757 if ((lkr = usb_lock_device_for_reset(sc->dev, sc->intf)) < 0) {
1758 printk(KERN_NOTICE
1759 "%s: usb_lock_device_for_reset failed (%d)\n",
1760 sc->name, lkr);
1761 } else {
1762 rc = usb_reset_device(sc->dev);
1763 if (rc < 0) {
1764 printk(KERN_NOTICE "%s: "
1765 "usb_lock_device_for_reset failed (%d)\n",
1766 sc->name, rc);
1767 }
1768
1769 if (lkr)
1770 usb_unlock_device(sc->dev);
1771 }
1772 }
1773
1774 /*
1775 * In theory, no commands can be running while reset is active,
1776 * so nobody can ask for another reset, and so we do not need any
1777 * queues of resets or anything. We do need a spinlock though,
1778 * to interact with block layer.
1779 */
65b4fe55 1780 spin_lock_irqsave(sc->lock, flags);
2c26c9e6
PZ
1781 sc->reset = 0;
1782 tasklet_schedule(&sc->tasklet);
1783 list_for_each(p, &sc->luns) {
1784 lun = list_entry(p, struct ub_lun, link);
1785 blk_start_queue(lun->disk->queue);
1786 }
1787 wake_up(&sc->reset_wait);
65b4fe55 1788 spin_unlock_irqrestore(sc->lock, flags);
2c26c9e6
PZ
1789}
1790
1da177e4
LT
1791/*
1792 * This is called from a process context.
1793 */
f4800078 1794static void ub_revalidate(struct ub_dev *sc, struct ub_lun *lun)
1da177e4
LT
1795{
1796
f4800078 1797 lun->readonly = 0; /* XXX Query this from the device */
1da177e4 1798
f4800078
PZ
1799 lun->capacity.nsec = 0;
1800 lun->capacity.bsize = 512;
1801 lun->capacity.bshift = 0;
1da177e4 1802
f4800078 1803 if (ub_sync_tur(sc, lun) != 0)
1da177e4 1804 return; /* Not ready */
f4800078 1805 lun->changed = 0;
1da177e4 1806
f4800078 1807 if (ub_sync_read_cap(sc, lun, &lun->capacity) != 0) {
1da177e4
LT
1808 /*
1809 * The retry here means something is wrong, either with the
1810 * device, with the transport, or with our code.
1811 * We keep this because sd.c has retries for capacity.
1812 */
f4800078
PZ
1813 if (ub_sync_read_cap(sc, lun, &lun->capacity) != 0) {
1814 lun->capacity.nsec = 0;
1815 lun->capacity.bsize = 512;
1816 lun->capacity.bshift = 0;
1da177e4
LT
1817 }
1818 }
1819}
1820
1821/*
1822 * The open funcion.
1823 * This is mostly needed to keep refcounting, but also to support
1824 * media checks on removable media drives.
1825 */
1826static int ub_bd_open(struct inode *inode, struct file *filp)
1827{
1828 struct gendisk *disk = inode->i_bdev->bd_disk;
f4800078 1829 struct ub_lun *lun;
1da177e4
LT
1830 struct ub_dev *sc;
1831 unsigned long flags;
1832 int rc;
1833
f4800078 1834 if ((lun = disk->private_data) == NULL)
1da177e4 1835 return -ENXIO;
f4800078
PZ
1836 sc = lun->udev;
1837
1da177e4
LT
1838 spin_lock_irqsave(&ub_lock, flags);
1839 if (atomic_read(&sc->poison)) {
1840 spin_unlock_irqrestore(&ub_lock, flags);
1841 return -ENXIO;
1842 }
1843 sc->openc++;
1844 spin_unlock_irqrestore(&ub_lock, flags);
1845
1846 /*
1847 * This is a workaround for a specific problem in our block layer.
1848 * In 2.6.9, register_disk duplicates the code from rescan_partitions.
1849 * However, if we do add_disk with a device which persistently reports
1850 * a changed media, add_disk calls register_disk, which does do_open,
1851 * which will call rescan_paritions for changed media. After that,
1852 * register_disk attempts to do it all again and causes double kobject
1853 * registration and a eventually an oops on module removal.
1854 *
1855 * The bottom line is, Al Viro says that we should not allow
1856 * bdev->bd_invalidated to be set when doing add_disk no matter what.
1857 */
f4800078
PZ
1858 if (lun->first_open) {
1859 lun->first_open = 0;
1860 if (lun->changed) {
1da177e4
LT
1861 rc = -ENOMEDIUM;
1862 goto err_open;
1863 }
1864 }
1865
f4800078 1866 if (lun->removable || lun->readonly)
1da177e4
LT
1867 check_disk_change(inode->i_bdev);
1868
1869 /*
1870 * The sd.c considers ->media_present and ->changed not equivalent,
1871 * under some pretty murky conditions (a failure of READ CAPACITY).
1872 * We may need it one day.
1873 */
f4800078 1874 if (lun->removable && lun->changed && !(filp->f_flags & O_NDELAY)) {
1da177e4
LT
1875 rc = -ENOMEDIUM;
1876 goto err_open;
1877 }
1878
f4800078 1879 if (lun->readonly && (filp->f_mode & FMODE_WRITE)) {
1da177e4
LT
1880 rc = -EROFS;
1881 goto err_open;
1882 }
1883
1884 return 0;
1885
1886err_open:
1887 ub_put(sc);
1888 return rc;
1889}
1890
1891/*
1892 */
1893static int ub_bd_release(struct inode *inode, struct file *filp)
1894{
1895 struct gendisk *disk = inode->i_bdev->bd_disk;
f4800078
PZ
1896 struct ub_lun *lun = disk->private_data;
1897 struct ub_dev *sc = lun->udev;
1da177e4
LT
1898
1899 ub_put(sc);
1900 return 0;
1901}
1902
1903/*
1904 * The ioctl interface.
1905 */
1906static int ub_bd_ioctl(struct inode *inode, struct file *filp,
1907 unsigned int cmd, unsigned long arg)
1908{
1909 struct gendisk *disk = inode->i_bdev->bd_disk;
1910 void __user *usermem = (void __user *) arg;
1911
1912 return scsi_cmd_ioctl(filp, disk, cmd, usermem);
1913}
1914
1915/*
1916 * This is called once a new disk was seen by the block layer or by ub_probe().
1917 * The main onjective here is to discover the features of the media such as
1918 * the capacity, read-only status, etc. USB storage generally does not
1919 * need to be spun up, but if we needed it, this would be the place.
1920 *
1921 * This call can sleep.
1922 *
1923 * The return code is not used.
1924 */
1925static int ub_bd_revalidate(struct gendisk *disk)
1926{
f4800078
PZ
1927 struct ub_lun *lun = disk->private_data;
1928
1929 ub_revalidate(lun->udev, lun);
1da177e4
LT
1930
1931 /* XXX Support sector size switching like in sr.c */
f4800078
PZ
1932 blk_queue_hardsect_size(disk->queue, lun->capacity.bsize);
1933 set_capacity(disk, lun->capacity.nsec);
1934 // set_disk_ro(sdkp->disk, lun->readonly);
1da177e4
LT
1935
1936 return 0;
1937}
1938
1939/*
1940 * The check is called by the block layer to verify if the media
1941 * is still available. It is supposed to be harmless, lightweight and
1942 * non-intrusive in case the media was not changed.
1943 *
1944 * This call can sleep.
1945 *
1946 * The return code is bool!
1947 */
1948static int ub_bd_media_changed(struct gendisk *disk)
1949{
f4800078 1950 struct ub_lun *lun = disk->private_data;
1da177e4 1951
f4800078 1952 if (!lun->removable)
1da177e4
LT
1953 return 0;
1954
1955 /*
1956 * We clean checks always after every command, so this is not
1957 * as dangerous as it looks. If the TEST_UNIT_READY fails here,
1958 * the device is actually not ready with operator or software
1959 * intervention required. One dangerous item might be a drive which
1960 * spins itself down, and come the time to write dirty pages, this
1961 * will fail, then block layer discards the data. Since we never
1962 * spin drives up, such devices simply cannot be used with ub anyway.
1963 */
f4800078
PZ
1964 if (ub_sync_tur(lun->udev, lun) != 0) {
1965 lun->changed = 1;
1da177e4
LT
1966 return 1;
1967 }
1968
f4800078 1969 return lun->changed;
1da177e4
LT
1970}
1971
1972static struct block_device_operations ub_bd_fops = {
1973 .owner = THIS_MODULE,
1974 .open = ub_bd_open,
1975 .release = ub_bd_release,
1976 .ioctl = ub_bd_ioctl,
1977 .media_changed = ub_bd_media_changed,
1978 .revalidate_disk = ub_bd_revalidate,
1979};
1980
1981/*
1982 * Common ->done routine for commands executed synchronously.
1983 */
1984static void ub_probe_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1985{
1986 struct completion *cop = cmd->back;
1987 complete(cop);
1988}
1989
1990/*
1991 * Test if the device has a check condition on it, synchronously.
1992 */
f4800078 1993static int ub_sync_tur(struct ub_dev *sc, struct ub_lun *lun)
1da177e4
LT
1994{
1995 struct ub_scsi_cmd *cmd;
1996 enum { ALLOC_SIZE = sizeof(struct ub_scsi_cmd) };
1997 unsigned long flags;
1998 struct completion compl;
1999 int rc;
2000
2001 init_completion(&compl);
2002
2003 rc = -ENOMEM;
2004 if ((cmd = kmalloc(ALLOC_SIZE, GFP_KERNEL)) == NULL)
2005 goto err_alloc;
2006 memset(cmd, 0, ALLOC_SIZE);
2007
2008 cmd->cdb[0] = TEST_UNIT_READY;
2009 cmd->cdb_len = 6;
2010 cmd->dir = UB_DIR_NONE;
2011 cmd->state = UB_CMDST_INIT;
f4800078 2012 cmd->lun = lun; /* This may be NULL, but that's ok */
1da177e4
LT
2013 cmd->done = ub_probe_done;
2014 cmd->back = &compl;
2015
65b4fe55 2016 spin_lock_irqsave(sc->lock, flags);
1da177e4
LT
2017 cmd->tag = sc->tagcnt++;
2018
2019 rc = ub_submit_scsi(sc, cmd);
65b4fe55 2020 spin_unlock_irqrestore(sc->lock, flags);
1da177e4
LT
2021
2022 if (rc != 0) {
2023 printk("ub: testing ready: submit error (%d)\n", rc); /* P3 */
2024 goto err_submit;
2025 }
2026
2027 wait_for_completion(&compl);
2028
2029 rc = cmd->error;
2030
2031 if (rc == -EIO && cmd->key != 0) /* Retries for benh's key */
2032 rc = cmd->key;
2033
2034err_submit:
2035 kfree(cmd);
2036err_alloc:
2037 return rc;
2038}
2039
2040/*
2041 * Read the SCSI capacity synchronously (for probing).
2042 */
f4800078
PZ
2043static int ub_sync_read_cap(struct ub_dev *sc, struct ub_lun *lun,
2044 struct ub_capacity *ret)
1da177e4
LT
2045{
2046 struct ub_scsi_cmd *cmd;
a1cf96ef 2047 struct scatterlist *sg;
1da177e4
LT
2048 char *p;
2049 enum { ALLOC_SIZE = sizeof(struct ub_scsi_cmd) + 8 };
2050 unsigned long flags;
2051 unsigned int bsize, shift;
2052 unsigned long nsec;
2053 struct completion compl;
2054 int rc;
2055
2056 init_completion(&compl);
2057
2058 rc = -ENOMEM;
2059 if ((cmd = kmalloc(ALLOC_SIZE, GFP_KERNEL)) == NULL)
2060 goto err_alloc;
2061 memset(cmd, 0, ALLOC_SIZE);
2062 p = (char *)cmd + sizeof(struct ub_scsi_cmd);
2063
2064 cmd->cdb[0] = 0x25;
2065 cmd->cdb_len = 10;
2066 cmd->dir = UB_DIR_READ;
2067 cmd->state = UB_CMDST_INIT;
a1cf96ef
PZ
2068 cmd->nsg = 1;
2069 sg = &cmd->sgv[0];
2070 sg->page = virt_to_page(p);
38ffdd62 2071 sg->offset = (unsigned long)p & (PAGE_SIZE-1);
a1cf96ef 2072 sg->length = 8;
1da177e4 2073 cmd->len = 8;
f4800078 2074 cmd->lun = lun;
1da177e4
LT
2075 cmd->done = ub_probe_done;
2076 cmd->back = &compl;
2077
65b4fe55 2078 spin_lock_irqsave(sc->lock, flags);
1da177e4
LT
2079 cmd->tag = sc->tagcnt++;
2080
2081 rc = ub_submit_scsi(sc, cmd);
65b4fe55 2082 spin_unlock_irqrestore(sc->lock, flags);
1da177e4
LT
2083
2084 if (rc != 0) {
2085 printk("ub: reading capacity: submit error (%d)\n", rc); /* P3 */
2086 goto err_submit;
2087 }
2088
2089 wait_for_completion(&compl);
2090
2091 if (cmd->error != 0) {
2092 printk("ub: reading capacity: error %d\n", cmd->error); /* P3 */
2093 rc = -EIO;
2094 goto err_read;
2095 }
2096 if (cmd->act_len != 8) {
2097 printk("ub: reading capacity: size %d\n", cmd->act_len); /* P3 */
2098 rc = -EIO;
2099 goto err_read;
2100 }
2101
2102 /* sd.c special-cases sector size of 0 to mean 512. Needed? Safe? */
2103 nsec = be32_to_cpu(*(__be32 *)p) + 1;
2104 bsize = be32_to_cpu(*(__be32 *)(p + 4));
2105 switch (bsize) {
2106 case 512: shift = 0; break;
2107 case 1024: shift = 1; break;
2108 case 2048: shift = 2; break;
2109 case 4096: shift = 3; break;
2110 default:
2111 printk("ub: Bad sector size %u\n", bsize); /* P3 */
2112 rc = -EDOM;
2113 goto err_inv_bsize;
2114 }
2115
2116 ret->bsize = bsize;
2117 ret->bshift = shift;
2118 ret->nsec = nsec << shift;
2119 rc = 0;
2120
2121err_inv_bsize:
2122err_read:
2123err_submit:
2124 kfree(cmd);
2125err_alloc:
2126 return rc;
2127}
2128
2129/*
2130 */
2131static void ub_probe_urb_complete(struct urb *urb, struct pt_regs *pt)
2132{
2133 struct completion *cop = urb->context;
2134 complete(cop);
2135}
2136
2137static void ub_probe_timeout(unsigned long arg)
2138{
2139 struct completion *cop = (struct completion *) arg;
2140 complete(cop);
2141}
2142
f4800078
PZ
2143/*
2144 * Get number of LUNs by the way of Bulk GetMaxLUN command.
2145 */
2146static int ub_sync_getmaxlun(struct ub_dev *sc)
2147{
2148 int ifnum = sc->intf->cur_altsetting->desc.bInterfaceNumber;
2149 unsigned char *p;
2150 enum { ALLOC_SIZE = 1 };
2151 struct usb_ctrlrequest *cr;
2152 struct completion compl;
2153 struct timer_list timer;
2154 int nluns;
2155 int rc;
2156
2157 init_completion(&compl);
2158
2159 rc = -ENOMEM;
2160 if ((p = kmalloc(ALLOC_SIZE, GFP_KERNEL)) == NULL)
2161 goto err_alloc;
2162 *p = 55;
2163
2164 cr = &sc->work_cr;
2165 cr->bRequestType = USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
2166 cr->bRequest = US_BULK_GET_MAX_LUN;
2167 cr->wValue = cpu_to_le16(0);
2168 cr->wIndex = cpu_to_le16(ifnum);
2169 cr->wLength = cpu_to_le16(1);
2170
2171 usb_fill_control_urb(&sc->work_urb, sc->dev, sc->recv_ctrl_pipe,
2172 (unsigned char*) cr, p, 1, ub_probe_urb_complete, &compl);
f4800078
PZ
2173 sc->work_urb.actual_length = 0;
2174 sc->work_urb.error_count = 0;
2175 sc->work_urb.status = 0;
2176
2177 if ((rc = usb_submit_urb(&sc->work_urb, GFP_KERNEL)) != 0) {
2178 if (rc == -EPIPE) {
64bd8453 2179 printk("%s: Stall submitting GetMaxLUN, using 1 LUN\n",
f4800078
PZ
2180 sc->name); /* P3 */
2181 } else {
64bd8453 2182 printk(KERN_NOTICE
f4800078
PZ
2183 "%s: Unable to submit GetMaxLUN (%d)\n",
2184 sc->name, rc);
2185 }
2186 goto err_submit;
2187 }
2188
2189 init_timer(&timer);
2190 timer.function = ub_probe_timeout;
2191 timer.data = (unsigned long) &compl;
2192 timer.expires = jiffies + UB_CTRL_TIMEOUT;
2193 add_timer(&timer);
2194
2195 wait_for_completion(&compl);
2196
2197 del_timer_sync(&timer);
2198 usb_kill_urb(&sc->work_urb);
2199
64bd8453
PZ
2200 if ((rc = sc->work_urb.status) < 0) {
2201 if (rc == -EPIPE) {
2202 printk("%s: Stall at GetMaxLUN, using 1 LUN\n",
2203 sc->name); /* P3 */
2204 } else {
2205 printk(KERN_NOTICE
2206 "%s: Error at GetMaxLUN (%d)\n",
2207 sc->name, rc);
2208 }
2209 goto err_io;
2210 }
2211
f4800078
PZ
2212 if (sc->work_urb.actual_length != 1) {
2213 printk("%s: GetMaxLUN returned %d bytes\n", sc->name,
2214 sc->work_urb.actual_length); /* P3 */
2215 nluns = 0;
2216 } else {
2217 if ((nluns = *p) == 55) {
2218 nluns = 0;
2219 } else {
2220 /* GetMaxLUN returns the maximum LUN number */
2221 nluns += 1;
2222 if (nluns > UB_MAX_LUNS)
2223 nluns = UB_MAX_LUNS;
2224 }
2225 printk("%s: GetMaxLUN returned %d, using %d LUNs\n", sc->name,
2226 *p, nluns); /* P3 */
2227 }
2228
2229 kfree(p);
2230 return nluns;
2231
64bd8453 2232err_io:
f4800078
PZ
2233err_submit:
2234 kfree(p);
2235err_alloc:
2236 return rc;
2237}
2238
1da177e4
LT
2239/*
2240 * Clear initial stalls.
2241 */
2242static int ub_probe_clear_stall(struct ub_dev *sc, int stalled_pipe)
2243{
2244 int endp;
2245 struct usb_ctrlrequest *cr;
2246 struct completion compl;
2247 struct timer_list timer;
2248 int rc;
2249
2250 init_completion(&compl);
2251
2252 endp = usb_pipeendpoint(stalled_pipe);
2253 if (usb_pipein (stalled_pipe))
2254 endp |= USB_DIR_IN;
2255
2256 cr = &sc->work_cr;
2257 cr->bRequestType = USB_RECIP_ENDPOINT;
2258 cr->bRequest = USB_REQ_CLEAR_FEATURE;
2259 cr->wValue = cpu_to_le16(USB_ENDPOINT_HALT);
2260 cr->wIndex = cpu_to_le16(endp);
2261 cr->wLength = cpu_to_le16(0);
2262
2263 usb_fill_control_urb(&sc->work_urb, sc->dev, sc->send_ctrl_pipe,
2264 (unsigned char*) cr, NULL, 0, ub_probe_urb_complete, &compl);
1da177e4
LT
2265 sc->work_urb.actual_length = 0;
2266 sc->work_urb.error_count = 0;
2267 sc->work_urb.status = 0;
2268
2269 if ((rc = usb_submit_urb(&sc->work_urb, GFP_KERNEL)) != 0) {
2270 printk(KERN_WARNING
2271 "%s: Unable to submit a probe clear (%d)\n", sc->name, rc);
2272 return rc;
2273 }
2274
2275 init_timer(&timer);
2276 timer.function = ub_probe_timeout;
2277 timer.data = (unsigned long) &compl;
2278 timer.expires = jiffies + UB_CTRL_TIMEOUT;
2279 add_timer(&timer);
2280
2281 wait_for_completion(&compl);
2282
2283 del_timer_sync(&timer);
2284 usb_kill_urb(&sc->work_urb);
2285
2286 /* reset the endpoint toggle */
2287 usb_settoggle(sc->dev, endp, usb_pipeout(sc->last_pipe), 0);
2288
2289 return 0;
2290}
2291
2292/*
2293 * Get the pipe settings.
2294 */
2295static int ub_get_pipes(struct ub_dev *sc, struct usb_device *dev,
2296 struct usb_interface *intf)
2297{
2298 struct usb_host_interface *altsetting = intf->cur_altsetting;
2299 struct usb_endpoint_descriptor *ep_in = NULL;
2300 struct usb_endpoint_descriptor *ep_out = NULL;
2301 struct usb_endpoint_descriptor *ep;
2302 int i;
2303
2304 /*
2305 * Find the endpoints we need.
2306 * We are expecting a minimum of 2 endpoints - in and out (bulk).
2307 * We will ignore any others.
2308 */
2309 for (i = 0; i < altsetting->desc.bNumEndpoints; i++) {
2310 ep = &altsetting->endpoint[i].desc;
2311
2312 /* Is it a BULK endpoint? */
2313 if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
2314 == USB_ENDPOINT_XFER_BULK) {
2315 /* BULK in or out? */
2316 if (ep->bEndpointAddress & USB_DIR_IN)
2317 ep_in = ep;
2318 else
2319 ep_out = ep;
2320 }
2321 }
2322
2323 if (ep_in == NULL || ep_out == NULL) {
f4800078
PZ
2324 printk(KERN_NOTICE "%s: failed endpoint check\n",
2325 sc->name);
2c26c9e6 2326 return -ENODEV;
1da177e4
LT
2327 }
2328
2329 /* Calculate and store the pipe values */
2330 sc->send_ctrl_pipe = usb_sndctrlpipe(dev, 0);
2331 sc->recv_ctrl_pipe = usb_rcvctrlpipe(dev, 0);
2332 sc->send_bulk_pipe = usb_sndbulkpipe(dev,
2333 ep_out->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
2334 sc->recv_bulk_pipe = usb_rcvbulkpipe(dev,
2335 ep_in->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
2336
2337 return 0;
2338}
2339
2340/*
2341 * Probing is done in the process context, which allows us to cheat
2342 * and not to build a state machine for the discovery.
2343 */
2344static int ub_probe(struct usb_interface *intf,
2345 const struct usb_device_id *dev_id)
2346{
2347 struct ub_dev *sc;
f4800078 2348 int nluns;
1da177e4
LT
2349 int rc;
2350 int i;
2351
a00828e9
PZ
2352 if (usb_usual_check_type(dev_id, USB_US_TYPE_UB))
2353 return -ENXIO;
2354
1da177e4
LT
2355 rc = -ENOMEM;
2356 if ((sc = kmalloc(sizeof(struct ub_dev), GFP_KERNEL)) == NULL)
2357 goto err_core;
2358 memset(sc, 0, sizeof(struct ub_dev));
65b4fe55 2359 sc->lock = ub_next_lock();
f4800078 2360 INIT_LIST_HEAD(&sc->luns);
1da177e4
LT
2361 usb_init_urb(&sc->work_urb);
2362 tasklet_init(&sc->tasklet, ub_scsi_action, (unsigned long)sc);
2363 atomic_set(&sc->poison, 0);
2c26c9e6
PZ
2364 INIT_WORK(&sc->reset_work, ub_reset_task, sc);
2365 init_waitqueue_head(&sc->reset_wait);
1da177e4
LT
2366
2367 init_timer(&sc->work_timer);
2368 sc->work_timer.data = (unsigned long) sc;
2369 sc->work_timer.function = ub_urb_timeout;
2370
2371 ub_init_completion(&sc->work_done);
2372 sc->work_done.done = 1; /* A little yuk, but oh well... */
2373
1da177e4
LT
2374 sc->dev = interface_to_usbdev(intf);
2375 sc->intf = intf;
2376 // sc->ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
1da177e4
LT
2377 usb_set_intfdata(intf, sc);
2378 usb_get_dev(sc->dev);
2379 // usb_get_intf(sc->intf); /* Do we need this? */
2380
f4800078
PZ
2381 snprintf(sc->name, 12, DRV_NAME "(%d.%d)",
2382 sc->dev->bus->busnum, sc->dev->devnum);
2383
1da177e4
LT
2384 /* XXX Verify that we can handle the device (from descriptors) */
2385
2c26c9e6
PZ
2386 if (ub_get_pipes(sc, sc->dev, intf) != 0)
2387 goto err_dev_desc;
1da177e4
LT
2388
2389 if (device_create_file(&sc->intf->dev, &dev_attr_diag) != 0)
2390 goto err_diag;
2391
2392 /*
2393 * At this point, all USB initialization is done, do upper layer.
2394 * We really hate halfway initialized structures, so from the
2395 * invariants perspective, this ub_dev is fully constructed at
2396 * this point.
2397 */
2398
2399 /*
2400 * This is needed to clear toggles. It is a problem only if we do
2401 * `rmmod ub && modprobe ub` without disconnects, but we like that.
2402 */
c6c88834 2403#if 0 /* iPod Mini fails if we do this (big white iPod works) */
1da177e4
LT
2404 ub_probe_clear_stall(sc, sc->recv_bulk_pipe);
2405 ub_probe_clear_stall(sc, sc->send_bulk_pipe);
c6c88834 2406#endif
1da177e4
LT
2407
2408 /*
2409 * The way this is used by the startup code is a little specific.
2410 * A SCSI check causes a USB stall. Our common case code sees it
2411 * and clears the check, after which the device is ready for use.
2412 * But if a check was not present, any command other than
2413 * TEST_UNIT_READY ends with a lockup (including REQUEST_SENSE).
2414 *
2415 * If we neglect to clear the SCSI check, the first real command fails
2416 * (which is the capacity readout). We clear that and retry, but why
2417 * causing spurious retries for no reason.
2418 *
2419 * Revalidation may start with its own TEST_UNIT_READY, but that one
2420 * has to succeed, so we clear checks with an additional one here.
2421 * In any case it's not our business how revaliadation is implemented.
2422 */
2423 for (i = 0; i < 3; i++) { /* Retries for benh's key */
f4800078 2424 if ((rc = ub_sync_tur(sc, NULL)) <= 0) break;
1da177e4
LT
2425 if (rc != 0x6) break;
2426 msleep(10);
2427 }
2428
f4800078
PZ
2429 nluns = 1;
2430 for (i = 0; i < 3; i++) {
2431 if ((rc = ub_sync_getmaxlun(sc)) < 0) {
2432 /*
64bd8453
PZ
2433 * This segment is taken from usb-storage. They say
2434 * that ZIP-100 needs this, but my own ZIP-100 works
2435 * fine without this.
2436 * Still, it does not seem to hurt anything.
f4800078
PZ
2437 */
2438 if (rc == -EPIPE) {
2439 ub_probe_clear_stall(sc, sc->recv_bulk_pipe);
2440 ub_probe_clear_stall(sc, sc->send_bulk_pipe);
2441 }
2442 break;
2443 }
2444 if (rc != 0) {
2445 nluns = rc;
2446 break;
2447 }
9f793d2c 2448 msleep(100);
f4800078 2449 }
1da177e4 2450
f4800078
PZ
2451 for (i = 0; i < nluns; i++) {
2452 ub_probe_lun(sc, i);
2453 }
2454 return 0;
2455
2456 /* device_remove_file(&sc->intf->dev, &dev_attr_diag); */
2457err_diag:
2c26c9e6 2458err_dev_desc:
f4800078
PZ
2459 usb_set_intfdata(intf, NULL);
2460 // usb_put_intf(sc->intf);
2461 usb_put_dev(sc->dev);
2462 kfree(sc);
2463err_core:
2464 return rc;
2465}
2466
2467static int ub_probe_lun(struct ub_dev *sc, int lnum)
2468{
2469 struct ub_lun *lun;
2470 request_queue_t *q;
2471 struct gendisk *disk;
2472 int rc;
2473
2474 rc = -ENOMEM;
2475 if ((lun = kmalloc(sizeof(struct ub_lun), GFP_KERNEL)) == NULL)
2476 goto err_alloc;
2477 memset(lun, 0, sizeof(struct ub_lun));
2478 lun->num = lnum;
2479
2480 rc = -ENOSR;
2481 if ((lun->id = ub_id_get()) == -1)
2482 goto err_id;
2483
2484 lun->udev = sc;
2485 list_add(&lun->link, &sc->luns);
2486
2487 snprintf(lun->name, 16, DRV_NAME "%c(%d.%d.%d)",
2488 lun->id + 'a', sc->dev->bus->busnum, sc->dev->devnum, lun->num);
2489
2490 lun->removable = 1; /* XXX Query this from the device */
2491 lun->changed = 1; /* ub_revalidate clears only */
2492 lun->first_open = 1;
2493 ub_revalidate(sc, lun);
1da177e4 2494
1da177e4 2495 rc = -ENOMEM;
4fb729f5 2496 if ((disk = alloc_disk(UB_PARTS_PER_LUN)) == NULL)
1da177e4
LT
2497 goto err_diskalloc;
2498
f4800078
PZ
2499 lun->disk = disk;
2500 sprintf(disk->disk_name, DRV_NAME "%c", lun->id + 'a');
2501 sprintf(disk->devfs_name, DEVFS_NAME "/%c", lun->id + 'a');
1da177e4 2502 disk->major = UB_MAJOR;
4fb729f5 2503 disk->first_minor = lun->id * UB_PARTS_PER_LUN;
1da177e4 2504 disk->fops = &ub_bd_fops;
f4800078 2505 disk->private_data = lun;
64bd8453 2506 disk->driverfs_dev = &sc->intf->dev;
1da177e4
LT
2507
2508 rc = -ENOMEM;
65b4fe55 2509 if ((q = blk_init_queue(ub_request_fn, sc->lock)) == NULL)
1da177e4
LT
2510 goto err_blkqinit;
2511
2512 disk->queue = q;
2513
f4800078 2514 blk_queue_bounce_limit(q, BLK_BOUNCE_HIGH);
1da177e4
LT
2515 blk_queue_max_hw_segments(q, UB_MAX_REQ_SG);
2516 blk_queue_max_phys_segments(q, UB_MAX_REQ_SG);
f4800078 2517 blk_queue_segment_boundary(q, 0xffffffff); /* Dubious. */
1da177e4 2518 blk_queue_max_sectors(q, UB_MAX_SECTORS);
f4800078 2519 blk_queue_hardsect_size(q, lun->capacity.bsize);
1da177e4 2520
f4800078 2521 q->queuedata = lun;
1da177e4 2522
f4800078
PZ
2523 set_capacity(disk, lun->capacity.nsec);
2524 if (lun->removable)
1da177e4
LT
2525 disk->flags |= GENHD_FL_REMOVABLE;
2526
2527 add_disk(disk);
2528
2529 return 0;
2530
2531err_blkqinit:
2532 put_disk(disk);
2533err_diskalloc:
f4800078
PZ
2534 list_del(&lun->link);
2535 ub_id_put(lun->id);
1da177e4 2536err_id:
f4800078
PZ
2537 kfree(lun);
2538err_alloc:
1da177e4
LT
2539 return rc;
2540}
2541
2542static void ub_disconnect(struct usb_interface *intf)
2543{
2544 struct ub_dev *sc = usb_get_intfdata(intf);
f4800078
PZ
2545 struct list_head *p;
2546 struct ub_lun *lun;
2547 struct gendisk *disk;
1da177e4
LT
2548 unsigned long flags;
2549
2550 /*
2551 * Prevent ub_bd_release from pulling the rug from under us.
2552 * XXX This is starting to look like a kref.
2553 * XXX Why not to take this ref at probe time?
2554 */
2555 spin_lock_irqsave(&ub_lock, flags);
2556 sc->openc++;
2557 spin_unlock_irqrestore(&ub_lock, flags);
2558
2559 /*
2560 * Fence stall clearnings, operations triggered by unlinkings and so on.
2561 * We do not attempt to unlink any URBs, because we do not trust the
2562 * unlink paths in HC drivers. Also, we get -84 upon disconnect anyway.
2563 */
2564 atomic_set(&sc->poison, 1);
2565
2c26c9e6
PZ
2566 /*
2567 * Wait for reset to end, if any.
2568 */
2569 wait_event(sc->reset_wait, !sc->reset);
2570
1da177e4
LT
2571 /*
2572 * Blow away queued commands.
2573 *
2574 * Actually, this never works, because before we get here
2575 * the HCD terminates outstanding URB(s). It causes our
2576 * SCSI command queue to advance, commands fail to submit,
2577 * and the whole queue drains. So, we just use this code to
2578 * print warnings.
2579 */
65b4fe55 2580 spin_lock_irqsave(sc->lock, flags);
1da177e4
LT
2581 {
2582 struct ub_scsi_cmd *cmd;
2583 int cnt = 0;
2c26c9e6 2584 while ((cmd = ub_cmdq_peek(sc)) != NULL) {
1da177e4
LT
2585 cmd->error = -ENOTCONN;
2586 cmd->state = UB_CMDST_DONE;
2587 ub_cmdtr_state(sc, cmd);
2588 ub_cmdq_pop(sc);
2589 (*cmd->done)(sc, cmd);
2590 cnt++;
2591 }
2592 if (cnt != 0) {
2593 printk(KERN_WARNING "%s: "
2594 "%d was queued after shutdown\n", sc->name, cnt);
2595 }
2596 }
65b4fe55 2597 spin_unlock_irqrestore(sc->lock, flags);
1da177e4
LT
2598
2599 /*
2600 * Unregister the upper layer.
2601 */
f4800078
PZ
2602 list_for_each (p, &sc->luns) {
2603 lun = list_entry(p, struct ub_lun, link);
2604 disk = lun->disk;
2605 if (disk->flags & GENHD_FL_UP)
2606 del_gendisk(disk);
2607 /*
2608 * I wish I could do:
2609 * set_bit(QUEUE_FLAG_DEAD, &q->queue_flags);
2610 * As it is, we rely on our internal poisoning and let
2611 * the upper levels to spin furiously failing all the I/O.
2612 */
2613 }
1da177e4
LT
2614
2615 /*
1da177e4
LT
2616 * Testing for -EINPROGRESS is always a bug, so we are bending
2617 * the rules a little.
2618 */
65b4fe55 2619 spin_lock_irqsave(sc->lock, flags);
1da177e4
LT
2620 if (sc->work_urb.status == -EINPROGRESS) { /* janitors: ignore */
2621 printk(KERN_WARNING "%s: "
2622 "URB is active after disconnect\n", sc->name);
2623 }
65b4fe55 2624 spin_unlock_irqrestore(sc->lock, flags);
1da177e4
LT
2625
2626 /*
2627 * There is virtually no chance that other CPU runs times so long
2628 * after ub_urb_complete should have called del_timer, but only if HCD
2629 * didn't forget to deliver a callback on unlink.
2630 */
2631 del_timer_sync(&sc->work_timer);
2632
2633 /*
2634 * At this point there must be no commands coming from anyone
2635 * and no URBs left in transit.
2636 */
2637
2638 device_remove_file(&sc->intf->dev, &dev_attr_diag);
2639 usb_set_intfdata(intf, NULL);
2640 // usb_put_intf(sc->intf);
2641 sc->intf = NULL;
2642 usb_put_dev(sc->dev);
2643 sc->dev = NULL;
2644
2645 ub_put(sc);
2646}
2647
2648static struct usb_driver ub_driver = {
1da177e4
LT
2649 .name = "ub",
2650 .probe = ub_probe,
2651 .disconnect = ub_disconnect,
2652 .id_table = ub_usb_ids,
2653};
2654
2655static int __init ub_init(void)
2656{
2657 int rc;
65b4fe55
PZ
2658 int i;
2659
2660 for (i = 0; i < UB_QLOCK_NUM; i++)
2661 spin_lock_init(&ub_qlockv[i]);
1da177e4 2662
1da177e4
LT
2663 if ((rc = register_blkdev(UB_MAJOR, DRV_NAME)) != 0)
2664 goto err_regblkdev;
2665 devfs_mk_dir(DEVFS_NAME);
2666
2667 if ((rc = usb_register(&ub_driver)) != 0)
2668 goto err_register;
2669
a00828e9 2670 usb_usual_set_present(USB_US_TYPE_UB);
1da177e4
LT
2671 return 0;
2672
2673err_register:
2674 devfs_remove(DEVFS_NAME);
2675 unregister_blkdev(UB_MAJOR, DRV_NAME);
2676err_regblkdev:
2677 return rc;
2678}
2679
2680static void __exit ub_exit(void)
2681{
2682 usb_deregister(&ub_driver);
2683
2684 devfs_remove(DEVFS_NAME);
2685 unregister_blkdev(UB_MAJOR, DRV_NAME);
a00828e9 2686 usb_usual_clear_present(USB_US_TYPE_UB);
1da177e4
LT
2687}
2688
2689module_init(ub_init);
2690module_exit(ub_exit);
2691
2692MODULE_LICENSE("GPL");