pcm.c 48 KB

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