audio.c 4.6 KB

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