hid.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 <linux/module.h>
  17. #include <linux/usb/composite.h>
  18. #include "gadget_chips.h"
  19. #define DRIVER_DESC "HID Gadget"
  20. #define DRIVER_VERSION "2010/03/16"
  21. /*-------------------------------------------------------------------------*/
  22. #define HIDG_VENDOR_NUM 0x0525 /* XXX NetChip */
  23. #define HIDG_PRODUCT_NUM 0xa4ac /* Linux-USB HID gadget */
  24. /*-------------------------------------------------------------------------*/
  25. /*
  26. * kbuild is not very cooperative with respect to linking separately
  27. * compiled library objects into one module. So for now we won't use
  28. * separate compilation ... ensuring init/exit sections work to shrink
  29. * the runtime footprint, and giving us at least some parts of what
  30. * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
  31. */
  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. USB_GADGET_COMPOSITE_OPTIONS();
  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. static struct usb_string strings_dev[] = {
  74. [USB_GADGET_MANUFACTURER_IDX].s = "",
  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, 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. /* Allocate string descriptor numbers ... note that string
  124. * contents can be overridden by the composite_dev glue.
  125. */
  126. status = usb_string_ids_tab(cdev, strings_dev);
  127. if (status < 0)
  128. return status;
  129. device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
  130. device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
  131. /* register our configuration */
  132. status = usb_add_config(cdev, &config_driver, do_config);
  133. if (status < 0)
  134. return status;
  135. usb_composite_overwrite_options(cdev, &coverwrite);
  136. dev_info(&gadget->dev, DRIVER_DESC ", version: " DRIVER_VERSION "\n");
  137. return 0;
  138. }
  139. static int __exit hid_unbind(struct usb_composite_dev *cdev)
  140. {
  141. ghid_cleanup();
  142. return 0;
  143. }
  144. static int __init hidg_plat_driver_probe(struct platform_device *pdev)
  145. {
  146. struct hidg_func_descriptor *func = pdev->dev.platform_data;
  147. struct hidg_func_node *entry;
  148. if (!func) {
  149. dev_err(&pdev->dev, "Platform data missing\n");
  150. return -ENODEV;
  151. }
  152. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  153. if (!entry)
  154. return -ENOMEM;
  155. entry->func = func;
  156. list_add_tail(&entry->node, &hidg_func_list);
  157. return 0;
  158. }
  159. static int hidg_plat_driver_remove(struct platform_device *pdev)
  160. {
  161. struct hidg_func_node *e, *n;
  162. list_for_each_entry_safe(e, n, &hidg_func_list, node) {
  163. list_del(&e->node);
  164. kfree(e);
  165. }
  166. return 0;
  167. }
  168. /****************************** Some noise ******************************/
  169. static __refdata struct usb_composite_driver hidg_driver = {
  170. .name = "g_hid",
  171. .dev = &device_desc,
  172. .strings = dev_strings,
  173. .max_speed = USB_SPEED_HIGH,
  174. .bind = hid_bind,
  175. .unbind = __exit_p(hid_unbind),
  176. };
  177. static struct platform_driver hidg_plat_driver = {
  178. .remove = hidg_plat_driver_remove,
  179. .driver = {
  180. .owner = THIS_MODULE,
  181. .name = "hidg",
  182. },
  183. };
  184. MODULE_DESCRIPTION(DRIVER_DESC);
  185. MODULE_AUTHOR("Fabien Chouteau, Peter Korsgaard");
  186. MODULE_LICENSE("GPL");
  187. static int __init hidg_init(void)
  188. {
  189. int status;
  190. status = platform_driver_probe(&hidg_plat_driver,
  191. hidg_plat_driver_probe);
  192. if (status < 0)
  193. return status;
  194. status = usb_composite_probe(&hidg_driver);
  195. if (status < 0)
  196. platform_driver_unregister(&hidg_plat_driver);
  197. return status;
  198. }
  199. module_init(hidg_init);
  200. static void __exit hidg_cleanup(void)
  201. {
  202. platform_driver_unregister(&hidg_plat_driver);
  203. usb_composite_unregister(&hidg_driver);
  204. }
  205. module_exit(hidg_cleanup);