f_audio.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  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_audio.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. /* string IDs are assigned dynamically */
  189. #define STRING_MANUFACTURER_IDX 0
  190. #define STRING_PRODUCT_IDX 1
  191. static char manufacturer[50];
  192. static struct usb_string strings_dev[] = {
  193. [STRING_MANUFACTURER_IDX].s = manufacturer,
  194. [STRING_PRODUCT_IDX].s = DRIVER_DESC,
  195. { } /* end of list */
  196. };
  197. static struct usb_gadget_strings stringtab_dev = {
  198. .language = 0x0409, /* en-us */
  199. .strings = strings_dev,
  200. };
  201. static struct usb_gadget_strings *audio_strings[] = {
  202. &stringtab_dev,
  203. NULL,
  204. };
  205. /*
  206. * This function is an ALSA sound card following USB Audio Class Spec 1.0.
  207. */
  208. /*-------------------------------------------------------------------------*/
  209. struct f_audio_buf {
  210. u8 *buf;
  211. int actual;
  212. struct list_head list;
  213. };
  214. static struct f_audio_buf *f_audio_buffer_alloc(int buf_size)
  215. {
  216. struct f_audio_buf *copy_buf;
  217. copy_buf = kzalloc(sizeof *copy_buf, GFP_ATOMIC);
  218. if (!copy_buf)
  219. return ERR_PTR(-ENOMEM);
  220. copy_buf->buf = kzalloc(buf_size, GFP_ATOMIC);
  221. if (!copy_buf->buf) {
  222. kfree(copy_buf);
  223. return ERR_PTR(-ENOMEM);
  224. }
  225. return copy_buf;
  226. }
  227. static void f_audio_buffer_free(struct f_audio_buf *audio_buf)
  228. {
  229. kfree(audio_buf->buf);
  230. kfree(audio_buf);
  231. }
  232. /*-------------------------------------------------------------------------*/
  233. struct f_audio {
  234. struct gaudio card;
  235. /* endpoints handle full and/or high speeds */
  236. struct usb_ep *out_ep;
  237. spinlock_t lock;
  238. struct f_audio_buf *copy_buf;
  239. struct work_struct playback_work;
  240. struct list_head play_queue;
  241. /* Control Set command */
  242. struct list_head cs;
  243. u8 set_cmd;
  244. struct usb_audio_control *set_con;
  245. };
  246. static inline struct f_audio *func_to_audio(struct usb_function *f)
  247. {
  248. return container_of(f, struct f_audio, card.func);
  249. }
  250. /*-------------------------------------------------------------------------*/
  251. static void f_audio_playback_work(struct work_struct *data)
  252. {
  253. struct f_audio *audio = container_of(data, struct f_audio,
  254. playback_work);
  255. struct f_audio_buf *play_buf;
  256. spin_lock_irq(&audio->lock);
  257. if (list_empty(&audio->play_queue)) {
  258. spin_unlock_irq(&audio->lock);
  259. return;
  260. }
  261. play_buf = list_first_entry(&audio->play_queue,
  262. struct f_audio_buf, list);
  263. list_del(&play_buf->list);
  264. spin_unlock_irq(&audio->lock);
  265. u_audio_playback(&audio->card, play_buf->buf, play_buf->actual);
  266. f_audio_buffer_free(play_buf);
  267. }
  268. static int f_audio_out_ep_complete(struct usb_ep *ep, struct usb_request *req)
  269. {
  270. struct f_audio *audio = req->context;
  271. struct usb_composite_dev *cdev = audio->card.func.config->cdev;
  272. struct f_audio_buf *copy_buf = audio->copy_buf;
  273. int err;
  274. if (!copy_buf)
  275. return -EINVAL;
  276. /* Copy buffer is full, add it to the play_queue */
  277. if (audio_buf_size - copy_buf->actual < req->actual) {
  278. list_add_tail(&copy_buf->list, &audio->play_queue);
  279. schedule_work(&audio->playback_work);
  280. copy_buf = f_audio_buffer_alloc(audio_buf_size);
  281. if (IS_ERR(copy_buf))
  282. return -ENOMEM;
  283. }
  284. memcpy(copy_buf->buf + copy_buf->actual, req->buf, req->actual);
  285. copy_buf->actual += req->actual;
  286. audio->copy_buf = copy_buf;
  287. err = usb_ep_queue(ep, req, GFP_ATOMIC);
  288. if (err)
  289. ERROR(cdev, "%s queue req: %d\n", ep->name, err);
  290. return 0;
  291. }
  292. static void f_audio_complete(struct usb_ep *ep, struct usb_request *req)
  293. {
  294. struct f_audio *audio = req->context;
  295. int status = req->status;
  296. u32 data = 0;
  297. struct usb_ep *out_ep = audio->out_ep;
  298. switch (status) {
  299. case 0: /* normal completion? */
  300. if (ep == out_ep)
  301. f_audio_out_ep_complete(ep, req);
  302. else if (audio->set_con) {
  303. memcpy(&data, req->buf, req->length);
  304. audio->set_con->set(audio->set_con, audio->set_cmd,
  305. le16_to_cpu(data));
  306. audio->set_con = NULL;
  307. }
  308. break;
  309. default:
  310. break;
  311. }
  312. }
  313. static int audio_set_intf_req(struct usb_function *f,
  314. const struct usb_ctrlrequest *ctrl)
  315. {
  316. struct f_audio *audio = func_to_audio(f);
  317. struct usb_composite_dev *cdev = f->config->cdev;
  318. struct usb_request *req = cdev->req;
  319. u8 id = ((le16_to_cpu(ctrl->wIndex) >> 8) & 0xFF);
  320. u16 len = le16_to_cpu(ctrl->wLength);
  321. u16 w_value = le16_to_cpu(ctrl->wValue);
  322. u8 con_sel = (w_value >> 8) & 0xFF;
  323. u8 cmd = (ctrl->bRequest & 0x0F);
  324. struct usb_audio_control_selector *cs;
  325. struct usb_audio_control *con;
  326. DBG(cdev, "bRequest 0x%x, w_value 0x%04x, len %d, entity %d\n",
  327. ctrl->bRequest, w_value, len, id);
  328. list_for_each_entry(cs, &audio->cs, list) {
  329. if (cs->id == id) {
  330. list_for_each_entry(con, &cs->control, list) {
  331. if (con->type == con_sel) {
  332. audio->set_con = con;
  333. break;
  334. }
  335. }
  336. break;
  337. }
  338. }
  339. audio->set_cmd = cmd;
  340. req->context = audio;
  341. req->complete = f_audio_complete;
  342. return len;
  343. }
  344. static int audio_get_intf_req(struct usb_function *f,
  345. const struct usb_ctrlrequest *ctrl)
  346. {
  347. struct f_audio *audio = func_to_audio(f);
  348. struct usb_composite_dev *cdev = f->config->cdev;
  349. struct usb_request *req = cdev->req;
  350. int value = -EOPNOTSUPP;
  351. u8 id = ((le16_to_cpu(ctrl->wIndex) >> 8) & 0xFF);
  352. u16 len = le16_to_cpu(ctrl->wLength);
  353. u16 w_value = le16_to_cpu(ctrl->wValue);
  354. u8 con_sel = (w_value >> 8) & 0xFF;
  355. u8 cmd = (ctrl->bRequest & 0x0F);
  356. struct usb_audio_control_selector *cs;
  357. struct usb_audio_control *con;
  358. DBG(cdev, "bRequest 0x%x, w_value 0x%04x, len %d, entity %d\n",
  359. ctrl->bRequest, w_value, len, id);
  360. list_for_each_entry(cs, &audio->cs, list) {
  361. if (cs->id == id) {
  362. list_for_each_entry(con, &cs->control, list) {
  363. if (con->type == con_sel && con->get) {
  364. value = con->get(con, cmd);
  365. break;
  366. }
  367. }
  368. break;
  369. }
  370. }
  371. req->context = audio;
  372. req->complete = f_audio_complete;
  373. memcpy(req->buf, &value, len);
  374. return len;
  375. }
  376. static int audio_set_endpoint_req(struct usb_function *f,
  377. const struct usb_ctrlrequest *ctrl)
  378. {
  379. struct usb_composite_dev *cdev = f->config->cdev;
  380. int value = -EOPNOTSUPP;
  381. u16 ep = le16_to_cpu(ctrl->wIndex);
  382. u16 len = le16_to_cpu(ctrl->wLength);
  383. u16 w_value = le16_to_cpu(ctrl->wValue);
  384. DBG(cdev, "bRequest 0x%x, w_value 0x%04x, len %d, endpoint %d\n",
  385. ctrl->bRequest, w_value, len, ep);
  386. switch (ctrl->bRequest) {
  387. case UAC_SET_CUR:
  388. value = len;
  389. break;
  390. case UAC_SET_MIN:
  391. break;
  392. case UAC_SET_MAX:
  393. break;
  394. case UAC_SET_RES:
  395. break;
  396. case UAC_SET_MEM:
  397. break;
  398. default:
  399. break;
  400. }
  401. return value;
  402. }
  403. static int audio_get_endpoint_req(struct usb_function *f,
  404. const struct usb_ctrlrequest *ctrl)
  405. {
  406. struct usb_composite_dev *cdev = f->config->cdev;
  407. int value = -EOPNOTSUPP;
  408. u8 ep = ((le16_to_cpu(ctrl->wIndex) >> 8) & 0xFF);
  409. u16 len = le16_to_cpu(ctrl->wLength);
  410. u16 w_value = le16_to_cpu(ctrl->wValue);
  411. DBG(cdev, "bRequest 0x%x, w_value 0x%04x, len %d, endpoint %d\n",
  412. ctrl->bRequest, w_value, len, ep);
  413. switch (ctrl->bRequest) {
  414. case UAC_GET_CUR:
  415. case UAC_GET_MIN:
  416. case UAC_GET_MAX:
  417. case UAC_GET_RES:
  418. value = len;
  419. break;
  420. case UAC_GET_MEM:
  421. break;
  422. default:
  423. break;
  424. }
  425. return value;
  426. }
  427. static int
  428. f_audio_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
  429. {
  430. struct usb_composite_dev *cdev = f->config->cdev;
  431. struct usb_request *req = cdev->req;
  432. int value = -EOPNOTSUPP;
  433. u16 w_index = le16_to_cpu(ctrl->wIndex);
  434. u16 w_value = le16_to_cpu(ctrl->wValue);
  435. u16 w_length = le16_to_cpu(ctrl->wLength);
  436. /* composite driver infrastructure handles everything; interface
  437. * activation uses set_alt().
  438. */
  439. switch (ctrl->bRequestType) {
  440. case USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE:
  441. value = audio_set_intf_req(f, ctrl);
  442. break;
  443. case USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE:
  444. value = audio_get_intf_req(f, ctrl);
  445. break;
  446. case USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_ENDPOINT:
  447. value = audio_set_endpoint_req(f, ctrl);
  448. break;
  449. case USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT:
  450. value = audio_get_endpoint_req(f, ctrl);
  451. break;
  452. default:
  453. ERROR(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
  454. ctrl->bRequestType, ctrl->bRequest,
  455. w_value, w_index, w_length);
  456. }
  457. /* respond with data transfer or status phase? */
  458. if (value >= 0) {
  459. DBG(cdev, "audio req%02x.%02x v%04x i%04x l%d\n",
  460. ctrl->bRequestType, ctrl->bRequest,
  461. w_value, w_index, w_length);
  462. req->zero = 0;
  463. req->length = value;
  464. value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
  465. if (value < 0)
  466. ERROR(cdev, "audio response on err %d\n", value);
  467. }
  468. /* device either stalls (value < 0) or reports success */
  469. return value;
  470. }
  471. static int f_audio_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
  472. {
  473. struct f_audio *audio = func_to_audio(f);
  474. struct usb_composite_dev *cdev = f->config->cdev;
  475. struct usb_ep *out_ep = audio->out_ep;
  476. struct usb_request *req;
  477. int i = 0, err = 0;
  478. DBG(cdev, "intf %d, alt %d\n", intf, alt);
  479. if (intf == 1) {
  480. if (alt == 1) {
  481. usb_ep_enable(out_ep);
  482. out_ep->driver_data = audio;
  483. audio->copy_buf = f_audio_buffer_alloc(audio_buf_size);
  484. if (IS_ERR(audio->copy_buf))
  485. return -ENOMEM;
  486. /*
  487. * allocate a bunch of read buffers
  488. * and queue them all at once.
  489. */
  490. for (i = 0; i < req_count && err == 0; i++) {
  491. req = usb_ep_alloc_request(out_ep, GFP_ATOMIC);
  492. if (req) {
  493. req->buf = kzalloc(req_buf_size,
  494. GFP_ATOMIC);
  495. if (req->buf) {
  496. req->length = req_buf_size;
  497. req->context = audio;
  498. req->complete =
  499. f_audio_complete;
  500. err = usb_ep_queue(out_ep,
  501. req, GFP_ATOMIC);
  502. if (err)
  503. ERROR(cdev,
  504. "%s queue req: %d\n",
  505. out_ep->name, err);
  506. } else
  507. err = -ENOMEM;
  508. } else
  509. err = -ENOMEM;
  510. }
  511. } else {
  512. struct f_audio_buf *copy_buf = audio->copy_buf;
  513. if (copy_buf) {
  514. list_add_tail(&copy_buf->list,
  515. &audio->play_queue);
  516. schedule_work(&audio->playback_work);
  517. }
  518. }
  519. }
  520. return err;
  521. }
  522. static void f_audio_disable(struct usb_function *f)
  523. {
  524. return;
  525. }
  526. /*-------------------------------------------------------------------------*/
  527. static void f_audio_build_desc(struct f_audio *audio)
  528. {
  529. struct gaudio *card = &audio->card;
  530. u8 *sam_freq;
  531. int rate;
  532. /* Set channel numbers */
  533. input_terminal_desc.bNrChannels = u_audio_get_playback_channels(card);
  534. as_type_i_desc.bNrChannels = u_audio_get_playback_channels(card);
  535. /* Set sample rates */
  536. rate = u_audio_get_playback_rate(card);
  537. sam_freq = as_type_i_desc.tSamFreq[0];
  538. memcpy(sam_freq, &rate, 3);
  539. /* Todo: Set Sample bits and other parameters */
  540. return;
  541. }
  542. /* audio function driver setup/binding */
  543. static int __init
  544. f_audio_bind(struct usb_configuration *c, struct usb_function *f)
  545. {
  546. struct usb_composite_dev *cdev = c->cdev;
  547. struct f_audio *audio = func_to_audio(f);
  548. int status;
  549. struct usb_ep *ep;
  550. f_audio_build_desc(audio);
  551. /* allocate instance-specific interface IDs, and patch descriptors */
  552. status = usb_interface_id(c, f);
  553. if (status < 0)
  554. goto fail;
  555. ac_interface_desc.bInterfaceNumber = status;
  556. status = usb_interface_id(c, f);
  557. if (status < 0)
  558. goto fail;
  559. as_interface_alt_0_desc.bInterfaceNumber = status;
  560. as_interface_alt_1_desc.bInterfaceNumber = status;
  561. status = -ENODEV;
  562. /* allocate instance-specific endpoints */
  563. ep = usb_ep_autoconfig(cdev->gadget, &as_out_ep_desc);
  564. if (!ep)
  565. goto fail;
  566. audio->out_ep = ep;
  567. audio->out_ep->desc = &as_out_ep_desc;
  568. ep->driver_data = cdev; /* claim */
  569. status = -ENOMEM;
  570. /* copy descriptors, and track endpoint copies */
  571. f->descriptors = usb_copy_descriptors(f_audio_desc);
  572. /*
  573. * support all relevant hardware speeds... we expect that when
  574. * hardware is dual speed, all bulk-capable endpoints work at
  575. * both speeds
  576. */
  577. if (gadget_is_dualspeed(c->cdev->gadget)) {
  578. c->highspeed = true;
  579. f->hs_descriptors = usb_copy_descriptors(f_audio_desc);
  580. }
  581. return 0;
  582. fail:
  583. return status;
  584. }
  585. static void
  586. f_audio_unbind(struct usb_configuration *c, struct usb_function *f)
  587. {
  588. struct f_audio *audio = func_to_audio(f);
  589. usb_free_descriptors(f->descriptors);
  590. usb_free_descriptors(f->hs_descriptors);
  591. kfree(audio);
  592. }
  593. /*-------------------------------------------------------------------------*/
  594. static int generic_set_cmd(struct usb_audio_control *con, u8 cmd, int value)
  595. {
  596. con->data[cmd] = value;
  597. return 0;
  598. }
  599. static int generic_get_cmd(struct usb_audio_control *con, u8 cmd)
  600. {
  601. return con->data[cmd];
  602. }
  603. /* Todo: add more control selecotor dynamically */
  604. int __init control_selector_init(struct f_audio *audio)
  605. {
  606. INIT_LIST_HEAD(&audio->cs);
  607. list_add(&feature_unit.list, &audio->cs);
  608. INIT_LIST_HEAD(&feature_unit.control);
  609. list_add(&mute_control.list, &feature_unit.control);
  610. list_add(&volume_control.list, &feature_unit.control);
  611. volume_control.data[UAC__CUR] = 0xffc0;
  612. volume_control.data[UAC__MIN] = 0xe3a0;
  613. volume_control.data[UAC__MAX] = 0xfff0;
  614. volume_control.data[UAC__RES] = 0x0030;
  615. return 0;
  616. }
  617. /**
  618. * audio_bind_config - add USB audio function to a configuration
  619. * @c: the configuration to supcard the USB audio function
  620. * Context: single threaded during gadget setup
  621. *
  622. * Returns zero on success, else negative errno.
  623. */
  624. int __init audio_bind_config(struct usb_configuration *c)
  625. {
  626. struct f_audio *audio;
  627. int status;
  628. /* allocate and initialize one new instance */
  629. audio = kzalloc(sizeof *audio, GFP_KERNEL);
  630. if (!audio)
  631. return -ENOMEM;
  632. audio->card.func.name = "g_audio";
  633. audio->card.gadget = c->cdev->gadget;
  634. INIT_LIST_HEAD(&audio->play_queue);
  635. spin_lock_init(&audio->lock);
  636. /* set up ASLA audio devices */
  637. status = gaudio_setup(&audio->card);
  638. if (status < 0)
  639. goto setup_fail;
  640. audio->card.func.strings = audio_strings;
  641. audio->card.func.bind = f_audio_bind;
  642. audio->card.func.unbind = f_audio_unbind;
  643. audio->card.func.set_alt = f_audio_set_alt;
  644. audio->card.func.setup = f_audio_setup;
  645. audio->card.func.disable = f_audio_disable;
  646. control_selector_init(audio);
  647. INIT_WORK(&audio->playback_work, f_audio_playback_work);
  648. status = usb_add_function(c, &audio->card.func);
  649. if (status)
  650. goto add_fail;
  651. INFO(c->cdev, "audio_buf_size %d, req_buf_size %d, req_count %d\n",
  652. audio_buf_size, req_buf_size, req_count);
  653. return status;
  654. add_fail:
  655. gaudio_cleanup();
  656. setup_fail:
  657. kfree(audio);
  658. return status;
  659. }