Commit | Line | Data |
---|---|---|
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 */ | |
128 | struct 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 */ | |
144 | struct 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 | */ | |
163 | struct 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 | ||
188 | enum 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 | ||
200 | static 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 | ||
212 | struct 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 |
238 | struct 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 | */ | |
247 | struct 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 | ||
260 | struct 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 | ||
270 | struct 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 | */ | |
284 | struct ub_completion { | |
285 | unsigned int done; | |
286 | spinlock_t lock; | |
287 | }; | |
288 | ||
289 | static 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 | ||
297 | static 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 | ||
306 | static 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 | */ | |
319 | struct 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 | */ | |
327 | struct 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 | */ |
357 | struct ub_dev { | |
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 | */ | |
399 | static void ub_cleanup(struct ub_dev *sc); | |
6c1eb8c1 | 400 | static int ub_request_fn_1(struct ub_lun *lun, struct request *rq); |
2c26c9e6 PZ |
401 | static void ub_cmd_build_block(struct ub_dev *sc, struct ub_lun *lun, |
402 | struct ub_scsi_cmd *cmd, struct ub_request *urq); | |
403 | static 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 |
405 | static void ub_rw_cmd_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd); |
406 | static void ub_end_rq(struct request *rq, int uptodate); | |
2c26c9e6 PZ |
407 | static 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 |
409 | static int ub_submit_scsi(struct ub_dev *sc, struct ub_scsi_cmd *cmd); |
410 | static void ub_urb_complete(struct urb *urb, struct pt_regs *pt); | |
411 | static void ub_scsi_action(unsigned long _dev); | |
412 | static void ub_scsi_dispatch(struct ub_dev *sc); | |
413 | static void ub_scsi_urb_compl(struct ub_dev *sc, struct ub_scsi_cmd *cmd); | |
a1cf96ef | 414 | static void ub_data_start(struct ub_dev *sc, struct ub_scsi_cmd *cmd); |
1da177e4 | 415 | static void ub_state_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd, int rc); |
1872bceb | 416 | static int __ub_state_stat(struct ub_dev *sc, struct ub_scsi_cmd *cmd); |
1da177e4 | 417 | static void ub_state_stat(struct ub_dev *sc, struct ub_scsi_cmd *cmd); |
1872bceb | 418 | static void ub_state_stat_counted(struct ub_dev *sc, struct ub_scsi_cmd *cmd); |
1da177e4 LT |
419 | static void ub_state_sense(struct ub_dev *sc, struct ub_scsi_cmd *cmd); |
420 | static int ub_submit_clear_stall(struct ub_dev *sc, struct ub_scsi_cmd *cmd, | |
421 | int stalled_pipe); | |
422 | static void ub_top_sense_done(struct ub_dev *sc, struct ub_scsi_cmd *scmd); | |
2c26c9e6 PZ |
423 | static void ub_reset_enter(struct ub_dev *sc); |
424 | static void ub_reset_task(void *arg); | |
f4800078 PZ |
425 | static int ub_sync_tur(struct ub_dev *sc, struct ub_lun *lun); |
426 | static int ub_sync_read_cap(struct ub_dev *sc, struct ub_lun *lun, | |
427 | struct ub_capacity *ret); | |
428 | static 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 | 437 | static 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 | ||
442 | MODULE_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 | |
453 | static char ub_hostv[UB_MAX_HOSTS]; | |
f4800078 | 454 | |
1da177e4 LT |
455 | static DEFINE_SPINLOCK(ub_lock); /* Locks globals and ->openc */ |
456 | ||
457 | /* | |
458 | * The SCSI command tracing procedures. | |
459 | */ | |
460 | ||
461 | static void ub_cmdtr_new(struct ub_dev *sc, struct ub_scsi_cmd *cmd) | |
462 | { | |
463 | int n; | |
464 | struct ub_scsi_cmd_trace *t; | |
465 | ||
466 | if ((n = sc->tr.cur + 1) == SCMD_TRACE_SZ) n = 0; | |
467 | t = &sc->tr.vec[n]; | |
468 | ||
469 | memset(t, 0, sizeof(struct ub_scsi_cmd_trace)); | |
470 | t->tag = cmd->tag; | |
471 | t->op = cmd->cdb[0]; | |
472 | t->dir = cmd->dir; | |
473 | t->req_size = cmd->len; | |
474 | t->st_hst[0] = cmd->state; | |
475 | ||
476 | sc->tr.cur = n; | |
477 | cmd->trace_index = n; | |
478 | } | |
479 | ||
480 | static void ub_cmdtr_state(struct ub_dev *sc, struct ub_scsi_cmd *cmd) | |
481 | { | |
482 | int n; | |
483 | struct ub_scsi_cmd_trace *t; | |
484 | ||
485 | t = &sc->tr.vec[cmd->trace_index]; | |
486 | if (t->tag == cmd->tag) { | |
487 | if ((n = t->hcur + 1) == SCMD_ST_HIST_SZ) n = 0; | |
488 | t->st_hst[n] = cmd->state; | |
489 | t->hcur = n; | |
490 | } | |
491 | } | |
492 | ||
493 | static void ub_cmdtr_act_len(struct ub_dev *sc, struct ub_scsi_cmd *cmd) | |
494 | { | |
495 | struct ub_scsi_cmd_trace *t; | |
496 | ||
497 | t = &sc->tr.vec[cmd->trace_index]; | |
498 | if (t->tag == cmd->tag) | |
499 | t->act_size = cmd->act_len; | |
500 | } | |
501 | ||
502 | static void ub_cmdtr_sense(struct ub_dev *sc, struct ub_scsi_cmd *cmd, | |
503 | unsigned char *sense) | |
504 | { | |
505 | struct ub_scsi_cmd_trace *t; | |
506 | ||
507 | t = &sc->tr.vec[cmd->trace_index]; | |
508 | if (t->tag == cmd->tag) { | |
509 | t->key = sense[2] & 0x0F; | |
510 | t->asc = sense[12]; | |
511 | t->ascq = sense[13]; | |
512 | } | |
513 | } | |
514 | ||
07d4fd25 PZ |
515 | static ssize_t ub_diag_show(struct device *dev, struct device_attribute *attr, |
516 | char *page) | |
1da177e4 LT |
517 | { |
518 | struct usb_interface *intf; | |
519 | struct ub_dev *sc; | |
f4800078 PZ |
520 | struct list_head *p; |
521 | struct ub_lun *lun; | |
1da177e4 LT |
522 | int cnt; |
523 | unsigned long flags; | |
524 | int nc, nh; | |
525 | int i, j; | |
526 | struct ub_scsi_cmd_trace *t; | |
527 | ||
528 | intf = to_usb_interface(dev); | |
529 | sc = usb_get_intfdata(intf); | |
530 | if (sc == NULL) | |
531 | return 0; | |
532 | ||
533 | cnt = 0; | |
534 | spin_lock_irqsave(&sc->lock, flags); | |
535 | ||
2c26c9e6 PZ |
536 | cnt += sprintf(page + cnt, |
537 | "poison %d reset %d\n", | |
538 | atomic_read(&sc->poison), sc->reset); | |
1da177e4 | 539 | cnt += sprintf(page + cnt, |
f4800078 PZ |
540 | "qlen %d qmax %d\n", |
541 | sc->cmd_queue.qlen, sc->cmd_queue.qmax); | |
ba6abf13 | 542 | cnt += sprintf(page + cnt, |
64bd8453 | 543 | "sg %d %d %d %d %d .. %d\n", |
ba6abf13 PZ |
544 | sc->sg_stat[0], |
545 | sc->sg_stat[1], | |
546 | sc->sg_stat[2], | |
547 | sc->sg_stat[3], | |
64bd8453 PZ |
548 | sc->sg_stat[4], |
549 | sc->sg_stat[5]); | |
f4800078 PZ |
550 | |
551 | list_for_each (p, &sc->luns) { | |
552 | lun = list_entry(p, struct ub_lun, link); | |
553 | cnt += sprintf(page + cnt, | |
554 | "lun %u changed %d removable %d readonly %d\n", | |
555 | lun->num, lun->changed, lun->removable, lun->readonly); | |
556 | } | |
1da177e4 LT |
557 | |
558 | if ((nc = sc->tr.cur + 1) == SCMD_TRACE_SZ) nc = 0; | |
559 | for (j = 0; j < SCMD_TRACE_SZ; j++) { | |
560 | t = &sc->tr.vec[nc]; | |
561 | ||
562 | cnt += sprintf(page + cnt, "%08x %02x", t->tag, t->op); | |
563 | if (t->op == REQUEST_SENSE) { | |
564 | cnt += sprintf(page + cnt, " [sense %x %02x %02x]", | |
565 | t->key, t->asc, t->ascq); | |
566 | } else { | |
567 | cnt += sprintf(page + cnt, " %c", UB_DIR_CHAR(t->dir)); | |
568 | cnt += sprintf(page + cnt, " [%5d %5d]", | |
569 | t->req_size, t->act_size); | |
570 | } | |
571 | if ((nh = t->hcur + 1) == SCMD_ST_HIST_SZ) nh = 0; | |
572 | for (i = 0; i < SCMD_ST_HIST_SZ; i++) { | |
573 | cnt += sprintf(page + cnt, " %s", | |
574 | ub_scsi_cmd_stname[(int)t->st_hst[nh]]); | |
575 | if (++nh == SCMD_ST_HIST_SZ) nh = 0; | |
576 | } | |
577 | cnt += sprintf(page + cnt, "\n"); | |
578 | ||
579 | if (++nc == SCMD_TRACE_SZ) nc = 0; | |
580 | } | |
581 | ||
582 | spin_unlock_irqrestore(&sc->lock, flags); | |
583 | return cnt; | |
584 | } | |
585 | ||
586 | static DEVICE_ATTR(diag, S_IRUGO, ub_diag_show, NULL); /* N.B. World readable */ | |
587 | ||
588 | /* | |
589 | * The id allocator. | |
590 | * | |
591 | * This also stores the host for indexing by minor, which is somewhat dirty. | |
592 | */ | |
593 | static int ub_id_get(void) | |
594 | { | |
595 | unsigned long flags; | |
596 | int i; | |
597 | ||
598 | spin_lock_irqsave(&ub_lock, flags); | |
599 | for (i = 0; i < UB_MAX_HOSTS; i++) { | |
600 | if (ub_hostv[i] == 0) { | |
601 | ub_hostv[i] = 1; | |
602 | spin_unlock_irqrestore(&ub_lock, flags); | |
603 | return i; | |
604 | } | |
605 | } | |
606 | spin_unlock_irqrestore(&ub_lock, flags); | |
607 | return -1; | |
608 | } | |
609 | ||
610 | static void ub_id_put(int id) | |
611 | { | |
612 | unsigned long flags; | |
613 | ||
614 | if (id < 0 || id >= UB_MAX_HOSTS) { | |
615 | printk(KERN_ERR DRV_NAME ": bad host ID %d\n", id); | |
616 | return; | |
617 | } | |
618 | ||
619 | spin_lock_irqsave(&ub_lock, flags); | |
620 | if (ub_hostv[id] == 0) { | |
621 | spin_unlock_irqrestore(&ub_lock, flags); | |
622 | printk(KERN_ERR DRV_NAME ": freeing free host ID %d\n", id); | |
623 | return; | |
624 | } | |
625 | ub_hostv[id] = 0; | |
626 | spin_unlock_irqrestore(&ub_lock, flags); | |
627 | } | |
628 | ||
629 | /* | |
630 | * Downcount for deallocation. This rides on two assumptions: | |
631 | * - once something is poisoned, its refcount cannot grow | |
632 | * - opens cannot happen at this time (del_gendisk was done) | |
633 | * If the above is true, we can drop the lock, which we need for | |
634 | * blk_cleanup_queue(): the silly thing may attempt to sleep. | |
635 | * [Actually, it never needs to sleep for us, but it calls might_sleep()] | |
636 | */ | |
637 | static void ub_put(struct ub_dev *sc) | |
638 | { | |
639 | unsigned long flags; | |
640 | ||
641 | spin_lock_irqsave(&ub_lock, flags); | |
642 | --sc->openc; | |
643 | if (sc->openc == 0 && atomic_read(&sc->poison)) { | |
644 | spin_unlock_irqrestore(&ub_lock, flags); | |
645 | ub_cleanup(sc); | |
646 | } else { | |
647 | spin_unlock_irqrestore(&ub_lock, flags); | |
648 | } | |
649 | } | |
650 | ||
651 | /* | |
652 | * Final cleanup and deallocation. | |
653 | */ | |
654 | static void ub_cleanup(struct ub_dev *sc) | |
655 | { | |
f4800078 PZ |
656 | struct list_head *p; |
657 | struct ub_lun *lun; | |
1da177e4 LT |
658 | request_queue_t *q; |
659 | ||
f4800078 PZ |
660 | while (!list_empty(&sc->luns)) { |
661 | p = sc->luns.next; | |
662 | lun = list_entry(p, struct ub_lun, link); | |
663 | list_del(p); | |
1da177e4 | 664 | |
f4800078 PZ |
665 | /* I don't think queue can be NULL. But... Stolen from sx8.c */ |
666 | if ((q = lun->disk->queue) != NULL) | |
667 | blk_cleanup_queue(q); | |
668 | /* | |
669 | * If we zero disk->private_data BEFORE put_disk, we have | |
670 | * to check for NULL all over the place in open, release, | |
671 | * check_media and revalidate, because the block level | |
672 | * semaphore is well inside the put_disk. | |
673 | * But we cannot zero after the call, because *disk is gone. | |
674 | * The sd.c is blatantly racy in this area. | |
675 | */ | |
676 | /* disk->private_data = NULL; */ | |
677 | put_disk(lun->disk); | |
678 | lun->disk = NULL; | |
679 | ||
680 | ub_id_put(lun->id); | |
681 | kfree(lun); | |
682 | } | |
1da177e4 | 683 | |
1da177e4 LT |
684 | kfree(sc); |
685 | } | |
686 | ||
687 | /* | |
688 | * The "command allocator". | |
689 | */ | |
f4800078 | 690 | static struct ub_scsi_cmd *ub_get_cmd(struct ub_lun *lun) |
1da177e4 LT |
691 | { |
692 | struct ub_scsi_cmd *ret; | |
693 | ||
f4800078 | 694 | if (lun->cmda[0]) |
1da177e4 | 695 | return NULL; |
f4800078 PZ |
696 | ret = &lun->cmdv[0]; |
697 | lun->cmda[0] = 1; | |
1da177e4 LT |
698 | return ret; |
699 | } | |
700 | ||
f4800078 | 701 | static void ub_put_cmd(struct ub_lun *lun, struct ub_scsi_cmd *cmd) |
1da177e4 | 702 | { |
f4800078 | 703 | if (cmd != &lun->cmdv[0]) { |
1da177e4 | 704 | printk(KERN_WARNING "%s: releasing a foreign cmd %p\n", |
f4800078 | 705 | lun->name, cmd); |
1da177e4 LT |
706 | return; |
707 | } | |
f4800078 PZ |
708 | if (!lun->cmda[0]) { |
709 | printk(KERN_WARNING "%s: releasing a free cmd\n", lun->name); | |
1da177e4 LT |
710 | return; |
711 | } | |
f4800078 | 712 | lun->cmda[0] = 0; |
1da177e4 LT |
713 | } |
714 | ||
715 | /* | |
716 | * The command queue. | |
717 | */ | |
718 | static void ub_cmdq_add(struct ub_dev *sc, struct ub_scsi_cmd *cmd) | |
719 | { | |
720 | struct ub_scsi_cmd_queue *t = &sc->cmd_queue; | |
721 | ||
722 | if (t->qlen++ == 0) { | |
723 | t->head = cmd; | |
724 | t->tail = cmd; | |
725 | } else { | |
726 | t->tail->next = cmd; | |
727 | t->tail = cmd; | |
728 | } | |
729 | ||
730 | if (t->qlen > t->qmax) | |
731 | t->qmax = t->qlen; | |
732 | } | |
733 | ||
734 | static void ub_cmdq_insert(struct ub_dev *sc, struct ub_scsi_cmd *cmd) | |
735 | { | |
736 | struct ub_scsi_cmd_queue *t = &sc->cmd_queue; | |
737 | ||
738 | if (t->qlen++ == 0) { | |
739 | t->head = cmd; | |
740 | t->tail = cmd; | |
741 | } else { | |
742 | cmd->next = t->head; | |
743 | t->head = cmd; | |
744 | } | |
745 | ||
746 | if (t->qlen > t->qmax) | |
747 | t->qmax = t->qlen; | |
748 | } | |
749 | ||
750 | static struct ub_scsi_cmd *ub_cmdq_pop(struct ub_dev *sc) | |
751 | { | |
752 | struct ub_scsi_cmd_queue *t = &sc->cmd_queue; | |
753 | struct ub_scsi_cmd *cmd; | |
754 | ||
755 | if (t->qlen == 0) | |
756 | return NULL; | |
757 | if (--t->qlen == 0) | |
758 | t->tail = NULL; | |
759 | cmd = t->head; | |
760 | t->head = cmd->next; | |
761 | cmd->next = NULL; | |
762 | return cmd; | |
763 | } | |
764 | ||
765 | #define ub_cmdq_peek(sc) ((sc)->cmd_queue.head) | |
766 | ||
767 | /* | |
768 | * The request function is our main entry point | |
769 | */ | |
770 | ||
6c1eb8c1 | 771 | static void ub_request_fn(request_queue_t *q) |
1da177e4 | 772 | { |
f4800078 | 773 | struct ub_lun *lun = q->queuedata; |
1da177e4 LT |
774 | struct request *rq; |
775 | ||
776 | while ((rq = elv_next_request(q)) != NULL) { | |
6c1eb8c1 | 777 | if (ub_request_fn_1(lun, rq) != 0) { |
1da177e4 LT |
778 | blk_stop_queue(q); |
779 | break; | |
780 | } | |
781 | } | |
782 | } | |
783 | ||
6c1eb8c1 | 784 | static int ub_request_fn_1(struct ub_lun *lun, struct request *rq) |
1da177e4 | 785 | { |
f4800078 | 786 | struct ub_dev *sc = lun->udev; |
1da177e4 | 787 | struct ub_scsi_cmd *cmd; |
2c26c9e6 PZ |
788 | struct ub_request *urq; |
789 | int n_elem; | |
1da177e4 | 790 | |
f4800078 | 791 | if (atomic_read(&sc->poison) || lun->changed) { |
1da177e4 LT |
792 | blkdev_dequeue_request(rq); |
793 | ub_end_rq(rq, 0); | |
794 | return 0; | |
795 | } | |
796 | ||
2c26c9e6 PZ |
797 | if (lun->urq.rq != NULL) |
798 | return -1; | |
f4800078 | 799 | if ((cmd = ub_get_cmd(lun)) == NULL) |
1da177e4 LT |
800 | return -1; |
801 | memset(cmd, 0, sizeof(struct ub_scsi_cmd)); | |
802 | ||
803 | blkdev_dequeue_request(rq); | |
2c26c9e6 PZ |
804 | |
805 | urq = &lun->urq; | |
806 | memset(urq, 0, sizeof(struct ub_request)); | |
807 | urq->rq = rq; | |
808 | ||
809 | /* | |
810 | * get scatterlist from block layer | |
811 | */ | |
812 | n_elem = blk_rq_map_sg(lun->disk->queue, rq, &urq->sgv[0]); | |
813 | if (n_elem < 0) { | |
814 | printk(KERN_INFO "%s: failed request map (%d)\n", | |
815 | lun->name, n_elem); /* P3 */ | |
816 | goto drop; | |
817 | } | |
818 | if (n_elem > UB_MAX_REQ_SG) { /* Paranoia */ | |
819 | printk(KERN_WARNING "%s: request with %d segments\n", | |
820 | lun->name, n_elem); | |
821 | goto drop; | |
822 | } | |
823 | urq->nsg = n_elem; | |
824 | sc->sg_stat[n_elem < 5 ? n_elem : 5]++; | |
825 | ||
1da177e4 | 826 | if (blk_pc_request(rq)) { |
2c26c9e6 | 827 | ub_cmd_build_packet(sc, lun, cmd, urq); |
1da177e4 | 828 | } else { |
2c26c9e6 | 829 | ub_cmd_build_block(sc, lun, cmd, urq); |
1da177e4 | 830 | } |
1da177e4 | 831 | cmd->state = UB_CMDST_INIT; |
f4800078 | 832 | cmd->lun = lun; |
1da177e4 | 833 | cmd->done = ub_rw_cmd_done; |
2c26c9e6 | 834 | cmd->back = urq; |
1da177e4 LT |
835 | |
836 | cmd->tag = sc->tagcnt++; | |
2c26c9e6 PZ |
837 | if (ub_submit_scsi(sc, cmd) != 0) |
838 | goto drop; | |
839 | ||
840 | return 0; | |
1da177e4 | 841 | |
2c26c9e6 PZ |
842 | drop: |
843 | ub_put_cmd(lun, cmd); | |
844 | ub_end_rq(rq, 0); | |
1da177e4 LT |
845 | return 0; |
846 | } | |
847 | ||
2c26c9e6 PZ |
848 | static void ub_cmd_build_block(struct ub_dev *sc, struct ub_lun *lun, |
849 | struct ub_scsi_cmd *cmd, struct ub_request *urq) | |
1da177e4 | 850 | { |
2c26c9e6 | 851 | struct request *rq = urq->rq; |
a1cf96ef | 852 | unsigned int block, nblks; |
1da177e4 LT |
853 | |
854 | if (rq_data_dir(rq) == WRITE) | |
2c26c9e6 | 855 | cmd->dir = UB_DIR_WRITE; |
1da177e4 | 856 | else |
2c26c9e6 | 857 | cmd->dir = UB_DIR_READ; |
1da177e4 | 858 | |
2c26c9e6 PZ |
859 | cmd->nsg = urq->nsg; |
860 | memcpy(cmd->sgv, urq->sgv, sizeof(struct scatterlist) * cmd->nsg); | |
1da177e4 LT |
861 | |
862 | /* | |
863 | * build the command | |
864 | * | |
865 | * The call to blk_queue_hardsect_size() guarantees that request | |
866 | * is aligned, but it is given in terms of 512 byte units, always. | |
867 | */ | |
a1cf96ef PZ |
868 | block = rq->sector >> lun->capacity.bshift; |
869 | nblks = rq->nr_sectors >> lun->capacity.bshift; | |
ba6abf13 | 870 | |
2c26c9e6 | 871 | cmd->cdb[0] = (cmd->dir == UB_DIR_READ)? READ_10: WRITE_10; |
1da177e4 LT |
872 | /* 10-byte uses 4 bytes of LBA: 2147483648KB, 2097152MB, 2048GB */ |
873 | cmd->cdb[2] = block >> 24; | |
874 | cmd->cdb[3] = block >> 16; | |
875 | cmd->cdb[4] = block >> 8; | |
876 | cmd->cdb[5] = block; | |
877 | cmd->cdb[7] = nblks >> 8; | |
878 | cmd->cdb[8] = nblks; | |
879 | cmd->cdb_len = 10; | |
880 | ||
a1cf96ef | 881 | cmd->len = rq->nr_sectors * 512; |
1da177e4 LT |
882 | } |
883 | ||
2c26c9e6 PZ |
884 | static void ub_cmd_build_packet(struct ub_dev *sc, struct ub_lun *lun, |
885 | struct ub_scsi_cmd *cmd, struct ub_request *urq) | |
1da177e4 | 886 | { |
2c26c9e6 | 887 | struct request *rq = urq->rq; |
1da177e4 LT |
888 | |
889 | if (rq->data_len == 0) { | |
890 | cmd->dir = UB_DIR_NONE; | |
891 | } else { | |
892 | if (rq_data_dir(rq) == WRITE) | |
893 | cmd->dir = UB_DIR_WRITE; | |
894 | else | |
895 | cmd->dir = UB_DIR_READ; | |
896 | } | |
a1cf96ef | 897 | |
2c26c9e6 PZ |
898 | cmd->nsg = urq->nsg; |
899 | memcpy(cmd->sgv, urq->sgv, sizeof(struct scatterlist) * cmd->nsg); | |
a1cf96ef PZ |
900 | |
901 | memcpy(&cmd->cdb, rq->cmd, rq->cmd_len); | |
902 | cmd->cdb_len = rq->cmd_len; | |
903 | ||
1da177e4 | 904 | cmd->len = rq->data_len; |
1da177e4 LT |
905 | } |
906 | ||
907 | static void ub_rw_cmd_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd) | |
908 | { | |
f4800078 | 909 | struct ub_lun *lun = cmd->lun; |
2c26c9e6 PZ |
910 | struct ub_request *urq = cmd->back; |
911 | struct request *rq; | |
1da177e4 LT |
912 | int uptodate; |
913 | ||
2c26c9e6 PZ |
914 | rq = urq->rq; |
915 | ||
a1cf96ef | 916 | if (cmd->error == 0) { |
1da177e4 | 917 | uptodate = 1; |
1da177e4 | 918 | |
a1cf96ef PZ |
919 | if (blk_pc_request(rq)) { |
920 | if (cmd->act_len >= rq->data_len) | |
921 | rq->data_len = 0; | |
922 | else | |
923 | rq->data_len -= cmd->act_len; | |
ba6abf13 | 924 | } |
a1cf96ef | 925 | } else { |
ba6abf13 | 926 | uptodate = 0; |
ba6abf13 | 927 | |
a1cf96ef PZ |
928 | if (blk_pc_request(rq)) { |
929 | /* UB_SENSE_SIZE is smaller than SCSI_SENSE_BUFFERSIZE */ | |
930 | memcpy(rq->sense, sc->top_sense, UB_SENSE_SIZE); | |
931 | rq->sense_len = UB_SENSE_SIZE; | |
932 | if (sc->top_sense[0] != 0) | |
933 | rq->errors = SAM_STAT_CHECK_CONDITION; | |
934 | else | |
935 | rq->errors = DID_ERROR << 16; | |
2c26c9e6 PZ |
936 | } else { |
937 | if (cmd->error == -EIO) { | |
938 | if (ub_rw_cmd_retry(sc, lun, urq, cmd) == 0) | |
939 | return; | |
940 | } | |
a1cf96ef PZ |
941 | } |
942 | } | |
ba6abf13 | 943 | |
2c26c9e6 PZ |
944 | urq->rq = NULL; |
945 | ||
f4800078 | 946 | ub_put_cmd(lun, cmd); |
1da177e4 | 947 | ub_end_rq(rq, uptodate); |
ba6abf13 | 948 | blk_start_queue(lun->disk->queue); |
1da177e4 LT |
949 | } |
950 | ||
951 | static void ub_end_rq(struct request *rq, int uptodate) | |
952 | { | |
ab93091d | 953 | end_that_request_first(rq, uptodate, rq->hard_nr_sectors); |
1da177e4 LT |
954 | end_that_request_last(rq); |
955 | } | |
956 | ||
2c26c9e6 PZ |
957 | static int ub_rw_cmd_retry(struct ub_dev *sc, struct ub_lun *lun, |
958 | struct ub_request *urq, struct ub_scsi_cmd *cmd) | |
959 | { | |
960 | ||
961 | if (atomic_read(&sc->poison)) | |
962 | return -ENXIO; | |
963 | ||
964 | ub_reset_enter(sc); | |
965 | ||
966 | if (urq->current_try >= 3) | |
967 | return -EIO; | |
968 | urq->current_try++; | |
969 | /* P3 */ printk("%s: dir %c len/act %d/%d " | |
970 | "[sense %x %02x %02x] retry %d\n", | |
971 | sc->name, UB_DIR_CHAR(cmd->dir), cmd->len, cmd->act_len, | |
972 | cmd->key, cmd->asc, cmd->ascq, urq->current_try); | |
973 | ||
974 | memset(cmd, 0, sizeof(struct ub_scsi_cmd)); | |
975 | ub_cmd_build_block(sc, lun, cmd, urq); | |
976 | ||
977 | cmd->state = UB_CMDST_INIT; | |
978 | cmd->lun = lun; | |
979 | cmd->done = ub_rw_cmd_done; | |
980 | cmd->back = urq; | |
981 | ||
982 | cmd->tag = sc->tagcnt++; | |
983 | ||
984 | #if 0 /* Wasteful */ | |
985 | return ub_submit_scsi(sc, cmd); | |
986 | #else | |
987 | ub_cmdq_add(sc, cmd); | |
988 | return 0; | |
989 | #endif | |
990 | } | |
991 | ||
1da177e4 LT |
992 | /* |
993 | * Submit a regular SCSI operation (not an auto-sense). | |
994 | * | |
995 | * The Iron Law of Good Submit Routine is: | |
996 | * Zero return - callback is done, Nonzero return - callback is not done. | |
997 | * No exceptions. | |
998 | * | |
999 | * Host is assumed locked. | |
1000 | * | |
1001 | * XXX We only support Bulk for the moment. | |
1002 | */ | |
1003 | static int ub_submit_scsi(struct ub_dev *sc, struct ub_scsi_cmd *cmd) | |
1004 | { | |
1005 | ||
1006 | if (cmd->state != UB_CMDST_INIT || | |
1007 | (cmd->dir != UB_DIR_NONE && cmd->len == 0)) { | |
1008 | return -EINVAL; | |
1009 | } | |
1010 | ||
1011 | ub_cmdq_add(sc, cmd); | |
1012 | /* | |
1013 | * We can call ub_scsi_dispatch(sc) right away here, but it's a little | |
1014 | * safer to jump to a tasklet, in case upper layers do something silly. | |
1015 | */ | |
1016 | tasklet_schedule(&sc->tasklet); | |
1017 | return 0; | |
1018 | } | |
1019 | ||
1020 | /* | |
1021 | * Submit the first URB for the queued command. | |
1022 | * This function does not deal with queueing in any way. | |
1023 | */ | |
1024 | static int ub_scsi_cmd_start(struct ub_dev *sc, struct ub_scsi_cmd *cmd) | |
1025 | { | |
1026 | struct bulk_cb_wrap *bcb; | |
1027 | int rc; | |
1028 | ||
1029 | bcb = &sc->work_bcb; | |
1030 | ||
1031 | /* | |
1032 | * ``If the allocation length is eighteen or greater, and a device | |
1033 | * server returns less than eithteen bytes of data, the application | |
1034 | * client should assume that the bytes not transferred would have been | |
1035 | * zeroes had the device server returned those bytes.'' | |
1036 | * | |
1037 | * We zero sense for all commands so that when a packet request | |
1038 | * fails it does not return a stale sense. | |
1039 | */ | |
1040 | memset(&sc->top_sense, 0, UB_SENSE_SIZE); | |
1041 | ||
1042 | /* set up the command wrapper */ | |
1043 | bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN); | |
1044 | bcb->Tag = cmd->tag; /* Endianness is not important */ | |
1045 | bcb->DataTransferLength = cpu_to_le32(cmd->len); | |
1046 | bcb->Flags = (cmd->dir == UB_DIR_READ) ? 0x80 : 0; | |
f4800078 | 1047 | bcb->Lun = (cmd->lun != NULL) ? cmd->lun->num : 0; |
1da177e4 LT |
1048 | bcb->Length = cmd->cdb_len; |
1049 | ||
1050 | /* copy the command payload */ | |
1051 | memcpy(bcb->CDB, cmd->cdb, UB_MAX_CDB_SIZE); | |
1052 | ||
1053 | UB_INIT_COMPLETION(sc->work_done); | |
1054 | ||
1055 | sc->last_pipe = sc->send_bulk_pipe; | |
1056 | usb_fill_bulk_urb(&sc->work_urb, sc->dev, sc->send_bulk_pipe, | |
1057 | bcb, US_BULK_CB_WRAP_LEN, ub_urb_complete, sc); | |
1da177e4 LT |
1058 | |
1059 | /* Fill what we shouldn't be filling, because usb-storage did so. */ | |
1060 | sc->work_urb.actual_length = 0; | |
1061 | sc->work_urb.error_count = 0; | |
1062 | sc->work_urb.status = 0; | |
1063 | ||
1064 | if ((rc = usb_submit_urb(&sc->work_urb, GFP_ATOMIC)) != 0) { | |
1065 | /* XXX Clear stalls */ | |
1da177e4 LT |
1066 | ub_complete(&sc->work_done); |
1067 | return rc; | |
1068 | } | |
1069 | ||
1070 | sc->work_timer.expires = jiffies + UB_URB_TIMEOUT; | |
1071 | add_timer(&sc->work_timer); | |
1072 | ||
1073 | cmd->state = UB_CMDST_CMD; | |
1074 | ub_cmdtr_state(sc, cmd); | |
1075 | return 0; | |
1076 | } | |
1077 | ||
1078 | /* | |
1079 | * Timeout handler. | |
1080 | */ | |
1081 | static void ub_urb_timeout(unsigned long arg) | |
1082 | { | |
1083 | struct ub_dev *sc = (struct ub_dev *) arg; | |
1084 | unsigned long flags; | |
1085 | ||
1086 | spin_lock_irqsave(&sc->lock, flags); | |
1087 | usb_unlink_urb(&sc->work_urb); | |
1088 | spin_unlock_irqrestore(&sc->lock, flags); | |
1089 | } | |
1090 | ||
1091 | /* | |
1092 | * Completion routine for the work URB. | |
1093 | * | |
1094 | * This can be called directly from usb_submit_urb (while we have | |
1095 | * the sc->lock taken) and from an interrupt (while we do NOT have | |
1096 | * the sc->lock taken). Therefore, bounce this off to a tasklet. | |
1097 | */ | |
1098 | static void ub_urb_complete(struct urb *urb, struct pt_regs *pt) | |
1099 | { | |
1100 | struct ub_dev *sc = urb->context; | |
1101 | ||
1102 | ub_complete(&sc->work_done); | |
1103 | tasklet_schedule(&sc->tasklet); | |
1104 | } | |
1105 | ||
1106 | static void ub_scsi_action(unsigned long _dev) | |
1107 | { | |
1108 | struct ub_dev *sc = (struct ub_dev *) _dev; | |
1109 | unsigned long flags; | |
1110 | ||
1111 | spin_lock_irqsave(&sc->lock, flags); | |
1112 | del_timer(&sc->work_timer); | |
1113 | ub_scsi_dispatch(sc); | |
1114 | spin_unlock_irqrestore(&sc->lock, flags); | |
1115 | } | |
1116 | ||
1117 | static void ub_scsi_dispatch(struct ub_dev *sc) | |
1118 | { | |
1119 | struct ub_scsi_cmd *cmd; | |
1120 | int rc; | |
1121 | ||
2c26c9e6 | 1122 | while (!sc->reset && (cmd = ub_cmdq_peek(sc)) != NULL) { |
1da177e4 LT |
1123 | if (cmd->state == UB_CMDST_DONE) { |
1124 | ub_cmdq_pop(sc); | |
1125 | (*cmd->done)(sc, cmd); | |
1126 | } else if (cmd->state == UB_CMDST_INIT) { | |
1127 | ub_cmdtr_new(sc, cmd); | |
1128 | if ((rc = ub_scsi_cmd_start(sc, cmd)) == 0) | |
1129 | break; | |
1130 | cmd->error = rc; | |
1131 | cmd->state = UB_CMDST_DONE; | |
1132 | ub_cmdtr_state(sc, cmd); | |
1133 | } else { | |
1134 | if (!ub_is_completed(&sc->work_done)) | |
1135 | break; | |
1136 | ub_scsi_urb_compl(sc, cmd); | |
1137 | } | |
1138 | } | |
1139 | } | |
1140 | ||
1141 | static void ub_scsi_urb_compl(struct ub_dev *sc, struct ub_scsi_cmd *cmd) | |
1142 | { | |
1143 | struct urb *urb = &sc->work_urb; | |
1144 | struct bulk_cs_wrap *bcs; | |
2c26c9e6 | 1145 | int len; |
1da177e4 LT |
1146 | int rc; |
1147 | ||
1148 | if (atomic_read(&sc->poison)) { | |
2c26c9e6 PZ |
1149 | ub_state_done(sc, cmd, -ENODEV); |
1150 | return; | |
1da177e4 LT |
1151 | } |
1152 | ||
1153 | if (cmd->state == UB_CMDST_CLEAR) { | |
1154 | if (urb->status == -EPIPE) { | |
1155 | /* | |
1156 | * STALL while clearning STALL. | |
1157 | * The control pipe clears itself - nothing to do. | |
1da177e4 | 1158 | */ |
f4800078 PZ |
1159 | printk(KERN_NOTICE "%s: stall on control pipe\n", |
1160 | sc->name); | |
1da177e4 LT |
1161 | goto Bad_End; |
1162 | } | |
1163 | ||
1164 | /* | |
1165 | * We ignore the result for the halt clear. | |
1166 | */ | |
1167 | ||
1168 | /* reset the endpoint toggle */ | |
1169 | usb_settoggle(sc->dev, usb_pipeendpoint(sc->last_pipe), | |
1170 | usb_pipeout(sc->last_pipe), 0); | |
1171 | ||
1172 | ub_state_sense(sc, cmd); | |
1173 | ||
1174 | } else if (cmd->state == UB_CMDST_CLR2STS) { | |
1175 | if (urb->status == -EPIPE) { | |
f4800078 PZ |
1176 | printk(KERN_NOTICE "%s: stall on control pipe\n", |
1177 | sc->name); | |
1da177e4 LT |
1178 | goto Bad_End; |
1179 | } | |
1180 | ||
1181 | /* | |
1182 | * We ignore the result for the halt clear. | |
1183 | */ | |
1184 | ||
1185 | /* reset the endpoint toggle */ | |
1186 | usb_settoggle(sc->dev, usb_pipeendpoint(sc->last_pipe), | |
1187 | usb_pipeout(sc->last_pipe), 0); | |
1188 | ||
1189 | ub_state_stat(sc, cmd); | |
1190 | ||
1872bceb PZ |
1191 | } else if (cmd->state == UB_CMDST_CLRRS) { |
1192 | if (urb->status == -EPIPE) { | |
1872bceb PZ |
1193 | printk(KERN_NOTICE "%s: stall on control pipe\n", |
1194 | sc->name); | |
1195 | goto Bad_End; | |
1196 | } | |
1197 | ||
1198 | /* | |
1199 | * We ignore the result for the halt clear. | |
1200 | */ | |
1201 | ||
1202 | /* reset the endpoint toggle */ | |
1203 | usb_settoggle(sc->dev, usb_pipeendpoint(sc->last_pipe), | |
1204 | usb_pipeout(sc->last_pipe), 0); | |
1205 | ||
1206 | ub_state_stat_counted(sc, cmd); | |
1207 | ||
1da177e4 | 1208 | } else if (cmd->state == UB_CMDST_CMD) { |
2c26c9e6 PZ |
1209 | switch (urb->status) { |
1210 | case 0: | |
1211 | break; | |
1212 | case -EOVERFLOW: | |
1213 | goto Bad_End; | |
1214 | case -EPIPE: | |
1da177e4 LT |
1215 | rc = ub_submit_clear_stall(sc, cmd, sc->last_pipe); |
1216 | if (rc != 0) { | |
1217 | printk(KERN_NOTICE "%s: " | |
f4800078 PZ |
1218 | "unable to submit clear (%d)\n", |
1219 | sc->name, rc); | |
1da177e4 LT |
1220 | /* |
1221 | * This is typically ENOMEM or some other such shit. | |
1222 | * Retrying is pointless. Just do Bad End on it... | |
1223 | */ | |
2c26c9e6 PZ |
1224 | ub_state_done(sc, cmd, rc); |
1225 | return; | |
1da177e4 LT |
1226 | } |
1227 | cmd->state = UB_CMDST_CLEAR; | |
1228 | ub_cmdtr_state(sc, cmd); | |
1229 | return; | |
2c26c9e6 PZ |
1230 | case -ESHUTDOWN: /* unplug */ |
1231 | case -EILSEQ: /* unplug timeout on uhci */ | |
1232 | ub_state_done(sc, cmd, -ENODEV); | |
1233 | return; | |
1234 | default: | |
1da177e4 LT |
1235 | goto Bad_End; |
1236 | } | |
1237 | if (urb->actual_length != US_BULK_CB_WRAP_LEN) { | |
1da177e4 LT |
1238 | goto Bad_End; |
1239 | } | |
1240 | ||
a1cf96ef | 1241 | if (cmd->dir == UB_DIR_NONE || cmd->nsg < 1) { |
1da177e4 LT |
1242 | ub_state_stat(sc, cmd); |
1243 | return; | |
1244 | } | |
1245 | ||
a1cf96ef PZ |
1246 | // udelay(125); // usb-storage has this |
1247 | ub_data_start(sc, cmd); | |
1da177e4 LT |
1248 | |
1249 | } else if (cmd->state == UB_CMDST_DATA) { | |
1250 | if (urb->status == -EPIPE) { | |
1251 | rc = ub_submit_clear_stall(sc, cmd, sc->last_pipe); | |
1252 | if (rc != 0) { | |
1253 | printk(KERN_NOTICE "%s: " | |
f4800078 PZ |
1254 | "unable to submit clear (%d)\n", |
1255 | sc->name, rc); | |
2c26c9e6 PZ |
1256 | ub_state_done(sc, cmd, rc); |
1257 | return; | |
1da177e4 LT |
1258 | } |
1259 | cmd->state = UB_CMDST_CLR2STS; | |
1260 | ub_cmdtr_state(sc, cmd); | |
1261 | return; | |
1262 | } | |
1263 | if (urb->status == -EOVERFLOW) { | |
1264 | /* | |
1265 | * A babble? Failure, but we must transfer CSW now. | |
1266 | */ | |
1267 | cmd->error = -EOVERFLOW; /* A cheap trick... */ | |
a1cf96ef PZ |
1268 | ub_state_stat(sc, cmd); |
1269 | return; | |
1da177e4 | 1270 | } |
2c26c9e6 PZ |
1271 | |
1272 | if (cmd->dir == UB_DIR_WRITE) { | |
1273 | /* | |
1274 | * Do not continue writes in case of a failure. | |
1275 | * Doing so would cause sectors to be mixed up, | |
1276 | * which is worse than sectors lost. | |
1277 | * | |
1278 | * We must try to read the CSW, or many devices | |
1279 | * get confused. | |
1280 | */ | |
1281 | len = urb->actual_length; | |
1282 | if (urb->status != 0 || | |
1283 | len != cmd->sgv[cmd->current_sg].length) { | |
1284 | cmd->act_len += len; | |
1285 | ub_cmdtr_act_len(sc, cmd); | |
1286 | ||
1287 | cmd->error = -EIO; | |
1288 | ub_state_stat(sc, cmd); | |
1289 | return; | |
1290 | } | |
1291 | ||
1292 | } else { | |
1293 | /* | |
1294 | * If an error occurs on read, we record it, and | |
1295 | * continue to fetch data in order to avoid bubble. | |
1296 | * | |
1297 | * As a small shortcut, we stop if we detect that | |
1298 | * a CSW mixed into data. | |
1299 | */ | |
1300 | if (urb->status != 0) | |
1301 | cmd->error = -EIO; | |
1302 | ||
1303 | len = urb->actual_length; | |
1304 | if (urb->status != 0 || | |
1305 | len != cmd->sgv[cmd->current_sg].length) { | |
1306 | if ((len & 0x1FF) == US_BULK_CS_WRAP_LEN) | |
1307 | goto Bad_End; | |
1308 | } | |
1309 | } | |
1da177e4 | 1310 | |
a1cf96ef | 1311 | cmd->act_len += urb->actual_length; |
1da177e4 LT |
1312 | ub_cmdtr_act_len(sc, cmd); |
1313 | ||
a1cf96ef PZ |
1314 | if (++cmd->current_sg < cmd->nsg) { |
1315 | ub_data_start(sc, cmd); | |
1316 | return; | |
1317 | } | |
1da177e4 LT |
1318 | ub_state_stat(sc, cmd); |
1319 | ||
1320 | } else if (cmd->state == UB_CMDST_STAT) { | |
1321 | if (urb->status == -EPIPE) { | |
1322 | rc = ub_submit_clear_stall(sc, cmd, sc->last_pipe); | |
1323 | if (rc != 0) { | |
1324 | printk(KERN_NOTICE "%s: " | |
f4800078 PZ |
1325 | "unable to submit clear (%d)\n", |
1326 | sc->name, rc); | |
2c26c9e6 PZ |
1327 | ub_state_done(sc, cmd, rc); |
1328 | return; | |
1da177e4 | 1329 | } |
1872bceb PZ |
1330 | |
1331 | /* | |
1332 | * Having a stall when getting CSW is an error, so | |
1333 | * make sure uppper levels are not oblivious to it. | |
1334 | */ | |
1335 | cmd->error = -EIO; /* A cheap trick... */ | |
1336 | ||
1337 | cmd->state = UB_CMDST_CLRRS; | |
1da177e4 LT |
1338 | ub_cmdtr_state(sc, cmd); |
1339 | return; | |
1340 | } | |
2c26c9e6 PZ |
1341 | |
1342 | /* Catch everything, including -EOVERFLOW and other nasties. */ | |
1da177e4 LT |
1343 | if (urb->status != 0) |
1344 | goto Bad_End; | |
1345 | ||
1346 | if (urb->actual_length == 0) { | |
1872bceb | 1347 | ub_state_stat_counted(sc, cmd); |
1da177e4 LT |
1348 | return; |
1349 | } | |
1350 | ||
1351 | /* | |
1352 | * Check the returned Bulk protocol status. | |
1872bceb | 1353 | * The status block has to be validated first. |
1da177e4 LT |
1354 | */ |
1355 | ||
1356 | bcs = &sc->work_bcs; | |
1872bceb PZ |
1357 | |
1358 | if (sc->signature == cpu_to_le32(0)) { | |
1da177e4 | 1359 | /* |
1872bceb PZ |
1360 | * This is the first reply, so do not perform the check. |
1361 | * Instead, remember the signature the device uses | |
1362 | * for future checks. But do not allow a nul. | |
1da177e4 | 1363 | */ |
1872bceb PZ |
1364 | sc->signature = bcs->Signature; |
1365 | if (sc->signature == cpu_to_le32(0)) { | |
1366 | ub_state_stat_counted(sc, cmd); | |
1367 | return; | |
1368 | } | |
1369 | } else { | |
1370 | if (bcs->Signature != sc->signature) { | |
1371 | ub_state_stat_counted(sc, cmd); | |
1372 | return; | |
1373 | } | |
1da177e4 | 1374 | } |
1da177e4 LT |
1375 | |
1376 | if (bcs->Tag != cmd->tag) { | |
1377 | /* | |
1378 | * This usually happens when we disagree with the | |
1379 | * device's microcode about something. For instance, | |
1380 | * a few of them throw this after timeouts. They buffer | |
1381 | * commands and reply at commands we timed out before. | |
1382 | * Without flushing these replies we loop forever. | |
1383 | */ | |
1872bceb | 1384 | ub_state_stat_counted(sc, cmd); |
1da177e4 LT |
1385 | return; |
1386 | } | |
1387 | ||
2c26c9e6 PZ |
1388 | len = le32_to_cpu(bcs->Residue); |
1389 | if (len != cmd->len - cmd->act_len) { | |
1872bceb PZ |
1390 | /* |
1391 | * It is all right to transfer less, the caller has | |
1392 | * to check. But it's not all right if the device | |
1393 | * counts disagree with our counts. | |
1394 | */ | |
1395 | /* P3 */ printk("%s: resid %d len %d act %d\n", | |
2c26c9e6 | 1396 | sc->name, len, cmd->len, cmd->act_len); |
1872bceb PZ |
1397 | goto Bad_End; |
1398 | } | |
1399 | ||
1da177e4 LT |
1400 | switch (bcs->Status) { |
1401 | case US_BULK_STAT_OK: | |
1402 | break; | |
1403 | case US_BULK_STAT_FAIL: | |
1404 | ub_state_sense(sc, cmd); | |
1405 | return; | |
1406 | case US_BULK_STAT_PHASE: | |
1da177e4 LT |
1407 | /* P3 */ printk("%s: status PHASE\n", sc->name); |
1408 | goto Bad_End; | |
1409 | default: | |
1410 | printk(KERN_INFO "%s: unknown CSW status 0x%x\n", | |
1411 | sc->name, bcs->Status); | |
2c26c9e6 PZ |
1412 | ub_state_done(sc, cmd, -EINVAL); |
1413 | return; | |
1da177e4 LT |
1414 | } |
1415 | ||
1416 | /* Not zeroing error to preserve a babble indicator */ | |
1872bceb PZ |
1417 | if (cmd->error != 0) { |
1418 | ub_state_sense(sc, cmd); | |
1419 | return; | |
1420 | } | |
1da177e4 LT |
1421 | cmd->state = UB_CMDST_DONE; |
1422 | ub_cmdtr_state(sc, cmd); | |
1423 | ub_cmdq_pop(sc); | |
1424 | (*cmd->done)(sc, cmd); | |
1425 | ||
1426 | } else if (cmd->state == UB_CMDST_SENSE) { | |
1427 | ub_state_done(sc, cmd, -EIO); | |
1428 | ||
1429 | } else { | |
1430 | printk(KERN_WARNING "%s: " | |
f4800078 PZ |
1431 | "wrong command state %d\n", |
1432 | sc->name, cmd->state); | |
2c26c9e6 PZ |
1433 | ub_state_done(sc, cmd, -EINVAL); |
1434 | return; | |
1da177e4 LT |
1435 | } |
1436 | return; | |
1437 | ||
1438 | Bad_End: /* Little Excel is dead */ | |
1439 | ub_state_done(sc, cmd, -EIO); | |
1440 | } | |
1441 | ||
a1cf96ef PZ |
1442 | /* |
1443 | * Factorization helper for the command state machine: | |
1444 | * Initiate a data segment transfer. | |
1445 | */ | |
1446 | static void ub_data_start(struct ub_dev *sc, struct ub_scsi_cmd *cmd) | |
1447 | { | |
1448 | struct scatterlist *sg = &cmd->sgv[cmd->current_sg]; | |
1449 | int pipe; | |
1450 | int rc; | |
1451 | ||
1452 | UB_INIT_COMPLETION(sc->work_done); | |
1453 | ||
1454 | if (cmd->dir == UB_DIR_READ) | |
1455 | pipe = sc->recv_bulk_pipe; | |
1456 | else | |
1457 | pipe = sc->send_bulk_pipe; | |
1458 | sc->last_pipe = pipe; | |
1459 | usb_fill_bulk_urb(&sc->work_urb, sc->dev, pipe, | |
1460 | page_address(sg->page) + sg->offset, sg->length, | |
1461 | ub_urb_complete, sc); | |
a1cf96ef PZ |
1462 | sc->work_urb.actual_length = 0; |
1463 | sc->work_urb.error_count = 0; | |
1464 | sc->work_urb.status = 0; | |
1465 | ||
1466 | if ((rc = usb_submit_urb(&sc->work_urb, GFP_ATOMIC)) != 0) { | |
1467 | /* XXX Clear stalls */ | |
a1cf96ef PZ |
1468 | ub_complete(&sc->work_done); |
1469 | ub_state_done(sc, cmd, rc); | |
1470 | return; | |
1471 | } | |
1472 | ||
1473 | sc->work_timer.expires = jiffies + UB_DATA_TIMEOUT; | |
1474 | add_timer(&sc->work_timer); | |
1475 | ||
1476 | cmd->state = UB_CMDST_DATA; | |
1477 | ub_cmdtr_state(sc, cmd); | |
1478 | } | |
1479 | ||
1da177e4 LT |
1480 | /* |
1481 | * Factorization helper for the command state machine: | |
1482 | * Finish the command. | |
1483 | */ | |
1484 | static void ub_state_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd, int rc) | |
1485 | { | |
1486 | ||
1487 | cmd->error = rc; | |
1488 | cmd->state = UB_CMDST_DONE; | |
1489 | ub_cmdtr_state(sc, cmd); | |
1490 | ub_cmdq_pop(sc); | |
1491 | (*cmd->done)(sc, cmd); | |
1492 | } | |
1493 | ||
1494 | /* | |
1495 | * Factorization helper for the command state machine: | |
1496 | * Submit a CSW read. | |
1497 | */ | |
1872bceb | 1498 | static int __ub_state_stat(struct ub_dev *sc, struct ub_scsi_cmd *cmd) |
1da177e4 LT |
1499 | { |
1500 | int rc; | |
1501 | ||
1502 | UB_INIT_COMPLETION(sc->work_done); | |
1503 | ||
1504 | sc->last_pipe = sc->recv_bulk_pipe; | |
1505 | usb_fill_bulk_urb(&sc->work_urb, sc->dev, sc->recv_bulk_pipe, | |
1506 | &sc->work_bcs, US_BULK_CS_WRAP_LEN, ub_urb_complete, sc); | |
1da177e4 LT |
1507 | sc->work_urb.actual_length = 0; |
1508 | sc->work_urb.error_count = 0; | |
1509 | sc->work_urb.status = 0; | |
1510 | ||
1511 | if ((rc = usb_submit_urb(&sc->work_urb, GFP_ATOMIC)) != 0) { | |
1512 | /* XXX Clear stalls */ | |
1da177e4 LT |
1513 | ub_complete(&sc->work_done); |
1514 | ub_state_done(sc, cmd, rc); | |
1872bceb | 1515 | return -1; |
1da177e4 LT |
1516 | } |
1517 | ||
1518 | sc->work_timer.expires = jiffies + UB_STAT_TIMEOUT; | |
1519 | add_timer(&sc->work_timer); | |
1872bceb | 1520 | return 0; |
1da177e4 LT |
1521 | } |
1522 | ||
1523 | /* | |
1524 | * Factorization helper for the command state machine: | |
1525 | * Submit a CSW read and go to STAT state. | |
1526 | */ | |
1527 | static void ub_state_stat(struct ub_dev *sc, struct ub_scsi_cmd *cmd) | |
1528 | { | |
1872bceb PZ |
1529 | |
1530 | if (__ub_state_stat(sc, cmd) != 0) | |
1531 | return; | |
1da177e4 LT |
1532 | |
1533 | cmd->stat_count = 0; | |
1534 | cmd->state = UB_CMDST_STAT; | |
1872bceb PZ |
1535 | ub_cmdtr_state(sc, cmd); |
1536 | } | |
1537 | ||
1538 | /* | |
1539 | * Factorization helper for the command state machine: | |
1540 | * Submit a CSW read and go to STAT state with counter (along [C] path). | |
1541 | */ | |
1542 | static void ub_state_stat_counted(struct ub_dev *sc, struct ub_scsi_cmd *cmd) | |
1543 | { | |
1544 | ||
1545 | if (++cmd->stat_count >= 4) { | |
1546 | ub_state_sense(sc, cmd); | |
1547 | return; | |
1548 | } | |
1549 | ||
1550 | if (__ub_state_stat(sc, cmd) != 0) | |
1551 | return; | |
1552 | ||
1553 | cmd->state = UB_CMDST_STAT; | |
1da177e4 LT |
1554 | ub_cmdtr_state(sc, cmd); |
1555 | } | |
1556 | ||
1557 | /* | |
1558 | * Factorization helper for the command state machine: | |
1559 | * Submit a REQUEST SENSE and go to SENSE state. | |
1560 | */ | |
1561 | static void ub_state_sense(struct ub_dev *sc, struct ub_scsi_cmd *cmd) | |
1562 | { | |
1563 | struct ub_scsi_cmd *scmd; | |
a1cf96ef | 1564 | struct scatterlist *sg; |
1da177e4 LT |
1565 | int rc; |
1566 | ||
1567 | if (cmd->cdb[0] == REQUEST_SENSE) { | |
1568 | rc = -EPIPE; | |
1569 | goto error; | |
1570 | } | |
1571 | ||
1572 | scmd = &sc->top_rqs_cmd; | |
a1cf96ef | 1573 | memset(scmd, 0, sizeof(struct ub_scsi_cmd)); |
1da177e4 LT |
1574 | scmd->cdb[0] = REQUEST_SENSE; |
1575 | scmd->cdb[4] = UB_SENSE_SIZE; | |
1576 | scmd->cdb_len = 6; | |
1577 | scmd->dir = UB_DIR_READ; | |
1578 | scmd->state = UB_CMDST_INIT; | |
a1cf96ef PZ |
1579 | scmd->nsg = 1; |
1580 | sg = &scmd->sgv[0]; | |
1581 | sg->page = virt_to_page(sc->top_sense); | |
38ffdd62 | 1582 | sg->offset = (unsigned long)sc->top_sense & (PAGE_SIZE-1); |
a1cf96ef | 1583 | sg->length = UB_SENSE_SIZE; |
1da177e4 | 1584 | scmd->len = UB_SENSE_SIZE; |
f4800078 | 1585 | scmd->lun = cmd->lun; |
1da177e4 LT |
1586 | scmd->done = ub_top_sense_done; |
1587 | scmd->back = cmd; | |
1588 | ||
1589 | scmd->tag = sc->tagcnt++; | |
1590 | ||
1591 | cmd->state = UB_CMDST_SENSE; | |
1592 | ub_cmdtr_state(sc, cmd); | |
1593 | ||
1594 | ub_cmdq_insert(sc, scmd); | |
1595 | return; | |
1596 | ||
1597 | error: | |
1598 | ub_state_done(sc, cmd, rc); | |
1599 | } | |
1600 | ||
1601 | /* | |
1602 | * A helper for the command's state machine: | |
1603 | * Submit a stall clear. | |
1604 | */ | |
1605 | static int ub_submit_clear_stall(struct ub_dev *sc, struct ub_scsi_cmd *cmd, | |
1606 | int stalled_pipe) | |
1607 | { | |
1608 | int endp; | |
1609 | struct usb_ctrlrequest *cr; | |
1610 | int rc; | |
1611 | ||
1612 | endp = usb_pipeendpoint(stalled_pipe); | |
1613 | if (usb_pipein (stalled_pipe)) | |
1614 | endp |= USB_DIR_IN; | |
1615 | ||
1616 | cr = &sc->work_cr; | |
1617 | cr->bRequestType = USB_RECIP_ENDPOINT; | |
1618 | cr->bRequest = USB_REQ_CLEAR_FEATURE; | |
1619 | cr->wValue = cpu_to_le16(USB_ENDPOINT_HALT); | |
1620 | cr->wIndex = cpu_to_le16(endp); | |
1621 | cr->wLength = cpu_to_le16(0); | |
1622 | ||
1623 | UB_INIT_COMPLETION(sc->work_done); | |
1624 | ||
1625 | usb_fill_control_urb(&sc->work_urb, sc->dev, sc->send_ctrl_pipe, | |
1626 | (unsigned char*) cr, NULL, 0, ub_urb_complete, sc); | |
1da177e4 LT |
1627 | sc->work_urb.actual_length = 0; |
1628 | sc->work_urb.error_count = 0; | |
1629 | sc->work_urb.status = 0; | |
1630 | ||
1631 | if ((rc = usb_submit_urb(&sc->work_urb, GFP_ATOMIC)) != 0) { | |
1632 | ub_complete(&sc->work_done); | |
1633 | return rc; | |
1634 | } | |
1635 | ||
1636 | sc->work_timer.expires = jiffies + UB_CTRL_TIMEOUT; | |
1637 | add_timer(&sc->work_timer); | |
1638 | return 0; | |
1639 | } | |
1640 | ||
1641 | /* | |
1642 | */ | |
1643 | static void ub_top_sense_done(struct ub_dev *sc, struct ub_scsi_cmd *scmd) | |
1644 | { | |
a1cf96ef | 1645 | unsigned char *sense = sc->top_sense; |
1da177e4 LT |
1646 | struct ub_scsi_cmd *cmd; |
1647 | ||
1648 | /* | |
1649 | * Ignoring scmd->act_len, because the buffer was pre-zeroed. | |
1650 | */ | |
1651 | ub_cmdtr_sense(sc, scmd, sense); | |
1652 | ||
1653 | /* | |
1654 | * Find the command which triggered the unit attention or a check, | |
1655 | * save the sense into it, and advance its state machine. | |
1656 | */ | |
1657 | if ((cmd = ub_cmdq_peek(sc)) == NULL) { | |
1658 | printk(KERN_WARNING "%s: sense done while idle\n", sc->name); | |
1659 | return; | |
1660 | } | |
1661 | if (cmd != scmd->back) { | |
1662 | printk(KERN_WARNING "%s: " | |
f4800078 PZ |
1663 | "sense done for wrong command 0x%x\n", |
1664 | sc->name, cmd->tag); | |
1da177e4 LT |
1665 | return; |
1666 | } | |
1667 | if (cmd->state != UB_CMDST_SENSE) { | |
1668 | printk(KERN_WARNING "%s: " | |
f4800078 PZ |
1669 | "sense done with bad cmd state %d\n", |
1670 | sc->name, cmd->state); | |
1da177e4 LT |
1671 | return; |
1672 | } | |
1673 | ||
1674 | cmd->key = sense[2] & 0x0F; | |
1675 | cmd->asc = sense[12]; | |
1676 | cmd->ascq = sense[13]; | |
1677 | ||
1678 | ub_scsi_urb_compl(sc, cmd); | |
1679 | } | |
1680 | ||
2c26c9e6 PZ |
1681 | /* |
1682 | * Reset management | |
1683 | */ | |
1684 | ||
1685 | static void ub_reset_enter(struct ub_dev *sc) | |
1686 | { | |
1687 | ||
1688 | if (sc->reset) { | |
1689 | /* This happens often on multi-LUN devices. */ | |
1690 | return; | |
1691 | } | |
1692 | sc->reset = 1; | |
1693 | ||
1694 | #if 0 /* Not needed because the disconnect waits for us. */ | |
1695 | unsigned long flags; | |
1696 | spin_lock_irqsave(&ub_lock, flags); | |
1697 | sc->openc++; | |
1698 | spin_unlock_irqrestore(&ub_lock, flags); | |
1699 | #endif | |
1700 | ||
1701 | #if 0 /* We let them stop themselves. */ | |
1702 | struct list_head *p; | |
1703 | struct ub_lun *lun; | |
1704 | list_for_each(p, &sc->luns) { | |
1705 | lun = list_entry(p, struct ub_lun, link); | |
1706 | blk_stop_queue(lun->disk->queue); | |
1707 | } | |
1708 | #endif | |
1709 | ||
1710 | schedule_work(&sc->reset_work); | |
1711 | } | |
1712 | ||
1713 | static void ub_reset_task(void *arg) | |
1714 | { | |
1715 | struct ub_dev *sc = arg; | |
1716 | unsigned long flags; | |
1717 | struct list_head *p; | |
1718 | struct ub_lun *lun; | |
1719 | int lkr, rc; | |
1720 | ||
1721 | if (!sc->reset) { | |
1722 | printk(KERN_WARNING "%s: Running reset unrequested\n", | |
1723 | sc->name); | |
1724 | return; | |
1725 | } | |
1726 | ||
1727 | if (atomic_read(&sc->poison)) { | |
1728 | printk(KERN_NOTICE "%s: Not resetting disconnected device\n", | |
1729 | sc->name); /* P3 This floods. Remove soon. XXX */ | |
1730 | } else if (sc->dev->actconfig->desc.bNumInterfaces != 1) { | |
1731 | printk(KERN_NOTICE "%s: Not resetting multi-interface device\n", | |
1732 | sc->name); /* P3 This floods. Remove soon. XXX */ | |
1733 | } else { | |
1734 | if ((lkr = usb_lock_device_for_reset(sc->dev, sc->intf)) < 0) { | |
1735 | printk(KERN_NOTICE | |
1736 | "%s: usb_lock_device_for_reset failed (%d)\n", | |
1737 | sc->name, lkr); | |
1738 | } else { | |
1739 | rc = usb_reset_device(sc->dev); | |
1740 | if (rc < 0) { | |
1741 | printk(KERN_NOTICE "%s: " | |
1742 | "usb_lock_device_for_reset failed (%d)\n", | |
1743 | sc->name, rc); | |
1744 | } | |
1745 | ||
1746 | if (lkr) | |
1747 | usb_unlock_device(sc->dev); | |
1748 | } | |
1749 | } | |
1750 | ||
1751 | /* | |
1752 | * In theory, no commands can be running while reset is active, | |
1753 | * so nobody can ask for another reset, and so we do not need any | |
1754 | * queues of resets or anything. We do need a spinlock though, | |
1755 | * to interact with block layer. | |
1756 | */ | |
1757 | spin_lock_irqsave(&sc->lock, flags); | |
1758 | sc->reset = 0; | |
1759 | tasklet_schedule(&sc->tasklet); | |
1760 | list_for_each(p, &sc->luns) { | |
1761 | lun = list_entry(p, struct ub_lun, link); | |
1762 | blk_start_queue(lun->disk->queue); | |
1763 | } | |
1764 | wake_up(&sc->reset_wait); | |
1765 | spin_unlock_irqrestore(&sc->lock, flags); | |
1766 | } | |
1767 | ||
1da177e4 LT |
1768 | /* |
1769 | * This is called from a process context. | |
1770 | */ | |
f4800078 | 1771 | static void ub_revalidate(struct ub_dev *sc, struct ub_lun *lun) |
1da177e4 LT |
1772 | { |
1773 | ||
f4800078 | 1774 | lun->readonly = 0; /* XXX Query this from the device */ |
1da177e4 | 1775 | |
f4800078 PZ |
1776 | lun->capacity.nsec = 0; |
1777 | lun->capacity.bsize = 512; | |
1778 | lun->capacity.bshift = 0; | |
1da177e4 | 1779 | |
f4800078 | 1780 | if (ub_sync_tur(sc, lun) != 0) |
1da177e4 | 1781 | return; /* Not ready */ |
f4800078 | 1782 | lun->changed = 0; |
1da177e4 | 1783 | |
f4800078 | 1784 | if (ub_sync_read_cap(sc, lun, &lun->capacity) != 0) { |
1da177e4 LT |
1785 | /* |
1786 | * The retry here means something is wrong, either with the | |
1787 | * device, with the transport, or with our code. | |
1788 | * We keep this because sd.c has retries for capacity. | |
1789 | */ | |
f4800078 PZ |
1790 | if (ub_sync_read_cap(sc, lun, &lun->capacity) != 0) { |
1791 | lun->capacity.nsec = 0; | |
1792 | lun->capacity.bsize = 512; | |
1793 | lun->capacity.bshift = 0; | |
1da177e4 LT |
1794 | } |
1795 | } | |
1796 | } | |
1797 | ||
1798 | /* | |
1799 | * The open funcion. | |
1800 | * This is mostly needed to keep refcounting, but also to support | |
1801 | * media checks on removable media drives. | |
1802 | */ | |
1803 | static int ub_bd_open(struct inode *inode, struct file *filp) | |
1804 | { | |
1805 | struct gendisk *disk = inode->i_bdev->bd_disk; | |
f4800078 | 1806 | struct ub_lun *lun; |
1da177e4 LT |
1807 | struct ub_dev *sc; |
1808 | unsigned long flags; | |
1809 | int rc; | |
1810 | ||
f4800078 | 1811 | if ((lun = disk->private_data) == NULL) |
1da177e4 | 1812 | return -ENXIO; |
f4800078 PZ |
1813 | sc = lun->udev; |
1814 | ||
1da177e4 LT |
1815 | spin_lock_irqsave(&ub_lock, flags); |
1816 | if (atomic_read(&sc->poison)) { | |
1817 | spin_unlock_irqrestore(&ub_lock, flags); | |
1818 | return -ENXIO; | |
1819 | } | |
1820 | sc->openc++; | |
1821 | spin_unlock_irqrestore(&ub_lock, flags); | |
1822 | ||
1823 | /* | |
1824 | * This is a workaround for a specific problem in our block layer. | |
1825 | * In 2.6.9, register_disk duplicates the code from rescan_partitions. | |
1826 | * However, if we do add_disk with a device which persistently reports | |
1827 | * a changed media, add_disk calls register_disk, which does do_open, | |
1828 | * which will call rescan_paritions for changed media. After that, | |
1829 | * register_disk attempts to do it all again and causes double kobject | |
1830 | * registration and a eventually an oops on module removal. | |
1831 | * | |
1832 | * The bottom line is, Al Viro says that we should not allow | |
1833 | * bdev->bd_invalidated to be set when doing add_disk no matter what. | |
1834 | */ | |
f4800078 PZ |
1835 | if (lun->first_open) { |
1836 | lun->first_open = 0; | |
1837 | if (lun->changed) { | |
1da177e4 LT |
1838 | rc = -ENOMEDIUM; |
1839 | goto err_open; | |
1840 | } | |
1841 | } | |
1842 | ||
f4800078 | 1843 | if (lun->removable || lun->readonly) |
1da177e4 LT |
1844 | check_disk_change(inode->i_bdev); |
1845 | ||
1846 | /* | |
1847 | * The sd.c considers ->media_present and ->changed not equivalent, | |
1848 | * under some pretty murky conditions (a failure of READ CAPACITY). | |
1849 | * We may need it one day. | |
1850 | */ | |
f4800078 | 1851 | if (lun->removable && lun->changed && !(filp->f_flags & O_NDELAY)) { |
1da177e4 LT |
1852 | rc = -ENOMEDIUM; |
1853 | goto err_open; | |
1854 | } | |
1855 | ||
f4800078 | 1856 | if (lun->readonly && (filp->f_mode & FMODE_WRITE)) { |
1da177e4 LT |
1857 | rc = -EROFS; |
1858 | goto err_open; | |
1859 | } | |
1860 | ||
1861 | return 0; | |
1862 | ||
1863 | err_open: | |
1864 | ub_put(sc); | |
1865 | return rc; | |
1866 | } | |
1867 | ||
1868 | /* | |
1869 | */ | |
1870 | static int ub_bd_release(struct inode *inode, struct file *filp) | |
1871 | { | |
1872 | struct gendisk *disk = inode->i_bdev->bd_disk; | |
f4800078 PZ |
1873 | struct ub_lun *lun = disk->private_data; |
1874 | struct ub_dev *sc = lun->udev; | |
1da177e4 LT |
1875 | |
1876 | ub_put(sc); | |
1877 | return 0; | |
1878 | } | |
1879 | ||
1880 | /* | |
1881 | * The ioctl interface. | |
1882 | */ | |
1883 | static int ub_bd_ioctl(struct inode *inode, struct file *filp, | |
1884 | unsigned int cmd, unsigned long arg) | |
1885 | { | |
1886 | struct gendisk *disk = inode->i_bdev->bd_disk; | |
1887 | void __user *usermem = (void __user *) arg; | |
1888 | ||
1889 | return scsi_cmd_ioctl(filp, disk, cmd, usermem); | |
1890 | } | |
1891 | ||
1892 | /* | |
1893 | * This is called once a new disk was seen by the block layer or by ub_probe(). | |
1894 | * The main onjective here is to discover the features of the media such as | |
1895 | * the capacity, read-only status, etc. USB storage generally does not | |
1896 | * need to be spun up, but if we needed it, this would be the place. | |
1897 | * | |
1898 | * This call can sleep. | |
1899 | * | |
1900 | * The return code is not used. | |
1901 | */ | |
1902 | static int ub_bd_revalidate(struct gendisk *disk) | |
1903 | { | |
f4800078 PZ |
1904 | struct ub_lun *lun = disk->private_data; |
1905 | ||
1906 | ub_revalidate(lun->udev, lun); | |
1da177e4 LT |
1907 | |
1908 | /* XXX Support sector size switching like in sr.c */ | |
f4800078 PZ |
1909 | blk_queue_hardsect_size(disk->queue, lun->capacity.bsize); |
1910 | set_capacity(disk, lun->capacity.nsec); | |
1911 | // set_disk_ro(sdkp->disk, lun->readonly); | |
1da177e4 LT |
1912 | |
1913 | return 0; | |
1914 | } | |
1915 | ||
1916 | /* | |
1917 | * The check is called by the block layer to verify if the media | |
1918 | * is still available. It is supposed to be harmless, lightweight and | |
1919 | * non-intrusive in case the media was not changed. | |
1920 | * | |
1921 | * This call can sleep. | |
1922 | * | |
1923 | * The return code is bool! | |
1924 | */ | |
1925 | static int ub_bd_media_changed(struct gendisk *disk) | |
1926 | { | |
f4800078 | 1927 | struct ub_lun *lun = disk->private_data; |
1da177e4 | 1928 | |
f4800078 | 1929 | if (!lun->removable) |
1da177e4 LT |
1930 | return 0; |
1931 | ||
1932 | /* | |
1933 | * We clean checks always after every command, so this is not | |
1934 | * as dangerous as it looks. If the TEST_UNIT_READY fails here, | |
1935 | * the device is actually not ready with operator or software | |
1936 | * intervention required. One dangerous item might be a drive which | |
1937 | * spins itself down, and come the time to write dirty pages, this | |
1938 | * will fail, then block layer discards the data. Since we never | |
1939 | * spin drives up, such devices simply cannot be used with ub anyway. | |
1940 | */ | |
f4800078 PZ |
1941 | if (ub_sync_tur(lun->udev, lun) != 0) { |
1942 | lun->changed = 1; | |
1da177e4 LT |
1943 | return 1; |
1944 | } | |
1945 | ||
f4800078 | 1946 | return lun->changed; |
1da177e4 LT |
1947 | } |
1948 | ||
1949 | static struct block_device_operations ub_bd_fops = { | |
1950 | .owner = THIS_MODULE, | |
1951 | .open = ub_bd_open, | |
1952 | .release = ub_bd_release, | |
1953 | .ioctl = ub_bd_ioctl, | |
1954 | .media_changed = ub_bd_media_changed, | |
1955 | .revalidate_disk = ub_bd_revalidate, | |
1956 | }; | |
1957 | ||
1958 | /* | |
1959 | * Common ->done routine for commands executed synchronously. | |
1960 | */ | |
1961 | static void ub_probe_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd) | |
1962 | { | |
1963 | struct completion *cop = cmd->back; | |
1964 | complete(cop); | |
1965 | } | |
1966 | ||
1967 | /* | |
1968 | * Test if the device has a check condition on it, synchronously. | |
1969 | */ | |
f4800078 | 1970 | static int ub_sync_tur(struct ub_dev *sc, struct ub_lun *lun) |
1da177e4 LT |
1971 | { |
1972 | struct ub_scsi_cmd *cmd; | |
1973 | enum { ALLOC_SIZE = sizeof(struct ub_scsi_cmd) }; | |
1974 | unsigned long flags; | |
1975 | struct completion compl; | |
1976 | int rc; | |
1977 | ||
1978 | init_completion(&compl); | |
1979 | ||
1980 | rc = -ENOMEM; | |
1981 | if ((cmd = kmalloc(ALLOC_SIZE, GFP_KERNEL)) == NULL) | |
1982 | goto err_alloc; | |
1983 | memset(cmd, 0, ALLOC_SIZE); | |
1984 | ||
1985 | cmd->cdb[0] = TEST_UNIT_READY; | |
1986 | cmd->cdb_len = 6; | |
1987 | cmd->dir = UB_DIR_NONE; | |
1988 | cmd->state = UB_CMDST_INIT; | |
f4800078 | 1989 | cmd->lun = lun; /* This may be NULL, but that's ok */ |
1da177e4 LT |
1990 | cmd->done = ub_probe_done; |
1991 | cmd->back = &compl; | |
1992 | ||
1993 | spin_lock_irqsave(&sc->lock, flags); | |
1994 | cmd->tag = sc->tagcnt++; | |
1995 | ||
1996 | rc = ub_submit_scsi(sc, cmd); | |
1997 | spin_unlock_irqrestore(&sc->lock, flags); | |
1998 | ||
1999 | if (rc != 0) { | |
2000 | printk("ub: testing ready: submit error (%d)\n", rc); /* P3 */ | |
2001 | goto err_submit; | |
2002 | } | |
2003 | ||
2004 | wait_for_completion(&compl); | |
2005 | ||
2006 | rc = cmd->error; | |
2007 | ||
2008 | if (rc == -EIO && cmd->key != 0) /* Retries for benh's key */ | |
2009 | rc = cmd->key; | |
2010 | ||
2011 | err_submit: | |
2012 | kfree(cmd); | |
2013 | err_alloc: | |
2014 | return rc; | |
2015 | } | |
2016 | ||
2017 | /* | |
2018 | * Read the SCSI capacity synchronously (for probing). | |
2019 | */ | |
f4800078 PZ |
2020 | static int ub_sync_read_cap(struct ub_dev *sc, struct ub_lun *lun, |
2021 | struct ub_capacity *ret) | |
1da177e4 LT |
2022 | { |
2023 | struct ub_scsi_cmd *cmd; | |
a1cf96ef | 2024 | struct scatterlist *sg; |
1da177e4 LT |
2025 | char *p; |
2026 | enum { ALLOC_SIZE = sizeof(struct ub_scsi_cmd) + 8 }; | |
2027 | unsigned long flags; | |
2028 | unsigned int bsize, shift; | |
2029 | unsigned long nsec; | |
2030 | struct completion compl; | |
2031 | int rc; | |
2032 | ||
2033 | init_completion(&compl); | |
2034 | ||
2035 | rc = -ENOMEM; | |
2036 | if ((cmd = kmalloc(ALLOC_SIZE, GFP_KERNEL)) == NULL) | |
2037 | goto err_alloc; | |
2038 | memset(cmd, 0, ALLOC_SIZE); | |
2039 | p = (char *)cmd + sizeof(struct ub_scsi_cmd); | |
2040 | ||
2041 | cmd->cdb[0] = 0x25; | |
2042 | cmd->cdb_len = 10; | |
2043 | cmd->dir = UB_DIR_READ; | |
2044 | cmd->state = UB_CMDST_INIT; | |
a1cf96ef PZ |
2045 | cmd->nsg = 1; |
2046 | sg = &cmd->sgv[0]; | |
2047 | sg->page = virt_to_page(p); | |
38ffdd62 | 2048 | sg->offset = (unsigned long)p & (PAGE_SIZE-1); |
a1cf96ef | 2049 | sg->length = 8; |
1da177e4 | 2050 | cmd->len = 8; |
f4800078 | 2051 | cmd->lun = lun; |
1da177e4 LT |
2052 | cmd->done = ub_probe_done; |
2053 | cmd->back = &compl; | |
2054 | ||
2055 | spin_lock_irqsave(&sc->lock, flags); | |
2056 | cmd->tag = sc->tagcnt++; | |
2057 | ||
2058 | rc = ub_submit_scsi(sc, cmd); | |
2059 | spin_unlock_irqrestore(&sc->lock, flags); | |
2060 | ||
2061 | if (rc != 0) { | |
2062 | printk("ub: reading capacity: submit error (%d)\n", rc); /* P3 */ | |
2063 | goto err_submit; | |
2064 | } | |
2065 | ||
2066 | wait_for_completion(&compl); | |
2067 | ||
2068 | if (cmd->error != 0) { | |
2069 | printk("ub: reading capacity: error %d\n", cmd->error); /* P3 */ | |
2070 | rc = -EIO; | |
2071 | goto err_read; | |
2072 | } | |
2073 | if (cmd->act_len != 8) { | |
2074 | printk("ub: reading capacity: size %d\n", cmd->act_len); /* P3 */ | |
2075 | rc = -EIO; | |
2076 | goto err_read; | |
2077 | } | |
2078 | ||
2079 | /* sd.c special-cases sector size of 0 to mean 512. Needed? Safe? */ | |
2080 | nsec = be32_to_cpu(*(__be32 *)p) + 1; | |
2081 | bsize = be32_to_cpu(*(__be32 *)(p + 4)); | |
2082 | switch (bsize) { | |
2083 | case 512: shift = 0; break; | |
2084 | case 1024: shift = 1; break; | |
2085 | case 2048: shift = 2; break; | |
2086 | case 4096: shift = 3; break; | |
2087 | default: | |
2088 | printk("ub: Bad sector size %u\n", bsize); /* P3 */ | |
2089 | rc = -EDOM; | |
2090 | goto err_inv_bsize; | |
2091 | } | |
2092 | ||
2093 | ret->bsize = bsize; | |
2094 | ret->bshift = shift; | |
2095 | ret->nsec = nsec << shift; | |
2096 | rc = 0; | |
2097 | ||
2098 | err_inv_bsize: | |
2099 | err_read: | |
2100 | err_submit: | |
2101 | kfree(cmd); | |
2102 | err_alloc: | |
2103 | return rc; | |
2104 | } | |
2105 | ||
2106 | /* | |
2107 | */ | |
2108 | static void ub_probe_urb_complete(struct urb *urb, struct pt_regs *pt) | |
2109 | { | |
2110 | struct completion *cop = urb->context; | |
2111 | complete(cop); | |
2112 | } | |
2113 | ||
2114 | static void ub_probe_timeout(unsigned long arg) | |
2115 | { | |
2116 | struct completion *cop = (struct completion *) arg; | |
2117 | complete(cop); | |
2118 | } | |
2119 | ||
f4800078 PZ |
2120 | /* |
2121 | * Get number of LUNs by the way of Bulk GetMaxLUN command. | |
2122 | */ | |
2123 | static int ub_sync_getmaxlun(struct ub_dev *sc) | |
2124 | { | |
2125 | int ifnum = sc->intf->cur_altsetting->desc.bInterfaceNumber; | |
2126 | unsigned char *p; | |
2127 | enum { ALLOC_SIZE = 1 }; | |
2128 | struct usb_ctrlrequest *cr; | |
2129 | struct completion compl; | |
2130 | struct timer_list timer; | |
2131 | int nluns; | |
2132 | int rc; | |
2133 | ||
2134 | init_completion(&compl); | |
2135 | ||
2136 | rc = -ENOMEM; | |
2137 | if ((p = kmalloc(ALLOC_SIZE, GFP_KERNEL)) == NULL) | |
2138 | goto err_alloc; | |
2139 | *p = 55; | |
2140 | ||
2141 | cr = &sc->work_cr; | |
2142 | cr->bRequestType = USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE; | |
2143 | cr->bRequest = US_BULK_GET_MAX_LUN; | |
2144 | cr->wValue = cpu_to_le16(0); | |
2145 | cr->wIndex = cpu_to_le16(ifnum); | |
2146 | cr->wLength = cpu_to_le16(1); | |
2147 | ||
2148 | usb_fill_control_urb(&sc->work_urb, sc->dev, sc->recv_ctrl_pipe, | |
2149 | (unsigned char*) cr, p, 1, ub_probe_urb_complete, &compl); | |
f4800078 PZ |
2150 | sc->work_urb.actual_length = 0; |
2151 | sc->work_urb.error_count = 0; | |
2152 | sc->work_urb.status = 0; | |
2153 | ||
2154 | if ((rc = usb_submit_urb(&sc->work_urb, GFP_KERNEL)) != 0) { | |
2155 | if (rc == -EPIPE) { | |
64bd8453 | 2156 | printk("%s: Stall submitting GetMaxLUN, using 1 LUN\n", |
f4800078 PZ |
2157 | sc->name); /* P3 */ |
2158 | } else { | |
64bd8453 | 2159 | printk(KERN_NOTICE |
f4800078 PZ |
2160 | "%s: Unable to submit GetMaxLUN (%d)\n", |
2161 | sc->name, rc); | |
2162 | } | |
2163 | goto err_submit; | |
2164 | } | |
2165 | ||
2166 | init_timer(&timer); | |
2167 | timer.function = ub_probe_timeout; | |
2168 | timer.data = (unsigned long) &compl; | |
2169 | timer.expires = jiffies + UB_CTRL_TIMEOUT; | |
2170 | add_timer(&timer); | |
2171 | ||
2172 | wait_for_completion(&compl); | |
2173 | ||
2174 | del_timer_sync(&timer); | |
2175 | usb_kill_urb(&sc->work_urb); | |
2176 | ||
64bd8453 PZ |
2177 | if ((rc = sc->work_urb.status) < 0) { |
2178 | if (rc == -EPIPE) { | |
2179 | printk("%s: Stall at GetMaxLUN, using 1 LUN\n", | |
2180 | sc->name); /* P3 */ | |
2181 | } else { | |
2182 | printk(KERN_NOTICE | |
2183 | "%s: Error at GetMaxLUN (%d)\n", | |
2184 | sc->name, rc); | |
2185 | } | |
2186 | goto err_io; | |
2187 | } | |
2188 | ||
f4800078 PZ |
2189 | if (sc->work_urb.actual_length != 1) { |
2190 | printk("%s: GetMaxLUN returned %d bytes\n", sc->name, | |
2191 | sc->work_urb.actual_length); /* P3 */ | |
2192 | nluns = 0; | |
2193 | } else { | |
2194 | if ((nluns = *p) == 55) { | |
2195 | nluns = 0; | |
2196 | } else { | |
2197 | /* GetMaxLUN returns the maximum LUN number */ | |
2198 | nluns += 1; | |
2199 | if (nluns > UB_MAX_LUNS) | |
2200 | nluns = UB_MAX_LUNS; | |
2201 | } | |
2202 | printk("%s: GetMaxLUN returned %d, using %d LUNs\n", sc->name, | |
2203 | *p, nluns); /* P3 */ | |
2204 | } | |
2205 | ||
2206 | kfree(p); | |
2207 | return nluns; | |
2208 | ||
64bd8453 | 2209 | err_io: |
f4800078 PZ |
2210 | err_submit: |
2211 | kfree(p); | |
2212 | err_alloc: | |
2213 | return rc; | |
2214 | } | |
2215 | ||
1da177e4 LT |
2216 | /* |
2217 | * Clear initial stalls. | |
2218 | */ | |
2219 | static int ub_probe_clear_stall(struct ub_dev *sc, int stalled_pipe) | |
2220 | { | |
2221 | int endp; | |
2222 | struct usb_ctrlrequest *cr; | |
2223 | struct completion compl; | |
2224 | struct timer_list timer; | |
2225 | int rc; | |
2226 | ||
2227 | init_completion(&compl); | |
2228 | ||
2229 | endp = usb_pipeendpoint(stalled_pipe); | |
2230 | if (usb_pipein (stalled_pipe)) | |
2231 | endp |= USB_DIR_IN; | |
2232 | ||
2233 | cr = &sc->work_cr; | |
2234 | cr->bRequestType = USB_RECIP_ENDPOINT; | |
2235 | cr->bRequest = USB_REQ_CLEAR_FEATURE; | |
2236 | cr->wValue = cpu_to_le16(USB_ENDPOINT_HALT); | |
2237 | cr->wIndex = cpu_to_le16(endp); | |
2238 | cr->wLength = cpu_to_le16(0); | |
2239 | ||
2240 | usb_fill_control_urb(&sc->work_urb, sc->dev, sc->send_ctrl_pipe, | |
2241 | (unsigned char*) cr, NULL, 0, ub_probe_urb_complete, &compl); | |
1da177e4 LT |
2242 | sc->work_urb.actual_length = 0; |
2243 | sc->work_urb.error_count = 0; | |
2244 | sc->work_urb.status = 0; | |
2245 | ||
2246 | if ((rc = usb_submit_urb(&sc->work_urb, GFP_KERNEL)) != 0) { | |
2247 | printk(KERN_WARNING | |
2248 | "%s: Unable to submit a probe clear (%d)\n", sc->name, rc); | |
2249 | return rc; | |
2250 | } | |
2251 | ||
2252 | init_timer(&timer); | |
2253 | timer.function = ub_probe_timeout; | |
2254 | timer.data = (unsigned long) &compl; | |
2255 | timer.expires = jiffies + UB_CTRL_TIMEOUT; | |
2256 | add_timer(&timer); | |
2257 | ||
2258 | wait_for_completion(&compl); | |
2259 | ||
2260 | del_timer_sync(&timer); | |
2261 | usb_kill_urb(&sc->work_urb); | |
2262 | ||
2263 | /* reset the endpoint toggle */ | |
2264 | usb_settoggle(sc->dev, endp, usb_pipeout(sc->last_pipe), 0); | |
2265 | ||
2266 | return 0; | |
2267 | } | |
2268 | ||
2269 | /* | |
2270 | * Get the pipe settings. | |
2271 | */ | |
2272 | static int ub_get_pipes(struct ub_dev *sc, struct usb_device *dev, | |
2273 | struct usb_interface *intf) | |
2274 | { | |
2275 | struct usb_host_interface *altsetting = intf->cur_altsetting; | |
2276 | struct usb_endpoint_descriptor *ep_in = NULL; | |
2277 | struct usb_endpoint_descriptor *ep_out = NULL; | |
2278 | struct usb_endpoint_descriptor *ep; | |
2279 | int i; | |
2280 | ||
2281 | /* | |
2282 | * Find the endpoints we need. | |
2283 | * We are expecting a minimum of 2 endpoints - in and out (bulk). | |
2284 | * We will ignore any others. | |
2285 | */ | |
2286 | for (i = 0; i < altsetting->desc.bNumEndpoints; i++) { | |
2287 | ep = &altsetting->endpoint[i].desc; | |
2288 | ||
2289 | /* Is it a BULK endpoint? */ | |
2290 | if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) | |
2291 | == USB_ENDPOINT_XFER_BULK) { | |
2292 | /* BULK in or out? */ | |
2293 | if (ep->bEndpointAddress & USB_DIR_IN) | |
2294 | ep_in = ep; | |
2295 | else | |
2296 | ep_out = ep; | |
2297 | } | |
2298 | } | |
2299 | ||
2300 | if (ep_in == NULL || ep_out == NULL) { | |
f4800078 PZ |
2301 | printk(KERN_NOTICE "%s: failed endpoint check\n", |
2302 | sc->name); | |
2c26c9e6 | 2303 | return -ENODEV; |
1da177e4 LT |
2304 | } |
2305 | ||
2306 | /* Calculate and store the pipe values */ | |
2307 | sc->send_ctrl_pipe = usb_sndctrlpipe(dev, 0); | |
2308 | sc->recv_ctrl_pipe = usb_rcvctrlpipe(dev, 0); | |
2309 | sc->send_bulk_pipe = usb_sndbulkpipe(dev, | |
2310 | ep_out->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK); | |
2311 | sc->recv_bulk_pipe = usb_rcvbulkpipe(dev, | |
2312 | ep_in->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK); | |
2313 | ||
2314 | return 0; | |
2315 | } | |
2316 | ||
2317 | /* | |
2318 | * Probing is done in the process context, which allows us to cheat | |
2319 | * and not to build a state machine for the discovery. | |
2320 | */ | |
2321 | static int ub_probe(struct usb_interface *intf, | |
2322 | const struct usb_device_id *dev_id) | |
2323 | { | |
2324 | struct ub_dev *sc; | |
f4800078 | 2325 | int nluns; |
1da177e4 LT |
2326 | int rc; |
2327 | int i; | |
2328 | ||
a00828e9 PZ |
2329 | if (usb_usual_check_type(dev_id, USB_US_TYPE_UB)) |
2330 | return -ENXIO; | |
2331 | ||
1da177e4 LT |
2332 | rc = -ENOMEM; |
2333 | if ((sc = kmalloc(sizeof(struct ub_dev), GFP_KERNEL)) == NULL) | |
2334 | goto err_core; | |
2335 | memset(sc, 0, sizeof(struct ub_dev)); | |
2336 | spin_lock_init(&sc->lock); | |
f4800078 | 2337 | INIT_LIST_HEAD(&sc->luns); |
1da177e4 LT |
2338 | usb_init_urb(&sc->work_urb); |
2339 | tasklet_init(&sc->tasklet, ub_scsi_action, (unsigned long)sc); | |
2340 | atomic_set(&sc->poison, 0); | |
2c26c9e6 PZ |
2341 | INIT_WORK(&sc->reset_work, ub_reset_task, sc); |
2342 | init_waitqueue_head(&sc->reset_wait); | |
1da177e4 LT |
2343 | |
2344 | init_timer(&sc->work_timer); | |
2345 | sc->work_timer.data = (unsigned long) sc; | |
2346 | sc->work_timer.function = ub_urb_timeout; | |
2347 | ||
2348 | ub_init_completion(&sc->work_done); | |
2349 | sc->work_done.done = 1; /* A little yuk, but oh well... */ | |
2350 | ||
1da177e4 LT |
2351 | sc->dev = interface_to_usbdev(intf); |
2352 | sc->intf = intf; | |
2353 | // sc->ifnum = intf->cur_altsetting->desc.bInterfaceNumber; | |
1da177e4 LT |
2354 | usb_set_intfdata(intf, sc); |
2355 | usb_get_dev(sc->dev); | |
2356 | // usb_get_intf(sc->intf); /* Do we need this? */ | |
2357 | ||
f4800078 PZ |
2358 | snprintf(sc->name, 12, DRV_NAME "(%d.%d)", |
2359 | sc->dev->bus->busnum, sc->dev->devnum); | |
2360 | ||
1da177e4 LT |
2361 | /* XXX Verify that we can handle the device (from descriptors) */ |
2362 | ||
2c26c9e6 PZ |
2363 | if (ub_get_pipes(sc, sc->dev, intf) != 0) |
2364 | goto err_dev_desc; | |
1da177e4 LT |
2365 | |
2366 | if (device_create_file(&sc->intf->dev, &dev_attr_diag) != 0) | |
2367 | goto err_diag; | |
2368 | ||
2369 | /* | |
2370 | * At this point, all USB initialization is done, do upper layer. | |
2371 | * We really hate halfway initialized structures, so from the | |
2372 | * invariants perspective, this ub_dev is fully constructed at | |
2373 | * this point. | |
2374 | */ | |
2375 | ||
2376 | /* | |
2377 | * This is needed to clear toggles. It is a problem only if we do | |
2378 | * `rmmod ub && modprobe ub` without disconnects, but we like that. | |
2379 | */ | |
c6c88834 | 2380 | #if 0 /* iPod Mini fails if we do this (big white iPod works) */ |
1da177e4 LT |
2381 | ub_probe_clear_stall(sc, sc->recv_bulk_pipe); |
2382 | ub_probe_clear_stall(sc, sc->send_bulk_pipe); | |
c6c88834 | 2383 | #endif |
1da177e4 LT |
2384 | |
2385 | /* | |
2386 | * The way this is used by the startup code is a little specific. | |
2387 | * A SCSI check causes a USB stall. Our common case code sees it | |
2388 | * and clears the check, after which the device is ready for use. | |
2389 | * But if a check was not present, any command other than | |
2390 | * TEST_UNIT_READY ends with a lockup (including REQUEST_SENSE). | |
2391 | * | |
2392 | * If we neglect to clear the SCSI check, the first real command fails | |
2393 | * (which is the capacity readout). We clear that and retry, but why | |
2394 | * causing spurious retries for no reason. | |
2395 | * | |
2396 | * Revalidation may start with its own TEST_UNIT_READY, but that one | |
2397 | * has to succeed, so we clear checks with an additional one here. | |
2398 | * In any case it's not our business how revaliadation is implemented. | |
2399 | */ | |
2400 | for (i = 0; i < 3; i++) { /* Retries for benh's key */ | |
f4800078 | 2401 | if ((rc = ub_sync_tur(sc, NULL)) <= 0) break; |
1da177e4 LT |
2402 | if (rc != 0x6) break; |
2403 | msleep(10); | |
2404 | } | |
2405 | ||
f4800078 PZ |
2406 | nluns = 1; |
2407 | for (i = 0; i < 3; i++) { | |
2408 | if ((rc = ub_sync_getmaxlun(sc)) < 0) { | |
2409 | /* | |
64bd8453 PZ |
2410 | * This segment is taken from usb-storage. They say |
2411 | * that ZIP-100 needs this, but my own ZIP-100 works | |
2412 | * fine without this. | |
2413 | * Still, it does not seem to hurt anything. | |
f4800078 PZ |
2414 | */ |
2415 | if (rc == -EPIPE) { | |
2416 | ub_probe_clear_stall(sc, sc->recv_bulk_pipe); | |
2417 | ub_probe_clear_stall(sc, sc->send_bulk_pipe); | |
2418 | } | |
2419 | break; | |
2420 | } | |
2421 | if (rc != 0) { | |
2422 | nluns = rc; | |
2423 | break; | |
2424 | } | |
9f793d2c | 2425 | msleep(100); |
f4800078 | 2426 | } |
1da177e4 | 2427 | |
f4800078 PZ |
2428 | for (i = 0; i < nluns; i++) { |
2429 | ub_probe_lun(sc, i); | |
2430 | } | |
2431 | return 0; | |
2432 | ||
2433 | /* device_remove_file(&sc->intf->dev, &dev_attr_diag); */ | |
2434 | err_diag: | |
2c26c9e6 | 2435 | err_dev_desc: |
f4800078 PZ |
2436 | usb_set_intfdata(intf, NULL); |
2437 | // usb_put_intf(sc->intf); | |
2438 | usb_put_dev(sc->dev); | |
2439 | kfree(sc); | |
2440 | err_core: | |
2441 | return rc; | |
2442 | } | |
2443 | ||
2444 | static int ub_probe_lun(struct ub_dev *sc, int lnum) | |
2445 | { | |
2446 | struct ub_lun *lun; | |
2447 | request_queue_t *q; | |
2448 | struct gendisk *disk; | |
2449 | int rc; | |
2450 | ||
2451 | rc = -ENOMEM; | |
2452 | if ((lun = kmalloc(sizeof(struct ub_lun), GFP_KERNEL)) == NULL) | |
2453 | goto err_alloc; | |
2454 | memset(lun, 0, sizeof(struct ub_lun)); | |
2455 | lun->num = lnum; | |
2456 | ||
2457 | rc = -ENOSR; | |
2458 | if ((lun->id = ub_id_get()) == -1) | |
2459 | goto err_id; | |
2460 | ||
2461 | lun->udev = sc; | |
2462 | list_add(&lun->link, &sc->luns); | |
2463 | ||
2464 | snprintf(lun->name, 16, DRV_NAME "%c(%d.%d.%d)", | |
2465 | lun->id + 'a', sc->dev->bus->busnum, sc->dev->devnum, lun->num); | |
2466 | ||
2467 | lun->removable = 1; /* XXX Query this from the device */ | |
2468 | lun->changed = 1; /* ub_revalidate clears only */ | |
2469 | lun->first_open = 1; | |
2470 | ub_revalidate(sc, lun); | |
1da177e4 | 2471 | |
1da177e4 | 2472 | rc = -ENOMEM; |
4fb729f5 | 2473 | if ((disk = alloc_disk(UB_PARTS_PER_LUN)) == NULL) |
1da177e4 LT |
2474 | goto err_diskalloc; |
2475 | ||
f4800078 PZ |
2476 | lun->disk = disk; |
2477 | sprintf(disk->disk_name, DRV_NAME "%c", lun->id + 'a'); | |
2478 | sprintf(disk->devfs_name, DEVFS_NAME "/%c", lun->id + 'a'); | |
1da177e4 | 2479 | disk->major = UB_MAJOR; |
4fb729f5 | 2480 | disk->first_minor = lun->id * UB_PARTS_PER_LUN; |
1da177e4 | 2481 | disk->fops = &ub_bd_fops; |
f4800078 | 2482 | disk->private_data = lun; |
64bd8453 | 2483 | disk->driverfs_dev = &sc->intf->dev; |
1da177e4 LT |
2484 | |
2485 | rc = -ENOMEM; | |
6c1eb8c1 | 2486 | if ((q = blk_init_queue(ub_request_fn, &sc->lock)) == NULL) |
1da177e4 LT |
2487 | goto err_blkqinit; |
2488 | ||
2489 | disk->queue = q; | |
2490 | ||
f4800078 | 2491 | blk_queue_bounce_limit(q, BLK_BOUNCE_HIGH); |
1da177e4 LT |
2492 | blk_queue_max_hw_segments(q, UB_MAX_REQ_SG); |
2493 | blk_queue_max_phys_segments(q, UB_MAX_REQ_SG); | |
f4800078 | 2494 | blk_queue_segment_boundary(q, 0xffffffff); /* Dubious. */ |
1da177e4 | 2495 | blk_queue_max_sectors(q, UB_MAX_SECTORS); |
f4800078 | 2496 | blk_queue_hardsect_size(q, lun->capacity.bsize); |
1da177e4 | 2497 | |
f4800078 | 2498 | q->queuedata = lun; |
1da177e4 | 2499 | |
f4800078 PZ |
2500 | set_capacity(disk, lun->capacity.nsec); |
2501 | if (lun->removable) | |
1da177e4 LT |
2502 | disk->flags |= GENHD_FL_REMOVABLE; |
2503 | ||
2504 | add_disk(disk); | |
2505 | ||
2506 | return 0; | |
2507 | ||
2508 | err_blkqinit: | |
2509 | put_disk(disk); | |
2510 | err_diskalloc: | |
f4800078 PZ |
2511 | list_del(&lun->link); |
2512 | ub_id_put(lun->id); | |
1da177e4 | 2513 | err_id: |
f4800078 PZ |
2514 | kfree(lun); |
2515 | err_alloc: | |
1da177e4 LT |
2516 | return rc; |
2517 | } | |
2518 | ||
2519 | static void ub_disconnect(struct usb_interface *intf) | |
2520 | { | |
2521 | struct ub_dev *sc = usb_get_intfdata(intf); | |
f4800078 PZ |
2522 | struct list_head *p; |
2523 | struct ub_lun *lun; | |
2524 | struct gendisk *disk; | |
1da177e4 LT |
2525 | unsigned long flags; |
2526 | ||
2527 | /* | |
2528 | * Prevent ub_bd_release from pulling the rug from under us. | |
2529 | * XXX This is starting to look like a kref. | |
2530 | * XXX Why not to take this ref at probe time? | |
2531 | */ | |
2532 | spin_lock_irqsave(&ub_lock, flags); | |
2533 | sc->openc++; | |
2534 | spin_unlock_irqrestore(&ub_lock, flags); | |
2535 | ||
2536 | /* | |
2537 | * Fence stall clearnings, operations triggered by unlinkings and so on. | |
2538 | * We do not attempt to unlink any URBs, because we do not trust the | |
2539 | * unlink paths in HC drivers. Also, we get -84 upon disconnect anyway. | |
2540 | */ | |
2541 | atomic_set(&sc->poison, 1); | |
2542 | ||
2c26c9e6 PZ |
2543 | /* |
2544 | * Wait for reset to end, if any. | |
2545 | */ | |
2546 | wait_event(sc->reset_wait, !sc->reset); | |
2547 | ||
1da177e4 LT |
2548 | /* |
2549 | * Blow away queued commands. | |
2550 | * | |
2551 | * Actually, this never works, because before we get here | |
2552 | * the HCD terminates outstanding URB(s). It causes our | |
2553 | * SCSI command queue to advance, commands fail to submit, | |
2554 | * and the whole queue drains. So, we just use this code to | |
2555 | * print warnings. | |
2556 | */ | |
2557 | spin_lock_irqsave(&sc->lock, flags); | |
2558 | { | |
2559 | struct ub_scsi_cmd *cmd; | |
2560 | int cnt = 0; | |
2c26c9e6 | 2561 | while ((cmd = ub_cmdq_peek(sc)) != NULL) { |
1da177e4 LT |
2562 | cmd->error = -ENOTCONN; |
2563 | cmd->state = UB_CMDST_DONE; | |
2564 | ub_cmdtr_state(sc, cmd); | |
2565 | ub_cmdq_pop(sc); | |
2566 | (*cmd->done)(sc, cmd); | |
2567 | cnt++; | |
2568 | } | |
2569 | if (cnt != 0) { | |
2570 | printk(KERN_WARNING "%s: " | |
2571 | "%d was queued after shutdown\n", sc->name, cnt); | |
2572 | } | |
2573 | } | |
2574 | spin_unlock_irqrestore(&sc->lock, flags); | |
2575 | ||
2576 | /* | |
2577 | * Unregister the upper layer. | |
2578 | */ | |
f4800078 PZ |
2579 | list_for_each (p, &sc->luns) { |
2580 | lun = list_entry(p, struct ub_lun, link); | |
2581 | disk = lun->disk; | |
2582 | if (disk->flags & GENHD_FL_UP) | |
2583 | del_gendisk(disk); | |
2584 | /* | |
2585 | * I wish I could do: | |
2586 | * set_bit(QUEUE_FLAG_DEAD, &q->queue_flags); | |
2587 | * As it is, we rely on our internal poisoning and let | |
2588 | * the upper levels to spin furiously failing all the I/O. | |
2589 | */ | |
2590 | } | |
1da177e4 LT |
2591 | |
2592 | /* | |
2593 | * Taking a lock on a structure which is about to be freed | |
2594 | * is very nonsensual. Here it is largely a way to do a debug freeze, | |
2595 | * and a bracket which shows where the nonsensual code segment ends. | |
2596 | * | |
2597 | * Testing for -EINPROGRESS is always a bug, so we are bending | |
2598 | * the rules a little. | |
2599 | */ | |
2600 | spin_lock_irqsave(&sc->lock, flags); | |
2601 | if (sc->work_urb.status == -EINPROGRESS) { /* janitors: ignore */ | |
2602 | printk(KERN_WARNING "%s: " | |
2603 | "URB is active after disconnect\n", sc->name); | |
2604 | } | |
2605 | spin_unlock_irqrestore(&sc->lock, flags); | |
2606 | ||
2607 | /* | |
2608 | * There is virtually no chance that other CPU runs times so long | |
2609 | * after ub_urb_complete should have called del_timer, but only if HCD | |
2610 | * didn't forget to deliver a callback on unlink. | |
2611 | */ | |
2612 | del_timer_sync(&sc->work_timer); | |
2613 | ||
2614 | /* | |
2615 | * At this point there must be no commands coming from anyone | |
2616 | * and no URBs left in transit. | |
2617 | */ | |
2618 | ||
2619 | device_remove_file(&sc->intf->dev, &dev_attr_diag); | |
2620 | usb_set_intfdata(intf, NULL); | |
2621 | // usb_put_intf(sc->intf); | |
2622 | sc->intf = NULL; | |
2623 | usb_put_dev(sc->dev); | |
2624 | sc->dev = NULL; | |
2625 | ||
2626 | ub_put(sc); | |
2627 | } | |
2628 | ||
2629 | static struct usb_driver ub_driver = { | |
1da177e4 LT |
2630 | .name = "ub", |
2631 | .probe = ub_probe, | |
2632 | .disconnect = ub_disconnect, | |
2633 | .id_table = ub_usb_ids, | |
2634 | }; | |
2635 | ||
2636 | static int __init ub_init(void) | |
2637 | { | |
2638 | int rc; | |
2639 | ||
1da177e4 LT |
2640 | if ((rc = register_blkdev(UB_MAJOR, DRV_NAME)) != 0) |
2641 | goto err_regblkdev; | |
2642 | devfs_mk_dir(DEVFS_NAME); | |
2643 | ||
2644 | if ((rc = usb_register(&ub_driver)) != 0) | |
2645 | goto err_register; | |
2646 | ||
a00828e9 | 2647 | usb_usual_set_present(USB_US_TYPE_UB); |
1da177e4 LT |
2648 | return 0; |
2649 | ||
2650 | err_register: | |
2651 | devfs_remove(DEVFS_NAME); | |
2652 | unregister_blkdev(UB_MAJOR, DRV_NAME); | |
2653 | err_regblkdev: | |
2654 | return rc; | |
2655 | } | |
2656 | ||
2657 | static void __exit ub_exit(void) | |
2658 | { | |
2659 | usb_deregister(&ub_driver); | |
2660 | ||
2661 | devfs_remove(DEVFS_NAME); | |
2662 | unregister_blkdev(UB_MAJOR, DRV_NAME); | |
a00828e9 | 2663 | usb_usual_clear_present(USB_US_TYPE_UB); |
1da177e4 LT |
2664 | } |
2665 | ||
2666 | module_init(ub_init); | |
2667 | module_exit(ub_exit); | |
2668 | ||
2669 | MODULE_LICENSE("GPL"); |