f_uac1.c 25 KB

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