f_uac1_legacy.c 26 KB

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