100a2430fSAndrzej Pietrasiewicz /* 200a2430fSAndrzej Pietrasiewicz * f_hid.c -- USB HID function driver 300a2430fSAndrzej Pietrasiewicz * 400a2430fSAndrzej Pietrasiewicz * Copyright (C) 2010 Fabien Chouteau <fabien.chouteau@barco.com> 500a2430fSAndrzej Pietrasiewicz * 600a2430fSAndrzej Pietrasiewicz * This program is free software; you can redistribute it and/or modify 700a2430fSAndrzej Pietrasiewicz * it under the terms of the GNU General Public License as published by 800a2430fSAndrzej Pietrasiewicz * the Free Software Foundation; either version 2 of the License, or 900a2430fSAndrzej Pietrasiewicz * (at your option) any later version. 1000a2430fSAndrzej Pietrasiewicz */ 1100a2430fSAndrzej Pietrasiewicz 1200a2430fSAndrzej Pietrasiewicz #include <linux/kernel.h> 1300a2430fSAndrzej Pietrasiewicz #include <linux/module.h> 1400a2430fSAndrzej Pietrasiewicz #include <linux/hid.h> 15cb382536SAndrzej Pietrasiewicz #include <linux/idr.h> 1600a2430fSAndrzej Pietrasiewicz #include <linux/cdev.h> 1700a2430fSAndrzej Pietrasiewicz #include <linux/mutex.h> 1800a2430fSAndrzej Pietrasiewicz #include <linux/poll.h> 1900a2430fSAndrzej Pietrasiewicz #include <linux/uaccess.h> 2000a2430fSAndrzej Pietrasiewicz #include <linux/wait.h> 2100a2430fSAndrzej Pietrasiewicz #include <linux/sched.h> 2200a2430fSAndrzej Pietrasiewicz #include <linux/usb/g_hid.h> 2300a2430fSAndrzej Pietrasiewicz 2400a2430fSAndrzej Pietrasiewicz #include "u_f.h" 25cb382536SAndrzej Pietrasiewicz #include "u_hid.h" 26cb382536SAndrzej Pietrasiewicz 27cb382536SAndrzej Pietrasiewicz #define HIDG_MINORS 4 2800a2430fSAndrzej Pietrasiewicz 2900a2430fSAndrzej Pietrasiewicz static int major, minors; 3000a2430fSAndrzej Pietrasiewicz static struct class *hidg_class; 31cb382536SAndrzej Pietrasiewicz static DEFINE_IDA(hidg_ida); 32cb382536SAndrzej Pietrasiewicz static DEFINE_MUTEX(hidg_ida_lock); /* protects access to hidg_ida */ 3300a2430fSAndrzej Pietrasiewicz 3400a2430fSAndrzej Pietrasiewicz /*-------------------------------------------------------------------------*/ 3500a2430fSAndrzej Pietrasiewicz /* HID gadget struct */ 3600a2430fSAndrzej Pietrasiewicz 3700a2430fSAndrzej Pietrasiewicz struct f_hidg_req_list { 3800a2430fSAndrzej Pietrasiewicz struct usb_request *req; 3900a2430fSAndrzej Pietrasiewicz unsigned int pos; 4000a2430fSAndrzej Pietrasiewicz struct list_head list; 4100a2430fSAndrzej Pietrasiewicz }; 4200a2430fSAndrzej Pietrasiewicz 4300a2430fSAndrzej Pietrasiewicz struct f_hidg { 4400a2430fSAndrzej Pietrasiewicz /* configuration */ 4500a2430fSAndrzej Pietrasiewicz unsigned char bInterfaceSubClass; 4600a2430fSAndrzej Pietrasiewicz unsigned char bInterfaceProtocol; 4700a2430fSAndrzej Pietrasiewicz unsigned short report_desc_length; 4800a2430fSAndrzej Pietrasiewicz char *report_desc; 4900a2430fSAndrzej Pietrasiewicz unsigned short report_length; 5000a2430fSAndrzej Pietrasiewicz 5100a2430fSAndrzej Pietrasiewicz /* recv report */ 5200a2430fSAndrzej Pietrasiewicz struct list_head completed_out_req; 5333e4c1a9SKrzysztof Opasiak spinlock_t read_spinlock; 5400a2430fSAndrzej Pietrasiewicz wait_queue_head_t read_queue; 5500a2430fSAndrzej Pietrasiewicz unsigned int qlen; 5600a2430fSAndrzej Pietrasiewicz 5700a2430fSAndrzej Pietrasiewicz /* send report */ 5833e4c1a9SKrzysztof Opasiak spinlock_t write_spinlock; 5900a2430fSAndrzej Pietrasiewicz bool write_pending; 6000a2430fSAndrzej Pietrasiewicz wait_queue_head_t write_queue; 6100a2430fSAndrzej Pietrasiewicz struct usb_request *req; 6200a2430fSAndrzej Pietrasiewicz 6300a2430fSAndrzej Pietrasiewicz int minor; 6400a2430fSAndrzej Pietrasiewicz struct cdev cdev; 6500a2430fSAndrzej Pietrasiewicz struct usb_function func; 6600a2430fSAndrzej Pietrasiewicz 6700a2430fSAndrzej Pietrasiewicz struct usb_ep *in_ep; 6800a2430fSAndrzej Pietrasiewicz struct usb_ep *out_ep; 6900a2430fSAndrzej Pietrasiewicz }; 7000a2430fSAndrzej Pietrasiewicz 7100a2430fSAndrzej Pietrasiewicz static inline struct f_hidg *func_to_hidg(struct usb_function *f) 7200a2430fSAndrzej Pietrasiewicz { 7300a2430fSAndrzej Pietrasiewicz return container_of(f, struct f_hidg, func); 7400a2430fSAndrzej Pietrasiewicz } 7500a2430fSAndrzej Pietrasiewicz 7600a2430fSAndrzej Pietrasiewicz /*-------------------------------------------------------------------------*/ 7700a2430fSAndrzej Pietrasiewicz /* Static descriptors */ 7800a2430fSAndrzej Pietrasiewicz 7900a2430fSAndrzej Pietrasiewicz static struct usb_interface_descriptor hidg_interface_desc = { 8000a2430fSAndrzej Pietrasiewicz .bLength = sizeof hidg_interface_desc, 8100a2430fSAndrzej Pietrasiewicz .bDescriptorType = USB_DT_INTERFACE, 8200a2430fSAndrzej Pietrasiewicz /* .bInterfaceNumber = DYNAMIC */ 8300a2430fSAndrzej Pietrasiewicz .bAlternateSetting = 0, 8400a2430fSAndrzej Pietrasiewicz .bNumEndpoints = 2, 8500a2430fSAndrzej Pietrasiewicz .bInterfaceClass = USB_CLASS_HID, 8600a2430fSAndrzej Pietrasiewicz /* .bInterfaceSubClass = DYNAMIC */ 8700a2430fSAndrzej Pietrasiewicz /* .bInterfaceProtocol = DYNAMIC */ 8800a2430fSAndrzej Pietrasiewicz /* .iInterface = DYNAMIC */ 8900a2430fSAndrzej Pietrasiewicz }; 9000a2430fSAndrzej Pietrasiewicz 9100a2430fSAndrzej Pietrasiewicz static struct hid_descriptor hidg_desc = { 9200a2430fSAndrzej Pietrasiewicz .bLength = sizeof hidg_desc, 9300a2430fSAndrzej Pietrasiewicz .bDescriptorType = HID_DT_HID, 9400a2430fSAndrzej Pietrasiewicz .bcdHID = 0x0101, 9500a2430fSAndrzej Pietrasiewicz .bCountryCode = 0x00, 9600a2430fSAndrzej Pietrasiewicz .bNumDescriptors = 0x1, 9700a2430fSAndrzej Pietrasiewicz /*.desc[0].bDescriptorType = DYNAMIC */ 9800a2430fSAndrzej Pietrasiewicz /*.desc[0].wDescriptorLenght = DYNAMIC */ 9900a2430fSAndrzej Pietrasiewicz }; 10000a2430fSAndrzej Pietrasiewicz 101dbf499cfSJanusz Dziedzic /* Super-Speed Support */ 102dbf499cfSJanusz Dziedzic 103dbf499cfSJanusz Dziedzic static struct usb_endpoint_descriptor hidg_ss_in_ep_desc = { 104dbf499cfSJanusz Dziedzic .bLength = USB_DT_ENDPOINT_SIZE, 105dbf499cfSJanusz Dziedzic .bDescriptorType = USB_DT_ENDPOINT, 106dbf499cfSJanusz Dziedzic .bEndpointAddress = USB_DIR_IN, 107dbf499cfSJanusz Dziedzic .bmAttributes = USB_ENDPOINT_XFER_INT, 108dbf499cfSJanusz Dziedzic /*.wMaxPacketSize = DYNAMIC */ 109dbf499cfSJanusz Dziedzic .bInterval = 4, /* FIXME: Add this field in the 110dbf499cfSJanusz Dziedzic * HID gadget configuration? 111dbf499cfSJanusz Dziedzic * (struct hidg_func_descriptor) 112dbf499cfSJanusz Dziedzic */ 113dbf499cfSJanusz Dziedzic }; 114dbf499cfSJanusz Dziedzic 115dbf499cfSJanusz Dziedzic static struct usb_ss_ep_comp_descriptor hidg_ss_in_comp_desc = { 116dbf499cfSJanusz Dziedzic .bLength = sizeof(hidg_ss_in_comp_desc), 117dbf499cfSJanusz Dziedzic .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, 118dbf499cfSJanusz Dziedzic 119dbf499cfSJanusz Dziedzic /* .bMaxBurst = 0, */ 120dbf499cfSJanusz Dziedzic /* .bmAttributes = 0, */ 121dbf499cfSJanusz Dziedzic /* .wBytesPerInterval = DYNAMIC */ 122dbf499cfSJanusz Dziedzic }; 123dbf499cfSJanusz Dziedzic 124dbf499cfSJanusz Dziedzic static struct usb_endpoint_descriptor hidg_ss_out_ep_desc = { 125dbf499cfSJanusz Dziedzic .bLength = USB_DT_ENDPOINT_SIZE, 126dbf499cfSJanusz Dziedzic .bDescriptorType = USB_DT_ENDPOINT, 127dbf499cfSJanusz Dziedzic .bEndpointAddress = USB_DIR_OUT, 128dbf499cfSJanusz Dziedzic .bmAttributes = USB_ENDPOINT_XFER_INT, 129dbf499cfSJanusz Dziedzic /*.wMaxPacketSize = DYNAMIC */ 130dbf499cfSJanusz Dziedzic .bInterval = 4, /* FIXME: Add this field in the 131dbf499cfSJanusz Dziedzic * HID gadget configuration? 132dbf499cfSJanusz Dziedzic * (struct hidg_func_descriptor) 133dbf499cfSJanusz Dziedzic */ 134dbf499cfSJanusz Dziedzic }; 135dbf499cfSJanusz Dziedzic 136dbf499cfSJanusz Dziedzic static struct usb_ss_ep_comp_descriptor hidg_ss_out_comp_desc = { 137dbf499cfSJanusz Dziedzic .bLength = sizeof(hidg_ss_out_comp_desc), 138dbf499cfSJanusz Dziedzic .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, 139dbf499cfSJanusz Dziedzic 140dbf499cfSJanusz Dziedzic /* .bMaxBurst = 0, */ 141dbf499cfSJanusz Dziedzic /* .bmAttributes = 0, */ 142dbf499cfSJanusz Dziedzic /* .wBytesPerInterval = DYNAMIC */ 143dbf499cfSJanusz Dziedzic }; 144dbf499cfSJanusz Dziedzic 145dbf499cfSJanusz Dziedzic static struct usb_descriptor_header *hidg_ss_descriptors[] = { 146dbf499cfSJanusz Dziedzic (struct usb_descriptor_header *)&hidg_interface_desc, 147dbf499cfSJanusz Dziedzic (struct usb_descriptor_header *)&hidg_desc, 148dbf499cfSJanusz Dziedzic (struct usb_descriptor_header *)&hidg_ss_in_ep_desc, 149dbf499cfSJanusz Dziedzic (struct usb_descriptor_header *)&hidg_ss_in_comp_desc, 150dbf499cfSJanusz Dziedzic (struct usb_descriptor_header *)&hidg_ss_out_ep_desc, 151dbf499cfSJanusz Dziedzic (struct usb_descriptor_header *)&hidg_ss_out_comp_desc, 152dbf499cfSJanusz Dziedzic NULL, 153dbf499cfSJanusz Dziedzic }; 154dbf499cfSJanusz Dziedzic 15500a2430fSAndrzej Pietrasiewicz /* High-Speed Support */ 15600a2430fSAndrzej Pietrasiewicz 15700a2430fSAndrzej Pietrasiewicz static struct usb_endpoint_descriptor hidg_hs_in_ep_desc = { 15800a2430fSAndrzej Pietrasiewicz .bLength = USB_DT_ENDPOINT_SIZE, 15900a2430fSAndrzej Pietrasiewicz .bDescriptorType = USB_DT_ENDPOINT, 16000a2430fSAndrzej Pietrasiewicz .bEndpointAddress = USB_DIR_IN, 16100a2430fSAndrzej Pietrasiewicz .bmAttributes = USB_ENDPOINT_XFER_INT, 16200a2430fSAndrzej Pietrasiewicz /*.wMaxPacketSize = DYNAMIC */ 16300a2430fSAndrzej Pietrasiewicz .bInterval = 4, /* FIXME: Add this field in the 16400a2430fSAndrzej Pietrasiewicz * HID gadget configuration? 16500a2430fSAndrzej Pietrasiewicz * (struct hidg_func_descriptor) 16600a2430fSAndrzej Pietrasiewicz */ 16700a2430fSAndrzej Pietrasiewicz }; 16800a2430fSAndrzej Pietrasiewicz 16900a2430fSAndrzej Pietrasiewicz static struct usb_endpoint_descriptor hidg_hs_out_ep_desc = { 17000a2430fSAndrzej Pietrasiewicz .bLength = USB_DT_ENDPOINT_SIZE, 17100a2430fSAndrzej Pietrasiewicz .bDescriptorType = USB_DT_ENDPOINT, 17200a2430fSAndrzej Pietrasiewicz .bEndpointAddress = USB_DIR_OUT, 17300a2430fSAndrzej Pietrasiewicz .bmAttributes = USB_ENDPOINT_XFER_INT, 17400a2430fSAndrzej Pietrasiewicz /*.wMaxPacketSize = DYNAMIC */ 17500a2430fSAndrzej Pietrasiewicz .bInterval = 4, /* FIXME: Add this field in the 17600a2430fSAndrzej Pietrasiewicz * HID gadget configuration? 17700a2430fSAndrzej Pietrasiewicz * (struct hidg_func_descriptor) 17800a2430fSAndrzej Pietrasiewicz */ 17900a2430fSAndrzej Pietrasiewicz }; 18000a2430fSAndrzej Pietrasiewicz 18100a2430fSAndrzej Pietrasiewicz static struct usb_descriptor_header *hidg_hs_descriptors[] = { 18200a2430fSAndrzej Pietrasiewicz (struct usb_descriptor_header *)&hidg_interface_desc, 18300a2430fSAndrzej Pietrasiewicz (struct usb_descriptor_header *)&hidg_desc, 18400a2430fSAndrzej Pietrasiewicz (struct usb_descriptor_header *)&hidg_hs_in_ep_desc, 18500a2430fSAndrzej Pietrasiewicz (struct usb_descriptor_header *)&hidg_hs_out_ep_desc, 18600a2430fSAndrzej Pietrasiewicz NULL, 18700a2430fSAndrzej Pietrasiewicz }; 18800a2430fSAndrzej Pietrasiewicz 18900a2430fSAndrzej Pietrasiewicz /* Full-Speed Support */ 19000a2430fSAndrzej Pietrasiewicz 19100a2430fSAndrzej Pietrasiewicz static struct usb_endpoint_descriptor hidg_fs_in_ep_desc = { 19200a2430fSAndrzej Pietrasiewicz .bLength = USB_DT_ENDPOINT_SIZE, 19300a2430fSAndrzej Pietrasiewicz .bDescriptorType = USB_DT_ENDPOINT, 19400a2430fSAndrzej Pietrasiewicz .bEndpointAddress = USB_DIR_IN, 19500a2430fSAndrzej Pietrasiewicz .bmAttributes = USB_ENDPOINT_XFER_INT, 19600a2430fSAndrzej Pietrasiewicz /*.wMaxPacketSize = DYNAMIC */ 19700a2430fSAndrzej Pietrasiewicz .bInterval = 10, /* FIXME: Add this field in the 19800a2430fSAndrzej Pietrasiewicz * HID gadget configuration? 19900a2430fSAndrzej Pietrasiewicz * (struct hidg_func_descriptor) 20000a2430fSAndrzej Pietrasiewicz */ 20100a2430fSAndrzej Pietrasiewicz }; 20200a2430fSAndrzej Pietrasiewicz 20300a2430fSAndrzej Pietrasiewicz static struct usb_endpoint_descriptor hidg_fs_out_ep_desc = { 20400a2430fSAndrzej Pietrasiewicz .bLength = USB_DT_ENDPOINT_SIZE, 20500a2430fSAndrzej Pietrasiewicz .bDescriptorType = USB_DT_ENDPOINT, 20600a2430fSAndrzej Pietrasiewicz .bEndpointAddress = USB_DIR_OUT, 20700a2430fSAndrzej Pietrasiewicz .bmAttributes = USB_ENDPOINT_XFER_INT, 20800a2430fSAndrzej Pietrasiewicz /*.wMaxPacketSize = DYNAMIC */ 20900a2430fSAndrzej Pietrasiewicz .bInterval = 10, /* FIXME: Add this field in the 21000a2430fSAndrzej Pietrasiewicz * HID gadget configuration? 21100a2430fSAndrzej Pietrasiewicz * (struct hidg_func_descriptor) 21200a2430fSAndrzej Pietrasiewicz */ 21300a2430fSAndrzej Pietrasiewicz }; 21400a2430fSAndrzej Pietrasiewicz 21500a2430fSAndrzej Pietrasiewicz static struct usb_descriptor_header *hidg_fs_descriptors[] = { 21600a2430fSAndrzej Pietrasiewicz (struct usb_descriptor_header *)&hidg_interface_desc, 21700a2430fSAndrzej Pietrasiewicz (struct usb_descriptor_header *)&hidg_desc, 21800a2430fSAndrzej Pietrasiewicz (struct usb_descriptor_header *)&hidg_fs_in_ep_desc, 21900a2430fSAndrzej Pietrasiewicz (struct usb_descriptor_header *)&hidg_fs_out_ep_desc, 22000a2430fSAndrzej Pietrasiewicz NULL, 22100a2430fSAndrzej Pietrasiewicz }; 22200a2430fSAndrzej Pietrasiewicz 22300a2430fSAndrzej Pietrasiewicz /*-------------------------------------------------------------------------*/ 224cb382536SAndrzej Pietrasiewicz /* Strings */ 225cb382536SAndrzej Pietrasiewicz 226cb382536SAndrzej Pietrasiewicz #define CT_FUNC_HID_IDX 0 227cb382536SAndrzej Pietrasiewicz 228cb382536SAndrzej Pietrasiewicz static struct usb_string ct_func_string_defs[] = { 229cb382536SAndrzej Pietrasiewicz [CT_FUNC_HID_IDX].s = "HID Interface", 230cb382536SAndrzej Pietrasiewicz {}, /* end of list */ 231cb382536SAndrzej Pietrasiewicz }; 232cb382536SAndrzej Pietrasiewicz 233cb382536SAndrzej Pietrasiewicz static struct usb_gadget_strings ct_func_string_table = { 234cb382536SAndrzej Pietrasiewicz .language = 0x0409, /* en-US */ 235cb382536SAndrzej Pietrasiewicz .strings = ct_func_string_defs, 236cb382536SAndrzej Pietrasiewicz }; 237cb382536SAndrzej Pietrasiewicz 238cb382536SAndrzej Pietrasiewicz static struct usb_gadget_strings *ct_func_strings[] = { 239cb382536SAndrzej Pietrasiewicz &ct_func_string_table, 240cb382536SAndrzej Pietrasiewicz NULL, 241cb382536SAndrzej Pietrasiewicz }; 242cb382536SAndrzej Pietrasiewicz 243cb382536SAndrzej Pietrasiewicz /*-------------------------------------------------------------------------*/ 24400a2430fSAndrzej Pietrasiewicz /* Char Device */ 24500a2430fSAndrzej Pietrasiewicz 24600a2430fSAndrzej Pietrasiewicz static ssize_t f_hidg_read(struct file *file, char __user *buffer, 24700a2430fSAndrzej Pietrasiewicz size_t count, loff_t *ptr) 24800a2430fSAndrzej Pietrasiewicz { 24900a2430fSAndrzej Pietrasiewicz struct f_hidg *hidg = file->private_data; 25000a2430fSAndrzej Pietrasiewicz struct f_hidg_req_list *list; 25100a2430fSAndrzej Pietrasiewicz struct usb_request *req; 25200a2430fSAndrzej Pietrasiewicz unsigned long flags; 25300a2430fSAndrzej Pietrasiewicz int ret; 25400a2430fSAndrzej Pietrasiewicz 25500a2430fSAndrzej Pietrasiewicz if (!count) 25600a2430fSAndrzej Pietrasiewicz return 0; 25700a2430fSAndrzej Pietrasiewicz 25800a2430fSAndrzej Pietrasiewicz if (!access_ok(VERIFY_WRITE, buffer, count)) 25900a2430fSAndrzej Pietrasiewicz return -EFAULT; 26000a2430fSAndrzej Pietrasiewicz 26133e4c1a9SKrzysztof Opasiak spin_lock_irqsave(&hidg->read_spinlock, flags); 26200a2430fSAndrzej Pietrasiewicz 26300a2430fSAndrzej Pietrasiewicz #define READ_COND (!list_empty(&hidg->completed_out_req)) 26400a2430fSAndrzej Pietrasiewicz 26500a2430fSAndrzej Pietrasiewicz /* wait for at least one buffer to complete */ 26600a2430fSAndrzej Pietrasiewicz while (!READ_COND) { 26733e4c1a9SKrzysztof Opasiak spin_unlock_irqrestore(&hidg->read_spinlock, flags); 26800a2430fSAndrzej Pietrasiewicz if (file->f_flags & O_NONBLOCK) 26900a2430fSAndrzej Pietrasiewicz return -EAGAIN; 27000a2430fSAndrzej Pietrasiewicz 27100a2430fSAndrzej Pietrasiewicz if (wait_event_interruptible(hidg->read_queue, READ_COND)) 27200a2430fSAndrzej Pietrasiewicz return -ERESTARTSYS; 27300a2430fSAndrzej Pietrasiewicz 27433e4c1a9SKrzysztof Opasiak spin_lock_irqsave(&hidg->read_spinlock, flags); 27500a2430fSAndrzej Pietrasiewicz } 27600a2430fSAndrzej Pietrasiewicz 27700a2430fSAndrzej Pietrasiewicz /* pick the first one */ 27800a2430fSAndrzej Pietrasiewicz list = list_first_entry(&hidg->completed_out_req, 27900a2430fSAndrzej Pietrasiewicz struct f_hidg_req_list, list); 280aa65d11aSKrzysztof Opasiak 281aa65d11aSKrzysztof Opasiak /* 282aa65d11aSKrzysztof Opasiak * Remove this from list to protect it from beign free() 283aa65d11aSKrzysztof Opasiak * while host disables our function 284aa65d11aSKrzysztof Opasiak */ 285aa65d11aSKrzysztof Opasiak list_del(&list->list); 286aa65d11aSKrzysztof Opasiak 28700a2430fSAndrzej Pietrasiewicz req = list->req; 28800a2430fSAndrzej Pietrasiewicz count = min_t(unsigned int, count, req->actual - list->pos); 28933e4c1a9SKrzysztof Opasiak spin_unlock_irqrestore(&hidg->read_spinlock, flags); 29000a2430fSAndrzej Pietrasiewicz 29100a2430fSAndrzej Pietrasiewicz /* copy to user outside spinlock */ 29200a2430fSAndrzej Pietrasiewicz count -= copy_to_user(buffer, req->buf + list->pos, count); 29300a2430fSAndrzej Pietrasiewicz list->pos += count; 29400a2430fSAndrzej Pietrasiewicz 29500a2430fSAndrzej Pietrasiewicz /* 29600a2430fSAndrzej Pietrasiewicz * if this request is completely handled and transfered to 29700a2430fSAndrzej Pietrasiewicz * userspace, remove its entry from the list and requeue it 29800a2430fSAndrzej Pietrasiewicz * again. Otherwise, we will revisit it again upon the next 29900a2430fSAndrzej Pietrasiewicz * call, taking into account its current read position. 30000a2430fSAndrzej Pietrasiewicz */ 30100a2430fSAndrzej Pietrasiewicz if (list->pos == req->actual) { 30200a2430fSAndrzej Pietrasiewicz kfree(list); 30300a2430fSAndrzej Pietrasiewicz 30400a2430fSAndrzej Pietrasiewicz req->length = hidg->report_length; 30500a2430fSAndrzej Pietrasiewicz ret = usb_ep_queue(hidg->out_ep, req, GFP_KERNEL); 306aa65d11aSKrzysztof Opasiak if (ret < 0) { 307aa65d11aSKrzysztof Opasiak free_ep_req(hidg->out_ep, req); 30800a2430fSAndrzej Pietrasiewicz return ret; 30900a2430fSAndrzej Pietrasiewicz } 310aa65d11aSKrzysztof Opasiak } else { 31133e4c1a9SKrzysztof Opasiak spin_lock_irqsave(&hidg->read_spinlock, flags); 312aa65d11aSKrzysztof Opasiak list_add(&list->list, &hidg->completed_out_req); 31333e4c1a9SKrzysztof Opasiak spin_unlock_irqrestore(&hidg->read_spinlock, flags); 314aa65d11aSKrzysztof Opasiak 315aa65d11aSKrzysztof Opasiak wake_up(&hidg->read_queue); 316aa65d11aSKrzysztof Opasiak } 31700a2430fSAndrzej Pietrasiewicz 31800a2430fSAndrzej Pietrasiewicz return count; 31900a2430fSAndrzej Pietrasiewicz } 32000a2430fSAndrzej Pietrasiewicz 32100a2430fSAndrzej Pietrasiewicz static void f_hidg_req_complete(struct usb_ep *ep, struct usb_request *req) 32200a2430fSAndrzej Pietrasiewicz { 32300a2430fSAndrzej Pietrasiewicz struct f_hidg *hidg = (struct f_hidg *)ep->driver_data; 32433e4c1a9SKrzysztof Opasiak unsigned long flags; 32500a2430fSAndrzej Pietrasiewicz 32600a2430fSAndrzej Pietrasiewicz if (req->status != 0) { 32700a2430fSAndrzej Pietrasiewicz ERROR(hidg->func.config->cdev, 32800a2430fSAndrzej Pietrasiewicz "End Point Request ERROR: %d\n", req->status); 32900a2430fSAndrzej Pietrasiewicz } 33000a2430fSAndrzej Pietrasiewicz 33133e4c1a9SKrzysztof Opasiak spin_lock_irqsave(&hidg->write_spinlock, flags); 33200a2430fSAndrzej Pietrasiewicz hidg->write_pending = 0; 33333e4c1a9SKrzysztof Opasiak spin_unlock_irqrestore(&hidg->write_spinlock, flags); 33400a2430fSAndrzej Pietrasiewicz wake_up(&hidg->write_queue); 33500a2430fSAndrzej Pietrasiewicz } 33600a2430fSAndrzej Pietrasiewicz 33700a2430fSAndrzej Pietrasiewicz static ssize_t f_hidg_write(struct file *file, const char __user *buffer, 33800a2430fSAndrzej Pietrasiewicz size_t count, loff_t *offp) 33900a2430fSAndrzej Pietrasiewicz { 34000a2430fSAndrzej Pietrasiewicz struct f_hidg *hidg = file->private_data; 341749494b6SKrzysztof Opasiak struct usb_request *req; 34233e4c1a9SKrzysztof Opasiak unsigned long flags; 34300a2430fSAndrzej Pietrasiewicz ssize_t status = -ENOMEM; 34400a2430fSAndrzej Pietrasiewicz 34500a2430fSAndrzej Pietrasiewicz if (!access_ok(VERIFY_READ, buffer, count)) 34600a2430fSAndrzej Pietrasiewicz return -EFAULT; 34700a2430fSAndrzej Pietrasiewicz 34833e4c1a9SKrzysztof Opasiak spin_lock_irqsave(&hidg->write_spinlock, flags); 34900a2430fSAndrzej Pietrasiewicz 35000a2430fSAndrzej Pietrasiewicz #define WRITE_COND (!hidg->write_pending) 351749494b6SKrzysztof Opasiak try_again: 35200a2430fSAndrzej Pietrasiewicz /* write queue */ 35300a2430fSAndrzej Pietrasiewicz while (!WRITE_COND) { 35433e4c1a9SKrzysztof Opasiak spin_unlock_irqrestore(&hidg->write_spinlock, flags); 35500a2430fSAndrzej Pietrasiewicz if (file->f_flags & O_NONBLOCK) 35600a2430fSAndrzej Pietrasiewicz return -EAGAIN; 35700a2430fSAndrzej Pietrasiewicz 35800a2430fSAndrzej Pietrasiewicz if (wait_event_interruptible_exclusive( 35900a2430fSAndrzej Pietrasiewicz hidg->write_queue, WRITE_COND)) 36000a2430fSAndrzej Pietrasiewicz return -ERESTARTSYS; 36100a2430fSAndrzej Pietrasiewicz 36233e4c1a9SKrzysztof Opasiak spin_lock_irqsave(&hidg->write_spinlock, flags); 36300a2430fSAndrzej Pietrasiewicz } 36400a2430fSAndrzej Pietrasiewicz 36533e4c1a9SKrzysztof Opasiak hidg->write_pending = 1; 366749494b6SKrzysztof Opasiak req = hidg->req; 36700a2430fSAndrzej Pietrasiewicz count = min_t(unsigned, count, hidg->report_length); 36833e4c1a9SKrzysztof Opasiak 36933e4c1a9SKrzysztof Opasiak spin_unlock_irqrestore(&hidg->write_spinlock, flags); 370*25cd9721SKrzysztof Opasiak status = copy_from_user(req->buf, buffer, count); 37100a2430fSAndrzej Pietrasiewicz 37200a2430fSAndrzej Pietrasiewicz if (status != 0) { 37300a2430fSAndrzej Pietrasiewicz ERROR(hidg->func.config->cdev, 37400a2430fSAndrzej Pietrasiewicz "copy_from_user error\n"); 37533e4c1a9SKrzysztof Opasiak status = -EINVAL; 37633e4c1a9SKrzysztof Opasiak goto release_write_pending; 37700a2430fSAndrzej Pietrasiewicz } 37800a2430fSAndrzej Pietrasiewicz 379749494b6SKrzysztof Opasiak spin_lock_irqsave(&hidg->write_spinlock, flags); 380749494b6SKrzysztof Opasiak 381*25cd9721SKrzysztof Opasiak /* when our function has been disabled by host */ 382749494b6SKrzysztof Opasiak if (!hidg->req) { 383*25cd9721SKrzysztof Opasiak free_ep_req(hidg->in_ep, req); 384749494b6SKrzysztof Opasiak /* 385749494b6SKrzysztof Opasiak * TODO 386749494b6SKrzysztof Opasiak * Should we fail with error here? 387749494b6SKrzysztof Opasiak */ 388749494b6SKrzysztof Opasiak goto try_again; 389749494b6SKrzysztof Opasiak } 390749494b6SKrzysztof Opasiak 391749494b6SKrzysztof Opasiak req->status = 0; 392749494b6SKrzysztof Opasiak req->zero = 0; 393749494b6SKrzysztof Opasiak req->length = count; 394749494b6SKrzysztof Opasiak req->complete = f_hidg_req_complete; 395749494b6SKrzysztof Opasiak req->context = hidg; 39600a2430fSAndrzej Pietrasiewicz 397*25cd9721SKrzysztof Opasiak status = usb_ep_queue(hidg->in_ep, req, GFP_ATOMIC); 39800a2430fSAndrzej Pietrasiewicz if (status < 0) { 39900a2430fSAndrzej Pietrasiewicz ERROR(hidg->func.config->cdev, 40000a2430fSAndrzej Pietrasiewicz "usb_ep_queue error on int endpoint %zd\n", status); 401749494b6SKrzysztof Opasiak goto release_write_pending_unlocked; 40200a2430fSAndrzej Pietrasiewicz } else { 40300a2430fSAndrzej Pietrasiewicz status = count; 40400a2430fSAndrzej Pietrasiewicz } 405749494b6SKrzysztof Opasiak spin_unlock_irqrestore(&hidg->write_spinlock, flags); 40600a2430fSAndrzej Pietrasiewicz 40733e4c1a9SKrzysztof Opasiak return status; 40833e4c1a9SKrzysztof Opasiak release_write_pending: 40933e4c1a9SKrzysztof Opasiak spin_lock_irqsave(&hidg->write_spinlock, flags); 410749494b6SKrzysztof Opasiak release_write_pending_unlocked: 41133e4c1a9SKrzysztof Opasiak hidg->write_pending = 0; 41233e4c1a9SKrzysztof Opasiak spin_unlock_irqrestore(&hidg->write_spinlock, flags); 41333e4c1a9SKrzysztof Opasiak 41433e4c1a9SKrzysztof Opasiak wake_up(&hidg->write_queue); 41500a2430fSAndrzej Pietrasiewicz 41600a2430fSAndrzej Pietrasiewicz return status; 41700a2430fSAndrzej Pietrasiewicz } 41800a2430fSAndrzej Pietrasiewicz 41900a2430fSAndrzej Pietrasiewicz static unsigned int f_hidg_poll(struct file *file, poll_table *wait) 42000a2430fSAndrzej Pietrasiewicz { 42100a2430fSAndrzej Pietrasiewicz struct f_hidg *hidg = file->private_data; 42200a2430fSAndrzej Pietrasiewicz unsigned int ret = 0; 42300a2430fSAndrzej Pietrasiewicz 42400a2430fSAndrzej Pietrasiewicz poll_wait(file, &hidg->read_queue, wait); 42500a2430fSAndrzej Pietrasiewicz poll_wait(file, &hidg->write_queue, wait); 42600a2430fSAndrzej Pietrasiewicz 42700a2430fSAndrzej Pietrasiewicz if (WRITE_COND) 42800a2430fSAndrzej Pietrasiewicz ret |= POLLOUT | POLLWRNORM; 42900a2430fSAndrzej Pietrasiewicz 43000a2430fSAndrzej Pietrasiewicz if (READ_COND) 43100a2430fSAndrzej Pietrasiewicz ret |= POLLIN | POLLRDNORM; 43200a2430fSAndrzej Pietrasiewicz 43300a2430fSAndrzej Pietrasiewicz return ret; 43400a2430fSAndrzej Pietrasiewicz } 43500a2430fSAndrzej Pietrasiewicz 43600a2430fSAndrzej Pietrasiewicz #undef WRITE_COND 43700a2430fSAndrzej Pietrasiewicz #undef READ_COND 43800a2430fSAndrzej Pietrasiewicz 43900a2430fSAndrzej Pietrasiewicz static int f_hidg_release(struct inode *inode, struct file *fd) 44000a2430fSAndrzej Pietrasiewicz { 44100a2430fSAndrzej Pietrasiewicz fd->private_data = NULL; 44200a2430fSAndrzej Pietrasiewicz return 0; 44300a2430fSAndrzej Pietrasiewicz } 44400a2430fSAndrzej Pietrasiewicz 44500a2430fSAndrzej Pietrasiewicz static int f_hidg_open(struct inode *inode, struct file *fd) 44600a2430fSAndrzej Pietrasiewicz { 44700a2430fSAndrzej Pietrasiewicz struct f_hidg *hidg = 44800a2430fSAndrzej Pietrasiewicz container_of(inode->i_cdev, struct f_hidg, cdev); 44900a2430fSAndrzej Pietrasiewicz 45000a2430fSAndrzej Pietrasiewicz fd->private_data = hidg; 45100a2430fSAndrzej Pietrasiewicz 45200a2430fSAndrzej Pietrasiewicz return 0; 45300a2430fSAndrzej Pietrasiewicz } 45400a2430fSAndrzej Pietrasiewicz 45500a2430fSAndrzej Pietrasiewicz /*-------------------------------------------------------------------------*/ 45600a2430fSAndrzej Pietrasiewicz /* usb_function */ 45700a2430fSAndrzej Pietrasiewicz 45800a2430fSAndrzej Pietrasiewicz static inline struct usb_request *hidg_alloc_ep_req(struct usb_ep *ep, 45900a2430fSAndrzej Pietrasiewicz unsigned length) 46000a2430fSAndrzej Pietrasiewicz { 461aadbe812SFelipe F. Tonello return alloc_ep_req(ep, length); 46200a2430fSAndrzej Pietrasiewicz } 46300a2430fSAndrzej Pietrasiewicz 46400a2430fSAndrzej Pietrasiewicz static void hidg_set_report_complete(struct usb_ep *ep, struct usb_request *req) 46500a2430fSAndrzej Pietrasiewicz { 46600a2430fSAndrzej Pietrasiewicz struct f_hidg *hidg = (struct f_hidg *) req->context; 46720d2ca95SKrzysztof Opasiak struct usb_composite_dev *cdev = hidg->func.config->cdev; 46800a2430fSAndrzej Pietrasiewicz struct f_hidg_req_list *req_list; 46900a2430fSAndrzej Pietrasiewicz unsigned long flags; 47000a2430fSAndrzej Pietrasiewicz 47120d2ca95SKrzysztof Opasiak switch (req->status) { 47220d2ca95SKrzysztof Opasiak case 0: 47300a2430fSAndrzej Pietrasiewicz req_list = kzalloc(sizeof(*req_list), GFP_ATOMIC); 47420d2ca95SKrzysztof Opasiak if (!req_list) { 47520d2ca95SKrzysztof Opasiak ERROR(cdev, "Unable to allocate mem for req_list\n"); 47620d2ca95SKrzysztof Opasiak goto free_req; 47720d2ca95SKrzysztof Opasiak } 47800a2430fSAndrzej Pietrasiewicz 47900a2430fSAndrzej Pietrasiewicz req_list->req = req; 48000a2430fSAndrzej Pietrasiewicz 48133e4c1a9SKrzysztof Opasiak spin_lock_irqsave(&hidg->read_spinlock, flags); 48200a2430fSAndrzej Pietrasiewicz list_add_tail(&req_list->list, &hidg->completed_out_req); 48333e4c1a9SKrzysztof Opasiak spin_unlock_irqrestore(&hidg->read_spinlock, flags); 48400a2430fSAndrzej Pietrasiewicz 48500a2430fSAndrzej Pietrasiewicz wake_up(&hidg->read_queue); 48620d2ca95SKrzysztof Opasiak break; 48720d2ca95SKrzysztof Opasiak default: 48820d2ca95SKrzysztof Opasiak ERROR(cdev, "Set report failed %d\n", req->status); 48920d2ca95SKrzysztof Opasiak /* FALLTHROUGH */ 49020d2ca95SKrzysztof Opasiak case -ECONNABORTED: /* hardware forced ep reset */ 49120d2ca95SKrzysztof Opasiak case -ECONNRESET: /* request dequeued */ 49220d2ca95SKrzysztof Opasiak case -ESHUTDOWN: /* disconnect from host */ 49320d2ca95SKrzysztof Opasiak free_req: 49420d2ca95SKrzysztof Opasiak free_ep_req(ep, req); 49520d2ca95SKrzysztof Opasiak return; 49620d2ca95SKrzysztof Opasiak } 49700a2430fSAndrzej Pietrasiewicz } 49800a2430fSAndrzej Pietrasiewicz 49900a2430fSAndrzej Pietrasiewicz static int hidg_setup(struct usb_function *f, 50000a2430fSAndrzej Pietrasiewicz const struct usb_ctrlrequest *ctrl) 50100a2430fSAndrzej Pietrasiewicz { 50200a2430fSAndrzej Pietrasiewicz struct f_hidg *hidg = func_to_hidg(f); 50300a2430fSAndrzej Pietrasiewicz struct usb_composite_dev *cdev = f->config->cdev; 50400a2430fSAndrzej Pietrasiewicz struct usb_request *req = cdev->req; 50500a2430fSAndrzej Pietrasiewicz int status = 0; 50600a2430fSAndrzej Pietrasiewicz __u16 value, length; 50700a2430fSAndrzej Pietrasiewicz 50800a2430fSAndrzej Pietrasiewicz value = __le16_to_cpu(ctrl->wValue); 50900a2430fSAndrzej Pietrasiewicz length = __le16_to_cpu(ctrl->wLength); 51000a2430fSAndrzej Pietrasiewicz 511c9b3bde0SJulia Lawall VDBG(cdev, 512c9b3bde0SJulia Lawall "%s crtl_request : bRequestType:0x%x bRequest:0x%x Value:0x%x\n", 513c9b3bde0SJulia Lawall __func__, ctrl->bRequestType, ctrl->bRequest, value); 51400a2430fSAndrzej Pietrasiewicz 51500a2430fSAndrzej Pietrasiewicz switch ((ctrl->bRequestType << 8) | ctrl->bRequest) { 51600a2430fSAndrzej Pietrasiewicz case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8 51700a2430fSAndrzej Pietrasiewicz | HID_REQ_GET_REPORT): 51800a2430fSAndrzej Pietrasiewicz VDBG(cdev, "get_report\n"); 51900a2430fSAndrzej Pietrasiewicz 52000a2430fSAndrzej Pietrasiewicz /* send an empty report */ 52100a2430fSAndrzej Pietrasiewicz length = min_t(unsigned, length, hidg->report_length); 52200a2430fSAndrzej Pietrasiewicz memset(req->buf, 0x0, length); 52300a2430fSAndrzej Pietrasiewicz 52400a2430fSAndrzej Pietrasiewicz goto respond; 52500a2430fSAndrzej Pietrasiewicz break; 52600a2430fSAndrzej Pietrasiewicz 52700a2430fSAndrzej Pietrasiewicz case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8 52800a2430fSAndrzej Pietrasiewicz | HID_REQ_GET_PROTOCOL): 52900a2430fSAndrzej Pietrasiewicz VDBG(cdev, "get_protocol\n"); 53000a2430fSAndrzej Pietrasiewicz goto stall; 53100a2430fSAndrzej Pietrasiewicz break; 53200a2430fSAndrzej Pietrasiewicz 53300a2430fSAndrzej Pietrasiewicz case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8 53400a2430fSAndrzej Pietrasiewicz | HID_REQ_SET_REPORT): 5356774def6SMasanari Iida VDBG(cdev, "set_report | wLength=%d\n", ctrl->wLength); 53600a2430fSAndrzej Pietrasiewicz goto stall; 53700a2430fSAndrzej Pietrasiewicz break; 53800a2430fSAndrzej Pietrasiewicz 53900a2430fSAndrzej Pietrasiewicz case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8 54000a2430fSAndrzej Pietrasiewicz | HID_REQ_SET_PROTOCOL): 54100a2430fSAndrzej Pietrasiewicz VDBG(cdev, "set_protocol\n"); 54200a2430fSAndrzej Pietrasiewicz goto stall; 54300a2430fSAndrzej Pietrasiewicz break; 54400a2430fSAndrzej Pietrasiewicz 54500a2430fSAndrzej Pietrasiewicz case ((USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_INTERFACE) << 8 54600a2430fSAndrzej Pietrasiewicz | USB_REQ_GET_DESCRIPTOR): 54700a2430fSAndrzej Pietrasiewicz switch (value >> 8) { 54800a2430fSAndrzej Pietrasiewicz case HID_DT_HID: 549f286d487SKrzysztof Opasiak { 550f286d487SKrzysztof Opasiak struct hid_descriptor hidg_desc_copy = hidg_desc; 551f286d487SKrzysztof Opasiak 55200a2430fSAndrzej Pietrasiewicz VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: HID\n"); 553f286d487SKrzysztof Opasiak hidg_desc_copy.desc[0].bDescriptorType = HID_DT_REPORT; 554f286d487SKrzysztof Opasiak hidg_desc_copy.desc[0].wDescriptorLength = 555f286d487SKrzysztof Opasiak cpu_to_le16(hidg->report_desc_length); 556f286d487SKrzysztof Opasiak 55700a2430fSAndrzej Pietrasiewicz length = min_t(unsigned short, length, 558f286d487SKrzysztof Opasiak hidg_desc_copy.bLength); 559f286d487SKrzysztof Opasiak memcpy(req->buf, &hidg_desc_copy, length); 56000a2430fSAndrzej Pietrasiewicz goto respond; 56100a2430fSAndrzej Pietrasiewicz break; 562f286d487SKrzysztof Opasiak } 56300a2430fSAndrzej Pietrasiewicz case HID_DT_REPORT: 56400a2430fSAndrzej Pietrasiewicz VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: REPORT\n"); 56500a2430fSAndrzej Pietrasiewicz length = min_t(unsigned short, length, 56600a2430fSAndrzej Pietrasiewicz hidg->report_desc_length); 56700a2430fSAndrzej Pietrasiewicz memcpy(req->buf, hidg->report_desc, length); 56800a2430fSAndrzej Pietrasiewicz goto respond; 56900a2430fSAndrzej Pietrasiewicz break; 57000a2430fSAndrzej Pietrasiewicz 57100a2430fSAndrzej Pietrasiewicz default: 57200a2430fSAndrzej Pietrasiewicz VDBG(cdev, "Unknown descriptor request 0x%x\n", 57300a2430fSAndrzej Pietrasiewicz value >> 8); 57400a2430fSAndrzej Pietrasiewicz goto stall; 57500a2430fSAndrzej Pietrasiewicz break; 57600a2430fSAndrzej Pietrasiewicz } 57700a2430fSAndrzej Pietrasiewicz break; 57800a2430fSAndrzej Pietrasiewicz 57900a2430fSAndrzej Pietrasiewicz default: 58000a2430fSAndrzej Pietrasiewicz VDBG(cdev, "Unknown request 0x%x\n", 58100a2430fSAndrzej Pietrasiewicz ctrl->bRequest); 58200a2430fSAndrzej Pietrasiewicz goto stall; 58300a2430fSAndrzej Pietrasiewicz break; 58400a2430fSAndrzej Pietrasiewicz } 58500a2430fSAndrzej Pietrasiewicz 58600a2430fSAndrzej Pietrasiewicz stall: 58700a2430fSAndrzej Pietrasiewicz return -EOPNOTSUPP; 58800a2430fSAndrzej Pietrasiewicz 58900a2430fSAndrzej Pietrasiewicz respond: 59000a2430fSAndrzej Pietrasiewicz req->zero = 0; 59100a2430fSAndrzej Pietrasiewicz req->length = length; 59200a2430fSAndrzej Pietrasiewicz status = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC); 59300a2430fSAndrzej Pietrasiewicz if (status < 0) 59400a2430fSAndrzej Pietrasiewicz ERROR(cdev, "usb_ep_queue error on ep0 %d\n", value); 59500a2430fSAndrzej Pietrasiewicz return status; 59600a2430fSAndrzej Pietrasiewicz } 59700a2430fSAndrzej Pietrasiewicz 59800a2430fSAndrzej Pietrasiewicz static void hidg_disable(struct usb_function *f) 59900a2430fSAndrzej Pietrasiewicz { 60000a2430fSAndrzej Pietrasiewicz struct f_hidg *hidg = func_to_hidg(f); 60100a2430fSAndrzej Pietrasiewicz struct f_hidg_req_list *list, *next; 602aa65d11aSKrzysztof Opasiak unsigned long flags; 60300a2430fSAndrzej Pietrasiewicz 60400a2430fSAndrzej Pietrasiewicz usb_ep_disable(hidg->in_ep); 60500a2430fSAndrzej Pietrasiewicz usb_ep_disable(hidg->out_ep); 60600a2430fSAndrzej Pietrasiewicz 60733e4c1a9SKrzysztof Opasiak spin_lock_irqsave(&hidg->read_spinlock, flags); 60800a2430fSAndrzej Pietrasiewicz list_for_each_entry_safe(list, next, &hidg->completed_out_req, list) { 609aa65d11aSKrzysztof Opasiak free_ep_req(hidg->out_ep, list->req); 61000a2430fSAndrzej Pietrasiewicz list_del(&list->list); 61100a2430fSAndrzej Pietrasiewicz kfree(list); 61200a2430fSAndrzej Pietrasiewicz } 61333e4c1a9SKrzysztof Opasiak spin_unlock_irqrestore(&hidg->read_spinlock, flags); 614749494b6SKrzysztof Opasiak 615749494b6SKrzysztof Opasiak spin_lock_irqsave(&hidg->write_spinlock, flags); 616749494b6SKrzysztof Opasiak if (!hidg->write_pending) { 617749494b6SKrzysztof Opasiak free_ep_req(hidg->in_ep, hidg->req); 618749494b6SKrzysztof Opasiak hidg->write_pending = 1; 619749494b6SKrzysztof Opasiak } 620749494b6SKrzysztof Opasiak 621749494b6SKrzysztof Opasiak hidg->req = NULL; 622749494b6SKrzysztof Opasiak spin_unlock_irqrestore(&hidg->write_spinlock, flags); 62300a2430fSAndrzej Pietrasiewicz } 62400a2430fSAndrzej Pietrasiewicz 62500a2430fSAndrzej Pietrasiewicz static int hidg_set_alt(struct usb_function *f, unsigned intf, unsigned alt) 62600a2430fSAndrzej Pietrasiewicz { 62700a2430fSAndrzej Pietrasiewicz struct usb_composite_dev *cdev = f->config->cdev; 62800a2430fSAndrzej Pietrasiewicz struct f_hidg *hidg = func_to_hidg(f); 629749494b6SKrzysztof Opasiak struct usb_request *req_in = NULL; 630749494b6SKrzysztof Opasiak unsigned long flags; 63100a2430fSAndrzej Pietrasiewicz int i, status = 0; 63200a2430fSAndrzej Pietrasiewicz 63300a2430fSAndrzej Pietrasiewicz VDBG(cdev, "hidg_set_alt intf:%d alt:%d\n", intf, alt); 63400a2430fSAndrzej Pietrasiewicz 63500a2430fSAndrzej Pietrasiewicz if (hidg->in_ep != NULL) { 63600a2430fSAndrzej Pietrasiewicz /* restart endpoint */ 63700a2430fSAndrzej Pietrasiewicz usb_ep_disable(hidg->in_ep); 63800a2430fSAndrzej Pietrasiewicz 63900a2430fSAndrzej Pietrasiewicz status = config_ep_by_speed(f->config->cdev->gadget, f, 64000a2430fSAndrzej Pietrasiewicz hidg->in_ep); 64100a2430fSAndrzej Pietrasiewicz if (status) { 64200a2430fSAndrzej Pietrasiewicz ERROR(cdev, "config_ep_by_speed FAILED!\n"); 64300a2430fSAndrzej Pietrasiewicz goto fail; 64400a2430fSAndrzej Pietrasiewicz } 64500a2430fSAndrzej Pietrasiewicz status = usb_ep_enable(hidg->in_ep); 64600a2430fSAndrzej Pietrasiewicz if (status < 0) { 64700a2430fSAndrzej Pietrasiewicz ERROR(cdev, "Enable IN endpoint FAILED!\n"); 64800a2430fSAndrzej Pietrasiewicz goto fail; 64900a2430fSAndrzej Pietrasiewicz } 65000a2430fSAndrzej Pietrasiewicz hidg->in_ep->driver_data = hidg; 651749494b6SKrzysztof Opasiak 652749494b6SKrzysztof Opasiak req_in = hidg_alloc_ep_req(hidg->in_ep, hidg->report_length); 653749494b6SKrzysztof Opasiak if (!req_in) { 654749494b6SKrzysztof Opasiak status = -ENOMEM; 655749494b6SKrzysztof Opasiak goto disable_ep_in; 656749494b6SKrzysztof Opasiak } 65700a2430fSAndrzej Pietrasiewicz } 65800a2430fSAndrzej Pietrasiewicz 65900a2430fSAndrzej Pietrasiewicz 66000a2430fSAndrzej Pietrasiewicz if (hidg->out_ep != NULL) { 66100a2430fSAndrzej Pietrasiewicz /* restart endpoint */ 66200a2430fSAndrzej Pietrasiewicz usb_ep_disable(hidg->out_ep); 66300a2430fSAndrzej Pietrasiewicz 66400a2430fSAndrzej Pietrasiewicz status = config_ep_by_speed(f->config->cdev->gadget, f, 66500a2430fSAndrzej Pietrasiewicz hidg->out_ep); 66600a2430fSAndrzej Pietrasiewicz if (status) { 66700a2430fSAndrzej Pietrasiewicz ERROR(cdev, "config_ep_by_speed FAILED!\n"); 668749494b6SKrzysztof Opasiak goto free_req_in; 66900a2430fSAndrzej Pietrasiewicz } 67000a2430fSAndrzej Pietrasiewicz status = usb_ep_enable(hidg->out_ep); 67100a2430fSAndrzej Pietrasiewicz if (status < 0) { 67243aef5c2SDavid Lechner ERROR(cdev, "Enable OUT endpoint FAILED!\n"); 673749494b6SKrzysztof Opasiak goto free_req_in; 67400a2430fSAndrzej Pietrasiewicz } 67500a2430fSAndrzej Pietrasiewicz hidg->out_ep->driver_data = hidg; 67600a2430fSAndrzej Pietrasiewicz 67700a2430fSAndrzej Pietrasiewicz /* 67800a2430fSAndrzej Pietrasiewicz * allocate a bunch of read buffers and queue them all at once. 67900a2430fSAndrzej Pietrasiewicz */ 68000a2430fSAndrzej Pietrasiewicz for (i = 0; i < hidg->qlen && status == 0; i++) { 68100a2430fSAndrzej Pietrasiewicz struct usb_request *req = 68200a2430fSAndrzej Pietrasiewicz hidg_alloc_ep_req(hidg->out_ep, 68300a2430fSAndrzej Pietrasiewicz hidg->report_length); 68400a2430fSAndrzej Pietrasiewicz if (req) { 68500a2430fSAndrzej Pietrasiewicz req->complete = hidg_set_report_complete; 68600a2430fSAndrzej Pietrasiewicz req->context = hidg; 68700a2430fSAndrzej Pietrasiewicz status = usb_ep_queue(hidg->out_ep, req, 68800a2430fSAndrzej Pietrasiewicz GFP_ATOMIC); 689749494b6SKrzysztof Opasiak if (status) { 69000a2430fSAndrzej Pietrasiewicz ERROR(cdev, "%s queue req --> %d\n", 69100a2430fSAndrzej Pietrasiewicz hidg->out_ep->name, status); 692749494b6SKrzysztof Opasiak free_ep_req(hidg->out_ep, req); 693749494b6SKrzysztof Opasiak } 69400a2430fSAndrzej Pietrasiewicz } else { 69500a2430fSAndrzej Pietrasiewicz status = -ENOMEM; 696749494b6SKrzysztof Opasiak goto disable_out_ep; 69700a2430fSAndrzej Pietrasiewicz } 69800a2430fSAndrzej Pietrasiewicz } 69900a2430fSAndrzej Pietrasiewicz } 70000a2430fSAndrzej Pietrasiewicz 701749494b6SKrzysztof Opasiak if (hidg->in_ep != NULL) { 702749494b6SKrzysztof Opasiak spin_lock_irqsave(&hidg->write_spinlock, flags); 703749494b6SKrzysztof Opasiak hidg->req = req_in; 704749494b6SKrzysztof Opasiak hidg->write_pending = 0; 705749494b6SKrzysztof Opasiak spin_unlock_irqrestore(&hidg->write_spinlock, flags); 706749494b6SKrzysztof Opasiak 707749494b6SKrzysztof Opasiak wake_up(&hidg->write_queue); 708749494b6SKrzysztof Opasiak } 709749494b6SKrzysztof Opasiak return 0; 710749494b6SKrzysztof Opasiak disable_out_ep: 711749494b6SKrzysztof Opasiak usb_ep_disable(hidg->out_ep); 712749494b6SKrzysztof Opasiak free_req_in: 713749494b6SKrzysztof Opasiak if (req_in) 714749494b6SKrzysztof Opasiak free_ep_req(hidg->in_ep, req_in); 715749494b6SKrzysztof Opasiak 716749494b6SKrzysztof Opasiak disable_ep_in: 717749494b6SKrzysztof Opasiak if (hidg->in_ep) 718749494b6SKrzysztof Opasiak usb_ep_disable(hidg->in_ep); 719749494b6SKrzysztof Opasiak 72000a2430fSAndrzej Pietrasiewicz fail: 72100a2430fSAndrzej Pietrasiewicz return status; 72200a2430fSAndrzej Pietrasiewicz } 72300a2430fSAndrzej Pietrasiewicz 7247a3cc461SLad, Prabhakar static const struct file_operations f_hidg_fops = { 72500a2430fSAndrzej Pietrasiewicz .owner = THIS_MODULE, 72600a2430fSAndrzej Pietrasiewicz .open = f_hidg_open, 72700a2430fSAndrzej Pietrasiewicz .release = f_hidg_release, 72800a2430fSAndrzej Pietrasiewicz .write = f_hidg_write, 72900a2430fSAndrzej Pietrasiewicz .read = f_hidg_read, 73000a2430fSAndrzej Pietrasiewicz .poll = f_hidg_poll, 73100a2430fSAndrzej Pietrasiewicz .llseek = noop_llseek, 73200a2430fSAndrzej Pietrasiewicz }; 73300a2430fSAndrzej Pietrasiewicz 734cb382536SAndrzej Pietrasiewicz static int hidg_bind(struct usb_configuration *c, struct usb_function *f) 73500a2430fSAndrzej Pietrasiewicz { 73600a2430fSAndrzej Pietrasiewicz struct usb_ep *ep; 73700a2430fSAndrzej Pietrasiewicz struct f_hidg *hidg = func_to_hidg(f); 7385ca8d3ecSAndrzej Pietrasiewicz struct usb_string *us; 73963406087SAndrzej Pietrasiewicz struct device *device; 74000a2430fSAndrzej Pietrasiewicz int status; 74100a2430fSAndrzej Pietrasiewicz dev_t dev; 74200a2430fSAndrzej Pietrasiewicz 743cb382536SAndrzej Pietrasiewicz /* maybe allocate device-global string IDs, and patch descriptors */ 7445ca8d3ecSAndrzej Pietrasiewicz us = usb_gstrings_attach(c->cdev, ct_func_strings, 7455ca8d3ecSAndrzej Pietrasiewicz ARRAY_SIZE(ct_func_string_defs)); 7465ca8d3ecSAndrzej Pietrasiewicz if (IS_ERR(us)) 7475ca8d3ecSAndrzej Pietrasiewicz return PTR_ERR(us); 7485ca8d3ecSAndrzej Pietrasiewicz hidg_interface_desc.iInterface = us[CT_FUNC_HID_IDX].id; 749cb382536SAndrzej Pietrasiewicz 75000a2430fSAndrzej Pietrasiewicz /* allocate instance-specific interface IDs, and patch descriptors */ 75100a2430fSAndrzej Pietrasiewicz status = usb_interface_id(c, f); 75200a2430fSAndrzej Pietrasiewicz if (status < 0) 75300a2430fSAndrzej Pietrasiewicz goto fail; 75400a2430fSAndrzej Pietrasiewicz hidg_interface_desc.bInterfaceNumber = status; 75500a2430fSAndrzej Pietrasiewicz 75600a2430fSAndrzej Pietrasiewicz /* allocate instance-specific endpoints */ 75700a2430fSAndrzej Pietrasiewicz status = -ENODEV; 75800a2430fSAndrzej Pietrasiewicz ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_in_ep_desc); 75900a2430fSAndrzej Pietrasiewicz if (!ep) 76000a2430fSAndrzej Pietrasiewicz goto fail; 76100a2430fSAndrzej Pietrasiewicz hidg->in_ep = ep; 76200a2430fSAndrzej Pietrasiewicz 76300a2430fSAndrzej Pietrasiewicz ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_out_ep_desc); 76400a2430fSAndrzej Pietrasiewicz if (!ep) 76500a2430fSAndrzej Pietrasiewicz goto fail; 76600a2430fSAndrzej Pietrasiewicz hidg->out_ep = ep; 76700a2430fSAndrzej Pietrasiewicz 76800a2430fSAndrzej Pietrasiewicz /* set descriptor dynamic values */ 76900a2430fSAndrzej Pietrasiewicz hidg_interface_desc.bInterfaceSubClass = hidg->bInterfaceSubClass; 77000a2430fSAndrzej Pietrasiewicz hidg_interface_desc.bInterfaceProtocol = hidg->bInterfaceProtocol; 771dbf499cfSJanusz Dziedzic hidg_ss_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length); 772dbf499cfSJanusz Dziedzic hidg_ss_in_comp_desc.wBytesPerInterval = 773dbf499cfSJanusz Dziedzic cpu_to_le16(hidg->report_length); 77400a2430fSAndrzej Pietrasiewicz hidg_hs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length); 77500a2430fSAndrzej Pietrasiewicz hidg_fs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length); 776dbf499cfSJanusz Dziedzic hidg_ss_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length); 777dbf499cfSJanusz Dziedzic hidg_ss_out_comp_desc.wBytesPerInterval = 778dbf499cfSJanusz Dziedzic cpu_to_le16(hidg->report_length); 77900a2430fSAndrzej Pietrasiewicz hidg_hs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length); 78000a2430fSAndrzej Pietrasiewicz hidg_fs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length); 781f286d487SKrzysztof Opasiak /* 782f286d487SKrzysztof Opasiak * We can use hidg_desc struct here but we should not relay 783f286d487SKrzysztof Opasiak * that its content won't change after returning from this function. 784f286d487SKrzysztof Opasiak */ 78500a2430fSAndrzej Pietrasiewicz hidg_desc.desc[0].bDescriptorType = HID_DT_REPORT; 78600a2430fSAndrzej Pietrasiewicz hidg_desc.desc[0].wDescriptorLength = 78700a2430fSAndrzej Pietrasiewicz cpu_to_le16(hidg->report_desc_length); 78800a2430fSAndrzej Pietrasiewicz 78900a2430fSAndrzej Pietrasiewicz hidg_hs_in_ep_desc.bEndpointAddress = 79000a2430fSAndrzej Pietrasiewicz hidg_fs_in_ep_desc.bEndpointAddress; 79100a2430fSAndrzej Pietrasiewicz hidg_hs_out_ep_desc.bEndpointAddress = 79200a2430fSAndrzej Pietrasiewicz hidg_fs_out_ep_desc.bEndpointAddress; 79300a2430fSAndrzej Pietrasiewicz 794dbf499cfSJanusz Dziedzic hidg_ss_in_ep_desc.bEndpointAddress = 795dbf499cfSJanusz Dziedzic hidg_fs_in_ep_desc.bEndpointAddress; 796dbf499cfSJanusz Dziedzic hidg_ss_out_ep_desc.bEndpointAddress = 797dbf499cfSJanusz Dziedzic hidg_fs_out_ep_desc.bEndpointAddress; 798dbf499cfSJanusz Dziedzic 79900a2430fSAndrzej Pietrasiewicz status = usb_assign_descriptors(f, hidg_fs_descriptors, 800dbf499cfSJanusz Dziedzic hidg_hs_descriptors, hidg_ss_descriptors, NULL); 80100a2430fSAndrzej Pietrasiewicz if (status) 80200a2430fSAndrzej Pietrasiewicz goto fail; 80300a2430fSAndrzej Pietrasiewicz 80433e4c1a9SKrzysztof Opasiak spin_lock_init(&hidg->write_spinlock); 805749494b6SKrzysztof Opasiak hidg->write_pending = 1; 806749494b6SKrzysztof Opasiak hidg->req = NULL; 80733e4c1a9SKrzysztof Opasiak spin_lock_init(&hidg->read_spinlock); 80800a2430fSAndrzej Pietrasiewicz init_waitqueue_head(&hidg->write_queue); 80900a2430fSAndrzej Pietrasiewicz init_waitqueue_head(&hidg->read_queue); 81000a2430fSAndrzej Pietrasiewicz INIT_LIST_HEAD(&hidg->completed_out_req); 81100a2430fSAndrzej Pietrasiewicz 81200a2430fSAndrzej Pietrasiewicz /* create char device */ 81300a2430fSAndrzej Pietrasiewicz cdev_init(&hidg->cdev, &f_hidg_fops); 81400a2430fSAndrzej Pietrasiewicz dev = MKDEV(major, hidg->minor); 81500a2430fSAndrzej Pietrasiewicz status = cdev_add(&hidg->cdev, dev, 1); 81600a2430fSAndrzej Pietrasiewicz if (status) 817d12a8727SPavitrakumar Managutte goto fail_free_descs; 81800a2430fSAndrzej Pietrasiewicz 81963406087SAndrzej Pietrasiewicz device = device_create(hidg_class, NULL, dev, NULL, 82063406087SAndrzej Pietrasiewicz "%s%d", "hidg", hidg->minor); 82163406087SAndrzej Pietrasiewicz if (IS_ERR(device)) { 82263406087SAndrzej Pietrasiewicz status = PTR_ERR(device); 82363406087SAndrzej Pietrasiewicz goto del; 82463406087SAndrzej Pietrasiewicz } 82500a2430fSAndrzej Pietrasiewicz 82600a2430fSAndrzej Pietrasiewicz return 0; 82763406087SAndrzej Pietrasiewicz del: 82863406087SAndrzej Pietrasiewicz cdev_del(&hidg->cdev); 829d12a8727SPavitrakumar Managutte fail_free_descs: 830d12a8727SPavitrakumar Managutte usb_free_all_descriptors(f); 83100a2430fSAndrzej Pietrasiewicz fail: 83200a2430fSAndrzej Pietrasiewicz ERROR(f->config->cdev, "hidg_bind FAILED\n"); 83314794d71SFelipe F. Tonello if (hidg->req != NULL) 83414794d71SFelipe F. Tonello free_ep_req(hidg->in_ep, hidg->req); 83500a2430fSAndrzej Pietrasiewicz 83600a2430fSAndrzej Pietrasiewicz return status; 83700a2430fSAndrzej Pietrasiewicz } 83800a2430fSAndrzej Pietrasiewicz 839cb382536SAndrzej Pietrasiewicz static inline int hidg_get_minor(void) 840cb382536SAndrzej Pietrasiewicz { 841cb382536SAndrzej Pietrasiewicz int ret; 842cb382536SAndrzej Pietrasiewicz 843cb382536SAndrzej Pietrasiewicz ret = ida_simple_get(&hidg_ida, 0, 0, GFP_KERNEL); 844774cf72fSAndrzej Pietrasiewicz if (ret >= HIDG_MINORS) { 845774cf72fSAndrzej Pietrasiewicz ida_simple_remove(&hidg_ida, ret); 846774cf72fSAndrzej Pietrasiewicz ret = -ENODEV; 847774cf72fSAndrzej Pietrasiewicz } 848cb382536SAndrzej Pietrasiewicz 849cb382536SAndrzej Pietrasiewicz return ret; 850cb382536SAndrzej Pietrasiewicz } 851cb382536SAndrzej Pietrasiewicz 85221a9476aSAndrzej Pietrasiewicz static inline struct f_hid_opts *to_f_hid_opts(struct config_item *item) 85321a9476aSAndrzej Pietrasiewicz { 85421a9476aSAndrzej Pietrasiewicz return container_of(to_config_group(item), struct f_hid_opts, 85521a9476aSAndrzej Pietrasiewicz func_inst.group); 85621a9476aSAndrzej Pietrasiewicz } 85721a9476aSAndrzej Pietrasiewicz 85821a9476aSAndrzej Pietrasiewicz static void hid_attr_release(struct config_item *item) 85921a9476aSAndrzej Pietrasiewicz { 86021a9476aSAndrzej Pietrasiewicz struct f_hid_opts *opts = to_f_hid_opts(item); 86121a9476aSAndrzej Pietrasiewicz 86221a9476aSAndrzej Pietrasiewicz usb_put_function_instance(&opts->func_inst); 86321a9476aSAndrzej Pietrasiewicz } 86421a9476aSAndrzej Pietrasiewicz 86521a9476aSAndrzej Pietrasiewicz static struct configfs_item_operations hidg_item_ops = { 86621a9476aSAndrzej Pietrasiewicz .release = hid_attr_release, 86721a9476aSAndrzej Pietrasiewicz }; 86821a9476aSAndrzej Pietrasiewicz 86921a9476aSAndrzej Pietrasiewicz #define F_HID_OPT(name, prec, limit) \ 870da4e527cSChristoph Hellwig static ssize_t f_hid_opts_##name##_show(struct config_item *item, char *page)\ 87121a9476aSAndrzej Pietrasiewicz { \ 872da4e527cSChristoph Hellwig struct f_hid_opts *opts = to_f_hid_opts(item); \ 87321a9476aSAndrzej Pietrasiewicz int result; \ 87421a9476aSAndrzej Pietrasiewicz \ 87521a9476aSAndrzej Pietrasiewicz mutex_lock(&opts->lock); \ 87621a9476aSAndrzej Pietrasiewicz result = sprintf(page, "%d\n", opts->name); \ 87721a9476aSAndrzej Pietrasiewicz mutex_unlock(&opts->lock); \ 87821a9476aSAndrzej Pietrasiewicz \ 87921a9476aSAndrzej Pietrasiewicz return result; \ 88021a9476aSAndrzej Pietrasiewicz } \ 88121a9476aSAndrzej Pietrasiewicz \ 882da4e527cSChristoph Hellwig static ssize_t f_hid_opts_##name##_store(struct config_item *item, \ 88321a9476aSAndrzej Pietrasiewicz const char *page, size_t len) \ 88421a9476aSAndrzej Pietrasiewicz { \ 885da4e527cSChristoph Hellwig struct f_hid_opts *opts = to_f_hid_opts(item); \ 88621a9476aSAndrzej Pietrasiewicz int ret; \ 88721a9476aSAndrzej Pietrasiewicz u##prec num; \ 88821a9476aSAndrzej Pietrasiewicz \ 88921a9476aSAndrzej Pietrasiewicz mutex_lock(&opts->lock); \ 89021a9476aSAndrzej Pietrasiewicz if (opts->refcnt) { \ 89121a9476aSAndrzej Pietrasiewicz ret = -EBUSY; \ 89221a9476aSAndrzej Pietrasiewicz goto end; \ 89321a9476aSAndrzej Pietrasiewicz } \ 89421a9476aSAndrzej Pietrasiewicz \ 89521a9476aSAndrzej Pietrasiewicz ret = kstrtou##prec(page, 0, &num); \ 89621a9476aSAndrzej Pietrasiewicz if (ret) \ 89721a9476aSAndrzej Pietrasiewicz goto end; \ 89821a9476aSAndrzej Pietrasiewicz \ 89921a9476aSAndrzej Pietrasiewicz if (num > limit) { \ 90021a9476aSAndrzej Pietrasiewicz ret = -EINVAL; \ 90121a9476aSAndrzej Pietrasiewicz goto end; \ 90221a9476aSAndrzej Pietrasiewicz } \ 90321a9476aSAndrzej Pietrasiewicz opts->name = num; \ 90421a9476aSAndrzej Pietrasiewicz ret = len; \ 90521a9476aSAndrzej Pietrasiewicz \ 90621a9476aSAndrzej Pietrasiewicz end: \ 90721a9476aSAndrzej Pietrasiewicz mutex_unlock(&opts->lock); \ 90821a9476aSAndrzej Pietrasiewicz return ret; \ 90921a9476aSAndrzej Pietrasiewicz } \ 91021a9476aSAndrzej Pietrasiewicz \ 911da4e527cSChristoph Hellwig CONFIGFS_ATTR(f_hid_opts_, name) 91221a9476aSAndrzej Pietrasiewicz 91321a9476aSAndrzej Pietrasiewicz F_HID_OPT(subclass, 8, 255); 91421a9476aSAndrzej Pietrasiewicz F_HID_OPT(protocol, 8, 255); 91539a2ac27SAndrzej Pietrasiewicz F_HID_OPT(report_length, 16, 65535); 91621a9476aSAndrzej Pietrasiewicz 917da4e527cSChristoph Hellwig static ssize_t f_hid_opts_report_desc_show(struct config_item *item, char *page) 91821a9476aSAndrzej Pietrasiewicz { 919da4e527cSChristoph Hellwig struct f_hid_opts *opts = to_f_hid_opts(item); 92021a9476aSAndrzej Pietrasiewicz int result; 92121a9476aSAndrzej Pietrasiewicz 92221a9476aSAndrzej Pietrasiewicz mutex_lock(&opts->lock); 92321a9476aSAndrzej Pietrasiewicz result = opts->report_desc_length; 92421a9476aSAndrzej Pietrasiewicz memcpy(page, opts->report_desc, opts->report_desc_length); 92521a9476aSAndrzej Pietrasiewicz mutex_unlock(&opts->lock); 92621a9476aSAndrzej Pietrasiewicz 92721a9476aSAndrzej Pietrasiewicz return result; 92821a9476aSAndrzej Pietrasiewicz } 92921a9476aSAndrzej Pietrasiewicz 930da4e527cSChristoph Hellwig static ssize_t f_hid_opts_report_desc_store(struct config_item *item, 93121a9476aSAndrzej Pietrasiewicz const char *page, size_t len) 93221a9476aSAndrzej Pietrasiewicz { 933da4e527cSChristoph Hellwig struct f_hid_opts *opts = to_f_hid_opts(item); 93421a9476aSAndrzej Pietrasiewicz int ret = -EBUSY; 93521a9476aSAndrzej Pietrasiewicz char *d; 93621a9476aSAndrzej Pietrasiewicz 93721a9476aSAndrzej Pietrasiewicz mutex_lock(&opts->lock); 93821a9476aSAndrzej Pietrasiewicz 93921a9476aSAndrzej Pietrasiewicz if (opts->refcnt) 94021a9476aSAndrzej Pietrasiewicz goto end; 94121a9476aSAndrzej Pietrasiewicz if (len > PAGE_SIZE) { 94221a9476aSAndrzej Pietrasiewicz ret = -ENOSPC; 94321a9476aSAndrzej Pietrasiewicz goto end; 94421a9476aSAndrzej Pietrasiewicz } 94521a9476aSAndrzej Pietrasiewicz d = kmemdup(page, len, GFP_KERNEL); 94621a9476aSAndrzej Pietrasiewicz if (!d) { 94721a9476aSAndrzej Pietrasiewicz ret = -ENOMEM; 94821a9476aSAndrzej Pietrasiewicz goto end; 94921a9476aSAndrzej Pietrasiewicz } 95021a9476aSAndrzej Pietrasiewicz kfree(opts->report_desc); 95121a9476aSAndrzej Pietrasiewicz opts->report_desc = d; 95221a9476aSAndrzej Pietrasiewicz opts->report_desc_length = len; 95321a9476aSAndrzej Pietrasiewicz opts->report_desc_alloc = true; 95421a9476aSAndrzej Pietrasiewicz ret = len; 95521a9476aSAndrzej Pietrasiewicz end: 95621a9476aSAndrzej Pietrasiewicz mutex_unlock(&opts->lock); 95721a9476aSAndrzej Pietrasiewicz return ret; 95821a9476aSAndrzej Pietrasiewicz } 95921a9476aSAndrzej Pietrasiewicz 960da4e527cSChristoph Hellwig CONFIGFS_ATTR(f_hid_opts_, report_desc); 96121a9476aSAndrzej Pietrasiewicz 962ed6fe1f5SJohannes Berg static ssize_t f_hid_opts_dev_show(struct config_item *item, char *page) 963ed6fe1f5SJohannes Berg { 964ed6fe1f5SJohannes Berg struct f_hid_opts *opts = to_f_hid_opts(item); 965ed6fe1f5SJohannes Berg 966ed6fe1f5SJohannes Berg return sprintf(page, "%d:%d\n", major, opts->minor); 967ed6fe1f5SJohannes Berg } 968ed6fe1f5SJohannes Berg 969ed6fe1f5SJohannes Berg CONFIGFS_ATTR_RO(f_hid_opts_, dev); 970ed6fe1f5SJohannes Berg 97121a9476aSAndrzej Pietrasiewicz static struct configfs_attribute *hid_attrs[] = { 972da4e527cSChristoph Hellwig &f_hid_opts_attr_subclass, 973da4e527cSChristoph Hellwig &f_hid_opts_attr_protocol, 974da4e527cSChristoph Hellwig &f_hid_opts_attr_report_length, 975da4e527cSChristoph Hellwig &f_hid_opts_attr_report_desc, 976ed6fe1f5SJohannes Berg &f_hid_opts_attr_dev, 97721a9476aSAndrzej Pietrasiewicz NULL, 97821a9476aSAndrzej Pietrasiewicz }; 97921a9476aSAndrzej Pietrasiewicz 98021a9476aSAndrzej Pietrasiewicz static struct config_item_type hid_func_type = { 98121a9476aSAndrzej Pietrasiewicz .ct_item_ops = &hidg_item_ops, 98221a9476aSAndrzej Pietrasiewicz .ct_attrs = hid_attrs, 98321a9476aSAndrzej Pietrasiewicz .ct_owner = THIS_MODULE, 98421a9476aSAndrzej Pietrasiewicz }; 98521a9476aSAndrzej Pietrasiewicz 986cb382536SAndrzej Pietrasiewicz static inline void hidg_put_minor(int minor) 987cb382536SAndrzej Pietrasiewicz { 988cb382536SAndrzej Pietrasiewicz ida_simple_remove(&hidg_ida, minor); 989cb382536SAndrzej Pietrasiewicz } 990cb382536SAndrzej Pietrasiewicz 991cb382536SAndrzej Pietrasiewicz static void hidg_free_inst(struct usb_function_instance *f) 992cb382536SAndrzej Pietrasiewicz { 993cb382536SAndrzej Pietrasiewicz struct f_hid_opts *opts; 994cb382536SAndrzej Pietrasiewicz 995cb382536SAndrzej Pietrasiewicz opts = container_of(f, struct f_hid_opts, func_inst); 996cb382536SAndrzej Pietrasiewicz 997cb382536SAndrzej Pietrasiewicz mutex_lock(&hidg_ida_lock); 998cb382536SAndrzej Pietrasiewicz 999cb382536SAndrzej Pietrasiewicz hidg_put_minor(opts->minor); 100099c49407SMatthew Wilcox if (ida_is_empty(&hidg_ida)) 1001cb382536SAndrzej Pietrasiewicz ghid_cleanup(); 1002cb382536SAndrzej Pietrasiewicz 1003cb382536SAndrzej Pietrasiewicz mutex_unlock(&hidg_ida_lock); 1004cb382536SAndrzej Pietrasiewicz 1005cb382536SAndrzej Pietrasiewicz if (opts->report_desc_alloc) 1006cb382536SAndrzej Pietrasiewicz kfree(opts->report_desc); 1007cb382536SAndrzej Pietrasiewicz 1008cb382536SAndrzej Pietrasiewicz kfree(opts); 1009cb382536SAndrzej Pietrasiewicz } 1010cb382536SAndrzej Pietrasiewicz 1011cb382536SAndrzej Pietrasiewicz static struct usb_function_instance *hidg_alloc_inst(void) 1012cb382536SAndrzej Pietrasiewicz { 1013cb382536SAndrzej Pietrasiewicz struct f_hid_opts *opts; 1014cb382536SAndrzej Pietrasiewicz struct usb_function_instance *ret; 1015cb382536SAndrzej Pietrasiewicz int status = 0; 1016cb382536SAndrzej Pietrasiewicz 1017cb382536SAndrzej Pietrasiewicz opts = kzalloc(sizeof(*opts), GFP_KERNEL); 1018cb382536SAndrzej Pietrasiewicz if (!opts) 1019cb382536SAndrzej Pietrasiewicz return ERR_PTR(-ENOMEM); 102021a9476aSAndrzej Pietrasiewicz mutex_init(&opts->lock); 1021cb382536SAndrzej Pietrasiewicz opts->func_inst.free_func_inst = hidg_free_inst; 1022cb382536SAndrzej Pietrasiewicz ret = &opts->func_inst; 1023cb382536SAndrzej Pietrasiewicz 1024cb382536SAndrzej Pietrasiewicz mutex_lock(&hidg_ida_lock); 1025cb382536SAndrzej Pietrasiewicz 102699c49407SMatthew Wilcox if (ida_is_empty(&hidg_ida)) { 1027cb382536SAndrzej Pietrasiewicz status = ghid_setup(NULL, HIDG_MINORS); 1028cb382536SAndrzej Pietrasiewicz if (status) { 1029cb382536SAndrzej Pietrasiewicz ret = ERR_PTR(status); 1030cb382536SAndrzej Pietrasiewicz kfree(opts); 1031cb382536SAndrzej Pietrasiewicz goto unlock; 1032cb382536SAndrzej Pietrasiewicz } 1033cb382536SAndrzej Pietrasiewicz } 1034cb382536SAndrzej Pietrasiewicz 1035cb382536SAndrzej Pietrasiewicz opts->minor = hidg_get_minor(); 1036cb382536SAndrzej Pietrasiewicz if (opts->minor < 0) { 1037cb382536SAndrzej Pietrasiewicz ret = ERR_PTR(opts->minor); 1038cb382536SAndrzej Pietrasiewicz kfree(opts); 103999c49407SMatthew Wilcox if (ida_is_empty(&hidg_ida)) 1040cb382536SAndrzej Pietrasiewicz ghid_cleanup(); 1041828f6148SDan Carpenter goto unlock; 1042cb382536SAndrzej Pietrasiewicz } 104321a9476aSAndrzej Pietrasiewicz config_group_init_type_name(&opts->func_inst.group, "", &hid_func_type); 1044cb382536SAndrzej Pietrasiewicz 1045cb382536SAndrzej Pietrasiewicz unlock: 1046cb382536SAndrzej Pietrasiewicz mutex_unlock(&hidg_ida_lock); 1047cb382536SAndrzej Pietrasiewicz return ret; 1048cb382536SAndrzej Pietrasiewicz } 1049cb382536SAndrzej Pietrasiewicz 1050cb382536SAndrzej Pietrasiewicz static void hidg_free(struct usb_function *f) 1051cb382536SAndrzej Pietrasiewicz { 1052cb382536SAndrzej Pietrasiewicz struct f_hidg *hidg; 105321a9476aSAndrzej Pietrasiewicz struct f_hid_opts *opts; 1054cb382536SAndrzej Pietrasiewicz 1055cb382536SAndrzej Pietrasiewicz hidg = func_to_hidg(f); 105621a9476aSAndrzej Pietrasiewicz opts = container_of(f->fi, struct f_hid_opts, func_inst); 1057cb382536SAndrzej Pietrasiewicz kfree(hidg->report_desc); 1058cb382536SAndrzej Pietrasiewicz kfree(hidg); 105921a9476aSAndrzej Pietrasiewicz mutex_lock(&opts->lock); 106021a9476aSAndrzej Pietrasiewicz --opts->refcnt; 106121a9476aSAndrzej Pietrasiewicz mutex_unlock(&opts->lock); 1062cb382536SAndrzej Pietrasiewicz } 1063cb382536SAndrzej Pietrasiewicz 106400a2430fSAndrzej Pietrasiewicz static void hidg_unbind(struct usb_configuration *c, struct usb_function *f) 106500a2430fSAndrzej Pietrasiewicz { 106600a2430fSAndrzej Pietrasiewicz struct f_hidg *hidg = func_to_hidg(f); 106700a2430fSAndrzej Pietrasiewicz 106800a2430fSAndrzej Pietrasiewicz device_destroy(hidg_class, MKDEV(major, hidg->minor)); 106900a2430fSAndrzej Pietrasiewicz cdev_del(&hidg->cdev); 107000a2430fSAndrzej Pietrasiewicz 107100a2430fSAndrzej Pietrasiewicz usb_free_all_descriptors(f); 107200a2430fSAndrzej Pietrasiewicz } 107300a2430fSAndrzej Pietrasiewicz 10740fc57ea0SFengguang Wu static struct usb_function *hidg_alloc(struct usb_function_instance *fi) 107500a2430fSAndrzej Pietrasiewicz { 107600a2430fSAndrzej Pietrasiewicz struct f_hidg *hidg; 1077cb382536SAndrzej Pietrasiewicz struct f_hid_opts *opts; 107800a2430fSAndrzej Pietrasiewicz 107900a2430fSAndrzej Pietrasiewicz /* allocate and initialize one new instance */ 1080cb382536SAndrzej Pietrasiewicz hidg = kzalloc(sizeof(*hidg), GFP_KERNEL); 108100a2430fSAndrzej Pietrasiewicz if (!hidg) 1082cb382536SAndrzej Pietrasiewicz return ERR_PTR(-ENOMEM); 108300a2430fSAndrzej Pietrasiewicz 1084cb382536SAndrzej Pietrasiewicz opts = container_of(fi, struct f_hid_opts, func_inst); 1085cb382536SAndrzej Pietrasiewicz 108621a9476aSAndrzej Pietrasiewicz mutex_lock(&opts->lock); 108721a9476aSAndrzej Pietrasiewicz ++opts->refcnt; 108821a9476aSAndrzej Pietrasiewicz 1089cb382536SAndrzej Pietrasiewicz hidg->minor = opts->minor; 1090cb382536SAndrzej Pietrasiewicz hidg->bInterfaceSubClass = opts->subclass; 1091cb382536SAndrzej Pietrasiewicz hidg->bInterfaceProtocol = opts->protocol; 1092cb382536SAndrzej Pietrasiewicz hidg->report_length = opts->report_length; 1093cb382536SAndrzej Pietrasiewicz hidg->report_desc_length = opts->report_desc_length; 1094cb382536SAndrzej Pietrasiewicz if (opts->report_desc) { 1095cb382536SAndrzej Pietrasiewicz hidg->report_desc = kmemdup(opts->report_desc, 1096cb382536SAndrzej Pietrasiewicz opts->report_desc_length, 109700a2430fSAndrzej Pietrasiewicz GFP_KERNEL); 109800a2430fSAndrzej Pietrasiewicz if (!hidg->report_desc) { 109900a2430fSAndrzej Pietrasiewicz kfree(hidg); 110021a9476aSAndrzej Pietrasiewicz mutex_unlock(&opts->lock); 1101cb382536SAndrzej Pietrasiewicz return ERR_PTR(-ENOMEM); 1102cb382536SAndrzej Pietrasiewicz } 110300a2430fSAndrzej Pietrasiewicz } 110400a2430fSAndrzej Pietrasiewicz 110521a9476aSAndrzej Pietrasiewicz mutex_unlock(&opts->lock); 110621a9476aSAndrzej Pietrasiewicz 110700a2430fSAndrzej Pietrasiewicz hidg->func.name = "hid"; 110800a2430fSAndrzej Pietrasiewicz hidg->func.bind = hidg_bind; 110900a2430fSAndrzej Pietrasiewicz hidg->func.unbind = hidg_unbind; 111000a2430fSAndrzej Pietrasiewicz hidg->func.set_alt = hidg_set_alt; 111100a2430fSAndrzej Pietrasiewicz hidg->func.disable = hidg_disable; 111200a2430fSAndrzej Pietrasiewicz hidg->func.setup = hidg_setup; 1113cb382536SAndrzej Pietrasiewicz hidg->func.free_func = hidg_free; 111400a2430fSAndrzej Pietrasiewicz 111500a2430fSAndrzej Pietrasiewicz /* this could me made configurable at some point */ 111600a2430fSAndrzej Pietrasiewicz hidg->qlen = 4; 111700a2430fSAndrzej Pietrasiewicz 1118cb382536SAndrzej Pietrasiewicz return &hidg->func; 111900a2430fSAndrzej Pietrasiewicz } 112000a2430fSAndrzej Pietrasiewicz 1121cb382536SAndrzej Pietrasiewicz DECLARE_USB_FUNCTION_INIT(hid, hidg_alloc_inst, hidg_alloc); 1122cb382536SAndrzej Pietrasiewicz MODULE_LICENSE("GPL"); 1123cb382536SAndrzej Pietrasiewicz MODULE_AUTHOR("Fabien Chouteau"); 1124cb382536SAndrzej Pietrasiewicz 1125cb382536SAndrzej Pietrasiewicz int ghid_setup(struct usb_gadget *g, int count) 112600a2430fSAndrzej Pietrasiewicz { 112700a2430fSAndrzej Pietrasiewicz int status; 112800a2430fSAndrzej Pietrasiewicz dev_t dev; 112900a2430fSAndrzej Pietrasiewicz 113000a2430fSAndrzej Pietrasiewicz hidg_class = class_create(THIS_MODULE, "hidg"); 113106529407SAndrzej Pietrasiewicz if (IS_ERR(hidg_class)) { 11320448d38cSDan Carpenter status = PTR_ERR(hidg_class); 113306529407SAndrzej Pietrasiewicz hidg_class = NULL; 11340448d38cSDan Carpenter return status; 113500a2430fSAndrzej Pietrasiewicz } 113600a2430fSAndrzej Pietrasiewicz 113700a2430fSAndrzej Pietrasiewicz status = alloc_chrdev_region(&dev, 0, count, "hidg"); 11380448d38cSDan Carpenter if (status) { 11390448d38cSDan Carpenter class_destroy(hidg_class); 11400448d38cSDan Carpenter hidg_class = NULL; 114100a2430fSAndrzej Pietrasiewicz return status; 114200a2430fSAndrzej Pietrasiewicz } 114300a2430fSAndrzej Pietrasiewicz 11440448d38cSDan Carpenter major = MAJOR(dev); 11450448d38cSDan Carpenter minors = count; 11460448d38cSDan Carpenter 11470448d38cSDan Carpenter return 0; 114800a2430fSAndrzej Pietrasiewicz } 114900a2430fSAndrzej Pietrasiewicz 115000a2430fSAndrzej Pietrasiewicz void ghid_cleanup(void) 115100a2430fSAndrzej Pietrasiewicz { 115200a2430fSAndrzej Pietrasiewicz if (major) { 115300a2430fSAndrzej Pietrasiewicz unregister_chrdev_region(MKDEV(major, 0), minors); 115400a2430fSAndrzej Pietrasiewicz major = minors = 0; 115500a2430fSAndrzej Pietrasiewicz } 115600a2430fSAndrzej Pietrasiewicz 115700a2430fSAndrzej Pietrasiewicz class_destroy(hidg_class); 115800a2430fSAndrzej Pietrasiewicz hidg_class = NULL; 115900a2430fSAndrzej Pietrasiewicz } 1160