dummy.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  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. static struct platform_device *devices[SNDRV_CARDS];
  135. #define MIXER_ADDR_MASTER 0
  136. #define MIXER_ADDR_LINE 1
  137. #define MIXER_ADDR_MIC 2
  138. #define MIXER_ADDR_SYNTH 3
  139. #define MIXER_ADDR_CD 4
  140. #define MIXER_ADDR_LAST 4
  141. struct snd_dummy {
  142. struct snd_card *card;
  143. struct snd_pcm *pcm;
  144. spinlock_t mixer_lock;
  145. int mixer_volume[MIXER_ADDR_LAST+1][2];
  146. int capture_source[MIXER_ADDR_LAST+1][2];
  147. };
  148. struct snd_dummy_pcm {
  149. struct snd_dummy *dummy;
  150. spinlock_t lock;
  151. struct timer_list timer;
  152. unsigned int pcm_size;
  153. unsigned int pcm_count;
  154. unsigned int pcm_bps; /* bytes per second */
  155. unsigned int pcm_jiffie; /* bytes per one jiffie */
  156. unsigned int pcm_irq_pos; /* IRQ position */
  157. unsigned int pcm_buf_pos; /* position in buffer */
  158. struct snd_pcm_substream *substream;
  159. };
  160. static inline void snd_card_dummy_pcm_timer_start(struct snd_dummy_pcm *dpcm)
  161. {
  162. dpcm->timer.expires = 1 + jiffies;
  163. add_timer(&dpcm->timer);
  164. }
  165. static inline void snd_card_dummy_pcm_timer_stop(struct snd_dummy_pcm *dpcm)
  166. {
  167. del_timer(&dpcm->timer);
  168. }
  169. static int snd_card_dummy_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  170. {
  171. struct snd_pcm_runtime *runtime = substream->runtime;
  172. struct snd_dummy_pcm *dpcm = runtime->private_data;
  173. int err = 0;
  174. spin_lock(&dpcm->lock);
  175. switch (cmd) {
  176. case SNDRV_PCM_TRIGGER_START:
  177. case SNDRV_PCM_TRIGGER_RESUME:
  178. snd_card_dummy_pcm_timer_start(dpcm);
  179. break;
  180. case SNDRV_PCM_TRIGGER_STOP:
  181. case SNDRV_PCM_TRIGGER_SUSPEND:
  182. snd_card_dummy_pcm_timer_stop(dpcm);
  183. break;
  184. default:
  185. err = -EINVAL;
  186. break;
  187. }
  188. spin_unlock(&dpcm->lock);
  189. return 0;
  190. }
  191. static int snd_card_dummy_pcm_prepare(struct snd_pcm_substream *substream)
  192. {
  193. struct snd_pcm_runtime *runtime = substream->runtime;
  194. struct snd_dummy_pcm *dpcm = runtime->private_data;
  195. unsigned int bps;
  196. bps = runtime->rate * runtime->channels;
  197. bps *= snd_pcm_format_width(runtime->format);
  198. bps /= 8;
  199. if (bps <= 0)
  200. return -EINVAL;
  201. dpcm->pcm_bps = bps;
  202. dpcm->pcm_jiffie = bps / HZ;
  203. dpcm->pcm_size = snd_pcm_lib_buffer_bytes(substream);
  204. dpcm->pcm_count = snd_pcm_lib_period_bytes(substream);
  205. dpcm->pcm_irq_pos = 0;
  206. dpcm->pcm_buf_pos = 0;
  207. return 0;
  208. }
  209. static void snd_card_dummy_pcm_timer_function(unsigned long data)
  210. {
  211. struct snd_dummy_pcm *dpcm = (struct snd_dummy_pcm *)data;
  212. unsigned long flags;
  213. spin_lock_irqsave(&dpcm->lock, flags);
  214. dpcm->timer.expires = 1 + jiffies;
  215. add_timer(&dpcm->timer);
  216. dpcm->pcm_irq_pos += dpcm->pcm_jiffie;
  217. dpcm->pcm_buf_pos += dpcm->pcm_jiffie;
  218. dpcm->pcm_buf_pos %= dpcm->pcm_size;
  219. if (dpcm->pcm_irq_pos >= dpcm->pcm_count) {
  220. dpcm->pcm_irq_pos %= dpcm->pcm_count;
  221. spin_unlock_irqrestore(&dpcm->lock, flags);
  222. snd_pcm_period_elapsed(dpcm->substream);
  223. } else
  224. spin_unlock_irqrestore(&dpcm->lock, flags);
  225. }
  226. static snd_pcm_uframes_t snd_card_dummy_pcm_pointer(struct snd_pcm_substream *substream)
  227. {
  228. struct snd_pcm_runtime *runtime = substream->runtime;
  229. struct snd_dummy_pcm *dpcm = runtime->private_data;
  230. return bytes_to_frames(runtime, dpcm->pcm_buf_pos);
  231. }
  232. static struct snd_pcm_hardware snd_card_dummy_playback =
  233. {
  234. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  235. SNDRV_PCM_INFO_RESUME | SNDRV_PCM_INFO_MMAP_VALID),
  236. .formats = USE_FORMATS,
  237. .rates = USE_RATE,
  238. .rate_min = USE_RATE_MIN,
  239. .rate_max = USE_RATE_MAX,
  240. .channels_min = USE_CHANNELS_MIN,
  241. .channels_max = USE_CHANNELS_MAX,
  242. .buffer_bytes_max = MAX_BUFFER_SIZE,
  243. .period_bytes_min = 64,
  244. .period_bytes_max = MAX_BUFFER_SIZE,
  245. .periods_min = USE_PERIODS_MIN,
  246. .periods_max = USE_PERIODS_MAX,
  247. .fifo_size = 0,
  248. };
  249. static struct snd_pcm_hardware snd_card_dummy_capture =
  250. {
  251. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  252. SNDRV_PCM_INFO_RESUME | SNDRV_PCM_INFO_MMAP_VALID),
  253. .formats = USE_FORMATS,
  254. .rates = USE_RATE,
  255. .rate_min = USE_RATE_MIN,
  256. .rate_max = USE_RATE_MAX,
  257. .channels_min = USE_CHANNELS_MIN,
  258. .channels_max = USE_CHANNELS_MAX,
  259. .buffer_bytes_max = MAX_BUFFER_SIZE,
  260. .period_bytes_min = 64,
  261. .period_bytes_max = MAX_BUFFER_SIZE,
  262. .periods_min = USE_PERIODS_MIN,
  263. .periods_max = USE_PERIODS_MAX,
  264. .fifo_size = 0,
  265. };
  266. static void snd_card_dummy_runtime_free(struct snd_pcm_runtime *runtime)
  267. {
  268. kfree(runtime->private_data);
  269. }
  270. static int snd_card_dummy_hw_params(struct snd_pcm_substream *substream,
  271. struct snd_pcm_hw_params *hw_params)
  272. {
  273. return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
  274. }
  275. static int snd_card_dummy_hw_free(struct snd_pcm_substream *substream)
  276. {
  277. return snd_pcm_lib_free_pages(substream);
  278. }
  279. static struct snd_dummy_pcm *new_pcm_stream(struct snd_pcm_substream *substream)
  280. {
  281. struct snd_dummy_pcm *dpcm;
  282. dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
  283. if (! dpcm)
  284. return dpcm;
  285. init_timer(&dpcm->timer);
  286. dpcm->timer.data = (unsigned long) dpcm;
  287. dpcm->timer.function = snd_card_dummy_pcm_timer_function;
  288. spin_lock_init(&dpcm->lock);
  289. dpcm->substream = substream;
  290. return dpcm;
  291. }
  292. static int snd_card_dummy_playback_open(struct snd_pcm_substream *substream)
  293. {
  294. struct snd_pcm_runtime *runtime = substream->runtime;
  295. struct snd_dummy_pcm *dpcm;
  296. int err;
  297. if ((dpcm = new_pcm_stream(substream)) == NULL)
  298. return -ENOMEM;
  299. runtime->private_data = dpcm;
  300. runtime->private_free = snd_card_dummy_runtime_free;
  301. runtime->hw = snd_card_dummy_playback;
  302. if (substream->pcm->device & 1) {
  303. runtime->hw.info &= ~SNDRV_PCM_INFO_INTERLEAVED;
  304. runtime->hw.info |= SNDRV_PCM_INFO_NONINTERLEAVED;
  305. }
  306. if (substream->pcm->device & 2)
  307. runtime->hw.info &= ~(SNDRV_PCM_INFO_MMAP|SNDRV_PCM_INFO_MMAP_VALID);
  308. if ((err = add_playback_constraints(runtime)) < 0) {
  309. kfree(dpcm);
  310. return err;
  311. }
  312. return 0;
  313. }
  314. static int snd_card_dummy_capture_open(struct snd_pcm_substream *substream)
  315. {
  316. struct snd_pcm_runtime *runtime = substream->runtime;
  317. struct snd_dummy_pcm *dpcm;
  318. int err;
  319. if ((dpcm = new_pcm_stream(substream)) == NULL)
  320. return -ENOMEM;
  321. runtime->private_data = dpcm;
  322. runtime->private_free = snd_card_dummy_runtime_free;
  323. runtime->hw = snd_card_dummy_capture;
  324. if (substream->pcm->device == 1) {
  325. runtime->hw.info &= ~SNDRV_PCM_INFO_INTERLEAVED;
  326. runtime->hw.info |= SNDRV_PCM_INFO_NONINTERLEAVED;
  327. }
  328. if (substream->pcm->device & 2)
  329. runtime->hw.info &= ~(SNDRV_PCM_INFO_MMAP|SNDRV_PCM_INFO_MMAP_VALID);
  330. if ((err = add_capture_constraints(runtime)) < 0) {
  331. kfree(dpcm);
  332. return err;
  333. }
  334. return 0;
  335. }
  336. static int snd_card_dummy_playback_close(struct snd_pcm_substream *substream)
  337. {
  338. return 0;
  339. }
  340. static int snd_card_dummy_capture_close(struct snd_pcm_substream *substream)
  341. {
  342. return 0;
  343. }
  344. static struct snd_pcm_ops snd_card_dummy_playback_ops = {
  345. .open = snd_card_dummy_playback_open,
  346. .close = snd_card_dummy_playback_close,
  347. .ioctl = snd_pcm_lib_ioctl,
  348. .hw_params = snd_card_dummy_hw_params,
  349. .hw_free = snd_card_dummy_hw_free,
  350. .prepare = snd_card_dummy_pcm_prepare,
  351. .trigger = snd_card_dummy_pcm_trigger,
  352. .pointer = snd_card_dummy_pcm_pointer,
  353. };
  354. static struct snd_pcm_ops snd_card_dummy_capture_ops = {
  355. .open = snd_card_dummy_capture_open,
  356. .close = snd_card_dummy_capture_close,
  357. .ioctl = snd_pcm_lib_ioctl,
  358. .hw_params = snd_card_dummy_hw_params,
  359. .hw_free = snd_card_dummy_hw_free,
  360. .prepare = snd_card_dummy_pcm_prepare,
  361. .trigger = snd_card_dummy_pcm_trigger,
  362. .pointer = snd_card_dummy_pcm_pointer,
  363. };
  364. static int __init snd_card_dummy_pcm(struct snd_dummy *dummy, int device, int substreams)
  365. {
  366. struct snd_pcm *pcm;
  367. int err;
  368. if ((err = snd_pcm_new(dummy->card, "Dummy PCM", device,
  369. substreams, substreams, &pcm)) < 0)
  370. return err;
  371. dummy->pcm = pcm;
  372. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_card_dummy_playback_ops);
  373. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_card_dummy_capture_ops);
  374. pcm->private_data = dummy;
  375. pcm->info_flags = 0;
  376. strcpy(pcm->name, "Dummy PCM");
  377. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
  378. snd_dma_continuous_data(GFP_KERNEL),
  379. 0, 64*1024);
  380. return 0;
  381. }
  382. #define DUMMY_VOLUME(xname, xindex, addr) \
  383. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
  384. .info = snd_dummy_volume_info, \
  385. .get = snd_dummy_volume_get, .put = snd_dummy_volume_put, \
  386. .private_value = addr }
  387. static int snd_dummy_volume_info(struct snd_kcontrol *kcontrol,
  388. struct snd_ctl_elem_info *uinfo)
  389. {
  390. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  391. uinfo->count = 2;
  392. uinfo->value.integer.min = -50;
  393. uinfo->value.integer.max = 100;
  394. return 0;
  395. }
  396. static int snd_dummy_volume_get(struct snd_kcontrol *kcontrol,
  397. struct snd_ctl_elem_value *ucontrol)
  398. {
  399. struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
  400. int addr = kcontrol->private_value;
  401. spin_lock_irq(&dummy->mixer_lock);
  402. ucontrol->value.integer.value[0] = dummy->mixer_volume[addr][0];
  403. ucontrol->value.integer.value[1] = dummy->mixer_volume[addr][1];
  404. spin_unlock_irq(&dummy->mixer_lock);
  405. return 0;
  406. }
  407. static int snd_dummy_volume_put(struct snd_kcontrol *kcontrol,
  408. struct snd_ctl_elem_value *ucontrol)
  409. {
  410. struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
  411. int change, addr = kcontrol->private_value;
  412. int left, right;
  413. left = ucontrol->value.integer.value[0];
  414. if (left < -50)
  415. left = -50;
  416. if (left > 100)
  417. left = 100;
  418. right = ucontrol->value.integer.value[1];
  419. if (right < -50)
  420. right = -50;
  421. if (right > 100)
  422. right = 100;
  423. spin_lock_irq(&dummy->mixer_lock);
  424. change = dummy->mixer_volume[addr][0] != left ||
  425. dummy->mixer_volume[addr][1] != right;
  426. dummy->mixer_volume[addr][0] = left;
  427. dummy->mixer_volume[addr][1] = right;
  428. spin_unlock_irq(&dummy->mixer_lock);
  429. return change;
  430. }
  431. #define DUMMY_CAPSRC(xname, xindex, addr) \
  432. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
  433. .info = snd_dummy_capsrc_info, \
  434. .get = snd_dummy_capsrc_get, .put = snd_dummy_capsrc_put, \
  435. .private_value = addr }
  436. static int snd_dummy_capsrc_info(struct snd_kcontrol *kcontrol,
  437. struct snd_ctl_elem_info *uinfo)
  438. {
  439. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  440. uinfo->count = 2;
  441. uinfo->value.integer.min = 0;
  442. uinfo->value.integer.max = 1;
  443. return 0;
  444. }
  445. static int snd_dummy_capsrc_get(struct snd_kcontrol *kcontrol,
  446. struct snd_ctl_elem_value *ucontrol)
  447. {
  448. struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
  449. int addr = kcontrol->private_value;
  450. spin_lock_irq(&dummy->mixer_lock);
  451. ucontrol->value.integer.value[0] = dummy->capture_source[addr][0];
  452. ucontrol->value.integer.value[1] = dummy->capture_source[addr][1];
  453. spin_unlock_irq(&dummy->mixer_lock);
  454. return 0;
  455. }
  456. static int snd_dummy_capsrc_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  457. {
  458. struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
  459. int change, addr = kcontrol->private_value;
  460. int left, right;
  461. left = ucontrol->value.integer.value[0] & 1;
  462. right = ucontrol->value.integer.value[1] & 1;
  463. spin_lock_irq(&dummy->mixer_lock);
  464. change = dummy->capture_source[addr][0] != left &&
  465. dummy->capture_source[addr][1] != right;
  466. dummy->capture_source[addr][0] = left;
  467. dummy->capture_source[addr][1] = right;
  468. spin_unlock_irq(&dummy->mixer_lock);
  469. return change;
  470. }
  471. static struct snd_kcontrol_new snd_dummy_controls[] = {
  472. DUMMY_VOLUME("Master Volume", 0, MIXER_ADDR_MASTER),
  473. DUMMY_CAPSRC("Master Capture Switch", 0, MIXER_ADDR_MASTER),
  474. DUMMY_VOLUME("Synth Volume", 0, MIXER_ADDR_SYNTH),
  475. DUMMY_CAPSRC("Synth Capture Switch", 0, MIXER_ADDR_MASTER),
  476. DUMMY_VOLUME("Line Volume", 0, MIXER_ADDR_LINE),
  477. DUMMY_CAPSRC("Line Capture Switch", 0, MIXER_ADDR_MASTER),
  478. DUMMY_VOLUME("Mic Volume", 0, MIXER_ADDR_MIC),
  479. DUMMY_CAPSRC("Mic Capture Switch", 0, MIXER_ADDR_MASTER),
  480. DUMMY_VOLUME("CD Volume", 0, MIXER_ADDR_CD),
  481. DUMMY_CAPSRC("CD Capture Switch", 0, MIXER_ADDR_MASTER)
  482. };
  483. static int __init snd_card_dummy_new_mixer(struct snd_dummy *dummy)
  484. {
  485. struct snd_card *card = dummy->card;
  486. unsigned int idx;
  487. int err;
  488. snd_assert(dummy != NULL, return -EINVAL);
  489. spin_lock_init(&dummy->mixer_lock);
  490. strcpy(card->mixername, "Dummy Mixer");
  491. for (idx = 0; idx < ARRAY_SIZE(snd_dummy_controls); idx++) {
  492. if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_dummy_controls[idx], dummy))) < 0)
  493. return err;
  494. }
  495. return 0;
  496. }
  497. static int __init snd_dummy_probe(struct platform_device *devptr)
  498. {
  499. struct snd_card *card;
  500. struct snd_dummy *dummy;
  501. int idx, err;
  502. int dev = devptr->id;
  503. card = snd_card_new(index[dev], id[dev], THIS_MODULE,
  504. sizeof(struct snd_dummy));
  505. if (card == NULL)
  506. return -ENOMEM;
  507. dummy = card->private_data;
  508. dummy->card = card;
  509. for (idx = 0; idx < MAX_PCM_DEVICES && idx < pcm_devs[dev]; idx++) {
  510. if (pcm_substreams[dev] < 1)
  511. pcm_substreams[dev] = 1;
  512. if (pcm_substreams[dev] > MAX_PCM_SUBSTREAMS)
  513. pcm_substreams[dev] = MAX_PCM_SUBSTREAMS;
  514. if ((err = snd_card_dummy_pcm(dummy, idx, pcm_substreams[dev])) < 0)
  515. goto __nodev;
  516. }
  517. if ((err = snd_card_dummy_new_mixer(dummy)) < 0)
  518. goto __nodev;
  519. strcpy(card->driver, "Dummy");
  520. strcpy(card->shortname, "Dummy");
  521. sprintf(card->longname, "Dummy %i", dev + 1);
  522. snd_card_set_dev(card, &devptr->dev);
  523. if ((err = snd_card_register(card)) == 0) {
  524. platform_set_drvdata(devptr, card);
  525. return 0;
  526. }
  527. __nodev:
  528. snd_card_free(card);
  529. return err;
  530. }
  531. static int snd_dummy_remove(struct platform_device *devptr)
  532. {
  533. snd_card_free(platform_get_drvdata(devptr));
  534. platform_set_drvdata(devptr, NULL);
  535. return 0;
  536. }
  537. #ifdef CONFIG_PM
  538. static int snd_dummy_suspend(struct platform_device *pdev, pm_message_t state)
  539. {
  540. struct snd_card *card = platform_get_drvdata(pdev);
  541. struct snd_dummy *dummy = card->private_data;
  542. snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
  543. snd_pcm_suspend_all(dummy->pcm);
  544. return 0;
  545. }
  546. static int snd_dummy_resume(struct platform_device *pdev)
  547. {
  548. struct snd_card *card = platform_get_drvdata(pdev);
  549. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  550. return 0;
  551. }
  552. #endif
  553. #define SND_DUMMY_DRIVER "snd_dummy"
  554. static struct platform_driver snd_dummy_driver = {
  555. .probe = snd_dummy_probe,
  556. .remove = snd_dummy_remove,
  557. #ifdef CONFIG_PM
  558. .suspend = snd_dummy_suspend,
  559. .resume = snd_dummy_resume,
  560. #endif
  561. .driver = {
  562. .name = SND_DUMMY_DRIVER
  563. },
  564. };
  565. static void __init_or_module snd_dummy_unregister_all(void)
  566. {
  567. int i;
  568. for (i = 0; i < ARRAY_SIZE(devices); ++i)
  569. platform_device_unregister(devices[i]);
  570. platform_driver_unregister(&snd_dummy_driver);
  571. }
  572. static int __init alsa_card_dummy_init(void)
  573. {
  574. int i, cards, err;
  575. if ((err = platform_driver_register(&snd_dummy_driver)) < 0)
  576. return err;
  577. cards = 0;
  578. for (i = 0; i < SNDRV_CARDS && enable[i]; i++) {
  579. struct platform_device *device;
  580. device = platform_device_register_simple(SND_DUMMY_DRIVER,
  581. i, NULL, 0);
  582. if (IS_ERR(device)) {
  583. err = PTR_ERR(device);
  584. goto errout;
  585. }
  586. devices[i] = device;
  587. cards++;
  588. }
  589. if (!cards) {
  590. #ifdef MODULE
  591. printk(KERN_ERR "Dummy soundcard not found or device busy\n");
  592. #endif
  593. err = -ENODEV;
  594. goto errout;
  595. }
  596. return 0;
  597. errout:
  598. snd_dummy_unregister_all();
  599. return err;
  600. }
  601. static void __exit alsa_card_dummy_exit(void)
  602. {
  603. snd_dummy_unregister_all();
  604. }
  605. module_init(alsa_card_dummy_init)
  606. module_exit(alsa_card_dummy_exit)