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; |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 53 | spinlock_t spinlock; |
| 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 */ |
| 58 | struct mutex lock; |
| 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 | |
| 261 | spin_lock_irqsave(&hidg->spinlock, flags); |
| 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) { |
| 267 | spin_unlock_irqrestore(&hidg->spinlock, flags); |
| 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 | |
| 274 | spin_lock_irqsave(&hidg->spinlock, flags); |
| 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); |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 289 | spin_unlock_irqrestore(&hidg->spinlock, flags); |
| 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 { |
| 311 | spin_lock_irqsave(&hidg->spinlock, flags); |
| 312 | list_add(&list->list, &hidg->completed_out_req); |
| 313 | spin_unlock_irqrestore(&hidg->spinlock, flags); |
| 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; |
| 324 | |
| 325 | if (req->status != 0) { |
| 326 | ERROR(hidg->func.config->cdev, |
| 327 | "End Point Request ERROR: %d\n", req->status); |
| 328 | } |
| 329 | |
| 330 | hidg->write_pending = 0; |
| 331 | wake_up(&hidg->write_queue); |
| 332 | } |
| 333 | |
| 334 | static ssize_t f_hidg_write(struct file *file, const char __user *buffer, |
| 335 | size_t count, loff_t *offp) |
| 336 | { |
Joe Perches | fd63b10 | 2010-07-12 13:50:11 -0700 | [diff] [blame] | 337 | struct f_hidg *hidg = file->private_data; |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 338 | ssize_t status = -ENOMEM; |
| 339 | |
| 340 | if (!access_ok(VERIFY_READ, buffer, count)) |
| 341 | return -EFAULT; |
| 342 | |
| 343 | mutex_lock(&hidg->lock); |
| 344 | |
| 345 | #define WRITE_COND (!hidg->write_pending) |
| 346 | |
| 347 | /* write queue */ |
| 348 | while (!WRITE_COND) { |
| 349 | mutex_unlock(&hidg->lock); |
| 350 | if (file->f_flags & O_NONBLOCK) |
| 351 | return -EAGAIN; |
| 352 | |
| 353 | if (wait_event_interruptible_exclusive( |
| 354 | hidg->write_queue, WRITE_COND)) |
| 355 | return -ERESTARTSYS; |
| 356 | |
| 357 | mutex_lock(&hidg->lock); |
| 358 | } |
| 359 | |
| 360 | count = min_t(unsigned, count, hidg->report_length); |
| 361 | status = copy_from_user(hidg->req->buf, buffer, count); |
| 362 | |
| 363 | if (status != 0) { |
| 364 | ERROR(hidg->func.config->cdev, |
| 365 | "copy_from_user error\n"); |
| 366 | mutex_unlock(&hidg->lock); |
| 367 | return -EINVAL; |
| 368 | } |
| 369 | |
| 370 | hidg->req->status = 0; |
| 371 | hidg->req->zero = 0; |
| 372 | hidg->req->length = count; |
| 373 | hidg->req->complete = f_hidg_req_complete; |
| 374 | hidg->req->context = hidg; |
| 375 | hidg->write_pending = 1; |
| 376 | |
| 377 | status = usb_ep_queue(hidg->in_ep, hidg->req, GFP_ATOMIC); |
| 378 | if (status < 0) { |
| 379 | ERROR(hidg->func.config->cdev, |
| 380 | "usb_ep_queue error on int endpoint %zd\n", status); |
| 381 | hidg->write_pending = 0; |
| 382 | wake_up(&hidg->write_queue); |
| 383 | } else { |
| 384 | status = count; |
| 385 | } |
| 386 | |
| 387 | mutex_unlock(&hidg->lock); |
| 388 | |
| 389 | return status; |
| 390 | } |
| 391 | |
| 392 | static unsigned int f_hidg_poll(struct file *file, poll_table *wait) |
| 393 | { |
Joe Perches | fd63b10 | 2010-07-12 13:50:11 -0700 | [diff] [blame] | 394 | struct f_hidg *hidg = file->private_data; |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 395 | unsigned int ret = 0; |
| 396 | |
| 397 | poll_wait(file, &hidg->read_queue, wait); |
| 398 | poll_wait(file, &hidg->write_queue, wait); |
| 399 | |
| 400 | if (WRITE_COND) |
| 401 | ret |= POLLOUT | POLLWRNORM; |
| 402 | |
| 403 | if (READ_COND) |
| 404 | ret |= POLLIN | POLLRDNORM; |
| 405 | |
| 406 | return ret; |
| 407 | } |
| 408 | |
| 409 | #undef WRITE_COND |
| 410 | #undef READ_COND |
| 411 | |
| 412 | static int f_hidg_release(struct inode *inode, struct file *fd) |
| 413 | { |
| 414 | fd->private_data = NULL; |
| 415 | return 0; |
| 416 | } |
| 417 | |
| 418 | static int f_hidg_open(struct inode *inode, struct file *fd) |
| 419 | { |
| 420 | struct f_hidg *hidg = |
| 421 | container_of(inode->i_cdev, struct f_hidg, cdev); |
| 422 | |
| 423 | fd->private_data = hidg; |
| 424 | |
| 425 | return 0; |
| 426 | } |
| 427 | |
| 428 | /*-------------------------------------------------------------------------*/ |
| 429 | /* usb_function */ |
| 430 | |
Andrzej Pietrasiewicz | 1efd54e | 2013-11-07 08:41:26 +0100 | [diff] [blame] | 431 | static inline struct usb_request *hidg_alloc_ep_req(struct usb_ep *ep, |
| 432 | unsigned length) |
Daniel Mack | 99c515005 | 2012-06-13 12:54:45 +0200 | [diff] [blame] | 433 | { |
Felipe F. Tonello | aadbe81 | 2016-08-23 18:24:49 +0100 | [diff] [blame] | 434 | return alloc_ep_req(ep, length); |
Daniel Mack | 99c515005 | 2012-06-13 12:54:45 +0200 | [diff] [blame] | 435 | } |
| 436 | |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 437 | static void hidg_set_report_complete(struct usb_ep *ep, struct usb_request *req) |
| 438 | { |
Daniel Mack | 99c515005 | 2012-06-13 12:54:45 +0200 | [diff] [blame] | 439 | struct f_hidg *hidg = (struct f_hidg *) req->context; |
Krzysztof Opasiak | 20d2ca9 | 2017-01-19 18:55:27 +0100 | [diff] [blame] | 440 | struct usb_composite_dev *cdev = hidg->func.config->cdev; |
Daniel Mack | 99c515005 | 2012-06-13 12:54:45 +0200 | [diff] [blame] | 441 | struct f_hidg_req_list *req_list; |
| 442 | unsigned long flags; |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 443 | |
Krzysztof Opasiak | 20d2ca9 | 2017-01-19 18:55:27 +0100 | [diff] [blame] | 444 | switch (req->status) { |
| 445 | case 0: |
| 446 | req_list = kzalloc(sizeof(*req_list), GFP_ATOMIC); |
| 447 | if (!req_list) { |
| 448 | ERROR(cdev, "Unable to allocate mem for req_list\n"); |
| 449 | goto free_req; |
| 450 | } |
| 451 | |
| 452 | req_list->req = req; |
| 453 | |
| 454 | spin_lock_irqsave(&hidg->spinlock, flags); |
| 455 | list_add_tail(&req_list->list, &hidg->completed_out_req); |
| 456 | spin_unlock_irqrestore(&hidg->spinlock, flags); |
| 457 | |
| 458 | wake_up(&hidg->read_queue); |
| 459 | break; |
| 460 | default: |
| 461 | ERROR(cdev, "Set report failed %d\n", req->status); |
| 462 | /* FALLTHROUGH */ |
| 463 | case -ECONNABORTED: /* hardware forced ep reset */ |
| 464 | case -ECONNRESET: /* request dequeued */ |
| 465 | case -ESHUTDOWN: /* disconnect from host */ |
| 466 | free_req: |
| 467 | free_ep_req(ep, req); |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 468 | return; |
Krzysztof Opasiak | 20d2ca9 | 2017-01-19 18:55:27 +0100 | [diff] [blame] | 469 | } |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | static int hidg_setup(struct usb_function *f, |
| 473 | const struct usb_ctrlrequest *ctrl) |
| 474 | { |
| 475 | struct f_hidg *hidg = func_to_hidg(f); |
| 476 | struct usb_composite_dev *cdev = f->config->cdev; |
| 477 | struct usb_request *req = cdev->req; |
| 478 | int status = 0; |
| 479 | __u16 value, length; |
| 480 | |
| 481 | value = __le16_to_cpu(ctrl->wValue); |
| 482 | length = __le16_to_cpu(ctrl->wLength); |
| 483 | |
Julia Lawall | c9b3bde | 2014-12-07 20:21:00 +0100 | [diff] [blame] | 484 | VDBG(cdev, |
| 485 | "%s crtl_request : bRequestType:0x%x bRequest:0x%x Value:0x%x\n", |
| 486 | __func__, ctrl->bRequestType, ctrl->bRequest, value); |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 487 | |
| 488 | switch ((ctrl->bRequestType << 8) | ctrl->bRequest) { |
| 489 | case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8 |
| 490 | | HID_REQ_GET_REPORT): |
| 491 | VDBG(cdev, "get_report\n"); |
| 492 | |
| 493 | /* send an empty report */ |
| 494 | length = min_t(unsigned, length, hidg->report_length); |
| 495 | memset(req->buf, 0x0, length); |
| 496 | |
| 497 | goto respond; |
| 498 | break; |
| 499 | |
| 500 | case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8 |
| 501 | | HID_REQ_GET_PROTOCOL): |
| 502 | VDBG(cdev, "get_protocol\n"); |
| 503 | goto stall; |
| 504 | break; |
| 505 | |
| 506 | case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8 |
| 507 | | HID_REQ_SET_REPORT): |
Masanari Iida | 6774def | 2014-11-05 22:26:48 +0900 | [diff] [blame] | 508 | VDBG(cdev, "set_report | wLength=%d\n", ctrl->wLength); |
Daniel Mack | 99c515005 | 2012-06-13 12:54:45 +0200 | [diff] [blame] | 509 | goto stall; |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 510 | break; |
| 511 | |
| 512 | case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8 |
| 513 | | HID_REQ_SET_PROTOCOL): |
| 514 | VDBG(cdev, "set_protocol\n"); |
| 515 | goto stall; |
| 516 | break; |
| 517 | |
| 518 | case ((USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_INTERFACE) << 8 |
| 519 | | USB_REQ_GET_DESCRIPTOR): |
| 520 | switch (value >> 8) { |
Sebastian Bauer | c240d78 | 2011-07-21 15:40:07 +0200 | [diff] [blame] | 521 | case HID_DT_HID: |
Krzysztof Opasiak | f286d48 | 2015-03-27 09:35:44 +0100 | [diff] [blame] | 522 | { |
| 523 | struct hid_descriptor hidg_desc_copy = hidg_desc; |
| 524 | |
Sebastian Bauer | c240d78 | 2011-07-21 15:40:07 +0200 | [diff] [blame] | 525 | VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: HID\n"); |
Krzysztof Opasiak | f286d48 | 2015-03-27 09:35:44 +0100 | [diff] [blame] | 526 | hidg_desc_copy.desc[0].bDescriptorType = HID_DT_REPORT; |
| 527 | hidg_desc_copy.desc[0].wDescriptorLength = |
| 528 | cpu_to_le16(hidg->report_desc_length); |
| 529 | |
Sebastian Bauer | c240d78 | 2011-07-21 15:40:07 +0200 | [diff] [blame] | 530 | length = min_t(unsigned short, length, |
Krzysztof Opasiak | f286d48 | 2015-03-27 09:35:44 +0100 | [diff] [blame] | 531 | hidg_desc_copy.bLength); |
| 532 | memcpy(req->buf, &hidg_desc_copy, length); |
Sebastian Bauer | c240d78 | 2011-07-21 15:40:07 +0200 | [diff] [blame] | 533 | goto respond; |
| 534 | break; |
Krzysztof Opasiak | f286d48 | 2015-03-27 09:35:44 +0100 | [diff] [blame] | 535 | } |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 536 | case HID_DT_REPORT: |
| 537 | VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: REPORT\n"); |
| 538 | length = min_t(unsigned short, length, |
| 539 | hidg->report_desc_length); |
| 540 | memcpy(req->buf, hidg->report_desc, length); |
| 541 | goto respond; |
| 542 | break; |
| 543 | |
| 544 | default: |
Masanari Iida | 1c1301d | 2012-04-19 00:04:46 +0900 | [diff] [blame] | 545 | VDBG(cdev, "Unknown descriptor request 0x%x\n", |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 546 | value >> 8); |
| 547 | goto stall; |
| 548 | break; |
| 549 | } |
| 550 | break; |
| 551 | |
| 552 | default: |
| 553 | VDBG(cdev, "Unknown request 0x%x\n", |
| 554 | ctrl->bRequest); |
| 555 | goto stall; |
| 556 | break; |
| 557 | } |
| 558 | |
| 559 | stall: |
| 560 | return -EOPNOTSUPP; |
| 561 | |
| 562 | respond: |
| 563 | req->zero = 0; |
| 564 | req->length = length; |
| 565 | status = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC); |
| 566 | if (status < 0) |
| 567 | ERROR(cdev, "usb_ep_queue error on ep0 %d\n", value); |
| 568 | return status; |
| 569 | } |
| 570 | |
| 571 | static void hidg_disable(struct usb_function *f) |
| 572 | { |
| 573 | struct f_hidg *hidg = func_to_hidg(f); |
Daniel Mack | 99c515005 | 2012-06-13 12:54:45 +0200 | [diff] [blame] | 574 | struct f_hidg_req_list *list, *next; |
Krzysztof Opasiak | aa65d11 | 2017-01-19 18:55:28 +0100 | [diff] [blame^] | 575 | unsigned long flags; |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 576 | |
| 577 | usb_ep_disable(hidg->in_ep); |
Daniel Mack | 99c515005 | 2012-06-13 12:54:45 +0200 | [diff] [blame] | 578 | usb_ep_disable(hidg->out_ep); |
Daniel Mack | 99c515005 | 2012-06-13 12:54:45 +0200 | [diff] [blame] | 579 | |
Krzysztof Opasiak | aa65d11 | 2017-01-19 18:55:28 +0100 | [diff] [blame^] | 580 | spin_lock_irqsave(&hidg->spinlock, flags); |
Daniel Mack | 99c515005 | 2012-06-13 12:54:45 +0200 | [diff] [blame] | 581 | list_for_each_entry_safe(list, next, &hidg->completed_out_req, list) { |
Krzysztof Opasiak | aa65d11 | 2017-01-19 18:55:28 +0100 | [diff] [blame^] | 582 | free_ep_req(hidg->out_ep, list->req); |
Daniel Mack | 99c515005 | 2012-06-13 12:54:45 +0200 | [diff] [blame] | 583 | list_del(&list->list); |
| 584 | kfree(list); |
| 585 | } |
Krzysztof Opasiak | aa65d11 | 2017-01-19 18:55:28 +0100 | [diff] [blame^] | 586 | spin_unlock_irqrestore(&hidg->spinlock, flags); |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | static int hidg_set_alt(struct usb_function *f, unsigned intf, unsigned alt) |
| 590 | { |
| 591 | struct usb_composite_dev *cdev = f->config->cdev; |
| 592 | struct f_hidg *hidg = func_to_hidg(f); |
Daniel Mack | 99c515005 | 2012-06-13 12:54:45 +0200 | [diff] [blame] | 593 | int i, status = 0; |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 594 | |
| 595 | VDBG(cdev, "hidg_set_alt intf:%d alt:%d\n", intf, alt); |
| 596 | |
| 597 | if (hidg->in_ep != NULL) { |
| 598 | /* restart endpoint */ |
Robert Baldyga | 2516a68 | 2015-09-16 12:10:46 +0200 | [diff] [blame] | 599 | usb_ep_disable(hidg->in_ep); |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 600 | |
Tatyana Brokhman | ea2a1df | 2011-06-28 16:33:50 +0300 | [diff] [blame] | 601 | status = config_ep_by_speed(f->config->cdev->gadget, f, |
| 602 | hidg->in_ep); |
| 603 | if (status) { |
| 604 | ERROR(cdev, "config_ep_by_speed FAILED!\n"); |
| 605 | goto fail; |
| 606 | } |
Tatyana Brokhman | 72c973d | 2011-06-28 16:33:48 +0300 | [diff] [blame] | 607 | status = usb_ep_enable(hidg->in_ep); |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 608 | if (status < 0) { |
Daniel Mack | 99c515005 | 2012-06-13 12:54:45 +0200 | [diff] [blame] | 609 | ERROR(cdev, "Enable IN endpoint FAILED!\n"); |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 610 | goto fail; |
| 611 | } |
| 612 | hidg->in_ep->driver_data = hidg; |
| 613 | } |
Daniel Mack | 99c515005 | 2012-06-13 12:54:45 +0200 | [diff] [blame] | 614 | |
| 615 | |
| 616 | if (hidg->out_ep != NULL) { |
| 617 | /* restart endpoint */ |
Robert Baldyga | 2516a68 | 2015-09-16 12:10:46 +0200 | [diff] [blame] | 618 | usb_ep_disable(hidg->out_ep); |
Daniel Mack | 99c515005 | 2012-06-13 12:54:45 +0200 | [diff] [blame] | 619 | |
| 620 | status = config_ep_by_speed(f->config->cdev->gadget, f, |
| 621 | hidg->out_ep); |
| 622 | if (status) { |
| 623 | ERROR(cdev, "config_ep_by_speed FAILED!\n"); |
| 624 | goto fail; |
| 625 | } |
| 626 | status = usb_ep_enable(hidg->out_ep); |
| 627 | if (status < 0) { |
David Lechner | 43aef5c | 2017-01-02 17:28:39 -0600 | [diff] [blame] | 628 | ERROR(cdev, "Enable OUT endpoint FAILED!\n"); |
Daniel Mack | 99c515005 | 2012-06-13 12:54:45 +0200 | [diff] [blame] | 629 | goto fail; |
| 630 | } |
| 631 | hidg->out_ep->driver_data = hidg; |
| 632 | |
| 633 | /* |
| 634 | * allocate a bunch of read buffers and queue them all at once. |
| 635 | */ |
| 636 | for (i = 0; i < hidg->qlen && status == 0; i++) { |
| 637 | struct usb_request *req = |
| 638 | hidg_alloc_ep_req(hidg->out_ep, |
| 639 | hidg->report_length); |
| 640 | if (req) { |
| 641 | req->complete = hidg_set_report_complete; |
| 642 | req->context = hidg; |
| 643 | status = usb_ep_queue(hidg->out_ep, req, |
| 644 | GFP_ATOMIC); |
| 645 | if (status) |
| 646 | ERROR(cdev, "%s queue req --> %d\n", |
| 647 | hidg->out_ep->name, status); |
| 648 | } else { |
| 649 | usb_ep_disable(hidg->out_ep); |
Daniel Mack | 99c515005 | 2012-06-13 12:54:45 +0200 | [diff] [blame] | 650 | status = -ENOMEM; |
| 651 | goto fail; |
| 652 | } |
| 653 | } |
| 654 | } |
| 655 | |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 656 | fail: |
| 657 | return status; |
| 658 | } |
| 659 | |
Lad, Prabhakar | 7a3cc46 | 2015-02-04 17:49:36 +0000 | [diff] [blame] | 660 | static const struct file_operations f_hidg_fops = { |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 661 | .owner = THIS_MODULE, |
| 662 | .open = f_hidg_open, |
| 663 | .release = f_hidg_release, |
| 664 | .write = f_hidg_write, |
| 665 | .read = f_hidg_read, |
| 666 | .poll = f_hidg_poll, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 667 | .llseek = noop_llseek, |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 668 | }; |
| 669 | |
Andrzej Pietrasiewicz | cb38253 | 2014-11-06 11:11:59 +0100 | [diff] [blame] | 670 | static int hidg_bind(struct usb_configuration *c, struct usb_function *f) |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 671 | { |
| 672 | struct usb_ep *ep; |
| 673 | struct f_hidg *hidg = func_to_hidg(f); |
Andrzej Pietrasiewicz | 5ca8d3ec | 2014-11-06 11:12:02 +0100 | [diff] [blame] | 674 | struct usb_string *us; |
Andrzej Pietrasiewicz | 6340608 | 2014-11-06 11:11:57 +0100 | [diff] [blame] | 675 | struct device *device; |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 676 | int status; |
| 677 | dev_t dev; |
| 678 | |
Andrzej Pietrasiewicz | cb38253 | 2014-11-06 11:11:59 +0100 | [diff] [blame] | 679 | /* maybe allocate device-global string IDs, and patch descriptors */ |
Andrzej Pietrasiewicz | 5ca8d3ec | 2014-11-06 11:12:02 +0100 | [diff] [blame] | 680 | us = usb_gstrings_attach(c->cdev, ct_func_strings, |
| 681 | ARRAY_SIZE(ct_func_string_defs)); |
| 682 | if (IS_ERR(us)) |
| 683 | return PTR_ERR(us); |
| 684 | hidg_interface_desc.iInterface = us[CT_FUNC_HID_IDX].id; |
Andrzej Pietrasiewicz | cb38253 | 2014-11-06 11:11:59 +0100 | [diff] [blame] | 685 | |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 686 | /* allocate instance-specific interface IDs, and patch descriptors */ |
| 687 | status = usb_interface_id(c, f); |
| 688 | if (status < 0) |
| 689 | goto fail; |
| 690 | hidg_interface_desc.bInterfaceNumber = status; |
| 691 | |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 692 | /* allocate instance-specific endpoints */ |
| 693 | status = -ENODEV; |
| 694 | ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_in_ep_desc); |
| 695 | if (!ep) |
| 696 | goto fail; |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 697 | hidg->in_ep = ep; |
| 698 | |
Daniel Mack | 99c515005 | 2012-06-13 12:54:45 +0200 | [diff] [blame] | 699 | ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_out_ep_desc); |
| 700 | if (!ep) |
| 701 | goto fail; |
Daniel Mack | 99c515005 | 2012-06-13 12:54:45 +0200 | [diff] [blame] | 702 | hidg->out_ep = ep; |
| 703 | |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 704 | /* preallocate request and buffer */ |
| 705 | status = -ENOMEM; |
Felipe F. Tonello | ba1582f | 2016-08-23 18:24:51 +0100 | [diff] [blame] | 706 | hidg->req = alloc_ep_req(hidg->in_ep, hidg->report_length); |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 707 | if (!hidg->req) |
| 708 | goto fail; |
| 709 | |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 710 | /* set descriptor dynamic values */ |
| 711 | hidg_interface_desc.bInterfaceSubClass = hidg->bInterfaceSubClass; |
| 712 | hidg_interface_desc.bInterfaceProtocol = hidg->bInterfaceProtocol; |
Janusz Dziedzic | dbf499c | 2016-11-03 10:27:15 +0100 | [diff] [blame] | 713 | hidg_ss_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length); |
| 714 | hidg_ss_in_comp_desc.wBytesPerInterval = |
| 715 | cpu_to_le16(hidg->report_length); |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 716 | hidg_hs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length); |
| 717 | hidg_fs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length); |
Janusz Dziedzic | dbf499c | 2016-11-03 10:27:15 +0100 | [diff] [blame] | 718 | hidg_ss_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length); |
| 719 | hidg_ss_out_comp_desc.wBytesPerInterval = |
| 720 | cpu_to_le16(hidg->report_length); |
Daniel Mack | 99c515005 | 2012-06-13 12:54:45 +0200 | [diff] [blame] | 721 | hidg_hs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length); |
| 722 | hidg_fs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length); |
Krzysztof Opasiak | f286d48 | 2015-03-27 09:35:44 +0100 | [diff] [blame] | 723 | /* |
| 724 | * We can use hidg_desc struct here but we should not relay |
| 725 | * that its content won't change after returning from this function. |
| 726 | */ |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 727 | hidg_desc.desc[0].bDescriptorType = HID_DT_REPORT; |
| 728 | hidg_desc.desc[0].wDescriptorLength = |
| 729 | cpu_to_le16(hidg->report_desc_length); |
| 730 | |
Sebastian Andrzej Siewior | 10287ba | 2012-10-22 22:15:06 +0200 | [diff] [blame] | 731 | hidg_hs_in_ep_desc.bEndpointAddress = |
| 732 | hidg_fs_in_ep_desc.bEndpointAddress; |
| 733 | hidg_hs_out_ep_desc.bEndpointAddress = |
| 734 | hidg_fs_out_ep_desc.bEndpointAddress; |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 735 | |
Janusz Dziedzic | dbf499c | 2016-11-03 10:27:15 +0100 | [diff] [blame] | 736 | hidg_ss_in_ep_desc.bEndpointAddress = |
| 737 | hidg_fs_in_ep_desc.bEndpointAddress; |
| 738 | hidg_ss_out_ep_desc.bEndpointAddress = |
| 739 | hidg_fs_out_ep_desc.bEndpointAddress; |
| 740 | |
Sebastian Andrzej Siewior | 10287ba | 2012-10-22 22:15:06 +0200 | [diff] [blame] | 741 | status = usb_assign_descriptors(f, hidg_fs_descriptors, |
Janusz Dziedzic | dbf499c | 2016-11-03 10:27:15 +0100 | [diff] [blame] | 742 | hidg_hs_descriptors, hidg_ss_descriptors, NULL); |
Sebastian Andrzej Siewior | 10287ba | 2012-10-22 22:15:06 +0200 | [diff] [blame] | 743 | if (status) |
| 744 | goto fail; |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 745 | |
| 746 | mutex_init(&hidg->lock); |
| 747 | spin_lock_init(&hidg->spinlock); |
| 748 | init_waitqueue_head(&hidg->write_queue); |
| 749 | init_waitqueue_head(&hidg->read_queue); |
Daniel Mack | 99c515005 | 2012-06-13 12:54:45 +0200 | [diff] [blame] | 750 | INIT_LIST_HEAD(&hidg->completed_out_req); |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 751 | |
| 752 | /* create char device */ |
| 753 | cdev_init(&hidg->cdev, &f_hidg_fops); |
| 754 | dev = MKDEV(major, hidg->minor); |
| 755 | status = cdev_add(&hidg->cdev, dev, 1); |
| 756 | if (status) |
Pavitrakumar Managutte | d12a872 | 2014-10-22 19:24:58 +0530 | [diff] [blame] | 757 | goto fail_free_descs; |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 758 | |
Andrzej Pietrasiewicz | 6340608 | 2014-11-06 11:11:57 +0100 | [diff] [blame] | 759 | device = device_create(hidg_class, NULL, dev, NULL, |
| 760 | "%s%d", "hidg", hidg->minor); |
| 761 | if (IS_ERR(device)) { |
| 762 | status = PTR_ERR(device); |
| 763 | goto del; |
| 764 | } |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 765 | |
| 766 | return 0; |
Andrzej Pietrasiewicz | 6340608 | 2014-11-06 11:11:57 +0100 | [diff] [blame] | 767 | del: |
| 768 | cdev_del(&hidg->cdev); |
Pavitrakumar Managutte | d12a872 | 2014-10-22 19:24:58 +0530 | [diff] [blame] | 769 | fail_free_descs: |
| 770 | usb_free_all_descriptors(f); |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 771 | fail: |
| 772 | ERROR(f->config->cdev, "hidg_bind FAILED\n"); |
Felipe F. Tonello | 14794d7 | 2016-08-23 18:24:50 +0100 | [diff] [blame] | 773 | if (hidg->req != NULL) |
| 774 | free_ep_req(hidg->in_ep, hidg->req); |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 775 | |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 776 | return status; |
| 777 | } |
| 778 | |
Andrzej Pietrasiewicz | cb38253 | 2014-11-06 11:11:59 +0100 | [diff] [blame] | 779 | static inline int hidg_get_minor(void) |
| 780 | { |
| 781 | int ret; |
| 782 | |
| 783 | ret = ida_simple_get(&hidg_ida, 0, 0, GFP_KERNEL); |
Andrzej Pietrasiewicz | 774cf72 | 2015-07-24 09:48:40 +0200 | [diff] [blame] | 784 | if (ret >= HIDG_MINORS) { |
| 785 | ida_simple_remove(&hidg_ida, ret); |
| 786 | ret = -ENODEV; |
| 787 | } |
Andrzej Pietrasiewicz | cb38253 | 2014-11-06 11:11:59 +0100 | [diff] [blame] | 788 | |
| 789 | return ret; |
| 790 | } |
| 791 | |
Andrzej Pietrasiewicz | 21a9476 | 2014-11-06 11:12:03 +0100 | [diff] [blame] | 792 | static inline struct f_hid_opts *to_f_hid_opts(struct config_item *item) |
| 793 | { |
| 794 | return container_of(to_config_group(item), struct f_hid_opts, |
| 795 | func_inst.group); |
| 796 | } |
| 797 | |
Andrzej Pietrasiewicz | 21a9476 | 2014-11-06 11:12:03 +0100 | [diff] [blame] | 798 | static void hid_attr_release(struct config_item *item) |
| 799 | { |
| 800 | struct f_hid_opts *opts = to_f_hid_opts(item); |
| 801 | |
| 802 | usb_put_function_instance(&opts->func_inst); |
| 803 | } |
| 804 | |
| 805 | static struct configfs_item_operations hidg_item_ops = { |
| 806 | .release = hid_attr_release, |
Andrzej Pietrasiewicz | 21a9476 | 2014-11-06 11:12:03 +0100 | [diff] [blame] | 807 | }; |
| 808 | |
| 809 | #define F_HID_OPT(name, prec, limit) \ |
Christoph Hellwig | da4e527 | 2015-10-03 15:32:40 +0200 | [diff] [blame] | 810 | 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] | 811 | { \ |
Christoph Hellwig | da4e527 | 2015-10-03 15:32:40 +0200 | [diff] [blame] | 812 | struct f_hid_opts *opts = to_f_hid_opts(item); \ |
Andrzej Pietrasiewicz | 21a9476 | 2014-11-06 11:12:03 +0100 | [diff] [blame] | 813 | int result; \ |
| 814 | \ |
| 815 | mutex_lock(&opts->lock); \ |
| 816 | result = sprintf(page, "%d\n", opts->name); \ |
| 817 | mutex_unlock(&opts->lock); \ |
| 818 | \ |
| 819 | return result; \ |
| 820 | } \ |
| 821 | \ |
Christoph Hellwig | da4e527 | 2015-10-03 15:32:40 +0200 | [diff] [blame] | 822 | static ssize_t f_hid_opts_##name##_store(struct config_item *item, \ |
Andrzej Pietrasiewicz | 21a9476 | 2014-11-06 11:12:03 +0100 | [diff] [blame] | 823 | const char *page, size_t len) \ |
| 824 | { \ |
Christoph Hellwig | da4e527 | 2015-10-03 15:32:40 +0200 | [diff] [blame] | 825 | struct f_hid_opts *opts = to_f_hid_opts(item); \ |
Andrzej Pietrasiewicz | 21a9476 | 2014-11-06 11:12:03 +0100 | [diff] [blame] | 826 | int ret; \ |
| 827 | u##prec num; \ |
| 828 | \ |
| 829 | mutex_lock(&opts->lock); \ |
| 830 | if (opts->refcnt) { \ |
| 831 | ret = -EBUSY; \ |
| 832 | goto end; \ |
| 833 | } \ |
| 834 | \ |
| 835 | ret = kstrtou##prec(page, 0, &num); \ |
| 836 | if (ret) \ |
| 837 | goto end; \ |
| 838 | \ |
| 839 | if (num > limit) { \ |
| 840 | ret = -EINVAL; \ |
| 841 | goto end; \ |
| 842 | } \ |
| 843 | opts->name = num; \ |
| 844 | ret = len; \ |
| 845 | \ |
| 846 | end: \ |
| 847 | mutex_unlock(&opts->lock); \ |
| 848 | return ret; \ |
| 849 | } \ |
| 850 | \ |
Christoph Hellwig | da4e527 | 2015-10-03 15:32:40 +0200 | [diff] [blame] | 851 | CONFIGFS_ATTR(f_hid_opts_, name) |
Andrzej Pietrasiewicz | 21a9476 | 2014-11-06 11:12:03 +0100 | [diff] [blame] | 852 | |
| 853 | F_HID_OPT(subclass, 8, 255); |
| 854 | F_HID_OPT(protocol, 8, 255); |
Andrzej Pietrasiewicz | 39a2ac27 | 2014-12-15 13:50:05 +0100 | [diff] [blame] | 855 | F_HID_OPT(report_length, 16, 65535); |
Andrzej Pietrasiewicz | 21a9476 | 2014-11-06 11:12:03 +0100 | [diff] [blame] | 856 | |
Christoph Hellwig | da4e527 | 2015-10-03 15:32:40 +0200 | [diff] [blame] | 857 | 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] | 858 | { |
Christoph Hellwig | da4e527 | 2015-10-03 15:32:40 +0200 | [diff] [blame] | 859 | struct f_hid_opts *opts = to_f_hid_opts(item); |
Andrzej Pietrasiewicz | 21a9476 | 2014-11-06 11:12:03 +0100 | [diff] [blame] | 860 | int result; |
| 861 | |
| 862 | mutex_lock(&opts->lock); |
| 863 | result = opts->report_desc_length; |
| 864 | memcpy(page, opts->report_desc, opts->report_desc_length); |
| 865 | mutex_unlock(&opts->lock); |
| 866 | |
| 867 | return result; |
| 868 | } |
| 869 | |
Christoph Hellwig | da4e527 | 2015-10-03 15:32:40 +0200 | [diff] [blame] | 870 | 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] | 871 | const char *page, size_t len) |
| 872 | { |
Christoph Hellwig | da4e527 | 2015-10-03 15:32:40 +0200 | [diff] [blame] | 873 | struct f_hid_opts *opts = to_f_hid_opts(item); |
Andrzej Pietrasiewicz | 21a9476 | 2014-11-06 11:12:03 +0100 | [diff] [blame] | 874 | int ret = -EBUSY; |
| 875 | char *d; |
| 876 | |
| 877 | mutex_lock(&opts->lock); |
| 878 | |
| 879 | if (opts->refcnt) |
| 880 | goto end; |
| 881 | if (len > PAGE_SIZE) { |
| 882 | ret = -ENOSPC; |
| 883 | goto end; |
| 884 | } |
| 885 | d = kmemdup(page, len, GFP_KERNEL); |
| 886 | if (!d) { |
| 887 | ret = -ENOMEM; |
| 888 | goto end; |
| 889 | } |
| 890 | kfree(opts->report_desc); |
| 891 | opts->report_desc = d; |
| 892 | opts->report_desc_length = len; |
| 893 | opts->report_desc_alloc = true; |
| 894 | ret = len; |
| 895 | end: |
| 896 | mutex_unlock(&opts->lock); |
| 897 | return ret; |
| 898 | } |
| 899 | |
Christoph Hellwig | da4e527 | 2015-10-03 15:32:40 +0200 | [diff] [blame] | 900 | CONFIGFS_ATTR(f_hid_opts_, report_desc); |
Andrzej Pietrasiewicz | 21a9476 | 2014-11-06 11:12:03 +0100 | [diff] [blame] | 901 | |
Johannes Berg | ed6fe1f | 2016-06-23 22:28:54 +0200 | [diff] [blame] | 902 | static ssize_t f_hid_opts_dev_show(struct config_item *item, char *page) |
| 903 | { |
| 904 | struct f_hid_opts *opts = to_f_hid_opts(item); |
| 905 | |
| 906 | return sprintf(page, "%d:%d\n", major, opts->minor); |
| 907 | } |
| 908 | |
| 909 | CONFIGFS_ATTR_RO(f_hid_opts_, dev); |
| 910 | |
Andrzej Pietrasiewicz | 21a9476 | 2014-11-06 11:12:03 +0100 | [diff] [blame] | 911 | static struct configfs_attribute *hid_attrs[] = { |
Christoph Hellwig | da4e527 | 2015-10-03 15:32:40 +0200 | [diff] [blame] | 912 | &f_hid_opts_attr_subclass, |
| 913 | &f_hid_opts_attr_protocol, |
| 914 | &f_hid_opts_attr_report_length, |
| 915 | &f_hid_opts_attr_report_desc, |
Johannes Berg | ed6fe1f | 2016-06-23 22:28:54 +0200 | [diff] [blame] | 916 | &f_hid_opts_attr_dev, |
Andrzej Pietrasiewicz | 21a9476 | 2014-11-06 11:12:03 +0100 | [diff] [blame] | 917 | NULL, |
| 918 | }; |
| 919 | |
| 920 | static struct config_item_type hid_func_type = { |
| 921 | .ct_item_ops = &hidg_item_ops, |
| 922 | .ct_attrs = hid_attrs, |
| 923 | .ct_owner = THIS_MODULE, |
| 924 | }; |
| 925 | |
Andrzej Pietrasiewicz | cb38253 | 2014-11-06 11:11:59 +0100 | [diff] [blame] | 926 | static inline void hidg_put_minor(int minor) |
| 927 | { |
| 928 | ida_simple_remove(&hidg_ida, minor); |
| 929 | } |
| 930 | |
| 931 | static void hidg_free_inst(struct usb_function_instance *f) |
| 932 | { |
| 933 | struct f_hid_opts *opts; |
| 934 | |
| 935 | opts = container_of(f, struct f_hid_opts, func_inst); |
| 936 | |
| 937 | mutex_lock(&hidg_ida_lock); |
| 938 | |
| 939 | hidg_put_minor(opts->minor); |
Matthew Wilcox | 99c4940 | 2016-12-14 15:09:13 -0800 | [diff] [blame] | 940 | if (ida_is_empty(&hidg_ida)) |
Andrzej Pietrasiewicz | cb38253 | 2014-11-06 11:11:59 +0100 | [diff] [blame] | 941 | ghid_cleanup(); |
| 942 | |
| 943 | mutex_unlock(&hidg_ida_lock); |
| 944 | |
| 945 | if (opts->report_desc_alloc) |
| 946 | kfree(opts->report_desc); |
| 947 | |
| 948 | kfree(opts); |
| 949 | } |
| 950 | |
| 951 | static struct usb_function_instance *hidg_alloc_inst(void) |
| 952 | { |
| 953 | struct f_hid_opts *opts; |
| 954 | struct usb_function_instance *ret; |
| 955 | int status = 0; |
| 956 | |
| 957 | opts = kzalloc(sizeof(*opts), GFP_KERNEL); |
| 958 | if (!opts) |
| 959 | return ERR_PTR(-ENOMEM); |
Andrzej Pietrasiewicz | 21a9476 | 2014-11-06 11:12:03 +0100 | [diff] [blame] | 960 | mutex_init(&opts->lock); |
Andrzej Pietrasiewicz | cb38253 | 2014-11-06 11:11:59 +0100 | [diff] [blame] | 961 | opts->func_inst.free_func_inst = hidg_free_inst; |
| 962 | ret = &opts->func_inst; |
| 963 | |
| 964 | mutex_lock(&hidg_ida_lock); |
| 965 | |
Matthew Wilcox | 99c4940 | 2016-12-14 15:09:13 -0800 | [diff] [blame] | 966 | if (ida_is_empty(&hidg_ida)) { |
Andrzej Pietrasiewicz | cb38253 | 2014-11-06 11:11:59 +0100 | [diff] [blame] | 967 | status = ghid_setup(NULL, HIDG_MINORS); |
| 968 | if (status) { |
| 969 | ret = ERR_PTR(status); |
| 970 | kfree(opts); |
| 971 | goto unlock; |
| 972 | } |
| 973 | } |
| 974 | |
| 975 | opts->minor = hidg_get_minor(); |
| 976 | if (opts->minor < 0) { |
| 977 | ret = ERR_PTR(opts->minor); |
| 978 | kfree(opts); |
Matthew Wilcox | 99c4940 | 2016-12-14 15:09:13 -0800 | [diff] [blame] | 979 | if (ida_is_empty(&hidg_ida)) |
Andrzej Pietrasiewicz | cb38253 | 2014-11-06 11:11:59 +0100 | [diff] [blame] | 980 | ghid_cleanup(); |
Dan Carpenter | 828f614 | 2014-11-13 09:19:47 +0300 | [diff] [blame] | 981 | goto unlock; |
Andrzej Pietrasiewicz | cb38253 | 2014-11-06 11:11:59 +0100 | [diff] [blame] | 982 | } |
Andrzej Pietrasiewicz | 21a9476 | 2014-11-06 11:12:03 +0100 | [diff] [blame] | 983 | config_group_init_type_name(&opts->func_inst.group, "", &hid_func_type); |
Andrzej Pietrasiewicz | cb38253 | 2014-11-06 11:11:59 +0100 | [diff] [blame] | 984 | |
| 985 | unlock: |
| 986 | mutex_unlock(&hidg_ida_lock); |
| 987 | return ret; |
| 988 | } |
| 989 | |
| 990 | static void hidg_free(struct usb_function *f) |
| 991 | { |
| 992 | struct f_hidg *hidg; |
Andrzej Pietrasiewicz | 21a9476 | 2014-11-06 11:12:03 +0100 | [diff] [blame] | 993 | struct f_hid_opts *opts; |
Andrzej Pietrasiewicz | cb38253 | 2014-11-06 11:11:59 +0100 | [diff] [blame] | 994 | |
| 995 | hidg = func_to_hidg(f); |
Andrzej Pietrasiewicz | 21a9476 | 2014-11-06 11:12:03 +0100 | [diff] [blame] | 996 | opts = container_of(f->fi, struct f_hid_opts, func_inst); |
Andrzej Pietrasiewicz | cb38253 | 2014-11-06 11:11:59 +0100 | [diff] [blame] | 997 | kfree(hidg->report_desc); |
| 998 | kfree(hidg); |
Andrzej Pietrasiewicz | 21a9476 | 2014-11-06 11:12:03 +0100 | [diff] [blame] | 999 | mutex_lock(&opts->lock); |
| 1000 | --opts->refcnt; |
| 1001 | mutex_unlock(&opts->lock); |
Andrzej Pietrasiewicz | cb38253 | 2014-11-06 11:11:59 +0100 | [diff] [blame] | 1002 | } |
| 1003 | |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 1004 | static void hidg_unbind(struct usb_configuration *c, struct usb_function *f) |
| 1005 | { |
| 1006 | struct f_hidg *hidg = func_to_hidg(f); |
| 1007 | |
| 1008 | device_destroy(hidg_class, MKDEV(major, hidg->minor)); |
| 1009 | cdev_del(&hidg->cdev); |
| 1010 | |
| 1011 | /* disable/free request and end point */ |
| 1012 | usb_ep_disable(hidg->in_ep); |
Felipe F. Tonello | 14794d7 | 2016-08-23 18:24:50 +0100 | [diff] [blame] | 1013 | free_ep_req(hidg->in_ep, hidg->req); |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 1014 | |
Sebastian Andrzej Siewior | 10287ba | 2012-10-22 22:15:06 +0200 | [diff] [blame] | 1015 | usb_free_all_descriptors(f); |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 1016 | } |
| 1017 | |
Fengguang Wu | 0fc57ea | 2014-11-12 22:44:05 +0800 | [diff] [blame] | 1018 | static struct usb_function *hidg_alloc(struct usb_function_instance *fi) |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 1019 | { |
| 1020 | struct f_hidg *hidg; |
Andrzej Pietrasiewicz | cb38253 | 2014-11-06 11:11:59 +0100 | [diff] [blame] | 1021 | struct f_hid_opts *opts; |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 1022 | |
| 1023 | /* allocate and initialize one new instance */ |
Andrzej Pietrasiewicz | cb38253 | 2014-11-06 11:11:59 +0100 | [diff] [blame] | 1024 | hidg = kzalloc(sizeof(*hidg), GFP_KERNEL); |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 1025 | if (!hidg) |
Andrzej Pietrasiewicz | cb38253 | 2014-11-06 11:11:59 +0100 | [diff] [blame] | 1026 | return ERR_PTR(-ENOMEM); |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 1027 | |
Andrzej Pietrasiewicz | cb38253 | 2014-11-06 11:11:59 +0100 | [diff] [blame] | 1028 | opts = container_of(fi, struct f_hid_opts, func_inst); |
| 1029 | |
Andrzej Pietrasiewicz | 21a9476 | 2014-11-06 11:12:03 +0100 | [diff] [blame] | 1030 | mutex_lock(&opts->lock); |
| 1031 | ++opts->refcnt; |
| 1032 | |
Andrzej Pietrasiewicz | cb38253 | 2014-11-06 11:11:59 +0100 | [diff] [blame] | 1033 | hidg->minor = opts->minor; |
| 1034 | hidg->bInterfaceSubClass = opts->subclass; |
| 1035 | hidg->bInterfaceProtocol = opts->protocol; |
| 1036 | hidg->report_length = opts->report_length; |
| 1037 | hidg->report_desc_length = opts->report_desc_length; |
| 1038 | if (opts->report_desc) { |
| 1039 | hidg->report_desc = kmemdup(opts->report_desc, |
| 1040 | opts->report_desc_length, |
| 1041 | GFP_KERNEL); |
| 1042 | if (!hidg->report_desc) { |
| 1043 | kfree(hidg); |
Andrzej Pietrasiewicz | 21a9476 | 2014-11-06 11:12:03 +0100 | [diff] [blame] | 1044 | mutex_unlock(&opts->lock); |
Andrzej Pietrasiewicz | cb38253 | 2014-11-06 11:11:59 +0100 | [diff] [blame] | 1045 | return ERR_PTR(-ENOMEM); |
| 1046 | } |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 1047 | } |
| 1048 | |
Andrzej Pietrasiewicz | 21a9476 | 2014-11-06 11:12:03 +0100 | [diff] [blame] | 1049 | mutex_unlock(&opts->lock); |
| 1050 | |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 1051 | hidg->func.name = "hid"; |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 1052 | hidg->func.bind = hidg_bind; |
| 1053 | hidg->func.unbind = hidg_unbind; |
| 1054 | hidg->func.set_alt = hidg_set_alt; |
| 1055 | hidg->func.disable = hidg_disable; |
| 1056 | hidg->func.setup = hidg_setup; |
Andrzej Pietrasiewicz | cb38253 | 2014-11-06 11:11:59 +0100 | [diff] [blame] | 1057 | hidg->func.free_func = hidg_free; |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 1058 | |
Daniel Mack | 99c515005 | 2012-06-13 12:54:45 +0200 | [diff] [blame] | 1059 | /* this could me made configurable at some point */ |
| 1060 | hidg->qlen = 4; |
| 1061 | |
Andrzej Pietrasiewicz | cb38253 | 2014-11-06 11:11:59 +0100 | [diff] [blame] | 1062 | return &hidg->func; |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 1063 | } |
| 1064 | |
Andrzej Pietrasiewicz | cb38253 | 2014-11-06 11:11:59 +0100 | [diff] [blame] | 1065 | DECLARE_USB_FUNCTION_INIT(hid, hidg_alloc_inst, hidg_alloc); |
| 1066 | MODULE_LICENSE("GPL"); |
| 1067 | MODULE_AUTHOR("Fabien Chouteau"); |
| 1068 | |
Andrzej Pietrasiewicz | cb38253 | 2014-11-06 11:11:59 +0100 | [diff] [blame] | 1069 | int ghid_setup(struct usb_gadget *g, int count) |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 1070 | { |
| 1071 | int status; |
| 1072 | dev_t dev; |
| 1073 | |
| 1074 | hidg_class = class_create(THIS_MODULE, "hidg"); |
Andrzej Pietrasiewicz | 0652940 | 2014-11-06 11:11:56 +0100 | [diff] [blame] | 1075 | if (IS_ERR(hidg_class)) { |
Dan Carpenter | 0448d38 | 2014-11-13 09:20:59 +0300 | [diff] [blame] | 1076 | status = PTR_ERR(hidg_class); |
Andrzej Pietrasiewicz | 0652940 | 2014-11-06 11:11:56 +0100 | [diff] [blame] | 1077 | hidg_class = NULL; |
Dan Carpenter | 0448d38 | 2014-11-13 09:20:59 +0300 | [diff] [blame] | 1078 | return status; |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 1079 | } |
| 1080 | |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 1081 | status = alloc_chrdev_region(&dev, 0, count, "hidg"); |
Dan Carpenter | 0448d38 | 2014-11-13 09:20:59 +0300 | [diff] [blame] | 1082 | if (status) { |
| 1083 | class_destroy(hidg_class); |
| 1084 | hidg_class = NULL; |
| 1085 | return status; |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 1086 | } |
| 1087 | |
Dan Carpenter | 0448d38 | 2014-11-13 09:20:59 +0300 | [diff] [blame] | 1088 | major = MAJOR(dev); |
| 1089 | minors = count; |
| 1090 | |
| 1091 | return 0; |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 1092 | } |
| 1093 | |
| 1094 | void ghid_cleanup(void) |
| 1095 | { |
| 1096 | if (major) { |
| 1097 | unregister_chrdev_region(MKDEV(major, 0), minors); |
| 1098 | major = minors = 0; |
| 1099 | } |
| 1100 | |
| 1101 | class_destroy(hidg_class); |
| 1102 | hidg_class = NULL; |
| 1103 | } |