dummy.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  1. /*
  2. * Dummy soundcard
  3. * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. */
  20. #include <linux/init.h>
  21. #include <linux/err.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/jiffies.h>
  24. #include <linux/slab.h>
  25. #include <linux/time.h>
  26. #include <linux/wait.h>
  27. #include <linux/hrtimer.h>
  28. #include <linux/math64.h>
  29. #include <linux/moduleparam.h>
  30. #include <sound/core.h>
  31. #include <sound/control.h>
  32. #include <sound/tlv.h>
  33. #include <sound/pcm.h>
  34. #include <sound/rawmidi.h>
  35. #include <sound/info.h>
  36. #include <sound/initval.h>
  37. MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
  38. MODULE_DESCRIPTION("Dummy soundcard (/dev/null)");
  39. MODULE_LICENSE("GPL");
  40. MODULE_SUPPORTED_DEVICE("{{ALSA,Dummy soundcard}}");
  41. #define MAX_PCM_DEVICES 4
  42. #define MAX_PCM_SUBSTREAMS 128
  43. #define MAX_MIDI_DEVICES 2
  44. #if 0 /* emu10k1 emulation */
  45. #define MAX_BUFFER_SIZE (128 * 1024)
  46. static int emu10k1_playback_constraints(struct snd_pcm_runtime *runtime)
  47. {
  48. int err;
  49. err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
  50. if (err < 0)
  51. return err;
  52. err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 256, UINT_MAX);
  53. if (err < 0)
  54. return err;
  55. return 0;
  56. }
  57. #define add_playback_constraints emu10k1_playback_constraints
  58. #endif
  59. #if 0 /* RME9652 emulation */
  60. #define MAX_BUFFER_SIZE (26 * 64 * 1024)
  61. #define USE_FORMATS SNDRV_PCM_FMTBIT_S32_LE
  62. #define USE_CHANNELS_MIN 26
  63. #define USE_CHANNELS_MAX 26
  64. #define USE_PERIODS_MIN 2
  65. #define USE_PERIODS_MAX 2
  66. #endif
  67. #if 0 /* ICE1712 emulation */
  68. #define MAX_BUFFER_SIZE (256 * 1024)
  69. #define USE_FORMATS SNDRV_PCM_FMTBIT_S32_LE
  70. #define USE_CHANNELS_MIN 10
  71. #define USE_CHANNELS_MAX 10
  72. #define USE_PERIODS_MIN 1
  73. #define USE_PERIODS_MAX 1024
  74. #endif
  75. #if 0 /* UDA1341 emulation */
  76. #define MAX_BUFFER_SIZE (16380)
  77. #define USE_FORMATS SNDRV_PCM_FMTBIT_S16_LE
  78. #define USE_CHANNELS_MIN 2
  79. #define USE_CHANNELS_MAX 2
  80. #define USE_PERIODS_MIN 2
  81. #define USE_PERIODS_MAX 255
  82. #endif
  83. #if 0 /* simple AC97 bridge (intel8x0) with 48kHz AC97 only codec */
  84. #define USE_FORMATS SNDRV_PCM_FMTBIT_S16_LE
  85. #define USE_CHANNELS_MIN 2
  86. #define USE_CHANNELS_MAX 2
  87. #define USE_RATE SNDRV_PCM_RATE_48000
  88. #define USE_RATE_MIN 48000
  89. #define USE_RATE_MAX 48000
  90. #endif
  91. #if 0 /* CA0106 */
  92. #define USE_FORMATS SNDRV_PCM_FMTBIT_S16_LE
  93. #define USE_CHANNELS_MIN 2
  94. #define USE_CHANNELS_MAX 2
  95. #define USE_RATE (SNDRV_PCM_RATE_48000|SNDRV_PCM_RATE_96000|SNDRV_PCM_RATE_192000)
  96. #define USE_RATE_MIN 48000
  97. #define USE_RATE_MAX 192000
  98. #define MAX_BUFFER_SIZE ((65536-64)*8)
  99. #define MAX_PERIOD_SIZE (65536-64)
  100. #define USE_PERIODS_MIN 2
  101. #define USE_PERIODS_MAX 8
  102. #endif
  103. /* defaults */
  104. #ifndef MAX_BUFFER_SIZE
  105. #define MAX_BUFFER_SIZE (64*1024)
  106. #endif
  107. #ifndef MAX_PERIOD_SIZE
  108. #define MAX_PERIOD_SIZE MAX_BUFFER_SIZE
  109. #endif
  110. #ifndef USE_FORMATS
  111. #define USE_FORMATS (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE)
  112. #endif
  113. #ifndef USE_RATE
  114. #define USE_RATE SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000
  115. #define USE_RATE_MIN 5500
  116. #define USE_RATE_MAX 48000
  117. #endif
  118. #ifndef USE_CHANNELS_MIN
  119. #define USE_CHANNELS_MIN 1
  120. #endif
  121. #ifndef USE_CHANNELS_MAX
  122. #define USE_CHANNELS_MAX 2
  123. #endif
  124. #ifndef USE_PERIODS_MIN
  125. #define USE_PERIODS_MIN 1
  126. #endif
  127. #ifndef USE_PERIODS_MAX
  128. #define USE_PERIODS_MAX 1024
  129. #endif
  130. #ifndef add_playback_constraints
  131. #define add_playback_constraints(x) 0
  132. #endif
  133. #ifndef add_capture_constraints
  134. #define add_capture_constraints(x) 0
  135. #endif
  136. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
  137. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
  138. static int enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 0};
  139. static int pcm_devs[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1};
  140. static int pcm_substreams[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 8};
  141. //static int midi_devs[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 2};
  142. #ifdef CONFIG_HIGH_RES_TIMERS
  143. static int hrtimer = 1;
  144. #endif
  145. static int fake_buffer = 1;
  146. module_param_array(index, int, NULL, 0444);
  147. MODULE_PARM_DESC(index, "Index value for dummy soundcard.");
  148. module_param_array(id, charp, NULL, 0444);
  149. MODULE_PARM_DESC(id, "ID string for dummy soundcard.");
  150. module_param_array(enable, bool, NULL, 0444);
  151. MODULE_PARM_DESC(enable, "Enable this dummy soundcard.");
  152. module_param_array(pcm_devs, int, NULL, 0444);
  153. MODULE_PARM_DESC(pcm_devs, "PCM devices # (0-4) for dummy driver.");
  154. module_param_array(pcm_substreams, int, NULL, 0444);
  155. MODULE_PARM_DESC(pcm_substreams, "PCM substreams # (1-128) for dummy driver.");
  156. //module_param_array(midi_devs, int, NULL, 0444);
  157. //MODULE_PARM_DESC(midi_devs, "MIDI devices # (0-2) for dummy driver.");
  158. module_param(fake_buffer, bool, 0444);
  159. MODULE_PARM_DESC(fake_buffer, "Fake buffer allocations.");
  160. #ifdef CONFIG_HIGH_RES_TIMERS
  161. module_param(hrtimer, bool, 0644);
  162. MODULE_PARM_DESC(hrtimer, "Use hrtimer as the timer source.");
  163. #endif
  164. static struct platform_device *devices[SNDRV_CARDS];
  165. #define MIXER_ADDR_MASTER 0
  166. #define MIXER_ADDR_LINE 1
  167. #define MIXER_ADDR_MIC 2
  168. #define MIXER_ADDR_SYNTH 3
  169. #define MIXER_ADDR_CD 4
  170. #define MIXER_ADDR_LAST 4
  171. struct dummy_timer_ops {
  172. int (*create)(struct snd_pcm_substream *);
  173. void (*free)(struct snd_pcm_substream *);
  174. int (*prepare)(struct snd_pcm_substream *);
  175. int (*start)(struct snd_pcm_substream *);
  176. int (*stop)(struct snd_pcm_substream *);
  177. snd_pcm_uframes_t (*pointer)(struct snd_pcm_substream *);
  178. };
  179. struct snd_dummy {
  180. struct snd_card *card;
  181. struct snd_pcm *pcm;
  182. spinlock_t mixer_lock;
  183. int mixer_volume[MIXER_ADDR_LAST+1][2];
  184. int capture_source[MIXER_ADDR_LAST+1][2];
  185. const struct dummy_timer_ops *timer_ops;
  186. };
  187. /*
  188. * system timer interface
  189. */
  190. struct dummy_systimer_pcm {
  191. spinlock_t lock;
  192. struct timer_list timer;
  193. unsigned long base_time;
  194. unsigned int frac_pos; /* fractional sample position (based HZ) */
  195. unsigned int frac_period_rest;
  196. unsigned int frac_buffer_size; /* buffer_size * HZ */
  197. unsigned int frac_period_size; /* period_size * HZ */
  198. unsigned int rate;
  199. int elapsed;
  200. struct snd_pcm_substream *substream;
  201. };
  202. static void dummy_systimer_rearm(struct dummy_systimer_pcm *dpcm)
  203. {
  204. dpcm->timer.expires = jiffies +
  205. (dpcm->frac_period_rest + dpcm->rate - 1) / dpcm->rate;
  206. add_timer(&dpcm->timer);
  207. }
  208. static void dummy_systimer_update(struct dummy_systimer_pcm *dpcm)
  209. {
  210. unsigned long delta;
  211. delta = jiffies - dpcm->base_time;
  212. if (!delta)
  213. return;
  214. dpcm->base_time += delta;
  215. delta *= dpcm->rate;
  216. dpcm->frac_pos += delta;
  217. while (dpcm->frac_pos >= dpcm->frac_buffer_size)
  218. dpcm->frac_pos -= dpcm->frac_buffer_size;
  219. while (dpcm->frac_period_rest <= delta) {
  220. dpcm->elapsed++;
  221. dpcm->frac_period_rest += dpcm->frac_period_size;
  222. }
  223. dpcm->frac_period_rest -= delta;
  224. }
  225. static int dummy_systimer_start(struct snd_pcm_substream *substream)
  226. {
  227. struct dummy_systimer_pcm *dpcm = substream->runtime->private_data;
  228. spin_lock(&dpcm->lock);
  229. dpcm->base_time = jiffies;
  230. dummy_systimer_rearm(dpcm);
  231. spin_unlock(&dpcm->lock);
  232. return 0;
  233. }
  234. static int dummy_systimer_stop(struct snd_pcm_substream *substream)
  235. {
  236. struct dummy_systimer_pcm *dpcm = substream->runtime->private_data;
  237. spin_lock(&dpcm->lock);
  238. del_timer(&dpcm->timer);
  239. spin_unlock(&dpcm->lock);
  240. return 0;
  241. }
  242. static int dummy_systimer_prepare(struct snd_pcm_substream *substream)
  243. {
  244. struct snd_pcm_runtime *runtime = substream->runtime;
  245. struct dummy_systimer_pcm *dpcm = runtime->private_data;
  246. dpcm->frac_pos = 0;
  247. dpcm->rate = runtime->rate;
  248. dpcm->frac_buffer_size = runtime->buffer_size * HZ;
  249. dpcm->frac_period_size = runtime->period_size * HZ;
  250. dpcm->frac_period_rest = dpcm->frac_period_size;
  251. dpcm->elapsed = 0;
  252. return 0;
  253. }
  254. static void dummy_systimer_callback(unsigned long data)
  255. {
  256. struct dummy_systimer_pcm *dpcm = (struct dummy_systimer_pcm *)data;
  257. unsigned long flags;
  258. int elapsed = 0;
  259. spin_lock_irqsave(&dpcm->lock, flags);
  260. dummy_systimer_update(dpcm);
  261. dummy_systimer_rearm(dpcm);
  262. elapsed = dpcm->elapsed;
  263. dpcm->elapsed = 0;
  264. spin_unlock_irqrestore(&dpcm->lock, flags);
  265. if (elapsed)
  266. snd_pcm_period_elapsed(dpcm->substream);
  267. }
  268. static snd_pcm_uframes_t
  269. dummy_systimer_pointer(struct snd_pcm_substream *substream)
  270. {
  271. struct dummy_systimer_pcm *dpcm = substream->runtime->private_data;
  272. snd_pcm_uframes_t pos;
  273. spin_lock(&dpcm->lock);
  274. dummy_systimer_update(dpcm);
  275. pos = dpcm->frac_pos / HZ;
  276. spin_unlock(&dpcm->lock);
  277. return pos;
  278. }
  279. static int dummy_systimer_create(struct snd_pcm_substream *substream)
  280. {
  281. struct dummy_systimer_pcm *dpcm;
  282. dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
  283. if (!dpcm)
  284. return -ENOMEM;
  285. substream->runtime->private_data = dpcm;
  286. init_timer(&dpcm->timer);
  287. dpcm->timer.data = (unsigned long) dpcm;
  288. dpcm->timer.function = dummy_systimer_callback;
  289. spin_lock_init(&dpcm->lock);
  290. dpcm->substream = substream;
  291. return 0;
  292. }
  293. static void dummy_systimer_free(struct snd_pcm_substream *substream)
  294. {
  295. kfree(substream->runtime->private_data);
  296. }
  297. static struct dummy_timer_ops dummy_systimer_ops = {
  298. .create = dummy_systimer_create,
  299. .free = dummy_systimer_free,
  300. .prepare = dummy_systimer_prepare,
  301. .start = dummy_systimer_start,
  302. .stop = dummy_systimer_stop,
  303. .pointer = dummy_systimer_pointer,
  304. };
  305. #ifdef CONFIG_HIGH_RES_TIMERS
  306. /*
  307. * hrtimer interface
  308. */
  309. struct dummy_hrtimer_pcm {
  310. ktime_t base_time;
  311. ktime_t period_time;
  312. atomic_t running;
  313. struct hrtimer timer;
  314. struct tasklet_struct tasklet;
  315. struct snd_pcm_substream *substream;
  316. };
  317. static void dummy_hrtimer_pcm_elapsed(unsigned long priv)
  318. {
  319. struct dummy_hrtimer_pcm *dpcm = (struct dummy_hrtimer_pcm *)priv;
  320. if (atomic_read(&dpcm->running))
  321. snd_pcm_period_elapsed(dpcm->substream);
  322. }
  323. static enum hrtimer_restart dummy_hrtimer_callback(struct hrtimer *timer)
  324. {
  325. struct dummy_hrtimer_pcm *dpcm;
  326. dpcm = container_of(timer, struct dummy_hrtimer_pcm, timer);
  327. if (!atomic_read(&dpcm->running))
  328. return HRTIMER_NORESTART;
  329. tasklet_schedule(&dpcm->tasklet);
  330. hrtimer_forward_now(timer, dpcm->period_time);
  331. return HRTIMER_RESTART;
  332. }
  333. static int dummy_hrtimer_start(struct snd_pcm_substream *substream)
  334. {
  335. struct dummy_hrtimer_pcm *dpcm = substream->runtime->private_data;
  336. dpcm->base_time = hrtimer_cb_get_time(&dpcm->timer);
  337. hrtimer_start(&dpcm->timer, dpcm->period_time, HRTIMER_MODE_REL);
  338. atomic_set(&dpcm->running, 1);
  339. return 0;
  340. }
  341. static int dummy_hrtimer_stop(struct snd_pcm_substream *substream)
  342. {
  343. struct dummy_hrtimer_pcm *dpcm = substream->runtime->private_data;
  344. atomic_set(&dpcm->running, 0);
  345. hrtimer_cancel(&dpcm->timer);
  346. return 0;
  347. }
  348. static inline void dummy_hrtimer_sync(struct dummy_hrtimer_pcm *dpcm)
  349. {
  350. tasklet_kill(&dpcm->tasklet);
  351. }
  352. static snd_pcm_uframes_t
  353. dummy_hrtimer_pointer(struct snd_pcm_substream *substream)
  354. {
  355. struct snd_pcm_runtime *runtime = substream->runtime;
  356. struct dummy_hrtimer_pcm *dpcm = runtime->private_data;
  357. u64 delta;
  358. u32 pos;
  359. delta = ktime_us_delta(hrtimer_cb_get_time(&dpcm->timer),
  360. dpcm->base_time);
  361. delta = div_u64(delta * runtime->rate + 999999, 1000000);
  362. div_u64_rem(delta, runtime->buffer_size, &pos);
  363. return pos;
  364. }
  365. static int dummy_hrtimer_prepare(struct snd_pcm_substream *substream)
  366. {
  367. struct snd_pcm_runtime *runtime = substream->runtime;
  368. struct dummy_hrtimer_pcm *dpcm = runtime->private_data;
  369. unsigned int period, rate;
  370. long sec;
  371. unsigned long nsecs;
  372. dummy_hrtimer_sync(dpcm);
  373. period = runtime->period_size;
  374. rate = runtime->rate;
  375. sec = period / rate;
  376. period %= rate;
  377. nsecs = div_u64((u64)period * 1000000000UL + rate - 1, rate);
  378. dpcm->period_time = ktime_set(sec, nsecs);
  379. return 0;
  380. }
  381. static int dummy_hrtimer_create(struct snd_pcm_substream *substream)
  382. {
  383. struct dummy_hrtimer_pcm *dpcm;
  384. dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
  385. if (!dpcm)
  386. return -ENOMEM;
  387. substream->runtime->private_data = dpcm;
  388. hrtimer_init(&dpcm->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  389. dpcm->timer.function = dummy_hrtimer_callback;
  390. dpcm->substream = substream;
  391. atomic_set(&dpcm->running, 0);
  392. tasklet_init(&dpcm->tasklet, dummy_hrtimer_pcm_elapsed,
  393. (unsigned long)dpcm);
  394. return 0;
  395. }
  396. static void dummy_hrtimer_free(struct snd_pcm_substream *substream)
  397. {
  398. struct dummy_hrtimer_pcm *dpcm = substream->runtime->private_data;
  399. dummy_hrtimer_sync(dpcm);
  400. kfree(dpcm);
  401. }
  402. static struct dummy_timer_ops dummy_hrtimer_ops = {
  403. .create = dummy_hrtimer_create,
  404. .free = dummy_hrtimer_free,
  405. .prepare = dummy_hrtimer_prepare,
  406. .start = dummy_hrtimer_start,
  407. .stop = dummy_hrtimer_stop,
  408. .pointer = dummy_hrtimer_pointer,
  409. };
  410. #endif /* CONFIG_HIGH_RES_TIMERS */
  411. /*
  412. * PCM interface
  413. */
  414. static int dummy_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  415. {
  416. struct snd_dummy *dummy = snd_pcm_substream_chip(substream);
  417. switch (cmd) {
  418. case SNDRV_PCM_TRIGGER_START:
  419. case SNDRV_PCM_TRIGGER_RESUME:
  420. return dummy->timer_ops->start(substream);
  421. case SNDRV_PCM_TRIGGER_STOP:
  422. case SNDRV_PCM_TRIGGER_SUSPEND:
  423. return dummy->timer_ops->stop(substream);
  424. }
  425. return -EINVAL;
  426. }
  427. static int dummy_pcm_prepare(struct snd_pcm_substream *substream)
  428. {
  429. struct snd_dummy *dummy = snd_pcm_substream_chip(substream);
  430. return dummy->timer_ops->prepare(substream);
  431. }
  432. static snd_pcm_uframes_t dummy_pcm_pointer(struct snd_pcm_substream *substream)
  433. {
  434. struct snd_dummy *dummy = snd_pcm_substream_chip(substream);
  435. return dummy->timer_ops->pointer(substream);
  436. }
  437. static struct snd_pcm_hardware dummy_pcm_hardware = {
  438. .info = (SNDRV_PCM_INFO_MMAP |
  439. SNDRV_PCM_INFO_INTERLEAVED |
  440. SNDRV_PCM_INFO_RESUME |
  441. SNDRV_PCM_INFO_MMAP_VALID),
  442. .formats = USE_FORMATS,
  443. .rates = USE_RATE,
  444. .rate_min = USE_RATE_MIN,
  445. .rate_max = USE_RATE_MAX,
  446. .channels_min = USE_CHANNELS_MIN,
  447. .channels_max = USE_CHANNELS_MAX,
  448. .buffer_bytes_max = MAX_BUFFER_SIZE,
  449. .period_bytes_min = 64,
  450. .period_bytes_max = MAX_PERIOD_SIZE,
  451. .periods_min = USE_PERIODS_MIN,
  452. .periods_max = USE_PERIODS_MAX,
  453. .fifo_size = 0,
  454. };
  455. static int dummy_pcm_hw_params(struct snd_pcm_substream *substream,
  456. struct snd_pcm_hw_params *hw_params)
  457. {
  458. if (fake_buffer) {
  459. /* runtime->dma_bytes has to be set manually to allow mmap */
  460. substream->runtime->dma_bytes = params_buffer_bytes(hw_params);
  461. return 0;
  462. }
  463. return snd_pcm_lib_malloc_pages(substream,
  464. params_buffer_bytes(hw_params));
  465. }
  466. static int dummy_pcm_hw_free(struct snd_pcm_substream *substream)
  467. {
  468. if (fake_buffer)
  469. return 0;
  470. return snd_pcm_lib_free_pages(substream);
  471. }
  472. static int dummy_pcm_open(struct snd_pcm_substream *substream)
  473. {
  474. struct snd_dummy *dummy = snd_pcm_substream_chip(substream);
  475. struct snd_pcm_runtime *runtime = substream->runtime;
  476. int err;
  477. dummy->timer_ops = &dummy_systimer_ops;
  478. #ifdef CONFIG_HIGH_RES_TIMERS
  479. if (hrtimer)
  480. dummy->timer_ops = &dummy_hrtimer_ops;
  481. #endif
  482. err = dummy->timer_ops->create(substream);
  483. if (err < 0)
  484. return err;
  485. runtime->hw = dummy_pcm_hardware;
  486. if (substream->pcm->device & 1) {
  487. runtime->hw.info &= ~SNDRV_PCM_INFO_INTERLEAVED;
  488. runtime->hw.info |= SNDRV_PCM_INFO_NONINTERLEAVED;
  489. }
  490. if (substream->pcm->device & 2)
  491. runtime->hw.info &= ~(SNDRV_PCM_INFO_MMAP |
  492. SNDRV_PCM_INFO_MMAP_VALID);
  493. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  494. err = add_playback_constraints(substream->runtime);
  495. else
  496. err = add_capture_constraints(substream->runtime);
  497. if (err < 0) {
  498. dummy->timer_ops->free(substream);
  499. return err;
  500. }
  501. return 0;
  502. }
  503. static int dummy_pcm_close(struct snd_pcm_substream *substream)
  504. {
  505. struct snd_dummy *dummy = snd_pcm_substream_chip(substream);
  506. dummy->timer_ops->free(substream);
  507. return 0;
  508. }
  509. /*
  510. * dummy buffer handling
  511. */
  512. static void *dummy_page[2];
  513. static void free_fake_buffer(void)
  514. {
  515. if (fake_buffer) {
  516. int i;
  517. for (i = 0; i < 2; i++)
  518. if (dummy_page[i]) {
  519. free_page((unsigned long)dummy_page[i]);
  520. dummy_page[i] = NULL;
  521. }
  522. }
  523. }
  524. static int alloc_fake_buffer(void)
  525. {
  526. int i;
  527. if (!fake_buffer)
  528. return 0;
  529. for (i = 0; i < 2; i++) {
  530. dummy_page[i] = (void *)get_zeroed_page(GFP_KERNEL);
  531. if (!dummy_page[i]) {
  532. free_fake_buffer();
  533. return -ENOMEM;
  534. }
  535. }
  536. return 0;
  537. }
  538. static int dummy_pcm_copy(struct snd_pcm_substream *substream,
  539. int channel, snd_pcm_uframes_t pos,
  540. void __user *dst, snd_pcm_uframes_t count)
  541. {
  542. return 0; /* do nothing */
  543. }
  544. static int dummy_pcm_silence(struct snd_pcm_substream *substream,
  545. int channel, snd_pcm_uframes_t pos,
  546. snd_pcm_uframes_t count)
  547. {
  548. return 0; /* do nothing */
  549. }
  550. static struct page *dummy_pcm_page(struct snd_pcm_substream *substream,
  551. unsigned long offset)
  552. {
  553. return virt_to_page(dummy_page[substream->stream]); /* the same page */
  554. }
  555. static struct snd_pcm_ops dummy_pcm_ops = {
  556. .open = dummy_pcm_open,
  557. .close = dummy_pcm_close,
  558. .ioctl = snd_pcm_lib_ioctl,
  559. .hw_params = dummy_pcm_hw_params,
  560. .hw_free = dummy_pcm_hw_free,
  561. .prepare = dummy_pcm_prepare,
  562. .trigger = dummy_pcm_trigger,
  563. .pointer = dummy_pcm_pointer,
  564. };
  565. static struct snd_pcm_ops dummy_pcm_ops_no_buf = {
  566. .open = dummy_pcm_open,
  567. .close = dummy_pcm_close,
  568. .ioctl = snd_pcm_lib_ioctl,
  569. .hw_params = dummy_pcm_hw_params,
  570. .hw_free = dummy_pcm_hw_free,
  571. .prepare = dummy_pcm_prepare,
  572. .trigger = dummy_pcm_trigger,
  573. .pointer = dummy_pcm_pointer,
  574. .copy = dummy_pcm_copy,
  575. .silence = dummy_pcm_silence,
  576. .page = dummy_pcm_page,
  577. };
  578. static int __devinit snd_card_dummy_pcm(struct snd_dummy *dummy, int device,
  579. int substreams)
  580. {
  581. struct snd_pcm *pcm;
  582. struct snd_pcm_ops *ops;
  583. int err;
  584. err = snd_pcm_new(dummy->card, "Dummy PCM", device,
  585. substreams, substreams, &pcm);
  586. if (err < 0)
  587. return err;
  588. dummy->pcm = pcm;
  589. if (fake_buffer)
  590. ops = &dummy_pcm_ops_no_buf;
  591. else
  592. ops = &dummy_pcm_ops;
  593. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, ops);
  594. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, ops);
  595. pcm->private_data = dummy;
  596. pcm->info_flags = 0;
  597. strcpy(pcm->name, "Dummy PCM");
  598. if (!fake_buffer) {
  599. snd_pcm_lib_preallocate_pages_for_all(pcm,
  600. SNDRV_DMA_TYPE_CONTINUOUS,
  601. snd_dma_continuous_data(GFP_KERNEL),
  602. 0, 64*1024);
  603. }
  604. return 0;
  605. }
  606. /*
  607. * mixer interface
  608. */
  609. #define DUMMY_VOLUME(xname, xindex, addr) \
  610. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
  611. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
  612. .name = xname, .index = xindex, \
  613. .info = snd_dummy_volume_info, \
  614. .get = snd_dummy_volume_get, .put = snd_dummy_volume_put, \
  615. .private_value = addr, \
  616. .tlv = { .p = db_scale_dummy } }
  617. static int snd_dummy_volume_info(struct snd_kcontrol *kcontrol,
  618. struct snd_ctl_elem_info *uinfo)
  619. {
  620. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  621. uinfo->count = 2;
  622. uinfo->value.integer.min = -50;
  623. uinfo->value.integer.max = 100;
  624. return 0;
  625. }
  626. static int snd_dummy_volume_get(struct snd_kcontrol *kcontrol,
  627. struct snd_ctl_elem_value *ucontrol)
  628. {
  629. struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
  630. int addr = kcontrol->private_value;
  631. spin_lock_irq(&dummy->mixer_lock);
  632. ucontrol->value.integer.value[0] = dummy->mixer_volume[addr][0];
  633. ucontrol->value.integer.value[1] = dummy->mixer_volume[addr][1];
  634. spin_unlock_irq(&dummy->mixer_lock);
  635. return 0;
  636. }
  637. static int snd_dummy_volume_put(struct snd_kcontrol *kcontrol,
  638. struct snd_ctl_elem_value *ucontrol)
  639. {
  640. struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
  641. int change, addr = kcontrol->private_value;
  642. int left, right;
  643. left = ucontrol->value.integer.value[0];
  644. if (left < -50)
  645. left = -50;
  646. if (left > 100)
  647. left = 100;
  648. right = ucontrol->value.integer.value[1];
  649. if (right < -50)
  650. right = -50;
  651. if (right > 100)
  652. right = 100;
  653. spin_lock_irq(&dummy->mixer_lock);
  654. change = dummy->mixer_volume[addr][0] != left ||
  655. dummy->mixer_volume[addr][1] != right;
  656. dummy->mixer_volume[addr][0] = left;
  657. dummy->mixer_volume[addr][1] = right;
  658. spin_unlock_irq(&dummy->mixer_lock);
  659. return change;
  660. }
  661. static const DECLARE_TLV_DB_SCALE(db_scale_dummy, -4500, 30, 0);
  662. #define DUMMY_CAPSRC(xname, xindex, addr) \
  663. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
  664. .info = snd_dummy_capsrc_info, \
  665. .get = snd_dummy_capsrc_get, .put = snd_dummy_capsrc_put, \
  666. .private_value = addr }
  667. #define snd_dummy_capsrc_info snd_ctl_boolean_stereo_info
  668. static int snd_dummy_capsrc_get(struct snd_kcontrol *kcontrol,
  669. struct snd_ctl_elem_value *ucontrol)
  670. {
  671. struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
  672. int addr = kcontrol->private_value;
  673. spin_lock_irq(&dummy->mixer_lock);
  674. ucontrol->value.integer.value[0] = dummy->capture_source[addr][0];
  675. ucontrol->value.integer.value[1] = dummy->capture_source[addr][1];
  676. spin_unlock_irq(&dummy->mixer_lock);
  677. return 0;
  678. }
  679. static int snd_dummy_capsrc_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  680. {
  681. struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
  682. int change, addr = kcontrol->private_value;
  683. int left, right;
  684. left = ucontrol->value.integer.value[0] & 1;
  685. right = ucontrol->value.integer.value[1] & 1;
  686. spin_lock_irq(&dummy->mixer_lock);
  687. change = dummy->capture_source[addr][0] != left &&
  688. dummy->capture_source[addr][1] != right;
  689. dummy->capture_source[addr][0] = left;
  690. dummy->capture_source[addr][1] = right;
  691. spin_unlock_irq(&dummy->mixer_lock);
  692. return change;
  693. }
  694. static struct snd_kcontrol_new snd_dummy_controls[] = {
  695. DUMMY_VOLUME("Master Volume", 0, MIXER_ADDR_MASTER),
  696. DUMMY_CAPSRC("Master Capture Switch", 0, MIXER_ADDR_MASTER),
  697. DUMMY_VOLUME("Synth Volume", 0, MIXER_ADDR_SYNTH),
  698. DUMMY_CAPSRC("Synth Capture Switch", 0, MIXER_ADDR_SYNTH),
  699. DUMMY_VOLUME("Line Volume", 0, MIXER_ADDR_LINE),
  700. DUMMY_CAPSRC("Line Capture Switch", 0, MIXER_ADDR_LINE),
  701. DUMMY_VOLUME("Mic Volume", 0, MIXER_ADDR_MIC),
  702. DUMMY_CAPSRC("Mic Capture Switch", 0, MIXER_ADDR_MIC),
  703. DUMMY_VOLUME("CD Volume", 0, MIXER_ADDR_CD),
  704. DUMMY_CAPSRC("CD Capture Switch", 0, MIXER_ADDR_CD)
  705. };
  706. static int __devinit snd_card_dummy_new_mixer(struct snd_dummy *dummy)
  707. {
  708. struct snd_card *card = dummy->card;
  709. unsigned int idx;
  710. int err;
  711. spin_lock_init(&dummy->mixer_lock);
  712. strcpy(card->mixername, "Dummy Mixer");
  713. for (idx = 0; idx < ARRAY_SIZE(snd_dummy_controls); idx++) {
  714. err = snd_ctl_add(card, snd_ctl_new1(&snd_dummy_controls[idx], dummy));
  715. if (err < 0)
  716. return err;
  717. }
  718. return 0;
  719. }
  720. #if defined(CONFIG_SND_DEBUG) && defined(CONFIG_PROC_FS)
  721. /*
  722. * proc interface
  723. */
  724. static void print_formats(struct snd_info_buffer *buffer)
  725. {
  726. int i;
  727. for (i = 0; i < SNDRV_PCM_FORMAT_LAST; i++) {
  728. if (dummy_pcm_hardware.formats & (1ULL << i))
  729. snd_iprintf(buffer, " %s", snd_pcm_format_name(i));
  730. }
  731. }
  732. static void print_rates(struct snd_info_buffer *buffer)
  733. {
  734. static int rates[] = {
  735. 5512, 8000, 11025, 16000, 22050, 32000, 44100, 48000,
  736. 64000, 88200, 96000, 176400, 192000,
  737. };
  738. int i;
  739. if (dummy_pcm_hardware.rates & SNDRV_PCM_RATE_CONTINUOUS)
  740. snd_iprintf(buffer, " continuous");
  741. if (dummy_pcm_hardware.rates & SNDRV_PCM_RATE_KNOT)
  742. snd_iprintf(buffer, " knot");
  743. for (i = 0; i < ARRAY_SIZE(rates); i++)
  744. if (dummy_pcm_hardware.rates & (1 << i))
  745. snd_iprintf(buffer, " %d", rates[i]);
  746. }
  747. #define get_dummy_int_ptr(ofs) \
  748. (unsigned int *)((char *)&dummy_pcm_hardware + (ofs))
  749. #define get_dummy_ll_ptr(ofs) \
  750. (unsigned long long *)((char *)&dummy_pcm_hardware + (ofs))
  751. struct dummy_hw_field {
  752. const char *name;
  753. const char *format;
  754. unsigned int offset;
  755. unsigned int size;
  756. };
  757. #define FIELD_ENTRY(item, fmt) { \
  758. .name = #item, \
  759. .format = fmt, \
  760. .offset = offsetof(struct snd_pcm_hardware, item), \
  761. .size = sizeof(dummy_pcm_hardware.item) }
  762. static struct dummy_hw_field fields[] = {
  763. FIELD_ENTRY(formats, "%#llx"),
  764. FIELD_ENTRY(rates, "%#x"),
  765. FIELD_ENTRY(rate_min, "%d"),
  766. FIELD_ENTRY(rate_max, "%d"),
  767. FIELD_ENTRY(channels_min, "%d"),
  768. FIELD_ENTRY(channels_max, "%d"),
  769. FIELD_ENTRY(buffer_bytes_max, "%ld"),
  770. FIELD_ENTRY(period_bytes_min, "%ld"),
  771. FIELD_ENTRY(period_bytes_max, "%ld"),
  772. FIELD_ENTRY(periods_min, "%d"),
  773. FIELD_ENTRY(periods_max, "%d"),
  774. };
  775. static void dummy_proc_read(struct snd_info_entry *entry,
  776. struct snd_info_buffer *buffer)
  777. {
  778. int i;
  779. for (i = 0; i < ARRAY_SIZE(fields); i++) {
  780. snd_iprintf(buffer, "%s ", fields[i].name);
  781. if (fields[i].size == sizeof(int))
  782. snd_iprintf(buffer, fields[i].format,
  783. *get_dummy_int_ptr(fields[i].offset));
  784. else
  785. snd_iprintf(buffer, fields[i].format,
  786. *get_dummy_ll_ptr(fields[i].offset));
  787. if (!strcmp(fields[i].name, "formats"))
  788. print_formats(buffer);
  789. else if (!strcmp(fields[i].name, "rates"))
  790. print_rates(buffer);
  791. snd_iprintf(buffer, "\n");
  792. }
  793. }
  794. static void dummy_proc_write(struct snd_info_entry *entry,
  795. struct snd_info_buffer *buffer)
  796. {
  797. char line[64];
  798. while (!snd_info_get_line(buffer, line, sizeof(line))) {
  799. char item[20];
  800. const char *ptr;
  801. unsigned long long val;
  802. int i;
  803. ptr = snd_info_get_str(item, line, sizeof(item));
  804. for (i = 0; i < ARRAY_SIZE(fields); i++) {
  805. if (!strcmp(item, fields[i].name))
  806. break;
  807. }
  808. if (i >= ARRAY_SIZE(fields))
  809. continue;
  810. snd_info_get_str(item, ptr, sizeof(item));
  811. if (strict_strtoull(item, 0, &val))
  812. continue;
  813. if (fields[i].size == sizeof(int))
  814. *get_dummy_int_ptr(fields[i].offset) = val;
  815. else
  816. *get_dummy_ll_ptr(fields[i].offset) = val;
  817. }
  818. }
  819. static void __devinit dummy_proc_init(struct snd_dummy *chip)
  820. {
  821. struct snd_info_entry *entry;
  822. if (!snd_card_proc_new(chip->card, "dummy_pcm", &entry)) {
  823. snd_info_set_text_ops(entry, chip, dummy_proc_read);
  824. entry->c.text.write = dummy_proc_write;
  825. entry->mode |= S_IWUSR;
  826. }
  827. }
  828. #else
  829. #define dummy_proc_init(x)
  830. #endif /* CONFIG_SND_DEBUG && CONFIG_PROC_FS */
  831. static int __devinit snd_dummy_probe(struct platform_device *devptr)
  832. {
  833. struct snd_card *card;
  834. struct snd_dummy *dummy;
  835. int idx, err;
  836. int dev = devptr->id;
  837. err = snd_card_create(index[dev], id[dev], THIS_MODULE,
  838. sizeof(struct snd_dummy), &card);
  839. if (err < 0)
  840. return err;
  841. dummy = card->private_data;
  842. dummy->card = card;
  843. for (idx = 0; idx < MAX_PCM_DEVICES && idx < pcm_devs[dev]; idx++) {
  844. if (pcm_substreams[dev] < 1)
  845. pcm_substreams[dev] = 1;
  846. if (pcm_substreams[dev] > MAX_PCM_SUBSTREAMS)
  847. pcm_substreams[dev] = MAX_PCM_SUBSTREAMS;
  848. err = snd_card_dummy_pcm(dummy, idx, pcm_substreams[dev]);
  849. if (err < 0)
  850. goto __nodev;
  851. }
  852. err = snd_card_dummy_new_mixer(dummy);
  853. if (err < 0)
  854. goto __nodev;
  855. strcpy(card->driver, "Dummy");
  856. strcpy(card->shortname, "Dummy");
  857. sprintf(card->longname, "Dummy %i", dev + 1);
  858. dummy_proc_init(dummy);
  859. snd_card_set_dev(card, &devptr->dev);
  860. err = snd_card_register(card);
  861. if (err == 0) {
  862. platform_set_drvdata(devptr, card);
  863. return 0;
  864. }
  865. __nodev:
  866. snd_card_free(card);
  867. return err;
  868. }
  869. static int __devexit snd_dummy_remove(struct platform_device *devptr)
  870. {
  871. snd_card_free(platform_get_drvdata(devptr));
  872. platform_set_drvdata(devptr, NULL);
  873. return 0;
  874. }
  875. #ifdef CONFIG_PM
  876. static int snd_dummy_suspend(struct platform_device *pdev, pm_message_t state)
  877. {
  878. struct snd_card *card = platform_get_drvdata(pdev);
  879. struct snd_dummy *dummy = card->private_data;
  880. snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
  881. snd_pcm_suspend_all(dummy->pcm);
  882. return 0;
  883. }
  884. static int snd_dummy_resume(struct platform_device *pdev)
  885. {
  886. struct snd_card *card = platform_get_drvdata(pdev);
  887. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  888. return 0;
  889. }
  890. #endif
  891. #define SND_DUMMY_DRIVER "snd_dummy"
  892. static struct platform_driver snd_dummy_driver = {
  893. .probe = snd_dummy_probe,
  894. .remove = __devexit_p(snd_dummy_remove),
  895. #ifdef CONFIG_PM
  896. .suspend = snd_dummy_suspend,
  897. .resume = snd_dummy_resume,
  898. #endif
  899. .driver = {
  900. .name = SND_DUMMY_DRIVER
  901. },
  902. };
  903. static void snd_dummy_unregister_all(void)
  904. {
  905. int i;
  906. for (i = 0; i < ARRAY_SIZE(devices); ++i)
  907. platform_device_unregister(devices[i]);
  908. platform_driver_unregister(&snd_dummy_driver);
  909. free_fake_buffer();
  910. }
  911. static int __init alsa_card_dummy_init(void)
  912. {
  913. int i, cards, err;
  914. err = platform_driver_register(&snd_dummy_driver);
  915. if (err < 0)
  916. return err;
  917. err = alloc_fake_buffer();
  918. if (err < 0) {
  919. platform_driver_unregister(&snd_dummy_driver);
  920. return err;
  921. }
  922. cards = 0;
  923. for (i = 0; i < SNDRV_CARDS; i++) {
  924. struct platform_device *device;
  925. if (! enable[i])
  926. continue;
  927. device = platform_device_register_simple(SND_DUMMY_DRIVER,
  928. i, NULL, 0);
  929. if (IS_ERR(device))
  930. continue;
  931. if (!platform_get_drvdata(device)) {
  932. platform_device_unregister(device);
  933. continue;
  934. }
  935. devices[i] = device;
  936. cards++;
  937. }
  938. if (!cards) {
  939. #ifdef MODULE
  940. printk(KERN_ERR "Dummy soundcard not found or device busy\n");
  941. #endif
  942. snd_dummy_unregister_all();
  943. return -ENODEV;
  944. }
  945. return 0;
  946. }
  947. static void __exit alsa_card_dummy_exit(void)
  948. {
  949. snd_dummy_unregister_all();
  950. }
  951. module_init(alsa_card_dummy_init)
  952. module_exit(alsa_card_dummy_exit)