snd-pcsp: use HRTIMER_CB_SOFTIRQ
[linux-2.6] / sound / drivers / pcsp / pcsp_lib.c
1 /*
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>
12 #include <asm/io.h>
13 #include "pcsp.h"
14
15 static int nforce_wa;
16 module_param(nforce_wa, bool, 0444);
17 MODULE_PARM_DESC(nforce_wa, "Apply NForce chipset workaround "
18                 "(expect bad sound)");
19
20 #define DMIX_WANTS_S16  1
21
22 enum hrtimer_restart pcsp_do_timer(struct hrtimer *handle)
23 {
24         unsigned char timer_cnt, val;
25         int fmt_size, periods_elapsed;
26         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;
37                 hrtimer_forward(&chip->timer, chip->timer.expires,
38                                 ktime_set(0, chip->ns_rem));
39                 return HRTIMER_RESTART;
40         }
41
42         spin_lock_irq(&chip->substream_lock);
43         /* 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;
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))
72                 val ^= 0x80;
73         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);
90         chip->playback_ptr += PCSP_INDEX_INC() * fmt_size;
91         periods_elapsed = chip->playback_ptr - chip->period_ptr;
92         if (periods_elapsed < 0) {
93 #if PCSP_DEBUG
94                 printk(KERN_INFO "PCSP: buffer_bytes mod period_bytes != 0 ? "
95                         "(%zi %zi %zi)\n",
96                         chip->playback_ptr, period_bytes, buffer_bytes);
97 #endif
98                 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
113         spin_unlock_irq(&chip->substream_lock);
114
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;
121         hrtimer_forward(&chip->timer, chip->timer.expires, ktime_set(0, ns));
122         return HRTIMER_RESTART;
123
124 exit_nr_unlock2:
125         snd_pcm_stream_unlock(substream);
126 exit_nr_unlock1:
127         spin_unlock_irq(&chip->substream_lock);
128         return HRTIMER_NORESTART;
129 }
130
131 static void pcsp_start_playing(struct snd_pcsp *chip)
132 {
133 #if PCSP_DEBUG
134         printk(KERN_INFO "PCSP: start_playing called\n");
135 #endif
136         if (atomic_read(&chip->timer_active)) {
137                 printk(KERN_ERR "PCSP: Timer already active\n");
138                 return;
139         }
140
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);
146         chip->thalf = 0;
147
148         hrtimer_start(&pcsp_chip.timer, ktime_set(0, 0), HRTIMER_MODE_REL);
149 }
150
151 static void pcsp_stop_playing(struct snd_pcsp *chip)
152 {
153 #if PCSP_DEBUG
154         printk(KERN_INFO "PCSP: stop_playing called\n");
155 #endif
156         if (!atomic_read(&chip->timer_active))
157                 return;
158
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);
165 }
166
167 static int snd_pcsp_playback_close(struct snd_pcm_substream *substream)
168 {
169         struct snd_pcsp *chip = snd_pcm_substream_chip(substream);
170 #if PCSP_DEBUG
171         printk(KERN_INFO "PCSP: close called\n");
172 #endif
173         if (atomic_read(&chip->timer_active)) {
174                 printk(KERN_ERR "PCSP: timer still active\n");
175                 pcsp_stop_playing(chip);
176         }
177         spin_lock_irq(&chip->substream_lock);
178         chip->playback_substream = NULL;
179         spin_unlock_irq(&chip->substream_lock);
180         return 0;
181 }
182
183 static int snd_pcsp_playback_hw_params(struct snd_pcm_substream *substream,
184                                        struct snd_pcm_hw_params *hw_params)
185 {
186         int err;
187         err = snd_pcm_lib_malloc_pages(substream,
188                                       params_buffer_bytes(hw_params));
189         if (err < 0)
190                 return err;
191         return 0;
192 }
193
194 static int snd_pcsp_playback_hw_free(struct snd_pcm_substream *substream)
195 {
196 #if PCSP_DEBUG
197         printk(KERN_INFO "PCSP: hw_free called\n");
198 #endif
199         return snd_pcm_lib_free_pages(substream);
200 }
201
202 static int snd_pcsp_playback_prepare(struct snd_pcm_substream *substream)
203 {
204         struct snd_pcsp *chip = snd_pcm_substream_chip(substream);
205 #if PCSP_DEBUG
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);
213 #endif
214         chip->playback_ptr = 0;
215         chip->period_ptr = 0;
216         return 0;
217 }
218
219 static int snd_pcsp_trigger(struct snd_pcm_substream *substream, int cmd)
220 {
221         struct snd_pcsp *chip = snd_pcm_substream_chip(substream);
222 #if PCSP_DEBUG
223         printk(KERN_INFO "PCSP: trigger called\n");
224 #endif
225         switch (cmd) {
226         case SNDRV_PCM_TRIGGER_START:
227         case SNDRV_PCM_TRIGGER_RESUME:
228                 pcsp_start_playing(chip);
229                 break;
230         case SNDRV_PCM_TRIGGER_STOP:
231         case SNDRV_PCM_TRIGGER_SUSPEND:
232                 pcsp_stop_playing(chip);
233                 break;
234         default:
235                 return -EINVAL;
236         }
237         return 0;
238 }
239
240 static snd_pcm_uframes_t snd_pcsp_playback_pointer(struct snd_pcm_substream
241                                                    *substream)
242 {
243         struct snd_pcsp *chip = snd_pcm_substream_chip(substream);
244         return bytes_to_frames(substream->runtime, chip->playback_ptr);
245 }
246
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
252 #if DMIX_WANTS_S16
253                     | SNDRV_PCM_FMTBIT_S16_LE
254 #endif
255             ),
256         .rates = SNDRV_PCM_RATE_KNOT,
257         .rate_min = PCSP_DEFAULT_SRATE,
258         .rate_max = PCSP_DEFAULT_SRATE,
259         .channels_min = 1,
260         .channels_max = 1,
261         .buffer_bytes_max = PCSP_BUFFER_SIZE,
262         .period_bytes_min = 64,
263         .period_bytes_max = PCSP_MAX_PERIOD_SIZE,
264         .periods_min = 2,
265         .periods_max = PCSP_MAX_PERIODS,
266         .fifo_size = 0,
267 };
268
269 static int snd_pcsp_playback_open(struct snd_pcm_substream *substream)
270 {
271         struct snd_pcsp *chip = snd_pcm_substream_chip(substream);
272         struct snd_pcm_runtime *runtime = substream->runtime;
273 #if PCSP_DEBUG
274         printk(KERN_INFO "PCSP: open called\n");
275 #endif
276         if (atomic_read(&chip->timer_active)) {
277                 printk(KERN_ERR "PCSP: still active!!\n");
278                 return -EBUSY;
279         }
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);
284         return 0;
285 }
286
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,
296 };
297
298 int __devinit snd_pcsp_new_pcm(struct snd_pcsp *chip)
299 {
300         int err;
301
302         err = snd_pcm_new(chip->card, "pcspeaker", 0, 1, 0, &chip->pcm);
303         if (err < 0)
304                 return err;
305
306         snd_pcm_set_ops(chip->pcm, SNDRV_PCM_STREAM_PLAYBACK,
307                         &snd_pcsp_playback_ops);
308
309         chip->pcm->private_data = chip;
310         chip->pcm->info_flags = SNDRV_PCM_INFO_HALF_DUPLEX;
311         strcpy(chip->pcm->name, "pcsp");
312
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,
317                                               PCSP_BUFFER_SIZE);
318
319         return 0;
320 }