pcm.c 40 KB

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