pcm.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324
  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->data_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. ret = set_format(subs, subs->cur_audiofmt);
  497. if (ret < 0)
  498. goto unlock;
  499. iface = usb_ifnum_to_if(subs->dev, subs->cur_audiofmt->iface);
  500. alts = &iface->altsetting[subs->cur_audiofmt->altset_idx];
  501. ret = snd_usb_init_sample_rate(subs->stream->chip,
  502. subs->cur_audiofmt->iface,
  503. alts,
  504. subs->cur_audiofmt,
  505. subs->cur_rate);
  506. if (ret < 0)
  507. goto unlock;
  508. if (subs->need_setup_ep) {
  509. ret = configure_endpoint(subs);
  510. if (ret < 0)
  511. goto unlock;
  512. subs->need_setup_ep = false;
  513. }
  514. /* some unit conversions in runtime */
  515. subs->data_endpoint->maxframesize =
  516. bytes_to_frames(runtime, subs->data_endpoint->maxpacksize);
  517. subs->data_endpoint->curframesize =
  518. bytes_to_frames(runtime, subs->data_endpoint->curpacksize);
  519. /* reset the pointer */
  520. subs->hwptr_done = 0;
  521. subs->transfer_done = 0;
  522. subs->last_delay = 0;
  523. subs->last_frame_number = 0;
  524. runtime->delay = 0;
  525. /* for playback, submit the URBs now; otherwise, the first hwptr_done
  526. * updates for all URBs would happen at the same time when starting */
  527. if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK)
  528. ret = start_endpoints(subs, 1);
  529. unlock:
  530. up_read(&subs->stream->chip->shutdown_rwsem);
  531. return ret;
  532. }
  533. static struct snd_pcm_hardware snd_usb_hardware =
  534. {
  535. .info = SNDRV_PCM_INFO_MMAP |
  536. SNDRV_PCM_INFO_MMAP_VALID |
  537. SNDRV_PCM_INFO_BATCH |
  538. SNDRV_PCM_INFO_INTERLEAVED |
  539. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  540. SNDRV_PCM_INFO_PAUSE,
  541. .buffer_bytes_max = 1024 * 1024,
  542. .period_bytes_min = 64,
  543. .period_bytes_max = 512 * 1024,
  544. .periods_min = 2,
  545. .periods_max = 1024,
  546. };
  547. static int hw_check_valid_format(struct snd_usb_substream *subs,
  548. struct snd_pcm_hw_params *params,
  549. struct audioformat *fp)
  550. {
  551. struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
  552. struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
  553. struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
  554. struct snd_interval *pt = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
  555. struct snd_mask check_fmts;
  556. unsigned int ptime;
  557. /* check the format */
  558. snd_mask_none(&check_fmts);
  559. check_fmts.bits[0] = (u32)fp->formats;
  560. check_fmts.bits[1] = (u32)(fp->formats >> 32);
  561. snd_mask_intersect(&check_fmts, fmts);
  562. if (snd_mask_empty(&check_fmts)) {
  563. hwc_debug(" > check: no supported format %d\n", fp->format);
  564. return 0;
  565. }
  566. /* check the channels */
  567. if (fp->channels < ct->min || fp->channels > ct->max) {
  568. hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max);
  569. return 0;
  570. }
  571. /* check the rate is within the range */
  572. if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) {
  573. hwc_debug(" > check: rate_min %d > max %d\n", fp->rate_min, it->max);
  574. return 0;
  575. }
  576. if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) {
  577. hwc_debug(" > check: rate_max %d < min %d\n", fp->rate_max, it->min);
  578. return 0;
  579. }
  580. /* check whether the period time is >= the data packet interval */
  581. if (subs->speed != USB_SPEED_FULL) {
  582. ptime = 125 * (1 << fp->datainterval);
  583. if (ptime > pt->max || (ptime == pt->max && pt->openmax)) {
  584. hwc_debug(" > check: ptime %u > max %u\n", ptime, pt->max);
  585. return 0;
  586. }
  587. }
  588. return 1;
  589. }
  590. static int hw_rule_rate(struct snd_pcm_hw_params *params,
  591. struct snd_pcm_hw_rule *rule)
  592. {
  593. struct snd_usb_substream *subs = rule->private;
  594. struct list_head *p;
  595. struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
  596. unsigned int rmin, rmax;
  597. int changed;
  598. hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max);
  599. changed = 0;
  600. rmin = rmax = 0;
  601. list_for_each(p, &subs->fmt_list) {
  602. struct audioformat *fp;
  603. fp = list_entry(p, struct audioformat, list);
  604. if (!hw_check_valid_format(subs, params, fp))
  605. continue;
  606. if (changed++) {
  607. if (rmin > fp->rate_min)
  608. rmin = fp->rate_min;
  609. if (rmax < fp->rate_max)
  610. rmax = fp->rate_max;
  611. } else {
  612. rmin = fp->rate_min;
  613. rmax = fp->rate_max;
  614. }
  615. }
  616. if (!changed) {
  617. hwc_debug(" --> get empty\n");
  618. it->empty = 1;
  619. return -EINVAL;
  620. }
  621. changed = 0;
  622. if (it->min < rmin) {
  623. it->min = rmin;
  624. it->openmin = 0;
  625. changed = 1;
  626. }
  627. if (it->max > rmax) {
  628. it->max = rmax;
  629. it->openmax = 0;
  630. changed = 1;
  631. }
  632. if (snd_interval_checkempty(it)) {
  633. it->empty = 1;
  634. return -EINVAL;
  635. }
  636. hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
  637. return changed;
  638. }
  639. static int hw_rule_channels(struct snd_pcm_hw_params *params,
  640. struct snd_pcm_hw_rule *rule)
  641. {
  642. struct snd_usb_substream *subs = rule->private;
  643. struct list_head *p;
  644. struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
  645. unsigned int rmin, rmax;
  646. int changed;
  647. hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max);
  648. changed = 0;
  649. rmin = rmax = 0;
  650. list_for_each(p, &subs->fmt_list) {
  651. struct audioformat *fp;
  652. fp = list_entry(p, struct audioformat, list);
  653. if (!hw_check_valid_format(subs, params, fp))
  654. continue;
  655. if (changed++) {
  656. if (rmin > fp->channels)
  657. rmin = fp->channels;
  658. if (rmax < fp->channels)
  659. rmax = fp->channels;
  660. } else {
  661. rmin = fp->channels;
  662. rmax = fp->channels;
  663. }
  664. }
  665. if (!changed) {
  666. hwc_debug(" --> get empty\n");
  667. it->empty = 1;
  668. return -EINVAL;
  669. }
  670. changed = 0;
  671. if (it->min < rmin) {
  672. it->min = rmin;
  673. it->openmin = 0;
  674. changed = 1;
  675. }
  676. if (it->max > rmax) {
  677. it->max = rmax;
  678. it->openmax = 0;
  679. changed = 1;
  680. }
  681. if (snd_interval_checkempty(it)) {
  682. it->empty = 1;
  683. return -EINVAL;
  684. }
  685. hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
  686. return changed;
  687. }
  688. static int hw_rule_format(struct snd_pcm_hw_params *params,
  689. struct snd_pcm_hw_rule *rule)
  690. {
  691. struct snd_usb_substream *subs = rule->private;
  692. struct list_head *p;
  693. struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
  694. u64 fbits;
  695. u32 oldbits[2];
  696. int changed;
  697. hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]);
  698. fbits = 0;
  699. list_for_each(p, &subs->fmt_list) {
  700. struct audioformat *fp;
  701. fp = list_entry(p, struct audioformat, list);
  702. if (!hw_check_valid_format(subs, params, fp))
  703. continue;
  704. fbits |= fp->formats;
  705. }
  706. oldbits[0] = fmt->bits[0];
  707. oldbits[1] = fmt->bits[1];
  708. fmt->bits[0] &= (u32)fbits;
  709. fmt->bits[1] &= (u32)(fbits >> 32);
  710. if (!fmt->bits[0] && !fmt->bits[1]) {
  711. hwc_debug(" --> get empty\n");
  712. return -EINVAL;
  713. }
  714. changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]);
  715. hwc_debug(" --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed);
  716. return changed;
  717. }
  718. static int hw_rule_period_time(struct snd_pcm_hw_params *params,
  719. struct snd_pcm_hw_rule *rule)
  720. {
  721. struct snd_usb_substream *subs = rule->private;
  722. struct audioformat *fp;
  723. struct snd_interval *it;
  724. unsigned char min_datainterval;
  725. unsigned int pmin;
  726. int changed;
  727. it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
  728. hwc_debug("hw_rule_period_time: (%u,%u)\n", it->min, it->max);
  729. min_datainterval = 0xff;
  730. list_for_each_entry(fp, &subs->fmt_list, list) {
  731. if (!hw_check_valid_format(subs, params, fp))
  732. continue;
  733. min_datainterval = min(min_datainterval, fp->datainterval);
  734. }
  735. if (min_datainterval == 0xff) {
  736. hwc_debug(" --> get empty\n");
  737. it->empty = 1;
  738. return -EINVAL;
  739. }
  740. pmin = 125 * (1 << min_datainterval);
  741. changed = 0;
  742. if (it->min < pmin) {
  743. it->min = pmin;
  744. it->openmin = 0;
  745. changed = 1;
  746. }
  747. if (snd_interval_checkempty(it)) {
  748. it->empty = 1;
  749. return -EINVAL;
  750. }
  751. hwc_debug(" --> (%u,%u) (changed = %d)\n", it->min, it->max, changed);
  752. return changed;
  753. }
  754. /*
  755. * If the device supports unusual bit rates, does the request meet these?
  756. */
  757. static int snd_usb_pcm_check_knot(struct snd_pcm_runtime *runtime,
  758. struct snd_usb_substream *subs)
  759. {
  760. struct audioformat *fp;
  761. int *rate_list;
  762. int count = 0, needs_knot = 0;
  763. int err;
  764. kfree(subs->rate_list.list);
  765. subs->rate_list.list = NULL;
  766. list_for_each_entry(fp, &subs->fmt_list, list) {
  767. if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)
  768. return 0;
  769. count += fp->nr_rates;
  770. if (fp->rates & SNDRV_PCM_RATE_KNOT)
  771. needs_knot = 1;
  772. }
  773. if (!needs_knot)
  774. return 0;
  775. subs->rate_list.list = rate_list =
  776. kmalloc(sizeof(int) * count, GFP_KERNEL);
  777. if (!subs->rate_list.list)
  778. return -ENOMEM;
  779. subs->rate_list.count = count;
  780. subs->rate_list.mask = 0;
  781. count = 0;
  782. list_for_each_entry(fp, &subs->fmt_list, list) {
  783. int i;
  784. for (i = 0; i < fp->nr_rates; i++)
  785. rate_list[count++] = fp->rate_table[i];
  786. }
  787. err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  788. &subs->rate_list);
  789. if (err < 0)
  790. return err;
  791. return 0;
  792. }
  793. /*
  794. * set up the runtime hardware information.
  795. */
  796. static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs)
  797. {
  798. struct list_head *p;
  799. unsigned int pt, ptmin;
  800. int param_period_time_if_needed;
  801. int err;
  802. runtime->hw.formats = subs->formats;
  803. runtime->hw.rate_min = 0x7fffffff;
  804. runtime->hw.rate_max = 0;
  805. runtime->hw.channels_min = 256;
  806. runtime->hw.channels_max = 0;
  807. runtime->hw.rates = 0;
  808. ptmin = UINT_MAX;
  809. /* check min/max rates and channels */
  810. list_for_each(p, &subs->fmt_list) {
  811. struct audioformat *fp;
  812. fp = list_entry(p, struct audioformat, list);
  813. runtime->hw.rates |= fp->rates;
  814. if (runtime->hw.rate_min > fp->rate_min)
  815. runtime->hw.rate_min = fp->rate_min;
  816. if (runtime->hw.rate_max < fp->rate_max)
  817. runtime->hw.rate_max = fp->rate_max;
  818. if (runtime->hw.channels_min > fp->channels)
  819. runtime->hw.channels_min = fp->channels;
  820. if (runtime->hw.channels_max < fp->channels)
  821. runtime->hw.channels_max = fp->channels;
  822. if (fp->fmt_type == UAC_FORMAT_TYPE_II && fp->frame_size > 0) {
  823. /* FIXME: there might be more than one audio formats... */
  824. runtime->hw.period_bytes_min = runtime->hw.period_bytes_max =
  825. fp->frame_size;
  826. }
  827. pt = 125 * (1 << fp->datainterval);
  828. ptmin = min(ptmin, pt);
  829. }
  830. err = snd_usb_autoresume(subs->stream->chip);
  831. if (err < 0)
  832. return err;
  833. param_period_time_if_needed = SNDRV_PCM_HW_PARAM_PERIOD_TIME;
  834. if (subs->speed == USB_SPEED_FULL)
  835. /* full speed devices have fixed data packet interval */
  836. ptmin = 1000;
  837. if (ptmin == 1000)
  838. /* if period time doesn't go below 1 ms, no rules needed */
  839. param_period_time_if_needed = -1;
  840. snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
  841. ptmin, UINT_MAX);
  842. if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  843. hw_rule_rate, subs,
  844. SNDRV_PCM_HW_PARAM_FORMAT,
  845. SNDRV_PCM_HW_PARAM_CHANNELS,
  846. param_period_time_if_needed,
  847. -1)) < 0)
  848. goto rep_err;
  849. if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
  850. hw_rule_channels, subs,
  851. SNDRV_PCM_HW_PARAM_FORMAT,
  852. SNDRV_PCM_HW_PARAM_RATE,
  853. param_period_time_if_needed,
  854. -1)) < 0)
  855. goto rep_err;
  856. if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
  857. hw_rule_format, subs,
  858. SNDRV_PCM_HW_PARAM_RATE,
  859. SNDRV_PCM_HW_PARAM_CHANNELS,
  860. param_period_time_if_needed,
  861. -1)) < 0)
  862. goto rep_err;
  863. if (param_period_time_if_needed >= 0) {
  864. err = snd_pcm_hw_rule_add(runtime, 0,
  865. SNDRV_PCM_HW_PARAM_PERIOD_TIME,
  866. hw_rule_period_time, subs,
  867. SNDRV_PCM_HW_PARAM_FORMAT,
  868. SNDRV_PCM_HW_PARAM_CHANNELS,
  869. SNDRV_PCM_HW_PARAM_RATE,
  870. -1);
  871. if (err < 0)
  872. goto rep_err;
  873. }
  874. if ((err = snd_usb_pcm_check_knot(runtime, subs)) < 0)
  875. goto rep_err;
  876. return 0;
  877. rep_err:
  878. snd_usb_autosuspend(subs->stream->chip);
  879. return err;
  880. }
  881. static int snd_usb_pcm_open(struct snd_pcm_substream *substream, int direction)
  882. {
  883. struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
  884. struct snd_pcm_runtime *runtime = substream->runtime;
  885. struct snd_usb_substream *subs = &as->substream[direction];
  886. subs->interface = -1;
  887. subs->altset_idx = 0;
  888. runtime->hw = snd_usb_hardware;
  889. runtime->private_data = subs;
  890. subs->pcm_substream = substream;
  891. /* runtime PM is also done there */
  892. return setup_hw_info(runtime, subs);
  893. }
  894. static int snd_usb_pcm_close(struct snd_pcm_substream *substream, int direction)
  895. {
  896. struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
  897. struct snd_usb_substream *subs = &as->substream[direction];
  898. stop_endpoints(subs, 0, 0, 0);
  899. if (!as->chip->shutdown && subs->interface >= 0) {
  900. usb_set_interface(subs->dev, subs->interface, 0);
  901. subs->interface = -1;
  902. }
  903. subs->pcm_substream = NULL;
  904. snd_usb_autosuspend(subs->stream->chip);
  905. return 0;
  906. }
  907. /* Since a URB can handle only a single linear buffer, we must use double
  908. * buffering when the data to be transferred overflows the buffer boundary.
  909. * To avoid inconsistencies when updating hwptr_done, we use double buffering
  910. * for all URBs.
  911. */
  912. static void retire_capture_urb(struct snd_usb_substream *subs,
  913. struct urb *urb)
  914. {
  915. struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
  916. unsigned int stride, frames, bytes, oldptr;
  917. int i, period_elapsed = 0;
  918. unsigned long flags;
  919. unsigned char *cp;
  920. stride = runtime->frame_bits >> 3;
  921. for (i = 0; i < urb->number_of_packets; i++) {
  922. cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset;
  923. if (urb->iso_frame_desc[i].status && printk_ratelimit()) {
  924. snd_printdd(KERN_ERR "frame %d active: %d\n", i, urb->iso_frame_desc[i].status);
  925. // continue;
  926. }
  927. bytes = urb->iso_frame_desc[i].actual_length;
  928. frames = bytes / stride;
  929. if (!subs->txfr_quirk)
  930. bytes = frames * stride;
  931. if (bytes % (runtime->sample_bits >> 3) != 0) {
  932. #ifdef CONFIG_SND_DEBUG_VERBOSE
  933. int oldbytes = bytes;
  934. #endif
  935. bytes = frames * stride;
  936. snd_printdd(KERN_ERR "Corrected urb data len. %d->%d\n",
  937. oldbytes, bytes);
  938. }
  939. /* update the current pointer */
  940. spin_lock_irqsave(&subs->lock, flags);
  941. oldptr = subs->hwptr_done;
  942. subs->hwptr_done += bytes;
  943. if (subs->hwptr_done >= runtime->buffer_size * stride)
  944. subs->hwptr_done -= runtime->buffer_size * stride;
  945. frames = (bytes + (oldptr % stride)) / stride;
  946. subs->transfer_done += frames;
  947. if (subs->transfer_done >= runtime->period_size) {
  948. subs->transfer_done -= runtime->period_size;
  949. period_elapsed = 1;
  950. }
  951. spin_unlock_irqrestore(&subs->lock, flags);
  952. /* copy a data chunk */
  953. if (oldptr + bytes > runtime->buffer_size * stride) {
  954. unsigned int bytes1 =
  955. runtime->buffer_size * stride - oldptr;
  956. memcpy(runtime->dma_area + oldptr, cp, bytes1);
  957. memcpy(runtime->dma_area, cp + bytes1, bytes - bytes1);
  958. } else {
  959. memcpy(runtime->dma_area + oldptr, cp, bytes);
  960. }
  961. }
  962. if (period_elapsed)
  963. snd_pcm_period_elapsed(subs->pcm_substream);
  964. }
  965. static void prepare_playback_urb(struct snd_usb_substream *subs,
  966. struct urb *urb)
  967. {
  968. struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
  969. struct snd_usb_endpoint *ep = subs->data_endpoint;
  970. struct snd_urb_ctx *ctx = urb->context;
  971. unsigned int counts, frames, bytes;
  972. int i, stride, period_elapsed = 0;
  973. unsigned long flags;
  974. stride = runtime->frame_bits >> 3;
  975. frames = 0;
  976. urb->number_of_packets = 0;
  977. spin_lock_irqsave(&subs->lock, flags);
  978. for (i = 0; i < ctx->packets; i++) {
  979. if (ctx->packet_size[i])
  980. counts = ctx->packet_size[i];
  981. else
  982. counts = snd_usb_endpoint_next_packet_size(ep);
  983. /* set up descriptor */
  984. urb->iso_frame_desc[i].offset = frames * stride;
  985. urb->iso_frame_desc[i].length = counts * stride;
  986. frames += counts;
  987. urb->number_of_packets++;
  988. subs->transfer_done += counts;
  989. if (subs->transfer_done >= runtime->period_size) {
  990. subs->transfer_done -= runtime->period_size;
  991. period_elapsed = 1;
  992. if (subs->fmt_type == UAC_FORMAT_TYPE_II) {
  993. if (subs->transfer_done > 0) {
  994. /* FIXME: fill-max mode is not
  995. * supported yet */
  996. frames -= subs->transfer_done;
  997. counts -= subs->transfer_done;
  998. urb->iso_frame_desc[i].length =
  999. counts * stride;
  1000. subs->transfer_done = 0;
  1001. }
  1002. i++;
  1003. if (i < ctx->packets) {
  1004. /* add a transfer delimiter */
  1005. urb->iso_frame_desc[i].offset =
  1006. frames * stride;
  1007. urb->iso_frame_desc[i].length = 0;
  1008. urb->number_of_packets++;
  1009. }
  1010. break;
  1011. }
  1012. }
  1013. if (period_elapsed &&
  1014. !snd_usb_endpoint_implict_feedback_sink(subs->data_endpoint)) /* finish at the period boundary */
  1015. break;
  1016. }
  1017. bytes = frames * stride;
  1018. if (subs->hwptr_done + bytes > runtime->buffer_size * stride) {
  1019. /* err, the transferred area goes over buffer boundary. */
  1020. unsigned int bytes1 =
  1021. runtime->buffer_size * stride - subs->hwptr_done;
  1022. memcpy(urb->transfer_buffer,
  1023. runtime->dma_area + subs->hwptr_done, bytes1);
  1024. memcpy(urb->transfer_buffer + bytes1,
  1025. runtime->dma_area, bytes - bytes1);
  1026. } else {
  1027. memcpy(urb->transfer_buffer,
  1028. runtime->dma_area + subs->hwptr_done, bytes);
  1029. }
  1030. subs->hwptr_done += bytes;
  1031. if (subs->hwptr_done >= runtime->buffer_size * stride)
  1032. subs->hwptr_done -= runtime->buffer_size * stride;
  1033. /* update delay with exact number of samples queued */
  1034. runtime->delay = subs->last_delay;
  1035. runtime->delay += frames;
  1036. subs->last_delay = runtime->delay;
  1037. /* realign last_frame_number */
  1038. subs->last_frame_number = usb_get_current_frame_number(subs->dev);
  1039. subs->last_frame_number &= 0xFF; /* keep 8 LSBs */
  1040. spin_unlock_irqrestore(&subs->lock, flags);
  1041. urb->transfer_buffer_length = bytes;
  1042. if (period_elapsed)
  1043. snd_pcm_period_elapsed(subs->pcm_substream);
  1044. }
  1045. /*
  1046. * process after playback data complete
  1047. * - decrease the delay count again
  1048. */
  1049. static void retire_playback_urb(struct snd_usb_substream *subs,
  1050. struct urb *urb)
  1051. {
  1052. unsigned long flags;
  1053. struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
  1054. int stride = runtime->frame_bits >> 3;
  1055. int processed = urb->transfer_buffer_length / stride;
  1056. int est_delay;
  1057. /* ignore the delay accounting when procssed=0 is given, i.e.
  1058. * silent payloads are procssed before handling the actual data
  1059. */
  1060. if (!processed)
  1061. return;
  1062. spin_lock_irqsave(&subs->lock, flags);
  1063. est_delay = snd_usb_pcm_delay(subs, runtime->rate);
  1064. /* update delay with exact number of samples played */
  1065. if (processed > subs->last_delay)
  1066. subs->last_delay = 0;
  1067. else
  1068. subs->last_delay -= processed;
  1069. runtime->delay = subs->last_delay;
  1070. /*
  1071. * Report when delay estimate is off by more than 2ms.
  1072. * The error should be lower than 2ms since the estimate relies
  1073. * on two reads of a counter updated every ms.
  1074. */
  1075. if (abs(est_delay - subs->last_delay) * 1000 > runtime->rate * 2)
  1076. snd_printk(KERN_DEBUG "delay: estimated %d, actual %d\n",
  1077. est_delay, subs->last_delay);
  1078. spin_unlock_irqrestore(&subs->lock, flags);
  1079. }
  1080. static int snd_usb_playback_open(struct snd_pcm_substream *substream)
  1081. {
  1082. return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_PLAYBACK);
  1083. }
  1084. static int snd_usb_playback_close(struct snd_pcm_substream *substream)
  1085. {
  1086. return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_PLAYBACK);
  1087. }
  1088. static int snd_usb_capture_open(struct snd_pcm_substream *substream)
  1089. {
  1090. return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_CAPTURE);
  1091. }
  1092. static int snd_usb_capture_close(struct snd_pcm_substream *substream)
  1093. {
  1094. return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_CAPTURE);
  1095. }
  1096. static int snd_usb_substream_playback_trigger(struct snd_pcm_substream *substream,
  1097. int cmd)
  1098. {
  1099. struct snd_usb_substream *subs = substream->runtime->private_data;
  1100. switch (cmd) {
  1101. case SNDRV_PCM_TRIGGER_START:
  1102. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  1103. subs->data_endpoint->prepare_data_urb = prepare_playback_urb;
  1104. subs->data_endpoint->retire_data_urb = retire_playback_urb;
  1105. subs->running = 1;
  1106. return 0;
  1107. case SNDRV_PCM_TRIGGER_STOP:
  1108. stop_endpoints(subs, 0, 0, 0);
  1109. subs->running = 0;
  1110. return 0;
  1111. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  1112. subs->data_endpoint->prepare_data_urb = NULL;
  1113. subs->data_endpoint->retire_data_urb = NULL;
  1114. subs->running = 0;
  1115. return 0;
  1116. }
  1117. return -EINVAL;
  1118. }
  1119. static int snd_usb_substream_capture_trigger(struct snd_pcm_substream *substream,
  1120. int cmd)
  1121. {
  1122. int err;
  1123. struct snd_usb_substream *subs = substream->runtime->private_data;
  1124. switch (cmd) {
  1125. case SNDRV_PCM_TRIGGER_START:
  1126. err = start_endpoints(subs, 0);
  1127. if (err < 0)
  1128. return err;
  1129. subs->data_endpoint->retire_data_urb = retire_capture_urb;
  1130. subs->running = 1;
  1131. return 0;
  1132. case SNDRV_PCM_TRIGGER_STOP:
  1133. stop_endpoints(subs, 0, 0, 0);
  1134. subs->running = 0;
  1135. return 0;
  1136. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  1137. subs->data_endpoint->retire_data_urb = NULL;
  1138. subs->running = 0;
  1139. return 0;
  1140. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  1141. subs->data_endpoint->retire_data_urb = retire_capture_urb;
  1142. subs->running = 1;
  1143. return 0;
  1144. }
  1145. return -EINVAL;
  1146. }
  1147. static struct snd_pcm_ops snd_usb_playback_ops = {
  1148. .open = snd_usb_playback_open,
  1149. .close = snd_usb_playback_close,
  1150. .ioctl = snd_pcm_lib_ioctl,
  1151. .hw_params = snd_usb_hw_params,
  1152. .hw_free = snd_usb_hw_free,
  1153. .prepare = snd_usb_pcm_prepare,
  1154. .trigger = snd_usb_substream_playback_trigger,
  1155. .pointer = snd_usb_pcm_pointer,
  1156. .page = snd_pcm_lib_get_vmalloc_page,
  1157. .mmap = snd_pcm_lib_mmap_vmalloc,
  1158. };
  1159. static struct snd_pcm_ops snd_usb_capture_ops = {
  1160. .open = snd_usb_capture_open,
  1161. .close = snd_usb_capture_close,
  1162. .ioctl = snd_pcm_lib_ioctl,
  1163. .hw_params = snd_usb_hw_params,
  1164. .hw_free = snd_usb_hw_free,
  1165. .prepare = snd_usb_pcm_prepare,
  1166. .trigger = snd_usb_substream_capture_trigger,
  1167. .pointer = snd_usb_pcm_pointer,
  1168. .page = snd_pcm_lib_get_vmalloc_page,
  1169. .mmap = snd_pcm_lib_mmap_vmalloc,
  1170. };
  1171. void snd_usb_set_pcm_ops(struct snd_pcm *pcm, int stream)
  1172. {
  1173. snd_pcm_set_ops(pcm, stream,
  1174. stream == SNDRV_PCM_STREAM_PLAYBACK ?
  1175. &snd_usb_playback_ops : &snd_usb_capture_ops);
  1176. }