2 * PC-Speaker driver for Linux
4 * Copyright (C) 1993-1997 Michael Beck
5 * Copyright (C) 1997-2001 David Woodhouse
6 * Copyright (C) 2001-2008 Stas Sergeev
9 #include <linux/module.h>
10 #include <linux/moduleparam.h>
11 #include <sound/pcm.h>
12 #include <linux/interrupt.h>
17 module_param(nforce_wa, bool, 0444);
18 MODULE_PARM_DESC(nforce_wa, "Apply NForce chipset workaround "
19 "(expect bad sound)");
21 #define DMIX_WANTS_S16 1
23 static void pcsp_start_timer(unsigned long dummy)
25 hrtimer_start(&pcsp_chip.timer, ktime_set(0, 0), HRTIMER_MODE_REL);
29 * We need the hrtimer_start as a tasklet to avoid
30 * the nasty locking problem. :(
32 * - The timer handler is called with the cpu_base->lock
33 * already held by hrtimer code.
34 * - snd_pcm_period_elapsed() takes the
35 * substream->self_group.lock.
37 * But the snd_pcsp_trigger() is called with the
38 * substream->self_group.lock held, and it calls
39 * hrtimer_start(), which takes the cpu_base->lock.
40 * You see the problem. We have the code pathes
41 * which take two locks in a reverse order. This
42 * can deadlock and the lock validator complains.
43 * The only solution I could find was to move the
44 * hrtimer_start() into a tasklet. -stsp
46 static DECLARE_TASKLET(pcsp_start_timer_tasklet, pcsp_start_timer, 0);
48 enum hrtimer_restart pcsp_do_timer(struct hrtimer *handle)
51 unsigned char timer_cnt, val;
52 int fmt_size, periods_elapsed;
54 size_t period_bytes, buffer_bytes;
55 struct snd_pcm_substream *substream;
56 struct snd_pcm_runtime *runtime;
57 struct snd_pcsp *chip = container_of(handle, struct snd_pcsp, timer);
60 outb(chip->val61, 0x61);
62 if (!atomic_read(&chip->timer_active))
63 return HRTIMER_NORESTART;
64 hrtimer_forward(&chip->timer, chip->timer.expires,
65 ktime_set(0, chip->ns_rem));
66 return HRTIMER_RESTART;
69 /* hrtimer calls us from both hardirq and softirq contexts,
71 spin_lock_irqsave(&chip->substream_lock, flags);
72 /* Takashi Iwai says regarding this extra lock:
74 If the irq handler handles some data on the DMA buffer, it should
75 do snd_pcm_stream_lock().
76 That protects basically against all races among PCM callbacks, yes.
77 However, there are two remaining issues:
78 1. The substream pointer you try to lock isn't protected _before_
80 2. snd_pcm_period_elapsed() itself acquires the lock.
81 The requirement of another lock is because of 1. When you get
82 chip->playback_substream, it's not protected.
83 Keeping this lock while snd_pcm_period_elapsed() assures the substream
84 is still protected (at least, not released). And the other status is
85 handled properly inside snd_pcm_stream_lock() in
86 snd_pcm_period_elapsed().
89 if (!chip->playback_substream)
91 substream = chip->playback_substream;
92 snd_pcm_stream_lock(substream);
93 if (!atomic_read(&chip->timer_active))
96 runtime = substream->runtime;
97 fmt_size = snd_pcm_format_physical_width(runtime->format) >> 3;
98 /* assume it is mono! */
99 val = runtime->dma_area[chip->playback_ptr + fmt_size - 1];
100 if (snd_pcm_format_signed(runtime->format))
102 timer_cnt = val * CUR_DIV() / 256;
104 if (timer_cnt && chip->enable) {
105 spin_lock(&i8253_lock);
107 outb_p(chip->val61, 0x61);
108 outb_p(timer_cnt, 0x42);
109 outb(chip->val61 ^ 1, 0x61);
111 outb(chip->val61 ^ 2, 0x61);
114 spin_unlock(&i8253_lock);
117 period_bytes = snd_pcm_lib_period_bytes(substream);
118 buffer_bytes = snd_pcm_lib_buffer_bytes(substream);
119 chip->playback_ptr += PCSP_INDEX_INC() * fmt_size;
120 periods_elapsed = chip->playback_ptr - chip->period_ptr;
121 if (periods_elapsed < 0) {
122 printk(KERN_WARNING "PCSP: playback_ptr inconsistent "
124 chip->playback_ptr, period_bytes, buffer_bytes);
125 periods_elapsed += buffer_bytes;
127 periods_elapsed /= period_bytes;
128 /* wrap the pointer _before_ calling snd_pcm_period_elapsed(),
129 * or ALSA will BUG on us. */
130 chip->playback_ptr %= buffer_bytes;
132 snd_pcm_stream_unlock(substream);
134 if (periods_elapsed) {
135 snd_pcm_period_elapsed(substream);
136 chip->period_ptr += periods_elapsed * period_bytes;
137 chip->period_ptr %= buffer_bytes;
140 spin_unlock_irqrestore(&chip->substream_lock, flags);
142 if (!atomic_read(&chip->timer_active))
143 return HRTIMER_NORESTART;
145 chip->ns_rem = PCSP_PERIOD_NS();
146 ns = (chip->thalf ? PCSP_CALC_NS(timer_cnt) : chip->ns_rem);
148 hrtimer_forward(&chip->timer, chip->timer.expires, ktime_set(0, ns));
149 return HRTIMER_RESTART;
152 snd_pcm_stream_unlock(substream);
154 spin_unlock_irqrestore(&chip->substream_lock, flags);
155 return HRTIMER_NORESTART;
158 static void pcsp_start_playing(struct snd_pcsp *chip)
161 printk(KERN_INFO "PCSP: start_playing called\n");
163 if (atomic_read(&chip->timer_active)) {
164 printk(KERN_ERR "PCSP: Timer already active\n");
168 spin_lock(&i8253_lock);
169 chip->val61 = inb(0x61) | 0x03;
170 outb_p(0x92, 0x43); /* binary, mode 1, LSB only, ch 2 */
171 spin_unlock(&i8253_lock);
172 atomic_set(&chip->timer_active, 1);
175 tasklet_schedule(&pcsp_start_timer_tasklet);
178 static void pcsp_stop_playing(struct snd_pcsp *chip)
181 printk(KERN_INFO "PCSP: stop_playing called\n");
183 if (!atomic_read(&chip->timer_active))
186 atomic_set(&chip->timer_active, 0);
187 spin_lock(&i8253_lock);
188 /* restore the timer */
189 outb_p(0xb6, 0x43); /* binary, mode 3, LSB/MSB, ch 2 */
190 outb(chip->val61 & 0xFC, 0x61);
191 spin_unlock(&i8253_lock);
194 static int snd_pcsp_playback_close(struct snd_pcm_substream *substream)
196 struct snd_pcsp *chip = snd_pcm_substream_chip(substream);
198 printk(KERN_INFO "PCSP: close called\n");
200 if (atomic_read(&chip->timer_active)) {
201 printk(KERN_ERR "PCSP: timer still active\n");
202 pcsp_stop_playing(chip);
204 spin_lock_irq(&chip->substream_lock);
205 chip->playback_substream = NULL;
206 spin_unlock_irq(&chip->substream_lock);
210 static int snd_pcsp_playback_hw_params(struct snd_pcm_substream *substream,
211 struct snd_pcm_hw_params *hw_params)
214 err = snd_pcm_lib_malloc_pages(substream,
215 params_buffer_bytes(hw_params));
221 static int snd_pcsp_playback_hw_free(struct snd_pcm_substream *substream)
224 printk(KERN_INFO "PCSP: hw_free called\n");
226 return snd_pcm_lib_free_pages(substream);
229 static int snd_pcsp_playback_prepare(struct snd_pcm_substream *substream)
231 struct snd_pcsp *chip = snd_pcm_substream_chip(substream);
233 printk(KERN_INFO "PCSP: prepare called, "
234 "size=%zi psize=%zi f=%zi f1=%i\n",
235 snd_pcm_lib_buffer_bytes(substream),
236 snd_pcm_lib_period_bytes(substream),
237 snd_pcm_lib_buffer_bytes(substream) /
238 snd_pcm_lib_period_bytes(substream),
239 substream->runtime->periods);
241 chip->playback_ptr = 0;
242 chip->period_ptr = 0;
246 static int snd_pcsp_trigger(struct snd_pcm_substream *substream, int cmd)
248 struct snd_pcsp *chip = snd_pcm_substream_chip(substream);
250 printk(KERN_INFO "PCSP: trigger called\n");
253 case SNDRV_PCM_TRIGGER_START:
254 case SNDRV_PCM_TRIGGER_RESUME:
255 pcsp_start_playing(chip);
257 case SNDRV_PCM_TRIGGER_STOP:
258 case SNDRV_PCM_TRIGGER_SUSPEND:
259 pcsp_stop_playing(chip);
267 static snd_pcm_uframes_t snd_pcsp_playback_pointer(struct snd_pcm_substream
270 struct snd_pcsp *chip = snd_pcm_substream_chip(substream);
271 return bytes_to_frames(substream->runtime, chip->playback_ptr);
274 static struct snd_pcm_hardware snd_pcsp_playback = {
275 .info = (SNDRV_PCM_INFO_INTERLEAVED |
276 SNDRV_PCM_INFO_HALF_DUPLEX |
277 SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID),
278 .formats = (SNDRV_PCM_FMTBIT_U8
280 | SNDRV_PCM_FMTBIT_S16_LE
283 .rates = SNDRV_PCM_RATE_KNOT,
284 .rate_min = PCSP_DEFAULT_SRATE,
285 .rate_max = PCSP_DEFAULT_SRATE,
288 .buffer_bytes_max = PCSP_BUFFER_SIZE,
289 .period_bytes_min = 64,
290 .period_bytes_max = PCSP_MAX_PERIOD_SIZE,
292 .periods_max = PCSP_MAX_PERIODS,
296 static int snd_pcsp_playback_open(struct snd_pcm_substream *substream)
298 struct snd_pcsp *chip = snd_pcm_substream_chip(substream);
299 struct snd_pcm_runtime *runtime = substream->runtime;
301 printk(KERN_INFO "PCSP: open called\n");
303 if (atomic_read(&chip->timer_active)) {
304 printk(KERN_ERR "PCSP: still active!!\n");
307 runtime->hw = snd_pcsp_playback;
308 spin_lock_irq(&chip->substream_lock);
309 chip->playback_substream = substream;
310 spin_unlock_irq(&chip->substream_lock);
314 static struct snd_pcm_ops snd_pcsp_playback_ops = {
315 .open = snd_pcsp_playback_open,
316 .close = snd_pcsp_playback_close,
317 .ioctl = snd_pcm_lib_ioctl,
318 .hw_params = snd_pcsp_playback_hw_params,
319 .hw_free = snd_pcsp_playback_hw_free,
320 .prepare = snd_pcsp_playback_prepare,
321 .trigger = snd_pcsp_trigger,
322 .pointer = snd_pcsp_playback_pointer,
325 int __devinit snd_pcsp_new_pcm(struct snd_pcsp *chip)
329 err = snd_pcm_new(chip->card, "pcspeaker", 0, 1, 0, &chip->pcm);
333 snd_pcm_set_ops(chip->pcm, SNDRV_PCM_STREAM_PLAYBACK,
334 &snd_pcsp_playback_ops);
336 chip->pcm->private_data = chip;
337 chip->pcm->info_flags = SNDRV_PCM_INFO_HALF_DUPLEX;
338 strcpy(chip->pcm->name, "pcsp");
340 snd_pcm_lib_preallocate_pages_for_all(chip->pcm,
341 SNDRV_DMA_TYPE_CONTINUOUS,
342 snd_dma_continuous_data
343 (GFP_KERNEL), PCSP_BUFFER_SIZE,