aloop.c 35 KB

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