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; 26*99f2d956SIvan Orlov 27*99f2d956SIvan Orlov static const struct class hidg_class = { 28*99f2d956SIvan Orlov .name = "hidg", 29*99f2d956SIvan Orlov }; 30*99f2d956SIvan Orlov 31cb382536SAndrzej Pietrasiewicz static DEFINE_IDA(hidg_ida); 32cb382536SAndrzej Pietrasiewicz static DEFINE_MUTEX(hidg_ida_lock); /* protects access to hidg_ida */ 3300a2430fSAndrzej Pietrasiewicz 3400a2430fSAndrzej Pietrasiewicz /*-------------------------------------------------------------------------*/ 3500a2430fSAndrzej Pietrasiewicz /* HID gadget struct */ 3600a2430fSAndrzej Pietrasiewicz 3700a2430fSAndrzej Pietrasiewicz struct f_hidg_req_list { 3800a2430fSAndrzej Pietrasiewicz struct usb_request *req; 3900a2430fSAndrzej Pietrasiewicz unsigned int pos; 4000a2430fSAndrzej Pietrasiewicz struct list_head list; 4100a2430fSAndrzej Pietrasiewicz }; 4200a2430fSAndrzej Pietrasiewicz 4300a2430fSAndrzej Pietrasiewicz struct f_hidg { 4400a2430fSAndrzej Pietrasiewicz /* configuration */ 4500a2430fSAndrzej Pietrasiewicz unsigned char bInterfaceSubClass; 4600a2430fSAndrzej Pietrasiewicz unsigned char bInterfaceProtocol; 47b3c4ec71SAbdulhadi Mohamed unsigned char protocol; 48afcff6dcSMaxim Devaev unsigned char idle; 4900a2430fSAndrzej Pietrasiewicz unsigned short report_desc_length; 5000a2430fSAndrzej Pietrasiewicz char *report_desc; 5100a2430fSAndrzej Pietrasiewicz unsigned short report_length; 52d7428bc2SMaxim Devaev /* 53d7428bc2SMaxim Devaev * use_out_ep - if true, the OUT Endpoint (interrupt out method) 54d7428bc2SMaxim Devaev * will be used to receive reports from the host 55d7428bc2SMaxim Devaev * using functions with the "intout" suffix. 56d7428bc2SMaxim Devaev * Otherwise, the OUT Endpoint will not be configured 57d7428bc2SMaxim Devaev * and the SETUP/SET_REPORT method ("ssreport" suffix) 58d7428bc2SMaxim Devaev * will be used to receive reports. 59d7428bc2SMaxim Devaev */ 60d7428bc2SMaxim Devaev bool use_out_ep; 6100a2430fSAndrzej Pietrasiewicz 6200a2430fSAndrzej Pietrasiewicz /* recv report */ 6333e4c1a9SKrzysztof Opasiak spinlock_t read_spinlock; 6400a2430fSAndrzej Pietrasiewicz wait_queue_head_t read_queue; 65d7428bc2SMaxim Devaev /* recv report - interrupt out only (use_out_ep == 1) */ 66d7428bc2SMaxim Devaev struct list_head completed_out_req; 6700a2430fSAndrzej Pietrasiewicz unsigned int qlen; 68d7428bc2SMaxim Devaev /* recv report - setup set_report only (use_out_ep == 0) */ 69d7428bc2SMaxim Devaev char *set_report_buf; 70d7428bc2SMaxim Devaev unsigned int set_report_length; 7100a2430fSAndrzej Pietrasiewicz 7200a2430fSAndrzej Pietrasiewicz /* send report */ 7333e4c1a9SKrzysztof Opasiak spinlock_t write_spinlock; 7400a2430fSAndrzej Pietrasiewicz bool write_pending; 7500a2430fSAndrzej Pietrasiewicz wait_queue_head_t write_queue; 7600a2430fSAndrzej Pietrasiewicz struct usb_request *req; 7700a2430fSAndrzej Pietrasiewicz 7889ff3dfaSJohn Keeping struct device dev; 7900a2430fSAndrzej Pietrasiewicz struct cdev cdev; 8000a2430fSAndrzej Pietrasiewicz struct usb_function func; 8100a2430fSAndrzej Pietrasiewicz 8200a2430fSAndrzej Pietrasiewicz struct usb_ep *in_ep; 8300a2430fSAndrzej Pietrasiewicz struct usb_ep *out_ep; 8400a2430fSAndrzej Pietrasiewicz }; 8500a2430fSAndrzej Pietrasiewicz 8600a2430fSAndrzej Pietrasiewicz static inline struct f_hidg *func_to_hidg(struct usb_function *f) 8700a2430fSAndrzej Pietrasiewicz { 8800a2430fSAndrzej Pietrasiewicz return container_of(f, struct f_hidg, func); 8900a2430fSAndrzej Pietrasiewicz } 9000a2430fSAndrzej Pietrasiewicz 9189ff3dfaSJohn Keeping static void hidg_release(struct device *dev) 9289ff3dfaSJohn Keeping { 9389ff3dfaSJohn Keeping struct f_hidg *hidg = container_of(dev, struct f_hidg, dev); 9489ff3dfaSJohn Keeping 9589ff3dfaSJohn Keeping kfree(hidg->set_report_buf); 9689ff3dfaSJohn Keeping kfree(hidg); 9789ff3dfaSJohn Keeping } 9889ff3dfaSJohn Keeping 9900a2430fSAndrzej Pietrasiewicz /*-------------------------------------------------------------------------*/ 10000a2430fSAndrzej Pietrasiewicz /* Static descriptors */ 10100a2430fSAndrzej Pietrasiewicz 10200a2430fSAndrzej Pietrasiewicz static struct usb_interface_descriptor hidg_interface_desc = { 10300a2430fSAndrzej Pietrasiewicz .bLength = sizeof hidg_interface_desc, 10400a2430fSAndrzej Pietrasiewicz .bDescriptorType = USB_DT_INTERFACE, 10500a2430fSAndrzej Pietrasiewicz /* .bInterfaceNumber = DYNAMIC */ 10600a2430fSAndrzej Pietrasiewicz .bAlternateSetting = 0, 107d7428bc2SMaxim Devaev /* .bNumEndpoints = DYNAMIC (depends on use_out_ep) */ 10800a2430fSAndrzej Pietrasiewicz .bInterfaceClass = USB_CLASS_HID, 10900a2430fSAndrzej Pietrasiewicz /* .bInterfaceSubClass = DYNAMIC */ 11000a2430fSAndrzej Pietrasiewicz /* .bInterfaceProtocol = DYNAMIC */ 11100a2430fSAndrzej Pietrasiewicz /* .iInterface = DYNAMIC */ 11200a2430fSAndrzej Pietrasiewicz }; 11300a2430fSAndrzej Pietrasiewicz 11400a2430fSAndrzej Pietrasiewicz static struct hid_descriptor hidg_desc = { 11500a2430fSAndrzej Pietrasiewicz .bLength = sizeof hidg_desc, 11600a2430fSAndrzej Pietrasiewicz .bDescriptorType = HID_DT_HID, 11733cb46c4SRuslan Bilovol .bcdHID = cpu_to_le16(0x0101), 11800a2430fSAndrzej Pietrasiewicz .bCountryCode = 0x00, 11900a2430fSAndrzej Pietrasiewicz .bNumDescriptors = 0x1, 12000a2430fSAndrzej Pietrasiewicz /*.desc[0].bDescriptorType = DYNAMIC */ 12100a2430fSAndrzej Pietrasiewicz /*.desc[0].wDescriptorLenght = DYNAMIC */ 12200a2430fSAndrzej Pietrasiewicz }; 12300a2430fSAndrzej Pietrasiewicz 124dbf499cfSJanusz Dziedzic /* Super-Speed Support */ 125dbf499cfSJanusz Dziedzic 126dbf499cfSJanusz Dziedzic static struct usb_endpoint_descriptor hidg_ss_in_ep_desc = { 127dbf499cfSJanusz Dziedzic .bLength = USB_DT_ENDPOINT_SIZE, 128dbf499cfSJanusz Dziedzic .bDescriptorType = USB_DT_ENDPOINT, 129dbf499cfSJanusz Dziedzic .bEndpointAddress = USB_DIR_IN, 130dbf499cfSJanusz Dziedzic .bmAttributes = USB_ENDPOINT_XFER_INT, 131dbf499cfSJanusz Dziedzic /*.wMaxPacketSize = DYNAMIC */ 132dbf499cfSJanusz Dziedzic .bInterval = 4, /* FIXME: Add this field in the 133dbf499cfSJanusz Dziedzic * HID gadget configuration? 134dbf499cfSJanusz Dziedzic * (struct hidg_func_descriptor) 135dbf499cfSJanusz Dziedzic */ 136dbf499cfSJanusz Dziedzic }; 137dbf499cfSJanusz Dziedzic 138dbf499cfSJanusz Dziedzic static struct usb_ss_ep_comp_descriptor hidg_ss_in_comp_desc = { 139dbf499cfSJanusz Dziedzic .bLength = sizeof(hidg_ss_in_comp_desc), 140dbf499cfSJanusz Dziedzic .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, 141dbf499cfSJanusz Dziedzic 142dbf499cfSJanusz Dziedzic /* .bMaxBurst = 0, */ 143dbf499cfSJanusz Dziedzic /* .bmAttributes = 0, */ 144dbf499cfSJanusz Dziedzic /* .wBytesPerInterval = DYNAMIC */ 145dbf499cfSJanusz Dziedzic }; 146dbf499cfSJanusz Dziedzic 147dbf499cfSJanusz Dziedzic static struct usb_endpoint_descriptor hidg_ss_out_ep_desc = { 148dbf499cfSJanusz Dziedzic .bLength = USB_DT_ENDPOINT_SIZE, 149dbf499cfSJanusz Dziedzic .bDescriptorType = USB_DT_ENDPOINT, 150dbf499cfSJanusz Dziedzic .bEndpointAddress = USB_DIR_OUT, 151dbf499cfSJanusz Dziedzic .bmAttributes = USB_ENDPOINT_XFER_INT, 152dbf499cfSJanusz Dziedzic /*.wMaxPacketSize = DYNAMIC */ 153dbf499cfSJanusz Dziedzic .bInterval = 4, /* FIXME: Add this field in the 154dbf499cfSJanusz Dziedzic * HID gadget configuration? 155dbf499cfSJanusz Dziedzic * (struct hidg_func_descriptor) 156dbf499cfSJanusz Dziedzic */ 157dbf499cfSJanusz Dziedzic }; 158dbf499cfSJanusz Dziedzic 159dbf499cfSJanusz Dziedzic static struct usb_ss_ep_comp_descriptor hidg_ss_out_comp_desc = { 160dbf499cfSJanusz Dziedzic .bLength = sizeof(hidg_ss_out_comp_desc), 161dbf499cfSJanusz Dziedzic .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, 162dbf499cfSJanusz Dziedzic 163dbf499cfSJanusz Dziedzic /* .bMaxBurst = 0, */ 164dbf499cfSJanusz Dziedzic /* .bmAttributes = 0, */ 165dbf499cfSJanusz Dziedzic /* .wBytesPerInterval = DYNAMIC */ 166dbf499cfSJanusz Dziedzic }; 167dbf499cfSJanusz Dziedzic 168d7428bc2SMaxim Devaev static struct usb_descriptor_header *hidg_ss_descriptors_intout[] = { 169dbf499cfSJanusz Dziedzic (struct usb_descriptor_header *)&hidg_interface_desc, 170dbf499cfSJanusz Dziedzic (struct usb_descriptor_header *)&hidg_desc, 171dbf499cfSJanusz Dziedzic (struct usb_descriptor_header *)&hidg_ss_in_ep_desc, 172dbf499cfSJanusz Dziedzic (struct usb_descriptor_header *)&hidg_ss_in_comp_desc, 173dbf499cfSJanusz Dziedzic (struct usb_descriptor_header *)&hidg_ss_out_ep_desc, 174dbf499cfSJanusz Dziedzic (struct usb_descriptor_header *)&hidg_ss_out_comp_desc, 175dbf499cfSJanusz Dziedzic NULL, 176dbf499cfSJanusz Dziedzic }; 177dbf499cfSJanusz Dziedzic 178d7428bc2SMaxim Devaev static struct usb_descriptor_header *hidg_ss_descriptors_ssreport[] = { 179d7428bc2SMaxim Devaev (struct usb_descriptor_header *)&hidg_interface_desc, 180d7428bc2SMaxim Devaev (struct usb_descriptor_header *)&hidg_desc, 181d7428bc2SMaxim Devaev (struct usb_descriptor_header *)&hidg_ss_in_ep_desc, 182d7428bc2SMaxim Devaev (struct usb_descriptor_header *)&hidg_ss_in_comp_desc, 183d7428bc2SMaxim Devaev NULL, 184d7428bc2SMaxim Devaev }; 185d7428bc2SMaxim Devaev 18600a2430fSAndrzej Pietrasiewicz /* High-Speed Support */ 18700a2430fSAndrzej Pietrasiewicz 18800a2430fSAndrzej Pietrasiewicz static struct usb_endpoint_descriptor hidg_hs_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 = 4, /* 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_hs_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 = 4, /* FIXME: Add this field in the 20700a2430fSAndrzej Pietrasiewicz * HID gadget configuration? 20800a2430fSAndrzej Pietrasiewicz * (struct hidg_func_descriptor) 20900a2430fSAndrzej Pietrasiewicz */ 21000a2430fSAndrzej Pietrasiewicz }; 21100a2430fSAndrzej Pietrasiewicz 212d7428bc2SMaxim Devaev static struct usb_descriptor_header *hidg_hs_descriptors_intout[] = { 21300a2430fSAndrzej Pietrasiewicz (struct usb_descriptor_header *)&hidg_interface_desc, 21400a2430fSAndrzej Pietrasiewicz (struct usb_descriptor_header *)&hidg_desc, 21500a2430fSAndrzej Pietrasiewicz (struct usb_descriptor_header *)&hidg_hs_in_ep_desc, 21600a2430fSAndrzej Pietrasiewicz (struct usb_descriptor_header *)&hidg_hs_out_ep_desc, 21700a2430fSAndrzej Pietrasiewicz NULL, 21800a2430fSAndrzej Pietrasiewicz }; 21900a2430fSAndrzej Pietrasiewicz 220d7428bc2SMaxim Devaev static struct usb_descriptor_header *hidg_hs_descriptors_ssreport[] = { 221d7428bc2SMaxim Devaev (struct usb_descriptor_header *)&hidg_interface_desc, 222d7428bc2SMaxim Devaev (struct usb_descriptor_header *)&hidg_desc, 223d7428bc2SMaxim Devaev (struct usb_descriptor_header *)&hidg_hs_in_ep_desc, 224d7428bc2SMaxim Devaev NULL, 225d7428bc2SMaxim Devaev }; 226d7428bc2SMaxim Devaev 22700a2430fSAndrzej Pietrasiewicz /* Full-Speed Support */ 22800a2430fSAndrzej Pietrasiewicz 22900a2430fSAndrzej Pietrasiewicz static struct usb_endpoint_descriptor hidg_fs_in_ep_desc = { 23000a2430fSAndrzej Pietrasiewicz .bLength = USB_DT_ENDPOINT_SIZE, 23100a2430fSAndrzej Pietrasiewicz .bDescriptorType = USB_DT_ENDPOINT, 23200a2430fSAndrzej Pietrasiewicz .bEndpointAddress = USB_DIR_IN, 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 24100a2430fSAndrzej Pietrasiewicz static struct usb_endpoint_descriptor hidg_fs_out_ep_desc = { 24200a2430fSAndrzej Pietrasiewicz .bLength = USB_DT_ENDPOINT_SIZE, 24300a2430fSAndrzej Pietrasiewicz .bDescriptorType = USB_DT_ENDPOINT, 24400a2430fSAndrzej Pietrasiewicz .bEndpointAddress = USB_DIR_OUT, 24500a2430fSAndrzej Pietrasiewicz .bmAttributes = USB_ENDPOINT_XFER_INT, 24600a2430fSAndrzej Pietrasiewicz /*.wMaxPacketSize = DYNAMIC */ 24700a2430fSAndrzej Pietrasiewicz .bInterval = 10, /* FIXME: Add this field in the 24800a2430fSAndrzej Pietrasiewicz * HID gadget configuration? 24900a2430fSAndrzej Pietrasiewicz * (struct hidg_func_descriptor) 25000a2430fSAndrzej Pietrasiewicz */ 25100a2430fSAndrzej Pietrasiewicz }; 25200a2430fSAndrzej Pietrasiewicz 253d7428bc2SMaxim Devaev static struct usb_descriptor_header *hidg_fs_descriptors_intout[] = { 25400a2430fSAndrzej Pietrasiewicz (struct usb_descriptor_header *)&hidg_interface_desc, 25500a2430fSAndrzej Pietrasiewicz (struct usb_descriptor_header *)&hidg_desc, 25600a2430fSAndrzej Pietrasiewicz (struct usb_descriptor_header *)&hidg_fs_in_ep_desc, 25700a2430fSAndrzej Pietrasiewicz (struct usb_descriptor_header *)&hidg_fs_out_ep_desc, 25800a2430fSAndrzej Pietrasiewicz NULL, 25900a2430fSAndrzej Pietrasiewicz }; 26000a2430fSAndrzej Pietrasiewicz 261d7428bc2SMaxim Devaev static struct usb_descriptor_header *hidg_fs_descriptors_ssreport[] = { 262d7428bc2SMaxim Devaev (struct usb_descriptor_header *)&hidg_interface_desc, 263d7428bc2SMaxim Devaev (struct usb_descriptor_header *)&hidg_desc, 264d7428bc2SMaxim Devaev (struct usb_descriptor_header *)&hidg_fs_in_ep_desc, 265d7428bc2SMaxim Devaev NULL, 266d7428bc2SMaxim Devaev }; 267d7428bc2SMaxim Devaev 26800a2430fSAndrzej Pietrasiewicz /*-------------------------------------------------------------------------*/ 269cb382536SAndrzej Pietrasiewicz /* Strings */ 270cb382536SAndrzej Pietrasiewicz 271cb382536SAndrzej Pietrasiewicz #define CT_FUNC_HID_IDX 0 272cb382536SAndrzej Pietrasiewicz 273cb382536SAndrzej Pietrasiewicz static struct usb_string ct_func_string_defs[] = { 274cb382536SAndrzej Pietrasiewicz [CT_FUNC_HID_IDX].s = "HID Interface", 275cb382536SAndrzej Pietrasiewicz {}, /* end of list */ 276cb382536SAndrzej Pietrasiewicz }; 277cb382536SAndrzej Pietrasiewicz 278cb382536SAndrzej Pietrasiewicz static struct usb_gadget_strings ct_func_string_table = { 279cb382536SAndrzej Pietrasiewicz .language = 0x0409, /* en-US */ 280cb382536SAndrzej Pietrasiewicz .strings = ct_func_string_defs, 281cb382536SAndrzej Pietrasiewicz }; 282cb382536SAndrzej Pietrasiewicz 283cb382536SAndrzej Pietrasiewicz static struct usb_gadget_strings *ct_func_strings[] = { 284cb382536SAndrzej Pietrasiewicz &ct_func_string_table, 285cb382536SAndrzej Pietrasiewicz NULL, 286cb382536SAndrzej Pietrasiewicz }; 287cb382536SAndrzej Pietrasiewicz 288cb382536SAndrzej Pietrasiewicz /*-------------------------------------------------------------------------*/ 28900a2430fSAndrzej Pietrasiewicz /* Char Device */ 29000a2430fSAndrzej Pietrasiewicz 291d7428bc2SMaxim Devaev static ssize_t f_hidg_intout_read(struct file *file, char __user *buffer, 29200a2430fSAndrzej Pietrasiewicz size_t count, loff_t *ptr) 29300a2430fSAndrzej Pietrasiewicz { 29400a2430fSAndrzej Pietrasiewicz struct f_hidg *hidg = file->private_data; 29500a2430fSAndrzej Pietrasiewicz struct f_hidg_req_list *list; 29600a2430fSAndrzej Pietrasiewicz struct usb_request *req; 29700a2430fSAndrzej Pietrasiewicz unsigned long flags; 29800a2430fSAndrzej Pietrasiewicz int ret; 29900a2430fSAndrzej Pietrasiewicz 30000a2430fSAndrzej Pietrasiewicz if (!count) 30100a2430fSAndrzej Pietrasiewicz return 0; 30200a2430fSAndrzej Pietrasiewicz 30333e4c1a9SKrzysztof Opasiak spin_lock_irqsave(&hidg->read_spinlock, flags); 30400a2430fSAndrzej Pietrasiewicz 305d7428bc2SMaxim Devaev #define READ_COND_INTOUT (!list_empty(&hidg->completed_out_req)) 30600a2430fSAndrzej Pietrasiewicz 30700a2430fSAndrzej Pietrasiewicz /* wait for at least one buffer to complete */ 308d7428bc2SMaxim Devaev while (!READ_COND_INTOUT) { 30933e4c1a9SKrzysztof Opasiak spin_unlock_irqrestore(&hidg->read_spinlock, flags); 31000a2430fSAndrzej Pietrasiewicz if (file->f_flags & O_NONBLOCK) 31100a2430fSAndrzej Pietrasiewicz return -EAGAIN; 31200a2430fSAndrzej Pietrasiewicz 313d7428bc2SMaxim Devaev if (wait_event_interruptible(hidg->read_queue, READ_COND_INTOUT)) 31400a2430fSAndrzej Pietrasiewicz return -ERESTARTSYS; 31500a2430fSAndrzej Pietrasiewicz 31633e4c1a9SKrzysztof Opasiak spin_lock_irqsave(&hidg->read_spinlock, flags); 31700a2430fSAndrzej Pietrasiewicz } 31800a2430fSAndrzej Pietrasiewicz 31900a2430fSAndrzej Pietrasiewicz /* pick the first one */ 32000a2430fSAndrzej Pietrasiewicz list = list_first_entry(&hidg->completed_out_req, 32100a2430fSAndrzej Pietrasiewicz struct f_hidg_req_list, list); 322aa65d11aSKrzysztof Opasiak 323aa65d11aSKrzysztof Opasiak /* 324aa65d11aSKrzysztof Opasiak * Remove this from list to protect it from beign free() 325aa65d11aSKrzysztof Opasiak * while host disables our function 326aa65d11aSKrzysztof Opasiak */ 327aa65d11aSKrzysztof Opasiak list_del(&list->list); 328aa65d11aSKrzysztof Opasiak 32900a2430fSAndrzej Pietrasiewicz req = list->req; 33000a2430fSAndrzej Pietrasiewicz count = min_t(unsigned int, count, req->actual - list->pos); 33133e4c1a9SKrzysztof Opasiak spin_unlock_irqrestore(&hidg->read_spinlock, flags); 33200a2430fSAndrzej Pietrasiewicz 33300a2430fSAndrzej Pietrasiewicz /* copy to user outside spinlock */ 33400a2430fSAndrzej Pietrasiewicz count -= copy_to_user(buffer, req->buf + list->pos, count); 33500a2430fSAndrzej Pietrasiewicz list->pos += count; 33600a2430fSAndrzej Pietrasiewicz 33700a2430fSAndrzej Pietrasiewicz /* 33800a2430fSAndrzej Pietrasiewicz * if this request is completely handled and transfered to 33900a2430fSAndrzej Pietrasiewicz * userspace, remove its entry from the list and requeue it 34000a2430fSAndrzej Pietrasiewicz * again. Otherwise, we will revisit it again upon the next 34100a2430fSAndrzej Pietrasiewicz * call, taking into account its current read position. 34200a2430fSAndrzej Pietrasiewicz */ 34300a2430fSAndrzej Pietrasiewicz if (list->pos == req->actual) { 34400a2430fSAndrzej Pietrasiewicz kfree(list); 34500a2430fSAndrzej Pietrasiewicz 34600a2430fSAndrzej Pietrasiewicz req->length = hidg->report_length; 34700a2430fSAndrzej Pietrasiewicz ret = usb_ep_queue(hidg->out_ep, req, GFP_KERNEL); 348aa65d11aSKrzysztof Opasiak if (ret < 0) { 349aa65d11aSKrzysztof Opasiak free_ep_req(hidg->out_ep, req); 35000a2430fSAndrzej Pietrasiewicz return ret; 35100a2430fSAndrzej Pietrasiewicz } 352aa65d11aSKrzysztof Opasiak } else { 35333e4c1a9SKrzysztof Opasiak spin_lock_irqsave(&hidg->read_spinlock, flags); 354aa65d11aSKrzysztof Opasiak list_add(&list->list, &hidg->completed_out_req); 35533e4c1a9SKrzysztof Opasiak spin_unlock_irqrestore(&hidg->read_spinlock, flags); 356aa65d11aSKrzysztof Opasiak 357aa65d11aSKrzysztof Opasiak wake_up(&hidg->read_queue); 358aa65d11aSKrzysztof Opasiak } 35900a2430fSAndrzej Pietrasiewicz 36000a2430fSAndrzej Pietrasiewicz return count; 36100a2430fSAndrzej Pietrasiewicz } 36200a2430fSAndrzej Pietrasiewicz 363d7428bc2SMaxim Devaev #define READ_COND_SSREPORT (hidg->set_report_buf != NULL) 364d7428bc2SMaxim Devaev 365d7428bc2SMaxim Devaev static ssize_t f_hidg_ssreport_read(struct file *file, char __user *buffer, 366d7428bc2SMaxim Devaev size_t count, loff_t *ptr) 367d7428bc2SMaxim Devaev { 368d7428bc2SMaxim Devaev struct f_hidg *hidg = file->private_data; 369d7428bc2SMaxim Devaev char *tmp_buf = NULL; 370d7428bc2SMaxim Devaev unsigned long flags; 371d7428bc2SMaxim Devaev 372d7428bc2SMaxim Devaev if (!count) 373d7428bc2SMaxim Devaev return 0; 374d7428bc2SMaxim Devaev 375d7428bc2SMaxim Devaev spin_lock_irqsave(&hidg->read_spinlock, flags); 376d7428bc2SMaxim Devaev 377d7428bc2SMaxim Devaev while (!READ_COND_SSREPORT) { 378d7428bc2SMaxim Devaev spin_unlock_irqrestore(&hidg->read_spinlock, flags); 379d7428bc2SMaxim Devaev if (file->f_flags & O_NONBLOCK) 380d7428bc2SMaxim Devaev return -EAGAIN; 381d7428bc2SMaxim Devaev 382d7428bc2SMaxim Devaev if (wait_event_interruptible(hidg->read_queue, READ_COND_SSREPORT)) 383d7428bc2SMaxim Devaev return -ERESTARTSYS; 384d7428bc2SMaxim Devaev 385d7428bc2SMaxim Devaev spin_lock_irqsave(&hidg->read_spinlock, flags); 386d7428bc2SMaxim Devaev } 387d7428bc2SMaxim Devaev 388d7428bc2SMaxim Devaev count = min_t(unsigned int, count, hidg->set_report_length); 389d7428bc2SMaxim Devaev tmp_buf = hidg->set_report_buf; 390d7428bc2SMaxim Devaev hidg->set_report_buf = NULL; 391d7428bc2SMaxim Devaev 392d7428bc2SMaxim Devaev spin_unlock_irqrestore(&hidg->read_spinlock, flags); 393d7428bc2SMaxim Devaev 394d7428bc2SMaxim Devaev if (tmp_buf != NULL) { 395d7428bc2SMaxim Devaev count -= copy_to_user(buffer, tmp_buf, count); 396d7428bc2SMaxim Devaev kfree(tmp_buf); 397d7428bc2SMaxim Devaev } else { 398d7428bc2SMaxim Devaev count = -ENOMEM; 399d7428bc2SMaxim Devaev } 400d7428bc2SMaxim Devaev 401d7428bc2SMaxim Devaev wake_up(&hidg->read_queue); 402d7428bc2SMaxim Devaev 403d7428bc2SMaxim Devaev return count; 404d7428bc2SMaxim Devaev } 405d7428bc2SMaxim Devaev 406d7428bc2SMaxim Devaev static ssize_t f_hidg_read(struct file *file, char __user *buffer, 407d7428bc2SMaxim Devaev size_t count, loff_t *ptr) 408d7428bc2SMaxim Devaev { 409d7428bc2SMaxim Devaev struct f_hidg *hidg = file->private_data; 410d7428bc2SMaxim Devaev 411d7428bc2SMaxim Devaev if (hidg->use_out_ep) 412d7428bc2SMaxim Devaev return f_hidg_intout_read(file, buffer, count, ptr); 413d7428bc2SMaxim Devaev else 414d7428bc2SMaxim Devaev return f_hidg_ssreport_read(file, buffer, count, ptr); 415d7428bc2SMaxim Devaev } 416d7428bc2SMaxim Devaev 41700a2430fSAndrzej Pietrasiewicz static void f_hidg_req_complete(struct usb_ep *ep, struct usb_request *req) 41800a2430fSAndrzej Pietrasiewicz { 41900a2430fSAndrzej Pietrasiewicz struct f_hidg *hidg = (struct f_hidg *)ep->driver_data; 42033e4c1a9SKrzysztof Opasiak unsigned long flags; 42100a2430fSAndrzej Pietrasiewicz 42200a2430fSAndrzej Pietrasiewicz if (req->status != 0) { 42300a2430fSAndrzej Pietrasiewicz ERROR(hidg->func.config->cdev, 42400a2430fSAndrzej Pietrasiewicz "End Point Request ERROR: %d\n", req->status); 42500a2430fSAndrzej Pietrasiewicz } 42600a2430fSAndrzej Pietrasiewicz 42733e4c1a9SKrzysztof Opasiak spin_lock_irqsave(&hidg->write_spinlock, flags); 42800a2430fSAndrzej Pietrasiewicz hidg->write_pending = 0; 42933e4c1a9SKrzysztof Opasiak spin_unlock_irqrestore(&hidg->write_spinlock, flags); 43000a2430fSAndrzej Pietrasiewicz wake_up(&hidg->write_queue); 43100a2430fSAndrzej Pietrasiewicz } 43200a2430fSAndrzej Pietrasiewicz 43300a2430fSAndrzej Pietrasiewicz static ssize_t f_hidg_write(struct file *file, const char __user *buffer, 43400a2430fSAndrzej Pietrasiewicz size_t count, loff_t *offp) 43500a2430fSAndrzej Pietrasiewicz { 43600a2430fSAndrzej Pietrasiewicz struct f_hidg *hidg = file->private_data; 437749494b6SKrzysztof Opasiak struct usb_request *req; 43833e4c1a9SKrzysztof Opasiak unsigned long flags; 43900a2430fSAndrzej Pietrasiewicz ssize_t status = -ENOMEM; 44000a2430fSAndrzej Pietrasiewicz 44133e4c1a9SKrzysztof Opasiak spin_lock_irqsave(&hidg->write_spinlock, flags); 44200a2430fSAndrzej Pietrasiewicz 4432867652eSPhil Elwell if (!hidg->req) { 4442867652eSPhil Elwell spin_unlock_irqrestore(&hidg->write_spinlock, flags); 4452867652eSPhil Elwell return -ESHUTDOWN; 4462867652eSPhil Elwell } 4472867652eSPhil Elwell 44800a2430fSAndrzej Pietrasiewicz #define WRITE_COND (!hidg->write_pending) 449749494b6SKrzysztof Opasiak try_again: 45000a2430fSAndrzej Pietrasiewicz /* write queue */ 45100a2430fSAndrzej Pietrasiewicz while (!WRITE_COND) { 45233e4c1a9SKrzysztof Opasiak spin_unlock_irqrestore(&hidg->write_spinlock, flags); 45300a2430fSAndrzej Pietrasiewicz if (file->f_flags & O_NONBLOCK) 45400a2430fSAndrzej Pietrasiewicz return -EAGAIN; 45500a2430fSAndrzej Pietrasiewicz 45600a2430fSAndrzej Pietrasiewicz if (wait_event_interruptible_exclusive( 45700a2430fSAndrzej Pietrasiewicz hidg->write_queue, WRITE_COND)) 45800a2430fSAndrzej Pietrasiewicz return -ERESTARTSYS; 45900a2430fSAndrzej Pietrasiewicz 46033e4c1a9SKrzysztof Opasiak spin_lock_irqsave(&hidg->write_spinlock, flags); 46100a2430fSAndrzej Pietrasiewicz } 46200a2430fSAndrzej Pietrasiewicz 46333e4c1a9SKrzysztof Opasiak hidg->write_pending = 1; 464749494b6SKrzysztof Opasiak req = hidg->req; 46500a2430fSAndrzej Pietrasiewicz count = min_t(unsigned, count, hidg->report_length); 46633e4c1a9SKrzysztof Opasiak 46733e4c1a9SKrzysztof Opasiak spin_unlock_irqrestore(&hidg->write_spinlock, flags); 46800a2430fSAndrzej Pietrasiewicz 4692867652eSPhil Elwell if (!req) { 4702867652eSPhil Elwell ERROR(hidg->func.config->cdev, "hidg->req is NULL\n"); 4712867652eSPhil Elwell status = -ESHUTDOWN; 4722867652eSPhil Elwell goto release_write_pending; 4732867652eSPhil Elwell } 4742867652eSPhil Elwell 4752867652eSPhil Elwell status = copy_from_user(req->buf, buffer, count); 47600a2430fSAndrzej Pietrasiewicz if (status != 0) { 47700a2430fSAndrzej Pietrasiewicz ERROR(hidg->func.config->cdev, 47800a2430fSAndrzej Pietrasiewicz "copy_from_user error\n"); 47933e4c1a9SKrzysztof Opasiak status = -EINVAL; 48033e4c1a9SKrzysztof Opasiak goto release_write_pending; 48100a2430fSAndrzej Pietrasiewicz } 48200a2430fSAndrzej Pietrasiewicz 483749494b6SKrzysztof Opasiak spin_lock_irqsave(&hidg->write_spinlock, flags); 484749494b6SKrzysztof Opasiak 48525cd9721SKrzysztof Opasiak /* when our function has been disabled by host */ 486749494b6SKrzysztof Opasiak if (!hidg->req) { 48725cd9721SKrzysztof Opasiak free_ep_req(hidg->in_ep, req); 488749494b6SKrzysztof Opasiak /* 489749494b6SKrzysztof Opasiak * TODO 490749494b6SKrzysztof Opasiak * Should we fail with error here? 491749494b6SKrzysztof Opasiak */ 492749494b6SKrzysztof Opasiak goto try_again; 493749494b6SKrzysztof Opasiak } 494749494b6SKrzysztof Opasiak 495749494b6SKrzysztof Opasiak req->status = 0; 496749494b6SKrzysztof Opasiak req->zero = 0; 497749494b6SKrzysztof Opasiak req->length = count; 498749494b6SKrzysztof Opasiak req->complete = f_hidg_req_complete; 499749494b6SKrzysztof Opasiak req->context = hidg; 50000a2430fSAndrzej Pietrasiewicz 501072684e8SRadoslav Gerganov spin_unlock_irqrestore(&hidg->write_spinlock, flags); 502072684e8SRadoslav Gerganov 5032867652eSPhil Elwell if (!hidg->in_ep->enabled) { 5042867652eSPhil Elwell ERROR(hidg->func.config->cdev, "in_ep is disabled\n"); 5052867652eSPhil Elwell status = -ESHUTDOWN; 506072684e8SRadoslav Gerganov goto release_write_pending; 50700a2430fSAndrzej Pietrasiewicz } 50800a2430fSAndrzej Pietrasiewicz 5092867652eSPhil Elwell status = usb_ep_queue(hidg->in_ep, req, GFP_ATOMIC); 5102867652eSPhil Elwell if (status < 0) 5112867652eSPhil Elwell goto release_write_pending; 5122867652eSPhil Elwell else 5132867652eSPhil Elwell status = count; 5142867652eSPhil Elwell 51533e4c1a9SKrzysztof Opasiak return status; 51633e4c1a9SKrzysztof Opasiak release_write_pending: 51733e4c1a9SKrzysztof Opasiak spin_lock_irqsave(&hidg->write_spinlock, flags); 51833e4c1a9SKrzysztof Opasiak hidg->write_pending = 0; 51933e4c1a9SKrzysztof Opasiak spin_unlock_irqrestore(&hidg->write_spinlock, flags); 52033e4c1a9SKrzysztof Opasiak 52133e4c1a9SKrzysztof Opasiak wake_up(&hidg->write_queue); 52200a2430fSAndrzej Pietrasiewicz 52300a2430fSAndrzej Pietrasiewicz return status; 52400a2430fSAndrzej Pietrasiewicz } 52500a2430fSAndrzej Pietrasiewicz 526afc9a42bSAl Viro static __poll_t f_hidg_poll(struct file *file, poll_table *wait) 52700a2430fSAndrzej Pietrasiewicz { 52800a2430fSAndrzej Pietrasiewicz struct f_hidg *hidg = file->private_data; 529afc9a42bSAl Viro __poll_t ret = 0; 53000a2430fSAndrzej Pietrasiewicz 53100a2430fSAndrzej Pietrasiewicz poll_wait(file, &hidg->read_queue, wait); 53200a2430fSAndrzej Pietrasiewicz poll_wait(file, &hidg->write_queue, wait); 53300a2430fSAndrzej Pietrasiewicz 53400a2430fSAndrzej Pietrasiewicz if (WRITE_COND) 535a9a08845SLinus Torvalds ret |= EPOLLOUT | EPOLLWRNORM; 53600a2430fSAndrzej Pietrasiewicz 537d7428bc2SMaxim Devaev if (hidg->use_out_ep) { 538d7428bc2SMaxim Devaev if (READ_COND_INTOUT) 539a9a08845SLinus Torvalds ret |= EPOLLIN | EPOLLRDNORM; 540d7428bc2SMaxim Devaev } else { 541d7428bc2SMaxim Devaev if (READ_COND_SSREPORT) 542d7428bc2SMaxim Devaev ret |= EPOLLIN | EPOLLRDNORM; 543d7428bc2SMaxim Devaev } 54400a2430fSAndrzej Pietrasiewicz 54500a2430fSAndrzej Pietrasiewicz return ret; 54600a2430fSAndrzej Pietrasiewicz } 54700a2430fSAndrzej Pietrasiewicz 54800a2430fSAndrzej Pietrasiewicz #undef WRITE_COND 549d7428bc2SMaxim Devaev #undef READ_COND_SSREPORT 550d7428bc2SMaxim Devaev #undef READ_COND_INTOUT 55100a2430fSAndrzej Pietrasiewicz 55200a2430fSAndrzej Pietrasiewicz static int f_hidg_release(struct inode *inode, struct file *fd) 55300a2430fSAndrzej Pietrasiewicz { 55400a2430fSAndrzej Pietrasiewicz fd->private_data = NULL; 55500a2430fSAndrzej Pietrasiewicz return 0; 55600a2430fSAndrzej Pietrasiewicz } 55700a2430fSAndrzej Pietrasiewicz 55800a2430fSAndrzej Pietrasiewicz static int f_hidg_open(struct inode *inode, struct file *fd) 55900a2430fSAndrzej Pietrasiewicz { 56000a2430fSAndrzej Pietrasiewicz struct f_hidg *hidg = 56100a2430fSAndrzej Pietrasiewicz container_of(inode->i_cdev, struct f_hidg, cdev); 56200a2430fSAndrzej Pietrasiewicz 56300a2430fSAndrzej Pietrasiewicz fd->private_data = hidg; 56400a2430fSAndrzej Pietrasiewicz 56500a2430fSAndrzej Pietrasiewicz return 0; 56600a2430fSAndrzej Pietrasiewicz } 56700a2430fSAndrzej Pietrasiewicz 56800a2430fSAndrzej Pietrasiewicz /*-------------------------------------------------------------------------*/ 56900a2430fSAndrzej Pietrasiewicz /* usb_function */ 57000a2430fSAndrzej Pietrasiewicz 57100a2430fSAndrzej Pietrasiewicz static inline struct usb_request *hidg_alloc_ep_req(struct usb_ep *ep, 57200a2430fSAndrzej Pietrasiewicz unsigned length) 57300a2430fSAndrzej Pietrasiewicz { 574aadbe812SFelipe F. Tonello return alloc_ep_req(ep, length); 57500a2430fSAndrzej Pietrasiewicz } 57600a2430fSAndrzej Pietrasiewicz 577d7428bc2SMaxim Devaev static void hidg_intout_complete(struct usb_ep *ep, struct usb_request *req) 57800a2430fSAndrzej Pietrasiewicz { 57900a2430fSAndrzej Pietrasiewicz struct f_hidg *hidg = (struct f_hidg *) req->context; 58020d2ca95SKrzysztof Opasiak struct usb_composite_dev *cdev = hidg->func.config->cdev; 58100a2430fSAndrzej Pietrasiewicz struct f_hidg_req_list *req_list; 58200a2430fSAndrzej Pietrasiewicz unsigned long flags; 58300a2430fSAndrzej Pietrasiewicz 58420d2ca95SKrzysztof Opasiak switch (req->status) { 58520d2ca95SKrzysztof Opasiak case 0: 58600a2430fSAndrzej Pietrasiewicz req_list = kzalloc(sizeof(*req_list), GFP_ATOMIC); 58720d2ca95SKrzysztof Opasiak if (!req_list) { 58820d2ca95SKrzysztof Opasiak ERROR(cdev, "Unable to allocate mem for req_list\n"); 58920d2ca95SKrzysztof Opasiak goto free_req; 59020d2ca95SKrzysztof Opasiak } 59100a2430fSAndrzej Pietrasiewicz 59200a2430fSAndrzej Pietrasiewicz req_list->req = req; 59300a2430fSAndrzej Pietrasiewicz 59433e4c1a9SKrzysztof Opasiak spin_lock_irqsave(&hidg->read_spinlock, flags); 59500a2430fSAndrzej Pietrasiewicz list_add_tail(&req_list->list, &hidg->completed_out_req); 59633e4c1a9SKrzysztof Opasiak spin_unlock_irqrestore(&hidg->read_spinlock, flags); 59700a2430fSAndrzej Pietrasiewicz 59800a2430fSAndrzej Pietrasiewicz wake_up(&hidg->read_queue); 59920d2ca95SKrzysztof Opasiak break; 60020d2ca95SKrzysztof Opasiak default: 60120d2ca95SKrzysztof Opasiak ERROR(cdev, "Set report failed %d\n", req->status); 602a74005abSGustavo A. R. Silva fallthrough; 60320d2ca95SKrzysztof Opasiak case -ECONNABORTED: /* hardware forced ep reset */ 60420d2ca95SKrzysztof Opasiak case -ECONNRESET: /* request dequeued */ 60520d2ca95SKrzysztof Opasiak case -ESHUTDOWN: /* disconnect from host */ 60620d2ca95SKrzysztof Opasiak free_req: 60720d2ca95SKrzysztof Opasiak free_ep_req(ep, req); 60820d2ca95SKrzysztof Opasiak return; 60920d2ca95SKrzysztof Opasiak } 61000a2430fSAndrzej Pietrasiewicz } 61100a2430fSAndrzej Pietrasiewicz 612d7428bc2SMaxim Devaev static void hidg_ssreport_complete(struct usb_ep *ep, struct usb_request *req) 613d7428bc2SMaxim Devaev { 614d7428bc2SMaxim Devaev struct f_hidg *hidg = (struct f_hidg *)req->context; 615d7428bc2SMaxim Devaev struct usb_composite_dev *cdev = hidg->func.config->cdev; 616d7428bc2SMaxim Devaev char *new_buf = NULL; 617d7428bc2SMaxim Devaev unsigned long flags; 618d7428bc2SMaxim Devaev 619d7428bc2SMaxim Devaev if (req->status != 0 || req->buf == NULL || req->actual == 0) { 620d7428bc2SMaxim Devaev ERROR(cdev, 621d7428bc2SMaxim Devaev "%s FAILED: status=%d, buf=%p, actual=%d\n", 622d7428bc2SMaxim Devaev __func__, req->status, req->buf, req->actual); 623d7428bc2SMaxim Devaev return; 624d7428bc2SMaxim Devaev } 625d7428bc2SMaxim Devaev 626d7428bc2SMaxim Devaev spin_lock_irqsave(&hidg->read_spinlock, flags); 627d7428bc2SMaxim Devaev 628d7428bc2SMaxim Devaev new_buf = krealloc(hidg->set_report_buf, req->actual, GFP_ATOMIC); 629d7428bc2SMaxim Devaev if (new_buf == NULL) { 630d7428bc2SMaxim Devaev spin_unlock_irqrestore(&hidg->read_spinlock, flags); 631d7428bc2SMaxim Devaev return; 632d7428bc2SMaxim Devaev } 633d7428bc2SMaxim Devaev hidg->set_report_buf = new_buf; 634d7428bc2SMaxim Devaev 635d7428bc2SMaxim Devaev hidg->set_report_length = req->actual; 636d7428bc2SMaxim Devaev memcpy(hidg->set_report_buf, req->buf, req->actual); 637d7428bc2SMaxim Devaev 638d7428bc2SMaxim Devaev spin_unlock_irqrestore(&hidg->read_spinlock, flags); 639d7428bc2SMaxim Devaev 640d7428bc2SMaxim Devaev wake_up(&hidg->read_queue); 641d7428bc2SMaxim Devaev } 642d7428bc2SMaxim Devaev 64300a2430fSAndrzej Pietrasiewicz static int hidg_setup(struct usb_function *f, 64400a2430fSAndrzej Pietrasiewicz const struct usb_ctrlrequest *ctrl) 64500a2430fSAndrzej Pietrasiewicz { 64600a2430fSAndrzej Pietrasiewicz struct f_hidg *hidg = func_to_hidg(f); 64700a2430fSAndrzej Pietrasiewicz struct usb_composite_dev *cdev = f->config->cdev; 64800a2430fSAndrzej Pietrasiewicz struct usb_request *req = cdev->req; 64900a2430fSAndrzej Pietrasiewicz int status = 0; 65000a2430fSAndrzej Pietrasiewicz __u16 value, length; 65100a2430fSAndrzej Pietrasiewicz 65200a2430fSAndrzej Pietrasiewicz value = __le16_to_cpu(ctrl->wValue); 65300a2430fSAndrzej Pietrasiewicz length = __le16_to_cpu(ctrl->wLength); 65400a2430fSAndrzej Pietrasiewicz 655c9b3bde0SJulia Lawall VDBG(cdev, 656c9b3bde0SJulia Lawall "%s crtl_request : bRequestType:0x%x bRequest:0x%x Value:0x%x\n", 657c9b3bde0SJulia Lawall __func__, ctrl->bRequestType, ctrl->bRequest, value); 65800a2430fSAndrzej Pietrasiewicz 65900a2430fSAndrzej Pietrasiewicz switch ((ctrl->bRequestType << 8) | ctrl->bRequest) { 66000a2430fSAndrzej Pietrasiewicz case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8 66100a2430fSAndrzej Pietrasiewicz | HID_REQ_GET_REPORT): 66200a2430fSAndrzej Pietrasiewicz VDBG(cdev, "get_report\n"); 66300a2430fSAndrzej Pietrasiewicz 66400a2430fSAndrzej Pietrasiewicz /* send an empty report */ 66500a2430fSAndrzej Pietrasiewicz length = min_t(unsigned, length, hidg->report_length); 66600a2430fSAndrzej Pietrasiewicz memset(req->buf, 0x0, length); 66700a2430fSAndrzej Pietrasiewicz 66800a2430fSAndrzej Pietrasiewicz goto respond; 66900a2430fSAndrzej Pietrasiewicz break; 67000a2430fSAndrzej Pietrasiewicz 67100a2430fSAndrzej Pietrasiewicz case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8 67200a2430fSAndrzej Pietrasiewicz | HID_REQ_GET_PROTOCOL): 67300a2430fSAndrzej Pietrasiewicz VDBG(cdev, "get_protocol\n"); 674b3c4ec71SAbdulhadi Mohamed length = min_t(unsigned int, length, 1); 675b3c4ec71SAbdulhadi Mohamed ((u8 *) req->buf)[0] = hidg->protocol; 676b3c4ec71SAbdulhadi Mohamed goto respond; 67700a2430fSAndrzej Pietrasiewicz break; 67800a2430fSAndrzej Pietrasiewicz 679afcff6dcSMaxim Devaev case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8 680afcff6dcSMaxim Devaev | HID_REQ_GET_IDLE): 681afcff6dcSMaxim Devaev VDBG(cdev, "get_idle\n"); 682afcff6dcSMaxim Devaev length = min_t(unsigned int, length, 1); 683afcff6dcSMaxim Devaev ((u8 *) req->buf)[0] = hidg->idle; 684afcff6dcSMaxim Devaev goto respond; 685afcff6dcSMaxim Devaev break; 686afcff6dcSMaxim Devaev 68700a2430fSAndrzej Pietrasiewicz case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8 68800a2430fSAndrzej Pietrasiewicz | HID_REQ_SET_REPORT): 6896774def6SMasanari Iida VDBG(cdev, "set_report | wLength=%d\n", ctrl->wLength); 690d7428bc2SMaxim Devaev if (hidg->use_out_ep) 69100a2430fSAndrzej Pietrasiewicz goto stall; 692d7428bc2SMaxim Devaev req->complete = hidg_ssreport_complete; 693d7428bc2SMaxim Devaev req->context = hidg; 694d7428bc2SMaxim Devaev goto respond; 69500a2430fSAndrzej Pietrasiewicz break; 69600a2430fSAndrzej Pietrasiewicz 69700a2430fSAndrzej Pietrasiewicz case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8 69800a2430fSAndrzej Pietrasiewicz | HID_REQ_SET_PROTOCOL): 69900a2430fSAndrzej Pietrasiewicz VDBG(cdev, "set_protocol\n"); 700b3c4ec71SAbdulhadi Mohamed if (value > HID_REPORT_PROTOCOL) 701b3c4ec71SAbdulhadi Mohamed goto stall; 702b3c4ec71SAbdulhadi Mohamed length = 0; 703b3c4ec71SAbdulhadi Mohamed /* 704b3c4ec71SAbdulhadi Mohamed * We assume that programs implementing the Boot protocol 705b3c4ec71SAbdulhadi Mohamed * are also compatible with the Report Protocol 706b3c4ec71SAbdulhadi Mohamed */ 707b3c4ec71SAbdulhadi Mohamed if (hidg->bInterfaceSubClass == USB_INTERFACE_SUBCLASS_BOOT) { 708b3c4ec71SAbdulhadi Mohamed hidg->protocol = value; 709b3c4ec71SAbdulhadi Mohamed goto respond; 710b3c4ec71SAbdulhadi Mohamed } 71100a2430fSAndrzej Pietrasiewicz goto stall; 71200a2430fSAndrzej Pietrasiewicz break; 71300a2430fSAndrzej Pietrasiewicz 714afcff6dcSMaxim Devaev case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8 715afcff6dcSMaxim Devaev | HID_REQ_SET_IDLE): 716afcff6dcSMaxim Devaev VDBG(cdev, "set_idle\n"); 717afcff6dcSMaxim Devaev length = 0; 718fa20badaSMaxim Devaev hidg->idle = value >> 8; 719afcff6dcSMaxim Devaev goto respond; 720afcff6dcSMaxim Devaev break; 721afcff6dcSMaxim Devaev 72200a2430fSAndrzej Pietrasiewicz case ((USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_INTERFACE) << 8 72300a2430fSAndrzej Pietrasiewicz | USB_REQ_GET_DESCRIPTOR): 72400a2430fSAndrzej Pietrasiewicz switch (value >> 8) { 72500a2430fSAndrzej Pietrasiewicz case HID_DT_HID: 726f286d487SKrzysztof Opasiak { 727f286d487SKrzysztof Opasiak struct hid_descriptor hidg_desc_copy = hidg_desc; 728f286d487SKrzysztof Opasiak 72900a2430fSAndrzej Pietrasiewicz VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: HID\n"); 730f286d487SKrzysztof Opasiak hidg_desc_copy.desc[0].bDescriptorType = HID_DT_REPORT; 731f286d487SKrzysztof Opasiak hidg_desc_copy.desc[0].wDescriptorLength = 732f286d487SKrzysztof Opasiak cpu_to_le16(hidg->report_desc_length); 733f286d487SKrzysztof Opasiak 73400a2430fSAndrzej Pietrasiewicz length = min_t(unsigned short, length, 735f286d487SKrzysztof Opasiak hidg_desc_copy.bLength); 736f286d487SKrzysztof Opasiak memcpy(req->buf, &hidg_desc_copy, length); 73700a2430fSAndrzej Pietrasiewicz goto respond; 73800a2430fSAndrzej Pietrasiewicz break; 739f286d487SKrzysztof Opasiak } 74000a2430fSAndrzej Pietrasiewicz case HID_DT_REPORT: 74100a2430fSAndrzej Pietrasiewicz VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: REPORT\n"); 74200a2430fSAndrzej Pietrasiewicz length = min_t(unsigned short, length, 74300a2430fSAndrzej Pietrasiewicz hidg->report_desc_length); 74400a2430fSAndrzej Pietrasiewicz memcpy(req->buf, hidg->report_desc, length); 74500a2430fSAndrzej Pietrasiewicz goto respond; 74600a2430fSAndrzej Pietrasiewicz break; 74700a2430fSAndrzej Pietrasiewicz 74800a2430fSAndrzej Pietrasiewicz default: 74900a2430fSAndrzej Pietrasiewicz VDBG(cdev, "Unknown descriptor request 0x%x\n", 75000a2430fSAndrzej Pietrasiewicz value >> 8); 75100a2430fSAndrzej Pietrasiewicz goto stall; 75200a2430fSAndrzej Pietrasiewicz break; 75300a2430fSAndrzej Pietrasiewicz } 75400a2430fSAndrzej Pietrasiewicz break; 75500a2430fSAndrzej Pietrasiewicz 75600a2430fSAndrzej Pietrasiewicz default: 75700a2430fSAndrzej Pietrasiewicz VDBG(cdev, "Unknown request 0x%x\n", 75800a2430fSAndrzej Pietrasiewicz ctrl->bRequest); 75900a2430fSAndrzej Pietrasiewicz goto stall; 76000a2430fSAndrzej Pietrasiewicz break; 76100a2430fSAndrzej Pietrasiewicz } 76200a2430fSAndrzej Pietrasiewicz 76300a2430fSAndrzej Pietrasiewicz stall: 76400a2430fSAndrzej Pietrasiewicz return -EOPNOTSUPP; 76500a2430fSAndrzej Pietrasiewicz 76600a2430fSAndrzej Pietrasiewicz respond: 76700a2430fSAndrzej Pietrasiewicz req->zero = 0; 76800a2430fSAndrzej Pietrasiewicz req->length = length; 76900a2430fSAndrzej Pietrasiewicz status = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC); 77000a2430fSAndrzej Pietrasiewicz if (status < 0) 77100a2430fSAndrzej Pietrasiewicz ERROR(cdev, "usb_ep_queue error on ep0 %d\n", value); 77200a2430fSAndrzej Pietrasiewicz return status; 77300a2430fSAndrzej Pietrasiewicz } 77400a2430fSAndrzej Pietrasiewicz 77500a2430fSAndrzej Pietrasiewicz static void hidg_disable(struct usb_function *f) 77600a2430fSAndrzej Pietrasiewicz { 77700a2430fSAndrzej Pietrasiewicz struct f_hidg *hidg = func_to_hidg(f); 77800a2430fSAndrzej Pietrasiewicz struct f_hidg_req_list *list, *next; 779aa65d11aSKrzysztof Opasiak unsigned long flags; 78000a2430fSAndrzej Pietrasiewicz 78100a2430fSAndrzej Pietrasiewicz usb_ep_disable(hidg->in_ep); 782d7428bc2SMaxim Devaev 783d7428bc2SMaxim Devaev if (hidg->out_ep) { 78400a2430fSAndrzej Pietrasiewicz usb_ep_disable(hidg->out_ep); 78500a2430fSAndrzej Pietrasiewicz 78633e4c1a9SKrzysztof Opasiak spin_lock_irqsave(&hidg->read_spinlock, flags); 78700a2430fSAndrzej Pietrasiewicz list_for_each_entry_safe(list, next, &hidg->completed_out_req, list) { 788aa65d11aSKrzysztof Opasiak free_ep_req(hidg->out_ep, list->req); 78900a2430fSAndrzej Pietrasiewicz list_del(&list->list); 79000a2430fSAndrzej Pietrasiewicz kfree(list); 79100a2430fSAndrzej Pietrasiewicz } 79233e4c1a9SKrzysztof Opasiak spin_unlock_irqrestore(&hidg->read_spinlock, flags); 793d7428bc2SMaxim Devaev } 794749494b6SKrzysztof Opasiak 795749494b6SKrzysztof Opasiak spin_lock_irqsave(&hidg->write_spinlock, flags); 796749494b6SKrzysztof Opasiak if (!hidg->write_pending) { 797749494b6SKrzysztof Opasiak free_ep_req(hidg->in_ep, hidg->req); 798749494b6SKrzysztof Opasiak hidg->write_pending = 1; 799749494b6SKrzysztof Opasiak } 800749494b6SKrzysztof Opasiak 801749494b6SKrzysztof Opasiak hidg->req = NULL; 802749494b6SKrzysztof Opasiak spin_unlock_irqrestore(&hidg->write_spinlock, flags); 80300a2430fSAndrzej Pietrasiewicz } 80400a2430fSAndrzej Pietrasiewicz 80500a2430fSAndrzej Pietrasiewicz static int hidg_set_alt(struct usb_function *f, unsigned intf, unsigned alt) 80600a2430fSAndrzej Pietrasiewicz { 80700a2430fSAndrzej Pietrasiewicz struct usb_composite_dev *cdev = f->config->cdev; 80800a2430fSAndrzej Pietrasiewicz struct f_hidg *hidg = func_to_hidg(f); 809749494b6SKrzysztof Opasiak struct usb_request *req_in = NULL; 810749494b6SKrzysztof Opasiak unsigned long flags; 81100a2430fSAndrzej Pietrasiewicz int i, status = 0; 81200a2430fSAndrzej Pietrasiewicz 81300a2430fSAndrzej Pietrasiewicz VDBG(cdev, "hidg_set_alt intf:%d alt:%d\n", intf, alt); 81400a2430fSAndrzej Pietrasiewicz 81500a2430fSAndrzej Pietrasiewicz if (hidg->in_ep != NULL) { 81600a2430fSAndrzej Pietrasiewicz /* restart endpoint */ 81700a2430fSAndrzej Pietrasiewicz usb_ep_disable(hidg->in_ep); 81800a2430fSAndrzej Pietrasiewicz 81900a2430fSAndrzej Pietrasiewicz status = config_ep_by_speed(f->config->cdev->gadget, f, 82000a2430fSAndrzej Pietrasiewicz hidg->in_ep); 82100a2430fSAndrzej Pietrasiewicz if (status) { 82200a2430fSAndrzej Pietrasiewicz ERROR(cdev, "config_ep_by_speed FAILED!\n"); 82300a2430fSAndrzej Pietrasiewicz goto fail; 82400a2430fSAndrzej Pietrasiewicz } 82500a2430fSAndrzej Pietrasiewicz status = usb_ep_enable(hidg->in_ep); 82600a2430fSAndrzej Pietrasiewicz if (status < 0) { 82700a2430fSAndrzej Pietrasiewicz ERROR(cdev, "Enable IN endpoint FAILED!\n"); 82800a2430fSAndrzej Pietrasiewicz goto fail; 82900a2430fSAndrzej Pietrasiewicz } 83000a2430fSAndrzej Pietrasiewicz hidg->in_ep->driver_data = hidg; 831749494b6SKrzysztof Opasiak 832749494b6SKrzysztof Opasiak req_in = hidg_alloc_ep_req(hidg->in_ep, hidg->report_length); 833749494b6SKrzysztof Opasiak if (!req_in) { 834749494b6SKrzysztof Opasiak status = -ENOMEM; 835749494b6SKrzysztof Opasiak goto disable_ep_in; 836749494b6SKrzysztof Opasiak } 83700a2430fSAndrzej Pietrasiewicz } 83800a2430fSAndrzej Pietrasiewicz 839d7428bc2SMaxim Devaev if (hidg->use_out_ep && hidg->out_ep != NULL) { 84000a2430fSAndrzej Pietrasiewicz /* restart endpoint */ 84100a2430fSAndrzej Pietrasiewicz usb_ep_disable(hidg->out_ep); 84200a2430fSAndrzej Pietrasiewicz 84300a2430fSAndrzej Pietrasiewicz status = config_ep_by_speed(f->config->cdev->gadget, f, 84400a2430fSAndrzej Pietrasiewicz hidg->out_ep); 84500a2430fSAndrzej Pietrasiewicz if (status) { 84600a2430fSAndrzej Pietrasiewicz ERROR(cdev, "config_ep_by_speed FAILED!\n"); 847749494b6SKrzysztof Opasiak goto free_req_in; 84800a2430fSAndrzej Pietrasiewicz } 84900a2430fSAndrzej Pietrasiewicz status = usb_ep_enable(hidg->out_ep); 85000a2430fSAndrzej Pietrasiewicz if (status < 0) { 85143aef5c2SDavid Lechner ERROR(cdev, "Enable OUT endpoint FAILED!\n"); 852749494b6SKrzysztof Opasiak goto free_req_in; 85300a2430fSAndrzej Pietrasiewicz } 85400a2430fSAndrzej Pietrasiewicz hidg->out_ep->driver_data = hidg; 85500a2430fSAndrzej Pietrasiewicz 85600a2430fSAndrzej Pietrasiewicz /* 85700a2430fSAndrzej Pietrasiewicz * allocate a bunch of read buffers and queue them all at once. 85800a2430fSAndrzej Pietrasiewicz */ 85900a2430fSAndrzej Pietrasiewicz for (i = 0; i < hidg->qlen && status == 0; i++) { 86000a2430fSAndrzej Pietrasiewicz struct usb_request *req = 86100a2430fSAndrzej Pietrasiewicz hidg_alloc_ep_req(hidg->out_ep, 86200a2430fSAndrzej Pietrasiewicz hidg->report_length); 86300a2430fSAndrzej Pietrasiewicz if (req) { 864d7428bc2SMaxim Devaev req->complete = hidg_intout_complete; 86500a2430fSAndrzej Pietrasiewicz req->context = hidg; 86600a2430fSAndrzej Pietrasiewicz status = usb_ep_queue(hidg->out_ep, req, 86700a2430fSAndrzej Pietrasiewicz GFP_ATOMIC); 868749494b6SKrzysztof Opasiak if (status) { 86900a2430fSAndrzej Pietrasiewicz ERROR(cdev, "%s queue req --> %d\n", 87000a2430fSAndrzej Pietrasiewicz hidg->out_ep->name, status); 871749494b6SKrzysztof Opasiak free_ep_req(hidg->out_ep, req); 872749494b6SKrzysztof Opasiak } 87300a2430fSAndrzej Pietrasiewicz } else { 87400a2430fSAndrzej Pietrasiewicz status = -ENOMEM; 875749494b6SKrzysztof Opasiak goto disable_out_ep; 87600a2430fSAndrzej Pietrasiewicz } 87700a2430fSAndrzej Pietrasiewicz } 87800a2430fSAndrzej Pietrasiewicz } 87900a2430fSAndrzej Pietrasiewicz 880749494b6SKrzysztof Opasiak if (hidg->in_ep != NULL) { 881749494b6SKrzysztof Opasiak spin_lock_irqsave(&hidg->write_spinlock, flags); 882749494b6SKrzysztof Opasiak hidg->req = req_in; 883749494b6SKrzysztof Opasiak hidg->write_pending = 0; 884749494b6SKrzysztof Opasiak spin_unlock_irqrestore(&hidg->write_spinlock, flags); 885749494b6SKrzysztof Opasiak 886749494b6SKrzysztof Opasiak wake_up(&hidg->write_queue); 887749494b6SKrzysztof Opasiak } 888749494b6SKrzysztof Opasiak return 0; 889749494b6SKrzysztof Opasiak disable_out_ep: 890d7428bc2SMaxim Devaev if (hidg->out_ep) 891749494b6SKrzysztof Opasiak usb_ep_disable(hidg->out_ep); 892749494b6SKrzysztof Opasiak free_req_in: 893749494b6SKrzysztof Opasiak if (req_in) 894749494b6SKrzysztof Opasiak free_ep_req(hidg->in_ep, req_in); 895749494b6SKrzysztof Opasiak 896749494b6SKrzysztof Opasiak disable_ep_in: 897749494b6SKrzysztof Opasiak if (hidg->in_ep) 898749494b6SKrzysztof Opasiak usb_ep_disable(hidg->in_ep); 899749494b6SKrzysztof Opasiak 90000a2430fSAndrzej Pietrasiewicz fail: 90100a2430fSAndrzej Pietrasiewicz return status; 90200a2430fSAndrzej Pietrasiewicz } 90300a2430fSAndrzej Pietrasiewicz 9047a3cc461SLad, Prabhakar static const struct file_operations f_hidg_fops = { 90500a2430fSAndrzej Pietrasiewicz .owner = THIS_MODULE, 90600a2430fSAndrzej Pietrasiewicz .open = f_hidg_open, 90700a2430fSAndrzej Pietrasiewicz .release = f_hidg_release, 90800a2430fSAndrzej Pietrasiewicz .write = f_hidg_write, 90900a2430fSAndrzej Pietrasiewicz .read = f_hidg_read, 91000a2430fSAndrzej Pietrasiewicz .poll = f_hidg_poll, 91100a2430fSAndrzej Pietrasiewicz .llseek = noop_llseek, 91200a2430fSAndrzej Pietrasiewicz }; 91300a2430fSAndrzej Pietrasiewicz 914cb382536SAndrzej Pietrasiewicz static int hidg_bind(struct usb_configuration *c, struct usb_function *f) 91500a2430fSAndrzej Pietrasiewicz { 91600a2430fSAndrzej Pietrasiewicz struct usb_ep *ep; 91700a2430fSAndrzej Pietrasiewicz struct f_hidg *hidg = func_to_hidg(f); 9185ca8d3ecSAndrzej Pietrasiewicz struct usb_string *us; 91900a2430fSAndrzej Pietrasiewicz int status; 92000a2430fSAndrzej Pietrasiewicz 921cb382536SAndrzej Pietrasiewicz /* maybe allocate device-global string IDs, and patch descriptors */ 9225ca8d3ecSAndrzej Pietrasiewicz us = usb_gstrings_attach(c->cdev, ct_func_strings, 9235ca8d3ecSAndrzej Pietrasiewicz ARRAY_SIZE(ct_func_string_defs)); 9245ca8d3ecSAndrzej Pietrasiewicz if (IS_ERR(us)) 9255ca8d3ecSAndrzej Pietrasiewicz return PTR_ERR(us); 9265ca8d3ecSAndrzej Pietrasiewicz hidg_interface_desc.iInterface = us[CT_FUNC_HID_IDX].id; 927cb382536SAndrzej Pietrasiewicz 92800a2430fSAndrzej Pietrasiewicz /* allocate instance-specific interface IDs, and patch descriptors */ 92900a2430fSAndrzej Pietrasiewicz status = usb_interface_id(c, f); 93000a2430fSAndrzej Pietrasiewicz if (status < 0) 93100a2430fSAndrzej Pietrasiewicz goto fail; 93200a2430fSAndrzej Pietrasiewicz hidg_interface_desc.bInterfaceNumber = status; 93300a2430fSAndrzej Pietrasiewicz 93400a2430fSAndrzej Pietrasiewicz /* allocate instance-specific endpoints */ 93500a2430fSAndrzej Pietrasiewicz status = -ENODEV; 93600a2430fSAndrzej Pietrasiewicz ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_in_ep_desc); 93700a2430fSAndrzej Pietrasiewicz if (!ep) 93800a2430fSAndrzej Pietrasiewicz goto fail; 93900a2430fSAndrzej Pietrasiewicz hidg->in_ep = ep; 94000a2430fSAndrzej Pietrasiewicz 941d7428bc2SMaxim Devaev hidg->out_ep = NULL; 942d7428bc2SMaxim Devaev if (hidg->use_out_ep) { 94300a2430fSAndrzej Pietrasiewicz ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_out_ep_desc); 94400a2430fSAndrzej Pietrasiewicz if (!ep) 94500a2430fSAndrzej Pietrasiewicz goto fail; 94600a2430fSAndrzej Pietrasiewicz hidg->out_ep = ep; 947d7428bc2SMaxim Devaev } 948d7428bc2SMaxim Devaev 949d7428bc2SMaxim Devaev /* used only if use_out_ep == 1 */ 950d7428bc2SMaxim Devaev hidg->set_report_buf = NULL; 95100a2430fSAndrzej Pietrasiewicz 95200a2430fSAndrzej Pietrasiewicz /* set descriptor dynamic values */ 95300a2430fSAndrzej Pietrasiewicz hidg_interface_desc.bInterfaceSubClass = hidg->bInterfaceSubClass; 95400a2430fSAndrzej Pietrasiewicz hidg_interface_desc.bInterfaceProtocol = hidg->bInterfaceProtocol; 955d7428bc2SMaxim Devaev hidg_interface_desc.bNumEndpoints = hidg->use_out_ep ? 2 : 1; 956b3c4ec71SAbdulhadi Mohamed hidg->protocol = HID_REPORT_PROTOCOL; 957afcff6dcSMaxim Devaev hidg->idle = 1; 958dbf499cfSJanusz Dziedzic hidg_ss_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length); 959dbf499cfSJanusz Dziedzic hidg_ss_in_comp_desc.wBytesPerInterval = 960dbf499cfSJanusz Dziedzic cpu_to_le16(hidg->report_length); 96100a2430fSAndrzej Pietrasiewicz hidg_hs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length); 96200a2430fSAndrzej Pietrasiewicz hidg_fs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length); 963dbf499cfSJanusz Dziedzic hidg_ss_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length); 964dbf499cfSJanusz Dziedzic hidg_ss_out_comp_desc.wBytesPerInterval = 965dbf499cfSJanusz Dziedzic cpu_to_le16(hidg->report_length); 96600a2430fSAndrzej Pietrasiewicz hidg_hs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length); 96700a2430fSAndrzej Pietrasiewicz hidg_fs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length); 968f286d487SKrzysztof Opasiak /* 969f286d487SKrzysztof Opasiak * We can use hidg_desc struct here but we should not relay 970f286d487SKrzysztof Opasiak * that its content won't change after returning from this function. 971f286d487SKrzysztof Opasiak */ 97200a2430fSAndrzej Pietrasiewicz hidg_desc.desc[0].bDescriptorType = HID_DT_REPORT; 97300a2430fSAndrzej Pietrasiewicz hidg_desc.desc[0].wDescriptorLength = 97400a2430fSAndrzej Pietrasiewicz cpu_to_le16(hidg->report_desc_length); 97500a2430fSAndrzej Pietrasiewicz 97600a2430fSAndrzej Pietrasiewicz hidg_hs_in_ep_desc.bEndpointAddress = 97700a2430fSAndrzej Pietrasiewicz hidg_fs_in_ep_desc.bEndpointAddress; 97800a2430fSAndrzej Pietrasiewicz hidg_hs_out_ep_desc.bEndpointAddress = 97900a2430fSAndrzej Pietrasiewicz hidg_fs_out_ep_desc.bEndpointAddress; 98000a2430fSAndrzej Pietrasiewicz 981dbf499cfSJanusz Dziedzic hidg_ss_in_ep_desc.bEndpointAddress = 982dbf499cfSJanusz Dziedzic hidg_fs_in_ep_desc.bEndpointAddress; 983dbf499cfSJanusz Dziedzic hidg_ss_out_ep_desc.bEndpointAddress = 984dbf499cfSJanusz Dziedzic hidg_fs_out_ep_desc.bEndpointAddress; 985dbf499cfSJanusz Dziedzic 986d7428bc2SMaxim Devaev if (hidg->use_out_ep) 987d7428bc2SMaxim Devaev status = usb_assign_descriptors(f, 988d7428bc2SMaxim Devaev hidg_fs_descriptors_intout, 989d7428bc2SMaxim Devaev hidg_hs_descriptors_intout, 990d7428bc2SMaxim Devaev hidg_ss_descriptors_intout, 991d7428bc2SMaxim Devaev hidg_ss_descriptors_intout); 992d7428bc2SMaxim Devaev else 993d7428bc2SMaxim Devaev status = usb_assign_descriptors(f, 994d7428bc2SMaxim Devaev hidg_fs_descriptors_ssreport, 995d7428bc2SMaxim Devaev hidg_hs_descriptors_ssreport, 996d7428bc2SMaxim Devaev hidg_ss_descriptors_ssreport, 997d7428bc2SMaxim Devaev hidg_ss_descriptors_ssreport); 998d7428bc2SMaxim Devaev 99900a2430fSAndrzej Pietrasiewicz if (status) 100000a2430fSAndrzej Pietrasiewicz goto fail; 100100a2430fSAndrzej Pietrasiewicz 100233e4c1a9SKrzysztof Opasiak spin_lock_init(&hidg->write_spinlock); 1003749494b6SKrzysztof Opasiak hidg->write_pending = 1; 1004749494b6SKrzysztof Opasiak hidg->req = NULL; 100533e4c1a9SKrzysztof Opasiak spin_lock_init(&hidg->read_spinlock); 100600a2430fSAndrzej Pietrasiewicz init_waitqueue_head(&hidg->write_queue); 100700a2430fSAndrzej Pietrasiewicz init_waitqueue_head(&hidg->read_queue); 100800a2430fSAndrzej Pietrasiewicz INIT_LIST_HEAD(&hidg->completed_out_req); 100900a2430fSAndrzej Pietrasiewicz 101000a2430fSAndrzej Pietrasiewicz /* create char device */ 101100a2430fSAndrzej Pietrasiewicz cdev_init(&hidg->cdev, &f_hidg_fops); 101289ff3dfaSJohn Keeping status = cdev_device_add(&hidg->cdev, &hidg->dev); 101300a2430fSAndrzej Pietrasiewicz if (status) 1014d12a8727SPavitrakumar Managutte goto fail_free_descs; 101500a2430fSAndrzej Pietrasiewicz 101600a2430fSAndrzej Pietrasiewicz return 0; 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); 1103d7428bc2SMaxim 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, 1163d7428bc2SMaxim 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); 124789ff3dfaSJohn Keeping put_device(&hidg->dev); 124821a9476aSAndrzej Pietrasiewicz mutex_lock(&opts->lock); 124921a9476aSAndrzej Pietrasiewicz --opts->refcnt; 125021a9476aSAndrzej Pietrasiewicz mutex_unlock(&opts->lock); 1251cb382536SAndrzej Pietrasiewicz } 1252cb382536SAndrzej Pietrasiewicz 125300a2430fSAndrzej Pietrasiewicz static void hidg_unbind(struct usb_configuration *c, struct usb_function *f) 125400a2430fSAndrzej Pietrasiewicz { 125500a2430fSAndrzej Pietrasiewicz struct f_hidg *hidg = func_to_hidg(f); 125600a2430fSAndrzej Pietrasiewicz 125789ff3dfaSJohn Keeping cdev_device_del(&hidg->cdev, &hidg->dev); 125800a2430fSAndrzej Pietrasiewicz 125900a2430fSAndrzej Pietrasiewicz usb_free_all_descriptors(f); 126000a2430fSAndrzej Pietrasiewicz } 126100a2430fSAndrzej Pietrasiewicz 12620fc57ea0SFengguang Wu static struct usb_function *hidg_alloc(struct usb_function_instance *fi) 126300a2430fSAndrzej Pietrasiewicz { 126400a2430fSAndrzej Pietrasiewicz struct f_hidg *hidg; 1265cb382536SAndrzej Pietrasiewicz struct f_hid_opts *opts; 126689ff3dfaSJohn Keeping int ret; 126700a2430fSAndrzej Pietrasiewicz 126800a2430fSAndrzej Pietrasiewicz /* allocate and initialize one new instance */ 1269cb382536SAndrzej Pietrasiewicz hidg = kzalloc(sizeof(*hidg), GFP_KERNEL); 127000a2430fSAndrzej Pietrasiewicz if (!hidg) 1271cb382536SAndrzej Pietrasiewicz return ERR_PTR(-ENOMEM); 127200a2430fSAndrzej Pietrasiewicz 1273cb382536SAndrzej Pietrasiewicz opts = container_of(fi, struct f_hid_opts, func_inst); 1274cb382536SAndrzej Pietrasiewicz 127521a9476aSAndrzej Pietrasiewicz mutex_lock(&opts->lock); 127621a9476aSAndrzej Pietrasiewicz 127789ff3dfaSJohn Keeping device_initialize(&hidg->dev); 127889ff3dfaSJohn Keeping hidg->dev.release = hidg_release; 1279*99f2d956SIvan Orlov hidg->dev.class = &hidg_class; 128089ff3dfaSJohn Keeping hidg->dev.devt = MKDEV(major, opts->minor); 128189ff3dfaSJohn Keeping ret = dev_set_name(&hidg->dev, "hidg%d", opts->minor); 1282944fe915SJohn Keeping if (ret) 1283944fe915SJohn Keeping goto err_unlock; 128489ff3dfaSJohn Keeping 1285cb382536SAndrzej Pietrasiewicz hidg->bInterfaceSubClass = opts->subclass; 1286cb382536SAndrzej Pietrasiewicz hidg->bInterfaceProtocol = opts->protocol; 1287cb382536SAndrzej Pietrasiewicz hidg->report_length = opts->report_length; 1288cb382536SAndrzej Pietrasiewicz hidg->report_desc_length = opts->report_desc_length; 1289cb382536SAndrzej Pietrasiewicz if (opts->report_desc) { 129089ff3dfaSJohn Keeping hidg->report_desc = devm_kmemdup(&hidg->dev, opts->report_desc, 1291cb382536SAndrzej Pietrasiewicz opts->report_desc_length, 129200a2430fSAndrzej Pietrasiewicz GFP_KERNEL); 129300a2430fSAndrzej Pietrasiewicz if (!hidg->report_desc) { 1294944fe915SJohn Keeping ret = -ENOMEM; 1295944fe915SJohn Keeping goto err_put_device; 1296cb382536SAndrzej Pietrasiewicz } 129700a2430fSAndrzej Pietrasiewicz } 1298d7428bc2SMaxim Devaev hidg->use_out_ep = !opts->no_out_endpoint; 129900a2430fSAndrzej Pietrasiewicz 1300944fe915SJohn Keeping ++opts->refcnt; 130121a9476aSAndrzej Pietrasiewicz mutex_unlock(&opts->lock); 130221a9476aSAndrzej Pietrasiewicz 130300a2430fSAndrzej Pietrasiewicz hidg->func.name = "hid"; 130400a2430fSAndrzej Pietrasiewicz hidg->func.bind = hidg_bind; 130500a2430fSAndrzej Pietrasiewicz hidg->func.unbind = hidg_unbind; 130600a2430fSAndrzej Pietrasiewicz hidg->func.set_alt = hidg_set_alt; 130700a2430fSAndrzej Pietrasiewicz hidg->func.disable = hidg_disable; 130800a2430fSAndrzej Pietrasiewicz hidg->func.setup = hidg_setup; 1309cb382536SAndrzej Pietrasiewicz hidg->func.free_func = hidg_free; 131000a2430fSAndrzej Pietrasiewicz 131129a812e4SWei Ming Chen /* this could be made configurable at some point */ 131200a2430fSAndrzej Pietrasiewicz hidg->qlen = 4; 131300a2430fSAndrzej Pietrasiewicz 1314cb382536SAndrzej Pietrasiewicz return &hidg->func; 1315944fe915SJohn Keeping 1316944fe915SJohn Keeping err_put_device: 1317944fe915SJohn Keeping put_device(&hidg->dev); 1318944fe915SJohn Keeping err_unlock: 1319944fe915SJohn Keeping mutex_unlock(&opts->lock); 1320944fe915SJohn Keeping return ERR_PTR(ret); 132100a2430fSAndrzej Pietrasiewicz } 132200a2430fSAndrzej Pietrasiewicz 1323cb382536SAndrzej Pietrasiewicz DECLARE_USB_FUNCTION_INIT(hid, hidg_alloc_inst, hidg_alloc); 1324cb382536SAndrzej Pietrasiewicz MODULE_LICENSE("GPL"); 1325cb382536SAndrzej Pietrasiewicz MODULE_AUTHOR("Fabien Chouteau"); 1326cb382536SAndrzej Pietrasiewicz 1327cb382536SAndrzej Pietrasiewicz int ghid_setup(struct usb_gadget *g, int count) 132800a2430fSAndrzej Pietrasiewicz { 132900a2430fSAndrzej Pietrasiewicz int status; 133000a2430fSAndrzej Pietrasiewicz dev_t dev; 133100a2430fSAndrzej Pietrasiewicz 1332*99f2d956SIvan Orlov status = class_register(&hidg_class); 1333*99f2d956SIvan Orlov if (status) 13340448d38cSDan Carpenter return status; 133500a2430fSAndrzej Pietrasiewicz 133600a2430fSAndrzej Pietrasiewicz status = alloc_chrdev_region(&dev, 0, count, "hidg"); 13370448d38cSDan Carpenter if (status) { 1338*99f2d956SIvan Orlov class_unregister(&hidg_class); 133900a2430fSAndrzej Pietrasiewicz return status; 134000a2430fSAndrzej Pietrasiewicz } 134100a2430fSAndrzej Pietrasiewicz 13420448d38cSDan Carpenter major = MAJOR(dev); 13430448d38cSDan Carpenter minors = count; 13440448d38cSDan Carpenter 13450448d38cSDan Carpenter return 0; 134600a2430fSAndrzej Pietrasiewicz } 134700a2430fSAndrzej Pietrasiewicz 134800a2430fSAndrzej Pietrasiewicz void ghid_cleanup(void) 134900a2430fSAndrzej Pietrasiewicz { 135000a2430fSAndrzej Pietrasiewicz if (major) { 135100a2430fSAndrzej Pietrasiewicz unregister_chrdev_region(MKDEV(major, 0), minors); 135200a2430fSAndrzej Pietrasiewicz major = minors = 0; 135300a2430fSAndrzej Pietrasiewicz } 135400a2430fSAndrzej Pietrasiewicz 1355*99f2d956SIvan Orlov class_unregister(&hidg_class); 135600a2430fSAndrzej Pietrasiewicz } 1357