audio.c 9.7 KB

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