f_uac1.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268
  1. /*
  2. * f_audio.c -- USB Audio class function driver
  3. *
  4. * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
  5. * Copyright (C) 2008 Bryan Wu <cooloney@kernel.org>
  6. * Copyright (C) 2008 Analog Devices, Inc
  7. *
  8. * Enter bugs at http://blackfin.uclinux.org/
  9. *
  10. * Licensed under the GPL-2 or later.
  11. */
  12. #ifdef pr_fmt
  13. #undef pr_fmt
  14. #endif
  15. #define pr_fmt(fmt) "%s: " fmt, __func__
  16. #include <linux/slab.h>
  17. #include <linux/kernel.h>
  18. #include <linux/device.h>
  19. #include <linux/atomic.h>
  20. #include <sound/core.h>
  21. #include <sound/initval.h>
  22. #include "u_uac1.h"
  23. /*
  24. * DESCRIPTORS ... most are static, but strings and full
  25. * configuration descriptors are built on demand.
  26. */
  27. /*
  28. * We have two interfaces- AudioControl and AudioStreaming
  29. */
  30. #define PLAYBACK_EP_MAX_PACKET_SIZE 32
  31. static int req_playback_buf_size = PLAYBACK_EP_MAX_PACKET_SIZE;
  32. module_param(req_playback_buf_size, int, S_IRUGO);
  33. MODULE_PARM_DESC(req_playback_buf_size, "ISO OUT endpoint (playback) request buffer size");
  34. static int req_playback_count = 48;
  35. module_param(req_playback_count, int, S_IRUGO);
  36. MODULE_PARM_DESC(req_playback_count, "ISO OUT endpoint (playback) request count");
  37. static int audio_playback_buf_size = 256*32;
  38. module_param(audio_playback_buf_size, int, S_IRUGO);
  39. MODULE_PARM_DESC(audio_playback_buf_size, "Audio buffer size");
  40. #define CAPTURE_EP_MAX_PACKET_SIZE 32
  41. static int req_capture_buf_size = CAPTURE_EP_MAX_PACKET_SIZE;
  42. module_param(req_capture_buf_size, int, S_IRUGO);
  43. MODULE_PARM_DESC(req_capture_buf_size, "ISO IN endpoint (capture) request buffer size");
  44. static int req_capture_count = 48;
  45. module_param(req_capture_count, int, S_IRUGO);
  46. MODULE_PARM_DESC(req_capture_count, "ISO IN endpoint (capture) request count");
  47. static int audio_capture_buf_size = 256*32;
  48. module_param(audio_capture_buf_size, int, S_IRUGO);
  49. MODULE_PARM_DESC(audio_capture_buf_size, "Microphone Audio buffer size");
  50. static int generic_set_cmd(struct usb_audio_control *con, u8 cmd, int value);
  51. static int generic_get_cmd(struct usb_audio_control *con, u8 cmd);
  52. #define SPEAKER_INPUT_TERMINAL_ID 3
  53. #define SPEAKER_OUTPUT_TERMINAL_ID 4
  54. #define MICROPHONE_INPUT_TERMINAL_ID 1
  55. #define MICROPHONE_OUTPUT_TERMINAL_ID 2
  56. /*
  57. * We have two interfaces- AudioControl and AudioStreaming
  58. */
  59. #define F_AUDIO_INTERFACE_MICROPHONE 2
  60. #define F_AUDIO_INTERFACE_SPEAKER 3
  61. #define F_AUDIO_NUM_INTERFACES 2
  62. /* B.3.1 Standard AC Interface Descriptor */
  63. struct usb_interface_descriptor uac1_ac_interface_desc = {
  64. .bLength = USB_DT_INTERFACE_SIZE,
  65. .bDescriptorType = USB_DT_INTERFACE,
  66. .bNumEndpoints = 0,
  67. .bInterfaceClass = USB_CLASS_AUDIO,
  68. .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL,
  69. };
  70. #define TOTAL_LENGTH ( \
  71. UAC_DT_AC_HEADER_SIZE(2) + \
  72. UAC_DT_INPUT_TERMINAL_SIZE + \
  73. UAC_DT_OUTPUT_TERMINAL_SIZE + \
  74. UAC_DT_INPUT_TERMINAL_SIZE + \
  75. UAC_DT_OUTPUT_TERMINAL_SIZE \
  76. )
  77. /* B.3.2 Class-Specific AC Interface Descriptor */
  78. struct uac1_ac_header_descriptor_2 uac1_ac_header_desc = {
  79. .bLength = UAC_DT_AC_HEADER_SIZE(2),
  80. .bDescriptorType = USB_DT_CS_INTERFACE,
  81. .bDescriptorSubtype = UAC_HEADER,
  82. .bcdADC = __constant_cpu_to_le16(0x0100),
  83. .wTotalLength = __constant_cpu_to_le16(TOTAL_LENGTH),
  84. .bInCollection = F_AUDIO_NUM_INTERFACES,
  85. /*.baInterfaceNr = {
  86. [0] = F_AUDIO_INTERFACE_MICROPHONE,
  87. [1] = F_AUDIO_INTERFACE_SPEAKER,
  88. }
  89. */
  90. };
  91. /*---------------------------------*/
  92. struct uac_input_terminal_descriptor speaker_input_terminal_desc = {
  93. .bLength = UAC_DT_INPUT_TERMINAL_SIZE,
  94. .bDescriptorType = USB_DT_CS_INTERFACE,
  95. .bDescriptorSubtype = UAC_INPUT_TERMINAL,
  96. .bTerminalID = SPEAKER_INPUT_TERMINAL_ID,
  97. .wTerminalType = UAC_TERMINAL_STREAMING,
  98. .bAssocTerminal = SPEAKER_OUTPUT_TERMINAL_ID,
  99. .wChannelConfig = 0x3,
  100. };
  101. struct uac1_output_terminal_descriptor speaker_output_terminal_desc = {
  102. .bLength = UAC_DT_OUTPUT_TERMINAL_SIZE,
  103. .bDescriptorType = USB_DT_CS_INTERFACE,
  104. .bDescriptorSubtype = UAC_OUTPUT_TERMINAL,
  105. .bTerminalID = SPEAKER_OUTPUT_TERMINAL_ID,
  106. .wTerminalType = UAC_OUTPUT_TERMINAL_SPEAKER,
  107. .bAssocTerminal = SPEAKER_INPUT_TERMINAL_ID,
  108. .bSourceID = SPEAKER_INPUT_TERMINAL_ID,
  109. };
  110. static struct usb_audio_control speaker_mute_control = {
  111. .list = LIST_HEAD_INIT(speaker_mute_control.list),
  112. .name = "Speaker Mute Control",
  113. .type = UAC_FU_MUTE,
  114. /* Todo: add real Mute control code */
  115. .set = generic_set_cmd,
  116. .get = generic_get_cmd,
  117. };
  118. static struct usb_audio_control speaker_volume_control = {
  119. .list = LIST_HEAD_INIT(speaker_volume_control.list),
  120. .name = "Speaker Volume Control",
  121. .type = UAC_FU_VOLUME,
  122. /* Todo: add real Volume control code */
  123. .set = generic_set_cmd,
  124. .get = generic_get_cmd,
  125. };
  126. static struct usb_audio_control_selector speaker_fu_controls = {
  127. .list = LIST_HEAD_INIT(speaker_fu_controls.list),
  128. .name = "Speaker Function Unit Controls",
  129. };
  130. static struct uac_input_terminal_descriptor microphone_input_terminal_desc = {
  131. .bLength = UAC_DT_INPUT_TERMINAL_SIZE,
  132. .bDescriptorType = USB_DT_CS_INTERFACE,
  133. .bDescriptorSubtype = UAC_INPUT_TERMINAL,
  134. .bTerminalID = MICROPHONE_INPUT_TERMINAL_ID,
  135. .wTerminalType = UAC_INPUT_TERMINAL_MICROPHONE,
  136. .bAssocTerminal = MICROPHONE_OUTPUT_TERMINAL_ID,
  137. .bNrChannels = 1,
  138. .wChannelConfig = 0x3,
  139. };
  140. static struct
  141. uac1_output_terminal_descriptor microphone_output_terminal_desc = {
  142. .bLength = UAC_DT_OUTPUT_TERMINAL_SIZE,
  143. .bDescriptorType = USB_DT_CS_INTERFACE,
  144. .bDescriptorSubtype = UAC_OUTPUT_TERMINAL,
  145. .bTerminalID = MICROPHONE_OUTPUT_TERMINAL_ID,
  146. .wTerminalType = UAC_TERMINAL_STREAMING,
  147. .bAssocTerminal = MICROPHONE_INPUT_TERMINAL_ID,
  148. .bSourceID = MICROPHONE_INPUT_TERMINAL_ID,
  149. };
  150. static struct usb_audio_control microphone_mute_control = {
  151. .list = LIST_HEAD_INIT(microphone_mute_control.list),
  152. .name = "Microphone Mute Control",
  153. .type = UAC_FU_MUTE,
  154. /* Todo: add real Mute control code */
  155. .set = generic_set_cmd,
  156. .get = generic_get_cmd,
  157. };
  158. static struct usb_audio_control microphone_volume_control = {
  159. .list = LIST_HEAD_INIT(microphone_volume_control.list),
  160. .name = "Microphone Volume Control",
  161. .type = UAC_FU_VOLUME,
  162. /* Todo: add real Volume control code */
  163. .set = generic_set_cmd,
  164. .get = generic_get_cmd,
  165. };
  166. static struct usb_audio_control_selector microphone_fu_controls = {
  167. .list = LIST_HEAD_INIT(microphone_fu_controls.list),
  168. .name = "Microphone Feature Unit Controls",
  169. };
  170. /*---------------------------------*/
  171. /* B.4.1 Standard AS Interface Descriptor */
  172. static struct usb_interface_descriptor speaker_as_interface_alt_0_desc = {
  173. .bLength = USB_DT_INTERFACE_SIZE,
  174. .bDescriptorType = USB_DT_INTERFACE,
  175. .bAlternateSetting = 0,
  176. .bNumEndpoints = 0,
  177. .bInterfaceClass = USB_CLASS_AUDIO,
  178. .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
  179. };
  180. static struct usb_interface_descriptor speaker_as_interface_alt_1_desc = {
  181. .bLength = USB_DT_INTERFACE_SIZE,
  182. .bDescriptorType = USB_DT_INTERFACE,
  183. .bAlternateSetting = 1,
  184. .bNumEndpoints = 1,
  185. .bInterfaceClass = USB_CLASS_AUDIO,
  186. .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
  187. };
  188. /* B.4.2 Class-Specific AS Interface Descriptor */
  189. static struct uac1_as_header_descriptor speaker_as_header_desc = {
  190. .bLength = UAC_DT_AS_HEADER_SIZE,
  191. .bDescriptorType = USB_DT_CS_INTERFACE,
  192. .bDescriptorSubtype = UAC_AS_GENERAL,
  193. .bTerminalLink = SPEAKER_INPUT_TERMINAL_ID,
  194. .bDelay = 1,
  195. .wFormatTag = UAC_FORMAT_TYPE_I_PCM,
  196. };
  197. static struct uac_format_type_i_discrete_descriptor_1 speaker_as_type_i_desc = {
  198. .bLength = UAC_FORMAT_TYPE_I_DISCRETE_DESC_SIZE(1),
  199. .bDescriptorType = USB_DT_CS_INTERFACE,
  200. .bDescriptorSubtype = UAC_FORMAT_TYPE,
  201. .bFormatType = UAC_FORMAT_TYPE_I,
  202. .bSubframeSize = 2,
  203. .bBitResolution = 16,
  204. .bSamFreqType = 1,
  205. };
  206. /* Standard ISO OUT Endpoint Descriptor */
  207. static struct usb_endpoint_descriptor speaker_as_ep_out_desc = {
  208. .bLength = USB_DT_ENDPOINT_AUDIO_SIZE,
  209. .bDescriptorType = USB_DT_ENDPOINT,
  210. .bEndpointAddress = USB_DIR_OUT,
  211. .bmAttributes = USB_ENDPOINT_SYNC_ADAPTIVE |
  212. USB_ENDPOINT_XFER_ISOC,
  213. .wMaxPacketSize =
  214. __constant_cpu_to_le16(PLAYBACK_EP_MAX_PACKET_SIZE),
  215. .bInterval = 4,
  216. };
  217. /* Class-specific AS ISO OUT Endpoint Descriptor */
  218. static struct uac_iso_endpoint_descriptor speaker_as_iso_out_desc = {
  219. .bLength = UAC_ISO_ENDPOINT_DESC_SIZE,
  220. .bDescriptorType = USB_DT_CS_ENDPOINT,
  221. .bDescriptorSubtype = UAC_EP_GENERAL,
  222. .bmAttributes = 1,
  223. .bLockDelayUnits = 1,
  224. .wLockDelay = __constant_cpu_to_le16(1),
  225. };
  226. static struct usb_audio_control speaker_sample_freq_control = {
  227. .list = LIST_HEAD_INIT(speaker_sample_freq_control.list),
  228. .name = "Speaker Sampling Frequency Control",
  229. .type = UAC_EP_CS_ATTR_SAMPLE_RATE,
  230. .set = generic_set_cmd,
  231. .get = generic_get_cmd,
  232. };
  233. static struct usb_audio_control_selector speaker_as_iso_out = {
  234. .list = LIST_HEAD_INIT(speaker_as_iso_out.list),
  235. .name = "Speaker Iso-out Endpoint Control",
  236. .type = UAC_EP_GENERAL,
  237. .desc = (struct usb_descriptor_header *)&speaker_as_iso_out_desc,
  238. };
  239. /*---------------------------------*/
  240. /* B.4.1 Standard AS Interface Descriptor */
  241. static struct usb_interface_descriptor microphone_as_interface_alt_0_desc = {
  242. .bLength = USB_DT_INTERFACE_SIZE,
  243. .bDescriptorType = USB_DT_INTERFACE,
  244. .bAlternateSetting = 0,
  245. .bNumEndpoints = 0,
  246. .bInterfaceClass = USB_CLASS_AUDIO,
  247. .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
  248. };
  249. static struct usb_interface_descriptor microphone_as_interface_alt_1_desc = {
  250. .bLength = USB_DT_INTERFACE_SIZE,
  251. .bDescriptorType = USB_DT_INTERFACE,
  252. .bAlternateSetting = 1,
  253. .bNumEndpoints = 1,
  254. .bInterfaceClass = USB_CLASS_AUDIO,
  255. .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
  256. };
  257. /* B.4.2 Class-Specific AS Interface Descriptor */
  258. static struct uac1_as_header_descriptor microphone_as_header_desc = {
  259. .bLength = UAC_DT_AS_HEADER_SIZE,
  260. .bDescriptorType = USB_DT_CS_INTERFACE,
  261. .bDescriptorSubtype = UAC_AS_GENERAL,
  262. .bTerminalLink = MICROPHONE_OUTPUT_TERMINAL_ID,
  263. .bDelay = 1,
  264. .wFormatTag = UAC_FORMAT_TYPE_I_PCM,
  265. };
  266. static struct
  267. uac_format_type_i_discrete_descriptor_1 microphone_as_type_i_desc = {
  268. .bLength = UAC_FORMAT_TYPE_I_DISCRETE_DESC_SIZE(1),
  269. .bDescriptorType = USB_DT_CS_INTERFACE,
  270. .bDescriptorSubtype = UAC_FORMAT_TYPE,
  271. .bFormatType = UAC_FORMAT_TYPE_I,
  272. .bNrChannels = 1,
  273. .bSubframeSize = 2,
  274. .bBitResolution = 16,
  275. .bSamFreqType = 1,
  276. };
  277. /* Standard ISO IN Endpoint Descriptor */
  278. static struct usb_endpoint_descriptor microphone_as_ep_in_desc = {
  279. .bLength = USB_DT_ENDPOINT_AUDIO_SIZE,
  280. .bDescriptorType = USB_DT_ENDPOINT,
  281. .bEndpointAddress = USB_DIR_IN,
  282. .bmAttributes =
  283. USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC,
  284. .wMaxPacketSize =
  285. __constant_cpu_to_le16(CAPTURE_EP_MAX_PACKET_SIZE),
  286. .bInterval = 4,
  287. };
  288. /* Class-specific AS ISO OUT Endpoint Descriptor */
  289. static struct uac_iso_endpoint_descriptor microphone_as_iso_in_desc = {
  290. .bLength = UAC_ISO_ENDPOINT_DESC_SIZE,
  291. .bDescriptorType = USB_DT_CS_ENDPOINT,
  292. .bDescriptorSubtype = UAC_EP_GENERAL,
  293. .bmAttributes = 1,
  294. .bLockDelayUnits = 1,
  295. .wLockDelay = __constant_cpu_to_le16(1),
  296. };
  297. static struct usb_audio_control microphone_sample_freq_control = {
  298. .list = LIST_HEAD_INIT(microphone_sample_freq_control.list),
  299. .name = "Microphone Sampling Frequency Control",
  300. .type = UAC_EP_CS_ATTR_SAMPLE_RATE,
  301. .set = generic_set_cmd,
  302. .get = generic_get_cmd,
  303. };
  304. static struct usb_audio_control_selector microphone_as_iso_in = {
  305. .list = LIST_HEAD_INIT(microphone_as_iso_in.list),
  306. .name = "Microphone Iso-IN Endpoint Control",
  307. .type = UAC_EP_GENERAL,
  308. .desc = (struct usb_descriptor_header *)&microphone_as_iso_in_desc,
  309. };
  310. /*--------------------------------- */
  311. static struct usb_descriptor_header *f_audio_desc[] = {
  312. (struct usb_descriptor_header *)&uac1_ac_interface_desc,
  313. (struct usb_descriptor_header *)&uac1_ac_header_desc,
  314. (struct usb_descriptor_header *)&microphone_input_terminal_desc,
  315. (struct usb_descriptor_header *)&microphone_output_terminal_desc,
  316. (struct usb_descriptor_header *)&speaker_input_terminal_desc,
  317. (struct usb_descriptor_header *)&speaker_output_terminal_desc,
  318. (struct usb_descriptor_header *)&microphone_as_interface_alt_0_desc,
  319. (struct usb_descriptor_header *)&microphone_as_interface_alt_1_desc,
  320. (struct usb_descriptor_header *)&microphone_as_header_desc,
  321. (struct usb_descriptor_header *)&microphone_as_type_i_desc,
  322. (struct usb_descriptor_header *)&microphone_as_ep_in_desc,
  323. (struct usb_descriptor_header *)&microphone_as_iso_in_desc,
  324. (struct usb_descriptor_header *)&speaker_as_interface_alt_0_desc,
  325. (struct usb_descriptor_header *)&speaker_as_interface_alt_1_desc,
  326. (struct usb_descriptor_header *)&speaker_as_header_desc,
  327. (struct usb_descriptor_header *)&speaker_as_type_i_desc,
  328. (struct usb_descriptor_header *)&speaker_as_ep_out_desc,
  329. (struct usb_descriptor_header *)&speaker_as_iso_out_desc,
  330. NULL,
  331. };
  332. /* string IDs are assigned dynamically */
  333. static struct usb_string audio_string_defs[] = {
  334. { } /* end of list */
  335. };
  336. static struct usb_gadget_strings audio_stringtab_dev = {
  337. .language = 0x0409, /* en-us */
  338. .strings = audio_string_defs,
  339. };
  340. static struct usb_gadget_strings *audio_strings[] = {
  341. &audio_stringtab_dev,
  342. NULL,
  343. };
  344. /*
  345. * This function is an ALSA sound card following USB Audio Class Spec 1.0.
  346. */
  347. /*-------------------------------------------------------------------------*/
  348. struct f_audio_buf {
  349. u8 *buf;
  350. int actual;
  351. struct list_head list;
  352. };
  353. static struct f_audio_buf *f_audio_buffer_alloc(int buf_size)
  354. {
  355. struct f_audio_buf *playback_copy_buf;
  356. playback_copy_buf = kzalloc(sizeof *playback_copy_buf, GFP_ATOMIC);
  357. if (!playback_copy_buf) {
  358. pr_err("Failed to allocate playback_copy_buf");
  359. return ERR_PTR(-ENOMEM);
  360. }
  361. playback_copy_buf->buf = kzalloc(buf_size, GFP_ATOMIC);
  362. if (!playback_copy_buf->buf) {
  363. pr_err("Failed to allocate playback_copy_buf buffer");
  364. kfree(playback_copy_buf);
  365. return ERR_PTR(-ENOMEM);
  366. }
  367. return playback_copy_buf;
  368. }
  369. static void f_audio_buffer_free(struct f_audio_buf *audio_buf)
  370. {
  371. if (audio_buf) {
  372. kfree(audio_buf->buf);
  373. audio_buf->buf = NULL;
  374. kfree(audio_buf);
  375. audio_buf = NULL;
  376. }
  377. }
  378. /*-------------------------------------------------------------------------*/
  379. struct f_audio {
  380. struct gaudio card;
  381. /* endpoints handle full and/or high speeds */
  382. struct usb_ep *out_ep;
  383. struct usb_ep *in_ep;
  384. spinlock_t playback_lock;
  385. struct f_audio_buf *playback_copy_buf;
  386. struct work_struct playback_work;
  387. struct list_head play_queue;
  388. spinlock_t capture_lock;
  389. struct f_audio_buf *capture_copy_buf;
  390. struct work_struct capture_work;
  391. struct list_head capture_queue;
  392. struct usb_request *capture_req;
  393. u8 alt_intf[F_AUDIO_NUM_INTERFACES];
  394. /* Control Set command */
  395. struct list_head fu_cs;
  396. struct list_head ep_cs;
  397. u8 set_cmd;
  398. struct usb_audio_control *set_con;
  399. };
  400. static inline struct f_audio *func_to_audio(struct usb_function *f)
  401. {
  402. return container_of(f, struct f_audio, card.func);
  403. }
  404. /*-------------------------------------------------------------------------*/
  405. static void f_audio_playback_work(struct work_struct *data)
  406. {
  407. struct f_audio *audio = container_of(data, struct f_audio,
  408. playback_work);
  409. struct f_audio_buf *play_buf;
  410. unsigned long flags;
  411. int res = 0;
  412. spin_lock_irqsave(&audio->playback_lock, flags);
  413. if (list_empty(&audio->play_queue)) {
  414. pr_err("playback_buf is empty");
  415. spin_unlock_irqrestore(&audio->playback_lock, flags);
  416. return;
  417. }
  418. play_buf = list_first_entry(&audio->play_queue,
  419. struct f_audio_buf, list);
  420. list_del(&play_buf->list);
  421. spin_unlock_irqrestore(&audio->playback_lock, flags);
  422. pr_debug("play_buf->actual = %d", play_buf->actual);
  423. res = u_audio_playback(&audio->card, play_buf->buf, play_buf->actual);
  424. if (res)
  425. pr_err("copying failed");
  426. f_audio_buffer_free(play_buf);
  427. }
  428. static int
  429. f_audio_playback_ep_complete(struct usb_ep *ep, struct usb_request *req)
  430. {
  431. struct f_audio *audio = req->context;
  432. struct f_audio_buf *copy_buf = audio->playback_copy_buf;
  433. int err;
  434. if (!copy_buf)
  435. return -EINVAL;
  436. /* Copy buffer is full, add it to the play_queue */
  437. if (audio_playback_buf_size - copy_buf->actual < req->actual) {
  438. pr_debug("audio_playback_buf_size %d - copy_buf->actual %d, req->actual %d",
  439. audio_playback_buf_size, copy_buf->actual, req->actual);
  440. list_add_tail(&copy_buf->list, &audio->play_queue);
  441. schedule_work(&audio->playback_work);
  442. copy_buf = f_audio_buffer_alloc(audio_playback_buf_size);
  443. if (IS_ERR(copy_buf)) {
  444. pr_err("Failed to allocate playback_copy_buf");
  445. return -ENOMEM;
  446. }
  447. }
  448. memcpy(copy_buf->buf + copy_buf->actual, req->buf, req->actual);
  449. copy_buf->actual += req->actual;
  450. audio->playback_copy_buf = copy_buf;
  451. err = usb_ep_queue(ep, req, GFP_ATOMIC);
  452. if (err)
  453. pr_err("Failed to queue %s req: err - %d\n", ep->name, err);
  454. return err;
  455. }
  456. static void f_audio_capture_work(struct work_struct *data)
  457. {
  458. struct f_audio *audio =
  459. container_of(data, struct f_audio, capture_work);
  460. struct f_audio_buf *capture_buf;
  461. unsigned long flags;
  462. int res = 0;
  463. capture_buf = f_audio_buffer_alloc(audio_capture_buf_size);
  464. if (capture_buf <= 0) {
  465. pr_err("%s: buffer alloc failed\n", __func__);
  466. return;
  467. }
  468. res = u_audio_capture(&audio->card, capture_buf->buf,
  469. audio_capture_buf_size);
  470. if (res)
  471. pr_err("copying failed");
  472. pr_debug("Queue capture packet: size %d", audio_capture_buf_size);
  473. spin_lock_irqsave(&audio->capture_lock, flags);
  474. list_add_tail(&capture_buf->list, &audio->capture_queue);
  475. spin_unlock_irqrestore(&audio->capture_lock, flags);
  476. }
  477. static int
  478. f_audio_capture_ep_complete(struct usb_ep *ep, struct usb_request *req)
  479. {
  480. struct f_audio *audio = req->context;
  481. struct f_audio_buf *copy_buf = audio->capture_copy_buf;
  482. unsigned long flags;
  483. int err = 0;
  484. if (copy_buf == 0) {
  485. pr_debug("copy_buf == 0");
  486. spin_lock_irqsave(&audio->capture_lock, flags);
  487. if (list_empty(&audio->capture_queue)) {
  488. spin_unlock_irqrestore(&audio->capture_lock, flags);
  489. schedule_work(&audio->capture_work);
  490. goto done;
  491. }
  492. copy_buf = list_first_entry(&audio->capture_queue,
  493. struct f_audio_buf, list);
  494. list_del(&copy_buf->list);
  495. audio->capture_copy_buf = copy_buf;
  496. spin_unlock_irqrestore(&audio->capture_lock, flags);
  497. }
  498. pr_debug("Copy %d bytes", req->actual);
  499. memcpy(req->buf, copy_buf->buf + copy_buf->actual, req->actual);
  500. copy_buf->actual += req->actual;
  501. if (audio_capture_buf_size - copy_buf->actual < req->actual) {
  502. f_audio_buffer_free(copy_buf);
  503. audio->capture_copy_buf = 0;
  504. schedule_work(&audio->capture_work);
  505. }
  506. done:
  507. err = usb_ep_queue(ep, req, GFP_ATOMIC);
  508. if (err)
  509. pr_err("Failed to queue %s req: err - %d\n", ep->name, err);
  510. return err;
  511. }
  512. static void f_audio_complete(struct usb_ep *ep, struct usb_request *req)
  513. {
  514. struct f_audio *audio = req->context;
  515. int status = req->status;
  516. u32 data = 0;
  517. switch (status) {
  518. case 0: /* normal completion? */
  519. if (ep == audio->out_ep) {
  520. f_audio_playback_ep_complete(ep, req);
  521. } else if (ep == audio->in_ep) {
  522. f_audio_capture_ep_complete(ep, req);
  523. } else if (audio->set_con) {
  524. memcpy(&data, req->buf, req->length);
  525. audio->set_con->set(audio->set_con, audio->set_cmd,
  526. le16_to_cpu(data));
  527. audio->set_con = NULL;
  528. }
  529. break;
  530. default:
  531. pr_err("Failed completion: status %d", status);
  532. break;
  533. }
  534. }
  535. static int audio_set_intf_req(struct usb_function *f,
  536. const struct usb_ctrlrequest *ctrl)
  537. {
  538. struct f_audio *audio = func_to_audio(f);
  539. struct usb_composite_dev *cdev = f->config->cdev;
  540. struct usb_request *req = cdev->req;
  541. u8 id = ((le16_to_cpu(ctrl->wIndex) >> 8) & 0xFF);
  542. u16 len = le16_to_cpu(ctrl->wLength);
  543. u16 w_value = le16_to_cpu(ctrl->wValue);
  544. u8 con_sel = (w_value >> 8) & 0xFF;
  545. u8 cmd = (ctrl->bRequest & 0x0F);
  546. struct usb_audio_control_selector *cs;
  547. struct usb_audio_control *con;
  548. pr_debug("bRequest 0x%x, w_value 0x%04x, len %d, entity %d\n",
  549. ctrl->bRequest, w_value, len, id);
  550. list_for_each_entry(cs, &audio->fu_cs, list) {
  551. if (cs->id == id) {
  552. list_for_each_entry(con, &cs->control, list) {
  553. if (con->type == con_sel) {
  554. audio->set_con = con;
  555. break;
  556. }
  557. }
  558. break;
  559. }
  560. }
  561. audio->set_cmd = cmd;
  562. req->context = audio;
  563. req->complete = f_audio_complete;
  564. return len;
  565. }
  566. static int audio_get_intf_req(struct usb_function *f,
  567. const struct usb_ctrlrequest *ctrl)
  568. {
  569. struct f_audio *audio = func_to_audio(f);
  570. struct usb_composite_dev *cdev = f->config->cdev;
  571. struct usb_request *req = cdev->req;
  572. int value = -EOPNOTSUPP;
  573. u8 id = ((le16_to_cpu(ctrl->wIndex) >> 8) & 0xFF);
  574. u16 len = le16_to_cpu(ctrl->wLength);
  575. u16 w_value = le16_to_cpu(ctrl->wValue);
  576. u8 con_sel = (w_value >> 8) & 0xFF;
  577. u8 cmd = (ctrl->bRequest & 0x0F);
  578. struct usb_audio_control_selector *cs;
  579. struct usb_audio_control *con;
  580. pr_debug("bRequest 0x%x, w_value 0x%04x, len %d, entity %d\n",
  581. ctrl->bRequest, w_value, len, id);
  582. list_for_each_entry(cs, &audio->fu_cs, list) {
  583. if (cs->id == id) {
  584. list_for_each_entry(con, &cs->control, list) {
  585. if (con->type == con_sel && con->get) {
  586. value = con->get(con, cmd);
  587. break;
  588. }
  589. }
  590. break;
  591. }
  592. }
  593. req->context = audio;
  594. req->complete = f_audio_complete;
  595. memcpy(req->buf, &value, len);
  596. return len;
  597. }
  598. static void audio_set_endpoint_complete(struct usb_ep *ep,
  599. struct usb_request *req)
  600. {
  601. struct f_audio *audio = req->context;
  602. u32 data = 0;
  603. if (req->status == 0 && audio->set_con) {
  604. memcpy(&data, req->buf, req->length);
  605. audio->set_con->set(audio->set_con, audio->set_cmd,
  606. le32_to_cpu(data));
  607. audio->set_con = NULL;
  608. }
  609. }
  610. static int audio_set_endpoint_req(struct usb_function *f,
  611. const struct usb_ctrlrequest *ctrl)
  612. {
  613. int value = -EOPNOTSUPP;
  614. u16 ep = le16_to_cpu(ctrl->wIndex);
  615. u16 len = le16_to_cpu(ctrl->wLength);
  616. u16 w_value = le16_to_cpu(ctrl->wValue);
  617. struct f_audio *audio = func_to_audio(f);
  618. struct usb_composite_dev *cdev = f->config->cdev;
  619. struct usb_request *req = cdev->req;
  620. struct usb_audio_control_selector *cs;
  621. struct usb_audio_control *con;
  622. u8 epnum = ep & ~0x80;
  623. u8 con_sel = (w_value >> 8) & 0xFF;
  624. u8 cmd = (ctrl->bRequest & 0x0F);
  625. pr_debug("bRequest 0x%x, w_value 0x%04x, len %d, endp %d, epnum %d\n",
  626. ctrl->bRequest, w_value, len, ep, epnum);
  627. list_for_each_entry(cs, &audio->ep_cs, list) {
  628. if (cs->id != epnum)
  629. continue;
  630. list_for_each_entry(con, &cs->control, list) {
  631. if (con->type != con_sel)
  632. continue;
  633. switch (cmd) {
  634. case UAC__CUR:
  635. case UAC__MIN:
  636. case UAC__MAX:
  637. case UAC__RES:
  638. audio->set_con = con;
  639. audio->set_cmd = cmd;
  640. req->context = audio;
  641. req->complete = audio_set_endpoint_complete;
  642. value = len;
  643. break;
  644. case UAC__MEM:
  645. break;
  646. default:
  647. pr_err("Unknown command");
  648. break;
  649. }
  650. break;
  651. }
  652. break;
  653. }
  654. return value;
  655. }
  656. static int audio_get_endpoint_req(struct usb_function *f,
  657. const struct usb_ctrlrequest *ctrl)
  658. {
  659. struct f_audio *audio = func_to_audio(f);
  660. struct usb_composite_dev *cdev = f->config->cdev;
  661. struct usb_request *req = cdev->req;
  662. struct usb_audio_control_selector *cs;
  663. struct usb_audio_control *con;
  664. int data;
  665. int value = -EOPNOTSUPP;
  666. u8 ep = (le16_to_cpu(ctrl->wIndex) & 0x7F);
  667. u8 epnum = ep & ~0x80;
  668. u16 len = le16_to_cpu(ctrl->wLength);
  669. u16 w_value = le16_to_cpu(ctrl->wValue);
  670. u8 con_sel = (w_value >> 8) & 0xFF;
  671. u8 cmd = (ctrl->bRequest & 0x0F);
  672. pr_debug("bRequest 0x%x, w_value 0x%04x, len %d, ep %d\n",
  673. ctrl->bRequest, w_value, len, ep);
  674. list_for_each_entry(cs, &audio->ep_cs, list) {
  675. if (cs->id != epnum)
  676. continue;
  677. list_for_each_entry(con, &cs->control, list) {
  678. if (con->type != con_sel)
  679. continue;
  680. switch (cmd) {
  681. case UAC__CUR:
  682. case UAC__MIN:
  683. case UAC__MAX:
  684. case UAC__RES:
  685. data = cpu_to_le32(generic_get_cmd(con, cmd));
  686. memcpy(req->buf, &data, len);
  687. value = len;
  688. break;
  689. case UAC__MEM:
  690. break;
  691. default:
  692. break;
  693. }
  694. break;
  695. }
  696. break;
  697. }
  698. return value;
  699. }
  700. static int
  701. f_audio_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
  702. {
  703. struct usb_composite_dev *cdev = f->config->cdev;
  704. struct usb_request *req = cdev->req;
  705. int value = -EOPNOTSUPP;
  706. u16 w_index = le16_to_cpu(ctrl->wIndex);
  707. u16 w_value = le16_to_cpu(ctrl->wValue);
  708. u16 w_length = le16_to_cpu(ctrl->wLength);
  709. /* composite driver infrastructure handles everything; interface
  710. * activation uses set_alt().
  711. */
  712. switch (ctrl->bRequestType) {
  713. case USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE:
  714. pr_debug("USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE");
  715. value = audio_set_intf_req(f, ctrl);
  716. break;
  717. case USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE:
  718. pr_debug("USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE");
  719. value = audio_get_intf_req(f, ctrl);
  720. break;
  721. case USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_ENDPOINT:
  722. pr_debug("USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_ENDPOINT");
  723. value = audio_set_endpoint_req(f, ctrl);
  724. break;
  725. case USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT:
  726. pr_debug("USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT");
  727. value = audio_get_endpoint_req(f, ctrl);
  728. break;
  729. default:
  730. pr_err("Unknown control request %02x.%02x v%04x i%04x l%d\n",
  731. ctrl->bRequestType, ctrl->bRequest,
  732. w_value, w_index, w_length);
  733. }
  734. /* respond with data transfer or status phase? */
  735. if (value >= 0) {
  736. pr_debug("audio req %02x.%02x v%04x i%04x l%d\n",
  737. ctrl->bRequestType, ctrl->bRequest,
  738. w_value, w_index, w_length);
  739. req->zero = 1;
  740. req->length = value;
  741. value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
  742. if (value < 0)
  743. pr_err("audio response failed on err %d\n", value);
  744. } else {
  745. pr_err("STALL\n");
  746. }
  747. /* device either stalls (value < 0) or reports success */
  748. return value;
  749. }
  750. static int f_audio_get_alt(struct usb_function *f, unsigned intf)
  751. {
  752. struct f_audio *audio = func_to_audio(f);
  753. if (intf == uac1_ac_header_desc.baInterfaceNr[0])
  754. return audio->alt_intf[0];
  755. if (intf == uac1_ac_header_desc.baInterfaceNr[1])
  756. return audio->alt_intf[1];
  757. return 0;
  758. }
  759. static int f_audio_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
  760. {
  761. struct f_audio *audio = func_to_audio(f);
  762. struct usb_ep *out_ep = audio->out_ep;
  763. struct usb_ep *in_ep = audio->in_ep;
  764. struct usb_request *req;
  765. unsigned long flags;
  766. int i = 0, err = 0;
  767. pr_debug("intf %d, alt %d\n", intf, alt);
  768. if (intf == uac1_ac_header_desc.baInterfaceNr[0]) {
  769. if (alt == 1) {
  770. err = usb_ep_enable(in_ep);
  771. if (err) {
  772. pr_err("Failed to enable capture ep");
  773. return err;
  774. }
  775. in_ep->driver_data = audio;
  776. audio->capture_copy_buf = 0;
  777. /* Allocate a write buffer */
  778. req = usb_ep_alloc_request(in_ep, GFP_ATOMIC);
  779. if (!req) {
  780. pr_err("request allocation failed\n");
  781. return -ENOMEM;
  782. }
  783. req->buf = kzalloc(req_capture_buf_size,
  784. GFP_ATOMIC);
  785. if (!req->buf) {
  786. pr_err("request buffer allocation failed\n");
  787. return -ENOMEM;
  788. }
  789. req->length = req_capture_buf_size;
  790. req->context = audio;
  791. req->complete = f_audio_complete;
  792. audio->capture_req = req;
  793. err = usb_ep_queue(in_ep, req, GFP_ATOMIC);
  794. if (err)
  795. pr_err("Failed to queue %s req: err %d\n",
  796. in_ep->name, err);
  797. schedule_work(&audio->capture_work);
  798. } else {
  799. struct f_audio_buf *capture_buf;
  800. spin_lock_irqsave(&audio->capture_lock, flags);
  801. while (!list_empty(&audio->capture_queue)) {
  802. capture_buf =
  803. list_first_entry(
  804. &audio->capture_queue,
  805. struct f_audio_buf,
  806. list);
  807. list_del(&capture_buf->list);
  808. f_audio_buffer_free(capture_buf);
  809. }
  810. spin_unlock_irqrestore(&audio->capture_lock, flags);
  811. }
  812. audio->alt_intf[0] = alt;
  813. } else if (intf == uac1_ac_header_desc.baInterfaceNr[1]) {
  814. if (alt == 1) {
  815. err = usb_ep_enable(out_ep);
  816. if (err) {
  817. pr_err("Failed to enable playback ep");
  818. return err;
  819. }
  820. out_ep->driver_data = audio;
  821. audio->playback_copy_buf =
  822. f_audio_buffer_alloc(audio_playback_buf_size);
  823. if (IS_ERR(audio->playback_copy_buf)) {
  824. pr_err("Failed to allocate playback_copy_buf");
  825. return -ENOMEM;
  826. }
  827. /*
  828. * allocate a bunch of read buffers
  829. * and queue them all at once.
  830. */
  831. for (i = 0; i < req_playback_count && err == 0; i++) {
  832. req = usb_ep_alloc_request(out_ep, GFP_ATOMIC);
  833. if (!req) {
  834. pr_err("request allocation failed\n");
  835. return -ENOMEM;
  836. }
  837. req->buf = kzalloc(req_playback_buf_size,
  838. GFP_ATOMIC);
  839. if (!req->buf) {
  840. pr_err("request buffer allocation failed\n");
  841. return -ENOMEM;
  842. }
  843. req->length = req_playback_buf_size;
  844. req->context = audio;
  845. req->complete = f_audio_complete;
  846. err = usb_ep_queue(out_ep, req, GFP_ATOMIC);
  847. if (err)
  848. pr_err("Failed to queue %s queue req: err %d\n",
  849. out_ep->name, err);
  850. }
  851. pr_debug("Allocated %d requests\n", req_playback_count);
  852. } else {
  853. struct f_audio_buf *playback_copy_buf =
  854. audio->playback_copy_buf;
  855. if (playback_copy_buf) {
  856. pr_err("Schedule playback_work");
  857. list_add_tail(&playback_copy_buf->list,
  858. &audio->play_queue);
  859. schedule_work(&audio->playback_work);
  860. audio->playback_copy_buf = NULL;
  861. } else {
  862. pr_err("playback_buf is empty. Stop.");
  863. }
  864. }
  865. audio->alt_intf[1] = alt;
  866. } else {
  867. pr_err("Interface %d. Do nothing. Return %d\n", intf, err);
  868. }
  869. return err;
  870. }
  871. static void f_audio_disable(struct usb_function *f)
  872. {
  873. u_audio_clear();
  874. }
  875. /*-------------------------------------------------------------------------*/
  876. static void f_audio_build_desc(struct f_audio *audio)
  877. {
  878. struct gaudio *card = &audio->card;
  879. u8 *sam_freq;
  880. int rate;
  881. /* Set channel numbers */
  882. speaker_input_terminal_desc.bNrChannels =
  883. u_audio_get_playback_channels(card);
  884. speaker_as_type_i_desc.bNrChannels =
  885. u_audio_get_playback_channels(card);
  886. microphone_input_terminal_desc.bNrChannels =
  887. u_audio_get_capture_channels(card);
  888. microphone_as_type_i_desc.bNrChannels =
  889. u_audio_get_capture_channels(card);
  890. /* Set sample rates */
  891. rate = u_audio_get_playback_rate(card);
  892. sam_freq = speaker_as_type_i_desc.tSamFreq[0];
  893. memcpy(sam_freq, &rate, 3);
  894. rate = u_audio_get_capture_rate(card);
  895. sam_freq = microphone_as_type_i_desc.tSamFreq[0];
  896. memcpy(sam_freq, &rate, 3);
  897. /* Todo: Set Sample bits and other parameters */
  898. return;
  899. }
  900. /* audio function driver setup/binding */
  901. static int
  902. f_audio_bind(struct usb_configuration *c, struct usb_function *f)
  903. {
  904. struct usb_composite_dev *cdev = c->cdev;
  905. struct f_audio *audio = func_to_audio(f);
  906. int status;
  907. struct usb_ep *ep = NULL;
  908. u8 epaddr;
  909. f_audio_build_desc(audio);
  910. /* allocate instance-specific interface IDs, and patch descriptors */
  911. status = usb_interface_id(c, f);
  912. if (status < 0) {
  913. pr_err("%s: failed to allocate desc interface", __func__);
  914. goto fail;
  915. }
  916. uac1_ac_interface_desc.bInterfaceNumber = status;
  917. status = -ENOMEM;
  918. status = usb_interface_id(c, f);
  919. if (status < 0) {
  920. pr_err("%s: failed to allocate alt interface", __func__);
  921. goto fail;
  922. }
  923. microphone_as_interface_alt_0_desc.bInterfaceNumber = status;
  924. microphone_as_interface_alt_1_desc.bInterfaceNumber = status;
  925. uac1_ac_header_desc.baInterfaceNr[0] = status;
  926. audio->alt_intf[0] = 0;
  927. status = -ENODEV;
  928. status = usb_interface_id(c, f);
  929. if (status < 0) {
  930. pr_err("%s: failed to allocate alt interface", __func__);
  931. goto fail;
  932. }
  933. speaker_as_interface_alt_0_desc.bInterfaceNumber = status;
  934. speaker_as_interface_alt_1_desc.bInterfaceNumber = status;
  935. uac1_ac_header_desc.baInterfaceNr[1] = status;
  936. audio->alt_intf[1] = 0;
  937. status = -ENODEV;
  938. /* allocate instance-specific endpoints */
  939. ep = usb_ep_autoconfig(cdev->gadget, &microphone_as_ep_in_desc);
  940. if (!ep) {
  941. pr_err("%s: failed to autoconfig in endpoint", __func__);
  942. goto fail;
  943. }
  944. audio->in_ep = ep;
  945. ep->desc = &microphone_as_ep_in_desc;
  946. ep->driver_data = cdev;
  947. status = -ENOMEM;
  948. ep = usb_ep_autoconfig(cdev->gadget, &speaker_as_ep_out_desc);
  949. if (!ep) {
  950. pr_err("%s: failed to autoconfig out endpoint", __func__);
  951. goto fail;
  952. }
  953. audio->out_ep = ep;
  954. ep->desc = &speaker_as_ep_out_desc;
  955. ep->driver_data = cdev;
  956. /* associate bEndpointAddress with usb_function */
  957. epaddr = microphone_as_ep_in_desc.bEndpointAddress & ~USB_DIR_IN;
  958. microphone_as_iso_in.id = epaddr;
  959. epaddr = speaker_as_ep_out_desc.bEndpointAddress & ~USB_DIR_IN;
  960. speaker_as_iso_out.id = epaddr;
  961. /* copy descriptors, and track endpoint copies */
  962. status = usb_assign_descriptors(f, f_audio_desc, f_audio_desc, NULL);
  963. if (status)
  964. goto fail;
  965. return 0;
  966. fail:
  967. if (ep)
  968. ep->driver_data = NULL;
  969. return status;
  970. }
  971. static void
  972. f_audio_unbind(struct usb_configuration *c, struct usb_function *f)
  973. {
  974. struct f_audio *audio = func_to_audio(f);
  975. usb_free_all_descriptors(f);
  976. kfree(audio);
  977. }
  978. /*-------------------------------------------------------------------------*/
  979. static int generic_set_cmd(struct usb_audio_control *con, u8 cmd, int value)
  980. {
  981. con->data[cmd] = value;
  982. return 0;
  983. }
  984. static int generic_get_cmd(struct usb_audio_control *con, u8 cmd)
  985. {
  986. return con->data[cmd];
  987. }
  988. /* Todo: add more control selecotor dynamically */
  989. int control_selector_init(struct f_audio *audio)
  990. {
  991. INIT_LIST_HEAD(&audio->fu_cs);
  992. list_add(&microphone_fu_controls.list, &audio->fu_cs);
  993. list_add(&speaker_fu_controls.list, &audio->fu_cs);
  994. INIT_LIST_HEAD(&microphone_fu_controls.control);
  995. list_add(&microphone_mute_control.list,
  996. &microphone_fu_controls.control);
  997. list_add(&microphone_volume_control.list,
  998. &microphone_fu_controls.control);
  999. INIT_LIST_HEAD(&speaker_fu_controls.control);
  1000. list_add(&speaker_mute_control.list,
  1001. &speaker_fu_controls.control);
  1002. list_add(&speaker_volume_control.list,
  1003. &speaker_fu_controls.control);
  1004. microphone_volume_control.data[UAC__CUR] = 0xffc0;
  1005. microphone_volume_control.data[UAC__MIN] = 0xe3a0;
  1006. microphone_volume_control.data[UAC__MAX] = 0xfff0;
  1007. microphone_volume_control.data[UAC__RES] = 0x0030;
  1008. speaker_volume_control.data[UAC__CUR] = 0xffc0;
  1009. speaker_volume_control.data[UAC__MIN] = 0xe3a0;
  1010. speaker_volume_control.data[UAC__MAX] = 0xfff0;
  1011. speaker_volume_control.data[UAC__RES] = 0x0030;
  1012. INIT_LIST_HEAD(&audio->ep_cs);
  1013. list_add(&speaker_as_iso_out.list, &audio->ep_cs);
  1014. list_add(&microphone_as_iso_in.list, &audio->ep_cs);
  1015. INIT_LIST_HEAD(&microphone_as_iso_in.control);
  1016. list_add(&microphone_sample_freq_control.list,
  1017. &microphone_as_iso_in.control);
  1018. INIT_LIST_HEAD(&speaker_as_iso_out.control);
  1019. list_add(&speaker_sample_freq_control.list,
  1020. &speaker_as_iso_out.control);
  1021. return 0;
  1022. }
  1023. /**
  1024. * audio_bind_config - add USB audio function to a configuration
  1025. * @c: the configuration to support the USB audio function
  1026. * Context: single threaded during gadget setup
  1027. *
  1028. * Returns zero on success, else negative errno.
  1029. */
  1030. int audio_bind_config(struct usb_configuration *c)
  1031. {
  1032. struct f_audio *audio;
  1033. int status;
  1034. /* allocate and initialize one new instance */
  1035. audio = kzalloc(sizeof *audio, GFP_KERNEL);
  1036. if (!audio)
  1037. return -ENOMEM;
  1038. audio->card.gadget = c->cdev->gadget;
  1039. INIT_LIST_HEAD(&audio->play_queue);
  1040. spin_lock_init(&audio->playback_lock);
  1041. INIT_LIST_HEAD(&audio->capture_queue);
  1042. spin_lock_init(&audio->capture_lock);
  1043. audio->card.func.name = "audio";
  1044. audio->card.func.strings = audio_strings;
  1045. audio->card.func.bind = f_audio_bind;
  1046. audio->card.func.unbind = f_audio_unbind;
  1047. audio->card.func.get_alt = f_audio_get_alt;
  1048. audio->card.func.set_alt = f_audio_set_alt;
  1049. audio->card.func.setup = f_audio_setup;
  1050. audio->card.func.disable = f_audio_disable;
  1051. control_selector_init(audio);
  1052. INIT_WORK(&audio->playback_work, f_audio_playback_work);
  1053. INIT_WORK(&audio->capture_work, f_audio_capture_work);
  1054. /* set up ASLA audio devices */
  1055. status = gaudio_setup(&audio->card);
  1056. if (status < 0)
  1057. goto add_fail;
  1058. status = usb_add_function(c, &audio->card.func);
  1059. if (status) {
  1060. pr_err("%s: Failed to add usb audio function, err = %d",
  1061. __func__, status);
  1062. goto setup_fail;
  1063. }
  1064. return status;
  1065. add_fail:
  1066. gaudio_cleanup();
  1067. setup_fail:
  1068. kfree(audio);
  1069. return status;
  1070. }