dummy.c 19 KB

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