hid.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/list.h>
  16. #include "gadget_chips.h"
  17. #define DRIVER_DESC "HID Gadget"
  18. #define DRIVER_VERSION "2010/03/16"
  19. /*-------------------------------------------------------------------------*/
  20. #define HIDG_VENDOR_NUM 0x0525 /* XXX NetChip */
  21. #define HIDG_PRODUCT_NUM 0xa4ac /* Linux-USB HID gadget */
  22. /*-------------------------------------------------------------------------*/
  23. /*
  24. * kbuild is not very cooperative with respect to linking separately
  25. * compiled library objects into one module. So for now we won't use
  26. * separate compilation ... ensuring init/exit sections work to shrink
  27. * the runtime footprint, and giving us at least some parts of what
  28. * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
  29. */
  30. #include "composite.c"
  31. #include "f_hid.c"
  32. struct hidg_func_node {
  33. struct list_head node;
  34. struct hidg_func_descriptor *func;
  35. };
  36. static LIST_HEAD(hidg_func_list);
  37. /*-------------------------------------------------------------------------*/
  38. USB_GADGET_COMPOSITE_OPTIONS();
  39. static struct usb_device_descriptor device_desc = {
  40. .bLength = sizeof device_desc,
  41. .bDescriptorType = USB_DT_DEVICE,
  42. .bcdUSB = cpu_to_le16(0x0200),
  43. /* .bDeviceClass = USB_CLASS_COMM, */
  44. /* .bDeviceSubClass = 0, */
  45. /* .bDeviceProtocol = 0, */
  46. .bDeviceClass = USB_CLASS_PER_INTERFACE,
  47. .bDeviceSubClass = 0,
  48. .bDeviceProtocol = 0,
  49. /* .bMaxPacketSize0 = f(hardware) */
  50. /* Vendor and product id can be overridden by module parameters. */
  51. .idVendor = cpu_to_le16(HIDG_VENDOR_NUM),
  52. .idProduct = cpu_to_le16(HIDG_PRODUCT_NUM),
  53. /* .bcdDevice = f(hardware) */
  54. /* .iManufacturer = DYNAMIC */
  55. /* .iProduct = DYNAMIC */
  56. /* NO SERIAL NUMBER */
  57. .bNumConfigurations = 1,
  58. };
  59. static struct usb_otg_descriptor otg_descriptor = {
  60. .bLength = sizeof otg_descriptor,
  61. .bDescriptorType = USB_DT_OTG,
  62. /* REVISIT SRP-only hardware is possible, although
  63. * it would not be called "OTG" ...
  64. */
  65. .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
  66. };
  67. static const struct usb_descriptor_header *otg_desc[] = {
  68. (struct usb_descriptor_header *) &otg_descriptor,
  69. NULL,
  70. };
  71. /* string IDs are assigned dynamically */
  72. static char manufacturer[50];
  73. static struct usb_string strings_dev[] = {
  74. [USB_GADGET_MANUFACTURER_IDX].s = manufacturer,
  75. [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC,
  76. [USB_GADGET_SERIAL_IDX].s = "",
  77. { } /* end of list */
  78. };
  79. static struct usb_gadget_strings stringtab_dev = {
  80. .language = 0x0409, /* en-us */
  81. .strings = strings_dev,
  82. };
  83. static struct usb_gadget_strings *dev_strings[] = {
  84. &stringtab_dev,
  85. NULL,
  86. };
  87. /****************************** Configurations ******************************/
  88. static int __init do_config(struct usb_configuration *c)
  89. {
  90. struct hidg_func_node *e;
  91. int func = 0, status = 0;
  92. if (gadget_is_otg(c->cdev->gadget)) {
  93. c->descriptors = otg_desc;
  94. c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  95. }
  96. list_for_each_entry(e, &hidg_func_list, node) {
  97. status = hidg_bind_config(c, e->func, func++);
  98. if (status)
  99. break;
  100. }
  101. return status;
  102. }
  103. static struct usb_configuration config_driver = {
  104. .label = "HID Gadget",
  105. .bConfigurationValue = 1,
  106. /* .iConfiguration = DYNAMIC */
  107. .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
  108. };
  109. /****************************** Gadget Bind ******************************/
  110. static int __init hid_bind(struct usb_composite_dev *cdev)
  111. {
  112. struct usb_gadget *gadget = cdev->gadget;
  113. struct list_head *tmp;
  114. int status, gcnum, funcs = 0;
  115. list_for_each(tmp, &hidg_func_list)
  116. funcs++;
  117. if (!funcs)
  118. return -ENODEV;
  119. /* set up HID */
  120. status = ghid_setup(cdev->gadget, funcs);
  121. if (status < 0)
  122. return status;
  123. gcnum = usb_gadget_controller_number(gadget);
  124. if (gcnum >= 0)
  125. device_desc.bcdDevice = cpu_to_le16(0x0300 | gcnum);
  126. else
  127. device_desc.bcdDevice = cpu_to_le16(0x0300 | 0x0099);
  128. /* Allocate string descriptor numbers ... note that string
  129. * contents can be overridden by the composite_dev glue.
  130. */
  131. /* device descriptor strings: manufacturer, product */
  132. snprintf(manufacturer, sizeof manufacturer, "%s %s with %s",
  133. init_utsname()->sysname, init_utsname()->release,
  134. gadget->name);
  135. status = usb_string_ids_tab(cdev, strings_dev);
  136. if (status < 0)
  137. return status;
  138. device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
  139. device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
  140. /* register our configuration */
  141. status = usb_add_config(cdev, &config_driver, do_config);
  142. if (status < 0)
  143. return status;
  144. usb_composite_overwrite_options(cdev, &coverwrite);
  145. dev_info(&gadget->dev, DRIVER_DESC ", version: " DRIVER_VERSION "\n");
  146. return 0;
  147. }
  148. static int __exit hid_unbind(struct usb_composite_dev *cdev)
  149. {
  150. ghid_cleanup();
  151. return 0;
  152. }
  153. static int __init hidg_plat_driver_probe(struct platform_device *pdev)
  154. {
  155. struct hidg_func_descriptor *func = pdev->dev.platform_data;
  156. struct hidg_func_node *entry;
  157. if (!func) {
  158. dev_err(&pdev->dev, "Platform data missing\n");
  159. return -ENODEV;
  160. }
  161. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  162. if (!entry)
  163. return -ENOMEM;
  164. entry->func = func;
  165. list_add_tail(&entry->node, &hidg_func_list);
  166. return 0;
  167. }
  168. static int __devexit hidg_plat_driver_remove(struct platform_device *pdev)
  169. {
  170. struct hidg_func_node *e, *n;
  171. list_for_each_entry_safe(e, n, &hidg_func_list, node) {
  172. list_del(&e->node);
  173. kfree(e);
  174. }
  175. return 0;
  176. }
  177. /****************************** Some noise ******************************/
  178. static __refdata struct usb_composite_driver hidg_driver = {
  179. .name = "g_hid",
  180. .dev = &device_desc,
  181. .strings = dev_strings,
  182. .max_speed = USB_SPEED_HIGH,
  183. .bind = hid_bind,
  184. .unbind = __exit_p(hid_unbind),
  185. };
  186. static struct platform_driver hidg_plat_driver = {
  187. .remove = __devexit_p(hidg_plat_driver_remove),
  188. .driver = {
  189. .owner = THIS_MODULE,
  190. .name = "hidg",
  191. },
  192. };
  193. MODULE_DESCRIPTION(DRIVER_DESC);
  194. MODULE_AUTHOR("Fabien Chouteau, Peter Korsgaard");
  195. MODULE_LICENSE("GPL");
  196. static int __init hidg_init(void)
  197. {
  198. int status;
  199. status = platform_driver_probe(&hidg_plat_driver,
  200. hidg_plat_driver_probe);
  201. if (status < 0)
  202. return status;
  203. status = usb_composite_probe(&hidg_driver);
  204. if (status < 0)
  205. platform_driver_unregister(&hidg_plat_driver);
  206. return status;
  207. }
  208. module_init(hidg_init);
  209. static void __exit hidg_cleanup(void)
  210. {
  211. platform_driver_unregister(&hidg_plat_driver);
  212. usb_composite_unregister(&hidg_driver);
  213. }
  214. module_exit(hidg_cleanup);