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/gfp.h>
11 #include <linux/moduleparam.h>
12 #include <linux/interrupt.h>
14 #include <sound/pcm.h>
17 static bool nforce_wa;
18 module_param(nforce_wa, bool, 0444);
19 MODULE_PARM_DESC(nforce_wa, "Apply NForce chipset workaround "
20 "(expect bad sound)");
22 #define DMIX_WANTS_S16 1
25 * Call snd_pcm_period_elapsed in a tasklet
26 * This avoids spinlock messes and long-running irq contexts
28 static void pcsp_call_pcm_elapsed(unsigned long priv)
30 if (atomic_read(&pcsp_chip.timer_active)) {
31 struct snd_pcm_substream *substream;
32 substream = pcsp_chip.playback_substream;
34 snd_pcm_period_elapsed(substream);
38 static DECLARE_TASKLET(pcsp_pcm_tasklet, pcsp_call_pcm_elapsed, 0);
40 /* write the port and returns the next expire time in ns;
41 * called at the trigger-start and in hrtimer callback
43 static u64 pcsp_timer_update(struct snd_pcsp *chip)
45 unsigned char timer_cnt, val;
47 struct snd_pcm_substream *substream;
48 struct snd_pcm_runtime *runtime;
52 outb(chip->val61, 0x61);
57 substream = chip->playback_substream;
61 runtime = substream->runtime;
62 /* assume it is mono! */
63 val = runtime->dma_area[chip->playback_ptr + chip->fmt_size - 1];
66 timer_cnt = val * CUR_DIV() / 256;
68 if (timer_cnt && chip->enable) {
69 raw_spin_lock_irqsave(&i8253_lock, flags);
71 outb_p(chip->val61, 0x61);
72 outb_p(timer_cnt, 0x42);
73 outb(chip->val61 ^ 1, 0x61);
75 outb(chip->val61 ^ 2, 0x61);
78 raw_spin_unlock_irqrestore(&i8253_lock, flags);
81 chip->ns_rem = PCSP_PERIOD_NS();
82 ns = (chip->thalf ? PCSP_CALC_NS(timer_cnt) : chip->ns_rem);
87 static void pcsp_pointer_update(struct snd_pcsp *chip)
89 struct snd_pcm_substream *substream;
90 size_t period_bytes, buffer_bytes;
94 /* update the playback position */
95 substream = chip->playback_substream;
99 period_bytes = snd_pcm_lib_period_bytes(substream);
100 buffer_bytes = snd_pcm_lib_buffer_bytes(substream);
102 spin_lock_irqsave(&chip->substream_lock, flags);
103 chip->playback_ptr += PCSP_INDEX_INC() * chip->fmt_size;
104 periods_elapsed = chip->playback_ptr - chip->period_ptr;
105 if (periods_elapsed < 0) {
107 printk(KERN_INFO "PCSP: buffer_bytes mod period_bytes != 0 ? "
109 chip->playback_ptr, period_bytes, buffer_bytes);
111 periods_elapsed += buffer_bytes;
113 periods_elapsed /= period_bytes;
114 /* wrap the pointer _before_ calling snd_pcm_period_elapsed(),
115 * or ALSA will BUG on us. */
116 chip->playback_ptr %= buffer_bytes;
118 if (periods_elapsed) {
119 chip->period_ptr += periods_elapsed * period_bytes;
120 chip->period_ptr %= buffer_bytes;
122 spin_unlock_irqrestore(&chip->substream_lock, flags);
125 tasklet_schedule(&pcsp_pcm_tasklet);
128 enum hrtimer_restart pcsp_do_timer(struct hrtimer *handle)
130 struct snd_pcsp *chip = container_of(handle, struct snd_pcsp, timer);
134 if (!atomic_read(&chip->timer_active) || !chip->playback_substream)
135 return HRTIMER_NORESTART;
137 pointer_update = !chip->thalf;
138 ns = pcsp_timer_update(chip);
140 printk(KERN_WARNING "PCSP: unexpected stop\n");
141 return HRTIMER_NORESTART;
145 pcsp_pointer_update(chip);
147 hrtimer_forward(handle, hrtimer_get_expires(handle), ns_to_ktime(ns));
149 return HRTIMER_RESTART;
152 static int pcsp_start_playing(struct snd_pcsp *chip)
155 printk(KERN_INFO "PCSP: start_playing called\n");
157 if (atomic_read(&chip->timer_active)) {
158 printk(KERN_ERR "PCSP: Timer already active\n");
162 raw_spin_lock(&i8253_lock);
163 chip->val61 = inb(0x61) | 0x03;
164 outb_p(0x92, 0x43); /* binary, mode 1, LSB only, ch 2 */
165 raw_spin_unlock(&i8253_lock);
166 atomic_set(&chip->timer_active, 1);
169 hrtimer_start(&pcsp_chip.timer, ktime_set(0, 0), HRTIMER_MODE_REL);
173 static void pcsp_stop_playing(struct snd_pcsp *chip)
176 printk(KERN_INFO "PCSP: stop_playing called\n");
178 if (!atomic_read(&chip->timer_active))
181 atomic_set(&chip->timer_active, 0);
182 raw_spin_lock(&i8253_lock);
183 /* restore the timer */
184 outb_p(0xb6, 0x43); /* binary, mode 3, LSB/MSB, ch 2 */
185 outb(chip->val61 & 0xFC, 0x61);
186 raw_spin_unlock(&i8253_lock);
190 * Force to stop and sync the stream
192 void pcsp_sync_stop(struct snd_pcsp *chip)
195 pcsp_stop_playing(chip);
197 hrtimer_cancel(&chip->timer);
198 tasklet_kill(&pcsp_pcm_tasklet);
201 static int snd_pcsp_playback_close(struct snd_pcm_substream *substream)
203 struct snd_pcsp *chip = snd_pcm_substream_chip(substream);
205 printk(KERN_INFO "PCSP: close called\n");
207 pcsp_sync_stop(chip);
208 chip->playback_substream = NULL;
212 static int snd_pcsp_playback_hw_params(struct snd_pcm_substream *substream,
213 struct snd_pcm_hw_params *hw_params)
215 struct snd_pcsp *chip = snd_pcm_substream_chip(substream);
217 pcsp_sync_stop(chip);
218 err = snd_pcm_lib_malloc_pages(substream,
219 params_buffer_bytes(hw_params));
225 static int snd_pcsp_playback_hw_free(struct snd_pcm_substream *substream)
227 struct snd_pcsp *chip = snd_pcm_substream_chip(substream);
229 printk(KERN_INFO "PCSP: hw_free called\n");
231 pcsp_sync_stop(chip);
232 return snd_pcm_lib_free_pages(substream);
235 static int snd_pcsp_playback_prepare(struct snd_pcm_substream *substream)
237 struct snd_pcsp *chip = snd_pcm_substream_chip(substream);
238 pcsp_sync_stop(chip);
239 chip->playback_ptr = 0;
240 chip->period_ptr = 0;
242 snd_pcm_format_physical_width(substream->runtime->format) >> 3;
243 chip->is_signed = snd_pcm_format_signed(substream->runtime->format);
245 printk(KERN_INFO "PCSP: prepare called, "
246 "size=%zi psize=%zi f=%zi f1=%i fsize=%i\n",
247 snd_pcm_lib_buffer_bytes(substream),
248 snd_pcm_lib_period_bytes(substream),
249 snd_pcm_lib_buffer_bytes(substream) /
250 snd_pcm_lib_period_bytes(substream),
251 substream->runtime->periods,
257 static int snd_pcsp_trigger(struct snd_pcm_substream *substream, int cmd)
259 struct snd_pcsp *chip = snd_pcm_substream_chip(substream);
261 printk(KERN_INFO "PCSP: trigger called\n");
264 case SNDRV_PCM_TRIGGER_START:
265 case SNDRV_PCM_TRIGGER_RESUME:
266 return pcsp_start_playing(chip);
267 case SNDRV_PCM_TRIGGER_STOP:
268 case SNDRV_PCM_TRIGGER_SUSPEND:
269 pcsp_stop_playing(chip);
277 static snd_pcm_uframes_t snd_pcsp_playback_pointer(struct snd_pcm_substream
280 struct snd_pcsp *chip = snd_pcm_substream_chip(substream);
282 spin_lock(&chip->substream_lock);
283 pos = chip->playback_ptr;
284 spin_unlock(&chip->substream_lock);
285 return bytes_to_frames(substream->runtime, pos);
288 static struct snd_pcm_hardware snd_pcsp_playback = {
289 .info = (SNDRV_PCM_INFO_INTERLEAVED |
290 SNDRV_PCM_INFO_HALF_DUPLEX |
291 SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID),
292 .formats = (SNDRV_PCM_FMTBIT_U8
294 | SNDRV_PCM_FMTBIT_S16_LE
297 .rates = SNDRV_PCM_RATE_KNOT,
298 .rate_min = PCSP_DEFAULT_SRATE,
299 .rate_max = PCSP_DEFAULT_SRATE,
302 .buffer_bytes_max = PCSP_BUFFER_SIZE,
303 .period_bytes_min = 64,
304 .period_bytes_max = PCSP_MAX_PERIOD_SIZE,
306 .periods_max = PCSP_MAX_PERIODS,
310 static int snd_pcsp_playback_open(struct snd_pcm_substream *substream)
312 struct snd_pcsp *chip = snd_pcm_substream_chip(substream);
313 struct snd_pcm_runtime *runtime = substream->runtime;
315 printk(KERN_INFO "PCSP: open called\n");
317 if (atomic_read(&chip->timer_active)) {
318 printk(KERN_ERR "PCSP: still active!!\n");
321 runtime->hw = snd_pcsp_playback;
322 chip->playback_substream = substream;
326 static struct snd_pcm_ops snd_pcsp_playback_ops = {
327 .open = snd_pcsp_playback_open,
328 .close = snd_pcsp_playback_close,
329 .ioctl = snd_pcm_lib_ioctl,
330 .hw_params = snd_pcsp_playback_hw_params,
331 .hw_free = snd_pcsp_playback_hw_free,
332 .prepare = snd_pcsp_playback_prepare,
333 .trigger = snd_pcsp_trigger,
334 .pointer = snd_pcsp_playback_pointer,
337 int snd_pcsp_new_pcm(struct snd_pcsp *chip)
341 err = snd_pcm_new(chip->card, "pcspeaker", 0, 1, 0, &chip->pcm);
345 snd_pcm_set_ops(chip->pcm, SNDRV_PCM_STREAM_PLAYBACK,
346 &snd_pcsp_playback_ops);
348 chip->pcm->private_data = chip;
349 chip->pcm->info_flags = SNDRV_PCM_INFO_HALF_DUPLEX;
350 strcpy(chip->pcm->name, "pcsp");
352 snd_pcm_lib_preallocate_pages_for_all(chip->pcm,
353 SNDRV_DMA_TYPE_CONTINUOUS,
354 snd_dma_continuous_data
355 (GFP_KERNEL), PCSP_BUFFER_SIZE,