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. struct dummy_model {
  103. const char *name;
  104. int (*playback_constraints)(struct snd_pcm_runtime *runtime);
  105. int (*capture_constraints)(struct snd_pcm_runtime *runtime);
  106. u64 formats;
  107. size_t buffer_bytes_max;
  108. size_t period_bytes_min;
  109. size_t period_bytes_max;
  110. unsigned int periods_min;
  111. unsigned int periods_max;
  112. unsigned int rates;
  113. unsigned int rate_min;
  114. unsigned int rate_max;
  115. unsigned int channels_min;
  116. unsigned int channels_max;
  117. };
  118. struct snd_dummy {
  119. struct snd_card *card;
  120. struct dummy_model *model;
  121. struct snd_pcm *pcm;
  122. struct snd_pcm_hardware pcm_hw;
  123. spinlock_t mixer_lock;
  124. int mixer_volume[MIXER_ADDR_LAST+1][2];
  125. int capture_source[MIXER_ADDR_LAST+1][2];
  126. int iobox;
  127. struct snd_kcontrol *cd_volume_ctl;
  128. struct snd_kcontrol *cd_switch_ctl;
  129. const struct dummy_timer_ops *timer_ops;
  130. };
  131. /*
  132. * card models
  133. */
  134. static int emu10k1_playback_constraints(struct snd_pcm_runtime *runtime)
  135. {
  136. int err;
  137. err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
  138. if (err < 0)
  139. return err;
  140. err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 256, UINT_MAX);
  141. if (err < 0)
  142. return err;
  143. return 0;
  144. }
  145. struct dummy_model model_emu10k1 = {
  146. .name = "emu10k1",
  147. .playback_constraints = emu10k1_playback_constraints,
  148. .buffer_bytes_max = 128 * 1024,
  149. };
  150. struct dummy_model model_rme9652 = {
  151. .name = "rme9652",
  152. .buffer_bytes_max = 26 * 64 * 1024,
  153. .formats = SNDRV_PCM_FMTBIT_S32_LE,
  154. .channels_min = 26,
  155. .channels_max = 26,
  156. .periods_min = 2,
  157. .periods_max = 2,
  158. };
  159. struct dummy_model model_ice1712 = {
  160. .name = "ice1712",
  161. .buffer_bytes_max = 256 * 1024,
  162. .formats = SNDRV_PCM_FMTBIT_S32_LE,
  163. .channels_min = 10,
  164. .channels_max = 10,
  165. .periods_min = 1,
  166. .periods_max = 1024,
  167. };
  168. struct dummy_model model_uda1341 = {
  169. .name = "uda1341",
  170. .buffer_bytes_max = 16380,
  171. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  172. .channels_min = 2,
  173. .channels_max = 2,
  174. .periods_min = 2,
  175. .periods_max = 255,
  176. };
  177. struct dummy_model model_ac97 = {
  178. .name = "ac97",
  179. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  180. .channels_min = 2,
  181. .channels_max = 2,
  182. .rates = SNDRV_PCM_RATE_48000,
  183. .rate_min = 48000,
  184. .rate_max = 48000,
  185. };
  186. struct dummy_model model_ca0106 = {
  187. .name = "ca0106",
  188. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  189. .buffer_bytes_max = ((65536-64)*8),
  190. .period_bytes_max = (65536-64),
  191. .periods_min = 2,
  192. .periods_max = 8,
  193. .channels_min = 2,
  194. .channels_max = 2,
  195. .rates = SNDRV_PCM_RATE_48000|SNDRV_PCM_RATE_96000|SNDRV_PCM_RATE_192000,
  196. .rate_min = 48000,
  197. .rate_max = 192000,
  198. };
  199. struct dummy_model *dummy_models[] = {
  200. &model_emu10k1,
  201. &model_rme9652,
  202. &model_ice1712,
  203. &model_uda1341,
  204. &model_ac97,
  205. &model_ca0106,
  206. NULL
  207. };
  208. /*
  209. * system timer interface
  210. */
  211. struct dummy_systimer_pcm {
  212. spinlock_t lock;
  213. struct timer_list timer;
  214. unsigned long base_time;
  215. unsigned int frac_pos; /* fractional sample position (based HZ) */
  216. unsigned int frac_period_rest;
  217. unsigned int frac_buffer_size; /* buffer_size * HZ */
  218. unsigned int frac_period_size; /* period_size * HZ */
  219. unsigned int rate;
  220. int elapsed;
  221. struct snd_pcm_substream *substream;
  222. };
  223. static void dummy_systimer_rearm(struct dummy_systimer_pcm *dpcm)
  224. {
  225. dpcm->timer.expires = jiffies +
  226. (dpcm->frac_period_rest + dpcm->rate - 1) / dpcm->rate;
  227. add_timer(&dpcm->timer);
  228. }
  229. static void dummy_systimer_update(struct dummy_systimer_pcm *dpcm)
  230. {
  231. unsigned long delta;
  232. delta = jiffies - dpcm->base_time;
  233. if (!delta)
  234. return;
  235. dpcm->base_time += delta;
  236. delta *= dpcm->rate;
  237. dpcm->frac_pos += delta;
  238. while (dpcm->frac_pos >= dpcm->frac_buffer_size)
  239. dpcm->frac_pos -= dpcm->frac_buffer_size;
  240. while (dpcm->frac_period_rest <= delta) {
  241. dpcm->elapsed++;
  242. dpcm->frac_period_rest += dpcm->frac_period_size;
  243. }
  244. dpcm->frac_period_rest -= delta;
  245. }
  246. static int dummy_systimer_start(struct snd_pcm_substream *substream)
  247. {
  248. struct dummy_systimer_pcm *dpcm = substream->runtime->private_data;
  249. spin_lock(&dpcm->lock);
  250. dpcm->base_time = jiffies;
  251. dummy_systimer_rearm(dpcm);
  252. spin_unlock(&dpcm->lock);
  253. return 0;
  254. }
  255. static int dummy_systimer_stop(struct snd_pcm_substream *substream)
  256. {
  257. struct dummy_systimer_pcm *dpcm = substream->runtime->private_data;
  258. spin_lock(&dpcm->lock);
  259. del_timer(&dpcm->timer);
  260. spin_unlock(&dpcm->lock);
  261. return 0;
  262. }
  263. static int dummy_systimer_prepare(struct snd_pcm_substream *substream)
  264. {
  265. struct snd_pcm_runtime *runtime = substream->runtime;
  266. struct dummy_systimer_pcm *dpcm = runtime->private_data;
  267. dpcm->frac_pos = 0;
  268. dpcm->rate = runtime->rate;
  269. dpcm->frac_buffer_size = runtime->buffer_size * HZ;
  270. dpcm->frac_period_size = runtime->period_size * HZ;
  271. dpcm->frac_period_rest = dpcm->frac_period_size;
  272. dpcm->elapsed = 0;
  273. return 0;
  274. }
  275. static void dummy_systimer_callback(unsigned long data)
  276. {
  277. struct dummy_systimer_pcm *dpcm = (struct dummy_systimer_pcm *)data;
  278. unsigned long flags;
  279. int elapsed = 0;
  280. spin_lock_irqsave(&dpcm->lock, flags);
  281. dummy_systimer_update(dpcm);
  282. dummy_systimer_rearm(dpcm);
  283. elapsed = dpcm->elapsed;
  284. dpcm->elapsed = 0;
  285. spin_unlock_irqrestore(&dpcm->lock, flags);
  286. if (elapsed)
  287. snd_pcm_period_elapsed(dpcm->substream);
  288. }
  289. static snd_pcm_uframes_t
  290. dummy_systimer_pointer(struct snd_pcm_substream *substream)
  291. {
  292. struct dummy_systimer_pcm *dpcm = substream->runtime->private_data;
  293. snd_pcm_uframes_t pos;
  294. spin_lock(&dpcm->lock);
  295. dummy_systimer_update(dpcm);
  296. pos = dpcm->frac_pos / HZ;
  297. spin_unlock(&dpcm->lock);
  298. return pos;
  299. }
  300. static int dummy_systimer_create(struct snd_pcm_substream *substream)
  301. {
  302. struct dummy_systimer_pcm *dpcm;
  303. dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
  304. if (!dpcm)
  305. return -ENOMEM;
  306. substream->runtime->private_data = dpcm;
  307. init_timer(&dpcm->timer);
  308. dpcm->timer.data = (unsigned long) dpcm;
  309. dpcm->timer.function = dummy_systimer_callback;
  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 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. ktime_t base_time;
  332. ktime_t period_time;
  333. atomic_t running;
  334. struct hrtimer timer;
  335. struct tasklet_struct tasklet;
  336. struct snd_pcm_substream *substream;
  337. };
  338. static void dummy_hrtimer_pcm_elapsed(unsigned long priv)
  339. {
  340. struct dummy_hrtimer_pcm *dpcm = (struct dummy_hrtimer_pcm *)priv;
  341. if (atomic_read(&dpcm->running))
  342. snd_pcm_period_elapsed(dpcm->substream);
  343. }
  344. static enum hrtimer_restart dummy_hrtimer_callback(struct hrtimer *timer)
  345. {
  346. struct dummy_hrtimer_pcm *dpcm;
  347. dpcm = container_of(timer, struct dummy_hrtimer_pcm, timer);
  348. if (!atomic_read(&dpcm->running))
  349. return HRTIMER_NORESTART;
  350. tasklet_schedule(&dpcm->tasklet);
  351. hrtimer_forward_now(timer, dpcm->period_time);
  352. return HRTIMER_RESTART;
  353. }
  354. static int dummy_hrtimer_start(struct snd_pcm_substream *substream)
  355. {
  356. struct dummy_hrtimer_pcm *dpcm = substream->runtime->private_data;
  357. dpcm->base_time = hrtimer_cb_get_time(&dpcm->timer);
  358. hrtimer_start(&dpcm->timer, dpcm->period_time, HRTIMER_MODE_REL);
  359. atomic_set(&dpcm->running, 1);
  360. return 0;
  361. }
  362. static int dummy_hrtimer_stop(struct snd_pcm_substream *substream)
  363. {
  364. struct dummy_hrtimer_pcm *dpcm = substream->runtime->private_data;
  365. atomic_set(&dpcm->running, 0);
  366. hrtimer_cancel(&dpcm->timer);
  367. return 0;
  368. }
  369. static inline void dummy_hrtimer_sync(struct dummy_hrtimer_pcm *dpcm)
  370. {
  371. tasklet_kill(&dpcm->tasklet);
  372. }
  373. static snd_pcm_uframes_t
  374. dummy_hrtimer_pointer(struct snd_pcm_substream *substream)
  375. {
  376. struct snd_pcm_runtime *runtime = substream->runtime;
  377. struct dummy_hrtimer_pcm *dpcm = runtime->private_data;
  378. u64 delta;
  379. u32 pos;
  380. delta = ktime_us_delta(hrtimer_cb_get_time(&dpcm->timer),
  381. dpcm->base_time);
  382. delta = div_u64(delta * runtime->rate + 999999, 1000000);
  383. div_u64_rem(delta, runtime->buffer_size, &pos);
  384. return pos;
  385. }
  386. static int dummy_hrtimer_prepare(struct snd_pcm_substream *substream)
  387. {
  388. struct snd_pcm_runtime *runtime = substream->runtime;
  389. struct dummy_hrtimer_pcm *dpcm = runtime->private_data;
  390. unsigned int period, rate;
  391. long sec;
  392. unsigned long nsecs;
  393. dummy_hrtimer_sync(dpcm);
  394. period = runtime->period_size;
  395. rate = runtime->rate;
  396. sec = period / rate;
  397. period %= rate;
  398. nsecs = div_u64((u64)period * 1000000000UL + rate - 1, rate);
  399. dpcm->period_time = ktime_set(sec, nsecs);
  400. return 0;
  401. }
  402. static int dummy_hrtimer_create(struct snd_pcm_substream *substream)
  403. {
  404. struct dummy_hrtimer_pcm *dpcm;
  405. dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
  406. if (!dpcm)
  407. return -ENOMEM;
  408. substream->runtime->private_data = dpcm;
  409. hrtimer_init(&dpcm->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  410. dpcm->timer.function = dummy_hrtimer_callback;
  411. dpcm->substream = substream;
  412. atomic_set(&dpcm->running, 0);
  413. tasklet_init(&dpcm->tasklet, dummy_hrtimer_pcm_elapsed,
  414. (unsigned long)dpcm);
  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 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. struct snd_dummy *dummy = snd_pcm_substream_chip(substream);
  438. switch (cmd) {
  439. case SNDRV_PCM_TRIGGER_START:
  440. case SNDRV_PCM_TRIGGER_RESUME:
  441. return dummy->timer_ops->start(substream);
  442. case SNDRV_PCM_TRIGGER_STOP:
  443. case SNDRV_PCM_TRIGGER_SUSPEND:
  444. return dummy->timer_ops->stop(substream);
  445. }
  446. return -EINVAL;
  447. }
  448. static int dummy_pcm_prepare(struct snd_pcm_substream *substream)
  449. {
  450. struct snd_dummy *dummy = snd_pcm_substream_chip(substream);
  451. return dummy->timer_ops->prepare(substream);
  452. }
  453. static snd_pcm_uframes_t dummy_pcm_pointer(struct snd_pcm_substream *substream)
  454. {
  455. struct snd_dummy *dummy = snd_pcm_substream_chip(substream);
  456. return dummy->timer_ops->pointer(substream);
  457. }
  458. static struct snd_pcm_hardware dummy_pcm_hardware = {
  459. .info = (SNDRV_PCM_INFO_MMAP |
  460. SNDRV_PCM_INFO_INTERLEAVED |
  461. SNDRV_PCM_INFO_RESUME |
  462. SNDRV_PCM_INFO_MMAP_VALID),
  463. .formats = USE_FORMATS,
  464. .rates = USE_RATE,
  465. .rate_min = USE_RATE_MIN,
  466. .rate_max = USE_RATE_MAX,
  467. .channels_min = USE_CHANNELS_MIN,
  468. .channels_max = USE_CHANNELS_MAX,
  469. .buffer_bytes_max = MAX_BUFFER_SIZE,
  470. .period_bytes_min = MIN_PERIOD_SIZE,
  471. .period_bytes_max = MAX_PERIOD_SIZE,
  472. .periods_min = USE_PERIODS_MIN,
  473. .periods_max = USE_PERIODS_MAX,
  474. .fifo_size = 0,
  475. };
  476. static int dummy_pcm_hw_params(struct snd_pcm_substream *substream,
  477. struct snd_pcm_hw_params *hw_params)
  478. {
  479. if (fake_buffer) {
  480. /* runtime->dma_bytes has to be set manually to allow mmap */
  481. substream->runtime->dma_bytes = params_buffer_bytes(hw_params);
  482. return 0;
  483. }
  484. return snd_pcm_lib_malloc_pages(substream,
  485. params_buffer_bytes(hw_params));
  486. }
  487. static int dummy_pcm_hw_free(struct snd_pcm_substream *substream)
  488. {
  489. if (fake_buffer)
  490. return 0;
  491. return snd_pcm_lib_free_pages(substream);
  492. }
  493. static int dummy_pcm_open(struct snd_pcm_substream *substream)
  494. {
  495. struct snd_dummy *dummy = snd_pcm_substream_chip(substream);
  496. struct dummy_model *model = dummy->model;
  497. struct snd_pcm_runtime *runtime = substream->runtime;
  498. int err;
  499. dummy->timer_ops = &dummy_systimer_ops;
  500. #ifdef CONFIG_HIGH_RES_TIMERS
  501. if (hrtimer)
  502. dummy->timer_ops = &dummy_hrtimer_ops;
  503. #endif
  504. err = dummy->timer_ops->create(substream);
  505. if (err < 0)
  506. return err;
  507. runtime->hw = dummy->pcm_hw;
  508. if (substream->pcm->device & 1) {
  509. runtime->hw.info &= ~SNDRV_PCM_INFO_INTERLEAVED;
  510. runtime->hw.info |= SNDRV_PCM_INFO_NONINTERLEAVED;
  511. }
  512. if (substream->pcm->device & 2)
  513. runtime->hw.info &= ~(SNDRV_PCM_INFO_MMAP |
  514. SNDRV_PCM_INFO_MMAP_VALID);
  515. if (model == NULL)
  516. return 0;
  517. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  518. if (model->playback_constraints)
  519. err = model->playback_constraints(substream->runtime);
  520. } else {
  521. if (model->capture_constraints)
  522. err = model->capture_constraints(substream->runtime);
  523. }
  524. if (err < 0) {
  525. dummy->timer_ops->free(substream);
  526. return err;
  527. }
  528. return 0;
  529. }
  530. static int dummy_pcm_close(struct snd_pcm_substream *substream)
  531. {
  532. struct snd_dummy *dummy = snd_pcm_substream_chip(substream);
  533. dummy->timer_ops->free(substream);
  534. return 0;
  535. }
  536. /*
  537. * dummy buffer handling
  538. */
  539. static void *dummy_page[2];
  540. static void free_fake_buffer(void)
  541. {
  542. if (fake_buffer) {
  543. int i;
  544. for (i = 0; i < 2; i++)
  545. if (dummy_page[i]) {
  546. free_page((unsigned long)dummy_page[i]);
  547. dummy_page[i] = NULL;
  548. }
  549. }
  550. }
  551. static int alloc_fake_buffer(void)
  552. {
  553. int i;
  554. if (!fake_buffer)
  555. return 0;
  556. for (i = 0; i < 2; i++) {
  557. dummy_page[i] = (void *)get_zeroed_page(GFP_KERNEL);
  558. if (!dummy_page[i]) {
  559. free_fake_buffer();
  560. return -ENOMEM;
  561. }
  562. }
  563. return 0;
  564. }
  565. static int dummy_pcm_copy(struct snd_pcm_substream *substream,
  566. int channel, snd_pcm_uframes_t pos,
  567. void __user *dst, snd_pcm_uframes_t count)
  568. {
  569. return 0; /* do nothing */
  570. }
  571. static int dummy_pcm_silence(struct snd_pcm_substream *substream,
  572. int channel, snd_pcm_uframes_t pos,
  573. snd_pcm_uframes_t count)
  574. {
  575. return 0; /* do nothing */
  576. }
  577. static struct page *dummy_pcm_page(struct snd_pcm_substream *substream,
  578. unsigned long offset)
  579. {
  580. return virt_to_page(dummy_page[substream->stream]); /* the same page */
  581. }
  582. static struct snd_pcm_ops dummy_pcm_ops = {
  583. .open = dummy_pcm_open,
  584. .close = dummy_pcm_close,
  585. .ioctl = snd_pcm_lib_ioctl,
  586. .hw_params = dummy_pcm_hw_params,
  587. .hw_free = dummy_pcm_hw_free,
  588. .prepare = dummy_pcm_prepare,
  589. .trigger = dummy_pcm_trigger,
  590. .pointer = dummy_pcm_pointer,
  591. };
  592. static struct snd_pcm_ops dummy_pcm_ops_no_buf = {
  593. .open = dummy_pcm_open,
  594. .close = dummy_pcm_close,
  595. .ioctl = snd_pcm_lib_ioctl,
  596. .hw_params = dummy_pcm_hw_params,
  597. .hw_free = dummy_pcm_hw_free,
  598. .prepare = dummy_pcm_prepare,
  599. .trigger = dummy_pcm_trigger,
  600. .pointer = dummy_pcm_pointer,
  601. .copy = dummy_pcm_copy,
  602. .silence = dummy_pcm_silence,
  603. .page = dummy_pcm_page,
  604. };
  605. static int snd_card_dummy_pcm(struct snd_dummy *dummy, int device,
  606. int substreams)
  607. {
  608. struct snd_pcm *pcm;
  609. struct snd_pcm_ops *ops;
  610. int err;
  611. err = snd_pcm_new(dummy->card, "Dummy PCM", device,
  612. substreams, substreams, &pcm);
  613. if (err < 0)
  614. return err;
  615. dummy->pcm = pcm;
  616. if (fake_buffer)
  617. ops = &dummy_pcm_ops_no_buf;
  618. else
  619. ops = &dummy_pcm_ops;
  620. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, ops);
  621. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, ops);
  622. pcm->private_data = dummy;
  623. pcm->info_flags = 0;
  624. strcpy(pcm->name, "Dummy PCM");
  625. if (!fake_buffer) {
  626. snd_pcm_lib_preallocate_pages_for_all(pcm,
  627. SNDRV_DMA_TYPE_CONTINUOUS,
  628. snd_dma_continuous_data(GFP_KERNEL),
  629. 0, 64*1024);
  630. }
  631. return 0;
  632. }
  633. /*
  634. * mixer interface
  635. */
  636. #define DUMMY_VOLUME(xname, xindex, addr) \
  637. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
  638. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
  639. .name = xname, .index = xindex, \
  640. .info = snd_dummy_volume_info, \
  641. .get = snd_dummy_volume_get, .put = snd_dummy_volume_put, \
  642. .private_value = addr, \
  643. .tlv = { .p = db_scale_dummy } }
  644. static int snd_dummy_volume_info(struct snd_kcontrol *kcontrol,
  645. struct snd_ctl_elem_info *uinfo)
  646. {
  647. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  648. uinfo->count = 2;
  649. uinfo->value.integer.min = -50;
  650. uinfo->value.integer.max = 100;
  651. return 0;
  652. }
  653. static int snd_dummy_volume_get(struct snd_kcontrol *kcontrol,
  654. struct snd_ctl_elem_value *ucontrol)
  655. {
  656. struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
  657. int addr = kcontrol->private_value;
  658. spin_lock_irq(&dummy->mixer_lock);
  659. ucontrol->value.integer.value[0] = dummy->mixer_volume[addr][0];
  660. ucontrol->value.integer.value[1] = dummy->mixer_volume[addr][1];
  661. spin_unlock_irq(&dummy->mixer_lock);
  662. return 0;
  663. }
  664. static int snd_dummy_volume_put(struct snd_kcontrol *kcontrol,
  665. struct snd_ctl_elem_value *ucontrol)
  666. {
  667. struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
  668. int change, addr = kcontrol->private_value;
  669. int left, right;
  670. left = ucontrol->value.integer.value[0];
  671. if (left < -50)
  672. left = -50;
  673. if (left > 100)
  674. left = 100;
  675. right = ucontrol->value.integer.value[1];
  676. if (right < -50)
  677. right = -50;
  678. if (right > 100)
  679. right = 100;
  680. spin_lock_irq(&dummy->mixer_lock);
  681. change = dummy->mixer_volume[addr][0] != left ||
  682. dummy->mixer_volume[addr][1] != right;
  683. dummy->mixer_volume[addr][0] = left;
  684. dummy->mixer_volume[addr][1] = right;
  685. spin_unlock_irq(&dummy->mixer_lock);
  686. return change;
  687. }
  688. static const DECLARE_TLV_DB_SCALE(db_scale_dummy, -4500, 30, 0);
  689. #define DUMMY_CAPSRC(xname, xindex, addr) \
  690. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
  691. .info = snd_dummy_capsrc_info, \
  692. .get = snd_dummy_capsrc_get, .put = snd_dummy_capsrc_put, \
  693. .private_value = addr }
  694. #define snd_dummy_capsrc_info snd_ctl_boolean_stereo_info
  695. static int snd_dummy_capsrc_get(struct snd_kcontrol *kcontrol,
  696. struct snd_ctl_elem_value *ucontrol)
  697. {
  698. struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
  699. int addr = kcontrol->private_value;
  700. spin_lock_irq(&dummy->mixer_lock);
  701. ucontrol->value.integer.value[0] = dummy->capture_source[addr][0];
  702. ucontrol->value.integer.value[1] = dummy->capture_source[addr][1];
  703. spin_unlock_irq(&dummy->mixer_lock);
  704. return 0;
  705. }
  706. static int snd_dummy_capsrc_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  707. {
  708. struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
  709. int change, addr = kcontrol->private_value;
  710. int left, right;
  711. left = ucontrol->value.integer.value[0] & 1;
  712. right = ucontrol->value.integer.value[1] & 1;
  713. spin_lock_irq(&dummy->mixer_lock);
  714. change = dummy->capture_source[addr][0] != left &&
  715. dummy->capture_source[addr][1] != right;
  716. dummy->capture_source[addr][0] = left;
  717. dummy->capture_source[addr][1] = right;
  718. spin_unlock_irq(&dummy->mixer_lock);
  719. return change;
  720. }
  721. static int snd_dummy_iobox_info(struct snd_kcontrol *kcontrol,
  722. struct snd_ctl_elem_info *info)
  723. {
  724. const char *const names[] = { "None", "CD Player" };
  725. return snd_ctl_enum_info(info, 1, 2, names);
  726. }
  727. static int snd_dummy_iobox_get(struct snd_kcontrol *kcontrol,
  728. struct snd_ctl_elem_value *value)
  729. {
  730. struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
  731. value->value.enumerated.item[0] = dummy->iobox;
  732. return 0;
  733. }
  734. static int snd_dummy_iobox_put(struct snd_kcontrol *kcontrol,
  735. struct snd_ctl_elem_value *value)
  736. {
  737. struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol);
  738. int changed;
  739. if (value->value.enumerated.item[0] > 1)
  740. return -EINVAL;
  741. changed = value->value.enumerated.item[0] != dummy->iobox;
  742. if (changed) {
  743. dummy->iobox = value->value.enumerated.item[0];
  744. if (dummy->iobox) {
  745. dummy->cd_volume_ctl->vd[0].access &=
  746. ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  747. dummy->cd_switch_ctl->vd[0].access &=
  748. ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  749. } else {
  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. }
  755. snd_ctl_notify(dummy->card, SNDRV_CTL_EVENT_MASK_INFO,
  756. &dummy->cd_volume_ctl->id);
  757. snd_ctl_notify(dummy->card, SNDRV_CTL_EVENT_MASK_INFO,
  758. &dummy->cd_switch_ctl->id);
  759. }
  760. return changed;
  761. }
  762. static struct snd_kcontrol_new snd_dummy_controls[] = {
  763. DUMMY_VOLUME("Master Volume", 0, MIXER_ADDR_MASTER),
  764. DUMMY_CAPSRC("Master Capture Switch", 0, MIXER_ADDR_MASTER),
  765. DUMMY_VOLUME("Synth Volume", 0, MIXER_ADDR_SYNTH),
  766. DUMMY_CAPSRC("Synth Capture Switch", 0, MIXER_ADDR_SYNTH),
  767. DUMMY_VOLUME("Line Volume", 0, MIXER_ADDR_LINE),
  768. DUMMY_CAPSRC("Line Capture Switch", 0, MIXER_ADDR_LINE),
  769. DUMMY_VOLUME("Mic Volume", 0, MIXER_ADDR_MIC),
  770. DUMMY_CAPSRC("Mic Capture Switch", 0, MIXER_ADDR_MIC),
  771. DUMMY_VOLUME("CD Volume", 0, MIXER_ADDR_CD),
  772. DUMMY_CAPSRC("CD Capture Switch", 0, MIXER_ADDR_CD),
  773. {
  774. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  775. .name = "External I/O Box",
  776. .info = snd_dummy_iobox_info,
  777. .get = snd_dummy_iobox_get,
  778. .put = snd_dummy_iobox_put,
  779. },
  780. };
  781. static int snd_card_dummy_new_mixer(struct snd_dummy *dummy)
  782. {
  783. struct snd_card *card = dummy->card;
  784. struct snd_kcontrol *kcontrol;
  785. unsigned int idx;
  786. int err;
  787. spin_lock_init(&dummy->mixer_lock);
  788. strcpy(card->mixername, "Dummy Mixer");
  789. dummy->iobox = 1;
  790. for (idx = 0; idx < ARRAY_SIZE(snd_dummy_controls); idx++) {
  791. kcontrol = snd_ctl_new1(&snd_dummy_controls[idx], dummy);
  792. err = snd_ctl_add(card, kcontrol);
  793. if (err < 0)
  794. return err;
  795. if (!strcmp(kcontrol->id.name, "CD Volume"))
  796. dummy->cd_volume_ctl = kcontrol;
  797. else if (!strcmp(kcontrol->id.name, "CD Capture Switch"))
  798. dummy->cd_switch_ctl = kcontrol;
  799. }
  800. return 0;
  801. }
  802. #if defined(CONFIG_SND_DEBUG) && defined(CONFIG_PROC_FS)
  803. /*
  804. * proc interface
  805. */
  806. static void print_formats(struct snd_dummy *dummy,
  807. struct snd_info_buffer *buffer)
  808. {
  809. int i;
  810. for (i = 0; i < SNDRV_PCM_FORMAT_LAST; i++) {
  811. if (dummy->pcm_hw.formats & (1ULL << i))
  812. snd_iprintf(buffer, " %s", snd_pcm_format_name(i));
  813. }
  814. }
  815. static void print_rates(struct snd_dummy *dummy,
  816. struct snd_info_buffer *buffer)
  817. {
  818. static int rates[] = {
  819. 5512, 8000, 11025, 16000, 22050, 32000, 44100, 48000,
  820. 64000, 88200, 96000, 176400, 192000,
  821. };
  822. int i;
  823. if (dummy->pcm_hw.rates & SNDRV_PCM_RATE_CONTINUOUS)
  824. snd_iprintf(buffer, " continuous");
  825. if (dummy->pcm_hw.rates & SNDRV_PCM_RATE_KNOT)
  826. snd_iprintf(buffer, " knot");
  827. for (i = 0; i < ARRAY_SIZE(rates); i++)
  828. if (dummy->pcm_hw.rates & (1 << i))
  829. snd_iprintf(buffer, " %d", rates[i]);
  830. }
  831. #define get_dummy_int_ptr(dummy, ofs) \
  832. (unsigned int *)((char *)&((dummy)->pcm_hw) + (ofs))
  833. #define get_dummy_ll_ptr(dummy, ofs) \
  834. (unsigned long long *)((char *)&((dummy)->pcm_hw) + (ofs))
  835. struct dummy_hw_field {
  836. const char *name;
  837. const char *format;
  838. unsigned int offset;
  839. unsigned int size;
  840. };
  841. #define FIELD_ENTRY(item, fmt) { \
  842. .name = #item, \
  843. .format = fmt, \
  844. .offset = offsetof(struct snd_pcm_hardware, item), \
  845. .size = sizeof(dummy_pcm_hardware.item) }
  846. static struct dummy_hw_field fields[] = {
  847. FIELD_ENTRY(formats, "%#llx"),
  848. FIELD_ENTRY(rates, "%#x"),
  849. FIELD_ENTRY(rate_min, "%d"),
  850. FIELD_ENTRY(rate_max, "%d"),
  851. FIELD_ENTRY(channels_min, "%d"),
  852. FIELD_ENTRY(channels_max, "%d"),
  853. FIELD_ENTRY(buffer_bytes_max, "%ld"),
  854. FIELD_ENTRY(period_bytes_min, "%ld"),
  855. FIELD_ENTRY(period_bytes_max, "%ld"),
  856. FIELD_ENTRY(periods_min, "%d"),
  857. FIELD_ENTRY(periods_max, "%d"),
  858. };
  859. static void dummy_proc_read(struct snd_info_entry *entry,
  860. struct snd_info_buffer *buffer)
  861. {
  862. struct snd_dummy *dummy = entry->private_data;
  863. int i;
  864. for (i = 0; i < ARRAY_SIZE(fields); i++) {
  865. snd_iprintf(buffer, "%s ", fields[i].name);
  866. if (fields[i].size == sizeof(int))
  867. snd_iprintf(buffer, fields[i].format,
  868. *get_dummy_int_ptr(dummy, fields[i].offset));
  869. else
  870. snd_iprintf(buffer, fields[i].format,
  871. *get_dummy_ll_ptr(dummy, fields[i].offset));
  872. if (!strcmp(fields[i].name, "formats"))
  873. print_formats(dummy, buffer);
  874. else if (!strcmp(fields[i].name, "rates"))
  875. print_rates(dummy, buffer);
  876. snd_iprintf(buffer, "\n");
  877. }
  878. }
  879. static void dummy_proc_write(struct snd_info_entry *entry,
  880. struct snd_info_buffer *buffer)
  881. {
  882. struct snd_dummy *dummy = entry->private_data;
  883. char line[64];
  884. while (!snd_info_get_line(buffer, line, sizeof(line))) {
  885. char item[20];
  886. const char *ptr;
  887. unsigned long long val;
  888. int i;
  889. ptr = snd_info_get_str(item, line, sizeof(item));
  890. for (i = 0; i < ARRAY_SIZE(fields); i++) {
  891. if (!strcmp(item, fields[i].name))
  892. break;
  893. }
  894. if (i >= ARRAY_SIZE(fields))
  895. continue;
  896. snd_info_get_str(item, ptr, sizeof(item));
  897. if (strict_strtoull(item, 0, &val))
  898. continue;
  899. if (fields[i].size == sizeof(int))
  900. *get_dummy_int_ptr(dummy, fields[i].offset) = val;
  901. else
  902. *get_dummy_ll_ptr(dummy, fields[i].offset) = val;
  903. }
  904. }
  905. static void dummy_proc_init(struct snd_dummy *chip)
  906. {
  907. struct snd_info_entry *entry;
  908. if (!snd_card_proc_new(chip->card, "dummy_pcm", &entry)) {
  909. snd_info_set_text_ops(entry, chip, dummy_proc_read);
  910. entry->c.text.write = dummy_proc_write;
  911. entry->mode |= S_IWUSR;
  912. entry->private_data = chip;
  913. }
  914. }
  915. #else
  916. #define dummy_proc_init(x)
  917. #endif /* CONFIG_SND_DEBUG && CONFIG_PROC_FS */
  918. static int snd_dummy_probe(struct platform_device *devptr)
  919. {
  920. struct snd_card *card;
  921. struct snd_dummy *dummy;
  922. struct dummy_model *m = NULL, **mdl;
  923. int idx, err;
  924. int dev = devptr->id;
  925. err = snd_card_create(index[dev], id[dev], THIS_MODULE,
  926. sizeof(struct snd_dummy), &card);
  927. if (err < 0)
  928. return err;
  929. dummy = card->private_data;
  930. dummy->card = card;
  931. for (mdl = dummy_models; *mdl && model[dev]; mdl++) {
  932. if (strcmp(model[dev], (*mdl)->name) == 0) {
  933. printk(KERN_INFO
  934. "snd-dummy: Using model '%s' for card %i\n",
  935. (*mdl)->name, card->number);
  936. m = dummy->model = *mdl;
  937. break;
  938. }
  939. }
  940. for (idx = 0; idx < MAX_PCM_DEVICES && idx < pcm_devs[dev]; idx++) {
  941. if (pcm_substreams[dev] < 1)
  942. pcm_substreams[dev] = 1;
  943. if (pcm_substreams[dev] > MAX_PCM_SUBSTREAMS)
  944. pcm_substreams[dev] = MAX_PCM_SUBSTREAMS;
  945. err = snd_card_dummy_pcm(dummy, idx, pcm_substreams[dev]);
  946. if (err < 0)
  947. goto __nodev;
  948. }
  949. dummy->pcm_hw = dummy_pcm_hardware;
  950. if (m) {
  951. if (m->formats)
  952. dummy->pcm_hw.formats = m->formats;
  953. if (m->buffer_bytes_max)
  954. dummy->pcm_hw.buffer_bytes_max = m->buffer_bytes_max;
  955. if (m->period_bytes_min)
  956. dummy->pcm_hw.period_bytes_min = m->period_bytes_min;
  957. if (m->period_bytes_max)
  958. dummy->pcm_hw.period_bytes_max = m->period_bytes_max;
  959. if (m->periods_min)
  960. dummy->pcm_hw.periods_min = m->periods_min;
  961. if (m->periods_max)
  962. dummy->pcm_hw.periods_max = m->periods_max;
  963. if (m->rates)
  964. dummy->pcm_hw.rates = m->rates;
  965. if (m->rate_min)
  966. dummy->pcm_hw.rate_min = m->rate_min;
  967. if (m->rate_max)
  968. dummy->pcm_hw.rate_max = m->rate_max;
  969. if (m->channels_min)
  970. dummy->pcm_hw.channels_min = m->channels_min;
  971. if (m->channels_max)
  972. dummy->pcm_hw.channels_max = m->channels_max;
  973. }
  974. err = snd_card_dummy_new_mixer(dummy);
  975. if (err < 0)
  976. goto __nodev;
  977. strcpy(card->driver, "Dummy");
  978. strcpy(card->shortname, "Dummy");
  979. sprintf(card->longname, "Dummy %i", dev + 1);
  980. dummy_proc_init(dummy);
  981. snd_card_set_dev(card, &devptr->dev);
  982. err = snd_card_register(card);
  983. if (err == 0) {
  984. platform_set_drvdata(devptr, card);
  985. return 0;
  986. }
  987. __nodev:
  988. snd_card_free(card);
  989. return err;
  990. }
  991. static int snd_dummy_remove(struct platform_device *devptr)
  992. {
  993. snd_card_free(platform_get_drvdata(devptr));
  994. platform_set_drvdata(devptr, NULL);
  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. .owner = THIS_MODULE,
  1024. .pm = SND_DUMMY_PM_OPS,
  1025. },
  1026. };
  1027. static void snd_dummy_unregister_all(void)
  1028. {
  1029. int i;
  1030. for (i = 0; i < ARRAY_SIZE(devices); ++i)
  1031. platform_device_unregister(devices[i]);
  1032. platform_driver_unregister(&snd_dummy_driver);
  1033. free_fake_buffer();
  1034. }
  1035. static int __init alsa_card_dummy_init(void)
  1036. {
  1037. int i, cards, err;
  1038. err = platform_driver_register(&snd_dummy_driver);
  1039. if (err < 0)
  1040. return err;
  1041. err = alloc_fake_buffer();
  1042. if (err < 0) {
  1043. platform_driver_unregister(&snd_dummy_driver);
  1044. return err;
  1045. }
  1046. cards = 0;
  1047. for (i = 0; i < SNDRV_CARDS; i++) {
  1048. struct platform_device *device;
  1049. if (! enable[i])
  1050. continue;
  1051. device = platform_device_register_simple(SND_DUMMY_DRIVER,
  1052. i, NULL, 0);
  1053. if (IS_ERR(device))
  1054. continue;
  1055. if (!platform_get_drvdata(device)) {
  1056. platform_device_unregister(device);
  1057. continue;
  1058. }
  1059. devices[i] = device;
  1060. cards++;
  1061. }
  1062. if (!cards) {
  1063. #ifdef MODULE
  1064. printk(KERN_ERR "Dummy soundcard not found or device busy\n");
  1065. #endif
  1066. snd_dummy_unregister_all();
  1067. return -ENODEV;
  1068. }
  1069. return 0;
  1070. }
  1071. static void __exit alsa_card_dummy_exit(void)
  1072. {
  1073. snd_dummy_unregister_all();
  1074. }
  1075. module_init(alsa_card_dummy_init)
  1076. module_exit(alsa_card_dummy_exit)