aloop.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Loopback soundcard
  4. *
  5. * Original code:
  6. * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
  7. *
  8. * More accurate positioning and full-duplex support:
  9. * Copyright (c) Ahmet İnan <ainan at mathematik.uni-freiburg.de>
  10. *
  11. * Major (almost complete) rewrite:
  12. * Copyright (c) by Takashi Iwai <tiwai@suse.de>
  13. *
  14. * A next major update in 2010 (separate timers for playback and capture):
  15. * Copyright (c) Jaroslav Kysela <perex@perex.cz>
  16. */
  17. #include <linux/init.h>
  18. #include <linux/jiffies.h>
  19. #include <linux/slab.h>
  20. #include <linux/string.h>
  21. #include <linux/time.h>
  22. #include <linux/wait.h>
  23. #include <linux/module.h>
  24. #include <linux/platform_device.h>
  25. #include <sound/core.h>
  26. #include <sound/control.h>
  27. #include <sound/pcm.h>
  28. #include <sound/pcm_params.h>
  29. #include <sound/info.h>
  30. #include <sound/initval.h>
  31. #include <sound/timer.h>
  32. MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
  33. MODULE_DESCRIPTION("A loopback soundcard");
  34. MODULE_LICENSE("GPL");
  35. #define MAX_PCM_SUBSTREAMS 8
  36. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
  37. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
  38. static bool enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 0};
  39. static int pcm_substreams[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 8};
  40. static int pcm_notify[SNDRV_CARDS];
  41. static char *timer_source[SNDRV_CARDS];
  42. module_param_array(index, int, NULL, 0444);
  43. MODULE_PARM_DESC(index, "Index value for loopback soundcard.");
  44. module_param_array(id, charp, NULL, 0444);
  45. MODULE_PARM_DESC(id, "ID string for loopback soundcard.");
  46. module_param_array(enable, bool, NULL, 0444);
  47. MODULE_PARM_DESC(enable, "Enable this loopback soundcard.");
  48. module_param_array(pcm_substreams, int, NULL, 0444);
  49. MODULE_PARM_DESC(pcm_substreams, "PCM substreams # (1-8) for loopback driver.");
  50. module_param_array(pcm_notify, int, NULL, 0444);
  51. MODULE_PARM_DESC(pcm_notify, "Break capture when PCM format/rate/channels changes.");
  52. module_param_array(timer_source, charp, NULL, 0444);
  53. MODULE_PARM_DESC(timer_source, "Sound card name or number and device/subdevice number of timer to be used. Empty string for jiffies timer [default].");
  54. #define NO_PITCH 100000
  55. #define CABLE_VALID_PLAYBACK BIT(SNDRV_PCM_STREAM_PLAYBACK)
  56. #define CABLE_VALID_CAPTURE BIT(SNDRV_PCM_STREAM_CAPTURE)
  57. #define CABLE_VALID_BOTH (CABLE_VALID_PLAYBACK | CABLE_VALID_CAPTURE)
  58. struct loopback_cable;
  59. struct loopback_pcm;
  60. struct loopback_ops {
  61. /* optional
  62. * call in loopback->cable_lock
  63. */
  64. int (*open)(struct loopback_pcm *dpcm);
  65. /* required
  66. * call in cable->lock
  67. */
  68. int (*start)(struct loopback_pcm *dpcm);
  69. /* required
  70. * call in cable->lock
  71. */
  72. int (*stop)(struct loopback_pcm *dpcm);
  73. /* optional */
  74. int (*stop_sync)(struct loopback_pcm *dpcm);
  75. /* optional */
  76. int (*close_substream)(struct loopback_pcm *dpcm);
  77. /* optional
  78. * call in loopback->cable_lock
  79. */
  80. int (*close_cable)(struct loopback_pcm *dpcm);
  81. /* optional
  82. * call in cable->lock
  83. */
  84. unsigned int (*pos_update)(struct loopback_cable *cable);
  85. /* optional */
  86. void (*dpcm_info)(struct loopback_pcm *dpcm,
  87. struct snd_info_buffer *buffer);
  88. };
  89. struct loopback_cable {
  90. spinlock_t lock;
  91. struct loopback_pcm *streams[2];
  92. struct snd_pcm_hardware hw;
  93. /* flags */
  94. unsigned int valid;
  95. unsigned int running;
  96. unsigned int pause;
  97. /* timer specific */
  98. const struct loopback_ops *ops;
  99. /* If sound timer is used */
  100. struct {
  101. int stream;
  102. struct snd_timer_id id;
  103. struct work_struct event_work;
  104. struct snd_timer_instance *instance;
  105. } snd_timer;
  106. };
  107. struct loopback_setup {
  108. unsigned int notify: 1;
  109. unsigned int rate_shift;
  110. snd_pcm_format_t format;
  111. unsigned int rate;
  112. snd_pcm_access_t access;
  113. unsigned int channels;
  114. struct snd_ctl_elem_id active_id;
  115. struct snd_ctl_elem_id format_id;
  116. struct snd_ctl_elem_id rate_id;
  117. struct snd_ctl_elem_id channels_id;
  118. struct snd_ctl_elem_id access_id;
  119. };
  120. struct loopback {
  121. struct snd_card *card;
  122. struct mutex cable_lock;
  123. struct loopback_cable *cables[MAX_PCM_SUBSTREAMS][2];
  124. struct snd_pcm *pcm[2];
  125. struct loopback_setup setup[MAX_PCM_SUBSTREAMS][2];
  126. const char *timer_source;
  127. };
  128. struct loopback_pcm {
  129. struct loopback *loopback;
  130. struct snd_pcm_substream *substream;
  131. struct loopback_cable *cable;
  132. unsigned int pcm_buffer_size;
  133. unsigned int buf_pos; /* position in buffer */
  134. unsigned int silent_size;
  135. /* PCM parameters */
  136. unsigned int pcm_period_size;
  137. unsigned int pcm_bps; /* bytes per second */
  138. unsigned int pcm_salign; /* bytes per sample * channels */
  139. unsigned int pcm_rate_shift; /* rate shift value */
  140. /* flags */
  141. unsigned int period_update_pending :1;
  142. /* timer stuff */
  143. unsigned int irq_pos; /* fractional IRQ position in jiffies
  144. * ticks
  145. */
  146. unsigned int period_size_frac; /* period size in jiffies ticks */
  147. unsigned int last_drift;
  148. unsigned long last_jiffies;
  149. /* If jiffies timer is used */
  150. struct timer_list timer;
  151. /* size of per channel buffer in case of non-interleaved access */
  152. unsigned int channel_buf_n;
  153. };
  154. static struct platform_device *devices[SNDRV_CARDS];
  155. static inline unsigned int byte_pos(struct loopback_pcm *dpcm, unsigned int x)
  156. {
  157. if (dpcm->pcm_rate_shift == NO_PITCH) {
  158. x /= HZ;
  159. } else {
  160. x = div_u64(NO_PITCH * (unsigned long long)x,
  161. HZ * (unsigned long long)dpcm->pcm_rate_shift);
  162. }
  163. return x - (x % dpcm->pcm_salign);
  164. }
  165. static inline unsigned int frac_pos(struct loopback_pcm *dpcm, unsigned int x)
  166. {
  167. if (dpcm->pcm_rate_shift == NO_PITCH) { /* no pitch */
  168. return x * HZ;
  169. } else {
  170. x = div_u64(dpcm->pcm_rate_shift * (unsigned long long)x * HZ,
  171. NO_PITCH);
  172. }
  173. return x;
  174. }
  175. static inline struct loopback_setup *get_setup(struct loopback_pcm *dpcm)
  176. {
  177. int device = dpcm->substream->pstr->pcm->device;
  178. if (dpcm->substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  179. device ^= 1;
  180. return &dpcm->loopback->setup[dpcm->substream->number][device];
  181. }
  182. static inline unsigned int get_notify(struct loopback_pcm *dpcm)
  183. {
  184. return get_setup(dpcm)->notify;
  185. }
  186. static inline unsigned int get_rate_shift(struct loopback_pcm *dpcm)
  187. {
  188. return get_setup(dpcm)->rate_shift;
  189. }
  190. /* call in cable->lock */
  191. static int loopback_jiffies_timer_start(struct loopback_pcm *dpcm)
  192. {
  193. unsigned long tick;
  194. unsigned int rate_shift = get_rate_shift(dpcm);
  195. if (rate_shift != dpcm->pcm_rate_shift) {
  196. dpcm->pcm_rate_shift = rate_shift;
  197. dpcm->period_size_frac = frac_pos(dpcm, dpcm->pcm_period_size);
  198. }
  199. if (dpcm->period_size_frac <= dpcm->irq_pos) {
  200. dpcm->irq_pos %= dpcm->period_size_frac;
  201. dpcm->period_update_pending = 1;
  202. }
  203. tick = dpcm->period_size_frac - dpcm->irq_pos;
  204. tick = DIV_ROUND_UP(tick, dpcm->pcm_bps);
  205. mod_timer(&dpcm->timer, jiffies + tick);
  206. return 0;
  207. }
  208. /* call in cable->lock */
  209. static int loopback_snd_timer_start(struct loopback_pcm *dpcm)
  210. {
  211. struct loopback_cable *cable = dpcm->cable;
  212. int err;
  213. /* Loopback device has to use same period as timer card. Therefore
  214. * wake up for each snd_pcm_period_elapsed() call of timer card.
  215. */
  216. err = snd_timer_start(cable->snd_timer.instance, 1);
  217. if (err < 0) {
  218. /* do not report error if trying to start but already
  219. * running. For example called by opposite substream
  220. * of the same cable
  221. */
  222. if (err == -EBUSY)
  223. return 0;
  224. pcm_err(dpcm->substream->pcm,
  225. "snd_timer_start(%d,%d,%d) failed with %d",
  226. cable->snd_timer.id.card,
  227. cable->snd_timer.id.device,
  228. cable->snd_timer.id.subdevice,
  229. err);
  230. }
  231. return err;
  232. }
  233. /* call in cable->lock */
  234. static inline int loopback_jiffies_timer_stop(struct loopback_pcm *dpcm)
  235. {
  236. timer_delete(&dpcm->timer);
  237. dpcm->timer.expires = 0;
  238. return 0;
  239. }
  240. /* call in cable->lock */
  241. static int loopback_snd_timer_stop(struct loopback_pcm *dpcm)
  242. {
  243. struct loopback_cable *cable = dpcm->cable;
  244. int err;
  245. /* only stop if both devices (playback and capture) are not running */
  246. if (cable->running ^ cable->pause)
  247. return 0;
  248. err = snd_timer_stop(cable->snd_timer.instance);
  249. if (err < 0) {
  250. pcm_err(dpcm->substream->pcm,
  251. "snd_timer_stop(%d,%d,%d) failed with %d",
  252. cable->snd_timer.id.card,
  253. cable->snd_timer.id.device,
  254. cable->snd_timer.id.subdevice,
  255. err);
  256. }
  257. return err;
  258. }
  259. static inline int loopback_jiffies_timer_stop_sync(struct loopback_pcm *dpcm)
  260. {
  261. timer_delete_sync(&dpcm->timer);
  262. return 0;
  263. }
  264. /* call in loopback->cable_lock */
  265. static int loopback_snd_timer_close_cable(struct loopback_pcm *dpcm)
  266. {
  267. struct loopback_cable *cable = dpcm->cable;
  268. /* snd_timer was not opened */
  269. if (!cable->snd_timer.instance)
  270. return 0;
  271. /* will only be called from free_cable() when other stream was
  272. * already closed. Other stream cannot be reopened as long as
  273. * loopback->cable_lock is locked. Therefore no need to lock
  274. * cable->lock;
  275. */
  276. snd_timer_close(cable->snd_timer.instance);
  277. /* wait till drain work has finished if requested */
  278. cancel_work_sync(&cable->snd_timer.event_work);
  279. snd_timer_instance_free(cable->snd_timer.instance);
  280. memset(&cable->snd_timer, 0, sizeof(cable->snd_timer));
  281. return 0;
  282. }
  283. static bool is_access_interleaved(snd_pcm_access_t access)
  284. {
  285. switch (access) {
  286. case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED:
  287. case SNDRV_PCM_ACCESS_RW_INTERLEAVED:
  288. return true;
  289. default:
  290. return false;
  291. }
  292. };
  293. static int loopback_check_format(struct loopback_cable *cable, int stream)
  294. {
  295. struct loopback_pcm *dpcm_play, *dpcm_capt;
  296. struct snd_pcm_runtime *runtime, *cruntime;
  297. struct loopback_setup *setup;
  298. struct snd_card *card;
  299. bool stop_capture = false;
  300. int check;
  301. scoped_guard(spinlock_irqsave, &cable->lock) {
  302. dpcm_play = cable->streams[SNDRV_PCM_STREAM_PLAYBACK];
  303. dpcm_capt = cable->streams[SNDRV_PCM_STREAM_CAPTURE];
  304. if (cable->valid != CABLE_VALID_BOTH) {
  305. if (stream == SNDRV_PCM_STREAM_CAPTURE || !dpcm_play)
  306. return 0;
  307. } else {
  308. if (!dpcm_play || !dpcm_capt)
  309. return -EIO;
  310. runtime = dpcm_play->substream->runtime;
  311. cruntime = dpcm_capt->substream->runtime;
  312. if (!runtime || !cruntime)
  313. return -EIO;
  314. check = runtime->format != cruntime->format ||
  315. runtime->rate != cruntime->rate ||
  316. runtime->channels != cruntime->channels ||
  317. is_access_interleaved(runtime->access) !=
  318. is_access_interleaved(cruntime->access);
  319. if (!check)
  320. return 0;
  321. if (stream == SNDRV_PCM_STREAM_CAPTURE)
  322. return -EIO;
  323. else if (cruntime->state == SNDRV_PCM_STATE_RUNNING)
  324. stop_capture = true;
  325. }
  326. setup = get_setup(dpcm_play);
  327. card = dpcm_play->loopback->card;
  328. runtime = dpcm_play->substream->runtime;
  329. if (setup->format != runtime->format) {
  330. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
  331. &setup->format_id);
  332. setup->format = runtime->format;
  333. }
  334. if (setup->rate != runtime->rate) {
  335. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
  336. &setup->rate_id);
  337. setup->rate = runtime->rate;
  338. }
  339. if (setup->channels != runtime->channels) {
  340. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
  341. &setup->channels_id);
  342. setup->channels = runtime->channels;
  343. }
  344. if (is_access_interleaved(setup->access) !=
  345. is_access_interleaved(runtime->access)) {
  346. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
  347. &setup->access_id);
  348. setup->access = runtime->access;
  349. }
  350. }
  351. if (stop_capture)
  352. snd_pcm_stop(dpcm_capt->substream, SNDRV_PCM_STATE_DRAINING);
  353. return 0;
  354. }
  355. static void loopback_active_notify(struct loopback_pcm *dpcm)
  356. {
  357. snd_ctl_notify(dpcm->loopback->card,
  358. SNDRV_CTL_EVENT_MASK_VALUE,
  359. &get_setup(dpcm)->active_id);
  360. }
  361. static int loopback_trigger(struct snd_pcm_substream *substream, int cmd)
  362. {
  363. struct snd_pcm_runtime *runtime = substream->runtime;
  364. struct loopback_pcm *dpcm = runtime->private_data;
  365. struct loopback_cable *cable = dpcm->cable;
  366. int err = 0, stream = 1 << substream->stream;
  367. switch (cmd) {
  368. case SNDRV_PCM_TRIGGER_START:
  369. err = loopback_check_format(cable, substream->stream);
  370. if (err < 0)
  371. return err;
  372. dpcm->last_jiffies = jiffies;
  373. dpcm->pcm_rate_shift = 0;
  374. dpcm->last_drift = 0;
  375. scoped_guard(spinlock, &cable->lock) {
  376. cable->running |= stream;
  377. cable->pause &= ~stream;
  378. err = cable->ops->start(dpcm);
  379. }
  380. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  381. loopback_active_notify(dpcm);
  382. break;
  383. case SNDRV_PCM_TRIGGER_STOP:
  384. scoped_guard(spinlock, &cable->lock) {
  385. cable->running &= ~stream;
  386. cable->pause &= ~stream;
  387. err = cable->ops->stop(dpcm);
  388. }
  389. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  390. loopback_active_notify(dpcm);
  391. break;
  392. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  393. case SNDRV_PCM_TRIGGER_SUSPEND:
  394. scoped_guard(spinlock, &cable->lock) {
  395. cable->pause |= stream;
  396. err = cable->ops->stop(dpcm);
  397. }
  398. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  399. loopback_active_notify(dpcm);
  400. break;
  401. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  402. case SNDRV_PCM_TRIGGER_RESUME:
  403. scoped_guard(spinlock, &cable->lock) {
  404. dpcm->last_jiffies = jiffies;
  405. cable->pause &= ~stream;
  406. err = cable->ops->start(dpcm);
  407. }
  408. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  409. loopback_active_notify(dpcm);
  410. break;
  411. default:
  412. return -EINVAL;
  413. }
  414. return err;
  415. }
  416. static void params_change(struct snd_pcm_substream *substream)
  417. {
  418. struct snd_pcm_runtime *runtime = substream->runtime;
  419. struct loopback_pcm *dpcm = runtime->private_data;
  420. struct loopback_cable *cable = dpcm->cable;
  421. cable->hw.formats = pcm_format_to_bits(runtime->format);
  422. cable->hw.rate_min = runtime->rate;
  423. cable->hw.rate_max = runtime->rate;
  424. cable->hw.channels_min = runtime->channels;
  425. cable->hw.channels_max = runtime->channels;
  426. if (cable->snd_timer.instance) {
  427. cable->hw.period_bytes_min =
  428. frames_to_bytes(runtime, runtime->period_size);
  429. cable->hw.period_bytes_max = cable->hw.period_bytes_min;
  430. }
  431. }
  432. static int loopback_prepare(struct snd_pcm_substream *substream)
  433. {
  434. struct snd_pcm_runtime *runtime = substream->runtime;
  435. struct loopback_pcm *dpcm = runtime->private_data;
  436. struct loopback_cable *cable = dpcm->cable;
  437. int err, bps, salign;
  438. if (cable->ops->stop_sync) {
  439. err = cable->ops->stop_sync(dpcm);
  440. if (err < 0)
  441. return err;
  442. }
  443. salign = (snd_pcm_format_physical_width(runtime->format) *
  444. runtime->channels) / 8;
  445. bps = salign * runtime->rate;
  446. if (bps <= 0 || salign <= 0)
  447. return -EINVAL;
  448. dpcm->buf_pos = 0;
  449. dpcm->pcm_buffer_size = frames_to_bytes(runtime, runtime->buffer_size);
  450. dpcm->channel_buf_n = dpcm->pcm_buffer_size / runtime->channels;
  451. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
  452. /* clear capture buffer */
  453. dpcm->silent_size = dpcm->pcm_buffer_size;
  454. snd_pcm_format_set_silence(runtime->format, runtime->dma_area,
  455. runtime->buffer_size * runtime->channels);
  456. }
  457. dpcm->irq_pos = 0;
  458. dpcm->period_update_pending = 0;
  459. dpcm->pcm_bps = bps;
  460. dpcm->pcm_salign = salign;
  461. dpcm->pcm_period_size = frames_to_bytes(runtime, runtime->period_size);
  462. guard(mutex)(&dpcm->loopback->cable_lock);
  463. if (!(cable->valid & ~(1 << substream->stream)) ||
  464. (get_setup(dpcm)->notify &&
  465. substream->stream == SNDRV_PCM_STREAM_PLAYBACK))
  466. params_change(substream);
  467. cable->valid |= 1 << substream->stream;
  468. return 0;
  469. }
  470. static void clear_capture_buf(struct loopback_pcm *dpcm, unsigned int bytes)
  471. {
  472. struct snd_pcm_runtime *runtime = dpcm->substream->runtime;
  473. char *dst = runtime->dma_area;
  474. unsigned int dst_off = dpcm->buf_pos;
  475. if (dpcm->silent_size >= dpcm->pcm_buffer_size)
  476. return;
  477. if (dpcm->silent_size + bytes > dpcm->pcm_buffer_size)
  478. bytes = dpcm->pcm_buffer_size - dpcm->silent_size;
  479. for (;;) {
  480. unsigned int size = bytes;
  481. if (dst_off + size > dpcm->pcm_buffer_size)
  482. size = dpcm->pcm_buffer_size - dst_off;
  483. snd_pcm_format_set_silence(runtime->format, dst + dst_off,
  484. bytes_to_frames(runtime, size) *
  485. runtime->channels);
  486. dpcm->silent_size += size;
  487. bytes -= size;
  488. if (!bytes)
  489. break;
  490. dst_off = 0;
  491. }
  492. }
  493. static void copy_play_buf_part_n(struct loopback_pcm *play, struct loopback_pcm *capt,
  494. unsigned int size, unsigned int src_off, unsigned int dst_off)
  495. {
  496. unsigned int channels = capt->substream->runtime->channels;
  497. unsigned int size_p_ch = size / channels;
  498. unsigned int src_off_ch = src_off / channels;
  499. unsigned int dst_off_ch = dst_off / channels;
  500. int i;
  501. for (i = 0; i < channels; i++) {
  502. memcpy(capt->substream->runtime->dma_area + capt->channel_buf_n * i + dst_off_ch,
  503. play->substream->runtime->dma_area + play->channel_buf_n * i + src_off_ch,
  504. size_p_ch);
  505. }
  506. }
  507. static void copy_play_buf(struct loopback_pcm *play,
  508. struct loopback_pcm *capt,
  509. unsigned int bytes)
  510. {
  511. struct snd_pcm_runtime *runtime = play->substream->runtime;
  512. char *src = runtime->dma_area;
  513. char *dst = capt->substream->runtime->dma_area;
  514. unsigned int src_off = play->buf_pos;
  515. unsigned int dst_off = capt->buf_pos;
  516. unsigned int clear_bytes = 0;
  517. /* check if playback is draining, trim the capture copy size
  518. * when our pointer is at the end of playback ring buffer */
  519. if (runtime->state == SNDRV_PCM_STATE_DRAINING &&
  520. snd_pcm_playback_hw_avail(runtime) < runtime->buffer_size) {
  521. snd_pcm_uframes_t appl_ptr, appl_ptr1, diff;
  522. appl_ptr = appl_ptr1 = runtime->control->appl_ptr;
  523. appl_ptr1 -= appl_ptr1 % runtime->buffer_size;
  524. appl_ptr1 += play->buf_pos / play->pcm_salign;
  525. if (appl_ptr < appl_ptr1)
  526. appl_ptr1 -= runtime->buffer_size;
  527. diff = (appl_ptr - appl_ptr1) * play->pcm_salign;
  528. if (diff < bytes) {
  529. clear_bytes = bytes - diff;
  530. bytes = diff;
  531. }
  532. }
  533. for (;;) {
  534. unsigned int size = bytes;
  535. if (src_off + size > play->pcm_buffer_size)
  536. size = play->pcm_buffer_size - src_off;
  537. if (dst_off + size > capt->pcm_buffer_size)
  538. size = capt->pcm_buffer_size - dst_off;
  539. if (!is_access_interleaved(runtime->access))
  540. copy_play_buf_part_n(play, capt, size, src_off, dst_off);
  541. else
  542. memcpy(dst + dst_off, src + src_off, size);
  543. capt->silent_size = 0;
  544. bytes -= size;
  545. if (!bytes)
  546. break;
  547. src_off = (src_off + size) % play->pcm_buffer_size;
  548. dst_off = (dst_off + size) % capt->pcm_buffer_size;
  549. }
  550. if (clear_bytes > 0) {
  551. clear_capture_buf(capt, clear_bytes);
  552. capt->silent_size = 0;
  553. }
  554. }
  555. static inline unsigned int bytepos_delta(struct loopback_pcm *dpcm,
  556. unsigned int jiffies_delta)
  557. {
  558. unsigned long last_pos;
  559. unsigned int delta;
  560. last_pos = byte_pos(dpcm, dpcm->irq_pos);
  561. dpcm->irq_pos += jiffies_delta * dpcm->pcm_bps;
  562. delta = byte_pos(dpcm, dpcm->irq_pos) - last_pos;
  563. if (delta >= dpcm->last_drift)
  564. delta -= dpcm->last_drift;
  565. dpcm->last_drift = 0;
  566. if (dpcm->irq_pos >= dpcm->period_size_frac) {
  567. dpcm->irq_pos %= dpcm->period_size_frac;
  568. dpcm->period_update_pending = 1;
  569. }
  570. return delta;
  571. }
  572. static inline void bytepos_finish(struct loopback_pcm *dpcm,
  573. unsigned int delta)
  574. {
  575. dpcm->buf_pos += delta;
  576. dpcm->buf_pos %= dpcm->pcm_buffer_size;
  577. }
  578. /* call in cable->lock */
  579. static unsigned int loopback_jiffies_timer_pos_update
  580. (struct loopback_cable *cable)
  581. {
  582. struct loopback_pcm *dpcm_play =
  583. cable->streams[SNDRV_PCM_STREAM_PLAYBACK];
  584. struct loopback_pcm *dpcm_capt =
  585. cable->streams[SNDRV_PCM_STREAM_CAPTURE];
  586. unsigned long delta_play = 0, delta_capt = 0, cur_jiffies;
  587. unsigned int running, count1, count2;
  588. cur_jiffies = jiffies;
  589. running = cable->running ^ cable->pause;
  590. if (running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) {
  591. delta_play = cur_jiffies - dpcm_play->last_jiffies;
  592. dpcm_play->last_jiffies += delta_play;
  593. }
  594. if (running & (1 << SNDRV_PCM_STREAM_CAPTURE)) {
  595. delta_capt = cur_jiffies - dpcm_capt->last_jiffies;
  596. dpcm_capt->last_jiffies += delta_capt;
  597. }
  598. if (delta_play == 0 && delta_capt == 0)
  599. goto unlock;
  600. if (delta_play > delta_capt) {
  601. count1 = bytepos_delta(dpcm_play, delta_play - delta_capt);
  602. bytepos_finish(dpcm_play, count1);
  603. delta_play = delta_capt;
  604. } else if (delta_play < delta_capt) {
  605. count1 = bytepos_delta(dpcm_capt, delta_capt - delta_play);
  606. clear_capture_buf(dpcm_capt, count1);
  607. bytepos_finish(dpcm_capt, count1);
  608. delta_capt = delta_play;
  609. }
  610. if (delta_play == 0 && delta_capt == 0)
  611. goto unlock;
  612. /* note delta_capt == delta_play at this moment */
  613. count1 = bytepos_delta(dpcm_play, delta_play);
  614. count2 = bytepos_delta(dpcm_capt, delta_capt);
  615. if (count1 < count2) {
  616. dpcm_capt->last_drift = count2 - count1;
  617. count1 = count2;
  618. } else if (count1 > count2) {
  619. dpcm_play->last_drift = count1 - count2;
  620. }
  621. copy_play_buf(dpcm_play, dpcm_capt, count1);
  622. bytepos_finish(dpcm_play, count1);
  623. bytepos_finish(dpcm_capt, count1);
  624. unlock:
  625. return running;
  626. }
  627. static void loopback_jiffies_timer_function(struct timer_list *t)
  628. {
  629. struct loopback_pcm *dpcm = timer_container_of(dpcm, t, timer);
  630. bool period_elapsed = false;
  631. scoped_guard(spinlock_irqsave, &dpcm->cable->lock) {
  632. if (loopback_jiffies_timer_pos_update(dpcm->cable) &
  633. (1 << dpcm->substream->stream)) {
  634. loopback_jiffies_timer_start(dpcm);
  635. if (dpcm->period_update_pending) {
  636. dpcm->period_update_pending = 0;
  637. period_elapsed = true;
  638. break;
  639. }
  640. }
  641. }
  642. if (period_elapsed)
  643. snd_pcm_period_elapsed(dpcm->substream);
  644. }
  645. /* call in cable->lock */
  646. static int loopback_snd_timer_check_resolution(struct snd_pcm_runtime *runtime,
  647. unsigned long resolution)
  648. {
  649. if (resolution != runtime->timer_resolution) {
  650. struct loopback_pcm *dpcm = runtime->private_data;
  651. struct loopback_cable *cable = dpcm->cable;
  652. /* Worst case estimation of possible values for resolution
  653. * resolution <= (512 * 1024) frames / 8kHz in nsec
  654. * resolution <= 65.536.000.000 nsec
  655. *
  656. * period_size <= 65.536.000.000 nsec / 1000nsec/usec * 192kHz +
  657. * 500.000
  658. * period_size <= 12.582.912.000.000 <64bit
  659. * / 1.000.000 usec/sec
  660. */
  661. snd_pcm_uframes_t period_size_usec =
  662. resolution / 1000 * runtime->rate;
  663. /* round to nearest sample rate */
  664. snd_pcm_uframes_t period_size =
  665. (period_size_usec + 500 * 1000) / (1000 * 1000);
  666. pcm_err(dpcm->substream->pcm,
  667. "Period size (%lu frames) of loopback device is not corresponding to timer resolution (%lu nsec = %lu frames) of card timer %d,%d,%d. Use period size of %lu frames for loopback device.",
  668. runtime->period_size, resolution, period_size,
  669. cable->snd_timer.id.card,
  670. cable->snd_timer.id.device,
  671. cable->snd_timer.id.subdevice,
  672. period_size);
  673. return -EINVAL;
  674. }
  675. return 0;
  676. }
  677. static void loopback_snd_timer_period_elapsed(struct loopback_cable *cable,
  678. int event,
  679. unsigned long resolution)
  680. {
  681. struct loopback_pcm *dpcm_play, *dpcm_capt;
  682. struct snd_pcm_substream *substream_play, *substream_capt;
  683. struct snd_pcm_runtime *valid_runtime;
  684. unsigned int running, elapsed_bytes;
  685. bool xrun = false;
  686. scoped_guard(spinlock_irqsave, &cable->lock) {
  687. running = cable->running ^ cable->pause;
  688. /* no need to do anything if no stream is running */
  689. if (!running)
  690. return;
  691. dpcm_play = cable->streams[SNDRV_PCM_STREAM_PLAYBACK];
  692. dpcm_capt = cable->streams[SNDRV_PCM_STREAM_CAPTURE];
  693. if (event == SNDRV_TIMER_EVENT_MSTOP) {
  694. if (!dpcm_play ||
  695. dpcm_play->substream->runtime->state !=
  696. SNDRV_PCM_STATE_DRAINING)
  697. return;
  698. }
  699. substream_play = (running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) ?
  700. dpcm_play->substream : NULL;
  701. substream_capt = (running & (1 << SNDRV_PCM_STREAM_CAPTURE)) ?
  702. dpcm_capt->substream : NULL;
  703. valid_runtime = (running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) ?
  704. dpcm_play->substream->runtime :
  705. dpcm_capt->substream->runtime;
  706. /* resolution is only valid for SNDRV_TIMER_EVENT_TICK events */
  707. if (event == SNDRV_TIMER_EVENT_TICK) {
  708. /* The hardware rules guarantee that playback and capture period
  709. * are the same. Therefore only one device has to be checked
  710. * here.
  711. */
  712. if (loopback_snd_timer_check_resolution(valid_runtime,
  713. resolution) < 0) {
  714. xrun = true;
  715. break;
  716. }
  717. }
  718. elapsed_bytes = frames_to_bytes(valid_runtime,
  719. valid_runtime->period_size);
  720. /* The same timer interrupt is used for playback and capture device */
  721. if ((running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) &&
  722. (running & (1 << SNDRV_PCM_STREAM_CAPTURE))) {
  723. copy_play_buf(dpcm_play, dpcm_capt, elapsed_bytes);
  724. bytepos_finish(dpcm_play, elapsed_bytes);
  725. bytepos_finish(dpcm_capt, elapsed_bytes);
  726. } else if (running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) {
  727. bytepos_finish(dpcm_play, elapsed_bytes);
  728. } else if (running & (1 << SNDRV_PCM_STREAM_CAPTURE)) {
  729. clear_capture_buf(dpcm_capt, elapsed_bytes);
  730. bytepos_finish(dpcm_capt, elapsed_bytes);
  731. }
  732. }
  733. if (xrun) {
  734. if (substream_play)
  735. snd_pcm_stop_xrun(substream_play);
  736. if (substream_capt)
  737. snd_pcm_stop_xrun(substream_capt);
  738. return;
  739. }
  740. if (substream_play)
  741. snd_pcm_period_elapsed(substream_play);
  742. if (substream_capt)
  743. snd_pcm_period_elapsed(substream_capt);
  744. }
  745. static void loopback_snd_timer_function(struct snd_timer_instance *timeri,
  746. unsigned long resolution,
  747. unsigned long ticks)
  748. {
  749. struct loopback_cable *cable = timeri->callback_data;
  750. loopback_snd_timer_period_elapsed(cable, SNDRV_TIMER_EVENT_TICK,
  751. resolution);
  752. }
  753. static void loopback_snd_timer_work(struct work_struct *work)
  754. {
  755. struct loopback_cable *cable;
  756. cable = container_of(work, struct loopback_cable, snd_timer.event_work);
  757. loopback_snd_timer_period_elapsed(cable, SNDRV_TIMER_EVENT_MSTOP, 0);
  758. }
  759. static void loopback_snd_timer_event(struct snd_timer_instance *timeri,
  760. int event,
  761. struct timespec64 *tstamp,
  762. unsigned long resolution)
  763. {
  764. /* Do not lock cable->lock here because timer->lock is already hold.
  765. * There are other functions which first lock cable->lock and than
  766. * timer->lock e.g.
  767. * loopback_trigger()
  768. * spin_lock(&cable->lock)
  769. * loopback_snd_timer_start()
  770. * snd_timer_start()
  771. * spin_lock(&timer->lock)
  772. * Therefore when using the oposit order of locks here it could result
  773. * in a deadlock.
  774. */
  775. if (event == SNDRV_TIMER_EVENT_MSTOP) {
  776. struct loopback_cable *cable = timeri->callback_data;
  777. /* sound card of the timer was stopped. Therefore there will not
  778. * be any further timer callbacks. Due to this forward audio
  779. * data from here if in draining state. When still in running
  780. * state the streaming will be aborted by the usual timeout. It
  781. * should not be aborted here because may be the timer sound
  782. * card does only a recovery and the timer is back soon.
  783. * This work triggers loopback_snd_timer_work()
  784. */
  785. schedule_work(&cable->snd_timer.event_work);
  786. }
  787. }
  788. static void loopback_jiffies_timer_dpcm_info(struct loopback_pcm *dpcm,
  789. struct snd_info_buffer *buffer)
  790. {
  791. snd_iprintf(buffer, " update_pending:\t%u\n",
  792. dpcm->period_update_pending);
  793. snd_iprintf(buffer, " irq_pos:\t\t%u\n", dpcm->irq_pos);
  794. snd_iprintf(buffer, " period_frac:\t%u\n", dpcm->period_size_frac);
  795. snd_iprintf(buffer, " last_jiffies:\t%lu (%lu)\n",
  796. dpcm->last_jiffies, jiffies);
  797. snd_iprintf(buffer, " timer_expires:\t%lu\n", dpcm->timer.expires);
  798. }
  799. static void loopback_snd_timer_dpcm_info(struct loopback_pcm *dpcm,
  800. struct snd_info_buffer *buffer)
  801. {
  802. struct loopback_cable *cable = dpcm->cable;
  803. snd_iprintf(buffer, " sound timer:\thw:%d,%d,%d\n",
  804. cable->snd_timer.id.card,
  805. cable->snd_timer.id.device,
  806. cable->snd_timer.id.subdevice);
  807. snd_iprintf(buffer, " timer open:\t\t%s\n",
  808. snd_pcm_direction_name(cable->snd_timer.stream));
  809. }
  810. static snd_pcm_uframes_t loopback_pointer(struct snd_pcm_substream *substream)
  811. {
  812. struct snd_pcm_runtime *runtime = substream->runtime;
  813. struct loopback_pcm *dpcm = runtime->private_data;
  814. snd_pcm_uframes_t pos;
  815. guard(spinlock)(&dpcm->cable->lock);
  816. if (dpcm->cable->ops->pos_update)
  817. dpcm->cable->ops->pos_update(dpcm->cable);
  818. pos = dpcm->buf_pos;
  819. return bytes_to_frames(runtime, pos);
  820. }
  821. static const struct snd_pcm_hardware loopback_pcm_hardware =
  822. {
  823. .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_MMAP |
  824. SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_PAUSE |
  825. SNDRV_PCM_INFO_RESUME | SNDRV_PCM_INFO_NONINTERLEAVED),
  826. .formats = (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE |
  827. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE |
  828. SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE |
  829. SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE |
  830. SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE |
  831. SNDRV_PCM_FMTBIT_DSD_U8 |
  832. SNDRV_PCM_FMTBIT_DSD_U16_LE | SNDRV_PCM_FMTBIT_DSD_U16_BE |
  833. SNDRV_PCM_FMTBIT_DSD_U32_LE | SNDRV_PCM_FMTBIT_DSD_U32_BE),
  834. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_768000,
  835. .rate_min = 8000,
  836. .rate_max = 768000,
  837. .channels_min = 1,
  838. .channels_max = 32,
  839. .buffer_bytes_max = 2 * 1024 * 1024,
  840. .period_bytes_min = 64,
  841. /* note check overflow in frac_pos() using pcm_rate_shift before
  842. changing period_bytes_max value */
  843. .period_bytes_max = 1024 * 1024,
  844. .periods_min = 1,
  845. .periods_max = 1024,
  846. .fifo_size = 0,
  847. };
  848. static void loopback_runtime_free(struct snd_pcm_runtime *runtime)
  849. {
  850. struct loopback_pcm *dpcm = runtime->private_data;
  851. kfree(dpcm);
  852. }
  853. static int loopback_hw_free(struct snd_pcm_substream *substream)
  854. {
  855. struct snd_pcm_runtime *runtime = substream->runtime;
  856. struct loopback_pcm *dpcm = runtime->private_data;
  857. struct loopback_cable *cable = dpcm->cable;
  858. guard(mutex)(&dpcm->loopback->cable_lock);
  859. cable->valid &= ~(1 << substream->stream);
  860. return 0;
  861. }
  862. static unsigned int get_cable_index(struct snd_pcm_substream *substream)
  863. {
  864. if (!substream->pcm->device)
  865. return substream->stream;
  866. else
  867. return !substream->stream;
  868. }
  869. static int rule_format(struct snd_pcm_hw_params *params,
  870. struct snd_pcm_hw_rule *rule)
  871. {
  872. struct loopback_pcm *dpcm = rule->private;
  873. struct loopback_cable *cable = dpcm->cable;
  874. struct snd_mask m;
  875. snd_mask_none(&m);
  876. scoped_guard(mutex, &dpcm->loopback->cable_lock) {
  877. m.bits[0] = (u_int32_t)cable->hw.formats;
  878. m.bits[1] = (u_int32_t)(cable->hw.formats >> 32);
  879. }
  880. return snd_mask_refine(hw_param_mask(params, rule->var), &m);
  881. }
  882. static int rule_rate(struct snd_pcm_hw_params *params,
  883. struct snd_pcm_hw_rule *rule)
  884. {
  885. struct loopback_pcm *dpcm = rule->private;
  886. struct loopback_cable *cable = dpcm->cable;
  887. struct snd_interval t;
  888. scoped_guard(mutex, &dpcm->loopback->cable_lock) {
  889. t.min = cable->hw.rate_min;
  890. t.max = cable->hw.rate_max;
  891. }
  892. t.openmin = t.openmax = 0;
  893. t.integer = 0;
  894. return snd_interval_refine(hw_param_interval(params, rule->var), &t);
  895. }
  896. static int rule_channels(struct snd_pcm_hw_params *params,
  897. struct snd_pcm_hw_rule *rule)
  898. {
  899. struct loopback_pcm *dpcm = rule->private;
  900. struct loopback_cable *cable = dpcm->cable;
  901. struct snd_interval t;
  902. scoped_guard(mutex, &dpcm->loopback->cable_lock) {
  903. t.min = cable->hw.channels_min;
  904. t.max = cable->hw.channels_max;
  905. }
  906. t.openmin = t.openmax = 0;
  907. t.integer = 0;
  908. return snd_interval_refine(hw_param_interval(params, rule->var), &t);
  909. }
  910. static int rule_period_bytes(struct snd_pcm_hw_params *params,
  911. struct snd_pcm_hw_rule *rule)
  912. {
  913. struct loopback_pcm *dpcm = rule->private;
  914. struct loopback_cable *cable = dpcm->cable;
  915. struct snd_interval t;
  916. scoped_guard(mutex, &dpcm->loopback->cable_lock) {
  917. t.min = cable->hw.period_bytes_min;
  918. t.max = cable->hw.period_bytes_max;
  919. }
  920. t.openmin = 0;
  921. t.openmax = 0;
  922. t.integer = 0;
  923. return snd_interval_refine(hw_param_interval(params, rule->var), &t);
  924. }
  925. static void free_cable(struct snd_pcm_substream *substream)
  926. {
  927. struct loopback *loopback = substream->private_data;
  928. int dev = get_cable_index(substream);
  929. struct loopback_cable *cable;
  930. cable = loopback->cables[substream->number][dev];
  931. if (!cable)
  932. return;
  933. if (cable->streams[!substream->stream]) {
  934. /* other stream is still alive */
  935. guard(spinlock_irq)(&cable->lock);
  936. cable->streams[substream->stream] = NULL;
  937. } else {
  938. struct loopback_pcm *dpcm = substream->runtime->private_data;
  939. if (cable->ops && cable->ops->close_cable && dpcm)
  940. cable->ops->close_cable(dpcm);
  941. /* free the cable */
  942. loopback->cables[substream->number][dev] = NULL;
  943. kfree(cable);
  944. }
  945. }
  946. static int loopback_jiffies_timer_open(struct loopback_pcm *dpcm)
  947. {
  948. timer_setup(&dpcm->timer, loopback_jiffies_timer_function, 0);
  949. return 0;
  950. }
  951. static const struct loopback_ops loopback_jiffies_timer_ops = {
  952. .open = loopback_jiffies_timer_open,
  953. .start = loopback_jiffies_timer_start,
  954. .stop = loopback_jiffies_timer_stop,
  955. .stop_sync = loopback_jiffies_timer_stop_sync,
  956. .close_substream = loopback_jiffies_timer_stop_sync,
  957. .pos_update = loopback_jiffies_timer_pos_update,
  958. .dpcm_info = loopback_jiffies_timer_dpcm_info,
  959. };
  960. static int loopback_parse_timer_id(const char *str,
  961. struct snd_timer_id *tid)
  962. {
  963. /* [<pref>:](<card name>|<card idx>)[{.,}<dev idx>[{.,}<subdev idx>]] */
  964. const char * const sep_dev = ".,";
  965. const char * const sep_pref = ":";
  966. const char *name = str;
  967. char *sep, save = '\0';
  968. int card_idx = 0, dev = 0, subdev = 0;
  969. int err;
  970. sep = strpbrk(str, sep_pref);
  971. if (sep)
  972. name = sep + 1;
  973. sep = strpbrk(name, sep_dev);
  974. if (sep) {
  975. save = *sep;
  976. *sep = '\0';
  977. }
  978. err = kstrtoint(name, 0, &card_idx);
  979. if (err == -EINVAL) {
  980. /* Must be the name, not number */
  981. for (card_idx = 0; card_idx < snd_ecards_limit; card_idx++) {
  982. struct snd_card *card = snd_card_ref(card_idx);
  983. if (card) {
  984. if (!strcmp(card->id, name))
  985. err = 0;
  986. snd_card_unref(card);
  987. }
  988. if (!err)
  989. break;
  990. }
  991. }
  992. if (sep) {
  993. *sep = save;
  994. if (!err) {
  995. char *sep2, save2 = '\0';
  996. sep2 = strpbrk(sep + 1, sep_dev);
  997. if (sep2) {
  998. save2 = *sep2;
  999. *sep2 = '\0';
  1000. }
  1001. err = kstrtoint(sep + 1, 0, &dev);
  1002. if (sep2) {
  1003. *sep2 = save2;
  1004. if (!err)
  1005. err = kstrtoint(sep2 + 1, 0, &subdev);
  1006. }
  1007. }
  1008. }
  1009. if (card_idx == -1)
  1010. tid->dev_class = SNDRV_TIMER_CLASS_GLOBAL;
  1011. if (!err && tid) {
  1012. tid->card = card_idx;
  1013. tid->device = dev;
  1014. tid->subdevice = subdev;
  1015. }
  1016. return err;
  1017. }
  1018. /* call in loopback->cable_lock */
  1019. static int loopback_snd_timer_open(struct loopback_pcm *dpcm)
  1020. {
  1021. int err = 0;
  1022. struct snd_timer_id tid = {
  1023. .dev_class = SNDRV_TIMER_CLASS_PCM,
  1024. .dev_sclass = SNDRV_TIMER_SCLASS_APPLICATION,
  1025. };
  1026. struct snd_timer_instance *timeri;
  1027. struct loopback_cable *cable = dpcm->cable;
  1028. /* check if timer was already opened. It is only opened once
  1029. * per playback and capture subdevice (aka cable).
  1030. */
  1031. if (cable->snd_timer.instance)
  1032. goto exit;
  1033. err = loopback_parse_timer_id(dpcm->loopback->timer_source, &tid);
  1034. if (err < 0) {
  1035. pcm_err(dpcm->substream->pcm,
  1036. "Parsing timer source \'%s\' failed with %d",
  1037. dpcm->loopback->timer_source, err);
  1038. goto exit;
  1039. }
  1040. cable->snd_timer.stream = dpcm->substream->stream;
  1041. cable->snd_timer.id = tid;
  1042. timeri = snd_timer_instance_new(dpcm->loopback->card->id);
  1043. if (!timeri) {
  1044. err = -ENOMEM;
  1045. goto exit;
  1046. }
  1047. /* The callback has to be called from another work. If
  1048. * SNDRV_TIMER_IFLG_FAST is specified it will be called from the
  1049. * snd_pcm_period_elapsed() call of the selected sound card.
  1050. * snd_pcm_period_elapsed() helds snd_pcm_stream_lock_irqsave().
  1051. * Due to our callback loopback_snd_timer_function() also calls
  1052. * snd_pcm_period_elapsed() which calls snd_pcm_stream_lock_irqsave().
  1053. * This would end up in a dead lock.
  1054. */
  1055. timeri->flags |= SNDRV_TIMER_IFLG_AUTO;
  1056. timeri->callback = loopback_snd_timer_function;
  1057. timeri->callback_data = (void *)cable;
  1058. timeri->ccallback = loopback_snd_timer_event;
  1059. /* initialise a work used for draining */
  1060. INIT_WORK(&cable->snd_timer.event_work, loopback_snd_timer_work);
  1061. /* The mutex loopback->cable_lock is kept locked.
  1062. * Therefore snd_timer_open() cannot be called a second time
  1063. * by the other device of the same cable.
  1064. * Therefore the following issue cannot happen:
  1065. * [proc1] Call loopback_timer_open() ->
  1066. * Unlock cable->lock for snd_timer_close/open() call
  1067. * [proc2] Call loopback_timer_open() -> snd_timer_open(),
  1068. * snd_timer_start()
  1069. * [proc1] Call snd_timer_open() and overwrite running timer
  1070. * instance
  1071. */
  1072. err = snd_timer_open(timeri, &cable->snd_timer.id, current->pid);
  1073. if (err < 0) {
  1074. pcm_err(dpcm->substream->pcm,
  1075. "snd_timer_open (%d,%d,%d) failed with %d",
  1076. cable->snd_timer.id.card,
  1077. cable->snd_timer.id.device,
  1078. cable->snd_timer.id.subdevice,
  1079. err);
  1080. snd_timer_instance_free(timeri);
  1081. goto exit;
  1082. }
  1083. cable->snd_timer.instance = timeri;
  1084. exit:
  1085. return err;
  1086. }
  1087. /* stop_sync() is not required for sound timer because it does not need to be
  1088. * restarted in loopback_prepare() on Xrun recovery
  1089. */
  1090. static const struct loopback_ops loopback_snd_timer_ops = {
  1091. .open = loopback_snd_timer_open,
  1092. .start = loopback_snd_timer_start,
  1093. .stop = loopback_snd_timer_stop,
  1094. .close_cable = loopback_snd_timer_close_cable,
  1095. .dpcm_info = loopback_snd_timer_dpcm_info,
  1096. };
  1097. static int loopback_open(struct snd_pcm_substream *substream)
  1098. {
  1099. struct snd_pcm_runtime *runtime = substream->runtime;
  1100. struct loopback *loopback = substream->private_data;
  1101. struct loopback_pcm *dpcm;
  1102. struct loopback_cable *cable = NULL;
  1103. int err = 0;
  1104. int dev = get_cable_index(substream);
  1105. guard(mutex)(&loopback->cable_lock);
  1106. dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
  1107. if (!dpcm)
  1108. return -ENOMEM;
  1109. dpcm->loopback = loopback;
  1110. dpcm->substream = substream;
  1111. cable = loopback->cables[substream->number][dev];
  1112. if (!cable) {
  1113. cable = kzalloc(sizeof(*cable), GFP_KERNEL);
  1114. if (!cable) {
  1115. err = -ENOMEM;
  1116. goto unlock;
  1117. }
  1118. spin_lock_init(&cable->lock);
  1119. cable->hw = loopback_pcm_hardware;
  1120. if (loopback->timer_source)
  1121. cable->ops = &loopback_snd_timer_ops;
  1122. else
  1123. cable->ops = &loopback_jiffies_timer_ops;
  1124. loopback->cables[substream->number][dev] = cable;
  1125. }
  1126. dpcm->cable = cable;
  1127. runtime->private_data = dpcm;
  1128. if (cable->ops->open) {
  1129. err = cable->ops->open(dpcm);
  1130. if (err < 0)
  1131. goto unlock;
  1132. }
  1133. snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
  1134. /* use dynamic rules based on actual runtime->hw values */
  1135. /* note that the default rules created in the PCM midlevel code */
  1136. /* are cached -> they do not reflect the actual state */
  1137. err = snd_pcm_hw_rule_add(runtime, 0,
  1138. SNDRV_PCM_HW_PARAM_FORMAT,
  1139. rule_format, dpcm,
  1140. SNDRV_PCM_HW_PARAM_FORMAT, -1);
  1141. if (err < 0)
  1142. goto unlock;
  1143. err = snd_pcm_hw_rule_add(runtime, 0,
  1144. SNDRV_PCM_HW_PARAM_RATE,
  1145. rule_rate, dpcm,
  1146. SNDRV_PCM_HW_PARAM_RATE, -1);
  1147. if (err < 0)
  1148. goto unlock;
  1149. err = snd_pcm_hw_rule_add(runtime, 0,
  1150. SNDRV_PCM_HW_PARAM_CHANNELS,
  1151. rule_channels, dpcm,
  1152. SNDRV_PCM_HW_PARAM_CHANNELS, -1);
  1153. if (err < 0)
  1154. goto unlock;
  1155. /* In case of sound timer the period time of both devices of the same
  1156. * loop has to be the same.
  1157. * This rule only takes effect if a sound timer was chosen
  1158. */
  1159. if (cable->snd_timer.instance) {
  1160. err = snd_pcm_hw_rule_add(runtime, 0,
  1161. SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
  1162. rule_period_bytes, dpcm,
  1163. SNDRV_PCM_HW_PARAM_PERIOD_BYTES, -1);
  1164. if (err < 0)
  1165. goto unlock;
  1166. }
  1167. /* loopback_runtime_free() has not to be called if kfree(dpcm) was
  1168. * already called here. Otherwise it will end up with a double free.
  1169. */
  1170. runtime->private_free = loopback_runtime_free;
  1171. if (get_notify(dpcm))
  1172. runtime->hw = loopback_pcm_hardware;
  1173. else
  1174. runtime->hw = cable->hw;
  1175. scoped_guard(spinlock_irq, &cable->lock) {
  1176. cable->streams[substream->stream] = dpcm;
  1177. }
  1178. unlock:
  1179. if (err < 0) {
  1180. free_cable(substream);
  1181. kfree(dpcm);
  1182. }
  1183. return err;
  1184. }
  1185. static int loopback_close(struct snd_pcm_substream *substream)
  1186. {
  1187. struct loopback *loopback = substream->private_data;
  1188. struct loopback_pcm *dpcm = substream->runtime->private_data;
  1189. int err = 0;
  1190. if (dpcm->cable->ops->close_substream)
  1191. err = dpcm->cable->ops->close_substream(dpcm);
  1192. guard(mutex)(&loopback->cable_lock);
  1193. free_cable(substream);
  1194. return err;
  1195. }
  1196. static const struct snd_pcm_ops loopback_pcm_ops = {
  1197. .open = loopback_open,
  1198. .close = loopback_close,
  1199. .hw_free = loopback_hw_free,
  1200. .prepare = loopback_prepare,
  1201. .trigger = loopback_trigger,
  1202. .pointer = loopback_pointer,
  1203. };
  1204. static int loopback_pcm_new(struct loopback *loopback,
  1205. int device, int substreams)
  1206. {
  1207. struct snd_pcm *pcm;
  1208. int err;
  1209. err = snd_pcm_new(loopback->card, "Loopback PCM", device,
  1210. substreams, substreams, &pcm);
  1211. if (err < 0)
  1212. return err;
  1213. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &loopback_pcm_ops);
  1214. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &loopback_pcm_ops);
  1215. snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_VMALLOC, NULL, 0, 0);
  1216. pcm->private_data = loopback;
  1217. pcm->info_flags = 0;
  1218. strscpy(pcm->name, "Loopback PCM");
  1219. loopback->pcm[device] = pcm;
  1220. return 0;
  1221. }
  1222. static int loopback_rate_shift_info(struct snd_kcontrol *kcontrol,
  1223. struct snd_ctl_elem_info *uinfo)
  1224. {
  1225. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1226. uinfo->count = 1;
  1227. uinfo->value.integer.min = 80000;
  1228. uinfo->value.integer.max = 120000;
  1229. uinfo->value.integer.step = 1;
  1230. return 0;
  1231. }
  1232. static int loopback_rate_shift_get(struct snd_kcontrol *kcontrol,
  1233. struct snd_ctl_elem_value *ucontrol)
  1234. {
  1235. struct loopback *loopback = snd_kcontrol_chip(kcontrol);
  1236. guard(mutex)(&loopback->cable_lock);
  1237. ucontrol->value.integer.value[0] =
  1238. loopback->setup[kcontrol->id.subdevice]
  1239. [kcontrol->id.device].rate_shift;
  1240. return 0;
  1241. }
  1242. static int loopback_rate_shift_put(struct snd_kcontrol *kcontrol,
  1243. struct snd_ctl_elem_value *ucontrol)
  1244. {
  1245. struct loopback *loopback = snd_kcontrol_chip(kcontrol);
  1246. unsigned int val;
  1247. int change = 0;
  1248. val = ucontrol->value.integer.value[0];
  1249. if (val < 80000)
  1250. val = 80000;
  1251. if (val > 120000)
  1252. val = 120000;
  1253. guard(mutex)(&loopback->cable_lock);
  1254. if (val != loopback->setup[kcontrol->id.subdevice]
  1255. [kcontrol->id.device].rate_shift) {
  1256. loopback->setup[kcontrol->id.subdevice]
  1257. [kcontrol->id.device].rate_shift = val;
  1258. change = 1;
  1259. }
  1260. return change;
  1261. }
  1262. static int loopback_notify_get(struct snd_kcontrol *kcontrol,
  1263. struct snd_ctl_elem_value *ucontrol)
  1264. {
  1265. struct loopback *loopback = snd_kcontrol_chip(kcontrol);
  1266. guard(mutex)(&loopback->cable_lock);
  1267. ucontrol->value.integer.value[0] =
  1268. loopback->setup[kcontrol->id.subdevice]
  1269. [kcontrol->id.device].notify;
  1270. return 0;
  1271. }
  1272. static int loopback_notify_put(struct snd_kcontrol *kcontrol,
  1273. struct snd_ctl_elem_value *ucontrol)
  1274. {
  1275. struct loopback *loopback = snd_kcontrol_chip(kcontrol);
  1276. unsigned int val;
  1277. int change = 0;
  1278. val = ucontrol->value.integer.value[0] ? 1 : 0;
  1279. guard(mutex)(&loopback->cable_lock);
  1280. if (val != loopback->setup[kcontrol->id.subdevice]
  1281. [kcontrol->id.device].notify) {
  1282. loopback->setup[kcontrol->id.subdevice]
  1283. [kcontrol->id.device].notify = val;
  1284. change = 1;
  1285. }
  1286. return change;
  1287. }
  1288. static int loopback_active_get(struct snd_kcontrol *kcontrol,
  1289. struct snd_ctl_elem_value *ucontrol)
  1290. {
  1291. struct loopback *loopback = snd_kcontrol_chip(kcontrol);
  1292. struct loopback_cable *cable;
  1293. unsigned int val = 0;
  1294. guard(mutex)(&loopback->cable_lock);
  1295. cable = loopback->cables[kcontrol->id.subdevice][kcontrol->id.device ^ 1];
  1296. if (cable != NULL) {
  1297. unsigned int running = cable->running ^ cable->pause;
  1298. val = (running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) ? 1 : 0;
  1299. }
  1300. ucontrol->value.integer.value[0] = val;
  1301. return 0;
  1302. }
  1303. static int loopback_format_info(struct snd_kcontrol *kcontrol,
  1304. struct snd_ctl_elem_info *uinfo)
  1305. {
  1306. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1307. uinfo->count = 1;
  1308. uinfo->value.integer.min = 0;
  1309. uinfo->value.integer.max = (__force int)SNDRV_PCM_FORMAT_LAST;
  1310. uinfo->value.integer.step = 1;
  1311. return 0;
  1312. }
  1313. static int loopback_format_get(struct snd_kcontrol *kcontrol,
  1314. struct snd_ctl_elem_value *ucontrol)
  1315. {
  1316. struct loopback *loopback = snd_kcontrol_chip(kcontrol);
  1317. ucontrol->value.integer.value[0] =
  1318. (__force int)loopback->setup[kcontrol->id.subdevice]
  1319. [kcontrol->id.device].format;
  1320. return 0;
  1321. }
  1322. static int loopback_rate_info(struct snd_kcontrol *kcontrol,
  1323. struct snd_ctl_elem_info *uinfo)
  1324. {
  1325. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1326. uinfo->count = 1;
  1327. uinfo->value.integer.min = 0;
  1328. uinfo->value.integer.max = 192000;
  1329. uinfo->value.integer.step = 1;
  1330. return 0;
  1331. }
  1332. static int loopback_rate_get(struct snd_kcontrol *kcontrol,
  1333. struct snd_ctl_elem_value *ucontrol)
  1334. {
  1335. struct loopback *loopback = snd_kcontrol_chip(kcontrol);
  1336. guard(mutex)(&loopback->cable_lock);
  1337. ucontrol->value.integer.value[0] =
  1338. loopback->setup[kcontrol->id.subdevice]
  1339. [kcontrol->id.device].rate;
  1340. return 0;
  1341. }
  1342. static int loopback_channels_info(struct snd_kcontrol *kcontrol,
  1343. struct snd_ctl_elem_info *uinfo)
  1344. {
  1345. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1346. uinfo->count = 1;
  1347. uinfo->value.integer.min = 1;
  1348. uinfo->value.integer.max = 1024;
  1349. uinfo->value.integer.step = 1;
  1350. return 0;
  1351. }
  1352. static int loopback_channels_get(struct snd_kcontrol *kcontrol,
  1353. struct snd_ctl_elem_value *ucontrol)
  1354. {
  1355. struct loopback *loopback = snd_kcontrol_chip(kcontrol);
  1356. guard(mutex)(&loopback->cable_lock);
  1357. ucontrol->value.integer.value[0] =
  1358. loopback->setup[kcontrol->id.subdevice]
  1359. [kcontrol->id.device].channels;
  1360. return 0;
  1361. }
  1362. static int loopback_access_info(struct snd_kcontrol *kcontrol,
  1363. struct snd_ctl_elem_info *uinfo)
  1364. {
  1365. const char * const texts[] = {"Interleaved", "Non-interleaved"};
  1366. return snd_ctl_enum_info(uinfo, 1, ARRAY_SIZE(texts), texts);
  1367. }
  1368. static int loopback_access_get(struct snd_kcontrol *kcontrol,
  1369. struct snd_ctl_elem_value *ucontrol)
  1370. {
  1371. struct loopback *loopback = snd_kcontrol_chip(kcontrol);
  1372. snd_pcm_access_t access;
  1373. guard(mutex)(&loopback->cable_lock);
  1374. access = loopback->setup[kcontrol->id.subdevice][kcontrol->id.device].access;
  1375. ucontrol->value.enumerated.item[0] = !is_access_interleaved(access);
  1376. return 0;
  1377. }
  1378. static const struct snd_kcontrol_new loopback_controls[] = {
  1379. {
  1380. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  1381. .name = "PCM Rate Shift 100000",
  1382. .info = loopback_rate_shift_info,
  1383. .get = loopback_rate_shift_get,
  1384. .put = loopback_rate_shift_put,
  1385. },
  1386. {
  1387. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  1388. .name = "PCM Notify",
  1389. .info = snd_ctl_boolean_mono_info,
  1390. .get = loopback_notify_get,
  1391. .put = loopback_notify_put,
  1392. },
  1393. #define ACTIVE_IDX 2
  1394. {
  1395. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  1396. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  1397. .name = "PCM Slave Active",
  1398. .info = snd_ctl_boolean_mono_info,
  1399. .get = loopback_active_get,
  1400. },
  1401. #define FORMAT_IDX 3
  1402. {
  1403. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  1404. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  1405. .name = "PCM Slave Format",
  1406. .info = loopback_format_info,
  1407. .get = loopback_format_get
  1408. },
  1409. #define RATE_IDX 4
  1410. {
  1411. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  1412. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  1413. .name = "PCM Slave Rate",
  1414. .info = loopback_rate_info,
  1415. .get = loopback_rate_get
  1416. },
  1417. #define CHANNELS_IDX 5
  1418. {
  1419. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  1420. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  1421. .name = "PCM Slave Channels",
  1422. .info = loopback_channels_info,
  1423. .get = loopback_channels_get
  1424. },
  1425. #define ACCESS_IDX 6
  1426. {
  1427. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  1428. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  1429. .name = "PCM Slave Access Mode",
  1430. .info = loopback_access_info,
  1431. .get = loopback_access_get,
  1432. },
  1433. };
  1434. static int loopback_mixer_new(struct loopback *loopback, int notify)
  1435. {
  1436. struct snd_card *card = loopback->card;
  1437. struct snd_pcm *pcm;
  1438. struct snd_kcontrol *kctl;
  1439. struct loopback_setup *setup;
  1440. int err, dev, substr, substr_count, idx;
  1441. strscpy(card->mixername, "Loopback Mixer");
  1442. for (dev = 0; dev < 2; dev++) {
  1443. pcm = loopback->pcm[dev];
  1444. substr_count =
  1445. pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count;
  1446. for (substr = 0; substr < substr_count; substr++) {
  1447. setup = &loopback->setup[substr][dev];
  1448. setup->notify = notify;
  1449. setup->rate_shift = NO_PITCH;
  1450. setup->format = SNDRV_PCM_FORMAT_S16_LE;
  1451. setup->access = SNDRV_PCM_ACCESS_RW_INTERLEAVED;
  1452. setup->rate = 48000;
  1453. setup->channels = 2;
  1454. for (idx = 0; idx < ARRAY_SIZE(loopback_controls);
  1455. idx++) {
  1456. kctl = snd_ctl_new1(&loopback_controls[idx],
  1457. loopback);
  1458. if (!kctl)
  1459. return -ENOMEM;
  1460. kctl->id.device = dev;
  1461. kctl->id.subdevice = substr;
  1462. /* Add the control before copying the id so that
  1463. * the numid field of the id is set in the copy.
  1464. */
  1465. err = snd_ctl_add(card, kctl);
  1466. if (err < 0)
  1467. return err;
  1468. switch (idx) {
  1469. case ACTIVE_IDX:
  1470. setup->active_id = kctl->id;
  1471. break;
  1472. case FORMAT_IDX:
  1473. setup->format_id = kctl->id;
  1474. break;
  1475. case RATE_IDX:
  1476. setup->rate_id = kctl->id;
  1477. break;
  1478. case CHANNELS_IDX:
  1479. setup->channels_id = kctl->id;
  1480. break;
  1481. case ACCESS_IDX:
  1482. setup->access_id = kctl->id;
  1483. break;
  1484. default:
  1485. break;
  1486. }
  1487. }
  1488. }
  1489. }
  1490. return 0;
  1491. }
  1492. static void print_dpcm_info(struct snd_info_buffer *buffer,
  1493. struct loopback_pcm *dpcm,
  1494. const char *id)
  1495. {
  1496. snd_iprintf(buffer, " %s\n", id);
  1497. if (dpcm == NULL) {
  1498. snd_iprintf(buffer, " inactive\n");
  1499. return;
  1500. }
  1501. snd_iprintf(buffer, " buffer_size:\t%u\n", dpcm->pcm_buffer_size);
  1502. snd_iprintf(buffer, " buffer_pos:\t\t%u\n", dpcm->buf_pos);
  1503. snd_iprintf(buffer, " silent_size:\t%u\n", dpcm->silent_size);
  1504. snd_iprintf(buffer, " period_size:\t%u\n", dpcm->pcm_period_size);
  1505. snd_iprintf(buffer, " bytes_per_sec:\t%u\n", dpcm->pcm_bps);
  1506. snd_iprintf(buffer, " sample_align:\t%u\n", dpcm->pcm_salign);
  1507. snd_iprintf(buffer, " rate_shift:\t\t%u\n", dpcm->pcm_rate_shift);
  1508. if (dpcm->cable->ops->dpcm_info)
  1509. dpcm->cable->ops->dpcm_info(dpcm, buffer);
  1510. }
  1511. static void print_substream_info(struct snd_info_buffer *buffer,
  1512. struct loopback *loopback,
  1513. int sub,
  1514. int num)
  1515. {
  1516. struct loopback_cable *cable = loopback->cables[sub][num];
  1517. snd_iprintf(buffer, "Cable %i substream %i:\n", num, sub);
  1518. if (cable == NULL) {
  1519. snd_iprintf(buffer, " inactive\n");
  1520. return;
  1521. }
  1522. snd_iprintf(buffer, " valid: %u\n", cable->valid);
  1523. snd_iprintf(buffer, " running: %u\n", cable->running);
  1524. snd_iprintf(buffer, " pause: %u\n", cable->pause);
  1525. print_dpcm_info(buffer, cable->streams[0], "Playback");
  1526. print_dpcm_info(buffer, cable->streams[1], "Capture");
  1527. }
  1528. static void print_cable_info(struct snd_info_entry *entry,
  1529. struct snd_info_buffer *buffer)
  1530. {
  1531. struct loopback *loopback = entry->private_data;
  1532. int sub, num;
  1533. guard(mutex)(&loopback->cable_lock);
  1534. num = entry->name[strlen(entry->name)-1];
  1535. num = num == '0' ? 0 : 1;
  1536. for (sub = 0; sub < MAX_PCM_SUBSTREAMS; sub++)
  1537. print_substream_info(buffer, loopback, sub, num);
  1538. }
  1539. static int loopback_cable_proc_new(struct loopback *loopback, int cidx)
  1540. {
  1541. char name[32];
  1542. snprintf(name, sizeof(name), "cable#%d", cidx);
  1543. return snd_card_ro_proc_new(loopback->card, name, loopback,
  1544. print_cable_info);
  1545. }
  1546. static void loopback_set_timer_source(struct loopback *loopback,
  1547. const char *value)
  1548. {
  1549. if (loopback->timer_source) {
  1550. devm_kfree(loopback->card->dev, loopback->timer_source);
  1551. loopback->timer_source = NULL;
  1552. }
  1553. if (value && *value)
  1554. loopback->timer_source = devm_kstrdup(loopback->card->dev,
  1555. value, GFP_KERNEL);
  1556. }
  1557. static void print_timer_source_info(struct snd_info_entry *entry,
  1558. struct snd_info_buffer *buffer)
  1559. {
  1560. struct loopback *loopback = entry->private_data;
  1561. guard(mutex)(&loopback->cable_lock);
  1562. snd_iprintf(buffer, "%s\n",
  1563. loopback->timer_source ? loopback->timer_source : "");
  1564. }
  1565. static void change_timer_source_info(struct snd_info_entry *entry,
  1566. struct snd_info_buffer *buffer)
  1567. {
  1568. struct loopback *loopback = entry->private_data;
  1569. char line[64];
  1570. guard(mutex)(&loopback->cable_lock);
  1571. if (!snd_info_get_line(buffer, line, sizeof(line)))
  1572. loopback_set_timer_source(loopback, strim(line));
  1573. }
  1574. static int loopback_timer_source_proc_new(struct loopback *loopback)
  1575. {
  1576. return snd_card_rw_proc_new(loopback->card, "timer_source", loopback,
  1577. print_timer_source_info,
  1578. change_timer_source_info);
  1579. }
  1580. static int loopback_probe(struct platform_device *devptr)
  1581. {
  1582. struct snd_card *card;
  1583. struct loopback *loopback;
  1584. int dev = devptr->id;
  1585. int err;
  1586. err = snd_devm_card_new(&devptr->dev, index[dev], id[dev], THIS_MODULE,
  1587. sizeof(struct loopback), &card);
  1588. if (err < 0)
  1589. return err;
  1590. loopback = card->private_data;
  1591. if (pcm_substreams[dev] < 1)
  1592. pcm_substreams[dev] = 1;
  1593. if (pcm_substreams[dev] > MAX_PCM_SUBSTREAMS)
  1594. pcm_substreams[dev] = MAX_PCM_SUBSTREAMS;
  1595. loopback->card = card;
  1596. loopback_set_timer_source(loopback, timer_source[dev]);
  1597. mutex_init(&loopback->cable_lock);
  1598. err = loopback_pcm_new(loopback, 0, pcm_substreams[dev]);
  1599. if (err < 0)
  1600. return err;
  1601. err = loopback_pcm_new(loopback, 1, pcm_substreams[dev]);
  1602. if (err < 0)
  1603. return err;
  1604. err = loopback_mixer_new(loopback, pcm_notify[dev] ? 1 : 0);
  1605. if (err < 0)
  1606. return err;
  1607. loopback_cable_proc_new(loopback, 0);
  1608. loopback_cable_proc_new(loopback, 1);
  1609. loopback_timer_source_proc_new(loopback);
  1610. strscpy(card->driver, "Loopback");
  1611. strscpy(card->shortname, "Loopback");
  1612. sprintf(card->longname, "Loopback %i", dev + 1);
  1613. err = snd_card_register(card);
  1614. if (err < 0)
  1615. return err;
  1616. platform_set_drvdata(devptr, card);
  1617. return 0;
  1618. }
  1619. static int loopback_suspend(struct device *pdev)
  1620. {
  1621. struct snd_card *card = dev_get_drvdata(pdev);
  1622. snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
  1623. return 0;
  1624. }
  1625. static int loopback_resume(struct device *pdev)
  1626. {
  1627. struct snd_card *card = dev_get_drvdata(pdev);
  1628. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  1629. return 0;
  1630. }
  1631. static DEFINE_SIMPLE_DEV_PM_OPS(loopback_pm, loopback_suspend, loopback_resume);
  1632. #define SND_LOOPBACK_DRIVER "snd_aloop"
  1633. static struct platform_driver loopback_driver = {
  1634. .probe = loopback_probe,
  1635. .driver = {
  1636. .name = SND_LOOPBACK_DRIVER,
  1637. .pm = &loopback_pm,
  1638. },
  1639. };
  1640. static void loopback_unregister_all(void)
  1641. {
  1642. int i;
  1643. for (i = 0; i < ARRAY_SIZE(devices); ++i)
  1644. platform_device_unregister(devices[i]);
  1645. platform_driver_unregister(&loopback_driver);
  1646. }
  1647. static int __init alsa_card_loopback_init(void)
  1648. {
  1649. int i, err, cards;
  1650. err = platform_driver_register(&loopback_driver);
  1651. if (err < 0)
  1652. return err;
  1653. cards = 0;
  1654. for (i = 0; i < SNDRV_CARDS; i++) {
  1655. struct platform_device *device;
  1656. if (!enable[i])
  1657. continue;
  1658. device = platform_device_register_simple(SND_LOOPBACK_DRIVER,
  1659. i, NULL, 0);
  1660. if (IS_ERR(device))
  1661. continue;
  1662. if (!platform_get_drvdata(device)) {
  1663. platform_device_unregister(device);
  1664. continue;
  1665. }
  1666. devices[i] = device;
  1667. cards++;
  1668. }
  1669. if (!cards) {
  1670. #ifdef MODULE
  1671. pr_err("aloop: No loopback enabled\n");
  1672. #endif
  1673. loopback_unregister_all();
  1674. return -ENODEV;
  1675. }
  1676. return 0;
  1677. }
  1678. static void __exit alsa_card_loopback_exit(void)
  1679. {
  1680. loopback_unregister_all();
  1681. }
  1682. module_init(alsa_card_loopback_init)
  1683. module_exit(alsa_card_loopback_exit)