dummy.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Dummy soundcard
  4. * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
  5. */
  6. #include <linux/init.h>
  7. #include <linux/err.h>
  8. #include <linux/platform_device.h>
  9. #include <linux/jiffies.h>
  10. #include <linux/slab.h>
  11. #include <linux/string.h>
  12. #include <linux/time.h>
  13. #include <linux/wait.h>
  14. #include <linux/hrtimer.h>
  15. #include <linux/math64.h>
  16. #include <linux/module.h>
  17. #include <sound/core.h>
  18. #include <sound/control.h>
  19. #include <sound/tlv.h>
  20. #include <sound/pcm.h>
  21. #include <sound/rawmidi.h>
  22. #include <sound/info.h>
  23. #include <sound/initval.h>
  24. MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
  25. MODULE_DESCRIPTION("Dummy soundcard (/dev/null)");
  26. MODULE_LICENSE("GPL");
  27. #define MAX_PCM_DEVICES 4
  28. #define MAX_PCM_SUBSTREAMS 128
  29. #define MAX_MIDI_DEVICES 2
  30. /* defaults */
  31. #define MAX_BUFFER_SIZE (64*1024)
  32. #define MIN_PERIOD_SIZE 64
  33. #define MAX_PERIOD_SIZE MAX_BUFFER_SIZE
  34. #define USE_FORMATS (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE)
  35. #define USE_RATE SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000
  36. #define USE_RATE_MIN 5500
  37. #define USE_RATE_MAX 48000
  38. #define USE_CHANNELS_MIN 1
  39. #define USE_CHANNELS_MAX 2
  40. #define USE_PERIODS_MIN 1
  41. #define USE_PERIODS_MAX 1024
  42. #define USE_MIXER_VOLUME_LEVEL_MIN -50
  43. #define USE_MIXER_VOLUME_LEVEL_MAX 100
  44. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
  45. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
  46. static bool enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 0};
  47. static char *model[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = NULL};
  48. static int pcm_devs[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1};
  49. static int pcm_substreams[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 8};
  50. //static int midi_devs[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 2};
  51. static int mixer_volume_level_min = USE_MIXER_VOLUME_LEVEL_MIN;
  52. static int mixer_volume_level_max = USE_MIXER_VOLUME_LEVEL_MAX;
  53. #ifdef CONFIG_HIGH_RES_TIMERS
  54. static bool hrtimer = 1;
  55. #endif
  56. static bool fake_buffer = 1;
  57. module_param_array(index, int, NULL, 0444);
  58. MODULE_PARM_DESC(index, "Index value for dummy soundcard.");
  59. module_param_array(id, charp, NULL, 0444);
  60. MODULE_PARM_DESC(id, "ID string for dummy soundcard.");
  61. module_param_array(enable, bool, NULL, 0444);
  62. MODULE_PARM_DESC(enable, "Enable this dummy soundcard.");
  63. module_param_array(model, charp, NULL, 0444);
  64. MODULE_PARM_DESC(model, "Soundcard model.");
  65. module_param_array(pcm_devs, int, NULL, 0444);
  66. MODULE_PARM_DESC(pcm_devs, "PCM devices # (0-4) for dummy driver.");
  67. module_param_array(pcm_substreams, int, NULL, 0444);
  68. MODULE_PARM_DESC(pcm_substreams, "PCM substreams # (1-128) for dummy driver.");
  69. //module_param_array(midi_devs, int, NULL, 0444);
  70. //MODULE_PARM_DESC(midi_devs, "MIDI devices # (0-2) for dummy driver.");
  71. module_param(mixer_volume_level_min, int, 0444);
  72. MODULE_PARM_DESC(mixer_volume_level_min, "Minimum mixer volume level for dummy driver. Default: -50");
  73. module_param(mixer_volume_level_max, int, 0444);
  74. MODULE_PARM_DESC(mixer_volume_level_max, "Maximum mixer volume level for dummy driver. Default: 100");
  75. module_param(fake_buffer, bool, 0444);
  76. MODULE_PARM_DESC(fake_buffer, "Fake buffer allocations.");
  77. #ifdef CONFIG_HIGH_RES_TIMERS
  78. module_param(hrtimer, bool, 0644);
  79. MODULE_PARM_DESC(hrtimer, "Use hrtimer as the timer source.");
  80. #endif
  81. static struct platform_device *devices[SNDRV_CARDS];
  82. #define MIXER_ADDR_MASTER 0
  83. #define MIXER_ADDR_LINE 1
  84. #define MIXER_ADDR_MIC 2
  85. #define MIXER_ADDR_SYNTH 3
  86. #define MIXER_ADDR_CD 4
  87. #define MIXER_ADDR_LAST 4
  88. struct dummy_timer_ops {
  89. int (*create)(struct snd_pcm_substream *);
  90. void (*free)(struct snd_pcm_substream *);
  91. int (*prepare)(struct snd_pcm_substream *);
  92. int (*start)(struct snd_pcm_substream *);
  93. int (*stop)(struct snd_pcm_substream *);
  94. snd_pcm_uframes_t (*pointer)(struct snd_pcm_substream *);
  95. };
  96. #define get_dummy_ops(substream) \
  97. (*(const struct dummy_timer_ops **)(substream)->runtime->private_data)
  98. struct dummy_model {
  99. const char *name;
  100. int (*playback_constraints)(struct snd_pcm_runtime *runtime);
  101. int (*capture_constraints)(struct snd_pcm_runtime *runtime);
  102. u64 formats;
  103. size_t buffer_bytes_max;
  104. size_t period_bytes_min;
  105. size_t period_bytes_max;
  106. unsigned int periods_min;
  107. unsigned int periods_max;
  108. unsigned int rates;
  109. unsigned int rate_min;
  110. unsigned int rate_max;
  111. unsigned int channels_min;
  112. unsigned int channels_max;
  113. };
  114. struct snd_dummy {
  115. struct snd_card *card;
  116. const struct dummy_model *model;
  117. struct snd_pcm *pcm;
  118. struct snd_pcm_hardware pcm_hw;
  119. spinlock_t mixer_lock;
  120. int mixer_volume[MIXER_ADDR_LAST+1][2];
  121. int capture_source[MIXER_ADDR_LAST+1][2];
  122. int iobox;
  123. struct snd_kcontrol *cd_volume_ctl;
  124. struct snd_kcontrol *cd_switch_ctl;
  125. };
  126. /*
  127. * card models
  128. */
  129. static int emu10k1_playback_constraints(struct snd_pcm_runtime *runtime)
  130. {
  131. int err;
  132. err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
  133. if (err < 0)
  134. return err;
  135. err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 256, UINT_MAX);
  136. if (err < 0)
  137. return err;
  138. return 0;
  139. }
  140. static const struct dummy_model model_emu10k1 = {
  141. .name = "emu10k1",
  142. .playback_constraints = emu10k1_playback_constraints,
  143. .buffer_bytes_max = 128 * 1024,
  144. };
  145. static const struct dummy_model model_rme9652 = {
  146. .name = "rme9652",
  147. .buffer_bytes_max = 26 * 64 * 1024,
  148. .formats = SNDRV_PCM_FMTBIT_S32_LE,
  149. .channels_min = 26,
  150. .channels_max = 26,
  151. .periods_min = 2,
  152. .periods_max = 2,
  153. };
  154. static const struct dummy_model model_ice1712 = {
  155. .name = "ice1712",
  156. .buffer_bytes_max = 256 * 1024,
  157. .formats = SNDRV_PCM_FMTBIT_S32_LE,
  158. .channels_min = 10,
  159. .channels_max = 10,
  160. .periods_min = 1,
  161. .periods_max = 1024,
  162. };
  163. static const struct dummy_model model_uda1341 = {
  164. .name = "uda1341",
  165. .buffer_bytes_max = 16380,
  166. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  167. .channels_min = 2,
  168. .channels_max = 2,
  169. .periods_min = 2,
  170. .periods_max = 255,
  171. };
  172. static const struct dummy_model model_ac97 = {
  173. .name = "ac97",
  174. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  175. .channels_min = 2,
  176. .channels_max = 2,
  177. .rates = SNDRV_PCM_RATE_48000,
  178. .rate_min = 48000,
  179. .rate_max = 48000,
  180. };
  181. static const struct dummy_model model_ca0106 = {
  182. .name = "ca0106",
  183. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  184. .buffer_bytes_max = ((65536-64)*8),
  185. .period_bytes_max = (65536-64),
  186. .periods_min = 2,
  187. .periods_max = 8,
  188. .channels_min = 2,
  189. .channels_max = 2,
  190. .rates = SNDRV_PCM_RATE_48000|SNDRV_PCM_RATE_96000|SNDRV_PCM_RATE_192000,
  191. .rate_min = 48000,
  192. .rate_max = 192000,
  193. };
  194. static const struct dummy_model *dummy_models[] = {
  195. &model_emu10k1,
  196. &model_rme9652,
  197. &model_ice1712,
  198. &model_uda1341,
  199. &model_ac97,
  200. &model_ca0106,
  201. NULL
  202. };
  203. /*
  204. * system timer interface
  205. */
  206. struct dummy_systimer_pcm {
  207. /* ops must be the first item */
  208. const struct dummy_timer_ops *timer_ops;
  209. spinlock_t lock;
  210. struct timer_list timer;
  211. unsigned long base_time;
  212. unsigned int frac_pos; /* fractional sample position (based HZ) */
  213. unsigned int frac_period_rest;
  214. unsigned int frac_buffer_size; /* buffer_size * HZ */
  215. unsigned int frac_period_size; /* period_size * HZ */
  216. unsigned int rate;
  217. int elapsed;
  218. struct snd_pcm_substream *substream;
  219. };
  220. static void dummy_systimer_rearm(struct dummy_systimer_pcm *dpcm)
  221. {
  222. mod_timer(&dpcm->timer, jiffies +
  223. DIV_ROUND_UP(dpcm->frac_period_rest, dpcm->rate));
  224. }
  225. static void dummy_systimer_update(struct dummy_systimer_pcm *dpcm)
  226. {
  227. unsigned long delta;
  228. delta = jiffies - dpcm->base_time;
  229. if (!delta)
  230. return;
  231. dpcm->base_time += delta;
  232. delta *= dpcm->rate;
  233. dpcm->frac_pos += delta;
  234. while (dpcm->frac_pos >= dpcm->frac_buffer_size)
  235. dpcm->frac_pos -= dpcm->frac_buffer_size;
  236. while (dpcm->frac_period_rest <= delta) {
  237. dpcm->elapsed++;
  238. dpcm->frac_period_rest += dpcm->frac_period_size;
  239. }
  240. dpcm->frac_period_rest -= delta;
  241. }
  242. static int dummy_systimer_start(struct snd_pcm_substream *substream)
  243. {
  244. struct dummy_systimer_pcm *dpcm = substream->runtime->private_data;
  245. guard(spinlock)(&dpcm->lock);
  246. dpcm->base_time = jiffies;
  247. dummy_systimer_rearm(dpcm);
  248. return 0;
  249. }
  250. static int dummy_systimer_stop(struct snd_pcm_substream *substream)
  251. {
  252. struct dummy_systimer_pcm *dpcm = substream->runtime->private_data;
  253. guard(spinlock)(&dpcm->lock);
  254. timer_delete(&dpcm->timer);
  255. return 0;
  256. }
  257. static int dummy_systimer_prepare(struct snd_pcm_substream *substream)
  258. {
  259. struct snd_pcm_runtime *runtime = substream->runtime;
  260. struct dummy_systimer_pcm *dpcm = runtime->private_data;
  261. dpcm->frac_pos = 0;
  262. dpcm->rate = runtime->rate;
  263. dpcm->frac_buffer_size = runtime->buffer_size * HZ;
  264. dpcm->frac_period_size = runtime->period_size * HZ;
  265. dpcm->frac_period_rest = dpcm->frac_period_size;
  266. dpcm->elapsed = 0;
  267. return 0;
  268. }
  269. static void dummy_systimer_callback(struct timer_list *t)
  270. {
  271. struct dummy_systimer_pcm *dpcm = timer_container_of(dpcm, t, timer);
  272. int elapsed = 0;
  273. scoped_guard(spinlock_irqsave, &dpcm->lock) {
  274. dummy_systimer_update(dpcm);
  275. dummy_systimer_rearm(dpcm);
  276. elapsed = dpcm->elapsed;
  277. dpcm->elapsed = 0;
  278. }
  279. if (elapsed)
  280. snd_pcm_period_elapsed(dpcm->substream);
  281. }
  282. static snd_pcm_uframes_t
  283. dummy_systimer_pointer(struct snd_pcm_substream *substream)
  284. {
  285. struct dummy_systimer_pcm *dpcm = substream->runtime->private_data;
  286. guard(spinlock)(&dpcm->lock);
  287. dummy_systimer_update(dpcm);
  288. return dpcm->frac_pos / HZ;
  289. }
  290. static int dummy_systimer_create(struct snd_pcm_substream *substream)
  291. {
  292. struct dummy_systimer_pcm *dpcm;
  293. dpcm = kzalloc_obj(*dpcm);
  294. if (!dpcm)
  295. return -ENOMEM;
  296. substream->runtime->private_data = dpcm;
  297. timer_setup(&dpcm->timer, dummy_systimer_callback, 0);
  298. spin_lock_init(&dpcm->lock);
  299. dpcm->substream = substream;
  300. return 0;
  301. }
  302. static void dummy_systimer_free(struct snd_pcm_substream *substream)
  303. {
  304. kfree(substream->runtime->private_data);
  305. }
  306. static const struct dummy_timer_ops dummy_systimer_ops = {
  307. .create = dummy_systimer_create,
  308. .free = dummy_systimer_free,
  309. .prepare = dummy_systimer_prepare,
  310. .start = dummy_systimer_start,
  311. .stop = dummy_systimer_stop,
  312. .pointer = dummy_systimer_pointer,
  313. };
  314. #ifdef CONFIG_HIGH_RES_TIMERS
  315. /*
  316. * hrtimer interface
  317. */
  318. struct dummy_hrtimer_pcm {
  319. /* ops must be the first item */
  320. const struct dummy_timer_ops *timer_ops;
  321. ktime_t base_time;
  322. ktime_t period_time;
  323. atomic_t running;
  324. struct hrtimer timer;
  325. struct snd_pcm_substream *substream;
  326. };
  327. static enum hrtimer_restart dummy_hrtimer_callback(struct hrtimer *timer)
  328. {
  329. struct dummy_hrtimer_pcm *dpcm;
  330. dpcm = container_of(timer, struct dummy_hrtimer_pcm, timer);
  331. if (!atomic_read(&dpcm->running))
  332. return HRTIMER_NORESTART;
  333. /*
  334. * In cases of XRUN and draining, this calls .trigger to stop PCM
  335. * substream.
  336. */
  337. snd_pcm_period_elapsed(dpcm->substream);
  338. if (!atomic_read(&dpcm->running))
  339. return HRTIMER_NORESTART;
  340. hrtimer_forward_now(timer, dpcm->period_time);
  341. return HRTIMER_RESTART;
  342. }
  343. static int dummy_hrtimer_start(struct snd_pcm_substream *substream)
  344. {
  345. struct dummy_hrtimer_pcm *dpcm = substream->runtime->private_data;
  346. dpcm->base_time = hrtimer_cb_get_time(&dpcm->timer);
  347. hrtimer_start(&dpcm->timer, dpcm->period_time, HRTIMER_MODE_REL_SOFT);
  348. atomic_set(&dpcm->running, 1);
  349. return 0;
  350. }
  351. static int dummy_hrtimer_stop(struct snd_pcm_substream *substream)
  352. {
  353. struct dummy_hrtimer_pcm *dpcm = substream->runtime->private_data;
  354. atomic_set(&dpcm->running, 0);
  355. if (!hrtimer_callback_running(&dpcm->timer))
  356. hrtimer_cancel(&dpcm->timer);
  357. return 0;
  358. }
  359. static inline void dummy_hrtimer_sync(struct dummy_hrtimer_pcm *dpcm)
  360. {
  361. hrtimer_cancel(&dpcm->timer);
  362. }
  363. static snd_pcm_uframes_t
  364. dummy_hrtimer_pointer(struct snd_pcm_substream *substream)
  365. {
  366. struct snd_pcm_runtime *runtime = substream->runtime;
  367. struct dummy_hrtimer_pcm *dpcm = runtime->private_data;
  368. u64 delta;
  369. u32 pos;
  370. delta = ktime_us_delta(hrtimer_cb_get_time(&dpcm->timer),
  371. dpcm->base_time);
  372. delta = div_u64(delta * runtime->rate + 999999, 1000000);
  373. div_u64_rem(delta, runtime->buffer_size, &pos);
  374. return pos;
  375. }
  376. static int dummy_hrtimer_prepare(struct snd_pcm_substream *substream)
  377. {
  378. struct snd_pcm_runtime *runtime = substream->runtime;
  379. struct dummy_hrtimer_pcm *dpcm = runtime->private_data;
  380. unsigned int period, rate;
  381. long sec;
  382. unsigned long nsecs;
  383. dummy_hrtimer_sync(dpcm);
  384. period = runtime->period_size;
  385. rate = runtime->rate;
  386. sec = period / rate;
  387. period %= rate;
  388. nsecs = div_u64((u64)period * 1000000000UL + rate - 1, rate);
  389. dpcm->period_time = ktime_set(sec, nsecs);
  390. return 0;
  391. }
  392. static int dummy_hrtimer_create(struct snd_pcm_substream *substream)
  393. {
  394. struct dummy_hrtimer_pcm *dpcm;
  395. dpcm = kzalloc_obj(*dpcm);
  396. if (!dpcm)
  397. return -ENOMEM;
  398. substream->runtime->private_data = dpcm;
  399. hrtimer_setup(&dpcm->timer, dummy_hrtimer_callback, CLOCK_MONOTONIC, HRTIMER_MODE_REL_SOFT);
  400. dpcm->substream = substream;
  401. atomic_set(&dpcm->running, 0);
  402. return 0;
  403. }
  404. static void dummy_hrtimer_free(struct snd_pcm_substream *substream)
  405. {
  406. struct dummy_hrtimer_pcm *dpcm = substream->runtime->private_data;
  407. dummy_hrtimer_sync(dpcm);
  408. kfree(dpcm);
  409. }
  410. static const struct dummy_timer_ops dummy_hrtimer_ops = {
  411. .create = dummy_hrtimer_create,
  412. .free = dummy_hrtimer_free,
  413. .prepare = dummy_hrtimer_prepare,
  414. .start = dummy_hrtimer_start,
  415. .stop = dummy_hrtimer_stop,
  416. .pointer = dummy_hrtimer_pointer,
  417. };
  418. #endif /* CONFIG_HIGH_RES_TIMERS */
  419. /*
  420. * PCM interface
  421. */
  422. static int dummy_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  423. {
  424. switch (cmd) {
  425. case SNDRV_PCM_TRIGGER_START:
  426. case SNDRV_PCM_TRIGGER_RESUME:
  427. return get_dummy_ops(substream)->start(substream);
  428. case SNDRV_PCM_TRIGGER_STOP:
  429. case SNDRV_PCM_TRIGGER_SUSPEND:
  430. return get_dummy_ops(substream)->stop(substream);
  431. }
  432. return -EINVAL;
  433. }
  434. static int dummy_pcm_prepare(struct snd_pcm_substream *substream)
  435. {
  436. return get_dummy_ops(substream)->prepare(substream);
  437. }
  438. static snd_pcm_uframes_t dummy_pcm_pointer(struct snd_pcm_substream *substream)
  439. {
  440. return get_dummy_ops(substream)->pointer(substream);
  441. }
  442. static const struct snd_pcm_hardware dummy_pcm_hardware = {
  443. .info = (SNDRV_PCM_INFO_MMAP |
  444. SNDRV_PCM_INFO_INTERLEAVED |
  445. SNDRV_PCM_INFO_RESUME |
  446. SNDRV_PCM_INFO_MMAP_VALID),
  447. .formats = USE_FORMATS,
  448. .rates = USE_RATE,
  449. .rate_min = USE_RATE_MIN,
  450. .rate_max = USE_RATE_MAX,
  451. .channels_min = USE_CHANNELS_MIN,
  452. .channels_max = USE_CHANNELS_MAX,
  453. .buffer_bytes_max = MAX_BUFFER_SIZE,
  454. .period_bytes_min = MIN_PERIOD_SIZE,
  455. .period_bytes_max = MAX_PERIOD_SIZE,
  456. .periods_min = USE_PERIODS_MIN,
  457. .periods_max = USE_PERIODS_MAX,
  458. .fifo_size = 0,
  459. };
  460. static int dummy_pcm_hw_params(struct snd_pcm_substream *substream,
  461. struct snd_pcm_hw_params *hw_params)
  462. {
  463. if (fake_buffer) {
  464. /* runtime->dma_bytes has to be set manually to allow mmap */
  465. substream->runtime->dma_bytes = params_buffer_bytes(hw_params);
  466. return 0;
  467. }
  468. return 0;
  469. }
  470. static int dummy_pcm_open(struct snd_pcm_substream *substream)
  471. {
  472. struct snd_dummy *dummy = snd_pcm_substream_chip(substream);
  473. const struct dummy_model *model = dummy->model;
  474. struct snd_pcm_runtime *runtime = substream->runtime;
  475. const struct dummy_timer_ops *ops;
  476. int err;
  477. ops = &dummy_systimer_ops;
  478. #ifdef CONFIG_HIGH_RES_TIMERS
  479. if (hrtimer)
  480. ops = &dummy_hrtimer_ops;
  481. #endif
  482. err = ops->create(substream);
  483. if (err < 0)
  484. return err;
  485. get_dummy_ops(substream) = ops;
  486. runtime->hw = dummy->pcm_hw;
  487. if (substream->pcm->device & 1) {
  488. runtime->hw.info &= ~SNDRV_PCM_INFO_INTERLEAVED;
  489. runtime->hw.info |= SNDRV_PCM_INFO_NONINTERLEAVED;
  490. }
  491. if (substream->pcm->device & 2)
  492. runtime->hw.info &= ~(SNDRV_PCM_INFO_MMAP |
  493. SNDRV_PCM_INFO_MMAP_VALID);
  494. if (model == NULL)
  495. return 0;
  496. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  497. if (model->playback_constraints)
  498. err = model->playback_constraints(substream->runtime);
  499. } else {
  500. if (model->capture_constraints)
  501. err = model->capture_constraints(substream->runtime);
  502. }
  503. if (err < 0) {
  504. get_dummy_ops(substream)->free(substream);
  505. return err;
  506. }
  507. return 0;
  508. }
  509. static int dummy_pcm_close(struct snd_pcm_substream *substream)
  510. {
  511. get_dummy_ops(substream)->free(substream);
  512. return 0;
  513. }
  514. /*
  515. * dummy buffer handling
  516. */
  517. static void *dummy_page[2];
  518. static void free_fake_buffer(void)
  519. {
  520. if (fake_buffer) {
  521. int i;
  522. for (i = 0; i < 2; i++)
  523. if (dummy_page[i]) {
  524. free_page((unsigned long)dummy_page[i]);
  525. dummy_page[i] = NULL;
  526. }
  527. }
  528. }
  529. static int alloc_fake_buffer(void)
  530. {
  531. int i;
  532. if (!fake_buffer)
  533. return 0;
  534. for (i = 0; i < 2; i++) {
  535. dummy_page[i] = (void *)get_zeroed_page(GFP_KERNEL);
  536. if (!dummy_page[i]) {
  537. free_fake_buffer();
  538. return -ENOMEM;
  539. }
  540. }
  541. return 0;
  542. }
  543. static int dummy_pcm_copy(struct snd_pcm_substream *substream,
  544. int channel, unsigned long pos,
  545. struct iov_iter *iter, unsigned long bytes)
  546. {
  547. return 0; /* do nothing */
  548. }
  549. static int dummy_pcm_silence(struct snd_pcm_substream *substream,
  550. int channel, unsigned long pos,
  551. unsigned long bytes)
  552. {
  553. return 0; /* do nothing */
  554. }
  555. static struct page *dummy_pcm_page(struct snd_pcm_substream *substream,
  556. unsigned long offset)
  557. {
  558. return virt_to_page(dummy_page[substream->stream]); /* the same page */
  559. }
  560. static const struct snd_pcm_ops dummy_pcm_ops = {
  561. .open = dummy_pcm_open,
  562. .close = dummy_pcm_close,
  563. .hw_params = dummy_pcm_hw_params,
  564. .prepare = dummy_pcm_prepare,
  565. .trigger = dummy_pcm_trigger,
  566. .pointer = dummy_pcm_pointer,
  567. };
  568. static const struct snd_pcm_ops dummy_pcm_ops_no_buf = {
  569. .open = dummy_pcm_open,
  570. .close = dummy_pcm_close,
  571. .hw_params = dummy_pcm_hw_params,
  572. .prepare = dummy_pcm_prepare,
  573. .trigger = dummy_pcm_trigger,
  574. .pointer = dummy_pcm_pointer,
  575. .copy = dummy_pcm_copy,
  576. .fill_silence = dummy_pcm_silence,
  577. .page = dummy_pcm_page,
  578. };
  579. static int snd_card_dummy_pcm(struct snd_dummy *dummy, int device,
  580. int substreams)
  581. {
  582. struct snd_pcm *pcm;
  583. const struct snd_pcm_ops *ops;
  584. int err;
  585. err = snd_pcm_new(dummy->card, "Dummy PCM", device,
  586. substreams, substreams, &pcm);
  587. if (err < 0)
  588. return err;
  589. dummy->pcm = pcm;
  590. if (fake_buffer)
  591. ops = &dummy_pcm_ops_no_buf;
  592. else
  593. ops = &dummy_pcm_ops;
  594. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, ops);
  595. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, ops);
  596. pcm->private_data = dummy;
  597. pcm->info_flags = 0;
  598. strscpy(pcm->name, "Dummy PCM");
  599. if (!fake_buffer) {
  600. snd_pcm_set_managed_buffer_all(pcm,
  601. SNDRV_DMA_TYPE_CONTINUOUS,
  602. NULL,
  603. 0, 64*1024);
  604. }
  605. return 0;
  606. }
  607. /*
  608. * mixer interface
  609. */
  610. #define DUMMY_VOLUME(xname, xindex, addr) \
  611. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
  612. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
  613. .name = xname, .index = xindex, \
  614. .info = snd_dummy_volume_info, \
  615. .get = snd_dummy_volume_get, .put = snd_dummy_volume_put, \
  616. .private_value = addr, \
  617. .tlv = { .p = db_scale_dummy } }
  618. static int snd_dummy_volume_info(struct snd_kcontrol *kcontrol,
  619. struct snd_ctl_elem_info *uinfo)
  620. {
  621. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  622. uinfo->count = 2;
  623. uinfo->value.integer.min = mixer_volume_level_min;
  624. uinfo->value.integer.max = mixer_volume_level_max;
  625. return 0;
  626. }
  627. static int snd_dummy_volume_get(struct snd_kcontrol *kcontrol,
  628. struct snd_ctl_elem_value *ucontrol)
  629. {
  630. struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
  631. int addr = kcontrol->private_value;
  632. guard(spinlock_irq)(&dummy->mixer_lock);
  633. ucontrol->value.integer.value[0] = dummy->mixer_volume[addr][0];
  634. ucontrol->value.integer.value[1] = dummy->mixer_volume[addr][1];
  635. return 0;
  636. }
  637. static int snd_dummy_volume_put(struct snd_kcontrol *kcontrol,
  638. struct snd_ctl_elem_value *ucontrol)
  639. {
  640. struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
  641. int change, addr = kcontrol->private_value;
  642. int left, right;
  643. left = ucontrol->value.integer.value[0];
  644. if (left < mixer_volume_level_min)
  645. left = mixer_volume_level_min;
  646. if (left > mixer_volume_level_max)
  647. left = mixer_volume_level_max;
  648. right = ucontrol->value.integer.value[1];
  649. if (right < mixer_volume_level_min)
  650. right = mixer_volume_level_min;
  651. if (right > mixer_volume_level_max)
  652. right = mixer_volume_level_max;
  653. guard(spinlock_irq)(&dummy->mixer_lock);
  654. change = dummy->mixer_volume[addr][0] != left ||
  655. dummy->mixer_volume[addr][1] != right;
  656. dummy->mixer_volume[addr][0] = left;
  657. dummy->mixer_volume[addr][1] = right;
  658. return change;
  659. }
  660. static const DECLARE_TLV_DB_SCALE(db_scale_dummy, -4500, 30, 0);
  661. #define DUMMY_CAPSRC(xname, xindex, addr) \
  662. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
  663. .info = snd_dummy_capsrc_info, \
  664. .get = snd_dummy_capsrc_get, .put = snd_dummy_capsrc_put, \
  665. .private_value = addr }
  666. #define snd_dummy_capsrc_info snd_ctl_boolean_stereo_info
  667. static int snd_dummy_capsrc_get(struct snd_kcontrol *kcontrol,
  668. struct snd_ctl_elem_value *ucontrol)
  669. {
  670. struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
  671. int addr = kcontrol->private_value;
  672. guard(spinlock_irq)(&dummy->mixer_lock);
  673. ucontrol->value.integer.value[0] = dummy->capture_source[addr][0];
  674. ucontrol->value.integer.value[1] = dummy->capture_source[addr][1];
  675. return 0;
  676. }
  677. static int snd_dummy_capsrc_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  678. {
  679. struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
  680. int change, addr = kcontrol->private_value;
  681. int left, right;
  682. left = ucontrol->value.integer.value[0] & 1;
  683. right = ucontrol->value.integer.value[1] & 1;
  684. guard(spinlock_irq)(&dummy->mixer_lock);
  685. change = dummy->capture_source[addr][0] != left &&
  686. dummy->capture_source[addr][1] != right;
  687. dummy->capture_source[addr][0] = left;
  688. dummy->capture_source[addr][1] = right;
  689. return change;
  690. }
  691. static int snd_dummy_iobox_info(struct snd_kcontrol *kcontrol,
  692. struct snd_ctl_elem_info *info)
  693. {
  694. static const char *const names[] = { "None", "CD Player" };
  695. return snd_ctl_enum_info(info, 1, 2, names);
  696. }
  697. static int snd_dummy_iobox_get(struct snd_kcontrol *kcontrol,
  698. struct snd_ctl_elem_value *value)
  699. {
  700. struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
  701. value->value.enumerated.item[0] = dummy->iobox;
  702. return 0;
  703. }
  704. static int snd_dummy_iobox_put(struct snd_kcontrol *kcontrol,
  705. struct snd_ctl_elem_value *value)
  706. {
  707. struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
  708. int changed;
  709. if (value->value.enumerated.item[0] > 1)
  710. return -EINVAL;
  711. changed = value->value.enumerated.item[0] != dummy->iobox;
  712. if (changed) {
  713. dummy->iobox = value->value.enumerated.item[0];
  714. if (dummy->iobox) {
  715. dummy->cd_volume_ctl->vd[0].access &=
  716. ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  717. dummy->cd_switch_ctl->vd[0].access &=
  718. ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  719. } else {
  720. dummy->cd_volume_ctl->vd[0].access |=
  721. SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  722. dummy->cd_switch_ctl->vd[0].access |=
  723. SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  724. }
  725. snd_ctl_notify(dummy->card, SNDRV_CTL_EVENT_MASK_INFO,
  726. &dummy->cd_volume_ctl->id);
  727. snd_ctl_notify(dummy->card, SNDRV_CTL_EVENT_MASK_INFO,
  728. &dummy->cd_switch_ctl->id);
  729. }
  730. return changed;
  731. }
  732. static const struct snd_kcontrol_new snd_dummy_controls[] = {
  733. DUMMY_VOLUME("Master Volume", 0, MIXER_ADDR_MASTER),
  734. DUMMY_CAPSRC("Master Capture Switch", 0, MIXER_ADDR_MASTER),
  735. DUMMY_VOLUME("Synth Volume", 0, MIXER_ADDR_SYNTH),
  736. DUMMY_CAPSRC("Synth Capture Switch", 0, MIXER_ADDR_SYNTH),
  737. DUMMY_VOLUME("Line Volume", 0, MIXER_ADDR_LINE),
  738. DUMMY_CAPSRC("Line Capture Switch", 0, MIXER_ADDR_LINE),
  739. DUMMY_VOLUME("Mic Volume", 0, MIXER_ADDR_MIC),
  740. DUMMY_CAPSRC("Mic Capture Switch", 0, MIXER_ADDR_MIC),
  741. DUMMY_VOLUME("CD Volume", 0, MIXER_ADDR_CD),
  742. DUMMY_CAPSRC("CD Capture Switch", 0, MIXER_ADDR_CD),
  743. {
  744. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  745. .name = "External I/O Box",
  746. .info = snd_dummy_iobox_info,
  747. .get = snd_dummy_iobox_get,
  748. .put = snd_dummy_iobox_put,
  749. },
  750. };
  751. static int snd_card_dummy_new_mixer(struct snd_dummy *dummy)
  752. {
  753. struct snd_card *card = dummy->card;
  754. struct snd_kcontrol *kcontrol;
  755. unsigned int idx;
  756. int err;
  757. spin_lock_init(&dummy->mixer_lock);
  758. strscpy(card->mixername, "Dummy Mixer");
  759. dummy->iobox = 1;
  760. for (idx = 0; idx < ARRAY_SIZE(snd_dummy_controls); idx++) {
  761. kcontrol = snd_ctl_new1(&snd_dummy_controls[idx], dummy);
  762. err = snd_ctl_add(card, kcontrol);
  763. if (err < 0)
  764. return err;
  765. if (!strcmp(kcontrol->id.name, "CD Volume"))
  766. dummy->cd_volume_ctl = kcontrol;
  767. else if (!strcmp(kcontrol->id.name, "CD Capture Switch"))
  768. dummy->cd_switch_ctl = kcontrol;
  769. }
  770. return 0;
  771. }
  772. #if defined(CONFIG_SND_DEBUG) && defined(CONFIG_SND_PROC_FS)
  773. /*
  774. * proc interface
  775. */
  776. static void print_formats(struct snd_dummy *dummy,
  777. struct snd_info_buffer *buffer)
  778. {
  779. snd_pcm_format_t i;
  780. pcm_for_each_format(i) {
  781. if (dummy->pcm_hw.formats & pcm_format_to_bits(i))
  782. snd_iprintf(buffer, " %s", snd_pcm_format_name(i));
  783. }
  784. }
  785. static void print_rates(struct snd_dummy *dummy,
  786. struct snd_info_buffer *buffer)
  787. {
  788. static const int rates[] = {
  789. 5512, 8000, 11025, 16000, 22050, 32000, 44100, 48000,
  790. 64000, 88200, 96000, 176400, 192000,
  791. };
  792. int i;
  793. if (dummy->pcm_hw.rates & SNDRV_PCM_RATE_CONTINUOUS)
  794. snd_iprintf(buffer, " continuous");
  795. if (dummy->pcm_hw.rates & SNDRV_PCM_RATE_KNOT)
  796. snd_iprintf(buffer, " knot");
  797. for (i = 0; i < ARRAY_SIZE(rates); i++)
  798. if (dummy->pcm_hw.rates & (1 << i))
  799. snd_iprintf(buffer, " %d", rates[i]);
  800. }
  801. #define get_dummy_int_ptr(dummy, ofs) \
  802. (unsigned int *)((char *)&((dummy)->pcm_hw) + (ofs))
  803. #define get_dummy_ll_ptr(dummy, ofs) \
  804. (unsigned long long *)((char *)&((dummy)->pcm_hw) + (ofs))
  805. struct dummy_hw_field {
  806. const char *name;
  807. const char *format;
  808. unsigned int offset;
  809. unsigned int size;
  810. };
  811. #define FIELD_ENTRY(item, fmt) { \
  812. .name = #item, \
  813. .format = fmt, \
  814. .offset = offsetof(struct snd_pcm_hardware, item), \
  815. .size = sizeof(dummy_pcm_hardware.item) }
  816. static const struct dummy_hw_field fields[] = {
  817. FIELD_ENTRY(formats, "%#llx"),
  818. FIELD_ENTRY(rates, "%#x"),
  819. FIELD_ENTRY(rate_min, "%d"),
  820. FIELD_ENTRY(rate_max, "%d"),
  821. FIELD_ENTRY(channels_min, "%d"),
  822. FIELD_ENTRY(channels_max, "%d"),
  823. FIELD_ENTRY(buffer_bytes_max, "%ld"),
  824. FIELD_ENTRY(period_bytes_min, "%ld"),
  825. FIELD_ENTRY(period_bytes_max, "%ld"),
  826. FIELD_ENTRY(periods_min, "%d"),
  827. FIELD_ENTRY(periods_max, "%d"),
  828. };
  829. static void dummy_proc_read(struct snd_info_entry *entry,
  830. struct snd_info_buffer *buffer)
  831. {
  832. struct snd_dummy *dummy = entry->private_data;
  833. int i;
  834. for (i = 0; i < ARRAY_SIZE(fields); i++) {
  835. snd_iprintf(buffer, "%s ", fields[i].name);
  836. if (fields[i].size == sizeof(int))
  837. snd_iprintf(buffer, fields[i].format,
  838. *get_dummy_int_ptr(dummy, fields[i].offset));
  839. else
  840. snd_iprintf(buffer, fields[i].format,
  841. *get_dummy_ll_ptr(dummy, fields[i].offset));
  842. if (!strcmp(fields[i].name, "formats"))
  843. print_formats(dummy, buffer);
  844. else if (!strcmp(fields[i].name, "rates"))
  845. print_rates(dummy, buffer);
  846. snd_iprintf(buffer, "\n");
  847. }
  848. }
  849. static void dummy_proc_write(struct snd_info_entry *entry,
  850. struct snd_info_buffer *buffer)
  851. {
  852. struct snd_dummy *dummy = entry->private_data;
  853. char line[64];
  854. while (!snd_info_get_line(buffer, line, sizeof(line))) {
  855. char item[20];
  856. const char *ptr;
  857. unsigned long long val;
  858. int i;
  859. ptr = snd_info_get_str(item, line, sizeof(item));
  860. for (i = 0; i < ARRAY_SIZE(fields); i++) {
  861. if (!strcmp(item, fields[i].name))
  862. break;
  863. }
  864. if (i >= ARRAY_SIZE(fields))
  865. continue;
  866. snd_info_get_str(item, ptr, sizeof(item));
  867. if (kstrtoull(item, 0, &val))
  868. continue;
  869. if (fields[i].size == sizeof(int))
  870. *get_dummy_int_ptr(dummy, fields[i].offset) = val;
  871. else
  872. *get_dummy_ll_ptr(dummy, fields[i].offset) = val;
  873. }
  874. }
  875. static void dummy_proc_init(struct snd_dummy *chip)
  876. {
  877. snd_card_rw_proc_new(chip->card, "dummy_pcm", chip,
  878. dummy_proc_read, dummy_proc_write);
  879. }
  880. #else
  881. #define dummy_proc_init(x)
  882. #endif /* CONFIG_SND_DEBUG && CONFIG_SND_PROC_FS */
  883. static int snd_dummy_probe(struct platform_device *devptr)
  884. {
  885. struct snd_card *card;
  886. struct snd_dummy *dummy;
  887. const struct dummy_model *m = NULL, **mdl;
  888. int idx, err;
  889. int dev = devptr->id;
  890. err = snd_devm_card_new(&devptr->dev, index[dev], id[dev], THIS_MODULE,
  891. sizeof(struct snd_dummy), &card);
  892. if (err < 0)
  893. return err;
  894. dummy = card->private_data;
  895. dummy->card = card;
  896. for (mdl = dummy_models; *mdl && model[dev]; mdl++) {
  897. if (strcmp(model[dev], (*mdl)->name) == 0) {
  898. pr_info("snd-dummy: Using model '%s' for card %i\n",
  899. (*mdl)->name, card->number);
  900. m = dummy->model = *mdl;
  901. break;
  902. }
  903. }
  904. for (idx = 0; idx < MAX_PCM_DEVICES && idx < pcm_devs[dev]; idx++) {
  905. if (pcm_substreams[dev] < 1)
  906. pcm_substreams[dev] = 1;
  907. if (pcm_substreams[dev] > MAX_PCM_SUBSTREAMS)
  908. pcm_substreams[dev] = MAX_PCM_SUBSTREAMS;
  909. err = snd_card_dummy_pcm(dummy, idx, pcm_substreams[dev]);
  910. if (err < 0)
  911. return err;
  912. }
  913. dummy->pcm_hw = dummy_pcm_hardware;
  914. if (m) {
  915. if (m->formats)
  916. dummy->pcm_hw.formats = m->formats;
  917. if (m->buffer_bytes_max)
  918. dummy->pcm_hw.buffer_bytes_max = m->buffer_bytes_max;
  919. if (m->period_bytes_min)
  920. dummy->pcm_hw.period_bytes_min = m->period_bytes_min;
  921. if (m->period_bytes_max)
  922. dummy->pcm_hw.period_bytes_max = m->period_bytes_max;
  923. if (m->periods_min)
  924. dummy->pcm_hw.periods_min = m->periods_min;
  925. if (m->periods_max)
  926. dummy->pcm_hw.periods_max = m->periods_max;
  927. if (m->rates)
  928. dummy->pcm_hw.rates = m->rates;
  929. if (m->rate_min)
  930. dummy->pcm_hw.rate_min = m->rate_min;
  931. if (m->rate_max)
  932. dummy->pcm_hw.rate_max = m->rate_max;
  933. if (m->channels_min)
  934. dummy->pcm_hw.channels_min = m->channels_min;
  935. if (m->channels_max)
  936. dummy->pcm_hw.channels_max = m->channels_max;
  937. }
  938. if (mixer_volume_level_min > mixer_volume_level_max) {
  939. pr_warn("snd-dummy: Invalid mixer volume level: min=%d, max=%d. Fall back to default value.\n",
  940. mixer_volume_level_min, mixer_volume_level_max);
  941. mixer_volume_level_min = USE_MIXER_VOLUME_LEVEL_MIN;
  942. mixer_volume_level_max = USE_MIXER_VOLUME_LEVEL_MAX;
  943. }
  944. err = snd_card_dummy_new_mixer(dummy);
  945. if (err < 0)
  946. return err;
  947. strscpy(card->driver, "Dummy");
  948. strscpy(card->shortname, "Dummy");
  949. sprintf(card->longname, "Dummy %i", dev + 1);
  950. dummy_proc_init(dummy);
  951. err = snd_card_register(card);
  952. if (err < 0)
  953. return err;
  954. platform_set_drvdata(devptr, card);
  955. return 0;
  956. }
  957. static int snd_dummy_suspend(struct device *pdev)
  958. {
  959. struct snd_card *card = dev_get_drvdata(pdev);
  960. snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
  961. return 0;
  962. }
  963. static int snd_dummy_resume(struct device *pdev)
  964. {
  965. struct snd_card *card = dev_get_drvdata(pdev);
  966. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  967. return 0;
  968. }
  969. static DEFINE_SIMPLE_DEV_PM_OPS(snd_dummy_pm, snd_dummy_suspend, snd_dummy_resume);
  970. #define SND_DUMMY_DRIVER "snd_dummy"
  971. static struct platform_driver snd_dummy_driver = {
  972. .probe = snd_dummy_probe,
  973. .driver = {
  974. .name = SND_DUMMY_DRIVER,
  975. .pm = &snd_dummy_pm,
  976. },
  977. };
  978. static void snd_dummy_unregister_all(void)
  979. {
  980. int i;
  981. for (i = 0; i < ARRAY_SIZE(devices); ++i)
  982. platform_device_unregister(devices[i]);
  983. platform_driver_unregister(&snd_dummy_driver);
  984. free_fake_buffer();
  985. }
  986. static int __init alsa_card_dummy_init(void)
  987. {
  988. int i, cards, err;
  989. err = platform_driver_register(&snd_dummy_driver);
  990. if (err < 0)
  991. return err;
  992. err = alloc_fake_buffer();
  993. if (err < 0) {
  994. platform_driver_unregister(&snd_dummy_driver);
  995. return err;
  996. }
  997. cards = 0;
  998. for (i = 0; i < SNDRV_CARDS; i++) {
  999. struct platform_device *device;
  1000. if (! enable[i])
  1001. continue;
  1002. device = platform_device_register_simple(SND_DUMMY_DRIVER,
  1003. i, NULL, 0);
  1004. if (IS_ERR(device))
  1005. continue;
  1006. if (!platform_get_drvdata(device)) {
  1007. platform_device_unregister(device);
  1008. continue;
  1009. }
  1010. devices[i] = device;
  1011. cards++;
  1012. }
  1013. if (!cards) {
  1014. #ifdef MODULE
  1015. pr_err("Dummy soundcard not found or device busy\n");
  1016. #endif
  1017. snd_dummy_unregister_all();
  1018. return -ENODEV;
  1019. }
  1020. return 0;
  1021. }
  1022. static void __exit alsa_card_dummy_exit(void)
  1023. {
  1024. snd_dummy_unregister_all();
  1025. }
  1026. module_init(alsa_card_dummy_init)
  1027. module_exit(alsa_card_dummy_exit)