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 #include <linux/configfs.h>
43 #include <linux/usb/composite.h>
45 #define MAX_INST_NAME_LEN 40
46 #define BULK_BUFFER_SIZE 16384
47 #define ACC_STRING_SIZE 256
49 #define PROTOCOL_VERSION 2
52 #define INTERFACE_STRING_INDEX 0
54 /* number of tx and rx requests to allocate */
59 struct list_head list;
60 struct hid_device *hid;
62 /* accessory defined ID */
64 /* HID report descriptor */
66 /* length of HID report descriptor */
68 /* number of bytes of report_desc we have received so far */
69 int report_desc_offset;
73 struct usb_function function;
74 struct usb_composite_dev *cdev;
78 struct usb_ep *ep_out;
80 /* set to 1 when we connect */
82 /* Set to 1 when we disconnect.
83 * Not cleared until our file is closed.
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];
95 /* for acc_complete_set_string */
98 /* set to 1 if we have a pending start request */
103 /* synchronize access to our device file */
106 struct list_head tx_idle;
108 wait_queue_head_t read_wq;
109 wait_queue_head_t write_wq;
110 struct usb_request *rx_req[RX_REQ_MAX];
113 /* delayed work for handling ACCESSORY_START */
114 struct delayed_work start_work;
116 /* worker for registering and unregistering hid devices */
117 struct work_struct hid_work;
119 /* list of active HID devices */
120 struct list_head hid_list;
122 /* list of new HID devices to register */
123 struct list_head new_hid_list;
125 /* list of dead HID devices to unregister */
126 struct list_head dead_hid_list;
129 static struct usb_interface_descriptor acc_interface_desc = {
130 .bLength = USB_DT_INTERFACE_SIZE,
131 .bDescriptorType = USB_DT_INTERFACE,
132 .bInterfaceNumber = 0,
134 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
135 .bInterfaceSubClass = USB_SUBCLASS_VENDOR_SPEC,
136 .bInterfaceProtocol = 0,
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),
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),
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,
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,
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,
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,
183 static struct usb_string acc_string_defs[] = {
184 [INTERFACE_STRING_INDEX].s = "Android Accessory Interface",
185 { }, /* end of list */
188 static struct usb_gadget_strings acc_string_table = {
189 .language = 0x0409, /* en-US */
190 .strings = acc_string_defs,
193 static struct usb_gadget_strings *acc_strings[] = {
198 /* temporary variable used between acc_open() and acc_gadget_bind() */
199 static struct acc_dev *_acc_dev;
201 struct acc_instance {
202 struct usb_function_instance func_inst;
206 static inline struct acc_dev *func_to_dev(struct usb_function *f)
208 return container_of(f, struct acc_dev, function);
211 static struct usb_request *acc_request_new(struct usb_ep *ep, int buffer_size)
213 struct usb_request *req = usb_ep_alloc_request(ep, GFP_KERNEL);
217 /* now allocate buffers for the requests */
218 req->buf = kmalloc(buffer_size, GFP_KERNEL);
220 usb_ep_free_request(ep, req);
227 static void acc_request_free(struct usb_request *req, struct usb_ep *ep)
231 usb_ep_free_request(ep, req);
235 /* add a request to the tail of a list */
236 static void req_put(struct acc_dev *dev, struct list_head *head,
237 struct usb_request *req)
241 spin_lock_irqsave(&dev->lock, flags);
242 list_add_tail(&req->list, head);
243 spin_unlock_irqrestore(&dev->lock, flags);
246 /* remove a request from the head of a list */
247 static struct usb_request *req_get(struct acc_dev *dev, struct list_head *head)
250 struct usb_request *req;
252 spin_lock_irqsave(&dev->lock, flags);
253 if (list_empty(head)) {
256 req = list_first_entry(head, struct usb_request, list);
257 list_del(&req->list);
259 spin_unlock_irqrestore(&dev->lock, flags);
263 static void acc_set_disconnected(struct acc_dev *dev)
266 dev->disconnected = 1;
269 static void acc_complete_in(struct usb_ep *ep, struct usb_request *req)
271 struct acc_dev *dev = _acc_dev;
273 if (req->status == -ESHUTDOWN) {
274 pr_debug("acc_complete_in set disconnected");
275 acc_set_disconnected(dev);
278 req_put(dev, &dev->tx_idle, req);
280 wake_up(&dev->write_wq);
283 static void acc_complete_out(struct usb_ep *ep, struct usb_request *req)
285 struct acc_dev *dev = _acc_dev;
288 if (req->status == -ESHUTDOWN) {
289 pr_debug("acc_complete_out set disconnected");
290 acc_set_disconnected(dev);
293 wake_up(&dev->read_wq);
296 static void acc_complete_set_string(struct usb_ep *ep, struct usb_request *req)
298 struct acc_dev *dev = ep->driver_data;
299 char *string_dest = NULL;
300 int length = req->actual;
302 if (req->status != 0) {
303 pr_err("acc_complete_set_string, err %d\n", req->status);
307 switch (dev->string_index) {
308 case ACCESSORY_STRING_MANUFACTURER:
309 string_dest = dev->manufacturer;
311 case ACCESSORY_STRING_MODEL:
312 string_dest = dev->model;
314 case ACCESSORY_STRING_DESCRIPTION:
315 string_dest = dev->description;
317 case ACCESSORY_STRING_VERSION:
318 string_dest = dev->version;
320 case ACCESSORY_STRING_URI:
321 string_dest = dev->uri;
323 case ACCESSORY_STRING_SERIAL:
324 string_dest = dev->serial;
330 if (length >= ACC_STRING_SIZE)
331 length = ACC_STRING_SIZE - 1;
333 spin_lock_irqsave(&dev->lock, flags);
334 memcpy(string_dest, req->buf, length);
335 /* ensure zero termination */
336 string_dest[length] = 0;
337 spin_unlock_irqrestore(&dev->lock, flags);
339 pr_err("unknown accessory string index %d\n",
344 static void acc_complete_set_hid_report_desc(struct usb_ep *ep,
345 struct usb_request *req)
347 struct acc_hid_dev *hid = req->context;
348 struct acc_dev *dev = hid->dev;
349 int length = req->actual;
351 if (req->status != 0) {
352 pr_err("acc_complete_set_hid_report_desc, err %d\n",
357 memcpy(hid->report_desc + hid->report_desc_offset, req->buf, length);
358 hid->report_desc_offset += length;
359 if (hid->report_desc_offset == hid->report_desc_len) {
360 /* After we have received the entire report descriptor
361 * we schedule work to initialize the HID device
363 schedule_work(&dev->hid_work);
367 static void acc_complete_send_hid_event(struct usb_ep *ep,
368 struct usb_request *req)
370 struct acc_hid_dev *hid = req->context;
371 int length = req->actual;
373 if (req->status != 0) {
374 pr_err("acc_complete_send_hid_event, err %d\n", req->status);
378 hid_report_raw_event(hid->hid, HID_INPUT_REPORT, req->buf, length, 1);
381 static int acc_hid_parse(struct hid_device *hid)
383 struct acc_hid_dev *hdev = hid->driver_data;
385 hid_parse_report(hid, hdev->report_desc, hdev->report_desc_len);
389 static int acc_hid_start(struct hid_device *hid)
394 static void acc_hid_stop(struct hid_device *hid)
398 static int acc_hid_open(struct hid_device *hid)
403 static void acc_hid_close(struct hid_device *hid)
407 static int acc_hid_raw_request(struct hid_device *hid, unsigned char reportnum,
408 __u8 *buf, size_t len, unsigned char rtype, int reqtype)
413 static struct hid_ll_driver acc_hid_ll_driver = {
414 .parse = acc_hid_parse,
415 .start = acc_hid_start,
416 .stop = acc_hid_stop,
417 .open = acc_hid_open,
418 .close = acc_hid_close,
419 .raw_request = acc_hid_raw_request,
422 static struct acc_hid_dev *acc_hid_new(struct acc_dev *dev,
423 int id, int desc_len)
425 struct acc_hid_dev *hdev;
427 hdev = kzalloc(sizeof(*hdev), GFP_ATOMIC);
430 hdev->report_desc = kzalloc(desc_len, GFP_ATOMIC);
431 if (!hdev->report_desc) {
437 hdev->report_desc_len = desc_len;
442 static struct acc_hid_dev *acc_hid_get(struct list_head *list, int id)
444 struct acc_hid_dev *hid;
446 list_for_each_entry(hid, list, list) {
453 static int acc_register_hid(struct acc_dev *dev, int id, int desc_length)
455 struct acc_hid_dev *hid;
458 /* report descriptor length must be > 0 */
459 if (desc_length <= 0)
462 spin_lock_irqsave(&dev->lock, flags);
463 /* replace HID if one already exists with this ID */
464 hid = acc_hid_get(&dev->hid_list, id);
466 hid = acc_hid_get(&dev->new_hid_list, id);
468 list_move(&hid->list, &dev->dead_hid_list);
470 hid = acc_hid_new(dev, id, desc_length);
472 spin_unlock_irqrestore(&dev->lock, flags);
476 list_add(&hid->list, &dev->new_hid_list);
477 spin_unlock_irqrestore(&dev->lock, flags);
479 /* schedule work to register the HID device */
480 schedule_work(&dev->hid_work);
484 static int acc_unregister_hid(struct acc_dev *dev, int id)
486 struct acc_hid_dev *hid;
489 spin_lock_irqsave(&dev->lock, flags);
490 hid = acc_hid_get(&dev->hid_list, id);
492 hid = acc_hid_get(&dev->new_hid_list, id);
494 spin_unlock_irqrestore(&dev->lock, flags);
498 list_move(&hid->list, &dev->dead_hid_list);
499 spin_unlock_irqrestore(&dev->lock, flags);
501 schedule_work(&dev->hid_work);
505 static int create_bulk_endpoints(struct acc_dev *dev,
506 struct usb_endpoint_descriptor *in_desc,
507 struct usb_endpoint_descriptor *out_desc)
509 struct usb_composite_dev *cdev = dev->cdev;
510 struct usb_request *req;
514 DBG(cdev, "create_bulk_endpoints dev: %p\n", dev);
516 ep = usb_ep_autoconfig(cdev->gadget, in_desc);
518 DBG(cdev, "usb_ep_autoconfig for ep_in failed\n");
521 DBG(cdev, "usb_ep_autoconfig for ep_in got %s\n", ep->name);
522 ep->driver_data = dev; /* claim the endpoint */
525 ep = usb_ep_autoconfig(cdev->gadget, out_desc);
527 DBG(cdev, "usb_ep_autoconfig for ep_out failed\n");
530 DBG(cdev, "usb_ep_autoconfig for ep_out got %s\n", ep->name);
531 ep->driver_data = dev; /* claim the endpoint */
534 /* now allocate requests for our endpoints */
535 for (i = 0; i < TX_REQ_MAX; i++) {
536 req = acc_request_new(dev->ep_in, BULK_BUFFER_SIZE);
539 req->complete = acc_complete_in;
540 req_put(dev, &dev->tx_idle, req);
542 for (i = 0; i < RX_REQ_MAX; i++) {
543 req = acc_request_new(dev->ep_out, BULK_BUFFER_SIZE);
546 req->complete = acc_complete_out;
547 dev->rx_req[i] = req;
553 pr_err("acc_bind() could not allocate requests\n");
554 while ((req = req_get(dev, &dev->tx_idle)))
555 acc_request_free(req, dev->ep_in);
556 for (i = 0; i < RX_REQ_MAX; i++)
557 acc_request_free(dev->rx_req[i], dev->ep_out);
561 static ssize_t acc_read(struct file *fp, char __user *buf,
562 size_t count, loff_t *pos)
564 struct acc_dev *dev = fp->private_data;
565 struct usb_request *req;
570 pr_debug("acc_read(%zu)\n", count);
572 if (dev->disconnected) {
573 pr_debug("acc_read disconnected");
577 if (count > BULK_BUFFER_SIZE)
578 count = BULK_BUFFER_SIZE;
580 /* we will block until we're online */
581 pr_debug("acc_read: waiting for online\n");
582 ret = wait_event_interruptible(dev->read_wq, dev->online);
589 // last req cancelled. try to get it.
590 req = dev->rx_req[0];
595 /* queue a request */
596 req = dev->rx_req[0];
599 ret = usb_ep_queue(dev->ep_out, req, GFP_KERNEL);
604 pr_debug("rx %p queue\n", req);
607 /* wait for a request to complete */
608 ret = wait_event_interruptible(dev->read_wq, dev->rx_done);
611 ret = usb_ep_dequeue(dev->ep_out, req);
613 // cancel failed. There can be a data already received.
614 // it will be retrieved in the next read.
615 pr_debug("acc_read: cancelling failed %d", ret);
623 /* If we got a 0-len packet, throw it back and try again. */
624 if (req->actual == 0)
627 pr_debug("rx %p %u\n", req, req->actual);
628 xfer = (req->actual < count) ? req->actual : count;
630 if (copy_to_user(buf, req->buf, xfer))
636 pr_debug("acc_read returning %zd\n", r);
640 static ssize_t acc_write(struct file *fp, const char __user *buf,
641 size_t count, loff_t *pos)
643 struct acc_dev *dev = fp->private_data;
644 struct usb_request *req = 0;
649 pr_debug("acc_write(%zu)\n", count);
651 if (!dev->online || dev->disconnected) {
652 pr_debug("acc_write disconnected or not online");
658 pr_debug("acc_write dev->error\n");
663 /* get an idle tx request to use */
665 ret = wait_event_interruptible(dev->write_wq,
666 ((req = req_get(dev, &dev->tx_idle)) || !dev->online));
672 if (count > BULK_BUFFER_SIZE) {
673 xfer = BULK_BUFFER_SIZE;
674 /* ZLP, They will be more TX requests so not yet. */
678 /* If the data length is a multple of the
679 * maxpacket size then send a zero length packet(ZLP).
681 req->zero = ((xfer % dev->ep_in->maxpacket) == 0);
683 if (copy_from_user(req->buf, buf, xfer)) {
689 ret = usb_ep_queue(dev->ep_in, req, GFP_KERNEL);
691 pr_debug("acc_write: xfer error %d\n", ret);
699 /* zero this so we don't try to free it on error exit */
704 req_put(dev, &dev->tx_idle, req);
706 pr_debug("acc_write returning %zd\n", r);
710 static long acc_ioctl(struct file *fp, unsigned code, unsigned long value)
712 struct acc_dev *dev = fp->private_data;
717 case ACCESSORY_GET_STRING_MANUFACTURER:
718 src = dev->manufacturer;
720 case ACCESSORY_GET_STRING_MODEL:
723 case ACCESSORY_GET_STRING_DESCRIPTION:
724 src = dev->description;
726 case ACCESSORY_GET_STRING_VERSION:
729 case ACCESSORY_GET_STRING_URI:
732 case ACCESSORY_GET_STRING_SERIAL:
735 case ACCESSORY_IS_START_REQUESTED:
736 return dev->start_requested;
737 case ACCESSORY_GET_AUDIO_MODE:
738 return dev->audio_mode;
743 ret = strlen(src) + 1;
744 if (copy_to_user((void __user *)value, src, ret))
749 static int acc_open(struct inode *ip, struct file *fp)
751 printk(KERN_INFO "acc_open\n");
752 if (atomic_xchg(&_acc_dev->open_excl, 1))
755 _acc_dev->disconnected = 0;
756 fp->private_data = _acc_dev;
760 static int acc_release(struct inode *ip, struct file *fp)
762 printk(KERN_INFO "acc_release\n");
764 WARN_ON(!atomic_xchg(&_acc_dev->open_excl, 0));
765 _acc_dev->disconnected = 0;
769 /* file operations for /dev/usb_accessory */
770 static const struct file_operations acc_fops = {
771 .owner = THIS_MODULE,
774 .unlocked_ioctl = acc_ioctl,
776 .compat_ioctl = acc_ioctl,
779 .release = acc_release,
782 static int acc_hid_probe(struct hid_device *hdev,
783 const struct hid_device_id *id)
787 ret = hid_parse(hdev);
790 return hid_hw_start(hdev, HID_CONNECT_DEFAULT);
793 static struct miscdevice acc_device = {
794 .minor = MISC_DYNAMIC_MINOR,
795 .name = "usb_accessory",
799 static const struct hid_device_id acc_hid_table[] = {
800 { HID_USB_DEVICE(HID_ANY_ID, HID_ANY_ID) },
804 static struct hid_driver acc_hid_driver = {
805 .name = "USB accessory",
806 .id_table = acc_hid_table,
807 .probe = acc_hid_probe,
810 int acc_ctrlrequest(struct usb_composite_dev *cdev,
811 const struct usb_ctrlrequest *ctrl)
813 struct acc_dev *dev = _acc_dev;
814 int value = -EOPNOTSUPP;
815 struct acc_hid_dev *hid;
817 u8 b_requestType = ctrl->bRequestType;
818 u8 b_request = ctrl->bRequest;
819 u16 w_index = le16_to_cpu(ctrl->wIndex);
820 u16 w_value = le16_to_cpu(ctrl->wValue);
821 u16 w_length = le16_to_cpu(ctrl->wLength);
825 printk(KERN_INFO "acc_ctrlrequest "
826 "%02x.%02x v%04x i%04x l%u\n",
827 b_requestType, b_request,
828 w_value, w_index, w_length);
831 if (b_requestType == (USB_DIR_OUT | USB_TYPE_VENDOR)) {
832 if (b_request == ACCESSORY_START) {
833 dev->start_requested = 1;
834 schedule_delayed_work(
835 &dev->start_work, msecs_to_jiffies(10));
837 } else if (b_request == ACCESSORY_SEND_STRING) {
838 dev->string_index = w_index;
839 cdev->gadget->ep0->driver_data = dev;
840 cdev->req->complete = acc_complete_set_string;
842 } else if (b_request == ACCESSORY_SET_AUDIO_MODE &&
843 w_index == 0 && w_length == 0) {
844 dev->audio_mode = w_value;
846 } else if (b_request == ACCESSORY_REGISTER_HID) {
847 value = acc_register_hid(dev, w_value, w_index);
848 } else if (b_request == ACCESSORY_UNREGISTER_HID) {
849 value = acc_unregister_hid(dev, w_value);
850 } else if (b_request == ACCESSORY_SET_HID_REPORT_DESC) {
851 spin_lock_irqsave(&dev->lock, flags);
852 hid = acc_hid_get(&dev->new_hid_list, w_value);
853 spin_unlock_irqrestore(&dev->lock, flags);
859 if (offset != hid->report_desc_offset
860 || offset + w_length > hid->report_desc_len) {
864 cdev->req->context = hid;
865 cdev->req->complete = acc_complete_set_hid_report_desc;
867 } else if (b_request == ACCESSORY_SEND_HID_EVENT) {
868 spin_lock_irqsave(&dev->lock, flags);
869 hid = acc_hid_get(&dev->hid_list, w_value);
870 spin_unlock_irqrestore(&dev->lock, flags);
875 cdev->req->context = hid;
876 cdev->req->complete = acc_complete_send_hid_event;
879 } else if (b_requestType == (USB_DIR_IN | USB_TYPE_VENDOR)) {
880 if (b_request == ACCESSORY_GET_PROTOCOL) {
881 *((u16 *)cdev->req->buf) = PROTOCOL_VERSION;
884 /* clear any string left over from a previous session */
885 memset(dev->manufacturer, 0, sizeof(dev->manufacturer));
886 memset(dev->model, 0, sizeof(dev->model));
887 memset(dev->description, 0, sizeof(dev->description));
888 memset(dev->version, 0, sizeof(dev->version));
889 memset(dev->uri, 0, sizeof(dev->uri));
890 memset(dev->serial, 0, sizeof(dev->serial));
891 dev->start_requested = 0;
898 cdev->req->length = value;
899 value = usb_ep_queue(cdev->gadget->ep0, cdev->req, GFP_ATOMIC);
901 ERROR(cdev, "%s setup response queue error\n",
906 if (value == -EOPNOTSUPP)
908 "unknown class-specific control req "
909 "%02x.%02x v%04x i%04x l%u\n",
910 ctrl->bRequestType, ctrl->bRequest,
911 w_value, w_index, w_length);
914 EXPORT_SYMBOL_GPL(acc_ctrlrequest);
917 __acc_function_bind(struct usb_configuration *c,
918 struct usb_function *f, bool configfs)
920 struct usb_composite_dev *cdev = c->cdev;
921 struct acc_dev *dev = func_to_dev(f);
925 DBG(cdev, "acc_function_bind dev: %p\n", dev);
928 if (acc_string_defs[INTERFACE_STRING_INDEX].id == 0) {
929 ret = usb_string_id(c->cdev);
932 acc_string_defs[INTERFACE_STRING_INDEX].id = ret;
933 acc_interface_desc.iInterface = ret;
937 ret = hid_register_driver(&acc_hid_driver);
941 dev->start_requested = 0;
943 /* allocate interface ID(s) */
944 id = usb_interface_id(c, f);
947 acc_interface_desc.bInterfaceNumber = id;
949 /* allocate endpoints */
950 ret = create_bulk_endpoints(dev, &acc_fullspeed_in_desc,
951 &acc_fullspeed_out_desc);
955 /* support high speed hardware */
956 if (gadget_is_dualspeed(c->cdev->gadget)) {
957 acc_highspeed_in_desc.bEndpointAddress =
958 acc_fullspeed_in_desc.bEndpointAddress;
959 acc_highspeed_out_desc.bEndpointAddress =
960 acc_fullspeed_out_desc.bEndpointAddress;
963 DBG(cdev, "%s speed %s: IN/%s, OUT/%s\n",
964 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
965 f->name, dev->ep_in->name, dev->ep_out->name);
970 acc_function_bind_configfs(struct usb_configuration *c,
971 struct usb_function *f) {
972 return __acc_function_bind(c, f, true);
976 kill_all_hid_devices(struct acc_dev *dev)
978 struct acc_hid_dev *hid;
979 struct list_head *entry, *temp;
982 /* do nothing if usb accessory device doesn't exist */
986 spin_lock_irqsave(&dev->lock, flags);
987 list_for_each_safe(entry, temp, &dev->hid_list) {
988 hid = list_entry(entry, struct acc_hid_dev, list);
989 list_del(&hid->list);
990 list_add(&hid->list, &dev->dead_hid_list);
992 list_for_each_safe(entry, temp, &dev->new_hid_list) {
993 hid = list_entry(entry, struct acc_hid_dev, list);
994 list_del(&hid->list);
995 list_add(&hid->list, &dev->dead_hid_list);
997 spin_unlock_irqrestore(&dev->lock, flags);
999 schedule_work(&dev->hid_work);
1003 acc_hid_unbind(struct acc_dev *dev)
1005 hid_unregister_driver(&acc_hid_driver);
1006 kill_all_hid_devices(dev);
1010 acc_function_unbind(struct usb_configuration *c, struct usb_function *f)
1012 struct acc_dev *dev = func_to_dev(f);
1013 struct usb_request *req;
1016 while ((req = req_get(dev, &dev->tx_idle)))
1017 acc_request_free(req, dev->ep_in);
1018 for (i = 0; i < RX_REQ_MAX; i++)
1019 acc_request_free(dev->rx_req[i], dev->ep_out);
1021 acc_hid_unbind(dev);
1024 static void acc_start_work(struct work_struct *data)
1026 char *envp[2] = { "ACCESSORY=START", NULL };
1027 kobject_uevent_env(&acc_device.this_device->kobj, KOBJ_CHANGE, envp);
1030 static int acc_hid_init(struct acc_hid_dev *hdev)
1032 struct hid_device *hid;
1035 hid = hid_allocate_device();
1037 return PTR_ERR(hid);
1039 hid->ll_driver = &acc_hid_ll_driver;
1040 hid->dev.parent = acc_device.this_device;
1043 hid->vendor = HID_ANY_ID;
1044 hid->product = HID_ANY_ID;
1045 hid->driver_data = hdev;
1046 ret = hid_add_device(hid);
1048 pr_err("can't add hid device: %d\n", ret);
1049 hid_destroy_device(hid);
1057 static void acc_hid_delete(struct acc_hid_dev *hid)
1059 kfree(hid->report_desc);
1063 static void acc_hid_work(struct work_struct *data)
1065 struct acc_dev *dev = _acc_dev;
1066 struct list_head *entry, *temp;
1067 struct acc_hid_dev *hid;
1068 struct list_head new_list, dead_list;
1069 unsigned long flags;
1071 INIT_LIST_HEAD(&new_list);
1073 spin_lock_irqsave(&dev->lock, flags);
1075 /* copy hids that are ready for initialization to new_list */
1076 list_for_each_safe(entry, temp, &dev->new_hid_list) {
1077 hid = list_entry(entry, struct acc_hid_dev, list);
1078 if (hid->report_desc_offset == hid->report_desc_len)
1079 list_move(&hid->list, &new_list);
1082 if (list_empty(&dev->dead_hid_list)) {
1083 INIT_LIST_HEAD(&dead_list);
1085 /* move all of dev->dead_hid_list to dead_list */
1086 dead_list.prev = dev->dead_hid_list.prev;
1087 dead_list.next = dev->dead_hid_list.next;
1088 dead_list.next->prev = &dead_list;
1089 dead_list.prev->next = &dead_list;
1090 INIT_LIST_HEAD(&dev->dead_hid_list);
1093 spin_unlock_irqrestore(&dev->lock, flags);
1095 /* register new HID devices */
1096 list_for_each_safe(entry, temp, &new_list) {
1097 hid = list_entry(entry, struct acc_hid_dev, list);
1098 if (acc_hid_init(hid)) {
1099 pr_err("can't add HID device %p\n", hid);
1100 acc_hid_delete(hid);
1102 spin_lock_irqsave(&dev->lock, flags);
1103 list_move(&hid->list, &dev->hid_list);
1104 spin_unlock_irqrestore(&dev->lock, flags);
1108 /* remove dead HID devices */
1109 list_for_each_safe(entry, temp, &dead_list) {
1110 hid = list_entry(entry, struct acc_hid_dev, list);
1111 list_del(&hid->list);
1113 hid_destroy_device(hid->hid);
1114 acc_hid_delete(hid);
1118 static int acc_function_set_alt(struct usb_function *f,
1119 unsigned intf, unsigned alt)
1121 struct acc_dev *dev = func_to_dev(f);
1122 struct usb_composite_dev *cdev = f->config->cdev;
1125 DBG(cdev, "acc_function_set_alt intf: %d alt: %d\n", intf, alt);
1127 ret = config_ep_by_speed(cdev->gadget, f, dev->ep_in);
1131 ret = usb_ep_enable(dev->ep_in);
1135 ret = config_ep_by_speed(cdev->gadget, f, dev->ep_out);
1139 ret = usb_ep_enable(dev->ep_out);
1141 usb_ep_disable(dev->ep_in);
1147 /* readers may be blocked waiting for us to go online */
1148 wake_up(&dev->read_wq);
1152 static void acc_function_disable(struct usb_function *f)
1154 struct acc_dev *dev = func_to_dev(f);
1155 struct usb_composite_dev *cdev = dev->cdev;
1157 DBG(cdev, "acc_function_disable\n");
1158 acc_set_disconnected(dev);
1159 usb_ep_disable(dev->ep_in);
1160 usb_ep_disable(dev->ep_out);
1162 /* readers may be blocked waiting for us to go online */
1163 wake_up(&dev->read_wq);
1165 VDBG(cdev, "%s disabled\n", dev->function.name);
1168 static int acc_setup(void)
1170 struct acc_dev *dev;
1173 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1177 spin_lock_init(&dev->lock);
1178 init_waitqueue_head(&dev->read_wq);
1179 init_waitqueue_head(&dev->write_wq);
1180 atomic_set(&dev->open_excl, 0);
1181 INIT_LIST_HEAD(&dev->tx_idle);
1182 INIT_LIST_HEAD(&dev->hid_list);
1183 INIT_LIST_HEAD(&dev->new_hid_list);
1184 INIT_LIST_HEAD(&dev->dead_hid_list);
1185 INIT_DELAYED_WORK(&dev->start_work, acc_start_work);
1186 INIT_WORK(&dev->hid_work, acc_hid_work);
1188 /* _acc_dev must be set before calling usb_gadget_register_driver */
1191 ret = misc_register(&acc_device);
1199 pr_err("USB accessory gadget driver failed to initialize\n");
1203 void acc_disconnect(void)
1205 /* unregister all HID devices if USB is disconnected */
1206 kill_all_hid_devices(_acc_dev);
1208 EXPORT_SYMBOL_GPL(acc_disconnect);
1210 static void acc_cleanup(void)
1212 misc_deregister(&acc_device);
1216 static struct acc_instance *to_acc_instance(struct config_item *item)
1218 return container_of(to_config_group(item), struct acc_instance,
1222 static void acc_attr_release(struct config_item *item)
1224 struct acc_instance *fi_acc = to_acc_instance(item);
1226 usb_put_function_instance(&fi_acc->func_inst);
1229 static struct configfs_item_operations acc_item_ops = {
1230 .release = acc_attr_release,
1233 static struct config_item_type acc_func_type = {
1234 .ct_item_ops = &acc_item_ops,
1235 .ct_owner = THIS_MODULE,
1238 static struct acc_instance *to_fi_acc(struct usb_function_instance *fi)
1240 return container_of(fi, struct acc_instance, func_inst);
1243 static int acc_set_inst_name(struct usb_function_instance *fi, const char *name)
1245 struct acc_instance *fi_acc;
1249 name_len = strlen(name) + 1;
1250 if (name_len > MAX_INST_NAME_LEN)
1251 return -ENAMETOOLONG;
1253 ptr = kstrndup(name, name_len, GFP_KERNEL);
1257 fi_acc = to_fi_acc(fi);
1262 static void acc_free_inst(struct usb_function_instance *fi)
1264 struct acc_instance *fi_acc;
1266 fi_acc = to_fi_acc(fi);
1267 kfree(fi_acc->name);
1271 static struct usb_function_instance *acc_alloc_inst(void)
1273 struct acc_instance *fi_acc;
1274 struct acc_dev *dev;
1277 fi_acc = kzalloc(sizeof(*fi_acc), GFP_KERNEL);
1279 return ERR_PTR(-ENOMEM);
1280 fi_acc->func_inst.set_inst_name = acc_set_inst_name;
1281 fi_acc->func_inst.free_func_inst = acc_free_inst;
1286 pr_err("Error setting ACCESSORY\n");
1287 return ERR_PTR(err);
1290 config_group_init_type_name(&fi_acc->func_inst.group,
1291 "", &acc_func_type);
1293 return &fi_acc->func_inst;
1296 static void acc_free(struct usb_function *f)
1298 /*NO-OP: no function specific resource allocation in mtp_alloc*/
1301 int acc_ctrlrequest_configfs(struct usb_function *f,
1302 const struct usb_ctrlrequest *ctrl) {
1303 if (f->config != NULL && f->config->cdev != NULL)
1304 return acc_ctrlrequest(f->config->cdev, ctrl);
1309 static struct usb_function *acc_alloc(struct usb_function_instance *fi)
1311 struct acc_dev *dev = _acc_dev;
1313 pr_info("acc_alloc\n");
1315 dev->function.name = "accessory";
1316 dev->function.strings = acc_strings,
1317 dev->function.fs_descriptors = fs_acc_descs;
1318 dev->function.hs_descriptors = hs_acc_descs;
1319 dev->function.bind = acc_function_bind_configfs;
1320 dev->function.unbind = acc_function_unbind;
1321 dev->function.set_alt = acc_function_set_alt;
1322 dev->function.disable = acc_function_disable;
1323 dev->function.free_func = acc_free;
1324 dev->function.setup = acc_ctrlrequest_configfs;
1326 return &dev->function;
1328 DECLARE_USB_FUNCTION_INIT(accessory, acc_alloc_inst, acc_alloc);
1329 MODULE_LICENSE("GPL");