audio.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. #ifndef CONFIG_GADGET_UAC1
  20. #include "u_uac2.h"
  21. /* Playback(USB-IN) Default Stereo - Fl/Fr */
  22. static int p_chmask = UAC2_DEF_PCHMASK;
  23. module_param(p_chmask, uint, S_IRUGO);
  24. MODULE_PARM_DESC(p_chmask, "Playback Channel Mask");
  25. /* Playback Default 48 KHz */
  26. static int p_srate = UAC2_DEF_PSRATE;
  27. module_param(p_srate, uint, S_IRUGO);
  28. MODULE_PARM_DESC(p_srate, "Playback Sampling Rate");
  29. /* Playback Default 16bits/sample */
  30. static int p_ssize = UAC2_DEF_PSSIZE;
  31. module_param(p_ssize, uint, S_IRUGO);
  32. MODULE_PARM_DESC(p_ssize, "Playback Sample Size(bytes)");
  33. /* Capture(USB-OUT) Default Stereo - Fl/Fr */
  34. static int c_chmask = UAC2_DEF_CCHMASK;
  35. module_param(c_chmask, uint, S_IRUGO);
  36. MODULE_PARM_DESC(c_chmask, "Capture Channel Mask");
  37. /* Capture Default 64 KHz */
  38. static int c_srate = UAC2_DEF_CSRATE;
  39. module_param(c_srate, uint, S_IRUGO);
  40. MODULE_PARM_DESC(c_srate, "Capture Sampling Rate");
  41. /* Capture Default 16bits/sample */
  42. static int c_ssize = UAC2_DEF_CSSIZE;
  43. module_param(c_ssize, uint, S_IRUGO);
  44. MODULE_PARM_DESC(c_ssize, "Capture Sample Size(bytes)");
  45. #else
  46. #include "u_uac1.h"
  47. static char *fn_play = FILE_PCM_PLAYBACK;
  48. module_param(fn_play, charp, S_IRUGO);
  49. MODULE_PARM_DESC(fn_play, "Playback PCM device file name");
  50. static char *fn_cap = FILE_PCM_CAPTURE;
  51. module_param(fn_cap, charp, S_IRUGO);
  52. MODULE_PARM_DESC(fn_cap, "Capture PCM device file name");
  53. static char *fn_cntl = FILE_CONTROL;
  54. module_param(fn_cntl, charp, S_IRUGO);
  55. MODULE_PARM_DESC(fn_cntl, "Control device file name");
  56. static int req_buf_size = UAC1_OUT_EP_MAX_PACKET_SIZE;
  57. module_param(req_buf_size, int, S_IRUGO);
  58. MODULE_PARM_DESC(req_buf_size, "ISO OUT endpoint request buffer size");
  59. static int req_count = UAC1_REQ_COUNT;
  60. module_param(req_count, int, S_IRUGO);
  61. MODULE_PARM_DESC(req_count, "ISO OUT endpoint request count");
  62. static int audio_buf_size = UAC1_AUDIO_BUF_SIZE;
  63. module_param(audio_buf_size, int, S_IRUGO);
  64. MODULE_PARM_DESC(audio_buf_size, "Audio buffer size");
  65. #endif
  66. /* string IDs are assigned dynamically */
  67. static struct usb_string strings_dev[] = {
  68. [USB_GADGET_MANUFACTURER_IDX].s = "",
  69. [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC,
  70. [USB_GADGET_SERIAL_IDX].s = "",
  71. { } /* end of list */
  72. };
  73. static struct usb_gadget_strings stringtab_dev = {
  74. .language = 0x0409, /* en-us */
  75. .strings = strings_dev,
  76. };
  77. static struct usb_gadget_strings *audio_strings[] = {
  78. &stringtab_dev,
  79. NULL,
  80. };
  81. #ifndef CONFIG_GADGET_UAC1
  82. static struct usb_function_instance *fi_uac2;
  83. static struct usb_function *f_uac2;
  84. #else
  85. static struct usb_function_instance *fi_uac1;
  86. static struct usb_function *f_uac1;
  87. #endif
  88. /*-------------------------------------------------------------------------*/
  89. /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
  90. * Instead: allocate your own, using normal USB-IF procedures.
  91. */
  92. /* Thanks to Linux Foundation for donating this product ID. */
  93. #define AUDIO_VENDOR_NUM 0x1d6b /* Linux Foundation */
  94. #define AUDIO_PRODUCT_NUM 0x0101 /* Linux-USB Audio Gadget */
  95. /*-------------------------------------------------------------------------*/
  96. static struct usb_device_descriptor device_desc = {
  97. .bLength = sizeof device_desc,
  98. .bDescriptorType = USB_DT_DEVICE,
  99. .bcdUSB = __constant_cpu_to_le16(0x200),
  100. #ifdef CONFIG_GADGET_UAC1
  101. .bDeviceClass = USB_CLASS_PER_INTERFACE,
  102. .bDeviceSubClass = 0,
  103. .bDeviceProtocol = 0,
  104. #else
  105. .bDeviceClass = USB_CLASS_MISC,
  106. .bDeviceSubClass = 0x02,
  107. .bDeviceProtocol = 0x01,
  108. #endif
  109. /* .bMaxPacketSize0 = f(hardware) */
  110. /* Vendor and product id defaults change according to what configs
  111. * we support. (As does bNumConfigurations.) These values can
  112. * also be overridden by module parameters.
  113. */
  114. .idVendor = __constant_cpu_to_le16(AUDIO_VENDOR_NUM),
  115. .idProduct = __constant_cpu_to_le16(AUDIO_PRODUCT_NUM),
  116. /* .bcdDevice = f(hardware) */
  117. /* .iManufacturer = DYNAMIC */
  118. /* .iProduct = DYNAMIC */
  119. /* NO SERIAL NUMBER */
  120. .bNumConfigurations = 1,
  121. };
  122. static struct usb_otg_descriptor otg_descriptor = {
  123. .bLength = sizeof otg_descriptor,
  124. .bDescriptorType = USB_DT_OTG,
  125. /* REVISIT SRP-only hardware is possible, although
  126. * it would not be called "OTG" ...
  127. */
  128. .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
  129. };
  130. static const struct usb_descriptor_header *otg_desc[] = {
  131. (struct usb_descriptor_header *) &otg_descriptor,
  132. NULL,
  133. };
  134. /*-------------------------------------------------------------------------*/
  135. static int __init audio_do_config(struct usb_configuration *c)
  136. {
  137. int status;
  138. /* FIXME alloc iConfiguration string, set it in c->strings */
  139. if (gadget_is_otg(c->cdev->gadget)) {
  140. c->descriptors = otg_desc;
  141. c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  142. }
  143. #ifdef CONFIG_GADGET_UAC1
  144. f_uac1 = usb_get_function(fi_uac1);
  145. if (IS_ERR(f_uac1)) {
  146. status = PTR_ERR(f_uac1);
  147. return status;
  148. }
  149. status = usb_add_function(c, f_uac1);
  150. if (status < 0) {
  151. usb_put_function(f_uac1);
  152. return status;
  153. }
  154. #else
  155. f_uac2 = usb_get_function(fi_uac2);
  156. if (IS_ERR(f_uac2)) {
  157. status = PTR_ERR(f_uac2);
  158. return status;
  159. }
  160. status = usb_add_function(c, f_uac2);
  161. if (status < 0) {
  162. usb_put_function(f_uac2);
  163. return status;
  164. }
  165. #endif
  166. return 0;
  167. }
  168. static struct usb_configuration audio_config_driver = {
  169. .label = DRIVER_DESC,
  170. .bConfigurationValue = 1,
  171. /* .iConfiguration = DYNAMIC */
  172. .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
  173. };
  174. /*-------------------------------------------------------------------------*/
  175. static int __init audio_bind(struct usb_composite_dev *cdev)
  176. {
  177. #ifndef CONFIG_GADGET_UAC1
  178. struct f_uac2_opts *uac2_opts;
  179. #else
  180. struct f_uac1_opts *uac1_opts;
  181. #endif
  182. int status;
  183. #ifndef CONFIG_GADGET_UAC1
  184. fi_uac2 = usb_get_function_instance("uac2");
  185. if (IS_ERR(fi_uac2))
  186. return PTR_ERR(fi_uac2);
  187. #else
  188. fi_uac1 = usb_get_function_instance("uac1");
  189. if (IS_ERR(fi_uac1))
  190. return PTR_ERR(fi_uac1);
  191. #endif
  192. #ifndef CONFIG_GADGET_UAC1
  193. uac2_opts = container_of(fi_uac2, struct f_uac2_opts, func_inst);
  194. uac2_opts->p_chmask = p_chmask;
  195. uac2_opts->p_srate = p_srate;
  196. uac2_opts->p_ssize = p_ssize;
  197. uac2_opts->c_chmask = c_chmask;
  198. uac2_opts->c_srate = c_srate;
  199. uac2_opts->c_ssize = c_ssize;
  200. #else
  201. uac1_opts = container_of(fi_uac1, struct f_uac1_opts, func_inst);
  202. uac1_opts->fn_play = fn_play;
  203. uac1_opts->fn_cap = fn_cap;
  204. uac1_opts->fn_cntl = fn_cntl;
  205. uac1_opts->req_buf_size = req_buf_size;
  206. uac1_opts->req_count = req_count;
  207. uac1_opts->audio_buf_size = audio_buf_size;
  208. #endif
  209. status = usb_string_ids_tab(cdev, strings_dev);
  210. if (status < 0)
  211. goto fail;
  212. device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
  213. device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
  214. status = usb_add_config(cdev, &audio_config_driver, audio_do_config);
  215. if (status < 0)
  216. goto fail;
  217. usb_composite_overwrite_options(cdev, &coverwrite);
  218. INFO(cdev, "%s, version: %s\n", DRIVER_DESC, DRIVER_VERSION);
  219. return 0;
  220. fail:
  221. #ifndef CONFIG_GADGET_UAC1
  222. usb_put_function_instance(fi_uac2);
  223. #else
  224. usb_put_function_instance(fi_uac1);
  225. #endif
  226. return status;
  227. }
  228. static int __exit audio_unbind(struct usb_composite_dev *cdev)
  229. {
  230. #ifdef CONFIG_GADGET_UAC1
  231. if (!IS_ERR_OR_NULL(f_uac1))
  232. usb_put_function(f_uac1);
  233. if (!IS_ERR_OR_NULL(fi_uac1))
  234. usb_put_function_instance(fi_uac1);
  235. #else
  236. if (!IS_ERR_OR_NULL(f_uac2))
  237. usb_put_function(f_uac2);
  238. if (!IS_ERR_OR_NULL(fi_uac2))
  239. usb_put_function_instance(fi_uac2);
  240. #endif
  241. return 0;
  242. }
  243. static __refdata struct usb_composite_driver audio_driver = {
  244. .name = "g_audio",
  245. .dev = &device_desc,
  246. .strings = audio_strings,
  247. .max_speed = USB_SPEED_HIGH,
  248. .bind = audio_bind,
  249. .unbind = __exit_p(audio_unbind),
  250. };
  251. module_usb_composite_driver(audio_driver);
  252. MODULE_DESCRIPTION(DRIVER_DESC);
  253. MODULE_AUTHOR("Bryan Wu <cooloney@kernel.org>");
  254. MODULE_LICENSE("GPL");