pcm.c 36 KB

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