f_uac1_legacy.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020
  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. * Enter bugs at http://blackfin.uclinux.org/
  9. */
  10. #include <linux/slab.h>
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/device.h>
  14. #include <linux/atomic.h>
  15. #include "u_uac1_legacy.h"
  16. static int generic_set_cmd(struct usb_audio_control *con, u8 cmd, int value);
  17. static int generic_get_cmd(struct usb_audio_control *con, u8 cmd);
  18. /*
  19. * DESCRIPTORS ... most are static, but strings and full
  20. * configuration descriptors are built on demand.
  21. */
  22. /*
  23. * We have two interfaces- AudioControl and AudioStreaming
  24. * TODO: only supcard playback currently
  25. */
  26. #define F_AUDIO_AC_INTERFACE 0
  27. #define F_AUDIO_AS_INTERFACE 1
  28. #define F_AUDIO_NUM_INTERFACES 1
  29. /* B.3.1 Standard AC Interface Descriptor */
  30. static struct usb_interface_descriptor ac_interface_desc = {
  31. .bLength = USB_DT_INTERFACE_SIZE,
  32. .bDescriptorType = USB_DT_INTERFACE,
  33. .bNumEndpoints = 0,
  34. .bInterfaceClass = USB_CLASS_AUDIO,
  35. .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL,
  36. };
  37. /*
  38. * The number of AudioStreaming and MIDIStreaming interfaces
  39. * in the Audio Interface Collection
  40. */
  41. DECLARE_UAC_AC_HEADER_DESCRIPTOR(1);
  42. #define UAC_DT_AC_HEADER_LENGTH UAC_DT_AC_HEADER_SIZE(F_AUDIO_NUM_INTERFACES)
  43. /* 1 input terminal, 1 output terminal and 1 feature unit */
  44. #define UAC_DT_TOTAL_LENGTH (UAC_DT_AC_HEADER_LENGTH + UAC_DT_INPUT_TERMINAL_SIZE \
  45. + UAC_DT_OUTPUT_TERMINAL_SIZE + UAC_DT_FEATURE_UNIT_SIZE(0))
  46. /* B.3.2 Class-Specific AC Interface Descriptor */
  47. static struct uac1_ac_header_descriptor_1 ac_header_desc = {
  48. .bLength = UAC_DT_AC_HEADER_LENGTH,
  49. .bDescriptorType = USB_DT_CS_INTERFACE,
  50. .bDescriptorSubtype = UAC_HEADER,
  51. .bcdADC = __constant_cpu_to_le16(0x0100),
  52. .wTotalLength = __constant_cpu_to_le16(UAC_DT_TOTAL_LENGTH),
  53. .bInCollection = F_AUDIO_NUM_INTERFACES,
  54. .baInterfaceNr = {
  55. /* Interface number of the first AudioStream interface */
  56. [0] = 1,
  57. }
  58. };
  59. #define INPUT_TERMINAL_ID 1
  60. static struct uac_input_terminal_descriptor input_terminal_desc = {
  61. .bLength = UAC_DT_INPUT_TERMINAL_SIZE,
  62. .bDescriptorType = USB_DT_CS_INTERFACE,
  63. .bDescriptorSubtype = UAC_INPUT_TERMINAL,
  64. .bTerminalID = INPUT_TERMINAL_ID,
  65. .wTerminalType = UAC_TERMINAL_STREAMING,
  66. .bAssocTerminal = 0,
  67. .wChannelConfig = 0x3,
  68. };
  69. DECLARE_UAC_FEATURE_UNIT_DESCRIPTOR(0);
  70. #define FEATURE_UNIT_ID 2
  71. static struct uac_feature_unit_descriptor_0 feature_unit_desc = {
  72. .bLength = UAC_DT_FEATURE_UNIT_SIZE(0),
  73. .bDescriptorType = USB_DT_CS_INTERFACE,
  74. .bDescriptorSubtype = UAC_FEATURE_UNIT,
  75. .bUnitID = FEATURE_UNIT_ID,
  76. .bSourceID = INPUT_TERMINAL_ID,
  77. .bControlSize = 2,
  78. .bmaControls[0] = (UAC_FU_MUTE | UAC_FU_VOLUME),
  79. };
  80. static struct usb_audio_control mute_control = {
  81. .list = LIST_HEAD_INIT(mute_control.list),
  82. .name = "Mute Control",
  83. .type = UAC_FU_MUTE,
  84. /* Todo: add real Mute control code */
  85. .set = generic_set_cmd,
  86. .get = generic_get_cmd,
  87. };
  88. static struct usb_audio_control volume_control = {
  89. .list = LIST_HEAD_INIT(volume_control.list),
  90. .name = "Volume Control",
  91. .type = UAC_FU_VOLUME,
  92. /* Todo: add real Volume control code */
  93. .set = generic_set_cmd,
  94. .get = generic_get_cmd,
  95. };
  96. static struct usb_audio_control_selector feature_unit = {
  97. .list = LIST_HEAD_INIT(feature_unit.list),
  98. .id = FEATURE_UNIT_ID,
  99. .name = "Mute & Volume Control",
  100. .type = UAC_FEATURE_UNIT,
  101. .desc = (struct usb_descriptor_header *)&feature_unit_desc,
  102. };
  103. #define OUTPUT_TERMINAL_ID 3
  104. static struct uac1_output_terminal_descriptor output_terminal_desc = {
  105. .bLength = UAC_DT_OUTPUT_TERMINAL_SIZE,
  106. .bDescriptorType = USB_DT_CS_INTERFACE,
  107. .bDescriptorSubtype = UAC_OUTPUT_TERMINAL,
  108. .bTerminalID = OUTPUT_TERMINAL_ID,
  109. .wTerminalType = UAC_OUTPUT_TERMINAL_SPEAKER,
  110. .bAssocTerminal = FEATURE_UNIT_ID,
  111. .bSourceID = FEATURE_UNIT_ID,
  112. };
  113. /* B.4.1 Standard AS Interface Descriptor */
  114. static struct usb_interface_descriptor as_interface_alt_0_desc = {
  115. .bLength = USB_DT_INTERFACE_SIZE,
  116. .bDescriptorType = USB_DT_INTERFACE,
  117. .bAlternateSetting = 0,
  118. .bNumEndpoints = 0,
  119. .bInterfaceClass = USB_CLASS_AUDIO,
  120. .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
  121. };
  122. static struct usb_interface_descriptor as_interface_alt_1_desc = {
  123. .bLength = USB_DT_INTERFACE_SIZE,
  124. .bDescriptorType = USB_DT_INTERFACE,
  125. .bAlternateSetting = 1,
  126. .bNumEndpoints = 1,
  127. .bInterfaceClass = USB_CLASS_AUDIO,
  128. .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
  129. };
  130. /* B.4.2 Class-Specific AS Interface Descriptor */
  131. static struct uac1_as_header_descriptor as_header_desc = {
  132. .bLength = UAC_DT_AS_HEADER_SIZE,
  133. .bDescriptorType = USB_DT_CS_INTERFACE,
  134. .bDescriptorSubtype = UAC_AS_GENERAL,
  135. .bTerminalLink = INPUT_TERMINAL_ID,
  136. .bDelay = 1,
  137. .wFormatTag = UAC_FORMAT_TYPE_I_PCM,
  138. };
  139. DECLARE_UAC_FORMAT_TYPE_I_DISCRETE_DESC(1);
  140. static struct uac_format_type_i_discrete_descriptor_1 as_type_i_desc = {
  141. .bLength = UAC_FORMAT_TYPE_I_DISCRETE_DESC_SIZE(1),
  142. .bDescriptorType = USB_DT_CS_INTERFACE,
  143. .bDescriptorSubtype = UAC_FORMAT_TYPE,
  144. .bFormatType = UAC_FORMAT_TYPE_I,
  145. .bSubframeSize = 2,
  146. .bBitResolution = 16,
  147. .bSamFreqType = 1,
  148. };
  149. /* Standard ISO OUT Endpoint Descriptor */
  150. static struct usb_endpoint_descriptor as_out_ep_desc = {
  151. .bLength = USB_DT_ENDPOINT_AUDIO_SIZE,
  152. .bDescriptorType = USB_DT_ENDPOINT,
  153. .bEndpointAddress = USB_DIR_OUT,
  154. .bmAttributes = USB_ENDPOINT_SYNC_ADAPTIVE
  155. | USB_ENDPOINT_XFER_ISOC,
  156. .wMaxPacketSize = cpu_to_le16(UAC1_OUT_EP_MAX_PACKET_SIZE),
  157. .bInterval = 4,
  158. };
  159. /* Class-specific AS ISO OUT Endpoint Descriptor */
  160. static struct uac_iso_endpoint_descriptor as_iso_out_desc = {
  161. .bLength = UAC_ISO_ENDPOINT_DESC_SIZE,
  162. .bDescriptorType = USB_DT_CS_ENDPOINT,
  163. .bDescriptorSubtype = UAC_EP_GENERAL,
  164. .bmAttributes = 1,
  165. .bLockDelayUnits = 1,
  166. .wLockDelay = __constant_cpu_to_le16(1),
  167. };
  168. static struct usb_descriptor_header *f_audio_desc[] = {
  169. (struct usb_descriptor_header *)&ac_interface_desc,
  170. (struct usb_descriptor_header *)&ac_header_desc,
  171. (struct usb_descriptor_header *)&input_terminal_desc,
  172. (struct usb_descriptor_header *)&output_terminal_desc,
  173. (struct usb_descriptor_header *)&feature_unit_desc,
  174. (struct usb_descriptor_header *)&as_interface_alt_0_desc,
  175. (struct usb_descriptor_header *)&as_interface_alt_1_desc,
  176. (struct usb_descriptor_header *)&as_header_desc,
  177. (struct usb_descriptor_header *)&as_type_i_desc,
  178. (struct usb_descriptor_header *)&as_out_ep_desc,
  179. (struct usb_descriptor_header *)&as_iso_out_desc,
  180. NULL,
  181. };
  182. enum {
  183. STR_AC_IF,
  184. STR_INPUT_TERMINAL,
  185. STR_INPUT_TERMINAL_CH_NAMES,
  186. STR_FEAT_DESC_0,
  187. STR_OUTPUT_TERMINAL,
  188. STR_AS_IF_ALT0,
  189. STR_AS_IF_ALT1,
  190. };
  191. static struct usb_string strings_uac1[] = {
  192. [STR_AC_IF].s = "AC Interface",
  193. [STR_INPUT_TERMINAL].s = "Input terminal",
  194. [STR_INPUT_TERMINAL_CH_NAMES].s = "Channels",
  195. [STR_FEAT_DESC_0].s = "Volume control & mute",
  196. [STR_OUTPUT_TERMINAL].s = "Output terminal",
  197. [STR_AS_IF_ALT0].s = "AS Interface",
  198. [STR_AS_IF_ALT1].s = "AS Interface",
  199. { },
  200. };
  201. static struct usb_gadget_strings str_uac1 = {
  202. .language = 0x0409, /* en-us */
  203. .strings = strings_uac1,
  204. };
  205. static struct usb_gadget_strings *uac1_strings[] = {
  206. &str_uac1,
  207. NULL,
  208. };
  209. /*
  210. * This function is an ALSA sound card following USB Audio Class Spec 1.0.
  211. */
  212. /*-------------------------------------------------------------------------*/
  213. struct f_audio_buf {
  214. u8 *buf;
  215. int actual;
  216. struct list_head list;
  217. };
  218. static struct f_audio_buf *f_audio_buffer_alloc(int buf_size)
  219. {
  220. struct f_audio_buf *copy_buf;
  221. copy_buf = kzalloc(sizeof *copy_buf, GFP_ATOMIC);
  222. if (!copy_buf)
  223. return ERR_PTR(-ENOMEM);
  224. copy_buf->buf = kzalloc(buf_size, GFP_ATOMIC);
  225. if (!copy_buf->buf) {
  226. kfree(copy_buf);
  227. return ERR_PTR(-ENOMEM);
  228. }
  229. return copy_buf;
  230. }
  231. static void f_audio_buffer_free(struct f_audio_buf *audio_buf)
  232. {
  233. kfree(audio_buf->buf);
  234. kfree(audio_buf);
  235. }
  236. /*-------------------------------------------------------------------------*/
  237. struct f_audio {
  238. struct gaudio card;
  239. u8 ac_intf, ac_alt;
  240. u8 as_intf, as_alt;
  241. /* endpoints handle full and/or high speeds */
  242. struct usb_ep *out_ep;
  243. spinlock_t lock;
  244. struct f_audio_buf *copy_buf;
  245. struct work_struct playback_work;
  246. struct list_head play_queue;
  247. /* Control Set command */
  248. struct list_head cs;
  249. u8 set_cmd;
  250. struct usb_audio_control *set_con;
  251. };
  252. static inline struct f_audio *func_to_audio(struct usb_function *f)
  253. {
  254. return container_of(f, struct f_audio, card.func);
  255. }
  256. /*-------------------------------------------------------------------------*/
  257. static void f_audio_playback_work(struct work_struct *data)
  258. {
  259. struct f_audio *audio = container_of(data, struct f_audio,
  260. playback_work);
  261. struct f_audio_buf *play_buf;
  262. spin_lock_irq(&audio->lock);
  263. if (list_empty(&audio->play_queue)) {
  264. spin_unlock_irq(&audio->lock);
  265. return;
  266. }
  267. play_buf = list_first_entry(&audio->play_queue,
  268. struct f_audio_buf, list);
  269. list_del(&play_buf->list);
  270. spin_unlock_irq(&audio->lock);
  271. u_audio_playback(&audio->card, play_buf->buf, play_buf->actual);
  272. f_audio_buffer_free(play_buf);
  273. }
  274. static int f_audio_out_ep_complete(struct usb_ep *ep, struct usb_request *req)
  275. {
  276. struct f_audio *audio = req->context;
  277. struct usb_composite_dev *cdev = audio->card.func.config->cdev;
  278. struct f_audio_buf *copy_buf = audio->copy_buf;
  279. struct f_uac1_legacy_opts *opts;
  280. int audio_buf_size;
  281. int err;
  282. opts = container_of(audio->card.func.fi, struct f_uac1_legacy_opts,
  283. func_inst);
  284. audio_buf_size = opts->audio_buf_size;
  285. if (!copy_buf)
  286. return -EINVAL;
  287. /* Copy buffer is full, add it to the play_queue */
  288. if (audio_buf_size - copy_buf->actual < req->actual) {
  289. list_add_tail(&copy_buf->list, &audio->play_queue);
  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. status = -ENOMEM;
  640. /* copy descriptors, and track endpoint copies */
  641. status = usb_assign_descriptors(f, f_audio_desc, f_audio_desc, NULL,
  642. NULL);
  643. if (status)
  644. goto fail;
  645. return 0;
  646. fail:
  647. gaudio_cleanup(&audio->card);
  648. return status;
  649. }
  650. /*-------------------------------------------------------------------------*/
  651. static int generic_set_cmd(struct usb_audio_control *con, u8 cmd, int value)
  652. {
  653. con->data[cmd] = value;
  654. return 0;
  655. }
  656. static int generic_get_cmd(struct usb_audio_control *con, u8 cmd)
  657. {
  658. return con->data[cmd];
  659. }
  660. /* Todo: add more control selecotor dynamically */
  661. static int control_selector_init(struct f_audio *audio)
  662. {
  663. INIT_LIST_HEAD(&audio->cs);
  664. list_add(&feature_unit.list, &audio->cs);
  665. INIT_LIST_HEAD(&feature_unit.control);
  666. list_add(&mute_control.list, &feature_unit.control);
  667. list_add(&volume_control.list, &feature_unit.control);
  668. volume_control.data[UAC__CUR] = 0xffc0;
  669. volume_control.data[UAC__MIN] = 0xe3a0;
  670. volume_control.data[UAC__MAX] = 0xfff0;
  671. volume_control.data[UAC__RES] = 0x0030;
  672. return 0;
  673. }
  674. static inline
  675. struct f_uac1_legacy_opts *to_f_uac1_opts(struct config_item *item)
  676. {
  677. return container_of(to_config_group(item), struct f_uac1_legacy_opts,
  678. func_inst.group);
  679. }
  680. static void f_uac1_attr_release(struct config_item *item)
  681. {
  682. struct f_uac1_legacy_opts *opts = to_f_uac1_opts(item);
  683. usb_put_function_instance(&opts->func_inst);
  684. }
  685. static struct configfs_item_operations f_uac1_item_ops = {
  686. .release = f_uac1_attr_release,
  687. };
  688. #define UAC1_INT_ATTRIBUTE(name) \
  689. static ssize_t f_uac1_opts_##name##_show(struct config_item *item, \
  690. char *page) \
  691. { \
  692. struct f_uac1_legacy_opts *opts = to_f_uac1_opts(item); \
  693. int result; \
  694. \
  695. mutex_lock(&opts->lock); \
  696. result = sprintf(page, "%u\n", opts->name); \
  697. mutex_unlock(&opts->lock); \
  698. \
  699. return result; \
  700. } \
  701. \
  702. static ssize_t f_uac1_opts_##name##_store(struct config_item *item, \
  703. const char *page, size_t len) \
  704. { \
  705. struct f_uac1_legacy_opts *opts = to_f_uac1_opts(item); \
  706. int ret; \
  707. u32 num; \
  708. \
  709. mutex_lock(&opts->lock); \
  710. if (opts->refcnt) { \
  711. ret = -EBUSY; \
  712. goto end; \
  713. } \
  714. \
  715. ret = kstrtou32(page, 0, &num); \
  716. if (ret) \
  717. goto end; \
  718. \
  719. opts->name = num; \
  720. ret = len; \
  721. \
  722. end: \
  723. mutex_unlock(&opts->lock); \
  724. return ret; \
  725. } \
  726. \
  727. CONFIGFS_ATTR(f_uac1_opts_, name)
  728. UAC1_INT_ATTRIBUTE(req_buf_size);
  729. UAC1_INT_ATTRIBUTE(req_count);
  730. UAC1_INT_ATTRIBUTE(audio_buf_size);
  731. #define UAC1_STR_ATTRIBUTE(name) \
  732. static ssize_t f_uac1_opts_##name##_show(struct config_item *item, \
  733. char *page) \
  734. { \
  735. struct f_uac1_legacy_opts *opts = to_f_uac1_opts(item); \
  736. int result; \
  737. \
  738. mutex_lock(&opts->lock); \
  739. result = sprintf(page, "%s\n", opts->name); \
  740. mutex_unlock(&opts->lock); \
  741. \
  742. return result; \
  743. } \
  744. \
  745. static ssize_t f_uac1_opts_##name##_store(struct config_item *item, \
  746. const char *page, size_t len) \
  747. { \
  748. struct f_uac1_legacy_opts *opts = to_f_uac1_opts(item); \
  749. int ret = -EBUSY; \
  750. char *tmp; \
  751. \
  752. mutex_lock(&opts->lock); \
  753. if (opts->refcnt) \
  754. goto end; \
  755. \
  756. tmp = kstrndup(page, len, GFP_KERNEL); \
  757. if (tmp) { \
  758. ret = -ENOMEM; \
  759. goto end; \
  760. } \
  761. if (opts->name##_alloc) \
  762. kfree(opts->name); \
  763. opts->name##_alloc = true; \
  764. opts->name = tmp; \
  765. ret = len; \
  766. \
  767. end: \
  768. mutex_unlock(&opts->lock); \
  769. return ret; \
  770. } \
  771. \
  772. CONFIGFS_ATTR(f_uac1_opts_, name)
  773. UAC1_STR_ATTRIBUTE(fn_play);
  774. UAC1_STR_ATTRIBUTE(fn_cap);
  775. UAC1_STR_ATTRIBUTE(fn_cntl);
  776. static struct configfs_attribute *f_uac1_attrs[] = {
  777. &f_uac1_opts_attr_req_buf_size,
  778. &f_uac1_opts_attr_req_count,
  779. &f_uac1_opts_attr_audio_buf_size,
  780. &f_uac1_opts_attr_fn_play,
  781. &f_uac1_opts_attr_fn_cap,
  782. &f_uac1_opts_attr_fn_cntl,
  783. NULL,
  784. };
  785. static const struct config_item_type f_uac1_func_type = {
  786. .ct_item_ops = &f_uac1_item_ops,
  787. .ct_attrs = f_uac1_attrs,
  788. .ct_owner = THIS_MODULE,
  789. };
  790. static void f_audio_free_inst(struct usb_function_instance *f)
  791. {
  792. struct f_uac1_legacy_opts *opts;
  793. opts = container_of(f, struct f_uac1_legacy_opts, func_inst);
  794. if (opts->fn_play_alloc)
  795. kfree(opts->fn_play);
  796. if (opts->fn_cap_alloc)
  797. kfree(opts->fn_cap);
  798. if (opts->fn_cntl_alloc)
  799. kfree(opts->fn_cntl);
  800. kfree(opts);
  801. }
  802. static struct usb_function_instance *f_audio_alloc_inst(void)
  803. {
  804. struct f_uac1_legacy_opts *opts;
  805. opts = kzalloc(sizeof(*opts), GFP_KERNEL);
  806. if (!opts)
  807. return ERR_PTR(-ENOMEM);
  808. mutex_init(&opts->lock);
  809. opts->func_inst.free_func_inst = f_audio_free_inst;
  810. config_group_init_type_name(&opts->func_inst.group, "",
  811. &f_uac1_func_type);
  812. opts->req_buf_size = UAC1_OUT_EP_MAX_PACKET_SIZE;
  813. opts->req_count = UAC1_REQ_COUNT;
  814. opts->audio_buf_size = UAC1_AUDIO_BUF_SIZE;
  815. opts->fn_play = FILE_PCM_PLAYBACK;
  816. opts->fn_cap = FILE_PCM_CAPTURE;
  817. opts->fn_cntl = FILE_CONTROL;
  818. return &opts->func_inst;
  819. }
  820. static void f_audio_free(struct usb_function *f)
  821. {
  822. struct f_audio *audio = func_to_audio(f);
  823. struct f_uac1_legacy_opts *opts;
  824. gaudio_cleanup(&audio->card);
  825. opts = container_of(f->fi, struct f_uac1_legacy_opts, func_inst);
  826. kfree(audio);
  827. mutex_lock(&opts->lock);
  828. --opts->refcnt;
  829. mutex_unlock(&opts->lock);
  830. }
  831. static void f_audio_unbind(struct usb_configuration *c, struct usb_function *f)
  832. {
  833. usb_free_all_descriptors(f);
  834. }
  835. static struct usb_function *f_audio_alloc(struct usb_function_instance *fi)
  836. {
  837. struct f_audio *audio;
  838. struct f_uac1_legacy_opts *opts;
  839. /* allocate and initialize one new instance */
  840. audio = kzalloc(sizeof(*audio), GFP_KERNEL);
  841. if (!audio)
  842. return ERR_PTR(-ENOMEM);
  843. audio->card.func.name = "g_audio";
  844. opts = container_of(fi, struct f_uac1_legacy_opts, func_inst);
  845. mutex_lock(&opts->lock);
  846. ++opts->refcnt;
  847. mutex_unlock(&opts->lock);
  848. INIT_LIST_HEAD(&audio->play_queue);
  849. spin_lock_init(&audio->lock);
  850. audio->card.func.bind = f_audio_bind;
  851. audio->card.func.unbind = f_audio_unbind;
  852. audio->card.func.set_alt = f_audio_set_alt;
  853. audio->card.func.get_alt = f_audio_get_alt;
  854. audio->card.func.setup = f_audio_setup;
  855. audio->card.func.disable = f_audio_disable;
  856. audio->card.func.free_func = f_audio_free;
  857. control_selector_init(audio);
  858. INIT_WORK(&audio->playback_work, f_audio_playback_work);
  859. return &audio->card.func;
  860. }
  861. DECLARE_USB_FUNCTION_INIT(uac1_legacy, f_audio_alloc_inst, f_audio_alloc);
  862. MODULE_LICENSE("GPL");
  863. MODULE_AUTHOR("Bryan Wu");