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 = cpu_to_le16(0x0100),
  50. .wTotalLength = 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 = 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. spin_lock_irq(&audio->lock);
  288. list_add_tail(&copy_buf->list, &audio->play_queue);
  289. spin_unlock_irq(&audio->lock);
  290. schedule_work(&audio->playback_work);
  291. copy_buf = f_audio_buffer_alloc(audio_buf_size);
  292. if (IS_ERR(copy_buf))
  293. return -ENOMEM;
  294. }
  295. memcpy(copy_buf->buf + copy_buf->actual, req->buf, req->actual);
  296. copy_buf->actual += req->actual;
  297. audio->copy_buf = copy_buf;
  298. err = usb_ep_queue(ep, req, GFP_ATOMIC);
  299. if (err)
  300. ERROR(cdev, "%s queue req: %d\n", ep->name, err);
  301. return 0;
  302. }
  303. static void f_audio_complete(struct usb_ep *ep, struct usb_request *req)
  304. {
  305. struct f_audio *audio = req->context;
  306. int status = req->status;
  307. u32 data = 0;
  308. struct usb_ep *out_ep = audio->out_ep;
  309. switch (status) {
  310. case 0: /* normal completion? */
  311. if (ep == out_ep)
  312. f_audio_out_ep_complete(ep, req);
  313. else if (audio->set_con) {
  314. memcpy(&data, req->buf, req->length);
  315. audio->set_con->set(audio->set_con, audio->set_cmd,
  316. le16_to_cpu(data));
  317. audio->set_con = NULL;
  318. }
  319. break;
  320. default:
  321. break;
  322. }
  323. }
  324. static int audio_set_intf_req(struct usb_function *f,
  325. const struct usb_ctrlrequest *ctrl)
  326. {
  327. struct f_audio *audio = func_to_audio(f);
  328. struct usb_composite_dev *cdev = f->config->cdev;
  329. struct usb_request *req = cdev->req;
  330. u8 id = ((le16_to_cpu(ctrl->wIndex) >> 8) & 0xFF);
  331. u16 len = le16_to_cpu(ctrl->wLength);
  332. u16 w_value = le16_to_cpu(ctrl->wValue);
  333. u8 con_sel = (w_value >> 8) & 0xFF;
  334. u8 cmd = (ctrl->bRequest & 0x0F);
  335. struct usb_audio_control_selector *cs;
  336. struct usb_audio_control *con;
  337. DBG(cdev, "bRequest 0x%x, w_value 0x%04x, len %d, entity %d\n",
  338. ctrl->bRequest, w_value, len, id);
  339. list_for_each_entry(cs, &audio->cs, list) {
  340. if (cs->id == id) {
  341. list_for_each_entry(con, &cs->control, list) {
  342. if (con->type == con_sel) {
  343. audio->set_con = con;
  344. break;
  345. }
  346. }
  347. break;
  348. }
  349. }
  350. audio->set_cmd = cmd;
  351. req->context = audio;
  352. req->complete = f_audio_complete;
  353. return len;
  354. }
  355. static int audio_get_intf_req(struct usb_function *f,
  356. const struct usb_ctrlrequest *ctrl)
  357. {
  358. struct f_audio *audio = func_to_audio(f);
  359. struct usb_composite_dev *cdev = f->config->cdev;
  360. struct usb_request *req = cdev->req;
  361. int value = -EOPNOTSUPP;
  362. u8 id = ((le16_to_cpu(ctrl->wIndex) >> 8) & 0xFF);
  363. u16 len = le16_to_cpu(ctrl->wLength);
  364. u16 w_value = le16_to_cpu(ctrl->wValue);
  365. u8 con_sel = (w_value >> 8) & 0xFF;
  366. u8 cmd = (ctrl->bRequest & 0x0F);
  367. struct usb_audio_control_selector *cs;
  368. struct usb_audio_control *con;
  369. DBG(cdev, "bRequest 0x%x, w_value 0x%04x, len %d, entity %d\n",
  370. ctrl->bRequest, w_value, len, id);
  371. list_for_each_entry(cs, &audio->cs, list) {
  372. if (cs->id == id) {
  373. list_for_each_entry(con, &cs->control, list) {
  374. if (con->type == con_sel && con->get) {
  375. value = con->get(con, cmd);
  376. break;
  377. }
  378. }
  379. break;
  380. }
  381. }
  382. req->context = audio;
  383. req->complete = f_audio_complete;
  384. len = min_t(size_t, sizeof(value), len);
  385. memcpy(req->buf, &value, len);
  386. return len;
  387. }
  388. static int audio_set_endpoint_req(struct usb_function *f,
  389. const struct usb_ctrlrequest *ctrl)
  390. {
  391. struct usb_composite_dev *cdev = f->config->cdev;
  392. int value = -EOPNOTSUPP;
  393. u16 ep = le16_to_cpu(ctrl->wIndex);
  394. u16 len = le16_to_cpu(ctrl->wLength);
  395. u16 w_value = le16_to_cpu(ctrl->wValue);
  396. DBG(cdev, "bRequest 0x%x, w_value 0x%04x, len %d, endpoint %d\n",
  397. ctrl->bRequest, w_value, len, ep);
  398. switch (ctrl->bRequest) {
  399. case UAC_SET_CUR:
  400. value = len;
  401. break;
  402. case UAC_SET_MIN:
  403. break;
  404. case UAC_SET_MAX:
  405. break;
  406. case UAC_SET_RES:
  407. break;
  408. case UAC_SET_MEM:
  409. break;
  410. default:
  411. break;
  412. }
  413. return value;
  414. }
  415. static int audio_get_endpoint_req(struct usb_function *f,
  416. const struct usb_ctrlrequest *ctrl)
  417. {
  418. struct usb_composite_dev *cdev = f->config->cdev;
  419. int value = -EOPNOTSUPP;
  420. u8 ep = ((le16_to_cpu(ctrl->wIndex) >> 8) & 0xFF);
  421. u16 len = le16_to_cpu(ctrl->wLength);
  422. u16 w_value = le16_to_cpu(ctrl->wValue);
  423. DBG(cdev, "bRequest 0x%x, w_value 0x%04x, len %d, endpoint %d\n",
  424. ctrl->bRequest, w_value, len, ep);
  425. switch (ctrl->bRequest) {
  426. case UAC_GET_CUR:
  427. case UAC_GET_MIN:
  428. case UAC_GET_MAX:
  429. case UAC_GET_RES:
  430. value = len;
  431. break;
  432. case UAC_GET_MEM:
  433. break;
  434. default:
  435. break;
  436. }
  437. return value;
  438. }
  439. static int
  440. f_audio_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
  441. {
  442. struct usb_composite_dev *cdev = f->config->cdev;
  443. struct usb_request *req = cdev->req;
  444. int value = -EOPNOTSUPP;
  445. u16 w_index = le16_to_cpu(ctrl->wIndex);
  446. u16 w_value = le16_to_cpu(ctrl->wValue);
  447. u16 w_length = le16_to_cpu(ctrl->wLength);
  448. /* composite driver infrastructure handles everything; interface
  449. * activation uses set_alt().
  450. */
  451. switch (ctrl->bRequestType) {
  452. case USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE:
  453. value = audio_set_intf_req(f, ctrl);
  454. break;
  455. case USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE:
  456. value = audio_get_intf_req(f, ctrl);
  457. break;
  458. case USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_ENDPOINT:
  459. value = audio_set_endpoint_req(f, ctrl);
  460. break;
  461. case USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT:
  462. value = audio_get_endpoint_req(f, ctrl);
  463. break;
  464. default:
  465. ERROR(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
  466. ctrl->bRequestType, ctrl->bRequest,
  467. w_value, w_index, w_length);
  468. }
  469. /* respond with data transfer or status phase? */
  470. if (value >= 0) {
  471. DBG(cdev, "audio req%02x.%02x v%04x i%04x l%d\n",
  472. ctrl->bRequestType, ctrl->bRequest,
  473. w_value, w_index, w_length);
  474. req->zero = 0;
  475. req->length = value;
  476. value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
  477. if (value < 0)
  478. ERROR(cdev, "audio response on err %d\n", value);
  479. }
  480. /* device either stalls (value < 0) or reports success */
  481. return value;
  482. }
  483. static int f_audio_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
  484. {
  485. struct f_audio *audio = func_to_audio(f);
  486. struct usb_composite_dev *cdev = f->config->cdev;
  487. struct usb_ep *out_ep = audio->out_ep;
  488. struct usb_request *req;
  489. struct f_uac1_legacy_opts *opts;
  490. int req_buf_size, req_count, audio_buf_size;
  491. int i = 0, err = 0;
  492. DBG(cdev, "intf %d, alt %d\n", intf, alt);
  493. opts = container_of(f->fi, struct f_uac1_legacy_opts, func_inst);
  494. req_buf_size = opts->req_buf_size;
  495. req_count = opts->req_count;
  496. audio_buf_size = opts->audio_buf_size;
  497. /* No i/f has more than 2 alt settings */
  498. if (alt > 1) {
  499. ERROR(cdev, "%s:%d Error!\n", __func__, __LINE__);
  500. return -EINVAL;
  501. }
  502. if (intf == audio->ac_intf) {
  503. /* Control I/f has only 1 AltSetting - 0 */
  504. if (alt) {
  505. ERROR(cdev, "%s:%d Error!\n", __func__, __LINE__);
  506. return -EINVAL;
  507. }
  508. return 0;
  509. } else if (intf == audio->as_intf) {
  510. if (alt == 1) {
  511. err = config_ep_by_speed(cdev->gadget, f, out_ep);
  512. if (err)
  513. return err;
  514. usb_ep_enable(out_ep);
  515. audio->copy_buf = f_audio_buffer_alloc(audio_buf_size);
  516. if (IS_ERR(audio->copy_buf))
  517. return -ENOMEM;
  518. /*
  519. * allocate a bunch of read buffers
  520. * and queue them all at once.
  521. */
  522. for (i = 0; i < req_count && err == 0; i++) {
  523. req = usb_ep_alloc_request(out_ep, GFP_ATOMIC);
  524. if (req) {
  525. req->buf = kzalloc(req_buf_size,
  526. GFP_ATOMIC);
  527. if (req->buf) {
  528. req->length = req_buf_size;
  529. req->context = audio;
  530. req->complete =
  531. f_audio_complete;
  532. err = usb_ep_queue(out_ep,
  533. req, GFP_ATOMIC);
  534. if (err)
  535. ERROR(cdev,
  536. "%s queue req: %d\n",
  537. out_ep->name, err);
  538. } else
  539. err = -ENOMEM;
  540. } else
  541. err = -ENOMEM;
  542. }
  543. } else {
  544. struct f_audio_buf *copy_buf = audio->copy_buf;
  545. if (copy_buf) {
  546. list_add_tail(&copy_buf->list,
  547. &audio->play_queue);
  548. schedule_work(&audio->playback_work);
  549. }
  550. }
  551. audio->as_alt = alt;
  552. }
  553. return err;
  554. }
  555. static int f_audio_get_alt(struct usb_function *f, unsigned intf)
  556. {
  557. struct f_audio *audio = func_to_audio(f);
  558. struct usb_composite_dev *cdev = f->config->cdev;
  559. if (intf == audio->ac_intf)
  560. return audio->ac_alt;
  561. else if (intf == audio->as_intf)
  562. return audio->as_alt;
  563. else
  564. ERROR(cdev, "%s:%d Invalid Interface %d!\n",
  565. __func__, __LINE__, intf);
  566. return -EINVAL;
  567. }
  568. static void f_audio_disable(struct usb_function *f)
  569. {
  570. return;
  571. }
  572. /*-------------------------------------------------------------------------*/
  573. static void f_audio_build_desc(struct f_audio *audio)
  574. {
  575. struct gaudio *card = &audio->card;
  576. u8 *sam_freq;
  577. int rate;
  578. /* Set channel numbers */
  579. input_terminal_desc.bNrChannels = u_audio_get_playback_channels(card);
  580. as_type_i_desc.bNrChannels = u_audio_get_playback_channels(card);
  581. /* Set sample rates */
  582. rate = u_audio_get_playback_rate(card);
  583. sam_freq = as_type_i_desc.tSamFreq[0];
  584. memcpy(sam_freq, &rate, 3);
  585. /* Todo: Set Sample bits and other parameters */
  586. return;
  587. }
  588. /* audio function driver setup/binding */
  589. static int
  590. f_audio_bind(struct usb_configuration *c, struct usb_function *f)
  591. {
  592. struct usb_composite_dev *cdev = c->cdev;
  593. struct f_audio *audio = func_to_audio(f);
  594. struct usb_string *us;
  595. int status;
  596. struct usb_ep *ep = NULL;
  597. struct f_uac1_legacy_opts *audio_opts;
  598. audio_opts = container_of(f->fi, struct f_uac1_legacy_opts, func_inst);
  599. audio->card.gadget = c->cdev->gadget;
  600. /* set up ASLA audio devices */
  601. if (!audio_opts->bound) {
  602. status = gaudio_setup(&audio->card);
  603. if (status < 0)
  604. return status;
  605. audio_opts->bound = true;
  606. }
  607. us = usb_gstrings_attach(cdev, uac1_strings, ARRAY_SIZE(strings_uac1));
  608. if (IS_ERR(us))
  609. return PTR_ERR(us);
  610. ac_interface_desc.iInterface = us[STR_AC_IF].id;
  611. input_terminal_desc.iTerminal = us[STR_INPUT_TERMINAL].id;
  612. input_terminal_desc.iChannelNames = us[STR_INPUT_TERMINAL_CH_NAMES].id;
  613. feature_unit_desc.iFeature = us[STR_FEAT_DESC_0].id;
  614. output_terminal_desc.iTerminal = us[STR_OUTPUT_TERMINAL].id;
  615. as_interface_alt_0_desc.iInterface = us[STR_AS_IF_ALT0].id;
  616. as_interface_alt_1_desc.iInterface = us[STR_AS_IF_ALT1].id;
  617. f_audio_build_desc(audio);
  618. /* allocate instance-specific interface IDs, and patch descriptors */
  619. status = usb_interface_id(c, f);
  620. if (status < 0)
  621. goto fail;
  622. ac_interface_desc.bInterfaceNumber = status;
  623. audio->ac_intf = status;
  624. audio->ac_alt = 0;
  625. status = usb_interface_id(c, f);
  626. if (status < 0)
  627. goto fail;
  628. as_interface_alt_0_desc.bInterfaceNumber = status;
  629. as_interface_alt_1_desc.bInterfaceNumber = status;
  630. audio->as_intf = status;
  631. audio->as_alt = 0;
  632. status = -ENODEV;
  633. /* allocate instance-specific endpoints */
  634. ep = usb_ep_autoconfig(cdev->gadget, &as_out_ep_desc);
  635. if (!ep)
  636. goto fail;
  637. audio->out_ep = ep;
  638. audio->out_ep->desc = &as_out_ep_desc;
  639. /* copy descriptors, and track endpoint copies */
  640. status = usb_assign_descriptors(f, f_audio_desc, f_audio_desc, NULL,
  641. NULL);
  642. if (status)
  643. goto fail;
  644. return 0;
  645. fail:
  646. gaudio_cleanup(&audio->card);
  647. return status;
  648. }
  649. /*-------------------------------------------------------------------------*/
  650. static int generic_set_cmd(struct usb_audio_control *con, u8 cmd, int value)
  651. {
  652. con->data[cmd] = value;
  653. return 0;
  654. }
  655. static int generic_get_cmd(struct usb_audio_control *con, u8 cmd)
  656. {
  657. return con->data[cmd];
  658. }
  659. /* Todo: add more control selecotor dynamically */
  660. static int control_selector_init(struct f_audio *audio)
  661. {
  662. INIT_LIST_HEAD(&audio->cs);
  663. list_add(&feature_unit.list, &audio->cs);
  664. INIT_LIST_HEAD(&feature_unit.control);
  665. list_add(&mute_control.list, &feature_unit.control);
  666. list_add(&volume_control.list, &feature_unit.control);
  667. volume_control.data[UAC__CUR] = 0xffc0;
  668. volume_control.data[UAC__MIN] = 0xe3a0;
  669. volume_control.data[UAC__MAX] = 0xfff0;
  670. volume_control.data[UAC__RES] = 0x0030;
  671. return 0;
  672. }
  673. static inline
  674. struct f_uac1_legacy_opts *to_f_uac1_opts(struct config_item *item)
  675. {
  676. return container_of(to_config_group(item), struct f_uac1_legacy_opts,
  677. func_inst.group);
  678. }
  679. static void f_uac1_attr_release(struct config_item *item)
  680. {
  681. struct f_uac1_legacy_opts *opts = to_f_uac1_opts(item);
  682. usb_put_function_instance(&opts->func_inst);
  683. }
  684. static struct configfs_item_operations f_uac1_item_ops = {
  685. .release = f_uac1_attr_release,
  686. };
  687. #define UAC1_INT_ATTRIBUTE(name) \
  688. static ssize_t f_uac1_opts_##name##_show(struct config_item *item, \
  689. char *page) \
  690. { \
  691. struct f_uac1_legacy_opts *opts = to_f_uac1_opts(item); \
  692. int result; \
  693. \
  694. mutex_lock(&opts->lock); \
  695. result = sprintf(page, "%u\n", opts->name); \
  696. mutex_unlock(&opts->lock); \
  697. \
  698. return result; \
  699. } \
  700. \
  701. static ssize_t f_uac1_opts_##name##_store(struct config_item *item, \
  702. const char *page, size_t len) \
  703. { \
  704. struct f_uac1_legacy_opts *opts = to_f_uac1_opts(item); \
  705. int ret; \
  706. u32 num; \
  707. \
  708. mutex_lock(&opts->lock); \
  709. if (opts->refcnt) { \
  710. ret = -EBUSY; \
  711. goto end; \
  712. } \
  713. \
  714. ret = kstrtou32(page, 0, &num); \
  715. if (ret) \
  716. goto end; \
  717. \
  718. opts->name = num; \
  719. ret = len; \
  720. \
  721. end: \
  722. mutex_unlock(&opts->lock); \
  723. return ret; \
  724. } \
  725. \
  726. CONFIGFS_ATTR(f_uac1_opts_, name)
  727. UAC1_INT_ATTRIBUTE(req_buf_size);
  728. UAC1_INT_ATTRIBUTE(req_count);
  729. UAC1_INT_ATTRIBUTE(audio_buf_size);
  730. #define UAC1_STR_ATTRIBUTE(name) \
  731. static ssize_t f_uac1_opts_##name##_show(struct config_item *item, \
  732. char *page) \
  733. { \
  734. struct f_uac1_legacy_opts *opts = to_f_uac1_opts(item); \
  735. int result; \
  736. \
  737. mutex_lock(&opts->lock); \
  738. result = sprintf(page, "%s\n", opts->name); \
  739. mutex_unlock(&opts->lock); \
  740. \
  741. return result; \
  742. } \
  743. \
  744. static ssize_t f_uac1_opts_##name##_store(struct config_item *item, \
  745. const char *page, size_t len) \
  746. { \
  747. struct f_uac1_legacy_opts *opts = to_f_uac1_opts(item); \
  748. int ret = -EBUSY; \
  749. char *tmp; \
  750. \
  751. mutex_lock(&opts->lock); \
  752. if (opts->refcnt) \
  753. goto end; \
  754. \
  755. tmp = kstrndup(page, len, GFP_KERNEL); \
  756. if (tmp) { \
  757. ret = -ENOMEM; \
  758. goto end; \
  759. } \
  760. if (opts->name##_alloc) \
  761. kfree(opts->name); \
  762. opts->name##_alloc = true; \
  763. opts->name = tmp; \
  764. ret = len; \
  765. \
  766. end: \
  767. mutex_unlock(&opts->lock); \
  768. return ret; \
  769. } \
  770. \
  771. CONFIGFS_ATTR(f_uac1_opts_, name)
  772. UAC1_STR_ATTRIBUTE(fn_play);
  773. UAC1_STR_ATTRIBUTE(fn_cap);
  774. UAC1_STR_ATTRIBUTE(fn_cntl);
  775. static struct configfs_attribute *f_uac1_attrs[] = {
  776. &f_uac1_opts_attr_req_buf_size,
  777. &f_uac1_opts_attr_req_count,
  778. &f_uac1_opts_attr_audio_buf_size,
  779. &f_uac1_opts_attr_fn_play,
  780. &f_uac1_opts_attr_fn_cap,
  781. &f_uac1_opts_attr_fn_cntl,
  782. NULL,
  783. };
  784. static const struct config_item_type f_uac1_func_type = {
  785. .ct_item_ops = &f_uac1_item_ops,
  786. .ct_attrs = f_uac1_attrs,
  787. .ct_owner = THIS_MODULE,
  788. };
  789. static void f_audio_free_inst(struct usb_function_instance *f)
  790. {
  791. struct f_uac1_legacy_opts *opts;
  792. opts = container_of(f, struct f_uac1_legacy_opts, func_inst);
  793. if (opts->fn_play_alloc)
  794. kfree(opts->fn_play);
  795. if (opts->fn_cap_alloc)
  796. kfree(opts->fn_cap);
  797. if (opts->fn_cntl_alloc)
  798. kfree(opts->fn_cntl);
  799. kfree(opts);
  800. }
  801. static struct usb_function_instance *f_audio_alloc_inst(void)
  802. {
  803. struct f_uac1_legacy_opts *opts;
  804. opts = kzalloc(sizeof(*opts), GFP_KERNEL);
  805. if (!opts)
  806. return ERR_PTR(-ENOMEM);
  807. mutex_init(&opts->lock);
  808. opts->func_inst.free_func_inst = f_audio_free_inst;
  809. config_group_init_type_name(&opts->func_inst.group, "",
  810. &f_uac1_func_type);
  811. opts->req_buf_size = UAC1_OUT_EP_MAX_PACKET_SIZE;
  812. opts->req_count = UAC1_REQ_COUNT;
  813. opts->audio_buf_size = UAC1_AUDIO_BUF_SIZE;
  814. opts->fn_play = FILE_PCM_PLAYBACK;
  815. opts->fn_cap = FILE_PCM_CAPTURE;
  816. opts->fn_cntl = FILE_CONTROL;
  817. return &opts->func_inst;
  818. }
  819. static void f_audio_free(struct usb_function *f)
  820. {
  821. struct f_audio *audio = func_to_audio(f);
  822. struct f_uac1_legacy_opts *opts;
  823. gaudio_cleanup(&audio->card);
  824. opts = container_of(f->fi, struct f_uac1_legacy_opts, func_inst);
  825. kfree(audio);
  826. mutex_lock(&opts->lock);
  827. --opts->refcnt;
  828. mutex_unlock(&opts->lock);
  829. }
  830. static void f_audio_unbind(struct usb_configuration *c, struct usb_function *f)
  831. {
  832. usb_free_all_descriptors(f);
  833. }
  834. static struct usb_function *f_audio_alloc(struct usb_function_instance *fi)
  835. {
  836. struct f_audio *audio;
  837. struct f_uac1_legacy_opts *opts;
  838. /* allocate and initialize one new instance */
  839. audio = kzalloc(sizeof(*audio), GFP_KERNEL);
  840. if (!audio)
  841. return ERR_PTR(-ENOMEM);
  842. audio->card.func.name = "g_audio";
  843. opts = container_of(fi, struct f_uac1_legacy_opts, func_inst);
  844. mutex_lock(&opts->lock);
  845. ++opts->refcnt;
  846. mutex_unlock(&opts->lock);
  847. INIT_LIST_HEAD(&audio->play_queue);
  848. spin_lock_init(&audio->lock);
  849. audio->card.func.bind = f_audio_bind;
  850. audio->card.func.unbind = f_audio_unbind;
  851. audio->card.func.set_alt = f_audio_set_alt;
  852. audio->card.func.get_alt = f_audio_get_alt;
  853. audio->card.func.setup = f_audio_setup;
  854. audio->card.func.disable = f_audio_disable;
  855. audio->card.func.free_func = f_audio_free;
  856. control_selector_init(audio);
  857. INIT_WORK(&audio->playback_work, f_audio_playback_work);
  858. return &audio->card.func;
  859. }
  860. DECLARE_USB_FUNCTION_INIT(uac1_legacy, f_audio_alloc_inst, f_audio_alloc);
  861. MODULE_DESCRIPTION("USB Audio class function driver");
  862. MODULE_LICENSE("GPL");
  863. MODULE_AUTHOR("Bryan Wu");