dummy.c 32 KB

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