100a2430fSAndrzej Pietrasiewicz /* 200a2430fSAndrzej Pietrasiewicz * f_hid.c -- USB HID function driver 300a2430fSAndrzej Pietrasiewicz * 400a2430fSAndrzej Pietrasiewicz * Copyright (C) 2010 Fabien Chouteau <fabien.chouteau@barco.com> 500a2430fSAndrzej Pietrasiewicz * 600a2430fSAndrzej Pietrasiewicz * This program is free software; you can redistribute it and/or modify 700a2430fSAndrzej Pietrasiewicz * it under the terms of the GNU General Public License as published by 800a2430fSAndrzej Pietrasiewicz * the Free Software Foundation; either version 2 of the License, or 900a2430fSAndrzej Pietrasiewicz * (at your option) any later version. 1000a2430fSAndrzej Pietrasiewicz */ 1100a2430fSAndrzej Pietrasiewicz 1200a2430fSAndrzej Pietrasiewicz #include <linux/kernel.h> 1300a2430fSAndrzej Pietrasiewicz #include <linux/module.h> 1400a2430fSAndrzej Pietrasiewicz #include <linux/hid.h> 15cb382536SAndrzej Pietrasiewicz #include <linux/idr.h> 1600a2430fSAndrzej Pietrasiewicz #include <linux/cdev.h> 1700a2430fSAndrzej Pietrasiewicz #include <linux/mutex.h> 1800a2430fSAndrzej Pietrasiewicz #include <linux/poll.h> 1900a2430fSAndrzej Pietrasiewicz #include <linux/uaccess.h> 2000a2430fSAndrzej Pietrasiewicz #include <linux/wait.h> 2100a2430fSAndrzej Pietrasiewicz #include <linux/sched.h> 2200a2430fSAndrzej Pietrasiewicz #include <linux/usb/g_hid.h> 2300a2430fSAndrzej Pietrasiewicz 2400a2430fSAndrzej Pietrasiewicz #include "u_f.h" 25cb382536SAndrzej Pietrasiewicz #include "u_hid.h" 26cb382536SAndrzej Pietrasiewicz 27cb382536SAndrzej Pietrasiewicz #define HIDG_MINORS 4 2800a2430fSAndrzej Pietrasiewicz 2900a2430fSAndrzej Pietrasiewicz static int major, minors; 3000a2430fSAndrzej Pietrasiewicz static struct class *hidg_class; 31cb382536SAndrzej Pietrasiewicz static DEFINE_IDA(hidg_ida); 32cb382536SAndrzej Pietrasiewicz static DEFINE_MUTEX(hidg_ida_lock); /* protects access to hidg_ida */ 3300a2430fSAndrzej Pietrasiewicz 3400a2430fSAndrzej Pietrasiewicz /*-------------------------------------------------------------------------*/ 3500a2430fSAndrzej Pietrasiewicz /* HID gadget struct */ 3600a2430fSAndrzej Pietrasiewicz 3700a2430fSAndrzej Pietrasiewicz struct f_hidg_req_list { 3800a2430fSAndrzej Pietrasiewicz struct usb_request *req; 3900a2430fSAndrzej Pietrasiewicz unsigned int pos; 4000a2430fSAndrzej Pietrasiewicz struct list_head list; 4100a2430fSAndrzej Pietrasiewicz }; 4200a2430fSAndrzej Pietrasiewicz 4300a2430fSAndrzej Pietrasiewicz struct f_hidg { 4400a2430fSAndrzej Pietrasiewicz /* configuration */ 4500a2430fSAndrzej Pietrasiewicz unsigned char bInterfaceSubClass; 4600a2430fSAndrzej Pietrasiewicz unsigned char bInterfaceProtocol; 4700a2430fSAndrzej Pietrasiewicz unsigned short report_desc_length; 4800a2430fSAndrzej Pietrasiewicz char *report_desc; 4900a2430fSAndrzej Pietrasiewicz unsigned short report_length; 5000a2430fSAndrzej Pietrasiewicz 5100a2430fSAndrzej Pietrasiewicz /* recv report */ 5200a2430fSAndrzej Pietrasiewicz struct list_head completed_out_req; 5300a2430fSAndrzej Pietrasiewicz spinlock_t spinlock; 5400a2430fSAndrzej Pietrasiewicz wait_queue_head_t read_queue; 5500a2430fSAndrzej Pietrasiewicz unsigned int qlen; 5600a2430fSAndrzej Pietrasiewicz 5700a2430fSAndrzej Pietrasiewicz /* send report */ 5800a2430fSAndrzej Pietrasiewicz struct mutex lock; 5900a2430fSAndrzej Pietrasiewicz bool write_pending; 6000a2430fSAndrzej Pietrasiewicz wait_queue_head_t write_queue; 6100a2430fSAndrzej Pietrasiewicz struct usb_request *req; 6200a2430fSAndrzej Pietrasiewicz 6300a2430fSAndrzej Pietrasiewicz int minor; 6400a2430fSAndrzej Pietrasiewicz struct cdev cdev; 6500a2430fSAndrzej Pietrasiewicz struct usb_function func; 6600a2430fSAndrzej Pietrasiewicz 6700a2430fSAndrzej Pietrasiewicz struct usb_ep *in_ep; 6800a2430fSAndrzej Pietrasiewicz struct usb_ep *out_ep; 6900a2430fSAndrzej Pietrasiewicz }; 7000a2430fSAndrzej Pietrasiewicz 7100a2430fSAndrzej Pietrasiewicz static inline struct f_hidg *func_to_hidg(struct usb_function *f) 7200a2430fSAndrzej Pietrasiewicz { 7300a2430fSAndrzej Pietrasiewicz return container_of(f, struct f_hidg, func); 7400a2430fSAndrzej Pietrasiewicz } 7500a2430fSAndrzej Pietrasiewicz 7600a2430fSAndrzej Pietrasiewicz /*-------------------------------------------------------------------------*/ 7700a2430fSAndrzej Pietrasiewicz /* Static descriptors */ 7800a2430fSAndrzej Pietrasiewicz 7900a2430fSAndrzej Pietrasiewicz static struct usb_interface_descriptor hidg_interface_desc = { 8000a2430fSAndrzej Pietrasiewicz .bLength = sizeof hidg_interface_desc, 8100a2430fSAndrzej Pietrasiewicz .bDescriptorType = USB_DT_INTERFACE, 8200a2430fSAndrzej Pietrasiewicz /* .bInterfaceNumber = DYNAMIC */ 8300a2430fSAndrzej Pietrasiewicz .bAlternateSetting = 0, 8400a2430fSAndrzej Pietrasiewicz .bNumEndpoints = 2, 8500a2430fSAndrzej Pietrasiewicz .bInterfaceClass = USB_CLASS_HID, 8600a2430fSAndrzej Pietrasiewicz /* .bInterfaceSubClass = DYNAMIC */ 8700a2430fSAndrzej Pietrasiewicz /* .bInterfaceProtocol = DYNAMIC */ 8800a2430fSAndrzej Pietrasiewicz /* .iInterface = DYNAMIC */ 8900a2430fSAndrzej Pietrasiewicz }; 9000a2430fSAndrzej Pietrasiewicz 9100a2430fSAndrzej Pietrasiewicz static struct hid_descriptor hidg_desc = { 9200a2430fSAndrzej Pietrasiewicz .bLength = sizeof hidg_desc, 9300a2430fSAndrzej Pietrasiewicz .bDescriptorType = HID_DT_HID, 9400a2430fSAndrzej Pietrasiewicz .bcdHID = 0x0101, 9500a2430fSAndrzej Pietrasiewicz .bCountryCode = 0x00, 9600a2430fSAndrzej Pietrasiewicz .bNumDescriptors = 0x1, 9700a2430fSAndrzej Pietrasiewicz /*.desc[0].bDescriptorType = DYNAMIC */ 9800a2430fSAndrzej Pietrasiewicz /*.desc[0].wDescriptorLenght = DYNAMIC */ 9900a2430fSAndrzej Pietrasiewicz }; 10000a2430fSAndrzej Pietrasiewicz 10100a2430fSAndrzej Pietrasiewicz /* High-Speed Support */ 10200a2430fSAndrzej Pietrasiewicz 10300a2430fSAndrzej Pietrasiewicz static struct usb_endpoint_descriptor hidg_hs_in_ep_desc = { 10400a2430fSAndrzej Pietrasiewicz .bLength = USB_DT_ENDPOINT_SIZE, 10500a2430fSAndrzej Pietrasiewicz .bDescriptorType = USB_DT_ENDPOINT, 10600a2430fSAndrzej Pietrasiewicz .bEndpointAddress = USB_DIR_IN, 10700a2430fSAndrzej Pietrasiewicz .bmAttributes = USB_ENDPOINT_XFER_INT, 10800a2430fSAndrzej Pietrasiewicz /*.wMaxPacketSize = DYNAMIC */ 10900a2430fSAndrzej Pietrasiewicz .bInterval = 4, /* FIXME: Add this field in the 11000a2430fSAndrzej Pietrasiewicz * HID gadget configuration? 11100a2430fSAndrzej Pietrasiewicz * (struct hidg_func_descriptor) 11200a2430fSAndrzej Pietrasiewicz */ 11300a2430fSAndrzej Pietrasiewicz }; 11400a2430fSAndrzej Pietrasiewicz 11500a2430fSAndrzej Pietrasiewicz static struct usb_endpoint_descriptor hidg_hs_out_ep_desc = { 11600a2430fSAndrzej Pietrasiewicz .bLength = USB_DT_ENDPOINT_SIZE, 11700a2430fSAndrzej Pietrasiewicz .bDescriptorType = USB_DT_ENDPOINT, 11800a2430fSAndrzej Pietrasiewicz .bEndpointAddress = USB_DIR_OUT, 11900a2430fSAndrzej Pietrasiewicz .bmAttributes = USB_ENDPOINT_XFER_INT, 12000a2430fSAndrzej Pietrasiewicz /*.wMaxPacketSize = DYNAMIC */ 12100a2430fSAndrzej Pietrasiewicz .bInterval = 4, /* FIXME: Add this field in the 12200a2430fSAndrzej Pietrasiewicz * HID gadget configuration? 12300a2430fSAndrzej Pietrasiewicz * (struct hidg_func_descriptor) 12400a2430fSAndrzej Pietrasiewicz */ 12500a2430fSAndrzej Pietrasiewicz }; 12600a2430fSAndrzej Pietrasiewicz 12700a2430fSAndrzej Pietrasiewicz static struct usb_descriptor_header *hidg_hs_descriptors[] = { 12800a2430fSAndrzej Pietrasiewicz (struct usb_descriptor_header *)&hidg_interface_desc, 12900a2430fSAndrzej Pietrasiewicz (struct usb_descriptor_header *)&hidg_desc, 13000a2430fSAndrzej Pietrasiewicz (struct usb_descriptor_header *)&hidg_hs_in_ep_desc, 13100a2430fSAndrzej Pietrasiewicz (struct usb_descriptor_header *)&hidg_hs_out_ep_desc, 13200a2430fSAndrzej Pietrasiewicz NULL, 13300a2430fSAndrzej Pietrasiewicz }; 13400a2430fSAndrzej Pietrasiewicz 13500a2430fSAndrzej Pietrasiewicz /* Full-Speed Support */ 13600a2430fSAndrzej Pietrasiewicz 13700a2430fSAndrzej Pietrasiewicz static struct usb_endpoint_descriptor hidg_fs_in_ep_desc = { 13800a2430fSAndrzej Pietrasiewicz .bLength = USB_DT_ENDPOINT_SIZE, 13900a2430fSAndrzej Pietrasiewicz .bDescriptorType = USB_DT_ENDPOINT, 14000a2430fSAndrzej Pietrasiewicz .bEndpointAddress = USB_DIR_IN, 14100a2430fSAndrzej Pietrasiewicz .bmAttributes = USB_ENDPOINT_XFER_INT, 14200a2430fSAndrzej Pietrasiewicz /*.wMaxPacketSize = DYNAMIC */ 14300a2430fSAndrzej Pietrasiewicz .bInterval = 10, /* FIXME: Add this field in the 14400a2430fSAndrzej Pietrasiewicz * HID gadget configuration? 14500a2430fSAndrzej Pietrasiewicz * (struct hidg_func_descriptor) 14600a2430fSAndrzej Pietrasiewicz */ 14700a2430fSAndrzej Pietrasiewicz }; 14800a2430fSAndrzej Pietrasiewicz 14900a2430fSAndrzej Pietrasiewicz static struct usb_endpoint_descriptor hidg_fs_out_ep_desc = { 15000a2430fSAndrzej Pietrasiewicz .bLength = USB_DT_ENDPOINT_SIZE, 15100a2430fSAndrzej Pietrasiewicz .bDescriptorType = USB_DT_ENDPOINT, 15200a2430fSAndrzej Pietrasiewicz .bEndpointAddress = USB_DIR_OUT, 15300a2430fSAndrzej Pietrasiewicz .bmAttributes = USB_ENDPOINT_XFER_INT, 15400a2430fSAndrzej Pietrasiewicz /*.wMaxPacketSize = DYNAMIC */ 15500a2430fSAndrzej Pietrasiewicz .bInterval = 10, /* FIXME: Add this field in the 15600a2430fSAndrzej Pietrasiewicz * HID gadget configuration? 15700a2430fSAndrzej Pietrasiewicz * (struct hidg_func_descriptor) 15800a2430fSAndrzej Pietrasiewicz */ 15900a2430fSAndrzej Pietrasiewicz }; 16000a2430fSAndrzej Pietrasiewicz 16100a2430fSAndrzej Pietrasiewicz static struct usb_descriptor_header *hidg_fs_descriptors[] = { 16200a2430fSAndrzej Pietrasiewicz (struct usb_descriptor_header *)&hidg_interface_desc, 16300a2430fSAndrzej Pietrasiewicz (struct usb_descriptor_header *)&hidg_desc, 16400a2430fSAndrzej Pietrasiewicz (struct usb_descriptor_header *)&hidg_fs_in_ep_desc, 16500a2430fSAndrzej Pietrasiewicz (struct usb_descriptor_header *)&hidg_fs_out_ep_desc, 16600a2430fSAndrzej Pietrasiewicz NULL, 16700a2430fSAndrzej Pietrasiewicz }; 16800a2430fSAndrzej Pietrasiewicz 16900a2430fSAndrzej Pietrasiewicz /*-------------------------------------------------------------------------*/ 170cb382536SAndrzej Pietrasiewicz /* Strings */ 171cb382536SAndrzej Pietrasiewicz 172cb382536SAndrzej Pietrasiewicz #define CT_FUNC_HID_IDX 0 173cb382536SAndrzej Pietrasiewicz 174cb382536SAndrzej Pietrasiewicz static struct usb_string ct_func_string_defs[] = { 175cb382536SAndrzej Pietrasiewicz [CT_FUNC_HID_IDX].s = "HID Interface", 176cb382536SAndrzej Pietrasiewicz {}, /* end of list */ 177cb382536SAndrzej Pietrasiewicz }; 178cb382536SAndrzej Pietrasiewicz 179cb382536SAndrzej Pietrasiewicz static struct usb_gadget_strings ct_func_string_table = { 180cb382536SAndrzej Pietrasiewicz .language = 0x0409, /* en-US */ 181cb382536SAndrzej Pietrasiewicz .strings = ct_func_string_defs, 182cb382536SAndrzej Pietrasiewicz }; 183cb382536SAndrzej Pietrasiewicz 184cb382536SAndrzej Pietrasiewicz static struct usb_gadget_strings *ct_func_strings[] = { 185cb382536SAndrzej Pietrasiewicz &ct_func_string_table, 186cb382536SAndrzej Pietrasiewicz NULL, 187cb382536SAndrzej Pietrasiewicz }; 188cb382536SAndrzej Pietrasiewicz 189cb382536SAndrzej Pietrasiewicz /*-------------------------------------------------------------------------*/ 19000a2430fSAndrzej Pietrasiewicz /* Char Device */ 19100a2430fSAndrzej Pietrasiewicz 19200a2430fSAndrzej Pietrasiewicz static ssize_t f_hidg_read(struct file *file, char __user *buffer, 19300a2430fSAndrzej Pietrasiewicz size_t count, loff_t *ptr) 19400a2430fSAndrzej Pietrasiewicz { 19500a2430fSAndrzej Pietrasiewicz struct f_hidg *hidg = file->private_data; 19600a2430fSAndrzej Pietrasiewicz struct f_hidg_req_list *list; 19700a2430fSAndrzej Pietrasiewicz struct usb_request *req; 19800a2430fSAndrzej Pietrasiewicz unsigned long flags; 19900a2430fSAndrzej Pietrasiewicz int ret; 20000a2430fSAndrzej Pietrasiewicz 20100a2430fSAndrzej Pietrasiewicz if (!count) 20200a2430fSAndrzej Pietrasiewicz return 0; 20300a2430fSAndrzej Pietrasiewicz 20400a2430fSAndrzej Pietrasiewicz if (!access_ok(VERIFY_WRITE, buffer, count)) 20500a2430fSAndrzej Pietrasiewicz return -EFAULT; 20600a2430fSAndrzej Pietrasiewicz 20700a2430fSAndrzej Pietrasiewicz spin_lock_irqsave(&hidg->spinlock, flags); 20800a2430fSAndrzej Pietrasiewicz 20900a2430fSAndrzej Pietrasiewicz #define READ_COND (!list_empty(&hidg->completed_out_req)) 21000a2430fSAndrzej Pietrasiewicz 21100a2430fSAndrzej Pietrasiewicz /* wait for at least one buffer to complete */ 21200a2430fSAndrzej Pietrasiewicz while (!READ_COND) { 21300a2430fSAndrzej Pietrasiewicz spin_unlock_irqrestore(&hidg->spinlock, flags); 21400a2430fSAndrzej Pietrasiewicz if (file->f_flags & O_NONBLOCK) 21500a2430fSAndrzej Pietrasiewicz return -EAGAIN; 21600a2430fSAndrzej Pietrasiewicz 21700a2430fSAndrzej Pietrasiewicz if (wait_event_interruptible(hidg->read_queue, READ_COND)) 21800a2430fSAndrzej Pietrasiewicz return -ERESTARTSYS; 21900a2430fSAndrzej Pietrasiewicz 22000a2430fSAndrzej Pietrasiewicz spin_lock_irqsave(&hidg->spinlock, flags); 22100a2430fSAndrzej Pietrasiewicz } 22200a2430fSAndrzej Pietrasiewicz 22300a2430fSAndrzej Pietrasiewicz /* pick the first one */ 22400a2430fSAndrzej Pietrasiewicz list = list_first_entry(&hidg->completed_out_req, 22500a2430fSAndrzej Pietrasiewicz struct f_hidg_req_list, list); 22600a2430fSAndrzej Pietrasiewicz req = list->req; 22700a2430fSAndrzej Pietrasiewicz count = min_t(unsigned int, count, req->actual - list->pos); 22800a2430fSAndrzej Pietrasiewicz spin_unlock_irqrestore(&hidg->spinlock, flags); 22900a2430fSAndrzej Pietrasiewicz 23000a2430fSAndrzej Pietrasiewicz /* copy to user outside spinlock */ 23100a2430fSAndrzej Pietrasiewicz count -= copy_to_user(buffer, req->buf + list->pos, count); 23200a2430fSAndrzej Pietrasiewicz list->pos += count; 23300a2430fSAndrzej Pietrasiewicz 23400a2430fSAndrzej Pietrasiewicz /* 23500a2430fSAndrzej Pietrasiewicz * if this request is completely handled and transfered to 23600a2430fSAndrzej Pietrasiewicz * userspace, remove its entry from the list and requeue it 23700a2430fSAndrzej Pietrasiewicz * again. Otherwise, we will revisit it again upon the next 23800a2430fSAndrzej Pietrasiewicz * call, taking into account its current read position. 23900a2430fSAndrzej Pietrasiewicz */ 24000a2430fSAndrzej Pietrasiewicz if (list->pos == req->actual) { 24100a2430fSAndrzej Pietrasiewicz spin_lock_irqsave(&hidg->spinlock, flags); 24200a2430fSAndrzej Pietrasiewicz list_del(&list->list); 24300a2430fSAndrzej Pietrasiewicz kfree(list); 24400a2430fSAndrzej Pietrasiewicz spin_unlock_irqrestore(&hidg->spinlock, flags); 24500a2430fSAndrzej Pietrasiewicz 24600a2430fSAndrzej Pietrasiewicz req->length = hidg->report_length; 24700a2430fSAndrzej Pietrasiewicz ret = usb_ep_queue(hidg->out_ep, req, GFP_KERNEL); 24800a2430fSAndrzej Pietrasiewicz if (ret < 0) 24900a2430fSAndrzej Pietrasiewicz return ret; 25000a2430fSAndrzej Pietrasiewicz } 25100a2430fSAndrzej Pietrasiewicz 25200a2430fSAndrzej Pietrasiewicz return count; 25300a2430fSAndrzej Pietrasiewicz } 25400a2430fSAndrzej Pietrasiewicz 25500a2430fSAndrzej Pietrasiewicz static void f_hidg_req_complete(struct usb_ep *ep, struct usb_request *req) 25600a2430fSAndrzej Pietrasiewicz { 25700a2430fSAndrzej Pietrasiewicz struct f_hidg *hidg = (struct f_hidg *)ep->driver_data; 25800a2430fSAndrzej Pietrasiewicz 25900a2430fSAndrzej Pietrasiewicz if (req->status != 0) { 26000a2430fSAndrzej Pietrasiewicz ERROR(hidg->func.config->cdev, 26100a2430fSAndrzej Pietrasiewicz "End Point Request ERROR: %d\n", req->status); 26200a2430fSAndrzej Pietrasiewicz } 26300a2430fSAndrzej Pietrasiewicz 26400a2430fSAndrzej Pietrasiewicz hidg->write_pending = 0; 26500a2430fSAndrzej Pietrasiewicz wake_up(&hidg->write_queue); 26600a2430fSAndrzej Pietrasiewicz } 26700a2430fSAndrzej Pietrasiewicz 26800a2430fSAndrzej Pietrasiewicz static ssize_t f_hidg_write(struct file *file, const char __user *buffer, 26900a2430fSAndrzej Pietrasiewicz size_t count, loff_t *offp) 27000a2430fSAndrzej Pietrasiewicz { 27100a2430fSAndrzej Pietrasiewicz struct f_hidg *hidg = file->private_data; 27200a2430fSAndrzej Pietrasiewicz ssize_t status = -ENOMEM; 27300a2430fSAndrzej Pietrasiewicz 27400a2430fSAndrzej Pietrasiewicz if (!access_ok(VERIFY_READ, buffer, count)) 27500a2430fSAndrzej Pietrasiewicz return -EFAULT; 27600a2430fSAndrzej Pietrasiewicz 27700a2430fSAndrzej Pietrasiewicz mutex_lock(&hidg->lock); 27800a2430fSAndrzej Pietrasiewicz 27900a2430fSAndrzej Pietrasiewicz #define WRITE_COND (!hidg->write_pending) 28000a2430fSAndrzej Pietrasiewicz 28100a2430fSAndrzej Pietrasiewicz /* write queue */ 28200a2430fSAndrzej Pietrasiewicz while (!WRITE_COND) { 28300a2430fSAndrzej Pietrasiewicz mutex_unlock(&hidg->lock); 28400a2430fSAndrzej Pietrasiewicz if (file->f_flags & O_NONBLOCK) 28500a2430fSAndrzej Pietrasiewicz return -EAGAIN; 28600a2430fSAndrzej Pietrasiewicz 28700a2430fSAndrzej Pietrasiewicz if (wait_event_interruptible_exclusive( 28800a2430fSAndrzej Pietrasiewicz hidg->write_queue, WRITE_COND)) 28900a2430fSAndrzej Pietrasiewicz return -ERESTARTSYS; 29000a2430fSAndrzej Pietrasiewicz 29100a2430fSAndrzej Pietrasiewicz mutex_lock(&hidg->lock); 29200a2430fSAndrzej Pietrasiewicz } 29300a2430fSAndrzej Pietrasiewicz 29400a2430fSAndrzej Pietrasiewicz count = min_t(unsigned, count, hidg->report_length); 29500a2430fSAndrzej Pietrasiewicz status = copy_from_user(hidg->req->buf, buffer, count); 29600a2430fSAndrzej Pietrasiewicz 29700a2430fSAndrzej Pietrasiewicz if (status != 0) { 29800a2430fSAndrzej Pietrasiewicz ERROR(hidg->func.config->cdev, 29900a2430fSAndrzej Pietrasiewicz "copy_from_user error\n"); 30000a2430fSAndrzej Pietrasiewicz mutex_unlock(&hidg->lock); 30100a2430fSAndrzej Pietrasiewicz return -EINVAL; 30200a2430fSAndrzej Pietrasiewicz } 30300a2430fSAndrzej Pietrasiewicz 30400a2430fSAndrzej Pietrasiewicz hidg->req->status = 0; 30500a2430fSAndrzej Pietrasiewicz hidg->req->zero = 0; 30600a2430fSAndrzej Pietrasiewicz hidg->req->length = count; 30700a2430fSAndrzej Pietrasiewicz hidg->req->complete = f_hidg_req_complete; 30800a2430fSAndrzej Pietrasiewicz hidg->req->context = hidg; 30900a2430fSAndrzej Pietrasiewicz hidg->write_pending = 1; 31000a2430fSAndrzej Pietrasiewicz 31100a2430fSAndrzej Pietrasiewicz status = usb_ep_queue(hidg->in_ep, hidg->req, GFP_ATOMIC); 31200a2430fSAndrzej Pietrasiewicz if (status < 0) { 31300a2430fSAndrzej Pietrasiewicz ERROR(hidg->func.config->cdev, 31400a2430fSAndrzej Pietrasiewicz "usb_ep_queue error on int endpoint %zd\n", status); 31500a2430fSAndrzej Pietrasiewicz hidg->write_pending = 0; 31600a2430fSAndrzej Pietrasiewicz wake_up(&hidg->write_queue); 31700a2430fSAndrzej Pietrasiewicz } else { 31800a2430fSAndrzej Pietrasiewicz status = count; 31900a2430fSAndrzej Pietrasiewicz } 32000a2430fSAndrzej Pietrasiewicz 32100a2430fSAndrzej Pietrasiewicz mutex_unlock(&hidg->lock); 32200a2430fSAndrzej Pietrasiewicz 32300a2430fSAndrzej Pietrasiewicz return status; 32400a2430fSAndrzej Pietrasiewicz } 32500a2430fSAndrzej Pietrasiewicz 32600a2430fSAndrzej Pietrasiewicz static unsigned int f_hidg_poll(struct file *file, poll_table *wait) 32700a2430fSAndrzej Pietrasiewicz { 32800a2430fSAndrzej Pietrasiewicz struct f_hidg *hidg = file->private_data; 32900a2430fSAndrzej Pietrasiewicz unsigned int ret = 0; 33000a2430fSAndrzej Pietrasiewicz 33100a2430fSAndrzej Pietrasiewicz poll_wait(file, &hidg->read_queue, wait); 33200a2430fSAndrzej Pietrasiewicz poll_wait(file, &hidg->write_queue, wait); 33300a2430fSAndrzej Pietrasiewicz 33400a2430fSAndrzej Pietrasiewicz if (WRITE_COND) 33500a2430fSAndrzej Pietrasiewicz ret |= POLLOUT | POLLWRNORM; 33600a2430fSAndrzej Pietrasiewicz 33700a2430fSAndrzej Pietrasiewicz if (READ_COND) 33800a2430fSAndrzej Pietrasiewicz ret |= POLLIN | POLLRDNORM; 33900a2430fSAndrzej Pietrasiewicz 34000a2430fSAndrzej Pietrasiewicz return ret; 34100a2430fSAndrzej Pietrasiewicz } 34200a2430fSAndrzej Pietrasiewicz 34300a2430fSAndrzej Pietrasiewicz #undef WRITE_COND 34400a2430fSAndrzej Pietrasiewicz #undef READ_COND 34500a2430fSAndrzej Pietrasiewicz 34600a2430fSAndrzej Pietrasiewicz static int f_hidg_release(struct inode *inode, struct file *fd) 34700a2430fSAndrzej Pietrasiewicz { 34800a2430fSAndrzej Pietrasiewicz fd->private_data = NULL; 34900a2430fSAndrzej Pietrasiewicz return 0; 35000a2430fSAndrzej Pietrasiewicz } 35100a2430fSAndrzej Pietrasiewicz 35200a2430fSAndrzej Pietrasiewicz static int f_hidg_open(struct inode *inode, struct file *fd) 35300a2430fSAndrzej Pietrasiewicz { 35400a2430fSAndrzej Pietrasiewicz struct f_hidg *hidg = 35500a2430fSAndrzej Pietrasiewicz container_of(inode->i_cdev, struct f_hidg, cdev); 35600a2430fSAndrzej Pietrasiewicz 35700a2430fSAndrzej Pietrasiewicz fd->private_data = hidg; 35800a2430fSAndrzej Pietrasiewicz 35900a2430fSAndrzej Pietrasiewicz return 0; 36000a2430fSAndrzej Pietrasiewicz } 36100a2430fSAndrzej Pietrasiewicz 36200a2430fSAndrzej Pietrasiewicz /*-------------------------------------------------------------------------*/ 36300a2430fSAndrzej Pietrasiewicz /* usb_function */ 36400a2430fSAndrzej Pietrasiewicz 36500a2430fSAndrzej Pietrasiewicz static inline struct usb_request *hidg_alloc_ep_req(struct usb_ep *ep, 36600a2430fSAndrzej Pietrasiewicz unsigned length) 36700a2430fSAndrzej Pietrasiewicz { 368*aadbe812SFelipe F. Tonello return alloc_ep_req(ep, length); 36900a2430fSAndrzej Pietrasiewicz } 37000a2430fSAndrzej Pietrasiewicz 37100a2430fSAndrzej Pietrasiewicz static void hidg_set_report_complete(struct usb_ep *ep, struct usb_request *req) 37200a2430fSAndrzej Pietrasiewicz { 37300a2430fSAndrzej Pietrasiewicz struct f_hidg *hidg = (struct f_hidg *) req->context; 37400a2430fSAndrzej Pietrasiewicz struct f_hidg_req_list *req_list; 37500a2430fSAndrzej Pietrasiewicz unsigned long flags; 37600a2430fSAndrzej Pietrasiewicz 37700a2430fSAndrzej Pietrasiewicz req_list = kzalloc(sizeof(*req_list), GFP_ATOMIC); 37800a2430fSAndrzej Pietrasiewicz if (!req_list) 37900a2430fSAndrzej Pietrasiewicz return; 38000a2430fSAndrzej Pietrasiewicz 38100a2430fSAndrzej Pietrasiewicz req_list->req = req; 38200a2430fSAndrzej Pietrasiewicz 38300a2430fSAndrzej Pietrasiewicz spin_lock_irqsave(&hidg->spinlock, flags); 38400a2430fSAndrzej Pietrasiewicz list_add_tail(&req_list->list, &hidg->completed_out_req); 38500a2430fSAndrzej Pietrasiewicz spin_unlock_irqrestore(&hidg->spinlock, flags); 38600a2430fSAndrzej Pietrasiewicz 38700a2430fSAndrzej Pietrasiewicz wake_up(&hidg->read_queue); 38800a2430fSAndrzej Pietrasiewicz } 38900a2430fSAndrzej Pietrasiewicz 39000a2430fSAndrzej Pietrasiewicz static int hidg_setup(struct usb_function *f, 39100a2430fSAndrzej Pietrasiewicz const struct usb_ctrlrequest *ctrl) 39200a2430fSAndrzej Pietrasiewicz { 39300a2430fSAndrzej Pietrasiewicz struct f_hidg *hidg = func_to_hidg(f); 39400a2430fSAndrzej Pietrasiewicz struct usb_composite_dev *cdev = f->config->cdev; 39500a2430fSAndrzej Pietrasiewicz struct usb_request *req = cdev->req; 39600a2430fSAndrzej Pietrasiewicz int status = 0; 39700a2430fSAndrzej Pietrasiewicz __u16 value, length; 39800a2430fSAndrzej Pietrasiewicz 39900a2430fSAndrzej Pietrasiewicz value = __le16_to_cpu(ctrl->wValue); 40000a2430fSAndrzej Pietrasiewicz length = __le16_to_cpu(ctrl->wLength); 40100a2430fSAndrzej Pietrasiewicz 402c9b3bde0SJulia Lawall VDBG(cdev, 403c9b3bde0SJulia Lawall "%s crtl_request : bRequestType:0x%x bRequest:0x%x Value:0x%x\n", 404c9b3bde0SJulia Lawall __func__, ctrl->bRequestType, ctrl->bRequest, value); 40500a2430fSAndrzej Pietrasiewicz 40600a2430fSAndrzej Pietrasiewicz switch ((ctrl->bRequestType << 8) | ctrl->bRequest) { 40700a2430fSAndrzej Pietrasiewicz case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8 40800a2430fSAndrzej Pietrasiewicz | HID_REQ_GET_REPORT): 40900a2430fSAndrzej Pietrasiewicz VDBG(cdev, "get_report\n"); 41000a2430fSAndrzej Pietrasiewicz 41100a2430fSAndrzej Pietrasiewicz /* send an empty report */ 41200a2430fSAndrzej Pietrasiewicz length = min_t(unsigned, length, hidg->report_length); 41300a2430fSAndrzej Pietrasiewicz memset(req->buf, 0x0, length); 41400a2430fSAndrzej Pietrasiewicz 41500a2430fSAndrzej Pietrasiewicz goto respond; 41600a2430fSAndrzej Pietrasiewicz break; 41700a2430fSAndrzej Pietrasiewicz 41800a2430fSAndrzej Pietrasiewicz case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8 41900a2430fSAndrzej Pietrasiewicz | HID_REQ_GET_PROTOCOL): 42000a2430fSAndrzej Pietrasiewicz VDBG(cdev, "get_protocol\n"); 42100a2430fSAndrzej Pietrasiewicz goto stall; 42200a2430fSAndrzej Pietrasiewicz break; 42300a2430fSAndrzej Pietrasiewicz 42400a2430fSAndrzej Pietrasiewicz case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8 42500a2430fSAndrzej Pietrasiewicz | HID_REQ_SET_REPORT): 4266774def6SMasanari Iida VDBG(cdev, "set_report | wLength=%d\n", ctrl->wLength); 42700a2430fSAndrzej Pietrasiewicz goto stall; 42800a2430fSAndrzej Pietrasiewicz break; 42900a2430fSAndrzej Pietrasiewicz 43000a2430fSAndrzej Pietrasiewicz case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8 43100a2430fSAndrzej Pietrasiewicz | HID_REQ_SET_PROTOCOL): 43200a2430fSAndrzej Pietrasiewicz VDBG(cdev, "set_protocol\n"); 43300a2430fSAndrzej Pietrasiewicz goto stall; 43400a2430fSAndrzej Pietrasiewicz break; 43500a2430fSAndrzej Pietrasiewicz 43600a2430fSAndrzej Pietrasiewicz case ((USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_INTERFACE) << 8 43700a2430fSAndrzej Pietrasiewicz | USB_REQ_GET_DESCRIPTOR): 43800a2430fSAndrzej Pietrasiewicz switch (value >> 8) { 43900a2430fSAndrzej Pietrasiewicz case HID_DT_HID: 440f286d487SKrzysztof Opasiak { 441f286d487SKrzysztof Opasiak struct hid_descriptor hidg_desc_copy = hidg_desc; 442f286d487SKrzysztof Opasiak 44300a2430fSAndrzej Pietrasiewicz VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: HID\n"); 444f286d487SKrzysztof Opasiak hidg_desc_copy.desc[0].bDescriptorType = HID_DT_REPORT; 445f286d487SKrzysztof Opasiak hidg_desc_copy.desc[0].wDescriptorLength = 446f286d487SKrzysztof Opasiak cpu_to_le16(hidg->report_desc_length); 447f286d487SKrzysztof Opasiak 44800a2430fSAndrzej Pietrasiewicz length = min_t(unsigned short, length, 449f286d487SKrzysztof Opasiak hidg_desc_copy.bLength); 450f286d487SKrzysztof Opasiak memcpy(req->buf, &hidg_desc_copy, length); 45100a2430fSAndrzej Pietrasiewicz goto respond; 45200a2430fSAndrzej Pietrasiewicz break; 453f286d487SKrzysztof Opasiak } 45400a2430fSAndrzej Pietrasiewicz case HID_DT_REPORT: 45500a2430fSAndrzej Pietrasiewicz VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: REPORT\n"); 45600a2430fSAndrzej Pietrasiewicz length = min_t(unsigned short, length, 45700a2430fSAndrzej Pietrasiewicz hidg->report_desc_length); 45800a2430fSAndrzej Pietrasiewicz memcpy(req->buf, hidg->report_desc, length); 45900a2430fSAndrzej Pietrasiewicz goto respond; 46000a2430fSAndrzej Pietrasiewicz break; 46100a2430fSAndrzej Pietrasiewicz 46200a2430fSAndrzej Pietrasiewicz default: 46300a2430fSAndrzej Pietrasiewicz VDBG(cdev, "Unknown descriptor request 0x%x\n", 46400a2430fSAndrzej Pietrasiewicz value >> 8); 46500a2430fSAndrzej Pietrasiewicz goto stall; 46600a2430fSAndrzej Pietrasiewicz break; 46700a2430fSAndrzej Pietrasiewicz } 46800a2430fSAndrzej Pietrasiewicz break; 46900a2430fSAndrzej Pietrasiewicz 47000a2430fSAndrzej Pietrasiewicz default: 47100a2430fSAndrzej Pietrasiewicz VDBG(cdev, "Unknown request 0x%x\n", 47200a2430fSAndrzej Pietrasiewicz ctrl->bRequest); 47300a2430fSAndrzej Pietrasiewicz goto stall; 47400a2430fSAndrzej Pietrasiewicz break; 47500a2430fSAndrzej Pietrasiewicz } 47600a2430fSAndrzej Pietrasiewicz 47700a2430fSAndrzej Pietrasiewicz stall: 47800a2430fSAndrzej Pietrasiewicz return -EOPNOTSUPP; 47900a2430fSAndrzej Pietrasiewicz 48000a2430fSAndrzej Pietrasiewicz respond: 48100a2430fSAndrzej Pietrasiewicz req->zero = 0; 48200a2430fSAndrzej Pietrasiewicz req->length = length; 48300a2430fSAndrzej Pietrasiewicz status = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC); 48400a2430fSAndrzej Pietrasiewicz if (status < 0) 48500a2430fSAndrzej Pietrasiewicz ERROR(cdev, "usb_ep_queue error on ep0 %d\n", value); 48600a2430fSAndrzej Pietrasiewicz return status; 48700a2430fSAndrzej Pietrasiewicz } 48800a2430fSAndrzej Pietrasiewicz 48900a2430fSAndrzej Pietrasiewicz static void hidg_disable(struct usb_function *f) 49000a2430fSAndrzej Pietrasiewicz { 49100a2430fSAndrzej Pietrasiewicz struct f_hidg *hidg = func_to_hidg(f); 49200a2430fSAndrzej Pietrasiewicz struct f_hidg_req_list *list, *next; 49300a2430fSAndrzej Pietrasiewicz 49400a2430fSAndrzej Pietrasiewicz usb_ep_disable(hidg->in_ep); 49500a2430fSAndrzej Pietrasiewicz usb_ep_disable(hidg->out_ep); 49600a2430fSAndrzej Pietrasiewicz 49700a2430fSAndrzej Pietrasiewicz list_for_each_entry_safe(list, next, &hidg->completed_out_req, list) { 49800a2430fSAndrzej Pietrasiewicz list_del(&list->list); 49900a2430fSAndrzej Pietrasiewicz kfree(list); 50000a2430fSAndrzej Pietrasiewicz } 50100a2430fSAndrzej Pietrasiewicz } 50200a2430fSAndrzej Pietrasiewicz 50300a2430fSAndrzej Pietrasiewicz static int hidg_set_alt(struct usb_function *f, unsigned intf, unsigned alt) 50400a2430fSAndrzej Pietrasiewicz { 50500a2430fSAndrzej Pietrasiewicz struct usb_composite_dev *cdev = f->config->cdev; 50600a2430fSAndrzej Pietrasiewicz struct f_hidg *hidg = func_to_hidg(f); 50700a2430fSAndrzej Pietrasiewicz int i, status = 0; 50800a2430fSAndrzej Pietrasiewicz 50900a2430fSAndrzej Pietrasiewicz VDBG(cdev, "hidg_set_alt intf:%d alt:%d\n", intf, alt); 51000a2430fSAndrzej Pietrasiewicz 51100a2430fSAndrzej Pietrasiewicz if (hidg->in_ep != NULL) { 51200a2430fSAndrzej Pietrasiewicz /* restart endpoint */ 51300a2430fSAndrzej Pietrasiewicz usb_ep_disable(hidg->in_ep); 51400a2430fSAndrzej Pietrasiewicz 51500a2430fSAndrzej Pietrasiewicz status = config_ep_by_speed(f->config->cdev->gadget, f, 51600a2430fSAndrzej Pietrasiewicz hidg->in_ep); 51700a2430fSAndrzej Pietrasiewicz if (status) { 51800a2430fSAndrzej Pietrasiewicz ERROR(cdev, "config_ep_by_speed FAILED!\n"); 51900a2430fSAndrzej Pietrasiewicz goto fail; 52000a2430fSAndrzej Pietrasiewicz } 52100a2430fSAndrzej Pietrasiewicz status = usb_ep_enable(hidg->in_ep); 52200a2430fSAndrzej Pietrasiewicz if (status < 0) { 52300a2430fSAndrzej Pietrasiewicz ERROR(cdev, "Enable IN endpoint FAILED!\n"); 52400a2430fSAndrzej Pietrasiewicz goto fail; 52500a2430fSAndrzej Pietrasiewicz } 52600a2430fSAndrzej Pietrasiewicz hidg->in_ep->driver_data = hidg; 52700a2430fSAndrzej Pietrasiewicz } 52800a2430fSAndrzej Pietrasiewicz 52900a2430fSAndrzej Pietrasiewicz 53000a2430fSAndrzej Pietrasiewicz if (hidg->out_ep != NULL) { 53100a2430fSAndrzej Pietrasiewicz /* restart endpoint */ 53200a2430fSAndrzej Pietrasiewicz usb_ep_disable(hidg->out_ep); 53300a2430fSAndrzej Pietrasiewicz 53400a2430fSAndrzej Pietrasiewicz status = config_ep_by_speed(f->config->cdev->gadget, f, 53500a2430fSAndrzej Pietrasiewicz hidg->out_ep); 53600a2430fSAndrzej Pietrasiewicz if (status) { 53700a2430fSAndrzej Pietrasiewicz ERROR(cdev, "config_ep_by_speed FAILED!\n"); 53800a2430fSAndrzej Pietrasiewicz goto fail; 53900a2430fSAndrzej Pietrasiewicz } 54000a2430fSAndrzej Pietrasiewicz status = usb_ep_enable(hidg->out_ep); 54100a2430fSAndrzej Pietrasiewicz if (status < 0) { 54200a2430fSAndrzej Pietrasiewicz ERROR(cdev, "Enable IN endpoint FAILED!\n"); 54300a2430fSAndrzej Pietrasiewicz goto fail; 54400a2430fSAndrzej Pietrasiewicz } 54500a2430fSAndrzej Pietrasiewicz hidg->out_ep->driver_data = hidg; 54600a2430fSAndrzej Pietrasiewicz 54700a2430fSAndrzej Pietrasiewicz /* 54800a2430fSAndrzej Pietrasiewicz * allocate a bunch of read buffers and queue them all at once. 54900a2430fSAndrzej Pietrasiewicz */ 55000a2430fSAndrzej Pietrasiewicz for (i = 0; i < hidg->qlen && status == 0; i++) { 55100a2430fSAndrzej Pietrasiewicz struct usb_request *req = 55200a2430fSAndrzej Pietrasiewicz hidg_alloc_ep_req(hidg->out_ep, 55300a2430fSAndrzej Pietrasiewicz hidg->report_length); 55400a2430fSAndrzej Pietrasiewicz if (req) { 55500a2430fSAndrzej Pietrasiewicz req->complete = hidg_set_report_complete; 55600a2430fSAndrzej Pietrasiewicz req->context = hidg; 55700a2430fSAndrzej Pietrasiewicz status = usb_ep_queue(hidg->out_ep, req, 55800a2430fSAndrzej Pietrasiewicz GFP_ATOMIC); 55900a2430fSAndrzej Pietrasiewicz if (status) 56000a2430fSAndrzej Pietrasiewicz ERROR(cdev, "%s queue req --> %d\n", 56100a2430fSAndrzej Pietrasiewicz hidg->out_ep->name, status); 56200a2430fSAndrzej Pietrasiewicz } else { 56300a2430fSAndrzej Pietrasiewicz usb_ep_disable(hidg->out_ep); 56400a2430fSAndrzej Pietrasiewicz status = -ENOMEM; 56500a2430fSAndrzej Pietrasiewicz goto fail; 56600a2430fSAndrzej Pietrasiewicz } 56700a2430fSAndrzej Pietrasiewicz } 56800a2430fSAndrzej Pietrasiewicz } 56900a2430fSAndrzej Pietrasiewicz 57000a2430fSAndrzej Pietrasiewicz fail: 57100a2430fSAndrzej Pietrasiewicz return status; 57200a2430fSAndrzej Pietrasiewicz } 57300a2430fSAndrzej Pietrasiewicz 5747a3cc461SLad, Prabhakar static const struct file_operations f_hidg_fops = { 57500a2430fSAndrzej Pietrasiewicz .owner = THIS_MODULE, 57600a2430fSAndrzej Pietrasiewicz .open = f_hidg_open, 57700a2430fSAndrzej Pietrasiewicz .release = f_hidg_release, 57800a2430fSAndrzej Pietrasiewicz .write = f_hidg_write, 57900a2430fSAndrzej Pietrasiewicz .read = f_hidg_read, 58000a2430fSAndrzej Pietrasiewicz .poll = f_hidg_poll, 58100a2430fSAndrzej Pietrasiewicz .llseek = noop_llseek, 58200a2430fSAndrzej Pietrasiewicz }; 58300a2430fSAndrzej Pietrasiewicz 584cb382536SAndrzej Pietrasiewicz static int hidg_bind(struct usb_configuration *c, struct usb_function *f) 58500a2430fSAndrzej Pietrasiewicz { 58600a2430fSAndrzej Pietrasiewicz struct usb_ep *ep; 58700a2430fSAndrzej Pietrasiewicz struct f_hidg *hidg = func_to_hidg(f); 5885ca8d3ecSAndrzej Pietrasiewicz struct usb_string *us; 58963406087SAndrzej Pietrasiewicz struct device *device; 59000a2430fSAndrzej Pietrasiewicz int status; 59100a2430fSAndrzej Pietrasiewicz dev_t dev; 59200a2430fSAndrzej Pietrasiewicz 593cb382536SAndrzej Pietrasiewicz /* maybe allocate device-global string IDs, and patch descriptors */ 5945ca8d3ecSAndrzej Pietrasiewicz us = usb_gstrings_attach(c->cdev, ct_func_strings, 5955ca8d3ecSAndrzej Pietrasiewicz ARRAY_SIZE(ct_func_string_defs)); 5965ca8d3ecSAndrzej Pietrasiewicz if (IS_ERR(us)) 5975ca8d3ecSAndrzej Pietrasiewicz return PTR_ERR(us); 5985ca8d3ecSAndrzej Pietrasiewicz hidg_interface_desc.iInterface = us[CT_FUNC_HID_IDX].id; 599cb382536SAndrzej Pietrasiewicz 60000a2430fSAndrzej Pietrasiewicz /* allocate instance-specific interface IDs, and patch descriptors */ 60100a2430fSAndrzej Pietrasiewicz status = usb_interface_id(c, f); 60200a2430fSAndrzej Pietrasiewicz if (status < 0) 60300a2430fSAndrzej Pietrasiewicz goto fail; 60400a2430fSAndrzej Pietrasiewicz hidg_interface_desc.bInterfaceNumber = status; 60500a2430fSAndrzej Pietrasiewicz 60600a2430fSAndrzej Pietrasiewicz /* allocate instance-specific endpoints */ 60700a2430fSAndrzej Pietrasiewicz status = -ENODEV; 60800a2430fSAndrzej Pietrasiewicz ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_in_ep_desc); 60900a2430fSAndrzej Pietrasiewicz if (!ep) 61000a2430fSAndrzej Pietrasiewicz goto fail; 61100a2430fSAndrzej Pietrasiewicz hidg->in_ep = ep; 61200a2430fSAndrzej Pietrasiewicz 61300a2430fSAndrzej Pietrasiewicz ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_out_ep_desc); 61400a2430fSAndrzej Pietrasiewicz if (!ep) 61500a2430fSAndrzej Pietrasiewicz goto fail; 61600a2430fSAndrzej Pietrasiewicz hidg->out_ep = ep; 61700a2430fSAndrzej Pietrasiewicz 61800a2430fSAndrzej Pietrasiewicz /* preallocate request and buffer */ 61900a2430fSAndrzej Pietrasiewicz status = -ENOMEM; 62000a2430fSAndrzej Pietrasiewicz hidg->req = usb_ep_alloc_request(hidg->in_ep, GFP_KERNEL); 62100a2430fSAndrzej Pietrasiewicz if (!hidg->req) 62200a2430fSAndrzej Pietrasiewicz goto fail; 62300a2430fSAndrzej Pietrasiewicz 62400a2430fSAndrzej Pietrasiewicz hidg->req->buf = kmalloc(hidg->report_length, GFP_KERNEL); 62500a2430fSAndrzej Pietrasiewicz if (!hidg->req->buf) 62600a2430fSAndrzej Pietrasiewicz goto fail; 62700a2430fSAndrzej Pietrasiewicz 62800a2430fSAndrzej Pietrasiewicz /* set descriptor dynamic values */ 62900a2430fSAndrzej Pietrasiewicz hidg_interface_desc.bInterfaceSubClass = hidg->bInterfaceSubClass; 63000a2430fSAndrzej Pietrasiewicz hidg_interface_desc.bInterfaceProtocol = hidg->bInterfaceProtocol; 63100a2430fSAndrzej Pietrasiewicz hidg_hs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length); 63200a2430fSAndrzej Pietrasiewicz hidg_fs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length); 63300a2430fSAndrzej Pietrasiewicz hidg_hs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length); 63400a2430fSAndrzej Pietrasiewicz hidg_fs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length); 635f286d487SKrzysztof Opasiak /* 636f286d487SKrzysztof Opasiak * We can use hidg_desc struct here but we should not relay 637f286d487SKrzysztof Opasiak * that its content won't change after returning from this function. 638f286d487SKrzysztof Opasiak */ 63900a2430fSAndrzej Pietrasiewicz hidg_desc.desc[0].bDescriptorType = HID_DT_REPORT; 64000a2430fSAndrzej Pietrasiewicz hidg_desc.desc[0].wDescriptorLength = 64100a2430fSAndrzej Pietrasiewicz cpu_to_le16(hidg->report_desc_length); 64200a2430fSAndrzej Pietrasiewicz 64300a2430fSAndrzej Pietrasiewicz hidg_hs_in_ep_desc.bEndpointAddress = 64400a2430fSAndrzej Pietrasiewicz hidg_fs_in_ep_desc.bEndpointAddress; 64500a2430fSAndrzej Pietrasiewicz hidg_hs_out_ep_desc.bEndpointAddress = 64600a2430fSAndrzej Pietrasiewicz hidg_fs_out_ep_desc.bEndpointAddress; 64700a2430fSAndrzej Pietrasiewicz 64800a2430fSAndrzej Pietrasiewicz status = usb_assign_descriptors(f, hidg_fs_descriptors, 649eaef50c7SJohn Youn hidg_hs_descriptors, NULL, NULL); 65000a2430fSAndrzej Pietrasiewicz if (status) 65100a2430fSAndrzej Pietrasiewicz goto fail; 65200a2430fSAndrzej Pietrasiewicz 65300a2430fSAndrzej Pietrasiewicz mutex_init(&hidg->lock); 65400a2430fSAndrzej Pietrasiewicz spin_lock_init(&hidg->spinlock); 65500a2430fSAndrzej Pietrasiewicz init_waitqueue_head(&hidg->write_queue); 65600a2430fSAndrzej Pietrasiewicz init_waitqueue_head(&hidg->read_queue); 65700a2430fSAndrzej Pietrasiewicz INIT_LIST_HEAD(&hidg->completed_out_req); 65800a2430fSAndrzej Pietrasiewicz 65900a2430fSAndrzej Pietrasiewicz /* create char device */ 66000a2430fSAndrzej Pietrasiewicz cdev_init(&hidg->cdev, &f_hidg_fops); 66100a2430fSAndrzej Pietrasiewicz dev = MKDEV(major, hidg->minor); 66200a2430fSAndrzej Pietrasiewicz status = cdev_add(&hidg->cdev, dev, 1); 66300a2430fSAndrzej Pietrasiewicz if (status) 664d12a8727SPavitrakumar Managutte goto fail_free_descs; 66500a2430fSAndrzej Pietrasiewicz 66663406087SAndrzej Pietrasiewicz device = device_create(hidg_class, NULL, dev, NULL, 66763406087SAndrzej Pietrasiewicz "%s%d", "hidg", hidg->minor); 66863406087SAndrzej Pietrasiewicz if (IS_ERR(device)) { 66963406087SAndrzej Pietrasiewicz status = PTR_ERR(device); 67063406087SAndrzej Pietrasiewicz goto del; 67163406087SAndrzej Pietrasiewicz } 67200a2430fSAndrzej Pietrasiewicz 67300a2430fSAndrzej Pietrasiewicz return 0; 67463406087SAndrzej Pietrasiewicz del: 67563406087SAndrzej Pietrasiewicz cdev_del(&hidg->cdev); 676d12a8727SPavitrakumar Managutte fail_free_descs: 677d12a8727SPavitrakumar Managutte usb_free_all_descriptors(f); 67800a2430fSAndrzej Pietrasiewicz fail: 67900a2430fSAndrzej Pietrasiewicz ERROR(f->config->cdev, "hidg_bind FAILED\n"); 68000a2430fSAndrzej Pietrasiewicz if (hidg->req != NULL) { 68100a2430fSAndrzej Pietrasiewicz kfree(hidg->req->buf); 68200a2430fSAndrzej Pietrasiewicz if (hidg->in_ep != NULL) 68300a2430fSAndrzej Pietrasiewicz usb_ep_free_request(hidg->in_ep, hidg->req); 68400a2430fSAndrzej Pietrasiewicz } 68500a2430fSAndrzej Pietrasiewicz 68600a2430fSAndrzej Pietrasiewicz return status; 68700a2430fSAndrzej Pietrasiewicz } 68800a2430fSAndrzej Pietrasiewicz 689cb382536SAndrzej Pietrasiewicz static inline int hidg_get_minor(void) 690cb382536SAndrzej Pietrasiewicz { 691cb382536SAndrzej Pietrasiewicz int ret; 692cb382536SAndrzej Pietrasiewicz 693cb382536SAndrzej Pietrasiewicz ret = ida_simple_get(&hidg_ida, 0, 0, GFP_KERNEL); 694774cf72fSAndrzej Pietrasiewicz if (ret >= HIDG_MINORS) { 695774cf72fSAndrzej Pietrasiewicz ida_simple_remove(&hidg_ida, ret); 696774cf72fSAndrzej Pietrasiewicz ret = -ENODEV; 697774cf72fSAndrzej Pietrasiewicz } 698cb382536SAndrzej Pietrasiewicz 699cb382536SAndrzej Pietrasiewicz return ret; 700cb382536SAndrzej Pietrasiewicz } 701cb382536SAndrzej Pietrasiewicz 70221a9476aSAndrzej Pietrasiewicz static inline struct f_hid_opts *to_f_hid_opts(struct config_item *item) 70321a9476aSAndrzej Pietrasiewicz { 70421a9476aSAndrzej Pietrasiewicz return container_of(to_config_group(item), struct f_hid_opts, 70521a9476aSAndrzej Pietrasiewicz func_inst.group); 70621a9476aSAndrzej Pietrasiewicz } 70721a9476aSAndrzej Pietrasiewicz 70821a9476aSAndrzej Pietrasiewicz static void hid_attr_release(struct config_item *item) 70921a9476aSAndrzej Pietrasiewicz { 71021a9476aSAndrzej Pietrasiewicz struct f_hid_opts *opts = to_f_hid_opts(item); 71121a9476aSAndrzej Pietrasiewicz 71221a9476aSAndrzej Pietrasiewicz usb_put_function_instance(&opts->func_inst); 71321a9476aSAndrzej Pietrasiewicz } 71421a9476aSAndrzej Pietrasiewicz 71521a9476aSAndrzej Pietrasiewicz static struct configfs_item_operations hidg_item_ops = { 71621a9476aSAndrzej Pietrasiewicz .release = hid_attr_release, 71721a9476aSAndrzej Pietrasiewicz }; 71821a9476aSAndrzej Pietrasiewicz 71921a9476aSAndrzej Pietrasiewicz #define F_HID_OPT(name, prec, limit) \ 720da4e527cSChristoph Hellwig static ssize_t f_hid_opts_##name##_show(struct config_item *item, char *page)\ 72121a9476aSAndrzej Pietrasiewicz { \ 722da4e527cSChristoph Hellwig struct f_hid_opts *opts = to_f_hid_opts(item); \ 72321a9476aSAndrzej Pietrasiewicz int result; \ 72421a9476aSAndrzej Pietrasiewicz \ 72521a9476aSAndrzej Pietrasiewicz mutex_lock(&opts->lock); \ 72621a9476aSAndrzej Pietrasiewicz result = sprintf(page, "%d\n", opts->name); \ 72721a9476aSAndrzej Pietrasiewicz mutex_unlock(&opts->lock); \ 72821a9476aSAndrzej Pietrasiewicz \ 72921a9476aSAndrzej Pietrasiewicz return result; \ 73021a9476aSAndrzej Pietrasiewicz } \ 73121a9476aSAndrzej Pietrasiewicz \ 732da4e527cSChristoph Hellwig static ssize_t f_hid_opts_##name##_store(struct config_item *item, \ 73321a9476aSAndrzej Pietrasiewicz const char *page, size_t len) \ 73421a9476aSAndrzej Pietrasiewicz { \ 735da4e527cSChristoph Hellwig struct f_hid_opts *opts = to_f_hid_opts(item); \ 73621a9476aSAndrzej Pietrasiewicz int ret; \ 73721a9476aSAndrzej Pietrasiewicz u##prec num; \ 73821a9476aSAndrzej Pietrasiewicz \ 73921a9476aSAndrzej Pietrasiewicz mutex_lock(&opts->lock); \ 74021a9476aSAndrzej Pietrasiewicz if (opts->refcnt) { \ 74121a9476aSAndrzej Pietrasiewicz ret = -EBUSY; \ 74221a9476aSAndrzej Pietrasiewicz goto end; \ 74321a9476aSAndrzej Pietrasiewicz } \ 74421a9476aSAndrzej Pietrasiewicz \ 74521a9476aSAndrzej Pietrasiewicz ret = kstrtou##prec(page, 0, &num); \ 74621a9476aSAndrzej Pietrasiewicz if (ret) \ 74721a9476aSAndrzej Pietrasiewicz goto end; \ 74821a9476aSAndrzej Pietrasiewicz \ 74921a9476aSAndrzej Pietrasiewicz if (num > limit) { \ 75021a9476aSAndrzej Pietrasiewicz ret = -EINVAL; \ 75121a9476aSAndrzej Pietrasiewicz goto end; \ 75221a9476aSAndrzej Pietrasiewicz } \ 75321a9476aSAndrzej Pietrasiewicz opts->name = num; \ 75421a9476aSAndrzej Pietrasiewicz ret = len; \ 75521a9476aSAndrzej Pietrasiewicz \ 75621a9476aSAndrzej Pietrasiewicz end: \ 75721a9476aSAndrzej Pietrasiewicz mutex_unlock(&opts->lock); \ 75821a9476aSAndrzej Pietrasiewicz return ret; \ 75921a9476aSAndrzej Pietrasiewicz } \ 76021a9476aSAndrzej Pietrasiewicz \ 761da4e527cSChristoph Hellwig CONFIGFS_ATTR(f_hid_opts_, name) 76221a9476aSAndrzej Pietrasiewicz 76321a9476aSAndrzej Pietrasiewicz F_HID_OPT(subclass, 8, 255); 76421a9476aSAndrzej Pietrasiewicz F_HID_OPT(protocol, 8, 255); 76539a2ac27SAndrzej Pietrasiewicz F_HID_OPT(report_length, 16, 65535); 76621a9476aSAndrzej Pietrasiewicz 767da4e527cSChristoph Hellwig static ssize_t f_hid_opts_report_desc_show(struct config_item *item, char *page) 76821a9476aSAndrzej Pietrasiewicz { 769da4e527cSChristoph Hellwig struct f_hid_opts *opts = to_f_hid_opts(item); 77021a9476aSAndrzej Pietrasiewicz int result; 77121a9476aSAndrzej Pietrasiewicz 77221a9476aSAndrzej Pietrasiewicz mutex_lock(&opts->lock); 77321a9476aSAndrzej Pietrasiewicz result = opts->report_desc_length; 77421a9476aSAndrzej Pietrasiewicz memcpy(page, opts->report_desc, opts->report_desc_length); 77521a9476aSAndrzej Pietrasiewicz mutex_unlock(&opts->lock); 77621a9476aSAndrzej Pietrasiewicz 77721a9476aSAndrzej Pietrasiewicz return result; 77821a9476aSAndrzej Pietrasiewicz } 77921a9476aSAndrzej Pietrasiewicz 780da4e527cSChristoph Hellwig static ssize_t f_hid_opts_report_desc_store(struct config_item *item, 78121a9476aSAndrzej Pietrasiewicz const char *page, size_t len) 78221a9476aSAndrzej Pietrasiewicz { 783da4e527cSChristoph Hellwig struct f_hid_opts *opts = to_f_hid_opts(item); 78421a9476aSAndrzej Pietrasiewicz int ret = -EBUSY; 78521a9476aSAndrzej Pietrasiewicz char *d; 78621a9476aSAndrzej Pietrasiewicz 78721a9476aSAndrzej Pietrasiewicz mutex_lock(&opts->lock); 78821a9476aSAndrzej Pietrasiewicz 78921a9476aSAndrzej Pietrasiewicz if (opts->refcnt) 79021a9476aSAndrzej Pietrasiewicz goto end; 79121a9476aSAndrzej Pietrasiewicz if (len > PAGE_SIZE) { 79221a9476aSAndrzej Pietrasiewicz ret = -ENOSPC; 79321a9476aSAndrzej Pietrasiewicz goto end; 79421a9476aSAndrzej Pietrasiewicz } 79521a9476aSAndrzej Pietrasiewicz d = kmemdup(page, len, GFP_KERNEL); 79621a9476aSAndrzej Pietrasiewicz if (!d) { 79721a9476aSAndrzej Pietrasiewicz ret = -ENOMEM; 79821a9476aSAndrzej Pietrasiewicz goto end; 79921a9476aSAndrzej Pietrasiewicz } 80021a9476aSAndrzej Pietrasiewicz kfree(opts->report_desc); 80121a9476aSAndrzej Pietrasiewicz opts->report_desc = d; 80221a9476aSAndrzej Pietrasiewicz opts->report_desc_length = len; 80321a9476aSAndrzej Pietrasiewicz opts->report_desc_alloc = true; 80421a9476aSAndrzej Pietrasiewicz ret = len; 80521a9476aSAndrzej Pietrasiewicz end: 80621a9476aSAndrzej Pietrasiewicz mutex_unlock(&opts->lock); 80721a9476aSAndrzej Pietrasiewicz return ret; 80821a9476aSAndrzej Pietrasiewicz } 80921a9476aSAndrzej Pietrasiewicz 810da4e527cSChristoph Hellwig CONFIGFS_ATTR(f_hid_opts_, report_desc); 81121a9476aSAndrzej Pietrasiewicz 812ed6fe1f5SJohannes Berg static ssize_t f_hid_opts_dev_show(struct config_item *item, char *page) 813ed6fe1f5SJohannes Berg { 814ed6fe1f5SJohannes Berg struct f_hid_opts *opts = to_f_hid_opts(item); 815ed6fe1f5SJohannes Berg 816ed6fe1f5SJohannes Berg return sprintf(page, "%d:%d\n", major, opts->minor); 817ed6fe1f5SJohannes Berg } 818ed6fe1f5SJohannes Berg 819ed6fe1f5SJohannes Berg CONFIGFS_ATTR_RO(f_hid_opts_, dev); 820ed6fe1f5SJohannes Berg 82121a9476aSAndrzej Pietrasiewicz static struct configfs_attribute *hid_attrs[] = { 822da4e527cSChristoph Hellwig &f_hid_opts_attr_subclass, 823da4e527cSChristoph Hellwig &f_hid_opts_attr_protocol, 824da4e527cSChristoph Hellwig &f_hid_opts_attr_report_length, 825da4e527cSChristoph Hellwig &f_hid_opts_attr_report_desc, 826ed6fe1f5SJohannes Berg &f_hid_opts_attr_dev, 82721a9476aSAndrzej Pietrasiewicz NULL, 82821a9476aSAndrzej Pietrasiewicz }; 82921a9476aSAndrzej Pietrasiewicz 83021a9476aSAndrzej Pietrasiewicz static struct config_item_type hid_func_type = { 83121a9476aSAndrzej Pietrasiewicz .ct_item_ops = &hidg_item_ops, 83221a9476aSAndrzej Pietrasiewicz .ct_attrs = hid_attrs, 83321a9476aSAndrzej Pietrasiewicz .ct_owner = THIS_MODULE, 83421a9476aSAndrzej Pietrasiewicz }; 83521a9476aSAndrzej Pietrasiewicz 836cb382536SAndrzej Pietrasiewicz static inline void hidg_put_minor(int minor) 837cb382536SAndrzej Pietrasiewicz { 838cb382536SAndrzej Pietrasiewicz ida_simple_remove(&hidg_ida, minor); 839cb382536SAndrzej Pietrasiewicz } 840cb382536SAndrzej Pietrasiewicz 841cb382536SAndrzej Pietrasiewicz static void hidg_free_inst(struct usb_function_instance *f) 842cb382536SAndrzej Pietrasiewicz { 843cb382536SAndrzej Pietrasiewicz struct f_hid_opts *opts; 844cb382536SAndrzej Pietrasiewicz 845cb382536SAndrzej Pietrasiewicz opts = container_of(f, struct f_hid_opts, func_inst); 846cb382536SAndrzej Pietrasiewicz 847cb382536SAndrzej Pietrasiewicz mutex_lock(&hidg_ida_lock); 848cb382536SAndrzej Pietrasiewicz 849cb382536SAndrzej Pietrasiewicz hidg_put_minor(opts->minor); 850cb382536SAndrzej Pietrasiewicz if (idr_is_empty(&hidg_ida.idr)) 851cb382536SAndrzej Pietrasiewicz ghid_cleanup(); 852cb382536SAndrzej Pietrasiewicz 853cb382536SAndrzej Pietrasiewicz mutex_unlock(&hidg_ida_lock); 854cb382536SAndrzej Pietrasiewicz 855cb382536SAndrzej Pietrasiewicz if (opts->report_desc_alloc) 856cb382536SAndrzej Pietrasiewicz kfree(opts->report_desc); 857cb382536SAndrzej Pietrasiewicz 858cb382536SAndrzej Pietrasiewicz kfree(opts); 859cb382536SAndrzej Pietrasiewicz } 860cb382536SAndrzej Pietrasiewicz 861cb382536SAndrzej Pietrasiewicz static struct usb_function_instance *hidg_alloc_inst(void) 862cb382536SAndrzej Pietrasiewicz { 863cb382536SAndrzej Pietrasiewicz struct f_hid_opts *opts; 864cb382536SAndrzej Pietrasiewicz struct usb_function_instance *ret; 865cb382536SAndrzej Pietrasiewicz int status = 0; 866cb382536SAndrzej Pietrasiewicz 867cb382536SAndrzej Pietrasiewicz opts = kzalloc(sizeof(*opts), GFP_KERNEL); 868cb382536SAndrzej Pietrasiewicz if (!opts) 869cb382536SAndrzej Pietrasiewicz return ERR_PTR(-ENOMEM); 87021a9476aSAndrzej Pietrasiewicz mutex_init(&opts->lock); 871cb382536SAndrzej Pietrasiewicz opts->func_inst.free_func_inst = hidg_free_inst; 872cb382536SAndrzej Pietrasiewicz ret = &opts->func_inst; 873cb382536SAndrzej Pietrasiewicz 874cb382536SAndrzej Pietrasiewicz mutex_lock(&hidg_ida_lock); 875cb382536SAndrzej Pietrasiewicz 876cb382536SAndrzej Pietrasiewicz if (idr_is_empty(&hidg_ida.idr)) { 877cb382536SAndrzej Pietrasiewicz status = ghid_setup(NULL, HIDG_MINORS); 878cb382536SAndrzej Pietrasiewicz if (status) { 879cb382536SAndrzej Pietrasiewicz ret = ERR_PTR(status); 880cb382536SAndrzej Pietrasiewicz kfree(opts); 881cb382536SAndrzej Pietrasiewicz goto unlock; 882cb382536SAndrzej Pietrasiewicz } 883cb382536SAndrzej Pietrasiewicz } 884cb382536SAndrzej Pietrasiewicz 885cb382536SAndrzej Pietrasiewicz opts->minor = hidg_get_minor(); 886cb382536SAndrzej Pietrasiewicz if (opts->minor < 0) { 887cb382536SAndrzej Pietrasiewicz ret = ERR_PTR(opts->minor); 888cb382536SAndrzej Pietrasiewicz kfree(opts); 889cb382536SAndrzej Pietrasiewicz if (idr_is_empty(&hidg_ida.idr)) 890cb382536SAndrzej Pietrasiewicz ghid_cleanup(); 891828f6148SDan Carpenter goto unlock; 892cb382536SAndrzej Pietrasiewicz } 89321a9476aSAndrzej Pietrasiewicz config_group_init_type_name(&opts->func_inst.group, "", &hid_func_type); 894cb382536SAndrzej Pietrasiewicz 895cb382536SAndrzej Pietrasiewicz unlock: 896cb382536SAndrzej Pietrasiewicz mutex_unlock(&hidg_ida_lock); 897cb382536SAndrzej Pietrasiewicz return ret; 898cb382536SAndrzej Pietrasiewicz } 899cb382536SAndrzej Pietrasiewicz 900cb382536SAndrzej Pietrasiewicz static void hidg_free(struct usb_function *f) 901cb382536SAndrzej Pietrasiewicz { 902cb382536SAndrzej Pietrasiewicz struct f_hidg *hidg; 90321a9476aSAndrzej Pietrasiewicz struct f_hid_opts *opts; 904cb382536SAndrzej Pietrasiewicz 905cb382536SAndrzej Pietrasiewicz hidg = func_to_hidg(f); 90621a9476aSAndrzej Pietrasiewicz opts = container_of(f->fi, struct f_hid_opts, func_inst); 907cb382536SAndrzej Pietrasiewicz kfree(hidg->report_desc); 908cb382536SAndrzej Pietrasiewicz kfree(hidg); 90921a9476aSAndrzej Pietrasiewicz mutex_lock(&opts->lock); 91021a9476aSAndrzej Pietrasiewicz --opts->refcnt; 91121a9476aSAndrzej Pietrasiewicz mutex_unlock(&opts->lock); 912cb382536SAndrzej Pietrasiewicz } 913cb382536SAndrzej Pietrasiewicz 91400a2430fSAndrzej Pietrasiewicz static void hidg_unbind(struct usb_configuration *c, struct usb_function *f) 91500a2430fSAndrzej Pietrasiewicz { 91600a2430fSAndrzej Pietrasiewicz struct f_hidg *hidg = func_to_hidg(f); 91700a2430fSAndrzej Pietrasiewicz 91800a2430fSAndrzej Pietrasiewicz device_destroy(hidg_class, MKDEV(major, hidg->minor)); 91900a2430fSAndrzej Pietrasiewicz cdev_del(&hidg->cdev); 92000a2430fSAndrzej Pietrasiewicz 92100a2430fSAndrzej Pietrasiewicz /* disable/free request and end point */ 92200a2430fSAndrzej Pietrasiewicz usb_ep_disable(hidg->in_ep); 92300a2430fSAndrzej Pietrasiewicz kfree(hidg->req->buf); 92400a2430fSAndrzej Pietrasiewicz usb_ep_free_request(hidg->in_ep, hidg->req); 92500a2430fSAndrzej Pietrasiewicz 92600a2430fSAndrzej Pietrasiewicz usb_free_all_descriptors(f); 92700a2430fSAndrzej Pietrasiewicz } 92800a2430fSAndrzej Pietrasiewicz 9290fc57ea0SFengguang Wu static struct usb_function *hidg_alloc(struct usb_function_instance *fi) 93000a2430fSAndrzej Pietrasiewicz { 93100a2430fSAndrzej Pietrasiewicz struct f_hidg *hidg; 932cb382536SAndrzej Pietrasiewicz struct f_hid_opts *opts; 93300a2430fSAndrzej Pietrasiewicz 93400a2430fSAndrzej Pietrasiewicz /* allocate and initialize one new instance */ 935cb382536SAndrzej Pietrasiewicz hidg = kzalloc(sizeof(*hidg), GFP_KERNEL); 93600a2430fSAndrzej Pietrasiewicz if (!hidg) 937cb382536SAndrzej Pietrasiewicz return ERR_PTR(-ENOMEM); 93800a2430fSAndrzej Pietrasiewicz 939cb382536SAndrzej Pietrasiewicz opts = container_of(fi, struct f_hid_opts, func_inst); 940cb382536SAndrzej Pietrasiewicz 94121a9476aSAndrzej Pietrasiewicz mutex_lock(&opts->lock); 94221a9476aSAndrzej Pietrasiewicz ++opts->refcnt; 94321a9476aSAndrzej Pietrasiewicz 944cb382536SAndrzej Pietrasiewicz hidg->minor = opts->minor; 945cb382536SAndrzej Pietrasiewicz hidg->bInterfaceSubClass = opts->subclass; 946cb382536SAndrzej Pietrasiewicz hidg->bInterfaceProtocol = opts->protocol; 947cb382536SAndrzej Pietrasiewicz hidg->report_length = opts->report_length; 948cb382536SAndrzej Pietrasiewicz hidg->report_desc_length = opts->report_desc_length; 949cb382536SAndrzej Pietrasiewicz if (opts->report_desc) { 950cb382536SAndrzej Pietrasiewicz hidg->report_desc = kmemdup(opts->report_desc, 951cb382536SAndrzej Pietrasiewicz opts->report_desc_length, 95200a2430fSAndrzej Pietrasiewicz GFP_KERNEL); 95300a2430fSAndrzej Pietrasiewicz if (!hidg->report_desc) { 95400a2430fSAndrzej Pietrasiewicz kfree(hidg); 95521a9476aSAndrzej Pietrasiewicz mutex_unlock(&opts->lock); 956cb382536SAndrzej Pietrasiewicz return ERR_PTR(-ENOMEM); 957cb382536SAndrzej Pietrasiewicz } 95800a2430fSAndrzej Pietrasiewicz } 95900a2430fSAndrzej Pietrasiewicz 96021a9476aSAndrzej Pietrasiewicz mutex_unlock(&opts->lock); 96121a9476aSAndrzej Pietrasiewicz 96200a2430fSAndrzej Pietrasiewicz hidg->func.name = "hid"; 96300a2430fSAndrzej Pietrasiewicz hidg->func.bind = hidg_bind; 96400a2430fSAndrzej Pietrasiewicz hidg->func.unbind = hidg_unbind; 96500a2430fSAndrzej Pietrasiewicz hidg->func.set_alt = hidg_set_alt; 96600a2430fSAndrzej Pietrasiewicz hidg->func.disable = hidg_disable; 96700a2430fSAndrzej Pietrasiewicz hidg->func.setup = hidg_setup; 968cb382536SAndrzej Pietrasiewicz hidg->func.free_func = hidg_free; 96900a2430fSAndrzej Pietrasiewicz 97000a2430fSAndrzej Pietrasiewicz /* this could me made configurable at some point */ 97100a2430fSAndrzej Pietrasiewicz hidg->qlen = 4; 97200a2430fSAndrzej Pietrasiewicz 973cb382536SAndrzej Pietrasiewicz return &hidg->func; 97400a2430fSAndrzej Pietrasiewicz } 97500a2430fSAndrzej Pietrasiewicz 976cb382536SAndrzej Pietrasiewicz DECLARE_USB_FUNCTION_INIT(hid, hidg_alloc_inst, hidg_alloc); 977cb382536SAndrzej Pietrasiewicz MODULE_LICENSE("GPL"); 978cb382536SAndrzej Pietrasiewicz MODULE_AUTHOR("Fabien Chouteau"); 979cb382536SAndrzej Pietrasiewicz 980cb382536SAndrzej Pietrasiewicz int ghid_setup(struct usb_gadget *g, int count) 98100a2430fSAndrzej Pietrasiewicz { 98200a2430fSAndrzej Pietrasiewicz int status; 98300a2430fSAndrzej Pietrasiewicz dev_t dev; 98400a2430fSAndrzej Pietrasiewicz 98500a2430fSAndrzej Pietrasiewicz hidg_class = class_create(THIS_MODULE, "hidg"); 98606529407SAndrzej Pietrasiewicz if (IS_ERR(hidg_class)) { 9870448d38cSDan Carpenter status = PTR_ERR(hidg_class); 98806529407SAndrzej Pietrasiewicz hidg_class = NULL; 9890448d38cSDan Carpenter return status; 99000a2430fSAndrzej Pietrasiewicz } 99100a2430fSAndrzej Pietrasiewicz 99200a2430fSAndrzej Pietrasiewicz status = alloc_chrdev_region(&dev, 0, count, "hidg"); 9930448d38cSDan Carpenter if (status) { 9940448d38cSDan Carpenter class_destroy(hidg_class); 9950448d38cSDan Carpenter hidg_class = NULL; 99600a2430fSAndrzej Pietrasiewicz return status; 99700a2430fSAndrzej Pietrasiewicz } 99800a2430fSAndrzej Pietrasiewicz 9990448d38cSDan Carpenter major = MAJOR(dev); 10000448d38cSDan Carpenter minors = count; 10010448d38cSDan Carpenter 10020448d38cSDan Carpenter return 0; 100300a2430fSAndrzej Pietrasiewicz } 100400a2430fSAndrzej Pietrasiewicz 100500a2430fSAndrzej Pietrasiewicz void ghid_cleanup(void) 100600a2430fSAndrzej Pietrasiewicz { 100700a2430fSAndrzej Pietrasiewicz if (major) { 100800a2430fSAndrzej Pietrasiewicz unregister_chrdev_region(MKDEV(major, 0), minors); 100900a2430fSAndrzej Pietrasiewicz major = minors = 0; 101000a2430fSAndrzej Pietrasiewicz } 101100a2430fSAndrzej Pietrasiewicz 101200a2430fSAndrzej Pietrasiewicz class_destroy(hidg_class); 101300a2430fSAndrzej Pietrasiewicz hidg_class = NULL; 101400a2430fSAndrzej Pietrasiewicz } 1015