aloop.c 52 KB

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