pcm.c 36 KB

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