pcm.c 34 KB

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