dummy.c 20 KB

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