pcm.c 40 KB

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