dummy.c 20 KB

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