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