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; 47*b3c4ec71SAbdulhadi Mohamed unsigned char protocol; 4800a2430fSAndrzej Pietrasiewicz unsigned short report_desc_length; 4900a2430fSAndrzej Pietrasiewicz char *report_desc; 5000a2430fSAndrzej Pietrasiewicz unsigned short report_length; 5100a2430fSAndrzej Pietrasiewicz 5200a2430fSAndrzej Pietrasiewicz /* recv report */ 5300a2430fSAndrzej Pietrasiewicz struct list_head completed_out_req; 5433e4c1a9SKrzysztof Opasiak spinlock_t read_spinlock; 5500a2430fSAndrzej Pietrasiewicz wait_queue_head_t read_queue; 5600a2430fSAndrzej Pietrasiewicz unsigned int qlen; 5700a2430fSAndrzej Pietrasiewicz 5800a2430fSAndrzej Pietrasiewicz /* send report */ 5933e4c1a9SKrzysztof Opasiak spinlock_t write_spinlock; 6000a2430fSAndrzej Pietrasiewicz bool write_pending; 6100a2430fSAndrzej Pietrasiewicz wait_queue_head_t write_queue; 6200a2430fSAndrzej Pietrasiewicz struct usb_request *req; 6300a2430fSAndrzej Pietrasiewicz 6400a2430fSAndrzej Pietrasiewicz int minor; 6500a2430fSAndrzej Pietrasiewicz struct cdev cdev; 6600a2430fSAndrzej Pietrasiewicz struct usb_function func; 6700a2430fSAndrzej Pietrasiewicz 6800a2430fSAndrzej Pietrasiewicz struct usb_ep *in_ep; 6900a2430fSAndrzej Pietrasiewicz struct usb_ep *out_ep; 7000a2430fSAndrzej Pietrasiewicz }; 7100a2430fSAndrzej Pietrasiewicz 7200a2430fSAndrzej Pietrasiewicz static inline struct f_hidg *func_to_hidg(struct usb_function *f) 7300a2430fSAndrzej Pietrasiewicz { 7400a2430fSAndrzej Pietrasiewicz return container_of(f, struct f_hidg, func); 7500a2430fSAndrzej Pietrasiewicz } 7600a2430fSAndrzej Pietrasiewicz 7700a2430fSAndrzej Pietrasiewicz /*-------------------------------------------------------------------------*/ 7800a2430fSAndrzej Pietrasiewicz /* Static descriptors */ 7900a2430fSAndrzej Pietrasiewicz 8000a2430fSAndrzej Pietrasiewicz static struct usb_interface_descriptor hidg_interface_desc = { 8100a2430fSAndrzej Pietrasiewicz .bLength = sizeof hidg_interface_desc, 8200a2430fSAndrzej Pietrasiewicz .bDescriptorType = USB_DT_INTERFACE, 8300a2430fSAndrzej Pietrasiewicz /* .bInterfaceNumber = DYNAMIC */ 8400a2430fSAndrzej Pietrasiewicz .bAlternateSetting = 0, 8500a2430fSAndrzej Pietrasiewicz .bNumEndpoints = 2, 8600a2430fSAndrzej Pietrasiewicz .bInterfaceClass = USB_CLASS_HID, 8700a2430fSAndrzej Pietrasiewicz /* .bInterfaceSubClass = DYNAMIC */ 8800a2430fSAndrzej Pietrasiewicz /* .bInterfaceProtocol = DYNAMIC */ 8900a2430fSAndrzej Pietrasiewicz /* .iInterface = DYNAMIC */ 9000a2430fSAndrzej Pietrasiewicz }; 9100a2430fSAndrzej Pietrasiewicz 9200a2430fSAndrzej Pietrasiewicz static struct hid_descriptor hidg_desc = { 9300a2430fSAndrzej Pietrasiewicz .bLength = sizeof hidg_desc, 9400a2430fSAndrzej Pietrasiewicz .bDescriptorType = HID_DT_HID, 9500a2430fSAndrzej Pietrasiewicz .bcdHID = 0x0101, 9600a2430fSAndrzej Pietrasiewicz .bCountryCode = 0x00, 9700a2430fSAndrzej Pietrasiewicz .bNumDescriptors = 0x1, 9800a2430fSAndrzej Pietrasiewicz /*.desc[0].bDescriptorType = DYNAMIC */ 9900a2430fSAndrzej Pietrasiewicz /*.desc[0].wDescriptorLenght = DYNAMIC */ 10000a2430fSAndrzej Pietrasiewicz }; 10100a2430fSAndrzej Pietrasiewicz 102dbf499cfSJanusz Dziedzic /* Super-Speed Support */ 103dbf499cfSJanusz Dziedzic 104dbf499cfSJanusz Dziedzic static struct usb_endpoint_descriptor hidg_ss_in_ep_desc = { 105dbf499cfSJanusz Dziedzic .bLength = USB_DT_ENDPOINT_SIZE, 106dbf499cfSJanusz Dziedzic .bDescriptorType = USB_DT_ENDPOINT, 107dbf499cfSJanusz Dziedzic .bEndpointAddress = USB_DIR_IN, 108dbf499cfSJanusz Dziedzic .bmAttributes = USB_ENDPOINT_XFER_INT, 109dbf499cfSJanusz Dziedzic /*.wMaxPacketSize = DYNAMIC */ 110dbf499cfSJanusz Dziedzic .bInterval = 4, /* FIXME: Add this field in the 111dbf499cfSJanusz Dziedzic * HID gadget configuration? 112dbf499cfSJanusz Dziedzic * (struct hidg_func_descriptor) 113dbf499cfSJanusz Dziedzic */ 114dbf499cfSJanusz Dziedzic }; 115dbf499cfSJanusz Dziedzic 116dbf499cfSJanusz Dziedzic static struct usb_ss_ep_comp_descriptor hidg_ss_in_comp_desc = { 117dbf499cfSJanusz Dziedzic .bLength = sizeof(hidg_ss_in_comp_desc), 118dbf499cfSJanusz Dziedzic .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, 119dbf499cfSJanusz Dziedzic 120dbf499cfSJanusz Dziedzic /* .bMaxBurst = 0, */ 121dbf499cfSJanusz Dziedzic /* .bmAttributes = 0, */ 122dbf499cfSJanusz Dziedzic /* .wBytesPerInterval = DYNAMIC */ 123dbf499cfSJanusz Dziedzic }; 124dbf499cfSJanusz Dziedzic 125dbf499cfSJanusz Dziedzic static struct usb_endpoint_descriptor hidg_ss_out_ep_desc = { 126dbf499cfSJanusz Dziedzic .bLength = USB_DT_ENDPOINT_SIZE, 127dbf499cfSJanusz Dziedzic .bDescriptorType = USB_DT_ENDPOINT, 128dbf499cfSJanusz Dziedzic .bEndpointAddress = USB_DIR_OUT, 129dbf499cfSJanusz Dziedzic .bmAttributes = USB_ENDPOINT_XFER_INT, 130dbf499cfSJanusz Dziedzic /*.wMaxPacketSize = DYNAMIC */ 131dbf499cfSJanusz Dziedzic .bInterval = 4, /* FIXME: Add this field in the 132dbf499cfSJanusz Dziedzic * HID gadget configuration? 133dbf499cfSJanusz Dziedzic * (struct hidg_func_descriptor) 134dbf499cfSJanusz Dziedzic */ 135dbf499cfSJanusz Dziedzic }; 136dbf499cfSJanusz Dziedzic 137dbf499cfSJanusz Dziedzic static struct usb_ss_ep_comp_descriptor hidg_ss_out_comp_desc = { 138dbf499cfSJanusz Dziedzic .bLength = sizeof(hidg_ss_out_comp_desc), 139dbf499cfSJanusz Dziedzic .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, 140dbf499cfSJanusz Dziedzic 141dbf499cfSJanusz Dziedzic /* .bMaxBurst = 0, */ 142dbf499cfSJanusz Dziedzic /* .bmAttributes = 0, */ 143dbf499cfSJanusz Dziedzic /* .wBytesPerInterval = DYNAMIC */ 144dbf499cfSJanusz Dziedzic }; 145dbf499cfSJanusz Dziedzic 146dbf499cfSJanusz Dziedzic static struct usb_descriptor_header *hidg_ss_descriptors[] = { 147dbf499cfSJanusz Dziedzic (struct usb_descriptor_header *)&hidg_interface_desc, 148dbf499cfSJanusz Dziedzic (struct usb_descriptor_header *)&hidg_desc, 149dbf499cfSJanusz Dziedzic (struct usb_descriptor_header *)&hidg_ss_in_ep_desc, 150dbf499cfSJanusz Dziedzic (struct usb_descriptor_header *)&hidg_ss_in_comp_desc, 151dbf499cfSJanusz Dziedzic (struct usb_descriptor_header *)&hidg_ss_out_ep_desc, 152dbf499cfSJanusz Dziedzic (struct usb_descriptor_header *)&hidg_ss_out_comp_desc, 153dbf499cfSJanusz Dziedzic NULL, 154dbf499cfSJanusz Dziedzic }; 155dbf499cfSJanusz Dziedzic 15600a2430fSAndrzej Pietrasiewicz /* High-Speed Support */ 15700a2430fSAndrzej Pietrasiewicz 15800a2430fSAndrzej Pietrasiewicz static struct usb_endpoint_descriptor hidg_hs_in_ep_desc = { 15900a2430fSAndrzej Pietrasiewicz .bLength = USB_DT_ENDPOINT_SIZE, 16000a2430fSAndrzej Pietrasiewicz .bDescriptorType = USB_DT_ENDPOINT, 16100a2430fSAndrzej Pietrasiewicz .bEndpointAddress = USB_DIR_IN, 16200a2430fSAndrzej Pietrasiewicz .bmAttributes = USB_ENDPOINT_XFER_INT, 16300a2430fSAndrzej Pietrasiewicz /*.wMaxPacketSize = DYNAMIC */ 16400a2430fSAndrzej Pietrasiewicz .bInterval = 4, /* FIXME: Add this field in the 16500a2430fSAndrzej Pietrasiewicz * HID gadget configuration? 16600a2430fSAndrzej Pietrasiewicz * (struct hidg_func_descriptor) 16700a2430fSAndrzej Pietrasiewicz */ 16800a2430fSAndrzej Pietrasiewicz }; 16900a2430fSAndrzej Pietrasiewicz 17000a2430fSAndrzej Pietrasiewicz static struct usb_endpoint_descriptor hidg_hs_out_ep_desc = { 17100a2430fSAndrzej Pietrasiewicz .bLength = USB_DT_ENDPOINT_SIZE, 17200a2430fSAndrzej Pietrasiewicz .bDescriptorType = USB_DT_ENDPOINT, 17300a2430fSAndrzej Pietrasiewicz .bEndpointAddress = USB_DIR_OUT, 17400a2430fSAndrzej Pietrasiewicz .bmAttributes = USB_ENDPOINT_XFER_INT, 17500a2430fSAndrzej Pietrasiewicz /*.wMaxPacketSize = DYNAMIC */ 17600a2430fSAndrzej Pietrasiewicz .bInterval = 4, /* FIXME: Add this field in the 17700a2430fSAndrzej Pietrasiewicz * HID gadget configuration? 17800a2430fSAndrzej Pietrasiewicz * (struct hidg_func_descriptor) 17900a2430fSAndrzej Pietrasiewicz */ 18000a2430fSAndrzej Pietrasiewicz }; 18100a2430fSAndrzej Pietrasiewicz 18200a2430fSAndrzej Pietrasiewicz static struct usb_descriptor_header *hidg_hs_descriptors[] = { 18300a2430fSAndrzej Pietrasiewicz (struct usb_descriptor_header *)&hidg_interface_desc, 18400a2430fSAndrzej Pietrasiewicz (struct usb_descriptor_header *)&hidg_desc, 18500a2430fSAndrzej Pietrasiewicz (struct usb_descriptor_header *)&hidg_hs_in_ep_desc, 18600a2430fSAndrzej Pietrasiewicz (struct usb_descriptor_header *)&hidg_hs_out_ep_desc, 18700a2430fSAndrzej Pietrasiewicz NULL, 18800a2430fSAndrzej Pietrasiewicz }; 18900a2430fSAndrzej Pietrasiewicz 19000a2430fSAndrzej Pietrasiewicz /* Full-Speed Support */ 19100a2430fSAndrzej Pietrasiewicz 19200a2430fSAndrzej Pietrasiewicz static struct usb_endpoint_descriptor hidg_fs_in_ep_desc = { 19300a2430fSAndrzej Pietrasiewicz .bLength = USB_DT_ENDPOINT_SIZE, 19400a2430fSAndrzej Pietrasiewicz .bDescriptorType = USB_DT_ENDPOINT, 19500a2430fSAndrzej Pietrasiewicz .bEndpointAddress = USB_DIR_IN, 19600a2430fSAndrzej Pietrasiewicz .bmAttributes = USB_ENDPOINT_XFER_INT, 19700a2430fSAndrzej Pietrasiewicz /*.wMaxPacketSize = DYNAMIC */ 19800a2430fSAndrzej Pietrasiewicz .bInterval = 10, /* FIXME: Add this field in the 19900a2430fSAndrzej Pietrasiewicz * HID gadget configuration? 20000a2430fSAndrzej Pietrasiewicz * (struct hidg_func_descriptor) 20100a2430fSAndrzej Pietrasiewicz */ 20200a2430fSAndrzej Pietrasiewicz }; 20300a2430fSAndrzej Pietrasiewicz 20400a2430fSAndrzej Pietrasiewicz static struct usb_endpoint_descriptor hidg_fs_out_ep_desc = { 20500a2430fSAndrzej Pietrasiewicz .bLength = USB_DT_ENDPOINT_SIZE, 20600a2430fSAndrzej Pietrasiewicz .bDescriptorType = USB_DT_ENDPOINT, 20700a2430fSAndrzej Pietrasiewicz .bEndpointAddress = USB_DIR_OUT, 20800a2430fSAndrzej Pietrasiewicz .bmAttributes = USB_ENDPOINT_XFER_INT, 20900a2430fSAndrzej Pietrasiewicz /*.wMaxPacketSize = DYNAMIC */ 21000a2430fSAndrzej Pietrasiewicz .bInterval = 10, /* FIXME: Add this field in the 21100a2430fSAndrzej Pietrasiewicz * HID gadget configuration? 21200a2430fSAndrzej Pietrasiewicz * (struct hidg_func_descriptor) 21300a2430fSAndrzej Pietrasiewicz */ 21400a2430fSAndrzej Pietrasiewicz }; 21500a2430fSAndrzej Pietrasiewicz 21600a2430fSAndrzej Pietrasiewicz static struct usb_descriptor_header *hidg_fs_descriptors[] = { 21700a2430fSAndrzej Pietrasiewicz (struct usb_descriptor_header *)&hidg_interface_desc, 21800a2430fSAndrzej Pietrasiewicz (struct usb_descriptor_header *)&hidg_desc, 21900a2430fSAndrzej Pietrasiewicz (struct usb_descriptor_header *)&hidg_fs_in_ep_desc, 22000a2430fSAndrzej Pietrasiewicz (struct usb_descriptor_header *)&hidg_fs_out_ep_desc, 22100a2430fSAndrzej Pietrasiewicz NULL, 22200a2430fSAndrzej Pietrasiewicz }; 22300a2430fSAndrzej Pietrasiewicz 22400a2430fSAndrzej Pietrasiewicz /*-------------------------------------------------------------------------*/ 225cb382536SAndrzej Pietrasiewicz /* Strings */ 226cb382536SAndrzej Pietrasiewicz 227cb382536SAndrzej Pietrasiewicz #define CT_FUNC_HID_IDX 0 228cb382536SAndrzej Pietrasiewicz 229cb382536SAndrzej Pietrasiewicz static struct usb_string ct_func_string_defs[] = { 230cb382536SAndrzej Pietrasiewicz [CT_FUNC_HID_IDX].s = "HID Interface", 231cb382536SAndrzej Pietrasiewicz {}, /* end of list */ 232cb382536SAndrzej Pietrasiewicz }; 233cb382536SAndrzej Pietrasiewicz 234cb382536SAndrzej Pietrasiewicz static struct usb_gadget_strings ct_func_string_table = { 235cb382536SAndrzej Pietrasiewicz .language = 0x0409, /* en-US */ 236cb382536SAndrzej Pietrasiewicz .strings = ct_func_string_defs, 237cb382536SAndrzej Pietrasiewicz }; 238cb382536SAndrzej Pietrasiewicz 239cb382536SAndrzej Pietrasiewicz static struct usb_gadget_strings *ct_func_strings[] = { 240cb382536SAndrzej Pietrasiewicz &ct_func_string_table, 241cb382536SAndrzej Pietrasiewicz NULL, 242cb382536SAndrzej Pietrasiewicz }; 243cb382536SAndrzej Pietrasiewicz 244cb382536SAndrzej Pietrasiewicz /*-------------------------------------------------------------------------*/ 24500a2430fSAndrzej Pietrasiewicz /* Char Device */ 24600a2430fSAndrzej Pietrasiewicz 24700a2430fSAndrzej Pietrasiewicz static ssize_t f_hidg_read(struct file *file, char __user *buffer, 24800a2430fSAndrzej Pietrasiewicz size_t count, loff_t *ptr) 24900a2430fSAndrzej Pietrasiewicz { 25000a2430fSAndrzej Pietrasiewicz struct f_hidg *hidg = file->private_data; 25100a2430fSAndrzej Pietrasiewicz struct f_hidg_req_list *list; 25200a2430fSAndrzej Pietrasiewicz struct usb_request *req; 25300a2430fSAndrzej Pietrasiewicz unsigned long flags; 25400a2430fSAndrzej Pietrasiewicz int ret; 25500a2430fSAndrzej Pietrasiewicz 25600a2430fSAndrzej Pietrasiewicz if (!count) 25700a2430fSAndrzej Pietrasiewicz return 0; 25800a2430fSAndrzej Pietrasiewicz 25900a2430fSAndrzej Pietrasiewicz if (!access_ok(VERIFY_WRITE, buffer, count)) 26000a2430fSAndrzej Pietrasiewicz return -EFAULT; 26100a2430fSAndrzej Pietrasiewicz 26233e4c1a9SKrzysztof Opasiak spin_lock_irqsave(&hidg->read_spinlock, flags); 26300a2430fSAndrzej Pietrasiewicz 26400a2430fSAndrzej Pietrasiewicz #define READ_COND (!list_empty(&hidg->completed_out_req)) 26500a2430fSAndrzej Pietrasiewicz 26600a2430fSAndrzej Pietrasiewicz /* wait for at least one buffer to complete */ 26700a2430fSAndrzej Pietrasiewicz while (!READ_COND) { 26833e4c1a9SKrzysztof Opasiak spin_unlock_irqrestore(&hidg->read_spinlock, flags); 26900a2430fSAndrzej Pietrasiewicz if (file->f_flags & O_NONBLOCK) 27000a2430fSAndrzej Pietrasiewicz return -EAGAIN; 27100a2430fSAndrzej Pietrasiewicz 27200a2430fSAndrzej Pietrasiewicz if (wait_event_interruptible(hidg->read_queue, READ_COND)) 27300a2430fSAndrzej Pietrasiewicz return -ERESTARTSYS; 27400a2430fSAndrzej Pietrasiewicz 27533e4c1a9SKrzysztof Opasiak spin_lock_irqsave(&hidg->read_spinlock, flags); 27600a2430fSAndrzej Pietrasiewicz } 27700a2430fSAndrzej Pietrasiewicz 27800a2430fSAndrzej Pietrasiewicz /* pick the first one */ 27900a2430fSAndrzej Pietrasiewicz list = list_first_entry(&hidg->completed_out_req, 28000a2430fSAndrzej Pietrasiewicz struct f_hidg_req_list, list); 281aa65d11aSKrzysztof Opasiak 282aa65d11aSKrzysztof Opasiak /* 283aa65d11aSKrzysztof Opasiak * Remove this from list to protect it from beign free() 284aa65d11aSKrzysztof Opasiak * while host disables our function 285aa65d11aSKrzysztof Opasiak */ 286aa65d11aSKrzysztof Opasiak list_del(&list->list); 287aa65d11aSKrzysztof Opasiak 28800a2430fSAndrzej Pietrasiewicz req = list->req; 28900a2430fSAndrzej Pietrasiewicz count = min_t(unsigned int, count, req->actual - list->pos); 29033e4c1a9SKrzysztof Opasiak spin_unlock_irqrestore(&hidg->read_spinlock, flags); 29100a2430fSAndrzej Pietrasiewicz 29200a2430fSAndrzej Pietrasiewicz /* copy to user outside spinlock */ 29300a2430fSAndrzej Pietrasiewicz count -= copy_to_user(buffer, req->buf + list->pos, count); 29400a2430fSAndrzej Pietrasiewicz list->pos += count; 29500a2430fSAndrzej Pietrasiewicz 29600a2430fSAndrzej Pietrasiewicz /* 29700a2430fSAndrzej Pietrasiewicz * if this request is completely handled and transfered to 29800a2430fSAndrzej Pietrasiewicz * userspace, remove its entry from the list and requeue it 29900a2430fSAndrzej Pietrasiewicz * again. Otherwise, we will revisit it again upon the next 30000a2430fSAndrzej Pietrasiewicz * call, taking into account its current read position. 30100a2430fSAndrzej Pietrasiewicz */ 30200a2430fSAndrzej Pietrasiewicz if (list->pos == req->actual) { 30300a2430fSAndrzej Pietrasiewicz kfree(list); 30400a2430fSAndrzej Pietrasiewicz 30500a2430fSAndrzej Pietrasiewicz req->length = hidg->report_length; 30600a2430fSAndrzej Pietrasiewicz ret = usb_ep_queue(hidg->out_ep, req, GFP_KERNEL); 307aa65d11aSKrzysztof Opasiak if (ret < 0) { 308aa65d11aSKrzysztof Opasiak free_ep_req(hidg->out_ep, req); 30900a2430fSAndrzej Pietrasiewicz return ret; 31000a2430fSAndrzej Pietrasiewicz } 311aa65d11aSKrzysztof Opasiak } else { 31233e4c1a9SKrzysztof Opasiak spin_lock_irqsave(&hidg->read_spinlock, flags); 313aa65d11aSKrzysztof Opasiak list_add(&list->list, &hidg->completed_out_req); 31433e4c1a9SKrzysztof Opasiak spin_unlock_irqrestore(&hidg->read_spinlock, flags); 315aa65d11aSKrzysztof Opasiak 316aa65d11aSKrzysztof Opasiak wake_up(&hidg->read_queue); 317aa65d11aSKrzysztof Opasiak } 31800a2430fSAndrzej Pietrasiewicz 31900a2430fSAndrzej Pietrasiewicz return count; 32000a2430fSAndrzej Pietrasiewicz } 32100a2430fSAndrzej Pietrasiewicz 32200a2430fSAndrzej Pietrasiewicz static void f_hidg_req_complete(struct usb_ep *ep, struct usb_request *req) 32300a2430fSAndrzej Pietrasiewicz { 32400a2430fSAndrzej Pietrasiewicz struct f_hidg *hidg = (struct f_hidg *)ep->driver_data; 32533e4c1a9SKrzysztof Opasiak unsigned long flags; 32600a2430fSAndrzej Pietrasiewicz 32700a2430fSAndrzej Pietrasiewicz if (req->status != 0) { 32800a2430fSAndrzej Pietrasiewicz ERROR(hidg->func.config->cdev, 32900a2430fSAndrzej Pietrasiewicz "End Point Request ERROR: %d\n", req->status); 33000a2430fSAndrzej Pietrasiewicz } 33100a2430fSAndrzej Pietrasiewicz 33233e4c1a9SKrzysztof Opasiak spin_lock_irqsave(&hidg->write_spinlock, flags); 33300a2430fSAndrzej Pietrasiewicz hidg->write_pending = 0; 33433e4c1a9SKrzysztof Opasiak spin_unlock_irqrestore(&hidg->write_spinlock, flags); 33500a2430fSAndrzej Pietrasiewicz wake_up(&hidg->write_queue); 33600a2430fSAndrzej Pietrasiewicz } 33700a2430fSAndrzej Pietrasiewicz 33800a2430fSAndrzej Pietrasiewicz static ssize_t f_hidg_write(struct file *file, const char __user *buffer, 33900a2430fSAndrzej Pietrasiewicz size_t count, loff_t *offp) 34000a2430fSAndrzej Pietrasiewicz { 34100a2430fSAndrzej Pietrasiewicz struct f_hidg *hidg = file->private_data; 342749494b6SKrzysztof Opasiak struct usb_request *req; 34333e4c1a9SKrzysztof Opasiak unsigned long flags; 34400a2430fSAndrzej Pietrasiewicz ssize_t status = -ENOMEM; 34500a2430fSAndrzej Pietrasiewicz 34600a2430fSAndrzej Pietrasiewicz if (!access_ok(VERIFY_READ, buffer, count)) 34700a2430fSAndrzej Pietrasiewicz return -EFAULT; 34800a2430fSAndrzej Pietrasiewicz 34933e4c1a9SKrzysztof Opasiak spin_lock_irqsave(&hidg->write_spinlock, flags); 35000a2430fSAndrzej Pietrasiewicz 35100a2430fSAndrzej Pietrasiewicz #define WRITE_COND (!hidg->write_pending) 352749494b6SKrzysztof Opasiak try_again: 35300a2430fSAndrzej Pietrasiewicz /* write queue */ 35400a2430fSAndrzej Pietrasiewicz while (!WRITE_COND) { 35533e4c1a9SKrzysztof Opasiak spin_unlock_irqrestore(&hidg->write_spinlock, flags); 35600a2430fSAndrzej Pietrasiewicz if (file->f_flags & O_NONBLOCK) 35700a2430fSAndrzej Pietrasiewicz return -EAGAIN; 35800a2430fSAndrzej Pietrasiewicz 35900a2430fSAndrzej Pietrasiewicz if (wait_event_interruptible_exclusive( 36000a2430fSAndrzej Pietrasiewicz hidg->write_queue, WRITE_COND)) 36100a2430fSAndrzej Pietrasiewicz return -ERESTARTSYS; 36200a2430fSAndrzej Pietrasiewicz 36333e4c1a9SKrzysztof Opasiak spin_lock_irqsave(&hidg->write_spinlock, flags); 36400a2430fSAndrzej Pietrasiewicz } 36500a2430fSAndrzej Pietrasiewicz 36633e4c1a9SKrzysztof Opasiak hidg->write_pending = 1; 367749494b6SKrzysztof Opasiak req = hidg->req; 36800a2430fSAndrzej Pietrasiewicz count = min_t(unsigned, count, hidg->report_length); 36933e4c1a9SKrzysztof Opasiak 37033e4c1a9SKrzysztof Opasiak spin_unlock_irqrestore(&hidg->write_spinlock, flags); 37125cd9721SKrzysztof Opasiak status = copy_from_user(req->buf, buffer, count); 37200a2430fSAndrzej Pietrasiewicz 37300a2430fSAndrzej Pietrasiewicz if (status != 0) { 37400a2430fSAndrzej Pietrasiewicz ERROR(hidg->func.config->cdev, 37500a2430fSAndrzej Pietrasiewicz "copy_from_user error\n"); 37633e4c1a9SKrzysztof Opasiak status = -EINVAL; 37733e4c1a9SKrzysztof Opasiak goto release_write_pending; 37800a2430fSAndrzej Pietrasiewicz } 37900a2430fSAndrzej Pietrasiewicz 380749494b6SKrzysztof Opasiak spin_lock_irqsave(&hidg->write_spinlock, flags); 381749494b6SKrzysztof Opasiak 38225cd9721SKrzysztof Opasiak /* when our function has been disabled by host */ 383749494b6SKrzysztof Opasiak if (!hidg->req) { 38425cd9721SKrzysztof Opasiak free_ep_req(hidg->in_ep, req); 385749494b6SKrzysztof Opasiak /* 386749494b6SKrzysztof Opasiak * TODO 387749494b6SKrzysztof Opasiak * Should we fail with error here? 388749494b6SKrzysztof Opasiak */ 389749494b6SKrzysztof Opasiak goto try_again; 390749494b6SKrzysztof Opasiak } 391749494b6SKrzysztof Opasiak 392749494b6SKrzysztof Opasiak req->status = 0; 393749494b6SKrzysztof Opasiak req->zero = 0; 394749494b6SKrzysztof Opasiak req->length = count; 395749494b6SKrzysztof Opasiak req->complete = f_hidg_req_complete; 396749494b6SKrzysztof Opasiak req->context = hidg; 39700a2430fSAndrzej Pietrasiewicz 39825cd9721SKrzysztof Opasiak status = usb_ep_queue(hidg->in_ep, req, GFP_ATOMIC); 39900a2430fSAndrzej Pietrasiewicz if (status < 0) { 40000a2430fSAndrzej Pietrasiewicz ERROR(hidg->func.config->cdev, 40100a2430fSAndrzej Pietrasiewicz "usb_ep_queue error on int endpoint %zd\n", status); 402749494b6SKrzysztof Opasiak goto release_write_pending_unlocked; 40300a2430fSAndrzej Pietrasiewicz } else { 40400a2430fSAndrzej Pietrasiewicz status = count; 40500a2430fSAndrzej Pietrasiewicz } 406749494b6SKrzysztof Opasiak spin_unlock_irqrestore(&hidg->write_spinlock, flags); 40700a2430fSAndrzej Pietrasiewicz 40833e4c1a9SKrzysztof Opasiak return status; 40933e4c1a9SKrzysztof Opasiak release_write_pending: 41033e4c1a9SKrzysztof Opasiak spin_lock_irqsave(&hidg->write_spinlock, flags); 411749494b6SKrzysztof Opasiak release_write_pending_unlocked: 41233e4c1a9SKrzysztof Opasiak hidg->write_pending = 0; 41333e4c1a9SKrzysztof Opasiak spin_unlock_irqrestore(&hidg->write_spinlock, flags); 41433e4c1a9SKrzysztof Opasiak 41533e4c1a9SKrzysztof Opasiak wake_up(&hidg->write_queue); 41600a2430fSAndrzej Pietrasiewicz 41700a2430fSAndrzej Pietrasiewicz return status; 41800a2430fSAndrzej Pietrasiewicz } 41900a2430fSAndrzej Pietrasiewicz 42000a2430fSAndrzej Pietrasiewicz static unsigned int f_hidg_poll(struct file *file, poll_table *wait) 42100a2430fSAndrzej Pietrasiewicz { 42200a2430fSAndrzej Pietrasiewicz struct f_hidg *hidg = file->private_data; 42300a2430fSAndrzej Pietrasiewicz unsigned int ret = 0; 42400a2430fSAndrzej Pietrasiewicz 42500a2430fSAndrzej Pietrasiewicz poll_wait(file, &hidg->read_queue, wait); 42600a2430fSAndrzej Pietrasiewicz poll_wait(file, &hidg->write_queue, wait); 42700a2430fSAndrzej Pietrasiewicz 42800a2430fSAndrzej Pietrasiewicz if (WRITE_COND) 42900a2430fSAndrzej Pietrasiewicz ret |= POLLOUT | POLLWRNORM; 43000a2430fSAndrzej Pietrasiewicz 43100a2430fSAndrzej Pietrasiewicz if (READ_COND) 43200a2430fSAndrzej Pietrasiewicz ret |= POLLIN | POLLRDNORM; 43300a2430fSAndrzej Pietrasiewicz 43400a2430fSAndrzej Pietrasiewicz return ret; 43500a2430fSAndrzej Pietrasiewicz } 43600a2430fSAndrzej Pietrasiewicz 43700a2430fSAndrzej Pietrasiewicz #undef WRITE_COND 43800a2430fSAndrzej Pietrasiewicz #undef READ_COND 43900a2430fSAndrzej Pietrasiewicz 44000a2430fSAndrzej Pietrasiewicz static int f_hidg_release(struct inode *inode, struct file *fd) 44100a2430fSAndrzej Pietrasiewicz { 44200a2430fSAndrzej Pietrasiewicz fd->private_data = NULL; 44300a2430fSAndrzej Pietrasiewicz return 0; 44400a2430fSAndrzej Pietrasiewicz } 44500a2430fSAndrzej Pietrasiewicz 44600a2430fSAndrzej Pietrasiewicz static int f_hidg_open(struct inode *inode, struct file *fd) 44700a2430fSAndrzej Pietrasiewicz { 44800a2430fSAndrzej Pietrasiewicz struct f_hidg *hidg = 44900a2430fSAndrzej Pietrasiewicz container_of(inode->i_cdev, struct f_hidg, cdev); 45000a2430fSAndrzej Pietrasiewicz 45100a2430fSAndrzej Pietrasiewicz fd->private_data = hidg; 45200a2430fSAndrzej Pietrasiewicz 45300a2430fSAndrzej Pietrasiewicz return 0; 45400a2430fSAndrzej Pietrasiewicz } 45500a2430fSAndrzej Pietrasiewicz 45600a2430fSAndrzej Pietrasiewicz /*-------------------------------------------------------------------------*/ 45700a2430fSAndrzej Pietrasiewicz /* usb_function */ 45800a2430fSAndrzej Pietrasiewicz 45900a2430fSAndrzej Pietrasiewicz static inline struct usb_request *hidg_alloc_ep_req(struct usb_ep *ep, 46000a2430fSAndrzej Pietrasiewicz unsigned length) 46100a2430fSAndrzej Pietrasiewicz { 462aadbe812SFelipe F. Tonello return alloc_ep_req(ep, length); 46300a2430fSAndrzej Pietrasiewicz } 46400a2430fSAndrzej Pietrasiewicz 46500a2430fSAndrzej Pietrasiewicz static void hidg_set_report_complete(struct usb_ep *ep, struct usb_request *req) 46600a2430fSAndrzej Pietrasiewicz { 46700a2430fSAndrzej Pietrasiewicz struct f_hidg *hidg = (struct f_hidg *) req->context; 46820d2ca95SKrzysztof Opasiak struct usb_composite_dev *cdev = hidg->func.config->cdev; 46900a2430fSAndrzej Pietrasiewicz struct f_hidg_req_list *req_list; 47000a2430fSAndrzej Pietrasiewicz unsigned long flags; 47100a2430fSAndrzej Pietrasiewicz 47220d2ca95SKrzysztof Opasiak switch (req->status) { 47320d2ca95SKrzysztof Opasiak case 0: 47400a2430fSAndrzej Pietrasiewicz req_list = kzalloc(sizeof(*req_list), GFP_ATOMIC); 47520d2ca95SKrzysztof Opasiak if (!req_list) { 47620d2ca95SKrzysztof Opasiak ERROR(cdev, "Unable to allocate mem for req_list\n"); 47720d2ca95SKrzysztof Opasiak goto free_req; 47820d2ca95SKrzysztof Opasiak } 47900a2430fSAndrzej Pietrasiewicz 48000a2430fSAndrzej Pietrasiewicz req_list->req = req; 48100a2430fSAndrzej Pietrasiewicz 48233e4c1a9SKrzysztof Opasiak spin_lock_irqsave(&hidg->read_spinlock, flags); 48300a2430fSAndrzej Pietrasiewicz list_add_tail(&req_list->list, &hidg->completed_out_req); 48433e4c1a9SKrzysztof Opasiak spin_unlock_irqrestore(&hidg->read_spinlock, flags); 48500a2430fSAndrzej Pietrasiewicz 48600a2430fSAndrzej Pietrasiewicz wake_up(&hidg->read_queue); 48720d2ca95SKrzysztof Opasiak break; 48820d2ca95SKrzysztof Opasiak default: 48920d2ca95SKrzysztof Opasiak ERROR(cdev, "Set report failed %d\n", req->status); 49020d2ca95SKrzysztof Opasiak /* FALLTHROUGH */ 49120d2ca95SKrzysztof Opasiak case -ECONNABORTED: /* hardware forced ep reset */ 49220d2ca95SKrzysztof Opasiak case -ECONNRESET: /* request dequeued */ 49320d2ca95SKrzysztof Opasiak case -ESHUTDOWN: /* disconnect from host */ 49420d2ca95SKrzysztof Opasiak free_req: 49520d2ca95SKrzysztof Opasiak free_ep_req(ep, req); 49620d2ca95SKrzysztof Opasiak return; 49720d2ca95SKrzysztof Opasiak } 49800a2430fSAndrzej Pietrasiewicz } 49900a2430fSAndrzej Pietrasiewicz 50000a2430fSAndrzej Pietrasiewicz static int hidg_setup(struct usb_function *f, 50100a2430fSAndrzej Pietrasiewicz const struct usb_ctrlrequest *ctrl) 50200a2430fSAndrzej Pietrasiewicz { 50300a2430fSAndrzej Pietrasiewicz struct f_hidg *hidg = func_to_hidg(f); 50400a2430fSAndrzej Pietrasiewicz struct usb_composite_dev *cdev = f->config->cdev; 50500a2430fSAndrzej Pietrasiewicz struct usb_request *req = cdev->req; 50600a2430fSAndrzej Pietrasiewicz int status = 0; 50700a2430fSAndrzej Pietrasiewicz __u16 value, length; 50800a2430fSAndrzej Pietrasiewicz 50900a2430fSAndrzej Pietrasiewicz value = __le16_to_cpu(ctrl->wValue); 51000a2430fSAndrzej Pietrasiewicz length = __le16_to_cpu(ctrl->wLength); 51100a2430fSAndrzej Pietrasiewicz 512c9b3bde0SJulia Lawall VDBG(cdev, 513c9b3bde0SJulia Lawall "%s crtl_request : bRequestType:0x%x bRequest:0x%x Value:0x%x\n", 514c9b3bde0SJulia Lawall __func__, ctrl->bRequestType, ctrl->bRequest, value); 51500a2430fSAndrzej Pietrasiewicz 51600a2430fSAndrzej Pietrasiewicz switch ((ctrl->bRequestType << 8) | ctrl->bRequest) { 51700a2430fSAndrzej Pietrasiewicz case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8 51800a2430fSAndrzej Pietrasiewicz | HID_REQ_GET_REPORT): 51900a2430fSAndrzej Pietrasiewicz VDBG(cdev, "get_report\n"); 52000a2430fSAndrzej Pietrasiewicz 52100a2430fSAndrzej Pietrasiewicz /* send an empty report */ 52200a2430fSAndrzej Pietrasiewicz length = min_t(unsigned, length, hidg->report_length); 52300a2430fSAndrzej Pietrasiewicz memset(req->buf, 0x0, length); 52400a2430fSAndrzej Pietrasiewicz 52500a2430fSAndrzej Pietrasiewicz goto respond; 52600a2430fSAndrzej Pietrasiewicz break; 52700a2430fSAndrzej Pietrasiewicz 52800a2430fSAndrzej Pietrasiewicz case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8 52900a2430fSAndrzej Pietrasiewicz | HID_REQ_GET_PROTOCOL): 53000a2430fSAndrzej Pietrasiewicz VDBG(cdev, "get_protocol\n"); 531*b3c4ec71SAbdulhadi Mohamed length = min_t(unsigned int, length, 1); 532*b3c4ec71SAbdulhadi Mohamed ((u8 *) req->buf)[0] = hidg->protocol; 533*b3c4ec71SAbdulhadi Mohamed goto respond; 53400a2430fSAndrzej Pietrasiewicz break; 53500a2430fSAndrzej Pietrasiewicz 53600a2430fSAndrzej Pietrasiewicz case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8 53700a2430fSAndrzej Pietrasiewicz | HID_REQ_SET_REPORT): 5386774def6SMasanari Iida VDBG(cdev, "set_report | wLength=%d\n", ctrl->wLength); 53900a2430fSAndrzej Pietrasiewicz goto stall; 54000a2430fSAndrzej Pietrasiewicz break; 54100a2430fSAndrzej Pietrasiewicz 54200a2430fSAndrzej Pietrasiewicz case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8 54300a2430fSAndrzej Pietrasiewicz | HID_REQ_SET_PROTOCOL): 54400a2430fSAndrzej Pietrasiewicz VDBG(cdev, "set_protocol\n"); 545*b3c4ec71SAbdulhadi Mohamed if (value > HID_REPORT_PROTOCOL) 546*b3c4ec71SAbdulhadi Mohamed goto stall; 547*b3c4ec71SAbdulhadi Mohamed length = 0; 548*b3c4ec71SAbdulhadi Mohamed /* 549*b3c4ec71SAbdulhadi Mohamed * We assume that programs implementing the Boot protocol 550*b3c4ec71SAbdulhadi Mohamed * are also compatible with the Report Protocol 551*b3c4ec71SAbdulhadi Mohamed */ 552*b3c4ec71SAbdulhadi Mohamed if (hidg->bInterfaceSubClass == USB_INTERFACE_SUBCLASS_BOOT) { 553*b3c4ec71SAbdulhadi Mohamed hidg->protocol = value; 554*b3c4ec71SAbdulhadi Mohamed goto respond; 555*b3c4ec71SAbdulhadi Mohamed } 55600a2430fSAndrzej Pietrasiewicz goto stall; 55700a2430fSAndrzej Pietrasiewicz break; 55800a2430fSAndrzej Pietrasiewicz 55900a2430fSAndrzej Pietrasiewicz case ((USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_INTERFACE) << 8 56000a2430fSAndrzej Pietrasiewicz | USB_REQ_GET_DESCRIPTOR): 56100a2430fSAndrzej Pietrasiewicz switch (value >> 8) { 56200a2430fSAndrzej Pietrasiewicz case HID_DT_HID: 563f286d487SKrzysztof Opasiak { 564f286d487SKrzysztof Opasiak struct hid_descriptor hidg_desc_copy = hidg_desc; 565f286d487SKrzysztof Opasiak 56600a2430fSAndrzej Pietrasiewicz VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: HID\n"); 567f286d487SKrzysztof Opasiak hidg_desc_copy.desc[0].bDescriptorType = HID_DT_REPORT; 568f286d487SKrzysztof Opasiak hidg_desc_copy.desc[0].wDescriptorLength = 569f286d487SKrzysztof Opasiak cpu_to_le16(hidg->report_desc_length); 570f286d487SKrzysztof Opasiak 57100a2430fSAndrzej Pietrasiewicz length = min_t(unsigned short, length, 572f286d487SKrzysztof Opasiak hidg_desc_copy.bLength); 573f286d487SKrzysztof Opasiak memcpy(req->buf, &hidg_desc_copy, length); 57400a2430fSAndrzej Pietrasiewicz goto respond; 57500a2430fSAndrzej Pietrasiewicz break; 576f286d487SKrzysztof Opasiak } 57700a2430fSAndrzej Pietrasiewicz case HID_DT_REPORT: 57800a2430fSAndrzej Pietrasiewicz VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: REPORT\n"); 57900a2430fSAndrzej Pietrasiewicz length = min_t(unsigned short, length, 58000a2430fSAndrzej Pietrasiewicz hidg->report_desc_length); 58100a2430fSAndrzej Pietrasiewicz memcpy(req->buf, hidg->report_desc, length); 58200a2430fSAndrzej Pietrasiewicz goto respond; 58300a2430fSAndrzej Pietrasiewicz break; 58400a2430fSAndrzej Pietrasiewicz 58500a2430fSAndrzej Pietrasiewicz default: 58600a2430fSAndrzej Pietrasiewicz VDBG(cdev, "Unknown descriptor request 0x%x\n", 58700a2430fSAndrzej Pietrasiewicz value >> 8); 58800a2430fSAndrzej Pietrasiewicz goto stall; 58900a2430fSAndrzej Pietrasiewicz break; 59000a2430fSAndrzej Pietrasiewicz } 59100a2430fSAndrzej Pietrasiewicz break; 59200a2430fSAndrzej Pietrasiewicz 59300a2430fSAndrzej Pietrasiewicz default: 59400a2430fSAndrzej Pietrasiewicz VDBG(cdev, "Unknown request 0x%x\n", 59500a2430fSAndrzej Pietrasiewicz ctrl->bRequest); 59600a2430fSAndrzej Pietrasiewicz goto stall; 59700a2430fSAndrzej Pietrasiewicz break; 59800a2430fSAndrzej Pietrasiewicz } 59900a2430fSAndrzej Pietrasiewicz 60000a2430fSAndrzej Pietrasiewicz stall: 60100a2430fSAndrzej Pietrasiewicz return -EOPNOTSUPP; 60200a2430fSAndrzej Pietrasiewicz 60300a2430fSAndrzej Pietrasiewicz respond: 60400a2430fSAndrzej Pietrasiewicz req->zero = 0; 60500a2430fSAndrzej Pietrasiewicz req->length = length; 60600a2430fSAndrzej Pietrasiewicz status = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC); 60700a2430fSAndrzej Pietrasiewicz if (status < 0) 60800a2430fSAndrzej Pietrasiewicz ERROR(cdev, "usb_ep_queue error on ep0 %d\n", value); 60900a2430fSAndrzej Pietrasiewicz return status; 61000a2430fSAndrzej Pietrasiewicz } 61100a2430fSAndrzej Pietrasiewicz 61200a2430fSAndrzej Pietrasiewicz static void hidg_disable(struct usb_function *f) 61300a2430fSAndrzej Pietrasiewicz { 61400a2430fSAndrzej Pietrasiewicz struct f_hidg *hidg = func_to_hidg(f); 61500a2430fSAndrzej Pietrasiewicz struct f_hidg_req_list *list, *next; 616aa65d11aSKrzysztof Opasiak unsigned long flags; 61700a2430fSAndrzej Pietrasiewicz 61800a2430fSAndrzej Pietrasiewicz usb_ep_disable(hidg->in_ep); 61900a2430fSAndrzej Pietrasiewicz usb_ep_disable(hidg->out_ep); 62000a2430fSAndrzej Pietrasiewicz 62133e4c1a9SKrzysztof Opasiak spin_lock_irqsave(&hidg->read_spinlock, flags); 62200a2430fSAndrzej Pietrasiewicz list_for_each_entry_safe(list, next, &hidg->completed_out_req, list) { 623aa65d11aSKrzysztof Opasiak free_ep_req(hidg->out_ep, list->req); 62400a2430fSAndrzej Pietrasiewicz list_del(&list->list); 62500a2430fSAndrzej Pietrasiewicz kfree(list); 62600a2430fSAndrzej Pietrasiewicz } 62733e4c1a9SKrzysztof Opasiak spin_unlock_irqrestore(&hidg->read_spinlock, flags); 628749494b6SKrzysztof Opasiak 629749494b6SKrzysztof Opasiak spin_lock_irqsave(&hidg->write_spinlock, flags); 630749494b6SKrzysztof Opasiak if (!hidg->write_pending) { 631749494b6SKrzysztof Opasiak free_ep_req(hidg->in_ep, hidg->req); 632749494b6SKrzysztof Opasiak hidg->write_pending = 1; 633749494b6SKrzysztof Opasiak } 634749494b6SKrzysztof Opasiak 635749494b6SKrzysztof Opasiak hidg->req = NULL; 636749494b6SKrzysztof Opasiak spin_unlock_irqrestore(&hidg->write_spinlock, flags); 63700a2430fSAndrzej Pietrasiewicz } 63800a2430fSAndrzej Pietrasiewicz 63900a2430fSAndrzej Pietrasiewicz static int hidg_set_alt(struct usb_function *f, unsigned intf, unsigned alt) 64000a2430fSAndrzej Pietrasiewicz { 64100a2430fSAndrzej Pietrasiewicz struct usb_composite_dev *cdev = f->config->cdev; 64200a2430fSAndrzej Pietrasiewicz struct f_hidg *hidg = func_to_hidg(f); 643749494b6SKrzysztof Opasiak struct usb_request *req_in = NULL; 644749494b6SKrzysztof Opasiak unsigned long flags; 64500a2430fSAndrzej Pietrasiewicz int i, status = 0; 64600a2430fSAndrzej Pietrasiewicz 64700a2430fSAndrzej Pietrasiewicz VDBG(cdev, "hidg_set_alt intf:%d alt:%d\n", intf, alt); 64800a2430fSAndrzej Pietrasiewicz 64900a2430fSAndrzej Pietrasiewicz if (hidg->in_ep != NULL) { 65000a2430fSAndrzej Pietrasiewicz /* restart endpoint */ 65100a2430fSAndrzej Pietrasiewicz usb_ep_disable(hidg->in_ep); 65200a2430fSAndrzej Pietrasiewicz 65300a2430fSAndrzej Pietrasiewicz status = config_ep_by_speed(f->config->cdev->gadget, f, 65400a2430fSAndrzej Pietrasiewicz hidg->in_ep); 65500a2430fSAndrzej Pietrasiewicz if (status) { 65600a2430fSAndrzej Pietrasiewicz ERROR(cdev, "config_ep_by_speed FAILED!\n"); 65700a2430fSAndrzej Pietrasiewicz goto fail; 65800a2430fSAndrzej Pietrasiewicz } 65900a2430fSAndrzej Pietrasiewicz status = usb_ep_enable(hidg->in_ep); 66000a2430fSAndrzej Pietrasiewicz if (status < 0) { 66100a2430fSAndrzej Pietrasiewicz ERROR(cdev, "Enable IN endpoint FAILED!\n"); 66200a2430fSAndrzej Pietrasiewicz goto fail; 66300a2430fSAndrzej Pietrasiewicz } 66400a2430fSAndrzej Pietrasiewicz hidg->in_ep->driver_data = hidg; 665749494b6SKrzysztof Opasiak 666749494b6SKrzysztof Opasiak req_in = hidg_alloc_ep_req(hidg->in_ep, hidg->report_length); 667749494b6SKrzysztof Opasiak if (!req_in) { 668749494b6SKrzysztof Opasiak status = -ENOMEM; 669749494b6SKrzysztof Opasiak goto disable_ep_in; 670749494b6SKrzysztof Opasiak } 67100a2430fSAndrzej Pietrasiewicz } 67200a2430fSAndrzej Pietrasiewicz 67300a2430fSAndrzej Pietrasiewicz 67400a2430fSAndrzej Pietrasiewicz if (hidg->out_ep != NULL) { 67500a2430fSAndrzej Pietrasiewicz /* restart endpoint */ 67600a2430fSAndrzej Pietrasiewicz usb_ep_disable(hidg->out_ep); 67700a2430fSAndrzej Pietrasiewicz 67800a2430fSAndrzej Pietrasiewicz status = config_ep_by_speed(f->config->cdev->gadget, f, 67900a2430fSAndrzej Pietrasiewicz hidg->out_ep); 68000a2430fSAndrzej Pietrasiewicz if (status) { 68100a2430fSAndrzej Pietrasiewicz ERROR(cdev, "config_ep_by_speed FAILED!\n"); 682749494b6SKrzysztof Opasiak goto free_req_in; 68300a2430fSAndrzej Pietrasiewicz } 68400a2430fSAndrzej Pietrasiewicz status = usb_ep_enable(hidg->out_ep); 68500a2430fSAndrzej Pietrasiewicz if (status < 0) { 68643aef5c2SDavid Lechner ERROR(cdev, "Enable OUT endpoint FAILED!\n"); 687749494b6SKrzysztof Opasiak goto free_req_in; 68800a2430fSAndrzej Pietrasiewicz } 68900a2430fSAndrzej Pietrasiewicz hidg->out_ep->driver_data = hidg; 69000a2430fSAndrzej Pietrasiewicz 69100a2430fSAndrzej Pietrasiewicz /* 69200a2430fSAndrzej Pietrasiewicz * allocate a bunch of read buffers and queue them all at once. 69300a2430fSAndrzej Pietrasiewicz */ 69400a2430fSAndrzej Pietrasiewicz for (i = 0; i < hidg->qlen && status == 0; i++) { 69500a2430fSAndrzej Pietrasiewicz struct usb_request *req = 69600a2430fSAndrzej Pietrasiewicz hidg_alloc_ep_req(hidg->out_ep, 69700a2430fSAndrzej Pietrasiewicz hidg->report_length); 69800a2430fSAndrzej Pietrasiewicz if (req) { 69900a2430fSAndrzej Pietrasiewicz req->complete = hidg_set_report_complete; 70000a2430fSAndrzej Pietrasiewicz req->context = hidg; 70100a2430fSAndrzej Pietrasiewicz status = usb_ep_queue(hidg->out_ep, req, 70200a2430fSAndrzej Pietrasiewicz GFP_ATOMIC); 703749494b6SKrzysztof Opasiak if (status) { 70400a2430fSAndrzej Pietrasiewicz ERROR(cdev, "%s queue req --> %d\n", 70500a2430fSAndrzej Pietrasiewicz hidg->out_ep->name, status); 706749494b6SKrzysztof Opasiak free_ep_req(hidg->out_ep, req); 707749494b6SKrzysztof Opasiak } 70800a2430fSAndrzej Pietrasiewicz } else { 70900a2430fSAndrzej Pietrasiewicz status = -ENOMEM; 710749494b6SKrzysztof Opasiak goto disable_out_ep; 71100a2430fSAndrzej Pietrasiewicz } 71200a2430fSAndrzej Pietrasiewicz } 71300a2430fSAndrzej Pietrasiewicz } 71400a2430fSAndrzej Pietrasiewicz 715749494b6SKrzysztof Opasiak if (hidg->in_ep != NULL) { 716749494b6SKrzysztof Opasiak spin_lock_irqsave(&hidg->write_spinlock, flags); 717749494b6SKrzysztof Opasiak hidg->req = req_in; 718749494b6SKrzysztof Opasiak hidg->write_pending = 0; 719749494b6SKrzysztof Opasiak spin_unlock_irqrestore(&hidg->write_spinlock, flags); 720749494b6SKrzysztof Opasiak 721749494b6SKrzysztof Opasiak wake_up(&hidg->write_queue); 722749494b6SKrzysztof Opasiak } 723749494b6SKrzysztof Opasiak return 0; 724749494b6SKrzysztof Opasiak disable_out_ep: 725749494b6SKrzysztof Opasiak usb_ep_disable(hidg->out_ep); 726749494b6SKrzysztof Opasiak free_req_in: 727749494b6SKrzysztof Opasiak if (req_in) 728749494b6SKrzysztof Opasiak free_ep_req(hidg->in_ep, req_in); 729749494b6SKrzysztof Opasiak 730749494b6SKrzysztof Opasiak disable_ep_in: 731749494b6SKrzysztof Opasiak if (hidg->in_ep) 732749494b6SKrzysztof Opasiak usb_ep_disable(hidg->in_ep); 733749494b6SKrzysztof Opasiak 73400a2430fSAndrzej Pietrasiewicz fail: 73500a2430fSAndrzej Pietrasiewicz return status; 73600a2430fSAndrzej Pietrasiewicz } 73700a2430fSAndrzej Pietrasiewicz 7387a3cc461SLad, Prabhakar static const struct file_operations f_hidg_fops = { 73900a2430fSAndrzej Pietrasiewicz .owner = THIS_MODULE, 74000a2430fSAndrzej Pietrasiewicz .open = f_hidg_open, 74100a2430fSAndrzej Pietrasiewicz .release = f_hidg_release, 74200a2430fSAndrzej Pietrasiewicz .write = f_hidg_write, 74300a2430fSAndrzej Pietrasiewicz .read = f_hidg_read, 74400a2430fSAndrzej Pietrasiewicz .poll = f_hidg_poll, 74500a2430fSAndrzej Pietrasiewicz .llseek = noop_llseek, 74600a2430fSAndrzej Pietrasiewicz }; 74700a2430fSAndrzej Pietrasiewicz 748cb382536SAndrzej Pietrasiewicz static int hidg_bind(struct usb_configuration *c, struct usb_function *f) 74900a2430fSAndrzej Pietrasiewicz { 75000a2430fSAndrzej Pietrasiewicz struct usb_ep *ep; 75100a2430fSAndrzej Pietrasiewicz struct f_hidg *hidg = func_to_hidg(f); 7525ca8d3ecSAndrzej Pietrasiewicz struct usb_string *us; 75363406087SAndrzej Pietrasiewicz struct device *device; 75400a2430fSAndrzej Pietrasiewicz int status; 75500a2430fSAndrzej Pietrasiewicz dev_t dev; 75600a2430fSAndrzej Pietrasiewicz 757cb382536SAndrzej Pietrasiewicz /* maybe allocate device-global string IDs, and patch descriptors */ 7585ca8d3ecSAndrzej Pietrasiewicz us = usb_gstrings_attach(c->cdev, ct_func_strings, 7595ca8d3ecSAndrzej Pietrasiewicz ARRAY_SIZE(ct_func_string_defs)); 7605ca8d3ecSAndrzej Pietrasiewicz if (IS_ERR(us)) 7615ca8d3ecSAndrzej Pietrasiewicz return PTR_ERR(us); 7625ca8d3ecSAndrzej Pietrasiewicz hidg_interface_desc.iInterface = us[CT_FUNC_HID_IDX].id; 763cb382536SAndrzej Pietrasiewicz 76400a2430fSAndrzej Pietrasiewicz /* allocate instance-specific interface IDs, and patch descriptors */ 76500a2430fSAndrzej Pietrasiewicz status = usb_interface_id(c, f); 76600a2430fSAndrzej Pietrasiewicz if (status < 0) 76700a2430fSAndrzej Pietrasiewicz goto fail; 76800a2430fSAndrzej Pietrasiewicz hidg_interface_desc.bInterfaceNumber = status; 76900a2430fSAndrzej Pietrasiewicz 77000a2430fSAndrzej Pietrasiewicz /* allocate instance-specific endpoints */ 77100a2430fSAndrzej Pietrasiewicz status = -ENODEV; 77200a2430fSAndrzej Pietrasiewicz ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_in_ep_desc); 77300a2430fSAndrzej Pietrasiewicz if (!ep) 77400a2430fSAndrzej Pietrasiewicz goto fail; 77500a2430fSAndrzej Pietrasiewicz hidg->in_ep = ep; 77600a2430fSAndrzej Pietrasiewicz 77700a2430fSAndrzej Pietrasiewicz ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_out_ep_desc); 77800a2430fSAndrzej Pietrasiewicz if (!ep) 77900a2430fSAndrzej Pietrasiewicz goto fail; 78000a2430fSAndrzej Pietrasiewicz hidg->out_ep = ep; 78100a2430fSAndrzej Pietrasiewicz 78200a2430fSAndrzej Pietrasiewicz /* set descriptor dynamic values */ 78300a2430fSAndrzej Pietrasiewicz hidg_interface_desc.bInterfaceSubClass = hidg->bInterfaceSubClass; 78400a2430fSAndrzej Pietrasiewicz hidg_interface_desc.bInterfaceProtocol = hidg->bInterfaceProtocol; 785*b3c4ec71SAbdulhadi Mohamed hidg->protocol = HID_REPORT_PROTOCOL; 786dbf499cfSJanusz Dziedzic hidg_ss_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length); 787dbf499cfSJanusz Dziedzic hidg_ss_in_comp_desc.wBytesPerInterval = 788dbf499cfSJanusz Dziedzic cpu_to_le16(hidg->report_length); 78900a2430fSAndrzej Pietrasiewicz hidg_hs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length); 79000a2430fSAndrzej Pietrasiewicz hidg_fs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length); 791dbf499cfSJanusz Dziedzic hidg_ss_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length); 792dbf499cfSJanusz Dziedzic hidg_ss_out_comp_desc.wBytesPerInterval = 793dbf499cfSJanusz Dziedzic cpu_to_le16(hidg->report_length); 79400a2430fSAndrzej Pietrasiewicz hidg_hs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length); 79500a2430fSAndrzej Pietrasiewicz hidg_fs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length); 796f286d487SKrzysztof Opasiak /* 797f286d487SKrzysztof Opasiak * We can use hidg_desc struct here but we should not relay 798f286d487SKrzysztof Opasiak * that its content won't change after returning from this function. 799f286d487SKrzysztof Opasiak */ 80000a2430fSAndrzej Pietrasiewicz hidg_desc.desc[0].bDescriptorType = HID_DT_REPORT; 80100a2430fSAndrzej Pietrasiewicz hidg_desc.desc[0].wDescriptorLength = 80200a2430fSAndrzej Pietrasiewicz cpu_to_le16(hidg->report_desc_length); 80300a2430fSAndrzej Pietrasiewicz 80400a2430fSAndrzej Pietrasiewicz hidg_hs_in_ep_desc.bEndpointAddress = 80500a2430fSAndrzej Pietrasiewicz hidg_fs_in_ep_desc.bEndpointAddress; 80600a2430fSAndrzej Pietrasiewicz hidg_hs_out_ep_desc.bEndpointAddress = 80700a2430fSAndrzej Pietrasiewicz hidg_fs_out_ep_desc.bEndpointAddress; 80800a2430fSAndrzej Pietrasiewicz 809dbf499cfSJanusz Dziedzic hidg_ss_in_ep_desc.bEndpointAddress = 810dbf499cfSJanusz Dziedzic hidg_fs_in_ep_desc.bEndpointAddress; 811dbf499cfSJanusz Dziedzic hidg_ss_out_ep_desc.bEndpointAddress = 812dbf499cfSJanusz Dziedzic hidg_fs_out_ep_desc.bEndpointAddress; 813dbf499cfSJanusz Dziedzic 81400a2430fSAndrzej Pietrasiewicz status = usb_assign_descriptors(f, hidg_fs_descriptors, 815dbf499cfSJanusz Dziedzic hidg_hs_descriptors, hidg_ss_descriptors, NULL); 81600a2430fSAndrzej Pietrasiewicz if (status) 81700a2430fSAndrzej Pietrasiewicz goto fail; 81800a2430fSAndrzej Pietrasiewicz 81933e4c1a9SKrzysztof Opasiak spin_lock_init(&hidg->write_spinlock); 820749494b6SKrzysztof Opasiak hidg->write_pending = 1; 821749494b6SKrzysztof Opasiak hidg->req = NULL; 82233e4c1a9SKrzysztof Opasiak spin_lock_init(&hidg->read_spinlock); 82300a2430fSAndrzej Pietrasiewicz init_waitqueue_head(&hidg->write_queue); 82400a2430fSAndrzej Pietrasiewicz init_waitqueue_head(&hidg->read_queue); 82500a2430fSAndrzej Pietrasiewicz INIT_LIST_HEAD(&hidg->completed_out_req); 82600a2430fSAndrzej Pietrasiewicz 82700a2430fSAndrzej Pietrasiewicz /* create char device */ 82800a2430fSAndrzej Pietrasiewicz cdev_init(&hidg->cdev, &f_hidg_fops); 82900a2430fSAndrzej Pietrasiewicz dev = MKDEV(major, hidg->minor); 83000a2430fSAndrzej Pietrasiewicz status = cdev_add(&hidg->cdev, dev, 1); 83100a2430fSAndrzej Pietrasiewicz if (status) 832d12a8727SPavitrakumar Managutte goto fail_free_descs; 83300a2430fSAndrzej Pietrasiewicz 83463406087SAndrzej Pietrasiewicz device = device_create(hidg_class, NULL, dev, NULL, 83563406087SAndrzej Pietrasiewicz "%s%d", "hidg", hidg->minor); 83663406087SAndrzej Pietrasiewicz if (IS_ERR(device)) { 83763406087SAndrzej Pietrasiewicz status = PTR_ERR(device); 83863406087SAndrzej Pietrasiewicz goto del; 83963406087SAndrzej Pietrasiewicz } 84000a2430fSAndrzej Pietrasiewicz 84100a2430fSAndrzej Pietrasiewicz return 0; 84263406087SAndrzej Pietrasiewicz del: 84363406087SAndrzej Pietrasiewicz cdev_del(&hidg->cdev); 844d12a8727SPavitrakumar Managutte fail_free_descs: 845d12a8727SPavitrakumar Managutte usb_free_all_descriptors(f); 84600a2430fSAndrzej Pietrasiewicz fail: 84700a2430fSAndrzej Pietrasiewicz ERROR(f->config->cdev, "hidg_bind FAILED\n"); 84814794d71SFelipe F. Tonello if (hidg->req != NULL) 84914794d71SFelipe F. Tonello free_ep_req(hidg->in_ep, hidg->req); 85000a2430fSAndrzej Pietrasiewicz 85100a2430fSAndrzej Pietrasiewicz return status; 85200a2430fSAndrzej Pietrasiewicz } 85300a2430fSAndrzej Pietrasiewicz 854cb382536SAndrzej Pietrasiewicz static inline int hidg_get_minor(void) 855cb382536SAndrzej Pietrasiewicz { 856cb382536SAndrzej Pietrasiewicz int ret; 857cb382536SAndrzej Pietrasiewicz 858cb382536SAndrzej Pietrasiewicz ret = ida_simple_get(&hidg_ida, 0, 0, GFP_KERNEL); 859774cf72fSAndrzej Pietrasiewicz if (ret >= HIDG_MINORS) { 860774cf72fSAndrzej Pietrasiewicz ida_simple_remove(&hidg_ida, ret); 861774cf72fSAndrzej Pietrasiewicz ret = -ENODEV; 862774cf72fSAndrzej Pietrasiewicz } 863cb382536SAndrzej Pietrasiewicz 864cb382536SAndrzej Pietrasiewicz return ret; 865cb382536SAndrzej Pietrasiewicz } 866cb382536SAndrzej Pietrasiewicz 86721a9476aSAndrzej Pietrasiewicz static inline struct f_hid_opts *to_f_hid_opts(struct config_item *item) 86821a9476aSAndrzej Pietrasiewicz { 86921a9476aSAndrzej Pietrasiewicz return container_of(to_config_group(item), struct f_hid_opts, 87021a9476aSAndrzej Pietrasiewicz func_inst.group); 87121a9476aSAndrzej Pietrasiewicz } 87221a9476aSAndrzej Pietrasiewicz 87321a9476aSAndrzej Pietrasiewicz static void hid_attr_release(struct config_item *item) 87421a9476aSAndrzej Pietrasiewicz { 87521a9476aSAndrzej Pietrasiewicz struct f_hid_opts *opts = to_f_hid_opts(item); 87621a9476aSAndrzej Pietrasiewicz 87721a9476aSAndrzej Pietrasiewicz usb_put_function_instance(&opts->func_inst); 87821a9476aSAndrzej Pietrasiewicz } 87921a9476aSAndrzej Pietrasiewicz 88021a9476aSAndrzej Pietrasiewicz static struct configfs_item_operations hidg_item_ops = { 88121a9476aSAndrzej Pietrasiewicz .release = hid_attr_release, 88221a9476aSAndrzej Pietrasiewicz }; 88321a9476aSAndrzej Pietrasiewicz 88421a9476aSAndrzej Pietrasiewicz #define F_HID_OPT(name, prec, limit) \ 885da4e527cSChristoph Hellwig static ssize_t f_hid_opts_##name##_show(struct config_item *item, char *page)\ 88621a9476aSAndrzej Pietrasiewicz { \ 887da4e527cSChristoph Hellwig struct f_hid_opts *opts = to_f_hid_opts(item); \ 88821a9476aSAndrzej Pietrasiewicz int result; \ 88921a9476aSAndrzej Pietrasiewicz \ 89021a9476aSAndrzej Pietrasiewicz mutex_lock(&opts->lock); \ 89121a9476aSAndrzej Pietrasiewicz result = sprintf(page, "%d\n", opts->name); \ 89221a9476aSAndrzej Pietrasiewicz mutex_unlock(&opts->lock); \ 89321a9476aSAndrzej Pietrasiewicz \ 89421a9476aSAndrzej Pietrasiewicz return result; \ 89521a9476aSAndrzej Pietrasiewicz } \ 89621a9476aSAndrzej Pietrasiewicz \ 897da4e527cSChristoph Hellwig static ssize_t f_hid_opts_##name##_store(struct config_item *item, \ 89821a9476aSAndrzej Pietrasiewicz const char *page, size_t len) \ 89921a9476aSAndrzej Pietrasiewicz { \ 900da4e527cSChristoph Hellwig struct f_hid_opts *opts = to_f_hid_opts(item); \ 90121a9476aSAndrzej Pietrasiewicz int ret; \ 90221a9476aSAndrzej Pietrasiewicz u##prec num; \ 90321a9476aSAndrzej Pietrasiewicz \ 90421a9476aSAndrzej Pietrasiewicz mutex_lock(&opts->lock); \ 90521a9476aSAndrzej Pietrasiewicz if (opts->refcnt) { \ 90621a9476aSAndrzej Pietrasiewicz ret = -EBUSY; \ 90721a9476aSAndrzej Pietrasiewicz goto end; \ 90821a9476aSAndrzej Pietrasiewicz } \ 90921a9476aSAndrzej Pietrasiewicz \ 91021a9476aSAndrzej Pietrasiewicz ret = kstrtou##prec(page, 0, &num); \ 91121a9476aSAndrzej Pietrasiewicz if (ret) \ 91221a9476aSAndrzej Pietrasiewicz goto end; \ 91321a9476aSAndrzej Pietrasiewicz \ 91421a9476aSAndrzej Pietrasiewicz if (num > limit) { \ 91521a9476aSAndrzej Pietrasiewicz ret = -EINVAL; \ 91621a9476aSAndrzej Pietrasiewicz goto end; \ 91721a9476aSAndrzej Pietrasiewicz } \ 91821a9476aSAndrzej Pietrasiewicz opts->name = num; \ 91921a9476aSAndrzej Pietrasiewicz ret = len; \ 92021a9476aSAndrzej Pietrasiewicz \ 92121a9476aSAndrzej Pietrasiewicz end: \ 92221a9476aSAndrzej Pietrasiewicz mutex_unlock(&opts->lock); \ 92321a9476aSAndrzej Pietrasiewicz return ret; \ 92421a9476aSAndrzej Pietrasiewicz } \ 92521a9476aSAndrzej Pietrasiewicz \ 926da4e527cSChristoph Hellwig CONFIGFS_ATTR(f_hid_opts_, name) 92721a9476aSAndrzej Pietrasiewicz 92821a9476aSAndrzej Pietrasiewicz F_HID_OPT(subclass, 8, 255); 92921a9476aSAndrzej Pietrasiewicz F_HID_OPT(protocol, 8, 255); 93039a2ac27SAndrzej Pietrasiewicz F_HID_OPT(report_length, 16, 65535); 93121a9476aSAndrzej Pietrasiewicz 932da4e527cSChristoph Hellwig static ssize_t f_hid_opts_report_desc_show(struct config_item *item, char *page) 93321a9476aSAndrzej Pietrasiewicz { 934da4e527cSChristoph Hellwig struct f_hid_opts *opts = to_f_hid_opts(item); 93521a9476aSAndrzej Pietrasiewicz int result; 93621a9476aSAndrzej Pietrasiewicz 93721a9476aSAndrzej Pietrasiewicz mutex_lock(&opts->lock); 93821a9476aSAndrzej Pietrasiewicz result = opts->report_desc_length; 93921a9476aSAndrzej Pietrasiewicz memcpy(page, opts->report_desc, opts->report_desc_length); 94021a9476aSAndrzej Pietrasiewicz mutex_unlock(&opts->lock); 94121a9476aSAndrzej Pietrasiewicz 94221a9476aSAndrzej Pietrasiewicz return result; 94321a9476aSAndrzej Pietrasiewicz } 94421a9476aSAndrzej Pietrasiewicz 945da4e527cSChristoph Hellwig static ssize_t f_hid_opts_report_desc_store(struct config_item *item, 94621a9476aSAndrzej Pietrasiewicz const char *page, size_t len) 94721a9476aSAndrzej Pietrasiewicz { 948da4e527cSChristoph Hellwig struct f_hid_opts *opts = to_f_hid_opts(item); 94921a9476aSAndrzej Pietrasiewicz int ret = -EBUSY; 95021a9476aSAndrzej Pietrasiewicz char *d; 95121a9476aSAndrzej Pietrasiewicz 95221a9476aSAndrzej Pietrasiewicz mutex_lock(&opts->lock); 95321a9476aSAndrzej Pietrasiewicz 95421a9476aSAndrzej Pietrasiewicz if (opts->refcnt) 95521a9476aSAndrzej Pietrasiewicz goto end; 95621a9476aSAndrzej Pietrasiewicz if (len > PAGE_SIZE) { 95721a9476aSAndrzej Pietrasiewicz ret = -ENOSPC; 95821a9476aSAndrzej Pietrasiewicz goto end; 95921a9476aSAndrzej Pietrasiewicz } 96021a9476aSAndrzej Pietrasiewicz d = kmemdup(page, len, GFP_KERNEL); 96121a9476aSAndrzej Pietrasiewicz if (!d) { 96221a9476aSAndrzej Pietrasiewicz ret = -ENOMEM; 96321a9476aSAndrzej Pietrasiewicz goto end; 96421a9476aSAndrzej Pietrasiewicz } 96521a9476aSAndrzej Pietrasiewicz kfree(opts->report_desc); 96621a9476aSAndrzej Pietrasiewicz opts->report_desc = d; 96721a9476aSAndrzej Pietrasiewicz opts->report_desc_length = len; 96821a9476aSAndrzej Pietrasiewicz opts->report_desc_alloc = true; 96921a9476aSAndrzej Pietrasiewicz ret = len; 97021a9476aSAndrzej Pietrasiewicz end: 97121a9476aSAndrzej Pietrasiewicz mutex_unlock(&opts->lock); 97221a9476aSAndrzej Pietrasiewicz return ret; 97321a9476aSAndrzej Pietrasiewicz } 97421a9476aSAndrzej Pietrasiewicz 975da4e527cSChristoph Hellwig CONFIGFS_ATTR(f_hid_opts_, report_desc); 97621a9476aSAndrzej Pietrasiewicz 977ed6fe1f5SJohannes Berg static ssize_t f_hid_opts_dev_show(struct config_item *item, char *page) 978ed6fe1f5SJohannes Berg { 979ed6fe1f5SJohannes Berg struct f_hid_opts *opts = to_f_hid_opts(item); 980ed6fe1f5SJohannes Berg 981ed6fe1f5SJohannes Berg return sprintf(page, "%d:%d\n", major, opts->minor); 982ed6fe1f5SJohannes Berg } 983ed6fe1f5SJohannes Berg 984ed6fe1f5SJohannes Berg CONFIGFS_ATTR_RO(f_hid_opts_, dev); 985ed6fe1f5SJohannes Berg 98621a9476aSAndrzej Pietrasiewicz static struct configfs_attribute *hid_attrs[] = { 987da4e527cSChristoph Hellwig &f_hid_opts_attr_subclass, 988da4e527cSChristoph Hellwig &f_hid_opts_attr_protocol, 989da4e527cSChristoph Hellwig &f_hid_opts_attr_report_length, 990da4e527cSChristoph Hellwig &f_hid_opts_attr_report_desc, 991ed6fe1f5SJohannes Berg &f_hid_opts_attr_dev, 99221a9476aSAndrzej Pietrasiewicz NULL, 99321a9476aSAndrzej Pietrasiewicz }; 99421a9476aSAndrzej Pietrasiewicz 99521a9476aSAndrzej Pietrasiewicz static struct config_item_type hid_func_type = { 99621a9476aSAndrzej Pietrasiewicz .ct_item_ops = &hidg_item_ops, 99721a9476aSAndrzej Pietrasiewicz .ct_attrs = hid_attrs, 99821a9476aSAndrzej Pietrasiewicz .ct_owner = THIS_MODULE, 99921a9476aSAndrzej Pietrasiewicz }; 100021a9476aSAndrzej Pietrasiewicz 1001cb382536SAndrzej Pietrasiewicz static inline void hidg_put_minor(int minor) 1002cb382536SAndrzej Pietrasiewicz { 1003cb382536SAndrzej Pietrasiewicz ida_simple_remove(&hidg_ida, minor); 1004cb382536SAndrzej Pietrasiewicz } 1005cb382536SAndrzej Pietrasiewicz 1006cb382536SAndrzej Pietrasiewicz static void hidg_free_inst(struct usb_function_instance *f) 1007cb382536SAndrzej Pietrasiewicz { 1008cb382536SAndrzej Pietrasiewicz struct f_hid_opts *opts; 1009cb382536SAndrzej Pietrasiewicz 1010cb382536SAndrzej Pietrasiewicz opts = container_of(f, struct f_hid_opts, func_inst); 1011cb382536SAndrzej Pietrasiewicz 1012cb382536SAndrzej Pietrasiewicz mutex_lock(&hidg_ida_lock); 1013cb382536SAndrzej Pietrasiewicz 1014cb382536SAndrzej Pietrasiewicz hidg_put_minor(opts->minor); 101599c49407SMatthew Wilcox if (ida_is_empty(&hidg_ida)) 1016cb382536SAndrzej Pietrasiewicz ghid_cleanup(); 1017cb382536SAndrzej Pietrasiewicz 1018cb382536SAndrzej Pietrasiewicz mutex_unlock(&hidg_ida_lock); 1019cb382536SAndrzej Pietrasiewicz 1020cb382536SAndrzej Pietrasiewicz if (opts->report_desc_alloc) 1021cb382536SAndrzej Pietrasiewicz kfree(opts->report_desc); 1022cb382536SAndrzej Pietrasiewicz 1023cb382536SAndrzej Pietrasiewicz kfree(opts); 1024cb382536SAndrzej Pietrasiewicz } 1025cb382536SAndrzej Pietrasiewicz 1026cb382536SAndrzej Pietrasiewicz static struct usb_function_instance *hidg_alloc_inst(void) 1027cb382536SAndrzej Pietrasiewicz { 1028cb382536SAndrzej Pietrasiewicz struct f_hid_opts *opts; 1029cb382536SAndrzej Pietrasiewicz struct usb_function_instance *ret; 1030cb382536SAndrzej Pietrasiewicz int status = 0; 1031cb382536SAndrzej Pietrasiewicz 1032cb382536SAndrzej Pietrasiewicz opts = kzalloc(sizeof(*opts), GFP_KERNEL); 1033cb382536SAndrzej Pietrasiewicz if (!opts) 1034cb382536SAndrzej Pietrasiewicz return ERR_PTR(-ENOMEM); 103521a9476aSAndrzej Pietrasiewicz mutex_init(&opts->lock); 1036cb382536SAndrzej Pietrasiewicz opts->func_inst.free_func_inst = hidg_free_inst; 1037cb382536SAndrzej Pietrasiewicz ret = &opts->func_inst; 1038cb382536SAndrzej Pietrasiewicz 1039cb382536SAndrzej Pietrasiewicz mutex_lock(&hidg_ida_lock); 1040cb382536SAndrzej Pietrasiewicz 104199c49407SMatthew Wilcox if (ida_is_empty(&hidg_ida)) { 1042cb382536SAndrzej Pietrasiewicz status = ghid_setup(NULL, HIDG_MINORS); 1043cb382536SAndrzej Pietrasiewicz if (status) { 1044cb382536SAndrzej Pietrasiewicz ret = ERR_PTR(status); 1045cb382536SAndrzej Pietrasiewicz kfree(opts); 1046cb382536SAndrzej Pietrasiewicz goto unlock; 1047cb382536SAndrzej Pietrasiewicz } 1048cb382536SAndrzej Pietrasiewicz } 1049cb382536SAndrzej Pietrasiewicz 1050cb382536SAndrzej Pietrasiewicz opts->minor = hidg_get_minor(); 1051cb382536SAndrzej Pietrasiewicz if (opts->minor < 0) { 1052cb382536SAndrzej Pietrasiewicz ret = ERR_PTR(opts->minor); 1053cb382536SAndrzej Pietrasiewicz kfree(opts); 105499c49407SMatthew Wilcox if (ida_is_empty(&hidg_ida)) 1055cb382536SAndrzej Pietrasiewicz ghid_cleanup(); 1056828f6148SDan Carpenter goto unlock; 1057cb382536SAndrzej Pietrasiewicz } 105821a9476aSAndrzej Pietrasiewicz config_group_init_type_name(&opts->func_inst.group, "", &hid_func_type); 1059cb382536SAndrzej Pietrasiewicz 1060cb382536SAndrzej Pietrasiewicz unlock: 1061cb382536SAndrzej Pietrasiewicz mutex_unlock(&hidg_ida_lock); 1062cb382536SAndrzej Pietrasiewicz return ret; 1063cb382536SAndrzej Pietrasiewicz } 1064cb382536SAndrzej Pietrasiewicz 1065cb382536SAndrzej Pietrasiewicz static void hidg_free(struct usb_function *f) 1066cb382536SAndrzej Pietrasiewicz { 1067cb382536SAndrzej Pietrasiewicz struct f_hidg *hidg; 106821a9476aSAndrzej Pietrasiewicz struct f_hid_opts *opts; 1069cb382536SAndrzej Pietrasiewicz 1070cb382536SAndrzej Pietrasiewicz hidg = func_to_hidg(f); 107121a9476aSAndrzej Pietrasiewicz opts = container_of(f->fi, struct f_hid_opts, func_inst); 1072cb382536SAndrzej Pietrasiewicz kfree(hidg->report_desc); 1073cb382536SAndrzej Pietrasiewicz kfree(hidg); 107421a9476aSAndrzej Pietrasiewicz mutex_lock(&opts->lock); 107521a9476aSAndrzej Pietrasiewicz --opts->refcnt; 107621a9476aSAndrzej Pietrasiewicz mutex_unlock(&opts->lock); 1077cb382536SAndrzej Pietrasiewicz } 1078cb382536SAndrzej Pietrasiewicz 107900a2430fSAndrzej Pietrasiewicz static void hidg_unbind(struct usb_configuration *c, struct usb_function *f) 108000a2430fSAndrzej Pietrasiewicz { 108100a2430fSAndrzej Pietrasiewicz struct f_hidg *hidg = func_to_hidg(f); 108200a2430fSAndrzej Pietrasiewicz 108300a2430fSAndrzej Pietrasiewicz device_destroy(hidg_class, MKDEV(major, hidg->minor)); 108400a2430fSAndrzej Pietrasiewicz cdev_del(&hidg->cdev); 108500a2430fSAndrzej Pietrasiewicz 108600a2430fSAndrzej Pietrasiewicz usb_free_all_descriptors(f); 108700a2430fSAndrzej Pietrasiewicz } 108800a2430fSAndrzej Pietrasiewicz 10890fc57ea0SFengguang Wu static struct usb_function *hidg_alloc(struct usb_function_instance *fi) 109000a2430fSAndrzej Pietrasiewicz { 109100a2430fSAndrzej Pietrasiewicz struct f_hidg *hidg; 1092cb382536SAndrzej Pietrasiewicz struct f_hid_opts *opts; 109300a2430fSAndrzej Pietrasiewicz 109400a2430fSAndrzej Pietrasiewicz /* allocate and initialize one new instance */ 1095cb382536SAndrzej Pietrasiewicz hidg = kzalloc(sizeof(*hidg), GFP_KERNEL); 109600a2430fSAndrzej Pietrasiewicz if (!hidg) 1097cb382536SAndrzej Pietrasiewicz return ERR_PTR(-ENOMEM); 109800a2430fSAndrzej Pietrasiewicz 1099cb382536SAndrzej Pietrasiewicz opts = container_of(fi, struct f_hid_opts, func_inst); 1100cb382536SAndrzej Pietrasiewicz 110121a9476aSAndrzej Pietrasiewicz mutex_lock(&opts->lock); 110221a9476aSAndrzej Pietrasiewicz ++opts->refcnt; 110321a9476aSAndrzej Pietrasiewicz 1104cb382536SAndrzej Pietrasiewicz hidg->minor = opts->minor; 1105cb382536SAndrzej Pietrasiewicz hidg->bInterfaceSubClass = opts->subclass; 1106cb382536SAndrzej Pietrasiewicz hidg->bInterfaceProtocol = opts->protocol; 1107cb382536SAndrzej Pietrasiewicz hidg->report_length = opts->report_length; 1108cb382536SAndrzej Pietrasiewicz hidg->report_desc_length = opts->report_desc_length; 1109cb382536SAndrzej Pietrasiewicz if (opts->report_desc) { 1110cb382536SAndrzej Pietrasiewicz hidg->report_desc = kmemdup(opts->report_desc, 1111cb382536SAndrzej Pietrasiewicz opts->report_desc_length, 111200a2430fSAndrzej Pietrasiewicz GFP_KERNEL); 111300a2430fSAndrzej Pietrasiewicz if (!hidg->report_desc) { 111400a2430fSAndrzej Pietrasiewicz kfree(hidg); 111521a9476aSAndrzej Pietrasiewicz mutex_unlock(&opts->lock); 1116cb382536SAndrzej Pietrasiewicz return ERR_PTR(-ENOMEM); 1117cb382536SAndrzej Pietrasiewicz } 111800a2430fSAndrzej Pietrasiewicz } 111900a2430fSAndrzej Pietrasiewicz 112021a9476aSAndrzej Pietrasiewicz mutex_unlock(&opts->lock); 112121a9476aSAndrzej Pietrasiewicz 112200a2430fSAndrzej Pietrasiewicz hidg->func.name = "hid"; 112300a2430fSAndrzej Pietrasiewicz hidg->func.bind = hidg_bind; 112400a2430fSAndrzej Pietrasiewicz hidg->func.unbind = hidg_unbind; 112500a2430fSAndrzej Pietrasiewicz hidg->func.set_alt = hidg_set_alt; 112600a2430fSAndrzej Pietrasiewicz hidg->func.disable = hidg_disable; 112700a2430fSAndrzej Pietrasiewicz hidg->func.setup = hidg_setup; 1128cb382536SAndrzej Pietrasiewicz hidg->func.free_func = hidg_free; 112900a2430fSAndrzej Pietrasiewicz 113000a2430fSAndrzej Pietrasiewicz /* this could me made configurable at some point */ 113100a2430fSAndrzej Pietrasiewicz hidg->qlen = 4; 113200a2430fSAndrzej Pietrasiewicz 1133cb382536SAndrzej Pietrasiewicz return &hidg->func; 113400a2430fSAndrzej Pietrasiewicz } 113500a2430fSAndrzej Pietrasiewicz 1136cb382536SAndrzej Pietrasiewicz DECLARE_USB_FUNCTION_INIT(hid, hidg_alloc_inst, hidg_alloc); 1137cb382536SAndrzej Pietrasiewicz MODULE_LICENSE("GPL"); 1138cb382536SAndrzej Pietrasiewicz MODULE_AUTHOR("Fabien Chouteau"); 1139cb382536SAndrzej Pietrasiewicz 1140cb382536SAndrzej Pietrasiewicz int ghid_setup(struct usb_gadget *g, int count) 114100a2430fSAndrzej Pietrasiewicz { 114200a2430fSAndrzej Pietrasiewicz int status; 114300a2430fSAndrzej Pietrasiewicz dev_t dev; 114400a2430fSAndrzej Pietrasiewicz 114500a2430fSAndrzej Pietrasiewicz hidg_class = class_create(THIS_MODULE, "hidg"); 114606529407SAndrzej Pietrasiewicz if (IS_ERR(hidg_class)) { 11470448d38cSDan Carpenter status = PTR_ERR(hidg_class); 114806529407SAndrzej Pietrasiewicz hidg_class = NULL; 11490448d38cSDan Carpenter return status; 115000a2430fSAndrzej Pietrasiewicz } 115100a2430fSAndrzej Pietrasiewicz 115200a2430fSAndrzej Pietrasiewicz status = alloc_chrdev_region(&dev, 0, count, "hidg"); 11530448d38cSDan Carpenter if (status) { 11540448d38cSDan Carpenter class_destroy(hidg_class); 11550448d38cSDan Carpenter hidg_class = NULL; 115600a2430fSAndrzej Pietrasiewicz return status; 115700a2430fSAndrzej Pietrasiewicz } 115800a2430fSAndrzej Pietrasiewicz 11590448d38cSDan Carpenter major = MAJOR(dev); 11600448d38cSDan Carpenter minors = count; 11610448d38cSDan Carpenter 11620448d38cSDan Carpenter return 0; 116300a2430fSAndrzej Pietrasiewicz } 116400a2430fSAndrzej Pietrasiewicz 116500a2430fSAndrzej Pietrasiewicz void ghid_cleanup(void) 116600a2430fSAndrzej Pietrasiewicz { 116700a2430fSAndrzej Pietrasiewicz if (major) { 116800a2430fSAndrzej Pietrasiewicz unregister_chrdev_region(MKDEV(major, 0), minors); 116900a2430fSAndrzej Pietrasiewicz major = minors = 0; 117000a2430fSAndrzej Pietrasiewicz } 117100a2430fSAndrzej Pietrasiewicz 117200a2430fSAndrzej Pietrasiewicz class_destroy(hidg_class); 117300a2430fSAndrzej Pietrasiewicz hidg_class = NULL; 117400a2430fSAndrzej Pietrasiewicz } 1175