pcm.c 48 KB

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