f_audio.c 18 KB

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