audio.c 5.5 KB

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