zero.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. #define DRIVER_VERSION "Cinco de Mayo 2008"
  54. static const char longname[] = "Gadget Zero";
  55. unsigned buflen = 4096;
  56. module_param(buflen, uint, 0);
  57. /*
  58. * Normally the "loopback" configuration is second (index 1) so
  59. * it's not the default. Here's where to change that order, to
  60. * work better with hosts where config changes are problematic or
  61. * controllers (like original superh) that only support one config.
  62. */
  63. static int loopdefault = 0;
  64. module_param(loopdefault, bool, S_IRUGO|S_IWUSR);
  65. /*-------------------------------------------------------------------------*/
  66. /* Thanks to NetChip Technologies for donating this product ID.
  67. *
  68. * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
  69. * Instead: allocate your own, using normal USB-IF procedures.
  70. */
  71. #ifndef CONFIG_USB_ZERO_HNPTEST
  72. #define DRIVER_VENDOR_NUM 0x0525 /* NetChip */
  73. #define DRIVER_PRODUCT_NUM 0xa4a0 /* Linux-USB "Gadget Zero" */
  74. #else
  75. #define DRIVER_VENDOR_NUM 0x1a0a /* OTG test device IDs */
  76. #define DRIVER_PRODUCT_NUM 0xbadd
  77. #endif
  78. /*-------------------------------------------------------------------------*/
  79. static struct usb_device_descriptor device_desc = {
  80. .bLength = sizeof device_desc,
  81. .bDescriptorType = USB_DT_DEVICE,
  82. .bcdUSB = __constant_cpu_to_le16(0x0200),
  83. .bDeviceClass = USB_CLASS_VENDOR_SPEC,
  84. .idVendor = __constant_cpu_to_le16(DRIVER_VENDOR_NUM),
  85. .idProduct = __constant_cpu_to_le16(DRIVER_PRODUCT_NUM),
  86. .bNumConfigurations = 2,
  87. };
  88. #ifdef CONFIG_USB_OTG
  89. static struct usb_otg_descriptor otg_descriptor = {
  90. .bLength = sizeof otg_descriptor,
  91. .bDescriptorType = USB_DT_OTG,
  92. /* REVISIT SRP-only hardware is possible, although
  93. * it would not be called "OTG" ...
  94. */
  95. .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
  96. };
  97. const struct usb_descriptor_header *otg_desc[] = {
  98. (struct usb_descriptor_header *) &otg_descriptor,
  99. NULL,
  100. };
  101. #endif
  102. /* string IDs are assigned dynamically */
  103. #define STRING_MANUFACTURER_IDX 0
  104. #define STRING_PRODUCT_IDX 1
  105. #define STRING_SERIAL_IDX 2
  106. static char manufacturer[50];
  107. /* default serial number takes at least two packets */
  108. static char serial[] = "0123456789.0123456789.0123456789";
  109. static struct usb_string strings_dev[] = {
  110. [STRING_MANUFACTURER_IDX].s = manufacturer,
  111. [STRING_PRODUCT_IDX].s = longname,
  112. [STRING_SERIAL_IDX].s = serial,
  113. { } /* end of list */
  114. };
  115. static struct usb_gadget_strings stringtab_dev = {
  116. .language = 0x0409, /* en-us */
  117. .strings = strings_dev,
  118. };
  119. static struct usb_gadget_strings *dev_strings[] = {
  120. &stringtab_dev,
  121. NULL,
  122. };
  123. /*-------------------------------------------------------------------------*/
  124. struct usb_request *alloc_ep_req(struct usb_ep *ep)
  125. {
  126. struct usb_request *req;
  127. req = usb_ep_alloc_request(ep, GFP_ATOMIC);
  128. if (req) {
  129. req->length = buflen;
  130. req->buf = kmalloc(buflen, GFP_ATOMIC);
  131. if (!req->buf) {
  132. usb_ep_free_request(ep, req);
  133. req = NULL;
  134. }
  135. }
  136. return req;
  137. }
  138. void free_ep_req(struct usb_ep *ep, struct usb_request *req)
  139. {
  140. kfree(req->buf);
  141. usb_ep_free_request(ep, req);
  142. }
  143. static void disable_ep(struct usb_composite_dev *cdev, struct usb_ep *ep)
  144. {
  145. int value;
  146. if (ep->driver_data) {
  147. value = usb_ep_disable(ep);
  148. if (value < 0)
  149. DBG(cdev, "disable %s --> %d\n",
  150. ep->name, value);
  151. ep->driver_data = NULL;
  152. }
  153. }
  154. void disable_endpoints(struct usb_composite_dev *cdev,
  155. struct usb_ep *in, struct usb_ep *out)
  156. {
  157. disable_ep(cdev, in);
  158. disable_ep(cdev, out);
  159. }
  160. /*-------------------------------------------------------------------------*/
  161. static int __init zero_bind(struct usb_composite_dev *cdev)
  162. {
  163. int gcnum;
  164. struct usb_gadget *gadget = cdev->gadget;
  165. int id;
  166. /* Allocate string descriptor numbers ... note that string
  167. * contents can be overridden by the composite_dev glue.
  168. */
  169. id = usb_string_id(cdev);
  170. if (id < 0)
  171. return id;
  172. strings_dev[STRING_MANUFACTURER_IDX].id = id;
  173. device_desc.iManufacturer = id;
  174. id = usb_string_id(cdev);
  175. if (id < 0)
  176. return id;
  177. strings_dev[STRING_PRODUCT_IDX].id = id;
  178. device_desc.iProduct = id;
  179. id = usb_string_id(cdev);
  180. if (id < 0)
  181. return id;
  182. strings_dev[STRING_SERIAL_IDX].id = id;
  183. device_desc.iSerialNumber = id;
  184. /* Register primary, then secondary configuration. Note that
  185. * SH3 only allows one config...
  186. */
  187. if (loopdefault) {
  188. loopback_add(cdev);
  189. if (!gadget_is_sh(gadget))
  190. sourcesink_add(cdev);
  191. } else {
  192. sourcesink_add(cdev);
  193. if (!gadget_is_sh(gadget))
  194. loopback_add(cdev);
  195. }
  196. gcnum = usb_gadget_controller_number(gadget);
  197. if (gcnum >= 0)
  198. device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
  199. else {
  200. /* gadget zero is so simple (for now, no altsettings) that
  201. * it SHOULD NOT have problems with bulk-capable hardware.
  202. * so just warn about unrcognized controllers -- don't panic.
  203. *
  204. * things like configuration and altsetting numbering
  205. * can need hardware-specific attention though.
  206. */
  207. pr_warning("%s: controller '%s' not recognized\n",
  208. longname, gadget->name);
  209. device_desc.bcdDevice = __constant_cpu_to_le16(0x9999);
  210. }
  211. INFO(cdev, "%s, version: " DRIVER_VERSION "\n", longname);
  212. snprintf(manufacturer, sizeof manufacturer, "%s %s with %s",
  213. init_utsname()->sysname, init_utsname()->release,
  214. gadget->name);
  215. return 0;
  216. }
  217. static struct usb_composite_driver zero_driver = {
  218. .name = "zero",
  219. .dev = &device_desc,
  220. .strings = dev_strings,
  221. .bind = zero_bind,
  222. };
  223. MODULE_AUTHOR("David Brownell");
  224. MODULE_LICENSE("GPL");
  225. static int __init init(void)
  226. {
  227. return usb_composite_register(&zero_driver);
  228. }
  229. module_init(init);
  230. static void __exit cleanup(void)
  231. {
  232. usb_composite_unregister(&zero_driver);
  233. }
  234. module_exit(cleanup);