audio.c 8.0 KB

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