dummy.c 23 KB

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