pcm.c 40 KB

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