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