zero.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*
  2. * zero.c -- Gadget Zero, for USB development
  3. *
  4. * Copyright (C) 2003-2008 David Brownell
  5. * Copyright (C) 2008 by Nokia Corporation
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. /*
  22. * Gadget Zero only needs two bulk endpoints, and is an example of how you
  23. * can write a hardware-agnostic gadget driver running inside a USB device.
  24. * Some hardware details are visible, but don't affect most of the driver.
  25. *
  26. * Use it with the Linux host/master side "usbtest" driver to get a basic
  27. * functional test of your device-side usb stack, or with "usb-skeleton".
  28. *
  29. * It supports two similar configurations. One sinks whatever the usb host
  30. * writes, and in return sources zeroes. The other loops whatever the host
  31. * writes back, so the host can read it.
  32. *
  33. * Many drivers will only have one configuration, letting them be much
  34. * simpler if they also don't support high speed operation (like this
  35. * driver does).
  36. *
  37. * Why is *this* driver using two configurations, rather than setting up
  38. * two interfaces with different functions? To help verify that multiple
  39. * configuration infrastucture is working correctly; also, so that it can
  40. * work with low capability USB controllers without four bulk endpoints.
  41. */
  42. /*
  43. * driver assumes self-powered hardware, and
  44. * has no way for users to trigger remote wakeup.
  45. */
  46. /* #define VERBOSE_DEBUG */
  47. #include <linux/kernel.h>
  48. #include <linux/utsname.h>
  49. #include <linux/device.h>
  50. #include "g_zero.h"
  51. #include "gadget_chips.h"
  52. /*-------------------------------------------------------------------------*/
  53. /*
  54. * Kbuild is not very cooperative with respect to linking separately
  55. * compiled library objects into one module. So for now we won't use
  56. * separate compilation ... ensuring init/exit sections work to shrink
  57. * the runtime footprint, and giving us at least some parts of what
  58. * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
  59. */
  60. #include "composite.c"
  61. #include "usbstring.c"
  62. #include "config.c"
  63. #include "epautoconf.c"
  64. #include "f_sourcesink.c"
  65. #include "f_loopback.c"
  66. /*-------------------------------------------------------------------------*/
  67. #define DRIVER_VERSION "Cinco de Mayo 2008"
  68. static const char longname[] = "Gadget Zero";
  69. unsigned buflen = 4096;
  70. module_param(buflen, uint, 0);
  71. /*
  72. * Normally the "loopback" configuration is second (index 1) so
  73. * it's not the default. Here's where to change that order, to
  74. * work better with hosts where config changes are problematic or
  75. * controllers (like original superh) that only support one config.
  76. */
  77. static int loopdefault = 0;
  78. module_param(loopdefault, bool, S_IRUGO|S_IWUSR);
  79. /*-------------------------------------------------------------------------*/
  80. /* Thanks to NetChip Technologies for donating this product ID.
  81. *
  82. * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
  83. * Instead: allocate your own, using normal USB-IF procedures.
  84. */
  85. #ifndef CONFIG_USB_ZERO_HNPTEST
  86. #define DRIVER_VENDOR_NUM 0x0525 /* NetChip */
  87. #define DRIVER_PRODUCT_NUM 0xa4a0 /* Linux-USB "Gadget Zero" */
  88. #else
  89. #define DRIVER_VENDOR_NUM 0x1a0a /* OTG test device IDs */
  90. #define DRIVER_PRODUCT_NUM 0xbadd
  91. #endif
  92. /*-------------------------------------------------------------------------*/
  93. static struct usb_device_descriptor device_desc = {
  94. .bLength = sizeof device_desc,
  95. .bDescriptorType = USB_DT_DEVICE,
  96. .bcdUSB = __constant_cpu_to_le16(0x0200),
  97. .bDeviceClass = USB_CLASS_VENDOR_SPEC,
  98. .idVendor = __constant_cpu_to_le16(DRIVER_VENDOR_NUM),
  99. .idProduct = __constant_cpu_to_le16(DRIVER_PRODUCT_NUM),
  100. .bNumConfigurations = 2,
  101. };
  102. #ifdef CONFIG_USB_OTG
  103. static struct usb_otg_descriptor otg_descriptor = {
  104. .bLength = sizeof otg_descriptor,
  105. .bDescriptorType = USB_DT_OTG,
  106. /* REVISIT SRP-only hardware is possible, although
  107. * it would not be called "OTG" ...
  108. */
  109. .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
  110. };
  111. const struct usb_descriptor_header *otg_desc[] = {
  112. (struct usb_descriptor_header *) &otg_descriptor,
  113. NULL,
  114. };
  115. #endif
  116. /* string IDs are assigned dynamically */
  117. #define STRING_MANUFACTURER_IDX 0
  118. #define STRING_PRODUCT_IDX 1
  119. #define STRING_SERIAL_IDX 2
  120. static char manufacturer[50];
  121. /* default serial number takes at least two packets */
  122. static char serial[] = "0123456789.0123456789.0123456789";
  123. static struct usb_string strings_dev[] = {
  124. [STRING_MANUFACTURER_IDX].s = manufacturer,
  125. [STRING_PRODUCT_IDX].s = longname,
  126. [STRING_SERIAL_IDX].s = serial,
  127. { } /* end of list */
  128. };
  129. static struct usb_gadget_strings stringtab_dev = {
  130. .language = 0x0409, /* en-us */
  131. .strings = strings_dev,
  132. };
  133. static struct usb_gadget_strings *dev_strings[] = {
  134. &stringtab_dev,
  135. NULL,
  136. };
  137. /*-------------------------------------------------------------------------*/
  138. struct usb_request *alloc_ep_req(struct usb_ep *ep)
  139. {
  140. struct usb_request *req;
  141. req = usb_ep_alloc_request(ep, GFP_ATOMIC);
  142. if (req) {
  143. req->length = buflen;
  144. req->buf = kmalloc(buflen, GFP_ATOMIC);
  145. if (!req->buf) {
  146. usb_ep_free_request(ep, req);
  147. req = NULL;
  148. }
  149. }
  150. return req;
  151. }
  152. void free_ep_req(struct usb_ep *ep, struct usb_request *req)
  153. {
  154. kfree(req->buf);
  155. usb_ep_free_request(ep, req);
  156. }
  157. static void disable_ep(struct usb_composite_dev *cdev, struct usb_ep *ep)
  158. {
  159. int value;
  160. if (ep->driver_data) {
  161. value = usb_ep_disable(ep);
  162. if (value < 0)
  163. DBG(cdev, "disable %s --> %d\n",
  164. ep->name, value);
  165. ep->driver_data = NULL;
  166. }
  167. }
  168. void disable_endpoints(struct usb_composite_dev *cdev,
  169. struct usb_ep *in, struct usb_ep *out)
  170. {
  171. disable_ep(cdev, in);
  172. disable_ep(cdev, out);
  173. }
  174. /*-------------------------------------------------------------------------*/
  175. static int __init zero_bind(struct usb_composite_dev *cdev)
  176. {
  177. int gcnum;
  178. struct usb_gadget *gadget = cdev->gadget;
  179. int id;
  180. /* Allocate string descriptor numbers ... note that string
  181. * contents can be overridden by the composite_dev glue.
  182. */
  183. id = usb_string_id(cdev);
  184. if (id < 0)
  185. return id;
  186. strings_dev[STRING_MANUFACTURER_IDX].id = id;
  187. device_desc.iManufacturer = id;
  188. id = usb_string_id(cdev);
  189. if (id < 0)
  190. return id;
  191. strings_dev[STRING_PRODUCT_IDX].id = id;
  192. device_desc.iProduct = id;
  193. id = usb_string_id(cdev);
  194. if (id < 0)
  195. return id;
  196. strings_dev[STRING_SERIAL_IDX].id = id;
  197. device_desc.iSerialNumber = id;
  198. /* Register primary, then secondary configuration. Note that
  199. * SH3 only allows one config...
  200. */
  201. if (loopdefault) {
  202. loopback_add(cdev);
  203. if (!gadget_is_sh(gadget))
  204. sourcesink_add(cdev);
  205. } else {
  206. sourcesink_add(cdev);
  207. if (!gadget_is_sh(gadget))
  208. loopback_add(cdev);
  209. }
  210. gcnum = usb_gadget_controller_number(gadget);
  211. if (gcnum >= 0)
  212. device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
  213. else {
  214. /* gadget zero is so simple (for now, no altsettings) that
  215. * it SHOULD NOT have problems with bulk-capable hardware.
  216. * so just warn about unrcognized controllers -- don't panic.
  217. *
  218. * things like configuration and altsetting numbering
  219. * can need hardware-specific attention though.
  220. */
  221. pr_warning("%s: controller '%s' not recognized\n",
  222. longname, gadget->name);
  223. device_desc.bcdDevice = __constant_cpu_to_le16(0x9999);
  224. }
  225. INFO(cdev, "%s, version: " DRIVER_VERSION "\n", longname);
  226. snprintf(manufacturer, sizeof manufacturer, "%s %s with %s",
  227. init_utsname()->sysname, init_utsname()->release,
  228. gadget->name);
  229. return 0;
  230. }
  231. static struct usb_composite_driver zero_driver = {
  232. .name = "zero",
  233. .dev = &device_desc,
  234. .strings = dev_strings,
  235. .bind = zero_bind,
  236. };
  237. MODULE_AUTHOR("David Brownell");
  238. MODULE_LICENSE("GPL");
  239. static int __init init(void)
  240. {
  241. return usb_composite_register(&zero_driver);
  242. }
  243. module_init(init);
  244. static void __exit cleanup(void)
  245. {
  246. usb_composite_unregister(&zero_driver);
  247. }
  248. module_exit(cleanup);