aloop.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057
  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/moduleparam.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/initval.h>
  42. MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
  43. MODULE_DESCRIPTION("A loopback soundcard");
  44. MODULE_LICENSE("GPL");
  45. MODULE_SUPPORTED_DEVICE("{{ALSA,Loopback soundcard}}");
  46. #define MAX_PCM_SUBSTREAMS 8
  47. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
  48. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
  49. static int enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 0};
  50. static int pcm_substreams[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 8};
  51. static int pcm_notify[SNDRV_CARDS];
  52. module_param_array(index, int, NULL, 0444);
  53. MODULE_PARM_DESC(index, "Index value for loopback soundcard.");
  54. module_param_array(id, charp, NULL, 0444);
  55. MODULE_PARM_DESC(id, "ID string for loopback soundcard.");
  56. module_param_array(enable, bool, NULL, 0444);
  57. MODULE_PARM_DESC(enable, "Enable this loopback soundcard.");
  58. module_param_array(pcm_substreams, int, NULL, 0444);
  59. MODULE_PARM_DESC(pcm_substreams, "PCM substreams # (1-8) for loopback driver.");
  60. module_param_array(pcm_notify, int, NULL, 0444);
  61. MODULE_PARM_DESC(pcm_notify, "Break capture when PCM format/rate/channels changes.");
  62. #define NO_PITCH 100000
  63. struct loopback_pcm;
  64. struct loopback_cable {
  65. spinlock_t lock;
  66. struct loopback_pcm *streams[2];
  67. struct snd_pcm_hardware hw;
  68. /* flags */
  69. unsigned int valid;
  70. unsigned int running;
  71. };
  72. struct loopback_setup {
  73. unsigned int notify: 1;
  74. unsigned int rate_shift;
  75. unsigned int format;
  76. unsigned int rate;
  77. unsigned int channels;
  78. struct snd_ctl_elem_id active_id;
  79. struct snd_ctl_elem_id format_id;
  80. struct snd_ctl_elem_id rate_id;
  81. struct snd_ctl_elem_id channels_id;
  82. };
  83. struct loopback {
  84. struct snd_card *card;
  85. struct mutex cable_lock;
  86. struct loopback_cable *cables[MAX_PCM_SUBSTREAMS][2];
  87. struct snd_pcm *pcm[2];
  88. struct loopback_setup setup[MAX_PCM_SUBSTREAMS][2];
  89. };
  90. struct loopback_pcm {
  91. struct loopback *loopback;
  92. struct snd_pcm_substream *substream;
  93. struct loopback_cable *cable;
  94. unsigned int pcm_buffer_size;
  95. unsigned int buf_pos; /* position in buffer */
  96. unsigned int silent_size;
  97. /* PCM parameters */
  98. unsigned int pcm_period_size;
  99. unsigned int pcm_bps; /* bytes per second */
  100. unsigned int pcm_salign; /* bytes per sample * channels */
  101. unsigned int pcm_rate_shift; /* rate shift value */
  102. /* flags */
  103. unsigned int period_update_pending :1;
  104. /* timer stuff */
  105. unsigned int irq_pos; /* fractional IRQ position */
  106. unsigned int period_size_frac;
  107. unsigned long last_jiffies;
  108. struct timer_list timer;
  109. };
  110. static struct platform_device *devices[SNDRV_CARDS];
  111. static inline unsigned int byte_pos(struct loopback_pcm *dpcm, unsigned int x)
  112. {
  113. if (dpcm->pcm_rate_shift == NO_PITCH) {
  114. x /= HZ;
  115. } else {
  116. x = div_u64(NO_PITCH * (unsigned long long)x,
  117. HZ * (unsigned long long)dpcm->pcm_rate_shift);
  118. }
  119. return x - (x % dpcm->pcm_salign);
  120. }
  121. static inline unsigned int frac_pos(struct loopback_pcm *dpcm, unsigned int x)
  122. {
  123. if (dpcm->pcm_rate_shift == NO_PITCH) { /* no pitch */
  124. return x * HZ;
  125. } else {
  126. x = div_u64(dpcm->pcm_rate_shift * (unsigned long long)x * HZ,
  127. NO_PITCH);
  128. }
  129. return x;
  130. }
  131. static inline struct loopback_setup *get_setup(struct loopback_pcm *dpcm)
  132. {
  133. int device = dpcm->substream->pstr->pcm->device;
  134. if (dpcm->substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  135. device ^= 1;
  136. return &dpcm->loopback->setup[dpcm->substream->number][device];
  137. }
  138. static inline unsigned int get_notify(struct loopback_pcm *dpcm)
  139. {
  140. return get_setup(dpcm)->notify;
  141. }
  142. static inline unsigned int get_rate_shift(struct loopback_pcm *dpcm)
  143. {
  144. return get_setup(dpcm)->rate_shift;
  145. }
  146. static void loopback_timer_start(struct loopback_pcm *dpcm)
  147. {
  148. unsigned long tick;
  149. unsigned int rate_shift = get_rate_shift(dpcm);
  150. if (rate_shift != dpcm->pcm_rate_shift) {
  151. dpcm->pcm_rate_shift = rate_shift;
  152. dpcm->period_size_frac = frac_pos(dpcm, dpcm->pcm_period_size);
  153. }
  154. tick = dpcm->period_size_frac - dpcm->irq_pos;
  155. tick = (tick + dpcm->pcm_bps - 1) / dpcm->pcm_bps;
  156. dpcm->timer.expires = jiffies + tick;
  157. add_timer(&dpcm->timer);
  158. }
  159. static inline void loopback_timer_stop(struct loopback_pcm *dpcm)
  160. {
  161. del_timer(&dpcm->timer);
  162. }
  163. #define CABLE_VALID_PLAYBACK (1 << SNDRV_PCM_STREAM_PLAYBACK)
  164. #define CABLE_VALID_CAPTURE (1 << SNDRV_PCM_STREAM_CAPTURE)
  165. #define CABLE_VALID_BOTH (CABLE_VALID_PLAYBACK|CABLE_VALID_CAPTURE)
  166. static int loopback_check_format(struct loopback_cable *cable, int stream)
  167. {
  168. struct snd_pcm_runtime *runtime;
  169. struct loopback_setup *setup;
  170. struct snd_card *card;
  171. int check;
  172. if (cable->valid != CABLE_VALID_BOTH) {
  173. if (stream == SNDRV_PCM_STREAM_PLAYBACK)
  174. goto __notify;
  175. return 0;
  176. }
  177. runtime = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]->
  178. substream->runtime;
  179. check = cable->hw.formats != (1ULL << runtime->format) ||
  180. cable->hw.rate_min != runtime->rate ||
  181. cable->hw.rate_max != runtime->rate ||
  182. cable->hw.channels_min != runtime->channels ||
  183. cable->hw.channels_max != runtime->channels;
  184. if (!check)
  185. return 0;
  186. if (stream == SNDRV_PCM_STREAM_CAPTURE) {
  187. return -EIO;
  188. } else {
  189. snd_pcm_stop(cable->streams[SNDRV_PCM_STREAM_CAPTURE]->
  190. substream, SNDRV_PCM_STATE_DRAINING);
  191. __notify:
  192. runtime = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]->
  193. substream->runtime;
  194. setup = get_setup(cable->streams[SNDRV_PCM_STREAM_PLAYBACK]);
  195. card = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]->loopback->card;
  196. if (setup->format != runtime->format) {
  197. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
  198. &setup->format_id);
  199. setup->format = runtime->format;
  200. }
  201. if (setup->rate != runtime->rate) {
  202. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
  203. &setup->rate_id);
  204. setup->rate = runtime->rate;
  205. }
  206. if (setup->channels != runtime->channels) {
  207. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
  208. &setup->channels_id);
  209. setup->channels = runtime->channels;
  210. }
  211. }
  212. return 0;
  213. }
  214. static void loopback_active_notify(struct loopback_pcm *dpcm)
  215. {
  216. snd_ctl_notify(dpcm->loopback->card,
  217. SNDRV_CTL_EVENT_MASK_VALUE,
  218. &get_setup(dpcm)->active_id);
  219. }
  220. static int loopback_trigger(struct snd_pcm_substream *substream, int cmd)
  221. {
  222. struct snd_pcm_runtime *runtime = substream->runtime;
  223. struct loopback_pcm *dpcm = runtime->private_data;
  224. struct loopback_cable *cable = dpcm->cable;
  225. int err;
  226. switch (cmd) {
  227. case SNDRV_PCM_TRIGGER_START:
  228. err = loopback_check_format(cable, substream->stream);
  229. if (err < 0)
  230. return err;
  231. dpcm->last_jiffies = jiffies;
  232. dpcm->pcm_rate_shift = 0;
  233. loopback_timer_start(dpcm);
  234. cable->running |= (1 << substream->stream);
  235. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  236. loopback_active_notify(dpcm);
  237. break;
  238. case SNDRV_PCM_TRIGGER_STOP:
  239. cable->running &= ~(1 << substream->stream);
  240. loopback_timer_stop(dpcm);
  241. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  242. loopback_active_notify(dpcm);
  243. break;
  244. default:
  245. return -EINVAL;
  246. }
  247. return 0;
  248. }
  249. static int loopback_prepare(struct snd_pcm_substream *substream)
  250. {
  251. struct snd_pcm_runtime *runtime = substream->runtime;
  252. struct loopback_pcm *dpcm = runtime->private_data;
  253. struct loopback_cable *cable = dpcm->cable;
  254. unsigned int bps, salign;
  255. salign = (snd_pcm_format_width(runtime->format) *
  256. runtime->channels) / 8;
  257. bps = salign * runtime->rate;
  258. if (bps <= 0 || salign <= 0)
  259. return -EINVAL;
  260. dpcm->buf_pos = 0;
  261. dpcm->pcm_buffer_size = frames_to_bytes(runtime, runtime->buffer_size);
  262. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
  263. /* clear capture buffer */
  264. dpcm->silent_size = dpcm->pcm_buffer_size;
  265. snd_pcm_format_set_silence(runtime->format, runtime->dma_area,
  266. runtime->buffer_size * runtime->channels);
  267. }
  268. dpcm->irq_pos = 0;
  269. dpcm->period_update_pending = 0;
  270. dpcm->pcm_bps = bps;
  271. dpcm->pcm_salign = salign;
  272. dpcm->pcm_period_size = frames_to_bytes(runtime, runtime->period_size);
  273. mutex_lock(&dpcm->loopback->cable_lock);
  274. if (!(cable->valid & ~(1 << substream->stream))) {
  275. cable->hw.formats = (1ULL << runtime->format);
  276. cable->hw.rate_min = runtime->rate;
  277. cable->hw.rate_max = runtime->rate;
  278. cable->hw.channels_min = runtime->channels;
  279. cable->hw.channels_max = runtime->channels;
  280. }
  281. cable->valid |= 1 << substream->stream;
  282. mutex_unlock(&dpcm->loopback->cable_lock);
  283. return 0;
  284. }
  285. static void clear_capture_buf(struct loopback_pcm *dpcm, unsigned int bytes)
  286. {
  287. struct snd_pcm_runtime *runtime = dpcm->substream->runtime;
  288. char *dst = runtime->dma_area;
  289. unsigned int dst_off = dpcm->buf_pos;
  290. if (dpcm->silent_size >= dpcm->pcm_buffer_size)
  291. return;
  292. if (dpcm->silent_size + bytes > dpcm->pcm_buffer_size)
  293. bytes = dpcm->pcm_buffer_size - dpcm->silent_size;
  294. for (;;) {
  295. unsigned int size = bytes;
  296. if (dst_off + size > dpcm->pcm_buffer_size)
  297. size = dpcm->pcm_buffer_size - dst_off;
  298. snd_pcm_format_set_silence(runtime->format, dst + dst_off,
  299. bytes_to_frames(runtime, size) *
  300. runtime->channels);
  301. dpcm->silent_size += size;
  302. bytes -= size;
  303. if (!bytes)
  304. break;
  305. dst_off = 0;
  306. }
  307. }
  308. static void copy_play_buf(struct loopback_pcm *play,
  309. struct loopback_pcm *capt,
  310. unsigned int bytes)
  311. {
  312. struct snd_pcm_runtime *runtime = play->substream->runtime;
  313. char *src = runtime->dma_area;
  314. char *dst = capt->substream->runtime->dma_area;
  315. unsigned int src_off = play->buf_pos;
  316. unsigned int dst_off = capt->buf_pos;
  317. unsigned int clear_bytes = 0;
  318. /* check if playback is draining, trim the capture copy size
  319. * when our pointer is at the end of playback ring buffer */
  320. if (runtime->status->state == SNDRV_PCM_STATE_DRAINING &&
  321. snd_pcm_playback_hw_avail(runtime) < runtime->buffer_size) {
  322. snd_pcm_uframes_t appl_ptr, appl_ptr1, diff;
  323. appl_ptr = appl_ptr1 = runtime->control->appl_ptr;
  324. appl_ptr1 -= appl_ptr1 % runtime->buffer_size;
  325. appl_ptr1 += play->buf_pos / play->pcm_salign;
  326. if (appl_ptr < appl_ptr1)
  327. appl_ptr1 -= runtime->buffer_size;
  328. diff = (appl_ptr - appl_ptr1) * play->pcm_salign;
  329. if (diff < bytes) {
  330. clear_bytes = bytes - diff;
  331. bytes = diff;
  332. }
  333. }
  334. for (;;) {
  335. unsigned int size = bytes;
  336. if (src_off + size > play->pcm_buffer_size)
  337. size = play->pcm_buffer_size - src_off;
  338. if (dst_off + size > capt->pcm_buffer_size)
  339. size = capt->pcm_buffer_size - dst_off;
  340. memcpy(dst + dst_off, src + src_off, size);
  341. capt->silent_size = 0;
  342. bytes -= size;
  343. if (!bytes)
  344. break;
  345. src_off = (src_off + size) % play->pcm_buffer_size;
  346. dst_off = (dst_off + size) % capt->pcm_buffer_size;
  347. }
  348. if (clear_bytes > 0) {
  349. clear_capture_buf(capt, clear_bytes);
  350. capt->silent_size = 0;
  351. }
  352. }
  353. #define BYTEPOS_UPDATE_POSONLY 0
  354. #define BYTEPOS_UPDATE_CLEAR 1
  355. #define BYTEPOS_UPDATE_COPY 2
  356. static void loopback_bytepos_update(struct loopback_pcm *dpcm,
  357. unsigned int delta,
  358. unsigned int cmd)
  359. {
  360. unsigned int count;
  361. unsigned long last_pos;
  362. last_pos = byte_pos(dpcm, dpcm->irq_pos);
  363. dpcm->irq_pos += delta * dpcm->pcm_bps;
  364. count = byte_pos(dpcm, dpcm->irq_pos) - last_pos;
  365. if (!count)
  366. return;
  367. if (cmd == BYTEPOS_UPDATE_CLEAR)
  368. clear_capture_buf(dpcm, count);
  369. else if (cmd == BYTEPOS_UPDATE_COPY)
  370. copy_play_buf(dpcm->cable->streams[SNDRV_PCM_STREAM_PLAYBACK],
  371. dpcm->cable->streams[SNDRV_PCM_STREAM_CAPTURE],
  372. count);
  373. dpcm->buf_pos += count;
  374. dpcm->buf_pos %= dpcm->pcm_buffer_size;
  375. if (dpcm->irq_pos >= dpcm->period_size_frac) {
  376. dpcm->irq_pos %= dpcm->period_size_frac;
  377. dpcm->period_update_pending = 1;
  378. }
  379. }
  380. static void loopback_pos_update(struct loopback_cable *cable)
  381. {
  382. struct loopback_pcm *dpcm_play =
  383. cable->streams[SNDRV_PCM_STREAM_PLAYBACK];
  384. struct loopback_pcm *dpcm_capt =
  385. cable->streams[SNDRV_PCM_STREAM_CAPTURE];
  386. unsigned long delta_play = 0, delta_capt = 0;
  387. spin_lock(&cable->lock);
  388. if (cable->running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) {
  389. delta_play = jiffies - dpcm_play->last_jiffies;
  390. dpcm_play->last_jiffies += delta_play;
  391. }
  392. if (cable->running & (1 << SNDRV_PCM_STREAM_CAPTURE)) {
  393. delta_capt = jiffies - dpcm_capt->last_jiffies;
  394. dpcm_capt->last_jiffies += delta_capt;
  395. }
  396. if (delta_play == 0 && delta_capt == 0) {
  397. spin_unlock(&cable->lock);
  398. return;
  399. }
  400. if (delta_play > delta_capt) {
  401. loopback_bytepos_update(dpcm_play, delta_play - delta_capt,
  402. BYTEPOS_UPDATE_POSONLY);
  403. delta_play = delta_capt;
  404. } else if (delta_play < delta_capt) {
  405. loopback_bytepos_update(dpcm_capt, delta_capt - delta_play,
  406. BYTEPOS_UPDATE_CLEAR);
  407. delta_capt = delta_play;
  408. }
  409. if (delta_play == 0 && delta_capt == 0) {
  410. spin_unlock(&cable->lock);
  411. return;
  412. }
  413. /* note delta_capt == delta_play at this moment */
  414. loopback_bytepos_update(dpcm_capt, delta_capt, BYTEPOS_UPDATE_COPY);
  415. loopback_bytepos_update(dpcm_play, delta_play, BYTEPOS_UPDATE_POSONLY);
  416. spin_unlock(&cable->lock);
  417. }
  418. static void loopback_timer_function(unsigned long data)
  419. {
  420. struct loopback_pcm *dpcm = (struct loopback_pcm *)data;
  421. int stream;
  422. loopback_pos_update(dpcm->cable);
  423. stream = dpcm->substream->stream;
  424. if (dpcm->cable->running & (1 << stream))
  425. loopback_timer_start(dpcm);
  426. if (dpcm->period_update_pending) {
  427. dpcm->period_update_pending = 0;
  428. if (dpcm->cable->running & (1 << stream))
  429. snd_pcm_period_elapsed(dpcm->substream);
  430. }
  431. }
  432. static snd_pcm_uframes_t loopback_pointer(struct snd_pcm_substream *substream)
  433. {
  434. struct snd_pcm_runtime *runtime = substream->runtime;
  435. struct loopback_pcm *dpcm = runtime->private_data;
  436. loopback_pos_update(dpcm->cable);
  437. return bytes_to_frames(runtime, dpcm->buf_pos);
  438. }
  439. static struct snd_pcm_hardware loopback_pcm_hardware =
  440. {
  441. .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_MMAP |
  442. SNDRV_PCM_INFO_MMAP_VALID),
  443. .formats = (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE |
  444. SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE |
  445. SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE),
  446. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_192000,
  447. .rate_min = 8000,
  448. .rate_max = 192000,
  449. .channels_min = 1,
  450. .channels_max = 32,
  451. .buffer_bytes_max = 2 * 1024 * 1024,
  452. .period_bytes_min = 64,
  453. .period_bytes_max = 2 * 1024 * 1024,
  454. .periods_min = 1,
  455. .periods_max = 1024,
  456. .fifo_size = 0,
  457. };
  458. static void loopback_runtime_free(struct snd_pcm_runtime *runtime)
  459. {
  460. struct loopback_pcm *dpcm = runtime->private_data;
  461. kfree(dpcm);
  462. }
  463. static int loopback_hw_params(struct snd_pcm_substream *substream,
  464. struct snd_pcm_hw_params *params)
  465. {
  466. return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
  467. }
  468. static int loopback_hw_free(struct snd_pcm_substream *substream)
  469. {
  470. struct snd_pcm_runtime *runtime = substream->runtime;
  471. struct loopback_pcm *dpcm = runtime->private_data;
  472. struct loopback_cable *cable = dpcm->cable;
  473. mutex_lock(&dpcm->loopback->cable_lock);
  474. cable->valid &= ~(1 << substream->stream);
  475. mutex_unlock(&dpcm->loopback->cable_lock);
  476. return snd_pcm_lib_free_pages(substream);
  477. }
  478. static unsigned int get_cable_index(struct snd_pcm_substream *substream)
  479. {
  480. if (!substream->pcm->device)
  481. return substream->stream;
  482. else
  483. return !substream->stream;
  484. }
  485. static int loopback_open(struct snd_pcm_substream *substream)
  486. {
  487. struct snd_pcm_runtime *runtime = substream->runtime;
  488. struct loopback *loopback = substream->private_data;
  489. struct loopback_pcm *dpcm;
  490. struct loopback_cable *cable;
  491. int err = 0;
  492. int dev = get_cable_index(substream);
  493. mutex_lock(&loopback->cable_lock);
  494. dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
  495. if (!dpcm) {
  496. err = -ENOMEM;
  497. goto unlock;
  498. }
  499. dpcm->loopback = loopback;
  500. dpcm->substream = substream;
  501. setup_timer(&dpcm->timer, loopback_timer_function,
  502. (unsigned long)dpcm);
  503. cable = loopback->cables[substream->number][dev];
  504. if (!cable) {
  505. cable = kzalloc(sizeof(*cable), GFP_KERNEL);
  506. if (!cable) {
  507. kfree(dpcm);
  508. err = -ENOMEM;
  509. goto unlock;
  510. }
  511. spin_lock_init(&cable->lock);
  512. cable->hw = loopback_pcm_hardware;
  513. loopback->cables[substream->number][dev] = cable;
  514. }
  515. dpcm->cable = cable;
  516. cable->streams[substream->stream] = dpcm;
  517. snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
  518. runtime->private_data = dpcm;
  519. runtime->private_free = loopback_runtime_free;
  520. if (get_notify(dpcm) &&
  521. substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  522. runtime->hw = loopback_pcm_hardware;
  523. } else {
  524. runtime->hw = cable->hw;
  525. }
  526. unlock:
  527. mutex_unlock(&loopback->cable_lock);
  528. return err;
  529. }
  530. static int loopback_close(struct snd_pcm_substream *substream)
  531. {
  532. struct loopback *loopback = substream->private_data;
  533. struct loopback_pcm *dpcm = substream->runtime->private_data;
  534. struct loopback_cable *cable;
  535. int dev = get_cable_index(substream);
  536. loopback_timer_stop(dpcm);
  537. mutex_lock(&loopback->cable_lock);
  538. cable = loopback->cables[substream->number][dev];
  539. if (cable->streams[!substream->stream]) {
  540. /* other stream is still alive */
  541. cable->streams[substream->stream] = NULL;
  542. } else {
  543. /* free the cable */
  544. loopback->cables[substream->number][dev] = NULL;
  545. kfree(cable);
  546. }
  547. mutex_unlock(&loopback->cable_lock);
  548. return 0;
  549. }
  550. static struct snd_pcm_ops loopback_playback_ops = {
  551. .open = loopback_open,
  552. .close = loopback_close,
  553. .ioctl = snd_pcm_lib_ioctl,
  554. .hw_params = loopback_hw_params,
  555. .hw_free = loopback_hw_free,
  556. .prepare = loopback_prepare,
  557. .trigger = loopback_trigger,
  558. .pointer = loopback_pointer,
  559. };
  560. static struct snd_pcm_ops loopback_capture_ops = {
  561. .open = loopback_open,
  562. .close = loopback_close,
  563. .ioctl = snd_pcm_lib_ioctl,
  564. .hw_params = loopback_hw_params,
  565. .hw_free = loopback_hw_free,
  566. .prepare = loopback_prepare,
  567. .trigger = loopback_trigger,
  568. .pointer = loopback_pointer,
  569. };
  570. static int __devinit loopback_pcm_new(struct loopback *loopback,
  571. int device, int substreams)
  572. {
  573. struct snd_pcm *pcm;
  574. int err;
  575. err = snd_pcm_new(loopback->card, "Loopback PCM", device,
  576. substreams, substreams, &pcm);
  577. if (err < 0)
  578. return err;
  579. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &loopback_playback_ops);
  580. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &loopback_capture_ops);
  581. pcm->private_data = loopback;
  582. pcm->info_flags = 0;
  583. strcpy(pcm->name, "Loopback PCM");
  584. loopback->pcm[device] = pcm;
  585. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
  586. snd_dma_continuous_data(GFP_KERNEL),
  587. 0, 2 * 1024 * 1024);
  588. return 0;
  589. }
  590. static int loopback_rate_shift_info(struct snd_kcontrol *kcontrol,
  591. struct snd_ctl_elem_info *uinfo)
  592. {
  593. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  594. uinfo->count = 1;
  595. uinfo->value.integer.min = 80000;
  596. uinfo->value.integer.max = 120000;
  597. uinfo->value.integer.step = 1;
  598. return 0;
  599. }
  600. static int loopback_rate_shift_get(struct snd_kcontrol *kcontrol,
  601. struct snd_ctl_elem_value *ucontrol)
  602. {
  603. struct loopback *loopback = snd_kcontrol_chip(kcontrol);
  604. ucontrol->value.integer.value[0] =
  605. loopback->setup[kcontrol->id.subdevice]
  606. [kcontrol->id.device].rate_shift;
  607. return 0;
  608. }
  609. static int loopback_rate_shift_put(struct snd_kcontrol *kcontrol,
  610. struct snd_ctl_elem_value *ucontrol)
  611. {
  612. struct loopback *loopback = snd_kcontrol_chip(kcontrol);
  613. unsigned int val;
  614. int change = 0;
  615. val = ucontrol->value.integer.value[0];
  616. if (val < 80000)
  617. val = 80000;
  618. if (val > 120000)
  619. val = 120000;
  620. mutex_lock(&loopback->cable_lock);
  621. if (val != loopback->setup[kcontrol->id.subdevice]
  622. [kcontrol->id.device].rate_shift) {
  623. loopback->setup[kcontrol->id.subdevice]
  624. [kcontrol->id.device].rate_shift = val;
  625. change = 1;
  626. }
  627. mutex_unlock(&loopback->cable_lock);
  628. return change;
  629. }
  630. static int loopback_notify_get(struct snd_kcontrol *kcontrol,
  631. struct snd_ctl_elem_value *ucontrol)
  632. {
  633. struct loopback *loopback = snd_kcontrol_chip(kcontrol);
  634. ucontrol->value.integer.value[0] =
  635. loopback->setup[kcontrol->id.subdevice]
  636. [kcontrol->id.device].notify;
  637. return 0;
  638. }
  639. static int loopback_notify_put(struct snd_kcontrol *kcontrol,
  640. struct snd_ctl_elem_value *ucontrol)
  641. {
  642. struct loopback *loopback = snd_kcontrol_chip(kcontrol);
  643. unsigned int val;
  644. int change = 0;
  645. val = ucontrol->value.integer.value[0] ? 1 : 0;
  646. if (val != loopback->setup[kcontrol->id.subdevice]
  647. [kcontrol->id.device].notify) {
  648. loopback->setup[kcontrol->id.subdevice]
  649. [kcontrol->id.device].notify = val;
  650. change = 1;
  651. }
  652. return change;
  653. }
  654. static int loopback_active_get(struct snd_kcontrol *kcontrol,
  655. struct snd_ctl_elem_value *ucontrol)
  656. {
  657. struct loopback *loopback = snd_kcontrol_chip(kcontrol);
  658. struct loopback_cable *cable = loopback->cables
  659. [kcontrol->id.subdevice][kcontrol->id.device ^ 1];
  660. unsigned int val = 0;
  661. if (cable != NULL)
  662. val = (cable->running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) ?
  663. 1 : 0;
  664. ucontrol->value.integer.value[0] = val;
  665. return 0;
  666. }
  667. static int loopback_format_info(struct snd_kcontrol *kcontrol,
  668. struct snd_ctl_elem_info *uinfo)
  669. {
  670. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  671. uinfo->count = 1;
  672. uinfo->value.integer.min = 0;
  673. uinfo->value.integer.max = SNDRV_PCM_FORMAT_LAST;
  674. uinfo->value.integer.step = 1;
  675. return 0;
  676. }
  677. static int loopback_format_get(struct snd_kcontrol *kcontrol,
  678. struct snd_ctl_elem_value *ucontrol)
  679. {
  680. struct loopback *loopback = snd_kcontrol_chip(kcontrol);
  681. ucontrol->value.integer.value[0] =
  682. loopback->setup[kcontrol->id.subdevice]
  683. [kcontrol->id.device].format;
  684. return 0;
  685. }
  686. static int loopback_rate_info(struct snd_kcontrol *kcontrol,
  687. struct snd_ctl_elem_info *uinfo)
  688. {
  689. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  690. uinfo->count = 1;
  691. uinfo->value.integer.min = 0;
  692. uinfo->value.integer.max = 192000;
  693. uinfo->value.integer.step = 1;
  694. return 0;
  695. }
  696. static int loopback_rate_get(struct snd_kcontrol *kcontrol,
  697. struct snd_ctl_elem_value *ucontrol)
  698. {
  699. struct loopback *loopback = snd_kcontrol_chip(kcontrol);
  700. ucontrol->value.integer.value[0] =
  701. loopback->setup[kcontrol->id.subdevice]
  702. [kcontrol->id.device].rate;
  703. return 0;
  704. }
  705. static int loopback_channels_info(struct snd_kcontrol *kcontrol,
  706. struct snd_ctl_elem_info *uinfo)
  707. {
  708. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  709. uinfo->count = 1;
  710. uinfo->value.integer.min = 1;
  711. uinfo->value.integer.max = 1024;
  712. uinfo->value.integer.step = 1;
  713. return 0;
  714. }
  715. static int loopback_channels_get(struct snd_kcontrol *kcontrol,
  716. struct snd_ctl_elem_value *ucontrol)
  717. {
  718. struct loopback *loopback = snd_kcontrol_chip(kcontrol);
  719. ucontrol->value.integer.value[0] =
  720. loopback->setup[kcontrol->id.subdevice]
  721. [kcontrol->id.device].channels;
  722. return 0;
  723. }
  724. static struct snd_kcontrol_new loopback_controls[] __devinitdata = {
  725. {
  726. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  727. .name = "PCM Rate Shift 100000",
  728. .info = loopback_rate_shift_info,
  729. .get = loopback_rate_shift_get,
  730. .put = loopback_rate_shift_put,
  731. },
  732. {
  733. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  734. .name = "PCM Notify",
  735. .info = snd_ctl_boolean_mono_info,
  736. .get = loopback_notify_get,
  737. .put = loopback_notify_put,
  738. },
  739. #define ACTIVE_IDX 2
  740. {
  741. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  742. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  743. .name = "PCM Slave Active",
  744. .info = snd_ctl_boolean_mono_info,
  745. .get = loopback_active_get,
  746. },
  747. #define FORMAT_IDX 3
  748. {
  749. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  750. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  751. .name = "PCM Slave Format",
  752. .info = loopback_format_info,
  753. .get = loopback_format_get
  754. },
  755. #define RATE_IDX 4
  756. {
  757. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  758. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  759. .name = "PCM Slave Rate",
  760. .info = loopback_rate_info,
  761. .get = loopback_rate_get
  762. },
  763. #define CHANNELS_IDX 5
  764. {
  765. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  766. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  767. .name = "PCM Slave Channels",
  768. .info = loopback_channels_info,
  769. .get = loopback_channels_get
  770. }
  771. };
  772. static int __devinit loopback_mixer_new(struct loopback *loopback, int notify)
  773. {
  774. struct snd_card *card = loopback->card;
  775. struct snd_pcm *pcm;
  776. struct snd_kcontrol *kctl;
  777. struct loopback_setup *setup;
  778. int err, dev, substr, substr_count, idx;
  779. strcpy(card->mixername, "Loopback Mixer");
  780. for (dev = 0; dev < 2; dev++) {
  781. pcm = loopback->pcm[dev];
  782. substr_count =
  783. pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count;
  784. for (substr = 0; substr < substr_count; substr++) {
  785. setup = &loopback->setup[substr][dev];
  786. setup->notify = notify;
  787. setup->rate_shift = NO_PITCH;
  788. setup->format = SNDRV_PCM_FORMAT_S16_LE;
  789. setup->rate = 48000;
  790. setup->channels = 2;
  791. for (idx = 0; idx < ARRAY_SIZE(loopback_controls);
  792. idx++) {
  793. kctl = snd_ctl_new1(&loopback_controls[idx],
  794. loopback);
  795. if (!kctl)
  796. return -ENOMEM;
  797. kctl->id.device = dev;
  798. kctl->id.subdevice = substr;
  799. switch (idx) {
  800. case ACTIVE_IDX:
  801. setup->active_id = kctl->id;
  802. break;
  803. case FORMAT_IDX:
  804. setup->format_id = kctl->id;
  805. break;
  806. case RATE_IDX:
  807. setup->rate_id = kctl->id;
  808. break;
  809. case CHANNELS_IDX:
  810. setup->channels_id = kctl->id;
  811. break;
  812. default:
  813. break;
  814. }
  815. err = snd_ctl_add(card, kctl);
  816. if (err < 0)
  817. return err;
  818. }
  819. }
  820. }
  821. return 0;
  822. }
  823. static int __devinit loopback_probe(struct platform_device *devptr)
  824. {
  825. struct snd_card *card;
  826. struct loopback *loopback;
  827. int dev = devptr->id;
  828. int err;
  829. err = snd_card_create(index[dev], id[dev], THIS_MODULE,
  830. sizeof(struct loopback), &card);
  831. if (err < 0)
  832. return err;
  833. loopback = card->private_data;
  834. if (pcm_substreams[dev] < 1)
  835. pcm_substreams[dev] = 1;
  836. if (pcm_substreams[dev] > MAX_PCM_SUBSTREAMS)
  837. pcm_substreams[dev] = MAX_PCM_SUBSTREAMS;
  838. loopback->card = card;
  839. mutex_init(&loopback->cable_lock);
  840. err = loopback_pcm_new(loopback, 0, pcm_substreams[dev]);
  841. if (err < 0)
  842. goto __nodev;
  843. err = loopback_pcm_new(loopback, 1, pcm_substreams[dev]);
  844. if (err < 0)
  845. goto __nodev;
  846. err = loopback_mixer_new(loopback, pcm_notify[dev] ? 1 : 0);
  847. if (err < 0)
  848. goto __nodev;
  849. strcpy(card->driver, "Loopback");
  850. strcpy(card->shortname, "Loopback");
  851. sprintf(card->longname, "Loopback %i", dev + 1);
  852. err = snd_card_register(card);
  853. if (!err) {
  854. platform_set_drvdata(devptr, card);
  855. return 0;
  856. }
  857. __nodev:
  858. snd_card_free(card);
  859. return err;
  860. }
  861. static int __devexit loopback_remove(struct platform_device *devptr)
  862. {
  863. snd_card_free(platform_get_drvdata(devptr));
  864. platform_set_drvdata(devptr, NULL);
  865. return 0;
  866. }
  867. #ifdef CONFIG_PM
  868. static int loopback_suspend(struct platform_device *pdev,
  869. pm_message_t state)
  870. {
  871. struct snd_card *card = platform_get_drvdata(pdev);
  872. struct loopback *loopback = card->private_data;
  873. snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
  874. snd_pcm_suspend_all(loopback->pcm[0]);
  875. snd_pcm_suspend_all(loopback->pcm[1]);
  876. return 0;
  877. }
  878. static int loopback_resume(struct platform_device *pdev)
  879. {
  880. struct snd_card *card = platform_get_drvdata(pdev);
  881. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  882. return 0;
  883. }
  884. #endif
  885. #define SND_LOOPBACK_DRIVER "snd_aloop"
  886. static struct platform_driver loopback_driver = {
  887. .probe = loopback_probe,
  888. .remove = __devexit_p(loopback_remove),
  889. #ifdef CONFIG_PM
  890. .suspend = loopback_suspend,
  891. .resume = loopback_resume,
  892. #endif
  893. .driver = {
  894. .name = SND_LOOPBACK_DRIVER
  895. },
  896. };
  897. static void loopback_unregister_all(void)
  898. {
  899. int i;
  900. for (i = 0; i < ARRAY_SIZE(devices); ++i)
  901. platform_device_unregister(devices[i]);
  902. platform_driver_unregister(&loopback_driver);
  903. }
  904. static int __init alsa_card_loopback_init(void)
  905. {
  906. int i, err, cards;
  907. err = platform_driver_register(&loopback_driver);
  908. if (err < 0)
  909. return err;
  910. cards = 0;
  911. for (i = 0; i < SNDRV_CARDS; i++) {
  912. struct platform_device *device;
  913. if (!enable[i])
  914. continue;
  915. device = platform_device_register_simple(SND_LOOPBACK_DRIVER,
  916. i, NULL, 0);
  917. if (IS_ERR(device))
  918. continue;
  919. if (!platform_get_drvdata(device)) {
  920. platform_device_unregister(device);
  921. continue;
  922. }
  923. devices[i] = device;
  924. cards++;
  925. }
  926. if (!cards) {
  927. #ifdef MODULE
  928. printk(KERN_ERR "aloop: No loopback enabled\n");
  929. #endif
  930. loopback_unregister_all();
  931. return -ENODEV;
  932. }
  933. return 0;
  934. }
  935. static void __exit alsa_card_loopback_exit(void)
  936. {
  937. loopback_unregister_all();
  938. }
  939. module_init(alsa_card_loopback_init)
  940. module_exit(alsa_card_loopback_exit)