1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486 |
- /*
- * f_hid.c -- USB HID function driver
- *
- * Copyright (C) 2010 Fabien Chouteau <fabien.chouteau@barco.com>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- */
- #include <linux/kernel.h>
- #include <linux/module.h>
- #include <linux/hid.h>
- #include <linux/idr.h>
- #include <linux/cdev.h>
- #include <linux/mutex.h>
- #include <linux/poll.h>
- #include <linux/uaccess.h>
- #include <linux/wait.h>
- #include <linux/sched.h>
- #include <linux/usb/g_hid.h>
- #include "u_f.h"
- #include "u_hid.h"
- #include "f_apple_common.h"
- #define HIDG_MINORS 4
- #define USE_OUTPUT_EP 0
- static int major, minors;
- static struct class *hidg_class;
- static DEFINE_IDA(hidg_ida);
- static DEFINE_MUTEX(hidg_ida_lock); /* protects access to hidg_ida */
- /*-------------------------------------------------------------------------*/
- /* HID gadget struct */
- struct f_hidg_req_list {
- #if USE_OUTPUT_EP
- struct usb_request *req;
- #else
- char* set_report_buff;
- unsigned int set_report_length;
- #endif
- unsigned int pos;
- struct list_head list;
- };
- struct f_hidg {
- /* configuration */
- unsigned char bInterfaceSubClass;
- unsigned char bInterfaceProtocol;
- unsigned char protocol;
- unsigned short report_desc_length;
- char *report_desc;
- unsigned short report_length;
- /* recv report */
- struct list_head completed_out_req;
- spinlock_t read_spinlock;
- wait_queue_head_t read_queue;
- unsigned int qlen;
- /* send report */
- spinlock_t write_spinlock;
- bool write_pending;
- wait_queue_head_t write_queue;
- struct usb_request *req;
- int minor;
- struct cdev cdev;
- struct usb_function func;
- struct usb_ep *in_ep;
- #if USE_OUTPUT_EP
- struct usb_ep *out_ep;
- #endif
- uint8_t ep_addr_in;
- };
- int usb_dev_ready_notify(int is_ready);
- static inline struct f_hidg *func_to_hidg(struct usb_function *f)
- {
- return container_of(f, struct f_hidg, func);
- }
- /*-------------------------------------------------------------------------*/
- /* Static descriptors */
- static struct usb_interface_descriptor hidg_interface_desc = {
- .bLength = sizeof hidg_interface_desc,
- .bDescriptorType = USB_DT_INTERFACE,
- /* .bInterfaceNumber = DYNAMIC */
- .bAlternateSetting = 0,
- #if USE_OUTPUT_EP
- .bNumEndpoints = 2,
- #else
- .bNumEndpoints = 1,
- #endif
- .bInterfaceClass = USB_CLASS_HID,
- /* .bInterfaceSubClass = DYNAMIC */
- /* .bInterfaceProtocol = DYNAMIC */
- /* .iInterface = DYNAMIC */
- };
- static struct hid_descriptor hidg_desc = {
- .bLength = sizeof hidg_desc,
- .bDescriptorType = HID_DT_HID,
- .bcdHID = 0x0111,
- .bCountryCode = 0x00,
- .bNumDescriptors = 0x1,
- /*.desc[0].bDescriptorType = DYNAMIC */
- /*.desc[0].wDescriptorLenght = DYNAMIC */
- };
- /* Super-Speed Support */
- static struct usb_endpoint_descriptor hidg_ss_in_ep_desc = {
- .bLength = USB_DT_ENDPOINT_SIZE,
- .bDescriptorType = USB_DT_ENDPOINT,
- .bEndpointAddress = USB_DIR_IN,
- .bmAttributes = USB_ENDPOINT_XFER_INT,
- /*.wMaxPacketSize = DYNAMIC */
- .bInterval = 1, /* FIXME: Add this field in the
- * HID gadget configuration?
- * (struct hidg_func_descriptor)
- */
- };
- static struct usb_ss_ep_comp_descriptor hidg_ss_in_comp_desc = {
- .bLength = sizeof(hidg_ss_in_comp_desc),
- .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
- /* .bMaxBurst = 0, */
- /* .bmAttributes = 0, */
- /* .wBytesPerInterval = DYNAMIC */
- };
- #if USE_OUTPUT_EP
- static struct usb_endpoint_descriptor hidg_ss_out_ep_desc = {
- .bLength = USB_DT_ENDPOINT_SIZE,
- .bDescriptorType = USB_DT_ENDPOINT,
- .bEndpointAddress = USB_DIR_OUT,
- .bmAttributes = USB_ENDPOINT_XFER_INT,
- /*.wMaxPacketSize = DYNAMIC */
- .bInterval = 4, /* FIXME: Add this field in the
- * HID gadget configuration?
- * (struct hidg_func_descriptor)
- */
- };
- static struct usb_ss_ep_comp_descriptor hidg_ss_out_comp_desc = {
- .bLength = sizeof(hidg_ss_out_comp_desc),
- .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
- /* .bMaxBurst = 0, */
- /* .bmAttributes = 0, */
- /* .wBytesPerInterval = DYNAMIC */
- };
- #endif
- static struct usb_descriptor_header *hidg_ss_descriptors[] = {
- (struct usb_descriptor_header *)&hidg_interface_desc,
- (struct usb_descriptor_header *)&hidg_desc,
- (struct usb_descriptor_header *)&hidg_ss_in_ep_desc,
- (struct usb_descriptor_header *)&hidg_ss_in_comp_desc,
- #if USE_OUTPUT_EP
- (struct usb_descriptor_header *)&hidg_ss_out_ep_desc,
- (struct usb_descriptor_header *)&hidg_ss_out_comp_desc,
- #endif
- NULL,
- };
- /* High-Speed Support */
- static struct usb_endpoint_descriptor hidg_hs_in_ep_desc = {
- .bLength = USB_DT_ENDPOINT_SIZE,
- .bDescriptorType = USB_DT_ENDPOINT,
- .bEndpointAddress = USB_DIR_IN,
- .bmAttributes = USB_ENDPOINT_XFER_INT,
- /*.wMaxPacketSize = DYNAMIC */
- .bInterval = 1, /* FIXME: Add this field in the
- * HID gadget configuration?
- * (struct hidg_func_descriptor)
- */
- };
- #if USE_OUTPUT_EP
- static struct usb_endpoint_descriptor hidg_hs_out_ep_desc = {
- .bLength = USB_DT_ENDPOINT_SIZE,
- .bDescriptorType = USB_DT_ENDPOINT,
- .bEndpointAddress = USB_DIR_OUT,
- .bmAttributes = USB_ENDPOINT_XFER_INT,
- /*.wMaxPacketSize = DYNAMIC */
- .bInterval = 4, /* FIXME: Add this field in the
- * HID gadget configuration?
- * (struct hidg_func_descriptor)
- */
- };
- #endif
- static struct usb_descriptor_header *hidg_hs_descriptors[] = {
- (struct usb_descriptor_header *)&hidg_interface_desc,
- (struct usb_descriptor_header *)&hidg_desc,
- (struct usb_descriptor_header *)&hidg_hs_in_ep_desc,
- #if USE_OUTPUT_EP
- (struct usb_descriptor_header *)&hidg_hs_out_ep_desc,
- #endif
- NULL,
- };
- /* Full-Speed Support */
- static struct usb_endpoint_descriptor hidg_fs_in_ep_desc = {
- .bLength = USB_DT_ENDPOINT_SIZE,
- .bDescriptorType = USB_DT_ENDPOINT,
- .bEndpointAddress = USB_DIR_IN,
- .bmAttributes = USB_ENDPOINT_XFER_INT,
- /*.wMaxPacketSize = DYNAMIC */
- .bInterval = 1, /* FIXME: Add this field in the
- * HID gadget configuration?
- * (struct hidg_func_descriptor)
- */
- };
- #if USE_OUTPUT_EP
- static struct usb_endpoint_descriptor hidg_fs_out_ep_desc = {
- .bLength = USB_DT_ENDPOINT_SIZE,
- .bDescriptorType = USB_DT_ENDPOINT,
- .bEndpointAddress = USB_DIR_OUT,
- .bmAttributes = USB_ENDPOINT_XFER_INT,
- /*.wMaxPacketSize = DYNAMIC */
- .bInterval = 10, /* FIXME: Add this field in the
- * HID gadget configuration?
- * (struct hidg_func_descriptor)
- */
- };
- #endif
- static struct usb_descriptor_header *hidg_fs_descriptors[] = {
- (struct usb_descriptor_header *)&hidg_interface_desc,
- (struct usb_descriptor_header *)&hidg_desc,
- (struct usb_descriptor_header *)&hidg_fs_in_ep_desc,
- #if USE_OUTPUT_EP
- (struct usb_descriptor_header *)&hidg_fs_out_ep_desc,
- #endif
- NULL,
- };
- /*-------------------------------------------------------------------------*/
- /* Strings */
- #if 0
- #define CT_FUNC_HID_IDX 0
- static struct usb_string ct_func_string_defs[] = {
- [CT_FUNC_HID_IDX].s = "HID Interface",
- {}, /* end of list */
- };
- static struct usb_gadget_strings ct_func_string_table = {
- .language = 0x0409, /* en-US */
- .strings = ct_func_string_defs,
- };
- static struct usb_gadget_strings *ct_func_strings[] = {
- &ct_func_string_table,
- NULL,
- };
- #endif
- /*-------------------------------------------------------------------------*/
- /* Char Device */
- #if USE_OUTPUT_EP
- static ssize_t f_hidg_read(struct file *file, char __user *buffer,
- size_t count, loff_t *ptr)
- {
- struct f_hidg *hidg = file->private_data;
- struct f_hidg_req_list *list;
- struct usb_request *req;
- unsigned long flags;
- int ret;
- //printk("%s:%d count=%d\n", __func__, __LINE__, count);
- if (!count){printk("%s:%d err\n", __func__, __LINE__);
- return 0;}
- if (!access_ok(VERIFY_WRITE, buffer, count))
- return -EFAULT;
- spin_lock_irqsave(&hidg->read_spinlock, flags);
- #define READ_COND (!list_empty(&hidg->completed_out_req))
- /* wait for at least one buffer to complete */
- while (!READ_COND) {
- spin_unlock_irqrestore(&hidg->read_spinlock, flags);
- if (file->f_flags & O_NONBLOCK)
- return -EAGAIN;
- if (wait_event_interruptible(hidg->read_queue, READ_COND))
- return -ERESTARTSYS;
- spin_lock_irqsave(&hidg->read_spinlock, flags);
- }
- /* pick the first one */
- list = list_first_entry(&hidg->completed_out_req,
- struct f_hidg_req_list, list);
- /*
- * Remove this from list to protect it from beign free()
- * while host disables our function
- */
- list_del(&list->list);
- req = list->req;
- count = min_t(unsigned int, count, req->actual - list->pos);
- spin_unlock_irqrestore(&hidg->read_spinlock, flags);
- /* copy to user outside spinlock */
- count -= copy_to_user(buffer, req->buf + list->pos, count);
- list->pos += count;
- /*
- * if this request is completely handled and transfered to
- * userspace, remove its entry from the list and requeue it
- * again. Otherwise, we will revisit it again upon the next
- * call, taking into account its current read position.
- */
- if (list->pos == req->actual) {
- kfree(list);
- req->length = hidg->report_length;
- ret = usb_ep_queue(hidg->out_ep, req, GFP_KERNEL);
- if (ret < 0) {
- free_ep_req(hidg->out_ep, req);
- return ret;
- }
- } else {
- spin_lock_irqsave(&hidg->read_spinlock, flags);
- list_add(&list->list, &hidg->completed_out_req);
- spin_unlock_irqrestore(&hidg->read_spinlock, flags);
- wake_up(&hidg->read_queue);
- }
- //printk("%s:%d end\n", __func__, __LINE__);
- return count;
- }
- #else
- static ssize_t f_hidg_read(struct file *file, char __user *buffer,
- size_t count, loff_t *ptr)
- {
- struct f_hidg *hidg = file->private_data;
- struct f_hidg_req_list *list;
- unsigned long flags;
- if (!count)
- return 0;
- if (!access_ok(VERIFY_WRITE, buffer, count))
- return -EFAULT;
- spin_lock_irqsave(&hidg->read_spinlock, flags);
- #define READ_COND (!list_empty(&hidg->completed_out_req))
- /* wait for at least one buffer to complete */
- while (!READ_COND) {
- spin_unlock_irqrestore(&hidg->read_spinlock, flags);
- if (file->f_flags & O_NONBLOCK)
- return -EAGAIN;
- if (wait_event_interruptible(hidg->read_queue, READ_COND))
- return -ERESTARTSYS;
- spin_lock_irqsave(&hidg->read_spinlock, flags);
- }
- /* pick the first one */
- list = list_first_entry(&hidg->completed_out_req,
- struct f_hidg_req_list, list);
- /*
- * Remove this from list to protect it from beign free()
- * while host disables our function
- */
- list_del(&list->list);
- count = min_t(unsigned int, count, list->set_report_length - list->pos);
- spin_unlock_irqrestore(&hidg->read_spinlock, flags);
- /* copy to user outside spinlock */
- count -= copy_to_user(buffer, list->set_report_buff + list->pos, count);
- list->pos += count;
- /*
- * if this request is completely handled and transfered to
- * userspace, remove its entry from the list and requeue it
- * again. Otherwise, we will revisit it again upon the next
- * call, taking into account its current read position.
- */
- if (list->pos == list->set_report_length) {
- if (NULL != list->set_report_buff)
- kfree(list->set_report_buff);
- kfree(list);
- } else {
- spin_lock_irqsave(&hidg->read_spinlock, flags);
- list_add(&list->list, &hidg->completed_out_req);
- spin_unlock_irqrestore(&hidg->read_spinlock, flags);
- wake_up(&hidg->read_queue);
- }
- return count;
- }
- #endif
- static void f_hidg_req_complete(struct usb_ep *ep, struct usb_request *req)
- {
- struct f_hidg *hidg = (struct f_hidg *)ep->driver_data;
- unsigned long flags;
- if (req->status != 0) {
- ERROR(hidg->func.config->cdev,
- "End Point Request ERROR: %d\n", req->status);
- }
- spin_lock_irqsave(&hidg->write_spinlock, flags);
- hidg->write_pending = 0;
- spin_unlock_irqrestore(&hidg->write_spinlock, flags);
- wake_up(&hidg->write_queue);
- }
- static ssize_t f_hidg_write(struct file *file, const char __user *buffer,
- size_t count, loff_t *offp)
- {
- struct f_hidg *hidg = file->private_data;
- struct usb_request *req;
- unsigned long flags;
- ssize_t status = -ENOMEM;
- if (!access_ok(VERIFY_READ, buffer, count))
- return -EFAULT;
- spin_lock_irqsave(&hidg->write_spinlock, flags);
- #define WRITE_COND (!hidg->write_pending)
- try_again:
- /* write queue */
- while (!WRITE_COND) {
- spin_unlock_irqrestore(&hidg->write_spinlock, flags);
- if (file->f_flags & O_NONBLOCK)
- return -EAGAIN;
- if (wait_event_interruptible_exclusive(
- hidg->write_queue, WRITE_COND))
- return -ERESTARTSYS;
- spin_lock_irqsave(&hidg->write_spinlock, flags);
- }
- hidg->write_pending = 1;
- req = hidg->req;
- count = min_t(unsigned, count, hidg->report_length);
- spin_unlock_irqrestore(&hidg->write_spinlock, flags);
- status = copy_from_user(req->buf, buffer, count);
- if (status != 0) {
- ERROR(hidg->func.config->cdev,
- "copy_from_user error\n");
- status = -EINVAL;
- goto release_write_pending;
- }
- spin_lock_irqsave(&hidg->write_spinlock, flags);
- /* when our function has been disabled by host */
- if (!hidg->req) {
- free_ep_req(hidg->in_ep, req);
- /*
- * TODO
- * Should we fail with error here?
- */
- goto try_again;
- }
- req->status = 0;
- req->zero = 0;
- req->length = count;
- req->complete = f_hidg_req_complete;
- req->context = hidg;
- status = usb_ep_queue(hidg->in_ep, req, GFP_ATOMIC);
- if (status < 0) {
- ERROR(hidg->func.config->cdev,
- "usb_ep_queue error on int endpoint %zd\n", status);
- goto release_write_pending_unlocked;
- } else {
- status = count;
- }
- spin_unlock_irqrestore(&hidg->write_spinlock, flags);
- return status;
- release_write_pending:
- spin_lock_irqsave(&hidg->write_spinlock, flags);
- release_write_pending_unlocked:
- hidg->write_pending = 0;
- spin_unlock_irqrestore(&hidg->write_spinlock, flags);
- wake_up(&hidg->write_queue);
- return status;
- }
- static unsigned int f_hidg_poll(struct file *file, poll_table *wait)
- {
- struct f_hidg *hidg = file->private_data;
- unsigned int ret = 0;
- poll_wait(file, &hidg->read_queue, wait);
- poll_wait(file, &hidg->write_queue, wait);
- if (WRITE_COND)
- ret |= POLLOUT | POLLWRNORM;
- if (READ_COND)
- ret |= POLLIN | POLLRDNORM;
- return ret;
- }
- #undef WRITE_COND
- #undef READ_COND
- static int f_hidg_release(struct inode *inode, struct file *fd)
- {
- fd->private_data = NULL;
- return 0;
- }
- static int f_hidg_open(struct inode *inode, struct file *fd)
- {
- struct f_hidg *hidg =
- container_of(inode->i_cdev, struct f_hidg, cdev);
- fd->private_data = hidg;
- return 0;
- }
- /*-------------------------------------------------------------------------*/
- /* usb_function */
- static inline struct usb_request *hidg_alloc_ep_req(struct usb_ep *ep,
- unsigned length)
- {
- return alloc_ep_req(ep, length);
- }
- //#if USE_OUTPUT_EP
- static void hidg_set_report_complete(struct usb_ep *ep, struct usb_request *req)
- {
- struct f_hidg *hidg = (struct f_hidg *) req->context;
- struct usb_composite_dev *cdev = hidg->func.config->cdev;
- struct f_hidg_req_list *req_list;
- unsigned long flags;
- switch (req->status) {
- case 0:
- req_list = kzalloc(sizeof(*req_list), GFP_ATOMIC);
- if (!req_list) {
- ERROR(cdev, "Unable to allocate mem for req_list\n");
- goto free_req;
- }
- #if USE_OUTPUT_EP
- req_list->req = req;
- #else
- req_list->set_report_buff = kzalloc(req->actual, GFP_ATOMIC);
- if (NULL == req_list->set_report_buff) {
- ERROR(cdev, "Unable to allocate mem for set_report_buff\n");
- goto free_req;
- }
-
- req_list->set_report_length = req->actual;
- memcpy(req_list->set_report_buff, req->buf, req->actual);
- #endif
- spin_lock_irqsave(&hidg->read_spinlock, flags);
- list_add_tail(&req_list->list, &hidg->completed_out_req);
- spin_unlock_irqrestore(&hidg->read_spinlock, flags);
- wake_up(&hidg->read_queue);
- break;
- default:
- ERROR(cdev, "Set report failed %d\n", req->status);
- /* FALLTHROUGH */
- case -ECONNABORTED: /* hardware forced ep reset */
- case -ECONNRESET: /* request dequeued */
- case -ESHUTDOWN: /* disconnect from host */
- free_req:
- #if USE_OUTPUT_EP
- free_ep_req(ep, req);
- #endif
- return;
- }
- }
- //#endif
- #if 0
- static bool f_hid_sim_req_match(struct usb_function *f,
- const struct usb_ctrlrequest *ctrl,
- bool config0)
- {
- /*printk("###hid req match %02x.%02x v%04x i%04x l%d\n",
- ctrl->bRequestType, ctrl->bRequest,
- ctrl->wValue, ctrl->wIndex, ctrl->wLength);*/
-
- switch (ctrl->bRequest) {
- case 0x53:
- if (0xc0 == ctrl->bRequestType)
- return true;
- case 0x51:
- if (0x40 == ctrl->bRequestType)
- return true;
- default:
- return false;
- }
- return false;
- }
- #endif
- static int hidg_setup(struct usb_function *f,
- const struct usb_ctrlrequest *ctrl)
- {
- struct f_hidg *hidg = func_to_hidg(f);
- struct usb_composite_dev *cdev = f->config->cdev;
- struct usb_request *req = cdev->req;
- int status = 0;
- __u16 value, length;
- //char result[4] = {0x01, 0x00, 0x00, 0x00};
- value = __le16_to_cpu(ctrl->wValue);
- length = __le16_to_cpu(ctrl->wLength);
- VDBG(cdev,
- "%s crtl_request : bRequestType:0x%x bRequest:0x%x Value:0x%x\n",
- __func__, ctrl->bRequestType, ctrl->bRequest, value);
- switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
- case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
- | HID_REQ_GET_REPORT):
- VDBG(cdev, "get_report\n");
- printk("get_report\n");
- /* send an empty report */
- length = min_t(unsigned, length, hidg->report_length);
- memset(req->buf, 0x1, length);
- goto stall;
- break;
- case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
- | HID_REQ_GET_PROTOCOL):
- VDBG(cdev, "get_protocol\n");
- length = min_t(unsigned int, length, 1);
- ((u8 *) req->buf)[0] = hidg->protocol;
- goto stall;
- break;
- case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
- | HID_REQ_SET_REPORT):
- VDBG(cdev, "set_report | wLength=%d\n", ctrl->wLength);
- //goto stall;
- req->complete = hidg_set_report_complete;
- req->context = (void*)hidg;
- goto respond;
- break;
- case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
- | HID_REQ_SET_PROTOCOL):
- VDBG(cdev, "set_protocol\n");
- if (value > HID_REPORT_PROTOCOL)
- goto stall;
- length = 0;
- /*
- * We assume that programs implementing the Boot protocol
- * are also compatible with the Report Protocol
- */
- if (hidg->bInterfaceSubClass == USB_INTERFACE_SUBCLASS_BOOT) {
- hidg->protocol = value;
- goto respond;
- }
- goto stall;
- break;
- case ((USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_INTERFACE) << 8
- | USB_REQ_GET_DESCRIPTOR):
- switch (value >> 8) {
- case HID_DT_HID:
- {
- struct hid_descriptor hidg_desc_copy = hidg_desc;
- VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: HID\n");
- hidg_desc_copy.desc[0].bDescriptorType = HID_DT_REPORT;
- hidg_desc_copy.desc[0].wDescriptorLength =
- cpu_to_le16(hidg->report_desc_length);
- length = min_t(unsigned short, length,
- hidg_desc_copy.bLength);
- memcpy(req->buf, &hidg_desc_copy, length);
- goto respond;
- break;
- }
- case HID_DT_REPORT:
- VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: REPORT\n");
- length = min_t(unsigned short, length,
- hidg->report_desc_length);
- memcpy(req->buf, hidg->report_desc, length);
- goto respond;
- break;
- default:
- VDBG(cdev, "Unknown descriptor request 0x%x\n",
- value >> 8);
- goto stall;
- break;
- }
- break;
- #if 0
- case (0x40 << 8) | 0x51 :
- if (ctrl->bRequest != 0x51)
- break;
- printk("%s %x-->receive switch ctrl\n", __func__, ctrl->bRequest);
- value = 0;
- usb_dev_ready_notify(1);
- goto respond;
- break;
- case (0xc0 << 8) | 0x53 :
- if (ctrl->bRequest != 0x53)
- break;
- printk("%s %x-->##receive query ctrl\n", __func__, ctrl->bRequest);
- length = 4;
- memcpy(req->buf, result, length);
- goto respond;
- break;
- #endif
- default:
- VDBG(cdev, "Unknown request 0x%x\n",
- ctrl->bRequest);
- goto stall;
- break;
- }
- stall:
- return -EOPNOTSUPP;
- respond:
- req->zero = 0;
- req->length = length;
- status = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
- if (status < 0)
- ERROR(cdev, "usb_ep_queue error on ep0 %d\n", value);
- return status;
- }
- static void hidg_disable(struct usb_function *f)
- {
- struct f_hidg *hidg = func_to_hidg(f);
- struct f_hidg_req_list *list, *next;
- unsigned long flags;
- //usb_dev_ready_notify(0);
- usb_ep_disable(hidg->in_ep);
- #if USE_OUTPUT_EP
- usb_ep_disable(hidg->out_ep);
- #endif
- spin_lock_irqsave(&hidg->read_spinlock, flags);
- list_for_each_entry_safe(list, next, &hidg->completed_out_req, list) {
- #if USE_OUTPUT_EP
- free_ep_req(hidg->out_ep, list->req);
- #else
- if (list->set_report_buff)
- kfree(list->set_report_buff);
- #endif
- list_del(&list->list);
- kfree(list);
- }
- spin_unlock_irqrestore(&hidg->read_spinlock, flags);
- spin_lock_irqsave(&hidg->write_spinlock, flags);
- if (!hidg->write_pending) {
- free_ep_req(hidg->in_ep, hidg->req);
- hidg->write_pending = 1;
- }
- hidg->req = NULL;
- spin_unlock_irqrestore(&hidg->write_spinlock, flags);
- #if EP_ASSIGN_AT_CONN
- usb_ep_autoconfig_release(hidg->in_ep);
- hidg->in_ep = NULL;
- #endif
- }
- static int hidg_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
- {
- struct usb_composite_dev *cdev = f->config->cdev;
- struct f_hidg *hidg = func_to_hidg(f);
- struct usb_request *req_in = NULL;
- unsigned long flags;
- #if USE_OUTPUT_EP
- int i, status = 0;
- #else
- int status = 0;
- #endif
- VDBG(cdev, "hidg_set_alt intf:%d alt:%d\n", intf, alt);printk("hidg_set_alt intf:%d alt:%d\n", intf, alt);
- printk("%s:%d\n", __func__, __LINE__);
- #if EP_ASSIGN_AT_CONN
- if (hidg->in_ep == NULL) {
- struct usb_ep *ep;
- ep = usb_ep_autoconfig_ex(f->config->cdev->gadget, &hidg_fs_in_ep_desc, hidg->ep_addr_in);
- if (!ep)
- goto fail;
- hidg->in_ep = ep;
- }
- #endif
- if (hidg->in_ep != NULL) {
- /* restart endpoint */
- usb_ep_disable(hidg->in_ep);
- status = config_ep_by_speed(f->config->cdev->gadget, f,
- hidg->in_ep);
- if (status) {
- ERROR(cdev, "config_ep_by_speed FAILED!\n");
- goto fail;
- }
- status = usb_ep_enable(hidg->in_ep);
- if (status < 0) {
- ERROR(cdev, "Enable IN endpoint FAILED!\n");
- goto fail;
- }
- hidg->in_ep->driver_data = hidg;
- req_in = hidg_alloc_ep_req(hidg->in_ep, hidg->report_length);
- if (!req_in) {
- status = -ENOMEM;
- goto disable_ep_in;
- }
- usb_dev_ready_notify(2);
- }
- #if USE_OUTPUT_EP
- if (hidg->out_ep != NULL) {
- /* restart endpoint */
- usb_ep_disable(hidg->out_ep);
- status = config_ep_by_speed(f->config->cdev->gadget, f,
- hidg->out_ep);
- if (status) {
- ERROR(cdev, "config_ep_by_speed FAILED!\n");
- goto free_req_in;
- }
- status = usb_ep_enable(hidg->out_ep);
- if (status < 0) {
- ERROR(cdev, "Enable OUT endpoint FAILED!\n");
- goto free_req_in;
- }
- hidg->out_ep->driver_data = hidg;
- /*
- * allocate a bunch of read buffers and queue them all at once.
- */
- for (i = 0; i < hidg->qlen && status == 0; i++) {
- struct usb_request *req =
- hidg_alloc_ep_req(hidg->out_ep,
- hidg->report_length);
- if (req) {
- req->complete = hidg_set_report_complete;
- req->context = hidg;
- status = usb_ep_queue(hidg->out_ep, req,
- GFP_ATOMIC);
- if (status) {
- ERROR(cdev, "%s queue req --> %d\n",
- hidg->out_ep->name, status);
- free_ep_req(hidg->out_ep, req);
- }
- } else {
- status = -ENOMEM;
- goto disable_out_ep;
- }
- }
- }
- #endif
- if (hidg->in_ep != NULL) {
- spin_lock_irqsave(&hidg->write_spinlock, flags);
- hidg->req = req_in;
- hidg->write_pending = 0;
- spin_unlock_irqrestore(&hidg->write_spinlock, flags);
- wake_up(&hidg->write_queue);
- }
- return 0;
- #if USE_OUTPUT_EP
- disable_out_ep:
- usb_ep_disable(hidg->out_ep);
- free_req_in:
- if (req_in)
- free_ep_req(hidg->in_ep, req_in);
- #endif
- disable_ep_in:
- if (hidg->in_ep)
- usb_ep_disable(hidg->in_ep);
- fail:
- return status;
- }
- static const struct file_operations f_hidg_fops = {
- .owner = THIS_MODULE,
- .open = f_hidg_open,
- .release = f_hidg_release,
- .write = f_hidg_write,
- .read = f_hidg_read,
- .poll = f_hidg_poll,
- .llseek = noop_llseek,
- };
- static int hidg_bind(struct usb_configuration *c, struct usb_function *f)
- {
- #if EP_ASSIGN_AT_CONN == 0
- struct usb_ep *ep;
- struct usb_ep *ep1;
- #endif
- struct f_hidg *hidg = func_to_hidg(f);
- //struct usb_string *us;
- struct device *device;
- int status;
- dev_t dev;
- printk("%s:%d\n", __func__, __LINE__);
- /* maybe allocate device-global string IDs, and patch descriptors */
- /*us = usb_gstrings_attach(c->cdev, ct_func_strings,
- ARRAY_SIZE(ct_func_string_defs));
- if (IS_ERR(us))
- return PTR_ERR(us);
- hidg_interface_desc.iInterface = us[CT_FUNC_HID_IDX].id;*/
- /* allocate instance-specific interface IDs, and patch descriptors */
- status = usb_interface_id(c, f);
- if (status < 0)
- goto fail;
- hidg_interface_desc.bInterfaceNumber = status;
- /* allocate instance-specific endpoints */
- status = -ENODEV;
-
- #if EP_ASSIGN_AT_CONN
- hidg->ep_addr_in = 0x83;
- hidg_fs_in_ep_desc.bEndpointAddress = hidg->ep_addr_in;
- #else
- ep1 = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_in_ep_desc);
- ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_in_ep_desc);
- if (!ep)
- goto fail;
- hidg->in_ep = ep;
- usb_ep_autoconfig_release(ep1);
- #endif
- #if USE_OUTPUT_EP
- ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_out_ep_desc);
- if (!ep)
- goto fail;
- hidg->out_ep = ep;
- #endif
- /* set descriptor dynamic values */
- hidg_interface_desc.bInterfaceSubClass = hidg->bInterfaceSubClass;
- hidg_interface_desc.bInterfaceProtocol = hidg->bInterfaceProtocol;
- hidg->protocol = HID_REPORT_PROTOCOL;
- hidg_ss_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
- hidg_ss_in_comp_desc.wBytesPerInterval =
- cpu_to_le16(hidg->report_length);
- hidg_hs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
- hidg_fs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
- #if USE_OUTPUT_EP
- hidg_ss_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
- hidg_ss_out_comp_desc.wBytesPerInterval =
- cpu_to_le16(hidg->report_length);
- hidg_hs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
- hidg_fs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
- #endif
- /*
- * We can use hidg_desc struct here but we should not relay
- * that its content won't change after returning from this function.
- */
- hidg_desc.desc[0].bDescriptorType = HID_DT_REPORT;
- hidg_desc.desc[0].wDescriptorLength =
- cpu_to_le16(hidg->report_desc_length);
- hidg_hs_in_ep_desc.bEndpointAddress =
- hidg_fs_in_ep_desc.bEndpointAddress;
- #if USE_OUTPUT_EP
- hidg_hs_out_ep_desc.bEndpointAddress =
- hidg_fs_out_ep_desc.bEndpointAddress;
- #endif
- hidg_ss_in_ep_desc.bEndpointAddress =
- hidg_fs_in_ep_desc.bEndpointAddress;
- #if USE_OUTPUT_EP
- hidg_ss_out_ep_desc.bEndpointAddress =
- hidg_fs_out_ep_desc.bEndpointAddress;
- #endif
- status = usb_assign_descriptors(f, hidg_fs_descriptors,
- hidg_hs_descriptors, hidg_ss_descriptors, NULL);
- if (status)
- goto fail;
- spin_lock_init(&hidg->write_spinlock);
- hidg->write_pending = 1;
- hidg->req = NULL;
- spin_lock_init(&hidg->read_spinlock);
- init_waitqueue_head(&hidg->write_queue);
- init_waitqueue_head(&hidg->read_queue);
- INIT_LIST_HEAD(&hidg->completed_out_req);
- /* create char device */
- cdev_init(&hidg->cdev, &f_hidg_fops);
- dev = MKDEV(major, hidg->minor);
- status = cdev_add(&hidg->cdev, dev, 1);
- if (status)
- goto fail_free_descs;
- device = device_create(hidg_class, NULL, dev, NULL,
- "%s%d", "hidg", hidg->minor);
- if (IS_ERR(device)) {
- status = PTR_ERR(device);
- goto del;
- }
- return 0;
- del:
- cdev_del(&hidg->cdev);
- fail_free_descs:
- usb_free_all_descriptors(f);
- fail:
- ERROR(f->config->cdev, "hidg_bind FAILED\n");
- if (hidg->req != NULL)
- free_ep_req(hidg->in_ep, hidg->req);
- return status;
- }
- static inline int hidg_get_minor(void)
- {
- int ret;
- ret = ida_simple_get(&hidg_ida, 0, 0, GFP_KERNEL);
- if (ret >= HIDG_MINORS) {
- ida_simple_remove(&hidg_ida, ret);
- ret = -ENODEV;
- }
- return ret;
- }
- static inline struct f_hid_opts *to_f_hid_opts(struct config_item *item)
- {
- return container_of(to_config_group(item), struct f_hid_opts,
- func_inst.group);
- }
- static void hid_attr_release(struct config_item *item)
- {
- struct f_hid_opts *opts = to_f_hid_opts(item);
- usb_put_function_instance(&opts->func_inst);
- }
- static struct configfs_item_operations hidg_item_ops = {
- .release = hid_attr_release,
- };
- #define F_HID_OPT(name, prec, limit) \
- static ssize_t f_hid_opts_##name##_show(struct config_item *item, char *page)\
- { \
- struct f_hid_opts *opts = to_f_hid_opts(item); \
- int result; \
- \
- mutex_lock(&opts->lock); \
- result = sprintf(page, "%d\n", opts->name); \
- mutex_unlock(&opts->lock); \
- \
- return result; \
- } \
- \
- static ssize_t f_hid_opts_##name##_store(struct config_item *item, \
- const char *page, size_t len) \
- { \
- struct f_hid_opts *opts = to_f_hid_opts(item); \
- int ret; \
- u##prec num; \
- \
- mutex_lock(&opts->lock); \
- if (opts->refcnt) { \
- ret = -EBUSY; \
- goto end; \
- } \
- \
- ret = kstrtou##prec(page, 0, &num); \
- if (ret) \
- goto end; \
- \
- if (num > limit) { \
- ret = -EINVAL; \
- goto end; \
- } \
- opts->name = num; \
- ret = len; \
- \
- end: \
- mutex_unlock(&opts->lock); \
- return ret; \
- } \
- \
- CONFIGFS_ATTR(f_hid_opts_, name)
- F_HID_OPT(subclass, 8, 255);
- F_HID_OPT(protocol, 8, 255);
- F_HID_OPT(report_length, 16, 65535);
- static ssize_t f_hid_opts_report_desc_show(struct config_item *item, char *page)
- {
- struct f_hid_opts *opts = to_f_hid_opts(item);
- int result;
- mutex_lock(&opts->lock);
- result = opts->report_desc_length;
- memcpy(page, opts->report_desc, opts->report_desc_length);
- mutex_unlock(&opts->lock);
- return result;
- }
- static ssize_t f_hid_opts_report_desc_store(struct config_item *item,
- const char *page, size_t len)
- {
- struct f_hid_opts *opts = to_f_hid_opts(item);
- int ret = -EBUSY;
- char *d;
- mutex_lock(&opts->lock);
- if (opts->refcnt)
- goto end;
- if (len > PAGE_SIZE) {
- ret = -ENOSPC;
- goto end;
- }
- d = kmemdup(page, len, GFP_KERNEL);
- if (!d) {
- ret = -ENOMEM;
- goto end;
- }
- kfree(opts->report_desc);
- opts->report_desc = d;
- opts->report_desc_length = len;
- opts->report_desc_alloc = true;
- ret = len;
- end:
- mutex_unlock(&opts->lock);
- return ret;
- }
- CONFIGFS_ATTR(f_hid_opts_, report_desc);
- static ssize_t f_hid_opts_dev_show(struct config_item *item, char *page)
- {
- struct f_hid_opts *opts = to_f_hid_opts(item);
- return sprintf(page, "%d:%d\n", major, opts->minor);
- }
- CONFIGFS_ATTR_RO(f_hid_opts_, dev);
- static struct configfs_attribute *hid_attrs[] = {
- &f_hid_opts_attr_subclass,
- &f_hid_opts_attr_protocol,
- &f_hid_opts_attr_report_length,
- &f_hid_opts_attr_report_desc,
- &f_hid_opts_attr_dev,
- NULL,
- };
- static struct config_item_type hid_func_type = {
- .ct_item_ops = &hidg_item_ops,
- .ct_attrs = hid_attrs,
- .ct_owner = THIS_MODULE,
- };
- static inline void hidg_put_minor(int minor)
- {
- ida_simple_remove(&hidg_ida, minor);
- }
- static void hidg_free_inst(struct usb_function_instance *f)
- {
- struct f_hid_opts *opts;
- opts = container_of(f, struct f_hid_opts, func_inst);
- mutex_lock(&hidg_ida_lock);
- hidg_put_minor(opts->minor);
- if (ida_is_empty(&hidg_ida))
- ghid_cleanup();
- mutex_unlock(&hidg_ida_lock);
- if (opts->report_desc_alloc)
- kfree(opts->report_desc);
- printk("%s:%d\n", __func__, __LINE__);
- kfree(opts);
- }
- static char g_report_desc[] = {
- 0x06, 0x00, 0xFF, //HID_UsagePageVendor(0)
- 0x09, 0x01, //HID_Usage(1)
- 0xA1, 0x01, //HID_Collection(1) application
- 0x75, 0x08, //HID_ReportSize(8)
- 0x26, 0x80, 0x00, //HID_LogicalMax(128)
- 0x15, 0x00, //HID_LogicalMin(0)
- 0x09, 0x01, //HID_Usage(1)
- 0x85, 0x01, //HID_ReportID(1)
- 0x95, 0x05, //HID_ReportCount(5)
- 0x82, 0x02, 0x01, //input(data, var, abs)
- 0x09, 0x01, //HID_Usage(1)
- 0x85, 0x02, //HID_ReportID(2)
- 0x95, 0x09, //HID_ReportCount(9)
- 0x82, 0x02, 0x01, //input(data, var, abs)
- 0x09, 0x01, //HID_Usage(1)
- 0x85, 0x03, //HID_ReportID(3)
- 0x95, 0x0D, //HID_ReportCount(0x0D)
- 0x82, 0x02, 0x01, //input(data, var, abs)
- 0x09, 0x01, //HID_Usage(1)
- 0x85, 0x04, //HID_ReportID(4)
- 0x95, 0x11, //HID_ReportCount(0x11)
- 0x82, 0x02, 0x01, //input(data, var, abs)
- 0x09, 0x01, //HID_Usage(1)
- 0x85, 0x05, //HID_ReportID(5)
- 0x95, 0x19, //HID_ReportCount(0x19)
- 0x82, 0x02, 0x01, //input(data, var, abs)
- 0x09, 0x01, //HID_Usage(1)
- 0x85, 0x06, //HID_ReportID(6)
- 0x95, 0x31, //HID_ReportCount(0x31)
- 0x82, 0x02, 0x01, //input(data, var, abs)
- 0x09, 0x01, //HID_Usage(1)
- 0x85, 0x07, //HID_ReportID(7)
- 0x95, 0x5F, //HID_ReportCount(0x5F)
- 0x82, 0x02, 0x01, //input(data, var, abs)
- 0x09, 0x01, //HID_Usage(1)
- 0x85, 0x08, //HID_ReportID(8)
- 0x95, 0xC1, //HID_ReportCount(0xC1)
- 0x82, 0x02, 0x01, //input(data, var, abs)
- 0x09, 0x01, //HID_Usage(1)
- 0x85, 0x09, //HID_ReportID(9)
- 0x96, 0x01, 0x01, //HID_ReportCount(0x101)
- 0x82, 0x02, 0x01, //input(data, var, abs)
- 0x09, 0x01, //HID_Usage(1)
- 0x85, 0x0A, //HID_ReportID(0x0A)
- 0x96, 0x81, 0x01, //HID_ReportCount(0x181)
- 0x82, 0x02, 0x01, //input(data, var, abs)
- 0x09, 0x01, //HID_Usage(1)
- 0x85, 0x0B, //HID_ReportID(0x0B)
- 0x96, 0x01, 0x02, //HID_ReportCount(0x201)
- 0x82, 0x02, 0x01, //input(data, var, abs)
- 0x09, 0x01, //HID_Usage(1)
- 0x85, 0x0C, //HID_ReportID(0x0C)
- 0x96, 0xFF, 0x02, //HID_ReportCount(0x2FF)
- 0x82, 0x02, 0x01, //input(data, var, abs)
- 0x09, 0x01, //HID_Usage(1)
- //output
- 0x85, 0x0D, //HID_ReportID(0x0D)
- 0x95, 0x05, //HID_ReportCount(0x05)
- 0x92, 0x02, 0x01, //output(data, var, abs)
- 0x09, 0x01, //HID_Usage(1)
- 0x85, 0x0E, //HID_ReportID(0x0E)
- 0x95, 0x09, //HID_ReportCount(0x09)
- 0x92, 0x02, 0x01, //output(data, var, abs)
- 0x09, 0x01, //HID_Usage(1)
- 0x85, 0x0F, //HID_ReportID(0x0F)
- 0x95, 0x0D, //HID_ReportCount(0x0D)
- 0x92, 0x02, 0x01, //output(data, var, abs)
- 0x09, 0x01, //HID_Usage(1)
- 0x85, 0x10, //HID_ReportID(0x10)
- 0x95, 0x11, //HID_ReportCount(0x11)
- 0x92, 0x02, 0x01, //output(data, var, abs)
- 0x09, 0x01, //HID_Usage(1)
- 0x85, 0x11, //HID_ReportID(0x11)
- 0x95, 0x19, //HID_ReportCount(0x19)
- 0x92, 0x02, 0x01, //output(data, var, abs)
- 0x09, 0x01, //HID_Usage(1)
- 0x85, 0x12, //HID_ReportID(0x12)
- 0x95, 0x31, //HID_ReportCount(0x31)
- 0x92, 0x02, 0x01, //output(data, var, abs)
- 0x09, 0x01, //HID_Usage(1)
- 0x85, 0x13, //HID_ReportID(0x13)
- 0x95, 0x5F, //HID_ReportCount(0x5F)
- 0x92, 0x02, 0x01, //output(data, var, abs)
- 0x09, 0x01, //HID_Usage(1)
- 0x85, 0x14, //HID_ReportID(0x14)
- 0x95, 0xC1, //HID_ReportCount(0xC1)
- 0x92, 0x02, 0x01, //output(data, var, abs)
- 0x09, 0x01, //HID_Usage(1)
- 0x85, 0x15, //HID_ReportID(0x15)
- 0x95, 0xFF, //HID_ReportCount(0xFF)
- 0x92, 0x02, 0x01, //output(data, var, abs)
- 0xC0 //end collection
- };
- static struct usb_function_instance *hidg_alloc_inst(void)
- {
- struct f_hid_opts *opts;
- struct usb_function_instance *ret;
- int status = 0;
- char *desc = NULL;
- printk("%s:%d\n", __func__, __LINE__);
- opts = kzalloc(sizeof(*opts), GFP_KERNEL);
- if (!opts)
- return ERR_PTR(-ENOMEM);
- desc = kmemdup(g_report_desc, sizeof(g_report_desc), GFP_KERNEL);
- if (!desc) {
- ret = ERR_PTR(-ENOMEM);
- goto unlock;
- }
- opts->report_desc = desc;
- opts->report_desc_length = sizeof(g_report_desc);
- opts->report_length = 64;
- mutex_init(&opts->lock);
- opts->func_inst.free_func_inst = hidg_free_inst;
- ret = &opts->func_inst;
- mutex_lock(&hidg_ida_lock);
- if (ida_is_empty(&hidg_ida)) {
- status = ghid_setup(NULL, HIDG_MINORS);
- if (status) {
- ret = ERR_PTR(status);
- kfree(opts);
- goto unlock;
- }
- }
- opts->minor = hidg_get_minor();
- if (opts->minor < 0) {
- ret = ERR_PTR(opts->minor);
- kfree(opts);
- if (ida_is_empty(&hidg_ida))
- ghid_cleanup();
- goto unlock;
- }
- config_group_init_type_name(&opts->func_inst.group, "", &hid_func_type);
- unlock:
- mutex_unlock(&hidg_ida_lock);
- return ret;
- }
- static void hidg_free(struct usb_function *f)
- {
- struct f_hidg *hidg;
- struct f_hid_opts *opts;
- printk("%s:%d\n", __func__, __LINE__);
- hidg = func_to_hidg(f);
- opts = container_of(f->fi, struct f_hid_opts, func_inst);
- kfree(hidg->report_desc);
- kfree(hidg);
- mutex_lock(&opts->lock);
- --opts->refcnt;
- mutex_unlock(&opts->lock);
- }
- static void hidg_unbind(struct usb_configuration *c, struct usb_function *f)
- {
- struct f_hidg *hidg = func_to_hidg(f);
- printk("%s:%d\n", __func__, __LINE__);
- device_destroy(hidg_class, MKDEV(major, hidg->minor));
- cdev_del(&hidg->cdev);
- usb_free_all_descriptors(f);
- }
- static struct usb_function *hidg_alloc(struct usb_function_instance *fi)
- {
- struct f_hidg *hidg;
- struct f_hid_opts *opts;
- /* allocate and initialize one new instance */
- hidg = kzalloc(sizeof(*hidg), GFP_KERNEL);
- if (!hidg)
- return ERR_PTR(-ENOMEM);
- printk("%s:%d\n", __func__, __LINE__);
- opts = container_of(fi, struct f_hid_opts, func_inst);
- mutex_lock(&opts->lock);
- ++opts->refcnt;
- hidg->minor = opts->minor;
- hidg->bInterfaceSubClass = opts->subclass;
- hidg->bInterfaceProtocol = opts->protocol;
- hidg->report_length = opts->report_length;
- hidg->report_desc_length = opts->report_desc_length;
- if (opts->report_desc) {
- hidg->report_desc = kmemdup(opts->report_desc,
- opts->report_desc_length,
- GFP_KERNEL);
- if (!hidg->report_desc) {
- kfree(hidg);
- mutex_unlock(&opts->lock);
- return ERR_PTR(-ENOMEM);
- }
- }
- mutex_unlock(&opts->lock);
- hidg->func.name = "hid";
- hidg->func.bind = hidg_bind;
- hidg->func.unbind = hidg_unbind;
- hidg->func.set_alt = hidg_set_alt;
- hidg->func.disable = hidg_disable;
- hidg->func.setup = hidg_setup;
- hidg->func.free_func = hidg_free;
- //hidg->func.req_match = f_hid_sim_req_match;
- /* this could me made configurable at some point */
- hidg->qlen = 4;
- return &hidg->func;
- }
- DECLARE_USB_FUNCTION_INIT(hid, hidg_alloc_inst, hidg_alloc);
- MODULE_LICENSE("GPL");
- MODULE_AUTHOR("Fabien Chouteau");
- int ghid_setup(struct usb_gadget *g, int count)
- {
- int status;
- dev_t dev;
- hidg_class = class_create(THIS_MODULE, "hidg");
- if (IS_ERR(hidg_class)) {
- status = PTR_ERR(hidg_class);
- hidg_class = NULL;
- return status;
- }
- status = alloc_chrdev_region(&dev, 0, count, "hidg");
- if (status) {
- class_destroy(hidg_class);
- hidg_class = NULL;
- return status;
- }
- major = MAJOR(dev);
- minors = count;
- return 0;
- }
- void ghid_cleanup(void)
- {
- if (major) {
- unregister_chrdev_region(MKDEV(major, 0), minors);
- major = minors = 0;
- }
- class_destroy(hidg_class);
- hidg_class = NULL;
- }
|