f_audio_source.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  1. /*
  2. * Gadget Function Driver for USB audio source device
  3. *
  4. * Copyright (C) 2012 Google, Inc.
  5. *
  6. * This software is licensed under the terms of the GNU General Public
  7. * License version 2, as published by the Free Software Foundation, and
  8. * may be copied, distributed, and modified under those terms.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. */
  16. #include <linux/device.h>
  17. #include <linux/usb/audio.h>
  18. #include <linux/wait.h>
  19. #include <sound/core.h>
  20. #include <sound/initval.h>
  21. #include <sound/pcm.h>
  22. #define SAMPLE_RATE 44100
  23. #define BYTES_PER_FRAME 4
  24. #define FRAMES_PER_MSEC (SAMPLE_RATE / 1000)
  25. #define IN_EP_MAX_PACKET_SIZE ((FRAMES_PER_MSEC + 1) * BYTES_PER_FRAME)
  26. /* Number of requests to allocate */
  27. #define IN_EP_REQ_COUNT 16
  28. #define AUDIO_AC_INTERFACE 0
  29. #define AUDIO_AS_INTERFACE 1
  30. #define AUDIO_NUM_INTERFACES 2
  31. /* B.3.1 Standard AC Interface Descriptor */
  32. static struct usb_interface_descriptor audio_ac_interface_desc = {
  33. .bLength = USB_DT_INTERFACE_SIZE,
  34. .bDescriptorType = USB_DT_INTERFACE,
  35. .bNumEndpoints = 0,
  36. .bInterfaceClass = USB_CLASS_AUDIO,
  37. .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL,
  38. };
  39. DECLARE_UAC_AC_HEADER_DESCRIPTOR(2);
  40. #define UAC_DT_AC_HEADER_LENGTH UAC_DT_AC_HEADER_SIZE(AUDIO_NUM_INTERFACES)
  41. /* 1 input terminal, 1 output terminal and 1 feature unit */
  42. #define UAC_DT_TOTAL_LENGTH (UAC_DT_AC_HEADER_LENGTH \
  43. + UAC_DT_INPUT_TERMINAL_SIZE + UAC_DT_OUTPUT_TERMINAL_SIZE \
  44. + UAC_DT_FEATURE_UNIT_SIZE(0))
  45. /* B.3.2 Class-Specific AC Interface Descriptor */
  46. static struct uac1_ac_header_descriptor_2 audio_ac_header_desc = {
  47. .bLength = UAC_DT_AC_HEADER_LENGTH,
  48. .bDescriptorType = USB_DT_CS_INTERFACE,
  49. .bDescriptorSubtype = UAC_HEADER,
  50. .bcdADC = __constant_cpu_to_le16(0x0100),
  51. .wTotalLength = __constant_cpu_to_le16(UAC_DT_TOTAL_LENGTH),
  52. .bInCollection = AUDIO_NUM_INTERFACES,
  53. .baInterfaceNr = {
  54. [0] = AUDIO_AC_INTERFACE,
  55. [1] = AUDIO_AS_INTERFACE,
  56. }
  57. };
  58. #define INPUT_TERMINAL_ID 1
  59. static struct uac_input_terminal_descriptor audio_input_terminal_desc = {
  60. .bLength = UAC_DT_INPUT_TERMINAL_SIZE,
  61. .bDescriptorType = USB_DT_CS_INTERFACE,
  62. .bDescriptorSubtype = UAC_INPUT_TERMINAL,
  63. .bTerminalID = INPUT_TERMINAL_ID,
  64. .wTerminalType = UAC_INPUT_TERMINAL_MICROPHONE,
  65. .bAssocTerminal = 0,
  66. .wChannelConfig = 0x3,
  67. };
  68. DECLARE_UAC_FEATURE_UNIT_DESCRIPTOR(0);
  69. #define FEATURE_UNIT_ID 2
  70. static struct uac_feature_unit_descriptor_0 audio_feature_unit_desc = {
  71. .bLength = UAC_DT_FEATURE_UNIT_SIZE(0),
  72. .bDescriptorType = USB_DT_CS_INTERFACE,
  73. .bDescriptorSubtype = UAC_FEATURE_UNIT,
  74. .bUnitID = FEATURE_UNIT_ID,
  75. .bSourceID = INPUT_TERMINAL_ID,
  76. .bControlSize = 2,
  77. };
  78. #define OUTPUT_TERMINAL_ID 3
  79. static struct uac1_output_terminal_descriptor audio_output_terminal_desc = {
  80. .bLength = UAC_DT_OUTPUT_TERMINAL_SIZE,
  81. .bDescriptorType = USB_DT_CS_INTERFACE,
  82. .bDescriptorSubtype = UAC_OUTPUT_TERMINAL,
  83. .bTerminalID = OUTPUT_TERMINAL_ID,
  84. .wTerminalType = UAC_TERMINAL_STREAMING,
  85. .bAssocTerminal = FEATURE_UNIT_ID,
  86. .bSourceID = FEATURE_UNIT_ID,
  87. };
  88. /* B.4.1 Standard AS Interface Descriptor */
  89. static struct usb_interface_descriptor audio_as_interface_alt_0_desc = {
  90. .bLength = USB_DT_INTERFACE_SIZE,
  91. .bDescriptorType = USB_DT_INTERFACE,
  92. .bAlternateSetting = 0,
  93. .bNumEndpoints = 0,
  94. .bInterfaceClass = USB_CLASS_AUDIO,
  95. .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
  96. };
  97. static struct usb_interface_descriptor audio_as_interface_alt_1_desc = {
  98. .bLength = USB_DT_INTERFACE_SIZE,
  99. .bDescriptorType = USB_DT_INTERFACE,
  100. .bAlternateSetting = 1,
  101. .bNumEndpoints = 1,
  102. .bInterfaceClass = USB_CLASS_AUDIO,
  103. .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
  104. };
  105. /* B.4.2 Class-Specific AS Interface Descriptor */
  106. static struct uac1_as_header_descriptor audio_as_header_desc = {
  107. .bLength = UAC_DT_AS_HEADER_SIZE,
  108. .bDescriptorType = USB_DT_CS_INTERFACE,
  109. .bDescriptorSubtype = UAC_AS_GENERAL,
  110. .bTerminalLink = INPUT_TERMINAL_ID,
  111. .bDelay = 1,
  112. .wFormatTag = UAC_FORMAT_TYPE_I_PCM,
  113. };
  114. DECLARE_UAC_FORMAT_TYPE_I_DISCRETE_DESC(1);
  115. static struct uac_format_type_i_discrete_descriptor_1 audio_as_type_i_desc = {
  116. .bLength = UAC_FORMAT_TYPE_I_DISCRETE_DESC_SIZE(1),
  117. .bDescriptorType = USB_DT_CS_INTERFACE,
  118. .bDescriptorSubtype = UAC_FORMAT_TYPE,
  119. .bFormatType = UAC_FORMAT_TYPE_I,
  120. .bSubframeSize = 2,
  121. .bBitResolution = 16,
  122. .bSamFreqType = 1,
  123. };
  124. /* Standard ISO IN Endpoint Descriptor for highspeed */
  125. static struct usb_endpoint_descriptor audio_hs_as_in_ep_desc = {
  126. .bLength = USB_DT_ENDPOINT_AUDIO_SIZE,
  127. .bDescriptorType = USB_DT_ENDPOINT,
  128. .bEndpointAddress = USB_DIR_IN,
  129. .bmAttributes = USB_ENDPOINT_SYNC_SYNC
  130. | USB_ENDPOINT_XFER_ISOC,
  131. .wMaxPacketSize = __constant_cpu_to_le16(IN_EP_MAX_PACKET_SIZE),
  132. .bInterval = 4, /* poll 1 per millisecond */
  133. };
  134. /* Standard ISO IN Endpoint Descriptor for highspeed */
  135. static struct usb_endpoint_descriptor audio_fs_as_in_ep_desc = {
  136. .bLength = USB_DT_ENDPOINT_AUDIO_SIZE,
  137. .bDescriptorType = USB_DT_ENDPOINT,
  138. .bEndpointAddress = USB_DIR_IN,
  139. .bmAttributes = USB_ENDPOINT_SYNC_SYNC
  140. | USB_ENDPOINT_XFER_ISOC,
  141. .wMaxPacketSize = __constant_cpu_to_le16(IN_EP_MAX_PACKET_SIZE),
  142. .bInterval = 1, /* poll 1 per millisecond */
  143. };
  144. /* Class-specific AS ISO OUT Endpoint Descriptor */
  145. static struct uac_iso_endpoint_descriptor audio_as_iso_in_desc = {
  146. .bLength = UAC_ISO_ENDPOINT_DESC_SIZE,
  147. .bDescriptorType = USB_DT_CS_ENDPOINT,
  148. .bDescriptorSubtype = UAC_EP_GENERAL,
  149. .bmAttributes = 1,
  150. .bLockDelayUnits = 1,
  151. .wLockDelay = __constant_cpu_to_le16(1),
  152. };
  153. static struct usb_descriptor_header *hs_audio_desc[] = {
  154. (struct usb_descriptor_header *)&audio_ac_interface_desc,
  155. (struct usb_descriptor_header *)&audio_ac_header_desc,
  156. (struct usb_descriptor_header *)&audio_input_terminal_desc,
  157. (struct usb_descriptor_header *)&audio_output_terminal_desc,
  158. (struct usb_descriptor_header *)&audio_feature_unit_desc,
  159. (struct usb_descriptor_header *)&audio_as_interface_alt_0_desc,
  160. (struct usb_descriptor_header *)&audio_as_interface_alt_1_desc,
  161. (struct usb_descriptor_header *)&audio_as_header_desc,
  162. (struct usb_descriptor_header *)&audio_as_type_i_desc,
  163. (struct usb_descriptor_header *)&audio_hs_as_in_ep_desc,
  164. (struct usb_descriptor_header *)&audio_as_iso_in_desc,
  165. NULL,
  166. };
  167. static struct usb_descriptor_header *fs_audio_desc[] = {
  168. (struct usb_descriptor_header *)&audio_ac_interface_desc,
  169. (struct usb_descriptor_header *)&audio_ac_header_desc,
  170. (struct usb_descriptor_header *)&audio_input_terminal_desc,
  171. (struct usb_descriptor_header *)&audio_output_terminal_desc,
  172. (struct usb_descriptor_header *)&audio_feature_unit_desc,
  173. (struct usb_descriptor_header *)&audio_as_interface_alt_0_desc,
  174. (struct usb_descriptor_header *)&audio_as_interface_alt_1_desc,
  175. (struct usb_descriptor_header *)&audio_as_header_desc,
  176. (struct usb_descriptor_header *)&audio_as_type_i_desc,
  177. (struct usb_descriptor_header *)&audio_fs_as_in_ep_desc,
  178. (struct usb_descriptor_header *)&audio_as_iso_in_desc,
  179. NULL,
  180. };
  181. static struct snd_pcm_hardware audio_hw_info = {
  182. .info = SNDRV_PCM_INFO_MMAP |
  183. SNDRV_PCM_INFO_MMAP_VALID |
  184. SNDRV_PCM_INFO_BATCH |
  185. SNDRV_PCM_INFO_INTERLEAVED |
  186. SNDRV_PCM_INFO_BLOCK_TRANSFER,
  187. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  188. .channels_min = 2,
  189. .channels_max = 2,
  190. .rate_min = SAMPLE_RATE,
  191. .rate_max = SAMPLE_RATE,
  192. .buffer_bytes_max = 1024 * 1024,
  193. .period_bytes_min = 64,
  194. .period_bytes_max = 512 * 1024,
  195. .periods_min = 2,
  196. .periods_max = 1024,
  197. };
  198. /*-------------------------------------------------------------------------*/
  199. struct audio_source_config {
  200. int card;
  201. int device;
  202. };
  203. struct audio_dev {
  204. struct usb_function func;
  205. struct snd_card *card;
  206. struct snd_pcm *pcm;
  207. struct snd_pcm_substream *substream;
  208. struct list_head idle_reqs;
  209. struct usb_ep *in_ep;
  210. struct usb_endpoint_descriptor *in_desc;
  211. spinlock_t lock;
  212. /* beginning, end and current position in our buffer */
  213. void *buffer_start;
  214. void *buffer_end;
  215. void *buffer_pos;
  216. /* byte size of a "period" */
  217. unsigned int period;
  218. /* bytes sent since last call to snd_pcm_period_elapsed */
  219. unsigned int period_offset;
  220. /* time we started playing */
  221. ktime_t start_time;
  222. /* number of frames sent since start_time */
  223. s64 frames_sent;
  224. };
  225. static inline struct audio_dev *func_to_audio(struct usb_function *f)
  226. {
  227. return container_of(f, struct audio_dev, func);
  228. }
  229. /*-------------------------------------------------------------------------*/
  230. static struct usb_request *audio_request_new(struct usb_ep *ep, int buffer_size)
  231. {
  232. struct usb_request *req = usb_ep_alloc_request(ep, GFP_KERNEL);
  233. if (!req)
  234. return NULL;
  235. req->buf = kmalloc(buffer_size, GFP_KERNEL);
  236. if (!req->buf) {
  237. usb_ep_free_request(ep, req);
  238. return NULL;
  239. }
  240. req->length = buffer_size;
  241. return req;
  242. }
  243. static void audio_request_free(struct usb_request *req, struct usb_ep *ep)
  244. {
  245. if (req) {
  246. kfree(req->buf);
  247. usb_ep_free_request(ep, req);
  248. }
  249. }
  250. static void audio_req_put(struct audio_dev *audio, struct usb_request *req)
  251. {
  252. unsigned long flags;
  253. spin_lock_irqsave(&audio->lock, flags);
  254. list_add_tail(&req->list, &audio->idle_reqs);
  255. spin_unlock_irqrestore(&audio->lock, flags);
  256. }
  257. static struct usb_request *audio_req_get(struct audio_dev *audio)
  258. {
  259. unsigned long flags;
  260. struct usb_request *req;
  261. spin_lock_irqsave(&audio->lock, flags);
  262. if (list_empty(&audio->idle_reqs)) {
  263. req = 0;
  264. } else {
  265. req = list_first_entry(&audio->idle_reqs, struct usb_request,
  266. list);
  267. list_del(&req->list);
  268. }
  269. spin_unlock_irqrestore(&audio->lock, flags);
  270. return req;
  271. }
  272. /* send the appropriate number of packets to match our bitrate */
  273. static void audio_send(struct audio_dev *audio)
  274. {
  275. struct snd_pcm_runtime *runtime;
  276. struct usb_request *req;
  277. int length, length1, length2, ret;
  278. s64 msecs;
  279. s64 frames;
  280. ktime_t now;
  281. unsigned long flags;
  282. spin_lock_irqsave(&audio->lock, flags);
  283. /* audio->substream will be null if we have been closed */
  284. if (!audio->substream) {
  285. spin_unlock_irqrestore(&audio->lock, flags);
  286. return;
  287. }
  288. /* audio->buffer_pos will be null if we have been stopped */
  289. if (!audio->buffer_pos) {
  290. spin_unlock_irqrestore(&audio->lock, flags);
  291. return;
  292. }
  293. runtime = audio->substream->runtime;
  294. spin_unlock_irqrestore(&audio->lock, flags);
  295. /* compute number of frames to send */
  296. now = ktime_get();
  297. msecs = ktime_to_ns(now) - ktime_to_ns(audio->start_time);
  298. do_div(msecs, 1000000);
  299. msecs += IN_EP_REQ_COUNT/2;
  300. frames = msecs * SAMPLE_RATE;
  301. do_div(frames, 1000);
  302. /* Readjust our frames_sent if we fall too far behind.
  303. * If we get too far behind it is better to drop some frames than
  304. * to keep sending data too fast in an attempt to catch up.
  305. */
  306. if (frames - audio->frames_sent > 2 * FRAMES_PER_MSEC * IN_EP_REQ_COUNT)
  307. audio->frames_sent = frames - FRAMES_PER_MSEC;
  308. frames -= audio->frames_sent;
  309. /* We need to send something to keep the pipeline going */
  310. while (frames > 0) {
  311. req = audio_req_get(audio);
  312. spin_lock_irqsave(&audio->lock, flags);
  313. /* audio->substream will be null if we have been closed */
  314. if (!audio->substream) {
  315. spin_unlock_irqrestore(&audio->lock, flags);
  316. return;
  317. }
  318. /* audio->buffer_pos will be null if we have been stopped */
  319. if (!audio->buffer_pos) {
  320. spin_unlock_irqrestore(&audio->lock, flags);
  321. return;
  322. }
  323. if (!req) {
  324. spin_unlock_irqrestore(&audio->lock, flags);
  325. break;
  326. }
  327. length = frames_to_bytes(runtime, frames);
  328. if (length > IN_EP_MAX_PACKET_SIZE)
  329. length = IN_EP_MAX_PACKET_SIZE;
  330. if (audio->buffer_pos + length > audio->buffer_end)
  331. length1 = audio->buffer_end - audio->buffer_pos;
  332. else
  333. length1 = length;
  334. memcpy(req->buf, audio->buffer_pos, length1);
  335. if (length1 < length) {
  336. /* Wrap around and copy remaining length
  337. * at beginning of buffer.
  338. */
  339. length2 = length - length1;
  340. memcpy(req->buf + length1, audio->buffer_start,
  341. length2);
  342. audio->buffer_pos = audio->buffer_start + length2;
  343. } else {
  344. audio->buffer_pos += length1;
  345. if (audio->buffer_pos >= audio->buffer_end)
  346. audio->buffer_pos = audio->buffer_start;
  347. }
  348. req->length = length;
  349. spin_unlock_irqrestore(&audio->lock, flags);
  350. ret = usb_ep_queue(audio->in_ep, req, GFP_ATOMIC);
  351. if (ret < 0) {
  352. pr_err("usb_ep_queue failed ret: %d\n", ret);
  353. audio_req_put(audio, req);
  354. break;
  355. }
  356. frames -= bytes_to_frames(runtime, length);
  357. audio->frames_sent += bytes_to_frames(runtime, length);
  358. }
  359. }
  360. static void audio_control_complete(struct usb_ep *ep, struct usb_request *req)
  361. {
  362. /* nothing to do here */
  363. }
  364. static void audio_data_complete(struct usb_ep *ep, struct usb_request *req)
  365. {
  366. struct audio_dev *audio = req->context;
  367. pr_debug("audio_data_complete req->status %d req->actual %d\n",
  368. req->status, req->actual);
  369. audio_req_put(audio, req);
  370. if (!audio->buffer_start || req->status)
  371. return;
  372. audio->period_offset += req->actual;
  373. if (audio->period_offset >= audio->period) {
  374. snd_pcm_period_elapsed(audio->substream);
  375. audio->period_offset = 0;
  376. }
  377. audio_send(audio);
  378. }
  379. static int audio_set_endpoint_req(struct usb_function *f,
  380. const struct usb_ctrlrequest *ctrl)
  381. {
  382. int value = -EOPNOTSUPP;
  383. u16 ep = le16_to_cpu(ctrl->wIndex);
  384. u16 len = le16_to_cpu(ctrl->wLength);
  385. u16 w_value = le16_to_cpu(ctrl->wValue);
  386. pr_debug("bRequest 0x%x, w_value 0x%04x, len %d, endpoint %d\n",
  387. ctrl->bRequest, w_value, len, ep);
  388. switch (ctrl->bRequest) {
  389. case UAC_SET_CUR:
  390. case UAC_SET_MIN:
  391. case UAC_SET_MAX:
  392. case UAC_SET_RES:
  393. value = len;
  394. break;
  395. default:
  396. break;
  397. }
  398. return value;
  399. }
  400. static int audio_get_endpoint_req(struct usb_function *f,
  401. const struct usb_ctrlrequest *ctrl)
  402. {
  403. struct usb_composite_dev *cdev = f->config->cdev;
  404. int value = -EOPNOTSUPP;
  405. u8 ep = ((le16_to_cpu(ctrl->wIndex) >> 8) & 0xFF);
  406. u16 len = le16_to_cpu(ctrl->wLength);
  407. u16 w_value = le16_to_cpu(ctrl->wValue);
  408. u8 *buf = cdev->req->buf;
  409. pr_debug("bRequest 0x%x, w_value 0x%04x, len %d, endpoint %d\n",
  410. ctrl->bRequest, w_value, len, ep);
  411. if (w_value == UAC_EP_CS_ATTR_SAMPLE_RATE << 8) {
  412. switch (ctrl->bRequest) {
  413. case UAC_GET_CUR:
  414. case UAC_GET_MIN:
  415. case UAC_GET_MAX:
  416. case UAC_GET_RES:
  417. /* return our sample rate */
  418. buf[0] = (u8)SAMPLE_RATE;
  419. buf[1] = (u8)(SAMPLE_RATE >> 8);
  420. buf[2] = (u8)(SAMPLE_RATE >> 16);
  421. value = 3;
  422. break;
  423. default:
  424. break;
  425. }
  426. }
  427. return value;
  428. }
  429. static int
  430. audio_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
  431. {
  432. struct usb_composite_dev *cdev = f->config->cdev;
  433. struct usb_request *req = cdev->req;
  434. int value = -EOPNOTSUPP;
  435. u16 w_index = le16_to_cpu(ctrl->wIndex);
  436. u16 w_value = le16_to_cpu(ctrl->wValue);
  437. u16 w_length = le16_to_cpu(ctrl->wLength);
  438. /* composite driver infrastructure handles everything; interface
  439. * activation uses set_alt().
  440. */
  441. switch (ctrl->bRequestType) {
  442. case USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_ENDPOINT:
  443. value = audio_set_endpoint_req(f, ctrl);
  444. break;
  445. case USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT:
  446. value = audio_get_endpoint_req(f, ctrl);
  447. break;
  448. }
  449. /* respond with data transfer or status phase? */
  450. if (value >= 0) {
  451. pr_debug("audio req%02x.%02x v%04x i%04x l%d\n",
  452. ctrl->bRequestType, ctrl->bRequest,
  453. w_value, w_index, w_length);
  454. req->zero = 0;
  455. req->length = value;
  456. req->complete = audio_control_complete;
  457. value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
  458. if (value < 0)
  459. pr_err("audio response on err %d\n", value);
  460. }
  461. /* device either stalls (value < 0) or reports success */
  462. return value;
  463. }
  464. static int audio_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
  465. {
  466. struct audio_dev *audio = func_to_audio(f);
  467. struct usb_composite_dev *cdev = f->config->cdev;
  468. int ret;
  469. pr_debug("audio_set_alt intf %d, alt %d\n", intf, alt);
  470. ret = config_ep_by_speed(cdev->gadget, f, audio->in_ep);
  471. if (ret)
  472. return ret;
  473. usb_ep_enable(audio->in_ep);
  474. return 0;
  475. }
  476. static void audio_disable(struct usb_function *f)
  477. {
  478. struct audio_dev *audio = func_to_audio(f);
  479. pr_debug("audio_disable\n");
  480. usb_ep_disable(audio->in_ep);
  481. }
  482. /*-------------------------------------------------------------------------*/
  483. static void audio_build_desc(struct audio_dev *audio)
  484. {
  485. u8 *sam_freq;
  486. int rate;
  487. /* Set channel numbers */
  488. audio_input_terminal_desc.bNrChannels = 2;
  489. audio_as_type_i_desc.bNrChannels = 2;
  490. /* Set sample rates */
  491. rate = SAMPLE_RATE;
  492. sam_freq = audio_as_type_i_desc.tSamFreq[0];
  493. memcpy(sam_freq, &rate, 3);
  494. }
  495. /* audio function driver setup/binding */
  496. static int
  497. audio_bind(struct usb_configuration *c, struct usb_function *f)
  498. {
  499. struct usb_composite_dev *cdev = c->cdev;
  500. struct audio_dev *audio = func_to_audio(f);
  501. int status;
  502. struct usb_ep *ep;
  503. struct usb_request *req;
  504. int i;
  505. audio_build_desc(audio);
  506. /* allocate instance-specific interface IDs, and patch descriptors */
  507. status = usb_interface_id(c, f);
  508. if (status < 0)
  509. goto fail;
  510. audio_ac_interface_desc.bInterfaceNumber = status;
  511. /* AUDIO_AC_INTERFACE */
  512. audio_ac_header_desc.baInterfaceNr[0] = status;
  513. status = usb_interface_id(c, f);
  514. if (status < 0)
  515. goto fail;
  516. audio_as_interface_alt_0_desc.bInterfaceNumber = status;
  517. audio_as_interface_alt_1_desc.bInterfaceNumber = status;
  518. /* AUDIO_AS_INTERFACE */
  519. audio_ac_header_desc.baInterfaceNr[1] = status;
  520. status = -ENODEV;
  521. /* allocate our endpoint */
  522. ep = usb_ep_autoconfig(cdev->gadget, &audio_fs_as_in_ep_desc);
  523. if (!ep)
  524. goto fail;
  525. audio->in_ep = ep;
  526. ep->driver_data = audio; /* claim */
  527. if (gadget_is_dualspeed(c->cdev->gadget))
  528. audio_hs_as_in_ep_desc.bEndpointAddress =
  529. audio_fs_as_in_ep_desc.bEndpointAddress;
  530. f->fs_descriptors = fs_audio_desc;
  531. f->hs_descriptors = hs_audio_desc;
  532. for (i = 0, status = 0; i < IN_EP_REQ_COUNT && status == 0; i++) {
  533. req = audio_request_new(ep, IN_EP_MAX_PACKET_SIZE);
  534. if (req) {
  535. req->context = audio;
  536. req->complete = audio_data_complete;
  537. audio_req_put(audio, req);
  538. } else
  539. status = -ENOMEM;
  540. }
  541. fail:
  542. return status;
  543. }
  544. static void
  545. audio_unbind(struct usb_configuration *c, struct usb_function *f)
  546. {
  547. struct audio_dev *audio = func_to_audio(f);
  548. struct usb_request *req;
  549. while ((req = audio_req_get(audio)))
  550. audio_request_free(req, audio->in_ep);
  551. snd_card_free_when_closed(audio->card);
  552. audio->card = NULL;
  553. audio->pcm = NULL;
  554. audio->substream = NULL;
  555. audio->in_ep = NULL;
  556. }
  557. static void audio_pcm_playback_start(struct audio_dev *audio)
  558. {
  559. audio->start_time = ktime_get();
  560. audio->frames_sent = 0;
  561. audio_send(audio);
  562. }
  563. static void audio_pcm_playback_stop(struct audio_dev *audio)
  564. {
  565. unsigned long flags;
  566. spin_lock_irqsave(&audio->lock, flags);
  567. audio->buffer_start = 0;
  568. audio->buffer_end = 0;
  569. audio->buffer_pos = 0;
  570. spin_unlock_irqrestore(&audio->lock, flags);
  571. }
  572. static int audio_pcm_open(struct snd_pcm_substream *substream)
  573. {
  574. struct snd_pcm_runtime *runtime = substream->runtime;
  575. struct audio_dev *audio = substream->private_data;
  576. runtime->private_data = audio;
  577. runtime->hw = audio_hw_info;
  578. snd_pcm_limit_hw_rates(runtime);
  579. runtime->hw.channels_max = 2;
  580. audio->substream = substream;
  581. return 0;
  582. }
  583. static int audio_pcm_close(struct snd_pcm_substream *substream)
  584. {
  585. struct audio_dev *audio = substream->private_data;
  586. unsigned long flags;
  587. spin_lock_irqsave(&audio->lock, flags);
  588. audio->substream = NULL;
  589. spin_unlock_irqrestore(&audio->lock, flags);
  590. return 0;
  591. }
  592. static int audio_pcm_hw_params(struct snd_pcm_substream *substream,
  593. struct snd_pcm_hw_params *params)
  594. {
  595. unsigned int channels = params_channels(params);
  596. unsigned int rate = params_rate(params);
  597. if (rate != SAMPLE_RATE)
  598. return -EINVAL;
  599. if (channels != 2)
  600. return -EINVAL;
  601. return snd_pcm_lib_alloc_vmalloc_buffer(substream,
  602. params_buffer_bytes(params));
  603. }
  604. static int audio_pcm_hw_free(struct snd_pcm_substream *substream)
  605. {
  606. return snd_pcm_lib_free_vmalloc_buffer(substream);
  607. }
  608. static int audio_pcm_prepare(struct snd_pcm_substream *substream)
  609. {
  610. struct snd_pcm_runtime *runtime = substream->runtime;
  611. struct audio_dev *audio = runtime->private_data;
  612. audio->period = snd_pcm_lib_period_bytes(substream);
  613. audio->period_offset = 0;
  614. audio->buffer_start = runtime->dma_area;
  615. audio->buffer_end = audio->buffer_start
  616. + snd_pcm_lib_buffer_bytes(substream);
  617. audio->buffer_pos = audio->buffer_start;
  618. return 0;
  619. }
  620. static snd_pcm_uframes_t audio_pcm_pointer(struct snd_pcm_substream *substream)
  621. {
  622. struct snd_pcm_runtime *runtime = substream->runtime;
  623. struct audio_dev *audio = runtime->private_data;
  624. ssize_t bytes = audio->buffer_pos - audio->buffer_start;
  625. /* return offset of next frame to fill in our buffer */
  626. return bytes_to_frames(runtime, bytes);
  627. }
  628. static int audio_pcm_playback_trigger(struct snd_pcm_substream *substream,
  629. int cmd)
  630. {
  631. struct audio_dev *audio = substream->runtime->private_data;
  632. int ret = 0;
  633. switch (cmd) {
  634. case SNDRV_PCM_TRIGGER_START:
  635. case SNDRV_PCM_TRIGGER_RESUME:
  636. audio_pcm_playback_start(audio);
  637. break;
  638. case SNDRV_PCM_TRIGGER_STOP:
  639. case SNDRV_PCM_TRIGGER_SUSPEND:
  640. audio_pcm_playback_stop(audio);
  641. break;
  642. default:
  643. ret = -EINVAL;
  644. }
  645. return ret;
  646. }
  647. static struct audio_dev _audio_dev = {
  648. .func = {
  649. .name = "audio_source",
  650. .bind = audio_bind,
  651. .unbind = audio_unbind,
  652. .set_alt = audio_set_alt,
  653. .setup = audio_setup,
  654. .disable = audio_disable,
  655. },
  656. .lock = __SPIN_LOCK_UNLOCKED(_audio_dev.lock),
  657. .idle_reqs = LIST_HEAD_INIT(_audio_dev.idle_reqs),
  658. };
  659. static struct snd_pcm_ops audio_playback_ops = {
  660. .open = audio_pcm_open,
  661. .close = audio_pcm_close,
  662. .ioctl = snd_pcm_lib_ioctl,
  663. .hw_params = audio_pcm_hw_params,
  664. .hw_free = audio_pcm_hw_free,
  665. .prepare = audio_pcm_prepare,
  666. .trigger = audio_pcm_playback_trigger,
  667. .pointer = audio_pcm_pointer,
  668. };
  669. int audio_source_bind_config(struct usb_configuration *c,
  670. struct audio_source_config *config)
  671. {
  672. struct audio_dev *audio;
  673. struct snd_card *card;
  674. struct snd_pcm *pcm;
  675. int err;
  676. config->card = -1;
  677. config->device = -1;
  678. audio = &_audio_dev;
  679. err = snd_card_new(&c->cdev->gadget->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
  680. THIS_MODULE, 0, &card);
  681. if (err)
  682. return err;
  683. snd_card_set_dev(card, &c->cdev->gadget->dev);
  684. err = snd_pcm_new(card, "USB audio source", 0, 1, 0, &pcm);
  685. if (err)
  686. goto pcm_fail;
  687. pcm->private_data = audio;
  688. pcm->info_flags = 0;
  689. audio->pcm = pcm;
  690. strlcpy(pcm->name, "USB gadget audio", sizeof(pcm->name));
  691. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &audio_playback_ops);
  692. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  693. NULL, 0, 64 * 1024);
  694. strlcpy(card->driver, "audio_source", sizeof(card->driver));
  695. strlcpy(card->shortname, card->driver, sizeof(card->shortname));
  696. strlcpy(card->longname, "USB accessory audio source",
  697. sizeof(card->longname));
  698. err = snd_card_register(card);
  699. if (err)
  700. goto register_fail;
  701. err = usb_add_function(c, &audio->func);
  702. if (err)
  703. goto add_fail;
  704. config->card = pcm->card->number;
  705. config->device = pcm->device;
  706. audio->card = card;
  707. return 0;
  708. add_fail:
  709. register_fail:
  710. pcm_fail:
  711. snd_card_free(audio->card);
  712. return err;
  713. }