dummy.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. /*
  2. * Dummy soundcard
  3. * Copyright (c) by Jaroslav Kysela <perex@suse.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 <sound/driver.h>
  21. #include <linux/init.h>
  22. #include <linux/jiffies.h>
  23. #include <linux/slab.h>
  24. #include <linux/time.h>
  25. #include <linux/wait.h>
  26. #include <linux/moduleparam.h>
  27. #include <sound/core.h>
  28. #include <sound/control.h>
  29. #include <sound/pcm.h>
  30. #include <sound/rawmidi.h>
  31. #include <sound/initval.h>
  32. MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
  33. MODULE_DESCRIPTION("Dummy soundcard (/dev/null)");
  34. MODULE_LICENSE("GPL");
  35. MODULE_SUPPORTED_DEVICE("{{ALSA,Dummy soundcard}}");
  36. #define MAX_PCM_DEVICES 4
  37. #define MAX_PCM_SUBSTREAMS 16
  38. #define MAX_MIDI_DEVICES 2
  39. #if 0 /* emu10k1 emulation */
  40. #define MAX_BUFFER_SIZE (128 * 1024)
  41. static int emu10k1_playback_constraints(snd_pcm_runtime_t *runtime)
  42. {
  43. int err;
  44. if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
  45. return err;
  46. if ((err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 256, UINT_MAX)) < 0)
  47. return err;
  48. return 0;
  49. }
  50. #define add_playback_constraints emu10k1_playback_constraints
  51. #endif
  52. #if 0 /* RME9652 emulation */
  53. #define MAX_BUFFER_SIZE (26 * 64 * 1024)
  54. #define USE_FORMATS SNDRV_PCM_FMTBIT_S32_LE
  55. #define USE_CHANNELS_MIN 26
  56. #define USE_CHANNELS_MAX 26
  57. #define USE_PERIODS_MIN 2
  58. #define USE_PERIODS_MAX 2
  59. #endif
  60. #if 0 /* ICE1712 emulation */
  61. #define MAX_BUFFER_SIZE (256 * 1024)
  62. #define USE_FORMATS SNDRV_PCM_FMTBIT_S32_LE
  63. #define USE_CHANNELS_MIN 10
  64. #define USE_CHANNELS_MAX 10
  65. #define USE_PERIODS_MIN 1
  66. #define USE_PERIODS_MAX 1024
  67. #endif
  68. #if 0 /* UDA1341 emulation */
  69. #define MAX_BUFFER_SIZE (16380)
  70. #define USE_FORMATS SNDRV_PCM_FMTBIT_S16_LE
  71. #define USE_CHANNELS_MIN 2
  72. #define USE_CHANNELS_MAX 2
  73. #define USE_PERIODS_MIN 2
  74. #define USE_PERIODS_MAX 255
  75. #endif
  76. #if 0 /* simple AC97 bridge (intel8x0) with 48kHz AC97 only codec */
  77. #define USE_FORMATS SNDRV_PCM_FMTBIT_S16_LE
  78. #define USE_CHANNELS_MIN 2
  79. #define USE_CHANNELS_MAX 2
  80. #define USE_RATE SNDRV_PCM_RATE_48000
  81. #define USE_RATE_MIN 48000
  82. #define USE_RATE_MAX 48000
  83. #endif
  84. /* defaults */
  85. #ifndef MAX_BUFFER_SIZE
  86. #define MAX_BUFFER_SIZE (64*1024)
  87. #endif
  88. #ifndef USE_FORMATS
  89. #define USE_FORMATS (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE)
  90. #endif
  91. #ifndef USE_RATE
  92. #define USE_RATE SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000
  93. #define USE_RATE_MIN 5500
  94. #define USE_RATE_MAX 48000
  95. #endif
  96. #ifndef USE_CHANNELS_MIN
  97. #define USE_CHANNELS_MIN 1
  98. #endif
  99. #ifndef USE_CHANNELS_MAX
  100. #define USE_CHANNELS_MAX 2
  101. #endif
  102. #ifndef USE_PERIODS_MIN
  103. #define USE_PERIODS_MIN 1
  104. #endif
  105. #ifndef USE_PERIODS_MAX
  106. #define USE_PERIODS_MAX 1024
  107. #endif
  108. #ifndef add_playback_constraints
  109. #define add_playback_constraints(x) 0
  110. #endif
  111. #ifndef add_capture_constraints
  112. #define add_capture_constraints(x) 0
  113. #endif
  114. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
  115. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
  116. static int enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 0};
  117. static int pcm_devs[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1};
  118. static int pcm_substreams[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 8};
  119. //static int midi_devs[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 2};
  120. module_param_array(index, int, NULL, 0444);
  121. MODULE_PARM_DESC(index, "Index value for dummy soundcard.");
  122. module_param_array(id, charp, NULL, 0444);
  123. MODULE_PARM_DESC(id, "ID string for dummy soundcard.");
  124. module_param_array(enable, bool, NULL, 0444);
  125. MODULE_PARM_DESC(enable, "Enable this dummy soundcard.");
  126. module_param_array(pcm_devs, int, NULL, 0444);
  127. MODULE_PARM_DESC(pcm_devs, "PCM devices # (0-4) for dummy driver.");
  128. module_param_array(pcm_substreams, int, NULL, 0444);
  129. MODULE_PARM_DESC(pcm_substreams, "PCM substreams # (1-16) for dummy driver.");
  130. //module_param_array(midi_devs, int, NULL, 0444);
  131. //MODULE_PARM_DESC(midi_devs, "MIDI devices # (0-2) for dummy driver.");
  132. #define MIXER_ADDR_MASTER 0
  133. #define MIXER_ADDR_LINE 1
  134. #define MIXER_ADDR_MIC 2
  135. #define MIXER_ADDR_SYNTH 3
  136. #define MIXER_ADDR_CD 4
  137. #define MIXER_ADDR_LAST 4
  138. typedef struct snd_card_dummy {
  139. snd_card_t *card;
  140. spinlock_t mixer_lock;
  141. int mixer_volume[MIXER_ADDR_LAST+1][2];
  142. int capture_source[MIXER_ADDR_LAST+1][2];
  143. } snd_card_dummy_t;
  144. typedef struct snd_card_dummy_pcm {
  145. snd_card_dummy_t *dummy;
  146. spinlock_t lock;
  147. struct timer_list timer;
  148. unsigned int pcm_size;
  149. unsigned int pcm_count;
  150. unsigned int pcm_bps; /* bytes per second */
  151. unsigned int pcm_jiffie; /* bytes per one jiffie */
  152. unsigned int pcm_irq_pos; /* IRQ position */
  153. unsigned int pcm_buf_pos; /* position in buffer */
  154. snd_pcm_substream_t *substream;
  155. } snd_card_dummy_pcm_t;
  156. static snd_card_t *snd_dummy_cards[SNDRV_CARDS] = SNDRV_DEFAULT_PTR;
  157. static void snd_card_dummy_pcm_timer_start(snd_pcm_substream_t * substream)
  158. {
  159. snd_pcm_runtime_t *runtime = substream->runtime;
  160. snd_card_dummy_pcm_t *dpcm = runtime->private_data;
  161. dpcm->timer.expires = 1 + jiffies;
  162. add_timer(&dpcm->timer);
  163. }
  164. static void snd_card_dummy_pcm_timer_stop(snd_pcm_substream_t * substream)
  165. {
  166. snd_pcm_runtime_t *runtime = substream->runtime;
  167. snd_card_dummy_pcm_t *dpcm = runtime->private_data;
  168. del_timer(&dpcm->timer);
  169. }
  170. static int snd_card_dummy_playback_trigger(snd_pcm_substream_t * substream,
  171. int cmd)
  172. {
  173. if (cmd == SNDRV_PCM_TRIGGER_START) {
  174. snd_card_dummy_pcm_timer_start(substream);
  175. } else if (cmd == SNDRV_PCM_TRIGGER_STOP) {
  176. snd_card_dummy_pcm_timer_stop(substream);
  177. } else {
  178. return -EINVAL;
  179. }
  180. return 0;
  181. }
  182. static int snd_card_dummy_capture_trigger(snd_pcm_substream_t * substream,
  183. int cmd)
  184. {
  185. if (cmd == SNDRV_PCM_TRIGGER_START) {
  186. snd_card_dummy_pcm_timer_start(substream);
  187. } else if (cmd == SNDRV_PCM_TRIGGER_STOP) {
  188. snd_card_dummy_pcm_timer_stop(substream);
  189. } else {
  190. return -EINVAL;
  191. }
  192. return 0;
  193. }
  194. static int snd_card_dummy_pcm_prepare(snd_pcm_substream_t * substream)
  195. {
  196. snd_pcm_runtime_t *runtime = substream->runtime;
  197. snd_card_dummy_pcm_t *dpcm = runtime->private_data;
  198. unsigned int bps;
  199. bps = runtime->rate * runtime->channels;
  200. bps *= snd_pcm_format_width(runtime->format);
  201. bps /= 8;
  202. if (bps <= 0)
  203. return -EINVAL;
  204. dpcm->pcm_bps = bps;
  205. dpcm->pcm_jiffie = bps / HZ;
  206. dpcm->pcm_size = snd_pcm_lib_buffer_bytes(substream);
  207. dpcm->pcm_count = snd_pcm_lib_period_bytes(substream);
  208. dpcm->pcm_irq_pos = 0;
  209. dpcm->pcm_buf_pos = 0;
  210. return 0;
  211. }
  212. static int snd_card_dummy_playback_prepare(snd_pcm_substream_t * substream)
  213. {
  214. return snd_card_dummy_pcm_prepare(substream);
  215. }
  216. static int snd_card_dummy_capture_prepare(snd_pcm_substream_t * substream)
  217. {
  218. return snd_card_dummy_pcm_prepare(substream);
  219. }
  220. static void snd_card_dummy_pcm_timer_function(unsigned long data)
  221. {
  222. snd_card_dummy_pcm_t *dpcm = (snd_card_dummy_pcm_t *)data;
  223. dpcm->timer.expires = 1 + jiffies;
  224. add_timer(&dpcm->timer);
  225. spin_lock_irq(&dpcm->lock);
  226. dpcm->pcm_irq_pos += dpcm->pcm_jiffie;
  227. dpcm->pcm_buf_pos += dpcm->pcm_jiffie;
  228. dpcm->pcm_buf_pos %= dpcm->pcm_size;
  229. if (dpcm->pcm_irq_pos >= dpcm->pcm_count) {
  230. dpcm->pcm_irq_pos %= dpcm->pcm_count;
  231. snd_pcm_period_elapsed(dpcm->substream);
  232. }
  233. spin_unlock_irq(&dpcm->lock);
  234. }
  235. static snd_pcm_uframes_t snd_card_dummy_playback_pointer(snd_pcm_substream_t * substream)
  236. {
  237. snd_pcm_runtime_t *runtime = substream->runtime;
  238. snd_card_dummy_pcm_t *dpcm = runtime->private_data;
  239. return bytes_to_frames(runtime, dpcm->pcm_buf_pos);
  240. }
  241. static snd_pcm_uframes_t snd_card_dummy_capture_pointer(snd_pcm_substream_t * substream)
  242. {
  243. snd_pcm_runtime_t *runtime = substream->runtime;
  244. snd_card_dummy_pcm_t *dpcm = runtime->private_data;
  245. return bytes_to_frames(runtime, dpcm->pcm_buf_pos);
  246. }
  247. static snd_pcm_hardware_t snd_card_dummy_playback =
  248. {
  249. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  250. SNDRV_PCM_INFO_MMAP_VALID),
  251. .formats = USE_FORMATS,
  252. .rates = USE_RATE,
  253. .rate_min = USE_RATE_MIN,
  254. .rate_max = USE_RATE_MAX,
  255. .channels_min = USE_CHANNELS_MIN,
  256. .channels_max = USE_CHANNELS_MAX,
  257. .buffer_bytes_max = MAX_BUFFER_SIZE,
  258. .period_bytes_min = 64,
  259. .period_bytes_max = MAX_BUFFER_SIZE,
  260. .periods_min = USE_PERIODS_MIN,
  261. .periods_max = USE_PERIODS_MAX,
  262. .fifo_size = 0,
  263. };
  264. static snd_pcm_hardware_t snd_card_dummy_capture =
  265. {
  266. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  267. SNDRV_PCM_INFO_MMAP_VALID),
  268. .formats = USE_FORMATS,
  269. .rates = USE_RATE,
  270. .rate_min = USE_RATE_MIN,
  271. .rate_max = USE_RATE_MAX,
  272. .channels_min = USE_CHANNELS_MIN,
  273. .channels_max = USE_CHANNELS_MAX,
  274. .buffer_bytes_max = MAX_BUFFER_SIZE,
  275. .period_bytes_min = 64,
  276. .period_bytes_max = MAX_BUFFER_SIZE,
  277. .periods_min = USE_PERIODS_MIN,
  278. .periods_max = USE_PERIODS_MAX,
  279. .fifo_size = 0,
  280. };
  281. static void snd_card_dummy_runtime_free(snd_pcm_runtime_t *runtime)
  282. {
  283. snd_card_dummy_pcm_t *dpcm = runtime->private_data;
  284. kfree(dpcm);
  285. }
  286. static int snd_card_dummy_hw_params(snd_pcm_substream_t * substream,
  287. snd_pcm_hw_params_t * hw_params)
  288. {
  289. return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
  290. }
  291. static int snd_card_dummy_hw_free(snd_pcm_substream_t * substream)
  292. {
  293. return snd_pcm_lib_free_pages(substream);
  294. }
  295. static int snd_card_dummy_playback_open(snd_pcm_substream_t * substream)
  296. {
  297. snd_pcm_runtime_t *runtime = substream->runtime;
  298. snd_card_dummy_pcm_t *dpcm;
  299. int err;
  300. dpcm = kcalloc(1, sizeof(*dpcm), GFP_KERNEL);
  301. if (dpcm == NULL)
  302. return -ENOMEM;
  303. init_timer(&dpcm->timer);
  304. dpcm->timer.data = (unsigned long) dpcm;
  305. dpcm->timer.function = snd_card_dummy_pcm_timer_function;
  306. spin_lock_init(&dpcm->lock);
  307. dpcm->substream = substream;
  308. runtime->private_data = dpcm;
  309. runtime->private_free = snd_card_dummy_runtime_free;
  310. runtime->hw = snd_card_dummy_playback;
  311. if (substream->pcm->device & 1) {
  312. runtime->hw.info &= ~SNDRV_PCM_INFO_INTERLEAVED;
  313. runtime->hw.info |= SNDRV_PCM_INFO_NONINTERLEAVED;
  314. }
  315. if (substream->pcm->device & 2)
  316. runtime->hw.info &= ~(SNDRV_PCM_INFO_MMAP|SNDRV_PCM_INFO_MMAP_VALID);
  317. if ((err = add_playback_constraints(runtime)) < 0) {
  318. kfree(dpcm);
  319. return err;
  320. }
  321. return 0;
  322. }
  323. static int snd_card_dummy_capture_open(snd_pcm_substream_t * substream)
  324. {
  325. snd_pcm_runtime_t *runtime = substream->runtime;
  326. snd_card_dummy_pcm_t *dpcm;
  327. int err;
  328. dpcm = kcalloc(1, sizeof(*dpcm), GFP_KERNEL);
  329. if (dpcm == NULL)
  330. return -ENOMEM;
  331. init_timer(&dpcm->timer);
  332. dpcm->timer.data = (unsigned long) dpcm;
  333. dpcm->timer.function = snd_card_dummy_pcm_timer_function;
  334. spin_lock_init(&dpcm->lock);
  335. dpcm->substream = substream;
  336. runtime->private_data = dpcm;
  337. runtime->private_free = snd_card_dummy_runtime_free;
  338. runtime->hw = snd_card_dummy_capture;
  339. if (substream->pcm->device == 1) {
  340. runtime->hw.info &= ~SNDRV_PCM_INFO_INTERLEAVED;
  341. runtime->hw.info |= SNDRV_PCM_INFO_NONINTERLEAVED;
  342. }
  343. if (substream->pcm->device & 2)
  344. runtime->hw.info &= ~(SNDRV_PCM_INFO_MMAP|SNDRV_PCM_INFO_MMAP_VALID);
  345. if ((err = add_capture_constraints(runtime)) < 0) {
  346. kfree(dpcm);
  347. return err;
  348. }
  349. return 0;
  350. }
  351. static int snd_card_dummy_playback_close(snd_pcm_substream_t * substream)
  352. {
  353. return 0;
  354. }
  355. static int snd_card_dummy_capture_close(snd_pcm_substream_t * substream)
  356. {
  357. return 0;
  358. }
  359. static snd_pcm_ops_t snd_card_dummy_playback_ops = {
  360. .open = snd_card_dummy_playback_open,
  361. .close = snd_card_dummy_playback_close,
  362. .ioctl = snd_pcm_lib_ioctl,
  363. .hw_params = snd_card_dummy_hw_params,
  364. .hw_free = snd_card_dummy_hw_free,
  365. .prepare = snd_card_dummy_playback_prepare,
  366. .trigger = snd_card_dummy_playback_trigger,
  367. .pointer = snd_card_dummy_playback_pointer,
  368. };
  369. static snd_pcm_ops_t snd_card_dummy_capture_ops = {
  370. .open = snd_card_dummy_capture_open,
  371. .close = snd_card_dummy_capture_close,
  372. .ioctl = snd_pcm_lib_ioctl,
  373. .hw_params = snd_card_dummy_hw_params,
  374. .hw_free = snd_card_dummy_hw_free,
  375. .prepare = snd_card_dummy_capture_prepare,
  376. .trigger = snd_card_dummy_capture_trigger,
  377. .pointer = snd_card_dummy_capture_pointer,
  378. };
  379. static int __init snd_card_dummy_pcm(snd_card_dummy_t *dummy, int device, int substreams)
  380. {
  381. snd_pcm_t *pcm;
  382. int err;
  383. if ((err = snd_pcm_new(dummy->card, "Dummy PCM", device, substreams, substreams, &pcm)) < 0)
  384. return err;
  385. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_card_dummy_playback_ops);
  386. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_card_dummy_capture_ops);
  387. pcm->private_data = dummy;
  388. pcm->info_flags = 0;
  389. strcpy(pcm->name, "Dummy PCM");
  390. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
  391. snd_dma_continuous_data(GFP_KERNEL),
  392. 0, 64*1024);
  393. return 0;
  394. }
  395. #define DUMMY_VOLUME(xname, xindex, addr) \
  396. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
  397. .info = snd_dummy_volume_info, \
  398. .get = snd_dummy_volume_get, .put = snd_dummy_volume_put, \
  399. .private_value = addr }
  400. static int snd_dummy_volume_info(snd_kcontrol_t * kcontrol, snd_ctl_elem_info_t * uinfo)
  401. {
  402. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  403. uinfo->count = 2;
  404. uinfo->value.integer.min = -50;
  405. uinfo->value.integer.max = 100;
  406. return 0;
  407. }
  408. static int snd_dummy_volume_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
  409. {
  410. snd_card_dummy_t *dummy = snd_kcontrol_chip(kcontrol);
  411. unsigned long flags;
  412. int addr = kcontrol->private_value;
  413. spin_lock_irqsave(&dummy->mixer_lock, flags);
  414. ucontrol->value.integer.value[0] = dummy->mixer_volume[addr][0];
  415. ucontrol->value.integer.value[1] = dummy->mixer_volume[addr][1];
  416. spin_unlock_irqrestore(&dummy->mixer_lock, flags);
  417. return 0;
  418. }
  419. static int snd_dummy_volume_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
  420. {
  421. snd_card_dummy_t *dummy = snd_kcontrol_chip(kcontrol);
  422. unsigned long flags;
  423. int change, addr = kcontrol->private_value;
  424. int left, right;
  425. left = ucontrol->value.integer.value[0];
  426. if (left < -50)
  427. left = -50;
  428. if (left > 100)
  429. left = 100;
  430. right = ucontrol->value.integer.value[1];
  431. if (right < -50)
  432. right = -50;
  433. if (right > 100)
  434. right = 100;
  435. spin_lock_irqsave(&dummy->mixer_lock, flags);
  436. change = dummy->mixer_volume[addr][0] != left ||
  437. dummy->mixer_volume[addr][1] != right;
  438. dummy->mixer_volume[addr][0] = left;
  439. dummy->mixer_volume[addr][1] = right;
  440. spin_unlock_irqrestore(&dummy->mixer_lock, flags);
  441. return change;
  442. }
  443. #define DUMMY_CAPSRC(xname, xindex, addr) \
  444. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
  445. .info = snd_dummy_capsrc_info, \
  446. .get = snd_dummy_capsrc_get, .put = snd_dummy_capsrc_put, \
  447. .private_value = addr }
  448. static int snd_dummy_capsrc_info(snd_kcontrol_t * kcontrol, snd_ctl_elem_info_t * uinfo)
  449. {
  450. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  451. uinfo->count = 2;
  452. uinfo->value.integer.min = 0;
  453. uinfo->value.integer.max = 1;
  454. return 0;
  455. }
  456. static int snd_dummy_capsrc_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
  457. {
  458. snd_card_dummy_t *dummy = snd_kcontrol_chip(kcontrol);
  459. unsigned long flags;
  460. int addr = kcontrol->private_value;
  461. spin_lock_irqsave(&dummy->mixer_lock, flags);
  462. ucontrol->value.integer.value[0] = dummy->capture_source[addr][0];
  463. ucontrol->value.integer.value[1] = dummy->capture_source[addr][1];
  464. spin_unlock_irqrestore(&dummy->mixer_lock, flags);
  465. return 0;
  466. }
  467. static int snd_dummy_capsrc_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
  468. {
  469. snd_card_dummy_t *dummy = snd_kcontrol_chip(kcontrol);
  470. unsigned long flags;
  471. int change, addr = kcontrol->private_value;
  472. int left, right;
  473. left = ucontrol->value.integer.value[0] & 1;
  474. right = ucontrol->value.integer.value[1] & 1;
  475. spin_lock_irqsave(&dummy->mixer_lock, flags);
  476. change = dummy->capture_source[addr][0] != left &&
  477. dummy->capture_source[addr][1] != right;
  478. dummy->capture_source[addr][0] = left;
  479. dummy->capture_source[addr][1] = right;
  480. spin_unlock_irqrestore(&dummy->mixer_lock, flags);
  481. return change;
  482. }
  483. static snd_kcontrol_new_t snd_dummy_controls[] = {
  484. DUMMY_VOLUME("Master Volume", 0, MIXER_ADDR_MASTER),
  485. DUMMY_CAPSRC("Master Capture Switch", 0, MIXER_ADDR_MASTER),
  486. DUMMY_VOLUME("Synth Volume", 0, MIXER_ADDR_SYNTH),
  487. DUMMY_CAPSRC("Synth Capture Switch", 0, MIXER_ADDR_MASTER),
  488. DUMMY_VOLUME("Line Volume", 0, MIXER_ADDR_LINE),
  489. DUMMY_CAPSRC("Line Capture Switch", 0, MIXER_ADDR_MASTER),
  490. DUMMY_VOLUME("Mic Volume", 0, MIXER_ADDR_MIC),
  491. DUMMY_CAPSRC("Mic Capture Switch", 0, MIXER_ADDR_MASTER),
  492. DUMMY_VOLUME("CD Volume", 0, MIXER_ADDR_CD),
  493. DUMMY_CAPSRC("CD Capture Switch", 0, MIXER_ADDR_MASTER)
  494. };
  495. static int __init snd_card_dummy_new_mixer(snd_card_dummy_t * dummy)
  496. {
  497. snd_card_t *card = dummy->card;
  498. unsigned int idx;
  499. int err;
  500. snd_assert(dummy != NULL, return -EINVAL);
  501. spin_lock_init(&dummy->mixer_lock);
  502. strcpy(card->mixername, "Dummy Mixer");
  503. for (idx = 0; idx < ARRAY_SIZE(snd_dummy_controls); idx++) {
  504. if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_dummy_controls[idx], dummy))) < 0)
  505. return err;
  506. }
  507. return 0;
  508. }
  509. static int __init snd_card_dummy_probe(int dev)
  510. {
  511. snd_card_t *card;
  512. struct snd_card_dummy *dummy;
  513. int idx, err;
  514. if (!enable[dev])
  515. return -ENODEV;
  516. card = snd_card_new(index[dev], id[dev], THIS_MODULE,
  517. sizeof(struct snd_card_dummy));
  518. if (card == NULL)
  519. return -ENOMEM;
  520. dummy = (struct snd_card_dummy *)card->private_data;
  521. dummy->card = card;
  522. for (idx = 0; idx < MAX_PCM_DEVICES && idx < pcm_devs[dev]; idx++) {
  523. if (pcm_substreams[dev] < 1)
  524. pcm_substreams[dev] = 1;
  525. if (pcm_substreams[dev] > MAX_PCM_SUBSTREAMS)
  526. pcm_substreams[dev] = MAX_PCM_SUBSTREAMS;
  527. if ((err = snd_card_dummy_pcm(dummy, idx, pcm_substreams[dev])) < 0)
  528. goto __nodev;
  529. }
  530. if ((err = snd_card_dummy_new_mixer(dummy)) < 0)
  531. goto __nodev;
  532. strcpy(card->driver, "Dummy");
  533. strcpy(card->shortname, "Dummy");
  534. sprintf(card->longname, "Dummy %i", dev + 1);
  535. if ((err = snd_card_register(card)) == 0) {
  536. snd_dummy_cards[dev] = card;
  537. return 0;
  538. }
  539. __nodev:
  540. snd_card_free(card);
  541. return err;
  542. }
  543. static int __init alsa_card_dummy_init(void)
  544. {
  545. int dev, cards;
  546. for (dev = cards = 0; dev < SNDRV_CARDS && enable[dev]; dev++) {
  547. if (snd_card_dummy_probe(dev) < 0) {
  548. #ifdef MODULE
  549. printk(KERN_ERR "Dummy soundcard #%i not found or device busy\n", dev + 1);
  550. #endif
  551. break;
  552. }
  553. cards++;
  554. }
  555. if (!cards) {
  556. #ifdef MODULE
  557. printk(KERN_ERR "Dummy soundcard not found or device busy\n");
  558. #endif
  559. return -ENODEV;
  560. }
  561. return 0;
  562. }
  563. static void __exit alsa_card_dummy_exit(void)
  564. {
  565. int idx;
  566. for (idx = 0; idx < SNDRV_CARDS; idx++)
  567. snd_card_free(snd_dummy_cards[idx]);
  568. }
  569. module_init(alsa_card_dummy_init)
  570. module_exit(alsa_card_dummy_exit)