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>
16 module_param(nforce_wa, bool, 0444);
17 MODULE_PARM_DESC(nforce_wa, "Apply NForce chipset workaround "
18 "(expect bad sound)");
20 #define DMIX_WANTS_S16 1
22 enum hrtimer_restart pcsp_do_timer(struct hrtimer *handle)
24 unsigned char timer_cnt, val;
25 int fmt_size, periods_elapsed;
27 size_t period_bytes, buffer_bytes;
28 struct snd_pcm_substream *substream;
29 struct snd_pcm_runtime *runtime;
30 struct snd_pcsp *chip = container_of(handle, struct snd_pcsp, timer);
33 outb(chip->val61, 0x61);
35 if (!atomic_read(&chip->timer_active))
36 return HRTIMER_NORESTART;
37 hrtimer_forward(&chip->timer, hrtimer_get_expires(&chip->timer),
38 ktime_set(0, chip->ns_rem));
39 return HRTIMER_RESTART;
42 spin_lock_irq(&chip->substream_lock);
43 /* Takashi Iwai says regarding this extra lock:
45 If the irq handler handles some data on the DMA buffer, it should
46 do snd_pcm_stream_lock().
47 That protects basically against all races among PCM callbacks, yes.
48 However, there are two remaining issues:
49 1. The substream pointer you try to lock isn't protected _before_
51 2. snd_pcm_period_elapsed() itself acquires the lock.
52 The requirement of another lock is because of 1. When you get
53 chip->playback_substream, it's not protected.
54 Keeping this lock while snd_pcm_period_elapsed() assures the substream
55 is still protected (at least, not released). And the other status is
56 handled properly inside snd_pcm_stream_lock() in
57 snd_pcm_period_elapsed().
60 if (!chip->playback_substream)
62 substream = chip->playback_substream;
63 snd_pcm_stream_lock(substream);
64 if (!atomic_read(&chip->timer_active))
67 runtime = substream->runtime;
68 fmt_size = snd_pcm_format_physical_width(runtime->format) >> 3;
69 /* assume it is mono! */
70 val = runtime->dma_area[chip->playback_ptr + fmt_size - 1];
71 if (snd_pcm_format_signed(runtime->format))
73 timer_cnt = val * CUR_DIV() / 256;
75 if (timer_cnt && chip->enable) {
76 spin_lock(&i8253_lock);
78 outb_p(chip->val61, 0x61);
79 outb_p(timer_cnt, 0x42);
80 outb(chip->val61 ^ 1, 0x61);
82 outb(chip->val61 ^ 2, 0x61);
85 spin_unlock(&i8253_lock);
88 period_bytes = snd_pcm_lib_period_bytes(substream);
89 buffer_bytes = snd_pcm_lib_buffer_bytes(substream);
90 chip->playback_ptr += PCSP_INDEX_INC() * fmt_size;
91 periods_elapsed = chip->playback_ptr - chip->period_ptr;
92 if (periods_elapsed < 0) {
94 printk(KERN_INFO "PCSP: buffer_bytes mod period_bytes != 0 ? "
96 chip->playback_ptr, period_bytes, buffer_bytes);
98 periods_elapsed += buffer_bytes;
100 periods_elapsed /= period_bytes;
101 /* wrap the pointer _before_ calling snd_pcm_period_elapsed(),
102 * or ALSA will BUG on us. */
103 chip->playback_ptr %= buffer_bytes;
105 snd_pcm_stream_unlock(substream);
107 if (periods_elapsed) {
108 snd_pcm_period_elapsed(substream);
109 chip->period_ptr += periods_elapsed * period_bytes;
110 chip->period_ptr %= buffer_bytes;
113 spin_unlock_irq(&chip->substream_lock);
115 if (!atomic_read(&chip->timer_active))
116 return HRTIMER_NORESTART;
118 chip->ns_rem = PCSP_PERIOD_NS();
119 ns = (chip->thalf ? PCSP_CALC_NS(timer_cnt) : chip->ns_rem);
121 hrtimer_forward(&chip->timer, hrtimer_get_expires(&chip->timer),
123 return HRTIMER_RESTART;
126 snd_pcm_stream_unlock(substream);
128 spin_unlock_irq(&chip->substream_lock);
129 return HRTIMER_NORESTART;
132 static void pcsp_start_playing(struct snd_pcsp *chip)
135 printk(KERN_INFO "PCSP: start_playing called\n");
137 if (atomic_read(&chip->timer_active)) {
138 printk(KERN_ERR "PCSP: Timer already active\n");
142 spin_lock(&i8253_lock);
143 chip->val61 = inb(0x61) | 0x03;
144 outb_p(0x92, 0x43); /* binary, mode 1, LSB only, ch 2 */
145 spin_unlock(&i8253_lock);
146 atomic_set(&chip->timer_active, 1);
149 hrtimer_start(&pcsp_chip.timer, ktime_set(0, 0), HRTIMER_MODE_REL);
152 static void pcsp_stop_playing(struct snd_pcsp *chip)
155 printk(KERN_INFO "PCSP: stop_playing called\n");
157 if (!atomic_read(&chip->timer_active))
160 atomic_set(&chip->timer_active, 0);
161 spin_lock(&i8253_lock);
162 /* restore the timer */
163 outb_p(0xb6, 0x43); /* binary, mode 3, LSB/MSB, ch 2 */
164 outb(chip->val61 & 0xFC, 0x61);
165 spin_unlock(&i8253_lock);
168 static int snd_pcsp_playback_close(struct snd_pcm_substream *substream)
170 struct snd_pcsp *chip = snd_pcm_substream_chip(substream);
172 printk(KERN_INFO "PCSP: close called\n");
174 if (atomic_read(&chip->timer_active)) {
175 printk(KERN_ERR "PCSP: timer still active\n");
176 pcsp_stop_playing(chip);
178 spin_lock_irq(&chip->substream_lock);
179 chip->playback_substream = NULL;
180 spin_unlock_irq(&chip->substream_lock);
184 static int snd_pcsp_playback_hw_params(struct snd_pcm_substream *substream,
185 struct snd_pcm_hw_params *hw_params)
188 err = snd_pcm_lib_malloc_pages(substream,
189 params_buffer_bytes(hw_params));
195 static int snd_pcsp_playback_hw_free(struct snd_pcm_substream *substream)
198 printk(KERN_INFO "PCSP: hw_free called\n");
200 return snd_pcm_lib_free_pages(substream);
203 static int snd_pcsp_playback_prepare(struct snd_pcm_substream *substream)
205 struct snd_pcsp *chip = snd_pcm_substream_chip(substream);
207 printk(KERN_INFO "PCSP: prepare called, "
208 "size=%zi psize=%zi f=%zi f1=%i\n",
209 snd_pcm_lib_buffer_bytes(substream),
210 snd_pcm_lib_period_bytes(substream),
211 snd_pcm_lib_buffer_bytes(substream) /
212 snd_pcm_lib_period_bytes(substream),
213 substream->runtime->periods);
215 chip->playback_ptr = 0;
216 chip->period_ptr = 0;
220 static int snd_pcsp_trigger(struct snd_pcm_substream *substream, int cmd)
222 struct snd_pcsp *chip = snd_pcm_substream_chip(substream);
224 printk(KERN_INFO "PCSP: trigger called\n");
227 case SNDRV_PCM_TRIGGER_START:
228 case SNDRV_PCM_TRIGGER_RESUME:
229 pcsp_start_playing(chip);
231 case SNDRV_PCM_TRIGGER_STOP:
232 case SNDRV_PCM_TRIGGER_SUSPEND:
233 pcsp_stop_playing(chip);
241 static snd_pcm_uframes_t snd_pcsp_playback_pointer(struct snd_pcm_substream
244 struct snd_pcsp *chip = snd_pcm_substream_chip(substream);
245 return bytes_to_frames(substream->runtime, chip->playback_ptr);
248 static struct snd_pcm_hardware snd_pcsp_playback = {
249 .info = (SNDRV_PCM_INFO_INTERLEAVED |
250 SNDRV_PCM_INFO_HALF_DUPLEX |
251 SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID),
252 .formats = (SNDRV_PCM_FMTBIT_U8
254 | SNDRV_PCM_FMTBIT_S16_LE
257 .rates = SNDRV_PCM_RATE_KNOT,
258 .rate_min = PCSP_DEFAULT_SRATE,
259 .rate_max = PCSP_DEFAULT_SRATE,
262 .buffer_bytes_max = PCSP_BUFFER_SIZE,
263 .period_bytes_min = 64,
264 .period_bytes_max = PCSP_MAX_PERIOD_SIZE,
266 .periods_max = PCSP_MAX_PERIODS,
270 static int snd_pcsp_playback_open(struct snd_pcm_substream *substream)
272 struct snd_pcsp *chip = snd_pcm_substream_chip(substream);
273 struct snd_pcm_runtime *runtime = substream->runtime;
275 printk(KERN_INFO "PCSP: open called\n");
277 if (atomic_read(&chip->timer_active)) {
278 printk(KERN_ERR "PCSP: still active!!\n");
281 runtime->hw = snd_pcsp_playback;
282 spin_lock_irq(&chip->substream_lock);
283 chip->playback_substream = substream;
284 spin_unlock_irq(&chip->substream_lock);
288 static struct snd_pcm_ops snd_pcsp_playback_ops = {
289 .open = snd_pcsp_playback_open,
290 .close = snd_pcsp_playback_close,
291 .ioctl = snd_pcm_lib_ioctl,
292 .hw_params = snd_pcsp_playback_hw_params,
293 .hw_free = snd_pcsp_playback_hw_free,
294 .prepare = snd_pcsp_playback_prepare,
295 .trigger = snd_pcsp_trigger,
296 .pointer = snd_pcsp_playback_pointer,
299 int __devinit snd_pcsp_new_pcm(struct snd_pcsp *chip)
303 err = snd_pcm_new(chip->card, "pcspeaker", 0, 1, 0, &chip->pcm);
307 snd_pcm_set_ops(chip->pcm, SNDRV_PCM_STREAM_PLAYBACK,
308 &snd_pcsp_playback_ops);
310 chip->pcm->private_data = chip;
311 chip->pcm->info_flags = SNDRV_PCM_INFO_HALF_DUPLEX;
312 strcpy(chip->pcm->name, "pcsp");
314 snd_pcm_lib_preallocate_pages_for_all(chip->pcm,
315 SNDRV_DMA_TYPE_CONTINUOUS,
316 snd_dma_continuous_data
317 (GFP_KERNEL), PCSP_BUFFER_SIZE,