dummy.c 20 KB

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