pcm.c 43 KB

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