aloop.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276
  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. spin_lock(&cable->lock);
  263. cable->pause |= stream;
  264. loopback_timer_stop(dpcm);
  265. spin_unlock(&cable->lock);
  266. break;
  267. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  268. spin_lock(&cable->lock);
  269. dpcm->last_jiffies = jiffies;
  270. cable->pause &= ~stream;
  271. loopback_timer_start(dpcm);
  272. spin_unlock(&cable->lock);
  273. break;
  274. default:
  275. return -EINVAL;
  276. }
  277. return 0;
  278. }
  279. static void params_change_substream(struct loopback_pcm *dpcm,
  280. struct snd_pcm_runtime *runtime)
  281. {
  282. struct snd_pcm_runtime *dst_runtime;
  283. if (dpcm == NULL || dpcm->substream == NULL)
  284. return;
  285. dst_runtime = dpcm->substream->runtime;
  286. if (dst_runtime == NULL)
  287. return;
  288. dst_runtime->hw = dpcm->cable->hw;
  289. }
  290. static void params_change(struct snd_pcm_substream *substream)
  291. {
  292. struct snd_pcm_runtime *runtime = substream->runtime;
  293. struct loopback_pcm *dpcm = runtime->private_data;
  294. struct loopback_cable *cable = dpcm->cable;
  295. cable->hw.formats = (1ULL << runtime->format);
  296. cable->hw.rate_min = runtime->rate;
  297. cable->hw.rate_max = runtime->rate;
  298. cable->hw.channels_min = runtime->channels;
  299. cable->hw.channels_max = runtime->channels;
  300. params_change_substream(cable->streams[SNDRV_PCM_STREAM_PLAYBACK],
  301. runtime);
  302. params_change_substream(cable->streams[SNDRV_PCM_STREAM_CAPTURE],
  303. runtime);
  304. }
  305. static int loopback_prepare(struct snd_pcm_substream *substream)
  306. {
  307. struct snd_pcm_runtime *runtime = substream->runtime;
  308. struct loopback_pcm *dpcm = runtime->private_data;
  309. struct loopback_cable *cable = dpcm->cable;
  310. int bps, salign;
  311. salign = (snd_pcm_format_width(runtime->format) *
  312. runtime->channels) / 8;
  313. bps = salign * runtime->rate;
  314. if (bps <= 0 || salign <= 0)
  315. return -EINVAL;
  316. dpcm->buf_pos = 0;
  317. dpcm->pcm_buffer_size = frames_to_bytes(runtime, runtime->buffer_size);
  318. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
  319. /* clear capture buffer */
  320. dpcm->silent_size = dpcm->pcm_buffer_size;
  321. snd_pcm_format_set_silence(runtime->format, runtime->dma_area,
  322. runtime->buffer_size * runtime->channels);
  323. }
  324. dpcm->irq_pos = 0;
  325. dpcm->period_update_pending = 0;
  326. dpcm->pcm_bps = bps;
  327. dpcm->pcm_salign = salign;
  328. dpcm->pcm_period_size = frames_to_bytes(runtime, runtime->period_size);
  329. mutex_lock(&dpcm->loopback->cable_lock);
  330. if (!(cable->valid & ~(1 << substream->stream)) ||
  331. (get_setup(dpcm)->notify &&
  332. substream->stream == SNDRV_PCM_STREAM_PLAYBACK))
  333. params_change(substream);
  334. cable->valid |= 1 << substream->stream;
  335. mutex_unlock(&dpcm->loopback->cable_lock);
  336. return 0;
  337. }
  338. static void clear_capture_buf(struct loopback_pcm *dpcm, unsigned int bytes)
  339. {
  340. struct snd_pcm_runtime *runtime = dpcm->substream->runtime;
  341. char *dst = runtime->dma_area;
  342. unsigned int dst_off = dpcm->buf_pos;
  343. if (dpcm->silent_size >= dpcm->pcm_buffer_size)
  344. return;
  345. if (dpcm->silent_size + bytes > dpcm->pcm_buffer_size)
  346. bytes = dpcm->pcm_buffer_size - dpcm->silent_size;
  347. for (;;) {
  348. unsigned int size = bytes;
  349. if (dst_off + size > dpcm->pcm_buffer_size)
  350. size = dpcm->pcm_buffer_size - dst_off;
  351. snd_pcm_format_set_silence(runtime->format, dst + dst_off,
  352. bytes_to_frames(runtime, size) *
  353. runtime->channels);
  354. dpcm->silent_size += size;
  355. bytes -= size;
  356. if (!bytes)
  357. break;
  358. dst_off = 0;
  359. }
  360. }
  361. static void copy_play_buf(struct loopback_pcm *play,
  362. struct loopback_pcm *capt,
  363. unsigned int bytes)
  364. {
  365. struct snd_pcm_runtime *runtime = play->substream->runtime;
  366. char *src = runtime->dma_area;
  367. char *dst = capt->substream->runtime->dma_area;
  368. unsigned int src_off = play->buf_pos;
  369. unsigned int dst_off = capt->buf_pos;
  370. unsigned int clear_bytes = 0;
  371. /* check if playback is draining, trim the capture copy size
  372. * when our pointer is at the end of playback ring buffer */
  373. if (runtime->status->state == SNDRV_PCM_STATE_DRAINING &&
  374. snd_pcm_playback_hw_avail(runtime) < runtime->buffer_size) {
  375. snd_pcm_uframes_t appl_ptr, appl_ptr1, diff;
  376. appl_ptr = appl_ptr1 = runtime->control->appl_ptr;
  377. appl_ptr1 -= appl_ptr1 % runtime->buffer_size;
  378. appl_ptr1 += play->buf_pos / play->pcm_salign;
  379. if (appl_ptr < appl_ptr1)
  380. appl_ptr1 -= runtime->buffer_size;
  381. diff = (appl_ptr - appl_ptr1) * play->pcm_salign;
  382. if (diff < bytes) {
  383. clear_bytes = bytes - diff;
  384. bytes = diff;
  385. }
  386. }
  387. for (;;) {
  388. unsigned int size = bytes;
  389. if (src_off + size > play->pcm_buffer_size)
  390. size = play->pcm_buffer_size - src_off;
  391. if (dst_off + size > capt->pcm_buffer_size)
  392. size = capt->pcm_buffer_size - dst_off;
  393. memcpy(dst + dst_off, src + src_off, size);
  394. capt->silent_size = 0;
  395. bytes -= size;
  396. if (!bytes)
  397. break;
  398. src_off = (src_off + size) % play->pcm_buffer_size;
  399. dst_off = (dst_off + size) % capt->pcm_buffer_size;
  400. }
  401. if (clear_bytes > 0) {
  402. clear_capture_buf(capt, clear_bytes);
  403. capt->silent_size = 0;
  404. }
  405. }
  406. static inline unsigned int bytepos_delta(struct loopback_pcm *dpcm,
  407. unsigned int jiffies_delta)
  408. {
  409. unsigned long last_pos;
  410. unsigned int delta;
  411. last_pos = byte_pos(dpcm, dpcm->irq_pos);
  412. dpcm->irq_pos += jiffies_delta * dpcm->pcm_bps;
  413. delta = byte_pos(dpcm, dpcm->irq_pos) - last_pos;
  414. if (delta >= dpcm->last_drift)
  415. delta -= dpcm->last_drift;
  416. dpcm->last_drift = 0;
  417. if (dpcm->irq_pos >= dpcm->period_size_frac) {
  418. dpcm->irq_pos %= dpcm->period_size_frac;
  419. dpcm->period_update_pending = 1;
  420. }
  421. return delta;
  422. }
  423. static inline void bytepos_finish(struct loopback_pcm *dpcm,
  424. unsigned int delta)
  425. {
  426. dpcm->buf_pos += delta;
  427. dpcm->buf_pos %= dpcm->pcm_buffer_size;
  428. }
  429. /* call in cable->lock */
  430. static unsigned int loopback_pos_update(struct loopback_cable *cable)
  431. {
  432. struct loopback_pcm *dpcm_play =
  433. cable->streams[SNDRV_PCM_STREAM_PLAYBACK];
  434. struct loopback_pcm *dpcm_capt =
  435. cable->streams[SNDRV_PCM_STREAM_CAPTURE];
  436. unsigned long delta_play = 0, delta_capt = 0;
  437. unsigned int running, count1, count2;
  438. running = cable->running ^ cable->pause;
  439. if (running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) {
  440. delta_play = jiffies - dpcm_play->last_jiffies;
  441. dpcm_play->last_jiffies += delta_play;
  442. }
  443. if (running & (1 << SNDRV_PCM_STREAM_CAPTURE)) {
  444. delta_capt = jiffies - dpcm_capt->last_jiffies;
  445. dpcm_capt->last_jiffies += delta_capt;
  446. }
  447. if (delta_play == 0 && delta_capt == 0)
  448. goto unlock;
  449. if (delta_play > delta_capt) {
  450. count1 = bytepos_delta(dpcm_play, delta_play - delta_capt);
  451. bytepos_finish(dpcm_play, count1);
  452. delta_play = delta_capt;
  453. } else if (delta_play < delta_capt) {
  454. count1 = bytepos_delta(dpcm_capt, delta_capt - delta_play);
  455. clear_capture_buf(dpcm_capt, count1);
  456. bytepos_finish(dpcm_capt, count1);
  457. delta_capt = delta_play;
  458. }
  459. if (delta_play == 0 && delta_capt == 0)
  460. goto unlock;
  461. /* note delta_capt == delta_play at this moment */
  462. count1 = bytepos_delta(dpcm_play, delta_play);
  463. count2 = bytepos_delta(dpcm_capt, delta_capt);
  464. if (count1 < count2) {
  465. dpcm_capt->last_drift = count2 - count1;
  466. count1 = count2;
  467. } else if (count1 > count2) {
  468. dpcm_play->last_drift = count1 - count2;
  469. }
  470. copy_play_buf(dpcm_play, dpcm_capt, count1);
  471. bytepos_finish(dpcm_play, count1);
  472. bytepos_finish(dpcm_capt, count1);
  473. unlock:
  474. return running;
  475. }
  476. static void loopback_timer_function(unsigned long data)
  477. {
  478. struct loopback_pcm *dpcm = (struct loopback_pcm *)data;
  479. unsigned long flags;
  480. spin_lock_irqsave(&dpcm->cable->lock, flags);
  481. if (loopback_pos_update(dpcm->cable) & (1 << dpcm->substream->stream)) {
  482. loopback_timer_start(dpcm);
  483. if (dpcm->period_update_pending) {
  484. dpcm->period_update_pending = 0;
  485. spin_unlock_irqrestore(&dpcm->cable->lock, flags);
  486. /* need to unlock before calling below */
  487. snd_pcm_period_elapsed(dpcm->substream);
  488. return;
  489. }
  490. }
  491. spin_unlock_irqrestore(&dpcm->cable->lock, flags);
  492. }
  493. static snd_pcm_uframes_t loopback_pointer(struct snd_pcm_substream *substream)
  494. {
  495. struct snd_pcm_runtime *runtime = substream->runtime;
  496. struct loopback_pcm *dpcm = runtime->private_data;
  497. snd_pcm_uframes_t pos;
  498. spin_lock(&dpcm->cable->lock);
  499. loopback_pos_update(dpcm->cable);
  500. pos = dpcm->buf_pos;
  501. spin_unlock(&dpcm->cable->lock);
  502. return bytes_to_frames(runtime, pos);
  503. }
  504. static struct snd_pcm_hardware loopback_pcm_hardware =
  505. {
  506. .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_MMAP |
  507. SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_PAUSE),
  508. .formats = (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE |
  509. SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE |
  510. SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE),
  511. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_192000,
  512. .rate_min = 8000,
  513. .rate_max = 192000,
  514. .channels_min = 1,
  515. .channels_max = 32,
  516. .buffer_bytes_max = 2 * 1024 * 1024,
  517. .period_bytes_min = 64,
  518. /* note check overflow in frac_pos() using pcm_rate_shift before
  519. changing period_bytes_max value */
  520. .period_bytes_max = 1024 * 1024,
  521. .periods_min = 1,
  522. .periods_max = 1024,
  523. .fifo_size = 0,
  524. };
  525. static void loopback_runtime_free(struct snd_pcm_runtime *runtime)
  526. {
  527. struct loopback_pcm *dpcm = runtime->private_data;
  528. kfree(dpcm);
  529. }
  530. static int loopback_hw_params(struct snd_pcm_substream *substream,
  531. struct snd_pcm_hw_params *params)
  532. {
  533. return snd_pcm_lib_alloc_vmalloc_buffer(substream,
  534. params_buffer_bytes(params));
  535. }
  536. static int loopback_hw_free(struct snd_pcm_substream *substream)
  537. {
  538. struct snd_pcm_runtime *runtime = substream->runtime;
  539. struct loopback_pcm *dpcm = runtime->private_data;
  540. struct loopback_cable *cable = dpcm->cable;
  541. mutex_lock(&dpcm->loopback->cable_lock);
  542. cable->valid &= ~(1 << substream->stream);
  543. mutex_unlock(&dpcm->loopback->cable_lock);
  544. return snd_pcm_lib_free_vmalloc_buffer(substream);
  545. }
  546. static unsigned int get_cable_index(struct snd_pcm_substream *substream)
  547. {
  548. if (!substream->pcm->device)
  549. return substream->stream;
  550. else
  551. return !substream->stream;
  552. }
  553. static int rule_format(struct snd_pcm_hw_params *params,
  554. struct snd_pcm_hw_rule *rule)
  555. {
  556. struct snd_pcm_hardware *hw = rule->private;
  557. struct snd_mask *maskp = hw_param_mask(params, rule->var);
  558. maskp->bits[0] &= (u_int32_t)hw->formats;
  559. maskp->bits[1] &= (u_int32_t)(hw->formats >> 32);
  560. memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */
  561. if (! maskp->bits[0] && ! maskp->bits[1])
  562. return -EINVAL;
  563. return 0;
  564. }
  565. static int rule_rate(struct snd_pcm_hw_params *params,
  566. struct snd_pcm_hw_rule *rule)
  567. {
  568. struct snd_pcm_hardware *hw = rule->private;
  569. struct snd_interval t;
  570. t.min = hw->rate_min;
  571. t.max = hw->rate_max;
  572. t.openmin = t.openmax = 0;
  573. t.integer = 0;
  574. return snd_interval_refine(hw_param_interval(params, rule->var), &t);
  575. }
  576. static int rule_channels(struct snd_pcm_hw_params *params,
  577. struct snd_pcm_hw_rule *rule)
  578. {
  579. struct snd_pcm_hardware *hw = rule->private;
  580. struct snd_interval t;
  581. t.min = hw->channels_min;
  582. t.max = hw->channels_max;
  583. t.openmin = t.openmax = 0;
  584. t.integer = 0;
  585. return snd_interval_refine(hw_param_interval(params, rule->var), &t);
  586. }
  587. static int loopback_open(struct snd_pcm_substream *substream)
  588. {
  589. struct snd_pcm_runtime *runtime = substream->runtime;
  590. struct loopback *loopback = substream->private_data;
  591. struct loopback_pcm *dpcm;
  592. struct loopback_cable *cable;
  593. int err = 0;
  594. int dev = get_cable_index(substream);
  595. mutex_lock(&loopback->cable_lock);
  596. dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
  597. if (!dpcm) {
  598. err = -ENOMEM;
  599. goto unlock;
  600. }
  601. dpcm->loopback = loopback;
  602. dpcm->substream = substream;
  603. setup_timer(&dpcm->timer, loopback_timer_function,
  604. (unsigned long)dpcm);
  605. cable = loopback->cables[substream->number][dev];
  606. if (!cable) {
  607. cable = kzalloc(sizeof(*cable), GFP_KERNEL);
  608. if (!cable) {
  609. kfree(dpcm);
  610. err = -ENOMEM;
  611. goto unlock;
  612. }
  613. spin_lock_init(&cable->lock);
  614. cable->hw = loopback_pcm_hardware;
  615. loopback->cables[substream->number][dev] = cable;
  616. }
  617. dpcm->cable = cable;
  618. cable->streams[substream->stream] = dpcm;
  619. snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
  620. /* use dynamic rules based on actual runtime->hw values */
  621. /* note that the default rules created in the PCM midlevel code */
  622. /* are cached -> they do not reflect the actual state */
  623. err = snd_pcm_hw_rule_add(runtime, 0,
  624. SNDRV_PCM_HW_PARAM_FORMAT,
  625. rule_format, &runtime->hw,
  626. SNDRV_PCM_HW_PARAM_FORMAT, -1);
  627. if (err < 0)
  628. goto unlock;
  629. err = snd_pcm_hw_rule_add(runtime, 0,
  630. SNDRV_PCM_HW_PARAM_RATE,
  631. rule_rate, &runtime->hw,
  632. SNDRV_PCM_HW_PARAM_RATE, -1);
  633. if (err < 0)
  634. goto unlock;
  635. err = snd_pcm_hw_rule_add(runtime, 0,
  636. SNDRV_PCM_HW_PARAM_CHANNELS,
  637. rule_channels, &runtime->hw,
  638. SNDRV_PCM_HW_PARAM_CHANNELS, -1);
  639. if (err < 0)
  640. goto unlock;
  641. runtime->private_data = dpcm;
  642. runtime->private_free = loopback_runtime_free;
  643. if (get_notify(dpcm))
  644. runtime->hw = loopback_pcm_hardware;
  645. else
  646. runtime->hw = cable->hw;
  647. unlock:
  648. mutex_unlock(&loopback->cable_lock);
  649. return err;
  650. }
  651. static int loopback_close(struct snd_pcm_substream *substream)
  652. {
  653. struct loopback *loopback = substream->private_data;
  654. struct loopback_pcm *dpcm = substream->runtime->private_data;
  655. struct loopback_cable *cable;
  656. int dev = get_cable_index(substream);
  657. loopback_timer_stop(dpcm);
  658. mutex_lock(&loopback->cable_lock);
  659. cable = loopback->cables[substream->number][dev];
  660. if (cable->streams[!substream->stream]) {
  661. /* other stream is still alive */
  662. cable->streams[substream->stream] = NULL;
  663. } else {
  664. /* free the cable */
  665. loopback->cables[substream->number][dev] = NULL;
  666. kfree(cable);
  667. }
  668. mutex_unlock(&loopback->cable_lock);
  669. return 0;
  670. }
  671. static struct snd_pcm_ops loopback_playback_ops = {
  672. .open = loopback_open,
  673. .close = loopback_close,
  674. .ioctl = snd_pcm_lib_ioctl,
  675. .hw_params = loopback_hw_params,
  676. .hw_free = loopback_hw_free,
  677. .prepare = loopback_prepare,
  678. .trigger = loopback_trigger,
  679. .pointer = loopback_pointer,
  680. .page = snd_pcm_lib_get_vmalloc_page,
  681. .mmap = snd_pcm_lib_mmap_vmalloc,
  682. };
  683. static struct snd_pcm_ops loopback_capture_ops = {
  684. .open = loopback_open,
  685. .close = loopback_close,
  686. .ioctl = snd_pcm_lib_ioctl,
  687. .hw_params = loopback_hw_params,
  688. .hw_free = loopback_hw_free,
  689. .prepare = loopback_prepare,
  690. .trigger = loopback_trigger,
  691. .pointer = loopback_pointer,
  692. .page = snd_pcm_lib_get_vmalloc_page,
  693. .mmap = snd_pcm_lib_mmap_vmalloc,
  694. };
  695. static int loopback_pcm_new(struct loopback *loopback,
  696. int device, int substreams)
  697. {
  698. struct snd_pcm *pcm;
  699. int err;
  700. err = snd_pcm_new(loopback->card, "Loopback PCM", device,
  701. substreams, substreams, &pcm);
  702. if (err < 0)
  703. return err;
  704. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &loopback_playback_ops);
  705. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &loopback_capture_ops);
  706. pcm->private_data = loopback;
  707. pcm->info_flags = 0;
  708. strcpy(pcm->name, "Loopback PCM");
  709. loopback->pcm[device] = pcm;
  710. return 0;
  711. }
  712. static int loopback_rate_shift_info(struct snd_kcontrol *kcontrol,
  713. struct snd_ctl_elem_info *uinfo)
  714. {
  715. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  716. uinfo->count = 1;
  717. uinfo->value.integer.min = 80000;
  718. uinfo->value.integer.max = 120000;
  719. uinfo->value.integer.step = 1;
  720. return 0;
  721. }
  722. static int loopback_rate_shift_get(struct snd_kcontrol *kcontrol,
  723. struct snd_ctl_elem_value *ucontrol)
  724. {
  725. struct loopback *loopback = snd_kcontrol_chip(kcontrol);
  726. ucontrol->value.integer.value[0] =
  727. loopback->setup[kcontrol->id.subdevice]
  728. [kcontrol->id.device].rate_shift;
  729. return 0;
  730. }
  731. static int loopback_rate_shift_put(struct snd_kcontrol *kcontrol,
  732. struct snd_ctl_elem_value *ucontrol)
  733. {
  734. struct loopback *loopback = snd_kcontrol_chip(kcontrol);
  735. unsigned int val;
  736. int change = 0;
  737. val = ucontrol->value.integer.value[0];
  738. if (val < 80000)
  739. val = 80000;
  740. if (val > 120000)
  741. val = 120000;
  742. mutex_lock(&loopback->cable_lock);
  743. if (val != loopback->setup[kcontrol->id.subdevice]
  744. [kcontrol->id.device].rate_shift) {
  745. loopback->setup[kcontrol->id.subdevice]
  746. [kcontrol->id.device].rate_shift = val;
  747. change = 1;
  748. }
  749. mutex_unlock(&loopback->cable_lock);
  750. return change;
  751. }
  752. static int loopback_notify_get(struct snd_kcontrol *kcontrol,
  753. struct snd_ctl_elem_value *ucontrol)
  754. {
  755. struct loopback *loopback = snd_kcontrol_chip(kcontrol);
  756. ucontrol->value.integer.value[0] =
  757. loopback->setup[kcontrol->id.subdevice]
  758. [kcontrol->id.device].notify;
  759. return 0;
  760. }
  761. static int loopback_notify_put(struct snd_kcontrol *kcontrol,
  762. struct snd_ctl_elem_value *ucontrol)
  763. {
  764. struct loopback *loopback = snd_kcontrol_chip(kcontrol);
  765. unsigned int val;
  766. int change = 0;
  767. val = ucontrol->value.integer.value[0] ? 1 : 0;
  768. if (val != loopback->setup[kcontrol->id.subdevice]
  769. [kcontrol->id.device].notify) {
  770. loopback->setup[kcontrol->id.subdevice]
  771. [kcontrol->id.device].notify = val;
  772. change = 1;
  773. }
  774. return change;
  775. }
  776. static int loopback_active_get(struct snd_kcontrol *kcontrol,
  777. struct snd_ctl_elem_value *ucontrol)
  778. {
  779. struct loopback *loopback = snd_kcontrol_chip(kcontrol);
  780. struct loopback_cable *cable = loopback->cables
  781. [kcontrol->id.subdevice][kcontrol->id.device ^ 1];
  782. unsigned int val = 0;
  783. if (cable != NULL)
  784. val = (cable->running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) ?
  785. 1 : 0;
  786. ucontrol->value.integer.value[0] = val;
  787. return 0;
  788. }
  789. static int loopback_format_info(struct snd_kcontrol *kcontrol,
  790. struct snd_ctl_elem_info *uinfo)
  791. {
  792. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  793. uinfo->count = 1;
  794. uinfo->value.integer.min = 0;
  795. uinfo->value.integer.max = SNDRV_PCM_FORMAT_LAST;
  796. uinfo->value.integer.step = 1;
  797. return 0;
  798. }
  799. static int loopback_format_get(struct snd_kcontrol *kcontrol,
  800. struct snd_ctl_elem_value *ucontrol)
  801. {
  802. struct loopback *loopback = snd_kcontrol_chip(kcontrol);
  803. ucontrol->value.integer.value[0] =
  804. loopback->setup[kcontrol->id.subdevice]
  805. [kcontrol->id.device].format;
  806. return 0;
  807. }
  808. static int loopback_rate_info(struct snd_kcontrol *kcontrol,
  809. struct snd_ctl_elem_info *uinfo)
  810. {
  811. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  812. uinfo->count = 1;
  813. uinfo->value.integer.min = 0;
  814. uinfo->value.integer.max = 192000;
  815. uinfo->value.integer.step = 1;
  816. return 0;
  817. }
  818. static int loopback_rate_get(struct snd_kcontrol *kcontrol,
  819. struct snd_ctl_elem_value *ucontrol)
  820. {
  821. struct loopback *loopback = snd_kcontrol_chip(kcontrol);
  822. ucontrol->value.integer.value[0] =
  823. loopback->setup[kcontrol->id.subdevice]
  824. [kcontrol->id.device].rate;
  825. return 0;
  826. }
  827. static int loopback_channels_info(struct snd_kcontrol *kcontrol,
  828. struct snd_ctl_elem_info *uinfo)
  829. {
  830. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  831. uinfo->count = 1;
  832. uinfo->value.integer.min = 1;
  833. uinfo->value.integer.max = 1024;
  834. uinfo->value.integer.step = 1;
  835. return 0;
  836. }
  837. static int loopback_channels_get(struct snd_kcontrol *kcontrol,
  838. struct snd_ctl_elem_value *ucontrol)
  839. {
  840. struct loopback *loopback = snd_kcontrol_chip(kcontrol);
  841. ucontrol->value.integer.value[0] =
  842. loopback->setup[kcontrol->id.subdevice]
  843. [kcontrol->id.device].channels;
  844. return 0;
  845. }
  846. static struct snd_kcontrol_new loopback_controls[] = {
  847. {
  848. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  849. .name = "PCM Rate Shift 100000",
  850. .info = loopback_rate_shift_info,
  851. .get = loopback_rate_shift_get,
  852. .put = loopback_rate_shift_put,
  853. },
  854. {
  855. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  856. .name = "PCM Notify",
  857. .info = snd_ctl_boolean_mono_info,
  858. .get = loopback_notify_get,
  859. .put = loopback_notify_put,
  860. },
  861. #define ACTIVE_IDX 2
  862. {
  863. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  864. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  865. .name = "PCM Slave Active",
  866. .info = snd_ctl_boolean_mono_info,
  867. .get = loopback_active_get,
  868. },
  869. #define FORMAT_IDX 3
  870. {
  871. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  872. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  873. .name = "PCM Slave Format",
  874. .info = loopback_format_info,
  875. .get = loopback_format_get
  876. },
  877. #define RATE_IDX 4
  878. {
  879. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  880. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  881. .name = "PCM Slave Rate",
  882. .info = loopback_rate_info,
  883. .get = loopback_rate_get
  884. },
  885. #define CHANNELS_IDX 5
  886. {
  887. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  888. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  889. .name = "PCM Slave Channels",
  890. .info = loopback_channels_info,
  891. .get = loopback_channels_get
  892. }
  893. };
  894. static int loopback_mixer_new(struct loopback *loopback, int notify)
  895. {
  896. struct snd_card *card = loopback->card;
  897. struct snd_pcm *pcm;
  898. struct snd_kcontrol *kctl;
  899. struct loopback_setup *setup;
  900. int err, dev, substr, substr_count, idx;
  901. strcpy(card->mixername, "Loopback Mixer");
  902. for (dev = 0; dev < 2; dev++) {
  903. pcm = loopback->pcm[dev];
  904. substr_count =
  905. pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count;
  906. for (substr = 0; substr < substr_count; substr++) {
  907. setup = &loopback->setup[substr][dev];
  908. setup->notify = notify;
  909. setup->rate_shift = NO_PITCH;
  910. setup->format = SNDRV_PCM_FORMAT_S16_LE;
  911. setup->rate = 48000;
  912. setup->channels = 2;
  913. for (idx = 0; idx < ARRAY_SIZE(loopback_controls);
  914. idx++) {
  915. kctl = snd_ctl_new1(&loopback_controls[idx],
  916. loopback);
  917. if (!kctl)
  918. return -ENOMEM;
  919. kctl->id.device = dev;
  920. kctl->id.subdevice = substr;
  921. switch (idx) {
  922. case ACTIVE_IDX:
  923. setup->active_id = kctl->id;
  924. break;
  925. case FORMAT_IDX:
  926. setup->format_id = kctl->id;
  927. break;
  928. case RATE_IDX:
  929. setup->rate_id = kctl->id;
  930. break;
  931. case CHANNELS_IDX:
  932. setup->channels_id = kctl->id;
  933. break;
  934. default:
  935. break;
  936. }
  937. err = snd_ctl_add(card, kctl);
  938. if (err < 0)
  939. return err;
  940. }
  941. }
  942. }
  943. return 0;
  944. }
  945. #ifdef CONFIG_PROC_FS
  946. static void print_dpcm_info(struct snd_info_buffer *buffer,
  947. struct loopback_pcm *dpcm,
  948. const char *id)
  949. {
  950. snd_iprintf(buffer, " %s\n", id);
  951. if (dpcm == NULL) {
  952. snd_iprintf(buffer, " inactive\n");
  953. return;
  954. }
  955. snd_iprintf(buffer, " buffer_size:\t%u\n", dpcm->pcm_buffer_size);
  956. snd_iprintf(buffer, " buffer_pos:\t\t%u\n", dpcm->buf_pos);
  957. snd_iprintf(buffer, " silent_size:\t%u\n", dpcm->silent_size);
  958. snd_iprintf(buffer, " period_size:\t%u\n", dpcm->pcm_period_size);
  959. snd_iprintf(buffer, " bytes_per_sec:\t%u\n", dpcm->pcm_bps);
  960. snd_iprintf(buffer, " sample_align:\t%u\n", dpcm->pcm_salign);
  961. snd_iprintf(buffer, " rate_shift:\t\t%u\n", dpcm->pcm_rate_shift);
  962. snd_iprintf(buffer, " update_pending:\t%u\n",
  963. dpcm->period_update_pending);
  964. snd_iprintf(buffer, " irq_pos:\t\t%u\n", dpcm->irq_pos);
  965. snd_iprintf(buffer, " period_frac:\t%u\n", dpcm->period_size_frac);
  966. snd_iprintf(buffer, " last_jiffies:\t%lu (%lu)\n",
  967. dpcm->last_jiffies, jiffies);
  968. snd_iprintf(buffer, " timer_expires:\t%lu\n", dpcm->timer.expires);
  969. }
  970. static void print_substream_info(struct snd_info_buffer *buffer,
  971. struct loopback *loopback,
  972. int sub,
  973. int num)
  974. {
  975. struct loopback_cable *cable = loopback->cables[sub][num];
  976. snd_iprintf(buffer, "Cable %i substream %i:\n", num, sub);
  977. if (cable == NULL) {
  978. snd_iprintf(buffer, " inactive\n");
  979. return;
  980. }
  981. snd_iprintf(buffer, " valid: %u\n", cable->valid);
  982. snd_iprintf(buffer, " running: %u\n", cable->running);
  983. snd_iprintf(buffer, " pause: %u\n", cable->pause);
  984. print_dpcm_info(buffer, cable->streams[0], "Playback");
  985. print_dpcm_info(buffer, cable->streams[1], "Capture");
  986. }
  987. static void print_cable_info(struct snd_info_entry *entry,
  988. struct snd_info_buffer *buffer)
  989. {
  990. struct loopback *loopback = entry->private_data;
  991. int sub, num;
  992. mutex_lock(&loopback->cable_lock);
  993. num = entry->name[strlen(entry->name)-1];
  994. num = num == '0' ? 0 : 1;
  995. for (sub = 0; sub < MAX_PCM_SUBSTREAMS; sub++)
  996. print_substream_info(buffer, loopback, sub, num);
  997. mutex_unlock(&loopback->cable_lock);
  998. }
  999. static int loopback_proc_new(struct loopback *loopback, int cidx)
  1000. {
  1001. char name[32];
  1002. struct snd_info_entry *entry;
  1003. int err;
  1004. snprintf(name, sizeof(name), "cable#%d", cidx);
  1005. err = snd_card_proc_new(loopback->card, name, &entry);
  1006. if (err < 0)
  1007. return err;
  1008. snd_info_set_text_ops(entry, loopback, print_cable_info);
  1009. return 0;
  1010. }
  1011. #else /* !CONFIG_PROC_FS */
  1012. #define loopback_proc_new(loopback, cidx) do { } while (0)
  1013. #endif
  1014. static int loopback_probe(struct platform_device *devptr)
  1015. {
  1016. struct snd_card *card;
  1017. struct loopback *loopback;
  1018. int dev = devptr->id;
  1019. int err;
  1020. err = snd_card_create(index[dev], id[dev], THIS_MODULE,
  1021. sizeof(struct loopback), &card);
  1022. if (err < 0)
  1023. return err;
  1024. loopback = card->private_data;
  1025. if (pcm_substreams[dev] < 1)
  1026. pcm_substreams[dev] = 1;
  1027. if (pcm_substreams[dev] > MAX_PCM_SUBSTREAMS)
  1028. pcm_substreams[dev] = MAX_PCM_SUBSTREAMS;
  1029. loopback->card = card;
  1030. mutex_init(&loopback->cable_lock);
  1031. err = loopback_pcm_new(loopback, 0, pcm_substreams[dev]);
  1032. if (err < 0)
  1033. goto __nodev;
  1034. err = loopback_pcm_new(loopback, 1, pcm_substreams[dev]);
  1035. if (err < 0)
  1036. goto __nodev;
  1037. err = loopback_mixer_new(loopback, pcm_notify[dev] ? 1 : 0);
  1038. if (err < 0)
  1039. goto __nodev;
  1040. loopback_proc_new(loopback, 0);
  1041. loopback_proc_new(loopback, 1);
  1042. strcpy(card->driver, "Loopback");
  1043. strcpy(card->shortname, "Loopback");
  1044. sprintf(card->longname, "Loopback %i", dev + 1);
  1045. err = snd_card_register(card);
  1046. if (!err) {
  1047. platform_set_drvdata(devptr, card);
  1048. return 0;
  1049. }
  1050. __nodev:
  1051. snd_card_free(card);
  1052. return err;
  1053. }
  1054. static int loopback_remove(struct platform_device *devptr)
  1055. {
  1056. snd_card_free(platform_get_drvdata(devptr));
  1057. platform_set_drvdata(devptr, NULL);
  1058. return 0;
  1059. }
  1060. #ifdef CONFIG_PM_SLEEP
  1061. static int loopback_suspend(struct device *pdev)
  1062. {
  1063. struct snd_card *card = dev_get_drvdata(pdev);
  1064. struct loopback *loopback = card->private_data;
  1065. snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
  1066. snd_pcm_suspend_all(loopback->pcm[0]);
  1067. snd_pcm_suspend_all(loopback->pcm[1]);
  1068. return 0;
  1069. }
  1070. static int loopback_resume(struct device *pdev)
  1071. {
  1072. struct snd_card *card = dev_get_drvdata(pdev);
  1073. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  1074. return 0;
  1075. }
  1076. static SIMPLE_DEV_PM_OPS(loopback_pm, loopback_suspend, loopback_resume);
  1077. #define LOOPBACK_PM_OPS &loopback_pm
  1078. #else
  1079. #define LOOPBACK_PM_OPS NULL
  1080. #endif
  1081. #define SND_LOOPBACK_DRIVER "snd_aloop"
  1082. static struct platform_driver loopback_driver = {
  1083. .probe = loopback_probe,
  1084. .remove = loopback_remove,
  1085. .driver = {
  1086. .name = SND_LOOPBACK_DRIVER,
  1087. .owner = THIS_MODULE,
  1088. .pm = LOOPBACK_PM_OPS,
  1089. },
  1090. };
  1091. static void loopback_unregister_all(void)
  1092. {
  1093. int i;
  1094. for (i = 0; i < ARRAY_SIZE(devices); ++i)
  1095. platform_device_unregister(devices[i]);
  1096. platform_driver_unregister(&loopback_driver);
  1097. }
  1098. static int __init alsa_card_loopback_init(void)
  1099. {
  1100. int i, err, cards;
  1101. err = platform_driver_register(&loopback_driver);
  1102. if (err < 0)
  1103. return err;
  1104. cards = 0;
  1105. for (i = 0; i < SNDRV_CARDS; i++) {
  1106. struct platform_device *device;
  1107. if (!enable[i])
  1108. continue;
  1109. device = platform_device_register_simple(SND_LOOPBACK_DRIVER,
  1110. i, NULL, 0);
  1111. if (IS_ERR(device))
  1112. continue;
  1113. if (!platform_get_drvdata(device)) {
  1114. platform_device_unregister(device);
  1115. continue;
  1116. }
  1117. devices[i] = device;
  1118. cards++;
  1119. }
  1120. if (!cards) {
  1121. #ifdef MODULE
  1122. printk(KERN_ERR "aloop: No loopback enabled\n");
  1123. #endif
  1124. loopback_unregister_all();
  1125. return -ENODEV;
  1126. }
  1127. return 0;
  1128. }
  1129. static void __exit alsa_card_loopback_exit(void)
  1130. {
  1131. loopback_unregister_all();
  1132. }
  1133. module_init(alsa_card_loopback_init)
  1134. module_exit(alsa_card_loopback_exit)