f_uac1_legacy.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * f_audio.c -- USB Audio class function driver
  4. *
  5. * Copyright (C) 2008 Bryan Wu <cooloney@kernel.org>
  6. * Copyright (C) 2008 Analog Devices, Inc
  7. */
  8. #include <linux/slab.h>
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/device.h>
  12. #include <linux/atomic.h>
  13. #include "u_uac1_legacy.h"
  14. static int generic_set_cmd(struct usb_audio_control *con, u8 cmd, int value);
  15. static int generic_get_cmd(struct usb_audio_control *con, u8 cmd);
  16. /*
  17. * DESCRIPTORS ... most are static, but strings and full
  18. * configuration descriptors are built on demand.
  19. */
  20. /*
  21. * We have two interfaces- AudioControl and AudioStreaming
  22. * TODO: only supcard playback currently
  23. */
  24. #define F_AUDIO_AC_INTERFACE 0
  25. #define F_AUDIO_AS_INTERFACE 1
  26. #define F_AUDIO_NUM_INTERFACES 1
  27. /* B.3.1 Standard AC Interface Descriptor */
  28. static struct usb_interface_descriptor ac_interface_desc = {
  29. .bLength = USB_DT_INTERFACE_SIZE,
  30. .bDescriptorType = USB_DT_INTERFACE,
  31. .bNumEndpoints = 0,
  32. .bInterfaceClass = USB_CLASS_AUDIO,
  33. .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL,
  34. };
  35. /*
  36. * The number of AudioStreaming and MIDIStreaming interfaces
  37. * in the Audio Interface Collection
  38. */
  39. DECLARE_UAC_AC_HEADER_DESCRIPTOR(1);
  40. #define UAC_DT_AC_HEADER_LENGTH UAC_DT_AC_HEADER_SIZE(F_AUDIO_NUM_INTERFACES)
  41. /* 1 input terminal, 1 output terminal and 1 feature unit */
  42. #define UAC_DT_TOTAL_LENGTH (UAC_DT_AC_HEADER_LENGTH + UAC_DT_INPUT_TERMINAL_SIZE \
  43. + UAC_DT_OUTPUT_TERMINAL_SIZE + UAC_DT_FEATURE_UNIT_SIZE(0))
  44. /* B.3.2 Class-Specific AC Interface Descriptor */
  45. static struct uac1_ac_header_descriptor_1 ac_header_desc = {
  46. .bLength = UAC_DT_AC_HEADER_LENGTH,
  47. .bDescriptorType = USB_DT_CS_INTERFACE,
  48. .bDescriptorSubtype = UAC_HEADER,
  49. .bcdADC = __constant_cpu_to_le16(0x0100),
  50. .wTotalLength = __constant_cpu_to_le16(UAC_DT_TOTAL_LENGTH),
  51. .bInCollection = F_AUDIO_NUM_INTERFACES,
  52. .baInterfaceNr = {
  53. /* Interface number of the first AudioStream interface */
  54. [0] = 1,
  55. }
  56. };
  57. #define INPUT_TERMINAL_ID 1
  58. static struct uac_input_terminal_descriptor input_terminal_desc = {
  59. .bLength = UAC_DT_INPUT_TERMINAL_SIZE,
  60. .bDescriptorType = USB_DT_CS_INTERFACE,
  61. .bDescriptorSubtype = UAC_INPUT_TERMINAL,
  62. .bTerminalID = INPUT_TERMINAL_ID,
  63. .wTerminalType = UAC_TERMINAL_STREAMING,
  64. .bAssocTerminal = 0,
  65. .wChannelConfig = 0x3,
  66. };
  67. DECLARE_UAC_FEATURE_UNIT_DESCRIPTOR(0);
  68. #define FEATURE_UNIT_ID 2
  69. static struct uac_feature_unit_descriptor_0 feature_unit_desc = {
  70. .bLength = UAC_DT_FEATURE_UNIT_SIZE(0),
  71. .bDescriptorType = USB_DT_CS_INTERFACE,
  72. .bDescriptorSubtype = UAC_FEATURE_UNIT,
  73. .bUnitID = FEATURE_UNIT_ID,
  74. .bSourceID = INPUT_TERMINAL_ID,
  75. .bControlSize = 2,
  76. .bmaControls[0] = (UAC_FU_MUTE | UAC_FU_VOLUME),
  77. };
  78. static struct usb_audio_control mute_control = {
  79. .list = LIST_HEAD_INIT(mute_control.list),
  80. .name = "Mute Control",
  81. .type = UAC_FU_MUTE,
  82. /* Todo: add real Mute control code */
  83. .set = generic_set_cmd,
  84. .get = generic_get_cmd,
  85. };
  86. static struct usb_audio_control volume_control = {
  87. .list = LIST_HEAD_INIT(volume_control.list),
  88. .name = "Volume Control",
  89. .type = UAC_FU_VOLUME,
  90. /* Todo: add real Volume control code */
  91. .set = generic_set_cmd,
  92. .get = generic_get_cmd,
  93. };
  94. static struct usb_audio_control_selector feature_unit = {
  95. .list = LIST_HEAD_INIT(feature_unit.list),
  96. .id = FEATURE_UNIT_ID,
  97. .name = "Mute & Volume Control",
  98. .type = UAC_FEATURE_UNIT,
  99. .desc = (struct usb_descriptor_header *)&feature_unit_desc,
  100. };
  101. #define OUTPUT_TERMINAL_ID 3
  102. static struct uac1_output_terminal_descriptor output_terminal_desc = {
  103. .bLength = UAC_DT_OUTPUT_TERMINAL_SIZE,
  104. .bDescriptorType = USB_DT_CS_INTERFACE,
  105. .bDescriptorSubtype = UAC_OUTPUT_TERMINAL,
  106. .bTerminalID = OUTPUT_TERMINAL_ID,
  107. .wTerminalType = UAC_OUTPUT_TERMINAL_SPEAKER,
  108. .bAssocTerminal = FEATURE_UNIT_ID,
  109. .bSourceID = FEATURE_UNIT_ID,
  110. };
  111. /* B.4.1 Standard AS Interface Descriptor */
  112. static struct usb_interface_descriptor as_interface_alt_0_desc = {
  113. .bLength = USB_DT_INTERFACE_SIZE,
  114. .bDescriptorType = USB_DT_INTERFACE,
  115. .bAlternateSetting = 0,
  116. .bNumEndpoints = 0,
  117. .bInterfaceClass = USB_CLASS_AUDIO,
  118. .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
  119. };
  120. static struct usb_interface_descriptor as_interface_alt_1_desc = {
  121. .bLength = USB_DT_INTERFACE_SIZE,
  122. .bDescriptorType = USB_DT_INTERFACE,
  123. .bAlternateSetting = 1,
  124. .bNumEndpoints = 1,
  125. .bInterfaceClass = USB_CLASS_AUDIO,
  126. .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
  127. };
  128. /* B.4.2 Class-Specific AS Interface Descriptor */
  129. static struct uac1_as_header_descriptor as_header_desc = {
  130. .bLength = UAC_DT_AS_HEADER_SIZE,
  131. .bDescriptorType = USB_DT_CS_INTERFACE,
  132. .bDescriptorSubtype = UAC_AS_GENERAL,
  133. .bTerminalLink = INPUT_TERMINAL_ID,
  134. .bDelay = 1,
  135. .wFormatTag = UAC_FORMAT_TYPE_I_PCM,
  136. };
  137. DECLARE_UAC_FORMAT_TYPE_I_DISCRETE_DESC(1);
  138. static struct uac_format_type_i_discrete_descriptor_1 as_type_i_desc = {
  139. .bLength = UAC_FORMAT_TYPE_I_DISCRETE_DESC_SIZE(1),
  140. .bDescriptorType = USB_DT_CS_INTERFACE,
  141. .bDescriptorSubtype = UAC_FORMAT_TYPE,
  142. .bFormatType = UAC_FORMAT_TYPE_I,
  143. .bSubframeSize = 2,
  144. .bBitResolution = 16,
  145. .bSamFreqType = 1,
  146. };
  147. /* Standard ISO OUT Endpoint Descriptor */
  148. static struct usb_endpoint_descriptor as_out_ep_desc = {
  149. .bLength = USB_DT_ENDPOINT_AUDIO_SIZE,
  150. .bDescriptorType = USB_DT_ENDPOINT,
  151. .bEndpointAddress = USB_DIR_OUT,
  152. .bmAttributes = USB_ENDPOINT_SYNC_ADAPTIVE
  153. | USB_ENDPOINT_XFER_ISOC,
  154. .wMaxPacketSize = cpu_to_le16(UAC1_OUT_EP_MAX_PACKET_SIZE),
  155. .bInterval = 4,
  156. };
  157. /* Class-specific AS ISO OUT Endpoint Descriptor */
  158. static struct uac_iso_endpoint_descriptor as_iso_out_desc = {
  159. .bLength = UAC_ISO_ENDPOINT_DESC_SIZE,
  160. .bDescriptorType = USB_DT_CS_ENDPOINT,
  161. .bDescriptorSubtype = UAC_EP_GENERAL,
  162. .bmAttributes = 1,
  163. .bLockDelayUnits = 1,
  164. .wLockDelay = __constant_cpu_to_le16(1),
  165. };
  166. static struct usb_descriptor_header *f_audio_desc[] = {
  167. (struct usb_descriptor_header *)&ac_interface_desc,
  168. (struct usb_descriptor_header *)&ac_header_desc,
  169. (struct usb_descriptor_header *)&input_terminal_desc,
  170. (struct usb_descriptor_header *)&output_terminal_desc,
  171. (struct usb_descriptor_header *)&feature_unit_desc,
  172. (struct usb_descriptor_header *)&as_interface_alt_0_desc,
  173. (struct usb_descriptor_header *)&as_interface_alt_1_desc,
  174. (struct usb_descriptor_header *)&as_header_desc,
  175. (struct usb_descriptor_header *)&as_type_i_desc,
  176. (struct usb_descriptor_header *)&as_out_ep_desc,
  177. (struct usb_descriptor_header *)&as_iso_out_desc,
  178. NULL,
  179. };
  180. enum {
  181. STR_AC_IF,
  182. STR_INPUT_TERMINAL,
  183. STR_INPUT_TERMINAL_CH_NAMES,
  184. STR_FEAT_DESC_0,
  185. STR_OUTPUT_TERMINAL,
  186. STR_AS_IF_ALT0,
  187. STR_AS_IF_ALT1,
  188. };
  189. static struct usb_string strings_uac1[] = {
  190. [STR_AC_IF].s = "AC Interface",
  191. [STR_INPUT_TERMINAL].s = "Input terminal",
  192. [STR_INPUT_TERMINAL_CH_NAMES].s = "Channels",
  193. [STR_FEAT_DESC_0].s = "Volume control & mute",
  194. [STR_OUTPUT_TERMINAL].s = "Output terminal",
  195. [STR_AS_IF_ALT0].s = "AS Interface",
  196. [STR_AS_IF_ALT1].s = "AS Interface",
  197. { },
  198. };
  199. static struct usb_gadget_strings str_uac1 = {
  200. .language = 0x0409, /* en-us */
  201. .strings = strings_uac1,
  202. };
  203. static struct usb_gadget_strings *uac1_strings[] = {
  204. &str_uac1,
  205. NULL,
  206. };
  207. /*
  208. * This function is an ALSA sound card following USB Audio Class Spec 1.0.
  209. */
  210. /*-------------------------------------------------------------------------*/
  211. struct f_audio_buf {
  212. u8 *buf;
  213. int actual;
  214. struct list_head list;
  215. };
  216. static struct f_audio_buf *f_audio_buffer_alloc(int buf_size)
  217. {
  218. struct f_audio_buf *copy_buf;
  219. copy_buf = kzalloc(sizeof *copy_buf, GFP_ATOMIC);
  220. if (!copy_buf)
  221. return ERR_PTR(-ENOMEM);
  222. copy_buf->buf = kzalloc(buf_size, GFP_ATOMIC);
  223. if (!copy_buf->buf) {
  224. kfree(copy_buf);
  225. return ERR_PTR(-ENOMEM);
  226. }
  227. return copy_buf;
  228. }
  229. static void f_audio_buffer_free(struct f_audio_buf *audio_buf)
  230. {
  231. kfree(audio_buf->buf);
  232. kfree(audio_buf);
  233. }
  234. /*-------------------------------------------------------------------------*/
  235. struct f_audio {
  236. struct gaudio card;
  237. u8 ac_intf, ac_alt;
  238. u8 as_intf, as_alt;
  239. /* endpoints handle full and/or high speeds */
  240. struct usb_ep *out_ep;
  241. spinlock_t lock;
  242. struct f_audio_buf *copy_buf;
  243. struct work_struct playback_work;
  244. struct list_head play_queue;
  245. /* Control Set command */
  246. struct list_head cs;
  247. u8 set_cmd;
  248. struct usb_audio_control *set_con;
  249. };
  250. static inline struct f_audio *func_to_audio(struct usb_function *f)
  251. {
  252. return container_of(f, struct f_audio, card.func);
  253. }
  254. /*-------------------------------------------------------------------------*/
  255. static void f_audio_playback_work(struct work_struct *data)
  256. {
  257. struct f_audio *audio = container_of(data, struct f_audio,
  258. playback_work);
  259. struct f_audio_buf *play_buf;
  260. spin_lock_irq(&audio->lock);
  261. if (list_empty(&audio->play_queue)) {
  262. spin_unlock_irq(&audio->lock);
  263. return;
  264. }
  265. play_buf = list_first_entry(&audio->play_queue,
  266. struct f_audio_buf, list);
  267. list_del(&play_buf->list);
  268. spin_unlock_irq(&audio->lock);
  269. u_audio_playback(&audio->card, play_buf->buf, play_buf->actual);
  270. f_audio_buffer_free(play_buf);
  271. }
  272. static int f_audio_out_ep_complete(struct usb_ep *ep, struct usb_request *req)
  273. {
  274. struct f_audio *audio = req->context;
  275. struct usb_composite_dev *cdev = audio->card.func.config->cdev;
  276. struct f_audio_buf *copy_buf = audio->copy_buf;
  277. struct f_uac1_legacy_opts *opts;
  278. int audio_buf_size;
  279. int err;
  280. opts = container_of(audio->card.func.fi, struct f_uac1_legacy_opts,
  281. func_inst);
  282. audio_buf_size = opts->audio_buf_size;
  283. if (!copy_buf)
  284. return -EINVAL;
  285. /* Copy buffer is full, add it to the play_queue */
  286. if (audio_buf_size - copy_buf->actual < req->actual) {
  287. list_add_tail(&copy_buf->list, &audio->play_queue);
  288. schedule_work(&audio->playback_work);
  289. copy_buf = f_audio_buffer_alloc(audio_buf_size);
  290. if (IS_ERR(copy_buf))
  291. return -ENOMEM;
  292. }
  293. memcpy(copy_buf->buf + copy_buf->actual, req->buf, req->actual);
  294. copy_buf->actual += req->actual;
  295. audio->copy_buf = copy_buf;
  296. err = usb_ep_queue(ep, req, GFP_ATOMIC);
  297. if (err)
  298. ERROR(cdev, "%s queue req: %d\n", ep->name, err);
  299. return 0;
  300. }
  301. static void f_audio_complete(struct usb_ep *ep, struct usb_request *req)
  302. {
  303. struct f_audio *audio = req->context;
  304. int status = req->status;
  305. u32 data = 0;
  306. struct usb_ep *out_ep = audio->out_ep;
  307. switch (status) {
  308. case 0: /* normal completion? */
  309. if (ep == out_ep)
  310. f_audio_out_ep_complete(ep, req);
  311. else if (audio->set_con) {
  312. memcpy(&data, req->buf, req->length);
  313. audio->set_con->set(audio->set_con, audio->set_cmd,
  314. le16_to_cpu(data));
  315. audio->set_con = NULL;
  316. }
  317. break;
  318. default:
  319. break;
  320. }
  321. }
  322. static int audio_set_intf_req(struct usb_function *f,
  323. const struct usb_ctrlrequest *ctrl)
  324. {
  325. struct f_audio *audio = func_to_audio(f);
  326. struct usb_composite_dev *cdev = f->config->cdev;
  327. struct usb_request *req = cdev->req;
  328. u8 id = ((le16_to_cpu(ctrl->wIndex) >> 8) & 0xFF);
  329. u16 len = le16_to_cpu(ctrl->wLength);
  330. u16 w_value = le16_to_cpu(ctrl->wValue);
  331. u8 con_sel = (w_value >> 8) & 0xFF;
  332. u8 cmd = (ctrl->bRequest & 0x0F);
  333. struct usb_audio_control_selector *cs;
  334. struct usb_audio_control *con;
  335. DBG(cdev, "bRequest 0x%x, w_value 0x%04x, len %d, entity %d\n",
  336. ctrl->bRequest, w_value, len, id);
  337. list_for_each_entry(cs, &audio->cs, list) {
  338. if (cs->id == id) {
  339. list_for_each_entry(con, &cs->control, list) {
  340. if (con->type == con_sel) {
  341. audio->set_con = con;
  342. break;
  343. }
  344. }
  345. break;
  346. }
  347. }
  348. audio->set_cmd = cmd;
  349. req->context = audio;
  350. req->complete = f_audio_complete;
  351. return len;
  352. }
  353. static int audio_get_intf_req(struct usb_function *f,
  354. const struct usb_ctrlrequest *ctrl)
  355. {
  356. struct f_audio *audio = func_to_audio(f);
  357. struct usb_composite_dev *cdev = f->config->cdev;
  358. struct usb_request *req = cdev->req;
  359. int value = -EOPNOTSUPP;
  360. u8 id = ((le16_to_cpu(ctrl->wIndex) >> 8) & 0xFF);
  361. u16 len = le16_to_cpu(ctrl->wLength);
  362. u16 w_value = le16_to_cpu(ctrl->wValue);
  363. u8 con_sel = (w_value >> 8) & 0xFF;
  364. u8 cmd = (ctrl->bRequest & 0x0F);
  365. struct usb_audio_control_selector *cs;
  366. struct usb_audio_control *con;
  367. DBG(cdev, "bRequest 0x%x, w_value 0x%04x, len %d, entity %d\n",
  368. ctrl->bRequest, w_value, len, id);
  369. list_for_each_entry(cs, &audio->cs, list) {
  370. if (cs->id == id) {
  371. list_for_each_entry(con, &cs->control, list) {
  372. if (con->type == con_sel && con->get) {
  373. value = con->get(con, cmd);
  374. break;
  375. }
  376. }
  377. break;
  378. }
  379. }
  380. req->context = audio;
  381. req->complete = f_audio_complete;
  382. len = min_t(size_t, sizeof(value), len);
  383. memcpy(req->buf, &value, len);
  384. return len;
  385. }
  386. static int audio_set_endpoint_req(struct usb_function *f,
  387. const struct usb_ctrlrequest *ctrl)
  388. {
  389. struct usb_composite_dev *cdev = f->config->cdev;
  390. int value = -EOPNOTSUPP;
  391. u16 ep = le16_to_cpu(ctrl->wIndex);
  392. u16 len = le16_to_cpu(ctrl->wLength);
  393. u16 w_value = le16_to_cpu(ctrl->wValue);
  394. DBG(cdev, "bRequest 0x%x, w_value 0x%04x, len %d, endpoint %d\n",
  395. ctrl->bRequest, w_value, len, ep);
  396. switch (ctrl->bRequest) {
  397. case UAC_SET_CUR:
  398. value = len;
  399. break;
  400. case UAC_SET_MIN:
  401. break;
  402. case UAC_SET_MAX:
  403. break;
  404. case UAC_SET_RES:
  405. break;
  406. case UAC_SET_MEM:
  407. break;
  408. default:
  409. break;
  410. }
  411. return value;
  412. }
  413. static int audio_get_endpoint_req(struct usb_function *f,
  414. const struct usb_ctrlrequest *ctrl)
  415. {
  416. struct usb_composite_dev *cdev = f->config->cdev;
  417. int value = -EOPNOTSUPP;
  418. u8 ep = ((le16_to_cpu(ctrl->wIndex) >> 8) & 0xFF);
  419. u16 len = le16_to_cpu(ctrl->wLength);
  420. u16 w_value = le16_to_cpu(ctrl->wValue);
  421. DBG(cdev, "bRequest 0x%x, w_value 0x%04x, len %d, endpoint %d\n",
  422. ctrl->bRequest, w_value, len, ep);
  423. switch (ctrl->bRequest) {
  424. case UAC_GET_CUR:
  425. case UAC_GET_MIN:
  426. case UAC_GET_MAX:
  427. case UAC_GET_RES:
  428. value = len;
  429. break;
  430. case UAC_GET_MEM:
  431. break;
  432. default:
  433. break;
  434. }
  435. return value;
  436. }
  437. static int
  438. f_audio_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
  439. {
  440. struct usb_composite_dev *cdev = f->config->cdev;
  441. struct usb_request *req = cdev->req;
  442. int value = -EOPNOTSUPP;
  443. u16 w_index = le16_to_cpu(ctrl->wIndex);
  444. u16 w_value = le16_to_cpu(ctrl->wValue);
  445. u16 w_length = le16_to_cpu(ctrl->wLength);
  446. /* composite driver infrastructure handles everything; interface
  447. * activation uses set_alt().
  448. */
  449. switch (ctrl->bRequestType) {
  450. case USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE:
  451. value = audio_set_intf_req(f, ctrl);
  452. break;
  453. case USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE:
  454. value = audio_get_intf_req(f, ctrl);
  455. break;
  456. case USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_ENDPOINT:
  457. value = audio_set_endpoint_req(f, ctrl);
  458. break;
  459. case USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT:
  460. value = audio_get_endpoint_req(f, ctrl);
  461. break;
  462. default:
  463. ERROR(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
  464. ctrl->bRequestType, ctrl->bRequest,
  465. w_value, w_index, w_length);
  466. }
  467. /* respond with data transfer or status phase? */
  468. if (value >= 0) {
  469. DBG(cdev, "audio req%02x.%02x v%04x i%04x l%d\n",
  470. ctrl->bRequestType, ctrl->bRequest,
  471. w_value, w_index, w_length);
  472. req->zero = 0;
  473. req->length = value;
  474. value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
  475. if (value < 0)
  476. ERROR(cdev, "audio response on err %d\n", value);
  477. }
  478. /* device either stalls (value < 0) or reports success */
  479. return value;
  480. }
  481. static int f_audio_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
  482. {
  483. struct f_audio *audio = func_to_audio(f);
  484. struct usb_composite_dev *cdev = f->config->cdev;
  485. struct usb_ep *out_ep = audio->out_ep;
  486. struct usb_request *req;
  487. struct f_uac1_legacy_opts *opts;
  488. int req_buf_size, req_count, audio_buf_size;
  489. int i = 0, err = 0;
  490. DBG(cdev, "intf %d, alt %d\n", intf, alt);
  491. opts = container_of(f->fi, struct f_uac1_legacy_opts, func_inst);
  492. req_buf_size = opts->req_buf_size;
  493. req_count = opts->req_count;
  494. audio_buf_size = opts->audio_buf_size;
  495. /* No i/f has more than 2 alt settings */
  496. if (alt > 1) {
  497. ERROR(cdev, "%s:%d Error!\n", __func__, __LINE__);
  498. return -EINVAL;
  499. }
  500. if (intf == audio->ac_intf) {
  501. /* Control I/f has only 1 AltSetting - 0 */
  502. if (alt) {
  503. ERROR(cdev, "%s:%d Error!\n", __func__, __LINE__);
  504. return -EINVAL;
  505. }
  506. return 0;
  507. } else if (intf == audio->as_intf) {
  508. if (alt == 1) {
  509. err = config_ep_by_speed(cdev->gadget, f, out_ep);
  510. if (err)
  511. return err;
  512. usb_ep_enable(out_ep);
  513. audio->copy_buf = f_audio_buffer_alloc(audio_buf_size);
  514. if (IS_ERR(audio->copy_buf))
  515. return -ENOMEM;
  516. /*
  517. * allocate a bunch of read buffers
  518. * and queue them all at once.
  519. */
  520. for (i = 0; i < req_count && err == 0; i++) {
  521. req = usb_ep_alloc_request(out_ep, GFP_ATOMIC);
  522. if (req) {
  523. req->buf = kzalloc(req_buf_size,
  524. GFP_ATOMIC);
  525. if (req->buf) {
  526. req->length = req_buf_size;
  527. req->context = audio;
  528. req->complete =
  529. f_audio_complete;
  530. err = usb_ep_queue(out_ep,
  531. req, GFP_ATOMIC);
  532. if (err)
  533. ERROR(cdev,
  534. "%s queue req: %d\n",
  535. out_ep->name, err);
  536. } else
  537. err = -ENOMEM;
  538. } else
  539. err = -ENOMEM;
  540. }
  541. } else {
  542. struct f_audio_buf *copy_buf = audio->copy_buf;
  543. if (copy_buf) {
  544. list_add_tail(&copy_buf->list,
  545. &audio->play_queue);
  546. schedule_work(&audio->playback_work);
  547. }
  548. }
  549. audio->as_alt = alt;
  550. }
  551. return err;
  552. }
  553. static int f_audio_get_alt(struct usb_function *f, unsigned intf)
  554. {
  555. struct f_audio *audio = func_to_audio(f);
  556. struct usb_composite_dev *cdev = f->config->cdev;
  557. if (intf == audio->ac_intf)
  558. return audio->ac_alt;
  559. else if (intf == audio->as_intf)
  560. return audio->as_alt;
  561. else
  562. ERROR(cdev, "%s:%d Invalid Interface %d!\n",
  563. __func__, __LINE__, intf);
  564. return -EINVAL;
  565. }
  566. static void f_audio_disable(struct usb_function *f)
  567. {
  568. return;
  569. }
  570. /*-------------------------------------------------------------------------*/
  571. static void f_audio_build_desc(struct f_audio *audio)
  572. {
  573. struct gaudio *card = &audio->card;
  574. u8 *sam_freq;
  575. int rate;
  576. /* Set channel numbers */
  577. input_terminal_desc.bNrChannels = u_audio_get_playback_channels(card);
  578. as_type_i_desc.bNrChannels = u_audio_get_playback_channels(card);
  579. /* Set sample rates */
  580. rate = u_audio_get_playback_rate(card);
  581. sam_freq = as_type_i_desc.tSamFreq[0];
  582. memcpy(sam_freq, &rate, 3);
  583. /* Todo: Set Sample bits and other parameters */
  584. return;
  585. }
  586. /* audio function driver setup/binding */
  587. static int
  588. f_audio_bind(struct usb_configuration *c, struct usb_function *f)
  589. {
  590. struct usb_composite_dev *cdev = c->cdev;
  591. struct f_audio *audio = func_to_audio(f);
  592. struct usb_string *us;
  593. int status;
  594. struct usb_ep *ep = NULL;
  595. struct f_uac1_legacy_opts *audio_opts;
  596. audio_opts = container_of(f->fi, struct f_uac1_legacy_opts, func_inst);
  597. audio->card.gadget = c->cdev->gadget;
  598. /* set up ASLA audio devices */
  599. if (!audio_opts->bound) {
  600. status = gaudio_setup(&audio->card);
  601. if (status < 0)
  602. return status;
  603. audio_opts->bound = true;
  604. }
  605. us = usb_gstrings_attach(cdev, uac1_strings, ARRAY_SIZE(strings_uac1));
  606. if (IS_ERR(us))
  607. return PTR_ERR(us);
  608. ac_interface_desc.iInterface = us[STR_AC_IF].id;
  609. input_terminal_desc.iTerminal = us[STR_INPUT_TERMINAL].id;
  610. input_terminal_desc.iChannelNames = us[STR_INPUT_TERMINAL_CH_NAMES].id;
  611. feature_unit_desc.iFeature = us[STR_FEAT_DESC_0].id;
  612. output_terminal_desc.iTerminal = us[STR_OUTPUT_TERMINAL].id;
  613. as_interface_alt_0_desc.iInterface = us[STR_AS_IF_ALT0].id;
  614. as_interface_alt_1_desc.iInterface = us[STR_AS_IF_ALT1].id;
  615. f_audio_build_desc(audio);
  616. /* allocate instance-specific interface IDs, and patch descriptors */
  617. status = usb_interface_id(c, f);
  618. if (status < 0)
  619. goto fail;
  620. ac_interface_desc.bInterfaceNumber = status;
  621. audio->ac_intf = status;
  622. audio->ac_alt = 0;
  623. status = usb_interface_id(c, f);
  624. if (status < 0)
  625. goto fail;
  626. as_interface_alt_0_desc.bInterfaceNumber = status;
  627. as_interface_alt_1_desc.bInterfaceNumber = status;
  628. audio->as_intf = status;
  629. audio->as_alt = 0;
  630. status = -ENODEV;
  631. /* allocate instance-specific endpoints */
  632. ep = usb_ep_autoconfig(cdev->gadget, &as_out_ep_desc);
  633. if (!ep)
  634. goto fail;
  635. audio->out_ep = ep;
  636. audio->out_ep->desc = &as_out_ep_desc;
  637. status = -ENOMEM;
  638. /* copy descriptors, and track endpoint copies */
  639. status = usb_assign_descriptors(f, f_audio_desc, f_audio_desc, NULL,
  640. NULL);
  641. if (status)
  642. goto fail;
  643. return 0;
  644. fail:
  645. gaudio_cleanup(&audio->card);
  646. return status;
  647. }
  648. /*-------------------------------------------------------------------------*/
  649. static int generic_set_cmd(struct usb_audio_control *con, u8 cmd, int value)
  650. {
  651. con->data[cmd] = value;
  652. return 0;
  653. }
  654. static int generic_get_cmd(struct usb_audio_control *con, u8 cmd)
  655. {
  656. return con->data[cmd];
  657. }
  658. /* Todo: add more control selecotor dynamically */
  659. static int control_selector_init(struct f_audio *audio)
  660. {
  661. INIT_LIST_HEAD(&audio->cs);
  662. list_add(&feature_unit.list, &audio->cs);
  663. INIT_LIST_HEAD(&feature_unit.control);
  664. list_add(&mute_control.list, &feature_unit.control);
  665. list_add(&volume_control.list, &feature_unit.control);
  666. volume_control.data[UAC__CUR] = 0xffc0;
  667. volume_control.data[UAC__MIN] = 0xe3a0;
  668. volume_control.data[UAC__MAX] = 0xfff0;
  669. volume_control.data[UAC__RES] = 0x0030;
  670. return 0;
  671. }
  672. static inline
  673. struct f_uac1_legacy_opts *to_f_uac1_opts(struct config_item *item)
  674. {
  675. return container_of(to_config_group(item), struct f_uac1_legacy_opts,
  676. func_inst.group);
  677. }
  678. static void f_uac1_attr_release(struct config_item *item)
  679. {
  680. struct f_uac1_legacy_opts *opts = to_f_uac1_opts(item);
  681. usb_put_function_instance(&opts->func_inst);
  682. }
  683. static struct configfs_item_operations f_uac1_item_ops = {
  684. .release = f_uac1_attr_release,
  685. };
  686. #define UAC1_INT_ATTRIBUTE(name) \
  687. static ssize_t f_uac1_opts_##name##_show(struct config_item *item, \
  688. char *page) \
  689. { \
  690. struct f_uac1_legacy_opts *opts = to_f_uac1_opts(item); \
  691. int result; \
  692. \
  693. mutex_lock(&opts->lock); \
  694. result = sprintf(page, "%u\n", opts->name); \
  695. mutex_unlock(&opts->lock); \
  696. \
  697. return result; \
  698. } \
  699. \
  700. static ssize_t f_uac1_opts_##name##_store(struct config_item *item, \
  701. const char *page, size_t len) \
  702. { \
  703. struct f_uac1_legacy_opts *opts = to_f_uac1_opts(item); \
  704. int ret; \
  705. u32 num; \
  706. \
  707. mutex_lock(&opts->lock); \
  708. if (opts->refcnt) { \
  709. ret = -EBUSY; \
  710. goto end; \
  711. } \
  712. \
  713. ret = kstrtou32(page, 0, &num); \
  714. if (ret) \
  715. goto end; \
  716. \
  717. opts->name = num; \
  718. ret = len; \
  719. \
  720. end: \
  721. mutex_unlock(&opts->lock); \
  722. return ret; \
  723. } \
  724. \
  725. CONFIGFS_ATTR(f_uac1_opts_, name)
  726. UAC1_INT_ATTRIBUTE(req_buf_size);
  727. UAC1_INT_ATTRIBUTE(req_count);
  728. UAC1_INT_ATTRIBUTE(audio_buf_size);
  729. #define UAC1_STR_ATTRIBUTE(name) \
  730. static ssize_t f_uac1_opts_##name##_show(struct config_item *item, \
  731. char *page) \
  732. { \
  733. struct f_uac1_legacy_opts *opts = to_f_uac1_opts(item); \
  734. int result; \
  735. \
  736. mutex_lock(&opts->lock); \
  737. result = sprintf(page, "%s\n", opts->name); \
  738. mutex_unlock(&opts->lock); \
  739. \
  740. return result; \
  741. } \
  742. \
  743. static ssize_t f_uac1_opts_##name##_store(struct config_item *item, \
  744. const char *page, size_t len) \
  745. { \
  746. struct f_uac1_legacy_opts *opts = to_f_uac1_opts(item); \
  747. int ret = -EBUSY; \
  748. char *tmp; \
  749. \
  750. mutex_lock(&opts->lock); \
  751. if (opts->refcnt) \
  752. goto end; \
  753. \
  754. tmp = kstrndup(page, len, GFP_KERNEL); \
  755. if (tmp) { \
  756. ret = -ENOMEM; \
  757. goto end; \
  758. } \
  759. if (opts->name##_alloc) \
  760. kfree(opts->name); \
  761. opts->name##_alloc = true; \
  762. opts->name = tmp; \
  763. ret = len; \
  764. \
  765. end: \
  766. mutex_unlock(&opts->lock); \
  767. return ret; \
  768. } \
  769. \
  770. CONFIGFS_ATTR(f_uac1_opts_, name)
  771. UAC1_STR_ATTRIBUTE(fn_play);
  772. UAC1_STR_ATTRIBUTE(fn_cap);
  773. UAC1_STR_ATTRIBUTE(fn_cntl);
  774. static struct configfs_attribute *f_uac1_attrs[] = {
  775. &f_uac1_opts_attr_req_buf_size,
  776. &f_uac1_opts_attr_req_count,
  777. &f_uac1_opts_attr_audio_buf_size,
  778. &f_uac1_opts_attr_fn_play,
  779. &f_uac1_opts_attr_fn_cap,
  780. &f_uac1_opts_attr_fn_cntl,
  781. NULL,
  782. };
  783. static const struct config_item_type f_uac1_func_type = {
  784. .ct_item_ops = &f_uac1_item_ops,
  785. .ct_attrs = f_uac1_attrs,
  786. .ct_owner = THIS_MODULE,
  787. };
  788. static void f_audio_free_inst(struct usb_function_instance *f)
  789. {
  790. struct f_uac1_legacy_opts *opts;
  791. opts = container_of(f, struct f_uac1_legacy_opts, func_inst);
  792. if (opts->fn_play_alloc)
  793. kfree(opts->fn_play);
  794. if (opts->fn_cap_alloc)
  795. kfree(opts->fn_cap);
  796. if (opts->fn_cntl_alloc)
  797. kfree(opts->fn_cntl);
  798. kfree(opts);
  799. }
  800. static struct usb_function_instance *f_audio_alloc_inst(void)
  801. {
  802. struct f_uac1_legacy_opts *opts;
  803. opts = kzalloc(sizeof(*opts), GFP_KERNEL);
  804. if (!opts)
  805. return ERR_PTR(-ENOMEM);
  806. mutex_init(&opts->lock);
  807. opts->func_inst.free_func_inst = f_audio_free_inst;
  808. config_group_init_type_name(&opts->func_inst.group, "",
  809. &f_uac1_func_type);
  810. opts->req_buf_size = UAC1_OUT_EP_MAX_PACKET_SIZE;
  811. opts->req_count = UAC1_REQ_COUNT;
  812. opts->audio_buf_size = UAC1_AUDIO_BUF_SIZE;
  813. opts->fn_play = FILE_PCM_PLAYBACK;
  814. opts->fn_cap = FILE_PCM_CAPTURE;
  815. opts->fn_cntl = FILE_CONTROL;
  816. return &opts->func_inst;
  817. }
  818. static void f_audio_free(struct usb_function *f)
  819. {
  820. struct f_audio *audio = func_to_audio(f);
  821. struct f_uac1_legacy_opts *opts;
  822. gaudio_cleanup(&audio->card);
  823. opts = container_of(f->fi, struct f_uac1_legacy_opts, func_inst);
  824. kfree(audio);
  825. mutex_lock(&opts->lock);
  826. --opts->refcnt;
  827. mutex_unlock(&opts->lock);
  828. }
  829. static void f_audio_unbind(struct usb_configuration *c, struct usb_function *f)
  830. {
  831. usb_free_all_descriptors(f);
  832. }
  833. static struct usb_function *f_audio_alloc(struct usb_function_instance *fi)
  834. {
  835. struct f_audio *audio;
  836. struct f_uac1_legacy_opts *opts;
  837. /* allocate and initialize one new instance */
  838. audio = kzalloc(sizeof(*audio), GFP_KERNEL);
  839. if (!audio)
  840. return ERR_PTR(-ENOMEM);
  841. audio->card.func.name = "g_audio";
  842. opts = container_of(fi, struct f_uac1_legacy_opts, func_inst);
  843. mutex_lock(&opts->lock);
  844. ++opts->refcnt;
  845. mutex_unlock(&opts->lock);
  846. INIT_LIST_HEAD(&audio->play_queue);
  847. spin_lock_init(&audio->lock);
  848. audio->card.func.bind = f_audio_bind;
  849. audio->card.func.unbind = f_audio_unbind;
  850. audio->card.func.set_alt = f_audio_set_alt;
  851. audio->card.func.get_alt = f_audio_get_alt;
  852. audio->card.func.setup = f_audio_setup;
  853. audio->card.func.disable = f_audio_disable;
  854. audio->card.func.free_func = f_audio_free;
  855. control_selector_init(audio);
  856. INIT_WORK(&audio->playback_work, f_audio_playback_work);
  857. return &audio->card.func;
  858. }
  859. DECLARE_USB_FUNCTION_INIT(uac1_legacy, f_audio_alloc_inst, f_audio_alloc);
  860. MODULE_LICENSE("GPL");
  861. MODULE_AUTHOR("Bryan Wu");