f_uac1.c 25 KB

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