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/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. /* 1 input terminal, 1 output terminal and 1 feature unit */
  49. #define UAC_DT_TOTAL_LENGTH (UAC_DT_AC_HEADER_LENGTH + UAC_DT_INPUT_TERMINAL_SIZE \
  50. + UAC_DT_OUTPUT_TERMINAL_SIZE + UAC_DT_FEATURE_UNIT_SIZE(0))
  51. /* B.3.2 Class-Specific AC Interface Descriptor */
  52. static struct uac_ac_header_descriptor_2 ac_header_desc = {
  53. .bLength = UAC_DT_AC_HEADER_LENGTH,
  54. .bDescriptorType = USB_DT_CS_INTERFACE,
  55. .bDescriptorSubtype = UAC_HEADER,
  56. .bcdADC = __constant_cpu_to_le16(0x0100),
  57. .wTotalLength = __constant_cpu_to_le16(UAC_DT_TOTAL_LENGTH),
  58. .bInCollection = F_AUDIO_NUM_INTERFACES,
  59. .baInterfaceNr = {
  60. [0] = F_AUDIO_AC_INTERFACE,
  61. [1] = F_AUDIO_AS_INTERFACE,
  62. }
  63. };
  64. #define INPUT_TERMINAL_ID 1
  65. static struct uac_input_terminal_descriptor input_terminal_desc = {
  66. .bLength = UAC_DT_INPUT_TERMINAL_SIZE,
  67. .bDescriptorType = USB_DT_CS_INTERFACE,
  68. .bDescriptorSubtype = UAC_INPUT_TERMINAL,
  69. .bTerminalID = INPUT_TERMINAL_ID,
  70. .wTerminalType = UAC_TERMINAL_STREAMING,
  71. .bAssocTerminal = 0,
  72. .wChannelConfig = 0x3,
  73. };
  74. DECLARE_UAC_FEATURE_UNIT_DESCRIPTOR(0);
  75. #define FEATURE_UNIT_ID 2
  76. static struct uac_feature_unit_descriptor_0 feature_unit_desc = {
  77. .bLength = UAC_DT_FEATURE_UNIT_SIZE(0),
  78. .bDescriptorType = USB_DT_CS_INTERFACE,
  79. .bDescriptorSubtype = UAC_FEATURE_UNIT,
  80. .bUnitID = FEATURE_UNIT_ID,
  81. .bSourceID = INPUT_TERMINAL_ID,
  82. .bControlSize = 2,
  83. .bmaControls[0] = (UAC_FU_MUTE | UAC_FU_VOLUME),
  84. };
  85. static struct usb_audio_control mute_control = {
  86. .list = LIST_HEAD_INIT(mute_control.list),
  87. .name = "Mute Control",
  88. .type = UAC_MUTE_CONTROL,
  89. /* Todo: add real Mute control code */
  90. .set = generic_set_cmd,
  91. .get = generic_get_cmd,
  92. };
  93. static struct usb_audio_control volume_control = {
  94. .list = LIST_HEAD_INIT(volume_control.list),
  95. .name = "Volume Control",
  96. .type = UAC_VOLUME_CONTROL,
  97. /* Todo: add real Volume control code */
  98. .set = generic_set_cmd,
  99. .get = generic_get_cmd,
  100. };
  101. static struct usb_audio_control_selector feature_unit = {
  102. .list = LIST_HEAD_INIT(feature_unit.list),
  103. .id = FEATURE_UNIT_ID,
  104. .name = "Mute & Volume Control",
  105. .type = UAC_FEATURE_UNIT,
  106. .desc = (struct usb_descriptor_header *)&feature_unit_desc,
  107. };
  108. #define OUTPUT_TERMINAL_ID 3
  109. static struct uac_output_terminal_descriptor output_terminal_desc = {
  110. .bLength = UAC_DT_OUTPUT_TERMINAL_SIZE,
  111. .bDescriptorType = USB_DT_CS_INTERFACE,
  112. .bDescriptorSubtype = UAC_OUTPUT_TERMINAL,
  113. .bTerminalID = OUTPUT_TERMINAL_ID,
  114. .wTerminalType = UAC_OUTPUT_TERMINAL_SPEAKER,
  115. .bAssocTerminal = FEATURE_UNIT_ID,
  116. .bSourceID = FEATURE_UNIT_ID,
  117. };
  118. /* B.4.1 Standard AS Interface Descriptor */
  119. static struct usb_interface_descriptor as_interface_alt_0_desc = {
  120. .bLength = USB_DT_INTERFACE_SIZE,
  121. .bDescriptorType = USB_DT_INTERFACE,
  122. .bAlternateSetting = 0,
  123. .bNumEndpoints = 0,
  124. .bInterfaceClass = USB_CLASS_AUDIO,
  125. .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
  126. };
  127. static struct usb_interface_descriptor as_interface_alt_1_desc = {
  128. .bLength = USB_DT_INTERFACE_SIZE,
  129. .bDescriptorType = USB_DT_INTERFACE,
  130. .bAlternateSetting = 1,
  131. .bNumEndpoints = 1,
  132. .bInterfaceClass = USB_CLASS_AUDIO,
  133. .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
  134. };
  135. /* B.4.2 Class-Specific AS Interface Descriptor */
  136. static struct uac_as_header_descriptor as_header_desc = {
  137. .bLength = UAC_DT_AS_HEADER_SIZE,
  138. .bDescriptorType = USB_DT_CS_INTERFACE,
  139. .bDescriptorSubtype = UAC_AS_GENERAL,
  140. .bTerminalLink = INPUT_TERMINAL_ID,
  141. .bDelay = 1,
  142. .wFormatTag = UAC_FORMAT_TYPE_I_PCM,
  143. };
  144. DECLARE_UAC_FORMAT_TYPE_I_DISCRETE_DESC(1);
  145. static struct uac_format_type_i_discrete_descriptor_1 as_type_i_desc = {
  146. .bLength = UAC_FORMAT_TYPE_I_DISCRETE_DESC_SIZE(1),
  147. .bDescriptorType = USB_DT_CS_INTERFACE,
  148. .bDescriptorSubtype = UAC_FORMAT_TYPE,
  149. .bFormatType = UAC_FORMAT_TYPE_I,
  150. .bSubframeSize = 2,
  151. .bBitResolution = 16,
  152. .bSamFreqType = 1,
  153. };
  154. /* Standard ISO OUT Endpoint Descriptor */
  155. static struct usb_endpoint_descriptor as_out_ep_desc __initdata = {
  156. .bLength = USB_DT_ENDPOINT_AUDIO_SIZE,
  157. .bDescriptorType = USB_DT_ENDPOINT,
  158. .bEndpointAddress = USB_DIR_OUT,
  159. .bmAttributes = USB_ENDPOINT_SYNC_ADAPTIVE
  160. | USB_ENDPOINT_XFER_ISOC,
  161. .wMaxPacketSize = __constant_cpu_to_le16(OUT_EP_MAX_PACKET_SIZE),
  162. .bInterval = 4,
  163. };
  164. /* Class-specific AS ISO OUT Endpoint Descriptor */
  165. static struct uac_iso_endpoint_descriptor as_iso_out_desc __initdata = {
  166. .bLength = UAC_ISO_ENDPOINT_DESC_SIZE,
  167. .bDescriptorType = USB_DT_CS_ENDPOINT,
  168. .bDescriptorSubtype = UAC_EP_GENERAL,
  169. .bmAttributes = 1,
  170. .bLockDelayUnits = 1,
  171. .wLockDelay = __constant_cpu_to_le16(1),
  172. };
  173. static struct usb_descriptor_header *f_audio_desc[] __initdata = {
  174. (struct usb_descriptor_header *)&ac_interface_desc,
  175. (struct usb_descriptor_header *)&ac_header_desc,
  176. (struct usb_descriptor_header *)&input_terminal_desc,
  177. (struct usb_descriptor_header *)&output_terminal_desc,
  178. (struct usb_descriptor_header *)&feature_unit_desc,
  179. (struct usb_descriptor_header *)&as_interface_alt_0_desc,
  180. (struct usb_descriptor_header *)&as_interface_alt_1_desc,
  181. (struct usb_descriptor_header *)&as_header_desc,
  182. (struct usb_descriptor_header *)&as_type_i_desc,
  183. (struct usb_descriptor_header *)&as_out_ep_desc,
  184. (struct usb_descriptor_header *)&as_iso_out_desc,
  185. NULL,
  186. };
  187. /* string IDs are assigned dynamically */
  188. #define STRING_MANUFACTURER_IDX 0
  189. #define STRING_PRODUCT_IDX 1
  190. static char manufacturer[50];
  191. static struct usb_string strings_dev[] = {
  192. [STRING_MANUFACTURER_IDX].s = manufacturer,
  193. [STRING_PRODUCT_IDX].s = DRIVER_DESC,
  194. { } /* end of list */
  195. };
  196. static struct usb_gadget_strings stringtab_dev = {
  197. .language = 0x0409, /* en-us */
  198. .strings = strings_dev,
  199. };
  200. static struct usb_gadget_strings *audio_strings[] = {
  201. &stringtab_dev,
  202. NULL,
  203. };
  204. /*
  205. * This function is an ALSA sound card following USB Audio Class Spec 1.0.
  206. */
  207. /*-------------------------------------------------------------------------*/
  208. struct f_audio_buf {
  209. u8 *buf;
  210. int actual;
  211. struct list_head list;
  212. };
  213. static struct f_audio_buf *f_audio_buffer_alloc(int buf_size)
  214. {
  215. struct f_audio_buf *copy_buf;
  216. copy_buf = kzalloc(sizeof *copy_buf, GFP_ATOMIC);
  217. if (!copy_buf)
  218. return ERR_PTR(-ENOMEM);
  219. copy_buf->buf = kzalloc(buf_size, GFP_ATOMIC);
  220. if (!copy_buf->buf) {
  221. kfree(copy_buf);
  222. return ERR_PTR(-ENOMEM);
  223. }
  224. return copy_buf;
  225. }
  226. static void f_audio_buffer_free(struct f_audio_buf *audio_buf)
  227. {
  228. kfree(audio_buf->buf);
  229. kfree(audio_buf);
  230. }
  231. /*-------------------------------------------------------------------------*/
  232. struct f_audio {
  233. struct gaudio card;
  234. /* endpoints handle full and/or high speeds */
  235. struct usb_ep *out_ep;
  236. struct usb_endpoint_descriptor *out_desc;
  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. return;
  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. kfree(audio);
  590. }
  591. /*-------------------------------------------------------------------------*/
  592. static int generic_set_cmd(struct usb_audio_control *con, u8 cmd, int value)
  593. {
  594. con->data[cmd] = value;
  595. return 0;
  596. }
  597. static int generic_get_cmd(struct usb_audio_control *con, u8 cmd)
  598. {
  599. return con->data[cmd];
  600. }
  601. /* Todo: add more control selecotor dynamically */
  602. int __init control_selector_init(struct f_audio *audio)
  603. {
  604. INIT_LIST_HEAD(&audio->cs);
  605. list_add(&feature_unit.list, &audio->cs);
  606. INIT_LIST_HEAD(&feature_unit.control);
  607. list_add(&mute_control.list, &feature_unit.control);
  608. list_add(&volume_control.list, &feature_unit.control);
  609. volume_control.data[UAC__CUR] = 0xffc0;
  610. volume_control.data[UAC__MIN] = 0xe3a0;
  611. volume_control.data[UAC__MAX] = 0xfff0;
  612. volume_control.data[UAC__RES] = 0x0030;
  613. return 0;
  614. }
  615. /**
  616. * audio_bind_config - add USB audio fucntion to a configuration
  617. * @c: the configuration to supcard the USB audio function
  618. * Context: single threaded during gadget setup
  619. *
  620. * Returns zero on success, else negative errno.
  621. */
  622. int __init audio_bind_config(struct usb_configuration *c)
  623. {
  624. struct f_audio *audio;
  625. int status;
  626. /* allocate and initialize one new instance */
  627. audio = kzalloc(sizeof *audio, GFP_KERNEL);
  628. if (!audio)
  629. return -ENOMEM;
  630. audio->card.func.name = "g_audio";
  631. audio->card.gadget = c->cdev->gadget;
  632. INIT_LIST_HEAD(&audio->play_queue);
  633. spin_lock_init(&audio->lock);
  634. /* set up ASLA audio devices */
  635. status = gaudio_setup(&audio->card);
  636. if (status < 0)
  637. goto setup_fail;
  638. audio->card.func.strings = audio_strings;
  639. audio->card.func.bind = f_audio_bind;
  640. audio->card.func.unbind = f_audio_unbind;
  641. audio->card.func.set_alt = f_audio_set_alt;
  642. audio->card.func.setup = f_audio_setup;
  643. audio->card.func.disable = f_audio_disable;
  644. audio->out_desc = &as_out_ep_desc;
  645. control_selector_init(audio);
  646. INIT_WORK(&audio->playback_work, f_audio_playback_work);
  647. status = usb_add_function(c, &audio->card.func);
  648. if (status)
  649. goto add_fail;
  650. INFO(c->cdev, "audio_buf_size %d, req_buf_size %d, req_count %d\n",
  651. audio_buf_size, req_buf_size, req_count);
  652. return status;
  653. add_fail:
  654. gaudio_cleanup();
  655. setup_fail:
  656. kfree(audio);
  657. return status;
  658. }