pcm.c 37 KB

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