dummy.c 31 KB

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