hid.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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 <[email protected]>
  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. status = PTR_ERR(e->f);
  80. goto put;
  81. }
  82. status = usb_add_function(c, e->f);
  83. if (status < 0) {
  84. usb_put_function(e->f);
  85. goto put;
  86. }
  87. }
  88. return 0;
  89. put:
  90. list_for_each_entry(n, &hidg_func_list, node) {
  91. if (n == e)
  92. break;
  93. usb_remove_function(c, n->f);
  94. usb_put_function(n->f);
  95. }
  96. return status;
  97. }
  98. static struct usb_configuration config_driver = {
  99. .label = "HID Gadget",
  100. .bConfigurationValue = 1,
  101. /* .iConfiguration = DYNAMIC */
  102. .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
  103. };
  104. /****************************** Gadget Bind ******************************/
  105. static int hid_bind(struct usb_composite_dev *cdev)
  106. {
  107. struct usb_gadget *gadget = cdev->gadget;
  108. struct list_head *tmp;
  109. struct hidg_func_node *n = NULL, *m, *iter_n;
  110. struct f_hid_opts *hid_opts;
  111. int status, funcs = 0;
  112. list_for_each(tmp, &hidg_func_list)
  113. funcs++;
  114. if (!funcs)
  115. return -ENODEV;
  116. list_for_each_entry(iter_n, &hidg_func_list, node) {
  117. iter_n->fi = usb_get_function_instance("hid");
  118. if (IS_ERR(iter_n->fi)) {
  119. status = PTR_ERR(iter_n->fi);
  120. n = iter_n;
  121. goto put;
  122. }
  123. hid_opts = container_of(iter_n->fi, struct f_hid_opts, func_inst);
  124. hid_opts->subclass = iter_n->func->subclass;
  125. hid_opts->protocol = iter_n->func->protocol;
  126. hid_opts->report_length = iter_n->func->report_length;
  127. hid_opts->report_desc_length = iter_n->func->report_desc_length;
  128. hid_opts->report_desc = iter_n->func->report_desc;
  129. }
  130. /* Allocate string descriptor numbers ... note that string
  131. * contents can be overridden by the composite_dev glue.
  132. */
  133. status = usb_string_ids_tab(cdev, strings_dev);
  134. if (status < 0)
  135. goto put;
  136. device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
  137. device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
  138. if (gadget_is_otg(gadget) && !otg_desc[0]) {
  139. struct usb_descriptor_header *usb_desc;
  140. usb_desc = usb_otg_descriptor_alloc(gadget);
  141. if (!usb_desc) {
  142. status = -ENOMEM;
  143. goto put;
  144. }
  145. usb_otg_descriptor_init(gadget, usb_desc);
  146. otg_desc[0] = usb_desc;
  147. otg_desc[1] = NULL;
  148. }
  149. /* register our configuration */
  150. status = usb_add_config(cdev, &config_driver, do_config);
  151. if (status < 0)
  152. goto free_otg_desc;
  153. usb_composite_overwrite_options(cdev, &coverwrite);
  154. dev_info(&gadget->dev, DRIVER_DESC ", version: " DRIVER_VERSION "\n");
  155. return 0;
  156. free_otg_desc:
  157. kfree(otg_desc[0]);
  158. otg_desc[0] = NULL;
  159. put:
  160. list_for_each_entry(m, &hidg_func_list, node) {
  161. if (m == n)
  162. break;
  163. usb_put_function_instance(m->fi);
  164. }
  165. return status;
  166. }
  167. static int hid_unbind(struct usb_composite_dev *cdev)
  168. {
  169. struct hidg_func_node *n;
  170. list_for_each_entry(n, &hidg_func_list, node) {
  171. usb_put_function(n->f);
  172. usb_put_function_instance(n->fi);
  173. }
  174. kfree(otg_desc[0]);
  175. otg_desc[0] = NULL;
  176. return 0;
  177. }
  178. static int hidg_plat_driver_probe(struct platform_device *pdev)
  179. {
  180. struct hidg_func_descriptor *func = dev_get_platdata(&pdev->dev);
  181. struct hidg_func_node *entry;
  182. if (!func) {
  183. dev_err(&pdev->dev, "Platform data missing\n");
  184. return -ENODEV;
  185. }
  186. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  187. if (!entry)
  188. return -ENOMEM;
  189. entry->func = func;
  190. list_add_tail(&entry->node, &hidg_func_list);
  191. return 0;
  192. }
  193. static int hidg_plat_driver_remove(struct platform_device *pdev)
  194. {
  195. struct hidg_func_node *e, *n;
  196. list_for_each_entry_safe(e, n, &hidg_func_list, node) {
  197. list_del(&e->node);
  198. kfree(e);
  199. }
  200. return 0;
  201. }
  202. /****************************** Some noise ******************************/
  203. static struct usb_composite_driver hidg_driver = {
  204. .name = "g_hid",
  205. .dev = &device_desc,
  206. .strings = dev_strings,
  207. .max_speed = USB_SPEED_HIGH,
  208. .bind = hid_bind,
  209. .unbind = hid_unbind,
  210. };
  211. static struct platform_driver hidg_plat_driver = {
  212. .remove = hidg_plat_driver_remove,
  213. .driver = {
  214. .name = "hidg",
  215. },
  216. };
  217. MODULE_DESCRIPTION(DRIVER_DESC);
  218. MODULE_AUTHOR("Fabien Chouteau, Peter Korsgaard");
  219. MODULE_LICENSE("GPL");
  220. static int __init hidg_init(void)
  221. {
  222. int status;
  223. status = platform_driver_probe(&hidg_plat_driver,
  224. hidg_plat_driver_probe);
  225. if (status < 0)
  226. return status;
  227. status = usb_composite_probe(&hidg_driver);
  228. if (status < 0)
  229. platform_driver_unregister(&hidg_plat_driver);
  230. return status;
  231. }
  232. module_init(hidg_init);
  233. static void __exit hidg_cleanup(void)
  234. {
  235. usb_composite_unregister(&hidg_driver);
  236. platform_driver_unregister(&hidg_plat_driver);
  237. }
  238. module_exit(hidg_cleanup);