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> |
| 18 | |
Sebastian Andrzej Siewior | dc995fc | 2012-09-06 20:11:12 +0200 | [diff] [blame^] | 19 | #include "gadget_chips.h" |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 20 | #define DRIVER_DESC "HID Gadget" |
| 21 | #define DRIVER_VERSION "2010/03/16" |
| 22 | |
| 23 | /*-------------------------------------------------------------------------*/ |
| 24 | |
| 25 | #define HIDG_VENDOR_NUM 0x0525 /* XXX NetChip */ |
| 26 | #define HIDG_PRODUCT_NUM 0xa4ac /* Linux-USB HID gadget */ |
| 27 | |
| 28 | /*-------------------------------------------------------------------------*/ |
| 29 | |
| 30 | /* |
| 31 | * kbuild is not very cooperative with respect to linking separately |
| 32 | * compiled library objects into one module. So for now we won't use |
| 33 | * separate compilation ... ensuring init/exit sections work to shrink |
| 34 | * the runtime footprint, and giving us at least some parts of what |
| 35 | * a "gcc --combine ... part1.c part2.c part3.c ... " build would. |
| 36 | */ |
| 37 | |
| 38 | #include "composite.c" |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 39 | |
| 40 | #include "f_hid.c" |
| 41 | |
| 42 | |
| 43 | struct hidg_func_node { |
| 44 | struct list_head node; |
| 45 | struct hidg_func_descriptor *func; |
| 46 | }; |
| 47 | |
| 48 | static LIST_HEAD(hidg_func_list); |
| 49 | |
| 50 | /*-------------------------------------------------------------------------*/ |
| 51 | |
| 52 | static struct usb_device_descriptor device_desc = { |
| 53 | .bLength = sizeof device_desc, |
| 54 | .bDescriptorType = USB_DT_DEVICE, |
| 55 | |
| 56 | .bcdUSB = cpu_to_le16(0x0200), |
| 57 | |
| 58 | /* .bDeviceClass = USB_CLASS_COMM, */ |
| 59 | /* .bDeviceSubClass = 0, */ |
| 60 | /* .bDeviceProtocol = 0, */ |
Orjan Friberg | 33d2832 | 2012-03-07 17:16:14 +0100 | [diff] [blame] | 61 | .bDeviceClass = USB_CLASS_PER_INTERFACE, |
| 62 | .bDeviceSubClass = 0, |
| 63 | .bDeviceProtocol = 0, |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 64 | /* .bMaxPacketSize0 = f(hardware) */ |
| 65 | |
| 66 | /* Vendor and product id can be overridden by module parameters. */ |
| 67 | .idVendor = cpu_to_le16(HIDG_VENDOR_NUM), |
| 68 | .idProduct = cpu_to_le16(HIDG_PRODUCT_NUM), |
| 69 | /* .bcdDevice = f(hardware) */ |
| 70 | /* .iManufacturer = DYNAMIC */ |
| 71 | /* .iProduct = DYNAMIC */ |
| 72 | /* NO SERIAL NUMBER */ |
| 73 | .bNumConfigurations = 1, |
| 74 | }; |
| 75 | |
| 76 | static struct usb_otg_descriptor otg_descriptor = { |
| 77 | .bLength = sizeof otg_descriptor, |
| 78 | .bDescriptorType = USB_DT_OTG, |
| 79 | |
| 80 | /* REVISIT SRP-only hardware is possible, although |
| 81 | * it would not be called "OTG" ... |
| 82 | */ |
| 83 | .bmAttributes = USB_OTG_SRP | USB_OTG_HNP, |
| 84 | }; |
| 85 | |
| 86 | static const struct usb_descriptor_header *otg_desc[] = { |
| 87 | (struct usb_descriptor_header *) &otg_descriptor, |
| 88 | NULL, |
| 89 | }; |
| 90 | |
| 91 | |
| 92 | /* string IDs are assigned dynamically */ |
| 93 | |
| 94 | #define STRING_MANUFACTURER_IDX 0 |
| 95 | #define STRING_PRODUCT_IDX 1 |
| 96 | |
| 97 | static char manufacturer[50]; |
| 98 | |
| 99 | static struct usb_string strings_dev[] = { |
| 100 | [STRING_MANUFACTURER_IDX].s = manufacturer, |
| 101 | [STRING_PRODUCT_IDX].s = DRIVER_DESC, |
| 102 | { } /* end of list */ |
| 103 | }; |
| 104 | |
| 105 | static struct usb_gadget_strings stringtab_dev = { |
| 106 | .language = 0x0409, /* en-us */ |
| 107 | .strings = strings_dev, |
| 108 | }; |
| 109 | |
| 110 | static struct usb_gadget_strings *dev_strings[] = { |
| 111 | &stringtab_dev, |
| 112 | NULL, |
| 113 | }; |
| 114 | |
| 115 | |
| 116 | |
| 117 | /****************************** Configurations ******************************/ |
| 118 | |
Michal Nazarewicz | e12995e | 2010-08-12 17:43:52 +0200 | [diff] [blame] | 119 | static int __init do_config(struct usb_configuration *c) |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 120 | { |
| 121 | struct hidg_func_node *e; |
| 122 | int func = 0, status = 0; |
| 123 | |
| 124 | if (gadget_is_otg(c->cdev->gadget)) { |
| 125 | c->descriptors = otg_desc; |
| 126 | c->bmAttributes |= USB_CONFIG_ATT_WAKEUP; |
| 127 | } |
| 128 | |
| 129 | list_for_each_entry(e, &hidg_func_list, node) { |
| 130 | status = hidg_bind_config(c, e->func, func++); |
| 131 | if (status) |
| 132 | break; |
| 133 | } |
| 134 | |
| 135 | return status; |
| 136 | } |
| 137 | |
| 138 | static struct usb_configuration config_driver = { |
| 139 | .label = "HID Gadget", |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 140 | .bConfigurationValue = 1, |
| 141 | /* .iConfiguration = DYNAMIC */ |
| 142 | .bmAttributes = USB_CONFIG_ATT_SELFPOWER, |
| 143 | }; |
| 144 | |
| 145 | /****************************** Gadget Bind ******************************/ |
| 146 | |
Michal Nazarewicz | e12995e | 2010-08-12 17:43:52 +0200 | [diff] [blame] | 147 | static int __init hid_bind(struct usb_composite_dev *cdev) |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 148 | { |
| 149 | struct usb_gadget *gadget = cdev->gadget; |
| 150 | struct list_head *tmp; |
| 151 | int status, gcnum, funcs = 0; |
| 152 | |
| 153 | list_for_each(tmp, &hidg_func_list) |
| 154 | funcs++; |
| 155 | |
| 156 | if (!funcs) |
| 157 | return -ENODEV; |
| 158 | |
| 159 | /* set up HID */ |
| 160 | status = ghid_setup(cdev->gadget, funcs); |
| 161 | if (status < 0) |
| 162 | return status; |
| 163 | |
| 164 | gcnum = usb_gadget_controller_number(gadget); |
| 165 | if (gcnum >= 0) |
| 166 | device_desc.bcdDevice = cpu_to_le16(0x0300 | gcnum); |
| 167 | else |
| 168 | device_desc.bcdDevice = cpu_to_le16(0x0300 | 0x0099); |
| 169 | |
| 170 | |
| 171 | /* Allocate string descriptor numbers ... note that string |
| 172 | * contents can be overridden by the composite_dev glue. |
| 173 | */ |
| 174 | |
| 175 | /* device descriptor strings: manufacturer, product */ |
| 176 | snprintf(manufacturer, sizeof manufacturer, "%s %s with %s", |
| 177 | init_utsname()->sysname, init_utsname()->release, |
| 178 | gadget->name); |
| 179 | status = usb_string_id(cdev); |
| 180 | if (status < 0) |
| 181 | return status; |
| 182 | strings_dev[STRING_MANUFACTURER_IDX].id = status; |
| 183 | device_desc.iManufacturer = status; |
| 184 | |
| 185 | status = usb_string_id(cdev); |
| 186 | if (status < 0) |
| 187 | return status; |
| 188 | strings_dev[STRING_PRODUCT_IDX].id = status; |
| 189 | device_desc.iProduct = status; |
| 190 | |
| 191 | /* register our configuration */ |
Uwe Kleine-König | c9bfff9 | 2010-08-12 17:43:55 +0200 | [diff] [blame] | 192 | status = usb_add_config(cdev, &config_driver, do_config); |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 193 | if (status < 0) |
| 194 | return status; |
| 195 | |
| 196 | dev_info(&gadget->dev, DRIVER_DESC ", version: " DRIVER_VERSION "\n"); |
| 197 | |
| 198 | return 0; |
| 199 | } |
| 200 | |
| 201 | static int __exit hid_unbind(struct usb_composite_dev *cdev) |
| 202 | { |
| 203 | ghid_cleanup(); |
| 204 | return 0; |
| 205 | } |
| 206 | |
| 207 | static int __init hidg_plat_driver_probe(struct platform_device *pdev) |
| 208 | { |
| 209 | struct hidg_func_descriptor *func = pdev->dev.platform_data; |
| 210 | struct hidg_func_node *entry; |
| 211 | |
| 212 | if (!func) { |
| 213 | dev_err(&pdev->dev, "Platform data missing\n"); |
| 214 | return -ENODEV; |
| 215 | } |
| 216 | |
| 217 | entry = kzalloc(sizeof(*entry), GFP_KERNEL); |
| 218 | if (!entry) |
| 219 | return -ENOMEM; |
| 220 | |
| 221 | entry->func = func; |
| 222 | list_add_tail(&entry->node, &hidg_func_list); |
| 223 | |
| 224 | return 0; |
| 225 | } |
| 226 | |
| 227 | static int __devexit hidg_plat_driver_remove(struct platform_device *pdev) |
| 228 | { |
| 229 | struct hidg_func_node *e, *n; |
| 230 | |
| 231 | list_for_each_entry_safe(e, n, &hidg_func_list, node) { |
| 232 | list_del(&e->node); |
| 233 | kfree(e); |
| 234 | } |
| 235 | |
| 236 | return 0; |
| 237 | } |
| 238 | |
| 239 | |
| 240 | /****************************** Some noise ******************************/ |
| 241 | |
| 242 | |
Sebastian Andrzej Siewior | c2ec75c | 2012-09-06 20:11:03 +0200 | [diff] [blame] | 243 | static __refdata struct usb_composite_driver hidg_driver = { |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 244 | .name = "g_hid", |
| 245 | .dev = &device_desc, |
| 246 | .strings = dev_strings, |
Tatyana Brokhman | 35a0e0b | 2011-06-29 16:41:49 +0300 | [diff] [blame] | 247 | .max_speed = USB_SPEED_HIGH, |
Sebastian Andrzej Siewior | 03e42bd | 2012-09-06 20:11:04 +0200 | [diff] [blame] | 248 | .bind = hid_bind, |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 249 | .unbind = __exit_p(hid_unbind), |
| 250 | }; |
| 251 | |
| 252 | static struct platform_driver hidg_plat_driver = { |
| 253 | .remove = __devexit_p(hidg_plat_driver_remove), |
| 254 | .driver = { |
| 255 | .owner = THIS_MODULE, |
| 256 | .name = "hidg", |
| 257 | }, |
| 258 | }; |
| 259 | |
| 260 | |
| 261 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 262 | MODULE_AUTHOR("Fabien Chouteau, Peter Korsgaard"); |
| 263 | MODULE_LICENSE("GPL"); |
| 264 | |
| 265 | static int __init hidg_init(void) |
| 266 | { |
Peter Korsgaard | da01c7a | 2010-04-26 10:05:06 +0200 | [diff] [blame] | 267 | int status; |
| 268 | |
| 269 | status = platform_driver_probe(&hidg_plat_driver, |
| 270 | hidg_plat_driver_probe); |
| 271 | if (status < 0) |
| 272 | return status; |
| 273 | |
Sebastian Andrzej Siewior | 03e42bd | 2012-09-06 20:11:04 +0200 | [diff] [blame] | 274 | status = usb_composite_probe(&hidg_driver); |
Peter Korsgaard | da01c7a | 2010-04-26 10:05:06 +0200 | [diff] [blame] | 275 | if (status < 0) |
| 276 | platform_driver_unregister(&hidg_plat_driver); |
| 277 | |
| 278 | return status; |
Fabien Chouteau | 71adf11 | 2010-04-08 09:31:15 +0200 | [diff] [blame] | 279 | } |
| 280 | module_init(hidg_init); |
| 281 | |
| 282 | static void __exit hidg_cleanup(void) |
| 283 | { |
| 284 | platform_driver_unregister(&hidg_plat_driver); |
| 285 | usb_composite_unregister(&hidg_driver); |
| 286 | } |
| 287 | module_exit(hidg_cleanup); |