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