dummy.c 29 KB

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