hid.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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 struct usb_string strings_dev[] = {
  73. [USB_GADGET_MANUFACTURER_IDX].s = "",
  74. [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC,
  75. [USB_GADGET_SERIAL_IDX].s = "",
  76. { } /* end of list */
  77. };
  78. static struct usb_gadget_strings stringtab_dev = {
  79. .language = 0x0409, /* en-us */
  80. .strings = strings_dev,
  81. };
  82. static struct usb_gadget_strings *dev_strings[] = {
  83. &stringtab_dev,
  84. NULL,
  85. };
  86. /****************************** Configurations ******************************/
  87. static int __init do_config(struct usb_configuration *c)
  88. {
  89. struct hidg_func_node *e;
  90. int func = 0, status = 0;
  91. if (gadget_is_otg(c->cdev->gadget)) {
  92. c->descriptors = otg_desc;
  93. c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  94. }
  95. list_for_each_entry(e, &hidg_func_list, node) {
  96. status = hidg_bind_config(c, e->func, func++);
  97. if (status)
  98. break;
  99. }
  100. return status;
  101. }
  102. static struct usb_configuration config_driver = {
  103. .label = "HID Gadget",
  104. .bConfigurationValue = 1,
  105. /* .iConfiguration = DYNAMIC */
  106. .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
  107. };
  108. /****************************** Gadget Bind ******************************/
  109. static int __init hid_bind(struct usb_composite_dev *cdev)
  110. {
  111. struct usb_gadget *gadget = cdev->gadget;
  112. struct list_head *tmp;
  113. int status, gcnum, funcs = 0;
  114. list_for_each(tmp, &hidg_func_list)
  115. funcs++;
  116. if (!funcs)
  117. return -ENODEV;
  118. /* set up HID */
  119. status = ghid_setup(cdev->gadget, funcs);
  120. if (status < 0)
  121. return status;
  122. gcnum = usb_gadget_controller_number(gadget);
  123. if (gcnum >= 0)
  124. device_desc.bcdDevice = cpu_to_le16(0x0300 | gcnum);
  125. else
  126. device_desc.bcdDevice = cpu_to_le16(0x0300 | 0x0099);
  127. /* Allocate string descriptor numbers ... note that string
  128. * contents can be overridden by the composite_dev glue.
  129. */
  130. status = usb_string_ids_tab(cdev, strings_dev);
  131. if (status < 0)
  132. return status;
  133. device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
  134. device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
  135. /* register our configuration */
  136. status = usb_add_config(cdev, &config_driver, do_config);
  137. if (status < 0)
  138. return status;
  139. usb_composite_overwrite_options(cdev, &coverwrite);
  140. dev_info(&gadget->dev, DRIVER_DESC ", version: " DRIVER_VERSION "\n");
  141. return 0;
  142. }
  143. static int __exit hid_unbind(struct usb_composite_dev *cdev)
  144. {
  145. ghid_cleanup();
  146. return 0;
  147. }
  148. static int __init hidg_plat_driver_probe(struct platform_device *pdev)
  149. {
  150. struct hidg_func_descriptor *func = pdev->dev.platform_data;
  151. struct hidg_func_node *entry;
  152. if (!func) {
  153. dev_err(&pdev->dev, "Platform data missing\n");
  154. return -ENODEV;
  155. }
  156. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  157. if (!entry)
  158. return -ENOMEM;
  159. entry->func = func;
  160. list_add_tail(&entry->node, &hidg_func_list);
  161. return 0;
  162. }
  163. static int __devexit hidg_plat_driver_remove(struct platform_device *pdev)
  164. {
  165. struct hidg_func_node *e, *n;
  166. list_for_each_entry_safe(e, n, &hidg_func_list, node) {
  167. list_del(&e->node);
  168. kfree(e);
  169. }
  170. return 0;
  171. }
  172. /****************************** Some noise ******************************/
  173. static __refdata struct usb_composite_driver hidg_driver = {
  174. .name = "g_hid",
  175. .dev = &device_desc,
  176. .strings = dev_strings,
  177. .max_speed = USB_SPEED_HIGH,
  178. .bind = hid_bind,
  179. .unbind = __exit_p(hid_unbind),
  180. };
  181. static struct platform_driver hidg_plat_driver = {
  182. .remove = __devexit_p(hidg_plat_driver_remove),
  183. .driver = {
  184. .owner = THIS_MODULE,
  185. .name = "hidg",
  186. },
  187. };
  188. MODULE_DESCRIPTION(DRIVER_DESC);
  189. MODULE_AUTHOR("Fabien Chouteau, Peter Korsgaard");
  190. MODULE_LICENSE("GPL");
  191. static int __init hidg_init(void)
  192. {
  193. int status;
  194. status = platform_driver_probe(&hidg_plat_driver,
  195. hidg_plat_driver_probe);
  196. if (status < 0)
  197. return status;
  198. status = usb_composite_probe(&hidg_driver);
  199. if (status < 0)
  200. platform_driver_unregister(&hidg_plat_driver);
  201. return status;
  202. }
  203. module_init(hidg_init);
  204. static void __exit hidg_cleanup(void)
  205. {
  206. platform_driver_unregister(&hidg_plat_driver);
  207. usb_composite_unregister(&hidg_driver);
  208. }
  209. module_exit(hidg_cleanup);