hid.c 6.7 KB

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