pcm.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729
  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/bitrev.h>
  19. #include <linux/ratelimit.h>
  20. #include <linux/usb.h>
  21. #include <linux/usb/audio.h>
  22. #include <linux/usb/audio-v2.h>
  23. #include <sound/core.h>
  24. #include <sound/pcm.h>
  25. #include <sound/pcm_params.h>
  26. #include "usbaudio.h"
  27. #include "card.h"
  28. #include "quirks.h"
  29. #include "debug.h"
  30. #include "endpoint.h"
  31. #include "helper.h"
  32. #include "pcm.h"
  33. #include "clock.h"
  34. #include "power.h"
  35. #define SUBSTREAM_FLAG_DATA_EP_STARTED 0
  36. #define SUBSTREAM_FLAG_SYNC_EP_STARTED 1
  37. /* return the estimated delay based on USB frame counters */
  38. snd_pcm_uframes_t snd_usb_pcm_delay(struct snd_usb_substream *subs,
  39. unsigned int rate)
  40. {
  41. int current_frame_number;
  42. int frame_diff;
  43. int est_delay;
  44. if (!subs->last_delay)
  45. return 0; /* short path */
  46. current_frame_number = usb_get_current_frame_number(subs->dev);
  47. /*
  48. * HCD implementations use different widths, use lower 8 bits.
  49. * The delay will be managed up to 256ms, which is more than
  50. * enough
  51. */
  52. frame_diff = (current_frame_number - subs->last_frame_number) & 0xff;
  53. /* Approximation based on number of samples per USB frame (ms),
  54. some truncation for 44.1 but the estimate is good enough */
  55. est_delay = frame_diff * rate / 1000;
  56. if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK)
  57. est_delay = subs->last_delay - est_delay;
  58. else
  59. est_delay = subs->last_delay + est_delay;
  60. if (est_delay < 0)
  61. est_delay = 0;
  62. return est_delay;
  63. }
  64. /*
  65. * return the current pcm pointer. just based on the hwptr_done value.
  66. */
  67. static snd_pcm_uframes_t snd_usb_pcm_pointer(struct snd_pcm_substream *substream)
  68. {
  69. struct snd_usb_substream *subs;
  70. unsigned int hwptr_done;
  71. subs = (struct snd_usb_substream *)substream->runtime->private_data;
  72. if (atomic_read(&subs->stream->chip->shutdown))
  73. return SNDRV_PCM_POS_XRUN;
  74. spin_lock(&subs->lock);
  75. hwptr_done = subs->hwptr_done;
  76. substream->runtime->delay = snd_usb_pcm_delay(subs,
  77. substream->runtime->rate);
  78. spin_unlock(&subs->lock);
  79. return hwptr_done / (substream->runtime->frame_bits >> 3);
  80. }
  81. /*
  82. * find a matching audio format
  83. */
  84. static struct audioformat *find_format(struct snd_usb_substream *subs)
  85. {
  86. struct audioformat *fp;
  87. struct audioformat *found = NULL;
  88. int cur_attr = 0, attr;
  89. list_for_each_entry(fp, &subs->fmt_list, list) {
  90. if (!(fp->formats & pcm_format_to_bits(subs->pcm_format)))
  91. continue;
  92. if (fp->channels != subs->channels)
  93. continue;
  94. if (subs->cur_rate < fp->rate_min ||
  95. subs->cur_rate > fp->rate_max)
  96. continue;
  97. if (! (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)) {
  98. unsigned int i;
  99. for (i = 0; i < fp->nr_rates; i++)
  100. if (fp->rate_table[i] == subs->cur_rate)
  101. break;
  102. if (i >= fp->nr_rates)
  103. continue;
  104. }
  105. attr = fp->ep_attr & USB_ENDPOINT_SYNCTYPE;
  106. if (! found) {
  107. found = fp;
  108. cur_attr = attr;
  109. continue;
  110. }
  111. /* avoid async out and adaptive in if the other method
  112. * supports the same format.
  113. * this is a workaround for the case like
  114. * M-audio audiophile USB.
  115. */
  116. if (attr != cur_attr) {
  117. if ((attr == USB_ENDPOINT_SYNC_ASYNC &&
  118. subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
  119. (attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
  120. subs->direction == SNDRV_PCM_STREAM_CAPTURE))
  121. continue;
  122. if ((cur_attr == USB_ENDPOINT_SYNC_ASYNC &&
  123. subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
  124. (cur_attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
  125. subs->direction == SNDRV_PCM_STREAM_CAPTURE)) {
  126. found = fp;
  127. cur_attr = attr;
  128. continue;
  129. }
  130. }
  131. /* find the format with the largest max. packet size */
  132. if (fp->maxpacksize > found->maxpacksize) {
  133. found = fp;
  134. cur_attr = attr;
  135. }
  136. }
  137. return found;
  138. }
  139. static int init_pitch_v1(struct snd_usb_audio *chip, int iface,
  140. struct usb_host_interface *alts,
  141. struct audioformat *fmt)
  142. {
  143. struct usb_device *dev = chip->dev;
  144. unsigned int ep;
  145. unsigned char data[1];
  146. int err;
  147. if (get_iface_desc(alts)->bNumEndpoints < 1)
  148. return -EINVAL;
  149. ep = get_endpoint(alts, 0)->bEndpointAddress;
  150. data[0] = 1;
  151. if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
  152. USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
  153. UAC_EP_CS_ATTR_PITCH_CONTROL << 8, ep,
  154. data, sizeof(data))) < 0) {
  155. usb_audio_err(chip, "%d:%d: cannot set enable PITCH\n",
  156. iface, ep);
  157. return err;
  158. }
  159. return 0;
  160. }
  161. static int init_pitch_v2(struct snd_usb_audio *chip, int iface,
  162. struct usb_host_interface *alts,
  163. struct audioformat *fmt)
  164. {
  165. struct usb_device *dev = chip->dev;
  166. unsigned char data[1];
  167. int err;
  168. data[0] = 1;
  169. if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC2_CS_CUR,
  170. USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_OUT,
  171. UAC2_EP_CS_PITCH << 8, 0,
  172. data, sizeof(data))) < 0) {
  173. usb_audio_err(chip, "%d:%d: cannot set enable PITCH (v2)\n",
  174. iface, fmt->altsetting);
  175. return err;
  176. }
  177. return 0;
  178. }
  179. /*
  180. * initialize the pitch control and sample rate
  181. */
  182. int snd_usb_init_pitch(struct snd_usb_audio *chip, int iface,
  183. struct usb_host_interface *alts,
  184. struct audioformat *fmt)
  185. {
  186. /* if endpoint doesn't have pitch control, bail out */
  187. if (!(fmt->attributes & UAC_EP_CS_ATTR_PITCH_CONTROL))
  188. return 0;
  189. switch (fmt->protocol) {
  190. case UAC_VERSION_1:
  191. default:
  192. return init_pitch_v1(chip, iface, alts, fmt);
  193. case UAC_VERSION_2:
  194. return init_pitch_v2(chip, iface, alts, fmt);
  195. }
  196. }
  197. static int start_endpoints(struct snd_usb_substream *subs)
  198. {
  199. int err;
  200. if (!subs->data_endpoint)
  201. return -EINVAL;
  202. if (!test_and_set_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags)) {
  203. struct snd_usb_endpoint *ep = subs->data_endpoint;
  204. dev_dbg(&subs->dev->dev, "Starting data EP @%p\n", ep);
  205. ep->data_subs = subs;
  206. err = snd_usb_endpoint_start(ep);
  207. if (err < 0) {
  208. clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags);
  209. return err;
  210. }
  211. }
  212. if (subs->sync_endpoint &&
  213. !test_and_set_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags)) {
  214. struct snd_usb_endpoint *ep = subs->sync_endpoint;
  215. if (subs->data_endpoint->iface != subs->sync_endpoint->iface ||
  216. subs->data_endpoint->altsetting != subs->sync_endpoint->altsetting) {
  217. err = usb_set_interface(subs->dev,
  218. subs->sync_endpoint->iface,
  219. subs->sync_endpoint->altsetting);
  220. if (err < 0) {
  221. clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags);
  222. dev_err(&subs->dev->dev,
  223. "%d:%d: cannot set interface (%d)\n",
  224. subs->sync_endpoint->iface,
  225. subs->sync_endpoint->altsetting, err);
  226. return -EIO;
  227. }
  228. }
  229. dev_dbg(&subs->dev->dev, "Starting sync EP @%p\n", ep);
  230. ep->sync_slave = subs->data_endpoint;
  231. err = snd_usb_endpoint_start(ep);
  232. if (err < 0) {
  233. clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags);
  234. return err;
  235. }
  236. }
  237. return 0;
  238. }
  239. static void stop_endpoints(struct snd_usb_substream *subs, bool wait)
  240. {
  241. if (test_and_clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags))
  242. snd_usb_endpoint_stop(subs->sync_endpoint);
  243. if (test_and_clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags))
  244. snd_usb_endpoint_stop(subs->data_endpoint);
  245. if (wait) {
  246. snd_usb_endpoint_sync_pending_stop(subs->sync_endpoint);
  247. snd_usb_endpoint_sync_pending_stop(subs->data_endpoint);
  248. }
  249. }
  250. static int search_roland_implicit_fb(struct usb_device *dev, int ifnum,
  251. unsigned int altsetting,
  252. struct usb_host_interface **alts,
  253. unsigned int *ep)
  254. {
  255. struct usb_interface *iface;
  256. struct usb_interface_descriptor *altsd;
  257. struct usb_endpoint_descriptor *epd;
  258. iface = usb_ifnum_to_if(dev, ifnum);
  259. if (!iface || iface->num_altsetting < altsetting + 1)
  260. return -ENOENT;
  261. *alts = &iface->altsetting[altsetting];
  262. altsd = get_iface_desc(*alts);
  263. if (altsd->bAlternateSetting != altsetting ||
  264. altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC ||
  265. (altsd->bInterfaceSubClass != 2 &&
  266. altsd->bInterfaceProtocol != 2 ) ||
  267. altsd->bNumEndpoints < 1)
  268. return -ENOENT;
  269. epd = get_endpoint(*alts, 0);
  270. if (!usb_endpoint_is_isoc_in(epd) ||
  271. (epd->bmAttributes & USB_ENDPOINT_USAGE_MASK) !=
  272. USB_ENDPOINT_USAGE_IMPLICIT_FB)
  273. return -ENOENT;
  274. *ep = epd->bEndpointAddress;
  275. return 0;
  276. }
  277. /* Setup an implicit feedback endpoint from a quirk. Returns 0 if no quirk
  278. * applies. Returns 1 if a quirk was found.
  279. */
  280. static int set_sync_ep_implicit_fb_quirk(struct snd_usb_substream *subs,
  281. struct usb_device *dev,
  282. struct usb_interface_descriptor *altsd,
  283. unsigned int attr)
  284. {
  285. struct usb_host_interface *alts;
  286. struct usb_interface *iface;
  287. unsigned int ep;
  288. /* Implicit feedback sync EPs consumers are always playback EPs */
  289. if (subs->direction != SNDRV_PCM_STREAM_PLAYBACK)
  290. return 0;
  291. switch (subs->stream->chip->usb_id) {
  292. case USB_ID(0x0763, 0x2030): /* M-Audio Fast Track C400 */
  293. case USB_ID(0x0763, 0x2031): /* M-Audio Fast Track C600 */
  294. ep = 0x81;
  295. iface = usb_ifnum_to_if(dev, 3);
  296. if (!iface || iface->num_altsetting == 0)
  297. return -EINVAL;
  298. alts = &iface->altsetting[1];
  299. goto add_sync_ep;
  300. break;
  301. case USB_ID(0x0763, 0x2080): /* M-Audio FastTrack Ultra */
  302. case USB_ID(0x0763, 0x2081):
  303. ep = 0x81;
  304. iface = usb_ifnum_to_if(dev, 2);
  305. if (!iface || iface->num_altsetting == 0)
  306. return -EINVAL;
  307. alts = &iface->altsetting[1];
  308. goto add_sync_ep;
  309. case USB_ID(0x1397, 0x0002):
  310. ep = 0x81;
  311. iface = usb_ifnum_to_if(dev, 1);
  312. if (!iface || iface->num_altsetting == 0)
  313. return -EINVAL;
  314. alts = &iface->altsetting[1];
  315. goto add_sync_ep;
  316. }
  317. if (attr == USB_ENDPOINT_SYNC_ASYNC &&
  318. altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC &&
  319. altsd->bInterfaceProtocol == 2 &&
  320. altsd->bNumEndpoints == 1 &&
  321. USB_ID_VENDOR(subs->stream->chip->usb_id) == 0x0582 /* Roland */ &&
  322. search_roland_implicit_fb(dev, altsd->bInterfaceNumber + 1,
  323. altsd->bAlternateSetting,
  324. &alts, &ep) >= 0) {
  325. goto add_sync_ep;
  326. }
  327. /* No quirk */
  328. return 0;
  329. add_sync_ep:
  330. subs->sync_endpoint = snd_usb_add_endpoint(subs->stream->chip,
  331. alts, ep, !subs->direction,
  332. SND_USB_ENDPOINT_TYPE_DATA);
  333. if (!subs->sync_endpoint)
  334. return -EINVAL;
  335. subs->data_endpoint->sync_master = subs->sync_endpoint;
  336. return 1;
  337. }
  338. static int set_sync_endpoint(struct snd_usb_substream *subs,
  339. struct audioformat *fmt,
  340. struct usb_device *dev,
  341. struct usb_host_interface *alts,
  342. struct usb_interface_descriptor *altsd)
  343. {
  344. int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
  345. unsigned int ep, attr;
  346. bool implicit_fb;
  347. int err;
  348. /* we need a sync pipe in async OUT or adaptive IN mode */
  349. /* check the number of EP, since some devices have broken
  350. * descriptors which fool us. if it has only one EP,
  351. * assume it as adaptive-out or sync-in.
  352. */
  353. attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE;
  354. if ((is_playback && (attr != USB_ENDPOINT_SYNC_ASYNC)) ||
  355. (!is_playback && (attr != USB_ENDPOINT_SYNC_ADAPTIVE))) {
  356. /*
  357. * In these modes the notion of sync_endpoint is irrelevant.
  358. * Reset pointers to avoid using stale data from previously
  359. * used settings, e.g. when configuration and endpoints were
  360. * changed
  361. */
  362. subs->sync_endpoint = NULL;
  363. subs->data_endpoint->sync_master = NULL;
  364. }
  365. err = set_sync_ep_implicit_fb_quirk(subs, dev, altsd, attr);
  366. if (err < 0)
  367. return err;
  368. /* endpoint set by quirk */
  369. if (err > 0)
  370. return 0;
  371. if (altsd->bNumEndpoints < 2)
  372. return 0;
  373. if ((is_playback && (attr == USB_ENDPOINT_SYNC_SYNC ||
  374. attr == USB_ENDPOINT_SYNC_ADAPTIVE)) ||
  375. (!is_playback && attr != USB_ENDPOINT_SYNC_ADAPTIVE))
  376. return 0;
  377. /*
  378. * In case of illegal SYNC_NONE for OUT endpoint, we keep going to see
  379. * if we don't find a sync endpoint, as on M-Audio Transit. In case of
  380. * error fall back to SYNC mode and don't create sync endpoint
  381. */
  382. /* check sync-pipe endpoint */
  383. /* ... and check descriptor size before accessing bSynchAddress
  384. because there is a version of the SB Audigy 2 NX firmware lacking
  385. the audio fields in the endpoint descriptors */
  386. if ((get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_ISOC ||
  387. (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
  388. get_endpoint(alts, 1)->bSynchAddress != 0)) {
  389. dev_err(&dev->dev,
  390. "%d:%d : invalid sync pipe. bmAttributes %02x, bLength %d, bSynchAddress %02x\n",
  391. fmt->iface, fmt->altsetting,
  392. get_endpoint(alts, 1)->bmAttributes,
  393. get_endpoint(alts, 1)->bLength,
  394. get_endpoint(alts, 1)->bSynchAddress);
  395. if (is_playback && attr == USB_ENDPOINT_SYNC_NONE)
  396. return 0;
  397. return -EINVAL;
  398. }
  399. ep = get_endpoint(alts, 1)->bEndpointAddress;
  400. if (get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
  401. ((is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) ||
  402. (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) {
  403. dev_err(&dev->dev,
  404. "%d:%d : invalid sync pipe. is_playback %d, ep %02x, bSynchAddress %02x\n",
  405. fmt->iface, fmt->altsetting,
  406. is_playback, ep, get_endpoint(alts, 0)->bSynchAddress);
  407. if (is_playback && attr == USB_ENDPOINT_SYNC_NONE)
  408. return 0;
  409. return -EINVAL;
  410. }
  411. implicit_fb = (get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_USAGE_MASK)
  412. == USB_ENDPOINT_USAGE_IMPLICIT_FB;
  413. subs->sync_endpoint = snd_usb_add_endpoint(subs->stream->chip,
  414. alts, ep, !subs->direction,
  415. implicit_fb ?
  416. SND_USB_ENDPOINT_TYPE_DATA :
  417. SND_USB_ENDPOINT_TYPE_SYNC);
  418. if (!subs->sync_endpoint) {
  419. if (is_playback && attr == USB_ENDPOINT_SYNC_NONE)
  420. return 0;
  421. return -EINVAL;
  422. }
  423. subs->data_endpoint->sync_master = subs->sync_endpoint;
  424. return 0;
  425. }
  426. /*
  427. * find a matching format and set up the interface
  428. */
  429. static int set_format(struct snd_usb_substream *subs, struct audioformat *fmt)
  430. {
  431. struct usb_device *dev = subs->dev;
  432. struct usb_host_interface *alts;
  433. struct usb_interface_descriptor *altsd;
  434. struct usb_interface *iface;
  435. int err;
  436. iface = usb_ifnum_to_if(dev, fmt->iface);
  437. if (WARN_ON(!iface))
  438. return -EINVAL;
  439. alts = &iface->altsetting[fmt->altset_idx];
  440. altsd = get_iface_desc(alts);
  441. if (WARN_ON(altsd->bAlternateSetting != fmt->altsetting))
  442. return -EINVAL;
  443. if (fmt == subs->cur_audiofmt)
  444. return 0;
  445. /* close the old interface */
  446. if (subs->interface >= 0 && subs->interface != fmt->iface) {
  447. err = usb_set_interface(subs->dev, subs->interface, 0);
  448. if (err < 0) {
  449. dev_err(&dev->dev,
  450. "%d:%d: return to setting 0 failed (%d)\n",
  451. fmt->iface, fmt->altsetting, err);
  452. return -EIO;
  453. }
  454. subs->interface = -1;
  455. subs->altset_idx = 0;
  456. }
  457. /* set interface */
  458. if (subs->interface != fmt->iface ||
  459. subs->altset_idx != fmt->altset_idx) {
  460. err = snd_usb_select_mode_quirk(subs, fmt);
  461. if (err < 0)
  462. return -EIO;
  463. err = usb_set_interface(dev, fmt->iface, fmt->altsetting);
  464. if (err < 0) {
  465. dev_err(&dev->dev,
  466. "%d:%d: usb_set_interface failed (%d)\n",
  467. fmt->iface, fmt->altsetting, err);
  468. return -EIO;
  469. }
  470. dev_dbg(&dev->dev, "setting usb interface %d:%d\n",
  471. fmt->iface, fmt->altsetting);
  472. subs->interface = fmt->iface;
  473. subs->altset_idx = fmt->altset_idx;
  474. snd_usb_set_interface_quirk(dev);
  475. }
  476. subs->data_endpoint = snd_usb_add_endpoint(subs->stream->chip,
  477. alts, fmt->endpoint, subs->direction,
  478. SND_USB_ENDPOINT_TYPE_DATA);
  479. if (!subs->data_endpoint)
  480. return -EINVAL;
  481. err = set_sync_endpoint(subs, fmt, dev, alts, altsd);
  482. if (err < 0)
  483. return err;
  484. err = snd_usb_init_pitch(subs->stream->chip, fmt->iface, alts, fmt);
  485. if (err < 0)
  486. return err;
  487. subs->cur_audiofmt = fmt;
  488. snd_usb_set_format_quirk(subs, fmt);
  489. return 0;
  490. }
  491. /*
  492. * Return the score of matching two audioformats.
  493. * Veto the audioformat if:
  494. * - It has no channels for some reason.
  495. * - Requested PCM format is not supported.
  496. * - Requested sample rate is not supported.
  497. */
  498. static int match_endpoint_audioformats(struct snd_usb_substream *subs,
  499. struct audioformat *fp,
  500. struct audioformat *match, int rate,
  501. snd_pcm_format_t pcm_format)
  502. {
  503. int i;
  504. int score = 0;
  505. if (fp->channels < 1) {
  506. dev_dbg(&subs->dev->dev,
  507. "%s: (fmt @%p) no channels\n", __func__, fp);
  508. return 0;
  509. }
  510. if (!(fp->formats & pcm_format_to_bits(pcm_format))) {
  511. dev_dbg(&subs->dev->dev,
  512. "%s: (fmt @%p) no match for format %d\n", __func__,
  513. fp, pcm_format);
  514. return 0;
  515. }
  516. for (i = 0; i < fp->nr_rates; i++) {
  517. if (fp->rate_table[i] == rate) {
  518. score++;
  519. break;
  520. }
  521. }
  522. if (!score) {
  523. dev_dbg(&subs->dev->dev,
  524. "%s: (fmt @%p) no match for rate %d\n", __func__,
  525. fp, rate);
  526. return 0;
  527. }
  528. if (fp->channels == match->channels)
  529. score++;
  530. dev_dbg(&subs->dev->dev,
  531. "%s: (fmt @%p) score %d\n", __func__, fp, score);
  532. return score;
  533. }
  534. /*
  535. * Configure the sync ep using the rate and pcm format of the data ep.
  536. */
  537. static int configure_sync_endpoint(struct snd_usb_substream *subs)
  538. {
  539. int ret;
  540. struct audioformat *fp;
  541. struct audioformat *sync_fp = NULL;
  542. int cur_score = 0;
  543. int sync_period_bytes = subs->period_bytes;
  544. struct snd_usb_substream *sync_subs =
  545. &subs->stream->substream[subs->direction ^ 1];
  546. if (subs->sync_endpoint->type != SND_USB_ENDPOINT_TYPE_DATA ||
  547. !subs->stream)
  548. return snd_usb_endpoint_set_params(subs->sync_endpoint,
  549. subs->pcm_format,
  550. subs->channels,
  551. subs->period_bytes,
  552. 0, 0,
  553. subs->cur_rate,
  554. subs->cur_audiofmt,
  555. NULL);
  556. /* Try to find the best matching audioformat. */
  557. list_for_each_entry(fp, &sync_subs->fmt_list, list) {
  558. int score = match_endpoint_audioformats(subs,
  559. fp, subs->cur_audiofmt,
  560. subs->cur_rate, subs->pcm_format);
  561. if (score > cur_score) {
  562. sync_fp = fp;
  563. cur_score = score;
  564. }
  565. }
  566. if (unlikely(sync_fp == NULL)) {
  567. dev_err(&subs->dev->dev,
  568. "%s: no valid audioformat for sync ep %x found\n",
  569. __func__, sync_subs->ep_num);
  570. return -EINVAL;
  571. }
  572. /*
  573. * Recalculate the period bytes if channel number differ between
  574. * data and sync ep audioformat.
  575. */
  576. if (sync_fp->channels != subs->channels) {
  577. sync_period_bytes = (subs->period_bytes / subs->channels) *
  578. sync_fp->channels;
  579. dev_dbg(&subs->dev->dev,
  580. "%s: adjusted sync ep period bytes (%d -> %d)\n",
  581. __func__, subs->period_bytes, sync_period_bytes);
  582. }
  583. ret = snd_usb_endpoint_set_params(subs->sync_endpoint,
  584. subs->pcm_format,
  585. sync_fp->channels,
  586. sync_period_bytes,
  587. 0, 0,
  588. subs->cur_rate,
  589. sync_fp,
  590. NULL);
  591. return ret;
  592. }
  593. /*
  594. * configure endpoint params
  595. *
  596. * called during initial setup and upon resume
  597. */
  598. static int configure_endpoint(struct snd_usb_substream *subs)
  599. {
  600. int ret;
  601. /* format changed */
  602. stop_endpoints(subs, true);
  603. ret = snd_usb_endpoint_set_params(subs->data_endpoint,
  604. subs->pcm_format,
  605. subs->channels,
  606. subs->period_bytes,
  607. subs->period_frames,
  608. subs->buffer_periods,
  609. subs->cur_rate,
  610. subs->cur_audiofmt,
  611. subs->sync_endpoint);
  612. if (ret < 0)
  613. return ret;
  614. if (subs->sync_endpoint)
  615. ret = configure_sync_endpoint(subs);
  616. return ret;
  617. }
  618. /*
  619. * hw_params callback
  620. *
  621. * allocate a buffer and set the given audio format.
  622. *
  623. * so far we use a physically linear buffer although packetize transfer
  624. * doesn't need a continuous area.
  625. * if sg buffer is supported on the later version of alsa, we'll follow
  626. * that.
  627. */
  628. static int snd_usb_hw_params(struct snd_pcm_substream *substream,
  629. struct snd_pcm_hw_params *hw_params)
  630. {
  631. struct snd_usb_substream *subs = substream->runtime->private_data;
  632. struct audioformat *fmt;
  633. int ret;
  634. ret = snd_pcm_lib_alloc_vmalloc_buffer(substream,
  635. params_buffer_bytes(hw_params));
  636. if (ret < 0)
  637. return ret;
  638. subs->pcm_format = params_format(hw_params);
  639. subs->period_bytes = params_period_bytes(hw_params);
  640. subs->period_frames = params_period_size(hw_params);
  641. subs->buffer_periods = params_periods(hw_params);
  642. subs->channels = params_channels(hw_params);
  643. subs->cur_rate = params_rate(hw_params);
  644. fmt = find_format(subs);
  645. if (!fmt) {
  646. dev_dbg(&subs->dev->dev,
  647. "cannot set format: format = %#x, rate = %d, channels = %d\n",
  648. subs->pcm_format, subs->cur_rate, subs->channels);
  649. return -EINVAL;
  650. }
  651. ret = snd_usb_lock_shutdown(subs->stream->chip);
  652. if (ret < 0)
  653. return ret;
  654. ret = set_format(subs, fmt);
  655. snd_usb_unlock_shutdown(subs->stream->chip);
  656. if (ret < 0)
  657. return ret;
  658. subs->interface = fmt->iface;
  659. subs->altset_idx = fmt->altset_idx;
  660. subs->need_setup_ep = true;
  661. return 0;
  662. }
  663. /*
  664. * hw_free callback
  665. *
  666. * reset the audio format and release the buffer
  667. */
  668. static int snd_usb_hw_free(struct snd_pcm_substream *substream)
  669. {
  670. struct snd_usb_substream *subs = substream->runtime->private_data;
  671. subs->cur_audiofmt = NULL;
  672. subs->cur_rate = 0;
  673. subs->period_bytes = 0;
  674. if (!snd_usb_lock_shutdown(subs->stream->chip)) {
  675. stop_endpoints(subs, true);
  676. snd_usb_endpoint_deactivate(subs->sync_endpoint);
  677. snd_usb_endpoint_deactivate(subs->data_endpoint);
  678. snd_usb_unlock_shutdown(subs->stream->chip);
  679. }
  680. return snd_pcm_lib_free_vmalloc_buffer(substream);
  681. }
  682. /*
  683. * prepare callback
  684. *
  685. * only a few subtle things...
  686. */
  687. static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream)
  688. {
  689. struct snd_pcm_runtime *runtime = substream->runtime;
  690. struct snd_usb_substream *subs = runtime->private_data;
  691. struct usb_host_interface *alts;
  692. struct usb_interface *iface;
  693. int ret;
  694. if (! subs->cur_audiofmt) {
  695. dev_err(&subs->dev->dev, "no format is specified!\n");
  696. return -ENXIO;
  697. }
  698. ret = snd_usb_lock_shutdown(subs->stream->chip);
  699. if (ret < 0)
  700. return ret;
  701. if (snd_BUG_ON(!subs->data_endpoint)) {
  702. ret = -EIO;
  703. goto unlock;
  704. }
  705. snd_usb_endpoint_sync_pending_stop(subs->sync_endpoint);
  706. snd_usb_endpoint_sync_pending_stop(subs->data_endpoint);
  707. ret = set_format(subs, subs->cur_audiofmt);
  708. if (ret < 0)
  709. goto unlock;
  710. iface = usb_ifnum_to_if(subs->dev, subs->cur_audiofmt->iface);
  711. alts = &iface->altsetting[subs->cur_audiofmt->altset_idx];
  712. ret = snd_usb_init_sample_rate(subs->stream->chip,
  713. subs->cur_audiofmt->iface,
  714. alts,
  715. subs->cur_audiofmt,
  716. subs->cur_rate);
  717. if (ret < 0)
  718. goto unlock;
  719. if (subs->need_setup_ep) {
  720. ret = configure_endpoint(subs);
  721. if (ret < 0)
  722. goto unlock;
  723. subs->need_setup_ep = false;
  724. }
  725. /* some unit conversions in runtime */
  726. subs->data_endpoint->maxframesize =
  727. bytes_to_frames(runtime, subs->data_endpoint->maxpacksize);
  728. subs->data_endpoint->curframesize =
  729. bytes_to_frames(runtime, subs->data_endpoint->curpacksize);
  730. /* reset the pointer */
  731. subs->hwptr_done = 0;
  732. subs->transfer_done = 0;
  733. subs->last_delay = 0;
  734. subs->last_frame_number = 0;
  735. runtime->delay = 0;
  736. /* for playback, submit the URBs now; otherwise, the first hwptr_done
  737. * updates for all URBs would happen at the same time when starting */
  738. if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK)
  739. ret = start_endpoints(subs);
  740. unlock:
  741. snd_usb_unlock_shutdown(subs->stream->chip);
  742. return ret;
  743. }
  744. static struct snd_pcm_hardware snd_usb_hardware =
  745. {
  746. .info = SNDRV_PCM_INFO_MMAP |
  747. SNDRV_PCM_INFO_MMAP_VALID |
  748. SNDRV_PCM_INFO_BATCH |
  749. SNDRV_PCM_INFO_INTERLEAVED |
  750. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  751. SNDRV_PCM_INFO_PAUSE,
  752. .buffer_bytes_max = 1024 * 1024,
  753. .period_bytes_min = 64,
  754. .period_bytes_max = 512 * 1024,
  755. .periods_min = 2,
  756. .periods_max = 1024,
  757. };
  758. static int hw_check_valid_format(struct snd_usb_substream *subs,
  759. struct snd_pcm_hw_params *params,
  760. struct audioformat *fp)
  761. {
  762. struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
  763. struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
  764. struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
  765. struct snd_interval *pt = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
  766. struct snd_mask check_fmts;
  767. unsigned int ptime;
  768. /* check the format */
  769. snd_mask_none(&check_fmts);
  770. check_fmts.bits[0] = (u32)fp->formats;
  771. check_fmts.bits[1] = (u32)(fp->formats >> 32);
  772. snd_mask_intersect(&check_fmts, fmts);
  773. if (snd_mask_empty(&check_fmts)) {
  774. hwc_debug(" > check: no supported format %d\n", fp->format);
  775. return 0;
  776. }
  777. /* check the channels */
  778. if (fp->channels < ct->min || fp->channels > ct->max) {
  779. hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max);
  780. return 0;
  781. }
  782. /* check the rate is within the range */
  783. if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) {
  784. hwc_debug(" > check: rate_min %d > max %d\n", fp->rate_min, it->max);
  785. return 0;
  786. }
  787. if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) {
  788. hwc_debug(" > check: rate_max %d < min %d\n", fp->rate_max, it->min);
  789. return 0;
  790. }
  791. /* check whether the period time is >= the data packet interval */
  792. if (subs->speed != USB_SPEED_FULL) {
  793. ptime = 125 * (1 << fp->datainterval);
  794. if (ptime > pt->max || (ptime == pt->max && pt->openmax)) {
  795. hwc_debug(" > check: ptime %u > max %u\n", ptime, pt->max);
  796. return 0;
  797. }
  798. }
  799. return 1;
  800. }
  801. static int hw_rule_rate(struct snd_pcm_hw_params *params,
  802. struct snd_pcm_hw_rule *rule)
  803. {
  804. struct snd_usb_substream *subs = rule->private;
  805. struct audioformat *fp;
  806. struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
  807. unsigned int rmin, rmax;
  808. int changed;
  809. hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max);
  810. changed = 0;
  811. rmin = rmax = 0;
  812. list_for_each_entry(fp, &subs->fmt_list, list) {
  813. if (!hw_check_valid_format(subs, params, fp))
  814. continue;
  815. if (changed++) {
  816. if (rmin > fp->rate_min)
  817. rmin = fp->rate_min;
  818. if (rmax < fp->rate_max)
  819. rmax = fp->rate_max;
  820. } else {
  821. rmin = fp->rate_min;
  822. rmax = fp->rate_max;
  823. }
  824. }
  825. if (!changed) {
  826. hwc_debug(" --> get empty\n");
  827. it->empty = 1;
  828. return -EINVAL;
  829. }
  830. changed = 0;
  831. if (it->min < rmin) {
  832. it->min = rmin;
  833. it->openmin = 0;
  834. changed = 1;
  835. }
  836. if (it->max > rmax) {
  837. it->max = rmax;
  838. it->openmax = 0;
  839. changed = 1;
  840. }
  841. if (snd_interval_checkempty(it)) {
  842. it->empty = 1;
  843. return -EINVAL;
  844. }
  845. hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
  846. return changed;
  847. }
  848. static int hw_rule_channels(struct snd_pcm_hw_params *params,
  849. struct snd_pcm_hw_rule *rule)
  850. {
  851. struct snd_usb_substream *subs = rule->private;
  852. struct audioformat *fp;
  853. struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
  854. unsigned int rmin, rmax;
  855. int changed;
  856. hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max);
  857. changed = 0;
  858. rmin = rmax = 0;
  859. list_for_each_entry(fp, &subs->fmt_list, list) {
  860. if (!hw_check_valid_format(subs, params, fp))
  861. continue;
  862. if (changed++) {
  863. if (rmin > fp->channels)
  864. rmin = fp->channels;
  865. if (rmax < fp->channels)
  866. rmax = fp->channels;
  867. } else {
  868. rmin = fp->channels;
  869. rmax = fp->channels;
  870. }
  871. }
  872. if (!changed) {
  873. hwc_debug(" --> get empty\n");
  874. it->empty = 1;
  875. return -EINVAL;
  876. }
  877. changed = 0;
  878. if (it->min < rmin) {
  879. it->min = rmin;
  880. it->openmin = 0;
  881. changed = 1;
  882. }
  883. if (it->max > rmax) {
  884. it->max = rmax;
  885. it->openmax = 0;
  886. changed = 1;
  887. }
  888. if (snd_interval_checkempty(it)) {
  889. it->empty = 1;
  890. return -EINVAL;
  891. }
  892. hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
  893. return changed;
  894. }
  895. static int hw_rule_format(struct snd_pcm_hw_params *params,
  896. struct snd_pcm_hw_rule *rule)
  897. {
  898. struct snd_usb_substream *subs = rule->private;
  899. struct audioformat *fp;
  900. struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
  901. u64 fbits;
  902. u32 oldbits[2];
  903. int changed;
  904. hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]);
  905. fbits = 0;
  906. list_for_each_entry(fp, &subs->fmt_list, list) {
  907. if (!hw_check_valid_format(subs, params, fp))
  908. continue;
  909. fbits |= fp->formats;
  910. }
  911. oldbits[0] = fmt->bits[0];
  912. oldbits[1] = fmt->bits[1];
  913. fmt->bits[0] &= (u32)fbits;
  914. fmt->bits[1] &= (u32)(fbits >> 32);
  915. if (!fmt->bits[0] && !fmt->bits[1]) {
  916. hwc_debug(" --> get empty\n");
  917. return -EINVAL;
  918. }
  919. changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]);
  920. hwc_debug(" --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed);
  921. return changed;
  922. }
  923. static int hw_rule_period_time(struct snd_pcm_hw_params *params,
  924. struct snd_pcm_hw_rule *rule)
  925. {
  926. struct snd_usb_substream *subs = rule->private;
  927. struct audioformat *fp;
  928. struct snd_interval *it;
  929. unsigned char min_datainterval;
  930. unsigned int pmin;
  931. int changed;
  932. it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
  933. hwc_debug("hw_rule_period_time: (%u,%u)\n", it->min, it->max);
  934. min_datainterval = 0xff;
  935. list_for_each_entry(fp, &subs->fmt_list, list) {
  936. if (!hw_check_valid_format(subs, params, fp))
  937. continue;
  938. min_datainterval = min(min_datainterval, fp->datainterval);
  939. }
  940. if (min_datainterval == 0xff) {
  941. hwc_debug(" --> get empty\n");
  942. it->empty = 1;
  943. return -EINVAL;
  944. }
  945. pmin = 125 * (1 << min_datainterval);
  946. changed = 0;
  947. if (it->min < pmin) {
  948. it->min = pmin;
  949. it->openmin = 0;
  950. changed = 1;
  951. }
  952. if (snd_interval_checkempty(it)) {
  953. it->empty = 1;
  954. return -EINVAL;
  955. }
  956. hwc_debug(" --> (%u,%u) (changed = %d)\n", it->min, it->max, changed);
  957. return changed;
  958. }
  959. /*
  960. * If the device supports unusual bit rates, does the request meet these?
  961. */
  962. static int snd_usb_pcm_check_knot(struct snd_pcm_runtime *runtime,
  963. struct snd_usb_substream *subs)
  964. {
  965. struct audioformat *fp;
  966. int *rate_list;
  967. int count = 0, needs_knot = 0;
  968. int err;
  969. kfree(subs->rate_list.list);
  970. subs->rate_list.list = NULL;
  971. list_for_each_entry(fp, &subs->fmt_list, list) {
  972. if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)
  973. return 0;
  974. count += fp->nr_rates;
  975. if (fp->rates & SNDRV_PCM_RATE_KNOT)
  976. needs_knot = 1;
  977. }
  978. if (!needs_knot)
  979. return 0;
  980. subs->rate_list.list = rate_list =
  981. kmalloc(sizeof(int) * count, GFP_KERNEL);
  982. if (!subs->rate_list.list)
  983. return -ENOMEM;
  984. subs->rate_list.count = count;
  985. subs->rate_list.mask = 0;
  986. count = 0;
  987. list_for_each_entry(fp, &subs->fmt_list, list) {
  988. int i;
  989. for (i = 0; i < fp->nr_rates; i++)
  990. rate_list[count++] = fp->rate_table[i];
  991. }
  992. err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  993. &subs->rate_list);
  994. if (err < 0)
  995. return err;
  996. return 0;
  997. }
  998. /*
  999. * set up the runtime hardware information.
  1000. */
  1001. static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs)
  1002. {
  1003. struct audioformat *fp;
  1004. unsigned int pt, ptmin;
  1005. int param_period_time_if_needed;
  1006. int err;
  1007. runtime->hw.formats = subs->formats;
  1008. runtime->hw.rate_min = 0x7fffffff;
  1009. runtime->hw.rate_max = 0;
  1010. runtime->hw.channels_min = 256;
  1011. runtime->hw.channels_max = 0;
  1012. runtime->hw.rates = 0;
  1013. ptmin = UINT_MAX;
  1014. /* check min/max rates and channels */
  1015. list_for_each_entry(fp, &subs->fmt_list, list) {
  1016. runtime->hw.rates |= fp->rates;
  1017. if (runtime->hw.rate_min > fp->rate_min)
  1018. runtime->hw.rate_min = fp->rate_min;
  1019. if (runtime->hw.rate_max < fp->rate_max)
  1020. runtime->hw.rate_max = fp->rate_max;
  1021. if (runtime->hw.channels_min > fp->channels)
  1022. runtime->hw.channels_min = fp->channels;
  1023. if (runtime->hw.channels_max < fp->channels)
  1024. runtime->hw.channels_max = fp->channels;
  1025. if (fp->fmt_type == UAC_FORMAT_TYPE_II && fp->frame_size > 0) {
  1026. /* FIXME: there might be more than one audio formats... */
  1027. runtime->hw.period_bytes_min = runtime->hw.period_bytes_max =
  1028. fp->frame_size;
  1029. }
  1030. pt = 125 * (1 << fp->datainterval);
  1031. ptmin = min(ptmin, pt);
  1032. }
  1033. err = snd_usb_autoresume(subs->stream->chip);
  1034. if (err < 0)
  1035. return err;
  1036. param_period_time_if_needed = SNDRV_PCM_HW_PARAM_PERIOD_TIME;
  1037. if (subs->speed == USB_SPEED_FULL)
  1038. /* full speed devices have fixed data packet interval */
  1039. ptmin = 1000;
  1040. if (ptmin == 1000)
  1041. /* if period time doesn't go below 1 ms, no rules needed */
  1042. param_period_time_if_needed = -1;
  1043. snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
  1044. ptmin, UINT_MAX);
  1045. if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  1046. hw_rule_rate, subs,
  1047. SNDRV_PCM_HW_PARAM_FORMAT,
  1048. SNDRV_PCM_HW_PARAM_CHANNELS,
  1049. param_period_time_if_needed,
  1050. -1)) < 0)
  1051. goto rep_err;
  1052. if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
  1053. hw_rule_channels, subs,
  1054. SNDRV_PCM_HW_PARAM_FORMAT,
  1055. SNDRV_PCM_HW_PARAM_RATE,
  1056. param_period_time_if_needed,
  1057. -1)) < 0)
  1058. goto rep_err;
  1059. if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
  1060. hw_rule_format, subs,
  1061. SNDRV_PCM_HW_PARAM_RATE,
  1062. SNDRV_PCM_HW_PARAM_CHANNELS,
  1063. param_period_time_if_needed,
  1064. -1)) < 0)
  1065. goto rep_err;
  1066. if (param_period_time_if_needed >= 0) {
  1067. err = snd_pcm_hw_rule_add(runtime, 0,
  1068. SNDRV_PCM_HW_PARAM_PERIOD_TIME,
  1069. hw_rule_period_time, subs,
  1070. SNDRV_PCM_HW_PARAM_FORMAT,
  1071. SNDRV_PCM_HW_PARAM_CHANNELS,
  1072. SNDRV_PCM_HW_PARAM_RATE,
  1073. -1);
  1074. if (err < 0)
  1075. goto rep_err;
  1076. }
  1077. if ((err = snd_usb_pcm_check_knot(runtime, subs)) < 0)
  1078. goto rep_err;
  1079. return 0;
  1080. rep_err:
  1081. snd_usb_autosuspend(subs->stream->chip);
  1082. return err;
  1083. }
  1084. static int snd_usb_pcm_open(struct snd_pcm_substream *substream, int direction)
  1085. {
  1086. struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
  1087. struct snd_pcm_runtime *runtime = substream->runtime;
  1088. struct snd_usb_substream *subs = &as->substream[direction];
  1089. subs->interface = -1;
  1090. subs->altset_idx = 0;
  1091. runtime->hw = snd_usb_hardware;
  1092. runtime->private_data = subs;
  1093. subs->pcm_substream = substream;
  1094. /* runtime PM is also done there */
  1095. /* initialize DSD/DOP context */
  1096. subs->dsd_dop.byte_idx = 0;
  1097. subs->dsd_dop.channel = 0;
  1098. subs->dsd_dop.marker = 1;
  1099. return setup_hw_info(runtime, subs);
  1100. }
  1101. static int snd_usb_pcm_close(struct snd_pcm_substream *substream, int direction)
  1102. {
  1103. struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
  1104. struct snd_usb_substream *subs = &as->substream[direction];
  1105. stop_endpoints(subs, true);
  1106. if (subs->interface >= 0 &&
  1107. !snd_usb_lock_shutdown(subs->stream->chip)) {
  1108. usb_set_interface(subs->dev, subs->interface, 0);
  1109. subs->interface = -1;
  1110. snd_usb_unlock_shutdown(subs->stream->chip);
  1111. }
  1112. subs->pcm_substream = NULL;
  1113. snd_usb_autosuspend(subs->stream->chip);
  1114. return 0;
  1115. }
  1116. /* Since a URB can handle only a single linear buffer, we must use double
  1117. * buffering when the data to be transferred overflows the buffer boundary.
  1118. * To avoid inconsistencies when updating hwptr_done, we use double buffering
  1119. * for all URBs.
  1120. */
  1121. static void retire_capture_urb(struct snd_usb_substream *subs,
  1122. struct urb *urb)
  1123. {
  1124. struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
  1125. unsigned int stride, frames, bytes, oldptr;
  1126. int i, period_elapsed = 0;
  1127. unsigned long flags;
  1128. unsigned char *cp;
  1129. int current_frame_number;
  1130. /* read frame number here, update pointer in critical section */
  1131. current_frame_number = usb_get_current_frame_number(subs->dev);
  1132. stride = runtime->frame_bits >> 3;
  1133. for (i = 0; i < urb->number_of_packets; i++) {
  1134. cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset + subs->pkt_offset_adj;
  1135. if (urb->iso_frame_desc[i].status && printk_ratelimit()) {
  1136. dev_dbg(&subs->dev->dev, "frame %d active: %d\n",
  1137. i, urb->iso_frame_desc[i].status);
  1138. // continue;
  1139. }
  1140. bytes = urb->iso_frame_desc[i].actual_length;
  1141. frames = bytes / stride;
  1142. if (!subs->txfr_quirk)
  1143. bytes = frames * stride;
  1144. if (bytes % (runtime->sample_bits >> 3) != 0) {
  1145. int oldbytes = bytes;
  1146. bytes = frames * stride;
  1147. dev_warn_ratelimited(&subs->dev->dev,
  1148. "Corrected urb data len. %d->%d\n",
  1149. oldbytes, bytes);
  1150. }
  1151. /* update the current pointer */
  1152. spin_lock_irqsave(&subs->lock, flags);
  1153. oldptr = subs->hwptr_done;
  1154. subs->hwptr_done += bytes;
  1155. if (subs->hwptr_done >= runtime->buffer_size * stride)
  1156. subs->hwptr_done -= runtime->buffer_size * stride;
  1157. frames = (bytes + (oldptr % stride)) / stride;
  1158. subs->transfer_done += frames;
  1159. if (subs->transfer_done >= runtime->period_size) {
  1160. subs->transfer_done -= runtime->period_size;
  1161. period_elapsed = 1;
  1162. }
  1163. /* capture delay is by construction limited to one URB,
  1164. * reset delays here
  1165. */
  1166. runtime->delay = subs->last_delay = 0;
  1167. /* realign last_frame_number */
  1168. subs->last_frame_number = current_frame_number;
  1169. subs->last_frame_number &= 0xFF; /* keep 8 LSBs */
  1170. spin_unlock_irqrestore(&subs->lock, flags);
  1171. /* copy a data chunk */
  1172. if (oldptr + bytes > runtime->buffer_size * stride) {
  1173. unsigned int bytes1 =
  1174. runtime->buffer_size * stride - oldptr;
  1175. memcpy(runtime->dma_area + oldptr, cp, bytes1);
  1176. memcpy(runtime->dma_area, cp + bytes1, bytes - bytes1);
  1177. } else {
  1178. memcpy(runtime->dma_area + oldptr, cp, bytes);
  1179. }
  1180. }
  1181. if (period_elapsed)
  1182. snd_pcm_period_elapsed(subs->pcm_substream);
  1183. }
  1184. static inline void fill_playback_urb_dsd_dop(struct snd_usb_substream *subs,
  1185. struct urb *urb, unsigned int bytes)
  1186. {
  1187. struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
  1188. unsigned int stride = runtime->frame_bits >> 3;
  1189. unsigned int dst_idx = 0;
  1190. unsigned int src_idx = subs->hwptr_done;
  1191. unsigned int wrap = runtime->buffer_size * stride;
  1192. u8 *dst = urb->transfer_buffer;
  1193. u8 *src = runtime->dma_area;
  1194. u8 marker[] = { 0x05, 0xfa };
  1195. /*
  1196. * The DSP DOP format defines a way to transport DSD samples over
  1197. * normal PCM data endpoints. It requires stuffing of marker bytes
  1198. * (0x05 and 0xfa, alternating per sample frame), and then expects
  1199. * 2 additional bytes of actual payload. The whole frame is stored
  1200. * LSB.
  1201. *
  1202. * Hence, for a stereo transport, the buffer layout looks like this,
  1203. * where L refers to left channel samples and R to right.
  1204. *
  1205. * L1 L2 0x05 R1 R2 0x05 L3 L4 0xfa R3 R4 0xfa
  1206. * L5 L6 0x05 R5 R6 0x05 L7 L8 0xfa R7 R8 0xfa
  1207. * .....
  1208. *
  1209. */
  1210. while (bytes--) {
  1211. if (++subs->dsd_dop.byte_idx == 3) {
  1212. /* frame boundary? */
  1213. dst[dst_idx++] = marker[subs->dsd_dop.marker];
  1214. src_idx += 2;
  1215. subs->dsd_dop.byte_idx = 0;
  1216. if (++subs->dsd_dop.channel % runtime->channels == 0) {
  1217. /* alternate the marker */
  1218. subs->dsd_dop.marker++;
  1219. subs->dsd_dop.marker %= ARRAY_SIZE(marker);
  1220. subs->dsd_dop.channel = 0;
  1221. }
  1222. } else {
  1223. /* stuff the DSD payload */
  1224. int idx = (src_idx + subs->dsd_dop.byte_idx - 1) % wrap;
  1225. if (subs->cur_audiofmt->dsd_bitrev)
  1226. dst[dst_idx++] = bitrev8(src[idx]);
  1227. else
  1228. dst[dst_idx++] = src[idx];
  1229. subs->hwptr_done++;
  1230. }
  1231. }
  1232. if (subs->hwptr_done >= runtime->buffer_size * stride)
  1233. subs->hwptr_done -= runtime->buffer_size * stride;
  1234. }
  1235. static void copy_to_urb(struct snd_usb_substream *subs, struct urb *urb,
  1236. int offset, int stride, unsigned int bytes)
  1237. {
  1238. struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
  1239. if (subs->hwptr_done + bytes > runtime->buffer_size * stride) {
  1240. /* err, the transferred area goes over buffer boundary. */
  1241. unsigned int bytes1 =
  1242. runtime->buffer_size * stride - subs->hwptr_done;
  1243. memcpy(urb->transfer_buffer + offset,
  1244. runtime->dma_area + subs->hwptr_done, bytes1);
  1245. memcpy(urb->transfer_buffer + offset + bytes1,
  1246. runtime->dma_area, bytes - bytes1);
  1247. } else {
  1248. memcpy(urb->transfer_buffer + offset,
  1249. runtime->dma_area + subs->hwptr_done, bytes);
  1250. }
  1251. subs->hwptr_done += bytes;
  1252. if (subs->hwptr_done >= runtime->buffer_size * stride)
  1253. subs->hwptr_done -= runtime->buffer_size * stride;
  1254. }
  1255. static unsigned int copy_to_urb_quirk(struct snd_usb_substream *subs,
  1256. struct urb *urb, int stride,
  1257. unsigned int bytes)
  1258. {
  1259. __le32 packet_length;
  1260. int i;
  1261. /* Put __le32 length descriptor at start of each packet. */
  1262. for (i = 0; i < urb->number_of_packets; i++) {
  1263. unsigned int length = urb->iso_frame_desc[i].length;
  1264. unsigned int offset = urb->iso_frame_desc[i].offset;
  1265. packet_length = cpu_to_le32(length);
  1266. offset += i * sizeof(packet_length);
  1267. urb->iso_frame_desc[i].offset = offset;
  1268. urb->iso_frame_desc[i].length += sizeof(packet_length);
  1269. memcpy(urb->transfer_buffer + offset,
  1270. &packet_length, sizeof(packet_length));
  1271. copy_to_urb(subs, urb, offset + sizeof(packet_length),
  1272. stride, length);
  1273. }
  1274. /* Adjust transfer size accordingly. */
  1275. bytes += urb->number_of_packets * sizeof(packet_length);
  1276. return bytes;
  1277. }
  1278. static void prepare_playback_urb(struct snd_usb_substream *subs,
  1279. struct urb *urb)
  1280. {
  1281. struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
  1282. struct snd_usb_endpoint *ep = subs->data_endpoint;
  1283. struct snd_urb_ctx *ctx = urb->context;
  1284. unsigned int counts, frames, bytes;
  1285. int i, stride, period_elapsed = 0;
  1286. unsigned long flags;
  1287. stride = runtime->frame_bits >> 3;
  1288. frames = 0;
  1289. urb->number_of_packets = 0;
  1290. spin_lock_irqsave(&subs->lock, flags);
  1291. subs->frame_limit += ep->max_urb_frames;
  1292. for (i = 0; i < ctx->packets; i++) {
  1293. if (ctx->packet_size[i])
  1294. counts = ctx->packet_size[i];
  1295. else
  1296. counts = snd_usb_endpoint_next_packet_size(ep);
  1297. /* set up descriptor */
  1298. urb->iso_frame_desc[i].offset = frames * ep->stride;
  1299. urb->iso_frame_desc[i].length = counts * ep->stride;
  1300. frames += counts;
  1301. urb->number_of_packets++;
  1302. subs->transfer_done += counts;
  1303. if (subs->transfer_done >= runtime->period_size) {
  1304. subs->transfer_done -= runtime->period_size;
  1305. subs->frame_limit = 0;
  1306. period_elapsed = 1;
  1307. if (subs->fmt_type == UAC_FORMAT_TYPE_II) {
  1308. if (subs->transfer_done > 0) {
  1309. /* FIXME: fill-max mode is not
  1310. * supported yet */
  1311. frames -= subs->transfer_done;
  1312. counts -= subs->transfer_done;
  1313. urb->iso_frame_desc[i].length =
  1314. counts * ep->stride;
  1315. subs->transfer_done = 0;
  1316. }
  1317. i++;
  1318. if (i < ctx->packets) {
  1319. /* add a transfer delimiter */
  1320. urb->iso_frame_desc[i].offset =
  1321. frames * ep->stride;
  1322. urb->iso_frame_desc[i].length = 0;
  1323. urb->number_of_packets++;
  1324. }
  1325. break;
  1326. }
  1327. }
  1328. /* finish at the period boundary or after enough frames */
  1329. if ((period_elapsed ||
  1330. subs->transfer_done >= subs->frame_limit) &&
  1331. !snd_usb_endpoint_implicit_feedback_sink(ep))
  1332. break;
  1333. }
  1334. bytes = frames * ep->stride;
  1335. if (unlikely(subs->pcm_format == SNDRV_PCM_FORMAT_DSD_U16_LE &&
  1336. subs->cur_audiofmt->dsd_dop)) {
  1337. fill_playback_urb_dsd_dop(subs, urb, bytes);
  1338. } else if (unlikely(subs->pcm_format == SNDRV_PCM_FORMAT_DSD_U8 &&
  1339. subs->cur_audiofmt->dsd_bitrev)) {
  1340. /* bit-reverse the bytes */
  1341. u8 *buf = urb->transfer_buffer;
  1342. for (i = 0; i < bytes; i++) {
  1343. int idx = (subs->hwptr_done + i)
  1344. % (runtime->buffer_size * stride);
  1345. buf[i] = bitrev8(runtime->dma_area[idx]);
  1346. }
  1347. subs->hwptr_done += bytes;
  1348. if (subs->hwptr_done >= runtime->buffer_size * stride)
  1349. subs->hwptr_done -= runtime->buffer_size * stride;
  1350. } else {
  1351. /* usual PCM */
  1352. if (!subs->tx_length_quirk)
  1353. copy_to_urb(subs, urb, 0, stride, bytes);
  1354. else
  1355. bytes = copy_to_urb_quirk(subs, urb, stride, bytes);
  1356. /* bytes is now amount of outgoing data */
  1357. }
  1358. /* update delay with exact number of samples queued */
  1359. runtime->delay = subs->last_delay;
  1360. runtime->delay += frames;
  1361. subs->last_delay = runtime->delay;
  1362. /* realign last_frame_number */
  1363. subs->last_frame_number = usb_get_current_frame_number(subs->dev);
  1364. subs->last_frame_number &= 0xFF; /* keep 8 LSBs */
  1365. if (subs->trigger_tstamp_pending_update) {
  1366. /* this is the first actual URB submitted,
  1367. * update trigger timestamp to reflect actual start time
  1368. */
  1369. snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
  1370. subs->trigger_tstamp_pending_update = false;
  1371. }
  1372. spin_unlock_irqrestore(&subs->lock, flags);
  1373. urb->transfer_buffer_length = bytes;
  1374. if (period_elapsed)
  1375. snd_pcm_period_elapsed(subs->pcm_substream);
  1376. }
  1377. /*
  1378. * process after playback data complete
  1379. * - decrease the delay count again
  1380. */
  1381. static void retire_playback_urb(struct snd_usb_substream *subs,
  1382. struct urb *urb)
  1383. {
  1384. unsigned long flags;
  1385. struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
  1386. struct snd_usb_endpoint *ep = subs->data_endpoint;
  1387. int processed = urb->transfer_buffer_length / ep->stride;
  1388. int est_delay;
  1389. /* ignore the delay accounting when procssed=0 is given, i.e.
  1390. * silent payloads are procssed before handling the actual data
  1391. */
  1392. if (!processed)
  1393. return;
  1394. spin_lock_irqsave(&subs->lock, flags);
  1395. if (!subs->last_delay)
  1396. goto out; /* short path */
  1397. est_delay = snd_usb_pcm_delay(subs, runtime->rate);
  1398. /* update delay with exact number of samples played */
  1399. if (processed > subs->last_delay)
  1400. subs->last_delay = 0;
  1401. else
  1402. subs->last_delay -= processed;
  1403. runtime->delay = subs->last_delay;
  1404. /*
  1405. * Report when delay estimate is off by more than 2ms.
  1406. * The error should be lower than 2ms since the estimate relies
  1407. * on two reads of a counter updated every ms.
  1408. */
  1409. if (abs(est_delay - subs->last_delay) * 1000 > runtime->rate * 2)
  1410. dev_dbg_ratelimited(&subs->dev->dev,
  1411. "delay: estimated %d, actual %d\n",
  1412. est_delay, subs->last_delay);
  1413. if (!subs->running) {
  1414. /* update last_frame_number for delay counting here since
  1415. * prepare_playback_urb won't be called during pause
  1416. */
  1417. subs->last_frame_number =
  1418. usb_get_current_frame_number(subs->dev) & 0xff;
  1419. }
  1420. out:
  1421. spin_unlock_irqrestore(&subs->lock, flags);
  1422. }
  1423. static int snd_usb_playback_open(struct snd_pcm_substream *substream)
  1424. {
  1425. return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_PLAYBACK);
  1426. }
  1427. static int snd_usb_playback_close(struct snd_pcm_substream *substream)
  1428. {
  1429. return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_PLAYBACK);
  1430. }
  1431. static int snd_usb_capture_open(struct snd_pcm_substream *substream)
  1432. {
  1433. return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_CAPTURE);
  1434. }
  1435. static int snd_usb_capture_close(struct snd_pcm_substream *substream)
  1436. {
  1437. return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_CAPTURE);
  1438. }
  1439. static int snd_usb_substream_playback_trigger(struct snd_pcm_substream *substream,
  1440. int cmd)
  1441. {
  1442. struct snd_usb_substream *subs = substream->runtime->private_data;
  1443. switch (cmd) {
  1444. case SNDRV_PCM_TRIGGER_START:
  1445. subs->trigger_tstamp_pending_update = true;
  1446. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  1447. subs->data_endpoint->prepare_data_urb = prepare_playback_urb;
  1448. subs->data_endpoint->retire_data_urb = retire_playback_urb;
  1449. subs->running = 1;
  1450. return 0;
  1451. case SNDRV_PCM_TRIGGER_STOP:
  1452. stop_endpoints(subs, false);
  1453. subs->running = 0;
  1454. return 0;
  1455. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  1456. subs->data_endpoint->prepare_data_urb = NULL;
  1457. /* keep retire_data_urb for delay calculation */
  1458. subs->data_endpoint->retire_data_urb = retire_playback_urb;
  1459. subs->running = 0;
  1460. return 0;
  1461. }
  1462. return -EINVAL;
  1463. }
  1464. static int snd_usb_substream_capture_trigger(struct snd_pcm_substream *substream,
  1465. int cmd)
  1466. {
  1467. int err;
  1468. struct snd_usb_substream *subs = substream->runtime->private_data;
  1469. switch (cmd) {
  1470. case SNDRV_PCM_TRIGGER_START:
  1471. err = start_endpoints(subs);
  1472. if (err < 0)
  1473. return err;
  1474. subs->data_endpoint->retire_data_urb = retire_capture_urb;
  1475. subs->running = 1;
  1476. return 0;
  1477. case SNDRV_PCM_TRIGGER_STOP:
  1478. stop_endpoints(subs, false);
  1479. subs->running = 0;
  1480. return 0;
  1481. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  1482. subs->data_endpoint->retire_data_urb = NULL;
  1483. subs->running = 0;
  1484. return 0;
  1485. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  1486. subs->data_endpoint->retire_data_urb = retire_capture_urb;
  1487. subs->running = 1;
  1488. return 0;
  1489. }
  1490. return -EINVAL;
  1491. }
  1492. static struct snd_pcm_ops snd_usb_playback_ops = {
  1493. .open = snd_usb_playback_open,
  1494. .close = snd_usb_playback_close,
  1495. .ioctl = snd_pcm_lib_ioctl,
  1496. .hw_params = snd_usb_hw_params,
  1497. .hw_free = snd_usb_hw_free,
  1498. .prepare = snd_usb_pcm_prepare,
  1499. .trigger = snd_usb_substream_playback_trigger,
  1500. .pointer = snd_usb_pcm_pointer,
  1501. .page = snd_pcm_lib_get_vmalloc_page,
  1502. .mmap = snd_pcm_lib_mmap_vmalloc,
  1503. };
  1504. static struct snd_pcm_ops snd_usb_capture_ops = {
  1505. .open = snd_usb_capture_open,
  1506. .close = snd_usb_capture_close,
  1507. .ioctl = snd_pcm_lib_ioctl,
  1508. .hw_params = snd_usb_hw_params,
  1509. .hw_free = snd_usb_hw_free,
  1510. .prepare = snd_usb_pcm_prepare,
  1511. .trigger = snd_usb_substream_capture_trigger,
  1512. .pointer = snd_usb_pcm_pointer,
  1513. .page = snd_pcm_lib_get_vmalloc_page,
  1514. .mmap = snd_pcm_lib_mmap_vmalloc,
  1515. };
  1516. void snd_usb_set_pcm_ops(struct snd_pcm *pcm, int stream)
  1517. {
  1518. snd_pcm_set_ops(pcm, stream,
  1519. stream == SNDRV_PCM_STREAM_PLAYBACK ?
  1520. &snd_usb_playback_ops : &snd_usb_capture_ops);
  1521. }