dummy.c 20 KB

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