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