Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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) |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 11 | * -- Kill first_open (Al Viro fixed the block layer now) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | * -- Do resets with usb_device_reset (needs a thread context, use khubd) |
| 13 | * -- set readonly flag for CDs, set removable flag for CF readers |
| 14 | * -- do inquiry and verify we got a disk and not a tape (for LUN mismatch) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | * -- special case some senses, e.g. 3a/0 -> no media present, reduce retries |
| 16 | * -- verify the 13 conditions and do bulk resets |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | * -- kill last_pipe and simply do two-state clearing on both pipes |
| 18 | * -- verify protocol (bulk) from USB descriptors (maybe...) |
| 19 | * -- highmem and sg |
| 20 | * -- move top_sense and work_bcs into separate allocations (if they survive) |
| 21 | * for cache purists and esoteric architectures. |
| 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=? |
| 26 | */ |
| 27 | #include <linux/kernel.h> |
| 28 | #include <linux/module.h> |
| 29 | #include <linux/usb.h> |
| 30 | #include <linux/blkdev.h> |
| 31 | #include <linux/devfs_fs_kernel.h> |
| 32 | #include <linux/timer.h> |
| 33 | #include <scsi/scsi.h> |
| 34 | |
| 35 | #define DRV_NAME "ub" |
| 36 | #define DEVFS_NAME DRV_NAME |
| 37 | |
| 38 | #define UB_MAJOR 180 |
| 39 | |
| 40 | /* |
| 41 | * Definitions which have to be scattered once we understand the layout better. |
| 42 | */ |
| 43 | |
| 44 | /* Transport (despite PR in the name) */ |
| 45 | #define US_PR_BULK 0x50 /* bulk only */ |
| 46 | |
| 47 | /* Protocol */ |
| 48 | #define US_SC_SCSI 0x06 /* Transparent */ |
| 49 | |
| 50 | /* |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 51 | * This many LUNs per USB device. |
| 52 | * Every one of them takes a host, see UB_MAX_HOSTS. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | */ |
Pete Zaitcev | 9f793d2 | 2005-06-06 13:54:59 -0700 | [diff] [blame] | 54 | #define UB_MAX_LUNS 9 |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 55 | |
| 56 | /* |
| 57 | */ |
| 58 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | #define UB_MINORS_PER_MAJOR 8 |
| 60 | |
| 61 | #define UB_MAX_CDB_SIZE 16 /* Corresponds to Bulk */ |
| 62 | |
| 63 | #define UB_SENSE_SIZE 18 |
| 64 | |
| 65 | /* |
| 66 | */ |
| 67 | |
| 68 | /* command block wrapper */ |
| 69 | struct bulk_cb_wrap { |
| 70 | __le32 Signature; /* contains 'USBC' */ |
| 71 | u32 Tag; /* unique per command id */ |
| 72 | __le32 DataTransferLength; /* size of data */ |
| 73 | u8 Flags; /* direction in bit 0 */ |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 74 | u8 Lun; /* LUN */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | u8 Length; /* of of the CDB */ |
| 76 | u8 CDB[UB_MAX_CDB_SIZE]; /* max command */ |
| 77 | }; |
| 78 | |
| 79 | #define US_BULK_CB_WRAP_LEN 31 |
| 80 | #define US_BULK_CB_SIGN 0x43425355 /*spells out USBC */ |
| 81 | #define US_BULK_FLAG_IN 1 |
| 82 | #define US_BULK_FLAG_OUT 0 |
| 83 | |
| 84 | /* command status wrapper */ |
| 85 | struct bulk_cs_wrap { |
| 86 | __le32 Signature; /* should = 'USBS' */ |
| 87 | u32 Tag; /* same as original command */ |
| 88 | __le32 Residue; /* amount not transferred */ |
| 89 | u8 Status; /* see below */ |
| 90 | }; |
| 91 | |
| 92 | #define US_BULK_CS_WRAP_LEN 13 |
| 93 | #define US_BULK_CS_SIGN 0x53425355 /* spells out 'USBS' */ |
| 94 | /* This is for Olympus Camedia digital cameras */ |
| 95 | #define US_BULK_CS_OLYMPUS_SIGN 0x55425355 /* spells out 'USBU' */ |
| 96 | #define US_BULK_STAT_OK 0 |
| 97 | #define US_BULK_STAT_FAIL 1 |
| 98 | #define US_BULK_STAT_PHASE 2 |
| 99 | |
| 100 | /* bulk-only class specific requests */ |
| 101 | #define US_BULK_RESET_REQUEST 0xff |
| 102 | #define US_BULK_GET_MAX_LUN 0xfe |
| 103 | |
| 104 | /* |
| 105 | */ |
| 106 | struct ub_dev; |
| 107 | |
| 108 | #define UB_MAX_REQ_SG 1 |
| 109 | #define UB_MAX_SECTORS 64 |
| 110 | |
| 111 | /* |
| 112 | * A second is more than enough for a 32K transfer (UB_MAX_SECTORS) |
| 113 | * even if a webcam hogs the bus, but some devices need time to spin up. |
| 114 | */ |
| 115 | #define UB_URB_TIMEOUT (HZ*2) |
| 116 | #define UB_DATA_TIMEOUT (HZ*5) /* ZIP does spin-ups in the data phase */ |
| 117 | #define UB_STAT_TIMEOUT (HZ*5) /* Same spinups and eject for a dataless cmd. */ |
| 118 | #define UB_CTRL_TIMEOUT (HZ/2) /* 500ms ought to be enough to clear a stall */ |
| 119 | |
| 120 | /* |
| 121 | * An instance of a SCSI command in transit. |
| 122 | */ |
| 123 | #define UB_DIR_NONE 0 |
| 124 | #define UB_DIR_READ 1 |
| 125 | #define UB_DIR_ILLEGAL2 2 |
| 126 | #define UB_DIR_WRITE 3 |
| 127 | |
| 128 | #define UB_DIR_CHAR(c) (((c)==UB_DIR_WRITE)? 'w': \ |
| 129 | (((c)==UB_DIR_READ)? 'r': 'n')) |
| 130 | |
| 131 | enum ub_scsi_cmd_state { |
| 132 | UB_CMDST_INIT, /* Initial state */ |
| 133 | UB_CMDST_CMD, /* Command submitted */ |
| 134 | UB_CMDST_DATA, /* Data phase */ |
| 135 | UB_CMDST_CLR2STS, /* Clearing before requesting status */ |
| 136 | UB_CMDST_STAT, /* Status phase */ |
| 137 | UB_CMDST_CLEAR, /* Clearing a stall (halt, actually) */ |
| 138 | UB_CMDST_SENSE, /* Sending Request Sense */ |
| 139 | UB_CMDST_DONE /* Final state */ |
| 140 | }; |
| 141 | |
| 142 | static char *ub_scsi_cmd_stname[] = { |
| 143 | ". ", |
| 144 | "Cmd", |
| 145 | "dat", |
| 146 | "c2s", |
| 147 | "sts", |
| 148 | "clr", |
| 149 | "Sen", |
| 150 | "fin" |
| 151 | }; |
| 152 | |
| 153 | struct ub_scsi_cmd { |
| 154 | unsigned char cdb[UB_MAX_CDB_SIZE]; |
| 155 | unsigned char cdb_len; |
| 156 | |
| 157 | unsigned char dir; /* 0 - none, 1 - read, 3 - write. */ |
| 158 | unsigned char trace_index; |
| 159 | enum ub_scsi_cmd_state state; |
| 160 | unsigned int tag; |
| 161 | struct ub_scsi_cmd *next; |
| 162 | |
| 163 | int error; /* Return code - valid upon done */ |
| 164 | unsigned int act_len; /* Return size */ |
| 165 | unsigned char key, asc, ascq; /* May be valid if error==-EIO */ |
| 166 | |
| 167 | int stat_count; /* Retries getting status. */ |
| 168 | |
| 169 | /* |
| 170 | * We do not support transfers from highmem pages |
| 171 | * because the underlying USB framework does not do what we need. |
| 172 | */ |
| 173 | char *data; /* Requested buffer */ |
| 174 | unsigned int len; /* Requested length */ |
| 175 | // struct scatterlist sgv[UB_MAX_REQ_SG]; |
| 176 | |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 177 | struct ub_lun *lun; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | void (*done)(struct ub_dev *, struct ub_scsi_cmd *); |
| 179 | void *back; |
| 180 | }; |
| 181 | |
| 182 | /* |
| 183 | */ |
| 184 | struct ub_capacity { |
| 185 | unsigned long nsec; /* Linux size - 512 byte sectors */ |
| 186 | unsigned int bsize; /* Linux hardsect_size */ |
| 187 | unsigned int bshift; /* Shift between 512 and hard sects */ |
| 188 | }; |
| 189 | |
| 190 | /* |
| 191 | * The SCSI command tracing structure. |
| 192 | */ |
| 193 | |
| 194 | #define SCMD_ST_HIST_SZ 8 |
| 195 | #define SCMD_TRACE_SZ 63 /* Less than 4KB of 61-byte lines */ |
| 196 | |
| 197 | struct ub_scsi_cmd_trace { |
| 198 | int hcur; |
| 199 | unsigned int tag; |
| 200 | unsigned int req_size, act_size; |
| 201 | unsigned char op; |
| 202 | unsigned char dir; |
| 203 | unsigned char key, asc, ascq; |
| 204 | char st_hst[SCMD_ST_HIST_SZ]; |
| 205 | }; |
| 206 | |
| 207 | struct ub_scsi_trace { |
| 208 | int cur; |
| 209 | struct ub_scsi_cmd_trace vec[SCMD_TRACE_SZ]; |
| 210 | }; |
| 211 | |
| 212 | /* |
| 213 | * This is a direct take-off from linux/include/completion.h |
| 214 | * The difference is that I do not wait on this thing, just poll. |
| 215 | * When I want to wait (ub_probe), I just use the stock completion. |
| 216 | * |
| 217 | * Note that INIT_COMPLETION takes no lock. It is correct. But why |
| 218 | * in the bloody hell that thing takes struct instead of pointer to struct |
| 219 | * is quite beyond me. I just copied it from the stock completion. |
| 220 | */ |
| 221 | struct ub_completion { |
| 222 | unsigned int done; |
| 223 | spinlock_t lock; |
| 224 | }; |
| 225 | |
| 226 | static inline void ub_init_completion(struct ub_completion *x) |
| 227 | { |
| 228 | x->done = 0; |
| 229 | spin_lock_init(&x->lock); |
| 230 | } |
| 231 | |
| 232 | #define UB_INIT_COMPLETION(x) ((x).done = 0) |
| 233 | |
| 234 | static void ub_complete(struct ub_completion *x) |
| 235 | { |
| 236 | unsigned long flags; |
| 237 | |
| 238 | spin_lock_irqsave(&x->lock, flags); |
| 239 | x->done++; |
| 240 | spin_unlock_irqrestore(&x->lock, flags); |
| 241 | } |
| 242 | |
| 243 | static int ub_is_completed(struct ub_completion *x) |
| 244 | { |
| 245 | unsigned long flags; |
| 246 | int ret; |
| 247 | |
| 248 | spin_lock_irqsave(&x->lock, flags); |
| 249 | ret = x->done; |
| 250 | spin_unlock_irqrestore(&x->lock, flags); |
| 251 | return ret; |
| 252 | } |
| 253 | |
| 254 | /* |
| 255 | */ |
| 256 | struct ub_scsi_cmd_queue { |
| 257 | int qlen, qmax; |
| 258 | struct ub_scsi_cmd *head, *tail; |
| 259 | }; |
| 260 | |
| 261 | /* |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 262 | * The block device instance (one per LUN). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | */ |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 264 | struct ub_lun { |
| 265 | struct ub_dev *udev; |
| 266 | struct list_head link; |
| 267 | struct gendisk *disk; |
| 268 | int id; /* Host index */ |
| 269 | int num; /* LUN number */ |
| 270 | char name[16]; |
| 271 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | int changed; /* Media was changed */ |
| 273 | int removable; |
| 274 | int readonly; |
| 275 | int first_open; /* Kludge. See ub_bd_open. */ |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 276 | |
| 277 | /* Use Ingo's mempool if or when we have more than one command. */ |
| 278 | /* |
| 279 | * Currently we never need more than one command for the whole device. |
| 280 | * However, giving every LUN a command is a cheap and automatic way |
| 281 | * to enforce fairness between them. |
| 282 | */ |
| 283 | int cmda[1]; |
| 284 | struct ub_scsi_cmd cmdv[1]; |
| 285 | |
| 286 | struct ub_capacity capacity; |
| 287 | }; |
| 288 | |
| 289 | /* |
| 290 | * The USB device instance. |
| 291 | */ |
| 292 | struct ub_dev { |
| 293 | spinlock_t lock; |
| 294 | atomic_t poison; /* The USB device is disconnected */ |
| 295 | int openc; /* protected by ub_lock! */ |
| 296 | /* kref is too implicit for our taste */ |
| 297 | unsigned int tagcnt; |
| 298 | char name[12]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | struct usb_device *dev; |
| 300 | struct usb_interface *intf; |
| 301 | |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 302 | struct list_head luns; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | |
| 304 | unsigned int send_bulk_pipe; /* cached pipe values */ |
| 305 | unsigned int recv_bulk_pipe; |
| 306 | unsigned int send_ctrl_pipe; |
| 307 | unsigned int recv_ctrl_pipe; |
| 308 | |
| 309 | struct tasklet_struct tasklet; |
| 310 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | struct ub_scsi_cmd_queue cmd_queue; |
| 312 | struct ub_scsi_cmd top_rqs_cmd; /* REQUEST SENSE */ |
| 313 | unsigned char top_sense[UB_SENSE_SIZE]; |
| 314 | |
| 315 | struct ub_completion work_done; |
| 316 | struct urb work_urb; |
| 317 | struct timer_list work_timer; |
| 318 | int last_pipe; /* What might need clearing */ |
| 319 | struct bulk_cb_wrap work_bcb; |
| 320 | struct bulk_cs_wrap work_bcs; |
| 321 | struct usb_ctrlrequest work_cr; |
| 322 | |
| 323 | struct ub_scsi_trace tr; |
| 324 | }; |
| 325 | |
| 326 | /* |
| 327 | */ |
| 328 | static void ub_cleanup(struct ub_dev *sc); |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 329 | static int ub_bd_rq_fn_1(struct ub_lun *lun, struct request *rq); |
| 330 | static int ub_cmd_build_block(struct ub_dev *sc, struct ub_lun *lun, |
| 331 | struct ub_scsi_cmd *cmd, struct request *rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | static int ub_cmd_build_packet(struct ub_dev *sc, struct ub_scsi_cmd *cmd, |
| 333 | struct request *rq); |
| 334 | static void ub_rw_cmd_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd); |
| 335 | static void ub_end_rq(struct request *rq, int uptodate); |
| 336 | static int ub_submit_scsi(struct ub_dev *sc, struct ub_scsi_cmd *cmd); |
| 337 | static void ub_urb_complete(struct urb *urb, struct pt_regs *pt); |
| 338 | static void ub_scsi_action(unsigned long _dev); |
| 339 | static void ub_scsi_dispatch(struct ub_dev *sc); |
| 340 | static void ub_scsi_urb_compl(struct ub_dev *sc, struct ub_scsi_cmd *cmd); |
| 341 | static void ub_state_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd, int rc); |
| 342 | static void __ub_state_stat(struct ub_dev *sc, struct ub_scsi_cmd *cmd); |
| 343 | static void ub_state_stat(struct ub_dev *sc, struct ub_scsi_cmd *cmd); |
| 344 | static void ub_state_sense(struct ub_dev *sc, struct ub_scsi_cmd *cmd); |
| 345 | static int ub_submit_clear_stall(struct ub_dev *sc, struct ub_scsi_cmd *cmd, |
| 346 | int stalled_pipe); |
| 347 | static void ub_top_sense_done(struct ub_dev *sc, struct ub_scsi_cmd *scmd); |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 348 | static int ub_sync_tur(struct ub_dev *sc, struct ub_lun *lun); |
| 349 | static int ub_sync_read_cap(struct ub_dev *sc, struct ub_lun *lun, |
| 350 | struct ub_capacity *ret); |
| 351 | static int ub_probe_lun(struct ub_dev *sc, int lnum); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | |
| 353 | /* |
| 354 | */ |
| 355 | static struct usb_device_id ub_usb_ids[] = { |
| 356 | // { USB_DEVICE_VER(0x0781, 0x0002, 0x0009, 0x0009) }, /* SDDR-31 */ |
| 357 | { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_SCSI, US_PR_BULK) }, |
| 358 | { } |
| 359 | }; |
| 360 | |
| 361 | MODULE_DEVICE_TABLE(usb, ub_usb_ids); |
| 362 | |
| 363 | /* |
| 364 | * Find me a way to identify "next free minor" for add_disk(), |
| 365 | * and the array disappears the next day. However, the number of |
| 366 | * hosts has something to do with the naming and /proc/partitions. |
| 367 | * This has to be thought out in detail before changing. |
| 368 | * If UB_MAX_HOST was 1000, we'd use a bitmap. Or a better data structure. |
| 369 | */ |
| 370 | #define UB_MAX_HOSTS 26 |
| 371 | static char ub_hostv[UB_MAX_HOSTS]; |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 372 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | static DEFINE_SPINLOCK(ub_lock); /* Locks globals and ->openc */ |
| 374 | |
| 375 | /* |
| 376 | * The SCSI command tracing procedures. |
| 377 | */ |
| 378 | |
| 379 | static void ub_cmdtr_new(struct ub_dev *sc, struct ub_scsi_cmd *cmd) |
| 380 | { |
| 381 | int n; |
| 382 | struct ub_scsi_cmd_trace *t; |
| 383 | |
| 384 | if ((n = sc->tr.cur + 1) == SCMD_TRACE_SZ) n = 0; |
| 385 | t = &sc->tr.vec[n]; |
| 386 | |
| 387 | memset(t, 0, sizeof(struct ub_scsi_cmd_trace)); |
| 388 | t->tag = cmd->tag; |
| 389 | t->op = cmd->cdb[0]; |
| 390 | t->dir = cmd->dir; |
| 391 | t->req_size = cmd->len; |
| 392 | t->st_hst[0] = cmd->state; |
| 393 | |
| 394 | sc->tr.cur = n; |
| 395 | cmd->trace_index = n; |
| 396 | } |
| 397 | |
| 398 | static void ub_cmdtr_state(struct ub_dev *sc, struct ub_scsi_cmd *cmd) |
| 399 | { |
| 400 | int n; |
| 401 | struct ub_scsi_cmd_trace *t; |
| 402 | |
| 403 | t = &sc->tr.vec[cmd->trace_index]; |
| 404 | if (t->tag == cmd->tag) { |
| 405 | if ((n = t->hcur + 1) == SCMD_ST_HIST_SZ) n = 0; |
| 406 | t->st_hst[n] = cmd->state; |
| 407 | t->hcur = n; |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | static void ub_cmdtr_act_len(struct ub_dev *sc, struct ub_scsi_cmd *cmd) |
| 412 | { |
| 413 | struct ub_scsi_cmd_trace *t; |
| 414 | |
| 415 | t = &sc->tr.vec[cmd->trace_index]; |
| 416 | if (t->tag == cmd->tag) |
| 417 | t->act_size = cmd->act_len; |
| 418 | } |
| 419 | |
| 420 | static void ub_cmdtr_sense(struct ub_dev *sc, struct ub_scsi_cmd *cmd, |
| 421 | unsigned char *sense) |
| 422 | { |
| 423 | struct ub_scsi_cmd_trace *t; |
| 424 | |
| 425 | t = &sc->tr.vec[cmd->trace_index]; |
| 426 | if (t->tag == cmd->tag) { |
| 427 | t->key = sense[2] & 0x0F; |
| 428 | t->asc = sense[12]; |
| 429 | t->ascq = sense[13]; |
| 430 | } |
| 431 | } |
| 432 | |
Yani Ioannou | 74880c0 | 2005-05-17 06:41:12 -0400 | [diff] [blame] | 433 | static ssize_t ub_diag_show(struct device *dev, struct device_attribute *attr, char *page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | { |
| 435 | struct usb_interface *intf; |
| 436 | struct ub_dev *sc; |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 437 | struct list_head *p; |
| 438 | struct ub_lun *lun; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | int cnt; |
| 440 | unsigned long flags; |
| 441 | int nc, nh; |
| 442 | int i, j; |
| 443 | struct ub_scsi_cmd_trace *t; |
| 444 | |
| 445 | intf = to_usb_interface(dev); |
| 446 | sc = usb_get_intfdata(intf); |
| 447 | if (sc == NULL) |
| 448 | return 0; |
| 449 | |
| 450 | cnt = 0; |
| 451 | spin_lock_irqsave(&sc->lock, flags); |
| 452 | |
| 453 | cnt += sprintf(page + cnt, |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 454 | "qlen %d qmax %d\n", |
| 455 | sc->cmd_queue.qlen, sc->cmd_queue.qmax); |
| 456 | |
| 457 | list_for_each (p, &sc->luns) { |
| 458 | lun = list_entry(p, struct ub_lun, link); |
| 459 | cnt += sprintf(page + cnt, |
| 460 | "lun %u changed %d removable %d readonly %d\n", |
| 461 | lun->num, lun->changed, lun->removable, lun->readonly); |
| 462 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | |
| 464 | if ((nc = sc->tr.cur + 1) == SCMD_TRACE_SZ) nc = 0; |
| 465 | for (j = 0; j < SCMD_TRACE_SZ; j++) { |
| 466 | t = &sc->tr.vec[nc]; |
| 467 | |
| 468 | cnt += sprintf(page + cnt, "%08x %02x", t->tag, t->op); |
| 469 | if (t->op == REQUEST_SENSE) { |
| 470 | cnt += sprintf(page + cnt, " [sense %x %02x %02x]", |
| 471 | t->key, t->asc, t->ascq); |
| 472 | } else { |
| 473 | cnt += sprintf(page + cnt, " %c", UB_DIR_CHAR(t->dir)); |
| 474 | cnt += sprintf(page + cnt, " [%5d %5d]", |
| 475 | t->req_size, t->act_size); |
| 476 | } |
| 477 | if ((nh = t->hcur + 1) == SCMD_ST_HIST_SZ) nh = 0; |
| 478 | for (i = 0; i < SCMD_ST_HIST_SZ; i++) { |
| 479 | cnt += sprintf(page + cnt, " %s", |
| 480 | ub_scsi_cmd_stname[(int)t->st_hst[nh]]); |
| 481 | if (++nh == SCMD_ST_HIST_SZ) nh = 0; |
| 482 | } |
| 483 | cnt += sprintf(page + cnt, "\n"); |
| 484 | |
| 485 | if (++nc == SCMD_TRACE_SZ) nc = 0; |
| 486 | } |
| 487 | |
| 488 | spin_unlock_irqrestore(&sc->lock, flags); |
| 489 | return cnt; |
| 490 | } |
| 491 | |
| 492 | static DEVICE_ATTR(diag, S_IRUGO, ub_diag_show, NULL); /* N.B. World readable */ |
| 493 | |
| 494 | /* |
| 495 | * The id allocator. |
| 496 | * |
| 497 | * This also stores the host for indexing by minor, which is somewhat dirty. |
| 498 | */ |
| 499 | static int ub_id_get(void) |
| 500 | { |
| 501 | unsigned long flags; |
| 502 | int i; |
| 503 | |
| 504 | spin_lock_irqsave(&ub_lock, flags); |
| 505 | for (i = 0; i < UB_MAX_HOSTS; i++) { |
| 506 | if (ub_hostv[i] == 0) { |
| 507 | ub_hostv[i] = 1; |
| 508 | spin_unlock_irqrestore(&ub_lock, flags); |
| 509 | return i; |
| 510 | } |
| 511 | } |
| 512 | spin_unlock_irqrestore(&ub_lock, flags); |
| 513 | return -1; |
| 514 | } |
| 515 | |
| 516 | static void ub_id_put(int id) |
| 517 | { |
| 518 | unsigned long flags; |
| 519 | |
| 520 | if (id < 0 || id >= UB_MAX_HOSTS) { |
| 521 | printk(KERN_ERR DRV_NAME ": bad host ID %d\n", id); |
| 522 | return; |
| 523 | } |
| 524 | |
| 525 | spin_lock_irqsave(&ub_lock, flags); |
| 526 | if (ub_hostv[id] == 0) { |
| 527 | spin_unlock_irqrestore(&ub_lock, flags); |
| 528 | printk(KERN_ERR DRV_NAME ": freeing free host ID %d\n", id); |
| 529 | return; |
| 530 | } |
| 531 | ub_hostv[id] = 0; |
| 532 | spin_unlock_irqrestore(&ub_lock, flags); |
| 533 | } |
| 534 | |
| 535 | /* |
| 536 | * Downcount for deallocation. This rides on two assumptions: |
| 537 | * - once something is poisoned, its refcount cannot grow |
| 538 | * - opens cannot happen at this time (del_gendisk was done) |
| 539 | * If the above is true, we can drop the lock, which we need for |
| 540 | * blk_cleanup_queue(): the silly thing may attempt to sleep. |
| 541 | * [Actually, it never needs to sleep for us, but it calls might_sleep()] |
| 542 | */ |
| 543 | static void ub_put(struct ub_dev *sc) |
| 544 | { |
| 545 | unsigned long flags; |
| 546 | |
| 547 | spin_lock_irqsave(&ub_lock, flags); |
| 548 | --sc->openc; |
| 549 | if (sc->openc == 0 && atomic_read(&sc->poison)) { |
| 550 | spin_unlock_irqrestore(&ub_lock, flags); |
| 551 | ub_cleanup(sc); |
| 552 | } else { |
| 553 | spin_unlock_irqrestore(&ub_lock, flags); |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | /* |
| 558 | * Final cleanup and deallocation. |
| 559 | */ |
| 560 | static void ub_cleanup(struct ub_dev *sc) |
| 561 | { |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 562 | struct list_head *p; |
| 563 | struct ub_lun *lun; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | request_queue_t *q; |
| 565 | |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 566 | while (!list_empty(&sc->luns)) { |
| 567 | p = sc->luns.next; |
| 568 | lun = list_entry(p, struct ub_lun, link); |
| 569 | list_del(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 571 | /* I don't think queue can be NULL. But... Stolen from sx8.c */ |
| 572 | if ((q = lun->disk->queue) != NULL) |
| 573 | blk_cleanup_queue(q); |
| 574 | /* |
| 575 | * If we zero disk->private_data BEFORE put_disk, we have |
| 576 | * to check for NULL all over the place in open, release, |
| 577 | * check_media and revalidate, because the block level |
| 578 | * semaphore is well inside the put_disk. |
| 579 | * But we cannot zero after the call, because *disk is gone. |
| 580 | * The sd.c is blatantly racy in this area. |
| 581 | */ |
| 582 | /* disk->private_data = NULL; */ |
| 583 | put_disk(lun->disk); |
| 584 | lun->disk = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 586 | ub_id_put(lun->id); |
| 587 | kfree(lun); |
| 588 | } |
| 589 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | kfree(sc); |
| 591 | } |
| 592 | |
| 593 | /* |
| 594 | * The "command allocator". |
| 595 | */ |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 596 | static struct ub_scsi_cmd *ub_get_cmd(struct ub_lun *lun) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | { |
| 598 | struct ub_scsi_cmd *ret; |
| 599 | |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 600 | if (lun->cmda[0]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | return NULL; |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 602 | ret = &lun->cmdv[0]; |
| 603 | lun->cmda[0] = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | return ret; |
| 605 | } |
| 606 | |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 607 | static void ub_put_cmd(struct ub_lun *lun, struct ub_scsi_cmd *cmd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | { |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 609 | if (cmd != &lun->cmdv[0]) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | printk(KERN_WARNING "%s: releasing a foreign cmd %p\n", |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 611 | lun->name, cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | return; |
| 613 | } |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 614 | if (!lun->cmda[0]) { |
| 615 | printk(KERN_WARNING "%s: releasing a free cmd\n", lun->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | return; |
| 617 | } |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 618 | lun->cmda[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | /* |
| 622 | * The command queue. |
| 623 | */ |
| 624 | static void ub_cmdq_add(struct ub_dev *sc, struct ub_scsi_cmd *cmd) |
| 625 | { |
| 626 | struct ub_scsi_cmd_queue *t = &sc->cmd_queue; |
| 627 | |
| 628 | if (t->qlen++ == 0) { |
| 629 | t->head = cmd; |
| 630 | t->tail = cmd; |
| 631 | } else { |
| 632 | t->tail->next = cmd; |
| 633 | t->tail = cmd; |
| 634 | } |
| 635 | |
| 636 | if (t->qlen > t->qmax) |
| 637 | t->qmax = t->qlen; |
| 638 | } |
| 639 | |
| 640 | static void ub_cmdq_insert(struct ub_dev *sc, struct ub_scsi_cmd *cmd) |
| 641 | { |
| 642 | struct ub_scsi_cmd_queue *t = &sc->cmd_queue; |
| 643 | |
| 644 | if (t->qlen++ == 0) { |
| 645 | t->head = cmd; |
| 646 | t->tail = cmd; |
| 647 | } else { |
| 648 | cmd->next = t->head; |
| 649 | t->head = cmd; |
| 650 | } |
| 651 | |
| 652 | if (t->qlen > t->qmax) |
| 653 | t->qmax = t->qlen; |
| 654 | } |
| 655 | |
| 656 | static struct ub_scsi_cmd *ub_cmdq_pop(struct ub_dev *sc) |
| 657 | { |
| 658 | struct ub_scsi_cmd_queue *t = &sc->cmd_queue; |
| 659 | struct ub_scsi_cmd *cmd; |
| 660 | |
| 661 | if (t->qlen == 0) |
| 662 | return NULL; |
| 663 | if (--t->qlen == 0) |
| 664 | t->tail = NULL; |
| 665 | cmd = t->head; |
| 666 | t->head = cmd->next; |
| 667 | cmd->next = NULL; |
| 668 | return cmd; |
| 669 | } |
| 670 | |
| 671 | #define ub_cmdq_peek(sc) ((sc)->cmd_queue.head) |
| 672 | |
| 673 | /* |
| 674 | * The request function is our main entry point |
| 675 | */ |
| 676 | |
| 677 | static void ub_bd_rq_fn(request_queue_t *q) |
| 678 | { |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 679 | struct ub_lun *lun = q->queuedata; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | struct request *rq; |
| 681 | |
| 682 | while ((rq = elv_next_request(q)) != NULL) { |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 683 | if (ub_bd_rq_fn_1(lun, rq) != 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | blk_stop_queue(q); |
| 685 | break; |
| 686 | } |
| 687 | } |
| 688 | } |
| 689 | |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 690 | static int ub_bd_rq_fn_1(struct ub_lun *lun, struct request *rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | { |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 692 | struct ub_dev *sc = lun->udev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | struct ub_scsi_cmd *cmd; |
| 694 | int rc; |
| 695 | |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 696 | if (atomic_read(&sc->poison) || lun->changed) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | blkdev_dequeue_request(rq); |
| 698 | ub_end_rq(rq, 0); |
| 699 | return 0; |
| 700 | } |
| 701 | |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 702 | if ((cmd = ub_get_cmd(lun)) == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | return -1; |
| 704 | memset(cmd, 0, sizeof(struct ub_scsi_cmd)); |
| 705 | |
| 706 | blkdev_dequeue_request(rq); |
| 707 | |
| 708 | if (blk_pc_request(rq)) { |
| 709 | rc = ub_cmd_build_packet(sc, cmd, rq); |
| 710 | } else { |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 711 | rc = ub_cmd_build_block(sc, lun, cmd, rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | } |
| 713 | if (rc != 0) { |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 714 | ub_put_cmd(lun, cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | ub_end_rq(rq, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | return 0; |
| 717 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | cmd->state = UB_CMDST_INIT; |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 719 | cmd->lun = lun; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | cmd->done = ub_rw_cmd_done; |
| 721 | cmd->back = rq; |
| 722 | |
| 723 | cmd->tag = sc->tagcnt++; |
| 724 | if ((rc = ub_submit_scsi(sc, cmd)) != 0) { |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 725 | ub_put_cmd(lun, cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | ub_end_rq(rq, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | return 0; |
| 728 | } |
| 729 | |
| 730 | return 0; |
| 731 | } |
| 732 | |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 733 | static int ub_cmd_build_block(struct ub_dev *sc, struct ub_lun *lun, |
| 734 | struct ub_scsi_cmd *cmd, struct request *rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | { |
| 736 | int ub_dir; |
| 737 | #if 0 /* We use rq->buffer for now */ |
| 738 | struct scatterlist *sg; |
| 739 | int n_elem; |
| 740 | #endif |
| 741 | unsigned int block, nblks; |
| 742 | |
| 743 | if (rq_data_dir(rq) == WRITE) |
| 744 | ub_dir = UB_DIR_WRITE; |
| 745 | else |
| 746 | ub_dir = UB_DIR_READ; |
| 747 | |
| 748 | /* |
| 749 | * get scatterlist from block layer |
| 750 | */ |
| 751 | #if 0 /* We use rq->buffer for now */ |
| 752 | sg = &cmd->sgv[0]; |
| 753 | n_elem = blk_rq_map_sg(q, rq, sg); |
| 754 | if (n_elem <= 0) { |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 755 | ub_put_cmd(lun, cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | ub_end_rq(rq, 0); |
| 757 | blk_start_queue(q); |
| 758 | return 0; /* request with no s/g entries? */ |
| 759 | } |
| 760 | |
| 761 | if (n_elem != 1) { /* Paranoia */ |
| 762 | printk(KERN_WARNING "%s: request with %d segments\n", |
| 763 | sc->name, n_elem); |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 764 | ub_put_cmd(lun, cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | ub_end_rq(rq, 0); |
| 766 | blk_start_queue(q); |
| 767 | return 0; |
| 768 | } |
| 769 | #endif |
| 770 | |
| 771 | /* |
| 772 | * XXX Unfortunately, this check does not work. It is quite possible |
| 773 | * to get bogus non-null rq->buffer if you allow sg by mistake. |
| 774 | */ |
| 775 | if (rq->buffer == NULL) { |
| 776 | /* |
| 777 | * This must not happen if we set the queue right. |
| 778 | * The block level must create bounce buffers for us. |
| 779 | */ |
| 780 | static int do_print = 1; |
| 781 | if (do_print) { |
| 782 | printk(KERN_WARNING "%s: unmapped block request" |
| 783 | " flags 0x%lx sectors %lu\n", |
| 784 | sc->name, rq->flags, rq->nr_sectors); |
| 785 | do_print = 0; |
| 786 | } |
| 787 | return -1; |
| 788 | } |
| 789 | |
| 790 | /* |
| 791 | * build the command |
| 792 | * |
| 793 | * The call to blk_queue_hardsect_size() guarantees that request |
| 794 | * is aligned, but it is given in terms of 512 byte units, always. |
| 795 | */ |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 796 | block = rq->sector >> lun->capacity.bshift; |
| 797 | nblks = rq->nr_sectors >> lun->capacity.bshift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | |
| 799 | cmd->cdb[0] = (ub_dir == UB_DIR_READ)? READ_10: WRITE_10; |
| 800 | /* 10-byte uses 4 bytes of LBA: 2147483648KB, 2097152MB, 2048GB */ |
| 801 | cmd->cdb[2] = block >> 24; |
| 802 | cmd->cdb[3] = block >> 16; |
| 803 | cmd->cdb[4] = block >> 8; |
| 804 | cmd->cdb[5] = block; |
| 805 | cmd->cdb[7] = nblks >> 8; |
| 806 | cmd->cdb[8] = nblks; |
| 807 | cmd->cdb_len = 10; |
| 808 | |
| 809 | cmd->dir = ub_dir; |
| 810 | cmd->data = rq->buffer; |
| 811 | cmd->len = rq->nr_sectors * 512; |
| 812 | |
| 813 | return 0; |
| 814 | } |
| 815 | |
| 816 | static int ub_cmd_build_packet(struct ub_dev *sc, struct ub_scsi_cmd *cmd, |
| 817 | struct request *rq) |
| 818 | { |
| 819 | |
| 820 | if (rq->data_len != 0 && rq->data == NULL) { |
| 821 | static int do_print = 1; |
| 822 | if (do_print) { |
| 823 | printk(KERN_WARNING "%s: unmapped packet request" |
| 824 | " flags 0x%lx length %d\n", |
| 825 | sc->name, rq->flags, rq->data_len); |
| 826 | do_print = 0; |
| 827 | } |
| 828 | return -1; |
| 829 | } |
| 830 | |
| 831 | memcpy(&cmd->cdb, rq->cmd, rq->cmd_len); |
| 832 | cmd->cdb_len = rq->cmd_len; |
| 833 | |
| 834 | if (rq->data_len == 0) { |
| 835 | cmd->dir = UB_DIR_NONE; |
| 836 | } else { |
| 837 | if (rq_data_dir(rq) == WRITE) |
| 838 | cmd->dir = UB_DIR_WRITE; |
| 839 | else |
| 840 | cmd->dir = UB_DIR_READ; |
| 841 | } |
| 842 | cmd->data = rq->data; |
| 843 | cmd->len = rq->data_len; |
| 844 | |
| 845 | return 0; |
| 846 | } |
| 847 | |
| 848 | static void ub_rw_cmd_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd) |
| 849 | { |
| 850 | struct request *rq = cmd->back; |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 851 | struct ub_lun *lun = cmd->lun; |
| 852 | struct gendisk *disk = lun->disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 853 | request_queue_t *q = disk->queue; |
| 854 | int uptodate; |
| 855 | |
| 856 | if (blk_pc_request(rq)) { |
| 857 | /* UB_SENSE_SIZE is smaller than SCSI_SENSE_BUFFERSIZE */ |
| 858 | memcpy(rq->sense, sc->top_sense, UB_SENSE_SIZE); |
| 859 | rq->sense_len = UB_SENSE_SIZE; |
| 860 | } |
| 861 | |
| 862 | if (cmd->error == 0) |
| 863 | uptodate = 1; |
| 864 | else |
| 865 | uptodate = 0; |
| 866 | |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 867 | ub_put_cmd(lun, cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | ub_end_rq(rq, uptodate); |
| 869 | blk_start_queue(q); |
| 870 | } |
| 871 | |
| 872 | static void ub_end_rq(struct request *rq, int uptodate) |
| 873 | { |
| 874 | int rc; |
| 875 | |
| 876 | rc = end_that_request_first(rq, uptodate, rq->hard_nr_sectors); |
| 877 | // assert(rc == 0); |
| 878 | end_that_request_last(rq); |
| 879 | } |
| 880 | |
| 881 | /* |
| 882 | * Submit a regular SCSI operation (not an auto-sense). |
| 883 | * |
| 884 | * The Iron Law of Good Submit Routine is: |
| 885 | * Zero return - callback is done, Nonzero return - callback is not done. |
| 886 | * No exceptions. |
| 887 | * |
| 888 | * Host is assumed locked. |
| 889 | * |
| 890 | * XXX We only support Bulk for the moment. |
| 891 | */ |
| 892 | static int ub_submit_scsi(struct ub_dev *sc, struct ub_scsi_cmd *cmd) |
| 893 | { |
| 894 | |
| 895 | if (cmd->state != UB_CMDST_INIT || |
| 896 | (cmd->dir != UB_DIR_NONE && cmd->len == 0)) { |
| 897 | return -EINVAL; |
| 898 | } |
| 899 | |
| 900 | ub_cmdq_add(sc, cmd); |
| 901 | /* |
| 902 | * We can call ub_scsi_dispatch(sc) right away here, but it's a little |
| 903 | * safer to jump to a tasklet, in case upper layers do something silly. |
| 904 | */ |
| 905 | tasklet_schedule(&sc->tasklet); |
| 906 | return 0; |
| 907 | } |
| 908 | |
| 909 | /* |
| 910 | * Submit the first URB for the queued command. |
| 911 | * This function does not deal with queueing in any way. |
| 912 | */ |
| 913 | static int ub_scsi_cmd_start(struct ub_dev *sc, struct ub_scsi_cmd *cmd) |
| 914 | { |
| 915 | struct bulk_cb_wrap *bcb; |
| 916 | int rc; |
| 917 | |
| 918 | bcb = &sc->work_bcb; |
| 919 | |
| 920 | /* |
| 921 | * ``If the allocation length is eighteen or greater, and a device |
| 922 | * server returns less than eithteen bytes of data, the application |
| 923 | * client should assume that the bytes not transferred would have been |
| 924 | * zeroes had the device server returned those bytes.'' |
| 925 | * |
| 926 | * We zero sense for all commands so that when a packet request |
| 927 | * fails it does not return a stale sense. |
| 928 | */ |
| 929 | memset(&sc->top_sense, 0, UB_SENSE_SIZE); |
| 930 | |
| 931 | /* set up the command wrapper */ |
| 932 | bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN); |
| 933 | bcb->Tag = cmd->tag; /* Endianness is not important */ |
| 934 | bcb->DataTransferLength = cpu_to_le32(cmd->len); |
| 935 | bcb->Flags = (cmd->dir == UB_DIR_READ) ? 0x80 : 0; |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 936 | bcb->Lun = (cmd->lun != NULL) ? cmd->lun->num : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 937 | bcb->Length = cmd->cdb_len; |
| 938 | |
| 939 | /* copy the command payload */ |
| 940 | memcpy(bcb->CDB, cmd->cdb, UB_MAX_CDB_SIZE); |
| 941 | |
| 942 | UB_INIT_COMPLETION(sc->work_done); |
| 943 | |
| 944 | sc->last_pipe = sc->send_bulk_pipe; |
| 945 | usb_fill_bulk_urb(&sc->work_urb, sc->dev, sc->send_bulk_pipe, |
| 946 | bcb, US_BULK_CB_WRAP_LEN, ub_urb_complete, sc); |
| 947 | sc->work_urb.transfer_flags = URB_ASYNC_UNLINK; |
| 948 | |
| 949 | /* Fill what we shouldn't be filling, because usb-storage did so. */ |
| 950 | sc->work_urb.actual_length = 0; |
| 951 | sc->work_urb.error_count = 0; |
| 952 | sc->work_urb.status = 0; |
| 953 | |
| 954 | if ((rc = usb_submit_urb(&sc->work_urb, GFP_ATOMIC)) != 0) { |
| 955 | /* XXX Clear stalls */ |
| 956 | printk("ub: cmd #%d start failed (%d)\n", cmd->tag, rc); /* P3 */ |
| 957 | ub_complete(&sc->work_done); |
| 958 | return rc; |
| 959 | } |
| 960 | |
| 961 | sc->work_timer.expires = jiffies + UB_URB_TIMEOUT; |
| 962 | add_timer(&sc->work_timer); |
| 963 | |
| 964 | cmd->state = UB_CMDST_CMD; |
| 965 | ub_cmdtr_state(sc, cmd); |
| 966 | return 0; |
| 967 | } |
| 968 | |
| 969 | /* |
| 970 | * Timeout handler. |
| 971 | */ |
| 972 | static void ub_urb_timeout(unsigned long arg) |
| 973 | { |
| 974 | struct ub_dev *sc = (struct ub_dev *) arg; |
| 975 | unsigned long flags; |
| 976 | |
| 977 | spin_lock_irqsave(&sc->lock, flags); |
| 978 | usb_unlink_urb(&sc->work_urb); |
| 979 | spin_unlock_irqrestore(&sc->lock, flags); |
| 980 | } |
| 981 | |
| 982 | /* |
| 983 | * Completion routine for the work URB. |
| 984 | * |
| 985 | * This can be called directly from usb_submit_urb (while we have |
| 986 | * the sc->lock taken) and from an interrupt (while we do NOT have |
| 987 | * the sc->lock taken). Therefore, bounce this off to a tasklet. |
| 988 | */ |
| 989 | static void ub_urb_complete(struct urb *urb, struct pt_regs *pt) |
| 990 | { |
| 991 | struct ub_dev *sc = urb->context; |
| 992 | |
| 993 | ub_complete(&sc->work_done); |
| 994 | tasklet_schedule(&sc->tasklet); |
| 995 | } |
| 996 | |
| 997 | static void ub_scsi_action(unsigned long _dev) |
| 998 | { |
| 999 | struct ub_dev *sc = (struct ub_dev *) _dev; |
| 1000 | unsigned long flags; |
| 1001 | |
| 1002 | spin_lock_irqsave(&sc->lock, flags); |
| 1003 | del_timer(&sc->work_timer); |
| 1004 | ub_scsi_dispatch(sc); |
| 1005 | spin_unlock_irqrestore(&sc->lock, flags); |
| 1006 | } |
| 1007 | |
| 1008 | static void ub_scsi_dispatch(struct ub_dev *sc) |
| 1009 | { |
| 1010 | struct ub_scsi_cmd *cmd; |
| 1011 | int rc; |
| 1012 | |
| 1013 | while ((cmd = ub_cmdq_peek(sc)) != NULL) { |
| 1014 | if (cmd->state == UB_CMDST_DONE) { |
| 1015 | ub_cmdq_pop(sc); |
| 1016 | (*cmd->done)(sc, cmd); |
| 1017 | } else if (cmd->state == UB_CMDST_INIT) { |
| 1018 | ub_cmdtr_new(sc, cmd); |
| 1019 | if ((rc = ub_scsi_cmd_start(sc, cmd)) == 0) |
| 1020 | break; |
| 1021 | cmd->error = rc; |
| 1022 | cmd->state = UB_CMDST_DONE; |
| 1023 | ub_cmdtr_state(sc, cmd); |
| 1024 | } else { |
| 1025 | if (!ub_is_completed(&sc->work_done)) |
| 1026 | break; |
| 1027 | ub_scsi_urb_compl(sc, cmd); |
| 1028 | } |
| 1029 | } |
| 1030 | } |
| 1031 | |
| 1032 | static void ub_scsi_urb_compl(struct ub_dev *sc, struct ub_scsi_cmd *cmd) |
| 1033 | { |
| 1034 | struct urb *urb = &sc->work_urb; |
| 1035 | struct bulk_cs_wrap *bcs; |
| 1036 | int pipe; |
| 1037 | int rc; |
| 1038 | |
| 1039 | if (atomic_read(&sc->poison)) { |
| 1040 | /* A little too simplistic, I feel... */ |
| 1041 | goto Bad_End; |
| 1042 | } |
| 1043 | |
| 1044 | if (cmd->state == UB_CMDST_CLEAR) { |
| 1045 | if (urb->status == -EPIPE) { |
| 1046 | /* |
| 1047 | * STALL while clearning STALL. |
| 1048 | * The control pipe clears itself - nothing to do. |
| 1049 | * XXX Might try to reset the device here and retry. |
| 1050 | */ |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 1051 | printk(KERN_NOTICE "%s: stall on control pipe\n", |
| 1052 | sc->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1053 | goto Bad_End; |
| 1054 | } |
| 1055 | |
| 1056 | /* |
| 1057 | * We ignore the result for the halt clear. |
| 1058 | */ |
| 1059 | |
| 1060 | /* reset the endpoint toggle */ |
| 1061 | usb_settoggle(sc->dev, usb_pipeendpoint(sc->last_pipe), |
| 1062 | usb_pipeout(sc->last_pipe), 0); |
| 1063 | |
| 1064 | ub_state_sense(sc, cmd); |
| 1065 | |
| 1066 | } else if (cmd->state == UB_CMDST_CLR2STS) { |
| 1067 | if (urb->status == -EPIPE) { |
| 1068 | /* |
| 1069 | * STALL while clearning STALL. |
| 1070 | * The control pipe clears itself - nothing to do. |
| 1071 | * XXX Might try to reset the device here and retry. |
| 1072 | */ |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 1073 | printk(KERN_NOTICE "%s: stall on control pipe\n", |
| 1074 | sc->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1075 | goto Bad_End; |
| 1076 | } |
| 1077 | |
| 1078 | /* |
| 1079 | * We ignore the result for the halt clear. |
| 1080 | */ |
| 1081 | |
| 1082 | /* reset the endpoint toggle */ |
| 1083 | usb_settoggle(sc->dev, usb_pipeendpoint(sc->last_pipe), |
| 1084 | usb_pipeout(sc->last_pipe), 0); |
| 1085 | |
| 1086 | ub_state_stat(sc, cmd); |
| 1087 | |
| 1088 | } else if (cmd->state == UB_CMDST_CMD) { |
| 1089 | if (urb->status == -EPIPE) { |
| 1090 | rc = ub_submit_clear_stall(sc, cmd, sc->last_pipe); |
| 1091 | if (rc != 0) { |
| 1092 | printk(KERN_NOTICE "%s: " |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 1093 | "unable to submit clear (%d)\n", |
| 1094 | sc->name, rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1095 | /* |
| 1096 | * This is typically ENOMEM or some other such shit. |
| 1097 | * Retrying is pointless. Just do Bad End on it... |
| 1098 | */ |
| 1099 | goto Bad_End; |
| 1100 | } |
| 1101 | cmd->state = UB_CMDST_CLEAR; |
| 1102 | ub_cmdtr_state(sc, cmd); |
| 1103 | return; |
| 1104 | } |
| 1105 | if (urb->status != 0) { |
| 1106 | printk("ub: cmd #%d cmd status (%d)\n", cmd->tag, urb->status); /* P3 */ |
| 1107 | goto Bad_End; |
| 1108 | } |
| 1109 | if (urb->actual_length != US_BULK_CB_WRAP_LEN) { |
| 1110 | printk("ub: cmd #%d xferred %d\n", cmd->tag, urb->actual_length); /* P3 */ |
| 1111 | /* XXX Must do reset here to unconfuse the device */ |
| 1112 | goto Bad_End; |
| 1113 | } |
| 1114 | |
| 1115 | if (cmd->dir == UB_DIR_NONE) { |
| 1116 | ub_state_stat(sc, cmd); |
| 1117 | return; |
| 1118 | } |
| 1119 | |
| 1120 | UB_INIT_COMPLETION(sc->work_done); |
| 1121 | |
| 1122 | if (cmd->dir == UB_DIR_READ) |
| 1123 | pipe = sc->recv_bulk_pipe; |
| 1124 | else |
| 1125 | pipe = sc->send_bulk_pipe; |
| 1126 | sc->last_pipe = pipe; |
| 1127 | usb_fill_bulk_urb(&sc->work_urb, sc->dev, pipe, |
| 1128 | cmd->data, cmd->len, ub_urb_complete, sc); |
| 1129 | sc->work_urb.transfer_flags = URB_ASYNC_UNLINK; |
| 1130 | sc->work_urb.actual_length = 0; |
| 1131 | sc->work_urb.error_count = 0; |
| 1132 | sc->work_urb.status = 0; |
| 1133 | |
| 1134 | if ((rc = usb_submit_urb(&sc->work_urb, GFP_ATOMIC)) != 0) { |
| 1135 | /* XXX Clear stalls */ |
| 1136 | printk("ub: data #%d submit failed (%d)\n", cmd->tag, rc); /* P3 */ |
| 1137 | ub_complete(&sc->work_done); |
| 1138 | ub_state_done(sc, cmd, rc); |
| 1139 | return; |
| 1140 | } |
| 1141 | |
| 1142 | sc->work_timer.expires = jiffies + UB_DATA_TIMEOUT; |
| 1143 | add_timer(&sc->work_timer); |
| 1144 | |
| 1145 | cmd->state = UB_CMDST_DATA; |
| 1146 | ub_cmdtr_state(sc, cmd); |
| 1147 | |
| 1148 | } else if (cmd->state == UB_CMDST_DATA) { |
| 1149 | if (urb->status == -EPIPE) { |
| 1150 | rc = ub_submit_clear_stall(sc, cmd, sc->last_pipe); |
| 1151 | if (rc != 0) { |
| 1152 | printk(KERN_NOTICE "%s: " |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 1153 | "unable to submit clear (%d)\n", |
| 1154 | sc->name, rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1155 | /* |
| 1156 | * This is typically ENOMEM or some other such shit. |
| 1157 | * Retrying is pointless. Just do Bad End on it... |
| 1158 | */ |
| 1159 | goto Bad_End; |
| 1160 | } |
| 1161 | cmd->state = UB_CMDST_CLR2STS; |
| 1162 | ub_cmdtr_state(sc, cmd); |
| 1163 | return; |
| 1164 | } |
| 1165 | if (urb->status == -EOVERFLOW) { |
| 1166 | /* |
| 1167 | * A babble? Failure, but we must transfer CSW now. |
| 1168 | */ |
| 1169 | cmd->error = -EOVERFLOW; /* A cheap trick... */ |
| 1170 | } else { |
| 1171 | if (urb->status != 0) |
| 1172 | goto Bad_End; |
| 1173 | } |
| 1174 | |
| 1175 | cmd->act_len = urb->actual_length; |
| 1176 | ub_cmdtr_act_len(sc, cmd); |
| 1177 | |
| 1178 | ub_state_stat(sc, cmd); |
| 1179 | |
| 1180 | } else if (cmd->state == UB_CMDST_STAT) { |
| 1181 | if (urb->status == -EPIPE) { |
| 1182 | rc = ub_submit_clear_stall(sc, cmd, sc->last_pipe); |
| 1183 | if (rc != 0) { |
| 1184 | printk(KERN_NOTICE "%s: " |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 1185 | "unable to submit clear (%d)\n", |
| 1186 | sc->name, rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1187 | /* |
| 1188 | * This is typically ENOMEM or some other such shit. |
| 1189 | * Retrying is pointless. Just do Bad End on it... |
| 1190 | */ |
| 1191 | goto Bad_End; |
| 1192 | } |
| 1193 | cmd->state = UB_CMDST_CLEAR; |
| 1194 | ub_cmdtr_state(sc, cmd); |
| 1195 | return; |
| 1196 | } |
| 1197 | if (urb->status != 0) |
| 1198 | goto Bad_End; |
| 1199 | |
| 1200 | if (urb->actual_length == 0) { |
| 1201 | /* |
| 1202 | * Some broken devices add unnecessary zero-length |
| 1203 | * packets to the end of their data transfers. |
| 1204 | * Such packets show up as 0-length CSWs. If we |
| 1205 | * encounter such a thing, try to read the CSW again. |
| 1206 | */ |
| 1207 | if (++cmd->stat_count >= 4) { |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 1208 | printk(KERN_NOTICE "%s: unable to get CSW\n", |
| 1209 | sc->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1210 | goto Bad_End; |
| 1211 | } |
| 1212 | __ub_state_stat(sc, cmd); |
| 1213 | return; |
| 1214 | } |
| 1215 | |
| 1216 | /* |
| 1217 | * Check the returned Bulk protocol status. |
| 1218 | */ |
| 1219 | |
| 1220 | bcs = &sc->work_bcs; |
| 1221 | rc = le32_to_cpu(bcs->Residue); |
| 1222 | if (rc != cmd->len - cmd->act_len) { |
| 1223 | /* |
| 1224 | * It is all right to transfer less, the caller has |
| 1225 | * to check. But it's not all right if the device |
| 1226 | * counts disagree with our counts. |
| 1227 | */ |
| 1228 | /* P3 */ printk("%s: resid %d len %d act %d\n", |
| 1229 | sc->name, rc, cmd->len, cmd->act_len); |
| 1230 | goto Bad_End; |
| 1231 | } |
| 1232 | |
| 1233 | #if 0 |
| 1234 | if (bcs->Signature != cpu_to_le32(US_BULK_CS_SIGN) && |
| 1235 | bcs->Signature != cpu_to_le32(US_BULK_CS_OLYMPUS_SIGN)) { |
| 1236 | /* Windows ignores signatures, so do we. */ |
| 1237 | } |
| 1238 | #endif |
| 1239 | |
| 1240 | if (bcs->Tag != cmd->tag) { |
| 1241 | /* |
| 1242 | * This usually happens when we disagree with the |
| 1243 | * device's microcode about something. For instance, |
| 1244 | * a few of them throw this after timeouts. They buffer |
| 1245 | * commands and reply at commands we timed out before. |
| 1246 | * Without flushing these replies we loop forever. |
| 1247 | */ |
| 1248 | if (++cmd->stat_count >= 4) { |
| 1249 | printk(KERN_NOTICE "%s: " |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 1250 | "tag mismatch orig 0x%x reply 0x%x\n", |
| 1251 | sc->name, cmd->tag, bcs->Tag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1252 | goto Bad_End; |
| 1253 | } |
| 1254 | __ub_state_stat(sc, cmd); |
| 1255 | return; |
| 1256 | } |
| 1257 | |
| 1258 | switch (bcs->Status) { |
| 1259 | case US_BULK_STAT_OK: |
| 1260 | break; |
| 1261 | case US_BULK_STAT_FAIL: |
| 1262 | ub_state_sense(sc, cmd); |
| 1263 | return; |
| 1264 | case US_BULK_STAT_PHASE: |
| 1265 | /* XXX We must reset the transport here */ |
| 1266 | /* P3 */ printk("%s: status PHASE\n", sc->name); |
| 1267 | goto Bad_End; |
| 1268 | default: |
| 1269 | printk(KERN_INFO "%s: unknown CSW status 0x%x\n", |
| 1270 | sc->name, bcs->Status); |
| 1271 | goto Bad_End; |
| 1272 | } |
| 1273 | |
| 1274 | /* Not zeroing error to preserve a babble indicator */ |
| 1275 | cmd->state = UB_CMDST_DONE; |
| 1276 | ub_cmdtr_state(sc, cmd); |
| 1277 | ub_cmdq_pop(sc); |
| 1278 | (*cmd->done)(sc, cmd); |
| 1279 | |
| 1280 | } else if (cmd->state == UB_CMDST_SENSE) { |
| 1281 | ub_state_done(sc, cmd, -EIO); |
| 1282 | |
| 1283 | } else { |
| 1284 | printk(KERN_WARNING "%s: " |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 1285 | "wrong command state %d\n", |
| 1286 | sc->name, cmd->state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1287 | goto Bad_End; |
| 1288 | } |
| 1289 | return; |
| 1290 | |
| 1291 | Bad_End: /* Little Excel is dead */ |
| 1292 | ub_state_done(sc, cmd, -EIO); |
| 1293 | } |
| 1294 | |
| 1295 | /* |
| 1296 | * Factorization helper for the command state machine: |
| 1297 | * Finish the command. |
| 1298 | */ |
| 1299 | static void ub_state_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd, int rc) |
| 1300 | { |
| 1301 | |
| 1302 | cmd->error = rc; |
| 1303 | cmd->state = UB_CMDST_DONE; |
| 1304 | ub_cmdtr_state(sc, cmd); |
| 1305 | ub_cmdq_pop(sc); |
| 1306 | (*cmd->done)(sc, cmd); |
| 1307 | } |
| 1308 | |
| 1309 | /* |
| 1310 | * Factorization helper for the command state machine: |
| 1311 | * Submit a CSW read. |
| 1312 | */ |
| 1313 | static void __ub_state_stat(struct ub_dev *sc, struct ub_scsi_cmd *cmd) |
| 1314 | { |
| 1315 | int rc; |
| 1316 | |
| 1317 | UB_INIT_COMPLETION(sc->work_done); |
| 1318 | |
| 1319 | sc->last_pipe = sc->recv_bulk_pipe; |
| 1320 | usb_fill_bulk_urb(&sc->work_urb, sc->dev, sc->recv_bulk_pipe, |
| 1321 | &sc->work_bcs, US_BULK_CS_WRAP_LEN, ub_urb_complete, sc); |
| 1322 | sc->work_urb.transfer_flags = URB_ASYNC_UNLINK; |
| 1323 | sc->work_urb.actual_length = 0; |
| 1324 | sc->work_urb.error_count = 0; |
| 1325 | sc->work_urb.status = 0; |
| 1326 | |
| 1327 | if ((rc = usb_submit_urb(&sc->work_urb, GFP_ATOMIC)) != 0) { |
| 1328 | /* XXX Clear stalls */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1329 | ub_complete(&sc->work_done); |
| 1330 | ub_state_done(sc, cmd, rc); |
| 1331 | return; |
| 1332 | } |
| 1333 | |
| 1334 | sc->work_timer.expires = jiffies + UB_STAT_TIMEOUT; |
| 1335 | add_timer(&sc->work_timer); |
| 1336 | } |
| 1337 | |
| 1338 | /* |
| 1339 | * Factorization helper for the command state machine: |
| 1340 | * Submit a CSW read and go to STAT state. |
| 1341 | */ |
| 1342 | static void ub_state_stat(struct ub_dev *sc, struct ub_scsi_cmd *cmd) |
| 1343 | { |
| 1344 | __ub_state_stat(sc, cmd); |
| 1345 | |
| 1346 | cmd->stat_count = 0; |
| 1347 | cmd->state = UB_CMDST_STAT; |
| 1348 | ub_cmdtr_state(sc, cmd); |
| 1349 | } |
| 1350 | |
| 1351 | /* |
| 1352 | * Factorization helper for the command state machine: |
| 1353 | * Submit a REQUEST SENSE and go to SENSE state. |
| 1354 | */ |
| 1355 | static void ub_state_sense(struct ub_dev *sc, struct ub_scsi_cmd *cmd) |
| 1356 | { |
| 1357 | struct ub_scsi_cmd *scmd; |
| 1358 | int rc; |
| 1359 | |
| 1360 | if (cmd->cdb[0] == REQUEST_SENSE) { |
| 1361 | rc = -EPIPE; |
| 1362 | goto error; |
| 1363 | } |
| 1364 | |
| 1365 | scmd = &sc->top_rqs_cmd; |
| 1366 | scmd->cdb[0] = REQUEST_SENSE; |
| 1367 | scmd->cdb[4] = UB_SENSE_SIZE; |
| 1368 | scmd->cdb_len = 6; |
| 1369 | scmd->dir = UB_DIR_READ; |
| 1370 | scmd->state = UB_CMDST_INIT; |
| 1371 | scmd->data = sc->top_sense; |
| 1372 | scmd->len = UB_SENSE_SIZE; |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 1373 | scmd->lun = cmd->lun; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1374 | scmd->done = ub_top_sense_done; |
| 1375 | scmd->back = cmd; |
| 1376 | |
| 1377 | scmd->tag = sc->tagcnt++; |
| 1378 | |
| 1379 | cmd->state = UB_CMDST_SENSE; |
| 1380 | ub_cmdtr_state(sc, cmd); |
| 1381 | |
| 1382 | ub_cmdq_insert(sc, scmd); |
| 1383 | return; |
| 1384 | |
| 1385 | error: |
| 1386 | ub_state_done(sc, cmd, rc); |
| 1387 | } |
| 1388 | |
| 1389 | /* |
| 1390 | * A helper for the command's state machine: |
| 1391 | * Submit a stall clear. |
| 1392 | */ |
| 1393 | static int ub_submit_clear_stall(struct ub_dev *sc, struct ub_scsi_cmd *cmd, |
| 1394 | int stalled_pipe) |
| 1395 | { |
| 1396 | int endp; |
| 1397 | struct usb_ctrlrequest *cr; |
| 1398 | int rc; |
| 1399 | |
| 1400 | endp = usb_pipeendpoint(stalled_pipe); |
| 1401 | if (usb_pipein (stalled_pipe)) |
| 1402 | endp |= USB_DIR_IN; |
| 1403 | |
| 1404 | cr = &sc->work_cr; |
| 1405 | cr->bRequestType = USB_RECIP_ENDPOINT; |
| 1406 | cr->bRequest = USB_REQ_CLEAR_FEATURE; |
| 1407 | cr->wValue = cpu_to_le16(USB_ENDPOINT_HALT); |
| 1408 | cr->wIndex = cpu_to_le16(endp); |
| 1409 | cr->wLength = cpu_to_le16(0); |
| 1410 | |
| 1411 | UB_INIT_COMPLETION(sc->work_done); |
| 1412 | |
| 1413 | usb_fill_control_urb(&sc->work_urb, sc->dev, sc->send_ctrl_pipe, |
| 1414 | (unsigned char*) cr, NULL, 0, ub_urb_complete, sc); |
| 1415 | sc->work_urb.transfer_flags = URB_ASYNC_UNLINK; |
| 1416 | sc->work_urb.actual_length = 0; |
| 1417 | sc->work_urb.error_count = 0; |
| 1418 | sc->work_urb.status = 0; |
| 1419 | |
| 1420 | if ((rc = usb_submit_urb(&sc->work_urb, GFP_ATOMIC)) != 0) { |
| 1421 | ub_complete(&sc->work_done); |
| 1422 | return rc; |
| 1423 | } |
| 1424 | |
| 1425 | sc->work_timer.expires = jiffies + UB_CTRL_TIMEOUT; |
| 1426 | add_timer(&sc->work_timer); |
| 1427 | return 0; |
| 1428 | } |
| 1429 | |
| 1430 | /* |
| 1431 | */ |
| 1432 | static void ub_top_sense_done(struct ub_dev *sc, struct ub_scsi_cmd *scmd) |
| 1433 | { |
| 1434 | unsigned char *sense = scmd->data; |
| 1435 | struct ub_scsi_cmd *cmd; |
| 1436 | |
| 1437 | /* |
| 1438 | * Ignoring scmd->act_len, because the buffer was pre-zeroed. |
| 1439 | */ |
| 1440 | ub_cmdtr_sense(sc, scmd, sense); |
| 1441 | |
| 1442 | /* |
| 1443 | * Find the command which triggered the unit attention or a check, |
| 1444 | * save the sense into it, and advance its state machine. |
| 1445 | */ |
| 1446 | if ((cmd = ub_cmdq_peek(sc)) == NULL) { |
| 1447 | printk(KERN_WARNING "%s: sense done while idle\n", sc->name); |
| 1448 | return; |
| 1449 | } |
| 1450 | if (cmd != scmd->back) { |
| 1451 | printk(KERN_WARNING "%s: " |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 1452 | "sense done for wrong command 0x%x\n", |
| 1453 | sc->name, cmd->tag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1454 | return; |
| 1455 | } |
| 1456 | if (cmd->state != UB_CMDST_SENSE) { |
| 1457 | printk(KERN_WARNING "%s: " |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 1458 | "sense done with bad cmd state %d\n", |
| 1459 | sc->name, cmd->state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1460 | return; |
| 1461 | } |
| 1462 | |
| 1463 | cmd->key = sense[2] & 0x0F; |
| 1464 | cmd->asc = sense[12]; |
| 1465 | cmd->ascq = sense[13]; |
| 1466 | |
| 1467 | ub_scsi_urb_compl(sc, cmd); |
| 1468 | } |
| 1469 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1470 | /* |
| 1471 | * This is called from a process context. |
| 1472 | */ |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 1473 | static void ub_revalidate(struct ub_dev *sc, struct ub_lun *lun) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1474 | { |
| 1475 | |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 1476 | lun->readonly = 0; /* XXX Query this from the device */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1477 | |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 1478 | lun->capacity.nsec = 0; |
| 1479 | lun->capacity.bsize = 512; |
| 1480 | lun->capacity.bshift = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1481 | |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 1482 | if (ub_sync_tur(sc, lun) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1483 | return; /* Not ready */ |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 1484 | lun->changed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1485 | |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 1486 | if (ub_sync_read_cap(sc, lun, &lun->capacity) != 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1487 | /* |
| 1488 | * The retry here means something is wrong, either with the |
| 1489 | * device, with the transport, or with our code. |
| 1490 | * We keep this because sd.c has retries for capacity. |
| 1491 | */ |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 1492 | if (ub_sync_read_cap(sc, lun, &lun->capacity) != 0) { |
| 1493 | lun->capacity.nsec = 0; |
| 1494 | lun->capacity.bsize = 512; |
| 1495 | lun->capacity.bshift = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1496 | } |
| 1497 | } |
| 1498 | } |
| 1499 | |
| 1500 | /* |
| 1501 | * The open funcion. |
| 1502 | * This is mostly needed to keep refcounting, but also to support |
| 1503 | * media checks on removable media drives. |
| 1504 | */ |
| 1505 | static int ub_bd_open(struct inode *inode, struct file *filp) |
| 1506 | { |
| 1507 | struct gendisk *disk = inode->i_bdev->bd_disk; |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 1508 | struct ub_lun *lun; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1509 | struct ub_dev *sc; |
| 1510 | unsigned long flags; |
| 1511 | int rc; |
| 1512 | |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 1513 | if ((lun = disk->private_data) == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1514 | return -ENXIO; |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 1515 | sc = lun->udev; |
| 1516 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1517 | spin_lock_irqsave(&ub_lock, flags); |
| 1518 | if (atomic_read(&sc->poison)) { |
| 1519 | spin_unlock_irqrestore(&ub_lock, flags); |
| 1520 | return -ENXIO; |
| 1521 | } |
| 1522 | sc->openc++; |
| 1523 | spin_unlock_irqrestore(&ub_lock, flags); |
| 1524 | |
| 1525 | /* |
| 1526 | * This is a workaround for a specific problem in our block layer. |
| 1527 | * In 2.6.9, register_disk duplicates the code from rescan_partitions. |
| 1528 | * However, if we do add_disk with a device which persistently reports |
| 1529 | * a changed media, add_disk calls register_disk, which does do_open, |
| 1530 | * which will call rescan_paritions for changed media. After that, |
| 1531 | * register_disk attempts to do it all again and causes double kobject |
| 1532 | * registration and a eventually an oops on module removal. |
| 1533 | * |
| 1534 | * The bottom line is, Al Viro says that we should not allow |
| 1535 | * bdev->bd_invalidated to be set when doing add_disk no matter what. |
| 1536 | */ |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 1537 | if (lun->first_open) { |
| 1538 | lun->first_open = 0; |
| 1539 | if (lun->changed) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1540 | rc = -ENOMEDIUM; |
| 1541 | goto err_open; |
| 1542 | } |
| 1543 | } |
| 1544 | |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 1545 | if (lun->removable || lun->readonly) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1546 | check_disk_change(inode->i_bdev); |
| 1547 | |
| 1548 | /* |
| 1549 | * The sd.c considers ->media_present and ->changed not equivalent, |
| 1550 | * under some pretty murky conditions (a failure of READ CAPACITY). |
| 1551 | * We may need it one day. |
| 1552 | */ |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 1553 | if (lun->removable && lun->changed && !(filp->f_flags & O_NDELAY)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1554 | rc = -ENOMEDIUM; |
| 1555 | goto err_open; |
| 1556 | } |
| 1557 | |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 1558 | if (lun->readonly && (filp->f_mode & FMODE_WRITE)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1559 | rc = -EROFS; |
| 1560 | goto err_open; |
| 1561 | } |
| 1562 | |
| 1563 | return 0; |
| 1564 | |
| 1565 | err_open: |
| 1566 | ub_put(sc); |
| 1567 | return rc; |
| 1568 | } |
| 1569 | |
| 1570 | /* |
| 1571 | */ |
| 1572 | static int ub_bd_release(struct inode *inode, struct file *filp) |
| 1573 | { |
| 1574 | struct gendisk *disk = inode->i_bdev->bd_disk; |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 1575 | struct ub_lun *lun = disk->private_data; |
| 1576 | struct ub_dev *sc = lun->udev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1577 | |
| 1578 | ub_put(sc); |
| 1579 | return 0; |
| 1580 | } |
| 1581 | |
| 1582 | /* |
| 1583 | * The ioctl interface. |
| 1584 | */ |
| 1585 | static int ub_bd_ioctl(struct inode *inode, struct file *filp, |
| 1586 | unsigned int cmd, unsigned long arg) |
| 1587 | { |
| 1588 | struct gendisk *disk = inode->i_bdev->bd_disk; |
| 1589 | void __user *usermem = (void __user *) arg; |
| 1590 | |
| 1591 | return scsi_cmd_ioctl(filp, disk, cmd, usermem); |
| 1592 | } |
| 1593 | |
| 1594 | /* |
| 1595 | * This is called once a new disk was seen by the block layer or by ub_probe(). |
| 1596 | * The main onjective here is to discover the features of the media such as |
| 1597 | * the capacity, read-only status, etc. USB storage generally does not |
| 1598 | * need to be spun up, but if we needed it, this would be the place. |
| 1599 | * |
| 1600 | * This call can sleep. |
| 1601 | * |
| 1602 | * The return code is not used. |
| 1603 | */ |
| 1604 | static int ub_bd_revalidate(struct gendisk *disk) |
| 1605 | { |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 1606 | struct ub_lun *lun = disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1607 | |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 1608 | ub_revalidate(lun->udev, lun); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1609 | |
| 1610 | /* XXX Support sector size switching like in sr.c */ |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 1611 | blk_queue_hardsect_size(disk->queue, lun->capacity.bsize); |
| 1612 | set_capacity(disk, lun->capacity.nsec); |
| 1613 | // set_disk_ro(sdkp->disk, lun->readonly); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1614 | |
| 1615 | return 0; |
| 1616 | } |
| 1617 | |
| 1618 | /* |
| 1619 | * The check is called by the block layer to verify if the media |
| 1620 | * is still available. It is supposed to be harmless, lightweight and |
| 1621 | * non-intrusive in case the media was not changed. |
| 1622 | * |
| 1623 | * This call can sleep. |
| 1624 | * |
| 1625 | * The return code is bool! |
| 1626 | */ |
| 1627 | static int ub_bd_media_changed(struct gendisk *disk) |
| 1628 | { |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 1629 | struct ub_lun *lun = disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1630 | |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 1631 | if (!lun->removable) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1632 | return 0; |
| 1633 | |
| 1634 | /* |
| 1635 | * We clean checks always after every command, so this is not |
| 1636 | * as dangerous as it looks. If the TEST_UNIT_READY fails here, |
| 1637 | * the device is actually not ready with operator or software |
| 1638 | * intervention required. One dangerous item might be a drive which |
| 1639 | * spins itself down, and come the time to write dirty pages, this |
| 1640 | * will fail, then block layer discards the data. Since we never |
| 1641 | * spin drives up, such devices simply cannot be used with ub anyway. |
| 1642 | */ |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 1643 | if (ub_sync_tur(lun->udev, lun) != 0) { |
| 1644 | lun->changed = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1645 | return 1; |
| 1646 | } |
| 1647 | |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 1648 | return lun->changed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1649 | } |
| 1650 | |
| 1651 | static struct block_device_operations ub_bd_fops = { |
| 1652 | .owner = THIS_MODULE, |
| 1653 | .open = ub_bd_open, |
| 1654 | .release = ub_bd_release, |
| 1655 | .ioctl = ub_bd_ioctl, |
| 1656 | .media_changed = ub_bd_media_changed, |
| 1657 | .revalidate_disk = ub_bd_revalidate, |
| 1658 | }; |
| 1659 | |
| 1660 | /* |
| 1661 | * Common ->done routine for commands executed synchronously. |
| 1662 | */ |
| 1663 | static void ub_probe_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd) |
| 1664 | { |
| 1665 | struct completion *cop = cmd->back; |
| 1666 | complete(cop); |
| 1667 | } |
| 1668 | |
| 1669 | /* |
| 1670 | * Test if the device has a check condition on it, synchronously. |
| 1671 | */ |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 1672 | static int ub_sync_tur(struct ub_dev *sc, struct ub_lun *lun) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1673 | { |
| 1674 | struct ub_scsi_cmd *cmd; |
| 1675 | enum { ALLOC_SIZE = sizeof(struct ub_scsi_cmd) }; |
| 1676 | unsigned long flags; |
| 1677 | struct completion compl; |
| 1678 | int rc; |
| 1679 | |
| 1680 | init_completion(&compl); |
| 1681 | |
| 1682 | rc = -ENOMEM; |
| 1683 | if ((cmd = kmalloc(ALLOC_SIZE, GFP_KERNEL)) == NULL) |
| 1684 | goto err_alloc; |
| 1685 | memset(cmd, 0, ALLOC_SIZE); |
| 1686 | |
| 1687 | cmd->cdb[0] = TEST_UNIT_READY; |
| 1688 | cmd->cdb_len = 6; |
| 1689 | cmd->dir = UB_DIR_NONE; |
| 1690 | cmd->state = UB_CMDST_INIT; |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 1691 | cmd->lun = lun; /* This may be NULL, but that's ok */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1692 | cmd->done = ub_probe_done; |
| 1693 | cmd->back = &compl; |
| 1694 | |
| 1695 | spin_lock_irqsave(&sc->lock, flags); |
| 1696 | cmd->tag = sc->tagcnt++; |
| 1697 | |
| 1698 | rc = ub_submit_scsi(sc, cmd); |
| 1699 | spin_unlock_irqrestore(&sc->lock, flags); |
| 1700 | |
| 1701 | if (rc != 0) { |
| 1702 | printk("ub: testing ready: submit error (%d)\n", rc); /* P3 */ |
| 1703 | goto err_submit; |
| 1704 | } |
| 1705 | |
| 1706 | wait_for_completion(&compl); |
| 1707 | |
| 1708 | rc = cmd->error; |
| 1709 | |
| 1710 | if (rc == -EIO && cmd->key != 0) /* Retries for benh's key */ |
| 1711 | rc = cmd->key; |
| 1712 | |
| 1713 | err_submit: |
| 1714 | kfree(cmd); |
| 1715 | err_alloc: |
| 1716 | return rc; |
| 1717 | } |
| 1718 | |
| 1719 | /* |
| 1720 | * Read the SCSI capacity synchronously (for probing). |
| 1721 | */ |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 1722 | static int ub_sync_read_cap(struct ub_dev *sc, struct ub_lun *lun, |
| 1723 | struct ub_capacity *ret) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1724 | { |
| 1725 | struct ub_scsi_cmd *cmd; |
| 1726 | char *p; |
| 1727 | enum { ALLOC_SIZE = sizeof(struct ub_scsi_cmd) + 8 }; |
| 1728 | unsigned long flags; |
| 1729 | unsigned int bsize, shift; |
| 1730 | unsigned long nsec; |
| 1731 | struct completion compl; |
| 1732 | int rc; |
| 1733 | |
| 1734 | init_completion(&compl); |
| 1735 | |
| 1736 | rc = -ENOMEM; |
| 1737 | if ((cmd = kmalloc(ALLOC_SIZE, GFP_KERNEL)) == NULL) |
| 1738 | goto err_alloc; |
| 1739 | memset(cmd, 0, ALLOC_SIZE); |
| 1740 | p = (char *)cmd + sizeof(struct ub_scsi_cmd); |
| 1741 | |
| 1742 | cmd->cdb[0] = 0x25; |
| 1743 | cmd->cdb_len = 10; |
| 1744 | cmd->dir = UB_DIR_READ; |
| 1745 | cmd->state = UB_CMDST_INIT; |
| 1746 | cmd->data = p; |
| 1747 | cmd->len = 8; |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 1748 | cmd->lun = lun; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1749 | cmd->done = ub_probe_done; |
| 1750 | cmd->back = &compl; |
| 1751 | |
| 1752 | spin_lock_irqsave(&sc->lock, flags); |
| 1753 | cmd->tag = sc->tagcnt++; |
| 1754 | |
| 1755 | rc = ub_submit_scsi(sc, cmd); |
| 1756 | spin_unlock_irqrestore(&sc->lock, flags); |
| 1757 | |
| 1758 | if (rc != 0) { |
| 1759 | printk("ub: reading capacity: submit error (%d)\n", rc); /* P3 */ |
| 1760 | goto err_submit; |
| 1761 | } |
| 1762 | |
| 1763 | wait_for_completion(&compl); |
| 1764 | |
| 1765 | if (cmd->error != 0) { |
| 1766 | printk("ub: reading capacity: error %d\n", cmd->error); /* P3 */ |
| 1767 | rc = -EIO; |
| 1768 | goto err_read; |
| 1769 | } |
| 1770 | if (cmd->act_len != 8) { |
| 1771 | printk("ub: reading capacity: size %d\n", cmd->act_len); /* P3 */ |
| 1772 | rc = -EIO; |
| 1773 | goto err_read; |
| 1774 | } |
| 1775 | |
| 1776 | /* sd.c special-cases sector size of 0 to mean 512. Needed? Safe? */ |
| 1777 | nsec = be32_to_cpu(*(__be32 *)p) + 1; |
| 1778 | bsize = be32_to_cpu(*(__be32 *)(p + 4)); |
| 1779 | switch (bsize) { |
| 1780 | case 512: shift = 0; break; |
| 1781 | case 1024: shift = 1; break; |
| 1782 | case 2048: shift = 2; break; |
| 1783 | case 4096: shift = 3; break; |
| 1784 | default: |
| 1785 | printk("ub: Bad sector size %u\n", bsize); /* P3 */ |
| 1786 | rc = -EDOM; |
| 1787 | goto err_inv_bsize; |
| 1788 | } |
| 1789 | |
| 1790 | ret->bsize = bsize; |
| 1791 | ret->bshift = shift; |
| 1792 | ret->nsec = nsec << shift; |
| 1793 | rc = 0; |
| 1794 | |
| 1795 | err_inv_bsize: |
| 1796 | err_read: |
| 1797 | err_submit: |
| 1798 | kfree(cmd); |
| 1799 | err_alloc: |
| 1800 | return rc; |
| 1801 | } |
| 1802 | |
| 1803 | /* |
| 1804 | */ |
| 1805 | static void ub_probe_urb_complete(struct urb *urb, struct pt_regs *pt) |
| 1806 | { |
| 1807 | struct completion *cop = urb->context; |
| 1808 | complete(cop); |
| 1809 | } |
| 1810 | |
| 1811 | static void ub_probe_timeout(unsigned long arg) |
| 1812 | { |
| 1813 | struct completion *cop = (struct completion *) arg; |
| 1814 | complete(cop); |
| 1815 | } |
| 1816 | |
| 1817 | /* |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 1818 | * Get number of LUNs by the way of Bulk GetMaxLUN command. |
| 1819 | */ |
| 1820 | static int ub_sync_getmaxlun(struct ub_dev *sc) |
| 1821 | { |
| 1822 | int ifnum = sc->intf->cur_altsetting->desc.bInterfaceNumber; |
| 1823 | unsigned char *p; |
| 1824 | enum { ALLOC_SIZE = 1 }; |
| 1825 | struct usb_ctrlrequest *cr; |
| 1826 | struct completion compl; |
| 1827 | struct timer_list timer; |
| 1828 | int nluns; |
| 1829 | int rc; |
| 1830 | |
| 1831 | init_completion(&compl); |
| 1832 | |
| 1833 | rc = -ENOMEM; |
| 1834 | if ((p = kmalloc(ALLOC_SIZE, GFP_KERNEL)) == NULL) |
| 1835 | goto err_alloc; |
| 1836 | *p = 55; |
| 1837 | |
| 1838 | cr = &sc->work_cr; |
| 1839 | cr->bRequestType = USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE; |
| 1840 | cr->bRequest = US_BULK_GET_MAX_LUN; |
| 1841 | cr->wValue = cpu_to_le16(0); |
| 1842 | cr->wIndex = cpu_to_le16(ifnum); |
| 1843 | cr->wLength = cpu_to_le16(1); |
| 1844 | |
| 1845 | usb_fill_control_urb(&sc->work_urb, sc->dev, sc->recv_ctrl_pipe, |
| 1846 | (unsigned char*) cr, p, 1, ub_probe_urb_complete, &compl); |
| 1847 | sc->work_urb.transfer_flags = 0; |
| 1848 | sc->work_urb.actual_length = 0; |
| 1849 | sc->work_urb.error_count = 0; |
| 1850 | sc->work_urb.status = 0; |
| 1851 | |
| 1852 | if ((rc = usb_submit_urb(&sc->work_urb, GFP_KERNEL)) != 0) { |
| 1853 | if (rc == -EPIPE) { |
| 1854 | printk("%s: Stall at GetMaxLUN, using 1 LUN\n", |
| 1855 | sc->name); /* P3 */ |
| 1856 | } else { |
| 1857 | printk(KERN_WARNING |
| 1858 | "%s: Unable to submit GetMaxLUN (%d)\n", |
| 1859 | sc->name, rc); |
| 1860 | } |
| 1861 | goto err_submit; |
| 1862 | } |
| 1863 | |
| 1864 | init_timer(&timer); |
| 1865 | timer.function = ub_probe_timeout; |
| 1866 | timer.data = (unsigned long) &compl; |
| 1867 | timer.expires = jiffies + UB_CTRL_TIMEOUT; |
| 1868 | add_timer(&timer); |
| 1869 | |
| 1870 | wait_for_completion(&compl); |
| 1871 | |
| 1872 | del_timer_sync(&timer); |
| 1873 | usb_kill_urb(&sc->work_urb); |
| 1874 | |
| 1875 | if (sc->work_urb.actual_length != 1) { |
| 1876 | printk("%s: GetMaxLUN returned %d bytes\n", sc->name, |
| 1877 | sc->work_urb.actual_length); /* P3 */ |
| 1878 | nluns = 0; |
| 1879 | } else { |
| 1880 | if ((nluns = *p) == 55) { |
| 1881 | nluns = 0; |
| 1882 | } else { |
| 1883 | /* GetMaxLUN returns the maximum LUN number */ |
| 1884 | nluns += 1; |
| 1885 | if (nluns > UB_MAX_LUNS) |
| 1886 | nluns = UB_MAX_LUNS; |
| 1887 | } |
| 1888 | printk("%s: GetMaxLUN returned %d, using %d LUNs\n", sc->name, |
| 1889 | *p, nluns); /* P3 */ |
| 1890 | } |
| 1891 | |
| 1892 | kfree(p); |
| 1893 | return nluns; |
| 1894 | |
| 1895 | err_submit: |
| 1896 | kfree(p); |
| 1897 | err_alloc: |
| 1898 | return rc; |
| 1899 | } |
| 1900 | |
| 1901 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1902 | * Clear initial stalls. |
| 1903 | */ |
| 1904 | static int ub_probe_clear_stall(struct ub_dev *sc, int stalled_pipe) |
| 1905 | { |
| 1906 | int endp; |
| 1907 | struct usb_ctrlrequest *cr; |
| 1908 | struct completion compl; |
| 1909 | struct timer_list timer; |
| 1910 | int rc; |
| 1911 | |
| 1912 | init_completion(&compl); |
| 1913 | |
| 1914 | endp = usb_pipeendpoint(stalled_pipe); |
| 1915 | if (usb_pipein (stalled_pipe)) |
| 1916 | endp |= USB_DIR_IN; |
| 1917 | |
| 1918 | cr = &sc->work_cr; |
| 1919 | cr->bRequestType = USB_RECIP_ENDPOINT; |
| 1920 | cr->bRequest = USB_REQ_CLEAR_FEATURE; |
| 1921 | cr->wValue = cpu_to_le16(USB_ENDPOINT_HALT); |
| 1922 | cr->wIndex = cpu_to_le16(endp); |
| 1923 | cr->wLength = cpu_to_le16(0); |
| 1924 | |
| 1925 | usb_fill_control_urb(&sc->work_urb, sc->dev, sc->send_ctrl_pipe, |
| 1926 | (unsigned char*) cr, NULL, 0, ub_probe_urb_complete, &compl); |
| 1927 | sc->work_urb.transfer_flags = 0; |
| 1928 | sc->work_urb.actual_length = 0; |
| 1929 | sc->work_urb.error_count = 0; |
| 1930 | sc->work_urb.status = 0; |
| 1931 | |
| 1932 | if ((rc = usb_submit_urb(&sc->work_urb, GFP_KERNEL)) != 0) { |
| 1933 | printk(KERN_WARNING |
| 1934 | "%s: Unable to submit a probe clear (%d)\n", sc->name, rc); |
| 1935 | return rc; |
| 1936 | } |
| 1937 | |
| 1938 | init_timer(&timer); |
| 1939 | timer.function = ub_probe_timeout; |
| 1940 | timer.data = (unsigned long) &compl; |
| 1941 | timer.expires = jiffies + UB_CTRL_TIMEOUT; |
| 1942 | add_timer(&timer); |
| 1943 | |
| 1944 | wait_for_completion(&compl); |
| 1945 | |
| 1946 | del_timer_sync(&timer); |
| 1947 | usb_kill_urb(&sc->work_urb); |
| 1948 | |
| 1949 | /* reset the endpoint toggle */ |
| 1950 | usb_settoggle(sc->dev, endp, usb_pipeout(sc->last_pipe), 0); |
| 1951 | |
| 1952 | return 0; |
| 1953 | } |
| 1954 | |
| 1955 | /* |
| 1956 | * Get the pipe settings. |
| 1957 | */ |
| 1958 | static int ub_get_pipes(struct ub_dev *sc, struct usb_device *dev, |
| 1959 | struct usb_interface *intf) |
| 1960 | { |
| 1961 | struct usb_host_interface *altsetting = intf->cur_altsetting; |
| 1962 | struct usb_endpoint_descriptor *ep_in = NULL; |
| 1963 | struct usb_endpoint_descriptor *ep_out = NULL; |
| 1964 | struct usb_endpoint_descriptor *ep; |
| 1965 | int i; |
| 1966 | |
| 1967 | /* |
| 1968 | * Find the endpoints we need. |
| 1969 | * We are expecting a minimum of 2 endpoints - in and out (bulk). |
| 1970 | * We will ignore any others. |
| 1971 | */ |
| 1972 | for (i = 0; i < altsetting->desc.bNumEndpoints; i++) { |
| 1973 | ep = &altsetting->endpoint[i].desc; |
| 1974 | |
| 1975 | /* Is it a BULK endpoint? */ |
| 1976 | if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) |
| 1977 | == USB_ENDPOINT_XFER_BULK) { |
| 1978 | /* BULK in or out? */ |
| 1979 | if (ep->bEndpointAddress & USB_DIR_IN) |
| 1980 | ep_in = ep; |
| 1981 | else |
| 1982 | ep_out = ep; |
| 1983 | } |
| 1984 | } |
| 1985 | |
| 1986 | if (ep_in == NULL || ep_out == NULL) { |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 1987 | printk(KERN_NOTICE "%s: failed endpoint check\n", |
| 1988 | sc->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1989 | return -EIO; |
| 1990 | } |
| 1991 | |
| 1992 | /* Calculate and store the pipe values */ |
| 1993 | sc->send_ctrl_pipe = usb_sndctrlpipe(dev, 0); |
| 1994 | sc->recv_ctrl_pipe = usb_rcvctrlpipe(dev, 0); |
| 1995 | sc->send_bulk_pipe = usb_sndbulkpipe(dev, |
| 1996 | ep_out->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK); |
| 1997 | sc->recv_bulk_pipe = usb_rcvbulkpipe(dev, |
| 1998 | ep_in->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK); |
| 1999 | |
| 2000 | return 0; |
| 2001 | } |
| 2002 | |
| 2003 | /* |
| 2004 | * Probing is done in the process context, which allows us to cheat |
| 2005 | * and not to build a state machine for the discovery. |
| 2006 | */ |
| 2007 | static int ub_probe(struct usb_interface *intf, |
| 2008 | const struct usb_device_id *dev_id) |
| 2009 | { |
| 2010 | struct ub_dev *sc; |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 2011 | int nluns; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2012 | int rc; |
| 2013 | int i; |
| 2014 | |
| 2015 | rc = -ENOMEM; |
| 2016 | if ((sc = kmalloc(sizeof(struct ub_dev), GFP_KERNEL)) == NULL) |
| 2017 | goto err_core; |
| 2018 | memset(sc, 0, sizeof(struct ub_dev)); |
| 2019 | spin_lock_init(&sc->lock); |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 2020 | INIT_LIST_HEAD(&sc->luns); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2021 | usb_init_urb(&sc->work_urb); |
| 2022 | tasklet_init(&sc->tasklet, ub_scsi_action, (unsigned long)sc); |
| 2023 | atomic_set(&sc->poison, 0); |
| 2024 | |
| 2025 | init_timer(&sc->work_timer); |
| 2026 | sc->work_timer.data = (unsigned long) sc; |
| 2027 | sc->work_timer.function = ub_urb_timeout; |
| 2028 | |
| 2029 | ub_init_completion(&sc->work_done); |
| 2030 | sc->work_done.done = 1; /* A little yuk, but oh well... */ |
| 2031 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2032 | sc->dev = interface_to_usbdev(intf); |
| 2033 | sc->intf = intf; |
| 2034 | // sc->ifnum = intf->cur_altsetting->desc.bInterfaceNumber; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2035 | usb_set_intfdata(intf, sc); |
| 2036 | usb_get_dev(sc->dev); |
| 2037 | // usb_get_intf(sc->intf); /* Do we need this? */ |
| 2038 | |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 2039 | snprintf(sc->name, 12, DRV_NAME "(%d.%d)", |
| 2040 | sc->dev->bus->busnum, sc->dev->devnum); |
| 2041 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2042 | /* XXX Verify that we can handle the device (from descriptors) */ |
| 2043 | |
| 2044 | ub_get_pipes(sc, sc->dev, intf); |
| 2045 | |
| 2046 | if (device_create_file(&sc->intf->dev, &dev_attr_diag) != 0) |
| 2047 | goto err_diag; |
| 2048 | |
| 2049 | /* |
| 2050 | * At this point, all USB initialization is done, do upper layer. |
| 2051 | * We really hate halfway initialized structures, so from the |
| 2052 | * invariants perspective, this ub_dev is fully constructed at |
| 2053 | * this point. |
| 2054 | */ |
| 2055 | |
| 2056 | /* |
| 2057 | * This is needed to clear toggles. It is a problem only if we do |
| 2058 | * `rmmod ub && modprobe ub` without disconnects, but we like that. |
| 2059 | */ |
| 2060 | ub_probe_clear_stall(sc, sc->recv_bulk_pipe); |
| 2061 | ub_probe_clear_stall(sc, sc->send_bulk_pipe); |
| 2062 | |
| 2063 | /* |
| 2064 | * The way this is used by the startup code is a little specific. |
| 2065 | * A SCSI check causes a USB stall. Our common case code sees it |
| 2066 | * and clears the check, after which the device is ready for use. |
| 2067 | * But if a check was not present, any command other than |
| 2068 | * TEST_UNIT_READY ends with a lockup (including REQUEST_SENSE). |
| 2069 | * |
| 2070 | * If we neglect to clear the SCSI check, the first real command fails |
| 2071 | * (which is the capacity readout). We clear that and retry, but why |
| 2072 | * causing spurious retries for no reason. |
| 2073 | * |
| 2074 | * Revalidation may start with its own TEST_UNIT_READY, but that one |
| 2075 | * has to succeed, so we clear checks with an additional one here. |
| 2076 | * In any case it's not our business how revaliadation is implemented. |
| 2077 | */ |
| 2078 | for (i = 0; i < 3; i++) { /* Retries for benh's key */ |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 2079 | if ((rc = ub_sync_tur(sc, NULL)) <= 0) break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2080 | if (rc != 0x6) break; |
| 2081 | msleep(10); |
| 2082 | } |
| 2083 | |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 2084 | nluns = 1; |
| 2085 | for (i = 0; i < 3; i++) { |
| 2086 | if ((rc = ub_sync_getmaxlun(sc)) < 0) { |
| 2087 | /* |
| 2088 | * Some devices (i.e. Iomega Zip100) need this -- |
| 2089 | * apparently the bulk pipes get STALLed when the |
| 2090 | * GetMaxLUN request is processed. |
| 2091 | * XXX I have a ZIP-100, verify it does this. |
| 2092 | */ |
| 2093 | if (rc == -EPIPE) { |
| 2094 | ub_probe_clear_stall(sc, sc->recv_bulk_pipe); |
| 2095 | ub_probe_clear_stall(sc, sc->send_bulk_pipe); |
| 2096 | } |
| 2097 | break; |
| 2098 | } |
| 2099 | if (rc != 0) { |
| 2100 | nluns = rc; |
| 2101 | break; |
| 2102 | } |
Pete Zaitcev | 9f793d2 | 2005-06-06 13:54:59 -0700 | [diff] [blame] | 2103 | msleep(100); |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 2104 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2105 | |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 2106 | for (i = 0; i < nluns; i++) { |
| 2107 | ub_probe_lun(sc, i); |
| 2108 | } |
| 2109 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2110 | |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 2111 | /* device_remove_file(&sc->intf->dev, &dev_attr_diag); */ |
| 2112 | err_diag: |
| 2113 | usb_set_intfdata(intf, NULL); |
| 2114 | // usb_put_intf(sc->intf); |
| 2115 | usb_put_dev(sc->dev); |
| 2116 | kfree(sc); |
| 2117 | err_core: |
| 2118 | return rc; |
| 2119 | } |
| 2120 | |
| 2121 | static int ub_probe_lun(struct ub_dev *sc, int lnum) |
| 2122 | { |
| 2123 | struct ub_lun *lun; |
| 2124 | request_queue_t *q; |
| 2125 | struct gendisk *disk; |
| 2126 | int rc; |
| 2127 | |
| 2128 | rc = -ENOMEM; |
| 2129 | if ((lun = kmalloc(sizeof(struct ub_lun), GFP_KERNEL)) == NULL) |
| 2130 | goto err_alloc; |
| 2131 | memset(lun, 0, sizeof(struct ub_lun)); |
| 2132 | lun->num = lnum; |
| 2133 | |
| 2134 | rc = -ENOSR; |
| 2135 | if ((lun->id = ub_id_get()) == -1) |
| 2136 | goto err_id; |
| 2137 | |
| 2138 | lun->udev = sc; |
| 2139 | list_add(&lun->link, &sc->luns); |
| 2140 | |
| 2141 | snprintf(lun->name, 16, DRV_NAME "%c(%d.%d.%d)", |
| 2142 | lun->id + 'a', sc->dev->bus->busnum, sc->dev->devnum, lun->num); |
| 2143 | |
| 2144 | lun->removable = 1; /* XXX Query this from the device */ |
| 2145 | lun->changed = 1; /* ub_revalidate clears only */ |
| 2146 | lun->first_open = 1; |
| 2147 | ub_revalidate(sc, lun); |
| 2148 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2149 | rc = -ENOMEM; |
| 2150 | if ((disk = alloc_disk(UB_MINORS_PER_MAJOR)) == NULL) |
| 2151 | goto err_diskalloc; |
| 2152 | |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 2153 | lun->disk = disk; |
| 2154 | sprintf(disk->disk_name, DRV_NAME "%c", lun->id + 'a'); |
| 2155 | sprintf(disk->devfs_name, DEVFS_NAME "/%c", lun->id + 'a'); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2156 | disk->major = UB_MAJOR; |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 2157 | disk->first_minor = lun->id * UB_MINORS_PER_MAJOR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2158 | disk->fops = &ub_bd_fops; |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 2159 | disk->private_data = lun; |
| 2160 | disk->driverfs_dev = &sc->intf->dev; /* XXX Many to one ok? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2161 | |
| 2162 | rc = -ENOMEM; |
| 2163 | if ((q = blk_init_queue(ub_bd_rq_fn, &sc->lock)) == NULL) |
| 2164 | goto err_blkqinit; |
| 2165 | |
| 2166 | disk->queue = q; |
| 2167 | |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 2168 | blk_queue_bounce_limit(q, BLK_BOUNCE_HIGH); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2169 | blk_queue_max_hw_segments(q, UB_MAX_REQ_SG); |
| 2170 | blk_queue_max_phys_segments(q, UB_MAX_REQ_SG); |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 2171 | blk_queue_segment_boundary(q, 0xffffffff); /* Dubious. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2172 | blk_queue_max_sectors(q, UB_MAX_SECTORS); |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 2173 | blk_queue_hardsect_size(q, lun->capacity.bsize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2174 | |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 2175 | q->queuedata = lun; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2176 | |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 2177 | set_capacity(disk, lun->capacity.nsec); |
| 2178 | if (lun->removable) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2179 | disk->flags |= GENHD_FL_REMOVABLE; |
| 2180 | |
| 2181 | add_disk(disk); |
| 2182 | |
| 2183 | return 0; |
| 2184 | |
| 2185 | err_blkqinit: |
| 2186 | put_disk(disk); |
| 2187 | err_diskalloc: |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 2188 | list_del(&lun->link); |
| 2189 | ub_id_put(lun->id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2190 | err_id: |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 2191 | kfree(lun); |
| 2192 | err_alloc: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2193 | return rc; |
| 2194 | } |
| 2195 | |
| 2196 | static void ub_disconnect(struct usb_interface *intf) |
| 2197 | { |
| 2198 | struct ub_dev *sc = usb_get_intfdata(intf); |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 2199 | struct list_head *p; |
| 2200 | struct ub_lun *lun; |
| 2201 | struct gendisk *disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2202 | unsigned long flags; |
| 2203 | |
| 2204 | /* |
| 2205 | * Prevent ub_bd_release from pulling the rug from under us. |
| 2206 | * XXX This is starting to look like a kref. |
| 2207 | * XXX Why not to take this ref at probe time? |
| 2208 | */ |
| 2209 | spin_lock_irqsave(&ub_lock, flags); |
| 2210 | sc->openc++; |
| 2211 | spin_unlock_irqrestore(&ub_lock, flags); |
| 2212 | |
| 2213 | /* |
| 2214 | * Fence stall clearnings, operations triggered by unlinkings and so on. |
| 2215 | * We do not attempt to unlink any URBs, because we do not trust the |
| 2216 | * unlink paths in HC drivers. Also, we get -84 upon disconnect anyway. |
| 2217 | */ |
| 2218 | atomic_set(&sc->poison, 1); |
| 2219 | |
| 2220 | /* |
| 2221 | * Blow away queued commands. |
| 2222 | * |
| 2223 | * Actually, this never works, because before we get here |
| 2224 | * the HCD terminates outstanding URB(s). It causes our |
| 2225 | * SCSI command queue to advance, commands fail to submit, |
| 2226 | * and the whole queue drains. So, we just use this code to |
| 2227 | * print warnings. |
| 2228 | */ |
| 2229 | spin_lock_irqsave(&sc->lock, flags); |
| 2230 | { |
| 2231 | struct ub_scsi_cmd *cmd; |
| 2232 | int cnt = 0; |
| 2233 | while ((cmd = ub_cmdq_pop(sc)) != NULL) { |
| 2234 | cmd->error = -ENOTCONN; |
| 2235 | cmd->state = UB_CMDST_DONE; |
| 2236 | ub_cmdtr_state(sc, cmd); |
| 2237 | ub_cmdq_pop(sc); |
| 2238 | (*cmd->done)(sc, cmd); |
| 2239 | cnt++; |
| 2240 | } |
| 2241 | if (cnt != 0) { |
| 2242 | printk(KERN_WARNING "%s: " |
| 2243 | "%d was queued after shutdown\n", sc->name, cnt); |
| 2244 | } |
| 2245 | } |
| 2246 | spin_unlock_irqrestore(&sc->lock, flags); |
| 2247 | |
| 2248 | /* |
| 2249 | * Unregister the upper layer. |
| 2250 | */ |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 2251 | list_for_each (p, &sc->luns) { |
| 2252 | lun = list_entry(p, struct ub_lun, link); |
| 2253 | disk = lun->disk; |
| 2254 | if (disk->flags & GENHD_FL_UP) |
| 2255 | del_gendisk(disk); |
| 2256 | /* |
| 2257 | * I wish I could do: |
| 2258 | * set_bit(QUEUE_FLAG_DEAD, &q->queue_flags); |
| 2259 | * As it is, we rely on our internal poisoning and let |
| 2260 | * the upper levels to spin furiously failing all the I/O. |
| 2261 | */ |
| 2262 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2263 | |
| 2264 | /* |
| 2265 | * Taking a lock on a structure which is about to be freed |
| 2266 | * is very nonsensual. Here it is largely a way to do a debug freeze, |
| 2267 | * and a bracket which shows where the nonsensual code segment ends. |
| 2268 | * |
| 2269 | * Testing for -EINPROGRESS is always a bug, so we are bending |
| 2270 | * the rules a little. |
| 2271 | */ |
| 2272 | spin_lock_irqsave(&sc->lock, flags); |
| 2273 | if (sc->work_urb.status == -EINPROGRESS) { /* janitors: ignore */ |
| 2274 | printk(KERN_WARNING "%s: " |
| 2275 | "URB is active after disconnect\n", sc->name); |
| 2276 | } |
| 2277 | spin_unlock_irqrestore(&sc->lock, flags); |
| 2278 | |
| 2279 | /* |
| 2280 | * There is virtually no chance that other CPU runs times so long |
| 2281 | * after ub_urb_complete should have called del_timer, but only if HCD |
| 2282 | * didn't forget to deliver a callback on unlink. |
| 2283 | */ |
| 2284 | del_timer_sync(&sc->work_timer); |
| 2285 | |
| 2286 | /* |
| 2287 | * At this point there must be no commands coming from anyone |
| 2288 | * and no URBs left in transit. |
| 2289 | */ |
| 2290 | |
| 2291 | device_remove_file(&sc->intf->dev, &dev_attr_diag); |
| 2292 | usb_set_intfdata(intf, NULL); |
| 2293 | // usb_put_intf(sc->intf); |
| 2294 | sc->intf = NULL; |
| 2295 | usb_put_dev(sc->dev); |
| 2296 | sc->dev = NULL; |
| 2297 | |
| 2298 | ub_put(sc); |
| 2299 | } |
| 2300 | |
| 2301 | static struct usb_driver ub_driver = { |
| 2302 | .owner = THIS_MODULE, |
| 2303 | .name = "ub", |
| 2304 | .probe = ub_probe, |
| 2305 | .disconnect = ub_disconnect, |
| 2306 | .id_table = ub_usb_ids, |
| 2307 | }; |
| 2308 | |
| 2309 | static int __init ub_init(void) |
| 2310 | { |
| 2311 | int rc; |
| 2312 | |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 2313 | /* P3 */ printk("ub: sizeof ub_scsi_cmd %zu ub_dev %zu ub_lun %zu\n", |
| 2314 | sizeof(struct ub_scsi_cmd), sizeof(struct ub_dev), sizeof(struct ub_lun)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2315 | |
| 2316 | if ((rc = register_blkdev(UB_MAJOR, DRV_NAME)) != 0) |
| 2317 | goto err_regblkdev; |
| 2318 | devfs_mk_dir(DEVFS_NAME); |
| 2319 | |
| 2320 | if ((rc = usb_register(&ub_driver)) != 0) |
| 2321 | goto err_register; |
| 2322 | |
| 2323 | return 0; |
| 2324 | |
| 2325 | err_register: |
| 2326 | devfs_remove(DEVFS_NAME); |
| 2327 | unregister_blkdev(UB_MAJOR, DRV_NAME); |
| 2328 | err_regblkdev: |
| 2329 | return rc; |
| 2330 | } |
| 2331 | |
| 2332 | static void __exit ub_exit(void) |
| 2333 | { |
| 2334 | usb_deregister(&ub_driver); |
| 2335 | |
| 2336 | devfs_remove(DEVFS_NAME); |
| 2337 | unregister_blkdev(UB_MAJOR, DRV_NAME); |
| 2338 | } |
| 2339 | |
| 2340 | module_init(ub_init); |
| 2341 | module_exit(ub_exit); |
| 2342 | |
| 2343 | MODULE_LICENSE("GPL"); |