pcm.c 34 KB

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