Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 1 | /* |
| 2 | * hid.c -- HID Composite driver |
| 3 | * |
| 4 | * Based on multi.c |
| 5 | * |
| 6 | * Copyright (C) 2010 Fabien Chouteau <fabien.chouteau@barco.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 12 | */ |
| 13 | |
| 14 | |
| 15 | #include <linux/kernel.h> |
| 16 | #include <linux/platform_device.h> |
| 17 | #include <linux/list.h> |
Sebastian Andrzej Siewior | 721e2e9 | 2012-09-06 20:11:27 +0200 | [diff] [blame] | 18 | #include <linux/module.h> |
| 19 | #include <linux/usb/composite.h> |
Andrzej Pietrasiewicz | 4bc8a33 | 2014-11-06 11:12:00 +0100 | [diff] [blame] | 20 | #include <linux/usb/g_hid.h> |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 21 | |
Sebastian Andrzej Siewior | dc995fc | 2012-09-06 20:11:12 +0200 | [diff] [blame] | 22 | #include "gadget_chips.h" |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 23 | #define DRIVER_DESC "HID Gadget" |
| 24 | #define DRIVER_VERSION "2010/03/16" |
| 25 | |
Andrzej Pietrasiewicz | 4bc8a33 | 2014-11-06 11:12:00 +0100 | [diff] [blame] | 26 | #include "u_hid.h" |
| 27 | |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 28 | /*-------------------------------------------------------------------------*/ |
| 29 | |
| 30 | #define HIDG_VENDOR_NUM 0x0525 /* XXX NetChip */ |
| 31 | #define HIDG_PRODUCT_NUM 0xa4ac /* Linux-USB HID gadget */ |
| 32 | |
| 33 | /*-------------------------------------------------------------------------*/ |
| 34 | |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 35 | struct hidg_func_node { |
Andrzej Pietrasiewicz | 4bc8a33 | 2014-11-06 11:12:00 +0100 | [diff] [blame] | 36 | struct usb_function_instance *fi; |
| 37 | struct usb_function *f; |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 38 | struct list_head node; |
| 39 | struct hidg_func_descriptor *func; |
| 40 | }; |
| 41 | |
| 42 | static LIST_HEAD(hidg_func_list); |
| 43 | |
| 44 | /*-------------------------------------------------------------------------*/ |
Sebastian Andrzej Siewior | 7d16e8d | 2012-09-10 15:01:53 +0200 | [diff] [blame] | 45 | USB_GADGET_COMPOSITE_OPTIONS(); |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 46 | |
| 47 | static struct usb_device_descriptor device_desc = { |
| 48 | .bLength = sizeof device_desc, |
| 49 | .bDescriptorType = USB_DT_DEVICE, |
| 50 | |
| 51 | .bcdUSB = cpu_to_le16(0x0200), |
| 52 | |
| 53 | /* .bDeviceClass = USB_CLASS_COMM, */ |
| 54 | /* .bDeviceSubClass = 0, */ |
| 55 | /* .bDeviceProtocol = 0, */ |
Orjan Friberg | 33d2832 | 2012-03-07 17:16:14 +0100 | [diff] [blame] | 56 | .bDeviceClass = USB_CLASS_PER_INTERFACE, |
| 57 | .bDeviceSubClass = 0, |
| 58 | .bDeviceProtocol = 0, |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 59 | /* .bMaxPacketSize0 = f(hardware) */ |
| 60 | |
| 61 | /* Vendor and product id can be overridden by module parameters. */ |
| 62 | .idVendor = cpu_to_le16(HIDG_VENDOR_NUM), |
| 63 | .idProduct = cpu_to_le16(HIDG_PRODUCT_NUM), |
| 64 | /* .bcdDevice = f(hardware) */ |
| 65 | /* .iManufacturer = DYNAMIC */ |
| 66 | /* .iProduct = DYNAMIC */ |
| 67 | /* NO SERIAL NUMBER */ |
| 68 | .bNumConfigurations = 1, |
| 69 | }; |
| 70 | |
| 71 | static struct usb_otg_descriptor otg_descriptor = { |
| 72 | .bLength = sizeof otg_descriptor, |
| 73 | .bDescriptorType = USB_DT_OTG, |
| 74 | |
| 75 | /* REVISIT SRP-only hardware is possible, although |
| 76 | * it would not be called "OTG" ... |
| 77 | */ |
| 78 | .bmAttributes = USB_OTG_SRP | USB_OTG_HNP, |
| 79 | }; |
| 80 | |
| 81 | static const struct usb_descriptor_header *otg_desc[] = { |
| 82 | (struct usb_descriptor_header *) &otg_descriptor, |
| 83 | NULL, |
| 84 | }; |
| 85 | |
| 86 | |
| 87 | /* string IDs are assigned dynamically */ |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 88 | static struct usb_string strings_dev[] = { |
Sebastian Andrzej Siewior | cc2683c | 2012-09-10 15:01:58 +0200 | [diff] [blame] | 89 | [USB_GADGET_MANUFACTURER_IDX].s = "", |
Sebastian Andrzej Siewior | 276e2e4 | 2012-09-06 20:11:21 +0200 | [diff] [blame] | 90 | [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC, |
| 91 | [USB_GADGET_SERIAL_IDX].s = "", |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 92 | { } /* end of list */ |
| 93 | }; |
| 94 | |
| 95 | static struct usb_gadget_strings stringtab_dev = { |
| 96 | .language = 0x0409, /* en-us */ |
| 97 | .strings = strings_dev, |
| 98 | }; |
| 99 | |
| 100 | static struct usb_gadget_strings *dev_strings[] = { |
| 101 | &stringtab_dev, |
| 102 | NULL, |
| 103 | }; |
| 104 | |
| 105 | |
| 106 | |
| 107 | /****************************** Configurations ******************************/ |
| 108 | |
Arnd Bergmann | c94e289 | 2015-04-11 00:14:21 +0200 | [diff] [blame] | 109 | static int do_config(struct usb_configuration *c) |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 110 | { |
Andrzej Pietrasiewicz | 4bc8a33 | 2014-11-06 11:12:00 +0100 | [diff] [blame] | 111 | struct hidg_func_node *e, *n; |
| 112 | int status = 0; |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 113 | |
| 114 | if (gadget_is_otg(c->cdev->gadget)) { |
| 115 | c->descriptors = otg_desc; |
| 116 | c->bmAttributes |= USB_CONFIG_ATT_WAKEUP; |
| 117 | } |
| 118 | |
| 119 | list_for_each_entry(e, &hidg_func_list, node) { |
Andrzej Pietrasiewicz | 4bc8a33 | 2014-11-06 11:12:00 +0100 | [diff] [blame] | 120 | e->f = usb_get_function(e->fi); |
| 121 | if (IS_ERR(e->f)) |
| 122 | goto put; |
| 123 | status = usb_add_function(c, e->f); |
| 124 | if (status < 0) { |
| 125 | usb_put_function(e->f); |
| 126 | goto put; |
| 127 | } |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 128 | } |
| 129 | |
Andrzej Pietrasiewicz | 4bc8a33 | 2014-11-06 11:12:00 +0100 | [diff] [blame] | 130 | return 0; |
| 131 | put: |
| 132 | list_for_each_entry(n, &hidg_func_list, node) { |
| 133 | if (n == e) |
| 134 | break; |
| 135 | usb_remove_function(c, n->f); |
| 136 | usb_put_function(n->f); |
| 137 | } |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 138 | return status; |
| 139 | } |
| 140 | |
| 141 | static struct usb_configuration config_driver = { |
| 142 | .label = "HID Gadget", |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 143 | .bConfigurationValue = 1, |
| 144 | /* .iConfiguration = DYNAMIC */ |
| 145 | .bmAttributes = USB_CONFIG_ATT_SELFPOWER, |
| 146 | }; |
| 147 | |
| 148 | /****************************** Gadget Bind ******************************/ |
| 149 | |
Arnd Bergmann | c94e289 | 2015-04-11 00:14:21 +0200 | [diff] [blame] | 150 | static int hid_bind(struct usb_composite_dev *cdev) |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 151 | { |
| 152 | struct usb_gadget *gadget = cdev->gadget; |
| 153 | struct list_head *tmp; |
Andrzej Pietrasiewicz | 4bc8a33 | 2014-11-06 11:12:00 +0100 | [diff] [blame] | 154 | struct hidg_func_node *n, *m; |
| 155 | struct f_hid_opts *hid_opts; |
Sebastian Andrzej Siewior | ed9cbda | 2012-09-10 09:16:07 +0200 | [diff] [blame] | 156 | int status, funcs = 0; |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 157 | |
| 158 | list_for_each(tmp, &hidg_func_list) |
| 159 | funcs++; |
| 160 | |
| 161 | if (!funcs) |
| 162 | return -ENODEV; |
| 163 | |
Andrzej Pietrasiewicz | 4bc8a33 | 2014-11-06 11:12:00 +0100 | [diff] [blame] | 164 | list_for_each_entry(n, &hidg_func_list, node) { |
| 165 | n->fi = usb_get_function_instance("hid"); |
| 166 | if (IS_ERR(n->fi)) { |
| 167 | status = PTR_ERR(n->fi); |
| 168 | goto put; |
| 169 | } |
| 170 | hid_opts = container_of(n->fi, struct f_hid_opts, func_inst); |
| 171 | hid_opts->subclass = n->func->subclass; |
| 172 | hid_opts->protocol = n->func->protocol; |
| 173 | hid_opts->report_length = n->func->report_length; |
| 174 | hid_opts->report_desc_length = n->func->report_desc_length; |
| 175 | hid_opts->report_desc = n->func->report_desc; |
| 176 | } |
| 177 | |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 178 | |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 179 | /* Allocate string descriptor numbers ... note that string |
| 180 | * contents can be overridden by the composite_dev glue. |
| 181 | */ |
| 182 | |
Sebastian Andrzej Siewior | e1f15cc | 2012-09-06 20:11:16 +0200 | [diff] [blame] | 183 | status = usb_string_ids_tab(cdev, strings_dev); |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 184 | if (status < 0) |
Andrzej Pietrasiewicz | 4bc8a33 | 2014-11-06 11:12:00 +0100 | [diff] [blame] | 185 | goto put; |
Sebastian Andrzej Siewior | 276e2e4 | 2012-09-06 20:11:21 +0200 | [diff] [blame] | 186 | device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id; |
| 187 | device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id; |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 188 | |
| 189 | /* register our configuration */ |
Uwe Kleine-König | c9bfff9 | 2010-08-12 17:43:55 +0200 | [diff] [blame] | 190 | status = usb_add_config(cdev, &config_driver, do_config); |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 191 | if (status < 0) |
Andrzej Pietrasiewicz | 4bc8a33 | 2014-11-06 11:12:00 +0100 | [diff] [blame] | 192 | goto put; |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 193 | |
Sebastian Andrzej Siewior | 7d16e8d | 2012-09-10 15:01:53 +0200 | [diff] [blame] | 194 | usb_composite_overwrite_options(cdev, &coverwrite); |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 195 | dev_info(&gadget->dev, DRIVER_DESC ", version: " DRIVER_VERSION "\n"); |
| 196 | |
| 197 | return 0; |
Andrzej Pietrasiewicz | 4bc8a33 | 2014-11-06 11:12:00 +0100 | [diff] [blame] | 198 | |
| 199 | put: |
| 200 | list_for_each_entry(m, &hidg_func_list, node) { |
| 201 | if (m == n) |
| 202 | break; |
| 203 | usb_put_function_instance(m->fi); |
| 204 | } |
| 205 | return status; |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 206 | } |
| 207 | |
Arnd Bergmann | c94e289 | 2015-04-11 00:14:21 +0200 | [diff] [blame] | 208 | static int hid_unbind(struct usb_composite_dev *cdev) |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 209 | { |
Andrzej Pietrasiewicz | 4bc8a33 | 2014-11-06 11:12:00 +0100 | [diff] [blame] | 210 | struct hidg_func_node *n; |
| 211 | |
| 212 | list_for_each_entry(n, &hidg_func_list, node) { |
| 213 | usb_put_function(n->f); |
| 214 | usb_put_function_instance(n->fi); |
| 215 | } |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 216 | return 0; |
| 217 | } |
| 218 | |
Arnd Bergmann | c94e289 | 2015-04-11 00:14:21 +0200 | [diff] [blame] | 219 | static int hidg_plat_driver_probe(struct platform_device *pdev) |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 220 | { |
Jingoo Han | e01ee9f | 2013-07-30 17:00:51 +0900 | [diff] [blame] | 221 | struct hidg_func_descriptor *func = dev_get_platdata(&pdev->dev); |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 222 | struct hidg_func_node *entry; |
| 223 | |
| 224 | if (!func) { |
| 225 | dev_err(&pdev->dev, "Platform data missing\n"); |
| 226 | return -ENODEV; |
| 227 | } |
| 228 | |
| 229 | entry = kzalloc(sizeof(*entry), GFP_KERNEL); |
| 230 | if (!entry) |
| 231 | return -ENOMEM; |
| 232 | |
| 233 | entry->func = func; |
| 234 | list_add_tail(&entry->node, &hidg_func_list); |
| 235 | |
| 236 | return 0; |
| 237 | } |
| 238 | |
Bill Pemberton | fb4e98a | 2012-11-19 13:26:20 -0500 | [diff] [blame] | 239 | static int hidg_plat_driver_remove(struct platform_device *pdev) |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 240 | { |
| 241 | struct hidg_func_node *e, *n; |
| 242 | |
| 243 | list_for_each_entry_safe(e, n, &hidg_func_list, node) { |
| 244 | list_del(&e->node); |
| 245 | kfree(e); |
| 246 | } |
| 247 | |
| 248 | return 0; |
| 249 | } |
| 250 | |
| 251 | |
| 252 | /****************************** Some noise ******************************/ |
| 253 | |
| 254 | |
Arnd Bergmann | c94e289 | 2015-04-11 00:14:21 +0200 | [diff] [blame] | 255 | static struct usb_composite_driver hidg_driver = { |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 256 | .name = "g_hid", |
| 257 | .dev = &device_desc, |
| 258 | .strings = dev_strings, |
Tatyana Brokhman | 35a0e0b | 2011-06-29 16:41:49 +0300 | [diff] [blame] | 259 | .max_speed = USB_SPEED_HIGH, |
Sebastian Andrzej Siewior | 03e42bd | 2012-09-06 20:11:04 +0200 | [diff] [blame] | 260 | .bind = hid_bind, |
Arnd Bergmann | c94e289 | 2015-04-11 00:14:21 +0200 | [diff] [blame] | 261 | .unbind = hid_unbind, |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 262 | }; |
| 263 | |
| 264 | static struct platform_driver hidg_plat_driver = { |
Bill Pemberton | 7690417 | 2012-11-19 13:21:08 -0500 | [diff] [blame] | 265 | .remove = hidg_plat_driver_remove, |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 266 | .driver = { |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 267 | .name = "hidg", |
| 268 | }, |
| 269 | }; |
| 270 | |
| 271 | |
| 272 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 273 | MODULE_AUTHOR("Fabien Chouteau, Peter Korsgaard"); |
| 274 | MODULE_LICENSE("GPL"); |
| 275 | |
| 276 | static int __init hidg_init(void) |
| 277 | { |
Peter Korsgaard | da01c7a | 2010-04-26 10:05:06 +0200 | [diff] [blame] | 278 | int status; |
| 279 | |
| 280 | status = platform_driver_probe(&hidg_plat_driver, |
| 281 | hidg_plat_driver_probe); |
| 282 | if (status < 0) |
| 283 | return status; |
| 284 | |
Sebastian Andrzej Siewior | 03e42bd | 2012-09-06 20:11:04 +0200 | [diff] [blame] | 285 | status = usb_composite_probe(&hidg_driver); |
Peter Korsgaard | da01c7a | 2010-04-26 10:05:06 +0200 | [diff] [blame] | 286 | if (status < 0) |
| 287 | platform_driver_unregister(&hidg_plat_driver); |
| 288 | |
| 289 | return status; |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 290 | } |
| 291 | module_init(hidg_init); |
| 292 | |
| 293 | static void __exit hidg_cleanup(void) |
| 294 | { |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 295 | usb_composite_unregister(&hidg_driver); |
Andrzej Pietrasiewicz | 00896f6 | 2014-11-06 11:11:58 +0100 | [diff] [blame] | 296 | platform_driver_unregister(&hidg_plat_driver); |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 297 | } |
| 298 | module_exit(hidg_cleanup); |