pcm.c 37 KB

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