dummy.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224
  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. tasklet_kill(&dpcm->tasklet);
  375. }
  376. static snd_pcm_uframes_t
  377. dummy_hrtimer_pointer(struct snd_pcm_substream *substream)
  378. {
  379. struct snd_pcm_runtime *runtime = substream->runtime;
  380. struct dummy_hrtimer_pcm *dpcm = runtime->private_data;
  381. u64 delta;
  382. u32 pos;
  383. delta = ktime_us_delta(hrtimer_cb_get_time(&dpcm->timer),
  384. dpcm->base_time);
  385. delta = div_u64(delta * runtime->rate + 999999, 1000000);
  386. div_u64_rem(delta, runtime->buffer_size, &pos);
  387. return pos;
  388. }
  389. static int dummy_hrtimer_prepare(struct snd_pcm_substream *substream)
  390. {
  391. struct snd_pcm_runtime *runtime = substream->runtime;
  392. struct dummy_hrtimer_pcm *dpcm = runtime->private_data;
  393. unsigned int period, rate;
  394. long sec;
  395. unsigned long nsecs;
  396. dummy_hrtimer_sync(dpcm);
  397. period = runtime->period_size;
  398. rate = runtime->rate;
  399. sec = period / rate;
  400. period %= rate;
  401. nsecs = div_u64((u64)period * 1000000000UL + rate - 1, rate);
  402. dpcm->period_time = ktime_set(sec, nsecs);
  403. return 0;
  404. }
  405. static int dummy_hrtimer_create(struct snd_pcm_substream *substream)
  406. {
  407. struct dummy_hrtimer_pcm *dpcm;
  408. dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
  409. if (!dpcm)
  410. return -ENOMEM;
  411. substream->runtime->private_data = dpcm;
  412. hrtimer_init(&dpcm->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  413. dpcm->timer.function = dummy_hrtimer_callback;
  414. dpcm->substream = substream;
  415. atomic_set(&dpcm->running, 0);
  416. tasklet_init(&dpcm->tasklet, dummy_hrtimer_pcm_elapsed,
  417. (unsigned long)dpcm);
  418. return 0;
  419. }
  420. static void dummy_hrtimer_free(struct snd_pcm_substream *substream)
  421. {
  422. struct dummy_hrtimer_pcm *dpcm = substream->runtime->private_data;
  423. dummy_hrtimer_sync(dpcm);
  424. kfree(dpcm);
  425. }
  426. static const struct dummy_timer_ops dummy_hrtimer_ops = {
  427. .create = dummy_hrtimer_create,
  428. .free = dummy_hrtimer_free,
  429. .prepare = dummy_hrtimer_prepare,
  430. .start = dummy_hrtimer_start,
  431. .stop = dummy_hrtimer_stop,
  432. .pointer = dummy_hrtimer_pointer,
  433. };
  434. #endif /* CONFIG_HIGH_RES_TIMERS */
  435. /*
  436. * PCM interface
  437. */
  438. static int dummy_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  439. {
  440. switch (cmd) {
  441. case SNDRV_PCM_TRIGGER_START:
  442. case SNDRV_PCM_TRIGGER_RESUME:
  443. return get_dummy_ops(substream)->start(substream);
  444. case SNDRV_PCM_TRIGGER_STOP:
  445. case SNDRV_PCM_TRIGGER_SUSPEND:
  446. return get_dummy_ops(substream)->stop(substream);
  447. }
  448. return -EINVAL;
  449. }
  450. static int dummy_pcm_prepare(struct snd_pcm_substream *substream)
  451. {
  452. return get_dummy_ops(substream)->prepare(substream);
  453. }
  454. static snd_pcm_uframes_t dummy_pcm_pointer(struct snd_pcm_substream *substream)
  455. {
  456. return get_dummy_ops(substream)->pointer(substream);
  457. }
  458. static struct snd_pcm_hardware dummy_pcm_hardware = {
  459. .info = (SNDRV_PCM_INFO_MMAP |
  460. SNDRV_PCM_INFO_INTERLEAVED |
  461. SNDRV_PCM_INFO_RESUME |
  462. SNDRV_PCM_INFO_MMAP_VALID),
  463. .formats = USE_FORMATS,
  464. .rates = USE_RATE,
  465. .rate_min = USE_RATE_MIN,
  466. .rate_max = USE_RATE_MAX,
  467. .channels_min = USE_CHANNELS_MIN,
  468. .channels_max = USE_CHANNELS_MAX,
  469. .buffer_bytes_max = MAX_BUFFER_SIZE,
  470. .period_bytes_min = MIN_PERIOD_SIZE,
  471. .period_bytes_max = MAX_PERIOD_SIZE,
  472. .periods_min = USE_PERIODS_MIN,
  473. .periods_max = USE_PERIODS_MAX,
  474. .fifo_size = 0,
  475. };
  476. static int dummy_pcm_hw_params(struct snd_pcm_substream *substream,
  477. struct snd_pcm_hw_params *hw_params)
  478. {
  479. if (fake_buffer) {
  480. /* runtime->dma_bytes has to be set manually to allow mmap */
  481. substream->runtime->dma_bytes = params_buffer_bytes(hw_params);
  482. return 0;
  483. }
  484. return snd_pcm_lib_malloc_pages(substream,
  485. params_buffer_bytes(hw_params));
  486. }
  487. static int dummy_pcm_hw_free(struct snd_pcm_substream *substream)
  488. {
  489. if (fake_buffer)
  490. return 0;
  491. return snd_pcm_lib_free_pages(substream);
  492. }
  493. static int dummy_pcm_open(struct snd_pcm_substream *substream)
  494. {
  495. struct snd_dummy *dummy = snd_pcm_substream_chip(substream);
  496. struct dummy_model *model = dummy->model;
  497. struct snd_pcm_runtime *runtime = substream->runtime;
  498. const struct dummy_timer_ops *ops;
  499. int err;
  500. ops = &dummy_systimer_ops;
  501. #ifdef CONFIG_HIGH_RES_TIMERS
  502. if (hrtimer)
  503. ops = &dummy_hrtimer_ops;
  504. #endif
  505. err = ops->create(substream);
  506. if (err < 0)
  507. return err;
  508. get_dummy_ops(substream) = ops;
  509. runtime->hw = dummy->pcm_hw;
  510. if (substream->pcm->device & 1) {
  511. runtime->hw.info &= ~SNDRV_PCM_INFO_INTERLEAVED;
  512. runtime->hw.info |= SNDRV_PCM_INFO_NONINTERLEAVED;
  513. }
  514. if (substream->pcm->device & 2)
  515. runtime->hw.info &= ~(SNDRV_PCM_INFO_MMAP |
  516. SNDRV_PCM_INFO_MMAP_VALID);
  517. if (model == NULL)
  518. return 0;
  519. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  520. if (model->playback_constraints)
  521. err = model->playback_constraints(substream->runtime);
  522. } else {
  523. if (model->capture_constraints)
  524. err = model->capture_constraints(substream->runtime);
  525. }
  526. if (err < 0) {
  527. get_dummy_ops(substream)->free(substream);
  528. return err;
  529. }
  530. return 0;
  531. }
  532. static int dummy_pcm_close(struct snd_pcm_substream *substream)
  533. {
  534. get_dummy_ops(substream)->free(substream);
  535. return 0;
  536. }
  537. /*
  538. * dummy buffer handling
  539. */
  540. static void *dummy_page[2];
  541. static void free_fake_buffer(void)
  542. {
  543. if (fake_buffer) {
  544. int i;
  545. for (i = 0; i < 2; i++)
  546. if (dummy_page[i]) {
  547. free_page((unsigned long)dummy_page[i]);
  548. dummy_page[i] = NULL;
  549. }
  550. }
  551. }
  552. static int alloc_fake_buffer(void)
  553. {
  554. int i;
  555. if (!fake_buffer)
  556. return 0;
  557. for (i = 0; i < 2; i++) {
  558. dummy_page[i] = (void *)get_zeroed_page(GFP_KERNEL);
  559. if (!dummy_page[i]) {
  560. free_fake_buffer();
  561. return -ENOMEM;
  562. }
  563. }
  564. return 0;
  565. }
  566. static int dummy_pcm_copy(struct snd_pcm_substream *substream,
  567. int channel, snd_pcm_uframes_t pos,
  568. void __user *dst, snd_pcm_uframes_t count)
  569. {
  570. return 0; /* do nothing */
  571. }
  572. static int dummy_pcm_silence(struct snd_pcm_substream *substream,
  573. int channel, snd_pcm_uframes_t pos,
  574. snd_pcm_uframes_t count)
  575. {
  576. return 0; /* do nothing */
  577. }
  578. static struct page *dummy_pcm_page(struct snd_pcm_substream *substream,
  579. unsigned long offset)
  580. {
  581. return virt_to_page(dummy_page[substream->stream]); /* the same page */
  582. }
  583. static struct snd_pcm_ops dummy_pcm_ops = {
  584. .open = dummy_pcm_open,
  585. .close = dummy_pcm_close,
  586. .ioctl = snd_pcm_lib_ioctl,
  587. .hw_params = dummy_pcm_hw_params,
  588. .hw_free = dummy_pcm_hw_free,
  589. .prepare = dummy_pcm_prepare,
  590. .trigger = dummy_pcm_trigger,
  591. .pointer = dummy_pcm_pointer,
  592. };
  593. static struct snd_pcm_ops dummy_pcm_ops_no_buf = {
  594. .open = dummy_pcm_open,
  595. .close = dummy_pcm_close,
  596. .ioctl = snd_pcm_lib_ioctl,
  597. .hw_params = dummy_pcm_hw_params,
  598. .hw_free = dummy_pcm_hw_free,
  599. .prepare = dummy_pcm_prepare,
  600. .trigger = dummy_pcm_trigger,
  601. .pointer = dummy_pcm_pointer,
  602. .copy = dummy_pcm_copy,
  603. .silence = dummy_pcm_silence,
  604. .page = dummy_pcm_page,
  605. };
  606. static int snd_card_dummy_pcm(struct snd_dummy *dummy, int device,
  607. int substreams)
  608. {
  609. struct snd_pcm *pcm;
  610. struct snd_pcm_ops *ops;
  611. int err;
  612. err = snd_pcm_new(dummy->card, "Dummy PCM", device,
  613. substreams, substreams, &pcm);
  614. if (err < 0)
  615. return err;
  616. dummy->pcm = pcm;
  617. if (fake_buffer)
  618. ops = &dummy_pcm_ops_no_buf;
  619. else
  620. ops = &dummy_pcm_ops;
  621. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, ops);
  622. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, ops);
  623. pcm->private_data = dummy;
  624. pcm->info_flags = 0;
  625. strcpy(pcm->name, "Dummy PCM");
  626. if (!fake_buffer) {
  627. snd_pcm_lib_preallocate_pages_for_all(pcm,
  628. SNDRV_DMA_TYPE_CONTINUOUS,
  629. snd_dma_continuous_data(GFP_KERNEL),
  630. 0, 64*1024);
  631. }
  632. return 0;
  633. }
  634. /*
  635. * mixer interface
  636. */
  637. #define DUMMY_VOLUME(xname, xindex, addr) \
  638. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
  639. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
  640. .name = xname, .index = xindex, \
  641. .info = snd_dummy_volume_info, \
  642. .get = snd_dummy_volume_get, .put = snd_dummy_volume_put, \
  643. .private_value = addr, \
  644. .tlv = { .p = db_scale_dummy } }
  645. static int snd_dummy_volume_info(struct snd_kcontrol *kcontrol,
  646. struct snd_ctl_elem_info *uinfo)
  647. {
  648. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  649. uinfo->count = 2;
  650. uinfo->value.integer.min = -50;
  651. uinfo->value.integer.max = 100;
  652. return 0;
  653. }
  654. static int snd_dummy_volume_get(struct snd_kcontrol *kcontrol,
  655. struct snd_ctl_elem_value *ucontrol)
  656. {
  657. struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
  658. int addr = kcontrol->private_value;
  659. spin_lock_irq(&dummy->mixer_lock);
  660. ucontrol->value.integer.value[0] = dummy->mixer_volume[addr][0];
  661. ucontrol->value.integer.value[1] = dummy->mixer_volume[addr][1];
  662. spin_unlock_irq(&dummy->mixer_lock);
  663. return 0;
  664. }
  665. static int snd_dummy_volume_put(struct snd_kcontrol *kcontrol,
  666. struct snd_ctl_elem_value *ucontrol)
  667. {
  668. struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
  669. int change, addr = kcontrol->private_value;
  670. int left, right;
  671. left = ucontrol->value.integer.value[0];
  672. if (left < -50)
  673. left = -50;
  674. if (left > 100)
  675. left = 100;
  676. right = ucontrol->value.integer.value[1];
  677. if (right < -50)
  678. right = -50;
  679. if (right > 100)
  680. right = 100;
  681. spin_lock_irq(&dummy->mixer_lock);
  682. change = dummy->mixer_volume[addr][0] != left ||
  683. dummy->mixer_volume[addr][1] != right;
  684. dummy->mixer_volume[addr][0] = left;
  685. dummy->mixer_volume[addr][1] = right;
  686. spin_unlock_irq(&dummy->mixer_lock);
  687. return change;
  688. }
  689. static const DECLARE_TLV_DB_SCALE(db_scale_dummy, -4500, 30, 0);
  690. #define DUMMY_CAPSRC(xname, xindex, addr) \
  691. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
  692. .info = snd_dummy_capsrc_info, \
  693. .get = snd_dummy_capsrc_get, .put = snd_dummy_capsrc_put, \
  694. .private_value = addr }
  695. #define snd_dummy_capsrc_info snd_ctl_boolean_stereo_info
  696. static int snd_dummy_capsrc_get(struct snd_kcontrol *kcontrol,
  697. struct snd_ctl_elem_value *ucontrol)
  698. {
  699. struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
  700. int addr = kcontrol->private_value;
  701. spin_lock_irq(&dummy->mixer_lock);
  702. ucontrol->value.integer.value[0] = dummy->capture_source[addr][0];
  703. ucontrol->value.integer.value[1] = dummy->capture_source[addr][1];
  704. spin_unlock_irq(&dummy->mixer_lock);
  705. return 0;
  706. }
  707. static int snd_dummy_capsrc_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  708. {
  709. struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
  710. int change, addr = kcontrol->private_value;
  711. int left, right;
  712. left = ucontrol->value.integer.value[0] & 1;
  713. right = ucontrol->value.integer.value[1] & 1;
  714. spin_lock_irq(&dummy->mixer_lock);
  715. change = dummy->capture_source[addr][0] != left &&
  716. dummy->capture_source[addr][1] != right;
  717. dummy->capture_source[addr][0] = left;
  718. dummy->capture_source[addr][1] = right;
  719. spin_unlock_irq(&dummy->mixer_lock);
  720. return change;
  721. }
  722. static int snd_dummy_iobox_info(struct snd_kcontrol *kcontrol,
  723. struct snd_ctl_elem_info *info)
  724. {
  725. const char *const names[] = { "None", "CD Player" };
  726. return snd_ctl_enum_info(info, 1, 2, names);
  727. }
  728. static int snd_dummy_iobox_get(struct snd_kcontrol *kcontrol,
  729. struct snd_ctl_elem_value *value)
  730. {
  731. struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
  732. value->value.enumerated.item[0] = dummy->iobox;
  733. return 0;
  734. }
  735. static int snd_dummy_iobox_put(struct snd_kcontrol *kcontrol,
  736. struct snd_ctl_elem_value *value)
  737. {
  738. struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
  739. int changed;
  740. if (value->value.enumerated.item[0] > 1)
  741. return -EINVAL;
  742. changed = value->value.enumerated.item[0] != dummy->iobox;
  743. if (changed) {
  744. dummy->iobox = value->value.enumerated.item[0];
  745. if (dummy->iobox) {
  746. dummy->cd_volume_ctl->vd[0].access &=
  747. ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  748. dummy->cd_switch_ctl->vd[0].access &=
  749. ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  750. } else {
  751. dummy->cd_volume_ctl->vd[0].access |=
  752. SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  753. dummy->cd_switch_ctl->vd[0].access |=
  754. SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  755. }
  756. snd_ctl_notify(dummy->card, SNDRV_CTL_EVENT_MASK_INFO,
  757. &dummy->cd_volume_ctl->id);
  758. snd_ctl_notify(dummy->card, SNDRV_CTL_EVENT_MASK_INFO,
  759. &dummy->cd_switch_ctl->id);
  760. }
  761. return changed;
  762. }
  763. static struct snd_kcontrol_new snd_dummy_controls[] = {
  764. DUMMY_VOLUME("Master Volume", 0, MIXER_ADDR_MASTER),
  765. DUMMY_CAPSRC("Master Capture Switch", 0, MIXER_ADDR_MASTER),
  766. DUMMY_VOLUME("Synth Volume", 0, MIXER_ADDR_SYNTH),
  767. DUMMY_CAPSRC("Synth Capture Switch", 0, MIXER_ADDR_SYNTH),
  768. DUMMY_VOLUME("Line Volume", 0, MIXER_ADDR_LINE),
  769. DUMMY_CAPSRC("Line Capture Switch", 0, MIXER_ADDR_LINE),
  770. DUMMY_VOLUME("Mic Volume", 0, MIXER_ADDR_MIC),
  771. DUMMY_CAPSRC("Mic Capture Switch", 0, MIXER_ADDR_MIC),
  772. DUMMY_VOLUME("CD Volume", 0, MIXER_ADDR_CD),
  773. DUMMY_CAPSRC("CD Capture Switch", 0, MIXER_ADDR_CD),
  774. {
  775. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  776. .name = "External I/O Box",
  777. .info = snd_dummy_iobox_info,
  778. .get = snd_dummy_iobox_get,
  779. .put = snd_dummy_iobox_put,
  780. },
  781. };
  782. static int snd_card_dummy_new_mixer(struct snd_dummy *dummy)
  783. {
  784. struct snd_card *card = dummy->card;
  785. struct snd_kcontrol *kcontrol;
  786. unsigned int idx;
  787. int err;
  788. spin_lock_init(&dummy->mixer_lock);
  789. strcpy(card->mixername, "Dummy Mixer");
  790. dummy->iobox = 1;
  791. for (idx = 0; idx < ARRAY_SIZE(snd_dummy_controls); idx++) {
  792. kcontrol = snd_ctl_new1(&snd_dummy_controls[idx], dummy);
  793. err = snd_ctl_add(card, kcontrol);
  794. if (err < 0)
  795. return err;
  796. if (!strcmp(kcontrol->id.name, "CD Volume"))
  797. dummy->cd_volume_ctl = kcontrol;
  798. else if (!strcmp(kcontrol->id.name, "CD Capture Switch"))
  799. dummy->cd_switch_ctl = kcontrol;
  800. }
  801. return 0;
  802. }
  803. #if defined(CONFIG_SND_DEBUG) && defined(CONFIG_SND_PROC_FS)
  804. /*
  805. * proc interface
  806. */
  807. static void print_formats(struct snd_dummy *dummy,
  808. struct snd_info_buffer *buffer)
  809. {
  810. int i;
  811. for (i = 0; i < SNDRV_PCM_FORMAT_LAST; i++) {
  812. if (dummy->pcm_hw.formats & (1ULL << i))
  813. snd_iprintf(buffer, " %s", snd_pcm_format_name(i));
  814. }
  815. }
  816. static void print_rates(struct snd_dummy *dummy,
  817. struct snd_info_buffer *buffer)
  818. {
  819. static int rates[] = {
  820. 5512, 8000, 11025, 16000, 22050, 32000, 44100, 48000,
  821. 64000, 88200, 96000, 176400, 192000,
  822. };
  823. int i;
  824. if (dummy->pcm_hw.rates & SNDRV_PCM_RATE_CONTINUOUS)
  825. snd_iprintf(buffer, " continuous");
  826. if (dummy->pcm_hw.rates & SNDRV_PCM_RATE_KNOT)
  827. snd_iprintf(buffer, " knot");
  828. for (i = 0; i < ARRAY_SIZE(rates); i++)
  829. if (dummy->pcm_hw.rates & (1 << i))
  830. snd_iprintf(buffer, " %d", rates[i]);
  831. }
  832. #define get_dummy_int_ptr(dummy, ofs) \
  833. (unsigned int *)((char *)&((dummy)->pcm_hw) + (ofs))
  834. #define get_dummy_ll_ptr(dummy, ofs) \
  835. (unsigned long long *)((char *)&((dummy)->pcm_hw) + (ofs))
  836. struct dummy_hw_field {
  837. const char *name;
  838. const char *format;
  839. unsigned int offset;
  840. unsigned int size;
  841. };
  842. #define FIELD_ENTRY(item, fmt) { \
  843. .name = #item, \
  844. .format = fmt, \
  845. .offset = offsetof(struct snd_pcm_hardware, item), \
  846. .size = sizeof(dummy_pcm_hardware.item) }
  847. static struct dummy_hw_field fields[] = {
  848. FIELD_ENTRY(formats, "%#llx"),
  849. FIELD_ENTRY(rates, "%#x"),
  850. FIELD_ENTRY(rate_min, "%d"),
  851. FIELD_ENTRY(rate_max, "%d"),
  852. FIELD_ENTRY(channels_min, "%d"),
  853. FIELD_ENTRY(channels_max, "%d"),
  854. FIELD_ENTRY(buffer_bytes_max, "%ld"),
  855. FIELD_ENTRY(period_bytes_min, "%ld"),
  856. FIELD_ENTRY(period_bytes_max, "%ld"),
  857. FIELD_ENTRY(periods_min, "%d"),
  858. FIELD_ENTRY(periods_max, "%d"),
  859. };
  860. static void dummy_proc_read(struct snd_info_entry *entry,
  861. struct snd_info_buffer *buffer)
  862. {
  863. struct snd_dummy *dummy = entry->private_data;
  864. int i;
  865. for (i = 0; i < ARRAY_SIZE(fields); i++) {
  866. snd_iprintf(buffer, "%s ", fields[i].name);
  867. if (fields[i].size == sizeof(int))
  868. snd_iprintf(buffer, fields[i].format,
  869. *get_dummy_int_ptr(dummy, fields[i].offset));
  870. else
  871. snd_iprintf(buffer, fields[i].format,
  872. *get_dummy_ll_ptr(dummy, fields[i].offset));
  873. if (!strcmp(fields[i].name, "formats"))
  874. print_formats(dummy, buffer);
  875. else if (!strcmp(fields[i].name, "rates"))
  876. print_rates(dummy, buffer);
  877. snd_iprintf(buffer, "\n");
  878. }
  879. }
  880. static void dummy_proc_write(struct snd_info_entry *entry,
  881. struct snd_info_buffer *buffer)
  882. {
  883. struct snd_dummy *dummy = entry->private_data;
  884. char line[64];
  885. while (!snd_info_get_line(buffer, line, sizeof(line))) {
  886. char item[20];
  887. const char *ptr;
  888. unsigned long long val;
  889. int i;
  890. ptr = snd_info_get_str(item, line, sizeof(item));
  891. for (i = 0; i < ARRAY_SIZE(fields); i++) {
  892. if (!strcmp(item, fields[i].name))
  893. break;
  894. }
  895. if (i >= ARRAY_SIZE(fields))
  896. continue;
  897. snd_info_get_str(item, ptr, sizeof(item));
  898. if (kstrtoull(item, 0, &val))
  899. continue;
  900. if (fields[i].size == sizeof(int))
  901. *get_dummy_int_ptr(dummy, fields[i].offset) = val;
  902. else
  903. *get_dummy_ll_ptr(dummy, fields[i].offset) = val;
  904. }
  905. }
  906. static void dummy_proc_init(struct snd_dummy *chip)
  907. {
  908. struct snd_info_entry *entry;
  909. if (!snd_card_proc_new(chip->card, "dummy_pcm", &entry)) {
  910. snd_info_set_text_ops(entry, chip, dummy_proc_read);
  911. entry->c.text.write = dummy_proc_write;
  912. entry->mode |= S_IWUSR;
  913. entry->private_data = chip;
  914. }
  915. }
  916. #else
  917. #define dummy_proc_init(x)
  918. #endif /* CONFIG_SND_DEBUG && CONFIG_SND_PROC_FS */
  919. static int snd_dummy_probe(struct platform_device *devptr)
  920. {
  921. struct snd_card *card;
  922. struct snd_dummy *dummy;
  923. struct dummy_model *m = NULL, **mdl;
  924. int idx, err;
  925. int dev = devptr->id;
  926. err = snd_card_new(&devptr->dev, index[dev], id[dev], THIS_MODULE,
  927. sizeof(struct snd_dummy), &card);
  928. if (err < 0)
  929. return err;
  930. dummy = card->private_data;
  931. dummy->card = card;
  932. for (mdl = dummy_models; *mdl && model[dev]; mdl++) {
  933. if (strcmp(model[dev], (*mdl)->name) == 0) {
  934. printk(KERN_INFO
  935. "snd-dummy: Using model '%s' for card %i\n",
  936. (*mdl)->name, card->number);
  937. m = dummy->model = *mdl;
  938. break;
  939. }
  940. }
  941. for (idx = 0; idx < MAX_PCM_DEVICES && idx < pcm_devs[dev]; idx++) {
  942. if (pcm_substreams[dev] < 1)
  943. pcm_substreams[dev] = 1;
  944. if (pcm_substreams[dev] > MAX_PCM_SUBSTREAMS)
  945. pcm_substreams[dev] = MAX_PCM_SUBSTREAMS;
  946. err = snd_card_dummy_pcm(dummy, idx, pcm_substreams[dev]);
  947. if (err < 0)
  948. goto __nodev;
  949. }
  950. dummy->pcm_hw = dummy_pcm_hardware;
  951. if (m) {
  952. if (m->formats)
  953. dummy->pcm_hw.formats = m->formats;
  954. if (m->buffer_bytes_max)
  955. dummy->pcm_hw.buffer_bytes_max = m->buffer_bytes_max;
  956. if (m->period_bytes_min)
  957. dummy->pcm_hw.period_bytes_min = m->period_bytes_min;
  958. if (m->period_bytes_max)
  959. dummy->pcm_hw.period_bytes_max = m->period_bytes_max;
  960. if (m->periods_min)
  961. dummy->pcm_hw.periods_min = m->periods_min;
  962. if (m->periods_max)
  963. dummy->pcm_hw.periods_max = m->periods_max;
  964. if (m->rates)
  965. dummy->pcm_hw.rates = m->rates;
  966. if (m->rate_min)
  967. dummy->pcm_hw.rate_min = m->rate_min;
  968. if (m->rate_max)
  969. dummy->pcm_hw.rate_max = m->rate_max;
  970. if (m->channels_min)
  971. dummy->pcm_hw.channels_min = m->channels_min;
  972. if (m->channels_max)
  973. dummy->pcm_hw.channels_max = m->channels_max;
  974. }
  975. err = snd_card_dummy_new_mixer(dummy);
  976. if (err < 0)
  977. goto __nodev;
  978. strcpy(card->driver, "Dummy");
  979. strcpy(card->shortname, "Dummy");
  980. sprintf(card->longname, "Dummy %i", dev + 1);
  981. dummy_proc_init(dummy);
  982. err = snd_card_register(card);
  983. if (err == 0) {
  984. platform_set_drvdata(devptr, card);
  985. return 0;
  986. }
  987. __nodev:
  988. snd_card_free(card);
  989. return err;
  990. }
  991. static int snd_dummy_remove(struct platform_device *devptr)
  992. {
  993. snd_card_free(platform_get_drvdata(devptr));
  994. return 0;
  995. }
  996. #ifdef CONFIG_PM_SLEEP
  997. static int snd_dummy_suspend(struct device *pdev)
  998. {
  999. struct snd_card *card = dev_get_drvdata(pdev);
  1000. struct snd_dummy *dummy = card->private_data;
  1001. snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
  1002. snd_pcm_suspend_all(dummy->pcm);
  1003. return 0;
  1004. }
  1005. static int snd_dummy_resume(struct device *pdev)
  1006. {
  1007. struct snd_card *card = dev_get_drvdata(pdev);
  1008. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  1009. return 0;
  1010. }
  1011. static SIMPLE_DEV_PM_OPS(snd_dummy_pm, snd_dummy_suspend, snd_dummy_resume);
  1012. #define SND_DUMMY_PM_OPS &snd_dummy_pm
  1013. #else
  1014. #define SND_DUMMY_PM_OPS NULL
  1015. #endif
  1016. #define SND_DUMMY_DRIVER "snd_dummy"
  1017. static struct platform_driver snd_dummy_driver = {
  1018. .probe = snd_dummy_probe,
  1019. .remove = snd_dummy_remove,
  1020. .driver = {
  1021. .name = SND_DUMMY_DRIVER,
  1022. .pm = SND_DUMMY_PM_OPS,
  1023. },
  1024. };
  1025. static void snd_dummy_unregister_all(void)
  1026. {
  1027. int i;
  1028. for (i = 0; i < ARRAY_SIZE(devices); ++i)
  1029. platform_device_unregister(devices[i]);
  1030. platform_driver_unregister(&snd_dummy_driver);
  1031. free_fake_buffer();
  1032. }
  1033. static int __init alsa_card_dummy_init(void)
  1034. {
  1035. int i, cards, err;
  1036. err = platform_driver_register(&snd_dummy_driver);
  1037. if (err < 0)
  1038. return err;
  1039. err = alloc_fake_buffer();
  1040. if (err < 0) {
  1041. platform_driver_unregister(&snd_dummy_driver);
  1042. return err;
  1043. }
  1044. cards = 0;
  1045. for (i = 0; i < SNDRV_CARDS; i++) {
  1046. struct platform_device *device;
  1047. if (! enable[i])
  1048. continue;
  1049. device = platform_device_register_simple(SND_DUMMY_DRIVER,
  1050. i, NULL, 0);
  1051. if (IS_ERR(device))
  1052. continue;
  1053. if (!platform_get_drvdata(device)) {
  1054. platform_device_unregister(device);
  1055. continue;
  1056. }
  1057. devices[i] = device;
  1058. cards++;
  1059. }
  1060. if (!cards) {
  1061. #ifdef MODULE
  1062. printk(KERN_ERR "Dummy soundcard not found or device busy\n");
  1063. #endif
  1064. snd_dummy_unregister_all();
  1065. return -ENODEV;
  1066. }
  1067. return 0;
  1068. }
  1069. static void __exit alsa_card_dummy_exit(void)
  1070. {
  1071. snd_dummy_unregister_all();
  1072. }
  1073. module_init(alsa_card_dummy_init)
  1074. module_exit(alsa_card_dummy_exit)