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
, chip
->timer
.expires
,
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
, chip
->timer
.expires
, ktime_set(0, ns
));
122 return HRTIMER_RESTART
;
125 snd_pcm_stream_unlock(substream
);
127 spin_unlock_irq(&chip
->substream_lock
);
128 return HRTIMER_NORESTART
;
131 static void pcsp_start_playing(struct snd_pcsp
*chip
)
134 printk(KERN_INFO
"PCSP: start_playing called\n");
136 if (atomic_read(&chip
->timer_active
)) {
137 printk(KERN_ERR
"PCSP: Timer already active\n");
141 spin_lock(&i8253_lock
);
142 chip
->val61
= inb(0x61) | 0x03;
143 outb_p(0x92, 0x43); /* binary, mode 1, LSB only, ch 2 */
144 spin_unlock(&i8253_lock
);
145 atomic_set(&chip
->timer_active
, 1);
148 hrtimer_start(&pcsp_chip
.timer
, ktime_set(0, 0), HRTIMER_MODE_REL
);
151 static void pcsp_stop_playing(struct snd_pcsp
*chip
)
154 printk(KERN_INFO
"PCSP: stop_playing called\n");
156 if (!atomic_read(&chip
->timer_active
))
159 atomic_set(&chip
->timer_active
, 0);
160 spin_lock(&i8253_lock
);
161 /* restore the timer */
162 outb_p(0xb6, 0x43); /* binary, mode 3, LSB/MSB, ch 2 */
163 outb(chip
->val61
& 0xFC, 0x61);
164 spin_unlock(&i8253_lock
);
167 static int snd_pcsp_playback_close(struct snd_pcm_substream
*substream
)
169 struct snd_pcsp
*chip
= snd_pcm_substream_chip(substream
);
171 printk(KERN_INFO
"PCSP: close called\n");
173 if (atomic_read(&chip
->timer_active
)) {
174 printk(KERN_ERR
"PCSP: timer still active\n");
175 pcsp_stop_playing(chip
);
177 spin_lock_irq(&chip
->substream_lock
);
178 chip
->playback_substream
= NULL
;
179 spin_unlock_irq(&chip
->substream_lock
);
183 static int snd_pcsp_playback_hw_params(struct snd_pcm_substream
*substream
,
184 struct snd_pcm_hw_params
*hw_params
)
187 err
= snd_pcm_lib_malloc_pages(substream
,
188 params_buffer_bytes(hw_params
));
194 static int snd_pcsp_playback_hw_free(struct snd_pcm_substream
*substream
)
197 printk(KERN_INFO
"PCSP: hw_free called\n");
199 return snd_pcm_lib_free_pages(substream
);
202 static int snd_pcsp_playback_prepare(struct snd_pcm_substream
*substream
)
204 struct snd_pcsp
*chip
= snd_pcm_substream_chip(substream
);
206 printk(KERN_INFO
"PCSP: prepare called, "
207 "size=%zi psize=%zi f=%zi f1=%i\n",
208 snd_pcm_lib_buffer_bytes(substream
),
209 snd_pcm_lib_period_bytes(substream
),
210 snd_pcm_lib_buffer_bytes(substream
) /
211 snd_pcm_lib_period_bytes(substream
),
212 substream
->runtime
->periods
);
214 chip
->playback_ptr
= 0;
215 chip
->period_ptr
= 0;
219 static int snd_pcsp_trigger(struct snd_pcm_substream
*substream
, int cmd
)
221 struct snd_pcsp
*chip
= snd_pcm_substream_chip(substream
);
223 printk(KERN_INFO
"PCSP: trigger called\n");
226 case SNDRV_PCM_TRIGGER_START
:
227 case SNDRV_PCM_TRIGGER_RESUME
:
228 pcsp_start_playing(chip
);
230 case SNDRV_PCM_TRIGGER_STOP
:
231 case SNDRV_PCM_TRIGGER_SUSPEND
:
232 pcsp_stop_playing(chip
);
240 static snd_pcm_uframes_t
snd_pcsp_playback_pointer(struct snd_pcm_substream
243 struct snd_pcsp
*chip
= snd_pcm_substream_chip(substream
);
244 return bytes_to_frames(substream
->runtime
, chip
->playback_ptr
);
247 static struct snd_pcm_hardware snd_pcsp_playback
= {
248 .info
= (SNDRV_PCM_INFO_INTERLEAVED
|
249 SNDRV_PCM_INFO_HALF_DUPLEX
|
250 SNDRV_PCM_INFO_MMAP
| SNDRV_PCM_INFO_MMAP_VALID
),
251 .formats
= (SNDRV_PCM_FMTBIT_U8
253 | SNDRV_PCM_FMTBIT_S16_LE
256 .rates
= SNDRV_PCM_RATE_KNOT
,
257 .rate_min
= PCSP_DEFAULT_SRATE
,
258 .rate_max
= PCSP_DEFAULT_SRATE
,
261 .buffer_bytes_max
= PCSP_BUFFER_SIZE
,
262 .period_bytes_min
= 64,
263 .period_bytes_max
= PCSP_MAX_PERIOD_SIZE
,
265 .periods_max
= PCSP_MAX_PERIODS
,
269 static int snd_pcsp_playback_open(struct snd_pcm_substream
*substream
)
271 struct snd_pcsp
*chip
= snd_pcm_substream_chip(substream
);
272 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
274 printk(KERN_INFO
"PCSP: open called\n");
276 if (atomic_read(&chip
->timer_active
)) {
277 printk(KERN_ERR
"PCSP: still active!!\n");
280 runtime
->hw
= snd_pcsp_playback
;
281 spin_lock_irq(&chip
->substream_lock
);
282 chip
->playback_substream
= substream
;
283 spin_unlock_irq(&chip
->substream_lock
);
287 static struct snd_pcm_ops snd_pcsp_playback_ops
= {
288 .open
= snd_pcsp_playback_open
,
289 .close
= snd_pcsp_playback_close
,
290 .ioctl
= snd_pcm_lib_ioctl
,
291 .hw_params
= snd_pcsp_playback_hw_params
,
292 .hw_free
= snd_pcsp_playback_hw_free
,
293 .prepare
= snd_pcsp_playback_prepare
,
294 .trigger
= snd_pcsp_trigger
,
295 .pointer
= snd_pcsp_playback_pointer
,
298 int __devinit
snd_pcsp_new_pcm(struct snd_pcsp
*chip
)
302 err
= snd_pcm_new(chip
->card
, "pcspeaker", 0, 1, 0, &chip
->pcm
);
306 snd_pcm_set_ops(chip
->pcm
, SNDRV_PCM_STREAM_PLAYBACK
,
307 &snd_pcsp_playback_ops
);
309 chip
->pcm
->private_data
= chip
;
310 chip
->pcm
->info_flags
= SNDRV_PCM_INFO_HALF_DUPLEX
;
311 strcpy(chip
->pcm
->name
, "pcsp");
313 snd_pcm_lib_preallocate_pages_for_all(chip
->pcm
,
314 SNDRV_DMA_TYPE_CONTINUOUS
,
315 snd_dma_continuous_data
316 (GFP_KERNEL
), PCSP_BUFFER_SIZE
,