1 // SPDX-License-Identifier: GPL-2.0
3 * PC-Speaker driver for Linux
5 * Copyright (C) 1993-1997 Michael Beck
6 * Copyright (C) 1997-2001 David Woodhouse
7 * Copyright (C) 2001-2008 Stas Sergeev
10 #include <linux/module.h>
11 #include <linux/gfp.h>
12 #include <linux/moduleparam.h>
13 #include <linux/interrupt.h>
15 #include <sound/pcm.h>
18 static bool nforce_wa;
19 module_param(nforce_wa, bool, 0444);
20 MODULE_PARM_DESC(nforce_wa, "Apply NForce chipset workaround "
21 "(expect bad sound)");
23 #define DMIX_WANTS_S16 1
26 * Call snd_pcm_period_elapsed in a tasklet
27 * This avoids spinlock messes and long-running irq contexts
29 static void pcsp_call_pcm_elapsed(unsigned long priv)
31 if (atomic_read(&pcsp_chip.timer_active)) {
32 struct snd_pcm_substream *substream;
33 substream = pcsp_chip.playback_substream;
35 snd_pcm_period_elapsed(substream);
39 static DECLARE_TASKLET(pcsp_pcm_tasklet, pcsp_call_pcm_elapsed, 0);
41 /* write the port and returns the next expire time in ns;
42 * called at the trigger-start and in hrtimer callback
44 static u64 pcsp_timer_update(struct snd_pcsp *chip)
46 unsigned char timer_cnt, val;
48 struct snd_pcm_substream *substream;
49 struct snd_pcm_runtime *runtime;
53 outb(chip->val61, 0x61);
58 substream = chip->playback_substream;
62 runtime = substream->runtime;
63 /* assume it is mono! */
64 val = runtime->dma_area[chip->playback_ptr + chip->fmt_size - 1];
67 timer_cnt = val * CUR_DIV() / 256;
69 if (timer_cnt && chip->enable) {
70 raw_spin_lock_irqsave(&i8253_lock, flags);
72 outb_p(chip->val61, 0x61);
73 outb_p(timer_cnt, 0x42);
74 outb(chip->val61 ^ 1, 0x61);
76 outb(chip->val61 ^ 2, 0x61);
79 raw_spin_unlock_irqrestore(&i8253_lock, flags);
82 chip->ns_rem = PCSP_PERIOD_NS();
83 ns = (chip->thalf ? PCSP_CALC_NS(timer_cnt) : chip->ns_rem);
88 static void pcsp_pointer_update(struct snd_pcsp *chip)
90 struct snd_pcm_substream *substream;
91 size_t period_bytes, buffer_bytes;
95 /* update the playback position */
96 substream = chip->playback_substream;
100 period_bytes = snd_pcm_lib_period_bytes(substream);
101 buffer_bytes = snd_pcm_lib_buffer_bytes(substream);
103 spin_lock_irqsave(&chip->substream_lock, flags);
104 chip->playback_ptr += PCSP_INDEX_INC() * chip->fmt_size;
105 periods_elapsed = chip->playback_ptr - chip->period_ptr;
106 if (periods_elapsed < 0) {
108 printk(KERN_INFO "PCSP: buffer_bytes mod period_bytes != 0 ? "
110 chip->playback_ptr, period_bytes, buffer_bytes);
112 periods_elapsed += buffer_bytes;
114 periods_elapsed /= period_bytes;
115 /* wrap the pointer _before_ calling snd_pcm_period_elapsed(),
116 * or ALSA will BUG on us. */
117 chip->playback_ptr %= buffer_bytes;
119 if (periods_elapsed) {
120 chip->period_ptr += periods_elapsed * period_bytes;
121 chip->period_ptr %= buffer_bytes;
123 spin_unlock_irqrestore(&chip->substream_lock, flags);
126 tasklet_schedule(&pcsp_pcm_tasklet);
129 enum hrtimer_restart pcsp_do_timer(struct hrtimer *handle)
131 struct snd_pcsp *chip = container_of(handle, struct snd_pcsp, timer);
135 if (!atomic_read(&chip->timer_active) || !chip->playback_substream)
136 return HRTIMER_NORESTART;
138 pointer_update = !chip->thalf;
139 ns = pcsp_timer_update(chip);
141 printk(KERN_WARNING "PCSP: unexpected stop\n");
142 return HRTIMER_NORESTART;
146 pcsp_pointer_update(chip);
148 hrtimer_forward(handle, hrtimer_get_expires(handle), ns_to_ktime(ns));
150 return HRTIMER_RESTART;
153 static int pcsp_start_playing(struct snd_pcsp *chip)
156 printk(KERN_INFO "PCSP: start_playing called\n");
158 if (atomic_read(&chip->timer_active)) {
159 printk(KERN_ERR "PCSP: Timer already active\n");
163 raw_spin_lock(&i8253_lock);
164 chip->val61 = inb(0x61) | 0x03;
165 outb_p(0x92, 0x43); /* binary, mode 1, LSB only, ch 2 */
166 raw_spin_unlock(&i8253_lock);
167 atomic_set(&chip->timer_active, 1);
170 hrtimer_start(&pcsp_chip.timer, 0, HRTIMER_MODE_REL);
174 static void pcsp_stop_playing(struct snd_pcsp *chip)
177 printk(KERN_INFO "PCSP: stop_playing called\n");
179 if (!atomic_read(&chip->timer_active))
182 atomic_set(&chip->timer_active, 0);
183 raw_spin_lock(&i8253_lock);
184 /* restore the timer */
185 outb_p(0xb6, 0x43); /* binary, mode 3, LSB/MSB, ch 2 */
186 outb(chip->val61 & 0xFC, 0x61);
187 raw_spin_unlock(&i8253_lock);
191 * Force to stop and sync the stream
193 void pcsp_sync_stop(struct snd_pcsp *chip)
196 pcsp_stop_playing(chip);
198 hrtimer_cancel(&chip->timer);
199 tasklet_kill(&pcsp_pcm_tasklet);
202 static int snd_pcsp_playback_close(struct snd_pcm_substream *substream)
204 struct snd_pcsp *chip = snd_pcm_substream_chip(substream);
206 printk(KERN_INFO "PCSP: close called\n");
208 pcsp_sync_stop(chip);
209 chip->playback_substream = NULL;
213 static int snd_pcsp_playback_hw_params(struct snd_pcm_substream *substream,
214 struct snd_pcm_hw_params *hw_params)
216 struct snd_pcsp *chip = snd_pcm_substream_chip(substream);
217 pcsp_sync_stop(chip);
221 static int snd_pcsp_playback_hw_free(struct snd_pcm_substream *substream)
223 struct snd_pcsp *chip = snd_pcm_substream_chip(substream);
225 printk(KERN_INFO "PCSP: hw_free called\n");
227 pcsp_sync_stop(chip);
231 static int snd_pcsp_playback_prepare(struct snd_pcm_substream *substream)
233 struct snd_pcsp *chip = snd_pcm_substream_chip(substream);
234 pcsp_sync_stop(chip);
235 chip->playback_ptr = 0;
236 chip->period_ptr = 0;
238 snd_pcm_format_physical_width(substream->runtime->format) >> 3;
239 chip->is_signed = snd_pcm_format_signed(substream->runtime->format);
241 printk(KERN_INFO "PCSP: prepare called, "
242 "size=%zi psize=%zi f=%zi f1=%i fsize=%i\n",
243 snd_pcm_lib_buffer_bytes(substream),
244 snd_pcm_lib_period_bytes(substream),
245 snd_pcm_lib_buffer_bytes(substream) /
246 snd_pcm_lib_period_bytes(substream),
247 substream->runtime->periods,
253 static int snd_pcsp_trigger(struct snd_pcm_substream *substream, int cmd)
255 struct snd_pcsp *chip = snd_pcm_substream_chip(substream);
257 printk(KERN_INFO "PCSP: trigger called\n");
260 case SNDRV_PCM_TRIGGER_START:
261 case SNDRV_PCM_TRIGGER_RESUME:
262 return pcsp_start_playing(chip);
263 case SNDRV_PCM_TRIGGER_STOP:
264 case SNDRV_PCM_TRIGGER_SUSPEND:
265 pcsp_stop_playing(chip);
273 static snd_pcm_uframes_t snd_pcsp_playback_pointer(struct snd_pcm_substream
276 struct snd_pcsp *chip = snd_pcm_substream_chip(substream);
278 spin_lock(&chip->substream_lock);
279 pos = chip->playback_ptr;
280 spin_unlock(&chip->substream_lock);
281 return bytes_to_frames(substream->runtime, pos);
284 static const struct snd_pcm_hardware snd_pcsp_playback = {
285 .info = (SNDRV_PCM_INFO_INTERLEAVED |
286 SNDRV_PCM_INFO_HALF_DUPLEX |
287 SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID),
288 .formats = (SNDRV_PCM_FMTBIT_U8
290 | SNDRV_PCM_FMTBIT_S16_LE
293 .rates = SNDRV_PCM_RATE_KNOT,
294 .rate_min = PCSP_DEFAULT_SRATE,
295 .rate_max = PCSP_DEFAULT_SRATE,
298 .buffer_bytes_max = PCSP_BUFFER_SIZE,
299 .period_bytes_min = 64,
300 .period_bytes_max = PCSP_MAX_PERIOD_SIZE,
302 .periods_max = PCSP_MAX_PERIODS,
306 static int snd_pcsp_playback_open(struct snd_pcm_substream *substream)
308 struct snd_pcsp *chip = snd_pcm_substream_chip(substream);
309 struct snd_pcm_runtime *runtime = substream->runtime;
311 printk(KERN_INFO "PCSP: open called\n");
313 if (atomic_read(&chip->timer_active)) {
314 printk(KERN_ERR "PCSP: still active!!\n");
317 runtime->hw = snd_pcsp_playback;
318 chip->playback_substream = substream;
322 static const struct snd_pcm_ops snd_pcsp_playback_ops = {
323 .open = snd_pcsp_playback_open,
324 .close = snd_pcsp_playback_close,
325 .hw_params = snd_pcsp_playback_hw_params,
326 .hw_free = snd_pcsp_playback_hw_free,
327 .prepare = snd_pcsp_playback_prepare,
328 .trigger = snd_pcsp_trigger,
329 .pointer = snd_pcsp_playback_pointer,
332 int snd_pcsp_new_pcm(struct snd_pcsp *chip)
336 err = snd_pcm_new(chip->card, "pcspeaker", 0, 1, 0, &chip->pcm);
340 snd_pcm_set_ops(chip->pcm, SNDRV_PCM_STREAM_PLAYBACK,
341 &snd_pcsp_playback_ops);
343 chip->pcm->private_data = chip;
344 chip->pcm->info_flags = SNDRV_PCM_INFO_HALF_DUPLEX;
345 strcpy(chip->pcm->name, "pcsp");
347 snd_pcm_set_managed_buffer_all(chip->pcm,
348 SNDRV_DMA_TYPE_CONTINUOUS,