pcm.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License as published by
  4. * the Free Software Foundation; either version 2 of the License, or
  5. * (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software
  14. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. */
  16. #include <linux/init.h>
  17. #include <linux/slab.h>
  18. #include <linux/ratelimit.h>
  19. #include <linux/usb.h>
  20. #include <linux/usb/audio.h>
  21. #include <linux/usb/audio-v2.h>
  22. #include <sound/core.h>
  23. #include <sound/pcm.h>
  24. #include <sound/pcm_params.h>
  25. #include "usbaudio.h"
  26. #include "card.h"
  27. #include "quirks.h"
  28. #include "debug.h"
  29. #include "endpoint.h"
  30. #include "helper.h"
  31. #include "pcm.h"
  32. #include "clock.h"
  33. #include "power.h"
  34. #define SUBSTREAM_FLAG_DATA_EP_STARTED 0
  35. #define SUBSTREAM_FLAG_SYNC_EP_STARTED 1
  36. /* return the estimated delay based on USB frame counters */
  37. snd_pcm_uframes_t snd_usb_pcm_delay(struct snd_usb_substream *subs,
  38. unsigned int rate)
  39. {
  40. int current_frame_number;
  41. int frame_diff;
  42. int est_delay;
  43. current_frame_number = usb_get_current_frame_number(subs->dev);
  44. /*
  45. * HCD implementations use different widths, use lower 8 bits.
  46. * The delay will be managed up to 256ms, which is more than
  47. * enough
  48. */
  49. frame_diff = (current_frame_number - subs->last_frame_number) & 0xff;
  50. /* Approximation based on number of samples per USB frame (ms),
  51. some truncation for 44.1 but the estimate is good enough */
  52. est_delay = subs->last_delay - (frame_diff * rate / 1000);
  53. if (est_delay < 0)
  54. est_delay = 0;
  55. return est_delay;
  56. }
  57. /*
  58. * return the current pcm pointer. just based on the hwptr_done value.
  59. */
  60. static snd_pcm_uframes_t snd_usb_pcm_pointer(struct snd_pcm_substream *substream)
  61. {
  62. struct snd_usb_substream *subs;
  63. unsigned int hwptr_done;
  64. subs = (struct snd_usb_substream *)substream->runtime->private_data;
  65. if (subs->stream->chip->shutdown)
  66. return SNDRV_PCM_POS_XRUN;
  67. spin_lock(&subs->lock);
  68. hwptr_done = subs->hwptr_done;
  69. substream->runtime->delay = snd_usb_pcm_delay(subs,
  70. substream->runtime->rate);
  71. spin_unlock(&subs->lock);
  72. return hwptr_done / (substream->runtime->frame_bits >> 3);
  73. }
  74. /*
  75. * find a matching audio format
  76. */
  77. static struct audioformat *find_format(struct snd_usb_substream *subs)
  78. {
  79. struct list_head *p;
  80. struct audioformat *found = NULL;
  81. int cur_attr = 0, attr;
  82. list_for_each(p, &subs->fmt_list) {
  83. struct audioformat *fp;
  84. fp = list_entry(p, struct audioformat, list);
  85. if (!(fp->formats & (1uLL << subs->pcm_format)))
  86. continue;
  87. if (fp->channels != subs->channels)
  88. continue;
  89. if (subs->cur_rate < fp->rate_min ||
  90. subs->cur_rate > fp->rate_max)
  91. continue;
  92. if (! (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)) {
  93. unsigned int i;
  94. for (i = 0; i < fp->nr_rates; i++)
  95. if (fp->rate_table[i] == subs->cur_rate)
  96. break;
  97. if (i >= fp->nr_rates)
  98. continue;
  99. }
  100. attr = fp->ep_attr & USB_ENDPOINT_SYNCTYPE;
  101. if (! found) {
  102. found = fp;
  103. cur_attr = attr;
  104. continue;
  105. }
  106. /* avoid async out and adaptive in if the other method
  107. * supports the same format.
  108. * this is a workaround for the case like
  109. * M-audio audiophile USB.
  110. */
  111. if (attr != cur_attr) {
  112. if ((attr == USB_ENDPOINT_SYNC_ASYNC &&
  113. subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
  114. (attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
  115. subs->direction == SNDRV_PCM_STREAM_CAPTURE))
  116. continue;
  117. if ((cur_attr == USB_ENDPOINT_SYNC_ASYNC &&
  118. subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
  119. (cur_attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
  120. subs->direction == SNDRV_PCM_STREAM_CAPTURE)) {
  121. found = fp;
  122. cur_attr = attr;
  123. continue;
  124. }
  125. }
  126. /* find the format with the largest max. packet size */
  127. if (fp->maxpacksize > found->maxpacksize) {
  128. found = fp;
  129. cur_attr = attr;
  130. }
  131. }
  132. return found;
  133. }
  134. static int init_pitch_v1(struct snd_usb_audio *chip, int iface,
  135. struct usb_host_interface *alts,
  136. struct audioformat *fmt)
  137. {
  138. struct usb_device *dev = chip->dev;
  139. unsigned int ep;
  140. unsigned char data[1];
  141. int err;
  142. ep = get_endpoint(alts, 0)->bEndpointAddress;
  143. data[0] = 1;
  144. if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
  145. USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
  146. UAC_EP_CS_ATTR_PITCH_CONTROL << 8, ep,
  147. data, sizeof(data))) < 0) {
  148. snd_printk(KERN_ERR "%d:%d:%d: cannot set enable PITCH\n",
  149. dev->devnum, iface, ep);
  150. return err;
  151. }
  152. return 0;
  153. }
  154. static int init_pitch_v2(struct snd_usb_audio *chip, int iface,
  155. struct usb_host_interface *alts,
  156. struct audioformat *fmt)
  157. {
  158. struct usb_device *dev = chip->dev;
  159. unsigned char data[1];
  160. int err;
  161. data[0] = 1;
  162. if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC2_CS_CUR,
  163. USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_OUT,
  164. UAC2_EP_CS_PITCH << 8, 0,
  165. data, sizeof(data))) < 0) {
  166. snd_printk(KERN_ERR "%d:%d:%d: cannot set enable PITCH (v2)\n",
  167. dev->devnum, iface, fmt->altsetting);
  168. return err;
  169. }
  170. return 0;
  171. }
  172. /*
  173. * initialize the pitch control and sample rate
  174. */
  175. int snd_usb_init_pitch(struct snd_usb_audio *chip, int iface,
  176. struct usb_host_interface *alts,
  177. struct audioformat *fmt)
  178. {
  179. struct usb_interface_descriptor *altsd = get_iface_desc(alts);
  180. /* if endpoint doesn't have pitch control, bail out */
  181. if (!(fmt->attributes & UAC_EP_CS_ATTR_PITCH_CONTROL))
  182. return 0;
  183. switch (altsd->bInterfaceProtocol) {
  184. case UAC_VERSION_1:
  185. default:
  186. return init_pitch_v1(chip, iface, alts, fmt);
  187. case UAC_VERSION_2:
  188. return init_pitch_v2(chip, iface, alts, fmt);
  189. }
  190. }
  191. static int start_endpoints(struct snd_usb_substream *subs, bool can_sleep)
  192. {
  193. int err;
  194. if (!subs->data_endpoint)
  195. return -EINVAL;
  196. if (!test_and_set_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags)) {
  197. struct snd_usb_endpoint *ep = subs->data_endpoint;
  198. snd_printdd(KERN_DEBUG "Starting data EP @%p\n", ep);
  199. ep->data_subs = subs;
  200. err = snd_usb_endpoint_start(ep, can_sleep);
  201. if (err < 0) {
  202. clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags);
  203. return err;
  204. }
  205. }
  206. if (subs->sync_endpoint &&
  207. !test_and_set_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags)) {
  208. struct snd_usb_endpoint *ep = subs->sync_endpoint;
  209. if (subs->data_endpoint->iface != subs->sync_endpoint->iface ||
  210. subs->data_endpoint->alt_idx != subs->sync_endpoint->alt_idx) {
  211. err = usb_set_interface(subs->dev,
  212. subs->sync_endpoint->iface,
  213. subs->sync_endpoint->alt_idx);
  214. if (err < 0) {
  215. snd_printk(KERN_ERR
  216. "%d:%d:%d: cannot set interface (%d)\n",
  217. subs->dev->devnum,
  218. subs->sync_endpoint->iface,
  219. subs->sync_endpoint->alt_idx, err);
  220. return -EIO;
  221. }
  222. }
  223. snd_printdd(KERN_DEBUG "Starting sync EP @%p\n", ep);
  224. ep->sync_slave = subs->data_endpoint;
  225. err = snd_usb_endpoint_start(ep, can_sleep);
  226. if (err < 0) {
  227. clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags);
  228. return err;
  229. }
  230. }
  231. return 0;
  232. }
  233. static void stop_endpoints(struct snd_usb_substream *subs, bool wait)
  234. {
  235. if (test_and_clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags))
  236. snd_usb_endpoint_stop(subs->sync_endpoint, wait);
  237. if (test_and_clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags))
  238. snd_usb_endpoint_stop(subs->data_endpoint, wait);
  239. }
  240. static int deactivate_endpoints(struct snd_usb_substream *subs)
  241. {
  242. int reta, retb;
  243. reta = snd_usb_endpoint_deactivate(subs->sync_endpoint);
  244. retb = snd_usb_endpoint_deactivate(subs->data_endpoint);
  245. if (reta < 0)
  246. return reta;
  247. if (retb < 0)
  248. return retb;
  249. return 0;
  250. }
  251. /*
  252. * find a matching format and set up the interface
  253. */
  254. static int set_format(struct snd_usb_substream *subs, struct audioformat *fmt)
  255. {
  256. struct usb_device *dev = subs->dev;
  257. struct usb_host_interface *alts;
  258. struct usb_interface_descriptor *altsd;
  259. struct usb_interface *iface;
  260. unsigned int ep, attr;
  261. int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
  262. int err, implicit_fb = 0;
  263. iface = usb_ifnum_to_if(dev, fmt->iface);
  264. if (WARN_ON(!iface))
  265. return -EINVAL;
  266. alts = &iface->altsetting[fmt->altset_idx];
  267. altsd = get_iface_desc(alts);
  268. if (WARN_ON(altsd->bAlternateSetting != fmt->altsetting))
  269. return -EINVAL;
  270. if (fmt == subs->cur_audiofmt)
  271. return 0;
  272. /* close the old interface */
  273. if (subs->interface >= 0 && subs->interface != fmt->iface) {
  274. err = usb_set_interface(subs->dev, subs->interface, 0);
  275. if (err < 0) {
  276. snd_printk(KERN_ERR "%d:%d:%d: return to setting 0 failed (%d)\n",
  277. dev->devnum, fmt->iface, fmt->altsetting, err);
  278. return -EIO;
  279. }
  280. subs->interface = -1;
  281. subs->altset_idx = 0;
  282. }
  283. /* set interface */
  284. if (subs->interface != fmt->iface ||
  285. subs->altset_idx != fmt->altset_idx) {
  286. err = usb_set_interface(dev, fmt->iface, fmt->altsetting);
  287. if (err < 0) {
  288. snd_printk(KERN_ERR "%d:%d:%d: usb_set_interface failed (%d)\n",
  289. dev->devnum, fmt->iface, fmt->altsetting, err);
  290. return -EIO;
  291. }
  292. snd_printdd(KERN_INFO "setting usb interface %d:%d\n",
  293. fmt->iface, fmt->altsetting);
  294. subs->interface = fmt->iface;
  295. subs->altset_idx = fmt->altset_idx;
  296. }
  297. subs->data_endpoint = snd_usb_add_endpoint(subs->stream->chip,
  298. alts, fmt->endpoint, subs->direction,
  299. SND_USB_ENDPOINT_TYPE_DATA);
  300. if (!subs->data_endpoint)
  301. return -EINVAL;
  302. /* we need a sync pipe in async OUT or adaptive IN mode */
  303. /* check the number of EP, since some devices have broken
  304. * descriptors which fool us. if it has only one EP,
  305. * assume it as adaptive-out or sync-in.
  306. */
  307. attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE;
  308. switch (subs->stream->chip->usb_id) {
  309. case USB_ID(0x0763, 0x2080): /* M-Audio FastTrack Ultra */
  310. case USB_ID(0x0763, 0x2081):
  311. if (is_playback) {
  312. implicit_fb = 1;
  313. ep = 0x81;
  314. iface = usb_ifnum_to_if(dev, 2);
  315. if (!iface || iface->num_altsetting == 0)
  316. return -EINVAL;
  317. alts = &iface->altsetting[1];
  318. goto add_sync_ep;
  319. }
  320. }
  321. if (((is_playback && attr == USB_ENDPOINT_SYNC_ASYNC) ||
  322. (!is_playback && attr == USB_ENDPOINT_SYNC_ADAPTIVE)) &&
  323. altsd->bNumEndpoints >= 2) {
  324. /* check sync-pipe endpoint */
  325. /* ... and check descriptor size before accessing bSynchAddress
  326. because there is a version of the SB Audigy 2 NX firmware lacking
  327. the audio fields in the endpoint descriptors */
  328. if ((get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != 0x01 ||
  329. (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
  330. get_endpoint(alts, 1)->bSynchAddress != 0 &&
  331. !implicit_fb)) {
  332. snd_printk(KERN_ERR "%d:%d:%d : invalid sync pipe. bmAttributes %02x, bLength %d, bSynchAddress %02x\n",
  333. dev->devnum, fmt->iface, fmt->altsetting,
  334. get_endpoint(alts, 1)->bmAttributes,
  335. get_endpoint(alts, 1)->bLength,
  336. get_endpoint(alts, 1)->bSynchAddress);
  337. return -EINVAL;
  338. }
  339. ep = get_endpoint(alts, 1)->bEndpointAddress;
  340. if (!implicit_fb &&
  341. get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
  342. (( is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) ||
  343. (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) {
  344. snd_printk(KERN_ERR "%d:%d:%d : invalid sync pipe. is_playback %d, ep %02x, bSynchAddress %02x\n",
  345. dev->devnum, fmt->iface, fmt->altsetting,
  346. is_playback, ep, get_endpoint(alts, 0)->bSynchAddress);
  347. return -EINVAL;
  348. }
  349. implicit_fb = (get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_USAGE_MASK)
  350. == USB_ENDPOINT_USAGE_IMPLICIT_FB;
  351. add_sync_ep:
  352. subs->sync_endpoint = snd_usb_add_endpoint(subs->stream->chip,
  353. alts, ep, !subs->direction,
  354. implicit_fb ?
  355. SND_USB_ENDPOINT_TYPE_DATA :
  356. SND_USB_ENDPOINT_TYPE_SYNC);
  357. if (!subs->sync_endpoint)
  358. return -EINVAL;
  359. subs->data_endpoint->sync_master = subs->sync_endpoint;
  360. }
  361. if ((err = snd_usb_init_pitch(subs->stream->chip, fmt->iface, alts, fmt)) < 0)
  362. return err;
  363. subs->cur_audiofmt = fmt;
  364. snd_usb_set_format_quirk(subs, fmt);
  365. #if 0
  366. printk(KERN_DEBUG
  367. "setting done: format = %d, rate = %d..%d, channels = %d\n",
  368. fmt->format, fmt->rate_min, fmt->rate_max, fmt->channels);
  369. printk(KERN_DEBUG
  370. " datapipe = 0x%0x, syncpipe = 0x%0x\n",
  371. subs->datapipe, subs->syncpipe);
  372. #endif
  373. return 0;
  374. }
  375. /*
  376. * configure endpoint params
  377. *
  378. * called during initial setup and upon resume
  379. */
  380. static int configure_endpoint(struct snd_usb_substream *subs)
  381. {
  382. int ret;
  383. /* format changed */
  384. stop_endpoints(subs, false);
  385. ret = snd_usb_endpoint_set_params(subs->data_endpoint,
  386. subs->pcm_format,
  387. subs->channels,
  388. subs->period_bytes,
  389. subs->cur_rate,
  390. subs->cur_audiofmt,
  391. subs->sync_endpoint);
  392. if (ret < 0)
  393. return ret;
  394. if (subs->sync_endpoint)
  395. ret = snd_usb_endpoint_set_params(subs->data_endpoint,
  396. subs->pcm_format,
  397. subs->channels,
  398. subs->period_bytes,
  399. subs->cur_rate,
  400. subs->cur_audiofmt,
  401. NULL);
  402. return ret;
  403. }
  404. /*
  405. * hw_params callback
  406. *
  407. * allocate a buffer and set the given audio format.
  408. *
  409. * so far we use a physically linear buffer although packetize transfer
  410. * doesn't need a continuous area.
  411. * if sg buffer is supported on the later version of alsa, we'll follow
  412. * that.
  413. */
  414. static int snd_usb_hw_params(struct snd_pcm_substream *substream,
  415. struct snd_pcm_hw_params *hw_params)
  416. {
  417. struct snd_usb_substream *subs = substream->runtime->private_data;
  418. struct audioformat *fmt;
  419. int ret;
  420. ret = snd_pcm_lib_alloc_vmalloc_buffer(substream,
  421. params_buffer_bytes(hw_params));
  422. if (ret < 0)
  423. return ret;
  424. subs->pcm_format = params_format(hw_params);
  425. subs->period_bytes = params_period_bytes(hw_params);
  426. subs->channels = params_channels(hw_params);
  427. subs->cur_rate = params_rate(hw_params);
  428. fmt = find_format(subs);
  429. if (!fmt) {
  430. snd_printd(KERN_DEBUG "cannot set format: format = %#x, rate = %d, channels = %d\n",
  431. subs->pcm_format, subs->cur_rate, subs->channels);
  432. return -EINVAL;
  433. }
  434. down_read(&subs->stream->chip->shutdown_rwsem);
  435. if (subs->stream->chip->shutdown)
  436. ret = -ENODEV;
  437. else
  438. ret = set_format(subs, fmt);
  439. up_read(&subs->stream->chip->shutdown_rwsem);
  440. if (ret < 0)
  441. return ret;
  442. subs->interface = fmt->iface;
  443. subs->altset_idx = fmt->altset_idx;
  444. subs->need_setup_ep = true;
  445. return 0;
  446. }
  447. /*
  448. * hw_free callback
  449. *
  450. * reset the audio format and release the buffer
  451. */
  452. static int snd_usb_hw_free(struct snd_pcm_substream *substream)
  453. {
  454. struct snd_usb_substream *subs = substream->runtime->private_data;
  455. subs->cur_audiofmt = NULL;
  456. subs->cur_rate = 0;
  457. subs->period_bytes = 0;
  458. down_read(&subs->stream->chip->shutdown_rwsem);
  459. if (!subs->stream->chip->shutdown) {
  460. stop_endpoints(subs, true);
  461. deactivate_endpoints(subs);
  462. }
  463. up_read(&subs->stream->chip->shutdown_rwsem);
  464. return snd_pcm_lib_free_vmalloc_buffer(substream);
  465. }
  466. /*
  467. * prepare callback
  468. *
  469. * only a few subtle things...
  470. */
  471. static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream)
  472. {
  473. struct snd_pcm_runtime *runtime = substream->runtime;
  474. struct snd_usb_substream *subs = runtime->private_data;
  475. struct usb_host_interface *alts;
  476. struct usb_interface *iface;
  477. int ret;
  478. if (! subs->cur_audiofmt) {
  479. snd_printk(KERN_ERR "usbaudio: no format is specified!\n");
  480. return -ENXIO;
  481. }
  482. down_read(&subs->stream->chip->shutdown_rwsem);
  483. if (subs->stream->chip->shutdown) {
  484. ret = -ENODEV;
  485. goto unlock;
  486. }
  487. if (snd_BUG_ON(!subs->data_endpoint)) {
  488. ret = -EIO;
  489. goto unlock;
  490. }
  491. snd_usb_endpoint_sync_pending_stop(subs->sync_endpoint);
  492. snd_usb_endpoint_sync_pending_stop(subs->data_endpoint);
  493. ret = set_format(subs, subs->cur_audiofmt);
  494. if (ret < 0)
  495. goto unlock;
  496. iface = usb_ifnum_to_if(subs->dev, subs->cur_audiofmt->iface);
  497. alts = &iface->altsetting[subs->cur_audiofmt->altset_idx];
  498. ret = snd_usb_init_sample_rate(subs->stream->chip,
  499. subs->cur_audiofmt->iface,
  500. alts,
  501. subs->cur_audiofmt,
  502. subs->cur_rate);
  503. if (ret < 0)
  504. goto unlock;
  505. if (subs->need_setup_ep) {
  506. ret = configure_endpoint(subs);
  507. if (ret < 0)
  508. goto unlock;
  509. subs->need_setup_ep = false;
  510. }
  511. /* some unit conversions in runtime */
  512. subs->data_endpoint->maxframesize =
  513. bytes_to_frames(runtime, subs->data_endpoint->maxpacksize);
  514. subs->data_endpoint->curframesize =
  515. bytes_to_frames(runtime, subs->data_endpoint->curpacksize);
  516. /* reset the pointer */
  517. subs->hwptr_done = 0;
  518. subs->transfer_done = 0;
  519. subs->last_delay = 0;
  520. subs->last_frame_number = 0;
  521. runtime->delay = 0;
  522. /* for playback, submit the URBs now; otherwise, the first hwptr_done
  523. * updates for all URBs would happen at the same time when starting */
  524. if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK)
  525. ret = start_endpoints(subs, true);
  526. unlock:
  527. up_read(&subs->stream->chip->shutdown_rwsem);
  528. return ret;
  529. }
  530. static struct snd_pcm_hardware snd_usb_hardware =
  531. {
  532. .info = SNDRV_PCM_INFO_MMAP |
  533. SNDRV_PCM_INFO_MMAP_VALID |
  534. SNDRV_PCM_INFO_BATCH |
  535. SNDRV_PCM_INFO_INTERLEAVED |
  536. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  537. SNDRV_PCM_INFO_PAUSE,
  538. .buffer_bytes_max = 1024 * 1024,
  539. .period_bytes_min = 64,
  540. .period_bytes_max = 512 * 1024,
  541. .periods_min = 2,
  542. .periods_max = 1024,
  543. };
  544. static int hw_check_valid_format(struct snd_usb_substream *subs,
  545. struct snd_pcm_hw_params *params,
  546. struct audioformat *fp)
  547. {
  548. struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
  549. struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
  550. struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
  551. struct snd_interval *pt = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
  552. struct snd_mask check_fmts;
  553. unsigned int ptime;
  554. /* check the format */
  555. snd_mask_none(&check_fmts);
  556. check_fmts.bits[0] = (u32)fp->formats;
  557. check_fmts.bits[1] = (u32)(fp->formats >> 32);
  558. snd_mask_intersect(&check_fmts, fmts);
  559. if (snd_mask_empty(&check_fmts)) {
  560. hwc_debug(" > check: no supported format %d\n", fp->format);
  561. return 0;
  562. }
  563. /* check the channels */
  564. if (fp->channels < ct->min || fp->channels > ct->max) {
  565. hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max);
  566. return 0;
  567. }
  568. /* check the rate is within the range */
  569. if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) {
  570. hwc_debug(" > check: rate_min %d > max %d\n", fp->rate_min, it->max);
  571. return 0;
  572. }
  573. if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) {
  574. hwc_debug(" > check: rate_max %d < min %d\n", fp->rate_max, it->min);
  575. return 0;
  576. }
  577. /* check whether the period time is >= the data packet interval */
  578. if (subs->speed != USB_SPEED_FULL) {
  579. ptime = 125 * (1 << fp->datainterval);
  580. if (ptime > pt->max || (ptime == pt->max && pt->openmax)) {
  581. hwc_debug(" > check: ptime %u > max %u\n", ptime, pt->max);
  582. return 0;
  583. }
  584. }
  585. return 1;
  586. }
  587. static int hw_rule_rate(struct snd_pcm_hw_params *params,
  588. struct snd_pcm_hw_rule *rule)
  589. {
  590. struct snd_usb_substream *subs = rule->private;
  591. struct list_head *p;
  592. struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
  593. unsigned int rmin, rmax;
  594. int changed;
  595. hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max);
  596. changed = 0;
  597. rmin = rmax = 0;
  598. list_for_each(p, &subs->fmt_list) {
  599. struct audioformat *fp;
  600. fp = list_entry(p, struct audioformat, list);
  601. if (!hw_check_valid_format(subs, params, fp))
  602. continue;
  603. if (changed++) {
  604. if (rmin > fp->rate_min)
  605. rmin = fp->rate_min;
  606. if (rmax < fp->rate_max)
  607. rmax = fp->rate_max;
  608. } else {
  609. rmin = fp->rate_min;
  610. rmax = fp->rate_max;
  611. }
  612. }
  613. if (!changed) {
  614. hwc_debug(" --> get empty\n");
  615. it->empty = 1;
  616. return -EINVAL;
  617. }
  618. changed = 0;
  619. if (it->min < rmin) {
  620. it->min = rmin;
  621. it->openmin = 0;
  622. changed = 1;
  623. }
  624. if (it->max > rmax) {
  625. it->max = rmax;
  626. it->openmax = 0;
  627. changed = 1;
  628. }
  629. if (snd_interval_checkempty(it)) {
  630. it->empty = 1;
  631. return -EINVAL;
  632. }
  633. hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
  634. return changed;
  635. }
  636. static int hw_rule_channels(struct snd_pcm_hw_params *params,
  637. struct snd_pcm_hw_rule *rule)
  638. {
  639. struct snd_usb_substream *subs = rule->private;
  640. struct list_head *p;
  641. struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
  642. unsigned int rmin, rmax;
  643. int changed;
  644. hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max);
  645. changed = 0;
  646. rmin = rmax = 0;
  647. list_for_each(p, &subs->fmt_list) {
  648. struct audioformat *fp;
  649. fp = list_entry(p, struct audioformat, list);
  650. if (!hw_check_valid_format(subs, params, fp))
  651. continue;
  652. if (changed++) {
  653. if (rmin > fp->channels)
  654. rmin = fp->channels;
  655. if (rmax < fp->channels)
  656. rmax = fp->channels;
  657. } else {
  658. rmin = fp->channels;
  659. rmax = fp->channels;
  660. }
  661. }
  662. if (!changed) {
  663. hwc_debug(" --> get empty\n");
  664. it->empty = 1;
  665. return -EINVAL;
  666. }
  667. changed = 0;
  668. if (it->min < rmin) {
  669. it->min = rmin;
  670. it->openmin = 0;
  671. changed = 1;
  672. }
  673. if (it->max > rmax) {
  674. it->max = rmax;
  675. it->openmax = 0;
  676. changed = 1;
  677. }
  678. if (snd_interval_checkempty(it)) {
  679. it->empty = 1;
  680. return -EINVAL;
  681. }
  682. hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
  683. return changed;
  684. }
  685. static int hw_rule_format(struct snd_pcm_hw_params *params,
  686. struct snd_pcm_hw_rule *rule)
  687. {
  688. struct snd_usb_substream *subs = rule->private;
  689. struct list_head *p;
  690. struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
  691. u64 fbits;
  692. u32 oldbits[2];
  693. int changed;
  694. hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]);
  695. fbits = 0;
  696. list_for_each(p, &subs->fmt_list) {
  697. struct audioformat *fp;
  698. fp = list_entry(p, struct audioformat, list);
  699. if (!hw_check_valid_format(subs, params, fp))
  700. continue;
  701. fbits |= fp->formats;
  702. }
  703. oldbits[0] = fmt->bits[0];
  704. oldbits[1] = fmt->bits[1];
  705. fmt->bits[0] &= (u32)fbits;
  706. fmt->bits[1] &= (u32)(fbits >> 32);
  707. if (!fmt->bits[0] && !fmt->bits[1]) {
  708. hwc_debug(" --> get empty\n");
  709. return -EINVAL;
  710. }
  711. changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]);
  712. hwc_debug(" --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed);
  713. return changed;
  714. }
  715. static int hw_rule_period_time(struct snd_pcm_hw_params *params,
  716. struct snd_pcm_hw_rule *rule)
  717. {
  718. struct snd_usb_substream *subs = rule->private;
  719. struct audioformat *fp;
  720. struct snd_interval *it;
  721. unsigned char min_datainterval;
  722. unsigned int pmin;
  723. int changed;
  724. it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
  725. hwc_debug("hw_rule_period_time: (%u,%u)\n", it->min, it->max);
  726. min_datainterval = 0xff;
  727. list_for_each_entry(fp, &subs->fmt_list, list) {
  728. if (!hw_check_valid_format(subs, params, fp))
  729. continue;
  730. min_datainterval = min(min_datainterval, fp->datainterval);
  731. }
  732. if (min_datainterval == 0xff) {
  733. hwc_debug(" --> get empty\n");
  734. it->empty = 1;
  735. return -EINVAL;
  736. }
  737. pmin = 125 * (1 << min_datainterval);
  738. changed = 0;
  739. if (it->min < pmin) {
  740. it->min = pmin;
  741. it->openmin = 0;
  742. changed = 1;
  743. }
  744. if (snd_interval_checkempty(it)) {
  745. it->empty = 1;
  746. return -EINVAL;
  747. }
  748. hwc_debug(" --> (%u,%u) (changed = %d)\n", it->min, it->max, changed);
  749. return changed;
  750. }
  751. /*
  752. * If the device supports unusual bit rates, does the request meet these?
  753. */
  754. static int snd_usb_pcm_check_knot(struct snd_pcm_runtime *runtime,
  755. struct snd_usb_substream *subs)
  756. {
  757. struct audioformat *fp;
  758. int *rate_list;
  759. int count = 0, needs_knot = 0;
  760. int err;
  761. kfree(subs->rate_list.list);
  762. subs->rate_list.list = NULL;
  763. list_for_each_entry(fp, &subs->fmt_list, list) {
  764. if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)
  765. return 0;
  766. count += fp->nr_rates;
  767. if (fp->rates & SNDRV_PCM_RATE_KNOT)
  768. needs_knot = 1;
  769. }
  770. if (!needs_knot)
  771. return 0;
  772. subs->rate_list.list = rate_list =
  773. kmalloc(sizeof(int) * count, GFP_KERNEL);
  774. if (!subs->rate_list.list)
  775. return -ENOMEM;
  776. subs->rate_list.count = count;
  777. subs->rate_list.mask = 0;
  778. count = 0;
  779. list_for_each_entry(fp, &subs->fmt_list, list) {
  780. int i;
  781. for (i = 0; i < fp->nr_rates; i++)
  782. rate_list[count++] = fp->rate_table[i];
  783. }
  784. err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  785. &subs->rate_list);
  786. if (err < 0)
  787. return err;
  788. return 0;
  789. }
  790. /*
  791. * set up the runtime hardware information.
  792. */
  793. static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs)
  794. {
  795. struct list_head *p;
  796. unsigned int pt, ptmin;
  797. int param_period_time_if_needed;
  798. int err;
  799. runtime->hw.formats = subs->formats;
  800. runtime->hw.rate_min = 0x7fffffff;
  801. runtime->hw.rate_max = 0;
  802. runtime->hw.channels_min = 256;
  803. runtime->hw.channels_max = 0;
  804. runtime->hw.rates = 0;
  805. ptmin = UINT_MAX;
  806. /* check min/max rates and channels */
  807. list_for_each(p, &subs->fmt_list) {
  808. struct audioformat *fp;
  809. fp = list_entry(p, struct audioformat, list);
  810. runtime->hw.rates |= fp->rates;
  811. if (runtime->hw.rate_min > fp->rate_min)
  812. runtime->hw.rate_min = fp->rate_min;
  813. if (runtime->hw.rate_max < fp->rate_max)
  814. runtime->hw.rate_max = fp->rate_max;
  815. if (runtime->hw.channels_min > fp->channels)
  816. runtime->hw.channels_min = fp->channels;
  817. if (runtime->hw.channels_max < fp->channels)
  818. runtime->hw.channels_max = fp->channels;
  819. if (fp->fmt_type == UAC_FORMAT_TYPE_II && fp->frame_size > 0) {
  820. /* FIXME: there might be more than one audio formats... */
  821. runtime->hw.period_bytes_min = runtime->hw.period_bytes_max =
  822. fp->frame_size;
  823. }
  824. pt = 125 * (1 << fp->datainterval);
  825. ptmin = min(ptmin, pt);
  826. }
  827. err = snd_usb_autoresume(subs->stream->chip);
  828. if (err < 0)
  829. return err;
  830. param_period_time_if_needed = SNDRV_PCM_HW_PARAM_PERIOD_TIME;
  831. if (subs->speed == USB_SPEED_FULL)
  832. /* full speed devices have fixed data packet interval */
  833. ptmin = 1000;
  834. if (ptmin == 1000)
  835. /* if period time doesn't go below 1 ms, no rules needed */
  836. param_period_time_if_needed = -1;
  837. snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
  838. ptmin, UINT_MAX);
  839. if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  840. hw_rule_rate, subs,
  841. SNDRV_PCM_HW_PARAM_FORMAT,
  842. SNDRV_PCM_HW_PARAM_CHANNELS,
  843. param_period_time_if_needed,
  844. -1)) < 0)
  845. goto rep_err;
  846. if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
  847. hw_rule_channels, subs,
  848. SNDRV_PCM_HW_PARAM_FORMAT,
  849. SNDRV_PCM_HW_PARAM_RATE,
  850. param_period_time_if_needed,
  851. -1)) < 0)
  852. goto rep_err;
  853. if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
  854. hw_rule_format, subs,
  855. SNDRV_PCM_HW_PARAM_RATE,
  856. SNDRV_PCM_HW_PARAM_CHANNELS,
  857. param_period_time_if_needed,
  858. -1)) < 0)
  859. goto rep_err;
  860. if (param_period_time_if_needed >= 0) {
  861. err = snd_pcm_hw_rule_add(runtime, 0,
  862. SNDRV_PCM_HW_PARAM_PERIOD_TIME,
  863. hw_rule_period_time, subs,
  864. SNDRV_PCM_HW_PARAM_FORMAT,
  865. SNDRV_PCM_HW_PARAM_CHANNELS,
  866. SNDRV_PCM_HW_PARAM_RATE,
  867. -1);
  868. if (err < 0)
  869. goto rep_err;
  870. }
  871. if ((err = snd_usb_pcm_check_knot(runtime, subs)) < 0)
  872. goto rep_err;
  873. return 0;
  874. rep_err:
  875. snd_usb_autosuspend(subs->stream->chip);
  876. return err;
  877. }
  878. static int snd_usb_pcm_open(struct snd_pcm_substream *substream, int direction)
  879. {
  880. struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
  881. struct snd_pcm_runtime *runtime = substream->runtime;
  882. struct snd_usb_substream *subs = &as->substream[direction];
  883. subs->interface = -1;
  884. subs->altset_idx = 0;
  885. runtime->hw = snd_usb_hardware;
  886. runtime->private_data = subs;
  887. subs->pcm_substream = substream;
  888. /* runtime PM is also done there */
  889. return setup_hw_info(runtime, subs);
  890. }
  891. static int snd_usb_pcm_close(struct snd_pcm_substream *substream, int direction)
  892. {
  893. struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
  894. struct snd_usb_substream *subs = &as->substream[direction];
  895. stop_endpoints(subs, false);
  896. if (!as->chip->shutdown && subs->interface >= 0) {
  897. usb_set_interface(subs->dev, subs->interface, 0);
  898. subs->interface = -1;
  899. }
  900. subs->pcm_substream = NULL;
  901. snd_usb_autosuspend(subs->stream->chip);
  902. return 0;
  903. }
  904. /* Since a URB can handle only a single linear buffer, we must use double
  905. * buffering when the data to be transferred overflows the buffer boundary.
  906. * To avoid inconsistencies when updating hwptr_done, we use double buffering
  907. * for all URBs.
  908. */
  909. static void retire_capture_urb(struct snd_usb_substream *subs,
  910. struct urb *urb)
  911. {
  912. struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
  913. unsigned int stride, frames, bytes, oldptr;
  914. int i, period_elapsed = 0;
  915. unsigned long flags;
  916. unsigned char *cp;
  917. stride = runtime->frame_bits >> 3;
  918. for (i = 0; i < urb->number_of_packets; i++) {
  919. cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset;
  920. if (urb->iso_frame_desc[i].status && printk_ratelimit()) {
  921. snd_printdd(KERN_ERR "frame %d active: %d\n", i, urb->iso_frame_desc[i].status);
  922. // continue;
  923. }
  924. bytes = urb->iso_frame_desc[i].actual_length;
  925. frames = bytes / stride;
  926. if (!subs->txfr_quirk)
  927. bytes = frames * stride;
  928. if (bytes % (runtime->sample_bits >> 3) != 0) {
  929. #ifdef CONFIG_SND_DEBUG_VERBOSE
  930. int oldbytes = bytes;
  931. #endif
  932. bytes = frames * stride;
  933. snd_printdd(KERN_ERR "Corrected urb data len. %d->%d\n",
  934. oldbytes, bytes);
  935. }
  936. /* update the current pointer */
  937. spin_lock_irqsave(&subs->lock, flags);
  938. oldptr = subs->hwptr_done;
  939. subs->hwptr_done += bytes;
  940. if (subs->hwptr_done >= runtime->buffer_size * stride)
  941. subs->hwptr_done -= runtime->buffer_size * stride;
  942. frames = (bytes + (oldptr % stride)) / stride;
  943. subs->transfer_done += frames;
  944. if (subs->transfer_done >= runtime->period_size) {
  945. subs->transfer_done -= runtime->period_size;
  946. period_elapsed = 1;
  947. }
  948. spin_unlock_irqrestore(&subs->lock, flags);
  949. /* copy a data chunk */
  950. if (oldptr + bytes > runtime->buffer_size * stride) {
  951. unsigned int bytes1 =
  952. runtime->buffer_size * stride - oldptr;
  953. memcpy(runtime->dma_area + oldptr, cp, bytes1);
  954. memcpy(runtime->dma_area, cp + bytes1, bytes - bytes1);
  955. } else {
  956. memcpy(runtime->dma_area + oldptr, cp, bytes);
  957. }
  958. }
  959. if (period_elapsed)
  960. snd_pcm_period_elapsed(subs->pcm_substream);
  961. }
  962. static void prepare_playback_urb(struct snd_usb_substream *subs,
  963. struct urb *urb)
  964. {
  965. struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
  966. struct snd_usb_endpoint *ep = subs->data_endpoint;
  967. struct snd_urb_ctx *ctx = urb->context;
  968. unsigned int counts, frames, bytes;
  969. int i, stride, period_elapsed = 0;
  970. unsigned long flags;
  971. stride = runtime->frame_bits >> 3;
  972. frames = 0;
  973. urb->number_of_packets = 0;
  974. spin_lock_irqsave(&subs->lock, flags);
  975. for (i = 0; i < ctx->packets; i++) {
  976. if (ctx->packet_size[i])
  977. counts = ctx->packet_size[i];
  978. else
  979. counts = snd_usb_endpoint_next_packet_size(ep);
  980. /* set up descriptor */
  981. urb->iso_frame_desc[i].offset = frames * stride;
  982. urb->iso_frame_desc[i].length = counts * stride;
  983. frames += counts;
  984. urb->number_of_packets++;
  985. subs->transfer_done += counts;
  986. if (subs->transfer_done >= runtime->period_size) {
  987. subs->transfer_done -= runtime->period_size;
  988. period_elapsed = 1;
  989. if (subs->fmt_type == UAC_FORMAT_TYPE_II) {
  990. if (subs->transfer_done > 0) {
  991. /* FIXME: fill-max mode is not
  992. * supported yet */
  993. frames -= subs->transfer_done;
  994. counts -= subs->transfer_done;
  995. urb->iso_frame_desc[i].length =
  996. counts * stride;
  997. subs->transfer_done = 0;
  998. }
  999. i++;
  1000. if (i < ctx->packets) {
  1001. /* add a transfer delimiter */
  1002. urb->iso_frame_desc[i].offset =
  1003. frames * stride;
  1004. urb->iso_frame_desc[i].length = 0;
  1005. urb->number_of_packets++;
  1006. }
  1007. break;
  1008. }
  1009. }
  1010. if (period_elapsed &&
  1011. !snd_usb_endpoint_implict_feedback_sink(subs->data_endpoint)) /* finish at the period boundary */
  1012. break;
  1013. }
  1014. bytes = frames * stride;
  1015. if (subs->hwptr_done + bytes > runtime->buffer_size * stride) {
  1016. /* err, the transferred area goes over buffer boundary. */
  1017. unsigned int bytes1 =
  1018. runtime->buffer_size * stride - subs->hwptr_done;
  1019. memcpy(urb->transfer_buffer,
  1020. runtime->dma_area + subs->hwptr_done, bytes1);
  1021. memcpy(urb->transfer_buffer + bytes1,
  1022. runtime->dma_area, bytes - bytes1);
  1023. } else {
  1024. memcpy(urb->transfer_buffer,
  1025. runtime->dma_area + subs->hwptr_done, bytes);
  1026. }
  1027. subs->hwptr_done += bytes;
  1028. if (subs->hwptr_done >= runtime->buffer_size * stride)
  1029. subs->hwptr_done -= runtime->buffer_size * stride;
  1030. /* update delay with exact number of samples queued */
  1031. runtime->delay = subs->last_delay;
  1032. runtime->delay += frames;
  1033. subs->last_delay = runtime->delay;
  1034. /* realign last_frame_number */
  1035. subs->last_frame_number = usb_get_current_frame_number(subs->dev);
  1036. subs->last_frame_number &= 0xFF; /* keep 8 LSBs */
  1037. spin_unlock_irqrestore(&subs->lock, flags);
  1038. urb->transfer_buffer_length = bytes;
  1039. if (period_elapsed)
  1040. snd_pcm_period_elapsed(subs->pcm_substream);
  1041. }
  1042. /*
  1043. * process after playback data complete
  1044. * - decrease the delay count again
  1045. */
  1046. static void retire_playback_urb(struct snd_usb_substream *subs,
  1047. struct urb *urb)
  1048. {
  1049. unsigned long flags;
  1050. struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
  1051. int stride = runtime->frame_bits >> 3;
  1052. int processed = urb->transfer_buffer_length / stride;
  1053. int est_delay;
  1054. /* ignore the delay accounting when procssed=0 is given, i.e.
  1055. * silent payloads are procssed before handling the actual data
  1056. */
  1057. if (!processed)
  1058. return;
  1059. spin_lock_irqsave(&subs->lock, flags);
  1060. est_delay = snd_usb_pcm_delay(subs, runtime->rate);
  1061. /* update delay with exact number of samples played */
  1062. if (processed > subs->last_delay)
  1063. subs->last_delay = 0;
  1064. else
  1065. subs->last_delay -= processed;
  1066. runtime->delay = subs->last_delay;
  1067. /*
  1068. * Report when delay estimate is off by more than 2ms.
  1069. * The error should be lower than 2ms since the estimate relies
  1070. * on two reads of a counter updated every ms.
  1071. */
  1072. if (abs(est_delay - subs->last_delay) * 1000 > runtime->rate * 2)
  1073. snd_printk(KERN_DEBUG "delay: estimated %d, actual %d\n",
  1074. est_delay, subs->last_delay);
  1075. spin_unlock_irqrestore(&subs->lock, flags);
  1076. }
  1077. static int snd_usb_playback_open(struct snd_pcm_substream *substream)
  1078. {
  1079. return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_PLAYBACK);
  1080. }
  1081. static int snd_usb_playback_close(struct snd_pcm_substream *substream)
  1082. {
  1083. return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_PLAYBACK);
  1084. }
  1085. static int snd_usb_capture_open(struct snd_pcm_substream *substream)
  1086. {
  1087. return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_CAPTURE);
  1088. }
  1089. static int snd_usb_capture_close(struct snd_pcm_substream *substream)
  1090. {
  1091. return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_CAPTURE);
  1092. }
  1093. static int snd_usb_substream_playback_trigger(struct snd_pcm_substream *substream,
  1094. int cmd)
  1095. {
  1096. struct snd_usb_substream *subs = substream->runtime->private_data;
  1097. switch (cmd) {
  1098. case SNDRV_PCM_TRIGGER_START:
  1099. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  1100. subs->data_endpoint->prepare_data_urb = prepare_playback_urb;
  1101. subs->data_endpoint->retire_data_urb = retire_playback_urb;
  1102. subs->running = 1;
  1103. return 0;
  1104. case SNDRV_PCM_TRIGGER_STOP:
  1105. stop_endpoints(subs, false);
  1106. subs->running = 0;
  1107. return 0;
  1108. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  1109. subs->data_endpoint->prepare_data_urb = NULL;
  1110. subs->data_endpoint->retire_data_urb = NULL;
  1111. subs->running = 0;
  1112. return 0;
  1113. }
  1114. return -EINVAL;
  1115. }
  1116. static int snd_usb_substream_capture_trigger(struct snd_pcm_substream *substream,
  1117. int cmd)
  1118. {
  1119. int err;
  1120. struct snd_usb_substream *subs = substream->runtime->private_data;
  1121. switch (cmd) {
  1122. case SNDRV_PCM_TRIGGER_START:
  1123. err = start_endpoints(subs, false);
  1124. if (err < 0)
  1125. return err;
  1126. subs->data_endpoint->retire_data_urb = retire_capture_urb;
  1127. subs->running = 1;
  1128. return 0;
  1129. case SNDRV_PCM_TRIGGER_STOP:
  1130. stop_endpoints(subs, false);
  1131. subs->running = 0;
  1132. return 0;
  1133. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  1134. subs->data_endpoint->retire_data_urb = NULL;
  1135. subs->running = 0;
  1136. return 0;
  1137. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  1138. subs->data_endpoint->retire_data_urb = retire_capture_urb;
  1139. subs->running = 1;
  1140. return 0;
  1141. }
  1142. return -EINVAL;
  1143. }
  1144. static struct snd_pcm_ops snd_usb_playback_ops = {
  1145. .open = snd_usb_playback_open,
  1146. .close = snd_usb_playback_close,
  1147. .ioctl = snd_pcm_lib_ioctl,
  1148. .hw_params = snd_usb_hw_params,
  1149. .hw_free = snd_usb_hw_free,
  1150. .prepare = snd_usb_pcm_prepare,
  1151. .trigger = snd_usb_substream_playback_trigger,
  1152. .pointer = snd_usb_pcm_pointer,
  1153. .page = snd_pcm_lib_get_vmalloc_page,
  1154. .mmap = snd_pcm_lib_mmap_vmalloc,
  1155. };
  1156. static struct snd_pcm_ops snd_usb_capture_ops = {
  1157. .open = snd_usb_capture_open,
  1158. .close = snd_usb_capture_close,
  1159. .ioctl = snd_pcm_lib_ioctl,
  1160. .hw_params = snd_usb_hw_params,
  1161. .hw_free = snd_usb_hw_free,
  1162. .prepare = snd_usb_pcm_prepare,
  1163. .trigger = snd_usb_substream_capture_trigger,
  1164. .pointer = snd_usb_pcm_pointer,
  1165. .page = snd_pcm_lib_get_vmalloc_page,
  1166. .mmap = snd_pcm_lib_mmap_vmalloc,
  1167. };
  1168. void snd_usb_set_pcm_ops(struct snd_pcm *pcm, int stream)
  1169. {
  1170. snd_pcm_set_ops(pcm, stream,
  1171. stream == SNDRV_PCM_STREAM_PLAYBACK ?
  1172. &snd_usb_playback_ops : &snd_usb_capture_ops);
  1173. }