hid.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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 <linux/usb/g_hid.h>
  19. #include "gadget_chips.h"
  20. #define DRIVER_DESC "HID Gadget"
  21. #define DRIVER_VERSION "2010/03/16"
  22. #include "u_hid.h"
  23. /*-------------------------------------------------------------------------*/
  24. #define HIDG_VENDOR_NUM 0x0525 /* XXX NetChip */
  25. #define HIDG_PRODUCT_NUM 0xa4ac /* Linux-USB HID gadget */
  26. /*-------------------------------------------------------------------------*/
  27. struct hidg_func_node {
  28. struct usb_function_instance *fi;
  29. struct usb_function *f;
  30. struct list_head node;
  31. struct hidg_func_descriptor *func;
  32. };
  33. static LIST_HEAD(hidg_func_list);
  34. /*-------------------------------------------------------------------------*/
  35. USB_GADGET_COMPOSITE_OPTIONS();
  36. static struct usb_device_descriptor device_desc = {
  37. .bLength = sizeof device_desc,
  38. .bDescriptorType = USB_DT_DEVICE,
  39. .bcdUSB = cpu_to_le16(0x0200),
  40. /* .bDeviceClass = USB_CLASS_COMM, */
  41. /* .bDeviceSubClass = 0, */
  42. /* .bDeviceProtocol = 0, */
  43. .bDeviceClass = USB_CLASS_PER_INTERFACE,
  44. .bDeviceSubClass = 0,
  45. .bDeviceProtocol = 0,
  46. /* .bMaxPacketSize0 = f(hardware) */
  47. /* Vendor and product id can be overridden by module parameters. */
  48. .idVendor = cpu_to_le16(HIDG_VENDOR_NUM),
  49. .idProduct = cpu_to_le16(HIDG_PRODUCT_NUM),
  50. /* .bcdDevice = f(hardware) */
  51. /* .iManufacturer = DYNAMIC */
  52. /* .iProduct = DYNAMIC */
  53. /* NO SERIAL NUMBER */
  54. .bNumConfigurations = 1,
  55. };
  56. static struct usb_otg_descriptor otg_descriptor = {
  57. .bLength = sizeof otg_descriptor,
  58. .bDescriptorType = USB_DT_OTG,
  59. /* REVISIT SRP-only hardware is possible, although
  60. * it would not be called "OTG" ...
  61. */
  62. .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
  63. };
  64. static const struct usb_descriptor_header *otg_desc[] = {
  65. (struct usb_descriptor_header *) &otg_descriptor,
  66. NULL,
  67. };
  68. /* string IDs are assigned dynamically */
  69. static struct usb_string strings_dev[] = {
  70. [USB_GADGET_MANUFACTURER_IDX].s = "",
  71. [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC,
  72. [USB_GADGET_SERIAL_IDX].s = "",
  73. { } /* end of list */
  74. };
  75. static struct usb_gadget_strings stringtab_dev = {
  76. .language = 0x0409, /* en-us */
  77. .strings = strings_dev,
  78. };
  79. static struct usb_gadget_strings *dev_strings[] = {
  80. &stringtab_dev,
  81. NULL,
  82. };
  83. /****************************** Configurations ******************************/
  84. static int __init do_config(struct usb_configuration *c)
  85. {
  86. struct hidg_func_node *e, *n;
  87. int status = 0;
  88. if (gadget_is_otg(c->cdev->gadget)) {
  89. c->descriptors = otg_desc;
  90. c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  91. }
  92. list_for_each_entry(e, &hidg_func_list, node) {
  93. e->f = usb_get_function(e->fi);
  94. if (IS_ERR(e->f))
  95. goto put;
  96. status = usb_add_function(c, e->f);
  97. if (status < 0) {
  98. usb_put_function(e->f);
  99. goto put;
  100. }
  101. }
  102. return 0;
  103. put:
  104. list_for_each_entry(n, &hidg_func_list, node) {
  105. if (n == e)
  106. break;
  107. usb_remove_function(c, n->f);
  108. usb_put_function(n->f);
  109. }
  110. return status;
  111. }
  112. static struct usb_configuration config_driver = {
  113. .label = "HID Gadget",
  114. .bConfigurationValue = 1,
  115. /* .iConfiguration = DYNAMIC */
  116. .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
  117. };
  118. /****************************** Gadget Bind ******************************/
  119. static int __init hid_bind(struct usb_composite_dev *cdev)
  120. {
  121. struct usb_gadget *gadget = cdev->gadget;
  122. struct list_head *tmp;
  123. struct hidg_func_node *n, *m;
  124. struct f_hid_opts *hid_opts;
  125. int status, funcs = 0;
  126. list_for_each(tmp, &hidg_func_list)
  127. funcs++;
  128. if (!funcs)
  129. return -ENODEV;
  130. list_for_each_entry(n, &hidg_func_list, node) {
  131. n->fi = usb_get_function_instance("hid");
  132. if (IS_ERR(n->fi)) {
  133. status = PTR_ERR(n->fi);
  134. goto put;
  135. }
  136. hid_opts = container_of(n->fi, struct f_hid_opts, func_inst);
  137. hid_opts->subclass = n->func->subclass;
  138. hid_opts->protocol = n->func->protocol;
  139. hid_opts->report_length = n->func->report_length;
  140. hid_opts->report_desc_length = n->func->report_desc_length;
  141. hid_opts->report_desc = n->func->report_desc;
  142. }
  143. /* Allocate string descriptor numbers ... note that string
  144. * contents can be overridden by the composite_dev glue.
  145. */
  146. status = usb_string_ids_tab(cdev, strings_dev);
  147. if (status < 0)
  148. goto put;
  149. device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
  150. device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
  151. /* register our configuration */
  152. status = usb_add_config(cdev, &config_driver, do_config);
  153. if (status < 0)
  154. goto put;
  155. usb_composite_overwrite_options(cdev, &coverwrite);
  156. dev_info(&gadget->dev, DRIVER_DESC ", version: " DRIVER_VERSION "\n");
  157. return 0;
  158. put:
  159. list_for_each_entry(m, &hidg_func_list, node) {
  160. if (m == n)
  161. break;
  162. usb_put_function_instance(m->fi);
  163. }
  164. return status;
  165. }
  166. static int __exit hid_unbind(struct usb_composite_dev *cdev)
  167. {
  168. struct hidg_func_node *n;
  169. list_for_each_entry(n, &hidg_func_list, node) {
  170. usb_put_function(n->f);
  171. usb_put_function_instance(n->fi);
  172. }
  173. return 0;
  174. }
  175. static int __init hidg_plat_driver_probe(struct platform_device *pdev)
  176. {
  177. struct hidg_func_descriptor *func = dev_get_platdata(&pdev->dev);
  178. struct hidg_func_node *entry;
  179. if (!func) {
  180. dev_err(&pdev->dev, "Platform data missing\n");
  181. return -ENODEV;
  182. }
  183. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  184. if (!entry)
  185. return -ENOMEM;
  186. entry->func = func;
  187. list_add_tail(&entry->node, &hidg_func_list);
  188. return 0;
  189. }
  190. static int hidg_plat_driver_remove(struct platform_device *pdev)
  191. {
  192. struct hidg_func_node *e, *n;
  193. list_for_each_entry_safe(e, n, &hidg_func_list, node) {
  194. list_del(&e->node);
  195. kfree(e);
  196. }
  197. return 0;
  198. }
  199. /****************************** Some noise ******************************/
  200. static __refdata struct usb_composite_driver hidg_driver = {
  201. .name = "g_hid",
  202. .dev = &device_desc,
  203. .strings = dev_strings,
  204. .max_speed = USB_SPEED_HIGH,
  205. .bind = hid_bind,
  206. .unbind = __exit_p(hid_unbind),
  207. };
  208. static struct platform_driver hidg_plat_driver = {
  209. .remove = hidg_plat_driver_remove,
  210. .driver = {
  211. .name = "hidg",
  212. },
  213. };
  214. MODULE_DESCRIPTION(DRIVER_DESC);
  215. MODULE_AUTHOR("Fabien Chouteau, Peter Korsgaard");
  216. MODULE_LICENSE("GPL");
  217. static int __init hidg_init(void)
  218. {
  219. int status;
  220. status = platform_driver_probe(&hidg_plat_driver,
  221. hidg_plat_driver_probe);
  222. if (status < 0)
  223. return status;
  224. status = usb_composite_probe(&hidg_driver);
  225. if (status < 0)
  226. platform_driver_unregister(&hidg_plat_driver);
  227. return status;
  228. }
  229. module_init(hidg_init);
  230. static void __exit hidg_cleanup(void)
  231. {
  232. usb_composite_unregister(&hidg_driver);
  233. platform_driver_unregister(&hidg_plat_driver);
  234. }
  235. module_exit(hidg_cleanup);