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