hid.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 "usbstring.c"
  31. #include "config.c"
  32. #include "epautoconf.c"
  33. #include "f_hid.c"
  34. struct hidg_func_node {
  35. struct list_head node;
  36. struct hidg_func_descriptor *func;
  37. };
  38. static LIST_HEAD(hidg_func_list);
  39. /*-------------------------------------------------------------------------*/
  40. static struct usb_device_descriptor device_desc = {
  41. .bLength = sizeof device_desc,
  42. .bDescriptorType = USB_DT_DEVICE,
  43. .bcdUSB = cpu_to_le16(0x0200),
  44. /* .bDeviceClass = USB_CLASS_COMM, */
  45. /* .bDeviceSubClass = 0, */
  46. /* .bDeviceProtocol = 0, */
  47. .bDeviceClass = USB_CLASS_PER_INTERFACE,
  48. .bDeviceSubClass = 0,
  49. .bDeviceProtocol = 0,
  50. /* .bMaxPacketSize0 = f(hardware) */
  51. /* Vendor and product id can be overridden by module parameters. */
  52. .idVendor = cpu_to_le16(HIDG_VENDOR_NUM),
  53. .idProduct = cpu_to_le16(HIDG_PRODUCT_NUM),
  54. /* .bcdDevice = f(hardware) */
  55. /* .iManufacturer = DYNAMIC */
  56. /* .iProduct = DYNAMIC */
  57. /* NO SERIAL NUMBER */
  58. .bNumConfigurations = 1,
  59. };
  60. static struct usb_otg_descriptor otg_descriptor = {
  61. .bLength = sizeof otg_descriptor,
  62. .bDescriptorType = USB_DT_OTG,
  63. /* REVISIT SRP-only hardware is possible, although
  64. * it would not be called "OTG" ...
  65. */
  66. .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
  67. };
  68. static const struct usb_descriptor_header *otg_desc[] = {
  69. (struct usb_descriptor_header *) &otg_descriptor,
  70. NULL,
  71. };
  72. /* string IDs are assigned dynamically */
  73. #define STRING_MANUFACTURER_IDX 0
  74. #define STRING_PRODUCT_IDX 1
  75. static char manufacturer[50];
  76. static struct usb_string strings_dev[] = {
  77. [STRING_MANUFACTURER_IDX].s = manufacturer,
  78. [STRING_PRODUCT_IDX].s = DRIVER_DESC,
  79. { } /* end of list */
  80. };
  81. static struct usb_gadget_strings stringtab_dev = {
  82. .language = 0x0409, /* en-us */
  83. .strings = strings_dev,
  84. };
  85. static struct usb_gadget_strings *dev_strings[] = {
  86. &stringtab_dev,
  87. NULL,
  88. };
  89. /****************************** Configurations ******************************/
  90. static int __init do_config(struct usb_configuration *c)
  91. {
  92. struct hidg_func_node *e;
  93. int func = 0, status = 0;
  94. if (gadget_is_otg(c->cdev->gadget)) {
  95. c->descriptors = otg_desc;
  96. c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  97. }
  98. list_for_each_entry(e, &hidg_func_list, node) {
  99. status = hidg_bind_config(c, e->func, func++);
  100. if (status)
  101. break;
  102. }
  103. return status;
  104. }
  105. static struct usb_configuration config_driver = {
  106. .label = "HID Gadget",
  107. .bConfigurationValue = 1,
  108. /* .iConfiguration = DYNAMIC */
  109. .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
  110. };
  111. /****************************** Gadget Bind ******************************/
  112. static int __init hid_bind(struct usb_composite_dev *cdev)
  113. {
  114. struct usb_gadget *gadget = cdev->gadget;
  115. struct list_head *tmp;
  116. int status, gcnum, funcs = 0;
  117. list_for_each(tmp, &hidg_func_list)
  118. funcs++;
  119. if (!funcs)
  120. return -ENODEV;
  121. /* set up HID */
  122. status = ghid_setup(cdev->gadget, funcs);
  123. if (status < 0)
  124. return status;
  125. gcnum = usb_gadget_controller_number(gadget);
  126. if (gcnum >= 0)
  127. device_desc.bcdDevice = cpu_to_le16(0x0300 | gcnum);
  128. else
  129. device_desc.bcdDevice = cpu_to_le16(0x0300 | 0x0099);
  130. /* Allocate string descriptor numbers ... note that string
  131. * contents can be overridden by the composite_dev glue.
  132. */
  133. /* device descriptor strings: manufacturer, product */
  134. snprintf(manufacturer, sizeof manufacturer, "%s %s with %s",
  135. init_utsname()->sysname, init_utsname()->release,
  136. gadget->name);
  137. status = usb_string_id(cdev);
  138. if (status < 0)
  139. return status;
  140. strings_dev[STRING_MANUFACTURER_IDX].id = status;
  141. device_desc.iManufacturer = status;
  142. status = usb_string_id(cdev);
  143. if (status < 0)
  144. return status;
  145. strings_dev[STRING_PRODUCT_IDX].id = status;
  146. device_desc.iProduct = status;
  147. /* register our configuration */
  148. status = usb_add_config(cdev, &config_driver, do_config);
  149. if (status < 0)
  150. return status;
  151. dev_info(&gadget->dev, DRIVER_DESC ", version: " DRIVER_VERSION "\n");
  152. return 0;
  153. }
  154. static int __exit hid_unbind(struct usb_composite_dev *cdev)
  155. {
  156. ghid_cleanup();
  157. return 0;
  158. }
  159. static int __init hidg_plat_driver_probe(struct platform_device *pdev)
  160. {
  161. struct hidg_func_descriptor *func = pdev->dev.platform_data;
  162. struct hidg_func_node *entry;
  163. if (!func) {
  164. dev_err(&pdev->dev, "Platform data missing\n");
  165. return -ENODEV;
  166. }
  167. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  168. if (!entry)
  169. return -ENOMEM;
  170. entry->func = func;
  171. list_add_tail(&entry->node, &hidg_func_list);
  172. return 0;
  173. }
  174. static int __devexit hidg_plat_driver_remove(struct platform_device *pdev)
  175. {
  176. struct hidg_func_node *e, *n;
  177. list_for_each_entry_safe(e, n, &hidg_func_list, node) {
  178. list_del(&e->node);
  179. kfree(e);
  180. }
  181. return 0;
  182. }
  183. /****************************** Some noise ******************************/
  184. static struct usb_composite_driver hidg_driver = {
  185. .name = "g_hid",
  186. .dev = &device_desc,
  187. .strings = dev_strings,
  188. .max_speed = USB_SPEED_HIGH,
  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, hid_bind);
  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);