f_uac1.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  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/device.h>
  14. #include <linux/atomic.h>
  15. #include "u_uac1.h"
  16. #define OUT_EP_MAX_PACKET_SIZE 200
  17. static int req_buf_size = OUT_EP_MAX_PACKET_SIZE;
  18. module_param(req_buf_size, int, S_IRUGO);
  19. MODULE_PARM_DESC(req_buf_size, "ISO OUT endpoint request buffer size");
  20. static int req_count = 256;
  21. module_param(req_count, int, S_IRUGO);
  22. MODULE_PARM_DESC(req_count, "ISO OUT endpoint request count");
  23. static int audio_buf_size = 48000;
  24. module_param(audio_buf_size, int, S_IRUGO);
  25. MODULE_PARM_DESC(audio_buf_size, "Audio buffer size");
  26. static int generic_set_cmd(struct usb_audio_control *con, u8 cmd, int value);
  27. static int generic_get_cmd(struct usb_audio_control *con, u8 cmd);
  28. /*
  29. * DESCRIPTORS ... most are static, but strings and full
  30. * configuration descriptors are built on demand.
  31. */
  32. /*
  33. * We have two interfaces- AudioControl and AudioStreaming
  34. * TODO: only supcard playback currently
  35. */
  36. #define F_AUDIO_AC_INTERFACE 0
  37. #define F_AUDIO_AS_INTERFACE 1
  38. #define F_AUDIO_NUM_INTERFACES 2
  39. /* B.3.1 Standard AC Interface Descriptor */
  40. static struct usb_interface_descriptor ac_interface_desc __initdata = {
  41. .bLength = USB_DT_INTERFACE_SIZE,
  42. .bDescriptorType = USB_DT_INTERFACE,
  43. .bNumEndpoints = 0,
  44. .bInterfaceClass = USB_CLASS_AUDIO,
  45. .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL,
  46. };
  47. DECLARE_UAC_AC_HEADER_DESCRIPTOR(2);
  48. #define UAC_DT_AC_HEADER_LENGTH UAC_DT_AC_HEADER_SIZE(F_AUDIO_NUM_INTERFACES)
  49. /* 1 input terminal, 1 output terminal and 1 feature unit */
  50. #define UAC_DT_TOTAL_LENGTH (UAC_DT_AC_HEADER_LENGTH + UAC_DT_INPUT_TERMINAL_SIZE \
  51. + UAC_DT_OUTPUT_TERMINAL_SIZE + UAC_DT_FEATURE_UNIT_SIZE(0))
  52. /* B.3.2 Class-Specific AC Interface Descriptor */
  53. static struct uac1_ac_header_descriptor_2 ac_header_desc = {
  54. .bLength = UAC_DT_AC_HEADER_LENGTH,
  55. .bDescriptorType = USB_DT_CS_INTERFACE,
  56. .bDescriptorSubtype = UAC_HEADER,
  57. .bcdADC = __constant_cpu_to_le16(0x0100),
  58. .wTotalLength = __constant_cpu_to_le16(UAC_DT_TOTAL_LENGTH),
  59. .bInCollection = F_AUDIO_NUM_INTERFACES,
  60. .baInterfaceNr = {
  61. [0] = F_AUDIO_AC_INTERFACE,
  62. [1] = F_AUDIO_AS_INTERFACE,
  63. }
  64. };
  65. #define INPUT_TERMINAL_ID 1
  66. static struct uac_input_terminal_descriptor input_terminal_desc = {
  67. .bLength = UAC_DT_INPUT_TERMINAL_SIZE,
  68. .bDescriptorType = USB_DT_CS_INTERFACE,
  69. .bDescriptorSubtype = UAC_INPUT_TERMINAL,
  70. .bTerminalID = INPUT_TERMINAL_ID,
  71. .wTerminalType = UAC_TERMINAL_STREAMING,
  72. .bAssocTerminal = 0,
  73. .wChannelConfig = 0x3,
  74. };
  75. DECLARE_UAC_FEATURE_UNIT_DESCRIPTOR(0);
  76. #define FEATURE_UNIT_ID 2
  77. static struct uac_feature_unit_descriptor_0 feature_unit_desc = {
  78. .bLength = UAC_DT_FEATURE_UNIT_SIZE(0),
  79. .bDescriptorType = USB_DT_CS_INTERFACE,
  80. .bDescriptorSubtype = UAC_FEATURE_UNIT,
  81. .bUnitID = FEATURE_UNIT_ID,
  82. .bSourceID = INPUT_TERMINAL_ID,
  83. .bControlSize = 2,
  84. .bmaControls[0] = (UAC_FU_MUTE | UAC_FU_VOLUME),
  85. };
  86. static struct usb_audio_control mute_control = {
  87. .list = LIST_HEAD_INIT(mute_control.list),
  88. .name = "Mute Control",
  89. .type = UAC_FU_MUTE,
  90. /* Todo: add real Mute control code */
  91. .set = generic_set_cmd,
  92. .get = generic_get_cmd,
  93. };
  94. static struct usb_audio_control volume_control = {
  95. .list = LIST_HEAD_INIT(volume_control.list),
  96. .name = "Volume Control",
  97. .type = UAC_FU_VOLUME,
  98. /* Todo: add real Volume control code */
  99. .set = generic_set_cmd,
  100. .get = generic_get_cmd,
  101. };
  102. static struct usb_audio_control_selector feature_unit = {
  103. .list = LIST_HEAD_INIT(feature_unit.list),
  104. .id = FEATURE_UNIT_ID,
  105. .name = "Mute & Volume Control",
  106. .type = UAC_FEATURE_UNIT,
  107. .desc = (struct usb_descriptor_header *)&feature_unit_desc,
  108. };
  109. #define OUTPUT_TERMINAL_ID 3
  110. static struct uac1_output_terminal_descriptor output_terminal_desc = {
  111. .bLength = UAC_DT_OUTPUT_TERMINAL_SIZE,
  112. .bDescriptorType = USB_DT_CS_INTERFACE,
  113. .bDescriptorSubtype = UAC_OUTPUT_TERMINAL,
  114. .bTerminalID = OUTPUT_TERMINAL_ID,
  115. .wTerminalType = UAC_OUTPUT_TERMINAL_SPEAKER,
  116. .bAssocTerminal = FEATURE_UNIT_ID,
  117. .bSourceID = FEATURE_UNIT_ID,
  118. };
  119. /* B.4.1 Standard AS Interface Descriptor */
  120. static struct usb_interface_descriptor as_interface_alt_0_desc = {
  121. .bLength = USB_DT_INTERFACE_SIZE,
  122. .bDescriptorType = USB_DT_INTERFACE,
  123. .bAlternateSetting = 0,
  124. .bNumEndpoints = 0,
  125. .bInterfaceClass = USB_CLASS_AUDIO,
  126. .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
  127. };
  128. static struct usb_interface_descriptor as_interface_alt_1_desc = {
  129. .bLength = USB_DT_INTERFACE_SIZE,
  130. .bDescriptorType = USB_DT_INTERFACE,
  131. .bAlternateSetting = 1,
  132. .bNumEndpoints = 1,
  133. .bInterfaceClass = USB_CLASS_AUDIO,
  134. .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
  135. };
  136. /* B.4.2 Class-Specific AS Interface Descriptor */
  137. static struct uac1_as_header_descriptor as_header_desc = {
  138. .bLength = UAC_DT_AS_HEADER_SIZE,
  139. .bDescriptorType = USB_DT_CS_INTERFACE,
  140. .bDescriptorSubtype = UAC_AS_GENERAL,
  141. .bTerminalLink = INPUT_TERMINAL_ID,
  142. .bDelay = 1,
  143. .wFormatTag = UAC_FORMAT_TYPE_I_PCM,
  144. };
  145. DECLARE_UAC_FORMAT_TYPE_I_DISCRETE_DESC(1);
  146. static struct uac_format_type_i_discrete_descriptor_1 as_type_i_desc = {
  147. .bLength = UAC_FORMAT_TYPE_I_DISCRETE_DESC_SIZE(1),
  148. .bDescriptorType = USB_DT_CS_INTERFACE,
  149. .bDescriptorSubtype = UAC_FORMAT_TYPE,
  150. .bFormatType = UAC_FORMAT_TYPE_I,
  151. .bSubframeSize = 2,
  152. .bBitResolution = 16,
  153. .bSamFreqType = 1,
  154. };
  155. /* Standard ISO OUT Endpoint Descriptor */
  156. static struct usb_endpoint_descriptor as_out_ep_desc = {
  157. .bLength = USB_DT_ENDPOINT_AUDIO_SIZE,
  158. .bDescriptorType = USB_DT_ENDPOINT,
  159. .bEndpointAddress = USB_DIR_OUT,
  160. .bmAttributes = USB_ENDPOINT_SYNC_ADAPTIVE
  161. | USB_ENDPOINT_XFER_ISOC,
  162. .wMaxPacketSize = __constant_cpu_to_le16(OUT_EP_MAX_PACKET_SIZE),
  163. .bInterval = 4,
  164. };
  165. /* Class-specific AS ISO OUT Endpoint Descriptor */
  166. static struct uac_iso_endpoint_descriptor as_iso_out_desc __initdata = {
  167. .bLength = UAC_ISO_ENDPOINT_DESC_SIZE,
  168. .bDescriptorType = USB_DT_CS_ENDPOINT,
  169. .bDescriptorSubtype = UAC_EP_GENERAL,
  170. .bmAttributes = 1,
  171. .bLockDelayUnits = 1,
  172. .wLockDelay = __constant_cpu_to_le16(1),
  173. };
  174. static struct usb_descriptor_header *f_audio_desc[] __initdata = {
  175. (struct usb_descriptor_header *)&ac_interface_desc,
  176. (struct usb_descriptor_header *)&ac_header_desc,
  177. (struct usb_descriptor_header *)&input_terminal_desc,
  178. (struct usb_descriptor_header *)&output_terminal_desc,
  179. (struct usb_descriptor_header *)&feature_unit_desc,
  180. (struct usb_descriptor_header *)&as_interface_alt_0_desc,
  181. (struct usb_descriptor_header *)&as_interface_alt_1_desc,
  182. (struct usb_descriptor_header *)&as_header_desc,
  183. (struct usb_descriptor_header *)&as_type_i_desc,
  184. (struct usb_descriptor_header *)&as_out_ep_desc,
  185. (struct usb_descriptor_header *)&as_iso_out_desc,
  186. NULL,
  187. };
  188. /*
  189. * This function is an ALSA sound card following USB Audio Class Spec 1.0.
  190. */
  191. /*-------------------------------------------------------------------------*/
  192. struct f_audio_buf {
  193. u8 *buf;
  194. int actual;
  195. struct list_head list;
  196. };
  197. static struct f_audio_buf *f_audio_buffer_alloc(int buf_size)
  198. {
  199. struct f_audio_buf *copy_buf;
  200. copy_buf = kzalloc(sizeof *copy_buf, GFP_ATOMIC);
  201. if (!copy_buf)
  202. return ERR_PTR(-ENOMEM);
  203. copy_buf->buf = kzalloc(buf_size, GFP_ATOMIC);
  204. if (!copy_buf->buf) {
  205. kfree(copy_buf);
  206. return ERR_PTR(-ENOMEM);
  207. }
  208. return copy_buf;
  209. }
  210. static void f_audio_buffer_free(struct f_audio_buf *audio_buf)
  211. {
  212. kfree(audio_buf->buf);
  213. kfree(audio_buf);
  214. }
  215. /*-------------------------------------------------------------------------*/
  216. struct f_audio {
  217. struct gaudio card;
  218. /* endpoints handle full and/or high speeds */
  219. struct usb_ep *out_ep;
  220. spinlock_t lock;
  221. struct f_audio_buf *copy_buf;
  222. struct work_struct playback_work;
  223. struct list_head play_queue;
  224. /* Control Set command */
  225. struct list_head cs;
  226. u8 set_cmd;
  227. struct usb_audio_control *set_con;
  228. };
  229. static inline struct f_audio *func_to_audio(struct usb_function *f)
  230. {
  231. return container_of(f, struct f_audio, card.func);
  232. }
  233. /*-------------------------------------------------------------------------*/
  234. static void f_audio_playback_work(struct work_struct *data)
  235. {
  236. struct f_audio *audio = container_of(data, struct f_audio,
  237. playback_work);
  238. struct f_audio_buf *play_buf;
  239. spin_lock_irq(&audio->lock);
  240. if (list_empty(&audio->play_queue)) {
  241. spin_unlock_irq(&audio->lock);
  242. return;
  243. }
  244. play_buf = list_first_entry(&audio->play_queue,
  245. struct f_audio_buf, list);
  246. list_del(&play_buf->list);
  247. spin_unlock_irq(&audio->lock);
  248. u_audio_playback(&audio->card, play_buf->buf, play_buf->actual);
  249. f_audio_buffer_free(play_buf);
  250. }
  251. static int f_audio_out_ep_complete(struct usb_ep *ep, struct usb_request *req)
  252. {
  253. struct f_audio *audio = req->context;
  254. struct usb_composite_dev *cdev = audio->card.func.config->cdev;
  255. struct f_audio_buf *copy_buf = audio->copy_buf;
  256. int err;
  257. if (!copy_buf)
  258. return -EINVAL;
  259. /* Copy buffer is full, add it to the play_queue */
  260. if (audio_buf_size - copy_buf->actual < req->actual) {
  261. list_add_tail(&copy_buf->list, &audio->play_queue);
  262. schedule_work(&audio->playback_work);
  263. copy_buf = f_audio_buffer_alloc(audio_buf_size);
  264. if (IS_ERR(copy_buf))
  265. return -ENOMEM;
  266. }
  267. memcpy(copy_buf->buf + copy_buf->actual, req->buf, req->actual);
  268. copy_buf->actual += req->actual;
  269. audio->copy_buf = copy_buf;
  270. err = usb_ep_queue(ep, req, GFP_ATOMIC);
  271. if (err)
  272. ERROR(cdev, "%s queue req: %d\n", ep->name, err);
  273. return 0;
  274. }
  275. static void f_audio_complete(struct usb_ep *ep, struct usb_request *req)
  276. {
  277. struct f_audio *audio = req->context;
  278. int status = req->status;
  279. u32 data = 0;
  280. struct usb_ep *out_ep = audio->out_ep;
  281. switch (status) {
  282. case 0: /* normal completion? */
  283. if (ep == out_ep)
  284. f_audio_out_ep_complete(ep, req);
  285. else if (audio->set_con) {
  286. memcpy(&data, req->buf, req->length);
  287. audio->set_con->set(audio->set_con, audio->set_cmd,
  288. le16_to_cpu(data));
  289. audio->set_con = NULL;
  290. }
  291. break;
  292. default:
  293. break;
  294. }
  295. }
  296. static int audio_set_intf_req(struct usb_function *f,
  297. const struct usb_ctrlrequest *ctrl)
  298. {
  299. struct f_audio *audio = func_to_audio(f);
  300. struct usb_composite_dev *cdev = f->config->cdev;
  301. struct usb_request *req = cdev->req;
  302. u8 id = ((le16_to_cpu(ctrl->wIndex) >> 8) & 0xFF);
  303. u16 len = le16_to_cpu(ctrl->wLength);
  304. u16 w_value = le16_to_cpu(ctrl->wValue);
  305. u8 con_sel = (w_value >> 8) & 0xFF;
  306. u8 cmd = (ctrl->bRequest & 0x0F);
  307. struct usb_audio_control_selector *cs;
  308. struct usb_audio_control *con;
  309. DBG(cdev, "bRequest 0x%x, w_value 0x%04x, len %d, entity %d\n",
  310. ctrl->bRequest, w_value, len, id);
  311. list_for_each_entry(cs, &audio->cs, list) {
  312. if (cs->id == id) {
  313. list_for_each_entry(con, &cs->control, list) {
  314. if (con->type == con_sel) {
  315. audio->set_con = con;
  316. break;
  317. }
  318. }
  319. break;
  320. }
  321. }
  322. audio->set_cmd = cmd;
  323. req->context = audio;
  324. req->complete = f_audio_complete;
  325. return len;
  326. }
  327. static int audio_get_intf_req(struct usb_function *f,
  328. const struct usb_ctrlrequest *ctrl)
  329. {
  330. struct f_audio *audio = func_to_audio(f);
  331. struct usb_composite_dev *cdev = f->config->cdev;
  332. struct usb_request *req = cdev->req;
  333. int value = -EOPNOTSUPP;
  334. u8 id = ((le16_to_cpu(ctrl->wIndex) >> 8) & 0xFF);
  335. u16 len = le16_to_cpu(ctrl->wLength);
  336. u16 w_value = le16_to_cpu(ctrl->wValue);
  337. u8 con_sel = (w_value >> 8) & 0xFF;
  338. u8 cmd = (ctrl->bRequest & 0x0F);
  339. struct usb_audio_control_selector *cs;
  340. struct usb_audio_control *con;
  341. DBG(cdev, "bRequest 0x%x, w_value 0x%04x, len %d, entity %d\n",
  342. ctrl->bRequest, w_value, len, id);
  343. list_for_each_entry(cs, &audio->cs, list) {
  344. if (cs->id == id) {
  345. list_for_each_entry(con, &cs->control, list) {
  346. if (con->type == con_sel && con->get) {
  347. value = con->get(con, cmd);
  348. break;
  349. }
  350. }
  351. break;
  352. }
  353. }
  354. req->context = audio;
  355. req->complete = f_audio_complete;
  356. memcpy(req->buf, &value, len);
  357. return len;
  358. }
  359. static int audio_set_endpoint_req(struct usb_function *f,
  360. const struct usb_ctrlrequest *ctrl)
  361. {
  362. struct usb_composite_dev *cdev = f->config->cdev;
  363. int value = -EOPNOTSUPP;
  364. u16 ep = le16_to_cpu(ctrl->wIndex);
  365. u16 len = le16_to_cpu(ctrl->wLength);
  366. u16 w_value = le16_to_cpu(ctrl->wValue);
  367. DBG(cdev, "bRequest 0x%x, w_value 0x%04x, len %d, endpoint %d\n",
  368. ctrl->bRequest, w_value, len, ep);
  369. switch (ctrl->bRequest) {
  370. case UAC_SET_CUR:
  371. value = len;
  372. break;
  373. case UAC_SET_MIN:
  374. break;
  375. case UAC_SET_MAX:
  376. break;
  377. case UAC_SET_RES:
  378. break;
  379. case UAC_SET_MEM:
  380. break;
  381. default:
  382. break;
  383. }
  384. return value;
  385. }
  386. static int audio_get_endpoint_req(struct usb_function *f,
  387. const struct usb_ctrlrequest *ctrl)
  388. {
  389. struct usb_composite_dev *cdev = f->config->cdev;
  390. int value = -EOPNOTSUPP;
  391. u8 ep = ((le16_to_cpu(ctrl->wIndex) >> 8) & 0xFF);
  392. u16 len = le16_to_cpu(ctrl->wLength);
  393. u16 w_value = le16_to_cpu(ctrl->wValue);
  394. DBG(cdev, "bRequest 0x%x, w_value 0x%04x, len %d, endpoint %d\n",
  395. ctrl->bRequest, w_value, len, ep);
  396. switch (ctrl->bRequest) {
  397. case UAC_GET_CUR:
  398. case UAC_GET_MIN:
  399. case UAC_GET_MAX:
  400. case UAC_GET_RES:
  401. value = len;
  402. break;
  403. case UAC_GET_MEM:
  404. break;
  405. default:
  406. break;
  407. }
  408. return value;
  409. }
  410. static int
  411. f_audio_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
  412. {
  413. struct usb_composite_dev *cdev = f->config->cdev;
  414. struct usb_request *req = cdev->req;
  415. int value = -EOPNOTSUPP;
  416. u16 w_index = le16_to_cpu(ctrl->wIndex);
  417. u16 w_value = le16_to_cpu(ctrl->wValue);
  418. u16 w_length = le16_to_cpu(ctrl->wLength);
  419. /* composite driver infrastructure handles everything; interface
  420. * activation uses set_alt().
  421. */
  422. switch (ctrl->bRequestType) {
  423. case USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE:
  424. value = audio_set_intf_req(f, ctrl);
  425. break;
  426. case USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE:
  427. value = audio_get_intf_req(f, ctrl);
  428. break;
  429. case USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_ENDPOINT:
  430. value = audio_set_endpoint_req(f, ctrl);
  431. break;
  432. case USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT:
  433. value = audio_get_endpoint_req(f, ctrl);
  434. break;
  435. default:
  436. ERROR(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
  437. ctrl->bRequestType, ctrl->bRequest,
  438. w_value, w_index, w_length);
  439. }
  440. /* respond with data transfer or status phase? */
  441. if (value >= 0) {
  442. DBG(cdev, "audio req%02x.%02x v%04x i%04x l%d\n",
  443. ctrl->bRequestType, ctrl->bRequest,
  444. w_value, w_index, w_length);
  445. req->zero = 0;
  446. req->length = value;
  447. value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
  448. if (value < 0)
  449. ERROR(cdev, "audio response on err %d\n", value);
  450. }
  451. /* device either stalls (value < 0) or reports success */
  452. return value;
  453. }
  454. static int f_audio_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
  455. {
  456. struct f_audio *audio = func_to_audio(f);
  457. struct usb_composite_dev *cdev = f->config->cdev;
  458. struct usb_ep *out_ep = audio->out_ep;
  459. struct usb_request *req;
  460. int i = 0, err = 0;
  461. DBG(cdev, "intf %d, alt %d\n", intf, alt);
  462. if (intf == 1) {
  463. if (alt == 1) {
  464. usb_ep_enable(out_ep);
  465. out_ep->driver_data = audio;
  466. audio->copy_buf = f_audio_buffer_alloc(audio_buf_size);
  467. if (IS_ERR(audio->copy_buf))
  468. return -ENOMEM;
  469. /*
  470. * allocate a bunch of read buffers
  471. * and queue them all at once.
  472. */
  473. for (i = 0; i < req_count && err == 0; i++) {
  474. req = usb_ep_alloc_request(out_ep, GFP_ATOMIC);
  475. if (req) {
  476. req->buf = kzalloc(req_buf_size,
  477. GFP_ATOMIC);
  478. if (req->buf) {
  479. req->length = req_buf_size;
  480. req->context = audio;
  481. req->complete =
  482. f_audio_complete;
  483. err = usb_ep_queue(out_ep,
  484. req, GFP_ATOMIC);
  485. if (err)
  486. ERROR(cdev,
  487. "%s queue req: %d\n",
  488. out_ep->name, err);
  489. } else
  490. err = -ENOMEM;
  491. } else
  492. err = -ENOMEM;
  493. }
  494. } else {
  495. struct f_audio_buf *copy_buf = audio->copy_buf;
  496. if (copy_buf) {
  497. list_add_tail(&copy_buf->list,
  498. &audio->play_queue);
  499. schedule_work(&audio->playback_work);
  500. }
  501. }
  502. }
  503. return err;
  504. }
  505. static void f_audio_disable(struct usb_function *f)
  506. {
  507. return;
  508. }
  509. /*-------------------------------------------------------------------------*/
  510. static void f_audio_build_desc(struct f_audio *audio)
  511. {
  512. struct gaudio *card = &audio->card;
  513. u8 *sam_freq;
  514. int rate;
  515. /* Set channel numbers */
  516. input_terminal_desc.bNrChannels = u_audio_get_playback_channels(card);
  517. as_type_i_desc.bNrChannels = u_audio_get_playback_channels(card);
  518. /* Set sample rates */
  519. rate = u_audio_get_playback_rate(card);
  520. sam_freq = as_type_i_desc.tSamFreq[0];
  521. memcpy(sam_freq, &rate, 3);
  522. /* Todo: Set Sample bits and other parameters */
  523. return;
  524. }
  525. /* audio function driver setup/binding */
  526. static int __init
  527. f_audio_bind(struct usb_configuration *c, struct usb_function *f)
  528. {
  529. struct usb_composite_dev *cdev = c->cdev;
  530. struct f_audio *audio = func_to_audio(f);
  531. int status;
  532. struct usb_ep *ep;
  533. f_audio_build_desc(audio);
  534. /* allocate instance-specific interface IDs, and patch descriptors */
  535. status = usb_interface_id(c, f);
  536. if (status < 0)
  537. goto fail;
  538. ac_interface_desc.bInterfaceNumber = status;
  539. status = usb_interface_id(c, f);
  540. if (status < 0)
  541. goto fail;
  542. as_interface_alt_0_desc.bInterfaceNumber = status;
  543. as_interface_alt_1_desc.bInterfaceNumber = status;
  544. status = -ENODEV;
  545. /* allocate instance-specific endpoints */
  546. ep = usb_ep_autoconfig(cdev->gadget, &as_out_ep_desc);
  547. if (!ep)
  548. goto fail;
  549. audio->out_ep = ep;
  550. audio->out_ep->desc = &as_out_ep_desc;
  551. ep->driver_data = cdev; /* claim */
  552. status = -ENOMEM;
  553. /* copy descriptors, and track endpoint copies */
  554. f->descriptors = usb_copy_descriptors(f_audio_desc);
  555. /*
  556. * support all relevant hardware speeds... we expect that when
  557. * hardware is dual speed, all bulk-capable endpoints work at
  558. * both speeds
  559. */
  560. if (gadget_is_dualspeed(c->cdev->gadget)) {
  561. c->highspeed = true;
  562. f->hs_descriptors = usb_copy_descriptors(f_audio_desc);
  563. }
  564. return 0;
  565. fail:
  566. return status;
  567. }
  568. static void
  569. f_audio_unbind(struct usb_configuration *c, struct usb_function *f)
  570. {
  571. struct f_audio *audio = func_to_audio(f);
  572. usb_free_descriptors(f->descriptors);
  573. usb_free_descriptors(f->hs_descriptors);
  574. kfree(audio);
  575. }
  576. /*-------------------------------------------------------------------------*/
  577. static int generic_set_cmd(struct usb_audio_control *con, u8 cmd, int value)
  578. {
  579. con->data[cmd] = value;
  580. return 0;
  581. }
  582. static int generic_get_cmd(struct usb_audio_control *con, u8 cmd)
  583. {
  584. return con->data[cmd];
  585. }
  586. /* Todo: add more control selecotor dynamically */
  587. int __init control_selector_init(struct f_audio *audio)
  588. {
  589. INIT_LIST_HEAD(&audio->cs);
  590. list_add(&feature_unit.list, &audio->cs);
  591. INIT_LIST_HEAD(&feature_unit.control);
  592. list_add(&mute_control.list, &feature_unit.control);
  593. list_add(&volume_control.list, &feature_unit.control);
  594. volume_control.data[UAC__CUR] = 0xffc0;
  595. volume_control.data[UAC__MIN] = 0xe3a0;
  596. volume_control.data[UAC__MAX] = 0xfff0;
  597. volume_control.data[UAC__RES] = 0x0030;
  598. return 0;
  599. }
  600. /**
  601. * audio_bind_config - add USB audio function to a configuration
  602. * @c: the configuration to supcard the USB audio function
  603. * Context: single threaded during gadget setup
  604. *
  605. * Returns zero on success, else negative errno.
  606. */
  607. int __init audio_bind_config(struct usb_configuration *c)
  608. {
  609. struct f_audio *audio;
  610. int status;
  611. /* allocate and initialize one new instance */
  612. audio = kzalloc(sizeof *audio, GFP_KERNEL);
  613. if (!audio)
  614. return -ENOMEM;
  615. audio->card.func.name = "g_audio";
  616. audio->card.gadget = c->cdev->gadget;
  617. INIT_LIST_HEAD(&audio->play_queue);
  618. spin_lock_init(&audio->lock);
  619. /* set up ASLA audio devices */
  620. status = gaudio_setup(&audio->card);
  621. if (status < 0)
  622. goto setup_fail;
  623. audio->card.func.strings = audio_strings;
  624. audio->card.func.bind = f_audio_bind;
  625. audio->card.func.unbind = f_audio_unbind;
  626. audio->card.func.set_alt = f_audio_set_alt;
  627. audio->card.func.setup = f_audio_setup;
  628. audio->card.func.disable = f_audio_disable;
  629. control_selector_init(audio);
  630. INIT_WORK(&audio->playback_work, f_audio_playback_work);
  631. status = usb_add_function(c, &audio->card.func);
  632. if (status)
  633. goto add_fail;
  634. INFO(c->cdev, "audio_buf_size %d, req_buf_size %d, req_count %d\n",
  635. audio_buf_size, req_buf_size, req_count);
  636. return status;
  637. add_fail:
  638. gaudio_cleanup();
  639. setup_fail:
  640. kfree(audio);
  641. return status;
  642. }