audio.c 11 KB

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