audio.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*
  2. * audio.c -- Audio gadget driver
  3. *
  4. * Copyright (C) 2008 Bryan Wu <cooloney@kernel.org>
  5. * Copyright (C) 2008 Analog Devices, Inc
  6. *
  7. * Enter bugs at http://blackfin.uclinux.org/
  8. *
  9. * Licensed under the GPL-2 or later.
  10. */
  11. /* #define VERBOSE_DEBUG */
  12. #include <linux/kernel.h>
  13. #include <linux/utsname.h>
  14. #include "u_audio.h"
  15. #define DRIVER_DESC "Linux USB Audio Gadget"
  16. #define DRIVER_VERSION "Dec 18, 2008"
  17. /*-------------------------------------------------------------------------*/
  18. /*
  19. * Kbuild is not very cooperative with respect to linking separately
  20. * compiled library objects into one module. So for now we won't use
  21. * separate compilation ... ensuring init/exit sections work to shrink
  22. * the runtime footprint, and giving us at least some parts of what
  23. * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
  24. */
  25. #include "composite.c"
  26. #include "usbstring.c"
  27. #include "config.c"
  28. #include "epautoconf.c"
  29. #include "u_audio.c"
  30. #include "f_audio.c"
  31. /*-------------------------------------------------------------------------*/
  32. /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
  33. * Instead: allocate your own, using normal USB-IF procedures.
  34. */
  35. /* Thanks to Linux Foundation for donating this product ID. */
  36. #define AUDIO_VENDOR_NUM 0x1d6b /* Linux Foundation */
  37. #define AUDIO_PRODUCT_NUM 0x0101 /* Linux-USB Audio Gadget */
  38. /*-------------------------------------------------------------------------*/
  39. static struct usb_device_descriptor device_desc = {
  40. .bLength = sizeof device_desc,
  41. .bDescriptorType = USB_DT_DEVICE,
  42. .bcdUSB = __constant_cpu_to_le16(0x200),
  43. .bDeviceClass = USB_CLASS_PER_INTERFACE,
  44. .bDeviceSubClass = 0,
  45. .bDeviceProtocol = 0,
  46. /* .bMaxPacketSize0 = f(hardware) */
  47. /* Vendor and product id defaults change according to what configs
  48. * we support. (As does bNumConfigurations.) These values can
  49. * also be overridden by module parameters.
  50. */
  51. .idVendor = __constant_cpu_to_le16(AUDIO_VENDOR_NUM),
  52. .idProduct = __constant_cpu_to_le16(AUDIO_PRODUCT_NUM),
  53. /* .bcdDevice = f(hardware) */
  54. /* .iManufacturer = DYNAMIC */
  55. /* .iProduct = DYNAMIC */
  56. /* NO SERIAL NUMBER */
  57. .bNumConfigurations = 1,
  58. };
  59. static struct usb_otg_descriptor otg_descriptor = {
  60. .bLength = sizeof otg_descriptor,
  61. .bDescriptorType = USB_DT_OTG,
  62. /* REVISIT SRP-only hardware is possible, although
  63. * it would not be called "OTG" ...
  64. */
  65. .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
  66. };
  67. static const struct usb_descriptor_header *otg_desc[] = {
  68. (struct usb_descriptor_header *) &otg_descriptor,
  69. NULL,
  70. };
  71. /*-------------------------------------------------------------------------*/
  72. /**
  73. * Handle USB audio endpoint set/get command in setup class request
  74. */
  75. static int audio_set_endpoint_req(struct usb_configuration *c,
  76. const struct usb_ctrlrequest *ctrl)
  77. {
  78. struct usb_composite_dev *cdev = c->cdev;
  79. int value = -EOPNOTSUPP;
  80. u16 ep = le16_to_cpu(ctrl->wIndex);
  81. u16 len = le16_to_cpu(ctrl->wLength);
  82. u16 w_value = le16_to_cpu(ctrl->wValue);
  83. DBG(cdev, "bRequest 0x%x, w_value 0x%04x, len %d, endpoint %d\n",
  84. ctrl->bRequest, w_value, len, ep);
  85. switch (ctrl->bRequest) {
  86. case SET_CUR:
  87. value = 0;
  88. break;
  89. case SET_MIN:
  90. break;
  91. case SET_MAX:
  92. break;
  93. case SET_RES:
  94. break;
  95. case SET_MEM:
  96. break;
  97. default:
  98. break;
  99. }
  100. return value;
  101. }
  102. static int audio_get_endpoint_req(struct usb_configuration *c,
  103. const struct usb_ctrlrequest *ctrl)
  104. {
  105. struct usb_composite_dev *cdev = c->cdev;
  106. int value = -EOPNOTSUPP;
  107. u8 ep = ((le16_to_cpu(ctrl->wIndex) >> 8) & 0xFF);
  108. u16 len = le16_to_cpu(ctrl->wLength);
  109. u16 w_value = le16_to_cpu(ctrl->wValue);
  110. DBG(cdev, "bRequest 0x%x, w_value 0x%04x, len %d, endpoint %d\n",
  111. ctrl->bRequest, w_value, len, ep);
  112. switch (ctrl->bRequest) {
  113. case GET_CUR:
  114. case GET_MIN:
  115. case GET_MAX:
  116. case GET_RES:
  117. value = 3;
  118. break;
  119. case GET_MEM:
  120. break;
  121. default:
  122. break;
  123. }
  124. return value;
  125. }
  126. static int
  127. audio_setup(struct usb_configuration *c, const struct usb_ctrlrequest *ctrl)
  128. {
  129. struct usb_composite_dev *cdev = c->cdev;
  130. struct usb_request *req = cdev->req;
  131. int value = -EOPNOTSUPP;
  132. u16 w_index = le16_to_cpu(ctrl->wIndex);
  133. u16 w_value = le16_to_cpu(ctrl->wValue);
  134. u16 w_length = le16_to_cpu(ctrl->wLength);
  135. /* composite driver infrastructure handles everything except
  136. * Audio class messages; interface activation uses set_alt().
  137. */
  138. switch (ctrl->bRequestType) {
  139. case USB_AUDIO_SET_ENDPOINT:
  140. value = audio_set_endpoint_req(c, ctrl);
  141. break;
  142. case USB_AUDIO_GET_ENDPOINT:
  143. value = audio_get_endpoint_req(c, ctrl);
  144. break;
  145. default:
  146. ERROR(cdev, "Invalid control req%02x.%02x v%04x i%04x l%d\n",
  147. ctrl->bRequestType, ctrl->bRequest,
  148. w_value, w_index, w_length);
  149. }
  150. /* respond with data transfer or status phase? */
  151. if (value >= 0) {
  152. DBG(cdev, "Audio req%02x.%02x v%04x i%04x l%d\n",
  153. ctrl->bRequestType, ctrl->bRequest,
  154. w_value, w_index, w_length);
  155. req->zero = 0;
  156. req->length = value;
  157. value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
  158. if (value < 0)
  159. ERROR(cdev, "Audio response on err %d\n", value);
  160. }
  161. /* device either stalls (value < 0) or reports success */
  162. return value;
  163. }
  164. /*-------------------------------------------------------------------------*/
  165. static int __init audio_do_config(struct usb_configuration *c)
  166. {
  167. /* FIXME alloc iConfiguration string, set it in c->strings */
  168. if (gadget_is_otg(c->cdev->gadget)) {
  169. c->descriptors = otg_desc;
  170. c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  171. }
  172. audio_bind_config(c);
  173. return 0;
  174. }
  175. static struct usb_configuration audio_config_driver = {
  176. .label = DRIVER_DESC,
  177. .bind = audio_do_config,
  178. .setup = audio_setup,
  179. .bConfigurationValue = 1,
  180. /* .iConfiguration = DYNAMIC */
  181. .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
  182. };
  183. /*-------------------------------------------------------------------------*/
  184. static int __init audio_bind(struct usb_composite_dev *cdev)
  185. {
  186. int gcnum;
  187. int status;
  188. gcnum = usb_gadget_controller_number(cdev->gadget);
  189. if (gcnum >= 0)
  190. device_desc.bcdDevice = cpu_to_le16(0x0300 | gcnum);
  191. else {
  192. ERROR(cdev, "controller '%s' not recognized; trying %s\n",
  193. cdev->gadget->name,
  194. audio_config_driver.label);
  195. device_desc.bcdDevice =
  196. __constant_cpu_to_le16(0x0300 | 0x0099);
  197. }
  198. /* device descriptor strings: manufacturer, product */
  199. snprintf(manufacturer, sizeof manufacturer, "%s %s with %s",
  200. init_utsname()->sysname, init_utsname()->release,
  201. cdev->gadget->name);
  202. status = usb_string_id(cdev);
  203. if (status < 0)
  204. goto fail;
  205. strings_dev[STRING_MANUFACTURER_IDX].id = status;
  206. device_desc.iManufacturer = status;
  207. status = usb_string_id(cdev);
  208. if (status < 0)
  209. goto fail;
  210. strings_dev[STRING_PRODUCT_IDX].id = status;
  211. device_desc.iProduct = status;
  212. status = usb_add_config(cdev, &audio_config_driver);
  213. if (status < 0)
  214. goto fail;
  215. INFO(cdev, "%s, version: %s\n", DRIVER_DESC, DRIVER_VERSION);
  216. return 0;
  217. fail:
  218. return status;
  219. }
  220. static int __exit audio_unbind(struct usb_composite_dev *cdev)
  221. {
  222. return 0;
  223. }
  224. static struct usb_composite_driver audio_driver = {
  225. .name = "g_audio",
  226. .dev = &device_desc,
  227. .strings = audio_strings,
  228. .bind = audio_bind,
  229. .unbind = __exit_p(audio_unbind),
  230. };
  231. static int __init init(void)
  232. {
  233. return usb_composite_register(&audio_driver);
  234. }
  235. module_init(init);
  236. static void __exit cleanup(void)
  237. {
  238. usb_composite_unregister(&audio_driver);
  239. }
  240. module_exit(cleanup);
  241. MODULE_DESCRIPTION(DRIVER_DESC);
  242. MODULE_AUTHOR("Bryan Wu <cooloney@kernel.org>");
  243. MODULE_LICENSE("GPL");