pcm.c 51 KB

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