b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * f_hid.c -- USB HID function driver |
| 4 | * |
| 5 | * Copyright (C) 2010 Fabien Chouteau <fabien.chouteau@barco.com> |
| 6 | */ |
| 7 | |
| 8 | #include <linux/kernel.h> |
| 9 | #include <linux/module.h> |
| 10 | #include <linux/hid.h> |
| 11 | #include <linux/idr.h> |
| 12 | #include <linux/cdev.h> |
| 13 | #include <linux/mutex.h> |
| 14 | #include <linux/poll.h> |
| 15 | #include <linux/uaccess.h> |
| 16 | #include <linux/wait.h> |
| 17 | #include <linux/sched.h> |
| 18 | #include <linux/usb/g_hid.h> |
| 19 | |
| 20 | #include "u_f.h" |
| 21 | #include "u_hid.h" |
| 22 | |
| 23 | #define HIDG_MINORS 4 |
| 24 | |
| 25 | static int major, minors; |
| 26 | static struct class *hidg_class; |
| 27 | static DEFINE_IDA(hidg_ida); |
| 28 | static DEFINE_MUTEX(hidg_ida_lock); /* protects access to hidg_ida */ |
| 29 | |
| 30 | /*-------------------------------------------------------------------------*/ |
| 31 | /* HID gadget struct */ |
| 32 | |
| 33 | struct f_hidg_req_list { |
| 34 | struct usb_request *req; |
| 35 | unsigned int pos; |
| 36 | struct list_head list; |
| 37 | }; |
| 38 | |
| 39 | struct f_hidg { |
| 40 | /* configuration */ |
| 41 | unsigned char bInterfaceSubClass; |
| 42 | unsigned char bInterfaceProtocol; |
| 43 | unsigned char protocol; |
| 44 | unsigned char idle; |
| 45 | unsigned short report_desc_length; |
| 46 | char *report_desc; |
| 47 | unsigned short report_length; |
| 48 | /* |
| 49 | * use_out_ep - if true, the OUT Endpoint (interrupt out method) |
| 50 | * will be used to receive reports from the host |
| 51 | * using functions with the "intout" suffix. |
| 52 | * Otherwise, the OUT Endpoint will not be configured |
| 53 | * and the SETUP/SET_REPORT method ("ssreport" suffix) |
| 54 | * will be used to receive reports. |
| 55 | */ |
| 56 | bool use_out_ep; |
| 57 | |
| 58 | /* recv report */ |
| 59 | spinlock_t read_spinlock; |
| 60 | wait_queue_head_t read_queue; |
| 61 | /* recv report - interrupt out only (use_out_ep == 1) */ |
| 62 | struct list_head completed_out_req; |
| 63 | unsigned int qlen; |
| 64 | /* recv report - setup set_report only (use_out_ep == 0) */ |
| 65 | char *set_report_buf; |
| 66 | unsigned int set_report_length; |
| 67 | |
| 68 | /* send report */ |
| 69 | spinlock_t write_spinlock; |
| 70 | bool write_pending; |
| 71 | wait_queue_head_t write_queue; |
| 72 | struct usb_request *req; |
| 73 | |
| 74 | struct device dev; |
| 75 | struct cdev cdev; |
| 76 | struct usb_function func; |
| 77 | |
| 78 | struct usb_ep *in_ep; |
| 79 | struct usb_ep *out_ep; |
| 80 | }; |
| 81 | |
| 82 | static inline struct f_hidg *func_to_hidg(struct usb_function *f) |
| 83 | { |
| 84 | return container_of(f, struct f_hidg, func); |
| 85 | } |
| 86 | |
| 87 | static void hidg_release(struct device *dev) |
| 88 | { |
| 89 | struct f_hidg *hidg = container_of(dev, struct f_hidg, dev); |
| 90 | |
| 91 | kfree(hidg->report_desc); |
| 92 | kfree(hidg->set_report_buf); |
| 93 | kfree(hidg); |
| 94 | } |
| 95 | |
| 96 | /*-------------------------------------------------------------------------*/ |
| 97 | /* Static descriptors */ |
| 98 | |
| 99 | static struct usb_interface_descriptor hidg_interface_desc = { |
| 100 | .bLength = sizeof hidg_interface_desc, |
| 101 | .bDescriptorType = USB_DT_INTERFACE, |
| 102 | /* .bInterfaceNumber = DYNAMIC */ |
| 103 | .bAlternateSetting = 0, |
| 104 | /* .bNumEndpoints = DYNAMIC (depends on use_out_ep) */ |
| 105 | .bInterfaceClass = USB_CLASS_HID, |
| 106 | /* .bInterfaceSubClass = DYNAMIC */ |
| 107 | /* .bInterfaceProtocol = DYNAMIC */ |
| 108 | /* .iInterface = DYNAMIC */ |
| 109 | }; |
| 110 | |
| 111 | static struct hid_descriptor hidg_desc = { |
| 112 | .bLength = sizeof hidg_desc, |
| 113 | .bDescriptorType = HID_DT_HID, |
| 114 | .bcdHID = cpu_to_le16(0x0101), |
| 115 | .bCountryCode = 0x00, |
| 116 | .bNumDescriptors = 0x1, |
| 117 | /*.desc[0].bDescriptorType = DYNAMIC */ |
| 118 | /*.desc[0].wDescriptorLenght = DYNAMIC */ |
| 119 | }; |
| 120 | |
| 121 | /* Super-Speed Support */ |
| 122 | |
| 123 | static struct usb_endpoint_descriptor hidg_ss_in_ep_desc = { |
| 124 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 125 | .bDescriptorType = USB_DT_ENDPOINT, |
| 126 | .bEndpointAddress = USB_DIR_IN, |
| 127 | .bmAttributes = USB_ENDPOINT_XFER_INT, |
| 128 | /*.wMaxPacketSize = DYNAMIC */ |
| 129 | .bInterval = 4, /* FIXME: Add this field in the |
| 130 | * HID gadget configuration? |
| 131 | * (struct hidg_func_descriptor) |
| 132 | */ |
| 133 | }; |
| 134 | |
| 135 | static struct usb_ss_ep_comp_descriptor hidg_ss_in_comp_desc = { |
| 136 | .bLength = sizeof(hidg_ss_in_comp_desc), |
| 137 | .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, |
| 138 | |
| 139 | /* .bMaxBurst = 0, */ |
| 140 | /* .bmAttributes = 0, */ |
| 141 | /* .wBytesPerInterval = DYNAMIC */ |
| 142 | }; |
| 143 | |
| 144 | static struct usb_endpoint_descriptor hidg_ss_out_ep_desc = { |
| 145 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 146 | .bDescriptorType = USB_DT_ENDPOINT, |
| 147 | .bEndpointAddress = USB_DIR_OUT, |
| 148 | .bmAttributes = USB_ENDPOINT_XFER_INT, |
| 149 | /*.wMaxPacketSize = DYNAMIC */ |
| 150 | .bInterval = 4, /* FIXME: Add this field in the |
| 151 | * HID gadget configuration? |
| 152 | * (struct hidg_func_descriptor) |
| 153 | */ |
| 154 | }; |
| 155 | |
| 156 | static struct usb_ss_ep_comp_descriptor hidg_ss_out_comp_desc = { |
| 157 | .bLength = sizeof(hidg_ss_out_comp_desc), |
| 158 | .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, |
| 159 | |
| 160 | /* .bMaxBurst = 0, */ |
| 161 | /* .bmAttributes = 0, */ |
| 162 | /* .wBytesPerInterval = DYNAMIC */ |
| 163 | }; |
| 164 | |
| 165 | static struct usb_descriptor_header *hidg_ss_descriptors_intout[] = { |
| 166 | (struct usb_descriptor_header *)&hidg_interface_desc, |
| 167 | (struct usb_descriptor_header *)&hidg_desc, |
| 168 | (struct usb_descriptor_header *)&hidg_ss_in_ep_desc, |
| 169 | (struct usb_descriptor_header *)&hidg_ss_in_comp_desc, |
| 170 | (struct usb_descriptor_header *)&hidg_ss_out_ep_desc, |
| 171 | (struct usb_descriptor_header *)&hidg_ss_out_comp_desc, |
| 172 | NULL, |
| 173 | }; |
| 174 | |
| 175 | static struct usb_descriptor_header *hidg_ss_descriptors_ssreport[] = { |
| 176 | (struct usb_descriptor_header *)&hidg_interface_desc, |
| 177 | (struct usb_descriptor_header *)&hidg_desc, |
| 178 | (struct usb_descriptor_header *)&hidg_ss_in_ep_desc, |
| 179 | (struct usb_descriptor_header *)&hidg_ss_in_comp_desc, |
| 180 | NULL, |
| 181 | }; |
| 182 | |
| 183 | /* High-Speed Support */ |
| 184 | |
| 185 | static struct usb_endpoint_descriptor hidg_hs_in_ep_desc = { |
| 186 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 187 | .bDescriptorType = USB_DT_ENDPOINT, |
| 188 | .bEndpointAddress = USB_DIR_IN, |
| 189 | .bmAttributes = USB_ENDPOINT_XFER_INT, |
| 190 | /*.wMaxPacketSize = DYNAMIC */ |
| 191 | .bInterval = 4, /* FIXME: Add this field in the |
| 192 | * HID gadget configuration? |
| 193 | * (struct hidg_func_descriptor) |
| 194 | */ |
| 195 | }; |
| 196 | |
| 197 | static struct usb_endpoint_descriptor hidg_hs_out_ep_desc = { |
| 198 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 199 | .bDescriptorType = USB_DT_ENDPOINT, |
| 200 | .bEndpointAddress = USB_DIR_OUT, |
| 201 | .bmAttributes = USB_ENDPOINT_XFER_INT, |
| 202 | /*.wMaxPacketSize = DYNAMIC */ |
| 203 | .bInterval = 4, /* FIXME: Add this field in the |
| 204 | * HID gadget configuration? |
| 205 | * (struct hidg_func_descriptor) |
| 206 | */ |
| 207 | }; |
| 208 | |
| 209 | static struct usb_descriptor_header *hidg_hs_descriptors_intout[] = { |
| 210 | (struct usb_descriptor_header *)&hidg_interface_desc, |
| 211 | (struct usb_descriptor_header *)&hidg_desc, |
| 212 | (struct usb_descriptor_header *)&hidg_hs_in_ep_desc, |
| 213 | (struct usb_descriptor_header *)&hidg_hs_out_ep_desc, |
| 214 | NULL, |
| 215 | }; |
| 216 | |
| 217 | static struct usb_descriptor_header *hidg_hs_descriptors_ssreport[] = { |
| 218 | (struct usb_descriptor_header *)&hidg_interface_desc, |
| 219 | (struct usb_descriptor_header *)&hidg_desc, |
| 220 | (struct usb_descriptor_header *)&hidg_hs_in_ep_desc, |
| 221 | NULL, |
| 222 | }; |
| 223 | |
| 224 | /* Full-Speed Support */ |
| 225 | |
| 226 | static struct usb_endpoint_descriptor hidg_fs_in_ep_desc = { |
| 227 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 228 | .bDescriptorType = USB_DT_ENDPOINT, |
| 229 | .bEndpointAddress = USB_DIR_IN, |
| 230 | .bmAttributes = USB_ENDPOINT_XFER_INT, |
| 231 | /*.wMaxPacketSize = DYNAMIC */ |
| 232 | .bInterval = 10, /* FIXME: Add this field in the |
| 233 | * HID gadget configuration? |
| 234 | * (struct hidg_func_descriptor) |
| 235 | */ |
| 236 | }; |
| 237 | |
| 238 | static struct usb_endpoint_descriptor hidg_fs_out_ep_desc = { |
| 239 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 240 | .bDescriptorType = USB_DT_ENDPOINT, |
| 241 | .bEndpointAddress = USB_DIR_OUT, |
| 242 | .bmAttributes = USB_ENDPOINT_XFER_INT, |
| 243 | /*.wMaxPacketSize = DYNAMIC */ |
| 244 | .bInterval = 10, /* FIXME: Add this field in the |
| 245 | * HID gadget configuration? |
| 246 | * (struct hidg_func_descriptor) |
| 247 | */ |
| 248 | }; |
| 249 | |
| 250 | static struct usb_descriptor_header *hidg_fs_descriptors_intout[] = { |
| 251 | (struct usb_descriptor_header *)&hidg_interface_desc, |
| 252 | (struct usb_descriptor_header *)&hidg_desc, |
| 253 | (struct usb_descriptor_header *)&hidg_fs_in_ep_desc, |
| 254 | (struct usb_descriptor_header *)&hidg_fs_out_ep_desc, |
| 255 | NULL, |
| 256 | }; |
| 257 | |
| 258 | static struct usb_descriptor_header *hidg_fs_descriptors_ssreport[] = { |
| 259 | (struct usb_descriptor_header *)&hidg_interface_desc, |
| 260 | (struct usb_descriptor_header *)&hidg_desc, |
| 261 | (struct usb_descriptor_header *)&hidg_fs_in_ep_desc, |
| 262 | NULL, |
| 263 | }; |
| 264 | |
| 265 | /*-------------------------------------------------------------------------*/ |
| 266 | /* Strings */ |
| 267 | |
| 268 | #define CT_FUNC_HID_IDX 0 |
| 269 | |
| 270 | static struct usb_string ct_func_string_defs[] = { |
| 271 | [CT_FUNC_HID_IDX].s = "HID Interface", |
| 272 | {}, /* end of list */ |
| 273 | }; |
| 274 | |
| 275 | static struct usb_gadget_strings ct_func_string_table = { |
| 276 | .language = 0x0409, /* en-US */ |
| 277 | .strings = ct_func_string_defs, |
| 278 | }; |
| 279 | |
| 280 | static struct usb_gadget_strings *ct_func_strings[] = { |
| 281 | &ct_func_string_table, |
| 282 | NULL, |
| 283 | }; |
| 284 | |
| 285 | /*-------------------------------------------------------------------------*/ |
| 286 | /* Char Device */ |
| 287 | |
| 288 | static ssize_t f_hidg_intout_read(struct file *file, char __user *buffer, |
| 289 | size_t count, loff_t *ptr) |
| 290 | { |
| 291 | struct f_hidg *hidg = file->private_data; |
| 292 | struct f_hidg_req_list *list; |
| 293 | struct usb_request *req; |
| 294 | unsigned long flags; |
| 295 | int ret; |
| 296 | |
| 297 | if (!count) |
| 298 | return 0; |
| 299 | |
| 300 | if (!access_ok(buffer, count)) |
| 301 | return -EFAULT; |
| 302 | |
| 303 | spin_lock_irqsave(&hidg->read_spinlock, flags); |
| 304 | |
| 305 | #define READ_COND_INTOUT (!list_empty(&hidg->completed_out_req)) |
| 306 | |
| 307 | /* wait for at least one buffer to complete */ |
| 308 | while (!READ_COND_INTOUT) { |
| 309 | spin_unlock_irqrestore(&hidg->read_spinlock, flags); |
| 310 | if (file->f_flags & O_NONBLOCK) |
| 311 | return -EAGAIN; |
| 312 | |
| 313 | if (wait_event_interruptible(hidg->read_queue, READ_COND_INTOUT)) |
| 314 | return -ERESTARTSYS; |
| 315 | |
| 316 | spin_lock_irqsave(&hidg->read_spinlock, flags); |
| 317 | } |
| 318 | |
| 319 | /* pick the first one */ |
| 320 | list = list_first_entry(&hidg->completed_out_req, |
| 321 | struct f_hidg_req_list, list); |
| 322 | |
| 323 | /* |
| 324 | * Remove this from list to protect it from beign free() |
| 325 | * while host disables our function |
| 326 | */ |
| 327 | list_del(&list->list); |
| 328 | |
| 329 | req = list->req; |
| 330 | count = min_t(unsigned int, count, req->actual - list->pos); |
| 331 | spin_unlock_irqrestore(&hidg->read_spinlock, flags); |
| 332 | |
| 333 | /* copy to user outside spinlock */ |
| 334 | count -= copy_to_user(buffer, req->buf + list->pos, count); |
| 335 | list->pos += count; |
| 336 | |
| 337 | /* |
| 338 | * if this request is completely handled and transfered to |
| 339 | * userspace, remove its entry from the list and requeue it |
| 340 | * again. Otherwise, we will revisit it again upon the next |
| 341 | * call, taking into account its current read position. |
| 342 | */ |
| 343 | if (list->pos == req->actual) { |
| 344 | kfree(list); |
| 345 | |
| 346 | req->length = hidg->report_length; |
| 347 | ret = usb_ep_queue(hidg->out_ep, req, GFP_KERNEL); |
| 348 | if (ret < 0) { |
| 349 | free_ep_req(hidg->out_ep, req); |
| 350 | return ret; |
| 351 | } |
| 352 | } else { |
| 353 | spin_lock_irqsave(&hidg->read_spinlock, flags); |
| 354 | list_add(&list->list, &hidg->completed_out_req); |
| 355 | spin_unlock_irqrestore(&hidg->read_spinlock, flags); |
| 356 | |
| 357 | wake_up(&hidg->read_queue); |
| 358 | } |
| 359 | |
| 360 | return count; |
| 361 | } |
| 362 | |
| 363 | #define READ_COND_SSREPORT (hidg->set_report_buf != NULL) |
| 364 | |
| 365 | static ssize_t f_hidg_ssreport_read(struct file *file, char __user *buffer, |
| 366 | size_t count, loff_t *ptr) |
| 367 | { |
| 368 | struct f_hidg *hidg = file->private_data; |
| 369 | char *tmp_buf = NULL; |
| 370 | unsigned long flags; |
| 371 | |
| 372 | if (!count) |
| 373 | return 0; |
| 374 | |
| 375 | spin_lock_irqsave(&hidg->read_spinlock, flags); |
| 376 | |
| 377 | while (!READ_COND_SSREPORT) { |
| 378 | spin_unlock_irqrestore(&hidg->read_spinlock, flags); |
| 379 | if (file->f_flags & O_NONBLOCK) |
| 380 | return -EAGAIN; |
| 381 | |
| 382 | if (wait_event_interruptible(hidg->read_queue, READ_COND_SSREPORT)) |
| 383 | return -ERESTARTSYS; |
| 384 | |
| 385 | spin_lock_irqsave(&hidg->read_spinlock, flags); |
| 386 | } |
| 387 | |
| 388 | count = min_t(unsigned int, count, hidg->set_report_length); |
| 389 | tmp_buf = hidg->set_report_buf; |
| 390 | hidg->set_report_buf = NULL; |
| 391 | |
| 392 | spin_unlock_irqrestore(&hidg->read_spinlock, flags); |
| 393 | |
| 394 | if (tmp_buf != NULL) { |
| 395 | count -= copy_to_user(buffer, tmp_buf, count); |
| 396 | kfree(tmp_buf); |
| 397 | } else { |
| 398 | count = -ENOMEM; |
| 399 | } |
| 400 | |
| 401 | wake_up(&hidg->read_queue); |
| 402 | |
| 403 | return count; |
| 404 | } |
| 405 | |
| 406 | static ssize_t f_hidg_read(struct file *file, char __user *buffer, |
| 407 | size_t count, loff_t *ptr) |
| 408 | { |
| 409 | struct f_hidg *hidg = file->private_data; |
| 410 | |
| 411 | if (hidg->use_out_ep) |
| 412 | return f_hidg_intout_read(file, buffer, count, ptr); |
| 413 | else |
| 414 | return f_hidg_ssreport_read(file, buffer, count, ptr); |
| 415 | } |
| 416 | |
| 417 | static void f_hidg_req_complete(struct usb_ep *ep, struct usb_request *req) |
| 418 | { |
| 419 | struct f_hidg *hidg = (struct f_hidg *)ep->driver_data; |
| 420 | unsigned long flags; |
| 421 | |
| 422 | if (req->status != 0) { |
| 423 | ERROR(hidg->func.config->cdev, |
| 424 | "End Point Request ERROR: %d\n", req->status); |
| 425 | } |
| 426 | |
| 427 | spin_lock_irqsave(&hidg->write_spinlock, flags); |
| 428 | hidg->write_pending = 0; |
| 429 | spin_unlock_irqrestore(&hidg->write_spinlock, flags); |
| 430 | wake_up(&hidg->write_queue); |
| 431 | } |
| 432 | |
| 433 | static ssize_t f_hidg_write(struct file *file, const char __user *buffer, |
| 434 | size_t count, loff_t *offp) |
| 435 | { |
| 436 | struct f_hidg *hidg = file->private_data; |
| 437 | struct usb_request *req; |
| 438 | unsigned long flags; |
| 439 | ssize_t status = -ENOMEM; |
| 440 | |
| 441 | if (!access_ok(buffer, count)) |
| 442 | return -EFAULT; |
| 443 | |
| 444 | spin_lock_irqsave(&hidg->write_spinlock, flags); |
| 445 | |
| 446 | if (!hidg->req) { |
| 447 | spin_unlock_irqrestore(&hidg->write_spinlock, flags); |
| 448 | return -ESHUTDOWN; |
| 449 | } |
| 450 | |
| 451 | #define WRITE_COND (!hidg->write_pending) |
| 452 | try_again: |
| 453 | /* write queue */ |
| 454 | while (!WRITE_COND) { |
| 455 | spin_unlock_irqrestore(&hidg->write_spinlock, flags); |
| 456 | if (file->f_flags & O_NONBLOCK) |
| 457 | return -EAGAIN; |
| 458 | |
| 459 | if (wait_event_interruptible_exclusive( |
| 460 | hidg->write_queue, WRITE_COND)) |
| 461 | return -ERESTARTSYS; |
| 462 | |
| 463 | spin_lock_irqsave(&hidg->write_spinlock, flags); |
| 464 | } |
| 465 | |
| 466 | hidg->write_pending = 1; |
| 467 | req = hidg->req; |
| 468 | count = min_t(unsigned, count, hidg->report_length); |
| 469 | |
| 470 | spin_unlock_irqrestore(&hidg->write_spinlock, flags); |
| 471 | |
| 472 | if (!req) { |
| 473 | ERROR(hidg->func.config->cdev, "hidg->req is NULL\n"); |
| 474 | status = -ESHUTDOWN; |
| 475 | goto release_write_pending; |
| 476 | } |
| 477 | |
| 478 | status = copy_from_user(req->buf, buffer, count); |
| 479 | if (status != 0) { |
| 480 | ERROR(hidg->func.config->cdev, |
| 481 | "copy_from_user error\n"); |
| 482 | status = -EINVAL; |
| 483 | goto release_write_pending; |
| 484 | } |
| 485 | |
| 486 | spin_lock_irqsave(&hidg->write_spinlock, flags); |
| 487 | |
| 488 | /* when our function has been disabled by host */ |
| 489 | if (!hidg->req) { |
| 490 | free_ep_req(hidg->in_ep, req); |
| 491 | /* |
| 492 | * TODO |
| 493 | * Should we fail with error here? |
| 494 | */ |
| 495 | goto try_again; |
| 496 | } |
| 497 | |
| 498 | req->status = 0; |
| 499 | req->zero = 0; |
| 500 | req->length = count; |
| 501 | req->complete = f_hidg_req_complete; |
| 502 | req->context = hidg; |
| 503 | |
| 504 | spin_unlock_irqrestore(&hidg->write_spinlock, flags); |
| 505 | |
| 506 | if (!hidg->in_ep->enabled) { |
| 507 | ERROR(hidg->func.config->cdev, "in_ep is disabled\n"); |
| 508 | status = -ESHUTDOWN; |
| 509 | goto release_write_pending; |
| 510 | } |
| 511 | |
| 512 | status = usb_ep_queue(hidg->in_ep, req, GFP_ATOMIC); |
| 513 | if (status < 0) |
| 514 | goto release_write_pending; |
| 515 | else |
| 516 | status = count; |
| 517 | |
| 518 | return status; |
| 519 | release_write_pending: |
| 520 | spin_lock_irqsave(&hidg->write_spinlock, flags); |
| 521 | hidg->write_pending = 0; |
| 522 | spin_unlock_irqrestore(&hidg->write_spinlock, flags); |
| 523 | |
| 524 | wake_up(&hidg->write_queue); |
| 525 | |
| 526 | return status; |
| 527 | } |
| 528 | |
| 529 | static __poll_t f_hidg_poll(struct file *file, poll_table *wait) |
| 530 | { |
| 531 | struct f_hidg *hidg = file->private_data; |
| 532 | __poll_t ret = 0; |
| 533 | |
| 534 | poll_wait(file, &hidg->read_queue, wait); |
| 535 | poll_wait(file, &hidg->write_queue, wait); |
| 536 | |
| 537 | if (WRITE_COND) |
| 538 | ret |= EPOLLOUT | EPOLLWRNORM; |
| 539 | |
| 540 | if (hidg->use_out_ep) { |
| 541 | if (READ_COND_INTOUT) |
| 542 | ret |= EPOLLIN | EPOLLRDNORM; |
| 543 | } else { |
| 544 | if (READ_COND_SSREPORT) |
| 545 | ret |= EPOLLIN | EPOLLRDNORM; |
| 546 | } |
| 547 | |
| 548 | return ret; |
| 549 | } |
| 550 | |
| 551 | #undef WRITE_COND |
| 552 | #undef READ_COND_SSREPORT |
| 553 | #undef READ_COND_INTOUT |
| 554 | |
| 555 | static int f_hidg_release(struct inode *inode, struct file *fd) |
| 556 | { |
| 557 | fd->private_data = NULL; |
| 558 | return 0; |
| 559 | } |
| 560 | |
| 561 | static int f_hidg_open(struct inode *inode, struct file *fd) |
| 562 | { |
| 563 | struct f_hidg *hidg = |
| 564 | container_of(inode->i_cdev, struct f_hidg, cdev); |
| 565 | |
| 566 | fd->private_data = hidg; |
| 567 | |
| 568 | return 0; |
| 569 | } |
| 570 | |
| 571 | /*-------------------------------------------------------------------------*/ |
| 572 | /* usb_function */ |
| 573 | |
| 574 | static inline struct usb_request *hidg_alloc_ep_req(struct usb_ep *ep, |
| 575 | unsigned length) |
| 576 | { |
| 577 | return alloc_ep_req(ep, length); |
| 578 | } |
| 579 | |
| 580 | static void hidg_intout_complete(struct usb_ep *ep, struct usb_request *req) |
| 581 | { |
| 582 | struct f_hidg *hidg = (struct f_hidg *) req->context; |
| 583 | struct usb_composite_dev *cdev = hidg->func.config->cdev; |
| 584 | struct f_hidg_req_list *req_list; |
| 585 | unsigned long flags; |
| 586 | |
| 587 | switch (req->status) { |
| 588 | case 0: |
| 589 | req_list = kzalloc(sizeof(*req_list), GFP_ATOMIC); |
| 590 | if (!req_list) { |
| 591 | ERROR(cdev, "Unable to allocate mem for req_list\n"); |
| 592 | goto free_req; |
| 593 | } |
| 594 | |
| 595 | req_list->req = req; |
| 596 | |
| 597 | spin_lock_irqsave(&hidg->read_spinlock, flags); |
| 598 | list_add_tail(&req_list->list, &hidg->completed_out_req); |
| 599 | spin_unlock_irqrestore(&hidg->read_spinlock, flags); |
| 600 | |
| 601 | wake_up(&hidg->read_queue); |
| 602 | break; |
| 603 | default: |
| 604 | ERROR(cdev, "Set report failed %d\n", req->status); |
| 605 | /* FALLTHROUGH */ |
| 606 | case -ECONNABORTED: /* hardware forced ep reset */ |
| 607 | case -ECONNRESET: /* request dequeued */ |
| 608 | case -ESHUTDOWN: /* disconnect from host */ |
| 609 | free_req: |
| 610 | free_ep_req(ep, req); |
| 611 | return; |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | static void hidg_ssreport_complete(struct usb_ep *ep, struct usb_request *req) |
| 616 | { |
| 617 | struct f_hidg *hidg = (struct f_hidg *)req->context; |
| 618 | struct usb_composite_dev *cdev = hidg->func.config->cdev; |
| 619 | char *new_buf = NULL; |
| 620 | unsigned long flags; |
| 621 | |
| 622 | if (req->status != 0 || req->buf == NULL || req->actual == 0) { |
| 623 | ERROR(cdev, |
| 624 | "%s FAILED: status=%d, buf=%p, actual=%d\n", |
| 625 | __func__, req->status, req->buf, req->actual); |
| 626 | return; |
| 627 | } |
| 628 | |
| 629 | spin_lock_irqsave(&hidg->read_spinlock, flags); |
| 630 | |
| 631 | new_buf = krealloc(hidg->set_report_buf, req->actual, GFP_ATOMIC); |
| 632 | if (new_buf == NULL) { |
| 633 | spin_unlock_irqrestore(&hidg->read_spinlock, flags); |
| 634 | return; |
| 635 | } |
| 636 | hidg->set_report_buf = new_buf; |
| 637 | |
| 638 | hidg->set_report_length = req->actual; |
| 639 | memcpy(hidg->set_report_buf, req->buf, req->actual); |
| 640 | |
| 641 | spin_unlock_irqrestore(&hidg->read_spinlock, flags); |
| 642 | |
| 643 | wake_up(&hidg->read_queue); |
| 644 | } |
| 645 | |
| 646 | static int hidg_setup(struct usb_function *f, |
| 647 | const struct usb_ctrlrequest *ctrl) |
| 648 | { |
| 649 | struct f_hidg *hidg = func_to_hidg(f); |
| 650 | struct usb_composite_dev *cdev = f->config->cdev; |
| 651 | struct usb_request *req = cdev->req; |
| 652 | int status = 0; |
| 653 | __u16 value, length; |
| 654 | |
| 655 | value = __le16_to_cpu(ctrl->wValue); |
| 656 | length = __le16_to_cpu(ctrl->wLength); |
| 657 | |
| 658 | VDBG(cdev, |
| 659 | "%s crtl_request : bRequestType:0x%x bRequest:0x%x Value:0x%x\n", |
| 660 | __func__, ctrl->bRequestType, ctrl->bRequest, value); |
| 661 | |
| 662 | switch ((ctrl->bRequestType << 8) | ctrl->bRequest) { |
| 663 | case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8 |
| 664 | | HID_REQ_GET_REPORT): |
| 665 | VDBG(cdev, "get_report\n"); |
| 666 | |
| 667 | /* send an empty report */ |
| 668 | length = min_t(unsigned, length, hidg->report_length); |
| 669 | memset(req->buf, 0x0, length); |
| 670 | |
| 671 | goto respond; |
| 672 | break; |
| 673 | |
| 674 | case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8 |
| 675 | | HID_REQ_GET_PROTOCOL): |
| 676 | VDBG(cdev, "get_protocol\n"); |
| 677 | length = min_t(unsigned int, length, 1); |
| 678 | ((u8 *) req->buf)[0] = hidg->protocol; |
| 679 | goto respond; |
| 680 | break; |
| 681 | |
| 682 | case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8 |
| 683 | | HID_REQ_GET_IDLE): |
| 684 | VDBG(cdev, "get_idle\n"); |
| 685 | length = min_t(unsigned int, length, 1); |
| 686 | ((u8 *) req->buf)[0] = hidg->idle; |
| 687 | goto respond; |
| 688 | break; |
| 689 | |
| 690 | case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8 |
| 691 | | HID_REQ_SET_REPORT): |
| 692 | VDBG(cdev, "set_report | wLength=%d\n", ctrl->wLength); |
| 693 | if (hidg->use_out_ep) |
| 694 | goto stall; |
| 695 | req->complete = hidg_ssreport_complete; |
| 696 | req->context = hidg; |
| 697 | goto respond; |
| 698 | break; |
| 699 | |
| 700 | case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8 |
| 701 | | HID_REQ_SET_PROTOCOL): |
| 702 | VDBG(cdev, "set_protocol\n"); |
| 703 | if (value > HID_REPORT_PROTOCOL) |
| 704 | goto stall; |
| 705 | length = 0; |
| 706 | /* |
| 707 | * We assume that programs implementing the Boot protocol |
| 708 | * are also compatible with the Report Protocol |
| 709 | */ |
| 710 | if (hidg->bInterfaceSubClass == USB_INTERFACE_SUBCLASS_BOOT) { |
| 711 | hidg->protocol = value; |
| 712 | goto respond; |
| 713 | } |
| 714 | goto stall; |
| 715 | break; |
| 716 | |
| 717 | case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8 |
| 718 | | HID_REQ_SET_IDLE): |
| 719 | VDBG(cdev, "set_idle\n"); |
| 720 | length = 0; |
| 721 | hidg->idle = value >> 8; |
| 722 | goto respond; |
| 723 | break; |
| 724 | |
| 725 | case ((USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_INTERFACE) << 8 |
| 726 | | USB_REQ_GET_DESCRIPTOR): |
| 727 | switch (value >> 8) { |
| 728 | case HID_DT_HID: |
| 729 | { |
| 730 | struct hid_descriptor hidg_desc_copy = hidg_desc; |
| 731 | |
| 732 | VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: HID\n"); |
| 733 | hidg_desc_copy.desc[0].bDescriptorType = HID_DT_REPORT; |
| 734 | hidg_desc_copy.desc[0].wDescriptorLength = |
| 735 | cpu_to_le16(hidg->report_desc_length); |
| 736 | |
| 737 | length = min_t(unsigned short, length, |
| 738 | hidg_desc_copy.bLength); |
| 739 | memcpy(req->buf, &hidg_desc_copy, length); |
| 740 | goto respond; |
| 741 | break; |
| 742 | } |
| 743 | case HID_DT_REPORT: |
| 744 | VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: REPORT\n"); |
| 745 | length = min_t(unsigned short, length, |
| 746 | hidg->report_desc_length); |
| 747 | memcpy(req->buf, hidg->report_desc, length); |
| 748 | goto respond; |
| 749 | break; |
| 750 | |
| 751 | default: |
| 752 | VDBG(cdev, "Unknown descriptor request 0x%x\n", |
| 753 | value >> 8); |
| 754 | goto stall; |
| 755 | break; |
| 756 | } |
| 757 | break; |
| 758 | |
| 759 | default: |
| 760 | VDBG(cdev, "Unknown request 0x%x\n", |
| 761 | ctrl->bRequest); |
| 762 | goto stall; |
| 763 | break; |
| 764 | } |
| 765 | |
| 766 | stall: |
| 767 | return -EOPNOTSUPP; |
| 768 | |
| 769 | respond: |
| 770 | req->zero = 0; |
| 771 | req->length = length; |
| 772 | status = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC); |
| 773 | if (status < 0) |
| 774 | ERROR(cdev, "usb_ep_queue error on ep0 %d\n", value); |
| 775 | return status; |
| 776 | } |
| 777 | |
| 778 | static void hidg_disable(struct usb_function *f) |
| 779 | { |
| 780 | struct f_hidg *hidg = func_to_hidg(f); |
| 781 | struct f_hidg_req_list *list, *next; |
| 782 | unsigned long flags; |
| 783 | |
| 784 | usb_ep_disable(hidg->in_ep); |
| 785 | |
| 786 | if (hidg->out_ep) { |
| 787 | usb_ep_disable(hidg->out_ep); |
| 788 | |
| 789 | spin_lock_irqsave(&hidg->read_spinlock, flags); |
| 790 | list_for_each_entry_safe(list, next, &hidg->completed_out_req, list) { |
| 791 | free_ep_req(hidg->out_ep, list->req); |
| 792 | list_del(&list->list); |
| 793 | kfree(list); |
| 794 | } |
| 795 | spin_unlock_irqrestore(&hidg->read_spinlock, flags); |
| 796 | } |
| 797 | |
| 798 | spin_lock_irqsave(&hidg->write_spinlock, flags); |
| 799 | if (!hidg->write_pending) { |
| 800 | free_ep_req(hidg->in_ep, hidg->req); |
| 801 | hidg->write_pending = 1; |
| 802 | } |
| 803 | |
| 804 | hidg->req = NULL; |
| 805 | spin_unlock_irqrestore(&hidg->write_spinlock, flags); |
| 806 | } |
| 807 | |
| 808 | static int hidg_set_alt(struct usb_function *f, unsigned intf, unsigned alt) |
| 809 | { |
| 810 | struct usb_composite_dev *cdev = f->config->cdev; |
| 811 | struct f_hidg *hidg = func_to_hidg(f); |
| 812 | struct usb_request *req_in = NULL; |
| 813 | unsigned long flags; |
| 814 | int i, status = 0; |
| 815 | |
| 816 | VDBG(cdev, "hidg_set_alt intf:%d alt:%d\n", intf, alt); |
| 817 | |
| 818 | if (hidg->in_ep != NULL) { |
| 819 | /* restart endpoint */ |
| 820 | usb_ep_disable(hidg->in_ep); |
| 821 | |
| 822 | status = config_ep_by_speed(f->config->cdev->gadget, f, |
| 823 | hidg->in_ep); |
| 824 | if (status) { |
| 825 | ERROR(cdev, "config_ep_by_speed FAILED!\n"); |
| 826 | goto fail; |
| 827 | } |
| 828 | status = usb_ep_enable(hidg->in_ep); |
| 829 | if (status < 0) { |
| 830 | ERROR(cdev, "Enable IN endpoint FAILED!\n"); |
| 831 | goto fail; |
| 832 | } |
| 833 | hidg->in_ep->driver_data = hidg; |
| 834 | |
| 835 | req_in = hidg_alloc_ep_req(hidg->in_ep, hidg->report_length); |
| 836 | if (!req_in) { |
| 837 | status = -ENOMEM; |
| 838 | goto disable_ep_in; |
| 839 | } |
| 840 | } |
| 841 | |
| 842 | if (hidg->use_out_ep && hidg->out_ep != NULL) { |
| 843 | /* restart endpoint */ |
| 844 | usb_ep_disable(hidg->out_ep); |
| 845 | |
| 846 | status = config_ep_by_speed(f->config->cdev->gadget, f, |
| 847 | hidg->out_ep); |
| 848 | if (status) { |
| 849 | ERROR(cdev, "config_ep_by_speed FAILED!\n"); |
| 850 | goto free_req_in; |
| 851 | } |
| 852 | status = usb_ep_enable(hidg->out_ep); |
| 853 | if (status < 0) { |
| 854 | ERROR(cdev, "Enable OUT endpoint FAILED!\n"); |
| 855 | goto free_req_in; |
| 856 | } |
| 857 | hidg->out_ep->driver_data = hidg; |
| 858 | |
| 859 | /* |
| 860 | * allocate a bunch of read buffers and queue them all at once. |
| 861 | */ |
| 862 | for (i = 0; i < hidg->qlen && status == 0; i++) { |
| 863 | struct usb_request *req = |
| 864 | hidg_alloc_ep_req(hidg->out_ep, |
| 865 | hidg->report_length); |
| 866 | if (req) { |
| 867 | req->complete = hidg_intout_complete; |
| 868 | req->context = hidg; |
| 869 | status = usb_ep_queue(hidg->out_ep, req, |
| 870 | GFP_ATOMIC); |
| 871 | if (status) { |
| 872 | ERROR(cdev, "%s queue req --> %d\n", |
| 873 | hidg->out_ep->name, status); |
| 874 | free_ep_req(hidg->out_ep, req); |
| 875 | } |
| 876 | } else { |
| 877 | status = -ENOMEM; |
| 878 | goto disable_out_ep; |
| 879 | } |
| 880 | } |
| 881 | } |
| 882 | |
| 883 | if (hidg->in_ep != NULL) { |
| 884 | spin_lock_irqsave(&hidg->write_spinlock, flags); |
| 885 | hidg->req = req_in; |
| 886 | hidg->write_pending = 0; |
| 887 | spin_unlock_irqrestore(&hidg->write_spinlock, flags); |
| 888 | |
| 889 | wake_up(&hidg->write_queue); |
| 890 | } |
| 891 | return 0; |
| 892 | disable_out_ep: |
| 893 | if (hidg->out_ep) |
| 894 | usb_ep_disable(hidg->out_ep); |
| 895 | free_req_in: |
| 896 | if (req_in) |
| 897 | free_ep_req(hidg->in_ep, req_in); |
| 898 | |
| 899 | disable_ep_in: |
| 900 | if (hidg->in_ep) |
| 901 | usb_ep_disable(hidg->in_ep); |
| 902 | |
| 903 | fail: |
| 904 | return status; |
| 905 | } |
| 906 | |
| 907 | static const struct file_operations f_hidg_fops = { |
| 908 | .owner = THIS_MODULE, |
| 909 | .open = f_hidg_open, |
| 910 | .release = f_hidg_release, |
| 911 | .write = f_hidg_write, |
| 912 | .read = f_hidg_read, |
| 913 | .poll = f_hidg_poll, |
| 914 | .llseek = noop_llseek, |
| 915 | }; |
| 916 | |
| 917 | static int hidg_bind(struct usb_configuration *c, struct usb_function *f) |
| 918 | { |
| 919 | struct usb_ep *ep; |
| 920 | struct f_hidg *hidg = func_to_hidg(f); |
| 921 | struct usb_string *us; |
| 922 | int status; |
| 923 | |
| 924 | /* maybe allocate device-global string IDs, and patch descriptors */ |
| 925 | us = usb_gstrings_attach(c->cdev, ct_func_strings, |
| 926 | ARRAY_SIZE(ct_func_string_defs)); |
| 927 | if (IS_ERR(us)) |
| 928 | return PTR_ERR(us); |
| 929 | hidg_interface_desc.iInterface = us[CT_FUNC_HID_IDX].id; |
| 930 | |
| 931 | /* allocate instance-specific interface IDs, and patch descriptors */ |
| 932 | status = usb_interface_id(c, f); |
| 933 | if (status < 0) |
| 934 | goto fail; |
| 935 | hidg_interface_desc.bInterfaceNumber = status; |
| 936 | |
| 937 | /* allocate instance-specific endpoints */ |
| 938 | status = -ENODEV; |
| 939 | ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_in_ep_desc); |
| 940 | if (!ep) |
| 941 | goto fail; |
| 942 | hidg->in_ep = ep; |
| 943 | |
| 944 | hidg->out_ep = NULL; |
| 945 | if (hidg->use_out_ep) { |
| 946 | ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_out_ep_desc); |
| 947 | if (!ep) |
| 948 | goto fail; |
| 949 | hidg->out_ep = ep; |
| 950 | } |
| 951 | |
| 952 | /* used only if use_out_ep == 1 */ |
| 953 | hidg->set_report_buf = NULL; |
| 954 | |
| 955 | /* set descriptor dynamic values */ |
| 956 | hidg_interface_desc.bInterfaceSubClass = hidg->bInterfaceSubClass; |
| 957 | hidg_interface_desc.bInterfaceProtocol = hidg->bInterfaceProtocol; |
| 958 | hidg_interface_desc.bNumEndpoints = hidg->use_out_ep ? 2 : 1; |
| 959 | hidg->protocol = HID_REPORT_PROTOCOL; |
| 960 | hidg->idle = 1; |
| 961 | hidg_ss_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length); |
| 962 | hidg_ss_in_comp_desc.wBytesPerInterval = |
| 963 | cpu_to_le16(hidg->report_length); |
| 964 | hidg_hs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length); |
| 965 | hidg_fs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length); |
| 966 | hidg_ss_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length); |
| 967 | hidg_ss_out_comp_desc.wBytesPerInterval = |
| 968 | cpu_to_le16(hidg->report_length); |
| 969 | hidg_hs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length); |
| 970 | hidg_fs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length); |
| 971 | /* |
| 972 | * We can use hidg_desc struct here but we should not relay |
| 973 | * that its content won't change after returning from this function. |
| 974 | */ |
| 975 | hidg_desc.desc[0].bDescriptorType = HID_DT_REPORT; |
| 976 | hidg_desc.desc[0].wDescriptorLength = |
| 977 | cpu_to_le16(hidg->report_desc_length); |
| 978 | |
| 979 | hidg_hs_in_ep_desc.bEndpointAddress = |
| 980 | hidg_fs_in_ep_desc.bEndpointAddress; |
| 981 | hidg_hs_out_ep_desc.bEndpointAddress = |
| 982 | hidg_fs_out_ep_desc.bEndpointAddress; |
| 983 | |
| 984 | hidg_ss_in_ep_desc.bEndpointAddress = |
| 985 | hidg_fs_in_ep_desc.bEndpointAddress; |
| 986 | hidg_ss_out_ep_desc.bEndpointAddress = |
| 987 | hidg_fs_out_ep_desc.bEndpointAddress; |
| 988 | |
| 989 | if (hidg->use_out_ep) |
| 990 | status = usb_assign_descriptors(f, |
| 991 | hidg_fs_descriptors_intout, |
| 992 | hidg_hs_descriptors_intout, |
| 993 | hidg_ss_descriptors_intout, |
| 994 | hidg_ss_descriptors_intout); |
| 995 | else |
| 996 | status = usb_assign_descriptors(f, |
| 997 | hidg_fs_descriptors_ssreport, |
| 998 | hidg_hs_descriptors_ssreport, |
| 999 | hidg_ss_descriptors_ssreport, |
| 1000 | hidg_ss_descriptors_ssreport); |
| 1001 | |
| 1002 | if (status) |
| 1003 | goto fail; |
| 1004 | |
| 1005 | spin_lock_init(&hidg->write_spinlock); |
| 1006 | hidg->write_pending = 1; |
| 1007 | hidg->req = NULL; |
| 1008 | spin_lock_init(&hidg->read_spinlock); |
| 1009 | init_waitqueue_head(&hidg->write_queue); |
| 1010 | init_waitqueue_head(&hidg->read_queue); |
| 1011 | INIT_LIST_HEAD(&hidg->completed_out_req); |
| 1012 | |
| 1013 | /* create char device */ |
| 1014 | cdev_init(&hidg->cdev, &f_hidg_fops); |
| 1015 | status = cdev_device_add(&hidg->cdev, &hidg->dev); |
| 1016 | if (status) |
| 1017 | goto fail_free_descs; |
| 1018 | |
| 1019 | return 0; |
| 1020 | fail_free_descs: |
| 1021 | usb_free_all_descriptors(f); |
| 1022 | fail: |
| 1023 | ERROR(f->config->cdev, "hidg_bind FAILED\n"); |
| 1024 | if (hidg->req != NULL) |
| 1025 | free_ep_req(hidg->in_ep, hidg->req); |
| 1026 | |
| 1027 | return status; |
| 1028 | } |
| 1029 | |
| 1030 | static inline int hidg_get_minor(void) |
| 1031 | { |
| 1032 | int ret; |
| 1033 | |
| 1034 | ret = ida_simple_get(&hidg_ida, 0, 0, GFP_KERNEL); |
| 1035 | if (ret >= HIDG_MINORS) { |
| 1036 | ida_simple_remove(&hidg_ida, ret); |
| 1037 | ret = -ENODEV; |
| 1038 | } |
| 1039 | |
| 1040 | return ret; |
| 1041 | } |
| 1042 | |
| 1043 | static inline struct f_hid_opts *to_f_hid_opts(struct config_item *item) |
| 1044 | { |
| 1045 | return container_of(to_config_group(item), struct f_hid_opts, |
| 1046 | func_inst.group); |
| 1047 | } |
| 1048 | |
| 1049 | static void hid_attr_release(struct config_item *item) |
| 1050 | { |
| 1051 | struct f_hid_opts *opts = to_f_hid_opts(item); |
| 1052 | |
| 1053 | usb_put_function_instance(&opts->func_inst); |
| 1054 | } |
| 1055 | |
| 1056 | static struct configfs_item_operations hidg_item_ops = { |
| 1057 | .release = hid_attr_release, |
| 1058 | }; |
| 1059 | |
| 1060 | #define F_HID_OPT(name, prec, limit) \ |
| 1061 | static ssize_t f_hid_opts_##name##_show(struct config_item *item, char *page)\ |
| 1062 | { \ |
| 1063 | struct f_hid_opts *opts = to_f_hid_opts(item); \ |
| 1064 | int result; \ |
| 1065 | \ |
| 1066 | mutex_lock(&opts->lock); \ |
| 1067 | result = sprintf(page, "%d\n", opts->name); \ |
| 1068 | mutex_unlock(&opts->lock); \ |
| 1069 | \ |
| 1070 | return result; \ |
| 1071 | } \ |
| 1072 | \ |
| 1073 | static ssize_t f_hid_opts_##name##_store(struct config_item *item, \ |
| 1074 | const char *page, size_t len) \ |
| 1075 | { \ |
| 1076 | struct f_hid_opts *opts = to_f_hid_opts(item); \ |
| 1077 | int ret; \ |
| 1078 | u##prec num; \ |
| 1079 | \ |
| 1080 | mutex_lock(&opts->lock); \ |
| 1081 | if (opts->refcnt) { \ |
| 1082 | ret = -EBUSY; \ |
| 1083 | goto end; \ |
| 1084 | } \ |
| 1085 | \ |
| 1086 | ret = kstrtou##prec(page, 0, &num); \ |
| 1087 | if (ret) \ |
| 1088 | goto end; \ |
| 1089 | \ |
| 1090 | if (num > limit) { \ |
| 1091 | ret = -EINVAL; \ |
| 1092 | goto end; \ |
| 1093 | } \ |
| 1094 | opts->name = num; \ |
| 1095 | ret = len; \ |
| 1096 | \ |
| 1097 | end: \ |
| 1098 | mutex_unlock(&opts->lock); \ |
| 1099 | return ret; \ |
| 1100 | } \ |
| 1101 | \ |
| 1102 | CONFIGFS_ATTR(f_hid_opts_, name) |
| 1103 | |
| 1104 | F_HID_OPT(subclass, 8, 255); |
| 1105 | F_HID_OPT(protocol, 8, 255); |
| 1106 | F_HID_OPT(no_out_endpoint, 8, 1); |
| 1107 | F_HID_OPT(report_length, 16, 65535); |
| 1108 | |
| 1109 | static ssize_t f_hid_opts_report_desc_show(struct config_item *item, char *page) |
| 1110 | { |
| 1111 | struct f_hid_opts *opts = to_f_hid_opts(item); |
| 1112 | int result; |
| 1113 | |
| 1114 | mutex_lock(&opts->lock); |
| 1115 | result = opts->report_desc_length; |
| 1116 | memcpy(page, opts->report_desc, opts->report_desc_length); |
| 1117 | mutex_unlock(&opts->lock); |
| 1118 | |
| 1119 | return result; |
| 1120 | } |
| 1121 | |
| 1122 | static ssize_t f_hid_opts_report_desc_store(struct config_item *item, |
| 1123 | const char *page, size_t len) |
| 1124 | { |
| 1125 | struct f_hid_opts *opts = to_f_hid_opts(item); |
| 1126 | int ret = -EBUSY; |
| 1127 | char *d; |
| 1128 | |
| 1129 | mutex_lock(&opts->lock); |
| 1130 | |
| 1131 | if (opts->refcnt) |
| 1132 | goto end; |
| 1133 | if (len > PAGE_SIZE) { |
| 1134 | ret = -ENOSPC; |
| 1135 | goto end; |
| 1136 | } |
| 1137 | d = kmemdup(page, len, GFP_KERNEL); |
| 1138 | if (!d) { |
| 1139 | ret = -ENOMEM; |
| 1140 | goto end; |
| 1141 | } |
| 1142 | kfree(opts->report_desc); |
| 1143 | opts->report_desc = d; |
| 1144 | opts->report_desc_length = len; |
| 1145 | opts->report_desc_alloc = true; |
| 1146 | ret = len; |
| 1147 | end: |
| 1148 | mutex_unlock(&opts->lock); |
| 1149 | return ret; |
| 1150 | } |
| 1151 | |
| 1152 | CONFIGFS_ATTR(f_hid_opts_, report_desc); |
| 1153 | |
| 1154 | static ssize_t f_hid_opts_dev_show(struct config_item *item, char *page) |
| 1155 | { |
| 1156 | struct f_hid_opts *opts = to_f_hid_opts(item); |
| 1157 | |
| 1158 | return sprintf(page, "%d:%d\n", major, opts->minor); |
| 1159 | } |
| 1160 | |
| 1161 | CONFIGFS_ATTR_RO(f_hid_opts_, dev); |
| 1162 | |
| 1163 | static struct configfs_attribute *hid_attrs[] = { |
| 1164 | &f_hid_opts_attr_subclass, |
| 1165 | &f_hid_opts_attr_protocol, |
| 1166 | &f_hid_opts_attr_no_out_endpoint, |
| 1167 | &f_hid_opts_attr_report_length, |
| 1168 | &f_hid_opts_attr_report_desc, |
| 1169 | &f_hid_opts_attr_dev, |
| 1170 | NULL, |
| 1171 | }; |
| 1172 | |
| 1173 | static const struct config_item_type hid_func_type = { |
| 1174 | .ct_item_ops = &hidg_item_ops, |
| 1175 | .ct_attrs = hid_attrs, |
| 1176 | .ct_owner = THIS_MODULE, |
| 1177 | }; |
| 1178 | |
| 1179 | static inline void hidg_put_minor(int minor) |
| 1180 | { |
| 1181 | ida_simple_remove(&hidg_ida, minor); |
| 1182 | } |
| 1183 | |
| 1184 | static void hidg_free_inst(struct usb_function_instance *f) |
| 1185 | { |
| 1186 | struct f_hid_opts *opts; |
| 1187 | |
| 1188 | opts = container_of(f, struct f_hid_opts, func_inst); |
| 1189 | |
| 1190 | mutex_lock(&hidg_ida_lock); |
| 1191 | |
| 1192 | hidg_put_minor(opts->minor); |
| 1193 | if (ida_is_empty(&hidg_ida)) |
| 1194 | ghid_cleanup(); |
| 1195 | |
| 1196 | mutex_unlock(&hidg_ida_lock); |
| 1197 | |
| 1198 | if (opts->report_desc_alloc) |
| 1199 | kfree(opts->report_desc); |
| 1200 | |
| 1201 | kfree(opts); |
| 1202 | } |
| 1203 | |
| 1204 | static struct usb_function_instance *hidg_alloc_inst(void) |
| 1205 | { |
| 1206 | struct f_hid_opts *opts; |
| 1207 | struct usb_function_instance *ret; |
| 1208 | int status = 0; |
| 1209 | |
| 1210 | opts = kzalloc(sizeof(*opts), GFP_KERNEL); |
| 1211 | if (!opts) |
| 1212 | return ERR_PTR(-ENOMEM); |
| 1213 | mutex_init(&opts->lock); |
| 1214 | opts->func_inst.free_func_inst = hidg_free_inst; |
| 1215 | ret = &opts->func_inst; |
| 1216 | |
| 1217 | mutex_lock(&hidg_ida_lock); |
| 1218 | |
| 1219 | if (ida_is_empty(&hidg_ida)) { |
| 1220 | status = ghid_setup(NULL, HIDG_MINORS); |
| 1221 | if (status) { |
| 1222 | ret = ERR_PTR(status); |
| 1223 | kfree(opts); |
| 1224 | goto unlock; |
| 1225 | } |
| 1226 | } |
| 1227 | |
| 1228 | opts->minor = hidg_get_minor(); |
| 1229 | if (opts->minor < 0) { |
| 1230 | ret = ERR_PTR(opts->minor); |
| 1231 | kfree(opts); |
| 1232 | if (ida_is_empty(&hidg_ida)) |
| 1233 | ghid_cleanup(); |
| 1234 | goto unlock; |
| 1235 | } |
| 1236 | config_group_init_type_name(&opts->func_inst.group, "", &hid_func_type); |
| 1237 | |
| 1238 | unlock: |
| 1239 | mutex_unlock(&hidg_ida_lock); |
| 1240 | return ret; |
| 1241 | } |
| 1242 | |
| 1243 | static void hidg_free(struct usb_function *f) |
| 1244 | { |
| 1245 | struct f_hidg *hidg; |
| 1246 | struct f_hid_opts *opts; |
| 1247 | |
| 1248 | hidg = func_to_hidg(f); |
| 1249 | opts = container_of(f->fi, struct f_hid_opts, func_inst); |
| 1250 | put_device(&hidg->dev); |
| 1251 | mutex_lock(&opts->lock); |
| 1252 | --opts->refcnt; |
| 1253 | mutex_unlock(&opts->lock); |
| 1254 | } |
| 1255 | |
| 1256 | static void hidg_unbind(struct usb_configuration *c, struct usb_function *f) |
| 1257 | { |
| 1258 | struct f_hidg *hidg = func_to_hidg(f); |
| 1259 | |
| 1260 | cdev_device_del(&hidg->cdev, &hidg->dev); |
| 1261 | |
| 1262 | usb_free_all_descriptors(f); |
| 1263 | } |
| 1264 | |
| 1265 | static struct usb_function *hidg_alloc(struct usb_function_instance *fi) |
| 1266 | { |
| 1267 | struct f_hidg *hidg; |
| 1268 | struct f_hid_opts *opts; |
| 1269 | int ret; |
| 1270 | |
| 1271 | /* allocate and initialize one new instance */ |
| 1272 | hidg = kzalloc(sizeof(*hidg), GFP_KERNEL); |
| 1273 | if (!hidg) |
| 1274 | return ERR_PTR(-ENOMEM); |
| 1275 | |
| 1276 | opts = container_of(fi, struct f_hid_opts, func_inst); |
| 1277 | |
| 1278 | mutex_lock(&opts->lock); |
| 1279 | ++opts->refcnt; |
| 1280 | |
| 1281 | device_initialize(&hidg->dev); |
| 1282 | hidg->dev.release = hidg_release; |
| 1283 | hidg->dev.class = hidg_class; |
| 1284 | hidg->dev.devt = MKDEV(major, opts->minor); |
| 1285 | ret = dev_set_name(&hidg->dev, "hidg%d", opts->minor); |
| 1286 | if (ret) { |
| 1287 | --opts->refcnt; |
| 1288 | mutex_unlock(&opts->lock); |
| 1289 | return ERR_PTR(ret); |
| 1290 | } |
| 1291 | |
| 1292 | hidg->bInterfaceSubClass = opts->subclass; |
| 1293 | hidg->bInterfaceProtocol = opts->protocol; |
| 1294 | hidg->report_length = opts->report_length; |
| 1295 | hidg->report_desc_length = opts->report_desc_length; |
| 1296 | if (opts->report_desc) { |
| 1297 | hidg->report_desc = kmemdup(opts->report_desc, |
| 1298 | opts->report_desc_length, |
| 1299 | GFP_KERNEL); |
| 1300 | if (!hidg->report_desc) { |
| 1301 | put_device(&hidg->dev); |
| 1302 | --opts->refcnt; |
| 1303 | mutex_unlock(&opts->lock); |
| 1304 | return ERR_PTR(-ENOMEM); |
| 1305 | } |
| 1306 | } |
| 1307 | hidg->use_out_ep = !opts->no_out_endpoint; |
| 1308 | |
| 1309 | mutex_unlock(&opts->lock); |
| 1310 | |
| 1311 | hidg->func.name = "hid"; |
| 1312 | hidg->func.bind = hidg_bind; |
| 1313 | hidg->func.unbind = hidg_unbind; |
| 1314 | hidg->func.set_alt = hidg_set_alt; |
| 1315 | hidg->func.disable = hidg_disable; |
| 1316 | hidg->func.setup = hidg_setup; |
| 1317 | hidg->func.free_func = hidg_free; |
| 1318 | |
| 1319 | /* this could me made configurable at some point */ |
| 1320 | hidg->qlen = 4; |
| 1321 | |
| 1322 | return &hidg->func; |
| 1323 | } |
| 1324 | |
| 1325 | DECLARE_USB_FUNCTION_INIT(hid, hidg_alloc_inst, hidg_alloc); |
| 1326 | MODULE_LICENSE("GPL"); |
| 1327 | MODULE_AUTHOR("Fabien Chouteau"); |
| 1328 | |
| 1329 | int ghid_setup(struct usb_gadget *g, int count) |
| 1330 | { |
| 1331 | int status; |
| 1332 | dev_t dev; |
| 1333 | |
| 1334 | hidg_class = class_create(THIS_MODULE, "hidg"); |
| 1335 | if (IS_ERR(hidg_class)) { |
| 1336 | status = PTR_ERR(hidg_class); |
| 1337 | hidg_class = NULL; |
| 1338 | return status; |
| 1339 | } |
| 1340 | |
| 1341 | status = alloc_chrdev_region(&dev, 0, count, "hidg"); |
| 1342 | if (status) { |
| 1343 | class_destroy(hidg_class); |
| 1344 | hidg_class = NULL; |
| 1345 | return status; |
| 1346 | } |
| 1347 | |
| 1348 | major = MAJOR(dev); |
| 1349 | minors = count; |
| 1350 | |
| 1351 | return 0; |
| 1352 | } |
| 1353 | |
| 1354 | void ghid_cleanup(void) |
| 1355 | { |
| 1356 | if (major) { |
| 1357 | unregister_chrdev_region(MKDEV(major, 0), minors); |
| 1358 | major = minors = 0; |
| 1359 | } |
| 1360 | |
| 1361 | class_destroy(hidg_class); |
| 1362 | hidg_class = NULL; |
| 1363 | } |