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) {
123 printk(KERN_INFO "PCSP: buffer_bytes mod period_bytes != 0 ? "
125 chip->playback_ptr, period_bytes, buffer_bytes);
127 periods_elapsed += buffer_bytes;
129 periods_elapsed /= period_bytes;
130 /* wrap the pointer _before_ calling snd_pcm_period_elapsed(),
131 * or ALSA will BUG on us. */
132 chip->playback_ptr %= buffer_bytes;
134 snd_pcm_stream_unlock(substream);
136 if (periods_elapsed) {
137 snd_pcm_period_elapsed(substream);
138 chip->period_ptr += periods_elapsed * period_bytes;
139 chip->period_ptr %= buffer_bytes;
142 spin_unlock_irqrestore(&chip->substream_lock, flags);
144 if (!atomic_read(&chip->timer_active))
145 return HRTIMER_NORESTART;
147 chip->ns_rem = PCSP_PERIOD_NS();
148 ns = (chip->thalf ? PCSP_CALC_NS(timer_cnt) : chip->ns_rem);
150 hrtimer_forward(&chip->timer, chip->timer.expires, ktime_set(0, ns));
151 return HRTIMER_RESTART;
154 snd_pcm_stream_unlock(substream);
156 spin_unlock_irqrestore(&chip->substream_lock, flags);
157 return HRTIMER_NORESTART;
160 static void pcsp_start_playing(struct snd_pcsp *chip)
163 printk(KERN_INFO "PCSP: start_playing called\n");
165 if (atomic_read(&chip->timer_active)) {
166 printk(KERN_ERR "PCSP: Timer already active\n");
170 spin_lock(&i8253_lock);
171 chip->val61 = inb(0x61) | 0x03;
172 outb_p(0x92, 0x43); /* binary, mode 1, LSB only, ch 2 */
173 spin_unlock(&i8253_lock);
174 atomic_set(&chip->timer_active, 1);
177 tasklet_schedule(&pcsp_start_timer_tasklet);
180 static void pcsp_stop_playing(struct snd_pcsp *chip)
183 printk(KERN_INFO "PCSP: stop_playing called\n");
185 if (!atomic_read(&chip->timer_active))
188 atomic_set(&chip->timer_active, 0);
189 spin_lock(&i8253_lock);
190 /* restore the timer */
191 outb_p(0xb6, 0x43); /* binary, mode 3, LSB/MSB, ch 2 */
192 outb(chip->val61 & 0xFC, 0x61);
193 spin_unlock(&i8253_lock);
196 static int snd_pcsp_playback_close(struct snd_pcm_substream *substream)
198 struct snd_pcsp *chip = snd_pcm_substream_chip(substream);
200 printk(KERN_INFO "PCSP: close called\n");
202 if (atomic_read(&chip->timer_active)) {
203 printk(KERN_ERR "PCSP: timer still active\n");
204 pcsp_stop_playing(chip);
206 spin_lock_irq(&chip->substream_lock);
207 chip->playback_substream = NULL;
208 spin_unlock_irq(&chip->substream_lock);
212 static int snd_pcsp_playback_hw_params(struct snd_pcm_substream *substream,
213 struct snd_pcm_hw_params *hw_params)
216 err = snd_pcm_lib_malloc_pages(substream,
217 params_buffer_bytes(hw_params));
223 static int snd_pcsp_playback_hw_free(struct snd_pcm_substream *substream)
226 printk(KERN_INFO "PCSP: hw_free called\n");
228 return snd_pcm_lib_free_pages(substream);
231 static int snd_pcsp_playback_prepare(struct snd_pcm_substream *substream)
233 struct snd_pcsp *chip = snd_pcm_substream_chip(substream);
235 printk(KERN_INFO "PCSP: prepare called, "
236 "size=%zi psize=%zi f=%zi f1=%i\n",
237 snd_pcm_lib_buffer_bytes(substream),
238 snd_pcm_lib_period_bytes(substream),
239 snd_pcm_lib_buffer_bytes(substream) /
240 snd_pcm_lib_period_bytes(substream),
241 substream->runtime->periods);
243 chip->playback_ptr = 0;
244 chip->period_ptr = 0;
248 static int snd_pcsp_trigger(struct snd_pcm_substream *substream, int cmd)
250 struct snd_pcsp *chip = snd_pcm_substream_chip(substream);
252 printk(KERN_INFO "PCSP: trigger called\n");
255 case SNDRV_PCM_TRIGGER_START:
256 case SNDRV_PCM_TRIGGER_RESUME:
257 pcsp_start_playing(chip);
259 case SNDRV_PCM_TRIGGER_STOP:
260 case SNDRV_PCM_TRIGGER_SUSPEND:
261 pcsp_stop_playing(chip);
269 static snd_pcm_uframes_t snd_pcsp_playback_pointer(struct snd_pcm_substream
272 struct snd_pcsp *chip = snd_pcm_substream_chip(substream);
273 return bytes_to_frames(substream->runtime, chip->playback_ptr);
276 static struct snd_pcm_hardware snd_pcsp_playback = {
277 .info = (SNDRV_PCM_INFO_INTERLEAVED |
278 SNDRV_PCM_INFO_HALF_DUPLEX |
279 SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID),
280 .formats = (SNDRV_PCM_FMTBIT_U8
282 | SNDRV_PCM_FMTBIT_S16_LE
285 .rates = SNDRV_PCM_RATE_KNOT,
286 .rate_min = PCSP_DEFAULT_SRATE,
287 .rate_max = PCSP_DEFAULT_SRATE,
290 .buffer_bytes_max = PCSP_BUFFER_SIZE,
291 .period_bytes_min = 64,
292 .period_bytes_max = PCSP_MAX_PERIOD_SIZE,
294 .periods_max = PCSP_MAX_PERIODS,
298 static int snd_pcsp_playback_open(struct snd_pcm_substream *substream)
300 struct snd_pcsp *chip = snd_pcm_substream_chip(substream);
301 struct snd_pcm_runtime *runtime = substream->runtime;
303 printk(KERN_INFO "PCSP: open called\n");
305 if (atomic_read(&chip->timer_active)) {
306 printk(KERN_ERR "PCSP: still active!!\n");
309 runtime->hw = snd_pcsp_playback;
310 spin_lock_irq(&chip->substream_lock);
311 chip->playback_substream = substream;
312 spin_unlock_irq(&chip->substream_lock);
316 static struct snd_pcm_ops snd_pcsp_playback_ops = {
317 .open = snd_pcsp_playback_open,
318 .close = snd_pcsp_playback_close,
319 .ioctl = snd_pcm_lib_ioctl,
320 .hw_params = snd_pcsp_playback_hw_params,
321 .hw_free = snd_pcsp_playback_hw_free,
322 .prepare = snd_pcsp_playback_prepare,
323 .trigger = snd_pcsp_trigger,
324 .pointer = snd_pcsp_playback_pointer,
327 int __devinit snd_pcsp_new_pcm(struct snd_pcsp *chip)
331 err = snd_pcm_new(chip->card, "pcspeaker", 0, 1, 0, &chip->pcm);
335 snd_pcm_set_ops(chip->pcm, SNDRV_PCM_STREAM_PLAYBACK,
336 &snd_pcsp_playback_ops);
338 chip->pcm->private_data = chip;
339 chip->pcm->info_flags = SNDRV_PCM_INFO_HALF_DUPLEX;
340 strcpy(chip->pcm->name, "pcsp");
342 snd_pcm_lib_preallocate_pages_for_all(chip->pcm,
343 SNDRV_DMA_TYPE_CONTINUOUS,
344 snd_dma_continuous_data
345 (GFP_KERNEL), PCSP_BUFFER_SIZE,