Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 1 | /* |
| 2 | * f_hid.c -- USB HID function driver |
| 3 | * |
| 4 | * Copyright (C) 2010 Fabien Chouteau <fabien.chouteau@barco.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <linux/kernel.h> |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 13 | #include <linux/module.h> |
| 14 | #include <linux/hid.h> |
Andrzej Pietrasiewicz | cb38253 | 2014-11-06 11:11:59 +0100 | [diff] [blame] | 15 | #include <linux/idr.h> |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 16 | #include <linux/cdev.h> |
| 17 | #include <linux/mutex.h> |
| 18 | #include <linux/poll.h> |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 19 | #include <linux/uaccess.h> |
| 20 | #include <linux/wait.h> |
Sebastian Andrzej Siewior | 721e2e9 | 2012-09-06 20:11:27 +0200 | [diff] [blame] | 21 | #include <linux/sched.h> |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 22 | #include <linux/usb/g_hid.h> |
| 23 | |
Andrzej Pietrasiewicz | 1efd54e | 2013-11-07 08:41:26 +0100 | [diff] [blame] | 24 | #include "u_f.h" |
Andrzej Pietrasiewicz | cb38253 | 2014-11-06 11:11:59 +0100 | [diff] [blame] | 25 | #include "u_hid.h" |
| 26 | |
| 27 | #define HIDG_MINORS 4 |
Andrzej Pietrasiewicz | 1efd54e | 2013-11-07 08:41:26 +0100 | [diff] [blame] | 28 | |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 29 | static int major, minors; |
| 30 | static struct class *hidg_class; |
Andrzej Pietrasiewicz | cb38253 | 2014-11-06 11:11:59 +0100 | [diff] [blame] | 31 | static DEFINE_IDA(hidg_ida); |
| 32 | static DEFINE_MUTEX(hidg_ida_lock); /* protects access to hidg_ida */ |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 33 | |
| 34 | /*-------------------------------------------------------------------------*/ |
| 35 | /* HID gadget struct */ |
| 36 | |
Daniel Mack | 99c515005 | 2012-06-13 12:54:45 +0200 | [diff] [blame] | 37 | struct f_hidg_req_list { |
| 38 | struct usb_request *req; |
| 39 | unsigned int pos; |
| 40 | struct list_head list; |
| 41 | }; |
| 42 | |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 43 | struct f_hidg { |
| 44 | /* configuration */ |
| 45 | unsigned char bInterfaceSubClass; |
| 46 | unsigned char bInterfaceProtocol; |
| 47 | unsigned short report_desc_length; |
| 48 | char *report_desc; |
| 49 | unsigned short report_length; |
| 50 | |
| 51 | /* recv report */ |
Daniel Mack | 99c515005 | 2012-06-13 12:54:45 +0200 | [diff] [blame] | 52 | struct list_head completed_out_req; |
Krzysztof Opasiak | 33e4c1a | 2017-01-19 18:55:29 +0100 | [diff] [blame^] | 53 | spinlock_t read_spinlock; |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 54 | wait_queue_head_t read_queue; |
Daniel Mack | 99c515005 | 2012-06-13 12:54:45 +0200 | [diff] [blame] | 55 | unsigned int qlen; |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 56 | |
| 57 | /* send report */ |
Krzysztof Opasiak | 33e4c1a | 2017-01-19 18:55:29 +0100 | [diff] [blame^] | 58 | spinlock_t write_spinlock; |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 59 | bool write_pending; |
| 60 | wait_queue_head_t write_queue; |
| 61 | struct usb_request *req; |
| 62 | |
| 63 | int minor; |
| 64 | struct cdev cdev; |
| 65 | struct usb_function func; |
Daniel Mack | 99c515005 | 2012-06-13 12:54:45 +0200 | [diff] [blame] | 66 | |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 67 | struct usb_ep *in_ep; |
Daniel Mack | 99c515005 | 2012-06-13 12:54:45 +0200 | [diff] [blame] | 68 | struct usb_ep *out_ep; |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 69 | }; |
| 70 | |
| 71 | static inline struct f_hidg *func_to_hidg(struct usb_function *f) |
| 72 | { |
| 73 | return container_of(f, struct f_hidg, func); |
| 74 | } |
| 75 | |
| 76 | /*-------------------------------------------------------------------------*/ |
| 77 | /* Static descriptors */ |
| 78 | |
| 79 | static struct usb_interface_descriptor hidg_interface_desc = { |
| 80 | .bLength = sizeof hidg_interface_desc, |
| 81 | .bDescriptorType = USB_DT_INTERFACE, |
| 82 | /* .bInterfaceNumber = DYNAMIC */ |
| 83 | .bAlternateSetting = 0, |
Daniel Mack | 99c515005 | 2012-06-13 12:54:45 +0200 | [diff] [blame] | 84 | .bNumEndpoints = 2, |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 85 | .bInterfaceClass = USB_CLASS_HID, |
| 86 | /* .bInterfaceSubClass = DYNAMIC */ |
| 87 | /* .bInterfaceProtocol = DYNAMIC */ |
| 88 | /* .iInterface = DYNAMIC */ |
| 89 | }; |
| 90 | |
| 91 | static struct hid_descriptor hidg_desc = { |
| 92 | .bLength = sizeof hidg_desc, |
| 93 | .bDescriptorType = HID_DT_HID, |
| 94 | .bcdHID = 0x0101, |
| 95 | .bCountryCode = 0x00, |
| 96 | .bNumDescriptors = 0x1, |
| 97 | /*.desc[0].bDescriptorType = DYNAMIC */ |
| 98 | /*.desc[0].wDescriptorLenght = DYNAMIC */ |
| 99 | }; |
| 100 | |
Janusz Dziedzic | dbf499c | 2016-11-03 10:27:15 +0100 | [diff] [blame] | 101 | /* Super-Speed Support */ |
| 102 | |
| 103 | static struct usb_endpoint_descriptor hidg_ss_in_ep_desc = { |
| 104 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 105 | .bDescriptorType = USB_DT_ENDPOINT, |
| 106 | .bEndpointAddress = USB_DIR_IN, |
| 107 | .bmAttributes = USB_ENDPOINT_XFER_INT, |
| 108 | /*.wMaxPacketSize = DYNAMIC */ |
| 109 | .bInterval = 4, /* FIXME: Add this field in the |
| 110 | * HID gadget configuration? |
| 111 | * (struct hidg_func_descriptor) |
| 112 | */ |
| 113 | }; |
| 114 | |
| 115 | static struct usb_ss_ep_comp_descriptor hidg_ss_in_comp_desc = { |
| 116 | .bLength = sizeof(hidg_ss_in_comp_desc), |
| 117 | .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, |
| 118 | |
| 119 | /* .bMaxBurst = 0, */ |
| 120 | /* .bmAttributes = 0, */ |
| 121 | /* .wBytesPerInterval = DYNAMIC */ |
| 122 | }; |
| 123 | |
| 124 | static struct usb_endpoint_descriptor hidg_ss_out_ep_desc = { |
| 125 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 126 | .bDescriptorType = USB_DT_ENDPOINT, |
| 127 | .bEndpointAddress = USB_DIR_OUT, |
| 128 | .bmAttributes = USB_ENDPOINT_XFER_INT, |
| 129 | /*.wMaxPacketSize = DYNAMIC */ |
| 130 | .bInterval = 4, /* FIXME: Add this field in the |
| 131 | * HID gadget configuration? |
| 132 | * (struct hidg_func_descriptor) |
| 133 | */ |
| 134 | }; |
| 135 | |
| 136 | static struct usb_ss_ep_comp_descriptor hidg_ss_out_comp_desc = { |
| 137 | .bLength = sizeof(hidg_ss_out_comp_desc), |
| 138 | .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, |
| 139 | |
| 140 | /* .bMaxBurst = 0, */ |
| 141 | /* .bmAttributes = 0, */ |
| 142 | /* .wBytesPerInterval = DYNAMIC */ |
| 143 | }; |
| 144 | |
| 145 | static struct usb_descriptor_header *hidg_ss_descriptors[] = { |
| 146 | (struct usb_descriptor_header *)&hidg_interface_desc, |
| 147 | (struct usb_descriptor_header *)&hidg_desc, |
| 148 | (struct usb_descriptor_header *)&hidg_ss_in_ep_desc, |
| 149 | (struct usb_descriptor_header *)&hidg_ss_in_comp_desc, |
| 150 | (struct usb_descriptor_header *)&hidg_ss_out_ep_desc, |
| 151 | (struct usb_descriptor_header *)&hidg_ss_out_comp_desc, |
| 152 | NULL, |
| 153 | }; |
| 154 | |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 155 | /* High-Speed Support */ |
| 156 | |
| 157 | static struct usb_endpoint_descriptor hidg_hs_in_ep_desc = { |
| 158 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 159 | .bDescriptorType = USB_DT_ENDPOINT, |
| 160 | .bEndpointAddress = USB_DIR_IN, |
| 161 | .bmAttributes = USB_ENDPOINT_XFER_INT, |
| 162 | /*.wMaxPacketSize = DYNAMIC */ |
| 163 | .bInterval = 4, /* FIXME: Add this field in the |
| 164 | * HID gadget configuration? |
| 165 | * (struct hidg_func_descriptor) |
| 166 | */ |
| 167 | }; |
| 168 | |
Daniel Mack | 99c515005 | 2012-06-13 12:54:45 +0200 | [diff] [blame] | 169 | static struct usb_endpoint_descriptor hidg_hs_out_ep_desc = { |
| 170 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 171 | .bDescriptorType = USB_DT_ENDPOINT, |
| 172 | .bEndpointAddress = USB_DIR_OUT, |
| 173 | .bmAttributes = USB_ENDPOINT_XFER_INT, |
| 174 | /*.wMaxPacketSize = DYNAMIC */ |
| 175 | .bInterval = 4, /* FIXME: Add this field in the |
| 176 | * HID gadget configuration? |
| 177 | * (struct hidg_func_descriptor) |
| 178 | */ |
| 179 | }; |
| 180 | |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 181 | static struct usb_descriptor_header *hidg_hs_descriptors[] = { |
| 182 | (struct usb_descriptor_header *)&hidg_interface_desc, |
| 183 | (struct usb_descriptor_header *)&hidg_desc, |
| 184 | (struct usb_descriptor_header *)&hidg_hs_in_ep_desc, |
Daniel Mack | 99c515005 | 2012-06-13 12:54:45 +0200 | [diff] [blame] | 185 | (struct usb_descriptor_header *)&hidg_hs_out_ep_desc, |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 186 | NULL, |
| 187 | }; |
| 188 | |
| 189 | /* Full-Speed Support */ |
| 190 | |
| 191 | static struct usb_endpoint_descriptor hidg_fs_in_ep_desc = { |
| 192 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 193 | .bDescriptorType = USB_DT_ENDPOINT, |
| 194 | .bEndpointAddress = USB_DIR_IN, |
| 195 | .bmAttributes = USB_ENDPOINT_XFER_INT, |
| 196 | /*.wMaxPacketSize = DYNAMIC */ |
| 197 | .bInterval = 10, /* FIXME: Add this field in the |
| 198 | * HID gadget configuration? |
| 199 | * (struct hidg_func_descriptor) |
| 200 | */ |
| 201 | }; |
| 202 | |
Daniel Mack | 99c515005 | 2012-06-13 12:54:45 +0200 | [diff] [blame] | 203 | static struct usb_endpoint_descriptor hidg_fs_out_ep_desc = { |
| 204 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 205 | .bDescriptorType = USB_DT_ENDPOINT, |
| 206 | .bEndpointAddress = USB_DIR_OUT, |
| 207 | .bmAttributes = USB_ENDPOINT_XFER_INT, |
| 208 | /*.wMaxPacketSize = DYNAMIC */ |
| 209 | .bInterval = 10, /* FIXME: Add this field in the |
| 210 | * HID gadget configuration? |
| 211 | * (struct hidg_func_descriptor) |
| 212 | */ |
| 213 | }; |
| 214 | |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 215 | static struct usb_descriptor_header *hidg_fs_descriptors[] = { |
| 216 | (struct usb_descriptor_header *)&hidg_interface_desc, |
| 217 | (struct usb_descriptor_header *)&hidg_desc, |
| 218 | (struct usb_descriptor_header *)&hidg_fs_in_ep_desc, |
Daniel Mack | 99c515005 | 2012-06-13 12:54:45 +0200 | [diff] [blame] | 219 | (struct usb_descriptor_header *)&hidg_fs_out_ep_desc, |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 220 | NULL, |
| 221 | }; |
| 222 | |
| 223 | /*-------------------------------------------------------------------------*/ |
Andrzej Pietrasiewicz | cb38253 | 2014-11-06 11:11:59 +0100 | [diff] [blame] | 224 | /* Strings */ |
| 225 | |
| 226 | #define CT_FUNC_HID_IDX 0 |
| 227 | |
| 228 | static struct usb_string ct_func_string_defs[] = { |
| 229 | [CT_FUNC_HID_IDX].s = "HID Interface", |
| 230 | {}, /* end of list */ |
| 231 | }; |
| 232 | |
| 233 | static struct usb_gadget_strings ct_func_string_table = { |
| 234 | .language = 0x0409, /* en-US */ |
| 235 | .strings = ct_func_string_defs, |
| 236 | }; |
| 237 | |
| 238 | static struct usb_gadget_strings *ct_func_strings[] = { |
| 239 | &ct_func_string_table, |
| 240 | NULL, |
| 241 | }; |
| 242 | |
| 243 | /*-------------------------------------------------------------------------*/ |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 244 | /* Char Device */ |
| 245 | |
| 246 | static ssize_t f_hidg_read(struct file *file, char __user *buffer, |
| 247 | size_t count, loff_t *ptr) |
| 248 | { |
Daniel Mack | 99c515005 | 2012-06-13 12:54:45 +0200 | [diff] [blame] | 249 | struct f_hidg *hidg = file->private_data; |
| 250 | struct f_hidg_req_list *list; |
| 251 | struct usb_request *req; |
| 252 | unsigned long flags; |
| 253 | int ret; |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 254 | |
| 255 | if (!count) |
| 256 | return 0; |
| 257 | |
| 258 | if (!access_ok(VERIFY_WRITE, buffer, count)) |
| 259 | return -EFAULT; |
| 260 | |
Krzysztof Opasiak | 33e4c1a | 2017-01-19 18:55:29 +0100 | [diff] [blame^] | 261 | spin_lock_irqsave(&hidg->read_spinlock, flags); |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 262 | |
Daniel Mack | 99c515005 | 2012-06-13 12:54:45 +0200 | [diff] [blame] | 263 | #define READ_COND (!list_empty(&hidg->completed_out_req)) |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 264 | |
Daniel Mack | 99c515005 | 2012-06-13 12:54:45 +0200 | [diff] [blame] | 265 | /* wait for at least one buffer to complete */ |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 266 | while (!READ_COND) { |
Krzysztof Opasiak | 33e4c1a | 2017-01-19 18:55:29 +0100 | [diff] [blame^] | 267 | spin_unlock_irqrestore(&hidg->read_spinlock, flags); |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 268 | if (file->f_flags & O_NONBLOCK) |
| 269 | return -EAGAIN; |
| 270 | |
| 271 | if (wait_event_interruptible(hidg->read_queue, READ_COND)) |
| 272 | return -ERESTARTSYS; |
| 273 | |
Krzysztof Opasiak | 33e4c1a | 2017-01-19 18:55:29 +0100 | [diff] [blame^] | 274 | spin_lock_irqsave(&hidg->read_spinlock, flags); |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 275 | } |
| 276 | |
Daniel Mack | 99c515005 | 2012-06-13 12:54:45 +0200 | [diff] [blame] | 277 | /* pick the first one */ |
| 278 | list = list_first_entry(&hidg->completed_out_req, |
| 279 | struct f_hidg_req_list, list); |
Krzysztof Opasiak | aa65d11 | 2017-01-19 18:55:28 +0100 | [diff] [blame] | 280 | |
| 281 | /* |
| 282 | * Remove this from list to protect it from beign free() |
| 283 | * while host disables our function |
| 284 | */ |
| 285 | list_del(&list->list); |
| 286 | |
Daniel Mack | 99c515005 | 2012-06-13 12:54:45 +0200 | [diff] [blame] | 287 | req = list->req; |
| 288 | count = min_t(unsigned int, count, req->actual - list->pos); |
Krzysztof Opasiak | 33e4c1a | 2017-01-19 18:55:29 +0100 | [diff] [blame^] | 289 | spin_unlock_irqrestore(&hidg->read_spinlock, flags); |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 290 | |
Daniel Mack | 99c515005 | 2012-06-13 12:54:45 +0200 | [diff] [blame] | 291 | /* copy to user outside spinlock */ |
| 292 | count -= copy_to_user(buffer, req->buf + list->pos, count); |
| 293 | list->pos += count; |
| 294 | |
| 295 | /* |
| 296 | * if this request is completely handled and transfered to |
| 297 | * userspace, remove its entry from the list and requeue it |
| 298 | * again. Otherwise, we will revisit it again upon the next |
| 299 | * call, taking into account its current read position. |
| 300 | */ |
| 301 | if (list->pos == req->actual) { |
Daniel Mack | 99c515005 | 2012-06-13 12:54:45 +0200 | [diff] [blame] | 302 | kfree(list); |
Daniel Mack | 99c515005 | 2012-06-13 12:54:45 +0200 | [diff] [blame] | 303 | |
| 304 | req->length = hidg->report_length; |
| 305 | ret = usb_ep_queue(hidg->out_ep, req, GFP_KERNEL); |
Krzysztof Opasiak | aa65d11 | 2017-01-19 18:55:28 +0100 | [diff] [blame] | 306 | if (ret < 0) { |
| 307 | free_ep_req(hidg->out_ep, req); |
Daniel Mack | 99c515005 | 2012-06-13 12:54:45 +0200 | [diff] [blame] | 308 | return ret; |
Krzysztof Opasiak | aa65d11 | 2017-01-19 18:55:28 +0100 | [diff] [blame] | 309 | } |
| 310 | } else { |
Krzysztof Opasiak | 33e4c1a | 2017-01-19 18:55:29 +0100 | [diff] [blame^] | 311 | spin_lock_irqsave(&hidg->read_spinlock, flags); |
Krzysztof Opasiak | aa65d11 | 2017-01-19 18:55:28 +0100 | [diff] [blame] | 312 | list_add(&list->list, &hidg->completed_out_req); |
Krzysztof Opasiak | 33e4c1a | 2017-01-19 18:55:29 +0100 | [diff] [blame^] | 313 | spin_unlock_irqrestore(&hidg->read_spinlock, flags); |
Krzysztof Opasiak | aa65d11 | 2017-01-19 18:55:28 +0100 | [diff] [blame] | 314 | |
| 315 | wake_up(&hidg->read_queue); |
Daniel Mack | 99c515005 | 2012-06-13 12:54:45 +0200 | [diff] [blame] | 316 | } |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 317 | |
| 318 | return count; |
| 319 | } |
| 320 | |
| 321 | static void f_hidg_req_complete(struct usb_ep *ep, struct usb_request *req) |
| 322 | { |
| 323 | struct f_hidg *hidg = (struct f_hidg *)ep->driver_data; |
Krzysztof Opasiak | 33e4c1a | 2017-01-19 18:55:29 +0100 | [diff] [blame^] | 324 | unsigned long flags; |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 325 | |
| 326 | if (req->status != 0) { |
| 327 | ERROR(hidg->func.config->cdev, |
| 328 | "End Point Request ERROR: %d\n", req->status); |
| 329 | } |
| 330 | |
Krzysztof Opasiak | 33e4c1a | 2017-01-19 18:55:29 +0100 | [diff] [blame^] | 331 | spin_lock_irqsave(&hidg->write_spinlock, flags); |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 332 | hidg->write_pending = 0; |
Krzysztof Opasiak | 33e4c1a | 2017-01-19 18:55:29 +0100 | [diff] [blame^] | 333 | spin_unlock_irqrestore(&hidg->write_spinlock, flags); |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 334 | wake_up(&hidg->write_queue); |
| 335 | } |
| 336 | |
| 337 | static ssize_t f_hidg_write(struct file *file, const char __user *buffer, |
| 338 | size_t count, loff_t *offp) |
| 339 | { |
Joe Perches | fd63b10 | 2010-07-12 13:50:11 -0700 | [diff] [blame] | 340 | struct f_hidg *hidg = file->private_data; |
Krzysztof Opasiak | 33e4c1a | 2017-01-19 18:55:29 +0100 | [diff] [blame^] | 341 | unsigned long flags; |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 342 | ssize_t status = -ENOMEM; |
| 343 | |
| 344 | if (!access_ok(VERIFY_READ, buffer, count)) |
| 345 | return -EFAULT; |
| 346 | |
Krzysztof Opasiak | 33e4c1a | 2017-01-19 18:55:29 +0100 | [diff] [blame^] | 347 | spin_lock_irqsave(&hidg->write_spinlock, flags); |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 348 | |
| 349 | #define WRITE_COND (!hidg->write_pending) |
| 350 | |
| 351 | /* write queue */ |
| 352 | while (!WRITE_COND) { |
Krzysztof Opasiak | 33e4c1a | 2017-01-19 18:55:29 +0100 | [diff] [blame^] | 353 | spin_unlock_irqrestore(&hidg->write_spinlock, flags); |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 354 | if (file->f_flags & O_NONBLOCK) |
| 355 | return -EAGAIN; |
| 356 | |
| 357 | if (wait_event_interruptible_exclusive( |
| 358 | hidg->write_queue, WRITE_COND)) |
| 359 | return -ERESTARTSYS; |
| 360 | |
Krzysztof Opasiak | 33e4c1a | 2017-01-19 18:55:29 +0100 | [diff] [blame^] | 361 | spin_lock_irqsave(&hidg->write_spinlock, flags); |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 362 | } |
| 363 | |
Krzysztof Opasiak | 33e4c1a | 2017-01-19 18:55:29 +0100 | [diff] [blame^] | 364 | hidg->write_pending = 1; |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 365 | count = min_t(unsigned, count, hidg->report_length); |
Krzysztof Opasiak | 33e4c1a | 2017-01-19 18:55:29 +0100 | [diff] [blame^] | 366 | |
| 367 | spin_unlock_irqrestore(&hidg->write_spinlock, flags); |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 368 | status = copy_from_user(hidg->req->buf, buffer, count); |
| 369 | |
| 370 | if (status != 0) { |
| 371 | ERROR(hidg->func.config->cdev, |
| 372 | "copy_from_user error\n"); |
Krzysztof Opasiak | 33e4c1a | 2017-01-19 18:55:29 +0100 | [diff] [blame^] | 373 | status = -EINVAL; |
| 374 | goto release_write_pending; |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | hidg->req->status = 0; |
| 378 | hidg->req->zero = 0; |
| 379 | hidg->req->length = count; |
| 380 | hidg->req->complete = f_hidg_req_complete; |
| 381 | hidg->req->context = hidg; |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 382 | |
| 383 | status = usb_ep_queue(hidg->in_ep, hidg->req, GFP_ATOMIC); |
| 384 | if (status < 0) { |
| 385 | ERROR(hidg->func.config->cdev, |
| 386 | "usb_ep_queue error on int endpoint %zd\n", status); |
Krzysztof Opasiak | 33e4c1a | 2017-01-19 18:55:29 +0100 | [diff] [blame^] | 387 | goto release_write_pending; |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 388 | } else { |
| 389 | status = count; |
| 390 | } |
| 391 | |
Krzysztof Opasiak | 33e4c1a | 2017-01-19 18:55:29 +0100 | [diff] [blame^] | 392 | return status; |
| 393 | release_write_pending: |
| 394 | spin_lock_irqsave(&hidg->write_spinlock, flags); |
| 395 | hidg->write_pending = 0; |
| 396 | spin_unlock_irqrestore(&hidg->write_spinlock, flags); |
| 397 | |
| 398 | wake_up(&hidg->write_queue); |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 399 | |
| 400 | return status; |
| 401 | } |
| 402 | |
| 403 | static unsigned int f_hidg_poll(struct file *file, poll_table *wait) |
| 404 | { |
Joe Perches | fd63b10 | 2010-07-12 13:50:11 -0700 | [diff] [blame] | 405 | struct f_hidg *hidg = file->private_data; |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 406 | unsigned int ret = 0; |
| 407 | |
| 408 | poll_wait(file, &hidg->read_queue, wait); |
| 409 | poll_wait(file, &hidg->write_queue, wait); |
| 410 | |
| 411 | if (WRITE_COND) |
| 412 | ret |= POLLOUT | POLLWRNORM; |
| 413 | |
| 414 | if (READ_COND) |
| 415 | ret |= POLLIN | POLLRDNORM; |
| 416 | |
| 417 | return ret; |
| 418 | } |
| 419 | |
| 420 | #undef WRITE_COND |
| 421 | #undef READ_COND |
| 422 | |
| 423 | static int f_hidg_release(struct inode *inode, struct file *fd) |
| 424 | { |
| 425 | fd->private_data = NULL; |
| 426 | return 0; |
| 427 | } |
| 428 | |
| 429 | static int f_hidg_open(struct inode *inode, struct file *fd) |
| 430 | { |
| 431 | struct f_hidg *hidg = |
| 432 | container_of(inode->i_cdev, struct f_hidg, cdev); |
| 433 | |
| 434 | fd->private_data = hidg; |
| 435 | |
| 436 | return 0; |
| 437 | } |
| 438 | |
| 439 | /*-------------------------------------------------------------------------*/ |
| 440 | /* usb_function */ |
| 441 | |
Andrzej Pietrasiewicz | 1efd54e | 2013-11-07 08:41:26 +0100 | [diff] [blame] | 442 | static inline struct usb_request *hidg_alloc_ep_req(struct usb_ep *ep, |
| 443 | unsigned length) |
Daniel Mack | 99c515005 | 2012-06-13 12:54:45 +0200 | [diff] [blame] | 444 | { |
Felipe F. Tonello | aadbe81 | 2016-08-23 18:24:49 +0100 | [diff] [blame] | 445 | return alloc_ep_req(ep, length); |
Daniel Mack | 99c515005 | 2012-06-13 12:54:45 +0200 | [diff] [blame] | 446 | } |
| 447 | |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 448 | static void hidg_set_report_complete(struct usb_ep *ep, struct usb_request *req) |
| 449 | { |
Daniel Mack | 99c515005 | 2012-06-13 12:54:45 +0200 | [diff] [blame] | 450 | struct f_hidg *hidg = (struct f_hidg *) req->context; |
Krzysztof Opasiak | 20d2ca9 | 2017-01-19 18:55:27 +0100 | [diff] [blame] | 451 | struct usb_composite_dev *cdev = hidg->func.config->cdev; |
Daniel Mack | 99c515005 | 2012-06-13 12:54:45 +0200 | [diff] [blame] | 452 | struct f_hidg_req_list *req_list; |
| 453 | unsigned long flags; |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 454 | |
Krzysztof Opasiak | 20d2ca9 | 2017-01-19 18:55:27 +0100 | [diff] [blame] | 455 | switch (req->status) { |
| 456 | case 0: |
| 457 | req_list = kzalloc(sizeof(*req_list), GFP_ATOMIC); |
| 458 | if (!req_list) { |
| 459 | ERROR(cdev, "Unable to allocate mem for req_list\n"); |
| 460 | goto free_req; |
| 461 | } |
| 462 | |
| 463 | req_list->req = req; |
| 464 | |
Krzysztof Opasiak | 33e4c1a | 2017-01-19 18:55:29 +0100 | [diff] [blame^] | 465 | spin_lock_irqsave(&hidg->read_spinlock, flags); |
Krzysztof Opasiak | 20d2ca9 | 2017-01-19 18:55:27 +0100 | [diff] [blame] | 466 | list_add_tail(&req_list->list, &hidg->completed_out_req); |
Krzysztof Opasiak | 33e4c1a | 2017-01-19 18:55:29 +0100 | [diff] [blame^] | 467 | spin_unlock_irqrestore(&hidg->read_spinlock, flags); |
Krzysztof Opasiak | 20d2ca9 | 2017-01-19 18:55:27 +0100 | [diff] [blame] | 468 | |
| 469 | wake_up(&hidg->read_queue); |
| 470 | break; |
| 471 | default: |
| 472 | ERROR(cdev, "Set report failed %d\n", req->status); |
| 473 | /* FALLTHROUGH */ |
| 474 | case -ECONNABORTED: /* hardware forced ep reset */ |
| 475 | case -ECONNRESET: /* request dequeued */ |
| 476 | case -ESHUTDOWN: /* disconnect from host */ |
| 477 | free_req: |
| 478 | free_ep_req(ep, req); |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 479 | return; |
Krzysztof Opasiak | 20d2ca9 | 2017-01-19 18:55:27 +0100 | [diff] [blame] | 480 | } |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | static int hidg_setup(struct usb_function *f, |
| 484 | const struct usb_ctrlrequest *ctrl) |
| 485 | { |
| 486 | struct f_hidg *hidg = func_to_hidg(f); |
| 487 | struct usb_composite_dev *cdev = f->config->cdev; |
| 488 | struct usb_request *req = cdev->req; |
| 489 | int status = 0; |
| 490 | __u16 value, length; |
| 491 | |
| 492 | value = __le16_to_cpu(ctrl->wValue); |
| 493 | length = __le16_to_cpu(ctrl->wLength); |
| 494 | |
Julia Lawall | c9b3bde | 2014-12-07 20:21:00 +0100 | [diff] [blame] | 495 | VDBG(cdev, |
| 496 | "%s crtl_request : bRequestType:0x%x bRequest:0x%x Value:0x%x\n", |
| 497 | __func__, ctrl->bRequestType, ctrl->bRequest, value); |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 498 | |
| 499 | switch ((ctrl->bRequestType << 8) | ctrl->bRequest) { |
| 500 | case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8 |
| 501 | | HID_REQ_GET_REPORT): |
| 502 | VDBG(cdev, "get_report\n"); |
| 503 | |
| 504 | /* send an empty report */ |
| 505 | length = min_t(unsigned, length, hidg->report_length); |
| 506 | memset(req->buf, 0x0, length); |
| 507 | |
| 508 | goto respond; |
| 509 | break; |
| 510 | |
| 511 | case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8 |
| 512 | | HID_REQ_GET_PROTOCOL): |
| 513 | VDBG(cdev, "get_protocol\n"); |
| 514 | goto stall; |
| 515 | break; |
| 516 | |
| 517 | case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8 |
| 518 | | HID_REQ_SET_REPORT): |
Masanari Iida | 6774def | 2014-11-05 22:26:48 +0900 | [diff] [blame] | 519 | VDBG(cdev, "set_report | wLength=%d\n", ctrl->wLength); |
Daniel Mack | 99c515005 | 2012-06-13 12:54:45 +0200 | [diff] [blame] | 520 | goto stall; |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 521 | break; |
| 522 | |
| 523 | case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8 |
| 524 | | HID_REQ_SET_PROTOCOL): |
| 525 | VDBG(cdev, "set_protocol\n"); |
| 526 | goto stall; |
| 527 | break; |
| 528 | |
| 529 | case ((USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_INTERFACE) << 8 |
| 530 | | USB_REQ_GET_DESCRIPTOR): |
| 531 | switch (value >> 8) { |
Sebastian Bauer | c240d78 | 2011-07-21 15:40:07 +0200 | [diff] [blame] | 532 | case HID_DT_HID: |
Krzysztof Opasiak | f286d48 | 2015-03-27 09:35:44 +0100 | [diff] [blame] | 533 | { |
| 534 | struct hid_descriptor hidg_desc_copy = hidg_desc; |
| 535 | |
Sebastian Bauer | c240d78 | 2011-07-21 15:40:07 +0200 | [diff] [blame] | 536 | VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: HID\n"); |
Krzysztof Opasiak | f286d48 | 2015-03-27 09:35:44 +0100 | [diff] [blame] | 537 | hidg_desc_copy.desc[0].bDescriptorType = HID_DT_REPORT; |
| 538 | hidg_desc_copy.desc[0].wDescriptorLength = |
| 539 | cpu_to_le16(hidg->report_desc_length); |
| 540 | |
Sebastian Bauer | c240d78 | 2011-07-21 15:40:07 +0200 | [diff] [blame] | 541 | length = min_t(unsigned short, length, |
Krzysztof Opasiak | f286d48 | 2015-03-27 09:35:44 +0100 | [diff] [blame] | 542 | hidg_desc_copy.bLength); |
| 543 | memcpy(req->buf, &hidg_desc_copy, length); |
Sebastian Bauer | c240d78 | 2011-07-21 15:40:07 +0200 | [diff] [blame] | 544 | goto respond; |
| 545 | break; |
Krzysztof Opasiak | f286d48 | 2015-03-27 09:35:44 +0100 | [diff] [blame] | 546 | } |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 547 | case HID_DT_REPORT: |
| 548 | VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: REPORT\n"); |
| 549 | length = min_t(unsigned short, length, |
| 550 | hidg->report_desc_length); |
| 551 | memcpy(req->buf, hidg->report_desc, length); |
| 552 | goto respond; |
| 553 | break; |
| 554 | |
| 555 | default: |
Masanari Iida | 1c1301d | 2012-04-19 00:04:46 +0900 | [diff] [blame] | 556 | VDBG(cdev, "Unknown descriptor request 0x%x\n", |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 557 | value >> 8); |
| 558 | goto stall; |
| 559 | break; |
| 560 | } |
| 561 | break; |
| 562 | |
| 563 | default: |
| 564 | VDBG(cdev, "Unknown request 0x%x\n", |
| 565 | ctrl->bRequest); |
| 566 | goto stall; |
| 567 | break; |
| 568 | } |
| 569 | |
| 570 | stall: |
| 571 | return -EOPNOTSUPP; |
| 572 | |
| 573 | respond: |
| 574 | req->zero = 0; |
| 575 | req->length = length; |
| 576 | status = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC); |
| 577 | if (status < 0) |
| 578 | ERROR(cdev, "usb_ep_queue error on ep0 %d\n", value); |
| 579 | return status; |
| 580 | } |
| 581 | |
| 582 | static void hidg_disable(struct usb_function *f) |
| 583 | { |
| 584 | struct f_hidg *hidg = func_to_hidg(f); |
Daniel Mack | 99c515005 | 2012-06-13 12:54:45 +0200 | [diff] [blame] | 585 | struct f_hidg_req_list *list, *next; |
Krzysztof Opasiak | aa65d11 | 2017-01-19 18:55:28 +0100 | [diff] [blame] | 586 | unsigned long flags; |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 587 | |
| 588 | usb_ep_disable(hidg->in_ep); |
Daniel Mack | 99c515005 | 2012-06-13 12:54:45 +0200 | [diff] [blame] | 589 | usb_ep_disable(hidg->out_ep); |
Daniel Mack | 99c515005 | 2012-06-13 12:54:45 +0200 | [diff] [blame] | 590 | |
Krzysztof Opasiak | 33e4c1a | 2017-01-19 18:55:29 +0100 | [diff] [blame^] | 591 | spin_lock_irqsave(&hidg->read_spinlock, flags); |
Daniel Mack | 99c515005 | 2012-06-13 12:54:45 +0200 | [diff] [blame] | 592 | list_for_each_entry_safe(list, next, &hidg->completed_out_req, list) { |
Krzysztof Opasiak | aa65d11 | 2017-01-19 18:55:28 +0100 | [diff] [blame] | 593 | free_ep_req(hidg->out_ep, list->req); |
Daniel Mack | 99c515005 | 2012-06-13 12:54:45 +0200 | [diff] [blame] | 594 | list_del(&list->list); |
| 595 | kfree(list); |
| 596 | } |
Krzysztof Opasiak | 33e4c1a | 2017-01-19 18:55:29 +0100 | [diff] [blame^] | 597 | spin_unlock_irqrestore(&hidg->read_spinlock, flags); |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | static int hidg_set_alt(struct usb_function *f, unsigned intf, unsigned alt) |
| 601 | { |
| 602 | struct usb_composite_dev *cdev = f->config->cdev; |
| 603 | struct f_hidg *hidg = func_to_hidg(f); |
Daniel Mack | 99c515005 | 2012-06-13 12:54:45 +0200 | [diff] [blame] | 604 | int i, status = 0; |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 605 | |
| 606 | VDBG(cdev, "hidg_set_alt intf:%d alt:%d\n", intf, alt); |
| 607 | |
| 608 | if (hidg->in_ep != NULL) { |
| 609 | /* restart endpoint */ |
Robert Baldyga | 2516a68 | 2015-09-16 12:10:46 +0200 | [diff] [blame] | 610 | usb_ep_disable(hidg->in_ep); |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 611 | |
Tatyana Brokhman | ea2a1df | 2011-06-28 16:33:50 +0300 | [diff] [blame] | 612 | status = config_ep_by_speed(f->config->cdev->gadget, f, |
| 613 | hidg->in_ep); |
| 614 | if (status) { |
| 615 | ERROR(cdev, "config_ep_by_speed FAILED!\n"); |
| 616 | goto fail; |
| 617 | } |
Tatyana Brokhman | 72c973d | 2011-06-28 16:33:48 +0300 | [diff] [blame] | 618 | status = usb_ep_enable(hidg->in_ep); |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 619 | if (status < 0) { |
Daniel Mack | 99c515005 | 2012-06-13 12:54:45 +0200 | [diff] [blame] | 620 | ERROR(cdev, "Enable IN endpoint FAILED!\n"); |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 621 | goto fail; |
| 622 | } |
| 623 | hidg->in_ep->driver_data = hidg; |
| 624 | } |
Daniel Mack | 99c515005 | 2012-06-13 12:54:45 +0200 | [diff] [blame] | 625 | |
| 626 | |
| 627 | if (hidg->out_ep != NULL) { |
| 628 | /* restart endpoint */ |
Robert Baldyga | 2516a68 | 2015-09-16 12:10:46 +0200 | [diff] [blame] | 629 | usb_ep_disable(hidg->out_ep); |
Daniel Mack | 99c515005 | 2012-06-13 12:54:45 +0200 | [diff] [blame] | 630 | |
| 631 | status = config_ep_by_speed(f->config->cdev->gadget, f, |
| 632 | hidg->out_ep); |
| 633 | if (status) { |
| 634 | ERROR(cdev, "config_ep_by_speed FAILED!\n"); |
| 635 | goto fail; |
| 636 | } |
| 637 | status = usb_ep_enable(hidg->out_ep); |
| 638 | if (status < 0) { |
David Lechner | 43aef5c | 2017-01-02 17:28:39 -0600 | [diff] [blame] | 639 | ERROR(cdev, "Enable OUT endpoint FAILED!\n"); |
Daniel Mack | 99c515005 | 2012-06-13 12:54:45 +0200 | [diff] [blame] | 640 | goto fail; |
| 641 | } |
| 642 | hidg->out_ep->driver_data = hidg; |
| 643 | |
| 644 | /* |
| 645 | * allocate a bunch of read buffers and queue them all at once. |
| 646 | */ |
| 647 | for (i = 0; i < hidg->qlen && status == 0; i++) { |
| 648 | struct usb_request *req = |
| 649 | hidg_alloc_ep_req(hidg->out_ep, |
| 650 | hidg->report_length); |
| 651 | if (req) { |
| 652 | req->complete = hidg_set_report_complete; |
| 653 | req->context = hidg; |
| 654 | status = usb_ep_queue(hidg->out_ep, req, |
| 655 | GFP_ATOMIC); |
| 656 | if (status) |
| 657 | ERROR(cdev, "%s queue req --> %d\n", |
| 658 | hidg->out_ep->name, status); |
| 659 | } else { |
| 660 | usb_ep_disable(hidg->out_ep); |
Daniel Mack | 99c515005 | 2012-06-13 12:54:45 +0200 | [diff] [blame] | 661 | status = -ENOMEM; |
| 662 | goto fail; |
| 663 | } |
| 664 | } |
| 665 | } |
| 666 | |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 667 | fail: |
| 668 | return status; |
| 669 | } |
| 670 | |
Lad, Prabhakar | 7a3cc46 | 2015-02-04 17:49:36 +0000 | [diff] [blame] | 671 | static const struct file_operations f_hidg_fops = { |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 672 | .owner = THIS_MODULE, |
| 673 | .open = f_hidg_open, |
| 674 | .release = f_hidg_release, |
| 675 | .write = f_hidg_write, |
| 676 | .read = f_hidg_read, |
| 677 | .poll = f_hidg_poll, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 678 | .llseek = noop_llseek, |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 679 | }; |
| 680 | |
Andrzej Pietrasiewicz | cb38253 | 2014-11-06 11:11:59 +0100 | [diff] [blame] | 681 | static int hidg_bind(struct usb_configuration *c, struct usb_function *f) |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 682 | { |
| 683 | struct usb_ep *ep; |
| 684 | struct f_hidg *hidg = func_to_hidg(f); |
Andrzej Pietrasiewicz | 5ca8d3ec | 2014-11-06 11:12:02 +0100 | [diff] [blame] | 685 | struct usb_string *us; |
Andrzej Pietrasiewicz | 6340608 | 2014-11-06 11:11:57 +0100 | [diff] [blame] | 686 | struct device *device; |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 687 | int status; |
| 688 | dev_t dev; |
| 689 | |
Andrzej Pietrasiewicz | cb38253 | 2014-11-06 11:11:59 +0100 | [diff] [blame] | 690 | /* maybe allocate device-global string IDs, and patch descriptors */ |
Andrzej Pietrasiewicz | 5ca8d3ec | 2014-11-06 11:12:02 +0100 | [diff] [blame] | 691 | us = usb_gstrings_attach(c->cdev, ct_func_strings, |
| 692 | ARRAY_SIZE(ct_func_string_defs)); |
| 693 | if (IS_ERR(us)) |
| 694 | return PTR_ERR(us); |
| 695 | hidg_interface_desc.iInterface = us[CT_FUNC_HID_IDX].id; |
Andrzej Pietrasiewicz | cb38253 | 2014-11-06 11:11:59 +0100 | [diff] [blame] | 696 | |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 697 | /* allocate instance-specific interface IDs, and patch descriptors */ |
| 698 | status = usb_interface_id(c, f); |
| 699 | if (status < 0) |
| 700 | goto fail; |
| 701 | hidg_interface_desc.bInterfaceNumber = status; |
| 702 | |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 703 | /* allocate instance-specific endpoints */ |
| 704 | status = -ENODEV; |
| 705 | ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_in_ep_desc); |
| 706 | if (!ep) |
| 707 | goto fail; |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 708 | hidg->in_ep = ep; |
| 709 | |
Daniel Mack | 99c515005 | 2012-06-13 12:54:45 +0200 | [diff] [blame] | 710 | ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_out_ep_desc); |
| 711 | if (!ep) |
| 712 | goto fail; |
Daniel Mack | 99c515005 | 2012-06-13 12:54:45 +0200 | [diff] [blame] | 713 | hidg->out_ep = ep; |
| 714 | |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 715 | /* preallocate request and buffer */ |
| 716 | status = -ENOMEM; |
Felipe F. Tonello | ba1582f | 2016-08-23 18:24:51 +0100 | [diff] [blame] | 717 | hidg->req = alloc_ep_req(hidg->in_ep, hidg->report_length); |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 718 | if (!hidg->req) |
| 719 | goto fail; |
| 720 | |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 721 | /* set descriptor dynamic values */ |
| 722 | hidg_interface_desc.bInterfaceSubClass = hidg->bInterfaceSubClass; |
| 723 | hidg_interface_desc.bInterfaceProtocol = hidg->bInterfaceProtocol; |
Janusz Dziedzic | dbf499c | 2016-11-03 10:27:15 +0100 | [diff] [blame] | 724 | hidg_ss_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length); |
| 725 | hidg_ss_in_comp_desc.wBytesPerInterval = |
| 726 | cpu_to_le16(hidg->report_length); |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 727 | hidg_hs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length); |
| 728 | hidg_fs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length); |
Janusz Dziedzic | dbf499c | 2016-11-03 10:27:15 +0100 | [diff] [blame] | 729 | hidg_ss_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length); |
| 730 | hidg_ss_out_comp_desc.wBytesPerInterval = |
| 731 | cpu_to_le16(hidg->report_length); |
Daniel Mack | 99c515005 | 2012-06-13 12:54:45 +0200 | [diff] [blame] | 732 | hidg_hs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length); |
| 733 | hidg_fs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length); |
Krzysztof Opasiak | f286d48 | 2015-03-27 09:35:44 +0100 | [diff] [blame] | 734 | /* |
| 735 | * We can use hidg_desc struct here but we should not relay |
| 736 | * that its content won't change after returning from this function. |
| 737 | */ |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 738 | hidg_desc.desc[0].bDescriptorType = HID_DT_REPORT; |
| 739 | hidg_desc.desc[0].wDescriptorLength = |
| 740 | cpu_to_le16(hidg->report_desc_length); |
| 741 | |
Sebastian Andrzej Siewior | 10287ba | 2012-10-22 22:15:06 +0200 | [diff] [blame] | 742 | hidg_hs_in_ep_desc.bEndpointAddress = |
| 743 | hidg_fs_in_ep_desc.bEndpointAddress; |
| 744 | hidg_hs_out_ep_desc.bEndpointAddress = |
| 745 | hidg_fs_out_ep_desc.bEndpointAddress; |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 746 | |
Janusz Dziedzic | dbf499c | 2016-11-03 10:27:15 +0100 | [diff] [blame] | 747 | hidg_ss_in_ep_desc.bEndpointAddress = |
| 748 | hidg_fs_in_ep_desc.bEndpointAddress; |
| 749 | hidg_ss_out_ep_desc.bEndpointAddress = |
| 750 | hidg_fs_out_ep_desc.bEndpointAddress; |
| 751 | |
Sebastian Andrzej Siewior | 10287ba | 2012-10-22 22:15:06 +0200 | [diff] [blame] | 752 | status = usb_assign_descriptors(f, hidg_fs_descriptors, |
Janusz Dziedzic | dbf499c | 2016-11-03 10:27:15 +0100 | [diff] [blame] | 753 | hidg_hs_descriptors, hidg_ss_descriptors, NULL); |
Sebastian Andrzej Siewior | 10287ba | 2012-10-22 22:15:06 +0200 | [diff] [blame] | 754 | if (status) |
| 755 | goto fail; |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 756 | |
Krzysztof Opasiak | 33e4c1a | 2017-01-19 18:55:29 +0100 | [diff] [blame^] | 757 | spin_lock_init(&hidg->write_spinlock); |
| 758 | spin_lock_init(&hidg->read_spinlock); |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 759 | init_waitqueue_head(&hidg->write_queue); |
| 760 | init_waitqueue_head(&hidg->read_queue); |
Daniel Mack | 99c515005 | 2012-06-13 12:54:45 +0200 | [diff] [blame] | 761 | INIT_LIST_HEAD(&hidg->completed_out_req); |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 762 | |
| 763 | /* create char device */ |
| 764 | cdev_init(&hidg->cdev, &f_hidg_fops); |
| 765 | dev = MKDEV(major, hidg->minor); |
| 766 | status = cdev_add(&hidg->cdev, dev, 1); |
| 767 | if (status) |
Pavitrakumar Managutte | d12a872 | 2014-10-22 19:24:58 +0530 | [diff] [blame] | 768 | goto fail_free_descs; |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 769 | |
Andrzej Pietrasiewicz | 6340608 | 2014-11-06 11:11:57 +0100 | [diff] [blame] | 770 | device = device_create(hidg_class, NULL, dev, NULL, |
| 771 | "%s%d", "hidg", hidg->minor); |
| 772 | if (IS_ERR(device)) { |
| 773 | status = PTR_ERR(device); |
| 774 | goto del; |
| 775 | } |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 776 | |
| 777 | return 0; |
Andrzej Pietrasiewicz | 6340608 | 2014-11-06 11:11:57 +0100 | [diff] [blame] | 778 | del: |
| 779 | cdev_del(&hidg->cdev); |
Pavitrakumar Managutte | d12a872 | 2014-10-22 19:24:58 +0530 | [diff] [blame] | 780 | fail_free_descs: |
| 781 | usb_free_all_descriptors(f); |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 782 | fail: |
| 783 | ERROR(f->config->cdev, "hidg_bind FAILED\n"); |
Felipe F. Tonello | 14794d7 | 2016-08-23 18:24:50 +0100 | [diff] [blame] | 784 | if (hidg->req != NULL) |
| 785 | free_ep_req(hidg->in_ep, hidg->req); |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 786 | |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 787 | return status; |
| 788 | } |
| 789 | |
Andrzej Pietrasiewicz | cb38253 | 2014-11-06 11:11:59 +0100 | [diff] [blame] | 790 | static inline int hidg_get_minor(void) |
| 791 | { |
| 792 | int ret; |
| 793 | |
| 794 | ret = ida_simple_get(&hidg_ida, 0, 0, GFP_KERNEL); |
Andrzej Pietrasiewicz | 774cf72 | 2015-07-24 09:48:40 +0200 | [diff] [blame] | 795 | if (ret >= HIDG_MINORS) { |
| 796 | ida_simple_remove(&hidg_ida, ret); |
| 797 | ret = -ENODEV; |
| 798 | } |
Andrzej Pietrasiewicz | cb38253 | 2014-11-06 11:11:59 +0100 | [diff] [blame] | 799 | |
| 800 | return ret; |
| 801 | } |
| 802 | |
Andrzej Pietrasiewicz | 21a9476 | 2014-11-06 11:12:03 +0100 | [diff] [blame] | 803 | static inline struct f_hid_opts *to_f_hid_opts(struct config_item *item) |
| 804 | { |
| 805 | return container_of(to_config_group(item), struct f_hid_opts, |
| 806 | func_inst.group); |
| 807 | } |
| 808 | |
Andrzej Pietrasiewicz | 21a9476 | 2014-11-06 11:12:03 +0100 | [diff] [blame] | 809 | static void hid_attr_release(struct config_item *item) |
| 810 | { |
| 811 | struct f_hid_opts *opts = to_f_hid_opts(item); |
| 812 | |
| 813 | usb_put_function_instance(&opts->func_inst); |
| 814 | } |
| 815 | |
| 816 | static struct configfs_item_operations hidg_item_ops = { |
| 817 | .release = hid_attr_release, |
Andrzej Pietrasiewicz | 21a9476 | 2014-11-06 11:12:03 +0100 | [diff] [blame] | 818 | }; |
| 819 | |
| 820 | #define F_HID_OPT(name, prec, limit) \ |
Christoph Hellwig | da4e527 | 2015-10-03 15:32:40 +0200 | [diff] [blame] | 821 | static ssize_t f_hid_opts_##name##_show(struct config_item *item, char *page)\ |
Andrzej Pietrasiewicz | 21a9476 | 2014-11-06 11:12:03 +0100 | [diff] [blame] | 822 | { \ |
Christoph Hellwig | da4e527 | 2015-10-03 15:32:40 +0200 | [diff] [blame] | 823 | struct f_hid_opts *opts = to_f_hid_opts(item); \ |
Andrzej Pietrasiewicz | 21a9476 | 2014-11-06 11:12:03 +0100 | [diff] [blame] | 824 | int result; \ |
| 825 | \ |
| 826 | mutex_lock(&opts->lock); \ |
| 827 | result = sprintf(page, "%d\n", opts->name); \ |
| 828 | mutex_unlock(&opts->lock); \ |
| 829 | \ |
| 830 | return result; \ |
| 831 | } \ |
| 832 | \ |
Christoph Hellwig | da4e527 | 2015-10-03 15:32:40 +0200 | [diff] [blame] | 833 | static ssize_t f_hid_opts_##name##_store(struct config_item *item, \ |
Andrzej Pietrasiewicz | 21a9476 | 2014-11-06 11:12:03 +0100 | [diff] [blame] | 834 | const char *page, size_t len) \ |
| 835 | { \ |
Christoph Hellwig | da4e527 | 2015-10-03 15:32:40 +0200 | [diff] [blame] | 836 | struct f_hid_opts *opts = to_f_hid_opts(item); \ |
Andrzej Pietrasiewicz | 21a9476 | 2014-11-06 11:12:03 +0100 | [diff] [blame] | 837 | int ret; \ |
| 838 | u##prec num; \ |
| 839 | \ |
| 840 | mutex_lock(&opts->lock); \ |
| 841 | if (opts->refcnt) { \ |
| 842 | ret = -EBUSY; \ |
| 843 | goto end; \ |
| 844 | } \ |
| 845 | \ |
| 846 | ret = kstrtou##prec(page, 0, &num); \ |
| 847 | if (ret) \ |
| 848 | goto end; \ |
| 849 | \ |
| 850 | if (num > limit) { \ |
| 851 | ret = -EINVAL; \ |
| 852 | goto end; \ |
| 853 | } \ |
| 854 | opts->name = num; \ |
| 855 | ret = len; \ |
| 856 | \ |
| 857 | end: \ |
| 858 | mutex_unlock(&opts->lock); \ |
| 859 | return ret; \ |
| 860 | } \ |
| 861 | \ |
Christoph Hellwig | da4e527 | 2015-10-03 15:32:40 +0200 | [diff] [blame] | 862 | CONFIGFS_ATTR(f_hid_opts_, name) |
Andrzej Pietrasiewicz | 21a9476 | 2014-11-06 11:12:03 +0100 | [diff] [blame] | 863 | |
| 864 | F_HID_OPT(subclass, 8, 255); |
| 865 | F_HID_OPT(protocol, 8, 255); |
Andrzej Pietrasiewicz | 39a2ac27 | 2014-12-15 13:50:05 +0100 | [diff] [blame] | 866 | F_HID_OPT(report_length, 16, 65535); |
Andrzej Pietrasiewicz | 21a9476 | 2014-11-06 11:12:03 +0100 | [diff] [blame] | 867 | |
Christoph Hellwig | da4e527 | 2015-10-03 15:32:40 +0200 | [diff] [blame] | 868 | static ssize_t f_hid_opts_report_desc_show(struct config_item *item, char *page) |
Andrzej Pietrasiewicz | 21a9476 | 2014-11-06 11:12:03 +0100 | [diff] [blame] | 869 | { |
Christoph Hellwig | da4e527 | 2015-10-03 15:32:40 +0200 | [diff] [blame] | 870 | struct f_hid_opts *opts = to_f_hid_opts(item); |
Andrzej Pietrasiewicz | 21a9476 | 2014-11-06 11:12:03 +0100 | [diff] [blame] | 871 | int result; |
| 872 | |
| 873 | mutex_lock(&opts->lock); |
| 874 | result = opts->report_desc_length; |
| 875 | memcpy(page, opts->report_desc, opts->report_desc_length); |
| 876 | mutex_unlock(&opts->lock); |
| 877 | |
| 878 | return result; |
| 879 | } |
| 880 | |
Christoph Hellwig | da4e527 | 2015-10-03 15:32:40 +0200 | [diff] [blame] | 881 | static ssize_t f_hid_opts_report_desc_store(struct config_item *item, |
Andrzej Pietrasiewicz | 21a9476 | 2014-11-06 11:12:03 +0100 | [diff] [blame] | 882 | const char *page, size_t len) |
| 883 | { |
Christoph Hellwig | da4e527 | 2015-10-03 15:32:40 +0200 | [diff] [blame] | 884 | struct f_hid_opts *opts = to_f_hid_opts(item); |
Andrzej Pietrasiewicz | 21a9476 | 2014-11-06 11:12:03 +0100 | [diff] [blame] | 885 | int ret = -EBUSY; |
| 886 | char *d; |
| 887 | |
| 888 | mutex_lock(&opts->lock); |
| 889 | |
| 890 | if (opts->refcnt) |
| 891 | goto end; |
| 892 | if (len > PAGE_SIZE) { |
| 893 | ret = -ENOSPC; |
| 894 | goto end; |
| 895 | } |
| 896 | d = kmemdup(page, len, GFP_KERNEL); |
| 897 | if (!d) { |
| 898 | ret = -ENOMEM; |
| 899 | goto end; |
| 900 | } |
| 901 | kfree(opts->report_desc); |
| 902 | opts->report_desc = d; |
| 903 | opts->report_desc_length = len; |
| 904 | opts->report_desc_alloc = true; |
| 905 | ret = len; |
| 906 | end: |
| 907 | mutex_unlock(&opts->lock); |
| 908 | return ret; |
| 909 | } |
| 910 | |
Christoph Hellwig | da4e527 | 2015-10-03 15:32:40 +0200 | [diff] [blame] | 911 | CONFIGFS_ATTR(f_hid_opts_, report_desc); |
Andrzej Pietrasiewicz | 21a9476 | 2014-11-06 11:12:03 +0100 | [diff] [blame] | 912 | |
Johannes Berg | ed6fe1f | 2016-06-23 22:28:54 +0200 | [diff] [blame] | 913 | static ssize_t f_hid_opts_dev_show(struct config_item *item, char *page) |
| 914 | { |
| 915 | struct f_hid_opts *opts = to_f_hid_opts(item); |
| 916 | |
| 917 | return sprintf(page, "%d:%d\n", major, opts->minor); |
| 918 | } |
| 919 | |
| 920 | CONFIGFS_ATTR_RO(f_hid_opts_, dev); |
| 921 | |
Andrzej Pietrasiewicz | 21a9476 | 2014-11-06 11:12:03 +0100 | [diff] [blame] | 922 | static struct configfs_attribute *hid_attrs[] = { |
Christoph Hellwig | da4e527 | 2015-10-03 15:32:40 +0200 | [diff] [blame] | 923 | &f_hid_opts_attr_subclass, |
| 924 | &f_hid_opts_attr_protocol, |
| 925 | &f_hid_opts_attr_report_length, |
| 926 | &f_hid_opts_attr_report_desc, |
Johannes Berg | ed6fe1f | 2016-06-23 22:28:54 +0200 | [diff] [blame] | 927 | &f_hid_opts_attr_dev, |
Andrzej Pietrasiewicz | 21a9476 | 2014-11-06 11:12:03 +0100 | [diff] [blame] | 928 | NULL, |
| 929 | }; |
| 930 | |
| 931 | static struct config_item_type hid_func_type = { |
| 932 | .ct_item_ops = &hidg_item_ops, |
| 933 | .ct_attrs = hid_attrs, |
| 934 | .ct_owner = THIS_MODULE, |
| 935 | }; |
| 936 | |
Andrzej Pietrasiewicz | cb38253 | 2014-11-06 11:11:59 +0100 | [diff] [blame] | 937 | static inline void hidg_put_minor(int minor) |
| 938 | { |
| 939 | ida_simple_remove(&hidg_ida, minor); |
| 940 | } |
| 941 | |
| 942 | static void hidg_free_inst(struct usb_function_instance *f) |
| 943 | { |
| 944 | struct f_hid_opts *opts; |
| 945 | |
| 946 | opts = container_of(f, struct f_hid_opts, func_inst); |
| 947 | |
| 948 | mutex_lock(&hidg_ida_lock); |
| 949 | |
| 950 | hidg_put_minor(opts->minor); |
Matthew Wilcox | 99c4940 | 2016-12-14 15:09:13 -0800 | [diff] [blame] | 951 | if (ida_is_empty(&hidg_ida)) |
Andrzej Pietrasiewicz | cb38253 | 2014-11-06 11:11:59 +0100 | [diff] [blame] | 952 | ghid_cleanup(); |
| 953 | |
| 954 | mutex_unlock(&hidg_ida_lock); |
| 955 | |
| 956 | if (opts->report_desc_alloc) |
| 957 | kfree(opts->report_desc); |
| 958 | |
| 959 | kfree(opts); |
| 960 | } |
| 961 | |
| 962 | static struct usb_function_instance *hidg_alloc_inst(void) |
| 963 | { |
| 964 | struct f_hid_opts *opts; |
| 965 | struct usb_function_instance *ret; |
| 966 | int status = 0; |
| 967 | |
| 968 | opts = kzalloc(sizeof(*opts), GFP_KERNEL); |
| 969 | if (!opts) |
| 970 | return ERR_PTR(-ENOMEM); |
Andrzej Pietrasiewicz | 21a9476 | 2014-11-06 11:12:03 +0100 | [diff] [blame] | 971 | mutex_init(&opts->lock); |
Andrzej Pietrasiewicz | cb38253 | 2014-11-06 11:11:59 +0100 | [diff] [blame] | 972 | opts->func_inst.free_func_inst = hidg_free_inst; |
| 973 | ret = &opts->func_inst; |
| 974 | |
| 975 | mutex_lock(&hidg_ida_lock); |
| 976 | |
Matthew Wilcox | 99c4940 | 2016-12-14 15:09:13 -0800 | [diff] [blame] | 977 | if (ida_is_empty(&hidg_ida)) { |
Andrzej Pietrasiewicz | cb38253 | 2014-11-06 11:11:59 +0100 | [diff] [blame] | 978 | status = ghid_setup(NULL, HIDG_MINORS); |
| 979 | if (status) { |
| 980 | ret = ERR_PTR(status); |
| 981 | kfree(opts); |
| 982 | goto unlock; |
| 983 | } |
| 984 | } |
| 985 | |
| 986 | opts->minor = hidg_get_minor(); |
| 987 | if (opts->minor < 0) { |
| 988 | ret = ERR_PTR(opts->minor); |
| 989 | kfree(opts); |
Matthew Wilcox | 99c4940 | 2016-12-14 15:09:13 -0800 | [diff] [blame] | 990 | if (ida_is_empty(&hidg_ida)) |
Andrzej Pietrasiewicz | cb38253 | 2014-11-06 11:11:59 +0100 | [diff] [blame] | 991 | ghid_cleanup(); |
Dan Carpenter | 828f614 | 2014-11-13 09:19:47 +0300 | [diff] [blame] | 992 | goto unlock; |
Andrzej Pietrasiewicz | cb38253 | 2014-11-06 11:11:59 +0100 | [diff] [blame] | 993 | } |
Andrzej Pietrasiewicz | 21a9476 | 2014-11-06 11:12:03 +0100 | [diff] [blame] | 994 | config_group_init_type_name(&opts->func_inst.group, "", &hid_func_type); |
Andrzej Pietrasiewicz | cb38253 | 2014-11-06 11:11:59 +0100 | [diff] [blame] | 995 | |
| 996 | unlock: |
| 997 | mutex_unlock(&hidg_ida_lock); |
| 998 | return ret; |
| 999 | } |
| 1000 | |
| 1001 | static void hidg_free(struct usb_function *f) |
| 1002 | { |
| 1003 | struct f_hidg *hidg; |
Andrzej Pietrasiewicz | 21a9476 | 2014-11-06 11:12:03 +0100 | [diff] [blame] | 1004 | struct f_hid_opts *opts; |
Andrzej Pietrasiewicz | cb38253 | 2014-11-06 11:11:59 +0100 | [diff] [blame] | 1005 | |
| 1006 | hidg = func_to_hidg(f); |
Andrzej Pietrasiewicz | 21a9476 | 2014-11-06 11:12:03 +0100 | [diff] [blame] | 1007 | opts = container_of(f->fi, struct f_hid_opts, func_inst); |
Andrzej Pietrasiewicz | cb38253 | 2014-11-06 11:11:59 +0100 | [diff] [blame] | 1008 | kfree(hidg->report_desc); |
| 1009 | kfree(hidg); |
Andrzej Pietrasiewicz | 21a9476 | 2014-11-06 11:12:03 +0100 | [diff] [blame] | 1010 | mutex_lock(&opts->lock); |
| 1011 | --opts->refcnt; |
| 1012 | mutex_unlock(&opts->lock); |
Andrzej Pietrasiewicz | cb38253 | 2014-11-06 11:11:59 +0100 | [diff] [blame] | 1013 | } |
| 1014 | |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 1015 | static void hidg_unbind(struct usb_configuration *c, struct usb_function *f) |
| 1016 | { |
| 1017 | struct f_hidg *hidg = func_to_hidg(f); |
| 1018 | |
| 1019 | device_destroy(hidg_class, MKDEV(major, hidg->minor)); |
| 1020 | cdev_del(&hidg->cdev); |
| 1021 | |
| 1022 | /* disable/free request and end point */ |
| 1023 | usb_ep_disable(hidg->in_ep); |
Felipe F. Tonello | 14794d7 | 2016-08-23 18:24:50 +0100 | [diff] [blame] | 1024 | free_ep_req(hidg->in_ep, hidg->req); |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 1025 | |
Sebastian Andrzej Siewior | 10287ba | 2012-10-22 22:15:06 +0200 | [diff] [blame] | 1026 | usb_free_all_descriptors(f); |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 1027 | } |
| 1028 | |
Fengguang Wu | 0fc57ea | 2014-11-12 22:44:05 +0800 | [diff] [blame] | 1029 | static struct usb_function *hidg_alloc(struct usb_function_instance *fi) |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 1030 | { |
| 1031 | struct f_hidg *hidg; |
Andrzej Pietrasiewicz | cb38253 | 2014-11-06 11:11:59 +0100 | [diff] [blame] | 1032 | struct f_hid_opts *opts; |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 1033 | |
| 1034 | /* allocate and initialize one new instance */ |
Andrzej Pietrasiewicz | cb38253 | 2014-11-06 11:11:59 +0100 | [diff] [blame] | 1035 | hidg = kzalloc(sizeof(*hidg), GFP_KERNEL); |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 1036 | if (!hidg) |
Andrzej Pietrasiewicz | cb38253 | 2014-11-06 11:11:59 +0100 | [diff] [blame] | 1037 | return ERR_PTR(-ENOMEM); |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 1038 | |
Andrzej Pietrasiewicz | cb38253 | 2014-11-06 11:11:59 +0100 | [diff] [blame] | 1039 | opts = container_of(fi, struct f_hid_opts, func_inst); |
| 1040 | |
Andrzej Pietrasiewicz | 21a9476 | 2014-11-06 11:12:03 +0100 | [diff] [blame] | 1041 | mutex_lock(&opts->lock); |
| 1042 | ++opts->refcnt; |
| 1043 | |
Andrzej Pietrasiewicz | cb38253 | 2014-11-06 11:11:59 +0100 | [diff] [blame] | 1044 | hidg->minor = opts->minor; |
| 1045 | hidg->bInterfaceSubClass = opts->subclass; |
| 1046 | hidg->bInterfaceProtocol = opts->protocol; |
| 1047 | hidg->report_length = opts->report_length; |
| 1048 | hidg->report_desc_length = opts->report_desc_length; |
| 1049 | if (opts->report_desc) { |
| 1050 | hidg->report_desc = kmemdup(opts->report_desc, |
| 1051 | opts->report_desc_length, |
| 1052 | GFP_KERNEL); |
| 1053 | if (!hidg->report_desc) { |
| 1054 | kfree(hidg); |
Andrzej Pietrasiewicz | 21a9476 | 2014-11-06 11:12:03 +0100 | [diff] [blame] | 1055 | mutex_unlock(&opts->lock); |
Andrzej Pietrasiewicz | cb38253 | 2014-11-06 11:11:59 +0100 | [diff] [blame] | 1056 | return ERR_PTR(-ENOMEM); |
| 1057 | } |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 1058 | } |
| 1059 | |
Andrzej Pietrasiewicz | 21a9476 | 2014-11-06 11:12:03 +0100 | [diff] [blame] | 1060 | mutex_unlock(&opts->lock); |
| 1061 | |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 1062 | hidg->func.name = "hid"; |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 1063 | hidg->func.bind = hidg_bind; |
| 1064 | hidg->func.unbind = hidg_unbind; |
| 1065 | hidg->func.set_alt = hidg_set_alt; |
| 1066 | hidg->func.disable = hidg_disable; |
| 1067 | hidg->func.setup = hidg_setup; |
Andrzej Pietrasiewicz | cb38253 | 2014-11-06 11:11:59 +0100 | [diff] [blame] | 1068 | hidg->func.free_func = hidg_free; |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 1069 | |
Daniel Mack | 99c515005 | 2012-06-13 12:54:45 +0200 | [diff] [blame] | 1070 | /* this could me made configurable at some point */ |
| 1071 | hidg->qlen = 4; |
| 1072 | |
Andrzej Pietrasiewicz | cb38253 | 2014-11-06 11:11:59 +0100 | [diff] [blame] | 1073 | return &hidg->func; |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 1074 | } |
| 1075 | |
Andrzej Pietrasiewicz | cb38253 | 2014-11-06 11:11:59 +0100 | [diff] [blame] | 1076 | DECLARE_USB_FUNCTION_INIT(hid, hidg_alloc_inst, hidg_alloc); |
| 1077 | MODULE_LICENSE("GPL"); |
| 1078 | MODULE_AUTHOR("Fabien Chouteau"); |
| 1079 | |
Andrzej Pietrasiewicz | cb38253 | 2014-11-06 11:11:59 +0100 | [diff] [blame] | 1080 | int ghid_setup(struct usb_gadget *g, int count) |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 1081 | { |
| 1082 | int status; |
| 1083 | dev_t dev; |
| 1084 | |
| 1085 | hidg_class = class_create(THIS_MODULE, "hidg"); |
Andrzej Pietrasiewicz | 0652940 | 2014-11-06 11:11:56 +0100 | [diff] [blame] | 1086 | if (IS_ERR(hidg_class)) { |
Dan Carpenter | 0448d38 | 2014-11-13 09:20:59 +0300 | [diff] [blame] | 1087 | status = PTR_ERR(hidg_class); |
Andrzej Pietrasiewicz | 0652940 | 2014-11-06 11:11:56 +0100 | [diff] [blame] | 1088 | hidg_class = NULL; |
Dan Carpenter | 0448d38 | 2014-11-13 09:20:59 +0300 | [diff] [blame] | 1089 | return status; |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 1090 | } |
| 1091 | |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 1092 | status = alloc_chrdev_region(&dev, 0, count, "hidg"); |
Dan Carpenter | 0448d38 | 2014-11-13 09:20:59 +0300 | [diff] [blame] | 1093 | if (status) { |
| 1094 | class_destroy(hidg_class); |
| 1095 | hidg_class = NULL; |
| 1096 | return status; |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 1097 | } |
| 1098 | |
Dan Carpenter | 0448d38 | 2014-11-13 09:20:59 +0300 | [diff] [blame] | 1099 | major = MAJOR(dev); |
| 1100 | minors = count; |
| 1101 | |
| 1102 | return 0; |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 1103 | } |
| 1104 | |
| 1105 | void ghid_cleanup(void) |
| 1106 | { |
| 1107 | if (major) { |
| 1108 | unregister_chrdev_region(MKDEV(major, 0), minors); |
| 1109 | major = minors = 0; |
| 1110 | } |
| 1111 | |
| 1112 | class_destroy(hidg_class); |
| 1113 | hidg_class = NULL; |
| 1114 | } |