f_audio_source.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  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 <asm/dma.h>
  17. #include <linux/dma-mapping.h>
  18. #include <linux/device.h>
  19. #include <linux/usb/audio.h>
  20. #include <linux/wait.h>
  21. #include <sound/core.h>
  22. #include <sound/initval.h>
  23. #include <sound/pcm.h>
  24. #define SAMPLE_RATE 44100
  25. #define FRAMES_PER_MSEC (SAMPLE_RATE / 1000)
  26. #define IN_EP_MAX_PACKET_SIZE 256
  27. /* Number of requests to allocate */
  28. #define IN_EP_REQ_COUNT 4
  29. #define AUDIO_AC_INTERFACE 0
  30. #define AUDIO_AS_INTERFACE 1
  31. #define AUDIO_NUM_INTERFACES 2
  32. /* B.3.1 Standard AC Interface Descriptor */
  33. static struct usb_interface_descriptor audio_source_ac_interface_desc = {
  34. .bLength = USB_DT_INTERFACE_SIZE,
  35. .bDescriptorType = USB_DT_INTERFACE,
  36. .bNumEndpoints = 0,
  37. .bInterfaceClass = USB_CLASS_AUDIO,
  38. .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL,
  39. };
  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_source_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 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 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 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 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 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 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. static struct uac_format_type_i_discrete_descriptor_1 as_type_i_desc = {
  115. .bLength = UAC_FORMAT_TYPE_I_DISCRETE_DESC_SIZE(1),
  116. .bDescriptorType = USB_DT_CS_INTERFACE,
  117. .bDescriptorSubtype = UAC_FORMAT_TYPE,
  118. .bFormatType = UAC_FORMAT_TYPE_I,
  119. .bSubframeSize = 2,
  120. .bBitResolution = 16,
  121. .bSamFreqType = 1,
  122. };
  123. /* Standard ISO IN Endpoint Descriptor for highspeed */
  124. static struct usb_endpoint_descriptor hs_as_in_ep_desc = {
  125. .bLength = USB_DT_ENDPOINT_AUDIO_SIZE,
  126. .bDescriptorType = USB_DT_ENDPOINT,
  127. .bEndpointAddress = USB_DIR_IN,
  128. .bmAttributes = USB_ENDPOINT_SYNC_SYNC
  129. | USB_ENDPOINT_XFER_ISOC,
  130. .wMaxPacketSize = __constant_cpu_to_le16(IN_EP_MAX_PACKET_SIZE),
  131. .bInterval = 4, /* poll 1 per millisecond */
  132. };
  133. /* Standard ISO IN Endpoint Descriptor for highspeed */
  134. static struct usb_endpoint_descriptor fs_as_in_ep_desc = {
  135. .bLength = USB_DT_ENDPOINT_AUDIO_SIZE,
  136. .bDescriptorType = USB_DT_ENDPOINT,
  137. .bEndpointAddress = USB_DIR_IN,
  138. .bmAttributes = USB_ENDPOINT_SYNC_SYNC
  139. | USB_ENDPOINT_XFER_ISOC,
  140. .wMaxPacketSize = __constant_cpu_to_le16(IN_EP_MAX_PACKET_SIZE),
  141. .bInterval = 1, /* poll 1 per millisecond */
  142. };
  143. /* Class-specific AS ISO OUT Endpoint Descriptor */
  144. static struct uac_iso_endpoint_descriptor as_iso_in_desc = {
  145. .bLength = UAC_ISO_ENDPOINT_DESC_SIZE,
  146. .bDescriptorType = USB_DT_CS_ENDPOINT,
  147. .bDescriptorSubtype = UAC_EP_GENERAL,
  148. .bmAttributes = 1,
  149. .bLockDelayUnits = 1,
  150. .wLockDelay = __constant_cpu_to_le16(1),
  151. };
  152. static struct usb_descriptor_header *hs_audio_desc[] = {
  153. (struct usb_descriptor_header *)&audio_source_ac_interface_desc,
  154. (struct usb_descriptor_header *)&audio_source_ac_header_desc,
  155. (struct usb_descriptor_header *)&input_terminal_desc,
  156. (struct usb_descriptor_header *)&output_terminal_desc,
  157. (struct usb_descriptor_header *)&feature_unit_desc,
  158. (struct usb_descriptor_header *)&as_interface_alt_0_desc,
  159. (struct usb_descriptor_header *)&as_interface_alt_1_desc,
  160. (struct usb_descriptor_header *)&as_header_desc,
  161. (struct usb_descriptor_header *)&as_type_i_desc,
  162. (struct usb_descriptor_header *)&hs_as_in_ep_desc,
  163. (struct usb_descriptor_header *)&as_iso_in_desc,
  164. NULL,
  165. };
  166. static struct usb_descriptor_header *fs_audio_desc[] = {
  167. (struct usb_descriptor_header *)&audio_source_ac_interface_desc,
  168. (struct usb_descriptor_header *)&audio_source_ac_header_desc,
  169. (struct usb_descriptor_header *)&input_terminal_desc,
  170. (struct usb_descriptor_header *)&output_terminal_desc,
  171. (struct usb_descriptor_header *)&feature_unit_desc,
  172. (struct usb_descriptor_header *)&as_interface_alt_0_desc,
  173. (struct usb_descriptor_header *)&as_interface_alt_1_desc,
  174. (struct usb_descriptor_header *)&as_header_desc,
  175. (struct usb_descriptor_header *)&as_type_i_desc,
  176. (struct usb_descriptor_header *)&fs_as_in_ep_desc,
  177. (struct usb_descriptor_header *)&as_iso_in_desc,
  178. NULL,
  179. };
  180. static struct snd_pcm_hardware audio_hw_info = {
  181. .info = SNDRV_PCM_INFO_MMAP |
  182. SNDRV_PCM_INFO_MMAP_VALID |
  183. SNDRV_PCM_INFO_BATCH |
  184. SNDRV_PCM_INFO_INTERLEAVED |
  185. SNDRV_PCM_INFO_BLOCK_TRANSFER,
  186. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  187. .channels_min = 2,
  188. .channels_max = 2,
  189. .rate_min = SAMPLE_RATE,
  190. .rate_max = SAMPLE_RATE,
  191. .buffer_bytes_max = 1024 * 1024,
  192. .period_bytes_min = 64,
  193. .period_bytes_max = 512 * 1024,
  194. .periods_min = 2,
  195. .periods_max = 1024,
  196. };
  197. /*-------------------------------------------------------------------------*/
  198. struct audio_source_config {
  199. int card;
  200. int device;
  201. };
  202. struct audio_dev {
  203. struct usb_function func;
  204. struct snd_card *card;
  205. struct snd_pcm *pcm;
  206. struct snd_pcm_substream *substream;
  207. struct list_head idle_reqs;
  208. struct usb_ep *in_ep;
  209. spinlock_t lock;
  210. /* beginning, end and current position in our buffer */
  211. void *buffer_start;
  212. void *buffer_end;
  213. void *buffer_pos;
  214. /* byte size of a "period" */
  215. unsigned int period;
  216. /* bytes sent since last call to snd_pcm_period_elapsed */
  217. unsigned int period_offset;
  218. /* time we started playing */
  219. ktime_t start_time;
  220. /* number of frames sent since start_time */
  221. s64 frames_sent;
  222. bool audio_ep_enabled;
  223. };
  224. static inline struct audio_dev *func_to_audio_source(struct usb_function *f)
  225. {
  226. return container_of(f, struct audio_dev, func);
  227. }
  228. /*-------------------------------------------------------------------------*/
  229. static struct usb_request *audio_request_new(struct usb_ep *ep, int buffer_size)
  230. {
  231. struct usb_request *req = usb_ep_alloc_request(ep, GFP_KERNEL);
  232. if (!req)
  233. return NULL;
  234. req->buf = kmalloc(buffer_size, GFP_KERNEL);
  235. if (!req->buf) {
  236. usb_ep_free_request(ep, req);
  237. return NULL;
  238. }
  239. req->length = buffer_size;
  240. return req;
  241. }
  242. static void audio_request_free(struct usb_request *req, struct usb_ep *ep)
  243. {
  244. if (req) {
  245. kfree(req->buf);
  246. usb_ep_free_request(ep, req);
  247. }
  248. }
  249. static void audio_req_put(struct audio_dev *audio, struct usb_request *req)
  250. {
  251. unsigned long flags;
  252. spin_lock_irqsave(&audio->lock, flags);
  253. list_add_tail(&req->list, &audio->idle_reqs);
  254. spin_unlock_irqrestore(&audio->lock, flags);
  255. }
  256. static struct usb_request *audio_req_get(struct audio_dev *audio)
  257. {
  258. unsigned long flags;
  259. struct usb_request *req;
  260. spin_lock_irqsave(&audio->lock, flags);
  261. if (list_empty(&audio->idle_reqs)) {
  262. req = 0;
  263. } else {
  264. req = list_first_entry(&audio->idle_reqs, struct usb_request,
  265. list);
  266. list_del(&req->list);
  267. }
  268. spin_unlock_irqrestore(&audio->lock, flags);
  269. return req;
  270. }
  271. /* send the appropriate number of packets to match our bitrate */
  272. static void audio_send(struct audio_dev *audio)
  273. {
  274. struct snd_pcm_runtime *runtime;
  275. struct usb_request *req;
  276. int length, length1, length2, ret;
  277. s64 msecs;
  278. s64 frames;
  279. ktime_t now;
  280. unsigned long flags;
  281. spin_lock_irqsave(&audio->lock, flags);
  282. /* audio->substream will be null if we have been closed */
  283. if (!audio->substream) {
  284. spin_unlock_irqrestore(&audio->lock, flags);
  285. return;
  286. }
  287. /* audio->buffer_pos will be null if we have been stopped */
  288. if (!audio->buffer_pos) {
  289. spin_unlock_irqrestore(&audio->lock, flags);
  290. return;
  291. }
  292. runtime = audio->substream->runtime;
  293. spin_unlock_irqrestore(&audio->lock, flags);
  294. /* compute number of frames to send */
  295. now = ktime_get();
  296. msecs = ktime_to_ns(now) - ktime_to_ns(audio->start_time);
  297. do_div(msecs, 1000000);
  298. frames = msecs * SAMPLE_RATE;
  299. do_div(frames, 1000);
  300. /* Readjust our frames_sent if we fall too far behind.
  301. * If we get too far behind it is better to drop some frames than
  302. * to keep sending data too fast in an attempt to catch up.
  303. */
  304. if (frames - audio->frames_sent > 10 * FRAMES_PER_MSEC)
  305. audio->frames_sent = frames - FRAMES_PER_MSEC;
  306. frames -= audio->frames_sent;
  307. /* We need to send something to keep the pipeline going */
  308. if (frames <= 0)
  309. frames = FRAMES_PER_MSEC;
  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_source_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_source_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_source_set_endpoint_req(f, ctrl);
  444. break;
  445. case USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT:
  446. value = audio_source_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_source(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. if (intf == as_interface_alt_1_desc.bInterfaceNumber) {
  471. if (alt && !audio->audio_ep_enabled) {
  472. ret = config_ep_by_speed(cdev->gadget, f, audio->in_ep);
  473. if (ret) {
  474. audio->in_ep->desc = NULL;
  475. ERROR(cdev, "config_ep fail ep %s, result %d\n",
  476. audio->in_ep->name, ret);
  477. return ret;
  478. }
  479. ret = usb_ep_enable(audio->in_ep);
  480. if (ret) {
  481. ERROR(cdev, "failedto enable ep%s, result %d\n",
  482. audio->in_ep->name, ret);
  483. return ret;
  484. }
  485. audio->audio_ep_enabled = true;
  486. } else if (!alt && audio->audio_ep_enabled) {
  487. usb_ep_disable(audio->in_ep);
  488. audio->audio_ep_enabled = false;
  489. }
  490. }
  491. return 0;
  492. }
  493. static void audio_disable(struct usb_function *f)
  494. {
  495. struct audio_dev *audio = func_to_audio_source(f);
  496. pr_debug("audio_disable\n");
  497. if (audio->audio_ep_enabled) {
  498. usb_ep_disable(audio->in_ep);
  499. audio->audio_ep_enabled = false;
  500. }
  501. }
  502. /*-------------------------------------------------------------------------*/
  503. static void audio_build_desc(struct audio_dev *audio)
  504. {
  505. u8 *sam_freq;
  506. int rate;
  507. /* Set channel numbers */
  508. input_terminal_desc.bNrChannels = 2;
  509. as_type_i_desc.bNrChannels = 2;
  510. /* Set sample rates */
  511. rate = SAMPLE_RATE;
  512. sam_freq = as_type_i_desc.tSamFreq[0];
  513. memcpy(sam_freq, &rate, 3);
  514. }
  515. /* audio function driver setup/binding */
  516. static int
  517. audio_bind(struct usb_configuration *c, struct usb_function *f)
  518. {
  519. struct usb_composite_dev *cdev = c->cdev;
  520. struct audio_dev *audio = func_to_audio_source(f);
  521. int status;
  522. struct usb_ep *ep;
  523. struct usb_request *req;
  524. int i;
  525. audio_build_desc(audio);
  526. /* allocate instance-specific interface IDs, and patch descriptors */
  527. status = usb_interface_id(c, f);
  528. if (status < 0)
  529. goto fail;
  530. audio_source_ac_interface_desc.bInterfaceNumber = status;
  531. status = usb_interface_id(c, f);
  532. if (status < 0)
  533. goto fail;
  534. as_interface_alt_0_desc.bInterfaceNumber = status;
  535. as_interface_alt_1_desc.bInterfaceNumber = status;
  536. status = -ENODEV;
  537. /* allocate our endpoint */
  538. ep = usb_ep_autoconfig(cdev->gadget, &fs_as_in_ep_desc);
  539. if (!ep)
  540. goto fail;
  541. audio->in_ep = ep;
  542. ep->driver_data = audio; /* claim */
  543. if (gadget_is_dualspeed(c->cdev->gadget))
  544. hs_as_in_ep_desc.bEndpointAddress =
  545. fs_as_in_ep_desc.bEndpointAddress;
  546. for (i = 0, status = 0; i < IN_EP_REQ_COUNT && status == 0; i++) {
  547. req = audio_request_new(ep, IN_EP_MAX_PACKET_SIZE);
  548. if (req) {
  549. req->context = audio;
  550. req->complete = audio_data_complete;
  551. audio_req_put(audio, req);
  552. } else
  553. status = -ENOMEM;
  554. }
  555. fail:
  556. return status;
  557. }
  558. static void
  559. audio_unbind(struct usb_configuration *c, struct usb_function *f)
  560. {
  561. struct audio_dev *audio = func_to_audio_source(f);
  562. struct usb_request *req;
  563. while ((req = audio_req_get(audio)))
  564. audio_request_free(req, audio->in_ep);
  565. snd_card_free_when_closed(audio->card);
  566. audio->card = NULL;
  567. audio->pcm = NULL;
  568. audio->substream = NULL;
  569. audio->in_ep = NULL;
  570. }
  571. static void audio_pcm_playback_start(struct audio_dev *audio)
  572. {
  573. audio->start_time = ktime_get();
  574. audio->frames_sent = 0;
  575. audio_send(audio);
  576. }
  577. static void audio_pcm_playback_stop(struct audio_dev *audio)
  578. {
  579. unsigned long flags;
  580. spin_lock_irqsave(&audio->lock, flags);
  581. audio->buffer_start = 0;
  582. audio->buffer_end = 0;
  583. audio->buffer_pos = 0;
  584. spin_unlock_irqrestore(&audio->lock, flags);
  585. }
  586. static int audio_pcm_open(struct snd_pcm_substream *substream)
  587. {
  588. struct snd_pcm_runtime *runtime = substream->runtime;
  589. struct audio_dev *audio = substream->private_data;
  590. runtime->private_data = audio;
  591. runtime->hw = audio_hw_info;
  592. snd_pcm_limit_hw_rates(runtime);
  593. runtime->hw.channels_max = 2;
  594. audio->substream = substream;
  595. return 0;
  596. }
  597. static int audio_pcm_close(struct snd_pcm_substream *substream)
  598. {
  599. struct audio_dev *audio = substream->private_data;
  600. unsigned long flags;
  601. spin_lock_irqsave(&audio->lock, flags);
  602. audio->substream = NULL;
  603. spin_unlock_irqrestore(&audio->lock, flags);
  604. return 0;
  605. }
  606. static int audio_pcm_hw_params(struct snd_pcm_substream *substream,
  607. struct snd_pcm_hw_params *params)
  608. {
  609. struct snd_dma_buffer *buf = &substream->dma_buffer;
  610. unsigned int channels = params_channels(params);
  611. unsigned int rate = params_rate(params);
  612. if (rate != SAMPLE_RATE)
  613. return -EINVAL;
  614. if (channels != 2)
  615. return -EINVAL;
  616. if (!substream->pcm->card->dev->coherent_dma_mask)
  617. substream->pcm->card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
  618. buf->dev.type = SNDRV_DMA_TYPE_DEV;
  619. buf->dev.dev = substream->pcm->card->dev;
  620. buf->private_data = NULL;
  621. buf->area = dma_alloc_coherent(substream->pcm->card->dev,
  622. params_buffer_bytes(params),
  623. &buf->addr, GFP_KERNEL);
  624. if (!buf->area)
  625. return -ENOMEM;
  626. buf->bytes = params_buffer_bytes(params);
  627. snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
  628. return 0;
  629. }
  630. static int audio_pcm_hw_free(struct snd_pcm_substream *substream)
  631. {
  632. struct snd_dma_buffer *buf = &substream->dma_buffer;
  633. if (buf->area != NULL)
  634. dma_free_coherent(substream->pcm->card->dev, buf->bytes,
  635. buf->area, buf->addr);
  636. buf->area = NULL;
  637. return 0;
  638. }
  639. static int audio_pcm_prepare(struct snd_pcm_substream *substream)
  640. {
  641. struct snd_pcm_runtime *runtime = substream->runtime;
  642. struct audio_dev *audio = runtime->private_data;
  643. audio->period = snd_pcm_lib_period_bytes(substream);
  644. audio->period_offset = 0;
  645. audio->buffer_start = runtime->dma_area;
  646. audio->buffer_end = audio->buffer_start
  647. + snd_pcm_lib_buffer_bytes(substream);
  648. audio->buffer_pos = audio->buffer_start;
  649. return 0;
  650. }
  651. static snd_pcm_uframes_t audio_pcm_pointer(struct snd_pcm_substream *substream)
  652. {
  653. struct snd_pcm_runtime *runtime = substream->runtime;
  654. struct audio_dev *audio = runtime->private_data;
  655. ssize_t bytes = audio->buffer_pos - audio->buffer_start;
  656. /* return offset of next frame to fill in our buffer */
  657. return bytes_to_frames(runtime, bytes);
  658. }
  659. static int audio_pcm_playback_trigger(struct snd_pcm_substream *substream,
  660. int cmd)
  661. {
  662. struct audio_dev *audio = substream->runtime->private_data;
  663. int ret = 0;
  664. switch (cmd) {
  665. case SNDRV_PCM_TRIGGER_START:
  666. case SNDRV_PCM_TRIGGER_RESUME:
  667. audio_pcm_playback_start(audio);
  668. break;
  669. case SNDRV_PCM_TRIGGER_STOP:
  670. case SNDRV_PCM_TRIGGER_SUSPEND:
  671. audio_pcm_playback_stop(audio);
  672. break;
  673. default:
  674. ret = -EINVAL;
  675. }
  676. return ret;
  677. }
  678. static int audio_pcm_mmap(struct snd_pcm_substream *substream,
  679. struct vm_area_struct *vma)
  680. {
  681. struct snd_pcm_runtime *runtime = substream->runtime;
  682. if (runtime->dma_addr && runtime->dma_bytes) {
  683. return dma_mmap_coherent(substream->pcm->card->dev, vma,
  684. runtime->dma_area,
  685. runtime->dma_addr,
  686. runtime->dma_bytes);
  687. } else {
  688. pr_err("Physical address or size of buf is NULL");
  689. return -EINVAL;
  690. }
  691. }
  692. static struct audio_dev _audio_dev = {
  693. .func = {
  694. .name = "audio_source",
  695. .bind = audio_bind,
  696. .unbind = audio_unbind,
  697. .set_alt = audio_set_alt,
  698. .setup = audio_setup,
  699. .disable = audio_disable,
  700. .fs_descriptors = fs_audio_desc,
  701. .hs_descriptors = hs_audio_desc,
  702. },
  703. .lock = __SPIN_LOCK_UNLOCKED(_audio_dev.lock),
  704. .idle_reqs = LIST_HEAD_INIT(_audio_dev.idle_reqs),
  705. };
  706. static struct snd_pcm_ops audio_playback_ops = {
  707. .open = audio_pcm_open,
  708. .close = audio_pcm_close,
  709. .ioctl = snd_pcm_lib_ioctl,
  710. .hw_params = audio_pcm_hw_params,
  711. .hw_free = audio_pcm_hw_free,
  712. .prepare = audio_pcm_prepare,
  713. .trigger = audio_pcm_playback_trigger,
  714. .pointer = audio_pcm_pointer,
  715. .mmap = audio_pcm_mmap,
  716. };
  717. int audio_source_bind_config(struct usb_configuration *c,
  718. struct audio_source_config *config)
  719. {
  720. struct audio_dev *audio;
  721. struct snd_card *card;
  722. struct snd_pcm *pcm;
  723. int err;
  724. config->card = -1;
  725. config->device = -1;
  726. audio = &_audio_dev;
  727. err = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
  728. THIS_MODULE, 0, &card);
  729. if (err)
  730. return err;
  731. snd_card_set_dev(card, &c->cdev->gadget->dev);
  732. err = snd_pcm_new(card, "USB audio source", 0, 1, 0, &pcm);
  733. if (err)
  734. goto pcm_fail;
  735. pcm->private_data = audio;
  736. pcm->info_flags = 0;
  737. audio->pcm = pcm;
  738. strlcpy(pcm->name, "USB gadget audio", sizeof(pcm->name));
  739. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &audio_playback_ops);
  740. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  741. NULL, 0, 64 * 1024);
  742. strlcpy(card->driver, "audio_source", sizeof(card->driver));
  743. strlcpy(card->shortname, card->driver, sizeof(card->shortname));
  744. strlcpy(card->longname, "USB accessory audio source",
  745. sizeof(card->longname));
  746. err = snd_card_register(card);
  747. if (err)
  748. goto register_fail;
  749. err = usb_add_function(c, &audio->func);
  750. if (err)
  751. goto add_fail;
  752. config->card = pcm->card->number;
  753. config->device = pcm->device;
  754. audio->card = card;
  755. return 0;
  756. add_fail:
  757. register_fail:
  758. pcm_fail:
  759. snd_card_free(audio->card);
  760. return err;
  761. }