pcm.c 38 KB

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