audio.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. .bConfigurationValue = 1,
  85. /* .iConfiguration = DYNAMIC */
  86. .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
  87. };
  88. /*-------------------------------------------------------------------------*/
  89. static int __init audio_bind(struct usb_composite_dev *cdev)
  90. {
  91. int gcnum;
  92. int status;
  93. gcnum = usb_gadget_controller_number(cdev->gadget);
  94. if (gcnum >= 0)
  95. device_desc.bcdDevice = cpu_to_le16(0x0300 | gcnum);
  96. else {
  97. ERROR(cdev, "controller '%s' not recognized; trying %s\n",
  98. cdev->gadget->name,
  99. audio_config_driver.label);
  100. device_desc.bcdDevice =
  101. __constant_cpu_to_le16(0x0300 | 0x0099);
  102. }
  103. /* device descriptor strings: manufacturer, product */
  104. snprintf(manufacturer, sizeof manufacturer, "%s %s with %s",
  105. init_utsname()->sysname, init_utsname()->release,
  106. cdev->gadget->name);
  107. status = usb_string_id(cdev);
  108. if (status < 0)
  109. goto fail;
  110. strings_dev[STRING_MANUFACTURER_IDX].id = status;
  111. device_desc.iManufacturer = status;
  112. status = usb_string_id(cdev);
  113. if (status < 0)
  114. goto fail;
  115. strings_dev[STRING_PRODUCT_IDX].id = status;
  116. device_desc.iProduct = status;
  117. status = usb_add_config(cdev, &audio_config_driver, audio_do_config);
  118. if (status < 0)
  119. goto fail;
  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. gaudio_cleanup();
  128. return 0;
  129. }
  130. static struct usb_composite_driver audio_driver = {
  131. .name = "g_audio",
  132. .dev = &device_desc,
  133. .strings = audio_strings,
  134. .unbind = __exit_p(audio_unbind),
  135. };
  136. static int __init init(void)
  137. {
  138. return usb_composite_probe(&audio_driver, audio_bind);
  139. }
  140. module_init(init);
  141. static void __exit cleanup(void)
  142. {
  143. usb_composite_unregister(&audio_driver);
  144. }
  145. module_exit(cleanup);
  146. MODULE_DESCRIPTION(DRIVER_DESC);
  147. MODULE_AUTHOR("Bryan Wu <cooloney@kernel.org>");
  148. MODULE_LICENSE("GPL");