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 enum hrtimer_restart
pcsp_do_timer(struct hrtimer
*handle
)
41 unsigned char timer_cnt
, val
;
42 int fmt_size
, periods_elapsed
;
44 size_t period_bytes
, buffer_bytes
;
45 struct snd_pcm_substream
*substream
;
46 struct snd_pcm_runtime
*runtime
;
47 struct snd_pcsp
*chip
= container_of(handle
, struct snd_pcsp
, timer
);
51 outb(chip
->val61
, 0x61);
53 if (!atomic_read(&chip
->timer_active
))
55 hrtimer_forward(&chip
->timer
, hrtimer_get_expires(&chip
->timer
),
56 ktime_set(0, chip
->ns_rem
));
57 return HRTIMER_RESTART
;
60 if (!atomic_read(&chip
->timer_active
))
62 substream
= chip
->playback_substream
;
66 runtime
= substream
->runtime
;
67 fmt_size
= snd_pcm_format_physical_width(runtime
->format
) >> 3;
68 /* assume it is mono! */
69 val
= runtime
->dma_area
[chip
->playback_ptr
+ fmt_size
- 1];
70 if (snd_pcm_format_signed(runtime
->format
))
72 timer_cnt
= val
* CUR_DIV() / 256;
74 if (timer_cnt
&& chip
->enable
) {
75 spin_lock(&i8253_lock
);
77 outb_p(chip
->val61
, 0x61);
78 outb_p(timer_cnt
, 0x42);
79 outb(chip
->val61
^ 1, 0x61);
81 outb(chip
->val61
^ 2, 0x61);
84 spin_unlock(&i8253_lock
);
87 period_bytes
= snd_pcm_lib_period_bytes(substream
);
88 buffer_bytes
= snd_pcm_lib_buffer_bytes(substream
);
90 spin_lock_irqsave(&chip
->substream_lock
, flags
);
91 chip
->playback_ptr
+= PCSP_INDEX_INC() * fmt_size
;
92 periods_elapsed
= chip
->playback_ptr
- chip
->period_ptr
;
93 if (periods_elapsed
< 0) {
95 printk(KERN_INFO
"PCSP: buffer_bytes mod period_bytes != 0 ? "
97 chip
->playback_ptr
, period_bytes
, buffer_bytes
);
99 periods_elapsed
+= buffer_bytes
;
101 periods_elapsed
/= period_bytes
;
102 /* wrap the pointer _before_ calling snd_pcm_period_elapsed(),
103 * or ALSA will BUG on us. */
104 chip
->playback_ptr
%= buffer_bytes
;
106 if (periods_elapsed
) {
107 chip
->period_ptr
+= periods_elapsed
* period_bytes
;
108 chip
->period_ptr
%= buffer_bytes
;
109 tasklet_schedule(&pcsp_pcm_tasklet
);
111 spin_unlock_irqrestore(&chip
->substream_lock
, flags
);
113 if (!atomic_read(&chip
->timer_active
))
116 chip
->ns_rem
= PCSP_PERIOD_NS();
117 ns
= (chip
->thalf
? PCSP_CALC_NS(timer_cnt
) : chip
->ns_rem
);
119 hrtimer_forward(&chip
->timer
, hrtimer_get_expires(&chip
->timer
),
121 return HRTIMER_RESTART
;
124 return HRTIMER_NORESTART
;
127 static void pcsp_start_playing(struct snd_pcsp
*chip
)
130 printk(KERN_INFO
"PCSP: start_playing called\n");
132 if (atomic_read(&chip
->timer_active
)) {
133 printk(KERN_ERR
"PCSP: Timer already active\n");
137 spin_lock(&i8253_lock
);
138 chip
->val61
= inb(0x61) | 0x03;
139 outb_p(0x92, 0x43); /* binary, mode 1, LSB only, ch 2 */
140 spin_unlock(&i8253_lock
);
141 atomic_set(&chip
->timer_active
, 1);
144 hrtimer_start(&pcsp_chip
.timer
, ktime_set(0, 0), HRTIMER_MODE_REL
);
147 static void pcsp_stop_playing(struct snd_pcsp
*chip
)
150 printk(KERN_INFO
"PCSP: stop_playing called\n");
152 if (!atomic_read(&chip
->timer_active
))
155 atomic_set(&chip
->timer_active
, 0);
156 spin_lock(&i8253_lock
);
157 /* restore the timer */
158 outb_p(0xb6, 0x43); /* binary, mode 3, LSB/MSB, ch 2 */
159 outb(chip
->val61
& 0xFC, 0x61);
160 spin_unlock(&i8253_lock
);
164 * Force to stop and sync the stream
166 void pcsp_sync_stop(struct snd_pcsp
*chip
)
169 pcsp_stop_playing(chip
);
171 hrtimer_cancel(&chip
->timer
);
172 tasklet_kill(&pcsp_pcm_tasklet
);
175 static int snd_pcsp_playback_close(struct snd_pcm_substream
*substream
)
177 struct snd_pcsp
*chip
= snd_pcm_substream_chip(substream
);
179 printk(KERN_INFO
"PCSP: close called\n");
181 pcsp_sync_stop(chip
);
182 chip
->playback_substream
= NULL
;
186 static int snd_pcsp_playback_hw_params(struct snd_pcm_substream
*substream
,
187 struct snd_pcm_hw_params
*hw_params
)
189 struct snd_pcsp
*chip
= snd_pcm_substream_chip(substream
);
191 pcsp_sync_stop(chip
);
192 err
= snd_pcm_lib_malloc_pages(substream
,
193 params_buffer_bytes(hw_params
));
199 static int snd_pcsp_playback_hw_free(struct snd_pcm_substream
*substream
)
201 struct snd_pcsp
*chip
= snd_pcm_substream_chip(substream
);
203 printk(KERN_INFO
"PCSP: hw_free called\n");
205 pcsp_sync_stop(chip
);
206 return snd_pcm_lib_free_pages(substream
);
209 static int snd_pcsp_playback_prepare(struct snd_pcm_substream
*substream
)
211 struct snd_pcsp
*chip
= snd_pcm_substream_chip(substream
);
213 printk(KERN_INFO
"PCSP: prepare called, "
214 "size=%zi psize=%zi f=%zi f1=%i\n",
215 snd_pcm_lib_buffer_bytes(substream
),
216 snd_pcm_lib_period_bytes(substream
),
217 snd_pcm_lib_buffer_bytes(substream
) /
218 snd_pcm_lib_period_bytes(substream
),
219 substream
->runtime
->periods
);
221 pcsp_sync_stop(chip
);
222 chip
->playback_ptr
= 0;
223 chip
->period_ptr
= 0;
227 static int snd_pcsp_trigger(struct snd_pcm_substream
*substream
, int cmd
)
229 struct snd_pcsp
*chip
= snd_pcm_substream_chip(substream
);
231 printk(KERN_INFO
"PCSP: trigger called\n");
234 case SNDRV_PCM_TRIGGER_START
:
235 case SNDRV_PCM_TRIGGER_RESUME
:
236 pcsp_start_playing(chip
);
238 case SNDRV_PCM_TRIGGER_STOP
:
239 case SNDRV_PCM_TRIGGER_SUSPEND
:
240 pcsp_stop_playing(chip
);
248 static snd_pcm_uframes_t
snd_pcsp_playback_pointer(struct snd_pcm_substream
251 struct snd_pcsp
*chip
= snd_pcm_substream_chip(substream
);
253 spin_lock(&chip
->substream_lock
);
254 pos
= chip
->playback_ptr
;
255 spin_unlock(&chip
->substream_lock
);
256 return bytes_to_frames(substream
->runtime
, pos
);
259 static struct snd_pcm_hardware snd_pcsp_playback
= {
260 .info
= (SNDRV_PCM_INFO_INTERLEAVED
|
261 SNDRV_PCM_INFO_HALF_DUPLEX
|
262 SNDRV_PCM_INFO_MMAP
| SNDRV_PCM_INFO_MMAP_VALID
),
263 .formats
= (SNDRV_PCM_FMTBIT_U8
265 | SNDRV_PCM_FMTBIT_S16_LE
268 .rates
= SNDRV_PCM_RATE_KNOT
,
269 .rate_min
= PCSP_DEFAULT_SRATE
,
270 .rate_max
= PCSP_DEFAULT_SRATE
,
273 .buffer_bytes_max
= PCSP_BUFFER_SIZE
,
274 .period_bytes_min
= 64,
275 .period_bytes_max
= PCSP_MAX_PERIOD_SIZE
,
277 .periods_max
= PCSP_MAX_PERIODS
,
281 static int snd_pcsp_playback_open(struct snd_pcm_substream
*substream
)
283 struct snd_pcsp
*chip
= snd_pcm_substream_chip(substream
);
284 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
286 printk(KERN_INFO
"PCSP: open called\n");
288 if (atomic_read(&chip
->timer_active
)) {
289 printk(KERN_ERR
"PCSP: still active!!\n");
292 runtime
->hw
= snd_pcsp_playback
;
293 chip
->playback_substream
= substream
;
297 static struct snd_pcm_ops snd_pcsp_playback_ops
= {
298 .open
= snd_pcsp_playback_open
,
299 .close
= snd_pcsp_playback_close
,
300 .ioctl
= snd_pcm_lib_ioctl
,
301 .hw_params
= snd_pcsp_playback_hw_params
,
302 .hw_free
= snd_pcsp_playback_hw_free
,
303 .prepare
= snd_pcsp_playback_prepare
,
304 .trigger
= snd_pcsp_trigger
,
305 .pointer
= snd_pcsp_playback_pointer
,
308 int __devinit
snd_pcsp_new_pcm(struct snd_pcsp
*chip
)
312 err
= snd_pcm_new(chip
->card
, "pcspeaker", 0, 1, 0, &chip
->pcm
);
316 snd_pcm_set_ops(chip
->pcm
, SNDRV_PCM_STREAM_PLAYBACK
,
317 &snd_pcsp_playback_ops
);
319 chip
->pcm
->private_data
= chip
;
320 chip
->pcm
->info_flags
= SNDRV_PCM_INFO_HALF_DUPLEX
;
321 strcpy(chip
->pcm
->name
, "pcsp");
323 snd_pcm_lib_preallocate_pages_for_all(chip
->pcm
,
324 SNDRV_DMA_TYPE_CONTINUOUS
,
325 snd_dma_continuous_data
326 (GFP_KERNEL
), PCSP_BUFFER_SIZE
,