Benoit Goby | cf3fc06 | 2011-12-19 14:39:37 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Gadget Function Driver for Android USB accessories |
| 3 | * |
| 4 | * Copyright (C) 2011 Google, Inc. |
| 5 | * Author: Mike Lockwood <lockwood@android.com> |
| 6 | * |
| 7 | * This software is licensed under the terms of the GNU General Public |
| 8 | * License version 2, as published by the Free Software Foundation, and |
| 9 | * may be copied, distributed, and modified under those terms. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | */ |
| 17 | |
| 18 | /* #define DEBUG */ |
| 19 | /* #define VERBOSE_DEBUG */ |
| 20 | |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/init.h> |
| 23 | #include <linux/poll.h> |
| 24 | #include <linux/delay.h> |
| 25 | #include <linux/wait.h> |
| 26 | #include <linux/err.h> |
| 27 | #include <linux/interrupt.h> |
| 28 | #include <linux/kthread.h> |
| 29 | #include <linux/freezer.h> |
| 30 | |
| 31 | #include <linux/types.h> |
| 32 | #include <linux/file.h> |
| 33 | #include <linux/device.h> |
| 34 | #include <linux/miscdevice.h> |
| 35 | |
| 36 | #include <linux/usb.h> |
| 37 | #include <linux/usb/ch9.h> |
| 38 | #include <linux/usb/f_accessory.h> |
| 39 | |
| 40 | #define BULK_BUFFER_SIZE 16384 |
| 41 | #define ACC_STRING_SIZE 256 |
| 42 | |
Mike Lockwood | cf17a84 | 2012-05-11 09:00:40 -0700 | [diff] [blame^] | 43 | #define PROTOCOL_VERSION 2 |
Benoit Goby | cf3fc06 | 2011-12-19 14:39:37 -0800 | [diff] [blame] | 44 | |
| 45 | /* String IDs */ |
| 46 | #define INTERFACE_STRING_INDEX 0 |
| 47 | |
| 48 | /* number of tx and rx requests to allocate */ |
| 49 | #define TX_REQ_MAX 4 |
| 50 | #define RX_REQ_MAX 2 |
| 51 | |
| 52 | struct acc_dev { |
| 53 | struct usb_function function; |
| 54 | struct usb_composite_dev *cdev; |
| 55 | spinlock_t lock; |
| 56 | |
| 57 | struct usb_ep *ep_in; |
| 58 | struct usb_ep *ep_out; |
| 59 | |
| 60 | /* set to 1 when we connect */ |
| 61 | int online:1; |
| 62 | /* Set to 1 when we disconnect. |
| 63 | * Not cleared until our file is closed. |
| 64 | */ |
| 65 | int disconnected:1; |
| 66 | |
| 67 | /* strings sent by the host */ |
| 68 | char manufacturer[ACC_STRING_SIZE]; |
| 69 | char model[ACC_STRING_SIZE]; |
| 70 | char description[ACC_STRING_SIZE]; |
| 71 | char version[ACC_STRING_SIZE]; |
| 72 | char uri[ACC_STRING_SIZE]; |
| 73 | char serial[ACC_STRING_SIZE]; |
| 74 | |
| 75 | /* for acc_complete_set_string */ |
| 76 | int string_index; |
| 77 | |
| 78 | /* set to 1 if we have a pending start request */ |
| 79 | int start_requested; |
| 80 | |
Mike Lockwood | cf17a84 | 2012-05-11 09:00:40 -0700 | [diff] [blame^] | 81 | int audio_mode; |
| 82 | |
Benoit Goby | cf3fc06 | 2011-12-19 14:39:37 -0800 | [diff] [blame] | 83 | /* synchronize access to our device file */ |
| 84 | atomic_t open_excl; |
| 85 | |
| 86 | struct list_head tx_idle; |
| 87 | |
| 88 | wait_queue_head_t read_wq; |
| 89 | wait_queue_head_t write_wq; |
| 90 | struct usb_request *rx_req[RX_REQ_MAX]; |
| 91 | int rx_done; |
| 92 | struct delayed_work work; |
| 93 | }; |
| 94 | |
| 95 | static struct usb_interface_descriptor acc_interface_desc = { |
| 96 | .bLength = USB_DT_INTERFACE_SIZE, |
| 97 | .bDescriptorType = USB_DT_INTERFACE, |
| 98 | .bInterfaceNumber = 0, |
| 99 | .bNumEndpoints = 2, |
| 100 | .bInterfaceClass = USB_CLASS_VENDOR_SPEC, |
| 101 | .bInterfaceSubClass = USB_SUBCLASS_VENDOR_SPEC, |
| 102 | .bInterfaceProtocol = 0, |
| 103 | }; |
| 104 | |
| 105 | static struct usb_endpoint_descriptor acc_highspeed_in_desc = { |
| 106 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 107 | .bDescriptorType = USB_DT_ENDPOINT, |
| 108 | .bEndpointAddress = USB_DIR_IN, |
| 109 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 110 | .wMaxPacketSize = __constant_cpu_to_le16(512), |
| 111 | }; |
| 112 | |
| 113 | static struct usb_endpoint_descriptor acc_highspeed_out_desc = { |
| 114 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 115 | .bDescriptorType = USB_DT_ENDPOINT, |
| 116 | .bEndpointAddress = USB_DIR_OUT, |
| 117 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 118 | .wMaxPacketSize = __constant_cpu_to_le16(512), |
| 119 | }; |
| 120 | |
| 121 | static struct usb_endpoint_descriptor acc_fullspeed_in_desc = { |
| 122 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 123 | .bDescriptorType = USB_DT_ENDPOINT, |
| 124 | .bEndpointAddress = USB_DIR_IN, |
| 125 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 126 | }; |
| 127 | |
| 128 | static struct usb_endpoint_descriptor acc_fullspeed_out_desc = { |
| 129 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 130 | .bDescriptorType = USB_DT_ENDPOINT, |
| 131 | .bEndpointAddress = USB_DIR_OUT, |
| 132 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 133 | }; |
| 134 | |
| 135 | static struct usb_descriptor_header *fs_acc_descs[] = { |
| 136 | (struct usb_descriptor_header *) &acc_interface_desc, |
| 137 | (struct usb_descriptor_header *) &acc_fullspeed_in_desc, |
| 138 | (struct usb_descriptor_header *) &acc_fullspeed_out_desc, |
| 139 | NULL, |
| 140 | }; |
| 141 | |
| 142 | static struct usb_descriptor_header *hs_acc_descs[] = { |
| 143 | (struct usb_descriptor_header *) &acc_interface_desc, |
| 144 | (struct usb_descriptor_header *) &acc_highspeed_in_desc, |
| 145 | (struct usb_descriptor_header *) &acc_highspeed_out_desc, |
| 146 | NULL, |
| 147 | }; |
| 148 | |
| 149 | static struct usb_string acc_string_defs[] = { |
| 150 | [INTERFACE_STRING_INDEX].s = "Android Accessory Interface", |
| 151 | { }, /* end of list */ |
| 152 | }; |
| 153 | |
| 154 | static struct usb_gadget_strings acc_string_table = { |
| 155 | .language = 0x0409, /* en-US */ |
| 156 | .strings = acc_string_defs, |
| 157 | }; |
| 158 | |
| 159 | static struct usb_gadget_strings *acc_strings[] = { |
| 160 | &acc_string_table, |
| 161 | NULL, |
| 162 | }; |
| 163 | |
| 164 | /* temporary variable used between acc_open() and acc_gadget_bind() */ |
| 165 | static struct acc_dev *_acc_dev; |
| 166 | |
| 167 | static inline struct acc_dev *func_to_dev(struct usb_function *f) |
| 168 | { |
| 169 | return container_of(f, struct acc_dev, function); |
| 170 | } |
| 171 | |
| 172 | static struct usb_request *acc_request_new(struct usb_ep *ep, int buffer_size) |
| 173 | { |
| 174 | struct usb_request *req = usb_ep_alloc_request(ep, GFP_KERNEL); |
| 175 | if (!req) |
| 176 | return NULL; |
| 177 | |
| 178 | /* now allocate buffers for the requests */ |
| 179 | req->buf = kmalloc(buffer_size, GFP_KERNEL); |
| 180 | if (!req->buf) { |
| 181 | usb_ep_free_request(ep, req); |
| 182 | return NULL; |
| 183 | } |
| 184 | |
| 185 | return req; |
| 186 | } |
| 187 | |
| 188 | static void acc_request_free(struct usb_request *req, struct usb_ep *ep) |
| 189 | { |
| 190 | if (req) { |
| 191 | kfree(req->buf); |
| 192 | usb_ep_free_request(ep, req); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | /* add a request to the tail of a list */ |
| 197 | static void req_put(struct acc_dev *dev, struct list_head *head, |
| 198 | struct usb_request *req) |
| 199 | { |
| 200 | unsigned long flags; |
| 201 | |
| 202 | spin_lock_irqsave(&dev->lock, flags); |
| 203 | list_add_tail(&req->list, head); |
| 204 | spin_unlock_irqrestore(&dev->lock, flags); |
| 205 | } |
| 206 | |
| 207 | /* remove a request from the head of a list */ |
| 208 | static struct usb_request *req_get(struct acc_dev *dev, struct list_head *head) |
| 209 | { |
| 210 | unsigned long flags; |
| 211 | struct usb_request *req; |
| 212 | |
| 213 | spin_lock_irqsave(&dev->lock, flags); |
| 214 | if (list_empty(head)) { |
| 215 | req = 0; |
| 216 | } else { |
| 217 | req = list_first_entry(head, struct usb_request, list); |
| 218 | list_del(&req->list); |
| 219 | } |
| 220 | spin_unlock_irqrestore(&dev->lock, flags); |
| 221 | return req; |
| 222 | } |
| 223 | |
| 224 | static void acc_set_disconnected(struct acc_dev *dev) |
| 225 | { |
| 226 | dev->online = 0; |
| 227 | dev->disconnected = 1; |
| 228 | } |
| 229 | |
| 230 | static void acc_complete_in(struct usb_ep *ep, struct usb_request *req) |
| 231 | { |
| 232 | struct acc_dev *dev = _acc_dev; |
| 233 | |
| 234 | if (req->status != 0) |
| 235 | acc_set_disconnected(dev); |
| 236 | |
| 237 | req_put(dev, &dev->tx_idle, req); |
| 238 | |
| 239 | wake_up(&dev->write_wq); |
| 240 | } |
| 241 | |
| 242 | static void acc_complete_out(struct usb_ep *ep, struct usb_request *req) |
| 243 | { |
| 244 | struct acc_dev *dev = _acc_dev; |
| 245 | |
| 246 | dev->rx_done = 1; |
| 247 | if (req->status != 0) |
| 248 | acc_set_disconnected(dev); |
| 249 | |
| 250 | wake_up(&dev->read_wq); |
| 251 | } |
| 252 | |
| 253 | static void acc_complete_set_string(struct usb_ep *ep, struct usb_request *req) |
| 254 | { |
| 255 | struct acc_dev *dev = ep->driver_data; |
| 256 | char *string_dest = NULL; |
| 257 | int length = req->actual; |
| 258 | |
| 259 | if (req->status != 0) { |
| 260 | pr_err("acc_complete_set_string, err %d\n", req->status); |
| 261 | return; |
| 262 | } |
| 263 | |
| 264 | switch (dev->string_index) { |
| 265 | case ACCESSORY_STRING_MANUFACTURER: |
| 266 | string_dest = dev->manufacturer; |
| 267 | break; |
| 268 | case ACCESSORY_STRING_MODEL: |
| 269 | string_dest = dev->model; |
| 270 | break; |
| 271 | case ACCESSORY_STRING_DESCRIPTION: |
| 272 | string_dest = dev->description; |
| 273 | break; |
| 274 | case ACCESSORY_STRING_VERSION: |
| 275 | string_dest = dev->version; |
| 276 | break; |
| 277 | case ACCESSORY_STRING_URI: |
| 278 | string_dest = dev->uri; |
| 279 | break; |
| 280 | case ACCESSORY_STRING_SERIAL: |
| 281 | string_dest = dev->serial; |
| 282 | break; |
| 283 | } |
| 284 | if (string_dest) { |
| 285 | unsigned long flags; |
| 286 | |
| 287 | if (length >= ACC_STRING_SIZE) |
| 288 | length = ACC_STRING_SIZE - 1; |
| 289 | |
| 290 | spin_lock_irqsave(&dev->lock, flags); |
| 291 | memcpy(string_dest, req->buf, length); |
| 292 | /* ensure zero termination */ |
| 293 | string_dest[length] = 0; |
| 294 | spin_unlock_irqrestore(&dev->lock, flags); |
| 295 | } else { |
| 296 | pr_err("unknown accessory string index %d\n", |
| 297 | dev->string_index); |
| 298 | } |
| 299 | } |
| 300 | |
Stephen Boyd | 7853128 | 2012-04-25 11:55:15 -0700 | [diff] [blame] | 301 | static int create_bulk_endpoints(struct acc_dev *dev, |
Benoit Goby | cf3fc06 | 2011-12-19 14:39:37 -0800 | [diff] [blame] | 302 | struct usb_endpoint_descriptor *in_desc, |
| 303 | struct usb_endpoint_descriptor *out_desc) |
| 304 | { |
| 305 | struct usb_composite_dev *cdev = dev->cdev; |
| 306 | struct usb_request *req; |
| 307 | struct usb_ep *ep; |
| 308 | int i; |
| 309 | |
| 310 | DBG(cdev, "create_bulk_endpoints dev: %p\n", dev); |
| 311 | |
| 312 | ep = usb_ep_autoconfig(cdev->gadget, in_desc); |
| 313 | if (!ep) { |
| 314 | DBG(cdev, "usb_ep_autoconfig for ep_in failed\n"); |
| 315 | return -ENODEV; |
| 316 | } |
| 317 | DBG(cdev, "usb_ep_autoconfig for ep_in got %s\n", ep->name); |
| 318 | ep->driver_data = dev; /* claim the endpoint */ |
| 319 | dev->ep_in = ep; |
| 320 | |
| 321 | ep = usb_ep_autoconfig(cdev->gadget, out_desc); |
| 322 | if (!ep) { |
| 323 | DBG(cdev, "usb_ep_autoconfig for ep_out failed\n"); |
| 324 | return -ENODEV; |
| 325 | } |
| 326 | DBG(cdev, "usb_ep_autoconfig for ep_out got %s\n", ep->name); |
| 327 | ep->driver_data = dev; /* claim the endpoint */ |
| 328 | dev->ep_out = ep; |
| 329 | |
| 330 | ep = usb_ep_autoconfig(cdev->gadget, out_desc); |
| 331 | if (!ep) { |
| 332 | DBG(cdev, "usb_ep_autoconfig for ep_out failed\n"); |
| 333 | return -ENODEV; |
| 334 | } |
| 335 | DBG(cdev, "usb_ep_autoconfig for ep_out got %s\n", ep->name); |
| 336 | ep->driver_data = dev; /* claim the endpoint */ |
| 337 | dev->ep_out = ep; |
| 338 | |
| 339 | /* now allocate requests for our endpoints */ |
| 340 | for (i = 0; i < TX_REQ_MAX; i++) { |
| 341 | req = acc_request_new(dev->ep_in, BULK_BUFFER_SIZE); |
| 342 | if (!req) |
| 343 | goto fail; |
| 344 | req->complete = acc_complete_in; |
| 345 | req_put(dev, &dev->tx_idle, req); |
| 346 | } |
| 347 | for (i = 0; i < RX_REQ_MAX; i++) { |
| 348 | req = acc_request_new(dev->ep_out, BULK_BUFFER_SIZE); |
| 349 | if (!req) |
| 350 | goto fail; |
| 351 | req->complete = acc_complete_out; |
| 352 | dev->rx_req[i] = req; |
| 353 | } |
| 354 | |
| 355 | return 0; |
| 356 | |
| 357 | fail: |
| 358 | printk(KERN_ERR "acc_bind() could not allocate requests\n"); |
| 359 | while ((req = req_get(dev, &dev->tx_idle))) |
| 360 | acc_request_free(req, dev->ep_in); |
| 361 | for (i = 0; i < RX_REQ_MAX; i++) |
| 362 | acc_request_free(dev->rx_req[i], dev->ep_out); |
| 363 | return -1; |
| 364 | } |
| 365 | |
| 366 | static ssize_t acc_read(struct file *fp, char __user *buf, |
| 367 | size_t count, loff_t *pos) |
| 368 | { |
| 369 | struct acc_dev *dev = fp->private_data; |
| 370 | struct usb_request *req; |
| 371 | int r = count, xfer; |
| 372 | int ret = 0; |
| 373 | |
| 374 | pr_debug("acc_read(%d)\n", count); |
| 375 | |
| 376 | if (dev->disconnected) |
| 377 | return -ENODEV; |
| 378 | |
| 379 | if (count > BULK_BUFFER_SIZE) |
| 380 | count = BULK_BUFFER_SIZE; |
| 381 | |
| 382 | /* we will block until we're online */ |
| 383 | pr_debug("acc_read: waiting for online\n"); |
| 384 | ret = wait_event_interruptible(dev->read_wq, dev->online); |
| 385 | if (ret < 0) { |
| 386 | r = ret; |
| 387 | goto done; |
| 388 | } |
| 389 | |
| 390 | requeue_req: |
| 391 | /* queue a request */ |
| 392 | req = dev->rx_req[0]; |
| 393 | req->length = count; |
| 394 | dev->rx_done = 0; |
| 395 | ret = usb_ep_queue(dev->ep_out, req, GFP_KERNEL); |
| 396 | if (ret < 0) { |
| 397 | r = -EIO; |
| 398 | goto done; |
| 399 | } else { |
| 400 | pr_debug("rx %p queue\n", req); |
| 401 | } |
| 402 | |
| 403 | /* wait for a request to complete */ |
| 404 | ret = wait_event_interruptible(dev->read_wq, dev->rx_done); |
| 405 | if (ret < 0) { |
| 406 | r = ret; |
| 407 | usb_ep_dequeue(dev->ep_out, req); |
| 408 | goto done; |
| 409 | } |
| 410 | if (dev->online) { |
| 411 | /* If we got a 0-len packet, throw it back and try again. */ |
| 412 | if (req->actual == 0) |
| 413 | goto requeue_req; |
| 414 | |
| 415 | pr_debug("rx %p %d\n", req, req->actual); |
| 416 | xfer = (req->actual < count) ? req->actual : count; |
| 417 | r = xfer; |
| 418 | if (copy_to_user(buf, req->buf, xfer)) |
| 419 | r = -EFAULT; |
| 420 | } else |
| 421 | r = -EIO; |
| 422 | |
| 423 | done: |
| 424 | pr_debug("acc_read returning %d\n", r); |
| 425 | return r; |
| 426 | } |
| 427 | |
| 428 | static ssize_t acc_write(struct file *fp, const char __user *buf, |
| 429 | size_t count, loff_t *pos) |
| 430 | { |
| 431 | struct acc_dev *dev = fp->private_data; |
| 432 | struct usb_request *req = 0; |
| 433 | int r = count, xfer; |
| 434 | int ret; |
| 435 | |
| 436 | pr_debug("acc_write(%d)\n", count); |
| 437 | |
| 438 | if (!dev->online || dev->disconnected) |
| 439 | return -ENODEV; |
| 440 | |
| 441 | while (count > 0) { |
| 442 | if (!dev->online) { |
| 443 | pr_debug("acc_write dev->error\n"); |
| 444 | r = -EIO; |
| 445 | break; |
| 446 | } |
| 447 | |
| 448 | /* get an idle tx request to use */ |
| 449 | req = 0; |
| 450 | ret = wait_event_interruptible(dev->write_wq, |
| 451 | ((req = req_get(dev, &dev->tx_idle)) || !dev->online)); |
| 452 | if (!req) { |
| 453 | r = ret; |
| 454 | break; |
| 455 | } |
| 456 | |
| 457 | if (count > BULK_BUFFER_SIZE) |
| 458 | xfer = BULK_BUFFER_SIZE; |
| 459 | else |
| 460 | xfer = count; |
| 461 | if (copy_from_user(req->buf, buf, xfer)) { |
| 462 | r = -EFAULT; |
| 463 | break; |
| 464 | } |
| 465 | |
| 466 | req->length = xfer; |
| 467 | ret = usb_ep_queue(dev->ep_in, req, GFP_KERNEL); |
| 468 | if (ret < 0) { |
| 469 | pr_debug("acc_write: xfer error %d\n", ret); |
| 470 | r = -EIO; |
| 471 | break; |
| 472 | } |
| 473 | |
| 474 | buf += xfer; |
| 475 | count -= xfer; |
| 476 | |
| 477 | /* zero this so we don't try to free it on error exit */ |
| 478 | req = 0; |
| 479 | } |
| 480 | |
| 481 | if (req) |
| 482 | req_put(dev, &dev->tx_idle, req); |
| 483 | |
| 484 | pr_debug("acc_write returning %d\n", r); |
| 485 | return r; |
| 486 | } |
| 487 | |
| 488 | static long acc_ioctl(struct file *fp, unsigned code, unsigned long value) |
| 489 | { |
| 490 | struct acc_dev *dev = fp->private_data; |
| 491 | char *src = NULL; |
| 492 | int ret; |
| 493 | |
| 494 | switch (code) { |
| 495 | case ACCESSORY_GET_STRING_MANUFACTURER: |
| 496 | src = dev->manufacturer; |
| 497 | break; |
| 498 | case ACCESSORY_GET_STRING_MODEL: |
| 499 | src = dev->model; |
| 500 | break; |
| 501 | case ACCESSORY_GET_STRING_DESCRIPTION: |
| 502 | src = dev->description; |
| 503 | break; |
| 504 | case ACCESSORY_GET_STRING_VERSION: |
| 505 | src = dev->version; |
| 506 | break; |
| 507 | case ACCESSORY_GET_STRING_URI: |
| 508 | src = dev->uri; |
| 509 | break; |
| 510 | case ACCESSORY_GET_STRING_SERIAL: |
| 511 | src = dev->serial; |
| 512 | break; |
| 513 | case ACCESSORY_IS_START_REQUESTED: |
| 514 | return dev->start_requested; |
Mike Lockwood | cf17a84 | 2012-05-11 09:00:40 -0700 | [diff] [blame^] | 515 | case ACCESSORY_GET_AUDIO_MODE: |
| 516 | return dev->audio_mode; |
Benoit Goby | cf3fc06 | 2011-12-19 14:39:37 -0800 | [diff] [blame] | 517 | } |
| 518 | if (!src) |
| 519 | return -EINVAL; |
| 520 | |
| 521 | ret = strlen(src) + 1; |
| 522 | if (copy_to_user((void __user *)value, src, ret)) |
| 523 | ret = -EFAULT; |
| 524 | return ret; |
| 525 | } |
| 526 | |
| 527 | static int acc_open(struct inode *ip, struct file *fp) |
| 528 | { |
| 529 | printk(KERN_INFO "acc_open\n"); |
| 530 | if (atomic_xchg(&_acc_dev->open_excl, 1)) |
| 531 | return -EBUSY; |
| 532 | |
| 533 | _acc_dev->disconnected = 0; |
| 534 | fp->private_data = _acc_dev; |
| 535 | return 0; |
| 536 | } |
| 537 | |
| 538 | static int acc_release(struct inode *ip, struct file *fp) |
| 539 | { |
| 540 | printk(KERN_INFO "acc_release\n"); |
| 541 | |
| 542 | WARN_ON(!atomic_xchg(&_acc_dev->open_excl, 0)); |
| 543 | _acc_dev->disconnected = 0; |
| 544 | return 0; |
| 545 | } |
| 546 | |
| 547 | /* file operations for /dev/acc_usb */ |
| 548 | static const struct file_operations acc_fops = { |
| 549 | .owner = THIS_MODULE, |
| 550 | .read = acc_read, |
| 551 | .write = acc_write, |
| 552 | .unlocked_ioctl = acc_ioctl, |
| 553 | .open = acc_open, |
| 554 | .release = acc_release, |
| 555 | }; |
| 556 | |
| 557 | static struct miscdevice acc_device = { |
| 558 | .minor = MISC_DYNAMIC_MINOR, |
| 559 | .name = "usb_accessory", |
| 560 | .fops = &acc_fops, |
| 561 | }; |
| 562 | |
| 563 | |
| 564 | static int acc_ctrlrequest(struct usb_composite_dev *cdev, |
| 565 | const struct usb_ctrlrequest *ctrl) |
| 566 | { |
| 567 | struct acc_dev *dev = _acc_dev; |
| 568 | int value = -EOPNOTSUPP; |
| 569 | u8 b_requestType = ctrl->bRequestType; |
| 570 | u8 b_request = ctrl->bRequest; |
| 571 | u16 w_index = le16_to_cpu(ctrl->wIndex); |
| 572 | u16 w_value = le16_to_cpu(ctrl->wValue); |
| 573 | u16 w_length = le16_to_cpu(ctrl->wLength); |
| 574 | |
| 575 | /* |
| 576 | printk(KERN_INFO "acc_ctrlrequest " |
| 577 | "%02x.%02x v%04x i%04x l%u\n", |
| 578 | b_requestType, b_request, |
| 579 | w_value, w_index, w_length); |
| 580 | */ |
| 581 | |
| 582 | if (b_requestType == (USB_DIR_OUT | USB_TYPE_VENDOR)) { |
| 583 | if (b_request == ACCESSORY_START) { |
| 584 | dev->start_requested = 1; |
| 585 | schedule_delayed_work( |
| 586 | &dev->work, msecs_to_jiffies(10)); |
| 587 | value = 0; |
| 588 | } else if (b_request == ACCESSORY_SEND_STRING) { |
| 589 | dev->string_index = w_index; |
| 590 | cdev->gadget->ep0->driver_data = dev; |
| 591 | cdev->req->complete = acc_complete_set_string; |
| 592 | value = w_length; |
Mike Lockwood | cf17a84 | 2012-05-11 09:00:40 -0700 | [diff] [blame^] | 593 | } else if (b_request == ACCESSORY_SET_AUDIO_MODE && |
| 594 | w_index == 0 && w_length == 0) { |
| 595 | dev->audio_mode = w_value; |
| 596 | value = 0; |
Benoit Goby | cf3fc06 | 2011-12-19 14:39:37 -0800 | [diff] [blame] | 597 | } |
| 598 | } else if (b_requestType == (USB_DIR_IN | USB_TYPE_VENDOR)) { |
| 599 | if (b_request == ACCESSORY_GET_PROTOCOL) { |
| 600 | *((u16 *)cdev->req->buf) = PROTOCOL_VERSION; |
| 601 | value = sizeof(u16); |
| 602 | |
| 603 | /* clear any string left over from a previous session */ |
| 604 | memset(dev->manufacturer, 0, sizeof(dev->manufacturer)); |
| 605 | memset(dev->model, 0, sizeof(dev->model)); |
| 606 | memset(dev->description, 0, sizeof(dev->description)); |
| 607 | memset(dev->version, 0, sizeof(dev->version)); |
| 608 | memset(dev->uri, 0, sizeof(dev->uri)); |
| 609 | memset(dev->serial, 0, sizeof(dev->serial)); |
| 610 | dev->start_requested = 0; |
Mike Lockwood | cf17a84 | 2012-05-11 09:00:40 -0700 | [diff] [blame^] | 611 | dev->audio_mode = 0; |
Benoit Goby | cf3fc06 | 2011-12-19 14:39:37 -0800 | [diff] [blame] | 612 | } |
| 613 | } |
| 614 | |
| 615 | if (value >= 0) { |
| 616 | cdev->req->zero = 0; |
| 617 | cdev->req->length = value; |
| 618 | value = usb_ep_queue(cdev->gadget->ep0, cdev->req, GFP_ATOMIC); |
| 619 | if (value < 0) |
| 620 | ERROR(cdev, "%s setup response queue error\n", |
| 621 | __func__); |
| 622 | } |
| 623 | |
| 624 | if (value == -EOPNOTSUPP) |
| 625 | VDBG(cdev, |
| 626 | "unknown class-specific control req " |
| 627 | "%02x.%02x v%04x i%04x l%u\n", |
| 628 | ctrl->bRequestType, ctrl->bRequest, |
| 629 | w_value, w_index, w_length); |
| 630 | return value; |
| 631 | } |
| 632 | |
| 633 | static int |
| 634 | acc_function_bind(struct usb_configuration *c, struct usb_function *f) |
| 635 | { |
| 636 | struct usb_composite_dev *cdev = c->cdev; |
| 637 | struct acc_dev *dev = func_to_dev(f); |
| 638 | int id; |
| 639 | int ret; |
| 640 | |
| 641 | DBG(cdev, "acc_function_bind dev: %p\n", dev); |
| 642 | |
| 643 | dev->start_requested = 0; |
| 644 | |
| 645 | /* allocate interface ID(s) */ |
| 646 | id = usb_interface_id(c, f); |
| 647 | if (id < 0) |
| 648 | return id; |
| 649 | acc_interface_desc.bInterfaceNumber = id; |
| 650 | |
| 651 | /* allocate endpoints */ |
| 652 | ret = create_bulk_endpoints(dev, &acc_fullspeed_in_desc, |
| 653 | &acc_fullspeed_out_desc); |
| 654 | if (ret) |
| 655 | return ret; |
| 656 | |
| 657 | /* support high speed hardware */ |
| 658 | if (gadget_is_dualspeed(c->cdev->gadget)) { |
| 659 | acc_highspeed_in_desc.bEndpointAddress = |
| 660 | acc_fullspeed_in_desc.bEndpointAddress; |
| 661 | acc_highspeed_out_desc.bEndpointAddress = |
| 662 | acc_fullspeed_out_desc.bEndpointAddress; |
| 663 | } |
| 664 | |
| 665 | DBG(cdev, "%s speed %s: IN/%s, OUT/%s\n", |
| 666 | gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full", |
| 667 | f->name, dev->ep_in->name, dev->ep_out->name); |
| 668 | return 0; |
| 669 | } |
| 670 | |
| 671 | static void |
| 672 | acc_function_unbind(struct usb_configuration *c, struct usb_function *f) |
| 673 | { |
| 674 | struct acc_dev *dev = func_to_dev(f); |
| 675 | struct usb_request *req; |
| 676 | int i; |
| 677 | |
| 678 | while ((req = req_get(dev, &dev->tx_idle))) |
| 679 | acc_request_free(req, dev->ep_in); |
| 680 | for (i = 0; i < RX_REQ_MAX; i++) |
| 681 | acc_request_free(dev->rx_req[i], dev->ep_out); |
| 682 | } |
| 683 | |
| 684 | static void acc_work(struct work_struct *data) |
| 685 | { |
| 686 | char *envp[2] = { "ACCESSORY=START", NULL }; |
| 687 | kobject_uevent_env(&acc_device.this_device->kobj, KOBJ_CHANGE, envp); |
| 688 | } |
| 689 | |
| 690 | static int acc_function_set_alt(struct usb_function *f, |
| 691 | unsigned intf, unsigned alt) |
| 692 | { |
| 693 | struct acc_dev *dev = func_to_dev(f); |
| 694 | struct usb_composite_dev *cdev = f->config->cdev; |
| 695 | int ret; |
| 696 | |
| 697 | DBG(cdev, "acc_function_set_alt intf: %d alt: %d\n", intf, alt); |
| 698 | |
| 699 | ret = config_ep_by_speed(cdev->gadget, f, dev->ep_in); |
Tatyana Brokhman | ebd3f39 | 2011-06-28 16:33:50 +0300 | [diff] [blame] | 700 | if (ret) { |
Tatyana Brokhman | 31ac352 | 2011-06-28 15:33:50 +0200 | [diff] [blame] | 701 | dev->ep_in->desc = NULL; |
| 702 | ERROR(cdev, "config_ep_by_speed failes for ep %s, result %d\n", |
| 703 | dev->ep_in->name, ret); |
| 704 | return ret; |
| 705 | } |
Benoit Goby | cf3fc06 | 2011-12-19 14:39:37 -0800 | [diff] [blame] | 706 | ret = usb_ep_enable(dev->ep_in); |
Tatyana Brokhman | 31ac352 | 2011-06-28 15:33:50 +0200 | [diff] [blame] | 707 | if (ret) { |
| 708 | ERROR(cdev, "failed to enable ep %s, result %d\n", |
| 709 | dev->ep_in->name, ret); |
Benoit Goby | cf3fc06 | 2011-12-19 14:39:37 -0800 | [diff] [blame] | 710 | return ret; |
Tatyana Brokhman | 31ac352 | 2011-06-28 15:33:50 +0200 | [diff] [blame] | 711 | } |
Benoit Goby | cf3fc06 | 2011-12-19 14:39:37 -0800 | [diff] [blame] | 712 | |
| 713 | ret = config_ep_by_speed(cdev->gadget, f, dev->ep_out); |
Tatyana Brokhman | 31ac352 | 2011-06-28 15:33:50 +0200 | [diff] [blame] | 714 | if (ret) { |
| 715 | dev->ep_out->desc = NULL; |
| 716 | ERROR(cdev, "config_ep_by_speed failes for ep %s, result %d\n", |
| 717 | dev->ep_out->name, ret); |
| 718 | usb_ep_disable(dev->ep_in); |
Benoit Goby | cf3fc06 | 2011-12-19 14:39:37 -0800 | [diff] [blame] | 719 | return ret; |
Tatyana Brokhman | 31ac352 | 2011-06-28 15:33:50 +0200 | [diff] [blame] | 720 | } |
Benoit Goby | cf3fc06 | 2011-12-19 14:39:37 -0800 | [diff] [blame] | 721 | ret = usb_ep_enable(dev->ep_out); |
| 722 | if (ret) { |
Tatyana Brokhman | 31ac352 | 2011-06-28 15:33:50 +0200 | [diff] [blame] | 723 | ERROR(cdev, "failed to enable ep %s, result %d\n", |
| 724 | dev->ep_out->name, ret); |
Benoit Goby | cf3fc06 | 2011-12-19 14:39:37 -0800 | [diff] [blame] | 725 | usb_ep_disable(dev->ep_in); |
| 726 | return ret; |
| 727 | } |
| 728 | |
| 729 | dev->online = 1; |
| 730 | |
| 731 | /* readers may be blocked waiting for us to go online */ |
| 732 | wake_up(&dev->read_wq); |
| 733 | return 0; |
| 734 | } |
| 735 | |
| 736 | static void acc_function_disable(struct usb_function *f) |
| 737 | { |
| 738 | struct acc_dev *dev = func_to_dev(f); |
| 739 | struct usb_composite_dev *cdev = dev->cdev; |
| 740 | |
| 741 | DBG(cdev, "acc_function_disable\n"); |
| 742 | acc_set_disconnected(dev); |
| 743 | usb_ep_disable(dev->ep_in); |
| 744 | usb_ep_disable(dev->ep_out); |
| 745 | |
| 746 | /* readers may be blocked waiting for us to go online */ |
| 747 | wake_up(&dev->read_wq); |
| 748 | |
| 749 | VDBG(cdev, "%s disabled\n", dev->function.name); |
| 750 | } |
| 751 | |
| 752 | static int acc_bind_config(struct usb_configuration *c) |
| 753 | { |
| 754 | struct acc_dev *dev = _acc_dev; |
| 755 | int ret; |
| 756 | |
| 757 | printk(KERN_INFO "acc_bind_config\n"); |
| 758 | |
| 759 | /* allocate a string ID for our interface */ |
| 760 | if (acc_string_defs[INTERFACE_STRING_INDEX].id == 0) { |
| 761 | ret = usb_string_id(c->cdev); |
| 762 | if (ret < 0) |
| 763 | return ret; |
| 764 | acc_string_defs[INTERFACE_STRING_INDEX].id = ret; |
| 765 | acc_interface_desc.iInterface = ret; |
| 766 | } |
| 767 | |
| 768 | dev->cdev = c->cdev; |
| 769 | dev->function.name = "accessory"; |
| 770 | dev->function.strings = acc_strings, |
| 771 | dev->function.descriptors = fs_acc_descs; |
| 772 | dev->function.hs_descriptors = hs_acc_descs; |
| 773 | dev->function.bind = acc_function_bind; |
| 774 | dev->function.unbind = acc_function_unbind; |
| 775 | dev->function.set_alt = acc_function_set_alt; |
| 776 | dev->function.disable = acc_function_disable; |
| 777 | |
| 778 | return usb_add_function(c, &dev->function); |
| 779 | } |
| 780 | |
| 781 | static int acc_setup(void) |
| 782 | { |
| 783 | struct acc_dev *dev; |
| 784 | int ret; |
| 785 | |
| 786 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
| 787 | if (!dev) |
| 788 | return -ENOMEM; |
| 789 | |
| 790 | spin_lock_init(&dev->lock); |
| 791 | init_waitqueue_head(&dev->read_wq); |
| 792 | init_waitqueue_head(&dev->write_wq); |
| 793 | atomic_set(&dev->open_excl, 0); |
| 794 | INIT_LIST_HEAD(&dev->tx_idle); |
| 795 | INIT_DELAYED_WORK(&dev->work, acc_work); |
| 796 | |
| 797 | /* _acc_dev must be set before calling usb_gadget_register_driver */ |
| 798 | _acc_dev = dev; |
| 799 | |
| 800 | ret = misc_register(&acc_device); |
| 801 | if (ret) |
| 802 | goto err; |
| 803 | |
| 804 | return 0; |
| 805 | |
| 806 | err: |
| 807 | kfree(dev); |
| 808 | printk(KERN_ERR "USB accessory gadget driver failed to initialize\n"); |
| 809 | return ret; |
| 810 | } |
| 811 | |
| 812 | static void acc_cleanup(void) |
| 813 | { |
| 814 | misc_deregister(&acc_device); |
| 815 | kfree(_acc_dev); |
| 816 | _acc_dev = NULL; |
| 817 | } |