pcm.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766
  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. struct usb_interface *iface;
  556. ret = snd_usb_lock_shutdown(chip);
  557. if (ret < 0)
  558. return ret;
  559. if (snd_BUG_ON(!subs->data_endpoint)) {
  560. ret = -EIO;
  561. goto unlock;
  562. }
  563. ret = snd_usb_pcm_change_state(subs, UAC3_PD_STATE_D0);
  564. if (ret < 0)
  565. goto unlock;
  566. again:
  567. if (subs->sync_endpoint) {
  568. ret = snd_usb_endpoint_prepare(chip, subs->sync_endpoint);
  569. if (ret < 0)
  570. goto unlock;
  571. }
  572. ret = snd_usb_endpoint_prepare(chip, subs->data_endpoint);
  573. if (ret < 0)
  574. goto unlock;
  575. else if (ret > 0)
  576. snd_usb_set_format_quirk(subs, subs->cur_audiofmt);
  577. ret = 0;
  578. iface = usb_ifnum_to_if(chip->dev, subs->data_endpoint->iface);
  579. ret = snd_vendor_set_pcm_intf(iface, subs->data_endpoint->iface,
  580. subs->data_endpoint->altsetting,
  581. subs->direction, subs);
  582. if (ret)
  583. goto unlock;
  584. /* reset the pointer */
  585. subs->buffer_bytes = frames_to_bytes(runtime, runtime->buffer_size);
  586. subs->inflight_bytes = 0;
  587. subs->hwptr_done = 0;
  588. subs->transfer_done = 0;
  589. subs->last_frame_number = 0;
  590. subs->period_elapsed_pending = 0;
  591. runtime->delay = 0;
  592. subs->lowlatency_playback = lowlatency_playback_available(runtime, subs);
  593. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
  594. !subs->lowlatency_playback) {
  595. ret = start_endpoints(subs);
  596. /* if XRUN happens at starting streams (possibly with implicit
  597. * fb case), restart again, but only try once.
  598. */
  599. if (ret == -EPIPE && !retry++) {
  600. sync_pending_stops(subs);
  601. goto again;
  602. }
  603. }
  604. unlock:
  605. snd_usb_unlock_shutdown(chip);
  606. return ret;
  607. }
  608. /*
  609. * h/w constraints
  610. */
  611. #ifdef HW_CONST_DEBUG
  612. #define hwc_debug(fmt, args...) pr_debug(fmt, ##args)
  613. #else
  614. #define hwc_debug(fmt, args...) do { } while(0)
  615. #endif
  616. static const struct snd_pcm_hardware snd_usb_hardware =
  617. {
  618. .info = SNDRV_PCM_INFO_MMAP |
  619. SNDRV_PCM_INFO_MMAP_VALID |
  620. SNDRV_PCM_INFO_BATCH |
  621. SNDRV_PCM_INFO_INTERLEAVED |
  622. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  623. SNDRV_PCM_INFO_PAUSE,
  624. .channels_min = 1,
  625. .channels_max = 256,
  626. .buffer_bytes_max = INT_MAX, /* limited by BUFFER_TIME later */
  627. .period_bytes_min = 64,
  628. .period_bytes_max = INT_MAX, /* limited by PERIOD_TIME later */
  629. .periods_min = 2,
  630. .periods_max = 1024,
  631. };
  632. static int hw_check_valid_format(struct snd_usb_substream *subs,
  633. struct snd_pcm_hw_params *params,
  634. const struct audioformat *fp)
  635. {
  636. struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
  637. struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
  638. struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
  639. struct snd_interval *pt = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
  640. struct snd_mask check_fmts;
  641. unsigned int ptime;
  642. /* check the format */
  643. snd_mask_none(&check_fmts);
  644. check_fmts.bits[0] = (u32)fp->formats;
  645. check_fmts.bits[1] = (u32)(fp->formats >> 32);
  646. snd_mask_intersect(&check_fmts, fmts);
  647. if (snd_mask_empty(&check_fmts)) {
  648. hwc_debug(" > check: no supported format 0x%llx\n", fp->formats);
  649. return 0;
  650. }
  651. /* check the channels */
  652. if (fp->channels < ct->min || fp->channels > ct->max) {
  653. hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max);
  654. return 0;
  655. }
  656. /* check the rate is within the range */
  657. if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) {
  658. hwc_debug(" > check: rate_min %d > max %d\n", fp->rate_min, it->max);
  659. return 0;
  660. }
  661. if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) {
  662. hwc_debug(" > check: rate_max %d < min %d\n", fp->rate_max, it->min);
  663. return 0;
  664. }
  665. /* check whether the period time is >= the data packet interval */
  666. if (subs->speed != USB_SPEED_FULL) {
  667. ptime = 125 * (1 << fp->datainterval);
  668. if (ptime > pt->max || (ptime == pt->max && pt->openmax)) {
  669. hwc_debug(" > check: ptime %u > max %u\n", ptime, pt->max);
  670. return 0;
  671. }
  672. }
  673. return 1;
  674. }
  675. static int apply_hw_params_minmax(struct snd_interval *it, unsigned int rmin,
  676. unsigned int rmax)
  677. {
  678. int changed;
  679. if (rmin > rmax) {
  680. hwc_debug(" --> get empty\n");
  681. it->empty = 1;
  682. return -EINVAL;
  683. }
  684. changed = 0;
  685. if (it->min < rmin) {
  686. it->min = rmin;
  687. it->openmin = 0;
  688. changed = 1;
  689. }
  690. if (it->max > rmax) {
  691. it->max = rmax;
  692. it->openmax = 0;
  693. changed = 1;
  694. }
  695. if (snd_interval_checkempty(it)) {
  696. it->empty = 1;
  697. return -EINVAL;
  698. }
  699. hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
  700. return changed;
  701. }
  702. static int hw_rule_rate(struct snd_pcm_hw_params *params,
  703. struct snd_pcm_hw_rule *rule)
  704. {
  705. struct snd_usb_substream *subs = rule->private;
  706. struct snd_usb_audio *chip = subs->stream->chip;
  707. const struct audioformat *fp;
  708. struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
  709. unsigned int rmin, rmax, r;
  710. int i;
  711. hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max);
  712. rmin = UINT_MAX;
  713. rmax = 0;
  714. list_for_each_entry(fp, &subs->fmt_list, list) {
  715. if (!hw_check_valid_format(subs, params, fp))
  716. continue;
  717. r = snd_usb_endpoint_get_clock_rate(chip, fp->clock);
  718. if (r > 0) {
  719. if (!snd_interval_test(it, r))
  720. continue;
  721. rmin = min(rmin, r);
  722. rmax = max(rmax, r);
  723. continue;
  724. }
  725. if (fp->rate_table && fp->nr_rates) {
  726. for (i = 0; i < fp->nr_rates; i++) {
  727. r = fp->rate_table[i];
  728. if (!snd_interval_test(it, r))
  729. continue;
  730. rmin = min(rmin, r);
  731. rmax = max(rmax, r);
  732. }
  733. } else {
  734. rmin = min(rmin, fp->rate_min);
  735. rmax = max(rmax, fp->rate_max);
  736. }
  737. }
  738. return apply_hw_params_minmax(it, rmin, rmax);
  739. }
  740. static int hw_rule_channels(struct snd_pcm_hw_params *params,
  741. struct snd_pcm_hw_rule *rule)
  742. {
  743. struct snd_usb_substream *subs = rule->private;
  744. const struct audioformat *fp;
  745. struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
  746. unsigned int rmin, rmax;
  747. hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max);
  748. rmin = UINT_MAX;
  749. rmax = 0;
  750. list_for_each_entry(fp, &subs->fmt_list, list) {
  751. if (!hw_check_valid_format(subs, params, fp))
  752. continue;
  753. rmin = min(rmin, fp->channels);
  754. rmax = max(rmax, fp->channels);
  755. }
  756. return apply_hw_params_minmax(it, rmin, rmax);
  757. }
  758. static int apply_hw_params_format_bits(struct snd_mask *fmt, u64 fbits)
  759. {
  760. u32 oldbits[2];
  761. int changed;
  762. oldbits[0] = fmt->bits[0];
  763. oldbits[1] = fmt->bits[1];
  764. fmt->bits[0] &= (u32)fbits;
  765. fmt->bits[1] &= (u32)(fbits >> 32);
  766. if (!fmt->bits[0] && !fmt->bits[1]) {
  767. hwc_debug(" --> get empty\n");
  768. return -EINVAL;
  769. }
  770. changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]);
  771. hwc_debug(" --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed);
  772. return changed;
  773. }
  774. static int hw_rule_format(struct snd_pcm_hw_params *params,
  775. struct snd_pcm_hw_rule *rule)
  776. {
  777. struct snd_usb_substream *subs = rule->private;
  778. const struct audioformat *fp;
  779. struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
  780. u64 fbits;
  781. hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]);
  782. fbits = 0;
  783. list_for_each_entry(fp, &subs->fmt_list, list) {
  784. if (!hw_check_valid_format(subs, params, fp))
  785. continue;
  786. fbits |= fp->formats;
  787. }
  788. return apply_hw_params_format_bits(fmt, fbits);
  789. }
  790. static int hw_rule_period_time(struct snd_pcm_hw_params *params,
  791. struct snd_pcm_hw_rule *rule)
  792. {
  793. struct snd_usb_substream *subs = rule->private;
  794. const struct audioformat *fp;
  795. struct snd_interval *it;
  796. unsigned char min_datainterval;
  797. unsigned int pmin;
  798. it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
  799. hwc_debug("hw_rule_period_time: (%u,%u)\n", it->min, it->max);
  800. min_datainterval = 0xff;
  801. list_for_each_entry(fp, &subs->fmt_list, list) {
  802. if (!hw_check_valid_format(subs, params, fp))
  803. continue;
  804. min_datainterval = min(min_datainterval, fp->datainterval);
  805. }
  806. if (min_datainterval == 0xff) {
  807. hwc_debug(" --> get empty\n");
  808. it->empty = 1;
  809. return -EINVAL;
  810. }
  811. pmin = 125 * (1 << min_datainterval);
  812. return apply_hw_params_minmax(it, pmin, UINT_MAX);
  813. }
  814. /* get the EP or the sync EP for implicit fb when it's already set up */
  815. static const struct snd_usb_endpoint *
  816. get_sync_ep_from_substream(struct snd_usb_substream *subs)
  817. {
  818. struct snd_usb_audio *chip = subs->stream->chip;
  819. const struct audioformat *fp;
  820. const struct snd_usb_endpoint *ep;
  821. list_for_each_entry(fp, &subs->fmt_list, list) {
  822. ep = snd_usb_get_endpoint(chip, fp->endpoint);
  823. if (ep && ep->cur_audiofmt) {
  824. /* if EP is already opened solely for this substream,
  825. * we still allow us to change the parameter; otherwise
  826. * this substream has to follow the existing parameter
  827. */
  828. if (ep->cur_audiofmt != subs->cur_audiofmt || ep->opened > 1)
  829. return ep;
  830. }
  831. if (!fp->implicit_fb)
  832. continue;
  833. /* for the implicit fb, check the sync ep as well */
  834. ep = snd_usb_get_endpoint(chip, fp->sync_ep);
  835. if (ep && ep->cur_audiofmt) {
  836. /* ditto, if the sync (data) ep is used by others,
  837. * this stream is restricted by the sync ep
  838. */
  839. if (ep != subs->sync_endpoint || ep->opened > 1)
  840. return ep;
  841. }
  842. }
  843. return NULL;
  844. }
  845. /* additional hw constraints for implicit feedback mode */
  846. static int hw_rule_format_implicit_fb(struct snd_pcm_hw_params *params,
  847. struct snd_pcm_hw_rule *rule)
  848. {
  849. struct snd_usb_substream *subs = rule->private;
  850. const struct snd_usb_endpoint *ep;
  851. struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
  852. ep = get_sync_ep_from_substream(subs);
  853. if (!ep)
  854. return 0;
  855. hwc_debug("applying %s\n", __func__);
  856. return apply_hw_params_format_bits(fmt, pcm_format_to_bits(ep->cur_format));
  857. }
  858. static int hw_rule_rate_implicit_fb(struct snd_pcm_hw_params *params,
  859. struct snd_pcm_hw_rule *rule)
  860. {
  861. struct snd_usb_substream *subs = rule->private;
  862. const struct snd_usb_endpoint *ep;
  863. struct snd_interval *it;
  864. ep = get_sync_ep_from_substream(subs);
  865. if (!ep)
  866. return 0;
  867. hwc_debug("applying %s\n", __func__);
  868. it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
  869. return apply_hw_params_minmax(it, ep->cur_rate, ep->cur_rate);
  870. }
  871. static int hw_rule_period_size_implicit_fb(struct snd_pcm_hw_params *params,
  872. struct snd_pcm_hw_rule *rule)
  873. {
  874. struct snd_usb_substream *subs = rule->private;
  875. const struct snd_usb_endpoint *ep;
  876. struct snd_interval *it;
  877. ep = get_sync_ep_from_substream(subs);
  878. if (!ep)
  879. return 0;
  880. hwc_debug("applying %s\n", __func__);
  881. it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
  882. return apply_hw_params_minmax(it, ep->cur_period_frames,
  883. ep->cur_period_frames);
  884. }
  885. static int hw_rule_periods_implicit_fb(struct snd_pcm_hw_params *params,
  886. struct snd_pcm_hw_rule *rule)
  887. {
  888. struct snd_usb_substream *subs = rule->private;
  889. const struct snd_usb_endpoint *ep;
  890. struct snd_interval *it;
  891. ep = get_sync_ep_from_substream(subs);
  892. if (!ep)
  893. return 0;
  894. hwc_debug("applying %s\n", __func__);
  895. it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIODS);
  896. return apply_hw_params_minmax(it, ep->cur_buffer_periods,
  897. ep->cur_buffer_periods);
  898. }
  899. /*
  900. * set up the runtime hardware information.
  901. */
  902. static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs)
  903. {
  904. const struct audioformat *fp;
  905. unsigned int pt, ptmin;
  906. int param_period_time_if_needed = -1;
  907. int err;
  908. runtime->hw.formats = subs->formats;
  909. runtime->hw.rate_min = 0x7fffffff;
  910. runtime->hw.rate_max = 0;
  911. runtime->hw.channels_min = 256;
  912. runtime->hw.channels_max = 0;
  913. runtime->hw.rates = 0;
  914. ptmin = UINT_MAX;
  915. /* check min/max rates and channels */
  916. list_for_each_entry(fp, &subs->fmt_list, list) {
  917. runtime->hw.rates |= fp->rates;
  918. if (runtime->hw.rate_min > fp->rate_min)
  919. runtime->hw.rate_min = fp->rate_min;
  920. if (runtime->hw.rate_max < fp->rate_max)
  921. runtime->hw.rate_max = fp->rate_max;
  922. if (runtime->hw.channels_min > fp->channels)
  923. runtime->hw.channels_min = fp->channels;
  924. if (runtime->hw.channels_max < fp->channels)
  925. runtime->hw.channels_max = fp->channels;
  926. if (fp->fmt_type == UAC_FORMAT_TYPE_II && fp->frame_size > 0) {
  927. /* FIXME: there might be more than one audio formats... */
  928. runtime->hw.period_bytes_min = runtime->hw.period_bytes_max =
  929. fp->frame_size;
  930. }
  931. pt = 125 * (1 << fp->datainterval);
  932. ptmin = min(ptmin, pt);
  933. }
  934. param_period_time_if_needed = SNDRV_PCM_HW_PARAM_PERIOD_TIME;
  935. if (subs->speed == USB_SPEED_FULL)
  936. /* full speed devices have fixed data packet interval */
  937. ptmin = 1000;
  938. if (ptmin == 1000)
  939. /* if period time doesn't go below 1 ms, no rules needed */
  940. param_period_time_if_needed = -1;
  941. err = snd_pcm_hw_constraint_minmax(runtime,
  942. SNDRV_PCM_HW_PARAM_PERIOD_TIME,
  943. ptmin, UINT_MAX);
  944. if (err < 0)
  945. return err;
  946. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  947. hw_rule_rate, subs,
  948. SNDRV_PCM_HW_PARAM_RATE,
  949. SNDRV_PCM_HW_PARAM_FORMAT,
  950. SNDRV_PCM_HW_PARAM_CHANNELS,
  951. param_period_time_if_needed,
  952. -1);
  953. if (err < 0)
  954. return err;
  955. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
  956. hw_rule_channels, subs,
  957. SNDRV_PCM_HW_PARAM_CHANNELS,
  958. SNDRV_PCM_HW_PARAM_FORMAT,
  959. SNDRV_PCM_HW_PARAM_RATE,
  960. param_period_time_if_needed,
  961. -1);
  962. if (err < 0)
  963. return err;
  964. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
  965. hw_rule_format, subs,
  966. SNDRV_PCM_HW_PARAM_FORMAT,
  967. SNDRV_PCM_HW_PARAM_RATE,
  968. SNDRV_PCM_HW_PARAM_CHANNELS,
  969. param_period_time_if_needed,
  970. -1);
  971. if (err < 0)
  972. return err;
  973. if (param_period_time_if_needed >= 0) {
  974. err = snd_pcm_hw_rule_add(runtime, 0,
  975. SNDRV_PCM_HW_PARAM_PERIOD_TIME,
  976. hw_rule_period_time, subs,
  977. SNDRV_PCM_HW_PARAM_FORMAT,
  978. SNDRV_PCM_HW_PARAM_CHANNELS,
  979. SNDRV_PCM_HW_PARAM_RATE,
  980. -1);
  981. if (err < 0)
  982. return err;
  983. }
  984. /* set max period and buffer sizes for 1 and 2 seconds, respectively */
  985. err = snd_pcm_hw_constraint_minmax(runtime,
  986. SNDRV_PCM_HW_PARAM_PERIOD_TIME,
  987. 0, 1000000);
  988. if (err < 0)
  989. return err;
  990. err = snd_pcm_hw_constraint_minmax(runtime,
  991. SNDRV_PCM_HW_PARAM_BUFFER_TIME,
  992. 0, 2000000);
  993. if (err < 0)
  994. return err;
  995. /* additional hw constraints for implicit fb */
  996. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
  997. hw_rule_format_implicit_fb, subs,
  998. SNDRV_PCM_HW_PARAM_FORMAT, -1);
  999. if (err < 0)
  1000. return err;
  1001. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  1002. hw_rule_rate_implicit_fb, subs,
  1003. SNDRV_PCM_HW_PARAM_RATE, -1);
  1004. if (err < 0)
  1005. return err;
  1006. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
  1007. hw_rule_period_size_implicit_fb, subs,
  1008. SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
  1009. if (err < 0)
  1010. return err;
  1011. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
  1012. hw_rule_periods_implicit_fb, subs,
  1013. SNDRV_PCM_HW_PARAM_PERIODS, -1);
  1014. if (err < 0)
  1015. return err;
  1016. list_for_each_entry(fp, &subs->fmt_list, list) {
  1017. if (fp->implicit_fb) {
  1018. runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
  1019. break;
  1020. }
  1021. }
  1022. return 0;
  1023. }
  1024. static int snd_usb_pcm_open(struct snd_pcm_substream *substream)
  1025. {
  1026. int direction = substream->stream;
  1027. struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
  1028. struct snd_pcm_runtime *runtime = substream->runtime;
  1029. struct snd_usb_substream *subs = &as->substream[direction];
  1030. int ret;
  1031. ret = snd_vendor_set_pcm_connection(subs->dev, SOUND_PCM_OPEN,
  1032. direction);
  1033. if (ret)
  1034. return ret;
  1035. runtime->hw = snd_usb_hardware;
  1036. /* need an explicit sync to catch applptr update in low-latency mode */
  1037. if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
  1038. as->chip->lowlatency)
  1039. runtime->hw.info |= SNDRV_PCM_INFO_SYNC_APPLPTR;
  1040. runtime->private_data = subs;
  1041. subs->pcm_substream = substream;
  1042. /* runtime PM is also done there */
  1043. /* initialize DSD/DOP context */
  1044. subs->dsd_dop.byte_idx = 0;
  1045. subs->dsd_dop.channel = 0;
  1046. subs->dsd_dop.marker = 1;
  1047. ret = setup_hw_info(runtime, subs);
  1048. if (ret < 0)
  1049. return ret;
  1050. ret = snd_usb_autoresume(subs->stream->chip);
  1051. if (ret < 0)
  1052. return ret;
  1053. ret = snd_media_stream_init(subs, as->pcm, direction);
  1054. if (ret < 0)
  1055. snd_usb_autosuspend(subs->stream->chip);
  1056. return ret;
  1057. }
  1058. static int snd_usb_pcm_close(struct snd_pcm_substream *substream)
  1059. {
  1060. int direction = substream->stream;
  1061. struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
  1062. struct snd_usb_substream *subs = &as->substream[direction];
  1063. int ret;
  1064. ret = snd_vendor_set_pcm_connection(subs->dev, SOUND_PCM_CLOSE,
  1065. direction);
  1066. if (ret)
  1067. return ret;
  1068. snd_media_stop_pipeline(subs);
  1069. if (!snd_usb_lock_shutdown(subs->stream->chip)) {
  1070. ret = snd_usb_pcm_change_state(subs, UAC3_PD_STATE_D1);
  1071. snd_usb_unlock_shutdown(subs->stream->chip);
  1072. if (ret < 0)
  1073. return ret;
  1074. }
  1075. subs->pcm_substream = NULL;
  1076. snd_usb_autosuspend(subs->stream->chip);
  1077. return 0;
  1078. }
  1079. /* Since a URB can handle only a single linear buffer, we must use double
  1080. * buffering when the data to be transferred overflows the buffer boundary.
  1081. * To avoid inconsistencies when updating hwptr_done, we use double buffering
  1082. * for all URBs.
  1083. */
  1084. static void retire_capture_urb(struct snd_usb_substream *subs,
  1085. struct urb *urb)
  1086. {
  1087. struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
  1088. unsigned int stride, frames, bytes, oldptr;
  1089. int i, period_elapsed = 0;
  1090. unsigned long flags;
  1091. unsigned char *cp;
  1092. int current_frame_number;
  1093. /* read frame number here, update pointer in critical section */
  1094. current_frame_number = usb_get_current_frame_number(subs->dev);
  1095. stride = runtime->frame_bits >> 3;
  1096. for (i = 0; i < urb->number_of_packets; i++) {
  1097. cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset + subs->pkt_offset_adj;
  1098. if (urb->iso_frame_desc[i].status && printk_ratelimit()) {
  1099. dev_dbg(&subs->dev->dev, "frame %d active: %d\n",
  1100. i, urb->iso_frame_desc[i].status);
  1101. // continue;
  1102. }
  1103. bytes = urb->iso_frame_desc[i].actual_length;
  1104. if (subs->stream_offset_adj > 0) {
  1105. unsigned int adj = min(subs->stream_offset_adj, bytes);
  1106. cp += adj;
  1107. bytes -= adj;
  1108. subs->stream_offset_adj -= adj;
  1109. }
  1110. frames = bytes / stride;
  1111. if (!subs->txfr_quirk)
  1112. bytes = frames * stride;
  1113. if (bytes % (runtime->sample_bits >> 3) != 0) {
  1114. int oldbytes = bytes;
  1115. bytes = frames * stride;
  1116. dev_warn_ratelimited(&subs->dev->dev,
  1117. "Corrected urb data len. %d->%d\n",
  1118. oldbytes, bytes);
  1119. }
  1120. /* update the current pointer */
  1121. spin_lock_irqsave(&subs->lock, flags);
  1122. oldptr = subs->hwptr_done;
  1123. subs->hwptr_done += bytes;
  1124. if (subs->hwptr_done >= subs->buffer_bytes)
  1125. subs->hwptr_done -= subs->buffer_bytes;
  1126. frames = (bytes + (oldptr % stride)) / stride;
  1127. subs->transfer_done += frames;
  1128. if (subs->transfer_done >= runtime->period_size) {
  1129. subs->transfer_done -= runtime->period_size;
  1130. period_elapsed = 1;
  1131. }
  1132. /* realign last_frame_number */
  1133. subs->last_frame_number = current_frame_number;
  1134. spin_unlock_irqrestore(&subs->lock, flags);
  1135. /* copy a data chunk */
  1136. if (oldptr + bytes > subs->buffer_bytes) {
  1137. unsigned int bytes1 = subs->buffer_bytes - oldptr;
  1138. memcpy(runtime->dma_area + oldptr, cp, bytes1);
  1139. memcpy(runtime->dma_area, cp + bytes1, bytes - bytes1);
  1140. } else {
  1141. memcpy(runtime->dma_area + oldptr, cp, bytes);
  1142. }
  1143. }
  1144. if (period_elapsed)
  1145. snd_pcm_period_elapsed(subs->pcm_substream);
  1146. }
  1147. static void urb_ctx_queue_advance(struct snd_usb_substream *subs,
  1148. struct urb *urb, unsigned int bytes)
  1149. {
  1150. struct snd_urb_ctx *ctx = urb->context;
  1151. ctx->queued += bytes;
  1152. subs->inflight_bytes += bytes;
  1153. subs->hwptr_done += bytes;
  1154. if (subs->hwptr_done >= subs->buffer_bytes)
  1155. subs->hwptr_done -= subs->buffer_bytes;
  1156. }
  1157. static inline void fill_playback_urb_dsd_dop(struct snd_usb_substream *subs,
  1158. struct urb *urb, unsigned int bytes)
  1159. {
  1160. struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
  1161. unsigned int dst_idx = 0;
  1162. unsigned int src_idx = subs->hwptr_done;
  1163. unsigned int wrap = subs->buffer_bytes;
  1164. u8 *dst = urb->transfer_buffer;
  1165. u8 *src = runtime->dma_area;
  1166. static const u8 marker[] = { 0x05, 0xfa };
  1167. unsigned int queued = 0;
  1168. /*
  1169. * The DSP DOP format defines a way to transport DSD samples over
  1170. * normal PCM data endpoints. It requires stuffing of marker bytes
  1171. * (0x05 and 0xfa, alternating per sample frame), and then expects
  1172. * 2 additional bytes of actual payload. The whole frame is stored
  1173. * LSB.
  1174. *
  1175. * Hence, for a stereo transport, the buffer layout looks like this,
  1176. * where L refers to left channel samples and R to right.
  1177. *
  1178. * L1 L2 0x05 R1 R2 0x05 L3 L4 0xfa R3 R4 0xfa
  1179. * L5 L6 0x05 R5 R6 0x05 L7 L8 0xfa R7 R8 0xfa
  1180. * .....
  1181. *
  1182. */
  1183. while (bytes--) {
  1184. if (++subs->dsd_dop.byte_idx == 3) {
  1185. /* frame boundary? */
  1186. dst[dst_idx++] = marker[subs->dsd_dop.marker];
  1187. src_idx += 2;
  1188. subs->dsd_dop.byte_idx = 0;
  1189. if (++subs->dsd_dop.channel % runtime->channels == 0) {
  1190. /* alternate the marker */
  1191. subs->dsd_dop.marker++;
  1192. subs->dsd_dop.marker %= ARRAY_SIZE(marker);
  1193. subs->dsd_dop.channel = 0;
  1194. }
  1195. } else {
  1196. /* stuff the DSD payload */
  1197. int idx = (src_idx + subs->dsd_dop.byte_idx - 1) % wrap;
  1198. if (subs->cur_audiofmt->dsd_bitrev)
  1199. dst[dst_idx++] = bitrev8(src[idx]);
  1200. else
  1201. dst[dst_idx++] = src[idx];
  1202. queued++;
  1203. }
  1204. }
  1205. urb_ctx_queue_advance(subs, urb, queued);
  1206. }
  1207. /* copy bit-reversed bytes onto transfer buffer */
  1208. static void fill_playback_urb_dsd_bitrev(struct snd_usb_substream *subs,
  1209. struct urb *urb, unsigned int bytes)
  1210. {
  1211. struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
  1212. const u8 *src = runtime->dma_area;
  1213. u8 *buf = urb->transfer_buffer;
  1214. int i, ofs = subs->hwptr_done;
  1215. for (i = 0; i < bytes; i++) {
  1216. *buf++ = bitrev8(src[ofs]);
  1217. if (++ofs >= subs->buffer_bytes)
  1218. ofs = 0;
  1219. }
  1220. urb_ctx_queue_advance(subs, urb, bytes);
  1221. }
  1222. static void copy_to_urb(struct snd_usb_substream *subs, struct urb *urb,
  1223. int offset, int stride, unsigned int bytes)
  1224. {
  1225. struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
  1226. if (subs->hwptr_done + bytes > subs->buffer_bytes) {
  1227. /* err, the transferred area goes over buffer boundary. */
  1228. unsigned int bytes1 = subs->buffer_bytes - subs->hwptr_done;
  1229. memcpy(urb->transfer_buffer + offset,
  1230. runtime->dma_area + subs->hwptr_done, bytes1);
  1231. memcpy(urb->transfer_buffer + offset + bytes1,
  1232. runtime->dma_area, bytes - bytes1);
  1233. } else {
  1234. memcpy(urb->transfer_buffer + offset,
  1235. runtime->dma_area + subs->hwptr_done, bytes);
  1236. }
  1237. urb_ctx_queue_advance(subs, urb, bytes);
  1238. }
  1239. static unsigned int copy_to_urb_quirk(struct snd_usb_substream *subs,
  1240. struct urb *urb, int stride,
  1241. unsigned int bytes)
  1242. {
  1243. __le32 packet_length;
  1244. int i;
  1245. /* Put __le32 length descriptor at start of each packet. */
  1246. for (i = 0; i < urb->number_of_packets; i++) {
  1247. unsigned int length = urb->iso_frame_desc[i].length;
  1248. unsigned int offset = urb->iso_frame_desc[i].offset;
  1249. packet_length = cpu_to_le32(length);
  1250. offset += i * sizeof(packet_length);
  1251. urb->iso_frame_desc[i].offset = offset;
  1252. urb->iso_frame_desc[i].length += sizeof(packet_length);
  1253. memcpy(urb->transfer_buffer + offset,
  1254. &packet_length, sizeof(packet_length));
  1255. copy_to_urb(subs, urb, offset + sizeof(packet_length),
  1256. stride, length);
  1257. }
  1258. /* Adjust transfer size accordingly. */
  1259. bytes += urb->number_of_packets * sizeof(packet_length);
  1260. return bytes;
  1261. }
  1262. static int prepare_playback_urb(struct snd_usb_substream *subs,
  1263. struct urb *urb,
  1264. bool in_stream_lock)
  1265. {
  1266. struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
  1267. struct snd_usb_endpoint *ep = subs->data_endpoint;
  1268. struct snd_urb_ctx *ctx = urb->context;
  1269. unsigned int frames, bytes;
  1270. int counts;
  1271. unsigned int transfer_done, frame_limit, avail = 0;
  1272. int i, stride, period_elapsed = 0;
  1273. unsigned long flags;
  1274. int err = 0;
  1275. stride = ep->stride;
  1276. frames = 0;
  1277. ctx->queued = 0;
  1278. urb->number_of_packets = 0;
  1279. spin_lock_irqsave(&subs->lock, flags);
  1280. frame_limit = subs->frame_limit + ep->max_urb_frames;
  1281. transfer_done = subs->transfer_done;
  1282. if (subs->lowlatency_playback &&
  1283. runtime->state != SNDRV_PCM_STATE_DRAINING) {
  1284. unsigned int hwptr = subs->hwptr_done / stride;
  1285. /* calculate the byte offset-in-buffer of the appl_ptr */
  1286. avail = (runtime->control->appl_ptr - runtime->hw_ptr_base)
  1287. % runtime->buffer_size;
  1288. if (avail <= hwptr)
  1289. avail += runtime->buffer_size;
  1290. avail -= hwptr;
  1291. }
  1292. for (i = 0; i < ctx->packets; i++) {
  1293. counts = snd_usb_endpoint_next_packet_size(ep, ctx, i, avail);
  1294. if (counts < 0)
  1295. break;
  1296. /* set up descriptor */
  1297. urb->iso_frame_desc[i].offset = frames * stride;
  1298. urb->iso_frame_desc[i].length = counts * stride;
  1299. frames += counts;
  1300. avail -= counts;
  1301. urb->number_of_packets++;
  1302. transfer_done += counts;
  1303. if (transfer_done >= runtime->period_size) {
  1304. transfer_done -= runtime->period_size;
  1305. frame_limit = 0;
  1306. period_elapsed = 1;
  1307. if (subs->fmt_type == UAC_FORMAT_TYPE_II) {
  1308. if (transfer_done > 0) {
  1309. /* FIXME: fill-max mode is not
  1310. * supported yet */
  1311. frames -= transfer_done;
  1312. counts -= transfer_done;
  1313. urb->iso_frame_desc[i].length =
  1314. counts * stride;
  1315. transfer_done = 0;
  1316. }
  1317. i++;
  1318. if (i < ctx->packets) {
  1319. /* add a transfer delimiter */
  1320. urb->iso_frame_desc[i].offset =
  1321. frames * stride;
  1322. urb->iso_frame_desc[i].length = 0;
  1323. urb->number_of_packets++;
  1324. }
  1325. break;
  1326. }
  1327. }
  1328. /* finish at the period boundary or after enough frames */
  1329. if ((period_elapsed || transfer_done >= frame_limit) &&
  1330. !snd_usb_endpoint_implicit_feedback_sink(ep))
  1331. break;
  1332. }
  1333. if (!frames) {
  1334. err = -EAGAIN;
  1335. goto unlock;
  1336. }
  1337. bytes = frames * stride;
  1338. subs->transfer_done = transfer_done;
  1339. subs->frame_limit = frame_limit;
  1340. if (unlikely(ep->cur_format == SNDRV_PCM_FORMAT_DSD_U16_LE &&
  1341. subs->cur_audiofmt->dsd_dop)) {
  1342. fill_playback_urb_dsd_dop(subs, urb, bytes);
  1343. } else if (unlikely(ep->cur_format == SNDRV_PCM_FORMAT_DSD_U8 &&
  1344. subs->cur_audiofmt->dsd_bitrev)) {
  1345. fill_playback_urb_dsd_bitrev(subs, urb, bytes);
  1346. } else {
  1347. /* usual PCM */
  1348. if (!subs->tx_length_quirk)
  1349. copy_to_urb(subs, urb, 0, stride, bytes);
  1350. else
  1351. bytes = copy_to_urb_quirk(subs, urb, stride, bytes);
  1352. /* bytes is now amount of outgoing data */
  1353. }
  1354. subs->last_frame_number = usb_get_current_frame_number(subs->dev);
  1355. if (subs->trigger_tstamp_pending_update) {
  1356. /* this is the first actual URB submitted,
  1357. * update trigger timestamp to reflect actual start time
  1358. */
  1359. snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
  1360. subs->trigger_tstamp_pending_update = false;
  1361. }
  1362. if (period_elapsed && !subs->running && subs->lowlatency_playback) {
  1363. subs->period_elapsed_pending = 1;
  1364. period_elapsed = 0;
  1365. }
  1366. unlock:
  1367. spin_unlock_irqrestore(&subs->lock, flags);
  1368. if (err < 0)
  1369. return err;
  1370. urb->transfer_buffer_length = bytes;
  1371. if (period_elapsed) {
  1372. if (in_stream_lock)
  1373. snd_pcm_period_elapsed_under_stream_lock(subs->pcm_substream);
  1374. else
  1375. snd_pcm_period_elapsed(subs->pcm_substream);
  1376. }
  1377. return 0;
  1378. }
  1379. /*
  1380. * process after playback data complete
  1381. * - decrease the delay count again
  1382. */
  1383. static void retire_playback_urb(struct snd_usb_substream *subs,
  1384. struct urb *urb)
  1385. {
  1386. unsigned long flags;
  1387. struct snd_urb_ctx *ctx = urb->context;
  1388. bool period_elapsed = false;
  1389. spin_lock_irqsave(&subs->lock, flags);
  1390. if (ctx->queued) {
  1391. if (subs->inflight_bytes >= ctx->queued)
  1392. subs->inflight_bytes -= ctx->queued;
  1393. else
  1394. subs->inflight_bytes = 0;
  1395. }
  1396. subs->last_frame_number = usb_get_current_frame_number(subs->dev);
  1397. if (subs->running) {
  1398. period_elapsed = subs->period_elapsed_pending;
  1399. subs->period_elapsed_pending = 0;
  1400. }
  1401. spin_unlock_irqrestore(&subs->lock, flags);
  1402. if (period_elapsed)
  1403. snd_pcm_period_elapsed(subs->pcm_substream);
  1404. }
  1405. /* PCM ack callback for the playback stream;
  1406. * this plays a role only when the stream is running in low-latency mode.
  1407. */
  1408. static int snd_usb_pcm_playback_ack(struct snd_pcm_substream *substream)
  1409. {
  1410. struct snd_usb_substream *subs = substream->runtime->private_data;
  1411. struct snd_usb_endpoint *ep;
  1412. if (!subs->lowlatency_playback || !subs->running)
  1413. return 0;
  1414. ep = subs->data_endpoint;
  1415. if (!ep)
  1416. return 0;
  1417. /* When no more in-flight URBs available, try to process the pending
  1418. * outputs here
  1419. */
  1420. if (!ep->active_mask)
  1421. return snd_usb_queue_pending_output_urbs(ep, true);
  1422. return 0;
  1423. }
  1424. static int snd_usb_substream_playback_trigger(struct snd_pcm_substream *substream,
  1425. int cmd)
  1426. {
  1427. struct snd_usb_substream *subs = substream->runtime->private_data;
  1428. int err;
  1429. switch (cmd) {
  1430. case SNDRV_PCM_TRIGGER_START:
  1431. subs->trigger_tstamp_pending_update = true;
  1432. fallthrough;
  1433. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  1434. snd_usb_endpoint_set_callback(subs->data_endpoint,
  1435. prepare_playback_urb,
  1436. retire_playback_urb,
  1437. subs);
  1438. if (subs->lowlatency_playback &&
  1439. cmd == SNDRV_PCM_TRIGGER_START) {
  1440. if (in_free_wheeling_mode(substream->runtime))
  1441. subs->lowlatency_playback = false;
  1442. err = start_endpoints(subs);
  1443. if (err < 0) {
  1444. snd_usb_endpoint_set_callback(subs->data_endpoint,
  1445. NULL, NULL, NULL);
  1446. return err;
  1447. }
  1448. }
  1449. subs->running = 1;
  1450. dev_dbg(&subs->dev->dev, "%d:%d Start Playback PCM\n",
  1451. subs->cur_audiofmt->iface,
  1452. subs->cur_audiofmt->altsetting);
  1453. return 0;
  1454. case SNDRV_PCM_TRIGGER_SUSPEND:
  1455. case SNDRV_PCM_TRIGGER_STOP:
  1456. stop_endpoints(subs, substream->runtime->state == SNDRV_PCM_STATE_DRAINING);
  1457. snd_usb_endpoint_set_callback(subs->data_endpoint,
  1458. NULL, NULL, NULL);
  1459. subs->running = 0;
  1460. dev_dbg(&subs->dev->dev, "%d:%d Stop Playback PCM\n",
  1461. subs->cur_audiofmt->iface,
  1462. subs->cur_audiofmt->altsetting);
  1463. return 0;
  1464. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  1465. /* keep retire_data_urb for delay calculation */
  1466. snd_usb_endpoint_set_callback(subs->data_endpoint,
  1467. NULL,
  1468. retire_playback_urb,
  1469. subs);
  1470. subs->running = 0;
  1471. dev_dbg(&subs->dev->dev, "%d:%d Pause Playback PCM\n",
  1472. subs->cur_audiofmt->iface,
  1473. subs->cur_audiofmt->altsetting);
  1474. return 0;
  1475. }
  1476. return -EINVAL;
  1477. }
  1478. static int snd_usb_substream_capture_trigger(struct snd_pcm_substream *substream,
  1479. int cmd)
  1480. {
  1481. int err;
  1482. struct snd_usb_substream *subs = substream->runtime->private_data;
  1483. switch (cmd) {
  1484. case SNDRV_PCM_TRIGGER_START:
  1485. err = start_endpoints(subs);
  1486. if (err < 0)
  1487. return err;
  1488. fallthrough;
  1489. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  1490. snd_usb_endpoint_set_callback(subs->data_endpoint,
  1491. NULL, retire_capture_urb,
  1492. subs);
  1493. subs->last_frame_number = usb_get_current_frame_number(subs->dev);
  1494. subs->running = 1;
  1495. dev_dbg(&subs->dev->dev, "%d:%d Start Capture PCM\n",
  1496. subs->cur_audiofmt->iface,
  1497. subs->cur_audiofmt->altsetting);
  1498. return 0;
  1499. case SNDRV_PCM_TRIGGER_SUSPEND:
  1500. case SNDRV_PCM_TRIGGER_STOP:
  1501. stop_endpoints(subs, false);
  1502. fallthrough;
  1503. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  1504. snd_usb_endpoint_set_callback(subs->data_endpoint,
  1505. NULL, NULL, NULL);
  1506. subs->running = 0;
  1507. dev_dbg(&subs->dev->dev, "%d:%d Stop Capture PCM\n",
  1508. subs->cur_audiofmt->iface,
  1509. subs->cur_audiofmt->altsetting);
  1510. return 0;
  1511. }
  1512. return -EINVAL;
  1513. }
  1514. static const struct snd_pcm_ops snd_usb_playback_ops = {
  1515. .open = snd_usb_pcm_open,
  1516. .close = snd_usb_pcm_close,
  1517. .hw_params = snd_usb_hw_params,
  1518. .hw_free = snd_usb_hw_free,
  1519. .prepare = snd_usb_pcm_prepare,
  1520. .trigger = snd_usb_substream_playback_trigger,
  1521. .sync_stop = snd_usb_pcm_sync_stop,
  1522. .pointer = snd_usb_pcm_pointer,
  1523. .ack = snd_usb_pcm_playback_ack,
  1524. };
  1525. static const struct snd_pcm_ops snd_usb_capture_ops = {
  1526. .open = snd_usb_pcm_open,
  1527. .close = snd_usb_pcm_close,
  1528. .hw_params = snd_usb_hw_params,
  1529. .hw_free = snd_usb_hw_free,
  1530. .prepare = snd_usb_pcm_prepare,
  1531. .trigger = snd_usb_substream_capture_trigger,
  1532. .sync_stop = snd_usb_pcm_sync_stop,
  1533. .pointer = snd_usb_pcm_pointer,
  1534. };
  1535. void snd_usb_set_pcm_ops(struct snd_pcm *pcm, int stream)
  1536. {
  1537. const struct snd_pcm_ops *ops;
  1538. ops = stream == SNDRV_PCM_STREAM_PLAYBACK ?
  1539. &snd_usb_playback_ops : &snd_usb_capture_ops;
  1540. snd_pcm_set_ops(pcm, stream, ops);
  1541. }
  1542. void snd_usb_preallocate_buffer(struct snd_usb_substream *subs)
  1543. {
  1544. struct snd_pcm *pcm = subs->stream->pcm;
  1545. struct snd_pcm_substream *s = pcm->streams[subs->direction].substream;
  1546. struct device *dev = subs->dev->bus->sysdev;
  1547. if (snd_usb_use_vmalloc)
  1548. snd_pcm_set_managed_buffer(s, SNDRV_DMA_TYPE_VMALLOC,
  1549. NULL, 0, 0);
  1550. else
  1551. snd_pcm_set_managed_buffer(s, SNDRV_DMA_TYPE_DEV_SG,
  1552. dev, 64*1024, 512*1024);
  1553. }