hid.c 6.9 KB

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