audio.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. static int __init audio_do_config(struct usb_configuration *c)
  73. {
  74. /* FIXME alloc iConfiguration string, set it in c->strings */
  75. if (gadget_is_otg(c->cdev->gadget)) {
  76. c->descriptors = otg_desc;
  77. c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  78. }
  79. audio_bind_config(c);
  80. return 0;
  81. }
  82. static struct usb_configuration audio_config_driver = {
  83. .label = DRIVER_DESC,
  84. .bind = audio_do_config,
  85. .bConfigurationValue = 1,
  86. /* .iConfiguration = DYNAMIC */
  87. .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
  88. };
  89. /*-------------------------------------------------------------------------*/
  90. static int __init audio_bind(struct usb_composite_dev *cdev)
  91. {
  92. int gcnum;
  93. int status;
  94. gcnum = usb_gadget_controller_number(cdev->gadget);
  95. if (gcnum >= 0)
  96. device_desc.bcdDevice = cpu_to_le16(0x0300 | gcnum);
  97. else {
  98. ERROR(cdev, "controller '%s' not recognized; trying %s\n",
  99. cdev->gadget->name,
  100. audio_config_driver.label);
  101. device_desc.bcdDevice =
  102. __constant_cpu_to_le16(0x0300 | 0x0099);
  103. }
  104. /* device descriptor strings: manufacturer, product */
  105. snprintf(manufacturer, sizeof manufacturer, "%s %s with %s",
  106. init_utsname()->sysname, init_utsname()->release,
  107. cdev->gadget->name);
  108. status = usb_string_id(cdev);
  109. if (status < 0)
  110. goto fail;
  111. strings_dev[STRING_MANUFACTURER_IDX].id = status;
  112. device_desc.iManufacturer = status;
  113. status = usb_string_id(cdev);
  114. if (status < 0)
  115. goto fail;
  116. strings_dev[STRING_PRODUCT_IDX].id = status;
  117. device_desc.iProduct = status;
  118. status = usb_add_config(cdev, &audio_config_driver);
  119. if (status < 0)
  120. goto fail;
  121. INFO(cdev, "%s, version: %s\n", DRIVER_DESC, DRIVER_VERSION);
  122. return 0;
  123. fail:
  124. return status;
  125. }
  126. static int __exit audio_unbind(struct usb_composite_dev *cdev)
  127. {
  128. gaudio_cleanup();
  129. return 0;
  130. }
  131. static struct usb_composite_driver audio_driver = {
  132. .name = "g_audio",
  133. .dev = &device_desc,
  134. .strings = audio_strings,
  135. .bind = audio_bind,
  136. .unbind = __exit_p(audio_unbind),
  137. };
  138. static int __init init(void)
  139. {
  140. return usb_composite_register(&audio_driver);
  141. }
  142. module_init(init);
  143. static void __exit cleanup(void)
  144. {
  145. usb_composite_unregister(&audio_driver);
  146. }
  147. module_exit(cleanup);
  148. MODULE_DESCRIPTION(DRIVER_DESC);
  149. MODULE_AUTHOR("Bryan Wu <cooloney@kernel.org>");
  150. MODULE_LICENSE("GPL");