f_audio.c 20 KB

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