dummy.c 31 KB

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