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