| Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1 | /* drivers/usb/function/ums.c |
| 2 | * |
| 3 | * Function Device for USB Mass Storage |
| 4 | * |
| 5 | * Copyright (C) 2007 Google, Inc. |
| 6 | * Author: Mike Lockwood <lockwood@android.com> |
| 7 | * |
| 8 | * This software is licensed under the terms of the GNU General Public |
| 9 | * License version 2, as published by the Free Software Foundation, and |
| 10 | * may be copied, distributed, and modified under those terms. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | */ |
| 18 | |
| 19 | #include <linux/init.h> |
| 20 | #include <linux/module.h> |
| 21 | #include <linux/kernel.h> |
| 22 | #include <linux/miscdevice.h> |
| 23 | #include <linux/fs.h> |
| 24 | |
| 25 | #include <linux/wait.h> |
| 26 | #include <linux/list.h> |
| 27 | |
| 28 | #include <linux/usb/ch9.h> |
| 29 | #include <linux/usb_usual.h> |
| 30 | |
| 31 | #include <asm/atomic.h> |
| 32 | #include <asm/uaccess.h> |
| 33 | |
| 34 | #include "usb_function.h" |
| 35 | |
| 36 | #if 1 |
| 37 | #define DBG(x...) do {} while (0) |
| 38 | #else |
| 39 | #define DBG(x...) printk(x) |
| 40 | #endif |
| 41 | |
| 42 | #define TXN_MAX 4096 |
| 43 | |
| 44 | /* UMS setup class requests */ |
| 45 | #define USB_BULK_GET_MAX_LUN_REQUEST 0xFE |
| 46 | #define USB_BULK_RESET_REQUEST 0xFF |
| 47 | |
| 48 | /* number of rx and tx requests to allocate */ |
| 49 | #define RX_REQ_MAX 4 |
| 50 | #define TX_REQ_MAX 4 |
| 51 | |
| 52 | /* FIXME - add ioctl() support for LUN count */ |
| 53 | int lun_count = 1; |
| 54 | |
| 55 | struct ums_context |
| 56 | { |
| 57 | int online; |
| 58 | int error; |
| 59 | |
| 60 | atomic_t read_excl; |
| 61 | atomic_t write_excl; |
| 62 | atomic_t open_excl; |
| 63 | spinlock_t lock; |
| 64 | |
| 65 | struct usb_endpoint *out; |
| 66 | struct usb_endpoint *in; |
| 67 | |
| 68 | struct list_head tx_idle; |
| 69 | struct list_head rx_idle; |
| 70 | struct list_head rx_done; |
| 71 | |
| 72 | wait_queue_head_t read_wq; |
| 73 | wait_queue_head_t write_wq; |
| 74 | |
| 75 | /* the request we're currently reading from */ |
| 76 | struct usb_request *read_req; |
| 77 | unsigned char *read_buf; |
| 78 | }; |
| 79 | |
| 80 | static struct ums_context _context; |
| 81 | |
| 82 | static inline int _lock(atomic_t *excl) |
| 83 | { |
| 84 | if(atomic_inc_return(excl) == 1) { |
| 85 | return 0; |
| 86 | } else { |
| 87 | atomic_dec(excl); |
| 88 | return -1; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | static inline void _unlock(atomic_t *excl) |
| 93 | { |
| 94 | atomic_dec(excl); |
| 95 | } |
| 96 | |
| 97 | /* add a request to the tail of a list */ |
| 98 | static void req_put(struct ums_context *ctxt, struct list_head *head, struct usb_request *req) |
| 99 | { |
| 100 | unsigned long flags; |
| 101 | |
| 102 | spin_lock_irqsave(&ctxt->lock, flags); |
| 103 | list_add_tail(&req->list, head); |
| 104 | spin_unlock_irqrestore(&ctxt->lock, flags); |
| 105 | } |
| 106 | |
| 107 | /* remove a request from the head of a list */ |
| 108 | static struct usb_request *req_get(struct ums_context *ctxt, struct list_head *head) |
| 109 | { |
| 110 | unsigned long flags; |
| 111 | struct usb_request *req; |
| 112 | |
| 113 | spin_lock_irqsave(&ctxt->lock, flags); |
| 114 | if(list_empty(head)) { |
| 115 | req = 0; |
| 116 | } else { |
| 117 | req = list_first_entry(head, struct usb_request, list); |
| 118 | list_del(&req->list); |
| 119 | } |
| 120 | spin_unlock_irqrestore(&ctxt->lock, flags); |
| 121 | return req; |
| 122 | } |
| 123 | |
| 124 | static void ums_complete_in(struct usb_endpoint *ept, struct usb_request *req) |
| 125 | { |
| 126 | struct ums_context *ctxt = req->context; |
| 127 | |
| 128 | DBG("ums_complete_in length: %d, actual: %d \n", req->length, req->actual); |
| 129 | |
| 130 | if(req->status != 0) |
| 131 | ctxt->error = 1; |
| 132 | |
| 133 | req_put(ctxt, &ctxt->tx_idle, req); |
| 134 | |
| 135 | wake_up(&ctxt->write_wq); |
| 136 | } |
| 137 | |
| 138 | static void ums_complete_out(struct usb_endpoint *ept, struct usb_request *req) |
| 139 | { |
| 140 | struct ums_context *ctxt = req->context; |
| 141 | |
| 142 | DBG("ums_complete_out length: %d, actual: %d \n", req->length, req->actual); |
| 143 | |
| 144 | if(req->status != 0) { |
| 145 | ctxt->error = 1; |
| 146 | req_put(ctxt, &ctxt->rx_idle, req); |
| 147 | } else { |
| 148 | req_put(ctxt, &ctxt->rx_done, req); |
| 149 | } |
| 150 | |
| 151 | wake_up(&ctxt->read_wq); |
| 152 | } |
| 153 | |
| 154 | static ssize_t ums_read(struct file *fp, char __user *buf, |
| 155 | size_t count, loff_t *pos) |
| 156 | { |
| 157 | struct ums_context *ctxt = &_context; |
| 158 | struct usb_request *req; |
| 159 | int r = count, xfer; |
| 160 | int ret; |
| 161 | |
| 162 | DBG("ums_read(%d)\n", count); |
| 163 | |
| 164 | if(_lock(&ctxt->read_excl)) |
| 165 | return -EBUSY; |
| 166 | |
| 167 | /* we will block until we're online */ |
| 168 | while(!(ctxt->online || ctxt->error)) { |
| 169 | DBG("ums_read: waiting for online state\n"); |
| 170 | ret = wait_event_interruptible(ctxt->read_wq, (ctxt->online || ctxt->error)); |
| 171 | if(ret < 0) { |
| 172 | _unlock(&ctxt->read_excl); |
| 173 | return ret; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | if(ctxt->error) { |
| 178 | r = -EIO; |
| 179 | goto fail; |
| 180 | } |
| 181 | |
| 182 | /* if we have idle read requests, get them queued */ |
| 183 | if((req = req_get(ctxt, &ctxt->rx_idle))) { |
| 184 | req->length = count; |
| 185 | ret = usb_ept_queue_xfer(ctxt->out, req); |
| 186 | if(ret < 0) { |
| 187 | DBG("ums_read: failed to queue req %p (%d)\n", req, ret); |
| 188 | r = -EIO; |
| 189 | ctxt->error = 1; |
| 190 | req_put(ctxt, &ctxt->rx_idle, req); |
| 191 | goto fail; |
| 192 | } else { |
| 193 | DBG("rx %p queue\n", req); |
| 194 | } |
| 195 | } else { |
| 196 | DBG("req_get failed!\n"); |
| 197 | goto fail; |
| 198 | } |
| 199 | |
| 200 | /* wait for a request to complete */ |
| 201 | req = 0; |
| 202 | ret = wait_event_interruptible(ctxt->read_wq, |
| 203 | ((req = req_get(ctxt, &ctxt->rx_done)) || ctxt->error)); |
| 204 | |
| 205 | if(req != 0) { |
| 206 | ctxt->read_req = req; |
| 207 | ctxt->read_buf = req->buf; |
| 208 | DBG("rx %p %d\n", req, req->actual); |
| 209 | |
| 210 | xfer = req->actual; |
| 211 | if (xfer > count) { |
| 212 | xfer = count; |
| 213 | } |
| 214 | r = xfer; |
| 215 | |
| 216 | if (xfer > 0) { |
| 217 | DBG("copy_to_user %d bytes\n", xfer); |
| 218 | if(copy_to_user(buf, ctxt->read_buf, xfer)) { |
| 219 | r = -EFAULT; |
| 220 | } |
| 221 | |
| 222 | } |
| 223 | req_put(ctxt, &ctxt->rx_idle, ctxt->read_req); |
| 224 | ctxt->read_req = 0; |
| 225 | } else { |
| 226 | r = ret; |
| 227 | } |
| 228 | |
| 229 | fail: |
| 230 | _unlock(&ctxt->read_excl); |
| 231 | DBG("ums_read returning %d\n", r); |
| 232 | return r; |
| 233 | } |
| 234 | |
| 235 | static ssize_t ums_write(struct file *fp, const char __user *buf, |
| 236 | size_t count, loff_t *pos) |
| 237 | { |
| 238 | struct ums_context *ctxt = &_context; |
| 239 | struct usb_request *req = 0; |
| 240 | int r = count, xfer; |
| 241 | int ret; |
| 242 | |
| 243 | DBG("ums_write(%d)\n", count); |
| 244 | |
| 245 | if(_lock(&ctxt->write_excl)) |
| 246 | return -EBUSY; |
| 247 | |
| 248 | while(count >= 0) { |
| 249 | if(ctxt->error) { |
| 250 | r = -EIO; |
| 251 | break; |
| 252 | } |
| 253 | |
| 254 | /* get an idle tx request to use */ |
| 255 | req = 0; |
| 256 | ret = wait_event_interruptible(ctxt->write_wq, |
| 257 | ((req = req_get(ctxt, &ctxt->tx_idle)) || ctxt->error)); |
| 258 | |
| 259 | if(ret < 0) { |
| 260 | r = ret; |
| 261 | break; |
| 262 | } |
| 263 | |
| 264 | if(req != 0) { |
| 265 | xfer = count > TXN_MAX ? TXN_MAX : count; |
| 266 | if(copy_from_user(req->buf, buf, xfer)){ |
| 267 | r = -EFAULT; |
| 268 | break; |
| 269 | } |
| 270 | |
| 271 | req->length = xfer; |
| 272 | ret = usb_ept_queue_xfer(ctxt->in, req); |
| 273 | if(ret < 0) { |
| 274 | DBG("ums_write: xfer error %d\n", ret); |
| 275 | ctxt->error = 1; |
| 276 | r = -EIO; |
| 277 | break; |
| 278 | } |
| 279 | |
| 280 | buf += xfer; |
| 281 | count -= xfer; |
| 282 | |
| 283 | /* zero this so we don't try to free it on error exit */ |
| 284 | req = 0; |
| 285 | if (count == 0) { |
| 286 | break; |
| 287 | } |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | |
| 292 | if(req) |
| 293 | req_put(ctxt, &ctxt->tx_idle, req); |
| 294 | |
| 295 | _unlock(&ctxt->write_excl); |
| 296 | DBG("ums_write returning %d\n", r); |
| 297 | return r; |
| 298 | } |
| 299 | |
| 300 | static int ums_open(struct inode *ip, struct file *fp) |
| 301 | { |
| 302 | struct ums_context *ctxt = &_context; |
| 303 | |
| 304 | if(_lock(&ctxt->open_excl)) |
| 305 | return -EBUSY; |
| 306 | |
| 307 | /* clear the error latch */ |
| 308 | ctxt->error = 0; |
| 309 | |
| 310 | return 0; |
| 311 | } |
| 312 | |
| 313 | static int ums_release(struct inode *ip, struct file *fp) |
| 314 | { |
| 315 | struct ums_context *ctxt = &_context; |
| 316 | |
| 317 | _unlock(&ctxt->open_excl); |
| 318 | return 0; |
| 319 | } |
| 320 | |
| 321 | static struct file_operations ums_fops = { |
| 322 | .owner = THIS_MODULE, |
| 323 | .read = ums_read, |
| 324 | .write = ums_write, |
| 325 | .open = ums_open, |
| 326 | .release = ums_release, |
| 327 | }; |
| 328 | |
| 329 | static struct miscdevice ums_device = { |
| 330 | .minor = MISC_DYNAMIC_MINOR, |
| 331 | .name = "android_ums", |
| 332 | .fops = &ums_fops, |
| 333 | }; |
| 334 | |
| 335 | static void ums_bind(struct usb_endpoint **ept, void *_ctxt) |
| 336 | { |
| 337 | struct ums_context *ctxt = _ctxt; |
| 338 | struct usb_request *req; |
| 339 | int n; |
| 340 | |
| 341 | ctxt->out = ept[0]; |
| 342 | ctxt->in = ept[1]; |
| 343 | |
| 344 | DBG("ums_bind() %p, %p\n", ctxt->out, ctxt->in); |
| 345 | |
| 346 | for(n = 0; n < RX_REQ_MAX; n++) { |
| 347 | req = usb_ept_alloc_req(ctxt->out, 4096); |
| 348 | if(req == 0) goto fail; |
| 349 | req->context = ctxt; |
| 350 | req->complete = ums_complete_out; |
| 351 | req_put(ctxt, &ctxt->rx_idle, req); |
| 352 | } |
| 353 | |
| 354 | for(n = 0; n < TX_REQ_MAX; n++) { |
| 355 | req = usb_ept_alloc_req(ctxt->in, 4096); |
| 356 | if(req == 0) goto fail; |
| 357 | req->context = ctxt; |
| 358 | req->complete = ums_complete_in; |
| 359 | req_put(ctxt, &ctxt->tx_idle, req); |
| 360 | } |
| 361 | |
| 362 | printk("ums_bind() allocated %d rx and %d tx requests\n", |
| 363 | RX_REQ_MAX, TX_REQ_MAX); |
| 364 | |
| 365 | misc_register(&ums_device); |
| 366 | return; |
| 367 | |
| 368 | fail: |
| 369 | printk("ums_bind() could not allocate requests\n"); |
| 370 | |
| 371 | /* XXX release any we did allocate */ |
| 372 | } |
| 373 | |
| 374 | static int ums_setup(struct usb_ctrlrequest* req, void* buf, int len, void *_ctxt) |
| 375 | { |
| 376 | if ((req->bRequestType & USB_TYPE_MASK) == USB_TYPE_CLASS) { |
| 377 | if (req->bRequest == USB_BULK_GET_MAX_LUN_REQUEST) { |
| 378 | if ((req->bRequestType & USB_DIR_IN) != USB_DIR_IN |
| 379 | || req->wValue != 0 || req->wIndex != 0) |
| 380 | return -1; |
| 381 | |
| 382 | ((u8*)buf)[0] = lun_count - 1; |
| 383 | printk("USB_BULK_GET_MAX_LUN_REQUEST returning %d\n", lun_count - 1); |
| 384 | return 1; |
| 385 | } else if (req->bRequest == USB_BULK_RESET_REQUEST) { |
| 386 | if ((req->bRequestType & USB_DIR_OUT) != USB_DIR_IN |
| 387 | || req->wValue != 0 || req->wIndex != 0) |
| 388 | return -1; |
| 389 | |
| 390 | /* FIXME - I'm not sure what to do here */ |
| 391 | printk("USB_BULK_RESET_REQUEST\n"); |
| 392 | return 0; |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | return -1; |
| 397 | } |
| 398 | |
| 399 | static void ums_configure(int configured, void *_ctxt) |
| 400 | { |
| 401 | struct ums_context *ctxt = _ctxt; |
| 402 | struct usb_request *req; |
| 403 | |
| 404 | DBG("ums_configure() %d\n", configured); |
| 405 | |
| 406 | if(configured) { |
| 407 | ctxt->online = 1; |
| 408 | |
| 409 | /* if we have a stale request being read, recycle it */ |
| 410 | ctxt->read_buf = 0; |
| 411 | if(ctxt->read_req) { |
| 412 | req_put(ctxt, &ctxt->rx_idle, ctxt->read_req); |
| 413 | ctxt->read_req = 0; |
| 414 | } |
| 415 | |
| 416 | /* retire any completed rx requests from previous session */ |
| 417 | while((req = req_get(ctxt, &ctxt->rx_done))) { |
| 418 | req_put(ctxt, &ctxt->rx_idle, req); |
| 419 | } |
| 420 | |
| 421 | } else { |
| 422 | ctxt->online = 0; |
| 423 | ctxt->error = 1; |
| 424 | } |
| 425 | |
| 426 | /* readers may be blocked waiting for us to go online */ |
| 427 | wake_up(&ctxt->read_wq); |
| 428 | } |
| 429 | |
| 430 | static struct usb_function usb_func_ums = { |
| 431 | .bind = ums_bind, |
| 432 | .configure = ums_configure, |
| 433 | .setup = ums_setup, |
| 434 | |
| 435 | .name = "ums", |
| 436 | .context = &_context, |
| 437 | |
| 438 | .ifc_class = USB_CLASS_MASS_STORAGE, |
| 439 | .ifc_subclass = US_SC_SCSI, |
| 440 | .ifc_protocol = US_PR_BULK, |
| 441 | |
| 442 | .ifc_name = "ums", |
| 443 | |
| 444 | .ifc_ept_count = 2, |
| 445 | .ifc_ept_type = { EPT_BULK_OUT, EPT_BULK_IN }, |
| 446 | }; |
| 447 | |
| 448 | static int __init ums_init(void) |
| 449 | { |
| 450 | struct ums_context *ctxt = &_context; |
| 451 | DBG("ums_init()\n"); |
| 452 | |
| 453 | spin_lock_init(&ctxt->lock); |
| 454 | |
| 455 | init_waitqueue_head(&ctxt->read_wq); |
| 456 | init_waitqueue_head(&ctxt->write_wq); |
| 457 | |
| 458 | atomic_set(&ctxt->open_excl, 0); |
| 459 | atomic_set(&ctxt->read_excl, 0); |
| 460 | atomic_set(&ctxt->write_excl, 0); |
| 461 | |
| 462 | INIT_LIST_HEAD(&ctxt->rx_idle); |
| 463 | INIT_LIST_HEAD(&ctxt->rx_done); |
| 464 | INIT_LIST_HEAD(&ctxt->tx_idle); |
| 465 | |
| 466 | return usb_function_register(&usb_func_ums); |
| 467 | } |
| 468 | |
| 469 | module_init(ums_init); |