pcm.c 43 KB

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