f_audio.c 20 KB

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