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; 44afcff6dcSMaxim Devaev unsigned char idle; 4500a2430fSAndrzej Pietrasiewicz unsigned short report_desc_length; 4600a2430fSAndrzej Pietrasiewicz char *report_desc; 4700a2430fSAndrzej Pietrasiewicz unsigned short report_length; 48*d7428bc2SMaxim Devaev /* 49*d7428bc2SMaxim Devaev * use_out_ep - if true, the OUT Endpoint (interrupt out method) 50*d7428bc2SMaxim Devaev * will be used to receive reports from the host 51*d7428bc2SMaxim Devaev * using functions with the "intout" suffix. 52*d7428bc2SMaxim Devaev * Otherwise, the OUT Endpoint will not be configured 53*d7428bc2SMaxim Devaev * and the SETUP/SET_REPORT method ("ssreport" suffix) 54*d7428bc2SMaxim Devaev * will be used to receive reports. 55*d7428bc2SMaxim Devaev */ 56*d7428bc2SMaxim Devaev bool use_out_ep; 5700a2430fSAndrzej Pietrasiewicz 5800a2430fSAndrzej Pietrasiewicz /* recv report */ 5933e4c1a9SKrzysztof Opasiak spinlock_t read_spinlock; 6000a2430fSAndrzej Pietrasiewicz wait_queue_head_t read_queue; 61*d7428bc2SMaxim Devaev /* recv report - interrupt out only (use_out_ep == 1) */ 62*d7428bc2SMaxim Devaev struct list_head completed_out_req; 6300a2430fSAndrzej Pietrasiewicz unsigned int qlen; 64*d7428bc2SMaxim Devaev /* recv report - setup set_report only (use_out_ep == 0) */ 65*d7428bc2SMaxim Devaev char *set_report_buf; 66*d7428bc2SMaxim Devaev unsigned int set_report_length; 6700a2430fSAndrzej Pietrasiewicz 6800a2430fSAndrzej Pietrasiewicz /* send report */ 6933e4c1a9SKrzysztof Opasiak spinlock_t write_spinlock; 7000a2430fSAndrzej Pietrasiewicz bool write_pending; 7100a2430fSAndrzej Pietrasiewicz wait_queue_head_t write_queue; 7200a2430fSAndrzej Pietrasiewicz struct usb_request *req; 7300a2430fSAndrzej Pietrasiewicz 7400a2430fSAndrzej Pietrasiewicz int minor; 7500a2430fSAndrzej Pietrasiewicz struct cdev cdev; 7600a2430fSAndrzej Pietrasiewicz struct usb_function func; 7700a2430fSAndrzej Pietrasiewicz 7800a2430fSAndrzej Pietrasiewicz struct usb_ep *in_ep; 7900a2430fSAndrzej Pietrasiewicz struct usb_ep *out_ep; 8000a2430fSAndrzej Pietrasiewicz }; 8100a2430fSAndrzej Pietrasiewicz 8200a2430fSAndrzej Pietrasiewicz static inline struct f_hidg *func_to_hidg(struct usb_function *f) 8300a2430fSAndrzej Pietrasiewicz { 8400a2430fSAndrzej Pietrasiewicz return container_of(f, struct f_hidg, func); 8500a2430fSAndrzej Pietrasiewicz } 8600a2430fSAndrzej Pietrasiewicz 8700a2430fSAndrzej Pietrasiewicz /*-------------------------------------------------------------------------*/ 8800a2430fSAndrzej Pietrasiewicz /* Static descriptors */ 8900a2430fSAndrzej Pietrasiewicz 9000a2430fSAndrzej Pietrasiewicz static struct usb_interface_descriptor hidg_interface_desc = { 9100a2430fSAndrzej Pietrasiewicz .bLength = sizeof hidg_interface_desc, 9200a2430fSAndrzej Pietrasiewicz .bDescriptorType = USB_DT_INTERFACE, 9300a2430fSAndrzej Pietrasiewicz /* .bInterfaceNumber = DYNAMIC */ 9400a2430fSAndrzej Pietrasiewicz .bAlternateSetting = 0, 95*d7428bc2SMaxim Devaev /* .bNumEndpoints = DYNAMIC (depends on use_out_ep) */ 9600a2430fSAndrzej Pietrasiewicz .bInterfaceClass = USB_CLASS_HID, 9700a2430fSAndrzej Pietrasiewicz /* .bInterfaceSubClass = DYNAMIC */ 9800a2430fSAndrzej Pietrasiewicz /* .bInterfaceProtocol = DYNAMIC */ 9900a2430fSAndrzej Pietrasiewicz /* .iInterface = DYNAMIC */ 10000a2430fSAndrzej Pietrasiewicz }; 10100a2430fSAndrzej Pietrasiewicz 10200a2430fSAndrzej Pietrasiewicz static struct hid_descriptor hidg_desc = { 10300a2430fSAndrzej Pietrasiewicz .bLength = sizeof hidg_desc, 10400a2430fSAndrzej Pietrasiewicz .bDescriptorType = HID_DT_HID, 10533cb46c4SRuslan Bilovol .bcdHID = cpu_to_le16(0x0101), 10600a2430fSAndrzej Pietrasiewicz .bCountryCode = 0x00, 10700a2430fSAndrzej Pietrasiewicz .bNumDescriptors = 0x1, 10800a2430fSAndrzej Pietrasiewicz /*.desc[0].bDescriptorType = DYNAMIC */ 10900a2430fSAndrzej Pietrasiewicz /*.desc[0].wDescriptorLenght = DYNAMIC */ 11000a2430fSAndrzej Pietrasiewicz }; 11100a2430fSAndrzej Pietrasiewicz 112dbf499cfSJanusz Dziedzic /* Super-Speed Support */ 113dbf499cfSJanusz Dziedzic 114dbf499cfSJanusz Dziedzic static struct usb_endpoint_descriptor hidg_ss_in_ep_desc = { 115dbf499cfSJanusz Dziedzic .bLength = USB_DT_ENDPOINT_SIZE, 116dbf499cfSJanusz Dziedzic .bDescriptorType = USB_DT_ENDPOINT, 117dbf499cfSJanusz Dziedzic .bEndpointAddress = USB_DIR_IN, 118dbf499cfSJanusz Dziedzic .bmAttributes = USB_ENDPOINT_XFER_INT, 119dbf499cfSJanusz Dziedzic /*.wMaxPacketSize = DYNAMIC */ 120dbf499cfSJanusz Dziedzic .bInterval = 4, /* FIXME: Add this field in the 121dbf499cfSJanusz Dziedzic * HID gadget configuration? 122dbf499cfSJanusz Dziedzic * (struct hidg_func_descriptor) 123dbf499cfSJanusz Dziedzic */ 124dbf499cfSJanusz Dziedzic }; 125dbf499cfSJanusz Dziedzic 126dbf499cfSJanusz Dziedzic static struct usb_ss_ep_comp_descriptor hidg_ss_in_comp_desc = { 127dbf499cfSJanusz Dziedzic .bLength = sizeof(hidg_ss_in_comp_desc), 128dbf499cfSJanusz Dziedzic .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, 129dbf499cfSJanusz Dziedzic 130dbf499cfSJanusz Dziedzic /* .bMaxBurst = 0, */ 131dbf499cfSJanusz Dziedzic /* .bmAttributes = 0, */ 132dbf499cfSJanusz Dziedzic /* .wBytesPerInterval = DYNAMIC */ 133dbf499cfSJanusz Dziedzic }; 134dbf499cfSJanusz Dziedzic 135dbf499cfSJanusz Dziedzic static struct usb_endpoint_descriptor hidg_ss_out_ep_desc = { 136dbf499cfSJanusz Dziedzic .bLength = USB_DT_ENDPOINT_SIZE, 137dbf499cfSJanusz Dziedzic .bDescriptorType = USB_DT_ENDPOINT, 138dbf499cfSJanusz Dziedzic .bEndpointAddress = USB_DIR_OUT, 139dbf499cfSJanusz Dziedzic .bmAttributes = USB_ENDPOINT_XFER_INT, 140dbf499cfSJanusz Dziedzic /*.wMaxPacketSize = DYNAMIC */ 141dbf499cfSJanusz Dziedzic .bInterval = 4, /* FIXME: Add this field in the 142dbf499cfSJanusz Dziedzic * HID gadget configuration? 143dbf499cfSJanusz Dziedzic * (struct hidg_func_descriptor) 144dbf499cfSJanusz Dziedzic */ 145dbf499cfSJanusz Dziedzic }; 146dbf499cfSJanusz Dziedzic 147dbf499cfSJanusz Dziedzic static struct usb_ss_ep_comp_descriptor hidg_ss_out_comp_desc = { 148dbf499cfSJanusz Dziedzic .bLength = sizeof(hidg_ss_out_comp_desc), 149dbf499cfSJanusz Dziedzic .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, 150dbf499cfSJanusz Dziedzic 151dbf499cfSJanusz Dziedzic /* .bMaxBurst = 0, */ 152dbf499cfSJanusz Dziedzic /* .bmAttributes = 0, */ 153dbf499cfSJanusz Dziedzic /* .wBytesPerInterval = DYNAMIC */ 154dbf499cfSJanusz Dziedzic }; 155dbf499cfSJanusz Dziedzic 156*d7428bc2SMaxim Devaev static struct usb_descriptor_header *hidg_ss_descriptors_intout[] = { 157dbf499cfSJanusz Dziedzic (struct usb_descriptor_header *)&hidg_interface_desc, 158dbf499cfSJanusz Dziedzic (struct usb_descriptor_header *)&hidg_desc, 159dbf499cfSJanusz Dziedzic (struct usb_descriptor_header *)&hidg_ss_in_ep_desc, 160dbf499cfSJanusz Dziedzic (struct usb_descriptor_header *)&hidg_ss_in_comp_desc, 161dbf499cfSJanusz Dziedzic (struct usb_descriptor_header *)&hidg_ss_out_ep_desc, 162dbf499cfSJanusz Dziedzic (struct usb_descriptor_header *)&hidg_ss_out_comp_desc, 163dbf499cfSJanusz Dziedzic NULL, 164dbf499cfSJanusz Dziedzic }; 165dbf499cfSJanusz Dziedzic 166*d7428bc2SMaxim Devaev static struct usb_descriptor_header *hidg_ss_descriptors_ssreport[] = { 167*d7428bc2SMaxim Devaev (struct usb_descriptor_header *)&hidg_interface_desc, 168*d7428bc2SMaxim Devaev (struct usb_descriptor_header *)&hidg_desc, 169*d7428bc2SMaxim Devaev (struct usb_descriptor_header *)&hidg_ss_in_ep_desc, 170*d7428bc2SMaxim Devaev (struct usb_descriptor_header *)&hidg_ss_in_comp_desc, 171*d7428bc2SMaxim Devaev NULL, 172*d7428bc2SMaxim Devaev }; 173*d7428bc2SMaxim Devaev 17400a2430fSAndrzej Pietrasiewicz /* High-Speed Support */ 17500a2430fSAndrzej Pietrasiewicz 17600a2430fSAndrzej Pietrasiewicz static struct usb_endpoint_descriptor hidg_hs_in_ep_desc = { 17700a2430fSAndrzej Pietrasiewicz .bLength = USB_DT_ENDPOINT_SIZE, 17800a2430fSAndrzej Pietrasiewicz .bDescriptorType = USB_DT_ENDPOINT, 17900a2430fSAndrzej Pietrasiewicz .bEndpointAddress = USB_DIR_IN, 18000a2430fSAndrzej Pietrasiewicz .bmAttributes = USB_ENDPOINT_XFER_INT, 18100a2430fSAndrzej Pietrasiewicz /*.wMaxPacketSize = DYNAMIC */ 18200a2430fSAndrzej Pietrasiewicz .bInterval = 4, /* FIXME: Add this field in the 18300a2430fSAndrzej Pietrasiewicz * HID gadget configuration? 18400a2430fSAndrzej Pietrasiewicz * (struct hidg_func_descriptor) 18500a2430fSAndrzej Pietrasiewicz */ 18600a2430fSAndrzej Pietrasiewicz }; 18700a2430fSAndrzej Pietrasiewicz 18800a2430fSAndrzej Pietrasiewicz static struct usb_endpoint_descriptor hidg_hs_out_ep_desc = { 18900a2430fSAndrzej Pietrasiewicz .bLength = USB_DT_ENDPOINT_SIZE, 19000a2430fSAndrzej Pietrasiewicz .bDescriptorType = USB_DT_ENDPOINT, 19100a2430fSAndrzej Pietrasiewicz .bEndpointAddress = USB_DIR_OUT, 19200a2430fSAndrzej Pietrasiewicz .bmAttributes = USB_ENDPOINT_XFER_INT, 19300a2430fSAndrzej Pietrasiewicz /*.wMaxPacketSize = DYNAMIC */ 19400a2430fSAndrzej Pietrasiewicz .bInterval = 4, /* FIXME: Add this field in the 19500a2430fSAndrzej Pietrasiewicz * HID gadget configuration? 19600a2430fSAndrzej Pietrasiewicz * (struct hidg_func_descriptor) 19700a2430fSAndrzej Pietrasiewicz */ 19800a2430fSAndrzej Pietrasiewicz }; 19900a2430fSAndrzej Pietrasiewicz 200*d7428bc2SMaxim Devaev static struct usb_descriptor_header *hidg_hs_descriptors_intout[] = { 20100a2430fSAndrzej Pietrasiewicz (struct usb_descriptor_header *)&hidg_interface_desc, 20200a2430fSAndrzej Pietrasiewicz (struct usb_descriptor_header *)&hidg_desc, 20300a2430fSAndrzej Pietrasiewicz (struct usb_descriptor_header *)&hidg_hs_in_ep_desc, 20400a2430fSAndrzej Pietrasiewicz (struct usb_descriptor_header *)&hidg_hs_out_ep_desc, 20500a2430fSAndrzej Pietrasiewicz NULL, 20600a2430fSAndrzej Pietrasiewicz }; 20700a2430fSAndrzej Pietrasiewicz 208*d7428bc2SMaxim Devaev static struct usb_descriptor_header *hidg_hs_descriptors_ssreport[] = { 209*d7428bc2SMaxim Devaev (struct usb_descriptor_header *)&hidg_interface_desc, 210*d7428bc2SMaxim Devaev (struct usb_descriptor_header *)&hidg_desc, 211*d7428bc2SMaxim Devaev (struct usb_descriptor_header *)&hidg_hs_in_ep_desc, 212*d7428bc2SMaxim Devaev NULL, 213*d7428bc2SMaxim Devaev }; 214*d7428bc2SMaxim Devaev 21500a2430fSAndrzej Pietrasiewicz /* Full-Speed Support */ 21600a2430fSAndrzej Pietrasiewicz 21700a2430fSAndrzej Pietrasiewicz static struct usb_endpoint_descriptor hidg_fs_in_ep_desc = { 21800a2430fSAndrzej Pietrasiewicz .bLength = USB_DT_ENDPOINT_SIZE, 21900a2430fSAndrzej Pietrasiewicz .bDescriptorType = USB_DT_ENDPOINT, 22000a2430fSAndrzej Pietrasiewicz .bEndpointAddress = USB_DIR_IN, 22100a2430fSAndrzej Pietrasiewicz .bmAttributes = USB_ENDPOINT_XFER_INT, 22200a2430fSAndrzej Pietrasiewicz /*.wMaxPacketSize = DYNAMIC */ 22300a2430fSAndrzej Pietrasiewicz .bInterval = 10, /* FIXME: Add this field in the 22400a2430fSAndrzej Pietrasiewicz * HID gadget configuration? 22500a2430fSAndrzej Pietrasiewicz * (struct hidg_func_descriptor) 22600a2430fSAndrzej Pietrasiewicz */ 22700a2430fSAndrzej Pietrasiewicz }; 22800a2430fSAndrzej Pietrasiewicz 22900a2430fSAndrzej Pietrasiewicz static struct usb_endpoint_descriptor hidg_fs_out_ep_desc = { 23000a2430fSAndrzej Pietrasiewicz .bLength = USB_DT_ENDPOINT_SIZE, 23100a2430fSAndrzej Pietrasiewicz .bDescriptorType = USB_DT_ENDPOINT, 23200a2430fSAndrzej Pietrasiewicz .bEndpointAddress = USB_DIR_OUT, 23300a2430fSAndrzej Pietrasiewicz .bmAttributes = USB_ENDPOINT_XFER_INT, 23400a2430fSAndrzej Pietrasiewicz /*.wMaxPacketSize = DYNAMIC */ 23500a2430fSAndrzej Pietrasiewicz .bInterval = 10, /* FIXME: Add this field in the 23600a2430fSAndrzej Pietrasiewicz * HID gadget configuration? 23700a2430fSAndrzej Pietrasiewicz * (struct hidg_func_descriptor) 23800a2430fSAndrzej Pietrasiewicz */ 23900a2430fSAndrzej Pietrasiewicz }; 24000a2430fSAndrzej Pietrasiewicz 241*d7428bc2SMaxim Devaev static struct usb_descriptor_header *hidg_fs_descriptors_intout[] = { 24200a2430fSAndrzej Pietrasiewicz (struct usb_descriptor_header *)&hidg_interface_desc, 24300a2430fSAndrzej Pietrasiewicz (struct usb_descriptor_header *)&hidg_desc, 24400a2430fSAndrzej Pietrasiewicz (struct usb_descriptor_header *)&hidg_fs_in_ep_desc, 24500a2430fSAndrzej Pietrasiewicz (struct usb_descriptor_header *)&hidg_fs_out_ep_desc, 24600a2430fSAndrzej Pietrasiewicz NULL, 24700a2430fSAndrzej Pietrasiewicz }; 24800a2430fSAndrzej Pietrasiewicz 249*d7428bc2SMaxim Devaev static struct usb_descriptor_header *hidg_fs_descriptors_ssreport[] = { 250*d7428bc2SMaxim Devaev (struct usb_descriptor_header *)&hidg_interface_desc, 251*d7428bc2SMaxim Devaev (struct usb_descriptor_header *)&hidg_desc, 252*d7428bc2SMaxim Devaev (struct usb_descriptor_header *)&hidg_fs_in_ep_desc, 253*d7428bc2SMaxim Devaev NULL, 254*d7428bc2SMaxim Devaev }; 255*d7428bc2SMaxim Devaev 25600a2430fSAndrzej Pietrasiewicz /*-------------------------------------------------------------------------*/ 257cb382536SAndrzej Pietrasiewicz /* Strings */ 258cb382536SAndrzej Pietrasiewicz 259cb382536SAndrzej Pietrasiewicz #define CT_FUNC_HID_IDX 0 260cb382536SAndrzej Pietrasiewicz 261cb382536SAndrzej Pietrasiewicz static struct usb_string ct_func_string_defs[] = { 262cb382536SAndrzej Pietrasiewicz [CT_FUNC_HID_IDX].s = "HID Interface", 263cb382536SAndrzej Pietrasiewicz {}, /* end of list */ 264cb382536SAndrzej Pietrasiewicz }; 265cb382536SAndrzej Pietrasiewicz 266cb382536SAndrzej Pietrasiewicz static struct usb_gadget_strings ct_func_string_table = { 267cb382536SAndrzej Pietrasiewicz .language = 0x0409, /* en-US */ 268cb382536SAndrzej Pietrasiewicz .strings = ct_func_string_defs, 269cb382536SAndrzej Pietrasiewicz }; 270cb382536SAndrzej Pietrasiewicz 271cb382536SAndrzej Pietrasiewicz static struct usb_gadget_strings *ct_func_strings[] = { 272cb382536SAndrzej Pietrasiewicz &ct_func_string_table, 273cb382536SAndrzej Pietrasiewicz NULL, 274cb382536SAndrzej Pietrasiewicz }; 275cb382536SAndrzej Pietrasiewicz 276cb382536SAndrzej Pietrasiewicz /*-------------------------------------------------------------------------*/ 27700a2430fSAndrzej Pietrasiewicz /* Char Device */ 27800a2430fSAndrzej Pietrasiewicz 279*d7428bc2SMaxim Devaev static ssize_t f_hidg_intout_read(struct file *file, char __user *buffer, 28000a2430fSAndrzej Pietrasiewicz size_t count, loff_t *ptr) 28100a2430fSAndrzej Pietrasiewicz { 28200a2430fSAndrzej Pietrasiewicz struct f_hidg *hidg = file->private_data; 28300a2430fSAndrzej Pietrasiewicz struct f_hidg_req_list *list; 28400a2430fSAndrzej Pietrasiewicz struct usb_request *req; 28500a2430fSAndrzej Pietrasiewicz unsigned long flags; 28600a2430fSAndrzej Pietrasiewicz int ret; 28700a2430fSAndrzej Pietrasiewicz 28800a2430fSAndrzej Pietrasiewicz if (!count) 28900a2430fSAndrzej Pietrasiewicz return 0; 29000a2430fSAndrzej Pietrasiewicz 29133e4c1a9SKrzysztof Opasiak spin_lock_irqsave(&hidg->read_spinlock, flags); 29200a2430fSAndrzej Pietrasiewicz 293*d7428bc2SMaxim Devaev #define READ_COND_INTOUT (!list_empty(&hidg->completed_out_req)) 29400a2430fSAndrzej Pietrasiewicz 29500a2430fSAndrzej Pietrasiewicz /* wait for at least one buffer to complete */ 296*d7428bc2SMaxim Devaev while (!READ_COND_INTOUT) { 29733e4c1a9SKrzysztof Opasiak spin_unlock_irqrestore(&hidg->read_spinlock, flags); 29800a2430fSAndrzej Pietrasiewicz if (file->f_flags & O_NONBLOCK) 29900a2430fSAndrzej Pietrasiewicz return -EAGAIN; 30000a2430fSAndrzej Pietrasiewicz 301*d7428bc2SMaxim Devaev if (wait_event_interruptible(hidg->read_queue, READ_COND_INTOUT)) 30200a2430fSAndrzej Pietrasiewicz return -ERESTARTSYS; 30300a2430fSAndrzej Pietrasiewicz 30433e4c1a9SKrzysztof Opasiak spin_lock_irqsave(&hidg->read_spinlock, flags); 30500a2430fSAndrzej Pietrasiewicz } 30600a2430fSAndrzej Pietrasiewicz 30700a2430fSAndrzej Pietrasiewicz /* pick the first one */ 30800a2430fSAndrzej Pietrasiewicz list = list_first_entry(&hidg->completed_out_req, 30900a2430fSAndrzej Pietrasiewicz struct f_hidg_req_list, list); 310aa65d11aSKrzysztof Opasiak 311aa65d11aSKrzysztof Opasiak /* 312aa65d11aSKrzysztof Opasiak * Remove this from list to protect it from beign free() 313aa65d11aSKrzysztof Opasiak * while host disables our function 314aa65d11aSKrzysztof Opasiak */ 315aa65d11aSKrzysztof Opasiak list_del(&list->list); 316aa65d11aSKrzysztof Opasiak 31700a2430fSAndrzej Pietrasiewicz req = list->req; 31800a2430fSAndrzej Pietrasiewicz count = min_t(unsigned int, count, req->actual - list->pos); 31933e4c1a9SKrzysztof Opasiak spin_unlock_irqrestore(&hidg->read_spinlock, flags); 32000a2430fSAndrzej Pietrasiewicz 32100a2430fSAndrzej Pietrasiewicz /* copy to user outside spinlock */ 32200a2430fSAndrzej Pietrasiewicz count -= copy_to_user(buffer, req->buf + list->pos, count); 32300a2430fSAndrzej Pietrasiewicz list->pos += count; 32400a2430fSAndrzej Pietrasiewicz 32500a2430fSAndrzej Pietrasiewicz /* 32600a2430fSAndrzej Pietrasiewicz * if this request is completely handled and transfered to 32700a2430fSAndrzej Pietrasiewicz * userspace, remove its entry from the list and requeue it 32800a2430fSAndrzej Pietrasiewicz * again. Otherwise, we will revisit it again upon the next 32900a2430fSAndrzej Pietrasiewicz * call, taking into account its current read position. 33000a2430fSAndrzej Pietrasiewicz */ 33100a2430fSAndrzej Pietrasiewicz if (list->pos == req->actual) { 33200a2430fSAndrzej Pietrasiewicz kfree(list); 33300a2430fSAndrzej Pietrasiewicz 33400a2430fSAndrzej Pietrasiewicz req->length = hidg->report_length; 33500a2430fSAndrzej Pietrasiewicz ret = usb_ep_queue(hidg->out_ep, req, GFP_KERNEL); 336aa65d11aSKrzysztof Opasiak if (ret < 0) { 337aa65d11aSKrzysztof Opasiak free_ep_req(hidg->out_ep, req); 33800a2430fSAndrzej Pietrasiewicz return ret; 33900a2430fSAndrzej Pietrasiewicz } 340aa65d11aSKrzysztof Opasiak } else { 34133e4c1a9SKrzysztof Opasiak spin_lock_irqsave(&hidg->read_spinlock, flags); 342aa65d11aSKrzysztof Opasiak list_add(&list->list, &hidg->completed_out_req); 34333e4c1a9SKrzysztof Opasiak spin_unlock_irqrestore(&hidg->read_spinlock, flags); 344aa65d11aSKrzysztof Opasiak 345aa65d11aSKrzysztof Opasiak wake_up(&hidg->read_queue); 346aa65d11aSKrzysztof Opasiak } 34700a2430fSAndrzej Pietrasiewicz 34800a2430fSAndrzej Pietrasiewicz return count; 34900a2430fSAndrzej Pietrasiewicz } 35000a2430fSAndrzej Pietrasiewicz 351*d7428bc2SMaxim Devaev #define READ_COND_SSREPORT (hidg->set_report_buf != NULL) 352*d7428bc2SMaxim Devaev 353*d7428bc2SMaxim Devaev static ssize_t f_hidg_ssreport_read(struct file *file, char __user *buffer, 354*d7428bc2SMaxim Devaev size_t count, loff_t *ptr) 355*d7428bc2SMaxim Devaev { 356*d7428bc2SMaxim Devaev struct f_hidg *hidg = file->private_data; 357*d7428bc2SMaxim Devaev char *tmp_buf = NULL; 358*d7428bc2SMaxim Devaev unsigned long flags; 359*d7428bc2SMaxim Devaev 360*d7428bc2SMaxim Devaev if (!count) 361*d7428bc2SMaxim Devaev return 0; 362*d7428bc2SMaxim Devaev 363*d7428bc2SMaxim Devaev spin_lock_irqsave(&hidg->read_spinlock, flags); 364*d7428bc2SMaxim Devaev 365*d7428bc2SMaxim Devaev while (!READ_COND_SSREPORT) { 366*d7428bc2SMaxim Devaev spin_unlock_irqrestore(&hidg->read_spinlock, flags); 367*d7428bc2SMaxim Devaev if (file->f_flags & O_NONBLOCK) 368*d7428bc2SMaxim Devaev return -EAGAIN; 369*d7428bc2SMaxim Devaev 370*d7428bc2SMaxim Devaev if (wait_event_interruptible(hidg->read_queue, READ_COND_SSREPORT)) 371*d7428bc2SMaxim Devaev return -ERESTARTSYS; 372*d7428bc2SMaxim Devaev 373*d7428bc2SMaxim Devaev spin_lock_irqsave(&hidg->read_spinlock, flags); 374*d7428bc2SMaxim Devaev } 375*d7428bc2SMaxim Devaev 376*d7428bc2SMaxim Devaev count = min_t(unsigned int, count, hidg->set_report_length); 377*d7428bc2SMaxim Devaev tmp_buf = hidg->set_report_buf; 378*d7428bc2SMaxim Devaev hidg->set_report_buf = NULL; 379*d7428bc2SMaxim Devaev 380*d7428bc2SMaxim Devaev spin_unlock_irqrestore(&hidg->read_spinlock, flags); 381*d7428bc2SMaxim Devaev 382*d7428bc2SMaxim Devaev if (tmp_buf != NULL) { 383*d7428bc2SMaxim Devaev count -= copy_to_user(buffer, tmp_buf, count); 384*d7428bc2SMaxim Devaev kfree(tmp_buf); 385*d7428bc2SMaxim Devaev } else { 386*d7428bc2SMaxim Devaev count = -ENOMEM; 387*d7428bc2SMaxim Devaev } 388*d7428bc2SMaxim Devaev 389*d7428bc2SMaxim Devaev wake_up(&hidg->read_queue); 390*d7428bc2SMaxim Devaev 391*d7428bc2SMaxim Devaev return count; 392*d7428bc2SMaxim Devaev } 393*d7428bc2SMaxim Devaev 394*d7428bc2SMaxim Devaev static ssize_t f_hidg_read(struct file *file, char __user *buffer, 395*d7428bc2SMaxim Devaev size_t count, loff_t *ptr) 396*d7428bc2SMaxim Devaev { 397*d7428bc2SMaxim Devaev struct f_hidg *hidg = file->private_data; 398*d7428bc2SMaxim Devaev 399*d7428bc2SMaxim Devaev if (hidg->use_out_ep) 400*d7428bc2SMaxim Devaev return f_hidg_intout_read(file, buffer, count, ptr); 401*d7428bc2SMaxim Devaev else 402*d7428bc2SMaxim Devaev return f_hidg_ssreport_read(file, buffer, count, ptr); 403*d7428bc2SMaxim Devaev } 404*d7428bc2SMaxim Devaev 40500a2430fSAndrzej Pietrasiewicz static void f_hidg_req_complete(struct usb_ep *ep, struct usb_request *req) 40600a2430fSAndrzej Pietrasiewicz { 40700a2430fSAndrzej Pietrasiewicz struct f_hidg *hidg = (struct f_hidg *)ep->driver_data; 40833e4c1a9SKrzysztof Opasiak unsigned long flags; 40900a2430fSAndrzej Pietrasiewicz 41000a2430fSAndrzej Pietrasiewicz if (req->status != 0) { 41100a2430fSAndrzej Pietrasiewicz ERROR(hidg->func.config->cdev, 41200a2430fSAndrzej Pietrasiewicz "End Point Request ERROR: %d\n", req->status); 41300a2430fSAndrzej Pietrasiewicz } 41400a2430fSAndrzej Pietrasiewicz 41533e4c1a9SKrzysztof Opasiak spin_lock_irqsave(&hidg->write_spinlock, flags); 41600a2430fSAndrzej Pietrasiewicz hidg->write_pending = 0; 41733e4c1a9SKrzysztof Opasiak spin_unlock_irqrestore(&hidg->write_spinlock, flags); 41800a2430fSAndrzej Pietrasiewicz wake_up(&hidg->write_queue); 41900a2430fSAndrzej Pietrasiewicz } 42000a2430fSAndrzej Pietrasiewicz 42100a2430fSAndrzej Pietrasiewicz static ssize_t f_hidg_write(struct file *file, const char __user *buffer, 42200a2430fSAndrzej Pietrasiewicz size_t count, loff_t *offp) 42300a2430fSAndrzej Pietrasiewicz { 42400a2430fSAndrzej Pietrasiewicz struct f_hidg *hidg = file->private_data; 425749494b6SKrzysztof Opasiak struct usb_request *req; 42633e4c1a9SKrzysztof Opasiak unsigned long flags; 42700a2430fSAndrzej Pietrasiewicz ssize_t status = -ENOMEM; 42800a2430fSAndrzej Pietrasiewicz 42933e4c1a9SKrzysztof Opasiak spin_lock_irqsave(&hidg->write_spinlock, flags); 43000a2430fSAndrzej Pietrasiewicz 4312867652eSPhil Elwell if (!hidg->req) { 4322867652eSPhil Elwell spin_unlock_irqrestore(&hidg->write_spinlock, flags); 4332867652eSPhil Elwell return -ESHUTDOWN; 4342867652eSPhil Elwell } 4352867652eSPhil Elwell 43600a2430fSAndrzej Pietrasiewicz #define WRITE_COND (!hidg->write_pending) 437749494b6SKrzysztof Opasiak try_again: 43800a2430fSAndrzej Pietrasiewicz /* write queue */ 43900a2430fSAndrzej Pietrasiewicz while (!WRITE_COND) { 44033e4c1a9SKrzysztof Opasiak spin_unlock_irqrestore(&hidg->write_spinlock, flags); 44100a2430fSAndrzej Pietrasiewicz if (file->f_flags & O_NONBLOCK) 44200a2430fSAndrzej Pietrasiewicz return -EAGAIN; 44300a2430fSAndrzej Pietrasiewicz 44400a2430fSAndrzej Pietrasiewicz if (wait_event_interruptible_exclusive( 44500a2430fSAndrzej Pietrasiewicz hidg->write_queue, WRITE_COND)) 44600a2430fSAndrzej Pietrasiewicz return -ERESTARTSYS; 44700a2430fSAndrzej Pietrasiewicz 44833e4c1a9SKrzysztof Opasiak spin_lock_irqsave(&hidg->write_spinlock, flags); 44900a2430fSAndrzej Pietrasiewicz } 45000a2430fSAndrzej Pietrasiewicz 45133e4c1a9SKrzysztof Opasiak hidg->write_pending = 1; 452749494b6SKrzysztof Opasiak req = hidg->req; 45300a2430fSAndrzej Pietrasiewicz count = min_t(unsigned, count, hidg->report_length); 45433e4c1a9SKrzysztof Opasiak 45533e4c1a9SKrzysztof Opasiak spin_unlock_irqrestore(&hidg->write_spinlock, flags); 45600a2430fSAndrzej Pietrasiewicz 4572867652eSPhil Elwell if (!req) { 4582867652eSPhil Elwell ERROR(hidg->func.config->cdev, "hidg->req is NULL\n"); 4592867652eSPhil Elwell status = -ESHUTDOWN; 4602867652eSPhil Elwell goto release_write_pending; 4612867652eSPhil Elwell } 4622867652eSPhil Elwell 4632867652eSPhil Elwell status = copy_from_user(req->buf, buffer, count); 46400a2430fSAndrzej Pietrasiewicz if (status != 0) { 46500a2430fSAndrzej Pietrasiewicz ERROR(hidg->func.config->cdev, 46600a2430fSAndrzej Pietrasiewicz "copy_from_user error\n"); 46733e4c1a9SKrzysztof Opasiak status = -EINVAL; 46833e4c1a9SKrzysztof Opasiak goto release_write_pending; 46900a2430fSAndrzej Pietrasiewicz } 47000a2430fSAndrzej Pietrasiewicz 471749494b6SKrzysztof Opasiak spin_lock_irqsave(&hidg->write_spinlock, flags); 472749494b6SKrzysztof Opasiak 47325cd9721SKrzysztof Opasiak /* when our function has been disabled by host */ 474749494b6SKrzysztof Opasiak if (!hidg->req) { 47525cd9721SKrzysztof Opasiak free_ep_req(hidg->in_ep, req); 476749494b6SKrzysztof Opasiak /* 477749494b6SKrzysztof Opasiak * TODO 478749494b6SKrzysztof Opasiak * Should we fail with error here? 479749494b6SKrzysztof Opasiak */ 480749494b6SKrzysztof Opasiak goto try_again; 481749494b6SKrzysztof Opasiak } 482749494b6SKrzysztof Opasiak 483749494b6SKrzysztof Opasiak req->status = 0; 484749494b6SKrzysztof Opasiak req->zero = 0; 485749494b6SKrzysztof Opasiak req->length = count; 486749494b6SKrzysztof Opasiak req->complete = f_hidg_req_complete; 487749494b6SKrzysztof Opasiak req->context = hidg; 48800a2430fSAndrzej Pietrasiewicz 489072684e8SRadoslav Gerganov spin_unlock_irqrestore(&hidg->write_spinlock, flags); 490072684e8SRadoslav Gerganov 4912867652eSPhil Elwell if (!hidg->in_ep->enabled) { 4922867652eSPhil Elwell ERROR(hidg->func.config->cdev, "in_ep is disabled\n"); 4932867652eSPhil Elwell status = -ESHUTDOWN; 494072684e8SRadoslav Gerganov goto release_write_pending; 49500a2430fSAndrzej Pietrasiewicz } 49600a2430fSAndrzej Pietrasiewicz 4972867652eSPhil Elwell status = usb_ep_queue(hidg->in_ep, req, GFP_ATOMIC); 4982867652eSPhil Elwell if (status < 0) 4992867652eSPhil Elwell goto release_write_pending; 5002867652eSPhil Elwell else 5012867652eSPhil Elwell status = count; 5022867652eSPhil Elwell 50333e4c1a9SKrzysztof Opasiak return status; 50433e4c1a9SKrzysztof Opasiak release_write_pending: 50533e4c1a9SKrzysztof Opasiak spin_lock_irqsave(&hidg->write_spinlock, flags); 50633e4c1a9SKrzysztof Opasiak hidg->write_pending = 0; 50733e4c1a9SKrzysztof Opasiak spin_unlock_irqrestore(&hidg->write_spinlock, flags); 50833e4c1a9SKrzysztof Opasiak 50933e4c1a9SKrzysztof Opasiak wake_up(&hidg->write_queue); 51000a2430fSAndrzej Pietrasiewicz 51100a2430fSAndrzej Pietrasiewicz return status; 51200a2430fSAndrzej Pietrasiewicz } 51300a2430fSAndrzej Pietrasiewicz 514afc9a42bSAl Viro static __poll_t f_hidg_poll(struct file *file, poll_table *wait) 51500a2430fSAndrzej Pietrasiewicz { 51600a2430fSAndrzej Pietrasiewicz struct f_hidg *hidg = file->private_data; 517afc9a42bSAl Viro __poll_t ret = 0; 51800a2430fSAndrzej Pietrasiewicz 51900a2430fSAndrzej Pietrasiewicz poll_wait(file, &hidg->read_queue, wait); 52000a2430fSAndrzej Pietrasiewicz poll_wait(file, &hidg->write_queue, wait); 52100a2430fSAndrzej Pietrasiewicz 52200a2430fSAndrzej Pietrasiewicz if (WRITE_COND) 523a9a08845SLinus Torvalds ret |= EPOLLOUT | EPOLLWRNORM; 52400a2430fSAndrzej Pietrasiewicz 525*d7428bc2SMaxim Devaev if (hidg->use_out_ep) { 526*d7428bc2SMaxim Devaev if (READ_COND_INTOUT) 527a9a08845SLinus Torvalds ret |= EPOLLIN | EPOLLRDNORM; 528*d7428bc2SMaxim Devaev } else { 529*d7428bc2SMaxim Devaev if (READ_COND_SSREPORT) 530*d7428bc2SMaxim Devaev ret |= EPOLLIN | EPOLLRDNORM; 531*d7428bc2SMaxim Devaev } 53200a2430fSAndrzej Pietrasiewicz 53300a2430fSAndrzej Pietrasiewicz return ret; 53400a2430fSAndrzej Pietrasiewicz } 53500a2430fSAndrzej Pietrasiewicz 53600a2430fSAndrzej Pietrasiewicz #undef WRITE_COND 537*d7428bc2SMaxim Devaev #undef READ_COND_SSREPORT 538*d7428bc2SMaxim Devaev #undef READ_COND_INTOUT 53900a2430fSAndrzej Pietrasiewicz 54000a2430fSAndrzej Pietrasiewicz static int f_hidg_release(struct inode *inode, struct file *fd) 54100a2430fSAndrzej Pietrasiewicz { 54200a2430fSAndrzej Pietrasiewicz fd->private_data = NULL; 54300a2430fSAndrzej Pietrasiewicz return 0; 54400a2430fSAndrzej Pietrasiewicz } 54500a2430fSAndrzej Pietrasiewicz 54600a2430fSAndrzej Pietrasiewicz static int f_hidg_open(struct inode *inode, struct file *fd) 54700a2430fSAndrzej Pietrasiewicz { 54800a2430fSAndrzej Pietrasiewicz struct f_hidg *hidg = 54900a2430fSAndrzej Pietrasiewicz container_of(inode->i_cdev, struct f_hidg, cdev); 55000a2430fSAndrzej Pietrasiewicz 55100a2430fSAndrzej Pietrasiewicz fd->private_data = hidg; 55200a2430fSAndrzej Pietrasiewicz 55300a2430fSAndrzej Pietrasiewicz return 0; 55400a2430fSAndrzej Pietrasiewicz } 55500a2430fSAndrzej Pietrasiewicz 55600a2430fSAndrzej Pietrasiewicz /*-------------------------------------------------------------------------*/ 55700a2430fSAndrzej Pietrasiewicz /* usb_function */ 55800a2430fSAndrzej Pietrasiewicz 55900a2430fSAndrzej Pietrasiewicz static inline struct usb_request *hidg_alloc_ep_req(struct usb_ep *ep, 56000a2430fSAndrzej Pietrasiewicz unsigned length) 56100a2430fSAndrzej Pietrasiewicz { 562aadbe812SFelipe F. Tonello return alloc_ep_req(ep, length); 56300a2430fSAndrzej Pietrasiewicz } 56400a2430fSAndrzej Pietrasiewicz 565*d7428bc2SMaxim Devaev static void hidg_intout_complete(struct usb_ep *ep, struct usb_request *req) 56600a2430fSAndrzej Pietrasiewicz { 56700a2430fSAndrzej Pietrasiewicz struct f_hidg *hidg = (struct f_hidg *) req->context; 56820d2ca95SKrzysztof Opasiak struct usb_composite_dev *cdev = hidg->func.config->cdev; 56900a2430fSAndrzej Pietrasiewicz struct f_hidg_req_list *req_list; 57000a2430fSAndrzej Pietrasiewicz unsigned long flags; 57100a2430fSAndrzej Pietrasiewicz 57220d2ca95SKrzysztof Opasiak switch (req->status) { 57320d2ca95SKrzysztof Opasiak case 0: 57400a2430fSAndrzej Pietrasiewicz req_list = kzalloc(sizeof(*req_list), GFP_ATOMIC); 57520d2ca95SKrzysztof Opasiak if (!req_list) { 57620d2ca95SKrzysztof Opasiak ERROR(cdev, "Unable to allocate mem for req_list\n"); 57720d2ca95SKrzysztof Opasiak goto free_req; 57820d2ca95SKrzysztof Opasiak } 57900a2430fSAndrzej Pietrasiewicz 58000a2430fSAndrzej Pietrasiewicz req_list->req = req; 58100a2430fSAndrzej Pietrasiewicz 58233e4c1a9SKrzysztof Opasiak spin_lock_irqsave(&hidg->read_spinlock, flags); 58300a2430fSAndrzej Pietrasiewicz list_add_tail(&req_list->list, &hidg->completed_out_req); 58433e4c1a9SKrzysztof Opasiak spin_unlock_irqrestore(&hidg->read_spinlock, flags); 58500a2430fSAndrzej Pietrasiewicz 58600a2430fSAndrzej Pietrasiewicz wake_up(&hidg->read_queue); 58720d2ca95SKrzysztof Opasiak break; 58820d2ca95SKrzysztof Opasiak default: 58920d2ca95SKrzysztof Opasiak ERROR(cdev, "Set report failed %d\n", req->status); 590a74005abSGustavo A. R. Silva fallthrough; 59120d2ca95SKrzysztof Opasiak case -ECONNABORTED: /* hardware forced ep reset */ 59220d2ca95SKrzysztof Opasiak case -ECONNRESET: /* request dequeued */ 59320d2ca95SKrzysztof Opasiak case -ESHUTDOWN: /* disconnect from host */ 59420d2ca95SKrzysztof Opasiak free_req: 59520d2ca95SKrzysztof Opasiak free_ep_req(ep, req); 59620d2ca95SKrzysztof Opasiak return; 59720d2ca95SKrzysztof Opasiak } 59800a2430fSAndrzej Pietrasiewicz } 59900a2430fSAndrzej Pietrasiewicz 600*d7428bc2SMaxim Devaev static void hidg_ssreport_complete(struct usb_ep *ep, struct usb_request *req) 601*d7428bc2SMaxim Devaev { 602*d7428bc2SMaxim Devaev struct f_hidg *hidg = (struct f_hidg *)req->context; 603*d7428bc2SMaxim Devaev struct usb_composite_dev *cdev = hidg->func.config->cdev; 604*d7428bc2SMaxim Devaev char *new_buf = NULL; 605*d7428bc2SMaxim Devaev unsigned long flags; 606*d7428bc2SMaxim Devaev 607*d7428bc2SMaxim Devaev if (req->status != 0 || req->buf == NULL || req->actual == 0) { 608*d7428bc2SMaxim Devaev ERROR(cdev, 609*d7428bc2SMaxim Devaev "%s FAILED: status=%d, buf=%p, actual=%d\n", 610*d7428bc2SMaxim Devaev __func__, req->status, req->buf, req->actual); 611*d7428bc2SMaxim Devaev return; 612*d7428bc2SMaxim Devaev } 613*d7428bc2SMaxim Devaev 614*d7428bc2SMaxim Devaev spin_lock_irqsave(&hidg->read_spinlock, flags); 615*d7428bc2SMaxim Devaev 616*d7428bc2SMaxim Devaev new_buf = krealloc(hidg->set_report_buf, req->actual, GFP_ATOMIC); 617*d7428bc2SMaxim Devaev if (new_buf == NULL) { 618*d7428bc2SMaxim Devaev spin_unlock_irqrestore(&hidg->read_spinlock, flags); 619*d7428bc2SMaxim Devaev return; 620*d7428bc2SMaxim Devaev } 621*d7428bc2SMaxim Devaev hidg->set_report_buf = new_buf; 622*d7428bc2SMaxim Devaev 623*d7428bc2SMaxim Devaev hidg->set_report_length = req->actual; 624*d7428bc2SMaxim Devaev memcpy(hidg->set_report_buf, req->buf, req->actual); 625*d7428bc2SMaxim Devaev 626*d7428bc2SMaxim Devaev spin_unlock_irqrestore(&hidg->read_spinlock, flags); 627*d7428bc2SMaxim Devaev 628*d7428bc2SMaxim Devaev wake_up(&hidg->read_queue); 629*d7428bc2SMaxim Devaev } 630*d7428bc2SMaxim Devaev 63100a2430fSAndrzej Pietrasiewicz static int hidg_setup(struct usb_function *f, 63200a2430fSAndrzej Pietrasiewicz const struct usb_ctrlrequest *ctrl) 63300a2430fSAndrzej Pietrasiewicz { 63400a2430fSAndrzej Pietrasiewicz struct f_hidg *hidg = func_to_hidg(f); 63500a2430fSAndrzej Pietrasiewicz struct usb_composite_dev *cdev = f->config->cdev; 63600a2430fSAndrzej Pietrasiewicz struct usb_request *req = cdev->req; 63700a2430fSAndrzej Pietrasiewicz int status = 0; 63800a2430fSAndrzej Pietrasiewicz __u16 value, length; 63900a2430fSAndrzej Pietrasiewicz 64000a2430fSAndrzej Pietrasiewicz value = __le16_to_cpu(ctrl->wValue); 64100a2430fSAndrzej Pietrasiewicz length = __le16_to_cpu(ctrl->wLength); 64200a2430fSAndrzej Pietrasiewicz 643c9b3bde0SJulia Lawall VDBG(cdev, 644c9b3bde0SJulia Lawall "%s crtl_request : bRequestType:0x%x bRequest:0x%x Value:0x%x\n", 645c9b3bde0SJulia Lawall __func__, ctrl->bRequestType, ctrl->bRequest, value); 64600a2430fSAndrzej Pietrasiewicz 64700a2430fSAndrzej Pietrasiewicz switch ((ctrl->bRequestType << 8) | ctrl->bRequest) { 64800a2430fSAndrzej Pietrasiewicz case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8 64900a2430fSAndrzej Pietrasiewicz | HID_REQ_GET_REPORT): 65000a2430fSAndrzej Pietrasiewicz VDBG(cdev, "get_report\n"); 65100a2430fSAndrzej Pietrasiewicz 65200a2430fSAndrzej Pietrasiewicz /* send an empty report */ 65300a2430fSAndrzej Pietrasiewicz length = min_t(unsigned, length, hidg->report_length); 65400a2430fSAndrzej Pietrasiewicz memset(req->buf, 0x0, length); 65500a2430fSAndrzej Pietrasiewicz 65600a2430fSAndrzej Pietrasiewicz goto respond; 65700a2430fSAndrzej Pietrasiewicz break; 65800a2430fSAndrzej Pietrasiewicz 65900a2430fSAndrzej Pietrasiewicz case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8 66000a2430fSAndrzej Pietrasiewicz | HID_REQ_GET_PROTOCOL): 66100a2430fSAndrzej Pietrasiewicz VDBG(cdev, "get_protocol\n"); 662b3c4ec71SAbdulhadi Mohamed length = min_t(unsigned int, length, 1); 663b3c4ec71SAbdulhadi Mohamed ((u8 *) req->buf)[0] = hidg->protocol; 664b3c4ec71SAbdulhadi Mohamed goto respond; 66500a2430fSAndrzej Pietrasiewicz break; 66600a2430fSAndrzej Pietrasiewicz 667afcff6dcSMaxim Devaev case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8 668afcff6dcSMaxim Devaev | HID_REQ_GET_IDLE): 669afcff6dcSMaxim Devaev VDBG(cdev, "get_idle\n"); 670afcff6dcSMaxim Devaev length = min_t(unsigned int, length, 1); 671afcff6dcSMaxim Devaev ((u8 *) req->buf)[0] = hidg->idle; 672afcff6dcSMaxim Devaev goto respond; 673afcff6dcSMaxim Devaev break; 674afcff6dcSMaxim Devaev 67500a2430fSAndrzej Pietrasiewicz case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8 67600a2430fSAndrzej Pietrasiewicz | HID_REQ_SET_REPORT): 6776774def6SMasanari Iida VDBG(cdev, "set_report | wLength=%d\n", ctrl->wLength); 678*d7428bc2SMaxim Devaev if (hidg->use_out_ep) 67900a2430fSAndrzej Pietrasiewicz goto stall; 680*d7428bc2SMaxim Devaev req->complete = hidg_ssreport_complete; 681*d7428bc2SMaxim Devaev req->context = hidg; 682*d7428bc2SMaxim Devaev goto respond; 68300a2430fSAndrzej Pietrasiewicz break; 68400a2430fSAndrzej Pietrasiewicz 68500a2430fSAndrzej Pietrasiewicz case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8 68600a2430fSAndrzej Pietrasiewicz | HID_REQ_SET_PROTOCOL): 68700a2430fSAndrzej Pietrasiewicz VDBG(cdev, "set_protocol\n"); 688b3c4ec71SAbdulhadi Mohamed if (value > HID_REPORT_PROTOCOL) 689b3c4ec71SAbdulhadi Mohamed goto stall; 690b3c4ec71SAbdulhadi Mohamed length = 0; 691b3c4ec71SAbdulhadi Mohamed /* 692b3c4ec71SAbdulhadi Mohamed * We assume that programs implementing the Boot protocol 693b3c4ec71SAbdulhadi Mohamed * are also compatible with the Report Protocol 694b3c4ec71SAbdulhadi Mohamed */ 695b3c4ec71SAbdulhadi Mohamed if (hidg->bInterfaceSubClass == USB_INTERFACE_SUBCLASS_BOOT) { 696b3c4ec71SAbdulhadi Mohamed hidg->protocol = value; 697b3c4ec71SAbdulhadi Mohamed goto respond; 698b3c4ec71SAbdulhadi Mohamed } 69900a2430fSAndrzej Pietrasiewicz goto stall; 70000a2430fSAndrzej Pietrasiewicz break; 70100a2430fSAndrzej Pietrasiewicz 702afcff6dcSMaxim Devaev case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8 703afcff6dcSMaxim Devaev | HID_REQ_SET_IDLE): 704afcff6dcSMaxim Devaev VDBG(cdev, "set_idle\n"); 705afcff6dcSMaxim Devaev length = 0; 706fa20badaSMaxim Devaev hidg->idle = value >> 8; 707afcff6dcSMaxim Devaev goto respond; 708afcff6dcSMaxim Devaev break; 709afcff6dcSMaxim Devaev 71000a2430fSAndrzej Pietrasiewicz case ((USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_INTERFACE) << 8 71100a2430fSAndrzej Pietrasiewicz | USB_REQ_GET_DESCRIPTOR): 71200a2430fSAndrzej Pietrasiewicz switch (value >> 8) { 71300a2430fSAndrzej Pietrasiewicz case HID_DT_HID: 714f286d487SKrzysztof Opasiak { 715f286d487SKrzysztof Opasiak struct hid_descriptor hidg_desc_copy = hidg_desc; 716f286d487SKrzysztof Opasiak 71700a2430fSAndrzej Pietrasiewicz VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: HID\n"); 718f286d487SKrzysztof Opasiak hidg_desc_copy.desc[0].bDescriptorType = HID_DT_REPORT; 719f286d487SKrzysztof Opasiak hidg_desc_copy.desc[0].wDescriptorLength = 720f286d487SKrzysztof Opasiak cpu_to_le16(hidg->report_desc_length); 721f286d487SKrzysztof Opasiak 72200a2430fSAndrzej Pietrasiewicz length = min_t(unsigned short, length, 723f286d487SKrzysztof Opasiak hidg_desc_copy.bLength); 724f286d487SKrzysztof Opasiak memcpy(req->buf, &hidg_desc_copy, length); 72500a2430fSAndrzej Pietrasiewicz goto respond; 72600a2430fSAndrzej Pietrasiewicz break; 727f286d487SKrzysztof Opasiak } 72800a2430fSAndrzej Pietrasiewicz case HID_DT_REPORT: 72900a2430fSAndrzej Pietrasiewicz VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: REPORT\n"); 73000a2430fSAndrzej Pietrasiewicz length = min_t(unsigned short, length, 73100a2430fSAndrzej Pietrasiewicz hidg->report_desc_length); 73200a2430fSAndrzej Pietrasiewicz memcpy(req->buf, hidg->report_desc, length); 73300a2430fSAndrzej Pietrasiewicz goto respond; 73400a2430fSAndrzej Pietrasiewicz break; 73500a2430fSAndrzej Pietrasiewicz 73600a2430fSAndrzej Pietrasiewicz default: 73700a2430fSAndrzej Pietrasiewicz VDBG(cdev, "Unknown descriptor request 0x%x\n", 73800a2430fSAndrzej Pietrasiewicz value >> 8); 73900a2430fSAndrzej Pietrasiewicz goto stall; 74000a2430fSAndrzej Pietrasiewicz break; 74100a2430fSAndrzej Pietrasiewicz } 74200a2430fSAndrzej Pietrasiewicz break; 74300a2430fSAndrzej Pietrasiewicz 74400a2430fSAndrzej Pietrasiewicz default: 74500a2430fSAndrzej Pietrasiewicz VDBG(cdev, "Unknown request 0x%x\n", 74600a2430fSAndrzej Pietrasiewicz ctrl->bRequest); 74700a2430fSAndrzej Pietrasiewicz goto stall; 74800a2430fSAndrzej Pietrasiewicz break; 74900a2430fSAndrzej Pietrasiewicz } 75000a2430fSAndrzej Pietrasiewicz 75100a2430fSAndrzej Pietrasiewicz stall: 75200a2430fSAndrzej Pietrasiewicz return -EOPNOTSUPP; 75300a2430fSAndrzej Pietrasiewicz 75400a2430fSAndrzej Pietrasiewicz respond: 75500a2430fSAndrzej Pietrasiewicz req->zero = 0; 75600a2430fSAndrzej Pietrasiewicz req->length = length; 75700a2430fSAndrzej Pietrasiewicz status = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC); 75800a2430fSAndrzej Pietrasiewicz if (status < 0) 75900a2430fSAndrzej Pietrasiewicz ERROR(cdev, "usb_ep_queue error on ep0 %d\n", value); 76000a2430fSAndrzej Pietrasiewicz return status; 76100a2430fSAndrzej Pietrasiewicz } 76200a2430fSAndrzej Pietrasiewicz 76300a2430fSAndrzej Pietrasiewicz static void hidg_disable(struct usb_function *f) 76400a2430fSAndrzej Pietrasiewicz { 76500a2430fSAndrzej Pietrasiewicz struct f_hidg *hidg = func_to_hidg(f); 76600a2430fSAndrzej Pietrasiewicz struct f_hidg_req_list *list, *next; 767aa65d11aSKrzysztof Opasiak unsigned long flags; 76800a2430fSAndrzej Pietrasiewicz 76900a2430fSAndrzej Pietrasiewicz usb_ep_disable(hidg->in_ep); 770*d7428bc2SMaxim Devaev 771*d7428bc2SMaxim Devaev if (hidg->out_ep) { 77200a2430fSAndrzej Pietrasiewicz usb_ep_disable(hidg->out_ep); 77300a2430fSAndrzej Pietrasiewicz 77433e4c1a9SKrzysztof Opasiak spin_lock_irqsave(&hidg->read_spinlock, flags); 77500a2430fSAndrzej Pietrasiewicz list_for_each_entry_safe(list, next, &hidg->completed_out_req, list) { 776aa65d11aSKrzysztof Opasiak free_ep_req(hidg->out_ep, list->req); 77700a2430fSAndrzej Pietrasiewicz list_del(&list->list); 77800a2430fSAndrzej Pietrasiewicz kfree(list); 77900a2430fSAndrzej Pietrasiewicz } 78033e4c1a9SKrzysztof Opasiak spin_unlock_irqrestore(&hidg->read_spinlock, flags); 781*d7428bc2SMaxim Devaev } 782749494b6SKrzysztof Opasiak 783749494b6SKrzysztof Opasiak spin_lock_irqsave(&hidg->write_spinlock, flags); 784749494b6SKrzysztof Opasiak if (!hidg->write_pending) { 785749494b6SKrzysztof Opasiak free_ep_req(hidg->in_ep, hidg->req); 786749494b6SKrzysztof Opasiak hidg->write_pending = 1; 787749494b6SKrzysztof Opasiak } 788749494b6SKrzysztof Opasiak 789749494b6SKrzysztof Opasiak hidg->req = NULL; 790749494b6SKrzysztof Opasiak spin_unlock_irqrestore(&hidg->write_spinlock, flags); 79100a2430fSAndrzej Pietrasiewicz } 79200a2430fSAndrzej Pietrasiewicz 79300a2430fSAndrzej Pietrasiewicz static int hidg_set_alt(struct usb_function *f, unsigned intf, unsigned alt) 79400a2430fSAndrzej Pietrasiewicz { 79500a2430fSAndrzej Pietrasiewicz struct usb_composite_dev *cdev = f->config->cdev; 79600a2430fSAndrzej Pietrasiewicz struct f_hidg *hidg = func_to_hidg(f); 797749494b6SKrzysztof Opasiak struct usb_request *req_in = NULL; 798749494b6SKrzysztof Opasiak unsigned long flags; 79900a2430fSAndrzej Pietrasiewicz int i, status = 0; 80000a2430fSAndrzej Pietrasiewicz 80100a2430fSAndrzej Pietrasiewicz VDBG(cdev, "hidg_set_alt intf:%d alt:%d\n", intf, alt); 80200a2430fSAndrzej Pietrasiewicz 80300a2430fSAndrzej Pietrasiewicz if (hidg->in_ep != NULL) { 80400a2430fSAndrzej Pietrasiewicz /* restart endpoint */ 80500a2430fSAndrzej Pietrasiewicz usb_ep_disable(hidg->in_ep); 80600a2430fSAndrzej Pietrasiewicz 80700a2430fSAndrzej Pietrasiewicz status = config_ep_by_speed(f->config->cdev->gadget, f, 80800a2430fSAndrzej Pietrasiewicz hidg->in_ep); 80900a2430fSAndrzej Pietrasiewicz if (status) { 81000a2430fSAndrzej Pietrasiewicz ERROR(cdev, "config_ep_by_speed FAILED!\n"); 81100a2430fSAndrzej Pietrasiewicz goto fail; 81200a2430fSAndrzej Pietrasiewicz } 81300a2430fSAndrzej Pietrasiewicz status = usb_ep_enable(hidg->in_ep); 81400a2430fSAndrzej Pietrasiewicz if (status < 0) { 81500a2430fSAndrzej Pietrasiewicz ERROR(cdev, "Enable IN endpoint FAILED!\n"); 81600a2430fSAndrzej Pietrasiewicz goto fail; 81700a2430fSAndrzej Pietrasiewicz } 81800a2430fSAndrzej Pietrasiewicz hidg->in_ep->driver_data = hidg; 819749494b6SKrzysztof Opasiak 820749494b6SKrzysztof Opasiak req_in = hidg_alloc_ep_req(hidg->in_ep, hidg->report_length); 821749494b6SKrzysztof Opasiak if (!req_in) { 822749494b6SKrzysztof Opasiak status = -ENOMEM; 823749494b6SKrzysztof Opasiak goto disable_ep_in; 824749494b6SKrzysztof Opasiak } 82500a2430fSAndrzej Pietrasiewicz } 82600a2430fSAndrzej Pietrasiewicz 827*d7428bc2SMaxim Devaev if (hidg->use_out_ep && hidg->out_ep != NULL) { 82800a2430fSAndrzej Pietrasiewicz /* restart endpoint */ 82900a2430fSAndrzej Pietrasiewicz usb_ep_disable(hidg->out_ep); 83000a2430fSAndrzej Pietrasiewicz 83100a2430fSAndrzej Pietrasiewicz status = config_ep_by_speed(f->config->cdev->gadget, f, 83200a2430fSAndrzej Pietrasiewicz hidg->out_ep); 83300a2430fSAndrzej Pietrasiewicz if (status) { 83400a2430fSAndrzej Pietrasiewicz ERROR(cdev, "config_ep_by_speed FAILED!\n"); 835749494b6SKrzysztof Opasiak goto free_req_in; 83600a2430fSAndrzej Pietrasiewicz } 83700a2430fSAndrzej Pietrasiewicz status = usb_ep_enable(hidg->out_ep); 83800a2430fSAndrzej Pietrasiewicz if (status < 0) { 83943aef5c2SDavid Lechner ERROR(cdev, "Enable OUT endpoint FAILED!\n"); 840749494b6SKrzysztof Opasiak goto free_req_in; 84100a2430fSAndrzej Pietrasiewicz } 84200a2430fSAndrzej Pietrasiewicz hidg->out_ep->driver_data = hidg; 84300a2430fSAndrzej Pietrasiewicz 84400a2430fSAndrzej Pietrasiewicz /* 84500a2430fSAndrzej Pietrasiewicz * allocate a bunch of read buffers and queue them all at once. 84600a2430fSAndrzej Pietrasiewicz */ 84700a2430fSAndrzej Pietrasiewicz for (i = 0; i < hidg->qlen && status == 0; i++) { 84800a2430fSAndrzej Pietrasiewicz struct usb_request *req = 84900a2430fSAndrzej Pietrasiewicz hidg_alloc_ep_req(hidg->out_ep, 85000a2430fSAndrzej Pietrasiewicz hidg->report_length); 85100a2430fSAndrzej Pietrasiewicz if (req) { 852*d7428bc2SMaxim Devaev req->complete = hidg_intout_complete; 85300a2430fSAndrzej Pietrasiewicz req->context = hidg; 85400a2430fSAndrzej Pietrasiewicz status = usb_ep_queue(hidg->out_ep, req, 85500a2430fSAndrzej Pietrasiewicz GFP_ATOMIC); 856749494b6SKrzysztof Opasiak if (status) { 85700a2430fSAndrzej Pietrasiewicz ERROR(cdev, "%s queue req --> %d\n", 85800a2430fSAndrzej Pietrasiewicz hidg->out_ep->name, status); 859749494b6SKrzysztof Opasiak free_ep_req(hidg->out_ep, req); 860749494b6SKrzysztof Opasiak } 86100a2430fSAndrzej Pietrasiewicz } else { 86200a2430fSAndrzej Pietrasiewicz status = -ENOMEM; 863749494b6SKrzysztof Opasiak goto disable_out_ep; 86400a2430fSAndrzej Pietrasiewicz } 86500a2430fSAndrzej Pietrasiewicz } 86600a2430fSAndrzej Pietrasiewicz } 86700a2430fSAndrzej Pietrasiewicz 868749494b6SKrzysztof Opasiak if (hidg->in_ep != NULL) { 869749494b6SKrzysztof Opasiak spin_lock_irqsave(&hidg->write_spinlock, flags); 870749494b6SKrzysztof Opasiak hidg->req = req_in; 871749494b6SKrzysztof Opasiak hidg->write_pending = 0; 872749494b6SKrzysztof Opasiak spin_unlock_irqrestore(&hidg->write_spinlock, flags); 873749494b6SKrzysztof Opasiak 874749494b6SKrzysztof Opasiak wake_up(&hidg->write_queue); 875749494b6SKrzysztof Opasiak } 876749494b6SKrzysztof Opasiak return 0; 877749494b6SKrzysztof Opasiak disable_out_ep: 878*d7428bc2SMaxim Devaev if (hidg->out_ep) 879749494b6SKrzysztof Opasiak usb_ep_disable(hidg->out_ep); 880749494b6SKrzysztof Opasiak free_req_in: 881749494b6SKrzysztof Opasiak if (req_in) 882749494b6SKrzysztof Opasiak free_ep_req(hidg->in_ep, req_in); 883749494b6SKrzysztof Opasiak 884749494b6SKrzysztof Opasiak disable_ep_in: 885749494b6SKrzysztof Opasiak if (hidg->in_ep) 886749494b6SKrzysztof Opasiak usb_ep_disable(hidg->in_ep); 887749494b6SKrzysztof Opasiak 88800a2430fSAndrzej Pietrasiewicz fail: 88900a2430fSAndrzej Pietrasiewicz return status; 89000a2430fSAndrzej Pietrasiewicz } 89100a2430fSAndrzej Pietrasiewicz 8927a3cc461SLad, Prabhakar static const struct file_operations f_hidg_fops = { 89300a2430fSAndrzej Pietrasiewicz .owner = THIS_MODULE, 89400a2430fSAndrzej Pietrasiewicz .open = f_hidg_open, 89500a2430fSAndrzej Pietrasiewicz .release = f_hidg_release, 89600a2430fSAndrzej Pietrasiewicz .write = f_hidg_write, 89700a2430fSAndrzej Pietrasiewicz .read = f_hidg_read, 89800a2430fSAndrzej Pietrasiewicz .poll = f_hidg_poll, 89900a2430fSAndrzej Pietrasiewicz .llseek = noop_llseek, 90000a2430fSAndrzej Pietrasiewicz }; 90100a2430fSAndrzej Pietrasiewicz 902cb382536SAndrzej Pietrasiewicz static int hidg_bind(struct usb_configuration *c, struct usb_function *f) 90300a2430fSAndrzej Pietrasiewicz { 90400a2430fSAndrzej Pietrasiewicz struct usb_ep *ep; 90500a2430fSAndrzej Pietrasiewicz struct f_hidg *hidg = func_to_hidg(f); 9065ca8d3ecSAndrzej Pietrasiewicz struct usb_string *us; 90763406087SAndrzej Pietrasiewicz struct device *device; 90800a2430fSAndrzej Pietrasiewicz int status; 90900a2430fSAndrzej Pietrasiewicz dev_t dev; 91000a2430fSAndrzej Pietrasiewicz 911cb382536SAndrzej Pietrasiewicz /* maybe allocate device-global string IDs, and patch descriptors */ 9125ca8d3ecSAndrzej Pietrasiewicz us = usb_gstrings_attach(c->cdev, ct_func_strings, 9135ca8d3ecSAndrzej Pietrasiewicz ARRAY_SIZE(ct_func_string_defs)); 9145ca8d3ecSAndrzej Pietrasiewicz if (IS_ERR(us)) 9155ca8d3ecSAndrzej Pietrasiewicz return PTR_ERR(us); 9165ca8d3ecSAndrzej Pietrasiewicz hidg_interface_desc.iInterface = us[CT_FUNC_HID_IDX].id; 917cb382536SAndrzej Pietrasiewicz 91800a2430fSAndrzej Pietrasiewicz /* allocate instance-specific interface IDs, and patch descriptors */ 91900a2430fSAndrzej Pietrasiewicz status = usb_interface_id(c, f); 92000a2430fSAndrzej Pietrasiewicz if (status < 0) 92100a2430fSAndrzej Pietrasiewicz goto fail; 92200a2430fSAndrzej Pietrasiewicz hidg_interface_desc.bInterfaceNumber = status; 92300a2430fSAndrzej Pietrasiewicz 92400a2430fSAndrzej Pietrasiewicz /* allocate instance-specific endpoints */ 92500a2430fSAndrzej Pietrasiewicz status = -ENODEV; 92600a2430fSAndrzej Pietrasiewicz ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_in_ep_desc); 92700a2430fSAndrzej Pietrasiewicz if (!ep) 92800a2430fSAndrzej Pietrasiewicz goto fail; 92900a2430fSAndrzej Pietrasiewicz hidg->in_ep = ep; 93000a2430fSAndrzej Pietrasiewicz 931*d7428bc2SMaxim Devaev hidg->out_ep = NULL; 932*d7428bc2SMaxim Devaev if (hidg->use_out_ep) { 93300a2430fSAndrzej Pietrasiewicz ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_out_ep_desc); 93400a2430fSAndrzej Pietrasiewicz if (!ep) 93500a2430fSAndrzej Pietrasiewicz goto fail; 93600a2430fSAndrzej Pietrasiewicz hidg->out_ep = ep; 937*d7428bc2SMaxim Devaev } 938*d7428bc2SMaxim Devaev 939*d7428bc2SMaxim Devaev /* used only if use_out_ep == 1 */ 940*d7428bc2SMaxim Devaev hidg->set_report_buf = NULL; 94100a2430fSAndrzej Pietrasiewicz 94200a2430fSAndrzej Pietrasiewicz /* set descriptor dynamic values */ 94300a2430fSAndrzej Pietrasiewicz hidg_interface_desc.bInterfaceSubClass = hidg->bInterfaceSubClass; 94400a2430fSAndrzej Pietrasiewicz hidg_interface_desc.bInterfaceProtocol = hidg->bInterfaceProtocol; 945*d7428bc2SMaxim Devaev hidg_interface_desc.bNumEndpoints = hidg->use_out_ep ? 2 : 1; 946b3c4ec71SAbdulhadi Mohamed hidg->protocol = HID_REPORT_PROTOCOL; 947afcff6dcSMaxim Devaev hidg->idle = 1; 948dbf499cfSJanusz Dziedzic hidg_ss_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length); 949dbf499cfSJanusz Dziedzic hidg_ss_in_comp_desc.wBytesPerInterval = 950dbf499cfSJanusz Dziedzic cpu_to_le16(hidg->report_length); 95100a2430fSAndrzej Pietrasiewicz hidg_hs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length); 95200a2430fSAndrzej Pietrasiewicz hidg_fs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length); 953dbf499cfSJanusz Dziedzic hidg_ss_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length); 954dbf499cfSJanusz Dziedzic hidg_ss_out_comp_desc.wBytesPerInterval = 955dbf499cfSJanusz Dziedzic cpu_to_le16(hidg->report_length); 95600a2430fSAndrzej Pietrasiewicz hidg_hs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length); 95700a2430fSAndrzej Pietrasiewicz hidg_fs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length); 958f286d487SKrzysztof Opasiak /* 959f286d487SKrzysztof Opasiak * We can use hidg_desc struct here but we should not relay 960f286d487SKrzysztof Opasiak * that its content won't change after returning from this function. 961f286d487SKrzysztof Opasiak */ 96200a2430fSAndrzej Pietrasiewicz hidg_desc.desc[0].bDescriptorType = HID_DT_REPORT; 96300a2430fSAndrzej Pietrasiewicz hidg_desc.desc[0].wDescriptorLength = 96400a2430fSAndrzej Pietrasiewicz cpu_to_le16(hidg->report_desc_length); 96500a2430fSAndrzej Pietrasiewicz 96600a2430fSAndrzej Pietrasiewicz hidg_hs_in_ep_desc.bEndpointAddress = 96700a2430fSAndrzej Pietrasiewicz hidg_fs_in_ep_desc.bEndpointAddress; 96800a2430fSAndrzej Pietrasiewicz hidg_hs_out_ep_desc.bEndpointAddress = 96900a2430fSAndrzej Pietrasiewicz hidg_fs_out_ep_desc.bEndpointAddress; 97000a2430fSAndrzej Pietrasiewicz 971dbf499cfSJanusz Dziedzic hidg_ss_in_ep_desc.bEndpointAddress = 972dbf499cfSJanusz Dziedzic hidg_fs_in_ep_desc.bEndpointAddress; 973dbf499cfSJanusz Dziedzic hidg_ss_out_ep_desc.bEndpointAddress = 974dbf499cfSJanusz Dziedzic hidg_fs_out_ep_desc.bEndpointAddress; 975dbf499cfSJanusz Dziedzic 976*d7428bc2SMaxim Devaev if (hidg->use_out_ep) 977*d7428bc2SMaxim Devaev status = usb_assign_descriptors(f, 978*d7428bc2SMaxim Devaev hidg_fs_descriptors_intout, 979*d7428bc2SMaxim Devaev hidg_hs_descriptors_intout, 980*d7428bc2SMaxim Devaev hidg_ss_descriptors_intout, 981*d7428bc2SMaxim Devaev hidg_ss_descriptors_intout); 982*d7428bc2SMaxim Devaev else 983*d7428bc2SMaxim Devaev status = usb_assign_descriptors(f, 984*d7428bc2SMaxim Devaev hidg_fs_descriptors_ssreport, 985*d7428bc2SMaxim Devaev hidg_hs_descriptors_ssreport, 986*d7428bc2SMaxim Devaev hidg_ss_descriptors_ssreport, 987*d7428bc2SMaxim Devaev hidg_ss_descriptors_ssreport); 988*d7428bc2SMaxim Devaev 98900a2430fSAndrzej Pietrasiewicz if (status) 99000a2430fSAndrzej Pietrasiewicz goto fail; 99100a2430fSAndrzej Pietrasiewicz 99233e4c1a9SKrzysztof Opasiak spin_lock_init(&hidg->write_spinlock); 993749494b6SKrzysztof Opasiak hidg->write_pending = 1; 994749494b6SKrzysztof Opasiak hidg->req = NULL; 99533e4c1a9SKrzysztof Opasiak spin_lock_init(&hidg->read_spinlock); 99600a2430fSAndrzej Pietrasiewicz init_waitqueue_head(&hidg->write_queue); 99700a2430fSAndrzej Pietrasiewicz init_waitqueue_head(&hidg->read_queue); 99800a2430fSAndrzej Pietrasiewicz INIT_LIST_HEAD(&hidg->completed_out_req); 99900a2430fSAndrzej Pietrasiewicz 100000a2430fSAndrzej Pietrasiewicz /* create char device */ 100100a2430fSAndrzej Pietrasiewicz cdev_init(&hidg->cdev, &f_hidg_fops); 100200a2430fSAndrzej Pietrasiewicz dev = MKDEV(major, hidg->minor); 100300a2430fSAndrzej Pietrasiewicz status = cdev_add(&hidg->cdev, dev, 1); 100400a2430fSAndrzej Pietrasiewicz if (status) 1005d12a8727SPavitrakumar Managutte goto fail_free_descs; 100600a2430fSAndrzej Pietrasiewicz 100763406087SAndrzej Pietrasiewicz device = device_create(hidg_class, NULL, dev, NULL, 100863406087SAndrzej Pietrasiewicz "%s%d", "hidg", hidg->minor); 100963406087SAndrzej Pietrasiewicz if (IS_ERR(device)) { 101063406087SAndrzej Pietrasiewicz status = PTR_ERR(device); 101163406087SAndrzej Pietrasiewicz goto del; 101263406087SAndrzej Pietrasiewicz } 101300a2430fSAndrzej Pietrasiewicz 101400a2430fSAndrzej Pietrasiewicz return 0; 101563406087SAndrzej Pietrasiewicz del: 101663406087SAndrzej Pietrasiewicz cdev_del(&hidg->cdev); 1017d12a8727SPavitrakumar Managutte fail_free_descs: 1018d12a8727SPavitrakumar Managutte usb_free_all_descriptors(f); 101900a2430fSAndrzej Pietrasiewicz fail: 102000a2430fSAndrzej Pietrasiewicz ERROR(f->config->cdev, "hidg_bind FAILED\n"); 102114794d71SFelipe F. Tonello if (hidg->req != NULL) 102214794d71SFelipe F. Tonello free_ep_req(hidg->in_ep, hidg->req); 102300a2430fSAndrzej Pietrasiewicz 102400a2430fSAndrzej Pietrasiewicz return status; 102500a2430fSAndrzej Pietrasiewicz } 102600a2430fSAndrzej Pietrasiewicz 1027cb382536SAndrzej Pietrasiewicz static inline int hidg_get_minor(void) 1028cb382536SAndrzej Pietrasiewicz { 1029cb382536SAndrzej Pietrasiewicz int ret; 1030cb382536SAndrzej Pietrasiewicz 1031cb382536SAndrzej Pietrasiewicz ret = ida_simple_get(&hidg_ida, 0, 0, GFP_KERNEL); 1032774cf72fSAndrzej Pietrasiewicz if (ret >= HIDG_MINORS) { 1033774cf72fSAndrzej Pietrasiewicz ida_simple_remove(&hidg_ida, ret); 1034774cf72fSAndrzej Pietrasiewicz ret = -ENODEV; 1035774cf72fSAndrzej Pietrasiewicz } 1036cb382536SAndrzej Pietrasiewicz 1037cb382536SAndrzej Pietrasiewicz return ret; 1038cb382536SAndrzej Pietrasiewicz } 1039cb382536SAndrzej Pietrasiewicz 104021a9476aSAndrzej Pietrasiewicz static inline struct f_hid_opts *to_f_hid_opts(struct config_item *item) 104121a9476aSAndrzej Pietrasiewicz { 104221a9476aSAndrzej Pietrasiewicz return container_of(to_config_group(item), struct f_hid_opts, 104321a9476aSAndrzej Pietrasiewicz func_inst.group); 104421a9476aSAndrzej Pietrasiewicz } 104521a9476aSAndrzej Pietrasiewicz 104621a9476aSAndrzej Pietrasiewicz static void hid_attr_release(struct config_item *item) 104721a9476aSAndrzej Pietrasiewicz { 104821a9476aSAndrzej Pietrasiewicz struct f_hid_opts *opts = to_f_hid_opts(item); 104921a9476aSAndrzej Pietrasiewicz 105021a9476aSAndrzej Pietrasiewicz usb_put_function_instance(&opts->func_inst); 105121a9476aSAndrzej Pietrasiewicz } 105221a9476aSAndrzej Pietrasiewicz 105321a9476aSAndrzej Pietrasiewicz static struct configfs_item_operations hidg_item_ops = { 105421a9476aSAndrzej Pietrasiewicz .release = hid_attr_release, 105521a9476aSAndrzej Pietrasiewicz }; 105621a9476aSAndrzej Pietrasiewicz 105721a9476aSAndrzej Pietrasiewicz #define F_HID_OPT(name, prec, limit) \ 1058da4e527cSChristoph Hellwig static ssize_t f_hid_opts_##name##_show(struct config_item *item, char *page)\ 105921a9476aSAndrzej Pietrasiewicz { \ 1060da4e527cSChristoph Hellwig struct f_hid_opts *opts = to_f_hid_opts(item); \ 106121a9476aSAndrzej Pietrasiewicz int result; \ 106221a9476aSAndrzej Pietrasiewicz \ 106321a9476aSAndrzej Pietrasiewicz mutex_lock(&opts->lock); \ 106421a9476aSAndrzej Pietrasiewicz result = sprintf(page, "%d\n", opts->name); \ 106521a9476aSAndrzej Pietrasiewicz mutex_unlock(&opts->lock); \ 106621a9476aSAndrzej Pietrasiewicz \ 106721a9476aSAndrzej Pietrasiewicz return result; \ 106821a9476aSAndrzej Pietrasiewicz } \ 106921a9476aSAndrzej Pietrasiewicz \ 1070da4e527cSChristoph Hellwig static ssize_t f_hid_opts_##name##_store(struct config_item *item, \ 107121a9476aSAndrzej Pietrasiewicz const char *page, size_t len) \ 107221a9476aSAndrzej Pietrasiewicz { \ 1073da4e527cSChristoph Hellwig struct f_hid_opts *opts = to_f_hid_opts(item); \ 107421a9476aSAndrzej Pietrasiewicz int ret; \ 107521a9476aSAndrzej Pietrasiewicz u##prec num; \ 107621a9476aSAndrzej Pietrasiewicz \ 107721a9476aSAndrzej Pietrasiewicz mutex_lock(&opts->lock); \ 107821a9476aSAndrzej Pietrasiewicz if (opts->refcnt) { \ 107921a9476aSAndrzej Pietrasiewicz ret = -EBUSY; \ 108021a9476aSAndrzej Pietrasiewicz goto end; \ 108121a9476aSAndrzej Pietrasiewicz } \ 108221a9476aSAndrzej Pietrasiewicz \ 108321a9476aSAndrzej Pietrasiewicz ret = kstrtou##prec(page, 0, &num); \ 108421a9476aSAndrzej Pietrasiewicz if (ret) \ 108521a9476aSAndrzej Pietrasiewicz goto end; \ 108621a9476aSAndrzej Pietrasiewicz \ 108721a9476aSAndrzej Pietrasiewicz if (num > limit) { \ 108821a9476aSAndrzej Pietrasiewicz ret = -EINVAL; \ 108921a9476aSAndrzej Pietrasiewicz goto end; \ 109021a9476aSAndrzej Pietrasiewicz } \ 109121a9476aSAndrzej Pietrasiewicz opts->name = num; \ 109221a9476aSAndrzej Pietrasiewicz ret = len; \ 109321a9476aSAndrzej Pietrasiewicz \ 109421a9476aSAndrzej Pietrasiewicz end: \ 109521a9476aSAndrzej Pietrasiewicz mutex_unlock(&opts->lock); \ 109621a9476aSAndrzej Pietrasiewicz return ret; \ 109721a9476aSAndrzej Pietrasiewicz } \ 109821a9476aSAndrzej Pietrasiewicz \ 1099da4e527cSChristoph Hellwig CONFIGFS_ATTR(f_hid_opts_, name) 110021a9476aSAndrzej Pietrasiewicz 110121a9476aSAndrzej Pietrasiewicz F_HID_OPT(subclass, 8, 255); 110221a9476aSAndrzej Pietrasiewicz F_HID_OPT(protocol, 8, 255); 1103*d7428bc2SMaxim Devaev F_HID_OPT(no_out_endpoint, 8, 1); 110439a2ac27SAndrzej Pietrasiewicz F_HID_OPT(report_length, 16, 65535); 110521a9476aSAndrzej Pietrasiewicz 1106da4e527cSChristoph Hellwig static ssize_t f_hid_opts_report_desc_show(struct config_item *item, char *page) 110721a9476aSAndrzej Pietrasiewicz { 1108da4e527cSChristoph Hellwig struct f_hid_opts *opts = to_f_hid_opts(item); 110921a9476aSAndrzej Pietrasiewicz int result; 111021a9476aSAndrzej Pietrasiewicz 111121a9476aSAndrzej Pietrasiewicz mutex_lock(&opts->lock); 111221a9476aSAndrzej Pietrasiewicz result = opts->report_desc_length; 111321a9476aSAndrzej Pietrasiewicz memcpy(page, opts->report_desc, opts->report_desc_length); 111421a9476aSAndrzej Pietrasiewicz mutex_unlock(&opts->lock); 111521a9476aSAndrzej Pietrasiewicz 111621a9476aSAndrzej Pietrasiewicz return result; 111721a9476aSAndrzej Pietrasiewicz } 111821a9476aSAndrzej Pietrasiewicz 1119da4e527cSChristoph Hellwig static ssize_t f_hid_opts_report_desc_store(struct config_item *item, 112021a9476aSAndrzej Pietrasiewicz const char *page, size_t len) 112121a9476aSAndrzej Pietrasiewicz { 1122da4e527cSChristoph Hellwig struct f_hid_opts *opts = to_f_hid_opts(item); 112321a9476aSAndrzej Pietrasiewicz int ret = -EBUSY; 112421a9476aSAndrzej Pietrasiewicz char *d; 112521a9476aSAndrzej Pietrasiewicz 112621a9476aSAndrzej Pietrasiewicz mutex_lock(&opts->lock); 112721a9476aSAndrzej Pietrasiewicz 112821a9476aSAndrzej Pietrasiewicz if (opts->refcnt) 112921a9476aSAndrzej Pietrasiewicz goto end; 113021a9476aSAndrzej Pietrasiewicz if (len > PAGE_SIZE) { 113121a9476aSAndrzej Pietrasiewicz ret = -ENOSPC; 113221a9476aSAndrzej Pietrasiewicz goto end; 113321a9476aSAndrzej Pietrasiewicz } 113421a9476aSAndrzej Pietrasiewicz d = kmemdup(page, len, GFP_KERNEL); 113521a9476aSAndrzej Pietrasiewicz if (!d) { 113621a9476aSAndrzej Pietrasiewicz ret = -ENOMEM; 113721a9476aSAndrzej Pietrasiewicz goto end; 113821a9476aSAndrzej Pietrasiewicz } 113921a9476aSAndrzej Pietrasiewicz kfree(opts->report_desc); 114021a9476aSAndrzej Pietrasiewicz opts->report_desc = d; 114121a9476aSAndrzej Pietrasiewicz opts->report_desc_length = len; 114221a9476aSAndrzej Pietrasiewicz opts->report_desc_alloc = true; 114321a9476aSAndrzej Pietrasiewicz ret = len; 114421a9476aSAndrzej Pietrasiewicz end: 114521a9476aSAndrzej Pietrasiewicz mutex_unlock(&opts->lock); 114621a9476aSAndrzej Pietrasiewicz return ret; 114721a9476aSAndrzej Pietrasiewicz } 114821a9476aSAndrzej Pietrasiewicz 1149da4e527cSChristoph Hellwig CONFIGFS_ATTR(f_hid_opts_, report_desc); 115021a9476aSAndrzej Pietrasiewicz 1151ed6fe1f5SJohannes Berg static ssize_t f_hid_opts_dev_show(struct config_item *item, char *page) 1152ed6fe1f5SJohannes Berg { 1153ed6fe1f5SJohannes Berg struct f_hid_opts *opts = to_f_hid_opts(item); 1154ed6fe1f5SJohannes Berg 1155ed6fe1f5SJohannes Berg return sprintf(page, "%d:%d\n", major, opts->minor); 1156ed6fe1f5SJohannes Berg } 1157ed6fe1f5SJohannes Berg 1158ed6fe1f5SJohannes Berg CONFIGFS_ATTR_RO(f_hid_opts_, dev); 1159ed6fe1f5SJohannes Berg 116021a9476aSAndrzej Pietrasiewicz static struct configfs_attribute *hid_attrs[] = { 1161da4e527cSChristoph Hellwig &f_hid_opts_attr_subclass, 1162da4e527cSChristoph Hellwig &f_hid_opts_attr_protocol, 1163*d7428bc2SMaxim Devaev &f_hid_opts_attr_no_out_endpoint, 1164da4e527cSChristoph Hellwig &f_hid_opts_attr_report_length, 1165da4e527cSChristoph Hellwig &f_hid_opts_attr_report_desc, 1166ed6fe1f5SJohannes Berg &f_hid_opts_attr_dev, 116721a9476aSAndrzej Pietrasiewicz NULL, 116821a9476aSAndrzej Pietrasiewicz }; 116921a9476aSAndrzej Pietrasiewicz 117097363902SBhumika Goyal static const struct config_item_type hid_func_type = { 117121a9476aSAndrzej Pietrasiewicz .ct_item_ops = &hidg_item_ops, 117221a9476aSAndrzej Pietrasiewicz .ct_attrs = hid_attrs, 117321a9476aSAndrzej Pietrasiewicz .ct_owner = THIS_MODULE, 117421a9476aSAndrzej Pietrasiewicz }; 117521a9476aSAndrzej Pietrasiewicz 1176cb382536SAndrzej Pietrasiewicz static inline void hidg_put_minor(int minor) 1177cb382536SAndrzej Pietrasiewicz { 1178cb382536SAndrzej Pietrasiewicz ida_simple_remove(&hidg_ida, minor); 1179cb382536SAndrzej Pietrasiewicz } 1180cb382536SAndrzej Pietrasiewicz 1181cb382536SAndrzej Pietrasiewicz static void hidg_free_inst(struct usb_function_instance *f) 1182cb382536SAndrzej Pietrasiewicz { 1183cb382536SAndrzej Pietrasiewicz struct f_hid_opts *opts; 1184cb382536SAndrzej Pietrasiewicz 1185cb382536SAndrzej Pietrasiewicz opts = container_of(f, struct f_hid_opts, func_inst); 1186cb382536SAndrzej Pietrasiewicz 1187cb382536SAndrzej Pietrasiewicz mutex_lock(&hidg_ida_lock); 1188cb382536SAndrzej Pietrasiewicz 1189cb382536SAndrzej Pietrasiewicz hidg_put_minor(opts->minor); 119099c49407SMatthew Wilcox if (ida_is_empty(&hidg_ida)) 1191cb382536SAndrzej Pietrasiewicz ghid_cleanup(); 1192cb382536SAndrzej Pietrasiewicz 1193cb382536SAndrzej Pietrasiewicz mutex_unlock(&hidg_ida_lock); 1194cb382536SAndrzej Pietrasiewicz 1195cb382536SAndrzej Pietrasiewicz if (opts->report_desc_alloc) 1196cb382536SAndrzej Pietrasiewicz kfree(opts->report_desc); 1197cb382536SAndrzej Pietrasiewicz 1198cb382536SAndrzej Pietrasiewicz kfree(opts); 1199cb382536SAndrzej Pietrasiewicz } 1200cb382536SAndrzej Pietrasiewicz 1201cb382536SAndrzej Pietrasiewicz static struct usb_function_instance *hidg_alloc_inst(void) 1202cb382536SAndrzej Pietrasiewicz { 1203cb382536SAndrzej Pietrasiewicz struct f_hid_opts *opts; 1204cb382536SAndrzej Pietrasiewicz struct usb_function_instance *ret; 1205cb382536SAndrzej Pietrasiewicz int status = 0; 1206cb382536SAndrzej Pietrasiewicz 1207cb382536SAndrzej Pietrasiewicz opts = kzalloc(sizeof(*opts), GFP_KERNEL); 1208cb382536SAndrzej Pietrasiewicz if (!opts) 1209cb382536SAndrzej Pietrasiewicz return ERR_PTR(-ENOMEM); 121021a9476aSAndrzej Pietrasiewicz mutex_init(&opts->lock); 1211cb382536SAndrzej Pietrasiewicz opts->func_inst.free_func_inst = hidg_free_inst; 1212cb382536SAndrzej Pietrasiewicz ret = &opts->func_inst; 1213cb382536SAndrzej Pietrasiewicz 1214cb382536SAndrzej Pietrasiewicz mutex_lock(&hidg_ida_lock); 1215cb382536SAndrzej Pietrasiewicz 121699c49407SMatthew Wilcox if (ida_is_empty(&hidg_ida)) { 1217cb382536SAndrzej Pietrasiewicz status = ghid_setup(NULL, HIDG_MINORS); 1218cb382536SAndrzej Pietrasiewicz if (status) { 1219cb382536SAndrzej Pietrasiewicz ret = ERR_PTR(status); 1220cb382536SAndrzej Pietrasiewicz kfree(opts); 1221cb382536SAndrzej Pietrasiewicz goto unlock; 1222cb382536SAndrzej Pietrasiewicz } 1223cb382536SAndrzej Pietrasiewicz } 1224cb382536SAndrzej Pietrasiewicz 1225cb382536SAndrzej Pietrasiewicz opts->minor = hidg_get_minor(); 1226cb382536SAndrzej Pietrasiewicz if (opts->minor < 0) { 1227cb382536SAndrzej Pietrasiewicz ret = ERR_PTR(opts->minor); 1228cb382536SAndrzej Pietrasiewicz kfree(opts); 122999c49407SMatthew Wilcox if (ida_is_empty(&hidg_ida)) 1230cb382536SAndrzej Pietrasiewicz ghid_cleanup(); 1231828f6148SDan Carpenter goto unlock; 1232cb382536SAndrzej Pietrasiewicz } 123321a9476aSAndrzej Pietrasiewicz config_group_init_type_name(&opts->func_inst.group, "", &hid_func_type); 1234cb382536SAndrzej Pietrasiewicz 1235cb382536SAndrzej Pietrasiewicz unlock: 1236cb382536SAndrzej Pietrasiewicz mutex_unlock(&hidg_ida_lock); 1237cb382536SAndrzej Pietrasiewicz return ret; 1238cb382536SAndrzej Pietrasiewicz } 1239cb382536SAndrzej Pietrasiewicz 1240cb382536SAndrzej Pietrasiewicz static void hidg_free(struct usb_function *f) 1241cb382536SAndrzej Pietrasiewicz { 1242cb382536SAndrzej Pietrasiewicz struct f_hidg *hidg; 124321a9476aSAndrzej Pietrasiewicz struct f_hid_opts *opts; 1244cb382536SAndrzej Pietrasiewicz 1245cb382536SAndrzej Pietrasiewicz hidg = func_to_hidg(f); 124621a9476aSAndrzej Pietrasiewicz opts = container_of(f->fi, struct f_hid_opts, func_inst); 1247cb382536SAndrzej Pietrasiewicz kfree(hidg->report_desc); 1248*d7428bc2SMaxim Devaev kfree(hidg->set_report_buf); 1249cb382536SAndrzej Pietrasiewicz kfree(hidg); 125021a9476aSAndrzej Pietrasiewicz mutex_lock(&opts->lock); 125121a9476aSAndrzej Pietrasiewicz --opts->refcnt; 125221a9476aSAndrzej Pietrasiewicz mutex_unlock(&opts->lock); 1253cb382536SAndrzej Pietrasiewicz } 1254cb382536SAndrzej Pietrasiewicz 125500a2430fSAndrzej Pietrasiewicz static void hidg_unbind(struct usb_configuration *c, struct usb_function *f) 125600a2430fSAndrzej Pietrasiewicz { 125700a2430fSAndrzej Pietrasiewicz struct f_hidg *hidg = func_to_hidg(f); 125800a2430fSAndrzej Pietrasiewicz 125900a2430fSAndrzej Pietrasiewicz device_destroy(hidg_class, MKDEV(major, hidg->minor)); 126000a2430fSAndrzej Pietrasiewicz cdev_del(&hidg->cdev); 126100a2430fSAndrzej Pietrasiewicz 126200a2430fSAndrzej Pietrasiewicz usb_free_all_descriptors(f); 126300a2430fSAndrzej Pietrasiewicz } 126400a2430fSAndrzej Pietrasiewicz 12650fc57ea0SFengguang Wu static struct usb_function *hidg_alloc(struct usb_function_instance *fi) 126600a2430fSAndrzej Pietrasiewicz { 126700a2430fSAndrzej Pietrasiewicz struct f_hidg *hidg; 1268cb382536SAndrzej Pietrasiewicz struct f_hid_opts *opts; 126900a2430fSAndrzej Pietrasiewicz 127000a2430fSAndrzej Pietrasiewicz /* allocate and initialize one new instance */ 1271cb382536SAndrzej Pietrasiewicz hidg = kzalloc(sizeof(*hidg), GFP_KERNEL); 127200a2430fSAndrzej Pietrasiewicz if (!hidg) 1273cb382536SAndrzej Pietrasiewicz return ERR_PTR(-ENOMEM); 127400a2430fSAndrzej Pietrasiewicz 1275cb382536SAndrzej Pietrasiewicz opts = container_of(fi, struct f_hid_opts, func_inst); 1276cb382536SAndrzej Pietrasiewicz 127721a9476aSAndrzej Pietrasiewicz mutex_lock(&opts->lock); 127821a9476aSAndrzej Pietrasiewicz ++opts->refcnt; 127921a9476aSAndrzej Pietrasiewicz 1280cb382536SAndrzej Pietrasiewicz hidg->minor = opts->minor; 1281cb382536SAndrzej Pietrasiewicz hidg->bInterfaceSubClass = opts->subclass; 1282cb382536SAndrzej Pietrasiewicz hidg->bInterfaceProtocol = opts->protocol; 1283cb382536SAndrzej Pietrasiewicz hidg->report_length = opts->report_length; 1284cb382536SAndrzej Pietrasiewicz hidg->report_desc_length = opts->report_desc_length; 1285cb382536SAndrzej Pietrasiewicz if (opts->report_desc) { 1286cb382536SAndrzej Pietrasiewicz hidg->report_desc = kmemdup(opts->report_desc, 1287cb382536SAndrzej Pietrasiewicz opts->report_desc_length, 128800a2430fSAndrzej Pietrasiewicz GFP_KERNEL); 128900a2430fSAndrzej Pietrasiewicz if (!hidg->report_desc) { 129000a2430fSAndrzej Pietrasiewicz kfree(hidg); 129121a9476aSAndrzej Pietrasiewicz mutex_unlock(&opts->lock); 1292cb382536SAndrzej Pietrasiewicz return ERR_PTR(-ENOMEM); 1293cb382536SAndrzej Pietrasiewicz } 129400a2430fSAndrzej Pietrasiewicz } 1295*d7428bc2SMaxim Devaev hidg->use_out_ep = !opts->no_out_endpoint; 129600a2430fSAndrzej Pietrasiewicz 129721a9476aSAndrzej Pietrasiewicz mutex_unlock(&opts->lock); 129821a9476aSAndrzej Pietrasiewicz 129900a2430fSAndrzej Pietrasiewicz hidg->func.name = "hid"; 130000a2430fSAndrzej Pietrasiewicz hidg->func.bind = hidg_bind; 130100a2430fSAndrzej Pietrasiewicz hidg->func.unbind = hidg_unbind; 130200a2430fSAndrzej Pietrasiewicz hidg->func.set_alt = hidg_set_alt; 130300a2430fSAndrzej Pietrasiewicz hidg->func.disable = hidg_disable; 130400a2430fSAndrzej Pietrasiewicz hidg->func.setup = hidg_setup; 1305cb382536SAndrzej Pietrasiewicz hidg->func.free_func = hidg_free; 130600a2430fSAndrzej Pietrasiewicz 130729a812e4SWei Ming Chen /* this could be made configurable at some point */ 130800a2430fSAndrzej Pietrasiewicz hidg->qlen = 4; 130900a2430fSAndrzej Pietrasiewicz 1310cb382536SAndrzej Pietrasiewicz return &hidg->func; 131100a2430fSAndrzej Pietrasiewicz } 131200a2430fSAndrzej Pietrasiewicz 1313cb382536SAndrzej Pietrasiewicz DECLARE_USB_FUNCTION_INIT(hid, hidg_alloc_inst, hidg_alloc); 1314cb382536SAndrzej Pietrasiewicz MODULE_LICENSE("GPL"); 1315cb382536SAndrzej Pietrasiewicz MODULE_AUTHOR("Fabien Chouteau"); 1316cb382536SAndrzej Pietrasiewicz 1317cb382536SAndrzej Pietrasiewicz int ghid_setup(struct usb_gadget *g, int count) 131800a2430fSAndrzej Pietrasiewicz { 131900a2430fSAndrzej Pietrasiewicz int status; 132000a2430fSAndrzej Pietrasiewicz dev_t dev; 132100a2430fSAndrzej Pietrasiewicz 132200a2430fSAndrzej Pietrasiewicz hidg_class = class_create(THIS_MODULE, "hidg"); 132306529407SAndrzej Pietrasiewicz if (IS_ERR(hidg_class)) { 13240448d38cSDan Carpenter status = PTR_ERR(hidg_class); 132506529407SAndrzej Pietrasiewicz hidg_class = NULL; 13260448d38cSDan Carpenter return status; 132700a2430fSAndrzej Pietrasiewicz } 132800a2430fSAndrzej Pietrasiewicz 132900a2430fSAndrzej Pietrasiewicz status = alloc_chrdev_region(&dev, 0, count, "hidg"); 13300448d38cSDan Carpenter if (status) { 13310448d38cSDan Carpenter class_destroy(hidg_class); 13320448d38cSDan Carpenter hidg_class = NULL; 133300a2430fSAndrzej Pietrasiewicz return status; 133400a2430fSAndrzej Pietrasiewicz } 133500a2430fSAndrzej Pietrasiewicz 13360448d38cSDan Carpenter major = MAJOR(dev); 13370448d38cSDan Carpenter minors = count; 13380448d38cSDan Carpenter 13390448d38cSDan Carpenter return 0; 134000a2430fSAndrzej Pietrasiewicz } 134100a2430fSAndrzej Pietrasiewicz 134200a2430fSAndrzej Pietrasiewicz void ghid_cleanup(void) 134300a2430fSAndrzej Pietrasiewicz { 134400a2430fSAndrzej Pietrasiewicz if (major) { 134500a2430fSAndrzej Pietrasiewicz unregister_chrdev_region(MKDEV(major, 0), minors); 134600a2430fSAndrzej Pietrasiewicz major = minors = 0; 134700a2430fSAndrzej Pietrasiewicz } 134800a2430fSAndrzej Pietrasiewicz 134900a2430fSAndrzej Pietrasiewicz class_destroy(hidg_class); 135000a2430fSAndrzej Pietrasiewicz hidg_class = NULL; 135100a2430fSAndrzej Pietrasiewicz } 1352