blob: 07c29d9b92642e70ad90fb10d55cf8bf7e3193c9 [file] [log] [blame]
Benoit Goby3d955272011-12-19 14:39:37 -08001/*
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 Lockwoodb16cf282012-05-11 09:00:40 -070043#define PROTOCOL_VERSION 2
Benoit Goby3d955272011-12-19 14:39:37 -080044
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
52struct 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 Lockwoodb16cf282012-05-11 09:00:40 -070081 int audio_mode;
82
Benoit Goby3d955272011-12-19 14:39:37 -080083 /* 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
95static 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
105static 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
113static 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
121static 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
128static 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
135static 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
142static 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
149static struct usb_string acc_string_defs[] = {
150 [INTERFACE_STRING_INDEX].s = "Android Accessory Interface",
151 { }, /* end of list */
152};
153
154static struct usb_gadget_strings acc_string_table = {
155 .language = 0x0409, /* en-US */
156 .strings = acc_string_defs,
157};
158
159static 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() */
165static struct acc_dev *_acc_dev;
166
167static inline struct acc_dev *func_to_dev(struct usb_function *f)
168{
169 return container_of(f, struct acc_dev, function);
170}
171
172static 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
188static 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 */
197static 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 */
208static 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
224static void acc_set_disconnected(struct acc_dev *dev)
225{
226 dev->online = 0;
227 dev->disconnected = 1;
228}
229
230static 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
242static 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
253static 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
Benoit Gobyceb2f0a2012-06-22 18:17:10 -0700301static int create_bulk_endpoints(struct acc_dev *dev,
Benoit Goby3d955272011-12-19 14:39:37 -0800302 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
357fail:
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
366static 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
390requeue_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
423done:
424 pr_debug("acc_read returning %d\n", r);
425 return r;
426}
427
428static 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
488static 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 Lockwoodb16cf282012-05-11 09:00:40 -0700515 case ACCESSORY_GET_AUDIO_MODE:
516 return dev->audio_mode;
Benoit Goby3d955272011-12-19 14:39:37 -0800517 }
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
527static 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
538static 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 */
548static 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
557static struct miscdevice acc_device = {
558 .minor = MISC_DYNAMIC_MINOR,
559 .name = "usb_accessory",
560 .fops = &acc_fops,
561};
562
563
564static 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 Lockwoodb16cf282012-05-11 09:00:40 -0700593 } 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 Goby3d955272011-12-19 14:39:37 -0800597 }
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 Lockwoodb16cf282012-05-11 09:00:40 -0700611 dev->audio_mode = 0;
Benoit Goby3d955272011-12-19 14:39:37 -0800612 }
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
633static int
634acc_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
671static void
672acc_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
684static 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
690static 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);
700 if (ret)
701 return ret;
702
703 ret = usb_ep_enable(dev->ep_in);
704 if (ret)
705 return ret;
706
707 ret = config_ep_by_speed(cdev->gadget, f, dev->ep_out);
708 if (ret)
709 return ret;
710
711 ret = usb_ep_enable(dev->ep_out);
712 if (ret) {
713 usb_ep_disable(dev->ep_in);
714 return ret;
715 }
716
717 dev->online = 1;
718
719 /* readers may be blocked waiting for us to go online */
720 wake_up(&dev->read_wq);
721 return 0;
722}
723
724static void acc_function_disable(struct usb_function *f)
725{
726 struct acc_dev *dev = func_to_dev(f);
727 struct usb_composite_dev *cdev = dev->cdev;
728
729 DBG(cdev, "acc_function_disable\n");
730 acc_set_disconnected(dev);
731 usb_ep_disable(dev->ep_in);
732 usb_ep_disable(dev->ep_out);
733
734 /* readers may be blocked waiting for us to go online */
735 wake_up(&dev->read_wq);
736
737 VDBG(cdev, "%s disabled\n", dev->function.name);
738}
739
740static int acc_bind_config(struct usb_configuration *c)
741{
742 struct acc_dev *dev = _acc_dev;
743 int ret;
744
745 printk(KERN_INFO "acc_bind_config\n");
746
747 /* allocate a string ID for our interface */
748 if (acc_string_defs[INTERFACE_STRING_INDEX].id == 0) {
749 ret = usb_string_id(c->cdev);
750 if (ret < 0)
751 return ret;
752 acc_string_defs[INTERFACE_STRING_INDEX].id = ret;
753 acc_interface_desc.iInterface = ret;
754 }
755
756 dev->cdev = c->cdev;
757 dev->function.name = "accessory";
758 dev->function.strings = acc_strings,
759 dev->function.descriptors = fs_acc_descs;
760 dev->function.hs_descriptors = hs_acc_descs;
761 dev->function.bind = acc_function_bind;
762 dev->function.unbind = acc_function_unbind;
763 dev->function.set_alt = acc_function_set_alt;
764 dev->function.disable = acc_function_disable;
765
766 return usb_add_function(c, &dev->function);
767}
768
769static int acc_setup(void)
770{
771 struct acc_dev *dev;
772 int ret;
773
774 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
775 if (!dev)
776 return -ENOMEM;
777
778 spin_lock_init(&dev->lock);
779 init_waitqueue_head(&dev->read_wq);
780 init_waitqueue_head(&dev->write_wq);
781 atomic_set(&dev->open_excl, 0);
782 INIT_LIST_HEAD(&dev->tx_idle);
783 INIT_DELAYED_WORK(&dev->work, acc_work);
784
785 /* _acc_dev must be set before calling usb_gadget_register_driver */
786 _acc_dev = dev;
787
788 ret = misc_register(&acc_device);
789 if (ret)
790 goto err;
791
792 return 0;
793
794err:
795 kfree(dev);
796 printk(KERN_ERR "USB accessory gadget driver failed to initialize\n");
797 return ret;
798}
799
800static void acc_cleanup(void)
801{
802 misc_deregister(&acc_device);
803 kfree(_acc_dev);
804 _acc_dev = NULL;
805}