2 * Gadget Function Driver for Android USB accessories
4 * Copyright (C) 2011 Google, Inc.
5 * Author: Mike Lockwood <lockwood@android.com>
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.
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.
19 /* #define VERBOSE_DEBUG */
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>
31 #include <linux/types.h>
32 #include <linux/file.h>
33 #include <linux/device.h>
34 #include <linux/miscdevice.h>
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>
42 #define BULK_BUFFER_SIZE 16384
43 #define ACC_STRING_SIZE 256
45 #define PROTOCOL_VERSION 2
48 #define INTERFACE_STRING_INDEX 0
50 /* number of tx and rx requests to allocate */
55 struct list_head list;
56 struct hid_device *hid;
58 /* accessory defined ID */
60 /* HID report descriptor */
62 /* length of HID report descriptor */
64 /* number of bytes of report_desc we have received so far */
65 int report_desc_offset;
69 struct usb_function function;
70 struct usb_composite_dev *cdev;
74 struct usb_ep *ep_out;
76 /* set to 1 when we connect */
78 /* Set to 1 when we disconnect.
79 * Not cleared until our file is closed.
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];
91 /* for acc_complete_set_string */
94 /* set to 1 if we have a pending start request */
99 /* synchronize access to our device file */
102 struct list_head tx_idle;
104 wait_queue_head_t read_wq;
105 wait_queue_head_t write_wq;
106 struct usb_request *rx_req[RX_REQ_MAX];
109 /* delayed work for handling ACCESSORY_START */
110 struct delayed_work start_work;
112 /* worker for registering and unregistering hid devices */
113 struct work_struct hid_work;
115 /* list of active HID devices */
116 struct list_head hid_list;
118 /* list of new HID devices to register */
119 struct list_head new_hid_list;
121 /* list of dead HID devices to unregister */
122 struct list_head dead_hid_list;
125 static struct usb_interface_descriptor acc_interface_desc = {
126 .bLength = USB_DT_INTERFACE_SIZE,
127 .bDescriptorType = USB_DT_INTERFACE,
128 .bInterfaceNumber = 0,
130 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
131 .bInterfaceSubClass = USB_SUBCLASS_VENDOR_SPEC,
132 .bInterfaceProtocol = 0,
135 static struct usb_endpoint_descriptor acc_highspeed_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(512),
143 static struct usb_endpoint_descriptor acc_highspeed_out_desc = {
144 .bLength = USB_DT_ENDPOINT_SIZE,
145 .bDescriptorType = USB_DT_ENDPOINT,
146 .bEndpointAddress = USB_DIR_OUT,
147 .bmAttributes = USB_ENDPOINT_XFER_BULK,
148 .wMaxPacketSize = __constant_cpu_to_le16(512),
151 static struct usb_endpoint_descriptor acc_fullspeed_in_desc = {
152 .bLength = USB_DT_ENDPOINT_SIZE,
153 .bDescriptorType = USB_DT_ENDPOINT,
154 .bEndpointAddress = USB_DIR_IN,
155 .bmAttributes = USB_ENDPOINT_XFER_BULK,
158 static struct usb_endpoint_descriptor acc_fullspeed_out_desc = {
159 .bLength = USB_DT_ENDPOINT_SIZE,
160 .bDescriptorType = USB_DT_ENDPOINT,
161 .bEndpointAddress = USB_DIR_OUT,
162 .bmAttributes = USB_ENDPOINT_XFER_BULK,
165 static struct usb_descriptor_header *fs_acc_descs[] = {
166 (struct usb_descriptor_header *) &acc_interface_desc,
167 (struct usb_descriptor_header *) &acc_fullspeed_in_desc,
168 (struct usb_descriptor_header *) &acc_fullspeed_out_desc,
172 static struct usb_descriptor_header *hs_acc_descs[] = {
173 (struct usb_descriptor_header *) &acc_interface_desc,
174 (struct usb_descriptor_header *) &acc_highspeed_in_desc,
175 (struct usb_descriptor_header *) &acc_highspeed_out_desc,
179 static struct usb_string acc_string_defs[] = {
180 [INTERFACE_STRING_INDEX].s = "Android Accessory Interface",
181 { }, /* end of list */
184 static struct usb_gadget_strings acc_string_table = {
185 .language = 0x0409, /* en-US */
186 .strings = acc_string_defs,
189 static struct usb_gadget_strings *acc_strings[] = {
194 /* temporary variable used between acc_open() and acc_gadget_bind() */
195 static struct acc_dev *_acc_dev;
197 static inline struct acc_dev *func_to_dev(struct usb_function *f)
199 return container_of(f, struct acc_dev, function);
202 static struct usb_request *acc_request_new(struct usb_ep *ep, int buffer_size)
204 struct usb_request *req = usb_ep_alloc_request(ep, GFP_KERNEL);
208 /* now allocate buffers for the requests */
209 req->buf = kmalloc(buffer_size, GFP_KERNEL);
211 usb_ep_free_request(ep, req);
218 static void acc_request_free(struct usb_request *req, struct usb_ep *ep)
222 usb_ep_free_request(ep, req);
226 /* add a request to the tail of a list */
227 static void req_put(struct acc_dev *dev, struct list_head *head,
228 struct usb_request *req)
232 spin_lock_irqsave(&dev->lock, flags);
233 list_add_tail(&req->list, head);
234 spin_unlock_irqrestore(&dev->lock, flags);
237 /* remove a request from the head of a list */
238 static struct usb_request *req_get(struct acc_dev *dev, struct list_head *head)
241 struct usb_request *req;
243 spin_lock_irqsave(&dev->lock, flags);
244 if (list_empty(head)) {
247 req = list_first_entry(head, struct usb_request, list);
248 list_del(&req->list);
250 spin_unlock_irqrestore(&dev->lock, flags);
254 static void acc_set_disconnected(struct acc_dev *dev)
257 dev->disconnected = 1;
260 static void acc_complete_in(struct usb_ep *ep, struct usb_request *req)
262 struct acc_dev *dev = _acc_dev;
264 if (req->status != 0)
265 acc_set_disconnected(dev);
267 req_put(dev, &dev->tx_idle, req);
269 wake_up(&dev->write_wq);
272 static void acc_complete_out(struct usb_ep *ep, struct usb_request *req)
274 struct acc_dev *dev = _acc_dev;
277 if (req->status != 0)
278 acc_set_disconnected(dev);
280 wake_up(&dev->read_wq);
283 static void acc_complete_set_string(struct usb_ep *ep, struct usb_request *req)
285 struct acc_dev *dev = ep->driver_data;
286 char *string_dest = NULL;
287 int length = req->actual;
289 if (req->status != 0) {
290 pr_err("acc_complete_set_string, err %d\n", req->status);
294 switch (dev->string_index) {
295 case ACCESSORY_STRING_MANUFACTURER:
296 string_dest = dev->manufacturer;
298 case ACCESSORY_STRING_MODEL:
299 string_dest = dev->model;
301 case ACCESSORY_STRING_DESCRIPTION:
302 string_dest = dev->description;
304 case ACCESSORY_STRING_VERSION:
305 string_dest = dev->version;
307 case ACCESSORY_STRING_URI:
308 string_dest = dev->uri;
310 case ACCESSORY_STRING_SERIAL:
311 string_dest = dev->serial;
317 if (length >= ACC_STRING_SIZE)
318 length = ACC_STRING_SIZE - 1;
320 spin_lock_irqsave(&dev->lock, flags);
321 memcpy(string_dest, req->buf, length);
322 /* ensure zero termination */
323 string_dest[length] = 0;
324 spin_unlock_irqrestore(&dev->lock, flags);
326 pr_err("unknown accessory string index %d\n",
331 static void acc_complete_set_hid_report_desc(struct usb_ep *ep,
332 struct usb_request *req)
334 struct acc_hid_dev *hid = req->context;
335 struct acc_dev *dev = hid->dev;
336 int length = req->actual;
338 if (req->status != 0) {
339 pr_err("acc_complete_set_hid_report_desc, err %d\n",
344 memcpy(hid->report_desc + hid->report_desc_offset, req->buf, length);
345 hid->report_desc_offset += length;
346 if (hid->report_desc_offset == hid->report_desc_len) {
347 /* After we have received the entire report descriptor
348 * we schedule work to initialize the HID device
350 schedule_work(&dev->hid_work);
354 static void acc_complete_send_hid_event(struct usb_ep *ep,
355 struct usb_request *req)
357 struct acc_hid_dev *hid = req->context;
358 int length = req->actual;
360 if (req->status != 0) {
361 pr_err("acc_complete_send_hid_event, err %d\n", req->status);
365 hid_report_raw_event(hid->hid, HID_INPUT_REPORT, req->buf, length, 1);
368 static int acc_hid_parse(struct hid_device *hid)
370 struct acc_hid_dev *hdev = hid->driver_data;
372 hid_parse_report(hid, hdev->report_desc, hdev->report_desc_len);
376 static int acc_hid_start(struct hid_device *hid)
381 static void acc_hid_stop(struct hid_device *hid)
385 static int acc_hid_open(struct hid_device *hid)
390 static void acc_hid_close(struct hid_device *hid)
394 static struct hid_ll_driver acc_hid_ll_driver = {
395 .parse = acc_hid_parse,
396 .start = acc_hid_start,
397 .stop = acc_hid_stop,
398 .open = acc_hid_open,
399 .close = acc_hid_close,
402 static struct acc_hid_dev *acc_hid_new(struct acc_dev *dev,
403 int id, int desc_len)
405 struct acc_hid_dev *hdev;
407 hdev = kzalloc(sizeof(*hdev), GFP_ATOMIC);
410 hdev->report_desc = kzalloc(desc_len, GFP_ATOMIC);
411 if (!hdev->report_desc) {
417 hdev->report_desc_len = desc_len;
422 static struct acc_hid_dev *acc_hid_get(struct list_head *list, int id)
424 struct acc_hid_dev *hid;
426 list_for_each_entry(hid, list, list) {
433 static int acc_register_hid(struct acc_dev *dev, int id, int desc_length)
435 struct acc_hid_dev *hid;
438 /* report descriptor length must be > 0 */
439 if (desc_length <= 0)
442 spin_lock_irqsave(&dev->lock, flags);
443 /* replace HID if one already exists with this ID */
444 hid = acc_hid_get(&dev->hid_list, id);
446 hid = acc_hid_get(&dev->new_hid_list, id);
448 list_move(&hid->list, &dev->dead_hid_list);
450 hid = acc_hid_new(dev, id, desc_length);
452 spin_unlock_irqrestore(&dev->lock, flags);
456 list_add(&hid->list, &dev->new_hid_list);
457 spin_unlock_irqrestore(&dev->lock, flags);
459 /* schedule work to register the HID device */
460 schedule_work(&dev->hid_work);
464 static int acc_unregister_hid(struct acc_dev *dev, int id)
466 struct acc_hid_dev *hid;
469 spin_lock_irqsave(&dev->lock, flags);
470 hid = acc_hid_get(&dev->hid_list, id);
472 hid = acc_hid_get(&dev->new_hid_list, id);
474 spin_unlock_irqrestore(&dev->lock, flags);
478 list_move(&hid->list, &dev->dead_hid_list);
479 spin_unlock_irqrestore(&dev->lock, flags);
481 schedule_work(&dev->hid_work);
485 static int create_bulk_endpoints(struct acc_dev *dev,
486 struct usb_endpoint_descriptor *in_desc,
487 struct usb_endpoint_descriptor *out_desc)
489 struct usb_composite_dev *cdev = dev->cdev;
490 struct usb_request *req;
494 DBG(cdev, "create_bulk_endpoints dev: %p\n", dev);
496 ep = usb_ep_autoconfig(cdev->gadget, in_desc);
498 DBG(cdev, "usb_ep_autoconfig for ep_in failed\n");
501 DBG(cdev, "usb_ep_autoconfig for ep_in got %s\n", ep->name);
502 ep->driver_data = dev; /* claim the endpoint */
505 ep = usb_ep_autoconfig(cdev->gadget, out_desc);
507 DBG(cdev, "usb_ep_autoconfig for ep_out failed\n");
510 DBG(cdev, "usb_ep_autoconfig for ep_out got %s\n", ep->name);
511 ep->driver_data = dev; /* claim the endpoint */
514 ep = usb_ep_autoconfig(cdev->gadget, out_desc);
516 DBG(cdev, "usb_ep_autoconfig for ep_out failed\n");
519 DBG(cdev, "usb_ep_autoconfig for ep_out got %s\n", ep->name);
520 ep->driver_data = dev; /* claim the endpoint */
523 /* now allocate requests for our endpoints */
524 for (i = 0; i < TX_REQ_MAX; i++) {
525 req = acc_request_new(dev->ep_in, BULK_BUFFER_SIZE);
528 req->complete = acc_complete_in;
529 req_put(dev, &dev->tx_idle, req);
531 for (i = 0; i < RX_REQ_MAX; i++) {
532 req = acc_request_new(dev->ep_out, BULK_BUFFER_SIZE);
535 req->complete = acc_complete_out;
536 dev->rx_req[i] = req;
542 pr_err("acc_bind() could not allocate requests\n");
543 while ((req = req_get(dev, &dev->tx_idle)))
544 acc_request_free(req, dev->ep_in);
545 for (i = 0; i < RX_REQ_MAX; i++)
546 acc_request_free(dev->rx_req[i], dev->ep_out);
550 static ssize_t acc_read(struct file *fp, char __user *buf,
551 size_t count, loff_t *pos)
553 struct acc_dev *dev = fp->private_data;
554 struct usb_request *req;
558 pr_debug("acc_read(%d)\n", count);
560 if (dev->disconnected)
563 if (count > BULK_BUFFER_SIZE)
564 count = BULK_BUFFER_SIZE;
566 /* we will block until we're online */
567 pr_debug("acc_read: waiting for online\n");
568 ret = wait_event_interruptible(dev->read_wq, dev->online);
575 /* queue a request */
576 req = dev->rx_req[0];
579 ret = usb_ep_queue(dev->ep_out, req, GFP_KERNEL);
584 pr_debug("rx %p queue\n", req);
587 /* wait for a request to complete */
588 ret = wait_event_interruptible(dev->read_wq, dev->rx_done);
591 usb_ep_dequeue(dev->ep_out, req);
595 /* If we got a 0-len packet, throw it back and try again. */
596 if (req->actual == 0)
599 pr_debug("rx %p %d\n", req, req->actual);
600 xfer = (req->actual < count) ? req->actual : count;
602 if (copy_to_user(buf, req->buf, xfer))
608 pr_debug("acc_read returning %d\n", r);
612 static ssize_t acc_write(struct file *fp, const char __user *buf,
613 size_t count, loff_t *pos)
615 struct acc_dev *dev = fp->private_data;
616 struct usb_request *req = 0;
620 pr_debug("acc_write(%d)\n", count);
622 if (!dev->online || dev->disconnected)
627 pr_debug("acc_write dev->error\n");
632 /* get an idle tx request to use */
634 ret = wait_event_interruptible(dev->write_wq,
635 ((req = req_get(dev, &dev->tx_idle)) || !dev->online));
641 if (count > BULK_BUFFER_SIZE)
642 xfer = BULK_BUFFER_SIZE;
645 if (copy_from_user(req->buf, buf, xfer)) {
651 ret = usb_ep_queue(dev->ep_in, req, GFP_KERNEL);
653 pr_debug("acc_write: xfer error %d\n", ret);
661 /* zero this so we don't try to free it on error exit */
666 req_put(dev, &dev->tx_idle, req);
668 pr_debug("acc_write returning %d\n", r);
672 static long acc_ioctl(struct file *fp, unsigned code, unsigned long value)
674 struct acc_dev *dev = fp->private_data;
679 case ACCESSORY_GET_STRING_MANUFACTURER:
680 src = dev->manufacturer;
682 case ACCESSORY_GET_STRING_MODEL:
685 case ACCESSORY_GET_STRING_DESCRIPTION:
686 src = dev->description;
688 case ACCESSORY_GET_STRING_VERSION:
691 case ACCESSORY_GET_STRING_URI:
694 case ACCESSORY_GET_STRING_SERIAL:
697 case ACCESSORY_IS_START_REQUESTED:
698 return dev->start_requested;
699 case ACCESSORY_GET_AUDIO_MODE:
700 return dev->audio_mode;
705 ret = strlen(src) + 1;
706 if (copy_to_user((void __user *)value, src, ret))
711 static int acc_open(struct inode *ip, struct file *fp)
713 printk(KERN_INFO "acc_open\n");
714 if (atomic_xchg(&_acc_dev->open_excl, 1))
717 _acc_dev->disconnected = 0;
718 fp->private_data = _acc_dev;
722 static int acc_release(struct inode *ip, struct file *fp)
724 printk(KERN_INFO "acc_release\n");
726 WARN_ON(!atomic_xchg(&_acc_dev->open_excl, 0));
727 _acc_dev->disconnected = 0;
731 /* file operations for /dev/usb_accessory */
732 static const struct file_operations acc_fops = {
733 .owner = THIS_MODULE,
736 .unlocked_ioctl = acc_ioctl,
738 .release = acc_release,
741 static int acc_hid_probe(struct hid_device *hdev,
742 const struct hid_device_id *id)
746 ret = hid_parse(hdev);
749 return hid_hw_start(hdev, HID_CONNECT_DEFAULT);
752 static struct miscdevice acc_device = {
753 .minor = MISC_DYNAMIC_MINOR,
754 .name = "usb_accessory",
758 static const struct hid_device_id acc_hid_table[] = {
759 { HID_USB_DEVICE(HID_ANY_ID, HID_ANY_ID) },
763 static struct hid_driver acc_hid_driver = {
764 .name = "USB accessory",
765 .id_table = acc_hid_table,
766 .probe = acc_hid_probe,
769 static int acc_ctrlrequest(struct usb_composite_dev *cdev,
770 const struct usb_ctrlrequest *ctrl)
772 struct acc_dev *dev = _acc_dev;
773 int value = -EOPNOTSUPP;
774 struct acc_hid_dev *hid;
776 u8 b_requestType = ctrl->bRequestType;
777 u8 b_request = ctrl->bRequest;
778 u16 w_index = le16_to_cpu(ctrl->wIndex);
779 u16 w_value = le16_to_cpu(ctrl->wValue);
780 u16 w_length = le16_to_cpu(ctrl->wLength);
784 printk(KERN_INFO "acc_ctrlrequest "
785 "%02x.%02x v%04x i%04x l%u\n",
786 b_requestType, b_request,
787 w_value, w_index, w_length);
790 if (b_requestType == (USB_DIR_OUT | USB_TYPE_VENDOR)) {
791 if (b_request == ACCESSORY_START) {
792 dev->start_requested = 1;
793 schedule_delayed_work(
794 &dev->start_work, msecs_to_jiffies(10));
796 } else if (b_request == ACCESSORY_SEND_STRING) {
797 dev->string_index = w_index;
798 cdev->gadget->ep0->driver_data = dev;
799 cdev->req->complete = acc_complete_set_string;
801 } else if (b_request == ACCESSORY_SET_AUDIO_MODE &&
802 w_index == 0 && w_length == 0) {
803 dev->audio_mode = w_value;
805 } else if (b_request == ACCESSORY_REGISTER_HID) {
806 value = acc_register_hid(dev, w_value, w_index);
807 } else if (b_request == ACCESSORY_UNREGISTER_HID) {
808 value = acc_unregister_hid(dev, w_value);
809 } else if (b_request == ACCESSORY_SET_HID_REPORT_DESC) {
810 spin_lock_irqsave(&dev->lock, flags);
811 hid = acc_hid_get(&dev->new_hid_list, w_value);
812 spin_unlock_irqrestore(&dev->lock, flags);
818 if (offset != hid->report_desc_offset
819 || offset + w_length > hid->report_desc_len) {
823 cdev->req->context = hid;
824 cdev->req->complete = acc_complete_set_hid_report_desc;
826 } else if (b_request == ACCESSORY_SEND_HID_EVENT) {
827 spin_lock_irqsave(&dev->lock, flags);
828 hid = acc_hid_get(&dev->hid_list, w_value);
829 spin_unlock_irqrestore(&dev->lock, flags);
834 cdev->req->context = hid;
835 cdev->req->complete = acc_complete_send_hid_event;
838 } else if (b_requestType == (USB_DIR_IN | USB_TYPE_VENDOR)) {
839 if (b_request == ACCESSORY_GET_PROTOCOL) {
840 *((u16 *)cdev->req->buf) = PROTOCOL_VERSION;
843 /* clear any string left over from a previous session */
844 memset(dev->manufacturer, 0, sizeof(dev->manufacturer));
845 memset(dev->model, 0, sizeof(dev->model));
846 memset(dev->description, 0, sizeof(dev->description));
847 memset(dev->version, 0, sizeof(dev->version));
848 memset(dev->uri, 0, sizeof(dev->uri));
849 memset(dev->serial, 0, sizeof(dev->serial));
850 dev->start_requested = 0;
857 cdev->req->length = value;
858 value = usb_ep_queue(cdev->gadget->ep0, cdev->req, GFP_ATOMIC);
860 ERROR(cdev, "%s setup response queue error\n",
865 if (value == -EOPNOTSUPP)
867 "unknown class-specific control req "
868 "%02x.%02x v%04x i%04x l%u\n",
869 ctrl->bRequestType, ctrl->bRequest,
870 w_value, w_index, w_length);
875 acc_function_bind(struct usb_configuration *c, struct usb_function *f)
877 struct usb_composite_dev *cdev = c->cdev;
878 struct acc_dev *dev = func_to_dev(f);
882 DBG(cdev, "acc_function_bind dev: %p\n", dev);
884 ret = hid_register_driver(&acc_hid_driver);
888 dev->start_requested = 0;
890 /* allocate interface ID(s) */
891 id = usb_interface_id(c, f);
894 acc_interface_desc.bInterfaceNumber = id;
896 /* allocate endpoints */
897 ret = create_bulk_endpoints(dev, &acc_fullspeed_in_desc,
898 &acc_fullspeed_out_desc);
902 /* support high speed hardware */
903 if (gadget_is_dualspeed(c->cdev->gadget)) {
904 acc_highspeed_in_desc.bEndpointAddress =
905 acc_fullspeed_in_desc.bEndpointAddress;
906 acc_highspeed_out_desc.bEndpointAddress =
907 acc_fullspeed_out_desc.bEndpointAddress;
910 DBG(cdev, "%s speed %s: IN/%s, OUT/%s\n",
911 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
912 f->name, dev->ep_in->name, dev->ep_out->name);
917 kill_all_hid_devices(struct acc_dev *dev)
919 struct acc_hid_dev *hid;
920 struct list_head *entry, *temp;
923 spin_lock_irqsave(&dev->lock, flags);
924 list_for_each_safe(entry, temp, &dev->hid_list) {
925 hid = list_entry(entry, struct acc_hid_dev, list);
926 list_del(&hid->list);
927 list_add(&hid->list, &dev->dead_hid_list);
929 list_for_each_safe(entry, temp, &dev->new_hid_list) {
930 hid = list_entry(entry, struct acc_hid_dev, list);
931 list_del(&hid->list);
932 list_add(&hid->list, &dev->dead_hid_list);
934 spin_unlock_irqrestore(&dev->lock, flags);
936 schedule_work(&dev->hid_work);
940 acc_hid_unbind(struct acc_dev *dev)
942 hid_unregister_driver(&acc_hid_driver);
943 kill_all_hid_devices(dev);
947 acc_function_unbind(struct usb_configuration *c, struct usb_function *f)
949 struct acc_dev *dev = func_to_dev(f);
950 struct usb_request *req;
953 while ((req = req_get(dev, &dev->tx_idle)))
954 acc_request_free(req, dev->ep_in);
955 for (i = 0; i < RX_REQ_MAX; i++)
956 acc_request_free(dev->rx_req[i], dev->ep_out);
961 static void acc_start_work(struct work_struct *data)
963 char *envp[2] = { "ACCESSORY=START", NULL };
964 kobject_uevent_env(&acc_device.this_device->kobj, KOBJ_CHANGE, envp);
967 static int acc_hid_init(struct acc_hid_dev *hdev)
969 struct hid_device *hid;
972 hid = hid_allocate_device();
976 hid->ll_driver = &acc_hid_ll_driver;
977 hid->dev.parent = acc_device.this_device;
980 hid->vendor = HID_ANY_ID;
981 hid->product = HID_ANY_ID;
982 hid->driver_data = hdev;
983 ret = hid_add_device(hid);
985 pr_err("can't add hid device: %d\n", ret);
986 hid_destroy_device(hid);
994 static void acc_hid_delete(struct acc_hid_dev *hid)
996 kfree(hid->report_desc);
1000 static void acc_hid_work(struct work_struct *data)
1002 struct acc_dev *dev = _acc_dev;
1003 struct list_head *entry, *temp;
1004 struct acc_hid_dev *hid;
1005 struct list_head new_list, dead_list;
1006 unsigned long flags;
1008 INIT_LIST_HEAD(&new_list);
1010 spin_lock_irqsave(&dev->lock, flags);
1012 /* copy hids that are ready for initialization to new_list */
1013 list_for_each_safe(entry, temp, &dev->new_hid_list) {
1014 hid = list_entry(entry, struct acc_hid_dev, list);
1015 if (hid->report_desc_offset == hid->report_desc_len)
1016 list_move(&hid->list, &new_list);
1019 if (list_empty(&dev->dead_hid_list)) {
1020 INIT_LIST_HEAD(&dead_list);
1022 /* move all of dev->dead_hid_list to dead_list */
1023 dead_list.prev = dev->dead_hid_list.prev;
1024 dead_list.next = dev->dead_hid_list.next;
1025 dead_list.next->prev = &dead_list;
1026 dead_list.prev->next = &dead_list;
1027 INIT_LIST_HEAD(&dev->dead_hid_list);
1030 spin_unlock_irqrestore(&dev->lock, flags);
1032 /* register new HID devices */
1033 list_for_each_safe(entry, temp, &new_list) {
1034 hid = list_entry(entry, struct acc_hid_dev, list);
1035 if (acc_hid_init(hid)) {
1036 pr_err("can't add HID device %p\n", hid);
1037 acc_hid_delete(hid);
1039 spin_lock_irqsave(&dev->lock, flags);
1040 list_move(&hid->list, &dev->hid_list);
1041 spin_unlock_irqrestore(&dev->lock, flags);
1045 /* remove dead HID devices */
1046 list_for_each_safe(entry, temp, &dead_list) {
1047 hid = list_entry(entry, struct acc_hid_dev, list);
1048 list_del(&hid->list);
1050 hid_destroy_device(hid->hid);
1051 acc_hid_delete(hid);
1055 static int acc_function_set_alt(struct usb_function *f,
1056 unsigned intf, unsigned alt)
1058 struct acc_dev *dev = func_to_dev(f);
1059 struct usb_composite_dev *cdev = f->config->cdev;
1062 DBG(cdev, "acc_function_set_alt intf: %d alt: %d\n", intf, alt);
1064 ret = config_ep_by_speed(cdev->gadget, f, dev->ep_in);
1068 ret = usb_ep_enable(dev->ep_in);
1072 ret = config_ep_by_speed(cdev->gadget, f, dev->ep_out);
1076 ret = usb_ep_enable(dev->ep_out);
1078 usb_ep_disable(dev->ep_in);
1084 /* readers may be blocked waiting for us to go online */
1085 wake_up(&dev->read_wq);
1089 static void acc_function_disable(struct usb_function *f)
1091 struct acc_dev *dev = func_to_dev(f);
1092 struct usb_composite_dev *cdev = dev->cdev;
1094 DBG(cdev, "acc_function_disable\n");
1095 acc_set_disconnected(dev);
1096 usb_ep_disable(dev->ep_in);
1097 usb_ep_disable(dev->ep_out);
1099 /* readers may be blocked waiting for us to go online */
1100 wake_up(&dev->read_wq);
1102 VDBG(cdev, "%s disabled\n", dev->function.name);
1105 static int acc_bind_config(struct usb_configuration *c)
1107 struct acc_dev *dev = _acc_dev;
1110 printk(KERN_INFO "acc_bind_config\n");
1112 /* allocate a string ID for our interface */
1113 if (acc_string_defs[INTERFACE_STRING_INDEX].id == 0) {
1114 ret = usb_string_id(c->cdev);
1117 acc_string_defs[INTERFACE_STRING_INDEX].id = ret;
1118 acc_interface_desc.iInterface = ret;
1121 dev->cdev = c->cdev;
1122 dev->function.name = "accessory";
1123 dev->function.strings = acc_strings,
1124 dev->function.descriptors = fs_acc_descs;
1125 dev->function.hs_descriptors = hs_acc_descs;
1126 dev->function.bind = acc_function_bind;
1127 dev->function.unbind = acc_function_unbind;
1128 dev->function.set_alt = acc_function_set_alt;
1129 dev->function.disable = acc_function_disable;
1131 return usb_add_function(c, &dev->function);
1134 static int acc_setup(void)
1136 struct acc_dev *dev;
1139 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1143 spin_lock_init(&dev->lock);
1144 init_waitqueue_head(&dev->read_wq);
1145 init_waitqueue_head(&dev->write_wq);
1146 atomic_set(&dev->open_excl, 0);
1147 INIT_LIST_HEAD(&dev->tx_idle);
1148 INIT_LIST_HEAD(&dev->hid_list);
1149 INIT_LIST_HEAD(&dev->new_hid_list);
1150 INIT_LIST_HEAD(&dev->dead_hid_list);
1151 INIT_DELAYED_WORK(&dev->start_work, acc_start_work);
1152 INIT_WORK(&dev->hid_work, acc_hid_work);
1154 /* _acc_dev must be set before calling usb_gadget_register_driver */
1157 ret = misc_register(&acc_device);
1165 pr_err("USB accessory gadget driver failed to initialize\n");
1169 static void acc_disconnect(void)
1171 /* unregister all HID devices if USB is disconnected */
1172 kill_all_hid_devices(_acc_dev);
1175 static void acc_cleanup(void)
1177 misc_deregister(&acc_device);