hid.c 7.1 KB

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