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);
218 /* now allocate buffers for the requests */
219 req->buf = kmalloc(buffer_size, GFP_KERNEL);
221 usb_ep_free_request(ep, req);
228 static void acc_request_free(struct usb_request *req, struct usb_ep *ep)
232 usb_ep_free_request(ep, req);
236 /* add a request to the tail of a list */
237 static void req_put(struct acc_dev *dev, struct list_head *head,
238 struct usb_request *req)
242 spin_lock_irqsave(&dev->lock, flags);
243 list_add_tail(&req->list, head);
244 spin_unlock_irqrestore(&dev->lock, flags);
247 /* remove a request from the head of a list */
248 static struct usb_request *req_get(struct acc_dev *dev, struct list_head *head)
251 struct usb_request *req;
253 spin_lock_irqsave(&dev->lock, flags);
254 if (list_empty(head)) {
257 req = list_first_entry(head, struct usb_request, list);
258 list_del(&req->list);
260 spin_unlock_irqrestore(&dev->lock, flags);
264 static void acc_set_disconnected(struct acc_dev *dev)
267 dev->disconnected = 1;
270 static void acc_complete_in(struct usb_ep *ep, struct usb_request *req)
272 struct acc_dev *dev = _acc_dev;
274 if (req->status == -ESHUTDOWN) {
275 pr_debug("acc_complete_in set disconnected");
276 acc_set_disconnected(dev);
279 req_put(dev, &dev->tx_idle, req);
281 wake_up(&dev->write_wq);
284 static void acc_complete_out(struct usb_ep *ep, struct usb_request *req)
286 struct acc_dev *dev = _acc_dev;
289 if (req->status == -ESHUTDOWN) {
290 pr_debug("acc_complete_out set disconnected");
291 acc_set_disconnected(dev);
294 wake_up(&dev->read_wq);
297 static void acc_complete_set_string(struct usb_ep *ep, struct usb_request *req)
299 struct acc_dev *dev = ep->driver_data;
300 char *string_dest = NULL;
301 int length = req->actual;
303 if (req->status != 0) {
304 pr_err("acc_complete_set_string, err %d\n", req->status);
308 switch (dev->string_index) {
309 case ACCESSORY_STRING_MANUFACTURER:
310 string_dest = dev->manufacturer;
312 case ACCESSORY_STRING_MODEL:
313 string_dest = dev->model;
315 case ACCESSORY_STRING_DESCRIPTION:
316 string_dest = dev->description;
318 case ACCESSORY_STRING_VERSION:
319 string_dest = dev->version;
321 case ACCESSORY_STRING_URI:
322 string_dest = dev->uri;
324 case ACCESSORY_STRING_SERIAL:
325 string_dest = dev->serial;
331 if (length >= ACC_STRING_SIZE)
332 length = ACC_STRING_SIZE - 1;
334 spin_lock_irqsave(&dev->lock, flags);
335 memcpy(string_dest, req->buf, length);
336 /* ensure zero termination */
337 string_dest[length] = 0;
338 spin_unlock_irqrestore(&dev->lock, flags);
340 pr_err("unknown accessory string index %d\n",
345 static void acc_complete_set_hid_report_desc(struct usb_ep *ep,
346 struct usb_request *req)
348 struct acc_hid_dev *hid = req->context;
349 struct acc_dev *dev = hid->dev;
350 int length = req->actual;
352 if (req->status != 0) {
353 pr_err("acc_complete_set_hid_report_desc, err %d\n",
358 memcpy(hid->report_desc + hid->report_desc_offset, req->buf, length);
359 hid->report_desc_offset += length;
360 if (hid->report_desc_offset == hid->report_desc_len) {
361 /* After we have received the entire report descriptor
362 * we schedule work to initialize the HID device
364 schedule_work(&dev->hid_work);
368 static void acc_complete_send_hid_event(struct usb_ep *ep,
369 struct usb_request *req)
371 struct acc_hid_dev *hid = req->context;
372 int length = req->actual;
374 if (req->status != 0) {
375 pr_err("acc_complete_send_hid_event, err %d\n", req->status);
379 hid_report_raw_event(hid->hid, HID_INPUT_REPORT, req->buf, length, 1);
382 static int acc_hid_parse(struct hid_device *hid)
384 struct acc_hid_dev *hdev = hid->driver_data;
386 hid_parse_report(hid, hdev->report_desc, hdev->report_desc_len);
390 static int acc_hid_start(struct hid_device *hid)
395 static void acc_hid_stop(struct hid_device *hid)
399 static int acc_hid_open(struct hid_device *hid)
404 static void acc_hid_close(struct hid_device *hid)
408 static int acc_hid_raw_request(struct hid_device *hid, unsigned char reportnum,
409 __u8 *buf, size_t len, unsigned char rtype, int reqtype)
414 static struct hid_ll_driver acc_hid_ll_driver = {
415 .parse = acc_hid_parse,
416 .start = acc_hid_start,
417 .stop = acc_hid_stop,
418 .open = acc_hid_open,
419 .close = acc_hid_close,
420 .raw_request = acc_hid_raw_request,
423 static struct acc_hid_dev *acc_hid_new(struct acc_dev *dev,
424 int id, int desc_len)
426 struct acc_hid_dev *hdev;
428 hdev = kzalloc(sizeof(*hdev), GFP_ATOMIC);
431 hdev->report_desc = kzalloc(desc_len, GFP_ATOMIC);
432 if (!hdev->report_desc) {
438 hdev->report_desc_len = desc_len;
443 static struct acc_hid_dev *acc_hid_get(struct list_head *list, int id)
445 struct acc_hid_dev *hid;
447 list_for_each_entry(hid, list, list) {
454 static int acc_register_hid(struct acc_dev *dev, int id, int desc_length)
456 struct acc_hid_dev *hid;
459 /* report descriptor length must be > 0 */
460 if (desc_length <= 0)
463 spin_lock_irqsave(&dev->lock, flags);
464 /* replace HID if one already exists with this ID */
465 hid = acc_hid_get(&dev->hid_list, id);
467 hid = acc_hid_get(&dev->new_hid_list, id);
469 list_move(&hid->list, &dev->dead_hid_list);
471 hid = acc_hid_new(dev, id, desc_length);
473 spin_unlock_irqrestore(&dev->lock, flags);
477 list_add(&hid->list, &dev->new_hid_list);
478 spin_unlock_irqrestore(&dev->lock, flags);
480 /* schedule work to register the HID device */
481 schedule_work(&dev->hid_work);
485 static int acc_unregister_hid(struct acc_dev *dev, int id)
487 struct acc_hid_dev *hid;
490 spin_lock_irqsave(&dev->lock, flags);
491 hid = acc_hid_get(&dev->hid_list, id);
493 hid = acc_hid_get(&dev->new_hid_list, id);
495 spin_unlock_irqrestore(&dev->lock, flags);
499 list_move(&hid->list, &dev->dead_hid_list);
500 spin_unlock_irqrestore(&dev->lock, flags);
502 schedule_work(&dev->hid_work);
506 static int create_bulk_endpoints(struct acc_dev *dev,
507 struct usb_endpoint_descriptor *in_desc,
508 struct usb_endpoint_descriptor *out_desc)
510 struct usb_composite_dev *cdev = dev->cdev;
511 struct usb_request *req;
515 DBG(cdev, "create_bulk_endpoints dev: %p\n", dev);
517 ep = usb_ep_autoconfig(cdev->gadget, in_desc);
519 DBG(cdev, "usb_ep_autoconfig for ep_in failed\n");
522 DBG(cdev, "usb_ep_autoconfig for ep_in got %s\n", ep->name);
523 ep->driver_data = dev; /* claim the endpoint */
526 ep = usb_ep_autoconfig(cdev->gadget, out_desc);
528 DBG(cdev, "usb_ep_autoconfig for ep_out failed\n");
531 DBG(cdev, "usb_ep_autoconfig for ep_out got %s\n", ep->name);
532 ep->driver_data = dev; /* claim the endpoint */
535 /* now allocate requests for our endpoints */
536 for (i = 0; i < TX_REQ_MAX; i++) {
537 req = acc_request_new(dev->ep_in, BULK_BUFFER_SIZE);
540 req->complete = acc_complete_in;
541 req_put(dev, &dev->tx_idle, req);
543 for (i = 0; i < RX_REQ_MAX; i++) {
544 req = acc_request_new(dev->ep_out, BULK_BUFFER_SIZE);
547 req->complete = acc_complete_out;
548 dev->rx_req[i] = req;
554 pr_err("acc_bind() could not allocate requests\n");
555 while ((req = req_get(dev, &dev->tx_idle)))
556 acc_request_free(req, dev->ep_in);
557 for (i = 0; i < RX_REQ_MAX; i++)
558 acc_request_free(dev->rx_req[i], dev->ep_out);
562 static ssize_t acc_read(struct file *fp, char __user *buf,
563 size_t count, loff_t *pos)
565 struct acc_dev *dev = fp->private_data;
566 struct usb_request *req;
571 pr_debug("acc_read(%zu)\n", count);
573 if (dev->disconnected) {
574 pr_debug("acc_read disconnected");
578 if (count > BULK_BUFFER_SIZE)
579 count = BULK_BUFFER_SIZE;
581 /* we will block until we're online */
582 pr_debug("acc_read: waiting for online\n");
583 ret = wait_event_interruptible(dev->read_wq, dev->online);
590 // last req cancelled. try to get it.
591 req = dev->rx_req[0];
596 /* queue a request */
597 req = dev->rx_req[0];
600 ret = usb_ep_queue(dev->ep_out, req, GFP_KERNEL);
605 pr_debug("rx %p queue\n", req);
608 /* wait for a request to complete */
609 ret = wait_event_interruptible(dev->read_wq, dev->rx_done);
612 ret = usb_ep_dequeue(dev->ep_out, req);
614 // cancel failed. There can be a data already received.
615 // it will be retrieved in the next read.
616 pr_debug("acc_read: cancelling failed %d", ret);
624 /* If we got a 0-len packet, throw it back and try again. */
625 if (req->actual == 0)
628 pr_debug("rx %p %u\n", req, req->actual);
629 xfer = (req->actual < count) ? req->actual : count;
631 if (copy_to_user(buf, req->buf, xfer))
637 pr_debug("acc_read returning %zd\n", r);
641 static ssize_t acc_write(struct file *fp, const char __user *buf,
642 size_t count, loff_t *pos)
644 struct acc_dev *dev = fp->private_data;
645 struct usb_request *req = 0;
650 pr_debug("acc_write(%zu)\n", count);
652 if (!dev->online || dev->disconnected) {
653 pr_debug("acc_write disconnected or not online");
659 pr_debug("acc_write dev->error\n");
664 /* get an idle tx request to use */
666 ret = wait_event_interruptible(dev->write_wq,
667 ((req = req_get(dev, &dev->tx_idle)) || !dev->online));
673 if (count > BULK_BUFFER_SIZE) {
674 xfer = BULK_BUFFER_SIZE;
675 /* ZLP, They will be more TX requests so not yet. */
679 /* If the data length is a multple of the
680 * maxpacket size then send a zero length packet(ZLP).
682 req->zero = ((xfer % dev->ep_in->maxpacket) == 0);
684 if (copy_from_user(req->buf, buf, xfer)) {
690 ret = usb_ep_queue(dev->ep_in, req, GFP_KERNEL);
692 pr_debug("acc_write: xfer error %d\n", ret);
700 /* zero this so we don't try to free it on error exit */
705 req_put(dev, &dev->tx_idle, req);
707 pr_debug("acc_write returning %zd\n", r);
711 static long acc_ioctl(struct file *fp, unsigned code, unsigned long value)
713 struct acc_dev *dev = fp->private_data;
718 case ACCESSORY_GET_STRING_MANUFACTURER:
719 src = dev->manufacturer;
721 case ACCESSORY_GET_STRING_MODEL:
724 case ACCESSORY_GET_STRING_DESCRIPTION:
725 src = dev->description;
727 case ACCESSORY_GET_STRING_VERSION:
730 case ACCESSORY_GET_STRING_URI:
733 case ACCESSORY_GET_STRING_SERIAL:
736 case ACCESSORY_IS_START_REQUESTED:
737 return dev->start_requested;
738 case ACCESSORY_GET_AUDIO_MODE:
739 return dev->audio_mode;
744 ret = strlen(src) + 1;
745 if (copy_to_user((void __user *)value, src, ret))
750 static int acc_open(struct inode *ip, struct file *fp)
752 printk(KERN_INFO "acc_open\n");
753 if (atomic_xchg(&_acc_dev->open_excl, 1))
756 _acc_dev->disconnected = 0;
757 fp->private_data = _acc_dev;
761 static int acc_release(struct inode *ip, struct file *fp)
763 printk(KERN_INFO "acc_release\n");
765 WARN_ON(!atomic_xchg(&_acc_dev->open_excl, 0));
766 _acc_dev->disconnected = 0;
770 /* file operations for /dev/usb_accessory */
771 static const struct file_operations acc_fops = {
772 .owner = THIS_MODULE,
775 .unlocked_ioctl = acc_ioctl,
777 .compat_ioctl = acc_ioctl,
780 .release = acc_release,
783 static int acc_hid_probe(struct hid_device *hdev,
784 const struct hid_device_id *id)
788 ret = hid_parse(hdev);
791 return hid_hw_start(hdev, HID_CONNECT_DEFAULT);
794 static struct miscdevice acc_device = {
795 .minor = MISC_DYNAMIC_MINOR,
796 .name = "usb_accessory",
800 static const struct hid_device_id acc_hid_table[] = {
801 { HID_USB_DEVICE(HID_ANY_ID, HID_ANY_ID) },
805 static struct hid_driver acc_hid_driver = {
806 .name = "USB accessory",
807 .id_table = acc_hid_table,
808 .probe = acc_hid_probe,
811 int acc_ctrlrequest(struct usb_composite_dev *cdev,
812 const struct usb_ctrlrequest *ctrl)
814 struct acc_dev *dev = _acc_dev;
815 int value = -EOPNOTSUPP;
816 struct acc_hid_dev *hid;
818 u8 b_requestType = ctrl->bRequestType;
819 u8 b_request = ctrl->bRequest;
820 u16 w_index = le16_to_cpu(ctrl->wIndex);
821 u16 w_value = le16_to_cpu(ctrl->wValue);
822 u16 w_length = le16_to_cpu(ctrl->wLength);
826 printk(KERN_INFO "acc_ctrlrequest "
827 "%02x.%02x v%04x i%04x l%u\n",
828 b_requestType, b_request,
829 w_value, w_index, w_length);
832 if (b_requestType == (USB_DIR_OUT | USB_TYPE_VENDOR)) {
833 if (b_request == ACCESSORY_START) {
834 dev->start_requested = 1;
835 schedule_delayed_work(
836 &dev->start_work, msecs_to_jiffies(10));
838 } else if (b_request == ACCESSORY_SEND_STRING) {
839 dev->string_index = w_index;
840 cdev->gadget->ep0->driver_data = dev;
841 cdev->req->complete = acc_complete_set_string;
843 } else if (b_request == ACCESSORY_SET_AUDIO_MODE &&
844 w_index == 0 && w_length == 0) {
845 dev->audio_mode = w_value;
847 } else if (b_request == ACCESSORY_REGISTER_HID) {
848 value = acc_register_hid(dev, w_value, w_index);
849 } else if (b_request == ACCESSORY_UNREGISTER_HID) {
850 value = acc_unregister_hid(dev, w_value);
851 } else if (b_request == ACCESSORY_SET_HID_REPORT_DESC) {
852 spin_lock_irqsave(&dev->lock, flags);
853 hid = acc_hid_get(&dev->new_hid_list, w_value);
854 spin_unlock_irqrestore(&dev->lock, flags);
860 if (offset != hid->report_desc_offset
861 || offset + w_length > hid->report_desc_len) {
865 cdev->req->context = hid;
866 cdev->req->complete = acc_complete_set_hid_report_desc;
868 } else if (b_request == ACCESSORY_SEND_HID_EVENT) {
869 spin_lock_irqsave(&dev->lock, flags);
870 hid = acc_hid_get(&dev->hid_list, w_value);
871 spin_unlock_irqrestore(&dev->lock, flags);
876 cdev->req->context = hid;
877 cdev->req->complete = acc_complete_send_hid_event;
880 } else if (b_requestType == (USB_DIR_IN | USB_TYPE_VENDOR)) {
881 if (b_request == ACCESSORY_GET_PROTOCOL) {
882 *((u16 *)cdev->req->buf) = PROTOCOL_VERSION;
885 /* clear any string left over from a previous session */
886 memset(dev->manufacturer, 0, sizeof(dev->manufacturer));
887 memset(dev->model, 0, sizeof(dev->model));
888 memset(dev->description, 0, sizeof(dev->description));
889 memset(dev->version, 0, sizeof(dev->version));
890 memset(dev->uri, 0, sizeof(dev->uri));
891 memset(dev->serial, 0, sizeof(dev->serial));
892 dev->start_requested = 0;
899 cdev->req->length = value;
900 value = usb_ep_queue(cdev->gadget->ep0, cdev->req, GFP_ATOMIC);
902 ERROR(cdev, "%s setup response queue error\n",
907 if (value == -EOPNOTSUPP)
909 "unknown class-specific control req "
910 "%02x.%02x v%04x i%04x l%u\n",
911 ctrl->bRequestType, ctrl->bRequest,
912 w_value, w_index, w_length);
915 EXPORT_SYMBOL_GPL(acc_ctrlrequest);
918 __acc_function_bind(struct usb_configuration *c,
919 struct usb_function *f, bool configfs)
921 struct usb_composite_dev *cdev = c->cdev;
922 struct acc_dev *dev = func_to_dev(f);
926 DBG(cdev, "acc_function_bind dev: %p\n", dev);
929 if (acc_string_defs[INTERFACE_STRING_INDEX].id == 0) {
930 ret = usb_string_id(c->cdev);
933 acc_string_defs[INTERFACE_STRING_INDEX].id = ret;
934 acc_interface_desc.iInterface = ret;
938 ret = hid_register_driver(&acc_hid_driver);
942 dev->start_requested = 0;
944 /* allocate interface ID(s) */
945 id = usb_interface_id(c, f);
948 acc_interface_desc.bInterfaceNumber = id;
950 /* allocate endpoints */
951 ret = create_bulk_endpoints(dev, &acc_fullspeed_in_desc,
952 &acc_fullspeed_out_desc);
956 /* support high speed hardware */
957 if (gadget_is_dualspeed(c->cdev->gadget)) {
958 acc_highspeed_in_desc.bEndpointAddress =
959 acc_fullspeed_in_desc.bEndpointAddress;
960 acc_highspeed_out_desc.bEndpointAddress =
961 acc_fullspeed_out_desc.bEndpointAddress;
964 DBG(cdev, "%s speed %s: IN/%s, OUT/%s\n",
965 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
966 f->name, dev->ep_in->name, dev->ep_out->name);
971 acc_function_bind_configfs(struct usb_configuration *c,
972 struct usb_function *f) {
973 return __acc_function_bind(c, f, true);
977 kill_all_hid_devices(struct acc_dev *dev)
979 struct acc_hid_dev *hid;
980 struct list_head *entry, *temp;
983 /* do nothing if usb accessory device doesn't exist */
987 spin_lock_irqsave(&dev->lock, flags);
988 list_for_each_safe(entry, temp, &dev->hid_list) {
989 hid = list_entry(entry, struct acc_hid_dev, list);
990 list_del(&hid->list);
991 list_add(&hid->list, &dev->dead_hid_list);
993 list_for_each_safe(entry, temp, &dev->new_hid_list) {
994 hid = list_entry(entry, struct acc_hid_dev, list);
995 list_del(&hid->list);
996 list_add(&hid->list, &dev->dead_hid_list);
998 spin_unlock_irqrestore(&dev->lock, flags);
1000 schedule_work(&dev->hid_work);
1004 acc_hid_unbind(struct acc_dev *dev)
1006 hid_unregister_driver(&acc_hid_driver);
1007 kill_all_hid_devices(dev);
1011 acc_function_unbind(struct usb_configuration *c, struct usb_function *f)
1013 struct acc_dev *dev = func_to_dev(f);
1014 struct usb_request *req;
1017 while ((req = req_get(dev, &dev->tx_idle)))
1018 acc_request_free(req, dev->ep_in);
1019 for (i = 0; i < RX_REQ_MAX; i++)
1020 acc_request_free(dev->rx_req[i], dev->ep_out);
1022 acc_hid_unbind(dev);
1025 static void acc_start_work(struct work_struct *data)
1027 char *envp[2] = { "ACCESSORY=START", NULL };
1029 kobject_uevent_env(&acc_device.this_device->kobj, KOBJ_CHANGE, envp);
1032 static int acc_hid_init(struct acc_hid_dev *hdev)
1034 struct hid_device *hid;
1037 hid = hid_allocate_device();
1039 return PTR_ERR(hid);
1041 hid->ll_driver = &acc_hid_ll_driver;
1042 hid->dev.parent = acc_device.this_device;
1045 hid->vendor = HID_ANY_ID;
1046 hid->product = HID_ANY_ID;
1047 hid->driver_data = hdev;
1048 ret = hid_add_device(hid);
1050 pr_err("can't add hid device: %d\n", ret);
1051 hid_destroy_device(hid);
1059 static void acc_hid_delete(struct acc_hid_dev *hid)
1061 kfree(hid->report_desc);
1065 static void acc_hid_work(struct work_struct *data)
1067 struct acc_dev *dev = _acc_dev;
1068 struct list_head *entry, *temp;
1069 struct acc_hid_dev *hid;
1070 struct list_head new_list, dead_list;
1071 unsigned long flags;
1073 INIT_LIST_HEAD(&new_list);
1075 spin_lock_irqsave(&dev->lock, flags);
1077 /* copy hids that are ready for initialization to new_list */
1078 list_for_each_safe(entry, temp, &dev->new_hid_list) {
1079 hid = list_entry(entry, struct acc_hid_dev, list);
1080 if (hid->report_desc_offset == hid->report_desc_len)
1081 list_move(&hid->list, &new_list);
1084 if (list_empty(&dev->dead_hid_list)) {
1085 INIT_LIST_HEAD(&dead_list);
1087 /* move all of dev->dead_hid_list to dead_list */
1088 dead_list.prev = dev->dead_hid_list.prev;
1089 dead_list.next = dev->dead_hid_list.next;
1090 dead_list.next->prev = &dead_list;
1091 dead_list.prev->next = &dead_list;
1092 INIT_LIST_HEAD(&dev->dead_hid_list);
1095 spin_unlock_irqrestore(&dev->lock, flags);
1097 /* register new HID devices */
1098 list_for_each_safe(entry, temp, &new_list) {
1099 hid = list_entry(entry, struct acc_hid_dev, list);
1100 if (acc_hid_init(hid)) {
1101 pr_err("can't add HID device %p\n", hid);
1102 acc_hid_delete(hid);
1104 spin_lock_irqsave(&dev->lock, flags);
1105 list_move(&hid->list, &dev->hid_list);
1106 spin_unlock_irqrestore(&dev->lock, flags);
1110 /* remove dead HID devices */
1111 list_for_each_safe(entry, temp, &dead_list) {
1112 hid = list_entry(entry, struct acc_hid_dev, list);
1113 list_del(&hid->list);
1115 hid_destroy_device(hid->hid);
1116 acc_hid_delete(hid);
1120 static int acc_function_set_alt(struct usb_function *f,
1121 unsigned intf, unsigned alt)
1123 struct acc_dev *dev = func_to_dev(f);
1124 struct usb_composite_dev *cdev = f->config->cdev;
1127 DBG(cdev, "acc_function_set_alt intf: %d alt: %d\n", intf, alt);
1129 ret = config_ep_by_speed(cdev->gadget, f, dev->ep_in);
1133 ret = usb_ep_enable(dev->ep_in);
1137 ret = config_ep_by_speed(cdev->gadget, f, dev->ep_out);
1141 ret = usb_ep_enable(dev->ep_out);
1143 usb_ep_disable(dev->ep_in);
1149 /* readers may be blocked waiting for us to go online */
1150 wake_up(&dev->read_wq);
1154 static void acc_function_disable(struct usb_function *f)
1156 struct acc_dev *dev = func_to_dev(f);
1157 struct usb_composite_dev *cdev = dev->cdev;
1159 DBG(cdev, "acc_function_disable\n");
1160 acc_set_disconnected(dev);
1161 usb_ep_disable(dev->ep_in);
1162 usb_ep_disable(dev->ep_out);
1164 /* readers may be blocked waiting for us to go online */
1165 wake_up(&dev->read_wq);
1167 VDBG(cdev, "%s disabled\n", dev->function.name);
1170 static int acc_setup(void)
1172 struct acc_dev *dev;
1175 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1179 spin_lock_init(&dev->lock);
1180 init_waitqueue_head(&dev->read_wq);
1181 init_waitqueue_head(&dev->write_wq);
1182 atomic_set(&dev->open_excl, 0);
1183 INIT_LIST_HEAD(&dev->tx_idle);
1184 INIT_LIST_HEAD(&dev->hid_list);
1185 INIT_LIST_HEAD(&dev->new_hid_list);
1186 INIT_LIST_HEAD(&dev->dead_hid_list);
1187 INIT_DELAYED_WORK(&dev->start_work, acc_start_work);
1188 INIT_WORK(&dev->hid_work, acc_hid_work);
1190 /* _acc_dev must be set before calling usb_gadget_register_driver */
1193 ret = misc_register(&acc_device);
1201 pr_err("USB accessory gadget driver failed to initialize\n");
1205 void acc_disconnect(void)
1207 /* unregister all HID devices if USB is disconnected */
1208 kill_all_hid_devices(_acc_dev);
1210 EXPORT_SYMBOL_GPL(acc_disconnect);
1212 static void acc_cleanup(void)
1214 misc_deregister(&acc_device);
1218 static struct acc_instance *to_acc_instance(struct config_item *item)
1220 return container_of(to_config_group(item), struct acc_instance,
1224 static void acc_attr_release(struct config_item *item)
1226 struct acc_instance *fi_acc = to_acc_instance(item);
1228 usb_put_function_instance(&fi_acc->func_inst);
1231 static struct configfs_item_operations acc_item_ops = {
1232 .release = acc_attr_release,
1235 static struct config_item_type acc_func_type = {
1236 .ct_item_ops = &acc_item_ops,
1237 .ct_owner = THIS_MODULE,
1240 static struct acc_instance *to_fi_acc(struct usb_function_instance *fi)
1242 return container_of(fi, struct acc_instance, func_inst);
1245 static int acc_set_inst_name(struct usb_function_instance *fi, const char *name)
1247 struct acc_instance *fi_acc;
1251 name_len = strlen(name) + 1;
1252 if (name_len > MAX_INST_NAME_LEN)
1253 return -ENAMETOOLONG;
1255 ptr = kstrndup(name, name_len, GFP_KERNEL);
1259 fi_acc = to_fi_acc(fi);
1264 static void acc_free_inst(struct usb_function_instance *fi)
1266 struct acc_instance *fi_acc;
1268 fi_acc = to_fi_acc(fi);
1269 kfree(fi_acc->name);
1273 static struct usb_function_instance *acc_alloc_inst(void)
1275 struct acc_instance *fi_acc;
1276 struct acc_dev *dev;
1279 fi_acc = kzalloc(sizeof(*fi_acc), GFP_KERNEL);
1281 return ERR_PTR(-ENOMEM);
1282 fi_acc->func_inst.set_inst_name = acc_set_inst_name;
1283 fi_acc->func_inst.free_func_inst = acc_free_inst;
1288 pr_err("Error setting ACCESSORY\n");
1289 return ERR_PTR(err);
1292 config_group_init_type_name(&fi_acc->func_inst.group,
1293 "", &acc_func_type);
1295 return &fi_acc->func_inst;
1298 static void acc_free(struct usb_function *f)
1300 /*NO-OP: no function specific resource allocation in mtp_alloc*/
1303 int acc_ctrlrequest_configfs(struct usb_function *f,
1304 const struct usb_ctrlrequest *ctrl) {
1305 if (f->config != NULL && f->config->cdev != NULL)
1306 return acc_ctrlrequest(f->config->cdev, ctrl);
1311 static struct usb_function *acc_alloc(struct usb_function_instance *fi)
1313 struct acc_dev *dev = _acc_dev;
1315 pr_info("acc_alloc\n");
1317 dev->function.name = "accessory";
1318 dev->function.strings = acc_strings,
1319 dev->function.fs_descriptors = fs_acc_descs;
1320 dev->function.hs_descriptors = hs_acc_descs;
1321 dev->function.bind = acc_function_bind_configfs;
1322 dev->function.unbind = acc_function_unbind;
1323 dev->function.set_alt = acc_function_set_alt;
1324 dev->function.disable = acc_function_disable;
1325 dev->function.free_func = acc_free;
1326 dev->function.setup = acc_ctrlrequest_configfs;
1328 return &dev->function;
1330 DECLARE_USB_FUNCTION_INIT(accessory, acc_alloc_inst, acc_alloc);
1331 MODULE_LICENSE("GPL");