blob: 5659c79f04bd9a0207f8538607e313aaa2ae9906 [file] [log] [blame]
Benoit Gobycf3fc062011-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
Mike Lockwoode7558bb2012-08-27 16:23:48 +053036#include <linux/hid.h>
37#include <linux/hiddev.h>
Benoit Gobycf3fc062011-12-19 14:39:37 -080038#include <linux/usb.h>
39#include <linux/usb/ch9.h>
40#include <linux/usb/f_accessory.h>
41
42#define BULK_BUFFER_SIZE 16384
43#define ACC_STRING_SIZE 256
44
Mike Lockwoodcf17a842012-05-11 09:00:40 -070045#define PROTOCOL_VERSION 2
Benoit Gobycf3fc062011-12-19 14:39:37 -080046
47/* String IDs */
48#define INTERFACE_STRING_INDEX 0
49
50/* number of tx and rx requests to allocate */
51#define TX_REQ_MAX 4
52#define RX_REQ_MAX 2
53
Mike Lockwoode7558bb2012-08-27 16:23:48 +053054struct acc_hid_dev {
55 struct list_head list;
56 struct hid_device *hid;
57 struct acc_dev *dev;
58 /* accessory defined ID */
59 int id;
60 /* HID report descriptor */
61 u8 *report_desc;
62 /* length of HID report descriptor */
63 int report_desc_len;
64 /* number of bytes of report_desc we have received so far */
65 int report_desc_offset;
66};
67
Benoit Gobycf3fc062011-12-19 14:39:37 -080068struct acc_dev {
69 struct usb_function function;
70 struct usb_composite_dev *cdev;
71 spinlock_t lock;
72
73 struct usb_ep *ep_in;
74 struct usb_ep *ep_out;
75
76 /* set to 1 when we connect */
77 int online:1;
78 /* Set to 1 when we disconnect.
79 * Not cleared until our file is closed.
80 */
81 int disconnected:1;
82
83 /* strings sent by the host */
84 char manufacturer[ACC_STRING_SIZE];
85 char model[ACC_STRING_SIZE];
86 char description[ACC_STRING_SIZE];
87 char version[ACC_STRING_SIZE];
88 char uri[ACC_STRING_SIZE];
89 char serial[ACC_STRING_SIZE];
90
91 /* for acc_complete_set_string */
92 int string_index;
93
94 /* set to 1 if we have a pending start request */
95 int start_requested;
96
Mike Lockwoodcf17a842012-05-11 09:00:40 -070097 int audio_mode;
98
Benoit Gobycf3fc062011-12-19 14:39:37 -080099 /* synchronize access to our device file */
100 atomic_t open_excl;
101
102 struct list_head tx_idle;
103
104 wait_queue_head_t read_wq;
105 wait_queue_head_t write_wq;
106 struct usb_request *rx_req[RX_REQ_MAX];
107 int rx_done;
Mike Lockwoode7558bb2012-08-27 16:23:48 +0530108
109 /* delayed work for handling ACCESSORY_START */
110 struct delayed_work start_work;
111
112 /* worker for registering and unregistering hid devices */
113 struct work_struct hid_work;
114
115 /* list of active HID devices */
116 struct list_head hid_list;
117
118 /* list of new HID devices to register */
119 struct list_head new_hid_list;
120
121 /* list of dead HID devices to unregister */
122 struct list_head dead_hid_list;
Benoit Gobycf3fc062011-12-19 14:39:37 -0800123};
124
125static struct usb_interface_descriptor acc_interface_desc = {
126 .bLength = USB_DT_INTERFACE_SIZE,
127 .bDescriptorType = USB_DT_INTERFACE,
128 .bInterfaceNumber = 0,
129 .bNumEndpoints = 2,
130 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
131 .bInterfaceSubClass = USB_SUBCLASS_VENDOR_SPEC,
132 .bInterfaceProtocol = 0,
133};
134
Pavankumar Kondeti6f94bc92012-08-03 09:34:32 +0530135static struct usb_endpoint_descriptor acc_superspeed_in_desc = {
136 .bLength = USB_DT_ENDPOINT_SIZE,
137 .bDescriptorType = USB_DT_ENDPOINT,
138 .bEndpointAddress = USB_DIR_IN,
139 .bmAttributes = USB_ENDPOINT_XFER_BULK,
140 .wMaxPacketSize = __constant_cpu_to_le16(1024),
141};
142
143static struct usb_ss_ep_comp_descriptor acc_superspeed_in_comp_desc = {
144 .bLength = sizeof acc_superspeed_in_comp_desc,
145 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
146
147 /* the following 2 values can be tweaked if necessary */
148 /* .bMaxBurst = 0, */
149 /* .bmAttributes = 0, */
150};
151
152static struct usb_endpoint_descriptor acc_superspeed_out_desc = {
153 .bLength = USB_DT_ENDPOINT_SIZE,
154 .bDescriptorType = USB_DT_ENDPOINT,
155 .bEndpointAddress = USB_DIR_OUT,
156 .bmAttributes = USB_ENDPOINT_XFER_BULK,
157 .wMaxPacketSize = __constant_cpu_to_le16(1024),
158};
159
160static struct usb_ss_ep_comp_descriptor acc_superspeed_out_comp_desc = {
161 .bLength = sizeof acc_superspeed_out_comp_desc,
162 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
163
164 /* the following 2 values can be tweaked if necessary */
165 /* .bMaxBurst = 0, */
166 /* .bmAttributes = 0, */
167};
168
169
Benoit Gobycf3fc062011-12-19 14:39:37 -0800170static struct usb_endpoint_descriptor acc_highspeed_in_desc = {
171 .bLength = USB_DT_ENDPOINT_SIZE,
172 .bDescriptorType = USB_DT_ENDPOINT,
173 .bEndpointAddress = USB_DIR_IN,
174 .bmAttributes = USB_ENDPOINT_XFER_BULK,
175 .wMaxPacketSize = __constant_cpu_to_le16(512),
176};
177
178static struct usb_endpoint_descriptor acc_highspeed_out_desc = {
179 .bLength = USB_DT_ENDPOINT_SIZE,
180 .bDescriptorType = USB_DT_ENDPOINT,
181 .bEndpointAddress = USB_DIR_OUT,
182 .bmAttributes = USB_ENDPOINT_XFER_BULK,
183 .wMaxPacketSize = __constant_cpu_to_le16(512),
184};
185
186static struct usb_endpoint_descriptor acc_fullspeed_in_desc = {
187 .bLength = USB_DT_ENDPOINT_SIZE,
188 .bDescriptorType = USB_DT_ENDPOINT,
189 .bEndpointAddress = USB_DIR_IN,
190 .bmAttributes = USB_ENDPOINT_XFER_BULK,
191};
192
193static struct usb_endpoint_descriptor acc_fullspeed_out_desc = {
194 .bLength = USB_DT_ENDPOINT_SIZE,
195 .bDescriptorType = USB_DT_ENDPOINT,
196 .bEndpointAddress = USB_DIR_OUT,
197 .bmAttributes = USB_ENDPOINT_XFER_BULK,
198};
199
200static struct usb_descriptor_header *fs_acc_descs[] = {
201 (struct usb_descriptor_header *) &acc_interface_desc,
202 (struct usb_descriptor_header *) &acc_fullspeed_in_desc,
203 (struct usb_descriptor_header *) &acc_fullspeed_out_desc,
204 NULL,
205};
206
207static struct usb_descriptor_header *hs_acc_descs[] = {
208 (struct usb_descriptor_header *) &acc_interface_desc,
209 (struct usb_descriptor_header *) &acc_highspeed_in_desc,
210 (struct usb_descriptor_header *) &acc_highspeed_out_desc,
211 NULL,
212};
213
Pavankumar Kondeti6f94bc92012-08-03 09:34:32 +0530214static struct usb_descriptor_header *ss_acc_descs[] = {
215 (struct usb_descriptor_header *) &acc_interface_desc,
216 (struct usb_descriptor_header *) &acc_superspeed_in_desc,
217 (struct usb_descriptor_header *) &acc_superspeed_in_comp_desc,
218 (struct usb_descriptor_header *) &acc_superspeed_out_desc,
219 (struct usb_descriptor_header *) &acc_superspeed_out_comp_desc,
220 NULL,
221};
222
Benoit Gobycf3fc062011-12-19 14:39:37 -0800223static struct usb_string acc_string_defs[] = {
224 [INTERFACE_STRING_INDEX].s = "Android Accessory Interface",
225 { }, /* end of list */
226};
227
228static struct usb_gadget_strings acc_string_table = {
229 .language = 0x0409, /* en-US */
230 .strings = acc_string_defs,
231};
232
233static struct usb_gadget_strings *acc_strings[] = {
234 &acc_string_table,
235 NULL,
236};
237
238/* temporary variable used between acc_open() and acc_gadget_bind() */
239static struct acc_dev *_acc_dev;
240
241static inline struct acc_dev *func_to_dev(struct usb_function *f)
242{
243 return container_of(f, struct acc_dev, function);
244}
245
246static struct usb_request *acc_request_new(struct usb_ep *ep, int buffer_size)
247{
248 struct usb_request *req = usb_ep_alloc_request(ep, GFP_KERNEL);
249 if (!req)
250 return NULL;
251
252 /* now allocate buffers for the requests */
253 req->buf = kmalloc(buffer_size, GFP_KERNEL);
254 if (!req->buf) {
255 usb_ep_free_request(ep, req);
256 return NULL;
257 }
258
259 return req;
260}
261
262static void acc_request_free(struct usb_request *req, struct usb_ep *ep)
263{
264 if (req) {
265 kfree(req->buf);
266 usb_ep_free_request(ep, req);
267 }
268}
269
270/* add a request to the tail of a list */
271static void req_put(struct acc_dev *dev, struct list_head *head,
272 struct usb_request *req)
273{
274 unsigned long flags;
275
276 spin_lock_irqsave(&dev->lock, flags);
277 list_add_tail(&req->list, head);
278 spin_unlock_irqrestore(&dev->lock, flags);
279}
280
281/* remove a request from the head of a list */
282static struct usb_request *req_get(struct acc_dev *dev, struct list_head *head)
283{
284 unsigned long flags;
285 struct usb_request *req;
286
287 spin_lock_irqsave(&dev->lock, flags);
288 if (list_empty(head)) {
289 req = 0;
290 } else {
291 req = list_first_entry(head, struct usb_request, list);
292 list_del(&req->list);
293 }
294 spin_unlock_irqrestore(&dev->lock, flags);
295 return req;
296}
297
298static void acc_set_disconnected(struct acc_dev *dev)
299{
300 dev->online = 0;
301 dev->disconnected = 1;
302}
303
304static void acc_complete_in(struct usb_ep *ep, struct usb_request *req)
305{
306 struct acc_dev *dev = _acc_dev;
307
308 if (req->status != 0)
309 acc_set_disconnected(dev);
310
311 req_put(dev, &dev->tx_idle, req);
312
313 wake_up(&dev->write_wq);
314}
315
316static void acc_complete_out(struct usb_ep *ep, struct usb_request *req)
317{
318 struct acc_dev *dev = _acc_dev;
319
320 dev->rx_done = 1;
321 if (req->status != 0)
322 acc_set_disconnected(dev);
323
324 wake_up(&dev->read_wq);
325}
326
327static void acc_complete_set_string(struct usb_ep *ep, struct usb_request *req)
328{
329 struct acc_dev *dev = ep->driver_data;
330 char *string_dest = NULL;
331 int length = req->actual;
332
333 if (req->status != 0) {
334 pr_err("acc_complete_set_string, err %d\n", req->status);
335 return;
336 }
337
338 switch (dev->string_index) {
339 case ACCESSORY_STRING_MANUFACTURER:
340 string_dest = dev->manufacturer;
341 break;
342 case ACCESSORY_STRING_MODEL:
343 string_dest = dev->model;
344 break;
345 case ACCESSORY_STRING_DESCRIPTION:
346 string_dest = dev->description;
347 break;
348 case ACCESSORY_STRING_VERSION:
349 string_dest = dev->version;
350 break;
351 case ACCESSORY_STRING_URI:
352 string_dest = dev->uri;
353 break;
354 case ACCESSORY_STRING_SERIAL:
355 string_dest = dev->serial;
356 break;
357 }
358 if (string_dest) {
359 unsigned long flags;
360
361 if (length >= ACC_STRING_SIZE)
362 length = ACC_STRING_SIZE - 1;
363
364 spin_lock_irqsave(&dev->lock, flags);
365 memcpy(string_dest, req->buf, length);
366 /* ensure zero termination */
367 string_dest[length] = 0;
368 spin_unlock_irqrestore(&dev->lock, flags);
369 } else {
370 pr_err("unknown accessory string index %d\n",
371 dev->string_index);
372 }
373}
374
Mike Lockwoode7558bb2012-08-27 16:23:48 +0530375static void acc_complete_set_hid_report_desc(struct usb_ep *ep,
376 struct usb_request *req)
377{
378 struct acc_hid_dev *hid = req->context;
379 struct acc_dev *dev = hid->dev;
380 int length = req->actual;
381
382 if (req->status != 0) {
383 pr_err("acc_complete_set_hid_report_desc, err %d\n",
384 req->status);
385 return;
386 }
387
388 memcpy(hid->report_desc + hid->report_desc_offset, req->buf, length);
389 hid->report_desc_offset += length;
390 if (hid->report_desc_offset == hid->report_desc_len) {
391 /* After we have received the entire report descriptor
392 * we schedule work to initialize the HID device
393 */
394 schedule_work(&dev->hid_work);
395 }
396}
397
398static void acc_complete_send_hid_event(struct usb_ep *ep,
399 struct usb_request *req)
400{
401 struct acc_hid_dev *hid = req->context;
402 int length = req->actual;
403
404 if (req->status != 0) {
405 pr_err("acc_complete_send_hid_event, err %d\n", req->status);
406 return;
407 }
408
409 hid_report_raw_event(hid->hid, HID_INPUT_REPORT, req->buf, length, 1);
410}
411
412static int acc_hid_parse(struct hid_device *hid)
413{
414 struct acc_hid_dev *hdev = hid->driver_data;
415
416 hid_parse_report(hid, hdev->report_desc, hdev->report_desc_len);
417 return 0;
418}
419
420static int acc_hid_start(struct hid_device *hid)
421{
422 return 0;
423}
424
425static void acc_hid_stop(struct hid_device *hid)
426{
427}
428
429static int acc_hid_open(struct hid_device *hid)
430{
431 return 0;
432}
433
434static void acc_hid_close(struct hid_device *hid)
435{
436}
437
438static struct hid_ll_driver acc_hid_ll_driver = {
439 .parse = acc_hid_parse,
440 .start = acc_hid_start,
441 .stop = acc_hid_stop,
442 .open = acc_hid_open,
443 .close = acc_hid_close,
444};
445
446static struct acc_hid_dev *acc_hid_new(struct acc_dev *dev,
447 int id, int desc_len)
448{
449 struct acc_hid_dev *hdev;
450
451 hdev = kzalloc(sizeof(*hdev), GFP_ATOMIC);
452 if (!hdev)
453 return NULL;
454 hdev->report_desc = kzalloc(desc_len, GFP_ATOMIC);
455 if (!hdev->report_desc) {
456 kfree(hdev);
457 return NULL;
458 }
459 hdev->dev = dev;
460 hdev->id = id;
461 hdev->report_desc_len = desc_len;
462
463 return hdev;
464}
465
466static struct acc_hid_dev *acc_hid_get(struct list_head *list, int id)
467{
468 struct acc_hid_dev *hid;
469
470 list_for_each_entry(hid, list, list) {
471 if (hid->id == id)
472 return hid;
473 }
474 return NULL;
475}
476
477static int acc_register_hid(struct acc_dev *dev, int id, int desc_length)
478{
479 struct acc_hid_dev *hid;
480 unsigned long flags;
481
482 /* report descriptor length must be > 0 */
483 if (desc_length <= 0)
484 return -EINVAL;
485
486 spin_lock_irqsave(&dev->lock, flags);
487 /* replace HID if one already exists with this ID */
488 hid = acc_hid_get(&dev->hid_list, id);
489 if (!hid)
490 hid = acc_hid_get(&dev->new_hid_list, id);
491 if (hid)
492 list_move(&hid->list, &dev->dead_hid_list);
493
494 hid = acc_hid_new(dev, id, desc_length);
495 if (!hid) {
496 spin_unlock_irqrestore(&dev->lock, flags);
497 return -ENOMEM;
498 }
499
500 list_add(&hid->list, &dev->new_hid_list);
501 spin_unlock_irqrestore(&dev->lock, flags);
502
503 /* schedule work to register the HID device */
504 schedule_work(&dev->hid_work);
505 return 0;
506}
507
508static int acc_unregister_hid(struct acc_dev *dev, int id)
509{
510 struct acc_hid_dev *hid;
511 unsigned long flags;
512
513 spin_lock_irqsave(&dev->lock, flags);
514 hid = acc_hid_get(&dev->hid_list, id);
515 if (!hid)
516 hid = acc_hid_get(&dev->new_hid_list, id);
517 if (!hid) {
518 spin_unlock_irqrestore(&dev->lock, flags);
519 return -EINVAL;
520 }
521
522 list_move(&hid->list, &dev->dead_hid_list);
523 spin_unlock_irqrestore(&dev->lock, flags);
524
525 schedule_work(&dev->hid_work);
526 return 0;
527}
528
Stephen Boyd78531282012-04-25 11:55:15 -0700529static int create_bulk_endpoints(struct acc_dev *dev,
Benoit Gobycf3fc062011-12-19 14:39:37 -0800530 struct usb_endpoint_descriptor *in_desc,
531 struct usb_endpoint_descriptor *out_desc)
532{
533 struct usb_composite_dev *cdev = dev->cdev;
534 struct usb_request *req;
535 struct usb_ep *ep;
536 int i;
537
538 DBG(cdev, "create_bulk_endpoints dev: %p\n", dev);
539
540 ep = usb_ep_autoconfig(cdev->gadget, in_desc);
541 if (!ep) {
542 DBG(cdev, "usb_ep_autoconfig for ep_in failed\n");
543 return -ENODEV;
544 }
545 DBG(cdev, "usb_ep_autoconfig for ep_in got %s\n", ep->name);
546 ep->driver_data = dev; /* claim the endpoint */
547 dev->ep_in = ep;
548
549 ep = usb_ep_autoconfig(cdev->gadget, out_desc);
550 if (!ep) {
551 DBG(cdev, "usb_ep_autoconfig for ep_out failed\n");
552 return -ENODEV;
553 }
554 DBG(cdev, "usb_ep_autoconfig for ep_out got %s\n", ep->name);
555 ep->driver_data = dev; /* claim the endpoint */
556 dev->ep_out = ep;
557
558 ep = usb_ep_autoconfig(cdev->gadget, out_desc);
559 if (!ep) {
560 DBG(cdev, "usb_ep_autoconfig for ep_out failed\n");
561 return -ENODEV;
562 }
563 DBG(cdev, "usb_ep_autoconfig for ep_out got %s\n", ep->name);
564 ep->driver_data = dev; /* claim the endpoint */
565 dev->ep_out = ep;
566
567 /* now allocate requests for our endpoints */
568 for (i = 0; i < TX_REQ_MAX; i++) {
569 req = acc_request_new(dev->ep_in, BULK_BUFFER_SIZE);
570 if (!req)
571 goto fail;
572 req->complete = acc_complete_in;
573 req_put(dev, &dev->tx_idle, req);
574 }
575 for (i = 0; i < RX_REQ_MAX; i++) {
576 req = acc_request_new(dev->ep_out, BULK_BUFFER_SIZE);
577 if (!req)
578 goto fail;
579 req->complete = acc_complete_out;
580 dev->rx_req[i] = req;
581 }
582
583 return 0;
584
585fail:
Mike Lockwoode7558bb2012-08-27 16:23:48 +0530586 pr_err("acc_bind() could not allocate requests\n");
Benoit Gobycf3fc062011-12-19 14:39:37 -0800587 while ((req = req_get(dev, &dev->tx_idle)))
588 acc_request_free(req, dev->ep_in);
589 for (i = 0; i < RX_REQ_MAX; i++)
590 acc_request_free(dev->rx_req[i], dev->ep_out);
591 return -1;
592}
593
594static ssize_t acc_read(struct file *fp, char __user *buf,
595 size_t count, loff_t *pos)
596{
597 struct acc_dev *dev = fp->private_data;
598 struct usb_request *req;
599 int r = count, xfer;
600 int ret = 0;
601
602 pr_debug("acc_read(%d)\n", count);
603
604 if (dev->disconnected)
605 return -ENODEV;
606
607 if (count > BULK_BUFFER_SIZE)
608 count = BULK_BUFFER_SIZE;
609
610 /* we will block until we're online */
611 pr_debug("acc_read: waiting for online\n");
612 ret = wait_event_interruptible(dev->read_wq, dev->online);
613 if (ret < 0) {
614 r = ret;
615 goto done;
616 }
617
618requeue_req:
619 /* queue a request */
620 req = dev->rx_req[0];
621 req->length = count;
622 dev->rx_done = 0;
623 ret = usb_ep_queue(dev->ep_out, req, GFP_KERNEL);
624 if (ret < 0) {
625 r = -EIO;
626 goto done;
627 } else {
628 pr_debug("rx %p queue\n", req);
629 }
630
631 /* wait for a request to complete */
632 ret = wait_event_interruptible(dev->read_wq, dev->rx_done);
633 if (ret < 0) {
634 r = ret;
635 usb_ep_dequeue(dev->ep_out, req);
636 goto done;
637 }
638 if (dev->online) {
639 /* If we got a 0-len packet, throw it back and try again. */
640 if (req->actual == 0)
641 goto requeue_req;
642
643 pr_debug("rx %p %d\n", req, req->actual);
644 xfer = (req->actual < count) ? req->actual : count;
645 r = xfer;
646 if (copy_to_user(buf, req->buf, xfer))
647 r = -EFAULT;
648 } else
649 r = -EIO;
650
651done:
652 pr_debug("acc_read returning %d\n", r);
653 return r;
654}
655
656static ssize_t acc_write(struct file *fp, const char __user *buf,
657 size_t count, loff_t *pos)
658{
659 struct acc_dev *dev = fp->private_data;
660 struct usb_request *req = 0;
661 int r = count, xfer;
662 int ret;
663
664 pr_debug("acc_write(%d)\n", count);
665
666 if (!dev->online || dev->disconnected)
667 return -ENODEV;
668
669 while (count > 0) {
670 if (!dev->online) {
671 pr_debug("acc_write dev->error\n");
672 r = -EIO;
673 break;
674 }
675
676 /* get an idle tx request to use */
677 req = 0;
678 ret = wait_event_interruptible(dev->write_wq,
679 ((req = req_get(dev, &dev->tx_idle)) || !dev->online));
680 if (!req) {
681 r = ret;
682 break;
683 }
684
685 if (count > BULK_BUFFER_SIZE)
686 xfer = BULK_BUFFER_SIZE;
687 else
688 xfer = count;
689 if (copy_from_user(req->buf, buf, xfer)) {
690 r = -EFAULT;
691 break;
692 }
693
694 req->length = xfer;
695 ret = usb_ep_queue(dev->ep_in, req, GFP_KERNEL);
696 if (ret < 0) {
697 pr_debug("acc_write: xfer error %d\n", ret);
698 r = -EIO;
699 break;
700 }
701
702 buf += xfer;
703 count -= xfer;
704
705 /* zero this so we don't try to free it on error exit */
706 req = 0;
707 }
708
709 if (req)
710 req_put(dev, &dev->tx_idle, req);
711
712 pr_debug("acc_write returning %d\n", r);
713 return r;
714}
715
716static long acc_ioctl(struct file *fp, unsigned code, unsigned long value)
717{
718 struct acc_dev *dev = fp->private_data;
719 char *src = NULL;
720 int ret;
721
722 switch (code) {
723 case ACCESSORY_GET_STRING_MANUFACTURER:
724 src = dev->manufacturer;
725 break;
726 case ACCESSORY_GET_STRING_MODEL:
727 src = dev->model;
728 break;
729 case ACCESSORY_GET_STRING_DESCRIPTION:
730 src = dev->description;
731 break;
732 case ACCESSORY_GET_STRING_VERSION:
733 src = dev->version;
734 break;
735 case ACCESSORY_GET_STRING_URI:
736 src = dev->uri;
737 break;
738 case ACCESSORY_GET_STRING_SERIAL:
739 src = dev->serial;
740 break;
741 case ACCESSORY_IS_START_REQUESTED:
742 return dev->start_requested;
Mike Lockwoodcf17a842012-05-11 09:00:40 -0700743 case ACCESSORY_GET_AUDIO_MODE:
744 return dev->audio_mode;
Benoit Gobycf3fc062011-12-19 14:39:37 -0800745 }
746 if (!src)
747 return -EINVAL;
748
749 ret = strlen(src) + 1;
750 if (copy_to_user((void __user *)value, src, ret))
751 ret = -EFAULT;
752 return ret;
753}
754
755static int acc_open(struct inode *ip, struct file *fp)
756{
757 printk(KERN_INFO "acc_open\n");
758 if (atomic_xchg(&_acc_dev->open_excl, 1))
759 return -EBUSY;
760
761 _acc_dev->disconnected = 0;
762 fp->private_data = _acc_dev;
763 return 0;
764}
765
766static int acc_release(struct inode *ip, struct file *fp)
767{
768 printk(KERN_INFO "acc_release\n");
769
770 WARN_ON(!atomic_xchg(&_acc_dev->open_excl, 0));
771 _acc_dev->disconnected = 0;
772 return 0;
773}
774
Mike Lockwoode7558bb2012-08-27 16:23:48 +0530775/* file operations for /dev/usb_accessory */
Benoit Gobycf3fc062011-12-19 14:39:37 -0800776static const struct file_operations acc_fops = {
777 .owner = THIS_MODULE,
778 .read = acc_read,
779 .write = acc_write,
780 .unlocked_ioctl = acc_ioctl,
781 .open = acc_open,
782 .release = acc_release,
783};
784
Mike Lockwoode7558bb2012-08-27 16:23:48 +0530785static int acc_hid_probe(struct hid_device *hdev,
786 const struct hid_device_id *id)
787{
788 int ret;
789
790 ret = hid_parse(hdev);
791 if (ret)
792 return ret;
793 return hid_hw_start(hdev, HID_CONNECT_DEFAULT);
794}
795
Benoit Gobycf3fc062011-12-19 14:39:37 -0800796static struct miscdevice acc_device = {
797 .minor = MISC_DYNAMIC_MINOR,
798 .name = "usb_accessory",
799 .fops = &acc_fops,
800};
801
Mike Lockwoode7558bb2012-08-27 16:23:48 +0530802static const struct hid_device_id acc_hid_table[] = {
803 { HID_USB_DEVICE(HID_ANY_ID, HID_ANY_ID) },
804 { }
805};
806
807static struct hid_driver acc_hid_driver = {
808 .name = "USB accessory",
809 .id_table = acc_hid_table,
810 .probe = acc_hid_probe,
811};
Benoit Gobycf3fc062011-12-19 14:39:37 -0800812
813static int acc_ctrlrequest(struct usb_composite_dev *cdev,
814 const struct usb_ctrlrequest *ctrl)
815{
816 struct acc_dev *dev = _acc_dev;
817 int value = -EOPNOTSUPP;
Mike Lockwoode7558bb2012-08-27 16:23:48 +0530818 struct acc_hid_dev *hid;
819 int offset;
Benoit Gobycf3fc062011-12-19 14:39:37 -0800820 u8 b_requestType = ctrl->bRequestType;
821 u8 b_request = ctrl->bRequest;
822 u16 w_index = le16_to_cpu(ctrl->wIndex);
823 u16 w_value = le16_to_cpu(ctrl->wValue);
824 u16 w_length = le16_to_cpu(ctrl->wLength);
Mike Lockwoode7558bb2012-08-27 16:23:48 +0530825 unsigned long flags;
Benoit Gobycf3fc062011-12-19 14:39:37 -0800826
827/*
828 printk(KERN_INFO "acc_ctrlrequest "
829 "%02x.%02x v%04x i%04x l%u\n",
830 b_requestType, b_request,
831 w_value, w_index, w_length);
832*/
833
834 if (b_requestType == (USB_DIR_OUT | USB_TYPE_VENDOR)) {
835 if (b_request == ACCESSORY_START) {
836 dev->start_requested = 1;
837 schedule_delayed_work(
Mike Lockwoode7558bb2012-08-27 16:23:48 +0530838 &dev->start_work, msecs_to_jiffies(10));
Benoit Gobycf3fc062011-12-19 14:39:37 -0800839 value = 0;
840 } else if (b_request == ACCESSORY_SEND_STRING) {
841 dev->string_index = w_index;
842 cdev->gadget->ep0->driver_data = dev;
843 cdev->req->complete = acc_complete_set_string;
844 value = w_length;
Mike Lockwoodcf17a842012-05-11 09:00:40 -0700845 } else if (b_request == ACCESSORY_SET_AUDIO_MODE &&
846 w_index == 0 && w_length == 0) {
847 dev->audio_mode = w_value;
848 value = 0;
Mike Lockwoode7558bb2012-08-27 16:23:48 +0530849 } else if (b_request == ACCESSORY_REGISTER_HID) {
850 value = acc_register_hid(dev, w_value, w_index);
851 } else if (b_request == ACCESSORY_UNREGISTER_HID) {
852 value = acc_unregister_hid(dev, w_value);
853 } else if (b_request == ACCESSORY_SET_HID_REPORT_DESC) {
854 spin_lock_irqsave(&dev->lock, flags);
855 hid = acc_hid_get(&dev->new_hid_list, w_value);
856 spin_unlock_irqrestore(&dev->lock, flags);
857 if (!hid) {
858 value = -EINVAL;
859 goto err;
860 }
861 offset = w_index;
862 if (offset != hid->report_desc_offset
863 || offset + w_length > hid->report_desc_len) {
864 value = -EINVAL;
865 goto err;
866 }
867 cdev->req->context = hid;
868 cdev->req->complete = acc_complete_set_hid_report_desc;
869 value = w_length;
870 } else if (b_request == ACCESSORY_SEND_HID_EVENT) {
871 spin_lock_irqsave(&dev->lock, flags);
872 hid = acc_hid_get(&dev->hid_list, w_value);
873 spin_unlock_irqrestore(&dev->lock, flags);
874 if (!hid) {
875 value = -EINVAL;
876 goto err;
877 }
878 cdev->req->context = hid;
879 cdev->req->complete = acc_complete_send_hid_event;
880 value = w_length;
Benoit Gobycf3fc062011-12-19 14:39:37 -0800881 }
882 } else if (b_requestType == (USB_DIR_IN | USB_TYPE_VENDOR)) {
883 if (b_request == ACCESSORY_GET_PROTOCOL) {
884 *((u16 *)cdev->req->buf) = PROTOCOL_VERSION;
885 value = sizeof(u16);
886
Mike Lockwoode7558bb2012-08-27 16:23:48 +0530887 /* clear strings left over from a previous session */
Benoit Gobycf3fc062011-12-19 14:39:37 -0800888 memset(dev->manufacturer, 0, sizeof(dev->manufacturer));
889 memset(dev->model, 0, sizeof(dev->model));
890 memset(dev->description, 0, sizeof(dev->description));
891 memset(dev->version, 0, sizeof(dev->version));
892 memset(dev->uri, 0, sizeof(dev->uri));
893 memset(dev->serial, 0, sizeof(dev->serial));
894 dev->start_requested = 0;
Mike Lockwoodcf17a842012-05-11 09:00:40 -0700895 dev->audio_mode = 0;
Benoit Gobycf3fc062011-12-19 14:39:37 -0800896 }
897 }
898
899 if (value >= 0) {
900 cdev->req->zero = 0;
901 cdev->req->length = value;
902 value = usb_ep_queue(cdev->gadget->ep0, cdev->req, GFP_ATOMIC);
903 if (value < 0)
904 ERROR(cdev, "%s setup response queue error\n",
905 __func__);
906 }
907
Mike Lockwoode7558bb2012-08-27 16:23:48 +0530908err:
Benoit Gobycf3fc062011-12-19 14:39:37 -0800909 if (value == -EOPNOTSUPP)
910 VDBG(cdev,
911 "unknown class-specific control req "
912 "%02x.%02x v%04x i%04x l%u\n",
913 ctrl->bRequestType, ctrl->bRequest,
914 w_value, w_index, w_length);
915 return value;
916}
917
918static int
919acc_function_bind(struct usb_configuration *c, struct usb_function *f)
920{
921 struct usb_composite_dev *cdev = c->cdev;
922 struct acc_dev *dev = func_to_dev(f);
923 int id;
924 int ret;
925
926 DBG(cdev, "acc_function_bind dev: %p\n", dev);
927
Mike Lockwoode7558bb2012-08-27 16:23:48 +0530928 ret = hid_register_driver(&acc_hid_driver);
929 if (ret)
930 return ret;
931
Benoit Gobycf3fc062011-12-19 14:39:37 -0800932 dev->start_requested = 0;
933
934 /* allocate interface ID(s) */
935 id = usb_interface_id(c, f);
936 if (id < 0)
937 return id;
938 acc_interface_desc.bInterfaceNumber = id;
939
940 /* allocate endpoints */
941 ret = create_bulk_endpoints(dev, &acc_fullspeed_in_desc,
942 &acc_fullspeed_out_desc);
943 if (ret)
944 return ret;
945
946 /* support high speed hardware */
947 if (gadget_is_dualspeed(c->cdev->gadget)) {
948 acc_highspeed_in_desc.bEndpointAddress =
949 acc_fullspeed_in_desc.bEndpointAddress;
950 acc_highspeed_out_desc.bEndpointAddress =
951 acc_fullspeed_out_desc.bEndpointAddress;
952 }
953
Pavankumar Kondeti6f94bc92012-08-03 09:34:32 +0530954 /* support super speed hardware */
955 if (gadget_is_superspeed(c->cdev->gadget)) {
956 acc_superspeed_in_desc.bEndpointAddress =
957 acc_fullspeed_in_desc.bEndpointAddress;
958 acc_superspeed_out_desc.bEndpointAddress =
959 acc_fullspeed_out_desc.bEndpointAddress;
960 }
961
Benoit Gobycf3fc062011-12-19 14:39:37 -0800962 DBG(cdev, "%s speed %s: IN/%s, OUT/%s\n",
963 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
964 f->name, dev->ep_in->name, dev->ep_out->name);
965 return 0;
966}
967
968static void
Mike Lockwoode7558bb2012-08-27 16:23:48 +0530969kill_all_hid_devices(struct acc_dev *dev)
970{
971 struct acc_hid_dev *hid;
972 struct list_head *entry, *temp;
973 unsigned long flags;
974
975 spin_lock_irqsave(&dev->lock, flags);
976 list_for_each_safe(entry, temp, &dev->hid_list) {
977 hid = list_entry(entry, struct acc_hid_dev, list);
978 list_del(&hid->list);
979 list_add(&hid->list, &dev->dead_hid_list);
980 }
981 list_for_each_safe(entry, temp, &dev->new_hid_list) {
982 hid = list_entry(entry, struct acc_hid_dev, list);
983 list_del(&hid->list);
984 list_add(&hid->list, &dev->dead_hid_list);
985 }
986 spin_unlock_irqrestore(&dev->lock, flags);
987
988 schedule_work(&dev->hid_work);
989}
990
991static void
992acc_hid_unbind(struct acc_dev *dev)
993{
994 hid_unregister_driver(&acc_hid_driver);
995 kill_all_hid_devices(dev);
996}
997
998static void
Benoit Gobycf3fc062011-12-19 14:39:37 -0800999acc_function_unbind(struct usb_configuration *c, struct usb_function *f)
1000{
1001 struct acc_dev *dev = func_to_dev(f);
1002 struct usb_request *req;
1003 int i;
1004
1005 while ((req = req_get(dev, &dev->tx_idle)))
1006 acc_request_free(req, dev->ep_in);
1007 for (i = 0; i < RX_REQ_MAX; i++)
1008 acc_request_free(dev->rx_req[i], dev->ep_out);
Mike Lockwoode7558bb2012-08-27 16:23:48 +05301009
1010 acc_hid_unbind(dev);
Benoit Gobycf3fc062011-12-19 14:39:37 -08001011}
1012
Mike Lockwoode7558bb2012-08-27 16:23:48 +05301013static void acc_start_work(struct work_struct *data)
Benoit Gobycf3fc062011-12-19 14:39:37 -08001014{
1015 char *envp[2] = { "ACCESSORY=START", NULL };
1016 kobject_uevent_env(&acc_device.this_device->kobj, KOBJ_CHANGE, envp);
1017}
1018
Mike Lockwoode7558bb2012-08-27 16:23:48 +05301019static int acc_hid_init(struct acc_hid_dev *hdev)
1020{
1021 struct hid_device *hid;
1022 int ret;
1023
1024 hid = hid_allocate_device();
1025 if (IS_ERR(hid))
1026 return PTR_ERR(hid);
1027
1028 hid->ll_driver = &acc_hid_ll_driver;
1029 hid->dev.parent = acc_device.this_device;
1030
1031 hid->bus = BUS_USB;
1032 hid->vendor = HID_ANY_ID;
1033 hid->product = HID_ANY_ID;
1034 hid->driver_data = hdev;
1035 ret = hid_add_device(hid);
1036 if (ret) {
1037 pr_err("can't add hid device: %d\n", ret);
1038 hid_destroy_device(hid);
1039 return ret;
1040 }
1041
1042 hdev->hid = hid;
1043 return 0;
1044}
1045
1046static void acc_hid_delete(struct acc_hid_dev *hid)
1047{
1048 kfree(hid->report_desc);
1049 kfree(hid);
1050}
1051
1052static void acc_hid_work(struct work_struct *data)
1053{
1054 struct acc_dev *dev = _acc_dev;
1055 struct list_head *entry, *temp;
1056 struct acc_hid_dev *hid;
1057 struct list_head new_list, dead_list;
1058 unsigned long flags;
1059
1060 INIT_LIST_HEAD(&new_list);
1061
1062 spin_lock_irqsave(&dev->lock, flags);
1063
1064 /* copy hids that are ready for initialization to new_list */
1065 list_for_each_safe(entry, temp, &dev->new_hid_list) {
1066 hid = list_entry(entry, struct acc_hid_dev, list);
1067 if (hid->report_desc_offset == hid->report_desc_len)
1068 list_move(&hid->list, &new_list);
1069 }
1070
1071 if (list_empty(&dev->dead_hid_list)) {
1072 INIT_LIST_HEAD(&dead_list);
1073 } else {
1074 /* move all of dev->dead_hid_list to dead_list */
1075 dead_list.prev = dev->dead_hid_list.prev;
1076 dead_list.next = dev->dead_hid_list.next;
1077 dead_list.next->prev = &dead_list;
1078 dead_list.prev->next = &dead_list;
1079 INIT_LIST_HEAD(&dev->dead_hid_list);
1080 }
1081
1082 spin_unlock_irqrestore(&dev->lock, flags);
1083
1084 /* register new HID devices */
1085 list_for_each_safe(entry, temp, &new_list) {
1086 hid = list_entry(entry, struct acc_hid_dev, list);
1087 if (acc_hid_init(hid)) {
1088 pr_err("can't add HID device %p\n", hid);
1089 acc_hid_delete(hid);
1090 } else {
1091 spin_lock_irqsave(&dev->lock, flags);
1092 list_move(&hid->list, &dev->hid_list);
1093 spin_unlock_irqrestore(&dev->lock, flags);
1094 }
1095 }
1096
1097 /* remove dead HID devices */
1098 list_for_each_safe(entry, temp, &dead_list) {
1099 hid = list_entry(entry, struct acc_hid_dev, list);
1100 list_del(&hid->list);
1101 if (hid->hid)
1102 hid_destroy_device(hid->hid);
1103 acc_hid_delete(hid);
1104 }
1105}
1106
Benoit Gobycf3fc062011-12-19 14:39:37 -08001107static int acc_function_set_alt(struct usb_function *f,
1108 unsigned intf, unsigned alt)
1109{
1110 struct acc_dev *dev = func_to_dev(f);
1111 struct usb_composite_dev *cdev = f->config->cdev;
1112 int ret;
1113
1114 DBG(cdev, "acc_function_set_alt intf: %d alt: %d\n", intf, alt);
1115
1116 ret = config_ep_by_speed(cdev->gadget, f, dev->ep_in);
Tatyana Brokhmanebd3f392011-06-28 16:33:50 +03001117 if (ret) {
Tatyana Brokhman31ac3522011-06-28 15:33:50 +02001118 dev->ep_in->desc = NULL;
1119 ERROR(cdev, "config_ep_by_speed failes for ep %s, result %d\n",
1120 dev->ep_in->name, ret);
1121 return ret;
1122 }
Benoit Gobycf3fc062011-12-19 14:39:37 -08001123 ret = usb_ep_enable(dev->ep_in);
Tatyana Brokhman31ac3522011-06-28 15:33:50 +02001124 if (ret) {
1125 ERROR(cdev, "failed to enable ep %s, result %d\n",
1126 dev->ep_in->name, ret);
Benoit Gobycf3fc062011-12-19 14:39:37 -08001127 return ret;
Tatyana Brokhman31ac3522011-06-28 15:33:50 +02001128 }
Benoit Gobycf3fc062011-12-19 14:39:37 -08001129
1130 ret = config_ep_by_speed(cdev->gadget, f, dev->ep_out);
Tatyana Brokhman31ac3522011-06-28 15:33:50 +02001131 if (ret) {
1132 dev->ep_out->desc = NULL;
1133 ERROR(cdev, "config_ep_by_speed failes for ep %s, result %d\n",
1134 dev->ep_out->name, ret);
1135 usb_ep_disable(dev->ep_in);
Benoit Gobycf3fc062011-12-19 14:39:37 -08001136 return ret;
Tatyana Brokhman31ac3522011-06-28 15:33:50 +02001137 }
Benoit Gobycf3fc062011-12-19 14:39:37 -08001138 ret = usb_ep_enable(dev->ep_out);
1139 if (ret) {
Tatyana Brokhman31ac3522011-06-28 15:33:50 +02001140 ERROR(cdev, "failed to enable ep %s, result %d\n",
1141 dev->ep_out->name, ret);
Benoit Gobycf3fc062011-12-19 14:39:37 -08001142 usb_ep_disable(dev->ep_in);
1143 return ret;
1144 }
1145
1146 dev->online = 1;
1147
1148 /* readers may be blocked waiting for us to go online */
1149 wake_up(&dev->read_wq);
1150 return 0;
1151}
1152
1153static void acc_function_disable(struct usb_function *f)
1154{
1155 struct acc_dev *dev = func_to_dev(f);
1156 struct usb_composite_dev *cdev = dev->cdev;
1157
1158 DBG(cdev, "acc_function_disable\n");
1159 acc_set_disconnected(dev);
1160 usb_ep_disable(dev->ep_in);
1161 usb_ep_disable(dev->ep_out);
1162
1163 /* readers may be blocked waiting for us to go online */
1164 wake_up(&dev->read_wq);
1165
1166 VDBG(cdev, "%s disabled\n", dev->function.name);
1167}
1168
1169static int acc_bind_config(struct usb_configuration *c)
1170{
1171 struct acc_dev *dev = _acc_dev;
1172 int ret;
1173
1174 printk(KERN_INFO "acc_bind_config\n");
1175
1176 /* allocate a string ID for our interface */
1177 if (acc_string_defs[INTERFACE_STRING_INDEX].id == 0) {
1178 ret = usb_string_id(c->cdev);
1179 if (ret < 0)
1180 return ret;
1181 acc_string_defs[INTERFACE_STRING_INDEX].id = ret;
1182 acc_interface_desc.iInterface = ret;
1183 }
1184
1185 dev->cdev = c->cdev;
1186 dev->function.name = "accessory";
1187 dev->function.strings = acc_strings,
1188 dev->function.descriptors = fs_acc_descs;
1189 dev->function.hs_descriptors = hs_acc_descs;
Pavankumar Kondeti6f94bc92012-08-03 09:34:32 +05301190 if (gadget_is_superspeed(c->cdev->gadget))
1191 dev->function.ss_descriptors = ss_acc_descs;
Benoit Gobycf3fc062011-12-19 14:39:37 -08001192 dev->function.bind = acc_function_bind;
1193 dev->function.unbind = acc_function_unbind;
1194 dev->function.set_alt = acc_function_set_alt;
1195 dev->function.disable = acc_function_disable;
1196
1197 return usb_add_function(c, &dev->function);
1198}
1199
1200static int acc_setup(void)
1201{
1202 struct acc_dev *dev;
1203 int ret;
1204
1205 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1206 if (!dev)
1207 return -ENOMEM;
1208
1209 spin_lock_init(&dev->lock);
1210 init_waitqueue_head(&dev->read_wq);
1211 init_waitqueue_head(&dev->write_wq);
1212 atomic_set(&dev->open_excl, 0);
1213 INIT_LIST_HEAD(&dev->tx_idle);
Mike Lockwoode7558bb2012-08-27 16:23:48 +05301214 INIT_LIST_HEAD(&dev->hid_list);
1215 INIT_LIST_HEAD(&dev->new_hid_list);
1216 INIT_LIST_HEAD(&dev->dead_hid_list);
1217 INIT_DELAYED_WORK(&dev->start_work, acc_start_work);
1218 INIT_WORK(&dev->hid_work, acc_hid_work);
Benoit Gobycf3fc062011-12-19 14:39:37 -08001219
1220 /* _acc_dev must be set before calling usb_gadget_register_driver */
1221 _acc_dev = dev;
1222
1223 ret = misc_register(&acc_device);
1224 if (ret)
1225 goto err;
1226
1227 return 0;
1228
1229err:
1230 kfree(dev);
Mike Lockwoode7558bb2012-08-27 16:23:48 +05301231 pr_err("USB accessory gadget driver failed to initialize\n");
Benoit Gobycf3fc062011-12-19 14:39:37 -08001232 return ret;
1233}
1234
Mike Lockwoode7558bb2012-08-27 16:23:48 +05301235static void acc_disconnect(void)
1236{
1237 /* unregister all HID devices if USB is disconnected */
1238 kill_all_hid_devices(_acc_dev);
1239}
1240
Benoit Gobycf3fc062011-12-19 14:39:37 -08001241static void acc_cleanup(void)
1242{
1243 misc_deregister(&acc_device);
1244 kfree(_acc_dev);
1245 _acc_dev = NULL;
1246}