hid.c 6.6 KB

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