pcm.c 41 KB

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