pcm.c 34 KB

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