audio.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. #define DRIVER_DESC "Linux USB Audio Gadget"
  15. #define DRIVER_VERSION "Feb 2, 2012"
  16. /*-------------------------------------------------------------------------*/
  17. /*
  18. * Kbuild is not very cooperative with respect to linking separately
  19. * compiled library objects into one module. So for now we won't use
  20. * separate compilation ... ensuring init/exit sections work to shrink
  21. * the runtime footprint, and giving us at least some parts of what
  22. * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
  23. */
  24. #include "composite.c"
  25. #include "config.c"
  26. #include "epautoconf.c"
  27. /* string IDs are assigned dynamically */
  28. #define STRING_MANUFACTURER_IDX 0
  29. #define STRING_PRODUCT_IDX 1
  30. static char manufacturer[50];
  31. static struct usb_string strings_dev[] = {
  32. [STRING_MANUFACTURER_IDX].s = manufacturer,
  33. [STRING_PRODUCT_IDX].s = DRIVER_DESC,
  34. { } /* end of list */
  35. };
  36. static struct usb_gadget_strings stringtab_dev = {
  37. .language = 0x0409, /* en-us */
  38. .strings = strings_dev,
  39. };
  40. static struct usb_gadget_strings *audio_strings[] = {
  41. &stringtab_dev,
  42. NULL,
  43. };
  44. #ifdef CONFIG_GADGET_UAC1
  45. #include "u_uac1.h"
  46. #include "u_uac1.c"
  47. #include "f_uac1.c"
  48. #else
  49. #include "f_uac2.c"
  50. #endif
  51. /*-------------------------------------------------------------------------*/
  52. /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
  53. * Instead: allocate your own, using normal USB-IF procedures.
  54. */
  55. /* Thanks to Linux Foundation for donating this product ID. */
  56. #define AUDIO_VENDOR_NUM 0x1d6b /* Linux Foundation */
  57. #define AUDIO_PRODUCT_NUM 0x0101 /* Linux-USB Audio Gadget */
  58. /*-------------------------------------------------------------------------*/
  59. static struct usb_device_descriptor device_desc = {
  60. .bLength = sizeof device_desc,
  61. .bDescriptorType = USB_DT_DEVICE,
  62. .bcdUSB = __constant_cpu_to_le16(0x200),
  63. #ifdef CONFIG_GADGET_UAC1
  64. .bDeviceClass = USB_CLASS_PER_INTERFACE,
  65. .bDeviceSubClass = 0,
  66. .bDeviceProtocol = 0,
  67. #else
  68. .bDeviceClass = USB_CLASS_MISC,
  69. .bDeviceSubClass = 0x02,
  70. .bDeviceProtocol = 0x01,
  71. #endif
  72. /* .bMaxPacketSize0 = f(hardware) */
  73. /* Vendor and product id defaults change according to what configs
  74. * we support. (As does bNumConfigurations.) These values can
  75. * also be overridden by module parameters.
  76. */
  77. .idVendor = __constant_cpu_to_le16(AUDIO_VENDOR_NUM),
  78. .idProduct = __constant_cpu_to_le16(AUDIO_PRODUCT_NUM),
  79. /* .bcdDevice = f(hardware) */
  80. /* .iManufacturer = DYNAMIC */
  81. /* .iProduct = DYNAMIC */
  82. /* NO SERIAL NUMBER */
  83. .bNumConfigurations = 1,
  84. };
  85. static struct usb_otg_descriptor otg_descriptor = {
  86. .bLength = sizeof otg_descriptor,
  87. .bDescriptorType = USB_DT_OTG,
  88. /* REVISIT SRP-only hardware is possible, although
  89. * it would not be called "OTG" ...
  90. */
  91. .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
  92. };
  93. static const struct usb_descriptor_header *otg_desc[] = {
  94. (struct usb_descriptor_header *) &otg_descriptor,
  95. NULL,
  96. };
  97. /*-------------------------------------------------------------------------*/
  98. static int __init audio_do_config(struct usb_configuration *c)
  99. {
  100. /* FIXME alloc iConfiguration string, set it in c->strings */
  101. if (gadget_is_otg(c->cdev->gadget)) {
  102. c->descriptors = otg_desc;
  103. c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  104. }
  105. audio_bind_config(c);
  106. return 0;
  107. }
  108. static struct usb_configuration audio_config_driver = {
  109. .label = DRIVER_DESC,
  110. .bConfigurationValue = 1,
  111. /* .iConfiguration = DYNAMIC */
  112. .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
  113. #ifndef CONFIG_GADGET_UAC1
  114. .unbind = uac2_unbind_config,
  115. #endif
  116. };
  117. /*-------------------------------------------------------------------------*/
  118. static int __init audio_bind(struct usb_composite_dev *cdev)
  119. {
  120. int gcnum;
  121. int status;
  122. gcnum = usb_gadget_controller_number(cdev->gadget);
  123. if (gcnum >= 0)
  124. device_desc.bcdDevice = cpu_to_le16(0x0300 | gcnum);
  125. else {
  126. ERROR(cdev, "controller '%s' not recognized; trying %s\n",
  127. cdev->gadget->name,
  128. audio_config_driver.label);
  129. device_desc.bcdDevice =
  130. __constant_cpu_to_le16(0x0300 | 0x0099);
  131. }
  132. /* device descriptor strings: manufacturer, product */
  133. snprintf(manufacturer, sizeof manufacturer, "%s %s with %s",
  134. init_utsname()->sysname, init_utsname()->release,
  135. cdev->gadget->name);
  136. status = usb_string_id(cdev);
  137. if (status < 0)
  138. goto fail;
  139. strings_dev[STRING_MANUFACTURER_IDX].id = status;
  140. device_desc.iManufacturer = status;
  141. status = usb_string_id(cdev);
  142. if (status < 0)
  143. goto fail;
  144. strings_dev[STRING_PRODUCT_IDX].id = status;
  145. device_desc.iProduct = status;
  146. status = usb_add_config(cdev, &audio_config_driver, audio_do_config);
  147. if (status < 0)
  148. goto fail;
  149. INFO(cdev, "%s, version: %s\n", DRIVER_DESC, DRIVER_VERSION);
  150. return 0;
  151. fail:
  152. return status;
  153. }
  154. static int __exit audio_unbind(struct usb_composite_dev *cdev)
  155. {
  156. #ifdef CONFIG_GADGET_UAC1
  157. gaudio_cleanup();
  158. #endif
  159. return 0;
  160. }
  161. static __refdata struct usb_composite_driver audio_driver = {
  162. .name = "g_audio",
  163. .dev = &device_desc,
  164. .strings = audio_strings,
  165. .max_speed = USB_SPEED_HIGH,
  166. .bind = audio_bind,
  167. .unbind = __exit_p(audio_unbind),
  168. };
  169. static int __init init(void)
  170. {
  171. return usb_composite_probe(&audio_driver);
  172. }
  173. module_init(init);
  174. static void __exit cleanup(void)
  175. {
  176. usb_composite_unregister(&audio_driver);
  177. }
  178. module_exit(cleanup);
  179. MODULE_DESCRIPTION(DRIVER_DESC);
  180. MODULE_AUTHOR("Bryan Wu <cooloney@kernel.org>");
  181. MODULE_LICENSE("GPL");