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