aloop.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279
  1. /*
  2. * Loopback soundcard
  3. *
  4. * Original code:
  5. * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
  6. *
  7. * More accurate positioning and full-duplex support:
  8. * Copyright (c) Ahmet İnan <ainan at mathematik.uni-freiburg.de>
  9. *
  10. * Major (almost complete) rewrite:
  11. * Copyright (c) by Takashi Iwai <tiwai@suse.de>
  12. *
  13. * A next major update in 2010 (separate timers for playback and capture):
  14. * Copyright (c) Jaroslav Kysela <perex@perex.cz>
  15. *
  16. * This program is free software; you can redistribute it and/or modify
  17. * it under the terms of the GNU General Public License as published by
  18. * the Free Software Foundation; either version 2 of the License, or
  19. * (at your option) any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program; if not, write to the Free Software
  28. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  29. *
  30. */
  31. #include <linux/init.h>
  32. #include <linux/jiffies.h>
  33. #include <linux/slab.h>
  34. #include <linux/time.h>
  35. #include <linux/wait.h>
  36. #include <linux/module.h>
  37. #include <linux/platform_device.h>
  38. #include <sound/core.h>
  39. #include <sound/control.h>
  40. #include <sound/pcm.h>
  41. #include <sound/info.h>
  42. #include <sound/initval.h>
  43. MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
  44. MODULE_DESCRIPTION("A loopback soundcard");
  45. MODULE_LICENSE("GPL");
  46. MODULE_SUPPORTED_DEVICE("{{ALSA,Loopback soundcard}}");
  47. #define MAX_PCM_SUBSTREAMS 8
  48. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
  49. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
  50. static bool enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 0};
  51. static int pcm_substreams[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 8};
  52. static int pcm_notify[SNDRV_CARDS];
  53. module_param_array(index, int, NULL, 0444);
  54. MODULE_PARM_DESC(index, "Index value for loopback soundcard.");
  55. module_param_array(id, charp, NULL, 0444);
  56. MODULE_PARM_DESC(id, "ID string for loopback soundcard.");
  57. module_param_array(enable, bool, NULL, 0444);
  58. MODULE_PARM_DESC(enable, "Enable this loopback soundcard.");
  59. module_param_array(pcm_substreams, int, NULL, 0444);
  60. MODULE_PARM_DESC(pcm_substreams, "PCM substreams # (1-8) for loopback driver.");
  61. module_param_array(pcm_notify, int, NULL, 0444);
  62. MODULE_PARM_DESC(pcm_notify, "Break capture when PCM format/rate/channels changes.");
  63. #define NO_PITCH 100000
  64. struct loopback_pcm;
  65. struct loopback_cable {
  66. spinlock_t lock;
  67. struct loopback_pcm *streams[2];
  68. struct snd_pcm_hardware hw;
  69. /* flags */
  70. unsigned int valid;
  71. unsigned int running;
  72. unsigned int pause;
  73. };
  74. struct loopback_setup {
  75. unsigned int notify: 1;
  76. unsigned int rate_shift;
  77. unsigned int format;
  78. unsigned int rate;
  79. unsigned int channels;
  80. struct snd_ctl_elem_id active_id;
  81. struct snd_ctl_elem_id format_id;
  82. struct snd_ctl_elem_id rate_id;
  83. struct snd_ctl_elem_id channels_id;
  84. };
  85. struct loopback {
  86. struct snd_card *card;
  87. struct mutex cable_lock;
  88. struct loopback_cable *cables[MAX_PCM_SUBSTREAMS][2];
  89. struct snd_pcm *pcm[2];
  90. struct loopback_setup setup[MAX_PCM_SUBSTREAMS][2];
  91. };
  92. struct loopback_pcm {
  93. struct loopback *loopback;
  94. struct snd_pcm_substream *substream;
  95. struct loopback_cable *cable;
  96. unsigned int pcm_buffer_size;
  97. unsigned int buf_pos; /* position in buffer */
  98. unsigned int silent_size;
  99. /* PCM parameters */
  100. unsigned int pcm_period_size;
  101. unsigned int pcm_bps; /* bytes per second */
  102. unsigned int pcm_salign; /* bytes per sample * channels */
  103. unsigned int pcm_rate_shift; /* rate shift value */
  104. /* flags */
  105. unsigned int period_update_pending :1;
  106. /* timer stuff */
  107. unsigned int irq_pos; /* fractional IRQ position */
  108. unsigned int period_size_frac;
  109. unsigned int last_drift;
  110. unsigned long last_jiffies;
  111. struct timer_list timer;
  112. };
  113. static struct platform_device *devices[SNDRV_CARDS];
  114. static inline unsigned int byte_pos(struct loopback_pcm *dpcm, unsigned int x)
  115. {
  116. if (dpcm->pcm_rate_shift == NO_PITCH) {
  117. x /= HZ;
  118. } else {
  119. x = div_u64(NO_PITCH * (unsigned long long)x,
  120. HZ * (unsigned long long)dpcm->pcm_rate_shift);
  121. }
  122. return x - (x % dpcm->pcm_salign);
  123. }
  124. static inline unsigned int frac_pos(struct loopback_pcm *dpcm, unsigned int x)
  125. {
  126. if (dpcm->pcm_rate_shift == NO_PITCH) { /* no pitch */
  127. return x * HZ;
  128. } else {
  129. x = div_u64(dpcm->pcm_rate_shift * (unsigned long long)x * HZ,
  130. NO_PITCH);
  131. }
  132. return x;
  133. }
  134. static inline struct loopback_setup *get_setup(struct loopback_pcm *dpcm)
  135. {
  136. int device = dpcm->substream->pstr->pcm->device;
  137. if (dpcm->substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  138. device ^= 1;
  139. return &dpcm->loopback->setup[dpcm->substream->number][device];
  140. }
  141. static inline unsigned int get_notify(struct loopback_pcm *dpcm)
  142. {
  143. return get_setup(dpcm)->notify;
  144. }
  145. static inline unsigned int get_rate_shift(struct loopback_pcm *dpcm)
  146. {
  147. return get_setup(dpcm)->rate_shift;
  148. }
  149. /* call in cable->lock */
  150. static void loopback_timer_start(struct loopback_pcm *dpcm)
  151. {
  152. unsigned long tick;
  153. unsigned int rate_shift = get_rate_shift(dpcm);
  154. if (rate_shift != dpcm->pcm_rate_shift) {
  155. dpcm->pcm_rate_shift = rate_shift;
  156. dpcm->period_size_frac = frac_pos(dpcm, dpcm->pcm_period_size);
  157. }
  158. if (dpcm->period_size_frac <= dpcm->irq_pos) {
  159. dpcm->irq_pos %= dpcm->period_size_frac;
  160. dpcm->period_update_pending = 1;
  161. }
  162. tick = dpcm->period_size_frac - dpcm->irq_pos;
  163. tick = (tick + dpcm->pcm_bps - 1) / dpcm->pcm_bps;
  164. dpcm->timer.expires = jiffies + tick;
  165. add_timer(&dpcm->timer);
  166. }
  167. /* call in cable->lock */
  168. static inline void loopback_timer_stop(struct loopback_pcm *dpcm)
  169. {
  170. del_timer(&dpcm->timer);
  171. dpcm->timer.expires = 0;
  172. }
  173. #define CABLE_VALID_PLAYBACK (1 << SNDRV_PCM_STREAM_PLAYBACK)
  174. #define CABLE_VALID_CAPTURE (1 << SNDRV_PCM_STREAM_CAPTURE)
  175. #define CABLE_VALID_BOTH (CABLE_VALID_PLAYBACK|CABLE_VALID_CAPTURE)
  176. static int loopback_check_format(struct loopback_cable *cable, int stream)
  177. {
  178. struct snd_pcm_runtime *runtime, *cruntime;
  179. struct loopback_setup *setup;
  180. struct snd_card *card;
  181. int check;
  182. if (cable->valid != CABLE_VALID_BOTH) {
  183. if (stream == SNDRV_PCM_STREAM_PLAYBACK)
  184. goto __notify;
  185. return 0;
  186. }
  187. runtime = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]->
  188. substream->runtime;
  189. cruntime = cable->streams[SNDRV_PCM_STREAM_CAPTURE]->
  190. substream->runtime;
  191. check = runtime->format != cruntime->format ||
  192. runtime->rate != cruntime->rate ||
  193. runtime->channels != cruntime->channels;
  194. if (!check)
  195. return 0;
  196. if (stream == SNDRV_PCM_STREAM_CAPTURE) {
  197. return -EIO;
  198. } else {
  199. snd_pcm_stop(cable->streams[SNDRV_PCM_STREAM_CAPTURE]->
  200. substream, SNDRV_PCM_STATE_DRAINING);
  201. __notify:
  202. runtime = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]->
  203. substream->runtime;
  204. setup = get_setup(cable->streams[SNDRV_PCM_STREAM_PLAYBACK]);
  205. card = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]->loopback->card;
  206. if (setup->format != runtime->format) {
  207. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
  208. &setup->format_id);
  209. setup->format = runtime->format;
  210. }
  211. if (setup->rate != runtime->rate) {
  212. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
  213. &setup->rate_id);
  214. setup->rate = runtime->rate;
  215. }
  216. if (setup->channels != runtime->channels) {
  217. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
  218. &setup->channels_id);
  219. setup->channels = runtime->channels;
  220. }
  221. }
  222. return 0;
  223. }
  224. static void loopback_active_notify(struct loopback_pcm *dpcm)
  225. {
  226. snd_ctl_notify(dpcm->loopback->card,
  227. SNDRV_CTL_EVENT_MASK_VALUE,
  228. &get_setup(dpcm)->active_id);
  229. }
  230. static int loopback_trigger(struct snd_pcm_substream *substream, int cmd)
  231. {
  232. struct snd_pcm_runtime *runtime = substream->runtime;
  233. struct loopback_pcm *dpcm = runtime->private_data;
  234. struct loopback_cable *cable = dpcm->cable;
  235. int err, stream = 1 << substream->stream;
  236. switch (cmd) {
  237. case SNDRV_PCM_TRIGGER_START:
  238. err = loopback_check_format(cable, substream->stream);
  239. if (err < 0)
  240. return err;
  241. dpcm->last_jiffies = jiffies;
  242. dpcm->pcm_rate_shift = 0;
  243. dpcm->last_drift = 0;
  244. spin_lock(&cable->lock);
  245. cable->running |= stream;
  246. cable->pause &= ~stream;
  247. loopback_timer_start(dpcm);
  248. spin_unlock(&cable->lock);
  249. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  250. loopback_active_notify(dpcm);
  251. break;
  252. case SNDRV_PCM_TRIGGER_STOP:
  253. spin_lock(&cable->lock);
  254. cable->running &= ~stream;
  255. cable->pause &= ~stream;
  256. loopback_timer_stop(dpcm);
  257. spin_unlock(&cable->lock);
  258. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  259. loopback_active_notify(dpcm);
  260. break;
  261. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  262. case SNDRV_PCM_TRIGGER_SUSPEND:
  263. spin_lock(&cable->lock);
  264. cable->pause |= stream;
  265. loopback_timer_stop(dpcm);
  266. spin_unlock(&cable->lock);
  267. break;
  268. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  269. case SNDRV_PCM_TRIGGER_RESUME:
  270. spin_lock(&cable->lock);
  271. dpcm->last_jiffies = jiffies;
  272. cable->pause &= ~stream;
  273. loopback_timer_start(dpcm);
  274. spin_unlock(&cable->lock);
  275. break;
  276. default:
  277. return -EINVAL;
  278. }
  279. return 0;
  280. }
  281. static void params_change_substream(struct loopback_pcm *dpcm,
  282. struct snd_pcm_runtime *runtime)
  283. {
  284. struct snd_pcm_runtime *dst_runtime;
  285. if (dpcm == NULL || dpcm->substream == NULL)
  286. return;
  287. dst_runtime = dpcm->substream->runtime;
  288. if (dst_runtime == NULL)
  289. return;
  290. dst_runtime->hw = dpcm->cable->hw;
  291. }
  292. static void params_change(struct snd_pcm_substream *substream)
  293. {
  294. struct snd_pcm_runtime *runtime = substream->runtime;
  295. struct loopback_pcm *dpcm = runtime->private_data;
  296. struct loopback_cable *cable = dpcm->cable;
  297. cable->hw.formats = (1ULL << runtime->format);
  298. cable->hw.rate_min = runtime->rate;
  299. cable->hw.rate_max = runtime->rate;
  300. cable->hw.channels_min = runtime->channels;
  301. cable->hw.channels_max = runtime->channels;
  302. params_change_substream(cable->streams[SNDRV_PCM_STREAM_PLAYBACK],
  303. runtime);
  304. params_change_substream(cable->streams[SNDRV_PCM_STREAM_CAPTURE],
  305. runtime);
  306. }
  307. static int loopback_prepare(struct snd_pcm_substream *substream)
  308. {
  309. struct snd_pcm_runtime *runtime = substream->runtime;
  310. struct loopback_pcm *dpcm = runtime->private_data;
  311. struct loopback_cable *cable = dpcm->cable;
  312. int bps, salign;
  313. salign = (snd_pcm_format_width(runtime->format) *
  314. runtime->channels) / 8;
  315. bps = salign * runtime->rate;
  316. if (bps <= 0 || salign <= 0)
  317. return -EINVAL;
  318. dpcm->buf_pos = 0;
  319. dpcm->pcm_buffer_size = frames_to_bytes(runtime, runtime->buffer_size);
  320. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
  321. /* clear capture buffer */
  322. dpcm->silent_size = dpcm->pcm_buffer_size;
  323. snd_pcm_format_set_silence(runtime->format, runtime->dma_area,
  324. runtime->buffer_size * runtime->channels);
  325. }
  326. dpcm->irq_pos = 0;
  327. dpcm->period_update_pending = 0;
  328. dpcm->pcm_bps = bps;
  329. dpcm->pcm_salign = salign;
  330. dpcm->pcm_period_size = frames_to_bytes(runtime, runtime->period_size);
  331. mutex_lock(&dpcm->loopback->cable_lock);
  332. if (!(cable->valid & ~(1 << substream->stream)) ||
  333. (get_setup(dpcm)->notify &&
  334. substream->stream == SNDRV_PCM_STREAM_PLAYBACK))
  335. params_change(substream);
  336. cable->valid |= 1 << substream->stream;
  337. mutex_unlock(&dpcm->loopback->cable_lock);
  338. return 0;
  339. }
  340. static void clear_capture_buf(struct loopback_pcm *dpcm, unsigned int bytes)
  341. {
  342. struct snd_pcm_runtime *runtime = dpcm->substream->runtime;
  343. char *dst = runtime->dma_area;
  344. unsigned int dst_off = dpcm->buf_pos;
  345. if (dpcm->silent_size >= dpcm->pcm_buffer_size)
  346. return;
  347. if (dpcm->silent_size + bytes > dpcm->pcm_buffer_size)
  348. bytes = dpcm->pcm_buffer_size - dpcm->silent_size;
  349. for (;;) {
  350. unsigned int size = bytes;
  351. if (dst_off + size > dpcm->pcm_buffer_size)
  352. size = dpcm->pcm_buffer_size - dst_off;
  353. snd_pcm_format_set_silence(runtime->format, dst + dst_off,
  354. bytes_to_frames(runtime, size) *
  355. runtime->channels);
  356. dpcm->silent_size += size;
  357. bytes -= size;
  358. if (!bytes)
  359. break;
  360. dst_off = 0;
  361. }
  362. }
  363. static void copy_play_buf(struct loopback_pcm *play,
  364. struct loopback_pcm *capt,
  365. unsigned int bytes)
  366. {
  367. struct snd_pcm_runtime *runtime = play->substream->runtime;
  368. char *src = runtime->dma_area;
  369. char *dst = capt->substream->runtime->dma_area;
  370. unsigned int src_off = play->buf_pos;
  371. unsigned int dst_off = capt->buf_pos;
  372. unsigned int clear_bytes = 0;
  373. /* check if playback is draining, trim the capture copy size
  374. * when our pointer is at the end of playback ring buffer */
  375. if (runtime->status->state == SNDRV_PCM_STATE_DRAINING &&
  376. snd_pcm_playback_hw_avail(runtime) < runtime->buffer_size) {
  377. snd_pcm_uframes_t appl_ptr, appl_ptr1, diff;
  378. appl_ptr = appl_ptr1 = runtime->control->appl_ptr;
  379. appl_ptr1 -= appl_ptr1 % runtime->buffer_size;
  380. appl_ptr1 += play->buf_pos / play->pcm_salign;
  381. if (appl_ptr < appl_ptr1)
  382. appl_ptr1 -= runtime->buffer_size;
  383. diff = (appl_ptr - appl_ptr1) * play->pcm_salign;
  384. if (diff < bytes) {
  385. clear_bytes = bytes - diff;
  386. bytes = diff;
  387. }
  388. }
  389. for (;;) {
  390. unsigned int size = bytes;
  391. if (src_off + size > play->pcm_buffer_size)
  392. size = play->pcm_buffer_size - src_off;
  393. if (dst_off + size > capt->pcm_buffer_size)
  394. size = capt->pcm_buffer_size - dst_off;
  395. memcpy(dst + dst_off, src + src_off, size);
  396. capt->silent_size = 0;
  397. bytes -= size;
  398. if (!bytes)
  399. break;
  400. src_off = (src_off + size) % play->pcm_buffer_size;
  401. dst_off = (dst_off + size) % capt->pcm_buffer_size;
  402. }
  403. if (clear_bytes > 0) {
  404. clear_capture_buf(capt, clear_bytes);
  405. capt->silent_size = 0;
  406. }
  407. }
  408. static inline unsigned int bytepos_delta(struct loopback_pcm *dpcm,
  409. unsigned int jiffies_delta)
  410. {
  411. unsigned long last_pos;
  412. unsigned int delta;
  413. last_pos = byte_pos(dpcm, dpcm->irq_pos);
  414. dpcm->irq_pos += jiffies_delta * dpcm->pcm_bps;
  415. delta = byte_pos(dpcm, dpcm->irq_pos) - last_pos;
  416. if (delta >= dpcm->last_drift)
  417. delta -= dpcm->last_drift;
  418. dpcm->last_drift = 0;
  419. if (dpcm->irq_pos >= dpcm->period_size_frac) {
  420. dpcm->irq_pos %= dpcm->period_size_frac;
  421. dpcm->period_update_pending = 1;
  422. }
  423. return delta;
  424. }
  425. static inline void bytepos_finish(struct loopback_pcm *dpcm,
  426. unsigned int delta)
  427. {
  428. dpcm->buf_pos += delta;
  429. dpcm->buf_pos %= dpcm->pcm_buffer_size;
  430. }
  431. /* call in cable->lock */
  432. static unsigned int loopback_pos_update(struct loopback_cable *cable)
  433. {
  434. struct loopback_pcm *dpcm_play =
  435. cable->streams[SNDRV_PCM_STREAM_PLAYBACK];
  436. struct loopback_pcm *dpcm_capt =
  437. cable->streams[SNDRV_PCM_STREAM_CAPTURE];
  438. unsigned long delta_play = 0, delta_capt = 0;
  439. unsigned int running, count1, count2;
  440. running = cable->running ^ cable->pause;
  441. if (running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) {
  442. delta_play = jiffies - dpcm_play->last_jiffies;
  443. dpcm_play->last_jiffies += delta_play;
  444. }
  445. if (running & (1 << SNDRV_PCM_STREAM_CAPTURE)) {
  446. delta_capt = jiffies - dpcm_capt->last_jiffies;
  447. dpcm_capt->last_jiffies += delta_capt;
  448. }
  449. if (delta_play == 0 && delta_capt == 0)
  450. goto unlock;
  451. if (delta_play > delta_capt) {
  452. count1 = bytepos_delta(dpcm_play, delta_play - delta_capt);
  453. bytepos_finish(dpcm_play, count1);
  454. delta_play = delta_capt;
  455. } else if (delta_play < delta_capt) {
  456. count1 = bytepos_delta(dpcm_capt, delta_capt - delta_play);
  457. clear_capture_buf(dpcm_capt, count1);
  458. bytepos_finish(dpcm_capt, count1);
  459. delta_capt = delta_play;
  460. }
  461. if (delta_play == 0 && delta_capt == 0)
  462. goto unlock;
  463. /* note delta_capt == delta_play at this moment */
  464. count1 = bytepos_delta(dpcm_play, delta_play);
  465. count2 = bytepos_delta(dpcm_capt, delta_capt);
  466. if (count1 < count2) {
  467. dpcm_capt->last_drift = count2 - count1;
  468. count1 = count2;
  469. } else if (count1 > count2) {
  470. dpcm_play->last_drift = count1 - count2;
  471. }
  472. copy_play_buf(dpcm_play, dpcm_capt, count1);
  473. bytepos_finish(dpcm_play, count1);
  474. bytepos_finish(dpcm_capt, count1);
  475. unlock:
  476. return running;
  477. }
  478. static void loopback_timer_function(unsigned long data)
  479. {
  480. struct loopback_pcm *dpcm = (struct loopback_pcm *)data;
  481. unsigned long flags;
  482. spin_lock_irqsave(&dpcm->cable->lock, flags);
  483. if (loopback_pos_update(dpcm->cable) & (1 << dpcm->substream->stream)) {
  484. loopback_timer_start(dpcm);
  485. if (dpcm->period_update_pending) {
  486. dpcm->period_update_pending = 0;
  487. spin_unlock_irqrestore(&dpcm->cable->lock, flags);
  488. /* need to unlock before calling below */
  489. snd_pcm_period_elapsed(dpcm->substream);
  490. return;
  491. }
  492. }
  493. spin_unlock_irqrestore(&dpcm->cable->lock, flags);
  494. }
  495. static snd_pcm_uframes_t loopback_pointer(struct snd_pcm_substream *substream)
  496. {
  497. struct snd_pcm_runtime *runtime = substream->runtime;
  498. struct loopback_pcm *dpcm = runtime->private_data;
  499. snd_pcm_uframes_t pos;
  500. spin_lock(&dpcm->cable->lock);
  501. loopback_pos_update(dpcm->cable);
  502. pos = dpcm->buf_pos;
  503. spin_unlock(&dpcm->cable->lock);
  504. return bytes_to_frames(runtime, pos);
  505. }
  506. static struct snd_pcm_hardware loopback_pcm_hardware =
  507. {
  508. .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_MMAP |
  509. SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_PAUSE |
  510. SNDRV_PCM_INFO_RESUME),
  511. .formats = (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE |
  512. SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE |
  513. SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE),
  514. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_192000,
  515. .rate_min = 8000,
  516. .rate_max = 192000,
  517. .channels_min = 1,
  518. .channels_max = 32,
  519. .buffer_bytes_max = 2 * 1024 * 1024,
  520. .period_bytes_min = 64,
  521. /* note check overflow in frac_pos() using pcm_rate_shift before
  522. changing period_bytes_max value */
  523. .period_bytes_max = 1024 * 1024,
  524. .periods_min = 1,
  525. .periods_max = 1024,
  526. .fifo_size = 0,
  527. };
  528. static void loopback_runtime_free(struct snd_pcm_runtime *runtime)
  529. {
  530. struct loopback_pcm *dpcm = runtime->private_data;
  531. kfree(dpcm);
  532. }
  533. static int loopback_hw_params(struct snd_pcm_substream *substream,
  534. struct snd_pcm_hw_params *params)
  535. {
  536. return snd_pcm_lib_alloc_vmalloc_buffer(substream,
  537. params_buffer_bytes(params));
  538. }
  539. static int loopback_hw_free(struct snd_pcm_substream *substream)
  540. {
  541. struct snd_pcm_runtime *runtime = substream->runtime;
  542. struct loopback_pcm *dpcm = runtime->private_data;
  543. struct loopback_cable *cable = dpcm->cable;
  544. mutex_lock(&dpcm->loopback->cable_lock);
  545. cable->valid &= ~(1 << substream->stream);
  546. mutex_unlock(&dpcm->loopback->cable_lock);
  547. return snd_pcm_lib_free_vmalloc_buffer(substream);
  548. }
  549. static unsigned int get_cable_index(struct snd_pcm_substream *substream)
  550. {
  551. if (!substream->pcm->device)
  552. return substream->stream;
  553. else
  554. return !substream->stream;
  555. }
  556. static int rule_format(struct snd_pcm_hw_params *params,
  557. struct snd_pcm_hw_rule *rule)
  558. {
  559. struct snd_pcm_hardware *hw = rule->private;
  560. struct snd_mask *maskp = hw_param_mask(params, rule->var);
  561. maskp->bits[0] &= (u_int32_t)hw->formats;
  562. maskp->bits[1] &= (u_int32_t)(hw->formats >> 32);
  563. memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */
  564. if (! maskp->bits[0] && ! maskp->bits[1])
  565. return -EINVAL;
  566. return 0;
  567. }
  568. static int rule_rate(struct snd_pcm_hw_params *params,
  569. struct snd_pcm_hw_rule *rule)
  570. {
  571. struct snd_pcm_hardware *hw = rule->private;
  572. struct snd_interval t;
  573. t.min = hw->rate_min;
  574. t.max = hw->rate_max;
  575. t.openmin = t.openmax = 0;
  576. t.integer = 0;
  577. return snd_interval_refine(hw_param_interval(params, rule->var), &t);
  578. }
  579. static int rule_channels(struct snd_pcm_hw_params *params,
  580. struct snd_pcm_hw_rule *rule)
  581. {
  582. struct snd_pcm_hardware *hw = rule->private;
  583. struct snd_interval t;
  584. t.min = hw->channels_min;
  585. t.max = hw->channels_max;
  586. t.openmin = t.openmax = 0;
  587. t.integer = 0;
  588. return snd_interval_refine(hw_param_interval(params, rule->var), &t);
  589. }
  590. static int loopback_open(struct snd_pcm_substream *substream)
  591. {
  592. struct snd_pcm_runtime *runtime = substream->runtime;
  593. struct loopback *loopback = substream->private_data;
  594. struct loopback_pcm *dpcm;
  595. struct loopback_cable *cable;
  596. int err = 0;
  597. int dev = get_cable_index(substream);
  598. mutex_lock(&loopback->cable_lock);
  599. dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
  600. if (!dpcm) {
  601. err = -ENOMEM;
  602. goto unlock;
  603. }
  604. dpcm->loopback = loopback;
  605. dpcm->substream = substream;
  606. setup_timer(&dpcm->timer, loopback_timer_function,
  607. (unsigned long)dpcm);
  608. cable = loopback->cables[substream->number][dev];
  609. if (!cable) {
  610. cable = kzalloc(sizeof(*cable), GFP_KERNEL);
  611. if (!cable) {
  612. kfree(dpcm);
  613. err = -ENOMEM;
  614. goto unlock;
  615. }
  616. spin_lock_init(&cable->lock);
  617. cable->hw = loopback_pcm_hardware;
  618. loopback->cables[substream->number][dev] = cable;
  619. }
  620. dpcm->cable = cable;
  621. cable->streams[substream->stream] = dpcm;
  622. snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
  623. /* use dynamic rules based on actual runtime->hw values */
  624. /* note that the default rules created in the PCM midlevel code */
  625. /* are cached -> they do not reflect the actual state */
  626. err = snd_pcm_hw_rule_add(runtime, 0,
  627. SNDRV_PCM_HW_PARAM_FORMAT,
  628. rule_format, &runtime->hw,
  629. SNDRV_PCM_HW_PARAM_FORMAT, -1);
  630. if (err < 0)
  631. goto unlock;
  632. err = snd_pcm_hw_rule_add(runtime, 0,
  633. SNDRV_PCM_HW_PARAM_RATE,
  634. rule_rate, &runtime->hw,
  635. SNDRV_PCM_HW_PARAM_RATE, -1);
  636. if (err < 0)
  637. goto unlock;
  638. err = snd_pcm_hw_rule_add(runtime, 0,
  639. SNDRV_PCM_HW_PARAM_CHANNELS,
  640. rule_channels, &runtime->hw,
  641. SNDRV_PCM_HW_PARAM_CHANNELS, -1);
  642. if (err < 0)
  643. goto unlock;
  644. runtime->private_data = dpcm;
  645. runtime->private_free = loopback_runtime_free;
  646. if (get_notify(dpcm))
  647. runtime->hw = loopback_pcm_hardware;
  648. else
  649. runtime->hw = cable->hw;
  650. unlock:
  651. mutex_unlock(&loopback->cable_lock);
  652. return err;
  653. }
  654. static int loopback_close(struct snd_pcm_substream *substream)
  655. {
  656. struct loopback *loopback = substream->private_data;
  657. struct loopback_pcm *dpcm = substream->runtime->private_data;
  658. struct loopback_cable *cable;
  659. int dev = get_cable_index(substream);
  660. loopback_timer_stop(dpcm);
  661. mutex_lock(&loopback->cable_lock);
  662. cable = loopback->cables[substream->number][dev];
  663. if (cable->streams[!substream->stream]) {
  664. /* other stream is still alive */
  665. cable->streams[substream->stream] = NULL;
  666. } else {
  667. /* free the cable */
  668. loopback->cables[substream->number][dev] = NULL;
  669. kfree(cable);
  670. }
  671. mutex_unlock(&loopback->cable_lock);
  672. return 0;
  673. }
  674. static struct snd_pcm_ops loopback_playback_ops = {
  675. .open = loopback_open,
  676. .close = loopback_close,
  677. .ioctl = snd_pcm_lib_ioctl,
  678. .hw_params = loopback_hw_params,
  679. .hw_free = loopback_hw_free,
  680. .prepare = loopback_prepare,
  681. .trigger = loopback_trigger,
  682. .pointer = loopback_pointer,
  683. .page = snd_pcm_lib_get_vmalloc_page,
  684. .mmap = snd_pcm_lib_mmap_vmalloc,
  685. };
  686. static struct snd_pcm_ops loopback_capture_ops = {
  687. .open = loopback_open,
  688. .close = loopback_close,
  689. .ioctl = snd_pcm_lib_ioctl,
  690. .hw_params = loopback_hw_params,
  691. .hw_free = loopback_hw_free,
  692. .prepare = loopback_prepare,
  693. .trigger = loopback_trigger,
  694. .pointer = loopback_pointer,
  695. .page = snd_pcm_lib_get_vmalloc_page,
  696. .mmap = snd_pcm_lib_mmap_vmalloc,
  697. };
  698. static int loopback_pcm_new(struct loopback *loopback,
  699. int device, int substreams)
  700. {
  701. struct snd_pcm *pcm;
  702. int err;
  703. err = snd_pcm_new(loopback->card, "Loopback PCM", device,
  704. substreams, substreams, &pcm);
  705. if (err < 0)
  706. return err;
  707. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &loopback_playback_ops);
  708. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &loopback_capture_ops);
  709. pcm->private_data = loopback;
  710. pcm->info_flags = 0;
  711. strcpy(pcm->name, "Loopback PCM");
  712. loopback->pcm[device] = pcm;
  713. return 0;
  714. }
  715. static int loopback_rate_shift_info(struct snd_kcontrol *kcontrol,
  716. struct snd_ctl_elem_info *uinfo)
  717. {
  718. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  719. uinfo->count = 1;
  720. uinfo->value.integer.min = 80000;
  721. uinfo->value.integer.max = 120000;
  722. uinfo->value.integer.step = 1;
  723. return 0;
  724. }
  725. static int loopback_rate_shift_get(struct snd_kcontrol *kcontrol,
  726. struct snd_ctl_elem_value *ucontrol)
  727. {
  728. struct loopback *loopback = snd_kcontrol_chip(kcontrol);
  729. ucontrol->value.integer.value[0] =
  730. loopback->setup[kcontrol->id.subdevice]
  731. [kcontrol->id.device].rate_shift;
  732. return 0;
  733. }
  734. static int loopback_rate_shift_put(struct snd_kcontrol *kcontrol,
  735. struct snd_ctl_elem_value *ucontrol)
  736. {
  737. struct loopback *loopback = snd_kcontrol_chip(kcontrol);
  738. unsigned int val;
  739. int change = 0;
  740. val = ucontrol->value.integer.value[0];
  741. if (val < 80000)
  742. val = 80000;
  743. if (val > 120000)
  744. val = 120000;
  745. mutex_lock(&loopback->cable_lock);
  746. if (val != loopback->setup[kcontrol->id.subdevice]
  747. [kcontrol->id.device].rate_shift) {
  748. loopback->setup[kcontrol->id.subdevice]
  749. [kcontrol->id.device].rate_shift = val;
  750. change = 1;
  751. }
  752. mutex_unlock(&loopback->cable_lock);
  753. return change;
  754. }
  755. static int loopback_notify_get(struct snd_kcontrol *kcontrol,
  756. struct snd_ctl_elem_value *ucontrol)
  757. {
  758. struct loopback *loopback = snd_kcontrol_chip(kcontrol);
  759. ucontrol->value.integer.value[0] =
  760. loopback->setup[kcontrol->id.subdevice]
  761. [kcontrol->id.device].notify;
  762. return 0;
  763. }
  764. static int loopback_notify_put(struct snd_kcontrol *kcontrol,
  765. struct snd_ctl_elem_value *ucontrol)
  766. {
  767. struct loopback *loopback = snd_kcontrol_chip(kcontrol);
  768. unsigned int val;
  769. int change = 0;
  770. val = ucontrol->value.integer.value[0] ? 1 : 0;
  771. if (val != loopback->setup[kcontrol->id.subdevice]
  772. [kcontrol->id.device].notify) {
  773. loopback->setup[kcontrol->id.subdevice]
  774. [kcontrol->id.device].notify = val;
  775. change = 1;
  776. }
  777. return change;
  778. }
  779. static int loopback_active_get(struct snd_kcontrol *kcontrol,
  780. struct snd_ctl_elem_value *ucontrol)
  781. {
  782. struct loopback *loopback = snd_kcontrol_chip(kcontrol);
  783. struct loopback_cable *cable = loopback->cables
  784. [kcontrol->id.subdevice][kcontrol->id.device ^ 1];
  785. unsigned int val = 0;
  786. if (cable != NULL)
  787. val = (cable->running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) ?
  788. 1 : 0;
  789. ucontrol->value.integer.value[0] = val;
  790. return 0;
  791. }
  792. static int loopback_format_info(struct snd_kcontrol *kcontrol,
  793. struct snd_ctl_elem_info *uinfo)
  794. {
  795. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  796. uinfo->count = 1;
  797. uinfo->value.integer.min = 0;
  798. uinfo->value.integer.max = SNDRV_PCM_FORMAT_LAST;
  799. uinfo->value.integer.step = 1;
  800. return 0;
  801. }
  802. static int loopback_format_get(struct snd_kcontrol *kcontrol,
  803. struct snd_ctl_elem_value *ucontrol)
  804. {
  805. struct loopback *loopback = snd_kcontrol_chip(kcontrol);
  806. ucontrol->value.integer.value[0] =
  807. loopback->setup[kcontrol->id.subdevice]
  808. [kcontrol->id.device].format;
  809. return 0;
  810. }
  811. static int loopback_rate_info(struct snd_kcontrol *kcontrol,
  812. struct snd_ctl_elem_info *uinfo)
  813. {
  814. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  815. uinfo->count = 1;
  816. uinfo->value.integer.min = 0;
  817. uinfo->value.integer.max = 192000;
  818. uinfo->value.integer.step = 1;
  819. return 0;
  820. }
  821. static int loopback_rate_get(struct snd_kcontrol *kcontrol,
  822. struct snd_ctl_elem_value *ucontrol)
  823. {
  824. struct loopback *loopback = snd_kcontrol_chip(kcontrol);
  825. ucontrol->value.integer.value[0] =
  826. loopback->setup[kcontrol->id.subdevice]
  827. [kcontrol->id.device].rate;
  828. return 0;
  829. }
  830. static int loopback_channels_info(struct snd_kcontrol *kcontrol,
  831. struct snd_ctl_elem_info *uinfo)
  832. {
  833. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  834. uinfo->count = 1;
  835. uinfo->value.integer.min = 1;
  836. uinfo->value.integer.max = 1024;
  837. uinfo->value.integer.step = 1;
  838. return 0;
  839. }
  840. static int loopback_channels_get(struct snd_kcontrol *kcontrol,
  841. struct snd_ctl_elem_value *ucontrol)
  842. {
  843. struct loopback *loopback = snd_kcontrol_chip(kcontrol);
  844. ucontrol->value.integer.value[0] =
  845. loopback->setup[kcontrol->id.subdevice]
  846. [kcontrol->id.device].channels;
  847. return 0;
  848. }
  849. static struct snd_kcontrol_new loopback_controls[] = {
  850. {
  851. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  852. .name = "PCM Rate Shift 100000",
  853. .info = loopback_rate_shift_info,
  854. .get = loopback_rate_shift_get,
  855. .put = loopback_rate_shift_put,
  856. },
  857. {
  858. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  859. .name = "PCM Notify",
  860. .info = snd_ctl_boolean_mono_info,
  861. .get = loopback_notify_get,
  862. .put = loopback_notify_put,
  863. },
  864. #define ACTIVE_IDX 2
  865. {
  866. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  867. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  868. .name = "PCM Slave Active",
  869. .info = snd_ctl_boolean_mono_info,
  870. .get = loopback_active_get,
  871. },
  872. #define FORMAT_IDX 3
  873. {
  874. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  875. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  876. .name = "PCM Slave Format",
  877. .info = loopback_format_info,
  878. .get = loopback_format_get
  879. },
  880. #define RATE_IDX 4
  881. {
  882. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  883. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  884. .name = "PCM Slave Rate",
  885. .info = loopback_rate_info,
  886. .get = loopback_rate_get
  887. },
  888. #define CHANNELS_IDX 5
  889. {
  890. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  891. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  892. .name = "PCM Slave Channels",
  893. .info = loopback_channels_info,
  894. .get = loopback_channels_get
  895. }
  896. };
  897. static int loopback_mixer_new(struct loopback *loopback, int notify)
  898. {
  899. struct snd_card *card = loopback->card;
  900. struct snd_pcm *pcm;
  901. struct snd_kcontrol *kctl;
  902. struct loopback_setup *setup;
  903. int err, dev, substr, substr_count, idx;
  904. strcpy(card->mixername, "Loopback Mixer");
  905. for (dev = 0; dev < 2; dev++) {
  906. pcm = loopback->pcm[dev];
  907. substr_count =
  908. pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count;
  909. for (substr = 0; substr < substr_count; substr++) {
  910. setup = &loopback->setup[substr][dev];
  911. setup->notify = notify;
  912. setup->rate_shift = NO_PITCH;
  913. setup->format = SNDRV_PCM_FORMAT_S16_LE;
  914. setup->rate = 48000;
  915. setup->channels = 2;
  916. for (idx = 0; idx < ARRAY_SIZE(loopback_controls);
  917. idx++) {
  918. kctl = snd_ctl_new1(&loopback_controls[idx],
  919. loopback);
  920. if (!kctl)
  921. return -ENOMEM;
  922. kctl->id.device = dev;
  923. kctl->id.subdevice = substr;
  924. switch (idx) {
  925. case ACTIVE_IDX:
  926. setup->active_id = kctl->id;
  927. break;
  928. case FORMAT_IDX:
  929. setup->format_id = kctl->id;
  930. break;
  931. case RATE_IDX:
  932. setup->rate_id = kctl->id;
  933. break;
  934. case CHANNELS_IDX:
  935. setup->channels_id = kctl->id;
  936. break;
  937. default:
  938. break;
  939. }
  940. err = snd_ctl_add(card, kctl);
  941. if (err < 0)
  942. return err;
  943. }
  944. }
  945. }
  946. return 0;
  947. }
  948. #ifdef CONFIG_PROC_FS
  949. static void print_dpcm_info(struct snd_info_buffer *buffer,
  950. struct loopback_pcm *dpcm,
  951. const char *id)
  952. {
  953. snd_iprintf(buffer, " %s\n", id);
  954. if (dpcm == NULL) {
  955. snd_iprintf(buffer, " inactive\n");
  956. return;
  957. }
  958. snd_iprintf(buffer, " buffer_size:\t%u\n", dpcm->pcm_buffer_size);
  959. snd_iprintf(buffer, " buffer_pos:\t\t%u\n", dpcm->buf_pos);
  960. snd_iprintf(buffer, " silent_size:\t%u\n", dpcm->silent_size);
  961. snd_iprintf(buffer, " period_size:\t%u\n", dpcm->pcm_period_size);
  962. snd_iprintf(buffer, " bytes_per_sec:\t%u\n", dpcm->pcm_bps);
  963. snd_iprintf(buffer, " sample_align:\t%u\n", dpcm->pcm_salign);
  964. snd_iprintf(buffer, " rate_shift:\t\t%u\n", dpcm->pcm_rate_shift);
  965. snd_iprintf(buffer, " update_pending:\t%u\n",
  966. dpcm->period_update_pending);
  967. snd_iprintf(buffer, " irq_pos:\t\t%u\n", dpcm->irq_pos);
  968. snd_iprintf(buffer, " period_frac:\t%u\n", dpcm->period_size_frac);
  969. snd_iprintf(buffer, " last_jiffies:\t%lu (%lu)\n",
  970. dpcm->last_jiffies, jiffies);
  971. snd_iprintf(buffer, " timer_expires:\t%lu\n", dpcm->timer.expires);
  972. }
  973. static void print_substream_info(struct snd_info_buffer *buffer,
  974. struct loopback *loopback,
  975. int sub,
  976. int num)
  977. {
  978. struct loopback_cable *cable = loopback->cables[sub][num];
  979. snd_iprintf(buffer, "Cable %i substream %i:\n", num, sub);
  980. if (cable == NULL) {
  981. snd_iprintf(buffer, " inactive\n");
  982. return;
  983. }
  984. snd_iprintf(buffer, " valid: %u\n", cable->valid);
  985. snd_iprintf(buffer, " running: %u\n", cable->running);
  986. snd_iprintf(buffer, " pause: %u\n", cable->pause);
  987. print_dpcm_info(buffer, cable->streams[0], "Playback");
  988. print_dpcm_info(buffer, cable->streams[1], "Capture");
  989. }
  990. static void print_cable_info(struct snd_info_entry *entry,
  991. struct snd_info_buffer *buffer)
  992. {
  993. struct loopback *loopback = entry->private_data;
  994. int sub, num;
  995. mutex_lock(&loopback->cable_lock);
  996. num = entry->name[strlen(entry->name)-1];
  997. num = num == '0' ? 0 : 1;
  998. for (sub = 0; sub < MAX_PCM_SUBSTREAMS; sub++)
  999. print_substream_info(buffer, loopback, sub, num);
  1000. mutex_unlock(&loopback->cable_lock);
  1001. }
  1002. static int loopback_proc_new(struct loopback *loopback, int cidx)
  1003. {
  1004. char name[32];
  1005. struct snd_info_entry *entry;
  1006. int err;
  1007. snprintf(name, sizeof(name), "cable#%d", cidx);
  1008. err = snd_card_proc_new(loopback->card, name, &entry);
  1009. if (err < 0)
  1010. return err;
  1011. snd_info_set_text_ops(entry, loopback, print_cable_info);
  1012. return 0;
  1013. }
  1014. #else /* !CONFIG_PROC_FS */
  1015. #define loopback_proc_new(loopback, cidx) do { } while (0)
  1016. #endif
  1017. static int loopback_probe(struct platform_device *devptr)
  1018. {
  1019. struct snd_card *card;
  1020. struct loopback *loopback;
  1021. int dev = devptr->id;
  1022. int err;
  1023. err = snd_card_create(index[dev], id[dev], THIS_MODULE,
  1024. sizeof(struct loopback), &card);
  1025. if (err < 0)
  1026. return err;
  1027. loopback = card->private_data;
  1028. if (pcm_substreams[dev] < 1)
  1029. pcm_substreams[dev] = 1;
  1030. if (pcm_substreams[dev] > MAX_PCM_SUBSTREAMS)
  1031. pcm_substreams[dev] = MAX_PCM_SUBSTREAMS;
  1032. loopback->card = card;
  1033. mutex_init(&loopback->cable_lock);
  1034. err = loopback_pcm_new(loopback, 0, pcm_substreams[dev]);
  1035. if (err < 0)
  1036. goto __nodev;
  1037. err = loopback_pcm_new(loopback, 1, pcm_substreams[dev]);
  1038. if (err < 0)
  1039. goto __nodev;
  1040. err = loopback_mixer_new(loopback, pcm_notify[dev] ? 1 : 0);
  1041. if (err < 0)
  1042. goto __nodev;
  1043. loopback_proc_new(loopback, 0);
  1044. loopback_proc_new(loopback, 1);
  1045. strcpy(card->driver, "Loopback");
  1046. strcpy(card->shortname, "Loopback");
  1047. sprintf(card->longname, "Loopback %i", dev + 1);
  1048. err = snd_card_register(card);
  1049. if (!err) {
  1050. platform_set_drvdata(devptr, card);
  1051. return 0;
  1052. }
  1053. __nodev:
  1054. snd_card_free(card);
  1055. return err;
  1056. }
  1057. static int loopback_remove(struct platform_device *devptr)
  1058. {
  1059. snd_card_free(platform_get_drvdata(devptr));
  1060. platform_set_drvdata(devptr, NULL);
  1061. return 0;
  1062. }
  1063. #ifdef CONFIG_PM_SLEEP
  1064. static int loopback_suspend(struct device *pdev)
  1065. {
  1066. struct snd_card *card = dev_get_drvdata(pdev);
  1067. struct loopback *loopback = card->private_data;
  1068. snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
  1069. snd_pcm_suspend_all(loopback->pcm[0]);
  1070. snd_pcm_suspend_all(loopback->pcm[1]);
  1071. return 0;
  1072. }
  1073. static int loopback_resume(struct device *pdev)
  1074. {
  1075. struct snd_card *card = dev_get_drvdata(pdev);
  1076. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  1077. return 0;
  1078. }
  1079. static SIMPLE_DEV_PM_OPS(loopback_pm, loopback_suspend, loopback_resume);
  1080. #define LOOPBACK_PM_OPS &loopback_pm
  1081. #else
  1082. #define LOOPBACK_PM_OPS NULL
  1083. #endif
  1084. #define SND_LOOPBACK_DRIVER "snd_aloop"
  1085. static struct platform_driver loopback_driver = {
  1086. .probe = loopback_probe,
  1087. .remove = loopback_remove,
  1088. .driver = {
  1089. .name = SND_LOOPBACK_DRIVER,
  1090. .owner = THIS_MODULE,
  1091. .pm = LOOPBACK_PM_OPS,
  1092. },
  1093. };
  1094. static void loopback_unregister_all(void)
  1095. {
  1096. int i;
  1097. for (i = 0; i < ARRAY_SIZE(devices); ++i)
  1098. platform_device_unregister(devices[i]);
  1099. platform_driver_unregister(&loopback_driver);
  1100. }
  1101. static int __init alsa_card_loopback_init(void)
  1102. {
  1103. int i, err, cards;
  1104. err = platform_driver_register(&loopback_driver);
  1105. if (err < 0)
  1106. return err;
  1107. cards = 0;
  1108. for (i = 0; i < SNDRV_CARDS; i++) {
  1109. struct platform_device *device;
  1110. if (!enable[i])
  1111. continue;
  1112. device = platform_device_register_simple(SND_LOOPBACK_DRIVER,
  1113. i, NULL, 0);
  1114. if (IS_ERR(device))
  1115. continue;
  1116. if (!platform_get_drvdata(device)) {
  1117. platform_device_unregister(device);
  1118. continue;
  1119. }
  1120. devices[i] = device;
  1121. cards++;
  1122. }
  1123. if (!cards) {
  1124. #ifdef MODULE
  1125. printk(KERN_ERR "aloop: No loopback enabled\n");
  1126. #endif
  1127. loopback_unregister_all();
  1128. return -ENODEV;
  1129. }
  1130. return 0;
  1131. }
  1132. static void __exit alsa_card_loopback_exit(void)
  1133. {
  1134. loopback_unregister_all();
  1135. }
  1136. module_init(alsa_card_loopback_init)
  1137. module_exit(alsa_card_loopback_exit)