aloop.c 54 KB

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