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 <linux/interrupt.h>
12 #include <sound/pcm.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
24 * Call snd_pcm_period_elapsed in a tasklet
25 * This avoids spinlock messes and long-running irq contexts
27 static void pcsp_call_pcm_elapsed(unsigned long priv
)
29 if (atomic_read(&pcsp_chip
.timer_active
)) {
30 struct snd_pcm_substream
*substream
;
31 substream
= pcsp_chip
.playback_substream
;
33 snd_pcm_period_elapsed(substream
);
37 static DECLARE_TASKLET(pcsp_pcm_tasklet
, pcsp_call_pcm_elapsed
, 0);
39 /* write the port and returns the next expire time in ns;
40 * called at the trigger-start and in hrtimer callback
42 static unsigned long pcsp_timer_update(struct hrtimer
*handle
)
44 unsigned char timer_cnt
, val
;
46 struct snd_pcm_substream
*substream
;
47 struct snd_pcm_runtime
*runtime
;
48 struct snd_pcsp
*chip
= container_of(handle
, struct snd_pcsp
, timer
);
52 outb(chip
->val61
, 0x61);
54 if (!atomic_read(&chip
->timer_active
))
59 if (!atomic_read(&chip
->timer_active
))
61 substream
= chip
->playback_substream
;
65 runtime
= substream
->runtime
;
66 /* assume it is mono! */
67 val
= runtime
->dma_area
[chip
->playback_ptr
+ chip
->fmt_size
- 1];
70 timer_cnt
= val
* CUR_DIV() / 256;
72 if (timer_cnt
&& chip
->enable
) {
73 spin_lock_irqsave(&i8253_lock
, flags
);
75 outb_p(chip
->val61
, 0x61);
76 outb_p(timer_cnt
, 0x42);
77 outb(chip
->val61
^ 1, 0x61);
79 outb(chip
->val61
^ 2, 0x61);
82 spin_unlock_irqrestore(&i8253_lock
, flags
);
85 chip
->ns_rem
= PCSP_PERIOD_NS();
86 ns
= (chip
->thalf
? PCSP_CALC_NS(timer_cnt
) : chip
->ns_rem
);
91 enum hrtimer_restart
pcsp_do_timer(struct hrtimer
*handle
)
93 struct snd_pcsp
*chip
= container_of(handle
, struct snd_pcsp
, timer
);
94 struct snd_pcm_substream
*substream
;
95 int periods_elapsed
, pointer_update
;
96 size_t period_bytes
, buffer_bytes
;
100 pointer_update
= !chip
->thalf
;
101 ns
= pcsp_timer_update(handle
);
103 return HRTIMER_NORESTART
;
105 /* update the playback position */
106 substream
= chip
->playback_substream
;
108 return HRTIMER_NORESTART
;
110 period_bytes
= snd_pcm_lib_period_bytes(substream
);
111 buffer_bytes
= snd_pcm_lib_buffer_bytes(substream
);
113 spin_lock_irqsave(&chip
->substream_lock
, flags
);
114 chip
->playback_ptr
+= PCSP_INDEX_INC() * chip
->fmt_size
;
115 periods_elapsed
= chip
->playback_ptr
- chip
->period_ptr
;
116 if (periods_elapsed
< 0) {
118 printk(KERN_INFO
"PCSP: buffer_bytes mod period_bytes != 0 ? "
120 chip
->playback_ptr
, period_bytes
, buffer_bytes
);
122 periods_elapsed
+= buffer_bytes
;
124 periods_elapsed
/= period_bytes
;
125 /* wrap the pointer _before_ calling snd_pcm_period_elapsed(),
126 * or ALSA will BUG on us. */
127 chip
->playback_ptr
%= buffer_bytes
;
129 if (periods_elapsed
) {
130 chip
->period_ptr
+= periods_elapsed
* period_bytes
;
131 chip
->period_ptr
%= buffer_bytes
;
133 spin_unlock_irqrestore(&chip
->substream_lock
, flags
);
136 tasklet_schedule(&pcsp_pcm_tasklet
);
138 hrtimer_forward(handle
, hrtimer_get_expires(handle
), ns_to_ktime(ns
));
140 return HRTIMER_RESTART
;
143 static int pcsp_start_playing(struct snd_pcsp
*chip
)
148 printk(KERN_INFO
"PCSP: start_playing called\n");
150 if (atomic_read(&chip
->timer_active
)) {
151 printk(KERN_ERR
"PCSP: Timer already active\n");
155 spin_lock(&i8253_lock
);
156 chip
->val61
= inb(0x61) | 0x03;
157 outb_p(0x92, 0x43); /* binary, mode 1, LSB only, ch 2 */
158 spin_unlock(&i8253_lock
);
159 atomic_set(&chip
->timer_active
, 1);
162 ns
= pcsp_timer_update(&pcsp_chip
.timer
);
166 hrtimer_start(&pcsp_chip
.timer
, ktime_set(0, ns
), HRTIMER_MODE_REL
);
170 static void pcsp_stop_playing(struct snd_pcsp
*chip
)
173 printk(KERN_INFO
"PCSP: stop_playing called\n");
175 if (!atomic_read(&chip
->timer_active
))
178 atomic_set(&chip
->timer_active
, 0);
179 spin_lock(&i8253_lock
);
180 /* restore the timer */
181 outb_p(0xb6, 0x43); /* binary, mode 3, LSB/MSB, ch 2 */
182 outb(chip
->val61
& 0xFC, 0x61);
183 spin_unlock(&i8253_lock
);
187 * Force to stop and sync the stream
189 void pcsp_sync_stop(struct snd_pcsp
*chip
)
192 pcsp_stop_playing(chip
);
194 hrtimer_cancel(&chip
->timer
);
195 tasklet_kill(&pcsp_pcm_tasklet
);
198 static int snd_pcsp_playback_close(struct snd_pcm_substream
*substream
)
200 struct snd_pcsp
*chip
= snd_pcm_substream_chip(substream
);
202 printk(KERN_INFO
"PCSP: close called\n");
204 pcsp_sync_stop(chip
);
205 chip
->playback_substream
= NULL
;
209 static int snd_pcsp_playback_hw_params(struct snd_pcm_substream
*substream
,
210 struct snd_pcm_hw_params
*hw_params
)
212 struct snd_pcsp
*chip
= snd_pcm_substream_chip(substream
);
214 pcsp_sync_stop(chip
);
215 err
= snd_pcm_lib_malloc_pages(substream
,
216 params_buffer_bytes(hw_params
));
222 static int snd_pcsp_playback_hw_free(struct snd_pcm_substream
*substream
)
224 struct snd_pcsp
*chip
= snd_pcm_substream_chip(substream
);
226 printk(KERN_INFO
"PCSP: hw_free called\n");
228 pcsp_sync_stop(chip
);
229 return snd_pcm_lib_free_pages(substream
);
232 static int snd_pcsp_playback_prepare(struct snd_pcm_substream
*substream
)
234 struct snd_pcsp
*chip
= snd_pcm_substream_chip(substream
);
236 printk(KERN_INFO
"PCSP: prepare called, "
237 "size=%zi psize=%zi f=%zi f1=%i\n",
238 snd_pcm_lib_buffer_bytes(substream
),
239 snd_pcm_lib_period_bytes(substream
),
240 snd_pcm_lib_buffer_bytes(substream
) /
241 snd_pcm_lib_period_bytes(substream
),
242 substream
->runtime
->periods
);
244 pcsp_sync_stop(chip
);
245 chip
->playback_ptr
= 0;
246 chip
->period_ptr
= 0;
248 snd_pcm_format_physical_width(substream
->runtime
->format
) >> 3;
249 chip
->is_signed
= snd_pcm_format_signed(substream
->runtime
->format
);
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 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 struct snd_pcm_ops snd_pcsp_playback_ops
= {
323 .open
= snd_pcsp_playback_open
,
324 .close
= snd_pcsp_playback_close
,
325 .ioctl
= snd_pcm_lib_ioctl
,
326 .hw_params
= snd_pcsp_playback_hw_params
,
327 .hw_free
= snd_pcsp_playback_hw_free
,
328 .prepare
= snd_pcsp_playback_prepare
,
329 .trigger
= snd_pcsp_trigger
,
330 .pointer
= snd_pcsp_playback_pointer
,
333 int __devinit
snd_pcsp_new_pcm(struct snd_pcsp
*chip
)
337 err
= snd_pcm_new(chip
->card
, "pcspeaker", 0, 1, 0, &chip
->pcm
);
341 snd_pcm_set_ops(chip
->pcm
, SNDRV_PCM_STREAM_PLAYBACK
,
342 &snd_pcsp_playback_ops
);
344 chip
->pcm
->private_data
= chip
;
345 chip
->pcm
->info_flags
= SNDRV_PCM_INFO_HALF_DUPLEX
;
346 strcpy(chip
->pcm
->name
, "pcsp");
348 snd_pcm_lib_preallocate_pages_for_all(chip
->pcm
,
349 SNDRV_DMA_TYPE_CONTINUOUS
,
350 snd_dma_continuous_data
351 (GFP_KERNEL
), PCSP_BUFFER_SIZE
,