snd-pcsp: put back the compatibility code for the older alsa-libs
[safe/jmp/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 <linux/interrupt.h>
13 #include <asm/io.h>
14 #include "pcsp.h"
15
16 static int nforce_wa;
17 module_param(nforce_wa, bool, 0444);
18 MODULE_PARM_DESC(nforce_wa, "Apply NForce chipset workaround "
19                 "(expect bad sound)");
20
21 #define DMIX_WANTS_S16  1
22
23 static void pcsp_start_timer(unsigned long dummy)
24 {
25         hrtimer_start(&pcsp_chip.timer, ktime_set(0, 0), HRTIMER_MODE_REL);
26 }
27
28 /*
29  * We need the hrtimer_start as a tasklet to avoid
30  * the nasty locking problem. :(
31  * The problem:
32  * - The timer handler is called with the cpu_base->lock
33  *   already held by hrtimer code.
34  * - snd_pcm_period_elapsed() takes the
35  *   substream->self_group.lock.
36  * So far so good.
37  * But the snd_pcsp_trigger() is called with the
38  * substream->self_group.lock held, and it calls
39  * hrtimer_start(), which takes the cpu_base->lock.
40  * You see the problem. We have the code pathes
41  * which take two locks in a reverse order. This
42  * can deadlock and the lock validator complains.
43  * The only solution I could find was to move the
44  * hrtimer_start() into a tasklet. -stsp
45  */
46 static DECLARE_TASKLET(pcsp_start_timer_tasklet, pcsp_start_timer, 0);
47
48 enum hrtimer_restart pcsp_do_timer(struct hrtimer *handle)
49 {
50         unsigned long flags;
51         unsigned char timer_cnt, val;
52         int fmt_size, periods_elapsed;
53         u64 ns;
54         size_t period_bytes, buffer_bytes;
55         struct snd_pcm_substream *substream;
56         struct snd_pcm_runtime *runtime;
57         struct snd_pcsp *chip = container_of(handle, struct snd_pcsp, timer);
58
59         if (chip->thalf) {
60                 outb(chip->val61, 0x61);
61                 chip->thalf = 0;
62                 if (!atomic_read(&chip->timer_active))
63                         return HRTIMER_NORESTART;
64                 hrtimer_forward(&chip->timer, chip->timer.expires,
65                                 ktime_set(0, chip->ns_rem));
66                 return HRTIMER_RESTART;
67         }
68
69         /* hrtimer calls us from both hardirq and softirq contexts,
70          * so irqsave :( */
71         spin_lock_irqsave(&chip->substream_lock, flags);
72         /* Takashi Iwai says regarding this extra lock:
73
74         If the irq handler handles some data on the DMA buffer, it should
75         do snd_pcm_stream_lock().
76         That protects basically against all races among PCM callbacks, yes.
77         However, there are two remaining issues:
78         1. The substream pointer you try to lock isn't protected _before_
79           this lock yet.
80         2. snd_pcm_period_elapsed() itself acquires the lock.
81         The requirement of another lock is because of 1.  When you get
82         chip->playback_substream, it's not protected.
83         Keeping this lock while snd_pcm_period_elapsed() assures the substream
84         is still protected (at least, not released).  And the other status is
85         handled properly inside snd_pcm_stream_lock() in
86         snd_pcm_period_elapsed().
87
88         */
89         if (!chip->playback_substream)
90                 goto exit_nr_unlock1;
91         substream = chip->playback_substream;
92         snd_pcm_stream_lock(substream);
93         if (!atomic_read(&chip->timer_active))
94                 goto exit_nr_unlock2;
95
96         runtime = substream->runtime;
97         fmt_size = snd_pcm_format_physical_width(runtime->format) >> 3;
98         /* assume it is mono! */
99         val = runtime->dma_area[chip->playback_ptr + fmt_size - 1];
100         if (snd_pcm_format_signed(runtime->format))
101                 val ^= 0x80;
102         timer_cnt = val * CUR_DIV() / 256;
103
104         if (timer_cnt && chip->enable) {
105                 spin_lock(&i8253_lock);
106                 if (!nforce_wa) {
107                         outb_p(chip->val61, 0x61);
108                         outb_p(timer_cnt, 0x42);
109                         outb(chip->val61 ^ 1, 0x61);
110                 } else {
111                         outb(chip->val61 ^ 2, 0x61);
112                         chip->thalf = 1;
113                 }
114                 spin_unlock(&i8253_lock);
115         }
116
117         period_bytes = snd_pcm_lib_period_bytes(substream);
118         buffer_bytes = snd_pcm_lib_buffer_bytes(substream);
119         chip->playback_ptr += PCSP_INDEX_INC() * fmt_size;
120         periods_elapsed = chip->playback_ptr - chip->period_ptr;
121         if (periods_elapsed < 0) {
122                 printk(KERN_WARNING "PCSP: playback_ptr inconsistent "
123                         "(%zi %zi %zi)\n",
124                         chip->playback_ptr, period_bytes, buffer_bytes);
125                 periods_elapsed += buffer_bytes;
126         }
127         periods_elapsed /= period_bytes;
128         /* wrap the pointer _before_ calling snd_pcm_period_elapsed(),
129          * or ALSA will BUG on us. */
130         chip->playback_ptr %= buffer_bytes;
131
132         snd_pcm_stream_unlock(substream);
133
134         if (periods_elapsed) {
135                 snd_pcm_period_elapsed(substream);
136                 chip->period_ptr += periods_elapsed * period_bytes;
137                 chip->period_ptr %= buffer_bytes;
138         }
139
140         spin_unlock_irqrestore(&chip->substream_lock, flags);
141
142         if (!atomic_read(&chip->timer_active))
143                 return HRTIMER_NORESTART;
144
145         chip->ns_rem = PCSP_PERIOD_NS();
146         ns = (chip->thalf ? PCSP_CALC_NS(timer_cnt) : chip->ns_rem);
147         chip->ns_rem -= ns;
148         hrtimer_forward(&chip->timer, chip->timer.expires, ktime_set(0, ns));
149         return HRTIMER_RESTART;
150
151 exit_nr_unlock2:
152         snd_pcm_stream_unlock(substream);
153 exit_nr_unlock1:
154         spin_unlock_irqrestore(&chip->substream_lock, flags);
155         return HRTIMER_NORESTART;
156 }
157
158 static void pcsp_start_playing(struct snd_pcsp *chip)
159 {
160 #if PCSP_DEBUG
161         printk(KERN_INFO "PCSP: start_playing called\n");
162 #endif
163         if (atomic_read(&chip->timer_active)) {
164                 printk(KERN_ERR "PCSP: Timer already active\n");
165                 return;
166         }
167
168         spin_lock(&i8253_lock);
169         chip->val61 = inb(0x61) | 0x03;
170         outb_p(0x92, 0x43);     /* binary, mode 1, LSB only, ch 2 */
171         spin_unlock(&i8253_lock);
172         atomic_set(&chip->timer_active, 1);
173         chip->thalf = 0;
174
175         tasklet_schedule(&pcsp_start_timer_tasklet);
176 }
177
178 static void pcsp_stop_playing(struct snd_pcsp *chip)
179 {
180 #if PCSP_DEBUG
181         printk(KERN_INFO "PCSP: stop_playing called\n");
182 #endif
183         if (!atomic_read(&chip->timer_active))
184                 return;
185
186         atomic_set(&chip->timer_active, 0);
187         spin_lock(&i8253_lock);
188         /* restore the timer */
189         outb_p(0xb6, 0x43);     /* binary, mode 3, LSB/MSB, ch 2 */
190         outb(chip->val61 & 0xFC, 0x61);
191         spin_unlock(&i8253_lock);
192 }
193
194 static int snd_pcsp_playback_close(struct snd_pcm_substream *substream)
195 {
196         struct snd_pcsp *chip = snd_pcm_substream_chip(substream);
197 #if PCSP_DEBUG
198         printk(KERN_INFO "PCSP: close called\n");
199 #endif
200         if (atomic_read(&chip->timer_active)) {
201                 printk(KERN_ERR "PCSP: timer still active\n");
202                 pcsp_stop_playing(chip);
203         }
204         spin_lock_irq(&chip->substream_lock);
205         chip->playback_substream = NULL;
206         spin_unlock_irq(&chip->substream_lock);
207         return 0;
208 }
209
210 static int snd_pcsp_playback_hw_params(struct snd_pcm_substream *substream,
211                                        struct snd_pcm_hw_params *hw_params)
212 {
213         int err;
214         err = snd_pcm_lib_malloc_pages(substream,
215                                       params_buffer_bytes(hw_params));
216         if (err < 0)
217                 return err;
218         return 0;
219 }
220
221 static int snd_pcsp_playback_hw_free(struct snd_pcm_substream *substream)
222 {
223 #if PCSP_DEBUG
224         printk(KERN_INFO "PCSP: hw_free called\n");
225 #endif
226         return snd_pcm_lib_free_pages(substream);
227 }
228
229 static int snd_pcsp_playback_prepare(struct snd_pcm_substream *substream)
230 {
231         struct snd_pcsp *chip = snd_pcm_substream_chip(substream);
232 #if PCSP_DEBUG
233         printk(KERN_INFO "PCSP: prepare called, "
234                         "size=%zi psize=%zi f=%zi f1=%i\n",
235                         snd_pcm_lib_buffer_bytes(substream),
236                         snd_pcm_lib_period_bytes(substream),
237                         snd_pcm_lib_buffer_bytes(substream) /
238                         snd_pcm_lib_period_bytes(substream),
239                         substream->runtime->periods);
240 #endif
241         chip->playback_ptr = 0;
242         chip->period_ptr = 0;
243         return 0;
244 }
245
246 static int snd_pcsp_trigger(struct snd_pcm_substream *substream, int cmd)
247 {
248         struct snd_pcsp *chip = snd_pcm_substream_chip(substream);
249 #if PCSP_DEBUG
250         printk(KERN_INFO "PCSP: trigger called\n");
251 #endif
252         switch (cmd) {
253         case SNDRV_PCM_TRIGGER_START:
254         case SNDRV_PCM_TRIGGER_RESUME:
255                 pcsp_start_playing(chip);
256                 break;
257         case SNDRV_PCM_TRIGGER_STOP:
258         case SNDRV_PCM_TRIGGER_SUSPEND:
259                 pcsp_stop_playing(chip);
260                 break;
261         default:
262                 return -EINVAL;
263         }
264         return 0;
265 }
266
267 static snd_pcm_uframes_t snd_pcsp_playback_pointer(struct snd_pcm_substream
268                                                    *substream)
269 {
270         struct snd_pcsp *chip = snd_pcm_substream_chip(substream);
271         return bytes_to_frames(substream->runtime, chip->playback_ptr);
272 }
273
274 static struct snd_pcm_hardware snd_pcsp_playback = {
275         .info = (SNDRV_PCM_INFO_INTERLEAVED |
276                  SNDRV_PCM_INFO_HALF_DUPLEX |
277                  SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID),
278         .formats = (SNDRV_PCM_FMTBIT_U8
279 #if DMIX_WANTS_S16
280                     | SNDRV_PCM_FMTBIT_S16_LE
281 #endif
282             ),
283         .rates = SNDRV_PCM_RATE_KNOT,
284         .rate_min = PCSP_DEFAULT_SRATE,
285         .rate_max = PCSP_DEFAULT_SRATE,
286         .channels_min = 1,
287         .channels_max = 1,
288         .buffer_bytes_max = PCSP_BUFFER_SIZE,
289         .period_bytes_min = 64,
290         .period_bytes_max = PCSP_MAX_PERIOD_SIZE,
291         .periods_min = 2,
292         .periods_max = PCSP_MAX_PERIODS,
293         .fifo_size = 0,
294 };
295
296 static int snd_pcsp_playback_open(struct snd_pcm_substream *substream)
297 {
298         struct snd_pcsp *chip = snd_pcm_substream_chip(substream);
299         struct snd_pcm_runtime *runtime = substream->runtime;
300 #if PCSP_DEBUG
301         printk(KERN_INFO "PCSP: open called\n");
302 #endif
303         if (atomic_read(&chip->timer_active)) {
304                 printk(KERN_ERR "PCSP: still active!!\n");
305                 return -EBUSY;
306         }
307         runtime->hw = snd_pcsp_playback;
308         spin_lock_irq(&chip->substream_lock);
309         chip->playback_substream = substream;
310         spin_unlock_irq(&chip->substream_lock);
311         return 0;
312 }
313
314 static struct snd_pcm_ops snd_pcsp_playback_ops = {
315         .open = snd_pcsp_playback_open,
316         .close = snd_pcsp_playback_close,
317         .ioctl = snd_pcm_lib_ioctl,
318         .hw_params = snd_pcsp_playback_hw_params,
319         .hw_free = snd_pcsp_playback_hw_free,
320         .prepare = snd_pcsp_playback_prepare,
321         .trigger = snd_pcsp_trigger,
322         .pointer = snd_pcsp_playback_pointer,
323 };
324
325 int __devinit snd_pcsp_new_pcm(struct snd_pcsp *chip)
326 {
327         int err;
328
329         err = snd_pcm_new(chip->card, "pcspeaker", 0, 1, 0, &chip->pcm);
330         if (err < 0)
331                 return err;
332
333         snd_pcm_set_ops(chip->pcm, SNDRV_PCM_STREAM_PLAYBACK,
334                         &snd_pcsp_playback_ops);
335
336         chip->pcm->private_data = chip;
337         chip->pcm->info_flags = SNDRV_PCM_INFO_HALF_DUPLEX;
338         strcpy(chip->pcm->name, "pcsp");
339
340         snd_pcm_lib_preallocate_pages_for_all(chip->pcm,
341                                               SNDRV_DMA_TYPE_CONTINUOUS,
342                                               snd_dma_continuous_data
343                                               (GFP_KERNEL), PCSP_BUFFER_SIZE,
344                                               PCSP_BUFFER_SIZE);
345
346         return 0;
347 }