1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282 |
- /*
- * Gadget Function Driver for Android USB accessories
- *
- * Copyright (C) 2011 Google, Inc.
- * Author: Mike Lockwood <lockwood@android.com>
- *
- * This software is licensed under the terms of the GNU General Public
- * License version 2, as published by the Free Software Foundation, and
- * may be copied, distributed, and modified under those terms.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- */
- /* #define DEBUG */
- /* #define VERBOSE_DEBUG */
- #include <linux/module.h>
- #include <linux/init.h>
- #include <linux/poll.h>
- #include <linux/delay.h>
- #include <linux/wait.h>
- #include <linux/err.h>
- #include <linux/interrupt.h>
- #include <linux/kthread.h>
- #include <linux/freezer.h>
- #include <linux/types.h>
- #include <linux/file.h>
- #include <linux/device.h>
- #include <linux/miscdevice.h>
- #include <linux/hid.h>
- #include <linux/hiddev.h>
- #include <linux/usb.h>
- #include <linux/usb/ch9.h>
- #include <linux/usb/f_accessory.h>
- #define BULK_BUFFER_SIZE 16384
- #define ACC_STRING_SIZE 256
- #define PROTOCOL_VERSION 2
- /* String IDs */
- #define INTERFACE_STRING_INDEX 0
- /* number of tx and rx requests to allocate */
- #define TX_REQ_MAX 4
- #define RX_REQ_MAX 2
- struct acc_hid_dev {
- struct list_head list;
- struct hid_device *hid;
- struct acc_dev *dev;
- /* accessory defined ID */
- int id;
- /* HID report descriptor */
- u8 *report_desc;
- /* length of HID report descriptor */
- int report_desc_len;
- /* number of bytes of report_desc we have received so far */
- int report_desc_offset;
- };
- struct acc_dev {
- struct usb_function function;
- struct usb_composite_dev *cdev;
- spinlock_t lock;
- struct usb_ep *ep_in;
- struct usb_ep *ep_out;
- /* set to 1 when we connect */
- int online:1;
- /* Set to 1 when we disconnect.
- * Not cleared until our file is closed.
- */
- int disconnected:1;
- /* strings sent by the host */
- char manufacturer[ACC_STRING_SIZE];
- char model[ACC_STRING_SIZE];
- char description[ACC_STRING_SIZE];
- char version[ACC_STRING_SIZE];
- char uri[ACC_STRING_SIZE];
- char serial[ACC_STRING_SIZE];
- /* for acc_complete_set_string */
- int string_index;
- /* set to 1 if we have a pending start request */
- int start_requested;
- int audio_mode;
- /* synchronize access to our device file */
- atomic_t open_excl;
- struct list_head tx_idle;
- wait_queue_head_t read_wq;
- wait_queue_head_t write_wq;
- struct usb_request *rx_req[RX_REQ_MAX];
- int rx_done;
- /* delayed work for handling ACCESSORY_START */
- struct delayed_work start_work;
- /* worker for registering and unregistering hid devices */
- struct work_struct hid_work;
- /* list of active HID devices */
- struct list_head hid_list;
- /* list of new HID devices to register */
- struct list_head new_hid_list;
- /* list of dead HID devices to unregister */
- struct list_head dead_hid_list;
- };
- static struct usb_interface_descriptor acc_interface_desc = {
- .bLength = USB_DT_INTERFACE_SIZE,
- .bDescriptorType = USB_DT_INTERFACE,
- .bInterfaceNumber = 0,
- .bNumEndpoints = 2,
- .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
- .bInterfaceSubClass = USB_SUBCLASS_VENDOR_SPEC,
- .bInterfaceProtocol = 0,
- };
- static struct usb_endpoint_descriptor acc_superspeed_in_desc = {
- .bLength = USB_DT_ENDPOINT_SIZE,
- .bDescriptorType = USB_DT_ENDPOINT,
- .bEndpointAddress = USB_DIR_IN,
- .bmAttributes = USB_ENDPOINT_XFER_BULK,
- .wMaxPacketSize = __constant_cpu_to_le16(1024),
- };
- static struct usb_ss_ep_comp_descriptor acc_superspeed_in_comp_desc = {
- .bLength = sizeof acc_superspeed_in_comp_desc,
- .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
- /* the following 2 values can be tweaked if necessary */
- /* .bMaxBurst = 0, */
- /* .bmAttributes = 0, */
- };
- static struct usb_endpoint_descriptor acc_superspeed_out_desc = {
- .bLength = USB_DT_ENDPOINT_SIZE,
- .bDescriptorType = USB_DT_ENDPOINT,
- .bEndpointAddress = USB_DIR_OUT,
- .bmAttributes = USB_ENDPOINT_XFER_BULK,
- .wMaxPacketSize = __constant_cpu_to_le16(1024),
- };
- static struct usb_ss_ep_comp_descriptor acc_superspeed_out_comp_desc = {
- .bLength = sizeof acc_superspeed_out_comp_desc,
- .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
- /* the following 2 values can be tweaked if necessary */
- /* .bMaxBurst = 0, */
- /* .bmAttributes = 0, */
- };
- static struct usb_endpoint_descriptor acc_highspeed_in_desc = {
- .bLength = USB_DT_ENDPOINT_SIZE,
- .bDescriptorType = USB_DT_ENDPOINT,
- .bEndpointAddress = USB_DIR_IN,
- .bmAttributes = USB_ENDPOINT_XFER_BULK,
- .wMaxPacketSize = __constant_cpu_to_le16(512),
- };
- static struct usb_endpoint_descriptor acc_highspeed_out_desc = {
- .bLength = USB_DT_ENDPOINT_SIZE,
- .bDescriptorType = USB_DT_ENDPOINT,
- .bEndpointAddress = USB_DIR_OUT,
- .bmAttributes = USB_ENDPOINT_XFER_BULK,
- .wMaxPacketSize = __constant_cpu_to_le16(512),
- };
- static struct usb_endpoint_descriptor acc_fullspeed_in_desc = {
- .bLength = USB_DT_ENDPOINT_SIZE,
- .bDescriptorType = USB_DT_ENDPOINT,
- .bEndpointAddress = USB_DIR_IN,
- .bmAttributes = USB_ENDPOINT_XFER_BULK,
- };
- static struct usb_endpoint_descriptor acc_fullspeed_out_desc = {
- .bLength = USB_DT_ENDPOINT_SIZE,
- .bDescriptorType = USB_DT_ENDPOINT,
- .bEndpointAddress = USB_DIR_OUT,
- .bmAttributes = USB_ENDPOINT_XFER_BULK,
- };
- static struct usb_descriptor_header *fs_acc_descs[] = {
- (struct usb_descriptor_header *) &acc_interface_desc,
- (struct usb_descriptor_header *) &acc_fullspeed_in_desc,
- (struct usb_descriptor_header *) &acc_fullspeed_out_desc,
- NULL,
- };
- static struct usb_descriptor_header *hs_acc_descs[] = {
- (struct usb_descriptor_header *) &acc_interface_desc,
- (struct usb_descriptor_header *) &acc_highspeed_in_desc,
- (struct usb_descriptor_header *) &acc_highspeed_out_desc,
- NULL,
- };
- static struct usb_descriptor_header *ss_acc_descs[] = {
- (struct usb_descriptor_header *) &acc_interface_desc,
- (struct usb_descriptor_header *) &acc_superspeed_in_desc,
- (struct usb_descriptor_header *) &acc_superspeed_in_comp_desc,
- (struct usb_descriptor_header *) &acc_superspeed_out_desc,
- (struct usb_descriptor_header *) &acc_superspeed_out_comp_desc,
- NULL,
- };
- static struct usb_string acc_string_defs[] = {
- [INTERFACE_STRING_INDEX].s = "Android Accessory Interface",
- { }, /* end of list */
- };
- static struct usb_gadget_strings acc_string_table = {
- .language = 0x0409, /* en-US */
- .strings = acc_string_defs,
- };
- static struct usb_gadget_strings *acc_strings[] = {
- &acc_string_table,
- NULL,
- };
- /* temporary variable used between acc_open() and acc_gadget_bind() */
- static struct acc_dev *_acc_dev;
- static inline struct acc_dev *func_to_dev(struct usb_function *f)
- {
- return container_of(f, struct acc_dev, function);
- }
- static struct usb_request *acc_request_new(struct usb_ep *ep, int buffer_size)
- {
- struct usb_request *req = usb_ep_alloc_request(ep, GFP_KERNEL);
- if (!req)
- return NULL;
- /* now allocate buffers for the requests */
- req->buf = kmalloc(buffer_size, GFP_KERNEL);
- if (!req->buf) {
- usb_ep_free_request(ep, req);
- return NULL;
- }
- return req;
- }
- static void acc_request_free(struct usb_request *req, struct usb_ep *ep)
- {
- if (req) {
- kfree(req->buf);
- usb_ep_free_request(ep, req);
- }
- }
- /* add a request to the tail of a list */
- static void req_put(struct acc_dev *dev, struct list_head *head,
- struct usb_request *req)
- {
- unsigned long flags;
- spin_lock_irqsave(&dev->lock, flags);
- list_add_tail(&req->list, head);
- spin_unlock_irqrestore(&dev->lock, flags);
- }
- /* remove a request from the head of a list */
- static struct usb_request *req_get(struct acc_dev *dev, struct list_head *head)
- {
- unsigned long flags;
- struct usb_request *req;
- spin_lock_irqsave(&dev->lock, flags);
- if (list_empty(head)) {
- req = 0;
- } else {
- req = list_first_entry(head, struct usb_request, list);
- list_del(&req->list);
- }
- spin_unlock_irqrestore(&dev->lock, flags);
- return req;
- }
- static void acc_set_disconnected(struct acc_dev *dev)
- {
- dev->online = 0;
- dev->disconnected = 1;
- }
- static void acc_complete_in(struct usb_ep *ep, struct usb_request *req)
- {
- struct acc_dev *dev = _acc_dev;
- if (req->status == -ESHUTDOWN) {
- pr_debug("acc_complete_in set disconnected");
- acc_set_disconnected(dev);
- }
- req_put(dev, &dev->tx_idle, req);
- wake_up(&dev->write_wq);
- }
- static void acc_complete_out(struct usb_ep *ep, struct usb_request *req)
- {
- struct acc_dev *dev = _acc_dev;
- dev->rx_done = 1;
- if (req->status == -ESHUTDOWN) {
- pr_debug("acc_complete_out set disconnected");
- acc_set_disconnected(dev);
- }
- wake_up(&dev->read_wq);
- }
- static void acc_complete_set_string(struct usb_ep *ep, struct usb_request *req)
- {
- struct acc_dev *dev = ep->driver_data;
- char *string_dest = NULL;
- int length = req->actual;
- if (req->status != 0) {
- pr_err("acc_complete_set_string, err %d\n", req->status);
- return;
- }
- switch (dev->string_index) {
- case ACCESSORY_STRING_MANUFACTURER:
- string_dest = dev->manufacturer;
- break;
- case ACCESSORY_STRING_MODEL:
- string_dest = dev->model;
- break;
- case ACCESSORY_STRING_DESCRIPTION:
- string_dest = dev->description;
- break;
- case ACCESSORY_STRING_VERSION:
- string_dest = dev->version;
- break;
- case ACCESSORY_STRING_URI:
- string_dest = dev->uri;
- break;
- case ACCESSORY_STRING_SERIAL:
- string_dest = dev->serial;
- break;
- }
- if (string_dest) {
- unsigned long flags;
- if (length >= ACC_STRING_SIZE)
- length = ACC_STRING_SIZE - 1;
- spin_lock_irqsave(&dev->lock, flags);
- memcpy(string_dest, req->buf, length);
- /* ensure zero termination */
- string_dest[length] = 0;
- spin_unlock_irqrestore(&dev->lock, flags);
- } else {
- pr_err("unknown accessory string index %d\n",
- dev->string_index);
- }
- }
- static void acc_complete_set_hid_report_desc(struct usb_ep *ep,
- struct usb_request *req)
- {
- struct acc_hid_dev *hid = req->context;
- struct acc_dev *dev = hid->dev;
- int length = req->actual;
- if (req->status != 0) {
- pr_err("acc_complete_set_hid_report_desc, err %d\n",
- req->status);
- return;
- }
- memcpy(hid->report_desc + hid->report_desc_offset, req->buf, length);
- hid->report_desc_offset += length;
- if (hid->report_desc_offset == hid->report_desc_len) {
- /* After we have received the entire report descriptor
- * we schedule work to initialize the HID device
- */
- schedule_work(&dev->hid_work);
- }
- }
- static void acc_complete_send_hid_event(struct usb_ep *ep,
- struct usb_request *req)
- {
- struct acc_hid_dev *hid = req->context;
- int length = req->actual;
- if (req->status != 0) {
- pr_err("acc_complete_send_hid_event, err %d\n", req->status);
- return;
- }
- hid_report_raw_event(hid->hid, HID_INPUT_REPORT, req->buf, length, 1);
- }
- static int acc_hid_parse(struct hid_device *hid)
- {
- struct acc_hid_dev *hdev = hid->driver_data;
- hid_parse_report(hid, hdev->report_desc, hdev->report_desc_len);
- return 0;
- }
- static int acc_hid_start(struct hid_device *hid)
- {
- return 0;
- }
- static void acc_hid_stop(struct hid_device *hid)
- {
- }
- static int acc_hid_open(struct hid_device *hid)
- {
- return 0;
- }
- static void acc_hid_close(struct hid_device *hid)
- {
- }
- static struct hid_ll_driver acc_hid_ll_driver = {
- .parse = acc_hid_parse,
- .start = acc_hid_start,
- .stop = acc_hid_stop,
- .open = acc_hid_open,
- .close = acc_hid_close,
- };
- static struct acc_hid_dev *acc_hid_new(struct acc_dev *dev,
- int id, int desc_len)
- {
- struct acc_hid_dev *hdev;
- hdev = kzalloc(sizeof(*hdev), GFP_ATOMIC);
- if (!hdev)
- return NULL;
- hdev->report_desc = kzalloc(desc_len, GFP_ATOMIC);
- if (!hdev->report_desc) {
- kfree(hdev);
- return NULL;
- }
- hdev->dev = dev;
- hdev->id = id;
- hdev->report_desc_len = desc_len;
- return hdev;
- }
- static struct acc_hid_dev *acc_hid_get(struct list_head *list, int id)
- {
- struct acc_hid_dev *hid;
- list_for_each_entry(hid, list, list) {
- if (hid->id == id)
- return hid;
- }
- return NULL;
- }
- static int acc_register_hid(struct acc_dev *dev, int id, int desc_length)
- {
- struct acc_hid_dev *hid;
- unsigned long flags;
- /* report descriptor length must be > 0 */
- if (desc_length <= 0)
- return -EINVAL;
- spin_lock_irqsave(&dev->lock, flags);
- /* replace HID if one already exists with this ID */
- hid = acc_hid_get(&dev->hid_list, id);
- if (!hid)
- hid = acc_hid_get(&dev->new_hid_list, id);
- if (hid)
- list_move(&hid->list, &dev->dead_hid_list);
- hid = acc_hid_new(dev, id, desc_length);
- if (!hid) {
- spin_unlock_irqrestore(&dev->lock, flags);
- return -ENOMEM;
- }
- list_add(&hid->list, &dev->new_hid_list);
- spin_unlock_irqrestore(&dev->lock, flags);
- /* schedule work to register the HID device */
- schedule_work(&dev->hid_work);
- return 0;
- }
- static int acc_unregister_hid(struct acc_dev *dev, int id)
- {
- struct acc_hid_dev *hid;
- unsigned long flags;
- spin_lock_irqsave(&dev->lock, flags);
- hid = acc_hid_get(&dev->hid_list, id);
- if (!hid)
- hid = acc_hid_get(&dev->new_hid_list, id);
- if (!hid) {
- spin_unlock_irqrestore(&dev->lock, flags);
- return -EINVAL;
- }
- list_move(&hid->list, &dev->dead_hid_list);
- spin_unlock_irqrestore(&dev->lock, flags);
- schedule_work(&dev->hid_work);
- return 0;
- }
- static int create_bulk_endpoints(struct acc_dev *dev,
- struct usb_endpoint_descriptor *in_desc,
- struct usb_endpoint_descriptor *out_desc)
- {
- struct usb_composite_dev *cdev = dev->cdev;
- struct usb_request *req;
- struct usb_ep *ep;
- int i;
- DBG(cdev, "create_bulk_endpoints dev: %pK\n", dev);
- ep = usb_ep_autoconfig(cdev->gadget, in_desc);
- if (!ep) {
- DBG(cdev, "usb_ep_autoconfig for ep_in failed\n");
- return -ENODEV;
- }
- DBG(cdev, "usb_ep_autoconfig for ep_in got %s\n", ep->name);
- ep->driver_data = dev; /* claim the endpoint */
- dev->ep_in = ep;
- ep = usb_ep_autoconfig(cdev->gadget, out_desc);
- if (!ep) {
- DBG(cdev, "usb_ep_autoconfig for ep_out failed\n");
- return -ENODEV;
- }
- DBG(cdev, "usb_ep_autoconfig for ep_out got %s\n", ep->name);
- ep->driver_data = dev; /* claim the endpoint */
- dev->ep_out = ep;
- ep = usb_ep_autoconfig(cdev->gadget, out_desc);
- if (!ep) {
- DBG(cdev, "usb_ep_autoconfig for ep_out failed\n");
- return -ENODEV;
- }
- DBG(cdev, "usb_ep_autoconfig for ep_out got %s\n", ep->name);
- ep->driver_data = dev; /* claim the endpoint */
- dev->ep_out = ep;
- /* now allocate requests for our endpoints */
- for (i = 0; i < TX_REQ_MAX; i++) {
- req = acc_request_new(dev->ep_in, BULK_BUFFER_SIZE);
- if (!req)
- goto fail;
- req->complete = acc_complete_in;
- req_put(dev, &dev->tx_idle, req);
- }
- for (i = 0; i < RX_REQ_MAX; i++) {
- req = acc_request_new(dev->ep_out, BULK_BUFFER_SIZE);
- if (!req)
- goto fail;
- req->complete = acc_complete_out;
- dev->rx_req[i] = req;
- }
- return 0;
- fail:
- pr_err("acc_bind() could not allocate requests\n");
- while ((req = req_get(dev, &dev->tx_idle)))
- acc_request_free(req, dev->ep_in);
- for (i = 0; i < RX_REQ_MAX; i++)
- acc_request_free(dev->rx_req[i], dev->ep_out);
- return -1;
- }
- static ssize_t acc_read(struct file *fp, char __user *buf,
- size_t count, loff_t *pos)
- {
- struct acc_dev *dev = fp->private_data;
- struct usb_request *req;
- int r = count, xfer, len;
- int ret = 0;
- pr_debug("acc_read(%d)\n", count);
- if (dev->disconnected) {
- pr_debug("acc_read disconnected");
- return -ENODEV;
- }
- if (count > BULK_BUFFER_SIZE)
- count = BULK_BUFFER_SIZE;
- len = ALIGN(count, dev->ep_out->maxpacket);
- /* we will block until we're online */
- pr_debug("acc_read: waiting for online\n");
- ret = wait_event_interruptible(dev->read_wq, dev->online);
- if (ret < 0) {
- r = ret;
- goto done;
- }
- if (dev->rx_done) {
- // last req cancelled. try to get it.
- req = dev->rx_req[0];
- goto copy_data;
- }
- requeue_req:
- /* queue a request */
- req = dev->rx_req[0];
- req->length = len;
- dev->rx_done = 0;
- ret = usb_ep_queue(dev->ep_out, req, GFP_KERNEL);
- if (ret < 0) {
- r = -EIO;
- goto done;
- } else {
- pr_debug("rx %pK queue\n", req);
- }
- /* wait for a request to complete */
- ret = wait_event_interruptible(dev->read_wq, dev->rx_done);
- if (ret < 0) {
- r = ret;
- ret = usb_ep_dequeue(dev->ep_out, req);
- if (ret != 0) {
- // cancel failed. There can be a data already received.
- // it will be retrieved in the next read.
- pr_debug("acc_read: cancelling failed %d", ret);
- }
- goto done;
- }
- copy_data:
- dev->rx_done = 0;
- if (dev->online) {
- /* If we got a 0-len packet, throw it back and try again. */
- if (req->actual == 0)
- goto requeue_req;
- pr_debug("rx %pK %d\n", req, req->actual);
- xfer = (req->actual < count) ? req->actual : count;
- r = xfer;
- if (copy_to_user(buf, req->buf, xfer))
- r = -EFAULT;
- } else
- r = -EIO;
- done:
- pr_debug("acc_read returning %d\n", r);
- return r;
- }
- static ssize_t acc_write(struct file *fp, const char __user *buf,
- size_t count, loff_t *pos)
- {
- struct acc_dev *dev = fp->private_data;
- struct usb_request *req = 0;
- int r = count, xfer;
- int ret;
- pr_debug("acc_write(%d)\n", count);
- if (!dev->online || dev->disconnected) {
- pr_debug("acc_write disconnected or not online");
- return -ENODEV;
- }
- while (count > 0) {
- if (!dev->online) {
- pr_debug("acc_write dev->error\n");
- r = -EIO;
- break;
- }
- /* get an idle tx request to use */
- req = 0;
- ret = wait_event_interruptible(dev->write_wq,
- ((req = req_get(dev, &dev->tx_idle)) || !dev->online));
- if (!req) {
- r = ret;
- break;
- }
- if (count > BULK_BUFFER_SIZE) {
- xfer = BULK_BUFFER_SIZE;
- /* ZLP, They will be more TX requests so not yet. */
- req->zero = 0;
- } else {
- xfer = count;
- /* If the data length is a multple of the
- * maxpacket size then send a zero length packet(ZLP).
- */
- req->zero = ((xfer % dev->ep_in->maxpacket) == 0);
- }
- if (copy_from_user(req->buf, buf, xfer)) {
- r = -EFAULT;
- break;
- }
- req->length = xfer;
- ret = usb_ep_queue(dev->ep_in, req, GFP_KERNEL);
- if (ret < 0) {
- pr_debug("acc_write: xfer error %d\n", ret);
- r = -EIO;
- break;
- }
- buf += xfer;
- count -= xfer;
- /* zero this so we don't try to free it on error exit */
- req = 0;
- }
- if (req)
- req_put(dev, &dev->tx_idle, req);
- pr_debug("acc_write returning %d\n", r);
- return r;
- }
- static long acc_ioctl(struct file *fp, unsigned code, unsigned long value)
- {
- struct acc_dev *dev = fp->private_data;
- char *src = NULL;
- int ret;
- switch (code) {
- case ACCESSORY_GET_STRING_MANUFACTURER:
- src = dev->manufacturer;
- break;
- case ACCESSORY_GET_STRING_MODEL:
- src = dev->model;
- break;
- case ACCESSORY_GET_STRING_DESCRIPTION:
- src = dev->description;
- break;
- case ACCESSORY_GET_STRING_VERSION:
- src = dev->version;
- break;
- case ACCESSORY_GET_STRING_URI:
- src = dev->uri;
- break;
- case ACCESSORY_GET_STRING_SERIAL:
- src = dev->serial;
- break;
- case ACCESSORY_IS_START_REQUESTED:
- return dev->start_requested;
- case ACCESSORY_GET_AUDIO_MODE:
- return dev->audio_mode;
- }
- if (!src)
- return -EINVAL;
- ret = strlen(src) + 1;
- if (copy_to_user((void __user *)value, src, ret))
- ret = -EFAULT;
- return ret;
- }
- static int acc_open(struct inode *ip, struct file *fp)
- {
- printk(KERN_INFO "acc_open\n");
- if (atomic_xchg(&_acc_dev->open_excl, 1))
- return -EBUSY;
- _acc_dev->disconnected = 0;
- fp->private_data = _acc_dev;
- return 0;
- }
- static int acc_release(struct inode *ip, struct file *fp)
- {
- printk(KERN_INFO "acc_release\n");
- WARN_ON(!atomic_xchg(&_acc_dev->open_excl, 0));
- _acc_dev->disconnected = 0;
- return 0;
- }
- /* file operations for /dev/usb_accessory */
- static const struct file_operations acc_fops = {
- .owner = THIS_MODULE,
- .read = acc_read,
- .write = acc_write,
- .unlocked_ioctl = acc_ioctl,
- .open = acc_open,
- .release = acc_release,
- };
- static int acc_hid_probe(struct hid_device *hdev,
- const struct hid_device_id *id)
- {
- int ret;
- ret = hid_parse(hdev);
- if (ret)
- return ret;
- return hid_hw_start(hdev, HID_CONNECT_DEFAULT);
- }
- static struct miscdevice acc_device = {
- .minor = MISC_DYNAMIC_MINOR,
- .name = "usb_accessory",
- .fops = &acc_fops,
- };
- static const struct hid_device_id acc_hid_table[] = {
- { HID_USB_DEVICE(HID_ANY_ID, HID_ANY_ID) },
- { }
- };
- static struct hid_driver acc_hid_driver = {
- .name = "USB accessory",
- .id_table = acc_hid_table,
- .probe = acc_hid_probe,
- };
- static int acc_ctrlrequest(struct usb_composite_dev *cdev,
- const struct usb_ctrlrequest *ctrl)
- {
- struct acc_dev *dev = _acc_dev;
- int value = -EOPNOTSUPP;
- struct acc_hid_dev *hid;
- int offset;
- u8 b_requestType = ctrl->bRequestType;
- u8 b_request = ctrl->bRequest;
- u16 w_index = le16_to_cpu(ctrl->wIndex);
- u16 w_value = le16_to_cpu(ctrl->wValue);
- u16 w_length = le16_to_cpu(ctrl->wLength);
- unsigned long flags;
- /*
- printk(KERN_INFO "acc_ctrlrequest "
- "%02x.%02x v%04x i%04x l%u\n",
- b_requestType, b_request,
- w_value, w_index, w_length);
- */
- if (b_requestType == (USB_DIR_OUT | USB_TYPE_VENDOR)) {
- if (b_request == ACCESSORY_START) {
- dev->start_requested = 1;
- schedule_delayed_work(
- &dev->start_work, msecs_to_jiffies(200));
- value = 0;
- } else if (b_request == ACCESSORY_SEND_STRING) {
- dev->string_index = w_index;
- cdev->gadget->ep0->driver_data = dev;
- cdev->req->complete = acc_complete_set_string;
- value = w_length;
- } else if (b_request == ACCESSORY_SET_AUDIO_MODE &&
- w_index == 0 && w_length == 0) {
- dev->audio_mode = w_value;
- value = 0;
- } else if (b_request == ACCESSORY_REGISTER_HID) {
- value = acc_register_hid(dev, w_value, w_index);
- } else if (b_request == ACCESSORY_UNREGISTER_HID) {
- value = acc_unregister_hid(dev, w_value);
- } else if (b_request == ACCESSORY_SET_HID_REPORT_DESC) {
- spin_lock_irqsave(&dev->lock, flags);
- hid = acc_hid_get(&dev->new_hid_list, w_value);
- spin_unlock_irqrestore(&dev->lock, flags);
- if (!hid) {
- value = -EINVAL;
- goto err;
- }
- offset = w_index;
- if (offset != hid->report_desc_offset
- || offset + w_length > hid->report_desc_len) {
- value = -EINVAL;
- goto err;
- }
- cdev->req->context = hid;
- cdev->req->complete = acc_complete_set_hid_report_desc;
- value = w_length;
- } else if (b_request == ACCESSORY_SEND_HID_EVENT) {
- spin_lock_irqsave(&dev->lock, flags);
- hid = acc_hid_get(&dev->hid_list, w_value);
- spin_unlock_irqrestore(&dev->lock, flags);
- if (!hid) {
- value = -EINVAL;
- goto err;
- }
- cdev->req->context = hid;
- cdev->req->complete = acc_complete_send_hid_event;
- value = w_length;
- }
- } else if (b_requestType == (USB_DIR_IN | USB_TYPE_VENDOR)) {
- if (b_request == ACCESSORY_GET_PROTOCOL) {
- *((u16 *)cdev->req->buf) = PROTOCOL_VERSION;
- value = sizeof(u16);
- /* clear strings left over from a previous session */
- memset(dev->manufacturer, 0, sizeof(dev->manufacturer));
- memset(dev->model, 0, sizeof(dev->model));
- memset(dev->description, 0, sizeof(dev->description));
- memset(dev->version, 0, sizeof(dev->version));
- memset(dev->uri, 0, sizeof(dev->uri));
- memset(dev->serial, 0, sizeof(dev->serial));
- dev->start_requested = 0;
- dev->audio_mode = 0;
- }
- }
- if (value >= 0) {
- cdev->req->zero = 0;
- cdev->req->length = value;
- value = usb_ep_queue(cdev->gadget->ep0, cdev->req, GFP_ATOMIC);
- if (value < 0)
- ERROR(cdev, "%s setup response queue error\n",
- __func__);
- }
- err:
- if (value == -EOPNOTSUPP)
- VDBG(cdev,
- "unknown class-specific control req "
- "%02x.%02x v%04x i%04x l%u\n",
- ctrl->bRequestType, ctrl->bRequest,
- w_value, w_index, w_length);
- return value;
- }
- static int
- acc_function_bind(struct usb_configuration *c, struct usb_function *f)
- {
- struct usb_composite_dev *cdev = c->cdev;
- struct acc_dev *dev = func_to_dev(f);
- int id;
- int ret;
- DBG(cdev, "acc_function_bind dev: %pK\n", dev);
- ret = hid_register_driver(&acc_hid_driver);
- if (ret)
- return ret;
- dev->start_requested = 0;
- /* allocate interface ID(s) */
- id = usb_interface_id(c, f);
- if (id < 0)
- return id;
- acc_interface_desc.bInterfaceNumber = id;
- /* allocate endpoints */
- ret = create_bulk_endpoints(dev, &acc_fullspeed_in_desc,
- &acc_fullspeed_out_desc);
- if (ret)
- return ret;
- /* support high speed hardware */
- if (gadget_is_dualspeed(c->cdev->gadget)) {
- acc_highspeed_in_desc.bEndpointAddress =
- acc_fullspeed_in_desc.bEndpointAddress;
- acc_highspeed_out_desc.bEndpointAddress =
- acc_fullspeed_out_desc.bEndpointAddress;
- }
- /* support super speed hardware */
- if (gadget_is_superspeed(c->cdev->gadget)) {
- acc_superspeed_in_desc.bEndpointAddress =
- acc_fullspeed_in_desc.bEndpointAddress;
- acc_superspeed_out_desc.bEndpointAddress =
- acc_fullspeed_out_desc.bEndpointAddress;
- }
- DBG(cdev, "%s speed %s: IN/%s, OUT/%s\n",
- gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
- f->name, dev->ep_in->name, dev->ep_out->name);
- return 0;
- }
- static void
- kill_all_hid_devices(struct acc_dev *dev)
- {
- struct acc_hid_dev *hid;
- struct list_head *entry, *temp;
- unsigned long flags;
- /* do nothing if usb accessory device doesn't exist */
- if (!dev)
- return;
- spin_lock_irqsave(&dev->lock, flags);
- list_for_each_safe(entry, temp, &dev->hid_list) {
- hid = list_entry(entry, struct acc_hid_dev, list);
- list_del(&hid->list);
- list_add(&hid->list, &dev->dead_hid_list);
- }
- list_for_each_safe(entry, temp, &dev->new_hid_list) {
- hid = list_entry(entry, struct acc_hid_dev, list);
- list_del(&hid->list);
- list_add(&hid->list, &dev->dead_hid_list);
- }
- spin_unlock_irqrestore(&dev->lock, flags);
- schedule_work(&dev->hid_work);
- }
- static void
- acc_hid_unbind(struct acc_dev *dev)
- {
- hid_unregister_driver(&acc_hid_driver);
- kill_all_hid_devices(dev);
- }
- static void
- acc_function_unbind(struct usb_configuration *c, struct usb_function *f)
- {
- struct acc_dev *dev = func_to_dev(f);
- struct usb_request *req;
- int i;
- while ((req = req_get(dev, &dev->tx_idle)))
- acc_request_free(req, dev->ep_in);
- for (i = 0; i < RX_REQ_MAX; i++)
- acc_request_free(dev->rx_req[i], dev->ep_out);
- acc_hid_unbind(dev);
- }
- static void acc_start_work(struct work_struct *data)
- {
- char *envp[2] = { "ACCESSORY=START", NULL };
- kobject_uevent_env(&acc_device.this_device->kobj, KOBJ_CHANGE, envp);
- }
- static int acc_hid_init(struct acc_hid_dev *hdev)
- {
- struct hid_device *hid;
- int ret;
- hid = hid_allocate_device();
- if (IS_ERR(hid))
- return PTR_ERR(hid);
- hid->ll_driver = &acc_hid_ll_driver;
- hid->dev.parent = acc_device.this_device;
- hid->bus = BUS_USB;
- hid->vendor = HID_ANY_ID;
- hid->product = HID_ANY_ID;
- hid->driver_data = hdev;
- ret = hid_add_device(hid);
- if (ret) {
- pr_err("can't add hid device: %d\n", ret);
- hid_destroy_device(hid);
- return ret;
- }
- hdev->hid = hid;
- return 0;
- }
- static void acc_hid_delete(struct acc_hid_dev *hid)
- {
- kfree(hid->report_desc);
- kfree(hid);
- }
- static void acc_hid_work(struct work_struct *data)
- {
- struct acc_dev *dev = _acc_dev;
- struct list_head *entry, *temp;
- struct acc_hid_dev *hid;
- struct list_head new_list, dead_list;
- unsigned long flags;
- INIT_LIST_HEAD(&new_list);
- spin_lock_irqsave(&dev->lock, flags);
- /* copy hids that are ready for initialization to new_list */
- list_for_each_safe(entry, temp, &dev->new_hid_list) {
- hid = list_entry(entry, struct acc_hid_dev, list);
- if (hid->report_desc_offset == hid->report_desc_len)
- list_move(&hid->list, &new_list);
- }
- if (list_empty(&dev->dead_hid_list)) {
- INIT_LIST_HEAD(&dead_list);
- } else {
- /* move all of dev->dead_hid_list to dead_list */
- dead_list.prev = dev->dead_hid_list.prev;
- dead_list.next = dev->dead_hid_list.next;
- dead_list.next->prev = &dead_list;
- dead_list.prev->next = &dead_list;
- INIT_LIST_HEAD(&dev->dead_hid_list);
- }
- spin_unlock_irqrestore(&dev->lock, flags);
- /* register new HID devices */
- list_for_each_safe(entry, temp, &new_list) {
- hid = list_entry(entry, struct acc_hid_dev, list);
- if (acc_hid_init(hid)) {
- pr_err("can't add HID device %pK\n", hid);
- acc_hid_delete(hid);
- } else {
- spin_lock_irqsave(&dev->lock, flags);
- list_move(&hid->list, &dev->hid_list);
- spin_unlock_irqrestore(&dev->lock, flags);
- }
- }
- /* remove dead HID devices */
- list_for_each_safe(entry, temp, &dead_list) {
- hid = list_entry(entry, struct acc_hid_dev, list);
- list_del(&hid->list);
- if (hid->hid)
- hid_destroy_device(hid->hid);
- acc_hid_delete(hid);
- }
- }
- static int acc_function_set_alt(struct usb_function *f,
- unsigned intf, unsigned alt)
- {
- struct acc_dev *dev = func_to_dev(f);
- struct usb_composite_dev *cdev = f->config->cdev;
- int ret;
- DBG(cdev, "acc_function_set_alt intf: %d alt: %d\n", intf, alt);
- ret = config_ep_by_speed(cdev->gadget, f, dev->ep_in);
- if (ret) {
- dev->ep_in->desc = NULL;
- ERROR(cdev, "config_ep_by_speed failes for ep %s, result %d\n",
- dev->ep_in->name, ret);
- return ret;
- }
- ret = usb_ep_enable(dev->ep_in);
- if (ret) {
- ERROR(cdev, "failed to enable ep %s, result %d\n",
- dev->ep_in->name, ret);
- return ret;
- }
- ret = config_ep_by_speed(cdev->gadget, f, dev->ep_out);
- if (ret) {
- dev->ep_out->desc = NULL;
- ERROR(cdev, "config_ep_by_speed failes for ep %s, result %d\n",
- dev->ep_out->name, ret);
- usb_ep_disable(dev->ep_in);
- return ret;
- }
- ret = usb_ep_enable(dev->ep_out);
- if (ret) {
- ERROR(cdev, "failed to enable ep %s, result %d\n",
- dev->ep_out->name, ret);
- usb_ep_disable(dev->ep_in);
- return ret;
- }
- dev->online = 1;
- /* readers may be blocked waiting for us to go online */
- wake_up(&dev->read_wq);
- return 0;
- }
- static void acc_function_disable(struct usb_function *f)
- {
- struct acc_dev *dev = func_to_dev(f);
- struct usb_composite_dev *cdev = dev->cdev;
- DBG(cdev, "acc_function_disable\n");
- acc_set_disconnected(dev);
- usb_ep_disable(dev->ep_in);
- usb_ep_disable(dev->ep_out);
- /* readers may be blocked waiting for us to go online */
- wake_up(&dev->read_wq);
- VDBG(cdev, "%s disabled\n", dev->function.name);
- }
- static int acc_bind_config(struct usb_configuration *c)
- {
- struct acc_dev *dev = _acc_dev;
- int ret;
- printk(KERN_INFO "acc_bind_config\n");
- /* allocate a string ID for our interface */
- if (acc_string_defs[INTERFACE_STRING_INDEX].id == 0) {
- ret = usb_string_id(c->cdev);
- if (ret < 0)
- return ret;
- acc_string_defs[INTERFACE_STRING_INDEX].id = ret;
- acc_interface_desc.iInterface = ret;
- }
- dev->cdev = c->cdev;
- dev->function.name = "accessory";
- dev->function.strings = acc_strings,
- dev->function.fs_descriptors = fs_acc_descs;
- dev->function.hs_descriptors = hs_acc_descs;
- if (gadget_is_superspeed(c->cdev->gadget))
- dev->function.ss_descriptors = ss_acc_descs;
- dev->function.bind = acc_function_bind;
- dev->function.unbind = acc_function_unbind;
- dev->function.set_alt = acc_function_set_alt;
- dev->function.disable = acc_function_disable;
- return usb_add_function(c, &dev->function);
- }
- static int acc_setup(void)
- {
- struct acc_dev *dev;
- int ret;
- dev = kzalloc(sizeof(*dev), GFP_KERNEL);
- if (!dev)
- return -ENOMEM;
- spin_lock_init(&dev->lock);
- init_waitqueue_head(&dev->read_wq);
- init_waitqueue_head(&dev->write_wq);
- atomic_set(&dev->open_excl, 0);
- INIT_LIST_HEAD(&dev->tx_idle);
- INIT_LIST_HEAD(&dev->hid_list);
- INIT_LIST_HEAD(&dev->new_hid_list);
- INIT_LIST_HEAD(&dev->dead_hid_list);
- INIT_DELAYED_WORK(&dev->start_work, acc_start_work);
- INIT_WORK(&dev->hid_work, acc_hid_work);
- /* _acc_dev must be set before calling usb_gadget_register_driver */
- _acc_dev = dev;
- ret = misc_register(&acc_device);
- if (ret)
- goto err;
- return 0;
- err:
- kfree(dev);
- pr_err("USB accessory gadget driver failed to initialize\n");
- return ret;
- }
- static void acc_disconnect(void)
- {
- /* unregister all HID devices if USB is disconnected */
- kill_all_hid_devices(_acc_dev);
- }
- static void acc_cleanup(void)
- {
- misc_deregister(&acc_device);
- kfree(_acc_dev);
- _acc_dev = NULL;
- }
|