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; 4400a2430fSAndrzej Pietrasiewicz unsigned short report_desc_length; 4500a2430fSAndrzej Pietrasiewicz char *report_desc; 4600a2430fSAndrzej Pietrasiewicz unsigned short report_length; 4700a2430fSAndrzej Pietrasiewicz 4800a2430fSAndrzej Pietrasiewicz /* recv report */ 4900a2430fSAndrzej Pietrasiewicz struct list_head completed_out_req; 5033e4c1a9SKrzysztof Opasiak spinlock_t read_spinlock; 5100a2430fSAndrzej Pietrasiewicz wait_queue_head_t read_queue; 5200a2430fSAndrzej Pietrasiewicz unsigned int qlen; 5300a2430fSAndrzej Pietrasiewicz 5400a2430fSAndrzej Pietrasiewicz /* send report */ 5533e4c1a9SKrzysztof Opasiak spinlock_t write_spinlock; 5600a2430fSAndrzej Pietrasiewicz bool write_pending; 5700a2430fSAndrzej Pietrasiewicz wait_queue_head_t write_queue; 5800a2430fSAndrzej Pietrasiewicz struct usb_request *req; 5900a2430fSAndrzej Pietrasiewicz 6000a2430fSAndrzej Pietrasiewicz int minor; 6100a2430fSAndrzej Pietrasiewicz struct cdev cdev; 6200a2430fSAndrzej Pietrasiewicz struct usb_function func; 6300a2430fSAndrzej Pietrasiewicz 6400a2430fSAndrzej Pietrasiewicz struct usb_ep *in_ep; 6500a2430fSAndrzej Pietrasiewicz struct usb_ep *out_ep; 6600a2430fSAndrzej Pietrasiewicz }; 6700a2430fSAndrzej Pietrasiewicz 6800a2430fSAndrzej Pietrasiewicz static inline struct f_hidg *func_to_hidg(struct usb_function *f) 6900a2430fSAndrzej Pietrasiewicz { 7000a2430fSAndrzej Pietrasiewicz return container_of(f, struct f_hidg, func); 7100a2430fSAndrzej Pietrasiewicz } 7200a2430fSAndrzej Pietrasiewicz 7300a2430fSAndrzej Pietrasiewicz /*-------------------------------------------------------------------------*/ 7400a2430fSAndrzej Pietrasiewicz /* Static descriptors */ 7500a2430fSAndrzej Pietrasiewicz 7600a2430fSAndrzej Pietrasiewicz static struct usb_interface_descriptor hidg_interface_desc = { 7700a2430fSAndrzej Pietrasiewicz .bLength = sizeof hidg_interface_desc, 7800a2430fSAndrzej Pietrasiewicz .bDescriptorType = USB_DT_INTERFACE, 7900a2430fSAndrzej Pietrasiewicz /* .bInterfaceNumber = DYNAMIC */ 8000a2430fSAndrzej Pietrasiewicz .bAlternateSetting = 0, 8100a2430fSAndrzej Pietrasiewicz .bNumEndpoints = 2, 8200a2430fSAndrzej Pietrasiewicz .bInterfaceClass = USB_CLASS_HID, 8300a2430fSAndrzej Pietrasiewicz /* .bInterfaceSubClass = DYNAMIC */ 8400a2430fSAndrzej Pietrasiewicz /* .bInterfaceProtocol = DYNAMIC */ 8500a2430fSAndrzej Pietrasiewicz /* .iInterface = DYNAMIC */ 8600a2430fSAndrzej Pietrasiewicz }; 8700a2430fSAndrzej Pietrasiewicz 8800a2430fSAndrzej Pietrasiewicz static struct hid_descriptor hidg_desc = { 8900a2430fSAndrzej Pietrasiewicz .bLength = sizeof hidg_desc, 9000a2430fSAndrzej Pietrasiewicz .bDescriptorType = HID_DT_HID, 91*33cb46c4SRuslan Bilovol .bcdHID = cpu_to_le16(0x0101), 9200a2430fSAndrzej Pietrasiewicz .bCountryCode = 0x00, 9300a2430fSAndrzej Pietrasiewicz .bNumDescriptors = 0x1, 9400a2430fSAndrzej Pietrasiewicz /*.desc[0].bDescriptorType = DYNAMIC */ 9500a2430fSAndrzej Pietrasiewicz /*.desc[0].wDescriptorLenght = DYNAMIC */ 9600a2430fSAndrzej Pietrasiewicz }; 9700a2430fSAndrzej Pietrasiewicz 98dbf499cfSJanusz Dziedzic /* Super-Speed Support */ 99dbf499cfSJanusz Dziedzic 100dbf499cfSJanusz Dziedzic static struct usb_endpoint_descriptor hidg_ss_in_ep_desc = { 101dbf499cfSJanusz Dziedzic .bLength = USB_DT_ENDPOINT_SIZE, 102dbf499cfSJanusz Dziedzic .bDescriptorType = USB_DT_ENDPOINT, 103dbf499cfSJanusz Dziedzic .bEndpointAddress = USB_DIR_IN, 104dbf499cfSJanusz Dziedzic .bmAttributes = USB_ENDPOINT_XFER_INT, 105dbf499cfSJanusz Dziedzic /*.wMaxPacketSize = DYNAMIC */ 106dbf499cfSJanusz Dziedzic .bInterval = 4, /* FIXME: Add this field in the 107dbf499cfSJanusz Dziedzic * HID gadget configuration? 108dbf499cfSJanusz Dziedzic * (struct hidg_func_descriptor) 109dbf499cfSJanusz Dziedzic */ 110dbf499cfSJanusz Dziedzic }; 111dbf499cfSJanusz Dziedzic 112dbf499cfSJanusz Dziedzic static struct usb_ss_ep_comp_descriptor hidg_ss_in_comp_desc = { 113dbf499cfSJanusz Dziedzic .bLength = sizeof(hidg_ss_in_comp_desc), 114dbf499cfSJanusz Dziedzic .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, 115dbf499cfSJanusz Dziedzic 116dbf499cfSJanusz Dziedzic /* .bMaxBurst = 0, */ 117dbf499cfSJanusz Dziedzic /* .bmAttributes = 0, */ 118dbf499cfSJanusz Dziedzic /* .wBytesPerInterval = DYNAMIC */ 119dbf499cfSJanusz Dziedzic }; 120dbf499cfSJanusz Dziedzic 121dbf499cfSJanusz Dziedzic static struct usb_endpoint_descriptor hidg_ss_out_ep_desc = { 122dbf499cfSJanusz Dziedzic .bLength = USB_DT_ENDPOINT_SIZE, 123dbf499cfSJanusz Dziedzic .bDescriptorType = USB_DT_ENDPOINT, 124dbf499cfSJanusz Dziedzic .bEndpointAddress = USB_DIR_OUT, 125dbf499cfSJanusz Dziedzic .bmAttributes = USB_ENDPOINT_XFER_INT, 126dbf499cfSJanusz Dziedzic /*.wMaxPacketSize = DYNAMIC */ 127dbf499cfSJanusz Dziedzic .bInterval = 4, /* FIXME: Add this field in the 128dbf499cfSJanusz Dziedzic * HID gadget configuration? 129dbf499cfSJanusz Dziedzic * (struct hidg_func_descriptor) 130dbf499cfSJanusz Dziedzic */ 131dbf499cfSJanusz Dziedzic }; 132dbf499cfSJanusz Dziedzic 133dbf499cfSJanusz Dziedzic static struct usb_ss_ep_comp_descriptor hidg_ss_out_comp_desc = { 134dbf499cfSJanusz Dziedzic .bLength = sizeof(hidg_ss_out_comp_desc), 135dbf499cfSJanusz Dziedzic .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, 136dbf499cfSJanusz Dziedzic 137dbf499cfSJanusz Dziedzic /* .bMaxBurst = 0, */ 138dbf499cfSJanusz Dziedzic /* .bmAttributes = 0, */ 139dbf499cfSJanusz Dziedzic /* .wBytesPerInterval = DYNAMIC */ 140dbf499cfSJanusz Dziedzic }; 141dbf499cfSJanusz Dziedzic 142dbf499cfSJanusz Dziedzic static struct usb_descriptor_header *hidg_ss_descriptors[] = { 143dbf499cfSJanusz Dziedzic (struct usb_descriptor_header *)&hidg_interface_desc, 144dbf499cfSJanusz Dziedzic (struct usb_descriptor_header *)&hidg_desc, 145dbf499cfSJanusz Dziedzic (struct usb_descriptor_header *)&hidg_ss_in_ep_desc, 146dbf499cfSJanusz Dziedzic (struct usb_descriptor_header *)&hidg_ss_in_comp_desc, 147dbf499cfSJanusz Dziedzic (struct usb_descriptor_header *)&hidg_ss_out_ep_desc, 148dbf499cfSJanusz Dziedzic (struct usb_descriptor_header *)&hidg_ss_out_comp_desc, 149dbf499cfSJanusz Dziedzic NULL, 150dbf499cfSJanusz Dziedzic }; 151dbf499cfSJanusz Dziedzic 15200a2430fSAndrzej Pietrasiewicz /* High-Speed Support */ 15300a2430fSAndrzej Pietrasiewicz 15400a2430fSAndrzej Pietrasiewicz static struct usb_endpoint_descriptor hidg_hs_in_ep_desc = { 15500a2430fSAndrzej Pietrasiewicz .bLength = USB_DT_ENDPOINT_SIZE, 15600a2430fSAndrzej Pietrasiewicz .bDescriptorType = USB_DT_ENDPOINT, 15700a2430fSAndrzej Pietrasiewicz .bEndpointAddress = USB_DIR_IN, 15800a2430fSAndrzej Pietrasiewicz .bmAttributes = USB_ENDPOINT_XFER_INT, 15900a2430fSAndrzej Pietrasiewicz /*.wMaxPacketSize = DYNAMIC */ 16000a2430fSAndrzej Pietrasiewicz .bInterval = 4, /* FIXME: Add this field in the 16100a2430fSAndrzej Pietrasiewicz * HID gadget configuration? 16200a2430fSAndrzej Pietrasiewicz * (struct hidg_func_descriptor) 16300a2430fSAndrzej Pietrasiewicz */ 16400a2430fSAndrzej Pietrasiewicz }; 16500a2430fSAndrzej Pietrasiewicz 16600a2430fSAndrzej Pietrasiewicz static struct usb_endpoint_descriptor hidg_hs_out_ep_desc = { 16700a2430fSAndrzej Pietrasiewicz .bLength = USB_DT_ENDPOINT_SIZE, 16800a2430fSAndrzej Pietrasiewicz .bDescriptorType = USB_DT_ENDPOINT, 16900a2430fSAndrzej Pietrasiewicz .bEndpointAddress = USB_DIR_OUT, 17000a2430fSAndrzej Pietrasiewicz .bmAttributes = USB_ENDPOINT_XFER_INT, 17100a2430fSAndrzej Pietrasiewicz /*.wMaxPacketSize = DYNAMIC */ 17200a2430fSAndrzej Pietrasiewicz .bInterval = 4, /* FIXME: Add this field in the 17300a2430fSAndrzej Pietrasiewicz * HID gadget configuration? 17400a2430fSAndrzej Pietrasiewicz * (struct hidg_func_descriptor) 17500a2430fSAndrzej Pietrasiewicz */ 17600a2430fSAndrzej Pietrasiewicz }; 17700a2430fSAndrzej Pietrasiewicz 17800a2430fSAndrzej Pietrasiewicz static struct usb_descriptor_header *hidg_hs_descriptors[] = { 17900a2430fSAndrzej Pietrasiewicz (struct usb_descriptor_header *)&hidg_interface_desc, 18000a2430fSAndrzej Pietrasiewicz (struct usb_descriptor_header *)&hidg_desc, 18100a2430fSAndrzej Pietrasiewicz (struct usb_descriptor_header *)&hidg_hs_in_ep_desc, 18200a2430fSAndrzej Pietrasiewicz (struct usb_descriptor_header *)&hidg_hs_out_ep_desc, 18300a2430fSAndrzej Pietrasiewicz NULL, 18400a2430fSAndrzej Pietrasiewicz }; 18500a2430fSAndrzej Pietrasiewicz 18600a2430fSAndrzej Pietrasiewicz /* Full-Speed Support */ 18700a2430fSAndrzej Pietrasiewicz 18800a2430fSAndrzej Pietrasiewicz static struct usb_endpoint_descriptor hidg_fs_in_ep_desc = { 18900a2430fSAndrzej Pietrasiewicz .bLength = USB_DT_ENDPOINT_SIZE, 19000a2430fSAndrzej Pietrasiewicz .bDescriptorType = USB_DT_ENDPOINT, 19100a2430fSAndrzej Pietrasiewicz .bEndpointAddress = USB_DIR_IN, 19200a2430fSAndrzej Pietrasiewicz .bmAttributes = USB_ENDPOINT_XFER_INT, 19300a2430fSAndrzej Pietrasiewicz /*.wMaxPacketSize = DYNAMIC */ 19400a2430fSAndrzej Pietrasiewicz .bInterval = 10, /* FIXME: Add this field in the 19500a2430fSAndrzej Pietrasiewicz * HID gadget configuration? 19600a2430fSAndrzej Pietrasiewicz * (struct hidg_func_descriptor) 19700a2430fSAndrzej Pietrasiewicz */ 19800a2430fSAndrzej Pietrasiewicz }; 19900a2430fSAndrzej Pietrasiewicz 20000a2430fSAndrzej Pietrasiewicz static struct usb_endpoint_descriptor hidg_fs_out_ep_desc = { 20100a2430fSAndrzej Pietrasiewicz .bLength = USB_DT_ENDPOINT_SIZE, 20200a2430fSAndrzej Pietrasiewicz .bDescriptorType = USB_DT_ENDPOINT, 20300a2430fSAndrzej Pietrasiewicz .bEndpointAddress = USB_DIR_OUT, 20400a2430fSAndrzej Pietrasiewicz .bmAttributes = USB_ENDPOINT_XFER_INT, 20500a2430fSAndrzej Pietrasiewicz /*.wMaxPacketSize = DYNAMIC */ 20600a2430fSAndrzej Pietrasiewicz .bInterval = 10, /* FIXME: Add this field in the 20700a2430fSAndrzej Pietrasiewicz * HID gadget configuration? 20800a2430fSAndrzej Pietrasiewicz * (struct hidg_func_descriptor) 20900a2430fSAndrzej Pietrasiewicz */ 21000a2430fSAndrzej Pietrasiewicz }; 21100a2430fSAndrzej Pietrasiewicz 21200a2430fSAndrzej Pietrasiewicz static struct usb_descriptor_header *hidg_fs_descriptors[] = { 21300a2430fSAndrzej Pietrasiewicz (struct usb_descriptor_header *)&hidg_interface_desc, 21400a2430fSAndrzej Pietrasiewicz (struct usb_descriptor_header *)&hidg_desc, 21500a2430fSAndrzej Pietrasiewicz (struct usb_descriptor_header *)&hidg_fs_in_ep_desc, 21600a2430fSAndrzej Pietrasiewicz (struct usb_descriptor_header *)&hidg_fs_out_ep_desc, 21700a2430fSAndrzej Pietrasiewicz NULL, 21800a2430fSAndrzej Pietrasiewicz }; 21900a2430fSAndrzej Pietrasiewicz 22000a2430fSAndrzej Pietrasiewicz /*-------------------------------------------------------------------------*/ 221cb382536SAndrzej Pietrasiewicz /* Strings */ 222cb382536SAndrzej Pietrasiewicz 223cb382536SAndrzej Pietrasiewicz #define CT_FUNC_HID_IDX 0 224cb382536SAndrzej Pietrasiewicz 225cb382536SAndrzej Pietrasiewicz static struct usb_string ct_func_string_defs[] = { 226cb382536SAndrzej Pietrasiewicz [CT_FUNC_HID_IDX].s = "HID Interface", 227cb382536SAndrzej Pietrasiewicz {}, /* end of list */ 228cb382536SAndrzej Pietrasiewicz }; 229cb382536SAndrzej Pietrasiewicz 230cb382536SAndrzej Pietrasiewicz static struct usb_gadget_strings ct_func_string_table = { 231cb382536SAndrzej Pietrasiewicz .language = 0x0409, /* en-US */ 232cb382536SAndrzej Pietrasiewicz .strings = ct_func_string_defs, 233cb382536SAndrzej Pietrasiewicz }; 234cb382536SAndrzej Pietrasiewicz 235cb382536SAndrzej Pietrasiewicz static struct usb_gadget_strings *ct_func_strings[] = { 236cb382536SAndrzej Pietrasiewicz &ct_func_string_table, 237cb382536SAndrzej Pietrasiewicz NULL, 238cb382536SAndrzej Pietrasiewicz }; 239cb382536SAndrzej Pietrasiewicz 240cb382536SAndrzej Pietrasiewicz /*-------------------------------------------------------------------------*/ 24100a2430fSAndrzej Pietrasiewicz /* Char Device */ 24200a2430fSAndrzej Pietrasiewicz 24300a2430fSAndrzej Pietrasiewicz static ssize_t f_hidg_read(struct file *file, char __user *buffer, 24400a2430fSAndrzej Pietrasiewicz size_t count, loff_t *ptr) 24500a2430fSAndrzej Pietrasiewicz { 24600a2430fSAndrzej Pietrasiewicz struct f_hidg *hidg = file->private_data; 24700a2430fSAndrzej Pietrasiewicz struct f_hidg_req_list *list; 24800a2430fSAndrzej Pietrasiewicz struct usb_request *req; 24900a2430fSAndrzej Pietrasiewicz unsigned long flags; 25000a2430fSAndrzej Pietrasiewicz int ret; 25100a2430fSAndrzej Pietrasiewicz 25200a2430fSAndrzej Pietrasiewicz if (!count) 25300a2430fSAndrzej Pietrasiewicz return 0; 25400a2430fSAndrzej Pietrasiewicz 25533e4c1a9SKrzysztof Opasiak spin_lock_irqsave(&hidg->read_spinlock, flags); 25600a2430fSAndrzej Pietrasiewicz 25700a2430fSAndrzej Pietrasiewicz #define READ_COND (!list_empty(&hidg->completed_out_req)) 25800a2430fSAndrzej Pietrasiewicz 25900a2430fSAndrzej Pietrasiewicz /* wait for at least one buffer to complete */ 26000a2430fSAndrzej Pietrasiewicz while (!READ_COND) { 26133e4c1a9SKrzysztof Opasiak spin_unlock_irqrestore(&hidg->read_spinlock, flags); 26200a2430fSAndrzej Pietrasiewicz if (file->f_flags & O_NONBLOCK) 26300a2430fSAndrzej Pietrasiewicz return -EAGAIN; 26400a2430fSAndrzej Pietrasiewicz 26500a2430fSAndrzej Pietrasiewicz if (wait_event_interruptible(hidg->read_queue, READ_COND)) 26600a2430fSAndrzej Pietrasiewicz return -ERESTARTSYS; 26700a2430fSAndrzej Pietrasiewicz 26833e4c1a9SKrzysztof Opasiak spin_lock_irqsave(&hidg->read_spinlock, flags); 26900a2430fSAndrzej Pietrasiewicz } 27000a2430fSAndrzej Pietrasiewicz 27100a2430fSAndrzej Pietrasiewicz /* pick the first one */ 27200a2430fSAndrzej Pietrasiewicz list = list_first_entry(&hidg->completed_out_req, 27300a2430fSAndrzej Pietrasiewicz struct f_hidg_req_list, list); 274aa65d11aSKrzysztof Opasiak 275aa65d11aSKrzysztof Opasiak /* 276aa65d11aSKrzysztof Opasiak * Remove this from list to protect it from beign free() 277aa65d11aSKrzysztof Opasiak * while host disables our function 278aa65d11aSKrzysztof Opasiak */ 279aa65d11aSKrzysztof Opasiak list_del(&list->list); 280aa65d11aSKrzysztof Opasiak 28100a2430fSAndrzej Pietrasiewicz req = list->req; 28200a2430fSAndrzej Pietrasiewicz count = min_t(unsigned int, count, req->actual - list->pos); 28333e4c1a9SKrzysztof Opasiak spin_unlock_irqrestore(&hidg->read_spinlock, flags); 28400a2430fSAndrzej Pietrasiewicz 28500a2430fSAndrzej Pietrasiewicz /* copy to user outside spinlock */ 28600a2430fSAndrzej Pietrasiewicz count -= copy_to_user(buffer, req->buf + list->pos, count); 28700a2430fSAndrzej Pietrasiewicz list->pos += count; 28800a2430fSAndrzej Pietrasiewicz 28900a2430fSAndrzej Pietrasiewicz /* 29000a2430fSAndrzej Pietrasiewicz * if this request is completely handled and transfered to 29100a2430fSAndrzej Pietrasiewicz * userspace, remove its entry from the list and requeue it 29200a2430fSAndrzej Pietrasiewicz * again. Otherwise, we will revisit it again upon the next 29300a2430fSAndrzej Pietrasiewicz * call, taking into account its current read position. 29400a2430fSAndrzej Pietrasiewicz */ 29500a2430fSAndrzej Pietrasiewicz if (list->pos == req->actual) { 29600a2430fSAndrzej Pietrasiewicz kfree(list); 29700a2430fSAndrzej Pietrasiewicz 29800a2430fSAndrzej Pietrasiewicz req->length = hidg->report_length; 29900a2430fSAndrzej Pietrasiewicz ret = usb_ep_queue(hidg->out_ep, req, GFP_KERNEL); 300aa65d11aSKrzysztof Opasiak if (ret < 0) { 301aa65d11aSKrzysztof Opasiak free_ep_req(hidg->out_ep, req); 30200a2430fSAndrzej Pietrasiewicz return ret; 30300a2430fSAndrzej Pietrasiewicz } 304aa65d11aSKrzysztof Opasiak } else { 30533e4c1a9SKrzysztof Opasiak spin_lock_irqsave(&hidg->read_spinlock, flags); 306aa65d11aSKrzysztof Opasiak list_add(&list->list, &hidg->completed_out_req); 30733e4c1a9SKrzysztof Opasiak spin_unlock_irqrestore(&hidg->read_spinlock, flags); 308aa65d11aSKrzysztof Opasiak 309aa65d11aSKrzysztof Opasiak wake_up(&hidg->read_queue); 310aa65d11aSKrzysztof Opasiak } 31100a2430fSAndrzej Pietrasiewicz 31200a2430fSAndrzej Pietrasiewicz return count; 31300a2430fSAndrzej Pietrasiewicz } 31400a2430fSAndrzej Pietrasiewicz 31500a2430fSAndrzej Pietrasiewicz static void f_hidg_req_complete(struct usb_ep *ep, struct usb_request *req) 31600a2430fSAndrzej Pietrasiewicz { 31700a2430fSAndrzej Pietrasiewicz struct f_hidg *hidg = (struct f_hidg *)ep->driver_data; 31833e4c1a9SKrzysztof Opasiak unsigned long flags; 31900a2430fSAndrzej Pietrasiewicz 32000a2430fSAndrzej Pietrasiewicz if (req->status != 0) { 32100a2430fSAndrzej Pietrasiewicz ERROR(hidg->func.config->cdev, 32200a2430fSAndrzej Pietrasiewicz "End Point Request ERROR: %d\n", req->status); 32300a2430fSAndrzej Pietrasiewicz } 32400a2430fSAndrzej Pietrasiewicz 32533e4c1a9SKrzysztof Opasiak spin_lock_irqsave(&hidg->write_spinlock, flags); 32600a2430fSAndrzej Pietrasiewicz hidg->write_pending = 0; 32733e4c1a9SKrzysztof Opasiak spin_unlock_irqrestore(&hidg->write_spinlock, flags); 32800a2430fSAndrzej Pietrasiewicz wake_up(&hidg->write_queue); 32900a2430fSAndrzej Pietrasiewicz } 33000a2430fSAndrzej Pietrasiewicz 33100a2430fSAndrzej Pietrasiewicz static ssize_t f_hidg_write(struct file *file, const char __user *buffer, 33200a2430fSAndrzej Pietrasiewicz size_t count, loff_t *offp) 33300a2430fSAndrzej Pietrasiewicz { 33400a2430fSAndrzej Pietrasiewicz struct f_hidg *hidg = file->private_data; 335749494b6SKrzysztof Opasiak struct usb_request *req; 33633e4c1a9SKrzysztof Opasiak unsigned long flags; 33700a2430fSAndrzej Pietrasiewicz ssize_t status = -ENOMEM; 33800a2430fSAndrzej Pietrasiewicz 33933e4c1a9SKrzysztof Opasiak spin_lock_irqsave(&hidg->write_spinlock, flags); 34000a2430fSAndrzej Pietrasiewicz 34100a2430fSAndrzej Pietrasiewicz #define WRITE_COND (!hidg->write_pending) 342749494b6SKrzysztof Opasiak try_again: 34300a2430fSAndrzej Pietrasiewicz /* write queue */ 34400a2430fSAndrzej Pietrasiewicz while (!WRITE_COND) { 34533e4c1a9SKrzysztof Opasiak spin_unlock_irqrestore(&hidg->write_spinlock, flags); 34600a2430fSAndrzej Pietrasiewicz if (file->f_flags & O_NONBLOCK) 34700a2430fSAndrzej Pietrasiewicz return -EAGAIN; 34800a2430fSAndrzej Pietrasiewicz 34900a2430fSAndrzej Pietrasiewicz if (wait_event_interruptible_exclusive( 35000a2430fSAndrzej Pietrasiewicz hidg->write_queue, WRITE_COND)) 35100a2430fSAndrzej Pietrasiewicz return -ERESTARTSYS; 35200a2430fSAndrzej Pietrasiewicz 35333e4c1a9SKrzysztof Opasiak spin_lock_irqsave(&hidg->write_spinlock, flags); 35400a2430fSAndrzej Pietrasiewicz } 35500a2430fSAndrzej Pietrasiewicz 35633e4c1a9SKrzysztof Opasiak hidg->write_pending = 1; 357749494b6SKrzysztof Opasiak req = hidg->req; 35800a2430fSAndrzej Pietrasiewicz count = min_t(unsigned, count, hidg->report_length); 35933e4c1a9SKrzysztof Opasiak 36033e4c1a9SKrzysztof Opasiak spin_unlock_irqrestore(&hidg->write_spinlock, flags); 36125cd9721SKrzysztof Opasiak status = copy_from_user(req->buf, buffer, count); 36200a2430fSAndrzej Pietrasiewicz 36300a2430fSAndrzej Pietrasiewicz if (status != 0) { 36400a2430fSAndrzej Pietrasiewicz ERROR(hidg->func.config->cdev, 36500a2430fSAndrzej Pietrasiewicz "copy_from_user error\n"); 36633e4c1a9SKrzysztof Opasiak status = -EINVAL; 36733e4c1a9SKrzysztof Opasiak goto release_write_pending; 36800a2430fSAndrzej Pietrasiewicz } 36900a2430fSAndrzej Pietrasiewicz 370749494b6SKrzysztof Opasiak spin_lock_irqsave(&hidg->write_spinlock, flags); 371749494b6SKrzysztof Opasiak 37225cd9721SKrzysztof Opasiak /* when our function has been disabled by host */ 373749494b6SKrzysztof Opasiak if (!hidg->req) { 37425cd9721SKrzysztof Opasiak free_ep_req(hidg->in_ep, req); 375749494b6SKrzysztof Opasiak /* 376749494b6SKrzysztof Opasiak * TODO 377749494b6SKrzysztof Opasiak * Should we fail with error here? 378749494b6SKrzysztof Opasiak */ 379749494b6SKrzysztof Opasiak goto try_again; 380749494b6SKrzysztof Opasiak } 381749494b6SKrzysztof Opasiak 382749494b6SKrzysztof Opasiak req->status = 0; 383749494b6SKrzysztof Opasiak req->zero = 0; 384749494b6SKrzysztof Opasiak req->length = count; 385749494b6SKrzysztof Opasiak req->complete = f_hidg_req_complete; 386749494b6SKrzysztof Opasiak req->context = hidg; 38700a2430fSAndrzej Pietrasiewicz 388072684e8SRadoslav Gerganov spin_unlock_irqrestore(&hidg->write_spinlock, flags); 389072684e8SRadoslav Gerganov 39025cd9721SKrzysztof Opasiak status = usb_ep_queue(hidg->in_ep, req, GFP_ATOMIC); 39100a2430fSAndrzej Pietrasiewicz if (status < 0) { 39200a2430fSAndrzej Pietrasiewicz ERROR(hidg->func.config->cdev, 39300a2430fSAndrzej Pietrasiewicz "usb_ep_queue error on int endpoint %zd\n", status); 394072684e8SRadoslav Gerganov goto release_write_pending; 39500a2430fSAndrzej Pietrasiewicz } else { 39600a2430fSAndrzej Pietrasiewicz status = count; 39700a2430fSAndrzej Pietrasiewicz } 39800a2430fSAndrzej Pietrasiewicz 39933e4c1a9SKrzysztof Opasiak return status; 40033e4c1a9SKrzysztof Opasiak release_write_pending: 40133e4c1a9SKrzysztof Opasiak spin_lock_irqsave(&hidg->write_spinlock, flags); 40233e4c1a9SKrzysztof Opasiak hidg->write_pending = 0; 40333e4c1a9SKrzysztof Opasiak spin_unlock_irqrestore(&hidg->write_spinlock, flags); 40433e4c1a9SKrzysztof Opasiak 40533e4c1a9SKrzysztof Opasiak wake_up(&hidg->write_queue); 40600a2430fSAndrzej Pietrasiewicz 40700a2430fSAndrzej Pietrasiewicz return status; 40800a2430fSAndrzej Pietrasiewicz } 40900a2430fSAndrzej Pietrasiewicz 410afc9a42bSAl Viro static __poll_t f_hidg_poll(struct file *file, poll_table *wait) 41100a2430fSAndrzej Pietrasiewicz { 41200a2430fSAndrzej Pietrasiewicz struct f_hidg *hidg = file->private_data; 413afc9a42bSAl Viro __poll_t ret = 0; 41400a2430fSAndrzej Pietrasiewicz 41500a2430fSAndrzej Pietrasiewicz poll_wait(file, &hidg->read_queue, wait); 41600a2430fSAndrzej Pietrasiewicz poll_wait(file, &hidg->write_queue, wait); 41700a2430fSAndrzej Pietrasiewicz 41800a2430fSAndrzej Pietrasiewicz if (WRITE_COND) 419a9a08845SLinus Torvalds ret |= EPOLLOUT | EPOLLWRNORM; 42000a2430fSAndrzej Pietrasiewicz 42100a2430fSAndrzej Pietrasiewicz if (READ_COND) 422a9a08845SLinus Torvalds ret |= EPOLLIN | EPOLLRDNORM; 42300a2430fSAndrzej Pietrasiewicz 42400a2430fSAndrzej Pietrasiewicz return ret; 42500a2430fSAndrzej Pietrasiewicz } 42600a2430fSAndrzej Pietrasiewicz 42700a2430fSAndrzej Pietrasiewicz #undef WRITE_COND 42800a2430fSAndrzej Pietrasiewicz #undef READ_COND 42900a2430fSAndrzej Pietrasiewicz 43000a2430fSAndrzej Pietrasiewicz static int f_hidg_release(struct inode *inode, struct file *fd) 43100a2430fSAndrzej Pietrasiewicz { 43200a2430fSAndrzej Pietrasiewicz fd->private_data = NULL; 43300a2430fSAndrzej Pietrasiewicz return 0; 43400a2430fSAndrzej Pietrasiewicz } 43500a2430fSAndrzej Pietrasiewicz 43600a2430fSAndrzej Pietrasiewicz static int f_hidg_open(struct inode *inode, struct file *fd) 43700a2430fSAndrzej Pietrasiewicz { 43800a2430fSAndrzej Pietrasiewicz struct f_hidg *hidg = 43900a2430fSAndrzej Pietrasiewicz container_of(inode->i_cdev, struct f_hidg, cdev); 44000a2430fSAndrzej Pietrasiewicz 44100a2430fSAndrzej Pietrasiewicz fd->private_data = hidg; 44200a2430fSAndrzej Pietrasiewicz 44300a2430fSAndrzej Pietrasiewicz return 0; 44400a2430fSAndrzej Pietrasiewicz } 44500a2430fSAndrzej Pietrasiewicz 44600a2430fSAndrzej Pietrasiewicz /*-------------------------------------------------------------------------*/ 44700a2430fSAndrzej Pietrasiewicz /* usb_function */ 44800a2430fSAndrzej Pietrasiewicz 44900a2430fSAndrzej Pietrasiewicz static inline struct usb_request *hidg_alloc_ep_req(struct usb_ep *ep, 45000a2430fSAndrzej Pietrasiewicz unsigned length) 45100a2430fSAndrzej Pietrasiewicz { 452aadbe812SFelipe F. Tonello return alloc_ep_req(ep, length); 45300a2430fSAndrzej Pietrasiewicz } 45400a2430fSAndrzej Pietrasiewicz 45500a2430fSAndrzej Pietrasiewicz static void hidg_set_report_complete(struct usb_ep *ep, struct usb_request *req) 45600a2430fSAndrzej Pietrasiewicz { 45700a2430fSAndrzej Pietrasiewicz struct f_hidg *hidg = (struct f_hidg *) req->context; 45820d2ca95SKrzysztof Opasiak struct usb_composite_dev *cdev = hidg->func.config->cdev; 45900a2430fSAndrzej Pietrasiewicz struct f_hidg_req_list *req_list; 46000a2430fSAndrzej Pietrasiewicz unsigned long flags; 46100a2430fSAndrzej Pietrasiewicz 46220d2ca95SKrzysztof Opasiak switch (req->status) { 46320d2ca95SKrzysztof Opasiak case 0: 46400a2430fSAndrzej Pietrasiewicz req_list = kzalloc(sizeof(*req_list), GFP_ATOMIC); 46520d2ca95SKrzysztof Opasiak if (!req_list) { 46620d2ca95SKrzysztof Opasiak ERROR(cdev, "Unable to allocate mem for req_list\n"); 46720d2ca95SKrzysztof Opasiak goto free_req; 46820d2ca95SKrzysztof Opasiak } 46900a2430fSAndrzej Pietrasiewicz 47000a2430fSAndrzej Pietrasiewicz req_list->req = req; 47100a2430fSAndrzej Pietrasiewicz 47233e4c1a9SKrzysztof Opasiak spin_lock_irqsave(&hidg->read_spinlock, flags); 47300a2430fSAndrzej Pietrasiewicz list_add_tail(&req_list->list, &hidg->completed_out_req); 47433e4c1a9SKrzysztof Opasiak spin_unlock_irqrestore(&hidg->read_spinlock, flags); 47500a2430fSAndrzej Pietrasiewicz 47600a2430fSAndrzej Pietrasiewicz wake_up(&hidg->read_queue); 47720d2ca95SKrzysztof Opasiak break; 47820d2ca95SKrzysztof Opasiak default: 47920d2ca95SKrzysztof Opasiak ERROR(cdev, "Set report failed %d\n", req->status); 480a74005abSGustavo A. R. Silva fallthrough; 48120d2ca95SKrzysztof Opasiak case -ECONNABORTED: /* hardware forced ep reset */ 48220d2ca95SKrzysztof Opasiak case -ECONNRESET: /* request dequeued */ 48320d2ca95SKrzysztof Opasiak case -ESHUTDOWN: /* disconnect from host */ 48420d2ca95SKrzysztof Opasiak free_req: 48520d2ca95SKrzysztof Opasiak free_ep_req(ep, req); 48620d2ca95SKrzysztof Opasiak return; 48720d2ca95SKrzysztof Opasiak } 48800a2430fSAndrzej Pietrasiewicz } 48900a2430fSAndrzej Pietrasiewicz 49000a2430fSAndrzej Pietrasiewicz static int hidg_setup(struct usb_function *f, 49100a2430fSAndrzej Pietrasiewicz const struct usb_ctrlrequest *ctrl) 49200a2430fSAndrzej Pietrasiewicz { 49300a2430fSAndrzej Pietrasiewicz struct f_hidg *hidg = func_to_hidg(f); 49400a2430fSAndrzej Pietrasiewicz struct usb_composite_dev *cdev = f->config->cdev; 49500a2430fSAndrzej Pietrasiewicz struct usb_request *req = cdev->req; 49600a2430fSAndrzej Pietrasiewicz int status = 0; 49700a2430fSAndrzej Pietrasiewicz __u16 value, length; 49800a2430fSAndrzej Pietrasiewicz 49900a2430fSAndrzej Pietrasiewicz value = __le16_to_cpu(ctrl->wValue); 50000a2430fSAndrzej Pietrasiewicz length = __le16_to_cpu(ctrl->wLength); 50100a2430fSAndrzej Pietrasiewicz 502c9b3bde0SJulia Lawall VDBG(cdev, 503c9b3bde0SJulia Lawall "%s crtl_request : bRequestType:0x%x bRequest:0x%x Value:0x%x\n", 504c9b3bde0SJulia Lawall __func__, ctrl->bRequestType, ctrl->bRequest, value); 50500a2430fSAndrzej Pietrasiewicz 50600a2430fSAndrzej Pietrasiewicz switch ((ctrl->bRequestType << 8) | ctrl->bRequest) { 50700a2430fSAndrzej Pietrasiewicz case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8 50800a2430fSAndrzej Pietrasiewicz | HID_REQ_GET_REPORT): 50900a2430fSAndrzej Pietrasiewicz VDBG(cdev, "get_report\n"); 51000a2430fSAndrzej Pietrasiewicz 51100a2430fSAndrzej Pietrasiewicz /* send an empty report */ 51200a2430fSAndrzej Pietrasiewicz length = min_t(unsigned, length, hidg->report_length); 51300a2430fSAndrzej Pietrasiewicz memset(req->buf, 0x0, length); 51400a2430fSAndrzej Pietrasiewicz 51500a2430fSAndrzej Pietrasiewicz goto respond; 51600a2430fSAndrzej Pietrasiewicz break; 51700a2430fSAndrzej Pietrasiewicz 51800a2430fSAndrzej Pietrasiewicz case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8 51900a2430fSAndrzej Pietrasiewicz | HID_REQ_GET_PROTOCOL): 52000a2430fSAndrzej Pietrasiewicz VDBG(cdev, "get_protocol\n"); 521b3c4ec71SAbdulhadi Mohamed length = min_t(unsigned int, length, 1); 522b3c4ec71SAbdulhadi Mohamed ((u8 *) req->buf)[0] = hidg->protocol; 523b3c4ec71SAbdulhadi Mohamed goto respond; 52400a2430fSAndrzej Pietrasiewicz break; 52500a2430fSAndrzej Pietrasiewicz 52600a2430fSAndrzej Pietrasiewicz case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8 52700a2430fSAndrzej Pietrasiewicz | HID_REQ_SET_REPORT): 5286774def6SMasanari Iida VDBG(cdev, "set_report | wLength=%d\n", ctrl->wLength); 52900a2430fSAndrzej Pietrasiewicz goto stall; 53000a2430fSAndrzej Pietrasiewicz break; 53100a2430fSAndrzej Pietrasiewicz 53200a2430fSAndrzej Pietrasiewicz case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8 53300a2430fSAndrzej Pietrasiewicz | HID_REQ_SET_PROTOCOL): 53400a2430fSAndrzej Pietrasiewicz VDBG(cdev, "set_protocol\n"); 535b3c4ec71SAbdulhadi Mohamed if (value > HID_REPORT_PROTOCOL) 536b3c4ec71SAbdulhadi Mohamed goto stall; 537b3c4ec71SAbdulhadi Mohamed length = 0; 538b3c4ec71SAbdulhadi Mohamed /* 539b3c4ec71SAbdulhadi Mohamed * We assume that programs implementing the Boot protocol 540b3c4ec71SAbdulhadi Mohamed * are also compatible with the Report Protocol 541b3c4ec71SAbdulhadi Mohamed */ 542b3c4ec71SAbdulhadi Mohamed if (hidg->bInterfaceSubClass == USB_INTERFACE_SUBCLASS_BOOT) { 543b3c4ec71SAbdulhadi Mohamed hidg->protocol = value; 544b3c4ec71SAbdulhadi Mohamed goto respond; 545b3c4ec71SAbdulhadi Mohamed } 54600a2430fSAndrzej Pietrasiewicz goto stall; 54700a2430fSAndrzej Pietrasiewicz break; 54800a2430fSAndrzej Pietrasiewicz 54900a2430fSAndrzej Pietrasiewicz case ((USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_INTERFACE) << 8 55000a2430fSAndrzej Pietrasiewicz | USB_REQ_GET_DESCRIPTOR): 55100a2430fSAndrzej Pietrasiewicz switch (value >> 8) { 55200a2430fSAndrzej Pietrasiewicz case HID_DT_HID: 553f286d487SKrzysztof Opasiak { 554f286d487SKrzysztof Opasiak struct hid_descriptor hidg_desc_copy = hidg_desc; 555f286d487SKrzysztof Opasiak 55600a2430fSAndrzej Pietrasiewicz VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: HID\n"); 557f286d487SKrzysztof Opasiak hidg_desc_copy.desc[0].bDescriptorType = HID_DT_REPORT; 558f286d487SKrzysztof Opasiak hidg_desc_copy.desc[0].wDescriptorLength = 559f286d487SKrzysztof Opasiak cpu_to_le16(hidg->report_desc_length); 560f286d487SKrzysztof Opasiak 56100a2430fSAndrzej Pietrasiewicz length = min_t(unsigned short, length, 562f286d487SKrzysztof Opasiak hidg_desc_copy.bLength); 563f286d487SKrzysztof Opasiak memcpy(req->buf, &hidg_desc_copy, length); 56400a2430fSAndrzej Pietrasiewicz goto respond; 56500a2430fSAndrzej Pietrasiewicz break; 566f286d487SKrzysztof Opasiak } 56700a2430fSAndrzej Pietrasiewicz case HID_DT_REPORT: 56800a2430fSAndrzej Pietrasiewicz VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: REPORT\n"); 56900a2430fSAndrzej Pietrasiewicz length = min_t(unsigned short, length, 57000a2430fSAndrzej Pietrasiewicz hidg->report_desc_length); 57100a2430fSAndrzej Pietrasiewicz memcpy(req->buf, hidg->report_desc, length); 57200a2430fSAndrzej Pietrasiewicz goto respond; 57300a2430fSAndrzej Pietrasiewicz break; 57400a2430fSAndrzej Pietrasiewicz 57500a2430fSAndrzej Pietrasiewicz default: 57600a2430fSAndrzej Pietrasiewicz VDBG(cdev, "Unknown descriptor request 0x%x\n", 57700a2430fSAndrzej Pietrasiewicz value >> 8); 57800a2430fSAndrzej Pietrasiewicz goto stall; 57900a2430fSAndrzej Pietrasiewicz break; 58000a2430fSAndrzej Pietrasiewicz } 58100a2430fSAndrzej Pietrasiewicz break; 58200a2430fSAndrzej Pietrasiewicz 58300a2430fSAndrzej Pietrasiewicz default: 58400a2430fSAndrzej Pietrasiewicz VDBG(cdev, "Unknown request 0x%x\n", 58500a2430fSAndrzej Pietrasiewicz ctrl->bRequest); 58600a2430fSAndrzej Pietrasiewicz goto stall; 58700a2430fSAndrzej Pietrasiewicz break; 58800a2430fSAndrzej Pietrasiewicz } 58900a2430fSAndrzej Pietrasiewicz 59000a2430fSAndrzej Pietrasiewicz stall: 59100a2430fSAndrzej Pietrasiewicz return -EOPNOTSUPP; 59200a2430fSAndrzej Pietrasiewicz 59300a2430fSAndrzej Pietrasiewicz respond: 59400a2430fSAndrzej Pietrasiewicz req->zero = 0; 59500a2430fSAndrzej Pietrasiewicz req->length = length; 59600a2430fSAndrzej Pietrasiewicz status = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC); 59700a2430fSAndrzej Pietrasiewicz if (status < 0) 59800a2430fSAndrzej Pietrasiewicz ERROR(cdev, "usb_ep_queue error on ep0 %d\n", value); 59900a2430fSAndrzej Pietrasiewicz return status; 60000a2430fSAndrzej Pietrasiewicz } 60100a2430fSAndrzej Pietrasiewicz 60200a2430fSAndrzej Pietrasiewicz static void hidg_disable(struct usb_function *f) 60300a2430fSAndrzej Pietrasiewicz { 60400a2430fSAndrzej Pietrasiewicz struct f_hidg *hidg = func_to_hidg(f); 60500a2430fSAndrzej Pietrasiewicz struct f_hidg_req_list *list, *next; 606aa65d11aSKrzysztof Opasiak unsigned long flags; 60700a2430fSAndrzej Pietrasiewicz 60800a2430fSAndrzej Pietrasiewicz usb_ep_disable(hidg->in_ep); 60900a2430fSAndrzej Pietrasiewicz usb_ep_disable(hidg->out_ep); 61000a2430fSAndrzej Pietrasiewicz 61133e4c1a9SKrzysztof Opasiak spin_lock_irqsave(&hidg->read_spinlock, flags); 61200a2430fSAndrzej Pietrasiewicz list_for_each_entry_safe(list, next, &hidg->completed_out_req, list) { 613aa65d11aSKrzysztof Opasiak free_ep_req(hidg->out_ep, list->req); 61400a2430fSAndrzej Pietrasiewicz list_del(&list->list); 61500a2430fSAndrzej Pietrasiewicz kfree(list); 61600a2430fSAndrzej Pietrasiewicz } 61733e4c1a9SKrzysztof Opasiak spin_unlock_irqrestore(&hidg->read_spinlock, flags); 618749494b6SKrzysztof Opasiak 619749494b6SKrzysztof Opasiak spin_lock_irqsave(&hidg->write_spinlock, flags); 620749494b6SKrzysztof Opasiak if (!hidg->write_pending) { 621749494b6SKrzysztof Opasiak free_ep_req(hidg->in_ep, hidg->req); 622749494b6SKrzysztof Opasiak hidg->write_pending = 1; 623749494b6SKrzysztof Opasiak } 624749494b6SKrzysztof Opasiak 625749494b6SKrzysztof Opasiak hidg->req = NULL; 626749494b6SKrzysztof Opasiak spin_unlock_irqrestore(&hidg->write_spinlock, flags); 62700a2430fSAndrzej Pietrasiewicz } 62800a2430fSAndrzej Pietrasiewicz 62900a2430fSAndrzej Pietrasiewicz static int hidg_set_alt(struct usb_function *f, unsigned intf, unsigned alt) 63000a2430fSAndrzej Pietrasiewicz { 63100a2430fSAndrzej Pietrasiewicz struct usb_composite_dev *cdev = f->config->cdev; 63200a2430fSAndrzej Pietrasiewicz struct f_hidg *hidg = func_to_hidg(f); 633749494b6SKrzysztof Opasiak struct usb_request *req_in = NULL; 634749494b6SKrzysztof Opasiak unsigned long flags; 63500a2430fSAndrzej Pietrasiewicz int i, status = 0; 63600a2430fSAndrzej Pietrasiewicz 63700a2430fSAndrzej Pietrasiewicz VDBG(cdev, "hidg_set_alt intf:%d alt:%d\n", intf, alt); 63800a2430fSAndrzej Pietrasiewicz 63900a2430fSAndrzej Pietrasiewicz if (hidg->in_ep != NULL) { 64000a2430fSAndrzej Pietrasiewicz /* restart endpoint */ 64100a2430fSAndrzej Pietrasiewicz usb_ep_disable(hidg->in_ep); 64200a2430fSAndrzej Pietrasiewicz 64300a2430fSAndrzej Pietrasiewicz status = config_ep_by_speed(f->config->cdev->gadget, f, 64400a2430fSAndrzej Pietrasiewicz hidg->in_ep); 64500a2430fSAndrzej Pietrasiewicz if (status) { 64600a2430fSAndrzej Pietrasiewicz ERROR(cdev, "config_ep_by_speed FAILED!\n"); 64700a2430fSAndrzej Pietrasiewicz goto fail; 64800a2430fSAndrzej Pietrasiewicz } 64900a2430fSAndrzej Pietrasiewicz status = usb_ep_enable(hidg->in_ep); 65000a2430fSAndrzej Pietrasiewicz if (status < 0) { 65100a2430fSAndrzej Pietrasiewicz ERROR(cdev, "Enable IN endpoint FAILED!\n"); 65200a2430fSAndrzej Pietrasiewicz goto fail; 65300a2430fSAndrzej Pietrasiewicz } 65400a2430fSAndrzej Pietrasiewicz hidg->in_ep->driver_data = hidg; 655749494b6SKrzysztof Opasiak 656749494b6SKrzysztof Opasiak req_in = hidg_alloc_ep_req(hidg->in_ep, hidg->report_length); 657749494b6SKrzysztof Opasiak if (!req_in) { 658749494b6SKrzysztof Opasiak status = -ENOMEM; 659749494b6SKrzysztof Opasiak goto disable_ep_in; 660749494b6SKrzysztof Opasiak } 66100a2430fSAndrzej Pietrasiewicz } 66200a2430fSAndrzej Pietrasiewicz 66300a2430fSAndrzej Pietrasiewicz 66400a2430fSAndrzej Pietrasiewicz if (hidg->out_ep != NULL) { 66500a2430fSAndrzej Pietrasiewicz /* restart endpoint */ 66600a2430fSAndrzej Pietrasiewicz usb_ep_disable(hidg->out_ep); 66700a2430fSAndrzej Pietrasiewicz 66800a2430fSAndrzej Pietrasiewicz status = config_ep_by_speed(f->config->cdev->gadget, f, 66900a2430fSAndrzej Pietrasiewicz hidg->out_ep); 67000a2430fSAndrzej Pietrasiewicz if (status) { 67100a2430fSAndrzej Pietrasiewicz ERROR(cdev, "config_ep_by_speed FAILED!\n"); 672749494b6SKrzysztof Opasiak goto free_req_in; 67300a2430fSAndrzej Pietrasiewicz } 67400a2430fSAndrzej Pietrasiewicz status = usb_ep_enable(hidg->out_ep); 67500a2430fSAndrzej Pietrasiewicz if (status < 0) { 67643aef5c2SDavid Lechner ERROR(cdev, "Enable OUT endpoint FAILED!\n"); 677749494b6SKrzysztof Opasiak goto free_req_in; 67800a2430fSAndrzej Pietrasiewicz } 67900a2430fSAndrzej Pietrasiewicz hidg->out_ep->driver_data = hidg; 68000a2430fSAndrzej Pietrasiewicz 68100a2430fSAndrzej Pietrasiewicz /* 68200a2430fSAndrzej Pietrasiewicz * allocate a bunch of read buffers and queue them all at once. 68300a2430fSAndrzej Pietrasiewicz */ 68400a2430fSAndrzej Pietrasiewicz for (i = 0; i < hidg->qlen && status == 0; i++) { 68500a2430fSAndrzej Pietrasiewicz struct usb_request *req = 68600a2430fSAndrzej Pietrasiewicz hidg_alloc_ep_req(hidg->out_ep, 68700a2430fSAndrzej Pietrasiewicz hidg->report_length); 68800a2430fSAndrzej Pietrasiewicz if (req) { 68900a2430fSAndrzej Pietrasiewicz req->complete = hidg_set_report_complete; 69000a2430fSAndrzej Pietrasiewicz req->context = hidg; 69100a2430fSAndrzej Pietrasiewicz status = usb_ep_queue(hidg->out_ep, req, 69200a2430fSAndrzej Pietrasiewicz GFP_ATOMIC); 693749494b6SKrzysztof Opasiak if (status) { 69400a2430fSAndrzej Pietrasiewicz ERROR(cdev, "%s queue req --> %d\n", 69500a2430fSAndrzej Pietrasiewicz hidg->out_ep->name, status); 696749494b6SKrzysztof Opasiak free_ep_req(hidg->out_ep, req); 697749494b6SKrzysztof Opasiak } 69800a2430fSAndrzej Pietrasiewicz } else { 69900a2430fSAndrzej Pietrasiewicz status = -ENOMEM; 700749494b6SKrzysztof Opasiak goto disable_out_ep; 70100a2430fSAndrzej Pietrasiewicz } 70200a2430fSAndrzej Pietrasiewicz } 70300a2430fSAndrzej Pietrasiewicz } 70400a2430fSAndrzej Pietrasiewicz 705749494b6SKrzysztof Opasiak if (hidg->in_ep != NULL) { 706749494b6SKrzysztof Opasiak spin_lock_irqsave(&hidg->write_spinlock, flags); 707749494b6SKrzysztof Opasiak hidg->req = req_in; 708749494b6SKrzysztof Opasiak hidg->write_pending = 0; 709749494b6SKrzysztof Opasiak spin_unlock_irqrestore(&hidg->write_spinlock, flags); 710749494b6SKrzysztof Opasiak 711749494b6SKrzysztof Opasiak wake_up(&hidg->write_queue); 712749494b6SKrzysztof Opasiak } 713749494b6SKrzysztof Opasiak return 0; 714749494b6SKrzysztof Opasiak disable_out_ep: 715749494b6SKrzysztof Opasiak usb_ep_disable(hidg->out_ep); 716749494b6SKrzysztof Opasiak free_req_in: 717749494b6SKrzysztof Opasiak if (req_in) 718749494b6SKrzysztof Opasiak free_ep_req(hidg->in_ep, req_in); 719749494b6SKrzysztof Opasiak 720749494b6SKrzysztof Opasiak disable_ep_in: 721749494b6SKrzysztof Opasiak if (hidg->in_ep) 722749494b6SKrzysztof Opasiak usb_ep_disable(hidg->in_ep); 723749494b6SKrzysztof Opasiak 72400a2430fSAndrzej Pietrasiewicz fail: 72500a2430fSAndrzej Pietrasiewicz return status; 72600a2430fSAndrzej Pietrasiewicz } 72700a2430fSAndrzej Pietrasiewicz 7287a3cc461SLad, Prabhakar static const struct file_operations f_hidg_fops = { 72900a2430fSAndrzej Pietrasiewicz .owner = THIS_MODULE, 73000a2430fSAndrzej Pietrasiewicz .open = f_hidg_open, 73100a2430fSAndrzej Pietrasiewicz .release = f_hidg_release, 73200a2430fSAndrzej Pietrasiewicz .write = f_hidg_write, 73300a2430fSAndrzej Pietrasiewicz .read = f_hidg_read, 73400a2430fSAndrzej Pietrasiewicz .poll = f_hidg_poll, 73500a2430fSAndrzej Pietrasiewicz .llseek = noop_llseek, 73600a2430fSAndrzej Pietrasiewicz }; 73700a2430fSAndrzej Pietrasiewicz 738cb382536SAndrzej Pietrasiewicz static int hidg_bind(struct usb_configuration *c, struct usb_function *f) 73900a2430fSAndrzej Pietrasiewicz { 74000a2430fSAndrzej Pietrasiewicz struct usb_ep *ep; 74100a2430fSAndrzej Pietrasiewicz struct f_hidg *hidg = func_to_hidg(f); 7425ca8d3ecSAndrzej Pietrasiewicz struct usb_string *us; 74363406087SAndrzej Pietrasiewicz struct device *device; 74400a2430fSAndrzej Pietrasiewicz int status; 74500a2430fSAndrzej Pietrasiewicz dev_t dev; 74600a2430fSAndrzej Pietrasiewicz 747cb382536SAndrzej Pietrasiewicz /* maybe allocate device-global string IDs, and patch descriptors */ 7485ca8d3ecSAndrzej Pietrasiewicz us = usb_gstrings_attach(c->cdev, ct_func_strings, 7495ca8d3ecSAndrzej Pietrasiewicz ARRAY_SIZE(ct_func_string_defs)); 7505ca8d3ecSAndrzej Pietrasiewicz if (IS_ERR(us)) 7515ca8d3ecSAndrzej Pietrasiewicz return PTR_ERR(us); 7525ca8d3ecSAndrzej Pietrasiewicz hidg_interface_desc.iInterface = us[CT_FUNC_HID_IDX].id; 753cb382536SAndrzej Pietrasiewicz 75400a2430fSAndrzej Pietrasiewicz /* allocate instance-specific interface IDs, and patch descriptors */ 75500a2430fSAndrzej Pietrasiewicz status = usb_interface_id(c, f); 75600a2430fSAndrzej Pietrasiewicz if (status < 0) 75700a2430fSAndrzej Pietrasiewicz goto fail; 75800a2430fSAndrzej Pietrasiewicz hidg_interface_desc.bInterfaceNumber = status; 75900a2430fSAndrzej Pietrasiewicz 76000a2430fSAndrzej Pietrasiewicz /* allocate instance-specific endpoints */ 76100a2430fSAndrzej Pietrasiewicz status = -ENODEV; 76200a2430fSAndrzej Pietrasiewicz ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_in_ep_desc); 76300a2430fSAndrzej Pietrasiewicz if (!ep) 76400a2430fSAndrzej Pietrasiewicz goto fail; 76500a2430fSAndrzej Pietrasiewicz hidg->in_ep = ep; 76600a2430fSAndrzej Pietrasiewicz 76700a2430fSAndrzej Pietrasiewicz ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_out_ep_desc); 76800a2430fSAndrzej Pietrasiewicz if (!ep) 76900a2430fSAndrzej Pietrasiewicz goto fail; 77000a2430fSAndrzej Pietrasiewicz hidg->out_ep = ep; 77100a2430fSAndrzej Pietrasiewicz 77200a2430fSAndrzej Pietrasiewicz /* set descriptor dynamic values */ 77300a2430fSAndrzej Pietrasiewicz hidg_interface_desc.bInterfaceSubClass = hidg->bInterfaceSubClass; 77400a2430fSAndrzej Pietrasiewicz hidg_interface_desc.bInterfaceProtocol = hidg->bInterfaceProtocol; 775b3c4ec71SAbdulhadi Mohamed hidg->protocol = HID_REPORT_PROTOCOL; 776dbf499cfSJanusz Dziedzic hidg_ss_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length); 777dbf499cfSJanusz Dziedzic hidg_ss_in_comp_desc.wBytesPerInterval = 778dbf499cfSJanusz Dziedzic cpu_to_le16(hidg->report_length); 77900a2430fSAndrzej Pietrasiewicz hidg_hs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length); 78000a2430fSAndrzej Pietrasiewicz hidg_fs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length); 781dbf499cfSJanusz Dziedzic hidg_ss_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length); 782dbf499cfSJanusz Dziedzic hidg_ss_out_comp_desc.wBytesPerInterval = 783dbf499cfSJanusz Dziedzic cpu_to_le16(hidg->report_length); 78400a2430fSAndrzej Pietrasiewicz hidg_hs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length); 78500a2430fSAndrzej Pietrasiewicz hidg_fs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length); 786f286d487SKrzysztof Opasiak /* 787f286d487SKrzysztof Opasiak * We can use hidg_desc struct here but we should not relay 788f286d487SKrzysztof Opasiak * that its content won't change after returning from this function. 789f286d487SKrzysztof Opasiak */ 79000a2430fSAndrzej Pietrasiewicz hidg_desc.desc[0].bDescriptorType = HID_DT_REPORT; 79100a2430fSAndrzej Pietrasiewicz hidg_desc.desc[0].wDescriptorLength = 79200a2430fSAndrzej Pietrasiewicz cpu_to_le16(hidg->report_desc_length); 79300a2430fSAndrzej Pietrasiewicz 79400a2430fSAndrzej Pietrasiewicz hidg_hs_in_ep_desc.bEndpointAddress = 79500a2430fSAndrzej Pietrasiewicz hidg_fs_in_ep_desc.bEndpointAddress; 79600a2430fSAndrzej Pietrasiewicz hidg_hs_out_ep_desc.bEndpointAddress = 79700a2430fSAndrzej Pietrasiewicz hidg_fs_out_ep_desc.bEndpointAddress; 79800a2430fSAndrzej Pietrasiewicz 799dbf499cfSJanusz Dziedzic hidg_ss_in_ep_desc.bEndpointAddress = 800dbf499cfSJanusz Dziedzic hidg_fs_in_ep_desc.bEndpointAddress; 801dbf499cfSJanusz Dziedzic hidg_ss_out_ep_desc.bEndpointAddress = 802dbf499cfSJanusz Dziedzic hidg_fs_out_ep_desc.bEndpointAddress; 803dbf499cfSJanusz Dziedzic 80400a2430fSAndrzej Pietrasiewicz status = usb_assign_descriptors(f, hidg_fs_descriptors, 80590c4d057SMaciej Żenczykowski hidg_hs_descriptors, hidg_ss_descriptors, 80690c4d057SMaciej Żenczykowski hidg_ss_descriptors); 80700a2430fSAndrzej Pietrasiewicz if (status) 80800a2430fSAndrzej Pietrasiewicz goto fail; 80900a2430fSAndrzej Pietrasiewicz 81033e4c1a9SKrzysztof Opasiak spin_lock_init(&hidg->write_spinlock); 811749494b6SKrzysztof Opasiak hidg->write_pending = 1; 812749494b6SKrzysztof Opasiak hidg->req = NULL; 81333e4c1a9SKrzysztof Opasiak spin_lock_init(&hidg->read_spinlock); 81400a2430fSAndrzej Pietrasiewicz init_waitqueue_head(&hidg->write_queue); 81500a2430fSAndrzej Pietrasiewicz init_waitqueue_head(&hidg->read_queue); 81600a2430fSAndrzej Pietrasiewicz INIT_LIST_HEAD(&hidg->completed_out_req); 81700a2430fSAndrzej Pietrasiewicz 81800a2430fSAndrzej Pietrasiewicz /* create char device */ 81900a2430fSAndrzej Pietrasiewicz cdev_init(&hidg->cdev, &f_hidg_fops); 82000a2430fSAndrzej Pietrasiewicz dev = MKDEV(major, hidg->minor); 82100a2430fSAndrzej Pietrasiewicz status = cdev_add(&hidg->cdev, dev, 1); 82200a2430fSAndrzej Pietrasiewicz if (status) 823d12a8727SPavitrakumar Managutte goto fail_free_descs; 82400a2430fSAndrzej Pietrasiewicz 82563406087SAndrzej Pietrasiewicz device = device_create(hidg_class, NULL, dev, NULL, 82663406087SAndrzej Pietrasiewicz "%s%d", "hidg", hidg->minor); 82763406087SAndrzej Pietrasiewicz if (IS_ERR(device)) { 82863406087SAndrzej Pietrasiewicz status = PTR_ERR(device); 82963406087SAndrzej Pietrasiewicz goto del; 83063406087SAndrzej Pietrasiewicz } 83100a2430fSAndrzej Pietrasiewicz 83200a2430fSAndrzej Pietrasiewicz return 0; 83363406087SAndrzej Pietrasiewicz del: 83463406087SAndrzej Pietrasiewicz cdev_del(&hidg->cdev); 835d12a8727SPavitrakumar Managutte fail_free_descs: 836d12a8727SPavitrakumar Managutte usb_free_all_descriptors(f); 83700a2430fSAndrzej Pietrasiewicz fail: 83800a2430fSAndrzej Pietrasiewicz ERROR(f->config->cdev, "hidg_bind FAILED\n"); 83914794d71SFelipe F. Tonello if (hidg->req != NULL) 84014794d71SFelipe F. Tonello free_ep_req(hidg->in_ep, hidg->req); 84100a2430fSAndrzej Pietrasiewicz 84200a2430fSAndrzej Pietrasiewicz return status; 84300a2430fSAndrzej Pietrasiewicz } 84400a2430fSAndrzej Pietrasiewicz 845cb382536SAndrzej Pietrasiewicz static inline int hidg_get_minor(void) 846cb382536SAndrzej Pietrasiewicz { 847cb382536SAndrzej Pietrasiewicz int ret; 848cb382536SAndrzej Pietrasiewicz 849cb382536SAndrzej Pietrasiewicz ret = ida_simple_get(&hidg_ida, 0, 0, GFP_KERNEL); 850774cf72fSAndrzej Pietrasiewicz if (ret >= HIDG_MINORS) { 851774cf72fSAndrzej Pietrasiewicz ida_simple_remove(&hidg_ida, ret); 852774cf72fSAndrzej Pietrasiewicz ret = -ENODEV; 853774cf72fSAndrzej Pietrasiewicz } 854cb382536SAndrzej Pietrasiewicz 855cb382536SAndrzej Pietrasiewicz return ret; 856cb382536SAndrzej Pietrasiewicz } 857cb382536SAndrzej Pietrasiewicz 85821a9476aSAndrzej Pietrasiewicz static inline struct f_hid_opts *to_f_hid_opts(struct config_item *item) 85921a9476aSAndrzej Pietrasiewicz { 86021a9476aSAndrzej Pietrasiewicz return container_of(to_config_group(item), struct f_hid_opts, 86121a9476aSAndrzej Pietrasiewicz func_inst.group); 86221a9476aSAndrzej Pietrasiewicz } 86321a9476aSAndrzej Pietrasiewicz 86421a9476aSAndrzej Pietrasiewicz static void hid_attr_release(struct config_item *item) 86521a9476aSAndrzej Pietrasiewicz { 86621a9476aSAndrzej Pietrasiewicz struct f_hid_opts *opts = to_f_hid_opts(item); 86721a9476aSAndrzej Pietrasiewicz 86821a9476aSAndrzej Pietrasiewicz usb_put_function_instance(&opts->func_inst); 86921a9476aSAndrzej Pietrasiewicz } 87021a9476aSAndrzej Pietrasiewicz 87121a9476aSAndrzej Pietrasiewicz static struct configfs_item_operations hidg_item_ops = { 87221a9476aSAndrzej Pietrasiewicz .release = hid_attr_release, 87321a9476aSAndrzej Pietrasiewicz }; 87421a9476aSAndrzej Pietrasiewicz 87521a9476aSAndrzej Pietrasiewicz #define F_HID_OPT(name, prec, limit) \ 876da4e527cSChristoph Hellwig static ssize_t f_hid_opts_##name##_show(struct config_item *item, char *page)\ 87721a9476aSAndrzej Pietrasiewicz { \ 878da4e527cSChristoph Hellwig struct f_hid_opts *opts = to_f_hid_opts(item); \ 87921a9476aSAndrzej Pietrasiewicz int result; \ 88021a9476aSAndrzej Pietrasiewicz \ 88121a9476aSAndrzej Pietrasiewicz mutex_lock(&opts->lock); \ 88221a9476aSAndrzej Pietrasiewicz result = sprintf(page, "%d\n", opts->name); \ 88321a9476aSAndrzej Pietrasiewicz mutex_unlock(&opts->lock); \ 88421a9476aSAndrzej Pietrasiewicz \ 88521a9476aSAndrzej Pietrasiewicz return result; \ 88621a9476aSAndrzej Pietrasiewicz } \ 88721a9476aSAndrzej Pietrasiewicz \ 888da4e527cSChristoph Hellwig static ssize_t f_hid_opts_##name##_store(struct config_item *item, \ 88921a9476aSAndrzej Pietrasiewicz const char *page, size_t len) \ 89021a9476aSAndrzej Pietrasiewicz { \ 891da4e527cSChristoph Hellwig struct f_hid_opts *opts = to_f_hid_opts(item); \ 89221a9476aSAndrzej Pietrasiewicz int ret; \ 89321a9476aSAndrzej Pietrasiewicz u##prec num; \ 89421a9476aSAndrzej Pietrasiewicz \ 89521a9476aSAndrzej Pietrasiewicz mutex_lock(&opts->lock); \ 89621a9476aSAndrzej Pietrasiewicz if (opts->refcnt) { \ 89721a9476aSAndrzej Pietrasiewicz ret = -EBUSY; \ 89821a9476aSAndrzej Pietrasiewicz goto end; \ 89921a9476aSAndrzej Pietrasiewicz } \ 90021a9476aSAndrzej Pietrasiewicz \ 90121a9476aSAndrzej Pietrasiewicz ret = kstrtou##prec(page, 0, &num); \ 90221a9476aSAndrzej Pietrasiewicz if (ret) \ 90321a9476aSAndrzej Pietrasiewicz goto end; \ 90421a9476aSAndrzej Pietrasiewicz \ 90521a9476aSAndrzej Pietrasiewicz if (num > limit) { \ 90621a9476aSAndrzej Pietrasiewicz ret = -EINVAL; \ 90721a9476aSAndrzej Pietrasiewicz goto end; \ 90821a9476aSAndrzej Pietrasiewicz } \ 90921a9476aSAndrzej Pietrasiewicz opts->name = num; \ 91021a9476aSAndrzej Pietrasiewicz ret = len; \ 91121a9476aSAndrzej Pietrasiewicz \ 91221a9476aSAndrzej Pietrasiewicz end: \ 91321a9476aSAndrzej Pietrasiewicz mutex_unlock(&opts->lock); \ 91421a9476aSAndrzej Pietrasiewicz return ret; \ 91521a9476aSAndrzej Pietrasiewicz } \ 91621a9476aSAndrzej Pietrasiewicz \ 917da4e527cSChristoph Hellwig CONFIGFS_ATTR(f_hid_opts_, name) 91821a9476aSAndrzej Pietrasiewicz 91921a9476aSAndrzej Pietrasiewicz F_HID_OPT(subclass, 8, 255); 92021a9476aSAndrzej Pietrasiewicz F_HID_OPT(protocol, 8, 255); 92139a2ac27SAndrzej Pietrasiewicz F_HID_OPT(report_length, 16, 65535); 92221a9476aSAndrzej Pietrasiewicz 923da4e527cSChristoph Hellwig static ssize_t f_hid_opts_report_desc_show(struct config_item *item, char *page) 92421a9476aSAndrzej Pietrasiewicz { 925da4e527cSChristoph Hellwig struct f_hid_opts *opts = to_f_hid_opts(item); 92621a9476aSAndrzej Pietrasiewicz int result; 92721a9476aSAndrzej Pietrasiewicz 92821a9476aSAndrzej Pietrasiewicz mutex_lock(&opts->lock); 92921a9476aSAndrzej Pietrasiewicz result = opts->report_desc_length; 93021a9476aSAndrzej Pietrasiewicz memcpy(page, opts->report_desc, opts->report_desc_length); 93121a9476aSAndrzej Pietrasiewicz mutex_unlock(&opts->lock); 93221a9476aSAndrzej Pietrasiewicz 93321a9476aSAndrzej Pietrasiewicz return result; 93421a9476aSAndrzej Pietrasiewicz } 93521a9476aSAndrzej Pietrasiewicz 936da4e527cSChristoph Hellwig static ssize_t f_hid_opts_report_desc_store(struct config_item *item, 93721a9476aSAndrzej Pietrasiewicz const char *page, size_t len) 93821a9476aSAndrzej Pietrasiewicz { 939da4e527cSChristoph Hellwig struct f_hid_opts *opts = to_f_hid_opts(item); 94021a9476aSAndrzej Pietrasiewicz int ret = -EBUSY; 94121a9476aSAndrzej Pietrasiewicz char *d; 94221a9476aSAndrzej Pietrasiewicz 94321a9476aSAndrzej Pietrasiewicz mutex_lock(&opts->lock); 94421a9476aSAndrzej Pietrasiewicz 94521a9476aSAndrzej Pietrasiewicz if (opts->refcnt) 94621a9476aSAndrzej Pietrasiewicz goto end; 94721a9476aSAndrzej Pietrasiewicz if (len > PAGE_SIZE) { 94821a9476aSAndrzej Pietrasiewicz ret = -ENOSPC; 94921a9476aSAndrzej Pietrasiewicz goto end; 95021a9476aSAndrzej Pietrasiewicz } 95121a9476aSAndrzej Pietrasiewicz d = kmemdup(page, len, GFP_KERNEL); 95221a9476aSAndrzej Pietrasiewicz if (!d) { 95321a9476aSAndrzej Pietrasiewicz ret = -ENOMEM; 95421a9476aSAndrzej Pietrasiewicz goto end; 95521a9476aSAndrzej Pietrasiewicz } 95621a9476aSAndrzej Pietrasiewicz kfree(opts->report_desc); 95721a9476aSAndrzej Pietrasiewicz opts->report_desc = d; 95821a9476aSAndrzej Pietrasiewicz opts->report_desc_length = len; 95921a9476aSAndrzej Pietrasiewicz opts->report_desc_alloc = true; 96021a9476aSAndrzej Pietrasiewicz ret = len; 96121a9476aSAndrzej Pietrasiewicz end: 96221a9476aSAndrzej Pietrasiewicz mutex_unlock(&opts->lock); 96321a9476aSAndrzej Pietrasiewicz return ret; 96421a9476aSAndrzej Pietrasiewicz } 96521a9476aSAndrzej Pietrasiewicz 966da4e527cSChristoph Hellwig CONFIGFS_ATTR(f_hid_opts_, report_desc); 96721a9476aSAndrzej Pietrasiewicz 968ed6fe1f5SJohannes Berg static ssize_t f_hid_opts_dev_show(struct config_item *item, char *page) 969ed6fe1f5SJohannes Berg { 970ed6fe1f5SJohannes Berg struct f_hid_opts *opts = to_f_hid_opts(item); 971ed6fe1f5SJohannes Berg 972ed6fe1f5SJohannes Berg return sprintf(page, "%d:%d\n", major, opts->minor); 973ed6fe1f5SJohannes Berg } 974ed6fe1f5SJohannes Berg 975ed6fe1f5SJohannes Berg CONFIGFS_ATTR_RO(f_hid_opts_, dev); 976ed6fe1f5SJohannes Berg 97721a9476aSAndrzej Pietrasiewicz static struct configfs_attribute *hid_attrs[] = { 978da4e527cSChristoph Hellwig &f_hid_opts_attr_subclass, 979da4e527cSChristoph Hellwig &f_hid_opts_attr_protocol, 980da4e527cSChristoph Hellwig &f_hid_opts_attr_report_length, 981da4e527cSChristoph Hellwig &f_hid_opts_attr_report_desc, 982ed6fe1f5SJohannes Berg &f_hid_opts_attr_dev, 98321a9476aSAndrzej Pietrasiewicz NULL, 98421a9476aSAndrzej Pietrasiewicz }; 98521a9476aSAndrzej Pietrasiewicz 98697363902SBhumika Goyal static const struct config_item_type hid_func_type = { 98721a9476aSAndrzej Pietrasiewicz .ct_item_ops = &hidg_item_ops, 98821a9476aSAndrzej Pietrasiewicz .ct_attrs = hid_attrs, 98921a9476aSAndrzej Pietrasiewicz .ct_owner = THIS_MODULE, 99021a9476aSAndrzej Pietrasiewicz }; 99121a9476aSAndrzej Pietrasiewicz 992cb382536SAndrzej Pietrasiewicz static inline void hidg_put_minor(int minor) 993cb382536SAndrzej Pietrasiewicz { 994cb382536SAndrzej Pietrasiewicz ida_simple_remove(&hidg_ida, minor); 995cb382536SAndrzej Pietrasiewicz } 996cb382536SAndrzej Pietrasiewicz 997cb382536SAndrzej Pietrasiewicz static void hidg_free_inst(struct usb_function_instance *f) 998cb382536SAndrzej Pietrasiewicz { 999cb382536SAndrzej Pietrasiewicz struct f_hid_opts *opts; 1000cb382536SAndrzej Pietrasiewicz 1001cb382536SAndrzej Pietrasiewicz opts = container_of(f, struct f_hid_opts, func_inst); 1002cb382536SAndrzej Pietrasiewicz 1003cb382536SAndrzej Pietrasiewicz mutex_lock(&hidg_ida_lock); 1004cb382536SAndrzej Pietrasiewicz 1005cb382536SAndrzej Pietrasiewicz hidg_put_minor(opts->minor); 100699c49407SMatthew Wilcox if (ida_is_empty(&hidg_ida)) 1007cb382536SAndrzej Pietrasiewicz ghid_cleanup(); 1008cb382536SAndrzej Pietrasiewicz 1009cb382536SAndrzej Pietrasiewicz mutex_unlock(&hidg_ida_lock); 1010cb382536SAndrzej Pietrasiewicz 1011cb382536SAndrzej Pietrasiewicz if (opts->report_desc_alloc) 1012cb382536SAndrzej Pietrasiewicz kfree(opts->report_desc); 1013cb382536SAndrzej Pietrasiewicz 1014cb382536SAndrzej Pietrasiewicz kfree(opts); 1015cb382536SAndrzej Pietrasiewicz } 1016cb382536SAndrzej Pietrasiewicz 1017cb382536SAndrzej Pietrasiewicz static struct usb_function_instance *hidg_alloc_inst(void) 1018cb382536SAndrzej Pietrasiewicz { 1019cb382536SAndrzej Pietrasiewicz struct f_hid_opts *opts; 1020cb382536SAndrzej Pietrasiewicz struct usb_function_instance *ret; 1021cb382536SAndrzej Pietrasiewicz int status = 0; 1022cb382536SAndrzej Pietrasiewicz 1023cb382536SAndrzej Pietrasiewicz opts = kzalloc(sizeof(*opts), GFP_KERNEL); 1024cb382536SAndrzej Pietrasiewicz if (!opts) 1025cb382536SAndrzej Pietrasiewicz return ERR_PTR(-ENOMEM); 102621a9476aSAndrzej Pietrasiewicz mutex_init(&opts->lock); 1027cb382536SAndrzej Pietrasiewicz opts->func_inst.free_func_inst = hidg_free_inst; 1028cb382536SAndrzej Pietrasiewicz ret = &opts->func_inst; 1029cb382536SAndrzej Pietrasiewicz 1030cb382536SAndrzej Pietrasiewicz mutex_lock(&hidg_ida_lock); 1031cb382536SAndrzej Pietrasiewicz 103299c49407SMatthew Wilcox if (ida_is_empty(&hidg_ida)) { 1033cb382536SAndrzej Pietrasiewicz status = ghid_setup(NULL, HIDG_MINORS); 1034cb382536SAndrzej Pietrasiewicz if (status) { 1035cb382536SAndrzej Pietrasiewicz ret = ERR_PTR(status); 1036cb382536SAndrzej Pietrasiewicz kfree(opts); 1037cb382536SAndrzej Pietrasiewicz goto unlock; 1038cb382536SAndrzej Pietrasiewicz } 1039cb382536SAndrzej Pietrasiewicz } 1040cb382536SAndrzej Pietrasiewicz 1041cb382536SAndrzej Pietrasiewicz opts->minor = hidg_get_minor(); 1042cb382536SAndrzej Pietrasiewicz if (opts->minor < 0) { 1043cb382536SAndrzej Pietrasiewicz ret = ERR_PTR(opts->minor); 1044cb382536SAndrzej Pietrasiewicz kfree(opts); 104599c49407SMatthew Wilcox if (ida_is_empty(&hidg_ida)) 1046cb382536SAndrzej Pietrasiewicz ghid_cleanup(); 1047828f6148SDan Carpenter goto unlock; 1048cb382536SAndrzej Pietrasiewicz } 104921a9476aSAndrzej Pietrasiewicz config_group_init_type_name(&opts->func_inst.group, "", &hid_func_type); 1050cb382536SAndrzej Pietrasiewicz 1051cb382536SAndrzej Pietrasiewicz unlock: 1052cb382536SAndrzej Pietrasiewicz mutex_unlock(&hidg_ida_lock); 1053cb382536SAndrzej Pietrasiewicz return ret; 1054cb382536SAndrzej Pietrasiewicz } 1055cb382536SAndrzej Pietrasiewicz 1056cb382536SAndrzej Pietrasiewicz static void hidg_free(struct usb_function *f) 1057cb382536SAndrzej Pietrasiewicz { 1058cb382536SAndrzej Pietrasiewicz struct f_hidg *hidg; 105921a9476aSAndrzej Pietrasiewicz struct f_hid_opts *opts; 1060cb382536SAndrzej Pietrasiewicz 1061cb382536SAndrzej Pietrasiewicz hidg = func_to_hidg(f); 106221a9476aSAndrzej Pietrasiewicz opts = container_of(f->fi, struct f_hid_opts, func_inst); 1063cb382536SAndrzej Pietrasiewicz kfree(hidg->report_desc); 1064cb382536SAndrzej Pietrasiewicz kfree(hidg); 106521a9476aSAndrzej Pietrasiewicz mutex_lock(&opts->lock); 106621a9476aSAndrzej Pietrasiewicz --opts->refcnt; 106721a9476aSAndrzej Pietrasiewicz mutex_unlock(&opts->lock); 1068cb382536SAndrzej Pietrasiewicz } 1069cb382536SAndrzej Pietrasiewicz 107000a2430fSAndrzej Pietrasiewicz static void hidg_unbind(struct usb_configuration *c, struct usb_function *f) 107100a2430fSAndrzej Pietrasiewicz { 107200a2430fSAndrzej Pietrasiewicz struct f_hidg *hidg = func_to_hidg(f); 107300a2430fSAndrzej Pietrasiewicz 107400a2430fSAndrzej Pietrasiewicz device_destroy(hidg_class, MKDEV(major, hidg->minor)); 107500a2430fSAndrzej Pietrasiewicz cdev_del(&hidg->cdev); 107600a2430fSAndrzej Pietrasiewicz 107700a2430fSAndrzej Pietrasiewicz usb_free_all_descriptors(f); 107800a2430fSAndrzej Pietrasiewicz } 107900a2430fSAndrzej Pietrasiewicz 10800fc57ea0SFengguang Wu static struct usb_function *hidg_alloc(struct usb_function_instance *fi) 108100a2430fSAndrzej Pietrasiewicz { 108200a2430fSAndrzej Pietrasiewicz struct f_hidg *hidg; 1083cb382536SAndrzej Pietrasiewicz struct f_hid_opts *opts; 108400a2430fSAndrzej Pietrasiewicz 108500a2430fSAndrzej Pietrasiewicz /* allocate and initialize one new instance */ 1086cb382536SAndrzej Pietrasiewicz hidg = kzalloc(sizeof(*hidg), GFP_KERNEL); 108700a2430fSAndrzej Pietrasiewicz if (!hidg) 1088cb382536SAndrzej Pietrasiewicz return ERR_PTR(-ENOMEM); 108900a2430fSAndrzej Pietrasiewicz 1090cb382536SAndrzej Pietrasiewicz opts = container_of(fi, struct f_hid_opts, func_inst); 1091cb382536SAndrzej Pietrasiewicz 109221a9476aSAndrzej Pietrasiewicz mutex_lock(&opts->lock); 109321a9476aSAndrzej Pietrasiewicz ++opts->refcnt; 109421a9476aSAndrzej Pietrasiewicz 1095cb382536SAndrzej Pietrasiewicz hidg->minor = opts->minor; 1096cb382536SAndrzej Pietrasiewicz hidg->bInterfaceSubClass = opts->subclass; 1097cb382536SAndrzej Pietrasiewicz hidg->bInterfaceProtocol = opts->protocol; 1098cb382536SAndrzej Pietrasiewicz hidg->report_length = opts->report_length; 1099cb382536SAndrzej Pietrasiewicz hidg->report_desc_length = opts->report_desc_length; 1100cb382536SAndrzej Pietrasiewicz if (opts->report_desc) { 1101cb382536SAndrzej Pietrasiewicz hidg->report_desc = kmemdup(opts->report_desc, 1102cb382536SAndrzej Pietrasiewicz opts->report_desc_length, 110300a2430fSAndrzej Pietrasiewicz GFP_KERNEL); 110400a2430fSAndrzej Pietrasiewicz if (!hidg->report_desc) { 110500a2430fSAndrzej Pietrasiewicz kfree(hidg); 110621a9476aSAndrzej Pietrasiewicz mutex_unlock(&opts->lock); 1107cb382536SAndrzej Pietrasiewicz return ERR_PTR(-ENOMEM); 1108cb382536SAndrzej Pietrasiewicz } 110900a2430fSAndrzej Pietrasiewicz } 111000a2430fSAndrzej Pietrasiewicz 111121a9476aSAndrzej Pietrasiewicz mutex_unlock(&opts->lock); 111221a9476aSAndrzej Pietrasiewicz 111300a2430fSAndrzej Pietrasiewicz hidg->func.name = "hid"; 111400a2430fSAndrzej Pietrasiewicz hidg->func.bind = hidg_bind; 111500a2430fSAndrzej Pietrasiewicz hidg->func.unbind = hidg_unbind; 111600a2430fSAndrzej Pietrasiewicz hidg->func.set_alt = hidg_set_alt; 111700a2430fSAndrzej Pietrasiewicz hidg->func.disable = hidg_disable; 111800a2430fSAndrzej Pietrasiewicz hidg->func.setup = hidg_setup; 1119cb382536SAndrzej Pietrasiewicz hidg->func.free_func = hidg_free; 112000a2430fSAndrzej Pietrasiewicz 112129a812e4SWei Ming Chen /* this could be made configurable at some point */ 112200a2430fSAndrzej Pietrasiewicz hidg->qlen = 4; 112300a2430fSAndrzej Pietrasiewicz 1124cb382536SAndrzej Pietrasiewicz return &hidg->func; 112500a2430fSAndrzej Pietrasiewicz } 112600a2430fSAndrzej Pietrasiewicz 1127cb382536SAndrzej Pietrasiewicz DECLARE_USB_FUNCTION_INIT(hid, hidg_alloc_inst, hidg_alloc); 1128cb382536SAndrzej Pietrasiewicz MODULE_LICENSE("GPL"); 1129cb382536SAndrzej Pietrasiewicz MODULE_AUTHOR("Fabien Chouteau"); 1130cb382536SAndrzej Pietrasiewicz 1131cb382536SAndrzej Pietrasiewicz int ghid_setup(struct usb_gadget *g, int count) 113200a2430fSAndrzej Pietrasiewicz { 113300a2430fSAndrzej Pietrasiewicz int status; 113400a2430fSAndrzej Pietrasiewicz dev_t dev; 113500a2430fSAndrzej Pietrasiewicz 113600a2430fSAndrzej Pietrasiewicz hidg_class = class_create(THIS_MODULE, "hidg"); 113706529407SAndrzej Pietrasiewicz if (IS_ERR(hidg_class)) { 11380448d38cSDan Carpenter status = PTR_ERR(hidg_class); 113906529407SAndrzej Pietrasiewicz hidg_class = NULL; 11400448d38cSDan Carpenter return status; 114100a2430fSAndrzej Pietrasiewicz } 114200a2430fSAndrzej Pietrasiewicz 114300a2430fSAndrzej Pietrasiewicz status = alloc_chrdev_region(&dev, 0, count, "hidg"); 11440448d38cSDan Carpenter if (status) { 11450448d38cSDan Carpenter class_destroy(hidg_class); 11460448d38cSDan Carpenter hidg_class = NULL; 114700a2430fSAndrzej Pietrasiewicz return status; 114800a2430fSAndrzej Pietrasiewicz } 114900a2430fSAndrzej Pietrasiewicz 11500448d38cSDan Carpenter major = MAJOR(dev); 11510448d38cSDan Carpenter minors = count; 11520448d38cSDan Carpenter 11530448d38cSDan Carpenter return 0; 115400a2430fSAndrzej Pietrasiewicz } 115500a2430fSAndrzej Pietrasiewicz 115600a2430fSAndrzej Pietrasiewicz void ghid_cleanup(void) 115700a2430fSAndrzej Pietrasiewicz { 115800a2430fSAndrzej Pietrasiewicz if (major) { 115900a2430fSAndrzej Pietrasiewicz unregister_chrdev_region(MKDEV(major, 0), minors); 116000a2430fSAndrzej Pietrasiewicz major = minors = 0; 116100a2430fSAndrzej Pietrasiewicz } 116200a2430fSAndrzej Pietrasiewicz 116300a2430fSAndrzej Pietrasiewicz class_destroy(hidg_class); 116400a2430fSAndrzej Pietrasiewicz hidg_class = NULL; 116500a2430fSAndrzej Pietrasiewicz } 1166