dummy.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226
  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(unsigned long data)
  278. {
  279. struct dummy_systimer_pcm *dpcm = (struct dummy_systimer_pcm *)data;
  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. setup_timer(&dpcm->timer, dummy_systimer_callback,
  310. (unsigned long) dpcm);
  311. spin_lock_init(&dpcm->lock);
  312. dpcm->substream = substream;
  313. return 0;
  314. }
  315. static void dummy_systimer_free(struct snd_pcm_substream *substream)
  316. {
  317. kfree(substream->runtime->private_data);
  318. }
  319. static const struct dummy_timer_ops dummy_systimer_ops = {
  320. .create = dummy_systimer_create,
  321. .free = dummy_systimer_free,
  322. .prepare = dummy_systimer_prepare,
  323. .start = dummy_systimer_start,
  324. .stop = dummy_systimer_stop,
  325. .pointer = dummy_systimer_pointer,
  326. };
  327. #ifdef CONFIG_HIGH_RES_TIMERS
  328. /*
  329. * hrtimer interface
  330. */
  331. struct dummy_hrtimer_pcm {
  332. /* ops must be the first item */
  333. const struct dummy_timer_ops *timer_ops;
  334. ktime_t base_time;
  335. ktime_t period_time;
  336. atomic_t running;
  337. struct hrtimer timer;
  338. struct tasklet_struct tasklet;
  339. struct snd_pcm_substream *substream;
  340. };
  341. static void dummy_hrtimer_pcm_elapsed(unsigned long priv)
  342. {
  343. struct dummy_hrtimer_pcm *dpcm = (struct dummy_hrtimer_pcm *)priv;
  344. if (atomic_read(&dpcm->running))
  345. snd_pcm_period_elapsed(dpcm->substream);
  346. }
  347. static enum hrtimer_restart dummy_hrtimer_callback(struct hrtimer *timer)
  348. {
  349. struct dummy_hrtimer_pcm *dpcm;
  350. dpcm = container_of(timer, struct dummy_hrtimer_pcm, timer);
  351. if (!atomic_read(&dpcm->running))
  352. return HRTIMER_NORESTART;
  353. tasklet_schedule(&dpcm->tasklet);
  354. hrtimer_forward_now(timer, dpcm->period_time);
  355. return HRTIMER_RESTART;
  356. }
  357. static int dummy_hrtimer_start(struct snd_pcm_substream *substream)
  358. {
  359. struct dummy_hrtimer_pcm *dpcm = substream->runtime->private_data;
  360. dpcm->base_time = hrtimer_cb_get_time(&dpcm->timer);
  361. hrtimer_start(&dpcm->timer, dpcm->period_time, HRTIMER_MODE_REL);
  362. atomic_set(&dpcm->running, 1);
  363. return 0;
  364. }
  365. static int dummy_hrtimer_stop(struct snd_pcm_substream *substream)
  366. {
  367. struct dummy_hrtimer_pcm *dpcm = substream->runtime->private_data;
  368. atomic_set(&dpcm->running, 0);
  369. hrtimer_cancel(&dpcm->timer);
  370. return 0;
  371. }
  372. static inline void dummy_hrtimer_sync(struct dummy_hrtimer_pcm *dpcm)
  373. {
  374. hrtimer_cancel(&dpcm->timer);
  375. tasklet_kill(&dpcm->tasklet);
  376. }
  377. static snd_pcm_uframes_t
  378. dummy_hrtimer_pointer(struct snd_pcm_substream *substream)
  379. {
  380. struct snd_pcm_runtime *runtime = substream->runtime;
  381. struct dummy_hrtimer_pcm *dpcm = runtime->private_data;
  382. u64 delta;
  383. u32 pos;
  384. delta = ktime_us_delta(hrtimer_cb_get_time(&dpcm->timer),
  385. dpcm->base_time);
  386. delta = div_u64(delta * runtime->rate + 999999, 1000000);
  387. div_u64_rem(delta, runtime->buffer_size, &pos);
  388. return pos;
  389. }
  390. static int dummy_hrtimer_prepare(struct snd_pcm_substream *substream)
  391. {
  392. struct snd_pcm_runtime *runtime = substream->runtime;
  393. struct dummy_hrtimer_pcm *dpcm = runtime->private_data;
  394. unsigned int period, rate;
  395. long sec;
  396. unsigned long nsecs;
  397. dummy_hrtimer_sync(dpcm);
  398. period = runtime->period_size;
  399. rate = runtime->rate;
  400. sec = period / rate;
  401. period %= rate;
  402. nsecs = div_u64((u64)period * 1000000000UL + rate - 1, rate);
  403. dpcm->period_time = ktime_set(sec, nsecs);
  404. return 0;
  405. }
  406. static int dummy_hrtimer_create(struct snd_pcm_substream *substream)
  407. {
  408. struct dummy_hrtimer_pcm *dpcm;
  409. dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
  410. if (!dpcm)
  411. return -ENOMEM;
  412. substream->runtime->private_data = dpcm;
  413. hrtimer_init(&dpcm->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  414. dpcm->timer.function = dummy_hrtimer_callback;
  415. dpcm->substream = substream;
  416. atomic_set(&dpcm->running, 0);
  417. tasklet_init(&dpcm->tasklet, dummy_hrtimer_pcm_elapsed,
  418. (unsigned long)dpcm);
  419. return 0;
  420. }
  421. static void dummy_hrtimer_free(struct snd_pcm_substream *substream)
  422. {
  423. struct dummy_hrtimer_pcm *dpcm = substream->runtime->private_data;
  424. dummy_hrtimer_sync(dpcm);
  425. kfree(dpcm);
  426. }
  427. static const struct dummy_timer_ops dummy_hrtimer_ops = {
  428. .create = dummy_hrtimer_create,
  429. .free = dummy_hrtimer_free,
  430. .prepare = dummy_hrtimer_prepare,
  431. .start = dummy_hrtimer_start,
  432. .stop = dummy_hrtimer_stop,
  433. .pointer = dummy_hrtimer_pointer,
  434. };
  435. #endif /* CONFIG_HIGH_RES_TIMERS */
  436. /*
  437. * PCM interface
  438. */
  439. static int dummy_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  440. {
  441. switch (cmd) {
  442. case SNDRV_PCM_TRIGGER_START:
  443. case SNDRV_PCM_TRIGGER_RESUME:
  444. return get_dummy_ops(substream)->start(substream);
  445. case SNDRV_PCM_TRIGGER_STOP:
  446. case SNDRV_PCM_TRIGGER_SUSPEND:
  447. return get_dummy_ops(substream)->stop(substream);
  448. }
  449. return -EINVAL;
  450. }
  451. static int dummy_pcm_prepare(struct snd_pcm_substream *substream)
  452. {
  453. return get_dummy_ops(substream)->prepare(substream);
  454. }
  455. static snd_pcm_uframes_t dummy_pcm_pointer(struct snd_pcm_substream *substream)
  456. {
  457. return get_dummy_ops(substream)->pointer(substream);
  458. }
  459. static struct snd_pcm_hardware dummy_pcm_hardware = {
  460. .info = (SNDRV_PCM_INFO_MMAP |
  461. SNDRV_PCM_INFO_INTERLEAVED |
  462. SNDRV_PCM_INFO_RESUME |
  463. SNDRV_PCM_INFO_MMAP_VALID),
  464. .formats = USE_FORMATS,
  465. .rates = USE_RATE,
  466. .rate_min = USE_RATE_MIN,
  467. .rate_max = USE_RATE_MAX,
  468. .channels_min = USE_CHANNELS_MIN,
  469. .channels_max = USE_CHANNELS_MAX,
  470. .buffer_bytes_max = MAX_BUFFER_SIZE,
  471. .period_bytes_min = MIN_PERIOD_SIZE,
  472. .period_bytes_max = MAX_PERIOD_SIZE,
  473. .periods_min = USE_PERIODS_MIN,
  474. .periods_max = USE_PERIODS_MAX,
  475. .fifo_size = 0,
  476. };
  477. static int dummy_pcm_hw_params(struct snd_pcm_substream *substream,
  478. struct snd_pcm_hw_params *hw_params)
  479. {
  480. if (fake_buffer) {
  481. /* runtime->dma_bytes has to be set manually to allow mmap */
  482. substream->runtime->dma_bytes = params_buffer_bytes(hw_params);
  483. return 0;
  484. }
  485. return snd_pcm_lib_malloc_pages(substream,
  486. params_buffer_bytes(hw_params));
  487. }
  488. static int dummy_pcm_hw_free(struct snd_pcm_substream *substream)
  489. {
  490. if (fake_buffer)
  491. return 0;
  492. return snd_pcm_lib_free_pages(substream);
  493. }
  494. static int dummy_pcm_open(struct snd_pcm_substream *substream)
  495. {
  496. struct snd_dummy *dummy = snd_pcm_substream_chip(substream);
  497. struct dummy_model *model = dummy->model;
  498. struct snd_pcm_runtime *runtime = substream->runtime;
  499. const struct dummy_timer_ops *ops;
  500. int err;
  501. ops = &dummy_systimer_ops;
  502. #ifdef CONFIG_HIGH_RES_TIMERS
  503. if (hrtimer)
  504. ops = &dummy_hrtimer_ops;
  505. #endif
  506. err = ops->create(substream);
  507. if (err < 0)
  508. return err;
  509. get_dummy_ops(substream) = ops;
  510. runtime->hw = dummy->pcm_hw;
  511. if (substream->pcm->device & 1) {
  512. runtime->hw.info &= ~SNDRV_PCM_INFO_INTERLEAVED;
  513. runtime->hw.info |= SNDRV_PCM_INFO_NONINTERLEAVED;
  514. }
  515. if (substream->pcm->device & 2)
  516. runtime->hw.info &= ~(SNDRV_PCM_INFO_MMAP |
  517. SNDRV_PCM_INFO_MMAP_VALID);
  518. if (model == NULL)
  519. return 0;
  520. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  521. if (model->playback_constraints)
  522. err = model->playback_constraints(substream->runtime);
  523. } else {
  524. if (model->capture_constraints)
  525. err = model->capture_constraints(substream->runtime);
  526. }
  527. if (err < 0) {
  528. get_dummy_ops(substream)->free(substream);
  529. return err;
  530. }
  531. return 0;
  532. }
  533. static int dummy_pcm_close(struct snd_pcm_substream *substream)
  534. {
  535. get_dummy_ops(substream)->free(substream);
  536. return 0;
  537. }
  538. /*
  539. * dummy buffer handling
  540. */
  541. static void *dummy_page[2];
  542. static void free_fake_buffer(void)
  543. {
  544. if (fake_buffer) {
  545. int i;
  546. for (i = 0; i < 2; i++)
  547. if (dummy_page[i]) {
  548. free_page((unsigned long)dummy_page[i]);
  549. dummy_page[i] = NULL;
  550. }
  551. }
  552. }
  553. static int alloc_fake_buffer(void)
  554. {
  555. int i;
  556. if (!fake_buffer)
  557. return 0;
  558. for (i = 0; i < 2; i++) {
  559. dummy_page[i] = (void *)get_zeroed_page(GFP_KERNEL);
  560. if (!dummy_page[i]) {
  561. free_fake_buffer();
  562. return -ENOMEM;
  563. }
  564. }
  565. return 0;
  566. }
  567. static int dummy_pcm_copy(struct snd_pcm_substream *substream,
  568. int channel, snd_pcm_uframes_t pos,
  569. void __user *dst, snd_pcm_uframes_t count)
  570. {
  571. return 0; /* do nothing */
  572. }
  573. static int dummy_pcm_silence(struct snd_pcm_substream *substream,
  574. int channel, snd_pcm_uframes_t pos,
  575. snd_pcm_uframes_t count)
  576. {
  577. return 0; /* do nothing */
  578. }
  579. static struct page *dummy_pcm_page(struct snd_pcm_substream *substream,
  580. unsigned long offset)
  581. {
  582. return virt_to_page(dummy_page[substream->stream]); /* the same page */
  583. }
  584. static struct snd_pcm_ops dummy_pcm_ops = {
  585. .open = dummy_pcm_open,
  586. .close = dummy_pcm_close,
  587. .ioctl = snd_pcm_lib_ioctl,
  588. .hw_params = dummy_pcm_hw_params,
  589. .hw_free = dummy_pcm_hw_free,
  590. .prepare = dummy_pcm_prepare,
  591. .trigger = dummy_pcm_trigger,
  592. .pointer = dummy_pcm_pointer,
  593. };
  594. static struct snd_pcm_ops dummy_pcm_ops_no_buf = {
  595. .open = dummy_pcm_open,
  596. .close = dummy_pcm_close,
  597. .ioctl = snd_pcm_lib_ioctl,
  598. .hw_params = dummy_pcm_hw_params,
  599. .hw_free = dummy_pcm_hw_free,
  600. .prepare = dummy_pcm_prepare,
  601. .trigger = dummy_pcm_trigger,
  602. .pointer = dummy_pcm_pointer,
  603. .copy = dummy_pcm_copy,
  604. .silence = dummy_pcm_silence,
  605. .page = dummy_pcm_page,
  606. };
  607. static int snd_card_dummy_pcm(struct snd_dummy *dummy, int device,
  608. int substreams)
  609. {
  610. struct snd_pcm *pcm;
  611. struct snd_pcm_ops *ops;
  612. int err;
  613. err = snd_pcm_new(dummy->card, "Dummy PCM", device,
  614. substreams, substreams, &pcm);
  615. if (err < 0)
  616. return err;
  617. dummy->pcm = pcm;
  618. if (fake_buffer)
  619. ops = &dummy_pcm_ops_no_buf;
  620. else
  621. ops = &dummy_pcm_ops;
  622. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, ops);
  623. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, ops);
  624. pcm->private_data = dummy;
  625. pcm->info_flags = 0;
  626. strcpy(pcm->name, "Dummy PCM");
  627. if (!fake_buffer) {
  628. snd_pcm_lib_preallocate_pages_for_all(pcm,
  629. SNDRV_DMA_TYPE_CONTINUOUS,
  630. snd_dma_continuous_data(GFP_KERNEL),
  631. 0, 64*1024);
  632. }
  633. return 0;
  634. }
  635. /*
  636. * mixer interface
  637. */
  638. #define DUMMY_VOLUME(xname, xindex, addr) \
  639. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
  640. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
  641. .name = xname, .index = xindex, \
  642. .info = snd_dummy_volume_info, \
  643. .get = snd_dummy_volume_get, .put = snd_dummy_volume_put, \
  644. .private_value = addr, \
  645. .tlv = { .p = db_scale_dummy } }
  646. static int snd_dummy_volume_info(struct snd_kcontrol *kcontrol,
  647. struct snd_ctl_elem_info *uinfo)
  648. {
  649. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  650. uinfo->count = 2;
  651. uinfo->value.integer.min = -50;
  652. uinfo->value.integer.max = 100;
  653. return 0;
  654. }
  655. static int snd_dummy_volume_get(struct snd_kcontrol *kcontrol,
  656. struct snd_ctl_elem_value *ucontrol)
  657. {
  658. struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
  659. int addr = kcontrol->private_value;
  660. spin_lock_irq(&dummy->mixer_lock);
  661. ucontrol->value.integer.value[0] = dummy->mixer_volume[addr][0];
  662. ucontrol->value.integer.value[1] = dummy->mixer_volume[addr][1];
  663. spin_unlock_irq(&dummy->mixer_lock);
  664. return 0;
  665. }
  666. static int snd_dummy_volume_put(struct snd_kcontrol *kcontrol,
  667. struct snd_ctl_elem_value *ucontrol)
  668. {
  669. struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
  670. int change, addr = kcontrol->private_value;
  671. int left, right;
  672. left = ucontrol->value.integer.value[0];
  673. if (left < -50)
  674. left = -50;
  675. if (left > 100)
  676. left = 100;
  677. right = ucontrol->value.integer.value[1];
  678. if (right < -50)
  679. right = -50;
  680. if (right > 100)
  681. right = 100;
  682. spin_lock_irq(&dummy->mixer_lock);
  683. change = dummy->mixer_volume[addr][0] != left ||
  684. dummy->mixer_volume[addr][1] != right;
  685. dummy->mixer_volume[addr][0] = left;
  686. dummy->mixer_volume[addr][1] = right;
  687. spin_unlock_irq(&dummy->mixer_lock);
  688. return change;
  689. }
  690. static const DECLARE_TLV_DB_SCALE(db_scale_dummy, -4500, 30, 0);
  691. #define DUMMY_CAPSRC(xname, xindex, addr) \
  692. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
  693. .info = snd_dummy_capsrc_info, \
  694. .get = snd_dummy_capsrc_get, .put = snd_dummy_capsrc_put, \
  695. .private_value = addr }
  696. #define snd_dummy_capsrc_info snd_ctl_boolean_stereo_info
  697. static int snd_dummy_capsrc_get(struct snd_kcontrol *kcontrol,
  698. struct snd_ctl_elem_value *ucontrol)
  699. {
  700. struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
  701. int addr = kcontrol->private_value;
  702. spin_lock_irq(&dummy->mixer_lock);
  703. ucontrol->value.integer.value[0] = dummy->capture_source[addr][0];
  704. ucontrol->value.integer.value[1] = dummy->capture_source[addr][1];
  705. spin_unlock_irq(&dummy->mixer_lock);
  706. return 0;
  707. }
  708. static int snd_dummy_capsrc_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  709. {
  710. struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
  711. int change, addr = kcontrol->private_value;
  712. int left, right;
  713. left = ucontrol->value.integer.value[0] & 1;
  714. right = ucontrol->value.integer.value[1] & 1;
  715. spin_lock_irq(&dummy->mixer_lock);
  716. change = dummy->capture_source[addr][0] != left &&
  717. dummy->capture_source[addr][1] != right;
  718. dummy->capture_source[addr][0] = left;
  719. dummy->capture_source[addr][1] = right;
  720. spin_unlock_irq(&dummy->mixer_lock);
  721. return change;
  722. }
  723. static int snd_dummy_iobox_info(struct snd_kcontrol *kcontrol,
  724. struct snd_ctl_elem_info *info)
  725. {
  726. const char *const names[] = { "None", "CD Player" };
  727. return snd_ctl_enum_info(info, 1, 2, names);
  728. }
  729. static int snd_dummy_iobox_get(struct snd_kcontrol *kcontrol,
  730. struct snd_ctl_elem_value *value)
  731. {
  732. struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
  733. value->value.enumerated.item[0] = dummy->iobox;
  734. return 0;
  735. }
  736. static int snd_dummy_iobox_put(struct snd_kcontrol *kcontrol,
  737. struct snd_ctl_elem_value *value)
  738. {
  739. struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
  740. int changed;
  741. if (value->value.enumerated.item[0] > 1)
  742. return -EINVAL;
  743. changed = value->value.enumerated.item[0] != dummy->iobox;
  744. if (changed) {
  745. dummy->iobox = value->value.enumerated.item[0];
  746. if (dummy->iobox) {
  747. dummy->cd_volume_ctl->vd[0].access &=
  748. ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  749. dummy->cd_switch_ctl->vd[0].access &=
  750. ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  751. } else {
  752. dummy->cd_volume_ctl->vd[0].access |=
  753. SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  754. dummy->cd_switch_ctl->vd[0].access |=
  755. SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  756. }
  757. snd_ctl_notify(dummy->card, SNDRV_CTL_EVENT_MASK_INFO,
  758. &dummy->cd_volume_ctl->id);
  759. snd_ctl_notify(dummy->card, SNDRV_CTL_EVENT_MASK_INFO,
  760. &dummy->cd_switch_ctl->id);
  761. }
  762. return changed;
  763. }
  764. static struct snd_kcontrol_new snd_dummy_controls[] = {
  765. DUMMY_VOLUME("Master Volume", 0, MIXER_ADDR_MASTER),
  766. DUMMY_CAPSRC("Master Capture Switch", 0, MIXER_ADDR_MASTER),
  767. DUMMY_VOLUME("Synth Volume", 0, MIXER_ADDR_SYNTH),
  768. DUMMY_CAPSRC("Synth Capture Switch", 0, MIXER_ADDR_SYNTH),
  769. DUMMY_VOLUME("Line Volume", 0, MIXER_ADDR_LINE),
  770. DUMMY_CAPSRC("Line Capture Switch", 0, MIXER_ADDR_LINE),
  771. DUMMY_VOLUME("Mic Volume", 0, MIXER_ADDR_MIC),
  772. DUMMY_CAPSRC("Mic Capture Switch", 0, MIXER_ADDR_MIC),
  773. DUMMY_VOLUME("CD Volume", 0, MIXER_ADDR_CD),
  774. DUMMY_CAPSRC("CD Capture Switch", 0, MIXER_ADDR_CD),
  775. {
  776. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  777. .name = "External I/O Box",
  778. .info = snd_dummy_iobox_info,
  779. .get = snd_dummy_iobox_get,
  780. .put = snd_dummy_iobox_put,
  781. },
  782. };
  783. static int snd_card_dummy_new_mixer(struct snd_dummy *dummy)
  784. {
  785. struct snd_card *card = dummy->card;
  786. struct snd_kcontrol *kcontrol;
  787. unsigned int idx;
  788. int err;
  789. spin_lock_init(&dummy->mixer_lock);
  790. strcpy(card->mixername, "Dummy Mixer");
  791. dummy->iobox = 1;
  792. for (idx = 0; idx < ARRAY_SIZE(snd_dummy_controls); idx++) {
  793. kcontrol = snd_ctl_new1(&snd_dummy_controls[idx], dummy);
  794. err = snd_ctl_add(card, kcontrol);
  795. if (err < 0)
  796. return err;
  797. if (!strcmp(kcontrol->id.name, "CD Volume"))
  798. dummy->cd_volume_ctl = kcontrol;
  799. else if (!strcmp(kcontrol->id.name, "CD Capture Switch"))
  800. dummy->cd_switch_ctl = kcontrol;
  801. }
  802. return 0;
  803. }
  804. #if defined(CONFIG_SND_DEBUG) && defined(CONFIG_SND_PROC_FS)
  805. /*
  806. * proc interface
  807. */
  808. static void print_formats(struct snd_dummy *dummy,
  809. struct snd_info_buffer *buffer)
  810. {
  811. int i;
  812. for (i = 0; i < SNDRV_PCM_FORMAT_LAST; i++) {
  813. if (dummy->pcm_hw.formats & (1ULL << i))
  814. snd_iprintf(buffer, " %s", snd_pcm_format_name(i));
  815. }
  816. }
  817. static void print_rates(struct snd_dummy *dummy,
  818. struct snd_info_buffer *buffer)
  819. {
  820. static int rates[] = {
  821. 5512, 8000, 11025, 16000, 22050, 32000, 44100, 48000,
  822. 64000, 88200, 96000, 176400, 192000,
  823. };
  824. int i;
  825. if (dummy->pcm_hw.rates & SNDRV_PCM_RATE_CONTINUOUS)
  826. snd_iprintf(buffer, " continuous");
  827. if (dummy->pcm_hw.rates & SNDRV_PCM_RATE_KNOT)
  828. snd_iprintf(buffer, " knot");
  829. for (i = 0; i < ARRAY_SIZE(rates); i++)
  830. if (dummy->pcm_hw.rates & (1 << i))
  831. snd_iprintf(buffer, " %d", rates[i]);
  832. }
  833. #define get_dummy_int_ptr(dummy, ofs) \
  834. (unsigned int *)((char *)&((dummy)->pcm_hw) + (ofs))
  835. #define get_dummy_ll_ptr(dummy, ofs) \
  836. (unsigned long long *)((char *)&((dummy)->pcm_hw) + (ofs))
  837. struct dummy_hw_field {
  838. const char *name;
  839. const char *format;
  840. unsigned int offset;
  841. unsigned int size;
  842. };
  843. #define FIELD_ENTRY(item, fmt) { \
  844. .name = #item, \
  845. .format = fmt, \
  846. .offset = offsetof(struct snd_pcm_hardware, item), \
  847. .size = sizeof(dummy_pcm_hardware.item) }
  848. static struct dummy_hw_field fields[] = {
  849. FIELD_ENTRY(formats, "%#llx"),
  850. FIELD_ENTRY(rates, "%#x"),
  851. FIELD_ENTRY(rate_min, "%d"),
  852. FIELD_ENTRY(rate_max, "%d"),
  853. FIELD_ENTRY(channels_min, "%d"),
  854. FIELD_ENTRY(channels_max, "%d"),
  855. FIELD_ENTRY(buffer_bytes_max, "%ld"),
  856. FIELD_ENTRY(period_bytes_min, "%ld"),
  857. FIELD_ENTRY(period_bytes_max, "%ld"),
  858. FIELD_ENTRY(periods_min, "%d"),
  859. FIELD_ENTRY(periods_max, "%d"),
  860. };
  861. static void dummy_proc_read(struct snd_info_entry *entry,
  862. struct snd_info_buffer *buffer)
  863. {
  864. struct snd_dummy *dummy = entry->private_data;
  865. int i;
  866. for (i = 0; i < ARRAY_SIZE(fields); i++) {
  867. snd_iprintf(buffer, "%s ", fields[i].name);
  868. if (fields[i].size == sizeof(int))
  869. snd_iprintf(buffer, fields[i].format,
  870. *get_dummy_int_ptr(dummy, fields[i].offset));
  871. else
  872. snd_iprintf(buffer, fields[i].format,
  873. *get_dummy_ll_ptr(dummy, fields[i].offset));
  874. if (!strcmp(fields[i].name, "formats"))
  875. print_formats(dummy, buffer);
  876. else if (!strcmp(fields[i].name, "rates"))
  877. print_rates(dummy, buffer);
  878. snd_iprintf(buffer, "\n");
  879. }
  880. }
  881. static void dummy_proc_write(struct snd_info_entry *entry,
  882. struct snd_info_buffer *buffer)
  883. {
  884. struct snd_dummy *dummy = entry->private_data;
  885. char line[64];
  886. while (!snd_info_get_line(buffer, line, sizeof(line))) {
  887. char item[20];
  888. const char *ptr;
  889. unsigned long long val;
  890. int i;
  891. ptr = snd_info_get_str(item, line, sizeof(item));
  892. for (i = 0; i < ARRAY_SIZE(fields); i++) {
  893. if (!strcmp(item, fields[i].name))
  894. break;
  895. }
  896. if (i >= ARRAY_SIZE(fields))
  897. continue;
  898. snd_info_get_str(item, ptr, sizeof(item));
  899. if (kstrtoull(item, 0, &val))
  900. continue;
  901. if (fields[i].size == sizeof(int))
  902. *get_dummy_int_ptr(dummy, fields[i].offset) = val;
  903. else
  904. *get_dummy_ll_ptr(dummy, fields[i].offset) = val;
  905. }
  906. }
  907. static void dummy_proc_init(struct snd_dummy *chip)
  908. {
  909. struct snd_info_entry *entry;
  910. if (!snd_card_proc_new(chip->card, "dummy_pcm", &entry)) {
  911. snd_info_set_text_ops(entry, chip, dummy_proc_read);
  912. entry->c.text.write = dummy_proc_write;
  913. entry->mode |= S_IWUSR;
  914. entry->private_data = chip;
  915. }
  916. }
  917. #else
  918. #define dummy_proc_init(x)
  919. #endif /* CONFIG_SND_DEBUG && CONFIG_SND_PROC_FS */
  920. static int snd_dummy_probe(struct platform_device *devptr)
  921. {
  922. struct snd_card *card;
  923. struct snd_dummy *dummy;
  924. struct dummy_model *m = NULL, **mdl;
  925. int idx, err;
  926. int dev = devptr->id;
  927. err = snd_card_new(&devptr->dev, index[dev], id[dev], THIS_MODULE,
  928. sizeof(struct snd_dummy), &card);
  929. if (err < 0)
  930. return err;
  931. dummy = card->private_data;
  932. dummy->card = card;
  933. for (mdl = dummy_models; *mdl && model[dev]; mdl++) {
  934. if (strcmp(model[dev], (*mdl)->name) == 0) {
  935. printk(KERN_INFO
  936. "snd-dummy: Using model '%s' for card %i\n",
  937. (*mdl)->name, card->number);
  938. m = dummy->model = *mdl;
  939. break;
  940. }
  941. }
  942. for (idx = 0; idx < MAX_PCM_DEVICES && idx < pcm_devs[dev]; idx++) {
  943. if (pcm_substreams[dev] < 1)
  944. pcm_substreams[dev] = 1;
  945. if (pcm_substreams[dev] > MAX_PCM_SUBSTREAMS)
  946. pcm_substreams[dev] = MAX_PCM_SUBSTREAMS;
  947. err = snd_card_dummy_pcm(dummy, idx, pcm_substreams[dev]);
  948. if (err < 0)
  949. goto __nodev;
  950. }
  951. dummy->pcm_hw = dummy_pcm_hardware;
  952. if (m) {
  953. if (m->formats)
  954. dummy->pcm_hw.formats = m->formats;
  955. if (m->buffer_bytes_max)
  956. dummy->pcm_hw.buffer_bytes_max = m->buffer_bytes_max;
  957. if (m->period_bytes_min)
  958. dummy->pcm_hw.period_bytes_min = m->period_bytes_min;
  959. if (m->period_bytes_max)
  960. dummy->pcm_hw.period_bytes_max = m->period_bytes_max;
  961. if (m->periods_min)
  962. dummy->pcm_hw.periods_min = m->periods_min;
  963. if (m->periods_max)
  964. dummy->pcm_hw.periods_max = m->periods_max;
  965. if (m->rates)
  966. dummy->pcm_hw.rates = m->rates;
  967. if (m->rate_min)
  968. dummy->pcm_hw.rate_min = m->rate_min;
  969. if (m->rate_max)
  970. dummy->pcm_hw.rate_max = m->rate_max;
  971. if (m->channels_min)
  972. dummy->pcm_hw.channels_min = m->channels_min;
  973. if (m->channels_max)
  974. dummy->pcm_hw.channels_max = m->channels_max;
  975. }
  976. err = snd_card_dummy_new_mixer(dummy);
  977. if (err < 0)
  978. goto __nodev;
  979. strcpy(card->driver, "Dummy");
  980. strcpy(card->shortname, "Dummy");
  981. sprintf(card->longname, "Dummy %i", dev + 1);
  982. dummy_proc_init(dummy);
  983. err = snd_card_register(card);
  984. if (err == 0) {
  985. platform_set_drvdata(devptr, card);
  986. return 0;
  987. }
  988. __nodev:
  989. snd_card_free(card);
  990. return err;
  991. }
  992. static int snd_dummy_remove(struct platform_device *devptr)
  993. {
  994. snd_card_free(platform_get_drvdata(devptr));
  995. return 0;
  996. }
  997. #ifdef CONFIG_PM_SLEEP
  998. static int snd_dummy_suspend(struct device *pdev)
  999. {
  1000. struct snd_card *card = dev_get_drvdata(pdev);
  1001. struct snd_dummy *dummy = card->private_data;
  1002. snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
  1003. snd_pcm_suspend_all(dummy->pcm);
  1004. return 0;
  1005. }
  1006. static int snd_dummy_resume(struct device *pdev)
  1007. {
  1008. struct snd_card *card = dev_get_drvdata(pdev);
  1009. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  1010. return 0;
  1011. }
  1012. static SIMPLE_DEV_PM_OPS(snd_dummy_pm, snd_dummy_suspend, snd_dummy_resume);
  1013. #define SND_DUMMY_PM_OPS &snd_dummy_pm
  1014. #else
  1015. #define SND_DUMMY_PM_OPS NULL
  1016. #endif
  1017. #define SND_DUMMY_DRIVER "snd_dummy"
  1018. static struct platform_driver snd_dummy_driver = {
  1019. .probe = snd_dummy_probe,
  1020. .remove = snd_dummy_remove,
  1021. .driver = {
  1022. .name = SND_DUMMY_DRIVER,
  1023. .pm = SND_DUMMY_PM_OPS,
  1024. },
  1025. };
  1026. static void snd_dummy_unregister_all(void)
  1027. {
  1028. int i;
  1029. for (i = 0; i < ARRAY_SIZE(devices); ++i)
  1030. platform_device_unregister(devices[i]);
  1031. platform_driver_unregister(&snd_dummy_driver);
  1032. free_fake_buffer();
  1033. }
  1034. static int __init alsa_card_dummy_init(void)
  1035. {
  1036. int i, cards, err;
  1037. err = platform_driver_register(&snd_dummy_driver);
  1038. if (err < 0)
  1039. return err;
  1040. err = alloc_fake_buffer();
  1041. if (err < 0) {
  1042. platform_driver_unregister(&snd_dummy_driver);
  1043. return err;
  1044. }
  1045. cards = 0;
  1046. for (i = 0; i < SNDRV_CARDS; i++) {
  1047. struct platform_device *device;
  1048. if (! enable[i])
  1049. continue;
  1050. device = platform_device_register_simple(SND_DUMMY_DRIVER,
  1051. i, NULL, 0);
  1052. if (IS_ERR(device))
  1053. continue;
  1054. if (!platform_get_drvdata(device)) {
  1055. platform_device_unregister(device);
  1056. continue;
  1057. }
  1058. devices[i] = device;
  1059. cards++;
  1060. }
  1061. if (!cards) {
  1062. #ifdef MODULE
  1063. printk(KERN_ERR "Dummy soundcard not found or device busy\n");
  1064. #endif
  1065. snd_dummy_unregister_all();
  1066. return -ENODEV;
  1067. }
  1068. return 0;
  1069. }
  1070. static void __exit alsa_card_dummy_exit(void)
  1071. {
  1072. snd_dummy_unregister_all();
  1073. }
  1074. module_init(alsa_card_dummy_init)
  1075. module_exit(alsa_card_dummy_exit)