blob: 1f42e406311835a5c98aae272d9969ec6d5aa61b [file] [log] [blame]
Stas Sergeev9ab4d072008-03-03 10:53:54 +01001/*
2 * PC-Speaker driver for Linux
3 *
4 * Copyright (C) 1993-1997 Michael Beck
5 * Copyright (C) 1997-2001 David Woodhouse
6 * Copyright (C) 2001-2008 Stas Sergeev
7 */
8
9#include <linux/module.h>
10#include <linux/moduleparam.h>
11#include <sound/pcm.h>
Stas Sergeev9ab4d072008-03-03 10:53:54 +010012#include <asm/io.h>
Stas Sergeev9ab4d072008-03-03 10:53:54 +010013#include "pcsp.h"
14
15static int nforce_wa;
16module_param(nforce_wa, bool, 0444);
17MODULE_PARM_DESC(nforce_wa, "Apply NForce chipset workaround "
18 "(expect bad sound)");
19
Stas Sergeev4dfd7952008-05-17 08:44:41 +020020#define DMIX_WANTS_S16 1
21
Stas Sergeev9ab4d072008-03-03 10:53:54 +010022enum hrtimer_restart pcsp_do_timer(struct hrtimer *handle)
23{
Stas Sergeev9ab4d072008-03-03 10:53:54 +010024 unsigned char timer_cnt, val;
Stas Sergeev4dfd7952008-05-17 08:44:41 +020025 int fmt_size, periods_elapsed;
Stas Sergeev9ab4d072008-03-03 10:53:54 +010026 u64 ns;
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);
31
32 if (chip->thalf) {
33 outb(chip->val61, 0x61);
34 chip->thalf = 0;
35 if (!atomic_read(&chip->timer_active))
36 return HRTIMER_NORESTART;
Arjan van de Ven5c73a7d2008-09-01 15:25:20 -070037 hrtimer_forward(&chip->timer, hrtimer_get_expires(&chip->timer),
Stas Sergeev9ab4d072008-03-03 10:53:54 +010038 ktime_set(0, chip->ns_rem));
39 return HRTIMER_RESTART;
40 }
41
Stas Sergeev4b7afb02008-05-20 11:47:29 +020042 spin_lock_irq(&chip->substream_lock);
Stas Sergeev9ab4d072008-03-03 10:53:54 +010043 /* Takashi Iwai says regarding this extra lock:
44
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_
50 this lock yet.
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().
58
59 */
60 if (!chip->playback_substream)
61 goto exit_nr_unlock1;
62 substream = chip->playback_substream;
63 snd_pcm_stream_lock(substream);
64 if (!atomic_read(&chip->timer_active))
65 goto exit_nr_unlock2;
66
67 runtime = substream->runtime;
Stas Sergeev4dfd7952008-05-17 08:44:41 +020068 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))
72 val ^= 0x80;
Stas Sergeev9ab4d072008-03-03 10:53:54 +010073 timer_cnt = val * CUR_DIV() / 256;
74
75 if (timer_cnt && chip->enable) {
76 spin_lock(&i8253_lock);
77 if (!nforce_wa) {
78 outb_p(chip->val61, 0x61);
79 outb_p(timer_cnt, 0x42);
80 outb(chip->val61 ^ 1, 0x61);
81 } else {
82 outb(chip->val61 ^ 2, 0x61);
83 chip->thalf = 1;
84 }
85 spin_unlock(&i8253_lock);
86 }
87
88 period_bytes = snd_pcm_lib_period_bytes(substream);
89 buffer_bytes = snd_pcm_lib_buffer_bytes(substream);
Stas Sergeev4dfd7952008-05-17 08:44:41 +020090 chip->playback_ptr += PCSP_INDEX_INC() * fmt_size;
Stas Sergeev9ab4d072008-03-03 10:53:54 +010091 periods_elapsed = chip->playback_ptr - chip->period_ptr;
92 if (periods_elapsed < 0) {
Stas Sergeev42ece6c2008-05-18 18:30:03 +020093#if PCSP_DEBUG
94 printk(KERN_INFO "PCSP: buffer_bytes mod period_bytes != 0 ? "
Stas Sergeev9ab4d072008-03-03 10:53:54 +010095 "(%zi %zi %zi)\n",
96 chip->playback_ptr, period_bytes, buffer_bytes);
Stas Sergeev42ece6c2008-05-18 18:30:03 +020097#endif
Stas Sergeev9ab4d072008-03-03 10:53:54 +010098 periods_elapsed += buffer_bytes;
99 }
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;
104
105 snd_pcm_stream_unlock(substream);
106
107 if (periods_elapsed) {
108 snd_pcm_period_elapsed(substream);
109 chip->period_ptr += periods_elapsed * period_bytes;
110 chip->period_ptr %= buffer_bytes;
111 }
112
Stas Sergeev4b7afb02008-05-20 11:47:29 +0200113 spin_unlock_irq(&chip->substream_lock);
Stas Sergeev9ab4d072008-03-03 10:53:54 +0100114
115 if (!atomic_read(&chip->timer_active))
116 return HRTIMER_NORESTART;
117
118 chip->ns_rem = PCSP_PERIOD_NS();
119 ns = (chip->thalf ? PCSP_CALC_NS(timer_cnt) : chip->ns_rem);
120 chip->ns_rem -= ns;
Arjan van de Ven5c73a7d2008-09-01 15:25:20 -0700121 hrtimer_forward(&chip->timer, hrtimer_get_expires(&chip->timer),
122 ktime_set(0, ns));
Stas Sergeev9ab4d072008-03-03 10:53:54 +0100123 return HRTIMER_RESTART;
124
125exit_nr_unlock2:
126 snd_pcm_stream_unlock(substream);
127exit_nr_unlock1:
Stas Sergeev4b7afb02008-05-20 11:47:29 +0200128 spin_unlock_irq(&chip->substream_lock);
Stas Sergeev9ab4d072008-03-03 10:53:54 +0100129 return HRTIMER_NORESTART;
130}
131
132static void pcsp_start_playing(struct snd_pcsp *chip)
133{
134#if PCSP_DEBUG
135 printk(KERN_INFO "PCSP: start_playing called\n");
136#endif
137 if (atomic_read(&chip->timer_active)) {
138 printk(KERN_ERR "PCSP: Timer already active\n");
139 return;
140 }
141
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);
147 chip->thalf = 0;
148
Stas Sergeev4b7afb02008-05-20 11:47:29 +0200149 hrtimer_start(&pcsp_chip.timer, ktime_set(0, 0), HRTIMER_MODE_REL);
Stas Sergeev9ab4d072008-03-03 10:53:54 +0100150}
151
152static void pcsp_stop_playing(struct snd_pcsp *chip)
153{
154#if PCSP_DEBUG
155 printk(KERN_INFO "PCSP: stop_playing called\n");
156#endif
157 if (!atomic_read(&chip->timer_active))
158 return;
159
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);
166}
167
168static int snd_pcsp_playback_close(struct snd_pcm_substream *substream)
169{
170 struct snd_pcsp *chip = snd_pcm_substream_chip(substream);
171#if PCSP_DEBUG
172 printk(KERN_INFO "PCSP: close called\n");
173#endif
174 if (atomic_read(&chip->timer_active)) {
175 printk(KERN_ERR "PCSP: timer still active\n");
176 pcsp_stop_playing(chip);
177 }
178 spin_lock_irq(&chip->substream_lock);
179 chip->playback_substream = NULL;
180 spin_unlock_irq(&chip->substream_lock);
181 return 0;
182}
183
184static int snd_pcsp_playback_hw_params(struct snd_pcm_substream *substream,
185 struct snd_pcm_hw_params *hw_params)
186{
187 int err;
188 err = snd_pcm_lib_malloc_pages(substream,
189 params_buffer_bytes(hw_params));
190 if (err < 0)
191 return err;
192 return 0;
193}
194
195static int snd_pcsp_playback_hw_free(struct snd_pcm_substream *substream)
196{
197#if PCSP_DEBUG
198 printk(KERN_INFO "PCSP: hw_free called\n");
199#endif
200 return snd_pcm_lib_free_pages(substream);
201}
202
203static int snd_pcsp_playback_prepare(struct snd_pcm_substream *substream)
204{
205 struct snd_pcsp *chip = snd_pcm_substream_chip(substream);
206#if PCSP_DEBUG
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);
214#endif
215 chip->playback_ptr = 0;
216 chip->period_ptr = 0;
217 return 0;
218}
219
220static int snd_pcsp_trigger(struct snd_pcm_substream *substream, int cmd)
221{
222 struct snd_pcsp *chip = snd_pcm_substream_chip(substream);
223#if PCSP_DEBUG
224 printk(KERN_INFO "PCSP: trigger called\n");
225#endif
226 switch (cmd) {
227 case SNDRV_PCM_TRIGGER_START:
228 case SNDRV_PCM_TRIGGER_RESUME:
229 pcsp_start_playing(chip);
230 break;
231 case SNDRV_PCM_TRIGGER_STOP:
232 case SNDRV_PCM_TRIGGER_SUSPEND:
233 pcsp_stop_playing(chip);
234 break;
235 default:
236 return -EINVAL;
237 }
238 return 0;
239}
240
241static snd_pcm_uframes_t snd_pcsp_playback_pointer(struct snd_pcm_substream
242 *substream)
243{
244 struct snd_pcsp *chip = snd_pcm_substream_chip(substream);
245 return bytes_to_frames(substream->runtime, chip->playback_ptr);
246}
247
248static 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),
Stas Sergeev4dfd7952008-05-17 08:44:41 +0200252 .formats = (SNDRV_PCM_FMTBIT_U8
253#if DMIX_WANTS_S16
254 | SNDRV_PCM_FMTBIT_S16_LE
255#endif
256 ),
Stas Sergeev9ab4d072008-03-03 10:53:54 +0100257 .rates = SNDRV_PCM_RATE_KNOT,
258 .rate_min = PCSP_DEFAULT_SRATE,
259 .rate_max = PCSP_DEFAULT_SRATE,
260 .channels_min = 1,
261 .channels_max = 1,
262 .buffer_bytes_max = PCSP_BUFFER_SIZE,
263 .period_bytes_min = 64,
264 .period_bytes_max = PCSP_MAX_PERIOD_SIZE,
265 .periods_min = 2,
266 .periods_max = PCSP_MAX_PERIODS,
267 .fifo_size = 0,
268};
269
270static int snd_pcsp_playback_open(struct snd_pcm_substream *substream)
271{
272 struct snd_pcsp *chip = snd_pcm_substream_chip(substream);
273 struct snd_pcm_runtime *runtime = substream->runtime;
274#if PCSP_DEBUG
275 printk(KERN_INFO "PCSP: open called\n");
276#endif
277 if (atomic_read(&chip->timer_active)) {
278 printk(KERN_ERR "PCSP: still active!!\n");
279 return -EBUSY;
280 }
281 runtime->hw = snd_pcsp_playback;
Stas Sergeev1bc1f302008-03-12 13:12:15 +0100282 spin_lock_irq(&chip->substream_lock);
Stas Sergeev9ab4d072008-03-03 10:53:54 +0100283 chip->playback_substream = substream;
Stas Sergeev1bc1f302008-03-12 13:12:15 +0100284 spin_unlock_irq(&chip->substream_lock);
Stas Sergeev9ab4d072008-03-03 10:53:54 +0100285 return 0;
286}
287
288static 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,
297};
298
299int __devinit snd_pcsp_new_pcm(struct snd_pcsp *chip)
300{
301 int err;
302
303 err = snd_pcm_new(chip->card, "pcspeaker", 0, 1, 0, &chip->pcm);
304 if (err < 0)
305 return err;
306
307 snd_pcm_set_ops(chip->pcm, SNDRV_PCM_STREAM_PLAYBACK,
308 &snd_pcsp_playback_ops);
309
310 chip->pcm->private_data = chip;
311 chip->pcm->info_flags = SNDRV_PCM_INFO_HALF_DUPLEX;
312 strcpy(chip->pcm->name, "pcsp");
313
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,
318 PCSP_BUFFER_SIZE);
319
320 return 0;
321}