f_uac2.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * f_uac2.c -- USB Audio Class 2.0 Function
  4. *
  5. * Copyright (C) 2011
  6. * Yadwinder Singh (yadi.brar01@gmail.com)
  7. * Jaswinder Singh (jaswinder.singh@linaro.org)
  8. */
  9. #include <linux/usb/audio.h>
  10. #include <linux/usb/audio-v2.h>
  11. #include <linux/module.h>
  12. #include "u_audio.h"
  13. #include "u_uac2.h"
  14. /*
  15. * The driver implements a simple UAC_2 topology.
  16. * USB-OUT -> IT_1 -> OT_3 -> ALSA_Capture
  17. * ALSA_Playback -> IT_2 -> OT_4 -> USB-IN
  18. * Capture and Playback sampling rates are independently
  19. * controlled by two clock sources :
  20. * CLK_5 := c_srate, and CLK_6 := p_srate
  21. */
  22. #define USB_OUT_IT_ID 1
  23. #define IO_IN_IT_ID 2
  24. #define IO_OUT_OT_ID 3
  25. #define USB_IN_OT_ID 4
  26. #define USB_OUT_CLK_ID 5
  27. #define USB_IN_CLK_ID 6
  28. #define CONTROL_ABSENT 0
  29. #define CONTROL_RDONLY 1
  30. #define CONTROL_RDWR 3
  31. #define CLK_FREQ_CTRL 0
  32. #define CLK_VLD_CTRL 2
  33. #define COPY_CTRL 0
  34. #define CONN_CTRL 2
  35. #define OVRLD_CTRL 4
  36. #define CLSTR_CTRL 6
  37. #define UNFLW_CTRL 8
  38. #define OVFLW_CTRL 10
  39. struct f_uac2 {
  40. struct g_audio g_audio;
  41. u8 ac_intf, as_in_intf, as_out_intf;
  42. u8 ac_alt, as_in_alt, as_out_alt; /* needed for get_alt() */
  43. };
  44. static inline struct f_uac2 *func_to_uac2(struct usb_function *f)
  45. {
  46. return container_of(f, struct f_uac2, g_audio.func);
  47. }
  48. static inline
  49. struct f_uac2_opts *g_audio_to_uac2_opts(struct g_audio *agdev)
  50. {
  51. return container_of(agdev->func.fi, struct f_uac2_opts, func_inst);
  52. }
  53. /* --------- USB Function Interface ------------- */
  54. enum {
  55. STR_ASSOC,
  56. STR_IF_CTRL,
  57. STR_CLKSRC_IN,
  58. STR_CLKSRC_OUT,
  59. STR_USB_IT,
  60. STR_IO_IT,
  61. STR_USB_OT,
  62. STR_IO_OT,
  63. STR_AS_OUT_ALT0,
  64. STR_AS_OUT_ALT1,
  65. STR_AS_IN_ALT0,
  66. STR_AS_IN_ALT1,
  67. };
  68. static char clksrc_in[8];
  69. static char clksrc_out[8];
  70. static struct usb_string strings_fn[] = {
  71. [STR_ASSOC].s = "Source/Sink",
  72. [STR_IF_CTRL].s = "Topology Control",
  73. [STR_CLKSRC_IN].s = clksrc_in,
  74. [STR_CLKSRC_OUT].s = clksrc_out,
  75. [STR_USB_IT].s = "USBH Out",
  76. [STR_IO_IT].s = "USBD Out",
  77. [STR_USB_OT].s = "USBH In",
  78. [STR_IO_OT].s = "USBD In",
  79. [STR_AS_OUT_ALT0].s = "Playback Inactive",
  80. [STR_AS_OUT_ALT1].s = "Playback Active",
  81. [STR_AS_IN_ALT0].s = "Capture Inactive",
  82. [STR_AS_IN_ALT1].s = "Capture Active",
  83. { },
  84. };
  85. static struct usb_gadget_strings str_fn = {
  86. .language = 0x0409, /* en-us */
  87. .strings = strings_fn,
  88. };
  89. static struct usb_gadget_strings *fn_strings[] = {
  90. &str_fn,
  91. NULL,
  92. };
  93. static struct usb_interface_assoc_descriptor iad_desc = {
  94. .bLength = sizeof iad_desc,
  95. .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
  96. .bFirstInterface = 0,
  97. .bInterfaceCount = 3,
  98. .bFunctionClass = USB_CLASS_AUDIO,
  99. .bFunctionSubClass = UAC2_FUNCTION_SUBCLASS_UNDEFINED,
  100. .bFunctionProtocol = UAC_VERSION_2,
  101. };
  102. /* Audio Control Interface */
  103. static struct usb_interface_descriptor std_ac_if_desc = {
  104. .bLength = sizeof std_ac_if_desc,
  105. .bDescriptorType = USB_DT_INTERFACE,
  106. .bAlternateSetting = 0,
  107. .bNumEndpoints = 0,
  108. .bInterfaceClass = USB_CLASS_AUDIO,
  109. .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL,
  110. .bInterfaceProtocol = UAC_VERSION_2,
  111. };
  112. /* Clock source for IN traffic */
  113. static struct uac_clock_source_descriptor in_clk_src_desc = {
  114. .bLength = sizeof in_clk_src_desc,
  115. .bDescriptorType = USB_DT_CS_INTERFACE,
  116. .bDescriptorSubtype = UAC2_CLOCK_SOURCE,
  117. .bClockID = USB_IN_CLK_ID,
  118. .bmAttributes = UAC_CLOCK_SOURCE_TYPE_INT_FIXED,
  119. .bmControls = (CONTROL_RDONLY << CLK_FREQ_CTRL),
  120. .bAssocTerminal = 0,
  121. };
  122. /* Clock source for OUT traffic */
  123. static struct uac_clock_source_descriptor out_clk_src_desc = {
  124. .bLength = sizeof out_clk_src_desc,
  125. .bDescriptorType = USB_DT_CS_INTERFACE,
  126. .bDescriptorSubtype = UAC2_CLOCK_SOURCE,
  127. .bClockID = USB_OUT_CLK_ID,
  128. .bmAttributes = UAC_CLOCK_SOURCE_TYPE_INT_FIXED,
  129. .bmControls = (CONTROL_RDONLY << CLK_FREQ_CTRL),
  130. .bAssocTerminal = 0,
  131. };
  132. /* Input Terminal for USB_OUT */
  133. static struct uac2_input_terminal_descriptor usb_out_it_desc = {
  134. .bLength = sizeof usb_out_it_desc,
  135. .bDescriptorType = USB_DT_CS_INTERFACE,
  136. .bDescriptorSubtype = UAC_INPUT_TERMINAL,
  137. .bTerminalID = USB_OUT_IT_ID,
  138. .wTerminalType = cpu_to_le16(UAC_TERMINAL_STREAMING),
  139. .bAssocTerminal = 0,
  140. .bCSourceID = USB_OUT_CLK_ID,
  141. .iChannelNames = 0,
  142. .bmControls = cpu_to_le16(CONTROL_RDWR << COPY_CTRL),
  143. };
  144. /* Input Terminal for I/O-In */
  145. static struct uac2_input_terminal_descriptor io_in_it_desc = {
  146. .bLength = sizeof io_in_it_desc,
  147. .bDescriptorType = USB_DT_CS_INTERFACE,
  148. .bDescriptorSubtype = UAC_INPUT_TERMINAL,
  149. .bTerminalID = IO_IN_IT_ID,
  150. .wTerminalType = cpu_to_le16(UAC_INPUT_TERMINAL_UNDEFINED),
  151. .bAssocTerminal = 0,
  152. .bCSourceID = USB_IN_CLK_ID,
  153. .iChannelNames = 0,
  154. .bmControls = cpu_to_le16(CONTROL_RDWR << COPY_CTRL),
  155. };
  156. /* Ouput Terminal for USB_IN */
  157. static struct uac2_output_terminal_descriptor usb_in_ot_desc = {
  158. .bLength = sizeof usb_in_ot_desc,
  159. .bDescriptorType = USB_DT_CS_INTERFACE,
  160. .bDescriptorSubtype = UAC_OUTPUT_TERMINAL,
  161. .bTerminalID = USB_IN_OT_ID,
  162. .wTerminalType = cpu_to_le16(UAC_TERMINAL_STREAMING),
  163. .bAssocTerminal = 0,
  164. .bSourceID = IO_IN_IT_ID,
  165. .bCSourceID = USB_IN_CLK_ID,
  166. .bmControls = cpu_to_le16(CONTROL_RDWR << COPY_CTRL),
  167. };
  168. /* Ouput Terminal for I/O-Out */
  169. static struct uac2_output_terminal_descriptor io_out_ot_desc = {
  170. .bLength = sizeof io_out_ot_desc,
  171. .bDescriptorType = USB_DT_CS_INTERFACE,
  172. .bDescriptorSubtype = UAC_OUTPUT_TERMINAL,
  173. .bTerminalID = IO_OUT_OT_ID,
  174. .wTerminalType = cpu_to_le16(UAC_OUTPUT_TERMINAL_UNDEFINED),
  175. .bAssocTerminal = 0,
  176. .bSourceID = USB_OUT_IT_ID,
  177. .bCSourceID = USB_OUT_CLK_ID,
  178. .bmControls = cpu_to_le16(CONTROL_RDWR << COPY_CTRL),
  179. };
  180. static struct uac2_ac_header_descriptor ac_hdr_desc = {
  181. .bLength = sizeof ac_hdr_desc,
  182. .bDescriptorType = USB_DT_CS_INTERFACE,
  183. .bDescriptorSubtype = UAC_MS_HEADER,
  184. .bcdADC = cpu_to_le16(0x200),
  185. .bCategory = UAC2_FUNCTION_IO_BOX,
  186. .wTotalLength = cpu_to_le16(sizeof in_clk_src_desc
  187. + sizeof out_clk_src_desc + sizeof usb_out_it_desc
  188. + sizeof io_in_it_desc + sizeof usb_in_ot_desc
  189. + sizeof io_out_ot_desc),
  190. .bmControls = 0,
  191. };
  192. /* Audio Streaming OUT Interface - Alt0 */
  193. static struct usb_interface_descriptor std_as_out_if0_desc = {
  194. .bLength = sizeof std_as_out_if0_desc,
  195. .bDescriptorType = USB_DT_INTERFACE,
  196. .bAlternateSetting = 0,
  197. .bNumEndpoints = 0,
  198. .bInterfaceClass = USB_CLASS_AUDIO,
  199. .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
  200. .bInterfaceProtocol = UAC_VERSION_2,
  201. };
  202. /* Audio Streaming OUT Interface - Alt1 */
  203. static struct usb_interface_descriptor std_as_out_if1_desc = {
  204. .bLength = sizeof std_as_out_if1_desc,
  205. .bDescriptorType = USB_DT_INTERFACE,
  206. .bAlternateSetting = 1,
  207. .bNumEndpoints = 1,
  208. .bInterfaceClass = USB_CLASS_AUDIO,
  209. .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
  210. .bInterfaceProtocol = UAC_VERSION_2,
  211. };
  212. /* Audio Stream OUT Intface Desc */
  213. static struct uac2_as_header_descriptor as_out_hdr_desc = {
  214. .bLength = sizeof as_out_hdr_desc,
  215. .bDescriptorType = USB_DT_CS_INTERFACE,
  216. .bDescriptorSubtype = UAC_AS_GENERAL,
  217. .bTerminalLink = USB_OUT_IT_ID,
  218. .bmControls = 0,
  219. .bFormatType = UAC_FORMAT_TYPE_I,
  220. .bmFormats = cpu_to_le32(UAC_FORMAT_TYPE_I_PCM),
  221. .iChannelNames = 0,
  222. };
  223. /* Audio USB_OUT Format */
  224. static struct uac2_format_type_i_descriptor as_out_fmt1_desc = {
  225. .bLength = sizeof as_out_fmt1_desc,
  226. .bDescriptorType = USB_DT_CS_INTERFACE,
  227. .bDescriptorSubtype = UAC_FORMAT_TYPE,
  228. .bFormatType = UAC_FORMAT_TYPE_I,
  229. };
  230. /* STD AS ISO OUT Endpoint */
  231. static struct usb_endpoint_descriptor fs_epout_desc = {
  232. .bLength = USB_DT_ENDPOINT_SIZE,
  233. .bDescriptorType = USB_DT_ENDPOINT,
  234. .bEndpointAddress = USB_DIR_OUT,
  235. .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC,
  236. .wMaxPacketSize = cpu_to_le16(1023),
  237. .bInterval = 1,
  238. };
  239. static struct usb_endpoint_descriptor hs_epout_desc = {
  240. .bLength = USB_DT_ENDPOINT_SIZE,
  241. .bDescriptorType = USB_DT_ENDPOINT,
  242. .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC,
  243. .wMaxPacketSize = cpu_to_le16(1024),
  244. .bInterval = 4,
  245. };
  246. /* CS AS ISO OUT Endpoint */
  247. static struct uac2_iso_endpoint_descriptor as_iso_out_desc = {
  248. .bLength = sizeof as_iso_out_desc,
  249. .bDescriptorType = USB_DT_CS_ENDPOINT,
  250. .bDescriptorSubtype = UAC_EP_GENERAL,
  251. .bmAttributes = 0,
  252. .bmControls = 0,
  253. .bLockDelayUnits = 0,
  254. .wLockDelay = 0,
  255. };
  256. /* Audio Streaming IN Interface - Alt0 */
  257. static struct usb_interface_descriptor std_as_in_if0_desc = {
  258. .bLength = sizeof std_as_in_if0_desc,
  259. .bDescriptorType = USB_DT_INTERFACE,
  260. .bAlternateSetting = 0,
  261. .bNumEndpoints = 0,
  262. .bInterfaceClass = USB_CLASS_AUDIO,
  263. .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
  264. .bInterfaceProtocol = UAC_VERSION_2,
  265. };
  266. /* Audio Streaming IN Interface - Alt1 */
  267. static struct usb_interface_descriptor std_as_in_if1_desc = {
  268. .bLength = sizeof std_as_in_if1_desc,
  269. .bDescriptorType = USB_DT_INTERFACE,
  270. .bAlternateSetting = 1,
  271. .bNumEndpoints = 1,
  272. .bInterfaceClass = USB_CLASS_AUDIO,
  273. .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
  274. .bInterfaceProtocol = UAC_VERSION_2,
  275. };
  276. /* Audio Stream IN Intface Desc */
  277. static struct uac2_as_header_descriptor as_in_hdr_desc = {
  278. .bLength = sizeof as_in_hdr_desc,
  279. .bDescriptorType = USB_DT_CS_INTERFACE,
  280. .bDescriptorSubtype = UAC_AS_GENERAL,
  281. .bTerminalLink = USB_IN_OT_ID,
  282. .bmControls = 0,
  283. .bFormatType = UAC_FORMAT_TYPE_I,
  284. .bmFormats = cpu_to_le32(UAC_FORMAT_TYPE_I_PCM),
  285. .iChannelNames = 0,
  286. };
  287. /* Audio USB_IN Format */
  288. static struct uac2_format_type_i_descriptor as_in_fmt1_desc = {
  289. .bLength = sizeof as_in_fmt1_desc,
  290. .bDescriptorType = USB_DT_CS_INTERFACE,
  291. .bDescriptorSubtype = UAC_FORMAT_TYPE,
  292. .bFormatType = UAC_FORMAT_TYPE_I,
  293. };
  294. /* STD AS ISO IN Endpoint */
  295. static struct usb_endpoint_descriptor fs_epin_desc = {
  296. .bLength = USB_DT_ENDPOINT_SIZE,
  297. .bDescriptorType = USB_DT_ENDPOINT,
  298. .bEndpointAddress = USB_DIR_IN,
  299. .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC,
  300. .wMaxPacketSize = cpu_to_le16(1023),
  301. .bInterval = 1,
  302. };
  303. static struct usb_endpoint_descriptor hs_epin_desc = {
  304. .bLength = USB_DT_ENDPOINT_SIZE,
  305. .bDescriptorType = USB_DT_ENDPOINT,
  306. .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC,
  307. .wMaxPacketSize = cpu_to_le16(1024),
  308. .bInterval = 4,
  309. };
  310. /* CS AS ISO IN Endpoint */
  311. static struct uac2_iso_endpoint_descriptor as_iso_in_desc = {
  312. .bLength = sizeof as_iso_in_desc,
  313. .bDescriptorType = USB_DT_CS_ENDPOINT,
  314. .bDescriptorSubtype = UAC_EP_GENERAL,
  315. .bmAttributes = 0,
  316. .bmControls = 0,
  317. .bLockDelayUnits = 0,
  318. .wLockDelay = 0,
  319. };
  320. static struct usb_descriptor_header *fs_audio_desc[] = {
  321. (struct usb_descriptor_header *)&iad_desc,
  322. (struct usb_descriptor_header *)&std_ac_if_desc,
  323. (struct usb_descriptor_header *)&ac_hdr_desc,
  324. (struct usb_descriptor_header *)&in_clk_src_desc,
  325. (struct usb_descriptor_header *)&out_clk_src_desc,
  326. (struct usb_descriptor_header *)&usb_out_it_desc,
  327. (struct usb_descriptor_header *)&io_in_it_desc,
  328. (struct usb_descriptor_header *)&usb_in_ot_desc,
  329. (struct usb_descriptor_header *)&io_out_ot_desc,
  330. (struct usb_descriptor_header *)&std_as_out_if0_desc,
  331. (struct usb_descriptor_header *)&std_as_out_if1_desc,
  332. (struct usb_descriptor_header *)&as_out_hdr_desc,
  333. (struct usb_descriptor_header *)&as_out_fmt1_desc,
  334. (struct usb_descriptor_header *)&fs_epout_desc,
  335. (struct usb_descriptor_header *)&as_iso_out_desc,
  336. (struct usb_descriptor_header *)&std_as_in_if0_desc,
  337. (struct usb_descriptor_header *)&std_as_in_if1_desc,
  338. (struct usb_descriptor_header *)&as_in_hdr_desc,
  339. (struct usb_descriptor_header *)&as_in_fmt1_desc,
  340. (struct usb_descriptor_header *)&fs_epin_desc,
  341. (struct usb_descriptor_header *)&as_iso_in_desc,
  342. NULL,
  343. };
  344. static struct usb_descriptor_header *hs_audio_desc[] = {
  345. (struct usb_descriptor_header *)&iad_desc,
  346. (struct usb_descriptor_header *)&std_ac_if_desc,
  347. (struct usb_descriptor_header *)&ac_hdr_desc,
  348. (struct usb_descriptor_header *)&in_clk_src_desc,
  349. (struct usb_descriptor_header *)&out_clk_src_desc,
  350. (struct usb_descriptor_header *)&usb_out_it_desc,
  351. (struct usb_descriptor_header *)&io_in_it_desc,
  352. (struct usb_descriptor_header *)&usb_in_ot_desc,
  353. (struct usb_descriptor_header *)&io_out_ot_desc,
  354. (struct usb_descriptor_header *)&std_as_out_if0_desc,
  355. (struct usb_descriptor_header *)&std_as_out_if1_desc,
  356. (struct usb_descriptor_header *)&as_out_hdr_desc,
  357. (struct usb_descriptor_header *)&as_out_fmt1_desc,
  358. (struct usb_descriptor_header *)&hs_epout_desc,
  359. (struct usb_descriptor_header *)&as_iso_out_desc,
  360. (struct usb_descriptor_header *)&std_as_in_if0_desc,
  361. (struct usb_descriptor_header *)&std_as_in_if1_desc,
  362. (struct usb_descriptor_header *)&as_in_hdr_desc,
  363. (struct usb_descriptor_header *)&as_in_fmt1_desc,
  364. (struct usb_descriptor_header *)&hs_epin_desc,
  365. (struct usb_descriptor_header *)&as_iso_in_desc,
  366. NULL,
  367. };
  368. struct cntrl_cur_lay3 {
  369. __u32 dCUR;
  370. };
  371. struct cntrl_range_lay3 {
  372. __u16 wNumSubRanges;
  373. __u32 dMIN;
  374. __u32 dMAX;
  375. __u32 dRES;
  376. } __packed;
  377. static void set_ep_max_packet_size(const struct f_uac2_opts *uac2_opts,
  378. struct usb_endpoint_descriptor *ep_desc,
  379. unsigned int factor, bool is_playback)
  380. {
  381. int chmask, srate, ssize;
  382. u16 max_packet_size;
  383. if (is_playback) {
  384. chmask = uac2_opts->p_chmask;
  385. srate = uac2_opts->p_srate;
  386. ssize = uac2_opts->p_ssize;
  387. } else {
  388. chmask = uac2_opts->c_chmask;
  389. srate = uac2_opts->c_srate;
  390. ssize = uac2_opts->c_ssize;
  391. }
  392. max_packet_size = num_channels(chmask) * ssize *
  393. DIV_ROUND_UP(srate, factor / (1 << (ep_desc->bInterval - 1)));
  394. ep_desc->wMaxPacketSize = cpu_to_le16(min_t(u16, max_packet_size,
  395. le16_to_cpu(ep_desc->wMaxPacketSize)));
  396. }
  397. static int
  398. afunc_bind(struct usb_configuration *cfg, struct usb_function *fn)
  399. {
  400. struct f_uac2 *uac2 = func_to_uac2(fn);
  401. struct g_audio *agdev = func_to_g_audio(fn);
  402. struct usb_composite_dev *cdev = cfg->cdev;
  403. struct usb_gadget *gadget = cdev->gadget;
  404. struct device *dev = &gadget->dev;
  405. struct f_uac2_opts *uac2_opts;
  406. struct usb_string *us;
  407. int ret;
  408. uac2_opts = container_of(fn->fi, struct f_uac2_opts, func_inst);
  409. us = usb_gstrings_attach(cdev, fn_strings, ARRAY_SIZE(strings_fn));
  410. if (IS_ERR(us))
  411. return PTR_ERR(us);
  412. iad_desc.iFunction = us[STR_ASSOC].id;
  413. std_ac_if_desc.iInterface = us[STR_IF_CTRL].id;
  414. in_clk_src_desc.iClockSource = us[STR_CLKSRC_IN].id;
  415. out_clk_src_desc.iClockSource = us[STR_CLKSRC_OUT].id;
  416. usb_out_it_desc.iTerminal = us[STR_USB_IT].id;
  417. io_in_it_desc.iTerminal = us[STR_IO_IT].id;
  418. usb_in_ot_desc.iTerminal = us[STR_USB_OT].id;
  419. io_out_ot_desc.iTerminal = us[STR_IO_OT].id;
  420. std_as_out_if0_desc.iInterface = us[STR_AS_OUT_ALT0].id;
  421. std_as_out_if1_desc.iInterface = us[STR_AS_OUT_ALT1].id;
  422. std_as_in_if0_desc.iInterface = us[STR_AS_IN_ALT0].id;
  423. std_as_in_if1_desc.iInterface = us[STR_AS_IN_ALT1].id;
  424. /* Initialize the configurable parameters */
  425. usb_out_it_desc.bNrChannels = num_channels(uac2_opts->c_chmask);
  426. usb_out_it_desc.bmChannelConfig = cpu_to_le32(uac2_opts->c_chmask);
  427. io_in_it_desc.bNrChannels = num_channels(uac2_opts->p_chmask);
  428. io_in_it_desc.bmChannelConfig = cpu_to_le32(uac2_opts->p_chmask);
  429. as_out_hdr_desc.bNrChannels = num_channels(uac2_opts->c_chmask);
  430. as_out_hdr_desc.bmChannelConfig = cpu_to_le32(uac2_opts->c_chmask);
  431. as_in_hdr_desc.bNrChannels = num_channels(uac2_opts->p_chmask);
  432. as_in_hdr_desc.bmChannelConfig = cpu_to_le32(uac2_opts->p_chmask);
  433. as_out_fmt1_desc.bSubslotSize = uac2_opts->c_ssize;
  434. as_out_fmt1_desc.bBitResolution = uac2_opts->c_ssize * 8;
  435. as_in_fmt1_desc.bSubslotSize = uac2_opts->p_ssize;
  436. as_in_fmt1_desc.bBitResolution = uac2_opts->p_ssize * 8;
  437. snprintf(clksrc_in, sizeof(clksrc_in), "%uHz", uac2_opts->p_srate);
  438. snprintf(clksrc_out, sizeof(clksrc_out), "%uHz", uac2_opts->c_srate);
  439. ret = usb_interface_id(cfg, fn);
  440. if (ret < 0) {
  441. dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
  442. return ret;
  443. }
  444. std_ac_if_desc.bInterfaceNumber = ret;
  445. uac2->ac_intf = ret;
  446. uac2->ac_alt = 0;
  447. ret = usb_interface_id(cfg, fn);
  448. if (ret < 0) {
  449. dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
  450. return ret;
  451. }
  452. std_as_out_if0_desc.bInterfaceNumber = ret;
  453. std_as_out_if1_desc.bInterfaceNumber = ret;
  454. uac2->as_out_intf = ret;
  455. uac2->as_out_alt = 0;
  456. ret = usb_interface_id(cfg, fn);
  457. if (ret < 0) {
  458. dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
  459. return ret;
  460. }
  461. std_as_in_if0_desc.bInterfaceNumber = ret;
  462. std_as_in_if1_desc.bInterfaceNumber = ret;
  463. uac2->as_in_intf = ret;
  464. uac2->as_in_alt = 0;
  465. /* Calculate wMaxPacketSize according to audio bandwidth */
  466. set_ep_max_packet_size(uac2_opts, &fs_epin_desc, 1000, true);
  467. set_ep_max_packet_size(uac2_opts, &fs_epout_desc, 1000, false);
  468. set_ep_max_packet_size(uac2_opts, &hs_epin_desc, 8000, true);
  469. set_ep_max_packet_size(uac2_opts, &hs_epout_desc, 8000, false);
  470. agdev->out_ep = usb_ep_autoconfig(gadget, &fs_epout_desc);
  471. if (!agdev->out_ep) {
  472. dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
  473. return ret;
  474. }
  475. agdev->in_ep = usb_ep_autoconfig(gadget, &fs_epin_desc);
  476. if (!agdev->in_ep) {
  477. dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
  478. return ret;
  479. }
  480. agdev->in_ep_maxpsize = max_t(u16,
  481. le16_to_cpu(fs_epin_desc.wMaxPacketSize),
  482. le16_to_cpu(hs_epin_desc.wMaxPacketSize));
  483. agdev->out_ep_maxpsize = max_t(u16,
  484. le16_to_cpu(fs_epout_desc.wMaxPacketSize),
  485. le16_to_cpu(hs_epout_desc.wMaxPacketSize));
  486. hs_epout_desc.bEndpointAddress = fs_epout_desc.bEndpointAddress;
  487. hs_epin_desc.bEndpointAddress = fs_epin_desc.bEndpointAddress;
  488. ret = usb_assign_descriptors(fn, fs_audio_desc, hs_audio_desc, NULL,
  489. NULL);
  490. if (ret)
  491. return ret;
  492. agdev->gadget = gadget;
  493. agdev->params.p_chmask = uac2_opts->p_chmask;
  494. agdev->params.p_srate = uac2_opts->p_srate;
  495. agdev->params.p_ssize = uac2_opts->p_ssize;
  496. agdev->params.c_chmask = uac2_opts->c_chmask;
  497. agdev->params.c_srate = uac2_opts->c_srate;
  498. agdev->params.c_ssize = uac2_opts->c_ssize;
  499. agdev->params.req_number = uac2_opts->req_number;
  500. ret = g_audio_setup(agdev, "UAC2 PCM", "UAC2_Gadget");
  501. if (ret)
  502. goto err_free_descs;
  503. return 0;
  504. err_free_descs:
  505. usb_free_all_descriptors(fn);
  506. agdev->gadget = NULL;
  507. return ret;
  508. }
  509. static int
  510. afunc_set_alt(struct usb_function *fn, unsigned intf, unsigned alt)
  511. {
  512. struct usb_composite_dev *cdev = fn->config->cdev;
  513. struct f_uac2 *uac2 = func_to_uac2(fn);
  514. struct usb_gadget *gadget = cdev->gadget;
  515. struct device *dev = &gadget->dev;
  516. int ret = 0;
  517. /* No i/f has more than 2 alt settings */
  518. if (alt > 1) {
  519. dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
  520. return -EINVAL;
  521. }
  522. if (intf == uac2->ac_intf) {
  523. /* Control I/f has only 1 AltSetting - 0 */
  524. if (alt) {
  525. dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
  526. return -EINVAL;
  527. }
  528. return 0;
  529. }
  530. if (intf == uac2->as_out_intf) {
  531. uac2->as_out_alt = alt;
  532. if (alt)
  533. ret = u_audio_start_capture(&uac2->g_audio);
  534. else
  535. u_audio_stop_capture(&uac2->g_audio);
  536. } else if (intf == uac2->as_in_intf) {
  537. uac2->as_in_alt = alt;
  538. if (alt)
  539. ret = u_audio_start_playback(&uac2->g_audio);
  540. else
  541. u_audio_stop_playback(&uac2->g_audio);
  542. } else {
  543. dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
  544. return -EINVAL;
  545. }
  546. return ret;
  547. }
  548. static int
  549. afunc_get_alt(struct usb_function *fn, unsigned intf)
  550. {
  551. struct f_uac2 *uac2 = func_to_uac2(fn);
  552. struct g_audio *agdev = func_to_g_audio(fn);
  553. if (intf == uac2->ac_intf)
  554. return uac2->ac_alt;
  555. else if (intf == uac2->as_out_intf)
  556. return uac2->as_out_alt;
  557. else if (intf == uac2->as_in_intf)
  558. return uac2->as_in_alt;
  559. else
  560. dev_err(&agdev->gadget->dev,
  561. "%s:%d Invalid Interface %d!\n",
  562. __func__, __LINE__, intf);
  563. return -EINVAL;
  564. }
  565. static void
  566. afunc_disable(struct usb_function *fn)
  567. {
  568. struct f_uac2 *uac2 = func_to_uac2(fn);
  569. uac2->as_in_alt = 0;
  570. uac2->as_out_alt = 0;
  571. u_audio_stop_capture(&uac2->g_audio);
  572. u_audio_stop_playback(&uac2->g_audio);
  573. }
  574. static int
  575. in_rq_cur(struct usb_function *fn, const struct usb_ctrlrequest *cr)
  576. {
  577. struct usb_request *req = fn->config->cdev->req;
  578. struct g_audio *agdev = func_to_g_audio(fn);
  579. struct f_uac2_opts *opts;
  580. u16 w_length = le16_to_cpu(cr->wLength);
  581. u16 w_index = le16_to_cpu(cr->wIndex);
  582. u16 w_value = le16_to_cpu(cr->wValue);
  583. u8 entity_id = (w_index >> 8) & 0xff;
  584. u8 control_selector = w_value >> 8;
  585. int value = -EOPNOTSUPP;
  586. int p_srate, c_srate;
  587. opts = g_audio_to_uac2_opts(agdev);
  588. p_srate = opts->p_srate;
  589. c_srate = opts->c_srate;
  590. if (control_selector == UAC2_CS_CONTROL_SAM_FREQ) {
  591. struct cntrl_cur_lay3 c;
  592. memset(&c, 0, sizeof(struct cntrl_cur_lay3));
  593. if (entity_id == USB_IN_CLK_ID)
  594. c.dCUR = p_srate;
  595. else if (entity_id == USB_OUT_CLK_ID)
  596. c.dCUR = c_srate;
  597. value = min_t(unsigned, w_length, sizeof c);
  598. memcpy(req->buf, &c, value);
  599. } else if (control_selector == UAC2_CS_CONTROL_CLOCK_VALID) {
  600. *(u8 *)req->buf = 1;
  601. value = min_t(unsigned, w_length, 1);
  602. } else {
  603. dev_err(&agdev->gadget->dev,
  604. "%s:%d control_selector=%d TODO!\n",
  605. __func__, __LINE__, control_selector);
  606. }
  607. return value;
  608. }
  609. static int
  610. in_rq_range(struct usb_function *fn, const struct usb_ctrlrequest *cr)
  611. {
  612. struct usb_request *req = fn->config->cdev->req;
  613. struct g_audio *agdev = func_to_g_audio(fn);
  614. struct f_uac2_opts *opts;
  615. u16 w_length = le16_to_cpu(cr->wLength);
  616. u16 w_index = le16_to_cpu(cr->wIndex);
  617. u16 w_value = le16_to_cpu(cr->wValue);
  618. u8 entity_id = (w_index >> 8) & 0xff;
  619. u8 control_selector = w_value >> 8;
  620. struct cntrl_range_lay3 r;
  621. int value = -EOPNOTSUPP;
  622. int p_srate, c_srate;
  623. opts = g_audio_to_uac2_opts(agdev);
  624. p_srate = opts->p_srate;
  625. c_srate = opts->c_srate;
  626. if (control_selector == UAC2_CS_CONTROL_SAM_FREQ) {
  627. if (entity_id == USB_IN_CLK_ID)
  628. r.dMIN = p_srate;
  629. else if (entity_id == USB_OUT_CLK_ID)
  630. r.dMIN = c_srate;
  631. else
  632. return -EOPNOTSUPP;
  633. r.dMAX = r.dMIN;
  634. r.dRES = 0;
  635. r.wNumSubRanges = 1;
  636. value = min_t(unsigned, w_length, sizeof r);
  637. memcpy(req->buf, &r, value);
  638. } else {
  639. dev_err(&agdev->gadget->dev,
  640. "%s:%d control_selector=%d TODO!\n",
  641. __func__, __LINE__, control_selector);
  642. }
  643. return value;
  644. }
  645. static int
  646. ac_rq_in(struct usb_function *fn, const struct usb_ctrlrequest *cr)
  647. {
  648. if (cr->bRequest == UAC2_CS_CUR)
  649. return in_rq_cur(fn, cr);
  650. else if (cr->bRequest == UAC2_CS_RANGE)
  651. return in_rq_range(fn, cr);
  652. else
  653. return -EOPNOTSUPP;
  654. }
  655. static int
  656. out_rq_cur(struct usb_function *fn, const struct usb_ctrlrequest *cr)
  657. {
  658. u16 w_length = le16_to_cpu(cr->wLength);
  659. u16 w_value = le16_to_cpu(cr->wValue);
  660. u8 control_selector = w_value >> 8;
  661. if (control_selector == UAC2_CS_CONTROL_SAM_FREQ)
  662. return w_length;
  663. return -EOPNOTSUPP;
  664. }
  665. static int
  666. setup_rq_inf(struct usb_function *fn, const struct usb_ctrlrequest *cr)
  667. {
  668. struct f_uac2 *uac2 = func_to_uac2(fn);
  669. struct g_audio *agdev = func_to_g_audio(fn);
  670. u16 w_index = le16_to_cpu(cr->wIndex);
  671. u8 intf = w_index & 0xff;
  672. if (intf != uac2->ac_intf) {
  673. dev_err(&agdev->gadget->dev,
  674. "%s:%d Error!\n", __func__, __LINE__);
  675. return -EOPNOTSUPP;
  676. }
  677. if (cr->bRequestType & USB_DIR_IN)
  678. return ac_rq_in(fn, cr);
  679. else if (cr->bRequest == UAC2_CS_CUR)
  680. return out_rq_cur(fn, cr);
  681. return -EOPNOTSUPP;
  682. }
  683. static int
  684. afunc_setup(struct usb_function *fn, const struct usb_ctrlrequest *cr)
  685. {
  686. struct usb_composite_dev *cdev = fn->config->cdev;
  687. struct g_audio *agdev = func_to_g_audio(fn);
  688. struct usb_request *req = cdev->req;
  689. u16 w_length = le16_to_cpu(cr->wLength);
  690. int value = -EOPNOTSUPP;
  691. /* Only Class specific requests are supposed to reach here */
  692. if ((cr->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS)
  693. return -EOPNOTSUPP;
  694. if ((cr->bRequestType & USB_RECIP_MASK) == USB_RECIP_INTERFACE)
  695. value = setup_rq_inf(fn, cr);
  696. else
  697. dev_err(&agdev->gadget->dev, "%s:%d Error!\n",
  698. __func__, __LINE__);
  699. if (value >= 0) {
  700. req->length = value;
  701. req->zero = value < w_length;
  702. value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
  703. if (value < 0) {
  704. dev_err(&agdev->gadget->dev,
  705. "%s:%d Error!\n", __func__, __LINE__);
  706. req->status = 0;
  707. }
  708. }
  709. return value;
  710. }
  711. static inline struct f_uac2_opts *to_f_uac2_opts(struct config_item *item)
  712. {
  713. return container_of(to_config_group(item), struct f_uac2_opts,
  714. func_inst.group);
  715. }
  716. static void f_uac2_attr_release(struct config_item *item)
  717. {
  718. struct f_uac2_opts *opts = to_f_uac2_opts(item);
  719. usb_put_function_instance(&opts->func_inst);
  720. }
  721. static struct configfs_item_operations f_uac2_item_ops = {
  722. .release = f_uac2_attr_release,
  723. };
  724. #define UAC2_ATTRIBUTE(name) \
  725. static ssize_t f_uac2_opts_##name##_show(struct config_item *item, \
  726. char *page) \
  727. { \
  728. struct f_uac2_opts *opts = to_f_uac2_opts(item); \
  729. int result; \
  730. \
  731. mutex_lock(&opts->lock); \
  732. result = sprintf(page, "%u\n", opts->name); \
  733. mutex_unlock(&opts->lock); \
  734. \
  735. return result; \
  736. } \
  737. \
  738. static ssize_t f_uac2_opts_##name##_store(struct config_item *item, \
  739. const char *page, size_t len) \
  740. { \
  741. struct f_uac2_opts *opts = to_f_uac2_opts(item); \
  742. int ret; \
  743. u32 num; \
  744. \
  745. mutex_lock(&opts->lock); \
  746. if (opts->refcnt) { \
  747. ret = -EBUSY; \
  748. goto end; \
  749. } \
  750. \
  751. ret = kstrtou32(page, 0, &num); \
  752. if (ret) \
  753. goto end; \
  754. \
  755. opts->name = num; \
  756. ret = len; \
  757. \
  758. end: \
  759. mutex_unlock(&opts->lock); \
  760. return ret; \
  761. } \
  762. \
  763. CONFIGFS_ATTR(f_uac2_opts_, name)
  764. UAC2_ATTRIBUTE(p_chmask);
  765. UAC2_ATTRIBUTE(p_srate);
  766. UAC2_ATTRIBUTE(p_ssize);
  767. UAC2_ATTRIBUTE(c_chmask);
  768. UAC2_ATTRIBUTE(c_srate);
  769. UAC2_ATTRIBUTE(c_ssize);
  770. UAC2_ATTRIBUTE(req_number);
  771. static struct configfs_attribute *f_uac2_attrs[] = {
  772. &f_uac2_opts_attr_p_chmask,
  773. &f_uac2_opts_attr_p_srate,
  774. &f_uac2_opts_attr_p_ssize,
  775. &f_uac2_opts_attr_c_chmask,
  776. &f_uac2_opts_attr_c_srate,
  777. &f_uac2_opts_attr_c_ssize,
  778. &f_uac2_opts_attr_req_number,
  779. NULL,
  780. };
  781. static struct config_item_type f_uac2_func_type = {
  782. .ct_item_ops = &f_uac2_item_ops,
  783. .ct_attrs = f_uac2_attrs,
  784. .ct_owner = THIS_MODULE,
  785. };
  786. static void afunc_free_inst(struct usb_function_instance *f)
  787. {
  788. struct f_uac2_opts *opts;
  789. opts = container_of(f, struct f_uac2_opts, func_inst);
  790. kfree(opts);
  791. }
  792. static struct usb_function_instance *afunc_alloc_inst(void)
  793. {
  794. struct f_uac2_opts *opts;
  795. opts = kzalloc(sizeof(*opts), GFP_KERNEL);
  796. if (!opts)
  797. return ERR_PTR(-ENOMEM);
  798. mutex_init(&opts->lock);
  799. opts->func_inst.free_func_inst = afunc_free_inst;
  800. config_group_init_type_name(&opts->func_inst.group, "",
  801. &f_uac2_func_type);
  802. opts->p_chmask = UAC2_DEF_PCHMASK;
  803. opts->p_srate = UAC2_DEF_PSRATE;
  804. opts->p_ssize = UAC2_DEF_PSSIZE;
  805. opts->c_chmask = UAC2_DEF_CCHMASK;
  806. opts->c_srate = UAC2_DEF_CSRATE;
  807. opts->c_ssize = UAC2_DEF_CSSIZE;
  808. opts->req_number = UAC2_DEF_REQ_NUM;
  809. return &opts->func_inst;
  810. }
  811. static void afunc_free(struct usb_function *f)
  812. {
  813. struct g_audio *agdev;
  814. struct f_uac2_opts *opts;
  815. agdev = func_to_g_audio(f);
  816. opts = container_of(f->fi, struct f_uac2_opts, func_inst);
  817. kfree(agdev);
  818. mutex_lock(&opts->lock);
  819. --opts->refcnt;
  820. mutex_unlock(&opts->lock);
  821. }
  822. static void afunc_unbind(struct usb_configuration *c, struct usb_function *f)
  823. {
  824. struct g_audio *agdev = func_to_g_audio(f);
  825. g_audio_cleanup(agdev);
  826. usb_free_all_descriptors(f);
  827. agdev->gadget = NULL;
  828. }
  829. static struct usb_function *afunc_alloc(struct usb_function_instance *fi)
  830. {
  831. struct f_uac2 *uac2;
  832. struct f_uac2_opts *opts;
  833. uac2 = kzalloc(sizeof(*uac2), GFP_KERNEL);
  834. if (uac2 == NULL)
  835. return ERR_PTR(-ENOMEM);
  836. opts = container_of(fi, struct f_uac2_opts, func_inst);
  837. mutex_lock(&opts->lock);
  838. ++opts->refcnt;
  839. mutex_unlock(&opts->lock);
  840. uac2->g_audio.func.name = "uac2_func";
  841. uac2->g_audio.func.bind = afunc_bind;
  842. uac2->g_audio.func.unbind = afunc_unbind;
  843. uac2->g_audio.func.set_alt = afunc_set_alt;
  844. uac2->g_audio.func.get_alt = afunc_get_alt;
  845. uac2->g_audio.func.disable = afunc_disable;
  846. uac2->g_audio.func.setup = afunc_setup;
  847. uac2->g_audio.func.free_func = afunc_free;
  848. return &uac2->g_audio.func;
  849. }
  850. DECLARE_USB_FUNCTION_INIT(uac2, afunc_alloc_inst, afunc_alloc);
  851. MODULE_LICENSE("GPL");
  852. MODULE_AUTHOR("Yadwinder Singh");
  853. MODULE_AUTHOR("Jaswinder Singh");