15fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0+ 200a2430fSAndrzej Pietrasiewicz /* 300a2430fSAndrzej Pietrasiewicz * f_hid.c -- USB HID function driver 400a2430fSAndrzej Pietrasiewicz * 500a2430fSAndrzej Pietrasiewicz * Copyright (C) 2010 Fabien Chouteau <fabien.chouteau@barco.com> 600a2430fSAndrzej Pietrasiewicz */ 700a2430fSAndrzej Pietrasiewicz 800a2430fSAndrzej Pietrasiewicz #include <linux/kernel.h> 900a2430fSAndrzej Pietrasiewicz #include <linux/module.h> 1000a2430fSAndrzej Pietrasiewicz #include <linux/hid.h> 11cb382536SAndrzej Pietrasiewicz #include <linux/idr.h> 1200a2430fSAndrzej Pietrasiewicz #include <linux/cdev.h> 1300a2430fSAndrzej Pietrasiewicz #include <linux/mutex.h> 1400a2430fSAndrzej Pietrasiewicz #include <linux/poll.h> 1500a2430fSAndrzej Pietrasiewicz #include <linux/uaccess.h> 1600a2430fSAndrzej Pietrasiewicz #include <linux/wait.h> 1700a2430fSAndrzej Pietrasiewicz #include <linux/sched.h> 1800a2430fSAndrzej Pietrasiewicz #include <linux/usb/g_hid.h> 1900a2430fSAndrzej Pietrasiewicz 2000a2430fSAndrzej Pietrasiewicz #include "u_f.h" 21cb382536SAndrzej Pietrasiewicz #include "u_hid.h" 22cb382536SAndrzej Pietrasiewicz 23cb382536SAndrzej Pietrasiewicz #define HIDG_MINORS 4 2400a2430fSAndrzej Pietrasiewicz 2500a2430fSAndrzej Pietrasiewicz static int major, minors; 2600a2430fSAndrzej Pietrasiewicz static struct class *hidg_class; 27cb382536SAndrzej Pietrasiewicz static DEFINE_IDA(hidg_ida); 28cb382536SAndrzej Pietrasiewicz static DEFINE_MUTEX(hidg_ida_lock); /* protects access to hidg_ida */ 2900a2430fSAndrzej Pietrasiewicz 3000a2430fSAndrzej Pietrasiewicz /*-------------------------------------------------------------------------*/ 3100a2430fSAndrzej Pietrasiewicz /* HID gadget struct */ 3200a2430fSAndrzej Pietrasiewicz 3300a2430fSAndrzej Pietrasiewicz struct f_hidg_req_list { 3400a2430fSAndrzej Pietrasiewicz struct usb_request *req; 3500a2430fSAndrzej Pietrasiewicz unsigned int pos; 3600a2430fSAndrzej Pietrasiewicz struct list_head list; 3700a2430fSAndrzej Pietrasiewicz }; 3800a2430fSAndrzej Pietrasiewicz 3900a2430fSAndrzej Pietrasiewicz struct f_hidg { 4000a2430fSAndrzej Pietrasiewicz /* configuration */ 4100a2430fSAndrzej Pietrasiewicz unsigned char bInterfaceSubClass; 4200a2430fSAndrzej Pietrasiewicz unsigned char bInterfaceProtocol; 43b3c4ec71SAbdulhadi Mohamed unsigned char protocol; 44*afcff6dcSMaxim Devaev unsigned char idle; 4500a2430fSAndrzej Pietrasiewicz unsigned short report_desc_length; 4600a2430fSAndrzej Pietrasiewicz char *report_desc; 4700a2430fSAndrzej Pietrasiewicz unsigned short report_length; 4800a2430fSAndrzej Pietrasiewicz 4900a2430fSAndrzej Pietrasiewicz /* recv report */ 5000a2430fSAndrzej Pietrasiewicz struct list_head completed_out_req; 5133e4c1a9SKrzysztof Opasiak spinlock_t read_spinlock; 5200a2430fSAndrzej Pietrasiewicz wait_queue_head_t read_queue; 5300a2430fSAndrzej Pietrasiewicz unsigned int qlen; 5400a2430fSAndrzej Pietrasiewicz 5500a2430fSAndrzej Pietrasiewicz /* send report */ 5633e4c1a9SKrzysztof Opasiak spinlock_t write_spinlock; 5700a2430fSAndrzej Pietrasiewicz bool write_pending; 5800a2430fSAndrzej Pietrasiewicz wait_queue_head_t write_queue; 5900a2430fSAndrzej Pietrasiewicz struct usb_request *req; 6000a2430fSAndrzej Pietrasiewicz 6100a2430fSAndrzej Pietrasiewicz int minor; 6200a2430fSAndrzej Pietrasiewicz struct cdev cdev; 6300a2430fSAndrzej Pietrasiewicz struct usb_function func; 6400a2430fSAndrzej Pietrasiewicz 6500a2430fSAndrzej Pietrasiewicz struct usb_ep *in_ep; 6600a2430fSAndrzej Pietrasiewicz struct usb_ep *out_ep; 6700a2430fSAndrzej Pietrasiewicz }; 6800a2430fSAndrzej Pietrasiewicz 6900a2430fSAndrzej Pietrasiewicz static inline struct f_hidg *func_to_hidg(struct usb_function *f) 7000a2430fSAndrzej Pietrasiewicz { 7100a2430fSAndrzej Pietrasiewicz return container_of(f, struct f_hidg, func); 7200a2430fSAndrzej Pietrasiewicz } 7300a2430fSAndrzej Pietrasiewicz 7400a2430fSAndrzej Pietrasiewicz /*-------------------------------------------------------------------------*/ 7500a2430fSAndrzej Pietrasiewicz /* Static descriptors */ 7600a2430fSAndrzej Pietrasiewicz 7700a2430fSAndrzej Pietrasiewicz static struct usb_interface_descriptor hidg_interface_desc = { 7800a2430fSAndrzej Pietrasiewicz .bLength = sizeof hidg_interface_desc, 7900a2430fSAndrzej Pietrasiewicz .bDescriptorType = USB_DT_INTERFACE, 8000a2430fSAndrzej Pietrasiewicz /* .bInterfaceNumber = DYNAMIC */ 8100a2430fSAndrzej Pietrasiewicz .bAlternateSetting = 0, 8200a2430fSAndrzej Pietrasiewicz .bNumEndpoints = 2, 8300a2430fSAndrzej Pietrasiewicz .bInterfaceClass = USB_CLASS_HID, 8400a2430fSAndrzej Pietrasiewicz /* .bInterfaceSubClass = DYNAMIC */ 8500a2430fSAndrzej Pietrasiewicz /* .bInterfaceProtocol = DYNAMIC */ 8600a2430fSAndrzej Pietrasiewicz /* .iInterface = DYNAMIC */ 8700a2430fSAndrzej Pietrasiewicz }; 8800a2430fSAndrzej Pietrasiewicz 8900a2430fSAndrzej Pietrasiewicz static struct hid_descriptor hidg_desc = { 9000a2430fSAndrzej Pietrasiewicz .bLength = sizeof hidg_desc, 9100a2430fSAndrzej Pietrasiewicz .bDescriptorType = HID_DT_HID, 9233cb46c4SRuslan Bilovol .bcdHID = cpu_to_le16(0x0101), 9300a2430fSAndrzej Pietrasiewicz .bCountryCode = 0x00, 9400a2430fSAndrzej Pietrasiewicz .bNumDescriptors = 0x1, 9500a2430fSAndrzej Pietrasiewicz /*.desc[0].bDescriptorType = DYNAMIC */ 9600a2430fSAndrzej Pietrasiewicz /*.desc[0].wDescriptorLenght = DYNAMIC */ 9700a2430fSAndrzej Pietrasiewicz }; 9800a2430fSAndrzej Pietrasiewicz 99dbf499cfSJanusz Dziedzic /* Super-Speed Support */ 100dbf499cfSJanusz Dziedzic 101dbf499cfSJanusz Dziedzic static struct usb_endpoint_descriptor hidg_ss_in_ep_desc = { 102dbf499cfSJanusz Dziedzic .bLength = USB_DT_ENDPOINT_SIZE, 103dbf499cfSJanusz Dziedzic .bDescriptorType = USB_DT_ENDPOINT, 104dbf499cfSJanusz Dziedzic .bEndpointAddress = USB_DIR_IN, 105dbf499cfSJanusz Dziedzic .bmAttributes = USB_ENDPOINT_XFER_INT, 106dbf499cfSJanusz Dziedzic /*.wMaxPacketSize = DYNAMIC */ 107dbf499cfSJanusz Dziedzic .bInterval = 4, /* FIXME: Add this field in the 108dbf499cfSJanusz Dziedzic * HID gadget configuration? 109dbf499cfSJanusz Dziedzic * (struct hidg_func_descriptor) 110dbf499cfSJanusz Dziedzic */ 111dbf499cfSJanusz Dziedzic }; 112dbf499cfSJanusz Dziedzic 113dbf499cfSJanusz Dziedzic static struct usb_ss_ep_comp_descriptor hidg_ss_in_comp_desc = { 114dbf499cfSJanusz Dziedzic .bLength = sizeof(hidg_ss_in_comp_desc), 115dbf499cfSJanusz Dziedzic .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, 116dbf499cfSJanusz Dziedzic 117dbf499cfSJanusz Dziedzic /* .bMaxBurst = 0, */ 118dbf499cfSJanusz Dziedzic /* .bmAttributes = 0, */ 119dbf499cfSJanusz Dziedzic /* .wBytesPerInterval = DYNAMIC */ 120dbf499cfSJanusz Dziedzic }; 121dbf499cfSJanusz Dziedzic 122dbf499cfSJanusz Dziedzic static struct usb_endpoint_descriptor hidg_ss_out_ep_desc = { 123dbf499cfSJanusz Dziedzic .bLength = USB_DT_ENDPOINT_SIZE, 124dbf499cfSJanusz Dziedzic .bDescriptorType = USB_DT_ENDPOINT, 125dbf499cfSJanusz Dziedzic .bEndpointAddress = USB_DIR_OUT, 126dbf499cfSJanusz Dziedzic .bmAttributes = USB_ENDPOINT_XFER_INT, 127dbf499cfSJanusz Dziedzic /*.wMaxPacketSize = DYNAMIC */ 128dbf499cfSJanusz Dziedzic .bInterval = 4, /* FIXME: Add this field in the 129dbf499cfSJanusz Dziedzic * HID gadget configuration? 130dbf499cfSJanusz Dziedzic * (struct hidg_func_descriptor) 131dbf499cfSJanusz Dziedzic */ 132dbf499cfSJanusz Dziedzic }; 133dbf499cfSJanusz Dziedzic 134dbf499cfSJanusz Dziedzic static struct usb_ss_ep_comp_descriptor hidg_ss_out_comp_desc = { 135dbf499cfSJanusz Dziedzic .bLength = sizeof(hidg_ss_out_comp_desc), 136dbf499cfSJanusz Dziedzic .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, 137dbf499cfSJanusz Dziedzic 138dbf499cfSJanusz Dziedzic /* .bMaxBurst = 0, */ 139dbf499cfSJanusz Dziedzic /* .bmAttributes = 0, */ 140dbf499cfSJanusz Dziedzic /* .wBytesPerInterval = DYNAMIC */ 141dbf499cfSJanusz Dziedzic }; 142dbf499cfSJanusz Dziedzic 143dbf499cfSJanusz Dziedzic static struct usb_descriptor_header *hidg_ss_descriptors[] = { 144dbf499cfSJanusz Dziedzic (struct usb_descriptor_header *)&hidg_interface_desc, 145dbf499cfSJanusz Dziedzic (struct usb_descriptor_header *)&hidg_desc, 146dbf499cfSJanusz Dziedzic (struct usb_descriptor_header *)&hidg_ss_in_ep_desc, 147dbf499cfSJanusz Dziedzic (struct usb_descriptor_header *)&hidg_ss_in_comp_desc, 148dbf499cfSJanusz Dziedzic (struct usb_descriptor_header *)&hidg_ss_out_ep_desc, 149dbf499cfSJanusz Dziedzic (struct usb_descriptor_header *)&hidg_ss_out_comp_desc, 150dbf499cfSJanusz Dziedzic NULL, 151dbf499cfSJanusz Dziedzic }; 152dbf499cfSJanusz Dziedzic 15300a2430fSAndrzej Pietrasiewicz /* High-Speed Support */ 15400a2430fSAndrzej Pietrasiewicz 15500a2430fSAndrzej Pietrasiewicz static struct usb_endpoint_descriptor hidg_hs_in_ep_desc = { 15600a2430fSAndrzej Pietrasiewicz .bLength = USB_DT_ENDPOINT_SIZE, 15700a2430fSAndrzej Pietrasiewicz .bDescriptorType = USB_DT_ENDPOINT, 15800a2430fSAndrzej Pietrasiewicz .bEndpointAddress = USB_DIR_IN, 15900a2430fSAndrzej Pietrasiewicz .bmAttributes = USB_ENDPOINT_XFER_INT, 16000a2430fSAndrzej Pietrasiewicz /*.wMaxPacketSize = DYNAMIC */ 16100a2430fSAndrzej Pietrasiewicz .bInterval = 4, /* FIXME: Add this field in the 16200a2430fSAndrzej Pietrasiewicz * HID gadget configuration? 16300a2430fSAndrzej Pietrasiewicz * (struct hidg_func_descriptor) 16400a2430fSAndrzej Pietrasiewicz */ 16500a2430fSAndrzej Pietrasiewicz }; 16600a2430fSAndrzej Pietrasiewicz 16700a2430fSAndrzej Pietrasiewicz static struct usb_endpoint_descriptor hidg_hs_out_ep_desc = { 16800a2430fSAndrzej Pietrasiewicz .bLength = USB_DT_ENDPOINT_SIZE, 16900a2430fSAndrzej Pietrasiewicz .bDescriptorType = USB_DT_ENDPOINT, 17000a2430fSAndrzej Pietrasiewicz .bEndpointAddress = USB_DIR_OUT, 17100a2430fSAndrzej Pietrasiewicz .bmAttributes = USB_ENDPOINT_XFER_INT, 17200a2430fSAndrzej Pietrasiewicz /*.wMaxPacketSize = DYNAMIC */ 17300a2430fSAndrzej Pietrasiewicz .bInterval = 4, /* FIXME: Add this field in the 17400a2430fSAndrzej Pietrasiewicz * HID gadget configuration? 17500a2430fSAndrzej Pietrasiewicz * (struct hidg_func_descriptor) 17600a2430fSAndrzej Pietrasiewicz */ 17700a2430fSAndrzej Pietrasiewicz }; 17800a2430fSAndrzej Pietrasiewicz 17900a2430fSAndrzej Pietrasiewicz static struct usb_descriptor_header *hidg_hs_descriptors[] = { 18000a2430fSAndrzej Pietrasiewicz (struct usb_descriptor_header *)&hidg_interface_desc, 18100a2430fSAndrzej Pietrasiewicz (struct usb_descriptor_header *)&hidg_desc, 18200a2430fSAndrzej Pietrasiewicz (struct usb_descriptor_header *)&hidg_hs_in_ep_desc, 18300a2430fSAndrzej Pietrasiewicz (struct usb_descriptor_header *)&hidg_hs_out_ep_desc, 18400a2430fSAndrzej Pietrasiewicz NULL, 18500a2430fSAndrzej Pietrasiewicz }; 18600a2430fSAndrzej Pietrasiewicz 18700a2430fSAndrzej Pietrasiewicz /* Full-Speed Support */ 18800a2430fSAndrzej Pietrasiewicz 18900a2430fSAndrzej Pietrasiewicz static struct usb_endpoint_descriptor hidg_fs_in_ep_desc = { 19000a2430fSAndrzej Pietrasiewicz .bLength = USB_DT_ENDPOINT_SIZE, 19100a2430fSAndrzej Pietrasiewicz .bDescriptorType = USB_DT_ENDPOINT, 19200a2430fSAndrzej Pietrasiewicz .bEndpointAddress = USB_DIR_IN, 19300a2430fSAndrzej Pietrasiewicz .bmAttributes = USB_ENDPOINT_XFER_INT, 19400a2430fSAndrzej Pietrasiewicz /*.wMaxPacketSize = DYNAMIC */ 19500a2430fSAndrzej Pietrasiewicz .bInterval = 10, /* FIXME: Add this field in the 19600a2430fSAndrzej Pietrasiewicz * HID gadget configuration? 19700a2430fSAndrzej Pietrasiewicz * (struct hidg_func_descriptor) 19800a2430fSAndrzej Pietrasiewicz */ 19900a2430fSAndrzej Pietrasiewicz }; 20000a2430fSAndrzej Pietrasiewicz 20100a2430fSAndrzej Pietrasiewicz static struct usb_endpoint_descriptor hidg_fs_out_ep_desc = { 20200a2430fSAndrzej Pietrasiewicz .bLength = USB_DT_ENDPOINT_SIZE, 20300a2430fSAndrzej Pietrasiewicz .bDescriptorType = USB_DT_ENDPOINT, 20400a2430fSAndrzej Pietrasiewicz .bEndpointAddress = USB_DIR_OUT, 20500a2430fSAndrzej Pietrasiewicz .bmAttributes = USB_ENDPOINT_XFER_INT, 20600a2430fSAndrzej Pietrasiewicz /*.wMaxPacketSize = DYNAMIC */ 20700a2430fSAndrzej Pietrasiewicz .bInterval = 10, /* FIXME: Add this field in the 20800a2430fSAndrzej Pietrasiewicz * HID gadget configuration? 20900a2430fSAndrzej Pietrasiewicz * (struct hidg_func_descriptor) 21000a2430fSAndrzej Pietrasiewicz */ 21100a2430fSAndrzej Pietrasiewicz }; 21200a2430fSAndrzej Pietrasiewicz 21300a2430fSAndrzej Pietrasiewicz static struct usb_descriptor_header *hidg_fs_descriptors[] = { 21400a2430fSAndrzej Pietrasiewicz (struct usb_descriptor_header *)&hidg_interface_desc, 21500a2430fSAndrzej Pietrasiewicz (struct usb_descriptor_header *)&hidg_desc, 21600a2430fSAndrzej Pietrasiewicz (struct usb_descriptor_header *)&hidg_fs_in_ep_desc, 21700a2430fSAndrzej Pietrasiewicz (struct usb_descriptor_header *)&hidg_fs_out_ep_desc, 21800a2430fSAndrzej Pietrasiewicz NULL, 21900a2430fSAndrzej Pietrasiewicz }; 22000a2430fSAndrzej Pietrasiewicz 22100a2430fSAndrzej Pietrasiewicz /*-------------------------------------------------------------------------*/ 222cb382536SAndrzej Pietrasiewicz /* Strings */ 223cb382536SAndrzej Pietrasiewicz 224cb382536SAndrzej Pietrasiewicz #define CT_FUNC_HID_IDX 0 225cb382536SAndrzej Pietrasiewicz 226cb382536SAndrzej Pietrasiewicz static struct usb_string ct_func_string_defs[] = { 227cb382536SAndrzej Pietrasiewicz [CT_FUNC_HID_IDX].s = "HID Interface", 228cb382536SAndrzej Pietrasiewicz {}, /* end of list */ 229cb382536SAndrzej Pietrasiewicz }; 230cb382536SAndrzej Pietrasiewicz 231cb382536SAndrzej Pietrasiewicz static struct usb_gadget_strings ct_func_string_table = { 232cb382536SAndrzej Pietrasiewicz .language = 0x0409, /* en-US */ 233cb382536SAndrzej Pietrasiewicz .strings = ct_func_string_defs, 234cb382536SAndrzej Pietrasiewicz }; 235cb382536SAndrzej Pietrasiewicz 236cb382536SAndrzej Pietrasiewicz static struct usb_gadget_strings *ct_func_strings[] = { 237cb382536SAndrzej Pietrasiewicz &ct_func_string_table, 238cb382536SAndrzej Pietrasiewicz NULL, 239cb382536SAndrzej Pietrasiewicz }; 240cb382536SAndrzej Pietrasiewicz 241cb382536SAndrzej Pietrasiewicz /*-------------------------------------------------------------------------*/ 24200a2430fSAndrzej Pietrasiewicz /* Char Device */ 24300a2430fSAndrzej Pietrasiewicz 24400a2430fSAndrzej Pietrasiewicz static ssize_t f_hidg_read(struct file *file, char __user *buffer, 24500a2430fSAndrzej Pietrasiewicz size_t count, loff_t *ptr) 24600a2430fSAndrzej Pietrasiewicz { 24700a2430fSAndrzej Pietrasiewicz struct f_hidg *hidg = file->private_data; 24800a2430fSAndrzej Pietrasiewicz struct f_hidg_req_list *list; 24900a2430fSAndrzej Pietrasiewicz struct usb_request *req; 25000a2430fSAndrzej Pietrasiewicz unsigned long flags; 25100a2430fSAndrzej Pietrasiewicz int ret; 25200a2430fSAndrzej Pietrasiewicz 25300a2430fSAndrzej Pietrasiewicz if (!count) 25400a2430fSAndrzej Pietrasiewicz return 0; 25500a2430fSAndrzej Pietrasiewicz 25633e4c1a9SKrzysztof Opasiak spin_lock_irqsave(&hidg->read_spinlock, flags); 25700a2430fSAndrzej Pietrasiewicz 25800a2430fSAndrzej Pietrasiewicz #define READ_COND (!list_empty(&hidg->completed_out_req)) 25900a2430fSAndrzej Pietrasiewicz 26000a2430fSAndrzej Pietrasiewicz /* wait for at least one buffer to complete */ 26100a2430fSAndrzej Pietrasiewicz while (!READ_COND) { 26233e4c1a9SKrzysztof Opasiak spin_unlock_irqrestore(&hidg->read_spinlock, flags); 26300a2430fSAndrzej Pietrasiewicz if (file->f_flags & O_NONBLOCK) 26400a2430fSAndrzej Pietrasiewicz return -EAGAIN; 26500a2430fSAndrzej Pietrasiewicz 26600a2430fSAndrzej Pietrasiewicz if (wait_event_interruptible(hidg->read_queue, READ_COND)) 26700a2430fSAndrzej Pietrasiewicz return -ERESTARTSYS; 26800a2430fSAndrzej Pietrasiewicz 26933e4c1a9SKrzysztof Opasiak spin_lock_irqsave(&hidg->read_spinlock, flags); 27000a2430fSAndrzej Pietrasiewicz } 27100a2430fSAndrzej Pietrasiewicz 27200a2430fSAndrzej Pietrasiewicz /* pick the first one */ 27300a2430fSAndrzej Pietrasiewicz list = list_first_entry(&hidg->completed_out_req, 27400a2430fSAndrzej Pietrasiewicz struct f_hidg_req_list, list); 275aa65d11aSKrzysztof Opasiak 276aa65d11aSKrzysztof Opasiak /* 277aa65d11aSKrzysztof Opasiak * Remove this from list to protect it from beign free() 278aa65d11aSKrzysztof Opasiak * while host disables our function 279aa65d11aSKrzysztof Opasiak */ 280aa65d11aSKrzysztof Opasiak list_del(&list->list); 281aa65d11aSKrzysztof Opasiak 28200a2430fSAndrzej Pietrasiewicz req = list->req; 28300a2430fSAndrzej Pietrasiewicz count = min_t(unsigned int, count, req->actual - list->pos); 28433e4c1a9SKrzysztof Opasiak spin_unlock_irqrestore(&hidg->read_spinlock, flags); 28500a2430fSAndrzej Pietrasiewicz 28600a2430fSAndrzej Pietrasiewicz /* copy to user outside spinlock */ 28700a2430fSAndrzej Pietrasiewicz count -= copy_to_user(buffer, req->buf + list->pos, count); 28800a2430fSAndrzej Pietrasiewicz list->pos += count; 28900a2430fSAndrzej Pietrasiewicz 29000a2430fSAndrzej Pietrasiewicz /* 29100a2430fSAndrzej Pietrasiewicz * if this request is completely handled and transfered to 29200a2430fSAndrzej Pietrasiewicz * userspace, remove its entry from the list and requeue it 29300a2430fSAndrzej Pietrasiewicz * again. Otherwise, we will revisit it again upon the next 29400a2430fSAndrzej Pietrasiewicz * call, taking into account its current read position. 29500a2430fSAndrzej Pietrasiewicz */ 29600a2430fSAndrzej Pietrasiewicz if (list->pos == req->actual) { 29700a2430fSAndrzej Pietrasiewicz kfree(list); 29800a2430fSAndrzej Pietrasiewicz 29900a2430fSAndrzej Pietrasiewicz req->length = hidg->report_length; 30000a2430fSAndrzej Pietrasiewicz ret = usb_ep_queue(hidg->out_ep, req, GFP_KERNEL); 301aa65d11aSKrzysztof Opasiak if (ret < 0) { 302aa65d11aSKrzysztof Opasiak free_ep_req(hidg->out_ep, req); 30300a2430fSAndrzej Pietrasiewicz return ret; 30400a2430fSAndrzej Pietrasiewicz } 305aa65d11aSKrzysztof Opasiak } else { 30633e4c1a9SKrzysztof Opasiak spin_lock_irqsave(&hidg->read_spinlock, flags); 307aa65d11aSKrzysztof Opasiak list_add(&list->list, &hidg->completed_out_req); 30833e4c1a9SKrzysztof Opasiak spin_unlock_irqrestore(&hidg->read_spinlock, flags); 309aa65d11aSKrzysztof Opasiak 310aa65d11aSKrzysztof Opasiak wake_up(&hidg->read_queue); 311aa65d11aSKrzysztof Opasiak } 31200a2430fSAndrzej Pietrasiewicz 31300a2430fSAndrzej Pietrasiewicz return count; 31400a2430fSAndrzej Pietrasiewicz } 31500a2430fSAndrzej Pietrasiewicz 31600a2430fSAndrzej Pietrasiewicz static void f_hidg_req_complete(struct usb_ep *ep, struct usb_request *req) 31700a2430fSAndrzej Pietrasiewicz { 31800a2430fSAndrzej Pietrasiewicz struct f_hidg *hidg = (struct f_hidg *)ep->driver_data; 31933e4c1a9SKrzysztof Opasiak unsigned long flags; 32000a2430fSAndrzej Pietrasiewicz 32100a2430fSAndrzej Pietrasiewicz if (req->status != 0) { 32200a2430fSAndrzej Pietrasiewicz ERROR(hidg->func.config->cdev, 32300a2430fSAndrzej Pietrasiewicz "End Point Request ERROR: %d\n", req->status); 32400a2430fSAndrzej Pietrasiewicz } 32500a2430fSAndrzej Pietrasiewicz 32633e4c1a9SKrzysztof Opasiak spin_lock_irqsave(&hidg->write_spinlock, flags); 32700a2430fSAndrzej Pietrasiewicz hidg->write_pending = 0; 32833e4c1a9SKrzysztof Opasiak spin_unlock_irqrestore(&hidg->write_spinlock, flags); 32900a2430fSAndrzej Pietrasiewicz wake_up(&hidg->write_queue); 33000a2430fSAndrzej Pietrasiewicz } 33100a2430fSAndrzej Pietrasiewicz 33200a2430fSAndrzej Pietrasiewicz static ssize_t f_hidg_write(struct file *file, const char __user *buffer, 33300a2430fSAndrzej Pietrasiewicz size_t count, loff_t *offp) 33400a2430fSAndrzej Pietrasiewicz { 33500a2430fSAndrzej Pietrasiewicz struct f_hidg *hidg = file->private_data; 336749494b6SKrzysztof Opasiak struct usb_request *req; 33733e4c1a9SKrzysztof Opasiak unsigned long flags; 33800a2430fSAndrzej Pietrasiewicz ssize_t status = -ENOMEM; 33900a2430fSAndrzej Pietrasiewicz 34033e4c1a9SKrzysztof Opasiak spin_lock_irqsave(&hidg->write_spinlock, flags); 34100a2430fSAndrzej Pietrasiewicz 3422867652eSPhil Elwell if (!hidg->req) { 3432867652eSPhil Elwell spin_unlock_irqrestore(&hidg->write_spinlock, flags); 3442867652eSPhil Elwell return -ESHUTDOWN; 3452867652eSPhil Elwell } 3462867652eSPhil Elwell 34700a2430fSAndrzej Pietrasiewicz #define WRITE_COND (!hidg->write_pending) 348749494b6SKrzysztof Opasiak try_again: 34900a2430fSAndrzej Pietrasiewicz /* write queue */ 35000a2430fSAndrzej Pietrasiewicz while (!WRITE_COND) { 35133e4c1a9SKrzysztof Opasiak spin_unlock_irqrestore(&hidg->write_spinlock, flags); 35200a2430fSAndrzej Pietrasiewicz if (file->f_flags & O_NONBLOCK) 35300a2430fSAndrzej Pietrasiewicz return -EAGAIN; 35400a2430fSAndrzej Pietrasiewicz 35500a2430fSAndrzej Pietrasiewicz if (wait_event_interruptible_exclusive( 35600a2430fSAndrzej Pietrasiewicz hidg->write_queue, WRITE_COND)) 35700a2430fSAndrzej Pietrasiewicz return -ERESTARTSYS; 35800a2430fSAndrzej Pietrasiewicz 35933e4c1a9SKrzysztof Opasiak spin_lock_irqsave(&hidg->write_spinlock, flags); 36000a2430fSAndrzej Pietrasiewicz } 36100a2430fSAndrzej Pietrasiewicz 36233e4c1a9SKrzysztof Opasiak hidg->write_pending = 1; 363749494b6SKrzysztof Opasiak req = hidg->req; 36400a2430fSAndrzej Pietrasiewicz count = min_t(unsigned, count, hidg->report_length); 36533e4c1a9SKrzysztof Opasiak 36633e4c1a9SKrzysztof Opasiak spin_unlock_irqrestore(&hidg->write_spinlock, flags); 36700a2430fSAndrzej Pietrasiewicz 3682867652eSPhil Elwell if (!req) { 3692867652eSPhil Elwell ERROR(hidg->func.config->cdev, "hidg->req is NULL\n"); 3702867652eSPhil Elwell status = -ESHUTDOWN; 3712867652eSPhil Elwell goto release_write_pending; 3722867652eSPhil Elwell } 3732867652eSPhil Elwell 3742867652eSPhil Elwell status = copy_from_user(req->buf, buffer, count); 37500a2430fSAndrzej Pietrasiewicz if (status != 0) { 37600a2430fSAndrzej Pietrasiewicz ERROR(hidg->func.config->cdev, 37700a2430fSAndrzej Pietrasiewicz "copy_from_user error\n"); 37833e4c1a9SKrzysztof Opasiak status = -EINVAL; 37933e4c1a9SKrzysztof Opasiak goto release_write_pending; 38000a2430fSAndrzej Pietrasiewicz } 38100a2430fSAndrzej Pietrasiewicz 382749494b6SKrzysztof Opasiak spin_lock_irqsave(&hidg->write_spinlock, flags); 383749494b6SKrzysztof Opasiak 38425cd9721SKrzysztof Opasiak /* when our function has been disabled by host */ 385749494b6SKrzysztof Opasiak if (!hidg->req) { 38625cd9721SKrzysztof Opasiak free_ep_req(hidg->in_ep, req); 387749494b6SKrzysztof Opasiak /* 388749494b6SKrzysztof Opasiak * TODO 389749494b6SKrzysztof Opasiak * Should we fail with error here? 390749494b6SKrzysztof Opasiak */ 391749494b6SKrzysztof Opasiak goto try_again; 392749494b6SKrzysztof Opasiak } 393749494b6SKrzysztof Opasiak 394749494b6SKrzysztof Opasiak req->status = 0; 395749494b6SKrzysztof Opasiak req->zero = 0; 396749494b6SKrzysztof Opasiak req->length = count; 397749494b6SKrzysztof Opasiak req->complete = f_hidg_req_complete; 398749494b6SKrzysztof Opasiak req->context = hidg; 39900a2430fSAndrzej Pietrasiewicz 400072684e8SRadoslav Gerganov spin_unlock_irqrestore(&hidg->write_spinlock, flags); 401072684e8SRadoslav Gerganov 4022867652eSPhil Elwell if (!hidg->in_ep->enabled) { 4032867652eSPhil Elwell ERROR(hidg->func.config->cdev, "in_ep is disabled\n"); 4042867652eSPhil Elwell status = -ESHUTDOWN; 405072684e8SRadoslav Gerganov goto release_write_pending; 40600a2430fSAndrzej Pietrasiewicz } 40700a2430fSAndrzej Pietrasiewicz 4082867652eSPhil Elwell status = usb_ep_queue(hidg->in_ep, req, GFP_ATOMIC); 4092867652eSPhil Elwell if (status < 0) 4102867652eSPhil Elwell goto release_write_pending; 4112867652eSPhil Elwell else 4122867652eSPhil Elwell status = count; 4132867652eSPhil Elwell 41433e4c1a9SKrzysztof Opasiak return status; 41533e4c1a9SKrzysztof Opasiak release_write_pending: 41633e4c1a9SKrzysztof Opasiak spin_lock_irqsave(&hidg->write_spinlock, flags); 41733e4c1a9SKrzysztof Opasiak hidg->write_pending = 0; 41833e4c1a9SKrzysztof Opasiak spin_unlock_irqrestore(&hidg->write_spinlock, flags); 41933e4c1a9SKrzysztof Opasiak 42033e4c1a9SKrzysztof Opasiak wake_up(&hidg->write_queue); 42100a2430fSAndrzej Pietrasiewicz 42200a2430fSAndrzej Pietrasiewicz return status; 42300a2430fSAndrzej Pietrasiewicz } 42400a2430fSAndrzej Pietrasiewicz 425afc9a42bSAl Viro static __poll_t f_hidg_poll(struct file *file, poll_table *wait) 42600a2430fSAndrzej Pietrasiewicz { 42700a2430fSAndrzej Pietrasiewicz struct f_hidg *hidg = file->private_data; 428afc9a42bSAl Viro __poll_t ret = 0; 42900a2430fSAndrzej Pietrasiewicz 43000a2430fSAndrzej Pietrasiewicz poll_wait(file, &hidg->read_queue, wait); 43100a2430fSAndrzej Pietrasiewicz poll_wait(file, &hidg->write_queue, wait); 43200a2430fSAndrzej Pietrasiewicz 43300a2430fSAndrzej Pietrasiewicz if (WRITE_COND) 434a9a08845SLinus Torvalds ret |= EPOLLOUT | EPOLLWRNORM; 43500a2430fSAndrzej Pietrasiewicz 43600a2430fSAndrzej Pietrasiewicz if (READ_COND) 437a9a08845SLinus Torvalds ret |= EPOLLIN | EPOLLRDNORM; 43800a2430fSAndrzej Pietrasiewicz 43900a2430fSAndrzej Pietrasiewicz return ret; 44000a2430fSAndrzej Pietrasiewicz } 44100a2430fSAndrzej Pietrasiewicz 44200a2430fSAndrzej Pietrasiewicz #undef WRITE_COND 44300a2430fSAndrzej Pietrasiewicz #undef READ_COND 44400a2430fSAndrzej Pietrasiewicz 44500a2430fSAndrzej Pietrasiewicz static int f_hidg_release(struct inode *inode, struct file *fd) 44600a2430fSAndrzej Pietrasiewicz { 44700a2430fSAndrzej Pietrasiewicz fd->private_data = NULL; 44800a2430fSAndrzej Pietrasiewicz return 0; 44900a2430fSAndrzej Pietrasiewicz } 45000a2430fSAndrzej Pietrasiewicz 45100a2430fSAndrzej Pietrasiewicz static int f_hidg_open(struct inode *inode, struct file *fd) 45200a2430fSAndrzej Pietrasiewicz { 45300a2430fSAndrzej Pietrasiewicz struct f_hidg *hidg = 45400a2430fSAndrzej Pietrasiewicz container_of(inode->i_cdev, struct f_hidg, cdev); 45500a2430fSAndrzej Pietrasiewicz 45600a2430fSAndrzej Pietrasiewicz fd->private_data = hidg; 45700a2430fSAndrzej Pietrasiewicz 45800a2430fSAndrzej Pietrasiewicz return 0; 45900a2430fSAndrzej Pietrasiewicz } 46000a2430fSAndrzej Pietrasiewicz 46100a2430fSAndrzej Pietrasiewicz /*-------------------------------------------------------------------------*/ 46200a2430fSAndrzej Pietrasiewicz /* usb_function */ 46300a2430fSAndrzej Pietrasiewicz 46400a2430fSAndrzej Pietrasiewicz static inline struct usb_request *hidg_alloc_ep_req(struct usb_ep *ep, 46500a2430fSAndrzej Pietrasiewicz unsigned length) 46600a2430fSAndrzej Pietrasiewicz { 467aadbe812SFelipe F. Tonello return alloc_ep_req(ep, length); 46800a2430fSAndrzej Pietrasiewicz } 46900a2430fSAndrzej Pietrasiewicz 47000a2430fSAndrzej Pietrasiewicz static void hidg_set_report_complete(struct usb_ep *ep, struct usb_request *req) 47100a2430fSAndrzej Pietrasiewicz { 47200a2430fSAndrzej Pietrasiewicz struct f_hidg *hidg = (struct f_hidg *) req->context; 47320d2ca95SKrzysztof Opasiak struct usb_composite_dev *cdev = hidg->func.config->cdev; 47400a2430fSAndrzej Pietrasiewicz struct f_hidg_req_list *req_list; 47500a2430fSAndrzej Pietrasiewicz unsigned long flags; 47600a2430fSAndrzej Pietrasiewicz 47720d2ca95SKrzysztof Opasiak switch (req->status) { 47820d2ca95SKrzysztof Opasiak case 0: 47900a2430fSAndrzej Pietrasiewicz req_list = kzalloc(sizeof(*req_list), GFP_ATOMIC); 48020d2ca95SKrzysztof Opasiak if (!req_list) { 48120d2ca95SKrzysztof Opasiak ERROR(cdev, "Unable to allocate mem for req_list\n"); 48220d2ca95SKrzysztof Opasiak goto free_req; 48320d2ca95SKrzysztof Opasiak } 48400a2430fSAndrzej Pietrasiewicz 48500a2430fSAndrzej Pietrasiewicz req_list->req = req; 48600a2430fSAndrzej Pietrasiewicz 48733e4c1a9SKrzysztof Opasiak spin_lock_irqsave(&hidg->read_spinlock, flags); 48800a2430fSAndrzej Pietrasiewicz list_add_tail(&req_list->list, &hidg->completed_out_req); 48933e4c1a9SKrzysztof Opasiak spin_unlock_irqrestore(&hidg->read_spinlock, flags); 49000a2430fSAndrzej Pietrasiewicz 49100a2430fSAndrzej Pietrasiewicz wake_up(&hidg->read_queue); 49220d2ca95SKrzysztof Opasiak break; 49320d2ca95SKrzysztof Opasiak default: 49420d2ca95SKrzysztof Opasiak ERROR(cdev, "Set report failed %d\n", req->status); 495a74005abSGustavo A. R. Silva fallthrough; 49620d2ca95SKrzysztof Opasiak case -ECONNABORTED: /* hardware forced ep reset */ 49720d2ca95SKrzysztof Opasiak case -ECONNRESET: /* request dequeued */ 49820d2ca95SKrzysztof Opasiak case -ESHUTDOWN: /* disconnect from host */ 49920d2ca95SKrzysztof Opasiak free_req: 50020d2ca95SKrzysztof Opasiak free_ep_req(ep, req); 50120d2ca95SKrzysztof Opasiak return; 50220d2ca95SKrzysztof Opasiak } 50300a2430fSAndrzej Pietrasiewicz } 50400a2430fSAndrzej Pietrasiewicz 50500a2430fSAndrzej Pietrasiewicz static int hidg_setup(struct usb_function *f, 50600a2430fSAndrzej Pietrasiewicz const struct usb_ctrlrequest *ctrl) 50700a2430fSAndrzej Pietrasiewicz { 50800a2430fSAndrzej Pietrasiewicz struct f_hidg *hidg = func_to_hidg(f); 50900a2430fSAndrzej Pietrasiewicz struct usb_composite_dev *cdev = f->config->cdev; 51000a2430fSAndrzej Pietrasiewicz struct usb_request *req = cdev->req; 51100a2430fSAndrzej Pietrasiewicz int status = 0; 51200a2430fSAndrzej Pietrasiewicz __u16 value, length; 51300a2430fSAndrzej Pietrasiewicz 51400a2430fSAndrzej Pietrasiewicz value = __le16_to_cpu(ctrl->wValue); 51500a2430fSAndrzej Pietrasiewicz length = __le16_to_cpu(ctrl->wLength); 51600a2430fSAndrzej Pietrasiewicz 517c9b3bde0SJulia Lawall VDBG(cdev, 518c9b3bde0SJulia Lawall "%s crtl_request : bRequestType:0x%x bRequest:0x%x Value:0x%x\n", 519c9b3bde0SJulia Lawall __func__, ctrl->bRequestType, ctrl->bRequest, value); 52000a2430fSAndrzej Pietrasiewicz 52100a2430fSAndrzej Pietrasiewicz switch ((ctrl->bRequestType << 8) | ctrl->bRequest) { 52200a2430fSAndrzej Pietrasiewicz case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8 52300a2430fSAndrzej Pietrasiewicz | HID_REQ_GET_REPORT): 52400a2430fSAndrzej Pietrasiewicz VDBG(cdev, "get_report\n"); 52500a2430fSAndrzej Pietrasiewicz 52600a2430fSAndrzej Pietrasiewicz /* send an empty report */ 52700a2430fSAndrzej Pietrasiewicz length = min_t(unsigned, length, hidg->report_length); 52800a2430fSAndrzej Pietrasiewicz memset(req->buf, 0x0, length); 52900a2430fSAndrzej Pietrasiewicz 53000a2430fSAndrzej Pietrasiewicz goto respond; 53100a2430fSAndrzej Pietrasiewicz break; 53200a2430fSAndrzej Pietrasiewicz 53300a2430fSAndrzej Pietrasiewicz case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8 53400a2430fSAndrzej Pietrasiewicz | HID_REQ_GET_PROTOCOL): 53500a2430fSAndrzej Pietrasiewicz VDBG(cdev, "get_protocol\n"); 536b3c4ec71SAbdulhadi Mohamed length = min_t(unsigned int, length, 1); 537b3c4ec71SAbdulhadi Mohamed ((u8 *) req->buf)[0] = hidg->protocol; 538b3c4ec71SAbdulhadi Mohamed goto respond; 53900a2430fSAndrzej Pietrasiewicz break; 54000a2430fSAndrzej Pietrasiewicz 541*afcff6dcSMaxim Devaev case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8 542*afcff6dcSMaxim Devaev | HID_REQ_GET_IDLE): 543*afcff6dcSMaxim Devaev VDBG(cdev, "get_idle\n"); 544*afcff6dcSMaxim Devaev length = min_t(unsigned int, length, 1); 545*afcff6dcSMaxim Devaev ((u8 *) req->buf)[0] = hidg->idle; 546*afcff6dcSMaxim Devaev goto respond; 547*afcff6dcSMaxim Devaev break; 548*afcff6dcSMaxim Devaev 54900a2430fSAndrzej Pietrasiewicz case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8 55000a2430fSAndrzej Pietrasiewicz | HID_REQ_SET_REPORT): 5516774def6SMasanari Iida VDBG(cdev, "set_report | wLength=%d\n", ctrl->wLength); 55200a2430fSAndrzej Pietrasiewicz goto stall; 55300a2430fSAndrzej Pietrasiewicz break; 55400a2430fSAndrzej Pietrasiewicz 55500a2430fSAndrzej Pietrasiewicz case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8 55600a2430fSAndrzej Pietrasiewicz | HID_REQ_SET_PROTOCOL): 55700a2430fSAndrzej Pietrasiewicz VDBG(cdev, "set_protocol\n"); 558b3c4ec71SAbdulhadi Mohamed if (value > HID_REPORT_PROTOCOL) 559b3c4ec71SAbdulhadi Mohamed goto stall; 560b3c4ec71SAbdulhadi Mohamed length = 0; 561b3c4ec71SAbdulhadi Mohamed /* 562b3c4ec71SAbdulhadi Mohamed * We assume that programs implementing the Boot protocol 563b3c4ec71SAbdulhadi Mohamed * are also compatible with the Report Protocol 564b3c4ec71SAbdulhadi Mohamed */ 565b3c4ec71SAbdulhadi Mohamed if (hidg->bInterfaceSubClass == USB_INTERFACE_SUBCLASS_BOOT) { 566b3c4ec71SAbdulhadi Mohamed hidg->protocol = value; 567b3c4ec71SAbdulhadi Mohamed goto respond; 568b3c4ec71SAbdulhadi Mohamed } 56900a2430fSAndrzej Pietrasiewicz goto stall; 57000a2430fSAndrzej Pietrasiewicz break; 57100a2430fSAndrzej Pietrasiewicz 572*afcff6dcSMaxim Devaev case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8 573*afcff6dcSMaxim Devaev | HID_REQ_SET_IDLE): 574*afcff6dcSMaxim Devaev VDBG(cdev, "set_idle\n"); 575*afcff6dcSMaxim Devaev length = 0; 576*afcff6dcSMaxim Devaev hidg->idle = value; 577*afcff6dcSMaxim Devaev goto respond; 578*afcff6dcSMaxim Devaev break; 579*afcff6dcSMaxim Devaev 58000a2430fSAndrzej Pietrasiewicz case ((USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_INTERFACE) << 8 58100a2430fSAndrzej Pietrasiewicz | USB_REQ_GET_DESCRIPTOR): 58200a2430fSAndrzej Pietrasiewicz switch (value >> 8) { 58300a2430fSAndrzej Pietrasiewicz case HID_DT_HID: 584f286d487SKrzysztof Opasiak { 585f286d487SKrzysztof Opasiak struct hid_descriptor hidg_desc_copy = hidg_desc; 586f286d487SKrzysztof Opasiak 58700a2430fSAndrzej Pietrasiewicz VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: HID\n"); 588f286d487SKrzysztof Opasiak hidg_desc_copy.desc[0].bDescriptorType = HID_DT_REPORT; 589f286d487SKrzysztof Opasiak hidg_desc_copy.desc[0].wDescriptorLength = 590f286d487SKrzysztof Opasiak cpu_to_le16(hidg->report_desc_length); 591f286d487SKrzysztof Opasiak 59200a2430fSAndrzej Pietrasiewicz length = min_t(unsigned short, length, 593f286d487SKrzysztof Opasiak hidg_desc_copy.bLength); 594f286d487SKrzysztof Opasiak memcpy(req->buf, &hidg_desc_copy, length); 59500a2430fSAndrzej Pietrasiewicz goto respond; 59600a2430fSAndrzej Pietrasiewicz break; 597f286d487SKrzysztof Opasiak } 59800a2430fSAndrzej Pietrasiewicz case HID_DT_REPORT: 59900a2430fSAndrzej Pietrasiewicz VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: REPORT\n"); 60000a2430fSAndrzej Pietrasiewicz length = min_t(unsigned short, length, 60100a2430fSAndrzej Pietrasiewicz hidg->report_desc_length); 60200a2430fSAndrzej Pietrasiewicz memcpy(req->buf, hidg->report_desc, length); 60300a2430fSAndrzej Pietrasiewicz goto respond; 60400a2430fSAndrzej Pietrasiewicz break; 60500a2430fSAndrzej Pietrasiewicz 60600a2430fSAndrzej Pietrasiewicz default: 60700a2430fSAndrzej Pietrasiewicz VDBG(cdev, "Unknown descriptor request 0x%x\n", 60800a2430fSAndrzej Pietrasiewicz value >> 8); 60900a2430fSAndrzej Pietrasiewicz goto stall; 61000a2430fSAndrzej Pietrasiewicz break; 61100a2430fSAndrzej Pietrasiewicz } 61200a2430fSAndrzej Pietrasiewicz break; 61300a2430fSAndrzej Pietrasiewicz 61400a2430fSAndrzej Pietrasiewicz default: 61500a2430fSAndrzej Pietrasiewicz VDBG(cdev, "Unknown request 0x%x\n", 61600a2430fSAndrzej Pietrasiewicz ctrl->bRequest); 61700a2430fSAndrzej Pietrasiewicz goto stall; 61800a2430fSAndrzej Pietrasiewicz break; 61900a2430fSAndrzej Pietrasiewicz } 62000a2430fSAndrzej Pietrasiewicz 62100a2430fSAndrzej Pietrasiewicz stall: 62200a2430fSAndrzej Pietrasiewicz return -EOPNOTSUPP; 62300a2430fSAndrzej Pietrasiewicz 62400a2430fSAndrzej Pietrasiewicz respond: 62500a2430fSAndrzej Pietrasiewicz req->zero = 0; 62600a2430fSAndrzej Pietrasiewicz req->length = length; 62700a2430fSAndrzej Pietrasiewicz status = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC); 62800a2430fSAndrzej Pietrasiewicz if (status < 0) 62900a2430fSAndrzej Pietrasiewicz ERROR(cdev, "usb_ep_queue error on ep0 %d\n", value); 63000a2430fSAndrzej Pietrasiewicz return status; 63100a2430fSAndrzej Pietrasiewicz } 63200a2430fSAndrzej Pietrasiewicz 63300a2430fSAndrzej Pietrasiewicz static void hidg_disable(struct usb_function *f) 63400a2430fSAndrzej Pietrasiewicz { 63500a2430fSAndrzej Pietrasiewicz struct f_hidg *hidg = func_to_hidg(f); 63600a2430fSAndrzej Pietrasiewicz struct f_hidg_req_list *list, *next; 637aa65d11aSKrzysztof Opasiak unsigned long flags; 63800a2430fSAndrzej Pietrasiewicz 63900a2430fSAndrzej Pietrasiewicz usb_ep_disable(hidg->in_ep); 64000a2430fSAndrzej Pietrasiewicz usb_ep_disable(hidg->out_ep); 64100a2430fSAndrzej Pietrasiewicz 64233e4c1a9SKrzysztof Opasiak spin_lock_irqsave(&hidg->read_spinlock, flags); 64300a2430fSAndrzej Pietrasiewicz list_for_each_entry_safe(list, next, &hidg->completed_out_req, list) { 644aa65d11aSKrzysztof Opasiak free_ep_req(hidg->out_ep, list->req); 64500a2430fSAndrzej Pietrasiewicz list_del(&list->list); 64600a2430fSAndrzej Pietrasiewicz kfree(list); 64700a2430fSAndrzej Pietrasiewicz } 64833e4c1a9SKrzysztof Opasiak spin_unlock_irqrestore(&hidg->read_spinlock, flags); 649749494b6SKrzysztof Opasiak 650749494b6SKrzysztof Opasiak spin_lock_irqsave(&hidg->write_spinlock, flags); 651749494b6SKrzysztof Opasiak if (!hidg->write_pending) { 652749494b6SKrzysztof Opasiak free_ep_req(hidg->in_ep, hidg->req); 653749494b6SKrzysztof Opasiak hidg->write_pending = 1; 654749494b6SKrzysztof Opasiak } 655749494b6SKrzysztof Opasiak 656749494b6SKrzysztof Opasiak hidg->req = NULL; 657749494b6SKrzysztof Opasiak spin_unlock_irqrestore(&hidg->write_spinlock, flags); 65800a2430fSAndrzej Pietrasiewicz } 65900a2430fSAndrzej Pietrasiewicz 66000a2430fSAndrzej Pietrasiewicz static int hidg_set_alt(struct usb_function *f, unsigned intf, unsigned alt) 66100a2430fSAndrzej Pietrasiewicz { 66200a2430fSAndrzej Pietrasiewicz struct usb_composite_dev *cdev = f->config->cdev; 66300a2430fSAndrzej Pietrasiewicz struct f_hidg *hidg = func_to_hidg(f); 664749494b6SKrzysztof Opasiak struct usb_request *req_in = NULL; 665749494b6SKrzysztof Opasiak unsigned long flags; 66600a2430fSAndrzej Pietrasiewicz int i, status = 0; 66700a2430fSAndrzej Pietrasiewicz 66800a2430fSAndrzej Pietrasiewicz VDBG(cdev, "hidg_set_alt intf:%d alt:%d\n", intf, alt); 66900a2430fSAndrzej Pietrasiewicz 67000a2430fSAndrzej Pietrasiewicz if (hidg->in_ep != NULL) { 67100a2430fSAndrzej Pietrasiewicz /* restart endpoint */ 67200a2430fSAndrzej Pietrasiewicz usb_ep_disable(hidg->in_ep); 67300a2430fSAndrzej Pietrasiewicz 67400a2430fSAndrzej Pietrasiewicz status = config_ep_by_speed(f->config->cdev->gadget, f, 67500a2430fSAndrzej Pietrasiewicz hidg->in_ep); 67600a2430fSAndrzej Pietrasiewicz if (status) { 67700a2430fSAndrzej Pietrasiewicz ERROR(cdev, "config_ep_by_speed FAILED!\n"); 67800a2430fSAndrzej Pietrasiewicz goto fail; 67900a2430fSAndrzej Pietrasiewicz } 68000a2430fSAndrzej Pietrasiewicz status = usb_ep_enable(hidg->in_ep); 68100a2430fSAndrzej Pietrasiewicz if (status < 0) { 68200a2430fSAndrzej Pietrasiewicz ERROR(cdev, "Enable IN endpoint FAILED!\n"); 68300a2430fSAndrzej Pietrasiewicz goto fail; 68400a2430fSAndrzej Pietrasiewicz } 68500a2430fSAndrzej Pietrasiewicz hidg->in_ep->driver_data = hidg; 686749494b6SKrzysztof Opasiak 687749494b6SKrzysztof Opasiak req_in = hidg_alloc_ep_req(hidg->in_ep, hidg->report_length); 688749494b6SKrzysztof Opasiak if (!req_in) { 689749494b6SKrzysztof Opasiak status = -ENOMEM; 690749494b6SKrzysztof Opasiak goto disable_ep_in; 691749494b6SKrzysztof Opasiak } 69200a2430fSAndrzej Pietrasiewicz } 69300a2430fSAndrzej Pietrasiewicz 69400a2430fSAndrzej Pietrasiewicz 69500a2430fSAndrzej Pietrasiewicz if (hidg->out_ep != NULL) { 69600a2430fSAndrzej Pietrasiewicz /* restart endpoint */ 69700a2430fSAndrzej Pietrasiewicz usb_ep_disable(hidg->out_ep); 69800a2430fSAndrzej Pietrasiewicz 69900a2430fSAndrzej Pietrasiewicz status = config_ep_by_speed(f->config->cdev->gadget, f, 70000a2430fSAndrzej Pietrasiewicz hidg->out_ep); 70100a2430fSAndrzej Pietrasiewicz if (status) { 70200a2430fSAndrzej Pietrasiewicz ERROR(cdev, "config_ep_by_speed FAILED!\n"); 703749494b6SKrzysztof Opasiak goto free_req_in; 70400a2430fSAndrzej Pietrasiewicz } 70500a2430fSAndrzej Pietrasiewicz status = usb_ep_enable(hidg->out_ep); 70600a2430fSAndrzej Pietrasiewicz if (status < 0) { 70743aef5c2SDavid Lechner ERROR(cdev, "Enable OUT endpoint FAILED!\n"); 708749494b6SKrzysztof Opasiak goto free_req_in; 70900a2430fSAndrzej Pietrasiewicz } 71000a2430fSAndrzej Pietrasiewicz hidg->out_ep->driver_data = hidg; 71100a2430fSAndrzej Pietrasiewicz 71200a2430fSAndrzej Pietrasiewicz /* 71300a2430fSAndrzej Pietrasiewicz * allocate a bunch of read buffers and queue them all at once. 71400a2430fSAndrzej Pietrasiewicz */ 71500a2430fSAndrzej Pietrasiewicz for (i = 0; i < hidg->qlen && status == 0; i++) { 71600a2430fSAndrzej Pietrasiewicz struct usb_request *req = 71700a2430fSAndrzej Pietrasiewicz hidg_alloc_ep_req(hidg->out_ep, 71800a2430fSAndrzej Pietrasiewicz hidg->report_length); 71900a2430fSAndrzej Pietrasiewicz if (req) { 72000a2430fSAndrzej Pietrasiewicz req->complete = hidg_set_report_complete; 72100a2430fSAndrzej Pietrasiewicz req->context = hidg; 72200a2430fSAndrzej Pietrasiewicz status = usb_ep_queue(hidg->out_ep, req, 72300a2430fSAndrzej Pietrasiewicz GFP_ATOMIC); 724749494b6SKrzysztof Opasiak if (status) { 72500a2430fSAndrzej Pietrasiewicz ERROR(cdev, "%s queue req --> %d\n", 72600a2430fSAndrzej Pietrasiewicz hidg->out_ep->name, status); 727749494b6SKrzysztof Opasiak free_ep_req(hidg->out_ep, req); 728749494b6SKrzysztof Opasiak } 72900a2430fSAndrzej Pietrasiewicz } else { 73000a2430fSAndrzej Pietrasiewicz status = -ENOMEM; 731749494b6SKrzysztof Opasiak goto disable_out_ep; 73200a2430fSAndrzej Pietrasiewicz } 73300a2430fSAndrzej Pietrasiewicz } 73400a2430fSAndrzej Pietrasiewicz } 73500a2430fSAndrzej Pietrasiewicz 736749494b6SKrzysztof Opasiak if (hidg->in_ep != NULL) { 737749494b6SKrzysztof Opasiak spin_lock_irqsave(&hidg->write_spinlock, flags); 738749494b6SKrzysztof Opasiak hidg->req = req_in; 739749494b6SKrzysztof Opasiak hidg->write_pending = 0; 740749494b6SKrzysztof Opasiak spin_unlock_irqrestore(&hidg->write_spinlock, flags); 741749494b6SKrzysztof Opasiak 742749494b6SKrzysztof Opasiak wake_up(&hidg->write_queue); 743749494b6SKrzysztof Opasiak } 744749494b6SKrzysztof Opasiak return 0; 745749494b6SKrzysztof Opasiak disable_out_ep: 746749494b6SKrzysztof Opasiak usb_ep_disable(hidg->out_ep); 747749494b6SKrzysztof Opasiak free_req_in: 748749494b6SKrzysztof Opasiak if (req_in) 749749494b6SKrzysztof Opasiak free_ep_req(hidg->in_ep, req_in); 750749494b6SKrzysztof Opasiak 751749494b6SKrzysztof Opasiak disable_ep_in: 752749494b6SKrzysztof Opasiak if (hidg->in_ep) 753749494b6SKrzysztof Opasiak usb_ep_disable(hidg->in_ep); 754749494b6SKrzysztof Opasiak 75500a2430fSAndrzej Pietrasiewicz fail: 75600a2430fSAndrzej Pietrasiewicz return status; 75700a2430fSAndrzej Pietrasiewicz } 75800a2430fSAndrzej Pietrasiewicz 7597a3cc461SLad, Prabhakar static const struct file_operations f_hidg_fops = { 76000a2430fSAndrzej Pietrasiewicz .owner = THIS_MODULE, 76100a2430fSAndrzej Pietrasiewicz .open = f_hidg_open, 76200a2430fSAndrzej Pietrasiewicz .release = f_hidg_release, 76300a2430fSAndrzej Pietrasiewicz .write = f_hidg_write, 76400a2430fSAndrzej Pietrasiewicz .read = f_hidg_read, 76500a2430fSAndrzej Pietrasiewicz .poll = f_hidg_poll, 76600a2430fSAndrzej Pietrasiewicz .llseek = noop_llseek, 76700a2430fSAndrzej Pietrasiewicz }; 76800a2430fSAndrzej Pietrasiewicz 769cb382536SAndrzej Pietrasiewicz static int hidg_bind(struct usb_configuration *c, struct usb_function *f) 77000a2430fSAndrzej Pietrasiewicz { 77100a2430fSAndrzej Pietrasiewicz struct usb_ep *ep; 77200a2430fSAndrzej Pietrasiewicz struct f_hidg *hidg = func_to_hidg(f); 7735ca8d3ecSAndrzej Pietrasiewicz struct usb_string *us; 77463406087SAndrzej Pietrasiewicz struct device *device; 77500a2430fSAndrzej Pietrasiewicz int status; 77600a2430fSAndrzej Pietrasiewicz dev_t dev; 77700a2430fSAndrzej Pietrasiewicz 778cb382536SAndrzej Pietrasiewicz /* maybe allocate device-global string IDs, and patch descriptors */ 7795ca8d3ecSAndrzej Pietrasiewicz us = usb_gstrings_attach(c->cdev, ct_func_strings, 7805ca8d3ecSAndrzej Pietrasiewicz ARRAY_SIZE(ct_func_string_defs)); 7815ca8d3ecSAndrzej Pietrasiewicz if (IS_ERR(us)) 7825ca8d3ecSAndrzej Pietrasiewicz return PTR_ERR(us); 7835ca8d3ecSAndrzej Pietrasiewicz hidg_interface_desc.iInterface = us[CT_FUNC_HID_IDX].id; 784cb382536SAndrzej Pietrasiewicz 78500a2430fSAndrzej Pietrasiewicz /* allocate instance-specific interface IDs, and patch descriptors */ 78600a2430fSAndrzej Pietrasiewicz status = usb_interface_id(c, f); 78700a2430fSAndrzej Pietrasiewicz if (status < 0) 78800a2430fSAndrzej Pietrasiewicz goto fail; 78900a2430fSAndrzej Pietrasiewicz hidg_interface_desc.bInterfaceNumber = status; 79000a2430fSAndrzej Pietrasiewicz 79100a2430fSAndrzej Pietrasiewicz /* allocate instance-specific endpoints */ 79200a2430fSAndrzej Pietrasiewicz status = -ENODEV; 79300a2430fSAndrzej Pietrasiewicz ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_in_ep_desc); 79400a2430fSAndrzej Pietrasiewicz if (!ep) 79500a2430fSAndrzej Pietrasiewicz goto fail; 79600a2430fSAndrzej Pietrasiewicz hidg->in_ep = ep; 79700a2430fSAndrzej Pietrasiewicz 79800a2430fSAndrzej Pietrasiewicz ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_out_ep_desc); 79900a2430fSAndrzej Pietrasiewicz if (!ep) 80000a2430fSAndrzej Pietrasiewicz goto fail; 80100a2430fSAndrzej Pietrasiewicz hidg->out_ep = ep; 80200a2430fSAndrzej Pietrasiewicz 80300a2430fSAndrzej Pietrasiewicz /* set descriptor dynamic values */ 80400a2430fSAndrzej Pietrasiewicz hidg_interface_desc.bInterfaceSubClass = hidg->bInterfaceSubClass; 80500a2430fSAndrzej Pietrasiewicz hidg_interface_desc.bInterfaceProtocol = hidg->bInterfaceProtocol; 806b3c4ec71SAbdulhadi Mohamed hidg->protocol = HID_REPORT_PROTOCOL; 807*afcff6dcSMaxim Devaev hidg->idle = 1; 808dbf499cfSJanusz Dziedzic hidg_ss_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length); 809dbf499cfSJanusz Dziedzic hidg_ss_in_comp_desc.wBytesPerInterval = 810dbf499cfSJanusz Dziedzic cpu_to_le16(hidg->report_length); 81100a2430fSAndrzej Pietrasiewicz hidg_hs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length); 81200a2430fSAndrzej Pietrasiewicz hidg_fs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length); 813dbf499cfSJanusz Dziedzic hidg_ss_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length); 814dbf499cfSJanusz Dziedzic hidg_ss_out_comp_desc.wBytesPerInterval = 815dbf499cfSJanusz Dziedzic cpu_to_le16(hidg->report_length); 81600a2430fSAndrzej Pietrasiewicz hidg_hs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length); 81700a2430fSAndrzej Pietrasiewicz hidg_fs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length); 818f286d487SKrzysztof Opasiak /* 819f286d487SKrzysztof Opasiak * We can use hidg_desc struct here but we should not relay 820f286d487SKrzysztof Opasiak * that its content won't change after returning from this function. 821f286d487SKrzysztof Opasiak */ 82200a2430fSAndrzej Pietrasiewicz hidg_desc.desc[0].bDescriptorType = HID_DT_REPORT; 82300a2430fSAndrzej Pietrasiewicz hidg_desc.desc[0].wDescriptorLength = 82400a2430fSAndrzej Pietrasiewicz cpu_to_le16(hidg->report_desc_length); 82500a2430fSAndrzej Pietrasiewicz 82600a2430fSAndrzej Pietrasiewicz hidg_hs_in_ep_desc.bEndpointAddress = 82700a2430fSAndrzej Pietrasiewicz hidg_fs_in_ep_desc.bEndpointAddress; 82800a2430fSAndrzej Pietrasiewicz hidg_hs_out_ep_desc.bEndpointAddress = 82900a2430fSAndrzej Pietrasiewicz hidg_fs_out_ep_desc.bEndpointAddress; 83000a2430fSAndrzej Pietrasiewicz 831dbf499cfSJanusz Dziedzic hidg_ss_in_ep_desc.bEndpointAddress = 832dbf499cfSJanusz Dziedzic hidg_fs_in_ep_desc.bEndpointAddress; 833dbf499cfSJanusz Dziedzic hidg_ss_out_ep_desc.bEndpointAddress = 834dbf499cfSJanusz Dziedzic hidg_fs_out_ep_desc.bEndpointAddress; 835dbf499cfSJanusz Dziedzic 83600a2430fSAndrzej Pietrasiewicz status = usb_assign_descriptors(f, hidg_fs_descriptors, 83790c4d057SMaciej Żenczykowski hidg_hs_descriptors, hidg_ss_descriptors, 83890c4d057SMaciej Żenczykowski hidg_ss_descriptors); 83900a2430fSAndrzej Pietrasiewicz if (status) 84000a2430fSAndrzej Pietrasiewicz goto fail; 84100a2430fSAndrzej Pietrasiewicz 84233e4c1a9SKrzysztof Opasiak spin_lock_init(&hidg->write_spinlock); 843749494b6SKrzysztof Opasiak hidg->write_pending = 1; 844749494b6SKrzysztof Opasiak hidg->req = NULL; 84533e4c1a9SKrzysztof Opasiak spin_lock_init(&hidg->read_spinlock); 84600a2430fSAndrzej Pietrasiewicz init_waitqueue_head(&hidg->write_queue); 84700a2430fSAndrzej Pietrasiewicz init_waitqueue_head(&hidg->read_queue); 84800a2430fSAndrzej Pietrasiewicz INIT_LIST_HEAD(&hidg->completed_out_req); 84900a2430fSAndrzej Pietrasiewicz 85000a2430fSAndrzej Pietrasiewicz /* create char device */ 85100a2430fSAndrzej Pietrasiewicz cdev_init(&hidg->cdev, &f_hidg_fops); 85200a2430fSAndrzej Pietrasiewicz dev = MKDEV(major, hidg->minor); 85300a2430fSAndrzej Pietrasiewicz status = cdev_add(&hidg->cdev, dev, 1); 85400a2430fSAndrzej Pietrasiewicz if (status) 855d12a8727SPavitrakumar Managutte goto fail_free_descs; 85600a2430fSAndrzej Pietrasiewicz 85763406087SAndrzej Pietrasiewicz device = device_create(hidg_class, NULL, dev, NULL, 85863406087SAndrzej Pietrasiewicz "%s%d", "hidg", hidg->minor); 85963406087SAndrzej Pietrasiewicz if (IS_ERR(device)) { 86063406087SAndrzej Pietrasiewicz status = PTR_ERR(device); 86163406087SAndrzej Pietrasiewicz goto del; 86263406087SAndrzej Pietrasiewicz } 86300a2430fSAndrzej Pietrasiewicz 86400a2430fSAndrzej Pietrasiewicz return 0; 86563406087SAndrzej Pietrasiewicz del: 86663406087SAndrzej Pietrasiewicz cdev_del(&hidg->cdev); 867d12a8727SPavitrakumar Managutte fail_free_descs: 868d12a8727SPavitrakumar Managutte usb_free_all_descriptors(f); 86900a2430fSAndrzej Pietrasiewicz fail: 87000a2430fSAndrzej Pietrasiewicz ERROR(f->config->cdev, "hidg_bind FAILED\n"); 87114794d71SFelipe F. Tonello if (hidg->req != NULL) 87214794d71SFelipe F. Tonello free_ep_req(hidg->in_ep, hidg->req); 87300a2430fSAndrzej Pietrasiewicz 87400a2430fSAndrzej Pietrasiewicz return status; 87500a2430fSAndrzej Pietrasiewicz } 87600a2430fSAndrzej Pietrasiewicz 877cb382536SAndrzej Pietrasiewicz static inline int hidg_get_minor(void) 878cb382536SAndrzej Pietrasiewicz { 879cb382536SAndrzej Pietrasiewicz int ret; 880cb382536SAndrzej Pietrasiewicz 881cb382536SAndrzej Pietrasiewicz ret = ida_simple_get(&hidg_ida, 0, 0, GFP_KERNEL); 882774cf72fSAndrzej Pietrasiewicz if (ret >= HIDG_MINORS) { 883774cf72fSAndrzej Pietrasiewicz ida_simple_remove(&hidg_ida, ret); 884774cf72fSAndrzej Pietrasiewicz ret = -ENODEV; 885774cf72fSAndrzej Pietrasiewicz } 886cb382536SAndrzej Pietrasiewicz 887cb382536SAndrzej Pietrasiewicz return ret; 888cb382536SAndrzej Pietrasiewicz } 889cb382536SAndrzej Pietrasiewicz 89021a9476aSAndrzej Pietrasiewicz static inline struct f_hid_opts *to_f_hid_opts(struct config_item *item) 89121a9476aSAndrzej Pietrasiewicz { 89221a9476aSAndrzej Pietrasiewicz return container_of(to_config_group(item), struct f_hid_opts, 89321a9476aSAndrzej Pietrasiewicz func_inst.group); 89421a9476aSAndrzej Pietrasiewicz } 89521a9476aSAndrzej Pietrasiewicz 89621a9476aSAndrzej Pietrasiewicz static void hid_attr_release(struct config_item *item) 89721a9476aSAndrzej Pietrasiewicz { 89821a9476aSAndrzej Pietrasiewicz struct f_hid_opts *opts = to_f_hid_opts(item); 89921a9476aSAndrzej Pietrasiewicz 90021a9476aSAndrzej Pietrasiewicz usb_put_function_instance(&opts->func_inst); 90121a9476aSAndrzej Pietrasiewicz } 90221a9476aSAndrzej Pietrasiewicz 90321a9476aSAndrzej Pietrasiewicz static struct configfs_item_operations hidg_item_ops = { 90421a9476aSAndrzej Pietrasiewicz .release = hid_attr_release, 90521a9476aSAndrzej Pietrasiewicz }; 90621a9476aSAndrzej Pietrasiewicz 90721a9476aSAndrzej Pietrasiewicz #define F_HID_OPT(name, prec, limit) \ 908da4e527cSChristoph Hellwig static ssize_t f_hid_opts_##name##_show(struct config_item *item, char *page)\ 90921a9476aSAndrzej Pietrasiewicz { \ 910da4e527cSChristoph Hellwig struct f_hid_opts *opts = to_f_hid_opts(item); \ 91121a9476aSAndrzej Pietrasiewicz int result; \ 91221a9476aSAndrzej Pietrasiewicz \ 91321a9476aSAndrzej Pietrasiewicz mutex_lock(&opts->lock); \ 91421a9476aSAndrzej Pietrasiewicz result = sprintf(page, "%d\n", opts->name); \ 91521a9476aSAndrzej Pietrasiewicz mutex_unlock(&opts->lock); \ 91621a9476aSAndrzej Pietrasiewicz \ 91721a9476aSAndrzej Pietrasiewicz return result; \ 91821a9476aSAndrzej Pietrasiewicz } \ 91921a9476aSAndrzej Pietrasiewicz \ 920da4e527cSChristoph Hellwig static ssize_t f_hid_opts_##name##_store(struct config_item *item, \ 92121a9476aSAndrzej Pietrasiewicz const char *page, size_t len) \ 92221a9476aSAndrzej Pietrasiewicz { \ 923da4e527cSChristoph Hellwig struct f_hid_opts *opts = to_f_hid_opts(item); \ 92421a9476aSAndrzej Pietrasiewicz int ret; \ 92521a9476aSAndrzej Pietrasiewicz u##prec num; \ 92621a9476aSAndrzej Pietrasiewicz \ 92721a9476aSAndrzej Pietrasiewicz mutex_lock(&opts->lock); \ 92821a9476aSAndrzej Pietrasiewicz if (opts->refcnt) { \ 92921a9476aSAndrzej Pietrasiewicz ret = -EBUSY; \ 93021a9476aSAndrzej Pietrasiewicz goto end; \ 93121a9476aSAndrzej Pietrasiewicz } \ 93221a9476aSAndrzej Pietrasiewicz \ 93321a9476aSAndrzej Pietrasiewicz ret = kstrtou##prec(page, 0, &num); \ 93421a9476aSAndrzej Pietrasiewicz if (ret) \ 93521a9476aSAndrzej Pietrasiewicz goto end; \ 93621a9476aSAndrzej Pietrasiewicz \ 93721a9476aSAndrzej Pietrasiewicz if (num > limit) { \ 93821a9476aSAndrzej Pietrasiewicz ret = -EINVAL; \ 93921a9476aSAndrzej Pietrasiewicz goto end; \ 94021a9476aSAndrzej Pietrasiewicz } \ 94121a9476aSAndrzej Pietrasiewicz opts->name = num; \ 94221a9476aSAndrzej Pietrasiewicz ret = len; \ 94321a9476aSAndrzej Pietrasiewicz \ 94421a9476aSAndrzej Pietrasiewicz end: \ 94521a9476aSAndrzej Pietrasiewicz mutex_unlock(&opts->lock); \ 94621a9476aSAndrzej Pietrasiewicz return ret; \ 94721a9476aSAndrzej Pietrasiewicz } \ 94821a9476aSAndrzej Pietrasiewicz \ 949da4e527cSChristoph Hellwig CONFIGFS_ATTR(f_hid_opts_, name) 95021a9476aSAndrzej Pietrasiewicz 95121a9476aSAndrzej Pietrasiewicz F_HID_OPT(subclass, 8, 255); 95221a9476aSAndrzej Pietrasiewicz F_HID_OPT(protocol, 8, 255); 95339a2ac27SAndrzej Pietrasiewicz F_HID_OPT(report_length, 16, 65535); 95421a9476aSAndrzej Pietrasiewicz 955da4e527cSChristoph Hellwig static ssize_t f_hid_opts_report_desc_show(struct config_item *item, char *page) 95621a9476aSAndrzej Pietrasiewicz { 957da4e527cSChristoph Hellwig struct f_hid_opts *opts = to_f_hid_opts(item); 95821a9476aSAndrzej Pietrasiewicz int result; 95921a9476aSAndrzej Pietrasiewicz 96021a9476aSAndrzej Pietrasiewicz mutex_lock(&opts->lock); 96121a9476aSAndrzej Pietrasiewicz result = opts->report_desc_length; 96221a9476aSAndrzej Pietrasiewicz memcpy(page, opts->report_desc, opts->report_desc_length); 96321a9476aSAndrzej Pietrasiewicz mutex_unlock(&opts->lock); 96421a9476aSAndrzej Pietrasiewicz 96521a9476aSAndrzej Pietrasiewicz return result; 96621a9476aSAndrzej Pietrasiewicz } 96721a9476aSAndrzej Pietrasiewicz 968da4e527cSChristoph Hellwig static ssize_t f_hid_opts_report_desc_store(struct config_item *item, 96921a9476aSAndrzej Pietrasiewicz const char *page, size_t len) 97021a9476aSAndrzej Pietrasiewicz { 971da4e527cSChristoph Hellwig struct f_hid_opts *opts = to_f_hid_opts(item); 97221a9476aSAndrzej Pietrasiewicz int ret = -EBUSY; 97321a9476aSAndrzej Pietrasiewicz char *d; 97421a9476aSAndrzej Pietrasiewicz 97521a9476aSAndrzej Pietrasiewicz mutex_lock(&opts->lock); 97621a9476aSAndrzej Pietrasiewicz 97721a9476aSAndrzej Pietrasiewicz if (opts->refcnt) 97821a9476aSAndrzej Pietrasiewicz goto end; 97921a9476aSAndrzej Pietrasiewicz if (len > PAGE_SIZE) { 98021a9476aSAndrzej Pietrasiewicz ret = -ENOSPC; 98121a9476aSAndrzej Pietrasiewicz goto end; 98221a9476aSAndrzej Pietrasiewicz } 98321a9476aSAndrzej Pietrasiewicz d = kmemdup(page, len, GFP_KERNEL); 98421a9476aSAndrzej Pietrasiewicz if (!d) { 98521a9476aSAndrzej Pietrasiewicz ret = -ENOMEM; 98621a9476aSAndrzej Pietrasiewicz goto end; 98721a9476aSAndrzej Pietrasiewicz } 98821a9476aSAndrzej Pietrasiewicz kfree(opts->report_desc); 98921a9476aSAndrzej Pietrasiewicz opts->report_desc = d; 99021a9476aSAndrzej Pietrasiewicz opts->report_desc_length = len; 99121a9476aSAndrzej Pietrasiewicz opts->report_desc_alloc = true; 99221a9476aSAndrzej Pietrasiewicz ret = len; 99321a9476aSAndrzej Pietrasiewicz end: 99421a9476aSAndrzej Pietrasiewicz mutex_unlock(&opts->lock); 99521a9476aSAndrzej Pietrasiewicz return ret; 99621a9476aSAndrzej Pietrasiewicz } 99721a9476aSAndrzej Pietrasiewicz 998da4e527cSChristoph Hellwig CONFIGFS_ATTR(f_hid_opts_, report_desc); 99921a9476aSAndrzej Pietrasiewicz 1000ed6fe1f5SJohannes Berg static ssize_t f_hid_opts_dev_show(struct config_item *item, char *page) 1001ed6fe1f5SJohannes Berg { 1002ed6fe1f5SJohannes Berg struct f_hid_opts *opts = to_f_hid_opts(item); 1003ed6fe1f5SJohannes Berg 1004ed6fe1f5SJohannes Berg return sprintf(page, "%d:%d\n", major, opts->minor); 1005ed6fe1f5SJohannes Berg } 1006ed6fe1f5SJohannes Berg 1007ed6fe1f5SJohannes Berg CONFIGFS_ATTR_RO(f_hid_opts_, dev); 1008ed6fe1f5SJohannes Berg 100921a9476aSAndrzej Pietrasiewicz static struct configfs_attribute *hid_attrs[] = { 1010da4e527cSChristoph Hellwig &f_hid_opts_attr_subclass, 1011da4e527cSChristoph Hellwig &f_hid_opts_attr_protocol, 1012da4e527cSChristoph Hellwig &f_hid_opts_attr_report_length, 1013da4e527cSChristoph Hellwig &f_hid_opts_attr_report_desc, 1014ed6fe1f5SJohannes Berg &f_hid_opts_attr_dev, 101521a9476aSAndrzej Pietrasiewicz NULL, 101621a9476aSAndrzej Pietrasiewicz }; 101721a9476aSAndrzej Pietrasiewicz 101897363902SBhumika Goyal static const struct config_item_type hid_func_type = { 101921a9476aSAndrzej Pietrasiewicz .ct_item_ops = &hidg_item_ops, 102021a9476aSAndrzej Pietrasiewicz .ct_attrs = hid_attrs, 102121a9476aSAndrzej Pietrasiewicz .ct_owner = THIS_MODULE, 102221a9476aSAndrzej Pietrasiewicz }; 102321a9476aSAndrzej Pietrasiewicz 1024cb382536SAndrzej Pietrasiewicz static inline void hidg_put_minor(int minor) 1025cb382536SAndrzej Pietrasiewicz { 1026cb382536SAndrzej Pietrasiewicz ida_simple_remove(&hidg_ida, minor); 1027cb382536SAndrzej Pietrasiewicz } 1028cb382536SAndrzej Pietrasiewicz 1029cb382536SAndrzej Pietrasiewicz static void hidg_free_inst(struct usb_function_instance *f) 1030cb382536SAndrzej Pietrasiewicz { 1031cb382536SAndrzej Pietrasiewicz struct f_hid_opts *opts; 1032cb382536SAndrzej Pietrasiewicz 1033cb382536SAndrzej Pietrasiewicz opts = container_of(f, struct f_hid_opts, func_inst); 1034cb382536SAndrzej Pietrasiewicz 1035cb382536SAndrzej Pietrasiewicz mutex_lock(&hidg_ida_lock); 1036cb382536SAndrzej Pietrasiewicz 1037cb382536SAndrzej Pietrasiewicz hidg_put_minor(opts->minor); 103899c49407SMatthew Wilcox if (ida_is_empty(&hidg_ida)) 1039cb382536SAndrzej Pietrasiewicz ghid_cleanup(); 1040cb382536SAndrzej Pietrasiewicz 1041cb382536SAndrzej Pietrasiewicz mutex_unlock(&hidg_ida_lock); 1042cb382536SAndrzej Pietrasiewicz 1043cb382536SAndrzej Pietrasiewicz if (opts->report_desc_alloc) 1044cb382536SAndrzej Pietrasiewicz kfree(opts->report_desc); 1045cb382536SAndrzej Pietrasiewicz 1046cb382536SAndrzej Pietrasiewicz kfree(opts); 1047cb382536SAndrzej Pietrasiewicz } 1048cb382536SAndrzej Pietrasiewicz 1049cb382536SAndrzej Pietrasiewicz static struct usb_function_instance *hidg_alloc_inst(void) 1050cb382536SAndrzej Pietrasiewicz { 1051cb382536SAndrzej Pietrasiewicz struct f_hid_opts *opts; 1052cb382536SAndrzej Pietrasiewicz struct usb_function_instance *ret; 1053cb382536SAndrzej Pietrasiewicz int status = 0; 1054cb382536SAndrzej Pietrasiewicz 1055cb382536SAndrzej Pietrasiewicz opts = kzalloc(sizeof(*opts), GFP_KERNEL); 1056cb382536SAndrzej Pietrasiewicz if (!opts) 1057cb382536SAndrzej Pietrasiewicz return ERR_PTR(-ENOMEM); 105821a9476aSAndrzej Pietrasiewicz mutex_init(&opts->lock); 1059cb382536SAndrzej Pietrasiewicz opts->func_inst.free_func_inst = hidg_free_inst; 1060cb382536SAndrzej Pietrasiewicz ret = &opts->func_inst; 1061cb382536SAndrzej Pietrasiewicz 1062cb382536SAndrzej Pietrasiewicz mutex_lock(&hidg_ida_lock); 1063cb382536SAndrzej Pietrasiewicz 106499c49407SMatthew Wilcox if (ida_is_empty(&hidg_ida)) { 1065cb382536SAndrzej Pietrasiewicz status = ghid_setup(NULL, HIDG_MINORS); 1066cb382536SAndrzej Pietrasiewicz if (status) { 1067cb382536SAndrzej Pietrasiewicz ret = ERR_PTR(status); 1068cb382536SAndrzej Pietrasiewicz kfree(opts); 1069cb382536SAndrzej Pietrasiewicz goto unlock; 1070cb382536SAndrzej Pietrasiewicz } 1071cb382536SAndrzej Pietrasiewicz } 1072cb382536SAndrzej Pietrasiewicz 1073cb382536SAndrzej Pietrasiewicz opts->minor = hidg_get_minor(); 1074cb382536SAndrzej Pietrasiewicz if (opts->minor < 0) { 1075cb382536SAndrzej Pietrasiewicz ret = ERR_PTR(opts->minor); 1076cb382536SAndrzej Pietrasiewicz kfree(opts); 107799c49407SMatthew Wilcox if (ida_is_empty(&hidg_ida)) 1078cb382536SAndrzej Pietrasiewicz ghid_cleanup(); 1079828f6148SDan Carpenter goto unlock; 1080cb382536SAndrzej Pietrasiewicz } 108121a9476aSAndrzej Pietrasiewicz config_group_init_type_name(&opts->func_inst.group, "", &hid_func_type); 1082cb382536SAndrzej Pietrasiewicz 1083cb382536SAndrzej Pietrasiewicz unlock: 1084cb382536SAndrzej Pietrasiewicz mutex_unlock(&hidg_ida_lock); 1085cb382536SAndrzej Pietrasiewicz return ret; 1086cb382536SAndrzej Pietrasiewicz } 1087cb382536SAndrzej Pietrasiewicz 1088cb382536SAndrzej Pietrasiewicz static void hidg_free(struct usb_function *f) 1089cb382536SAndrzej Pietrasiewicz { 1090cb382536SAndrzej Pietrasiewicz struct f_hidg *hidg; 109121a9476aSAndrzej Pietrasiewicz struct f_hid_opts *opts; 1092cb382536SAndrzej Pietrasiewicz 1093cb382536SAndrzej Pietrasiewicz hidg = func_to_hidg(f); 109421a9476aSAndrzej Pietrasiewicz opts = container_of(f->fi, struct f_hid_opts, func_inst); 1095cb382536SAndrzej Pietrasiewicz kfree(hidg->report_desc); 1096cb382536SAndrzej Pietrasiewicz kfree(hidg); 109721a9476aSAndrzej Pietrasiewicz mutex_lock(&opts->lock); 109821a9476aSAndrzej Pietrasiewicz --opts->refcnt; 109921a9476aSAndrzej Pietrasiewicz mutex_unlock(&opts->lock); 1100cb382536SAndrzej Pietrasiewicz } 1101cb382536SAndrzej Pietrasiewicz 110200a2430fSAndrzej Pietrasiewicz static void hidg_unbind(struct usb_configuration *c, struct usb_function *f) 110300a2430fSAndrzej Pietrasiewicz { 110400a2430fSAndrzej Pietrasiewicz struct f_hidg *hidg = func_to_hidg(f); 110500a2430fSAndrzej Pietrasiewicz 110600a2430fSAndrzej Pietrasiewicz device_destroy(hidg_class, MKDEV(major, hidg->minor)); 110700a2430fSAndrzej Pietrasiewicz cdev_del(&hidg->cdev); 110800a2430fSAndrzej Pietrasiewicz 110900a2430fSAndrzej Pietrasiewicz usb_free_all_descriptors(f); 111000a2430fSAndrzej Pietrasiewicz } 111100a2430fSAndrzej Pietrasiewicz 11120fc57ea0SFengguang Wu static struct usb_function *hidg_alloc(struct usb_function_instance *fi) 111300a2430fSAndrzej Pietrasiewicz { 111400a2430fSAndrzej Pietrasiewicz struct f_hidg *hidg; 1115cb382536SAndrzej Pietrasiewicz struct f_hid_opts *opts; 111600a2430fSAndrzej Pietrasiewicz 111700a2430fSAndrzej Pietrasiewicz /* allocate and initialize one new instance */ 1118cb382536SAndrzej Pietrasiewicz hidg = kzalloc(sizeof(*hidg), GFP_KERNEL); 111900a2430fSAndrzej Pietrasiewicz if (!hidg) 1120cb382536SAndrzej Pietrasiewicz return ERR_PTR(-ENOMEM); 112100a2430fSAndrzej Pietrasiewicz 1122cb382536SAndrzej Pietrasiewicz opts = container_of(fi, struct f_hid_opts, func_inst); 1123cb382536SAndrzej Pietrasiewicz 112421a9476aSAndrzej Pietrasiewicz mutex_lock(&opts->lock); 112521a9476aSAndrzej Pietrasiewicz ++opts->refcnt; 112621a9476aSAndrzej Pietrasiewicz 1127cb382536SAndrzej Pietrasiewicz hidg->minor = opts->minor; 1128cb382536SAndrzej Pietrasiewicz hidg->bInterfaceSubClass = opts->subclass; 1129cb382536SAndrzej Pietrasiewicz hidg->bInterfaceProtocol = opts->protocol; 1130cb382536SAndrzej Pietrasiewicz hidg->report_length = opts->report_length; 1131cb382536SAndrzej Pietrasiewicz hidg->report_desc_length = opts->report_desc_length; 1132cb382536SAndrzej Pietrasiewicz if (opts->report_desc) { 1133cb382536SAndrzej Pietrasiewicz hidg->report_desc = kmemdup(opts->report_desc, 1134cb382536SAndrzej Pietrasiewicz opts->report_desc_length, 113500a2430fSAndrzej Pietrasiewicz GFP_KERNEL); 113600a2430fSAndrzej Pietrasiewicz if (!hidg->report_desc) { 113700a2430fSAndrzej Pietrasiewicz kfree(hidg); 113821a9476aSAndrzej Pietrasiewicz mutex_unlock(&opts->lock); 1139cb382536SAndrzej Pietrasiewicz return ERR_PTR(-ENOMEM); 1140cb382536SAndrzej Pietrasiewicz } 114100a2430fSAndrzej Pietrasiewicz } 114200a2430fSAndrzej Pietrasiewicz 114321a9476aSAndrzej Pietrasiewicz mutex_unlock(&opts->lock); 114421a9476aSAndrzej Pietrasiewicz 114500a2430fSAndrzej Pietrasiewicz hidg->func.name = "hid"; 114600a2430fSAndrzej Pietrasiewicz hidg->func.bind = hidg_bind; 114700a2430fSAndrzej Pietrasiewicz hidg->func.unbind = hidg_unbind; 114800a2430fSAndrzej Pietrasiewicz hidg->func.set_alt = hidg_set_alt; 114900a2430fSAndrzej Pietrasiewicz hidg->func.disable = hidg_disable; 115000a2430fSAndrzej Pietrasiewicz hidg->func.setup = hidg_setup; 1151cb382536SAndrzej Pietrasiewicz hidg->func.free_func = hidg_free; 115200a2430fSAndrzej Pietrasiewicz 115329a812e4SWei Ming Chen /* this could be made configurable at some point */ 115400a2430fSAndrzej Pietrasiewicz hidg->qlen = 4; 115500a2430fSAndrzej Pietrasiewicz 1156cb382536SAndrzej Pietrasiewicz return &hidg->func; 115700a2430fSAndrzej Pietrasiewicz } 115800a2430fSAndrzej Pietrasiewicz 1159cb382536SAndrzej Pietrasiewicz DECLARE_USB_FUNCTION_INIT(hid, hidg_alloc_inst, hidg_alloc); 1160cb382536SAndrzej Pietrasiewicz MODULE_LICENSE("GPL"); 1161cb382536SAndrzej Pietrasiewicz MODULE_AUTHOR("Fabien Chouteau"); 1162cb382536SAndrzej Pietrasiewicz 1163cb382536SAndrzej Pietrasiewicz int ghid_setup(struct usb_gadget *g, int count) 116400a2430fSAndrzej Pietrasiewicz { 116500a2430fSAndrzej Pietrasiewicz int status; 116600a2430fSAndrzej Pietrasiewicz dev_t dev; 116700a2430fSAndrzej Pietrasiewicz 116800a2430fSAndrzej Pietrasiewicz hidg_class = class_create(THIS_MODULE, "hidg"); 116906529407SAndrzej Pietrasiewicz if (IS_ERR(hidg_class)) { 11700448d38cSDan Carpenter status = PTR_ERR(hidg_class); 117106529407SAndrzej Pietrasiewicz hidg_class = NULL; 11720448d38cSDan Carpenter return status; 117300a2430fSAndrzej Pietrasiewicz } 117400a2430fSAndrzej Pietrasiewicz 117500a2430fSAndrzej Pietrasiewicz status = alloc_chrdev_region(&dev, 0, count, "hidg"); 11760448d38cSDan Carpenter if (status) { 11770448d38cSDan Carpenter class_destroy(hidg_class); 11780448d38cSDan Carpenter hidg_class = NULL; 117900a2430fSAndrzej Pietrasiewicz return status; 118000a2430fSAndrzej Pietrasiewicz } 118100a2430fSAndrzej Pietrasiewicz 11820448d38cSDan Carpenter major = MAJOR(dev); 11830448d38cSDan Carpenter minors = count; 11840448d38cSDan Carpenter 11850448d38cSDan Carpenter return 0; 118600a2430fSAndrzej Pietrasiewicz } 118700a2430fSAndrzej Pietrasiewicz 118800a2430fSAndrzej Pietrasiewicz void ghid_cleanup(void) 118900a2430fSAndrzej Pietrasiewicz { 119000a2430fSAndrzej Pietrasiewicz if (major) { 119100a2430fSAndrzej Pietrasiewicz unregister_chrdev_region(MKDEV(major, 0), minors); 119200a2430fSAndrzej Pietrasiewicz major = minors = 0; 119300a2430fSAndrzej Pietrasiewicz } 119400a2430fSAndrzej Pietrasiewicz 119500a2430fSAndrzej Pietrasiewicz class_destroy(hidg_class); 119600a2430fSAndrzej Pietrasiewicz hidg_class = NULL; 119700a2430fSAndrzej Pietrasiewicz } 1198