dummy.c 32 KB

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