[PATCH] Fix sequencer missing negative bound check
[pandora-kernel.git] / sound / oss / waveartist.c
1 /*
2  * linux/drivers/sound/waveartist.c
3  *
4  * The low level driver for the RWA010 Rockwell Wave Artist
5  * codec chip used in the Rebel.com NetWinder.
6  *
7  * Cleaned up and integrated into 2.1 by Russell King (rmk@arm.linux.org.uk)
8  * and Pat Beirne (patb@corel.ca)
9  *
10  *
11  * Copyright (C) by Rebel.com 1998-1999
12  *
13  * RWA010 specs received under NDA from Rockwell
14  *
15  * Copyright (C) by Hannu Savolainen 1993-1997
16  *
17  * OSS/Free for Linux is distributed under the GNU GENERAL PUBLIC LICENSE (GPL)
18  * Version 2 (June 1991). See the "COPYING" file distributed with this software
19  * for more info.
20  *
21  * Changes:
22  * 11-10-2000   Bartlomiej Zolnierkiewicz <bkz@linux-ide.org>
23  *              Added __init to waveartist_init()
24  */
25
26 /* Debugging */
27 #define DEBUG_CMD       1
28 #define DEBUG_OUT       2
29 #define DEBUG_IN        4
30 #define DEBUG_INTR      8
31 #define DEBUG_MIXER     16
32 #define DEBUG_TRIGGER   32
33
34 #define debug_flg (0)
35
36 #include <linux/module.h>
37 #include <linux/init.h>
38 #include <linux/config.h>
39 #include <linux/sched.h>
40 #include <linux/interrupt.h>
41 #include <linux/delay.h>
42 #include <linux/spinlock.h>
43 #include <linux/bitops.h>
44
45 #include <asm/system.h>
46
47 #include "sound_config.h"
48 #include "waveartist.h"
49
50 #ifdef CONFIG_ARM
51 #include <asm/hardware.h>
52 #include <asm/mach-types.h>
53 #endif
54
55 #ifndef NO_DMA
56 #define NO_DMA  255
57 #endif
58
59 #define SUPPORTED_MIXER_DEVICES         (SOUND_MASK_SYNTH      |\
60                                          SOUND_MASK_PCM        |\
61                                          SOUND_MASK_LINE       |\
62                                          SOUND_MASK_MIC        |\
63                                          SOUND_MASK_LINE1      |\
64                                          SOUND_MASK_RECLEV     |\
65                                          SOUND_MASK_VOLUME     |\
66                                          SOUND_MASK_IMIX)
67
68 static unsigned short levels[SOUND_MIXER_NRDEVICES] = {
69         0x5555,         /* Master Volume         */
70         0x0000,         /* Bass                  */
71         0x0000,         /* Treble                */
72         0x2323,         /* Synth (FM)            */
73         0x4b4b,         /* PCM                   */
74         0x6464,         /* PC Speaker            */
75         0x0000,         /* Ext Line              */
76         0x0000,         /* Mic                   */
77         0x0000,         /* CD                    */
78         0x6464,         /* Recording monitor     */
79         0x0000,         /* SB PCM (ALT PCM)      */
80         0x0000,         /* Recording level       */
81         0x6464,         /* Input gain            */
82         0x6464,         /* Output gain           */
83         0x0000,         /* Line1 (Aux1)          */
84         0x0000,         /* Line2 (Aux2)          */
85         0x0000,         /* Line3 (Aux3)          */
86         0x0000,         /* Digital1              */
87         0x0000,         /* Digital2              */
88         0x0000,         /* Digital3              */
89         0x0000,         /* Phone In              */
90         0x6464,         /* Phone Out             */
91         0x0000,         /* Video                 */
92         0x0000,         /* Radio                 */
93         0x0000          /* Monitor               */
94 };
95
96 typedef struct {
97         struct address_info  hw;        /* hardware */
98         char            *chip_name;
99
100         int             xfer_count;
101         int             audio_mode;
102         int             open_mode;
103         int             audio_flags;
104         int             record_dev;
105         int             playback_dev;
106         int             dev_no;
107
108         /* Mixer parameters */
109         const struct waveartist_mixer_info *mix;
110
111         unsigned short  *levels;           /* cache of volume settings   */
112         int             recmask;           /* currently enabled recording device! */
113
114 #ifdef CONFIG_ARCH_NETWINDER
115         signed int      slider_vol;        /* hardware slider volume     */
116         unsigned int    handset_detect  :1;
117         unsigned int    telephone_detect:1;
118         unsigned int    no_autoselect   :1;/* handset/telephone autoselects a path */
119         unsigned int    spkr_mute_state :1;/* set by ioctl or autoselect */
120         unsigned int    line_mute_state :1;/* set by ioctl or autoselect */
121         unsigned int    use_slider      :1;/* use slider setting for o/p vol */
122 #endif
123 } wavnc_info;
124
125 /*
126  * This is the implementation specific mixer information.
127  */
128 struct waveartist_mixer_info {
129         unsigned int    supported_devs;    /* Supported devices */
130         unsigned int    recording_devs;    /* Recordable devies */
131         unsigned int    stereo_devs;       /* Stereo devices    */
132
133         unsigned int    (*select_input)(wavnc_info *, unsigned int,
134                                         unsigned char *, unsigned char *);
135         int             (*decode_mixer)(wavnc_info *, int,
136                                         unsigned char, unsigned char);
137         int             (*get_mixer)(wavnc_info *, int);
138 };
139
140 typedef struct wavnc_port_info {
141         int             open_mode;
142         int             speed;
143         int             channels;
144         int             audio_format;
145 } wavnc_port_info;
146
147 static int              nr_waveartist_devs;
148 static wavnc_info       adev_info[MAX_AUDIO_DEV];
149 static DEFINE_SPINLOCK(waveartist_lock);
150
151 #ifndef CONFIG_ARCH_NETWINDER
152 #define machine_is_netwinder() 0
153 #else
154 static struct timer_list vnc_timer;
155 static void vnc_configure_mixer(wavnc_info *devc, unsigned int input_mask);
156 static int vnc_private_ioctl(int dev, unsigned int cmd, int __user *arg);
157 static void vnc_slider_tick(unsigned long data);
158 #endif
159
160 static inline void
161 waveartist_set_ctlr(struct address_info *hw, unsigned char clear, unsigned char set)
162 {
163         unsigned int ctlr_port = hw->io_base + CTLR;
164
165         clear = ~clear & inb(ctlr_port);
166
167         outb(clear | set, ctlr_port);
168 }
169
170 /* Toggle IRQ acknowledge line
171  */
172 static inline void
173 waveartist_iack(wavnc_info *devc)
174 {
175         unsigned int ctlr_port = devc->hw.io_base + CTLR;
176         int old_ctlr;
177
178         old_ctlr = inb(ctlr_port) & ~IRQ_ACK;
179
180         outb(old_ctlr | IRQ_ACK, ctlr_port);
181         outb(old_ctlr, ctlr_port);
182 }
183
184 static inline int
185 waveartist_sleep(int timeout_ms)
186 {
187         unsigned int timeout = timeout_ms * 10 * HZ / 100;
188
189         do {
190                 set_current_state(TASK_INTERRUPTIBLE);
191                 timeout = schedule_timeout(timeout);
192         } while (timeout);
193
194         return 0;
195 }
196
197 static int
198 waveartist_reset(wavnc_info *devc)
199 {
200         struct address_info *hw = &devc->hw;
201         unsigned int timeout, res = -1;
202
203         waveartist_set_ctlr(hw, -1, RESET);
204         waveartist_sleep(2);
205         waveartist_set_ctlr(hw, RESET, 0);
206
207         timeout = 500;
208         do {
209                 mdelay(2);
210
211                 if (inb(hw->io_base + STATR) & CMD_RF) {
212                         res = inw(hw->io_base + CMDR);
213                         if (res == 0x55aa)
214                                 break;
215                 }
216         } while (--timeout);
217
218         if (timeout == 0) {
219                 printk(KERN_WARNING "WaveArtist: reset timeout ");
220                 if (res != (unsigned int)-1)
221                         printk("(res=%04X)", res);
222                 printk("\n");
223                 return 1;
224         }
225         return 0;
226 }
227
228 /* Helper function to send and receive words
229  * from WaveArtist.  It handles all the handshaking
230  * and can send or receive multiple words.
231  */
232 static int
233 waveartist_cmd(wavnc_info *devc,
234                 int nr_cmd, unsigned int *cmd,
235                 int nr_resp, unsigned int *resp)
236 {
237         unsigned int io_base = devc->hw.io_base;
238         unsigned int timed_out = 0;
239         unsigned int i;
240
241         if (debug_flg & DEBUG_CMD) {
242                 printk("waveartist_cmd: cmd=");
243
244                 for (i = 0; i < nr_cmd; i++)
245                         printk("%04X ", cmd[i]);
246
247                 printk("\n");
248         }
249
250         if (inb(io_base + STATR) & CMD_RF) {
251                 int old_data;
252
253                 /* flush the port
254                  */
255
256                 old_data = inw(io_base + CMDR);
257
258                 if (debug_flg & DEBUG_CMD)
259                         printk("flushed %04X...", old_data);
260
261                 udelay(10);
262         }
263
264         for (i = 0; !timed_out && i < nr_cmd; i++) {
265                 int count;
266
267                 for (count = 5000; count; count--)
268                         if (inb(io_base + STATR) & CMD_WE)
269                                 break;
270
271                 if (!count)
272                         timed_out = 1;
273                 else
274                         outw(cmd[i], io_base + CMDR);
275         }
276
277         for (i = 0; !timed_out && i < nr_resp; i++) {
278                 int count;
279
280                 for (count = 5000; count; count--)
281                         if (inb(io_base + STATR) & CMD_RF)
282                                 break;
283
284                 if (!count)
285                         timed_out = 1;
286                 else
287                         resp[i] = inw(io_base + CMDR);
288         }
289
290         if (debug_flg & DEBUG_CMD) {
291                 if (!timed_out) {
292                         printk("waveartist_cmd: resp=");
293
294                         for (i = 0; i < nr_resp; i++)
295                                 printk("%04X ", resp[i]);
296
297                         printk("\n");
298                 } else
299                         printk("waveartist_cmd: timed out\n");
300         }
301
302         return timed_out ? 1 : 0;
303 }
304
305 /*
306  * Send one command word
307  */
308 static inline int
309 waveartist_cmd1(wavnc_info *devc, unsigned int cmd)
310 {
311         return waveartist_cmd(devc, 1, &cmd, 0, NULL);
312 }
313
314 /*
315  * Send one command, receive one word
316  */
317 static inline unsigned int
318 waveartist_cmd1_r(wavnc_info *devc, unsigned int cmd)
319 {
320         unsigned int ret;
321
322         waveartist_cmd(devc, 1, &cmd, 1, &ret);
323
324         return ret;
325 }
326
327 /*
328  * Send a double command, receive one
329  * word (and throw it away)
330  */
331 static inline int
332 waveartist_cmd2(wavnc_info *devc, unsigned int cmd, unsigned int arg)
333 {
334         unsigned int vals[2];
335
336         vals[0] = cmd;
337         vals[1] = arg;
338
339         return waveartist_cmd(devc, 2, vals, 1, vals);
340 }
341
342 /*
343  * Send a triple command
344  */
345 static inline int
346 waveartist_cmd3(wavnc_info *devc, unsigned int cmd,
347                 unsigned int arg1, unsigned int arg2)
348 {
349         unsigned int vals[3];
350
351         vals[0] = cmd;
352         vals[1] = arg1;
353         vals[2] = arg2;
354
355         return waveartist_cmd(devc, 3, vals, 0, NULL);
356 }
357
358 static int
359 waveartist_getrev(wavnc_info *devc, char *rev)
360 {
361         unsigned int temp[2];
362         unsigned int cmd = WACMD_GETREV;
363
364         waveartist_cmd(devc, 1, &cmd, 2, temp);
365
366         rev[0] = temp[0] >> 8;
367         rev[1] = temp[0] & 255;
368         rev[2] = '\0';
369
370         return temp[0];
371 }
372
373 static void waveartist_halt_output(int dev);
374 static void waveartist_halt_input(int dev);
375 static void waveartist_halt(int dev);
376 static void waveartist_trigger(int dev, int state);
377
378 static int
379 waveartist_open(int dev, int mode)
380 {
381         wavnc_info      *devc;
382         wavnc_port_info *portc;
383         unsigned long   flags;
384
385         if (dev < 0 || dev >= num_audiodevs)
386                 return -ENXIO;
387
388         devc  = (wavnc_info *) audio_devs[dev]->devc;
389         portc = (wavnc_port_info *) audio_devs[dev]->portc;
390
391         spin_lock_irqsave(&waveartist_lock, flags);
392         if (portc->open_mode || (devc->open_mode & mode)) {
393                 spin_unlock_irqrestore(&waveartist_lock, flags);
394                 return -EBUSY;
395         }
396
397         devc->audio_mode  = 0;
398         devc->open_mode  |= mode;
399         portc->open_mode  = mode;
400         waveartist_trigger(dev, 0);
401
402         if (mode & OPEN_READ)
403                 devc->record_dev = dev;
404         if (mode & OPEN_WRITE)
405                 devc->playback_dev = dev;
406         spin_unlock_irqrestore(&waveartist_lock, flags);
407
408         return 0;
409 }
410
411 static void
412 waveartist_close(int dev)
413 {
414         wavnc_info      *devc = (wavnc_info *) audio_devs[dev]->devc;
415         wavnc_port_info *portc = (wavnc_port_info *) audio_devs[dev]->portc;
416         unsigned long   flags;
417
418         spin_lock_irqsave(&waveartist_lock, flags);
419
420         waveartist_halt(dev);
421
422         devc->audio_mode = 0;
423         devc->open_mode &= ~portc->open_mode;
424         portc->open_mode = 0;
425
426         spin_unlock_irqrestore(&waveartist_lock, flags);
427 }
428
429 static void
430 waveartist_output_block(int dev, unsigned long buf, int __count, int intrflag)
431 {
432         wavnc_port_info *portc = (wavnc_port_info *) audio_devs[dev]->portc;
433         wavnc_info      *devc = (wavnc_info *) audio_devs[dev]->devc;
434         unsigned long   flags;
435         unsigned int    count = __count; 
436
437         if (debug_flg & DEBUG_OUT)
438                 printk("waveartist: output block, buf=0x%lx, count=0x%x...\n",
439                         buf, count);
440         /*
441          * 16 bit data
442          */
443         if (portc->audio_format & (AFMT_S16_LE | AFMT_S16_BE))
444                 count >>= 1;
445
446         if (portc->channels > 1)
447                 count >>= 1;
448
449         count -= 1;
450
451         if (devc->audio_mode & PCM_ENABLE_OUTPUT &&
452             audio_devs[dev]->flags & DMA_AUTOMODE &&
453             intrflag &&
454             count == devc->xfer_count) {
455                 devc->audio_mode |= PCM_ENABLE_OUTPUT;
456                 return; /*
457                          * Auto DMA mode on. No need to react
458                          */
459         }
460
461         spin_lock_irqsave(&waveartist_lock, flags);
462
463         /*
464          * set sample count
465          */
466         waveartist_cmd2(devc, WACMD_OUTPUTSIZE, count);
467
468         devc->xfer_count = count;
469         devc->audio_mode |= PCM_ENABLE_OUTPUT;
470
471         spin_unlock_irqrestore(&waveartist_lock, flags);
472 }
473
474 static void
475 waveartist_start_input(int dev, unsigned long buf, int __count, int intrflag)
476 {
477         wavnc_port_info *portc = (wavnc_port_info *) audio_devs[dev]->portc;
478         wavnc_info      *devc = (wavnc_info *) audio_devs[dev]->devc;
479         unsigned long   flags;
480         unsigned int    count = __count;
481
482         if (debug_flg & DEBUG_IN)
483                 printk("waveartist: start input, buf=0x%lx, count=0x%x...\n",
484                         buf, count);
485
486         if (portc->audio_format & (AFMT_S16_LE | AFMT_S16_BE))  /* 16 bit data */
487                 count >>= 1;
488
489         if (portc->channels > 1)
490                 count >>= 1;
491
492         count -= 1;
493
494         if (devc->audio_mode & PCM_ENABLE_INPUT &&
495             audio_devs[dev]->flags & DMA_AUTOMODE &&
496             intrflag &&
497             count == devc->xfer_count) {
498                 devc->audio_mode |= PCM_ENABLE_INPUT;
499                 return; /*
500                          * Auto DMA mode on. No need to react
501                          */
502         }
503
504         spin_lock_irqsave(&waveartist_lock, flags);
505
506         /*
507          * set sample count
508          */
509         waveartist_cmd2(devc, WACMD_INPUTSIZE, count);
510
511         devc->xfer_count = count;
512         devc->audio_mode |= PCM_ENABLE_INPUT;
513
514         spin_unlock_irqrestore(&waveartist_lock, flags);
515 }
516
517 static int
518 waveartist_ioctl(int dev, unsigned int cmd, void __user * arg)
519 {
520         return -EINVAL;
521 }
522
523 static unsigned int
524 waveartist_get_speed(wavnc_port_info *portc)
525 {
526         unsigned int speed;
527
528         /*
529          * program the speed, channels, bits
530          */
531         if (portc->speed == 8000)
532                 speed = 0x2E71;
533         else if (portc->speed == 11025)
534                 speed = 0x4000;
535         else if (portc->speed == 22050)
536                 speed = 0x8000;
537         else if (portc->speed == 44100)
538                 speed = 0x0;
539         else {
540                 /*
541                  * non-standard - just calculate
542                  */
543                 speed = portc->speed << 16;
544
545                 speed = (speed / 44100) & 65535;
546         }
547
548         return speed;
549 }
550
551 static unsigned int
552 waveartist_get_bits(wavnc_port_info *portc)
553 {
554         unsigned int bits;
555
556         if (portc->audio_format == AFMT_S16_LE)
557                 bits = 1;
558         else if (portc->audio_format == AFMT_S8)
559                 bits = 0;
560         else
561                 bits = 2;       //default AFMT_U8
562
563         return bits;
564 }
565
566 static int
567 waveartist_prepare_for_input(int dev, int bsize, int bcount)
568 {
569         unsigned long   flags;
570         wavnc_info      *devc = (wavnc_info *) audio_devs[dev]->devc;
571         wavnc_port_info *portc = (wavnc_port_info *) audio_devs[dev]->portc;
572         unsigned int    speed, bits;
573
574         if (devc->audio_mode)
575                 return 0;
576
577         speed = waveartist_get_speed(portc);
578         bits  = waveartist_get_bits(portc);
579
580         spin_lock_irqsave(&waveartist_lock, flags);
581
582         if (waveartist_cmd2(devc, WACMD_INPUTFORMAT, bits))
583                 printk(KERN_WARNING "waveartist: error setting the "
584                        "record format to %d\n", portc->audio_format);
585
586         if (waveartist_cmd2(devc, WACMD_INPUTCHANNELS, portc->channels))
587                 printk(KERN_WARNING "waveartist: error setting record "
588                        "to %d channels\n", portc->channels);
589
590         /*
591          * write cmd SetSampleSpeedTimeConstant
592          */
593         if (waveartist_cmd2(devc, WACMD_INPUTSPEED, speed))
594                 printk(KERN_WARNING "waveartist: error setting the record "
595                        "speed to %dHz.\n", portc->speed);
596
597         if (waveartist_cmd2(devc, WACMD_INPUTDMA, 1))
598                 printk(KERN_WARNING "waveartist: error setting the record "
599                        "data path to 0x%X\n", 1);
600
601         if (waveartist_cmd2(devc, WACMD_INPUTFORMAT, bits))
602                 printk(KERN_WARNING "waveartist: error setting the record "
603                        "format to %d\n", portc->audio_format);
604
605         devc->xfer_count = 0;
606         spin_unlock_irqrestore(&waveartist_lock, flags);
607         waveartist_halt_input(dev);
608
609         if (debug_flg & DEBUG_INTR) {
610                 printk("WA CTLR reg: 0x%02X.\n",
611                        inb(devc->hw.io_base + CTLR));
612                 printk("WA STAT reg: 0x%02X.\n",
613                        inb(devc->hw.io_base + STATR));
614                 printk("WA IRQS reg: 0x%02X.\n",
615                        inb(devc->hw.io_base + IRQSTAT));
616         }
617
618         return 0;
619 }
620
621 static int
622 waveartist_prepare_for_output(int dev, int bsize, int bcount)
623 {
624         unsigned long   flags;
625         wavnc_info      *devc = (wavnc_info *) audio_devs[dev]->devc;
626         wavnc_port_info *portc = (wavnc_port_info *) audio_devs[dev]->portc;
627         unsigned int    speed, bits;
628
629         /*
630          * program the speed, channels, bits
631          */
632         speed = waveartist_get_speed(portc);
633         bits  = waveartist_get_bits(portc);
634
635         spin_lock_irqsave(&waveartist_lock, flags);
636
637         if (waveartist_cmd2(devc, WACMD_OUTPUTSPEED, speed) &&
638             waveartist_cmd2(devc, WACMD_OUTPUTSPEED, speed))
639                 printk(KERN_WARNING "waveartist: error setting the playback "
640                        "speed to %dHz.\n", portc->speed);
641
642         if (waveartist_cmd2(devc, WACMD_OUTPUTCHANNELS, portc->channels))
643                 printk(KERN_WARNING "waveartist: error setting the playback "
644                        "to %d channels\n", portc->channels);
645
646         if (waveartist_cmd2(devc, WACMD_OUTPUTDMA, 0))
647                 printk(KERN_WARNING "waveartist: error setting the playback "
648                        "data path to 0x%X\n", 0);
649
650         if (waveartist_cmd2(devc, WACMD_OUTPUTFORMAT, bits))
651                 printk(KERN_WARNING "waveartist: error setting the playback "
652                        "format to %d\n", portc->audio_format);
653
654         devc->xfer_count = 0;
655         spin_unlock_irqrestore(&waveartist_lock, flags);
656         waveartist_halt_output(dev);
657
658         if (debug_flg & DEBUG_INTR) {
659                 printk("WA CTLR reg: 0x%02X.\n",inb(devc->hw.io_base + CTLR));
660                 printk("WA STAT reg: 0x%02X.\n",inb(devc->hw.io_base + STATR));
661                 printk("WA IRQS reg: 0x%02X.\n",inb(devc->hw.io_base + IRQSTAT));
662         }
663
664         return 0;
665 }
666
667 static void
668 waveartist_halt(int dev)
669 {
670         wavnc_port_info *portc = (wavnc_port_info *) audio_devs[dev]->portc;
671         wavnc_info      *devc;
672
673         if (portc->open_mode & OPEN_WRITE)
674                 waveartist_halt_output(dev);
675
676         if (portc->open_mode & OPEN_READ)
677                 waveartist_halt_input(dev);
678
679         devc = (wavnc_info *) audio_devs[dev]->devc;
680         devc->audio_mode = 0;
681 }
682
683 static void
684 waveartist_halt_input(int dev)
685 {
686         wavnc_info      *devc = (wavnc_info *) audio_devs[dev]->devc;
687         unsigned long   flags;
688
689         spin_lock_irqsave(&waveartist_lock, flags);
690
691         /*
692          * Stop capture
693          */
694         waveartist_cmd1(devc, WACMD_INPUTSTOP);
695
696         devc->audio_mode &= ~PCM_ENABLE_INPUT;
697
698         /*
699          * Clear interrupt by toggling
700          * the IRQ_ACK bit in CTRL
701          */
702         if (inb(devc->hw.io_base + STATR) & IRQ_REQ)
703                 waveartist_iack(devc);
704
705 //      devc->audio_mode &= ~PCM_ENABLE_INPUT;
706
707         spin_unlock_irqrestore(&waveartist_lock, flags);
708 }
709
710 static void
711 waveartist_halt_output(int dev)
712 {
713         wavnc_info      *devc = (wavnc_info *) audio_devs[dev]->devc;
714         unsigned long   flags;
715
716         spin_lock_irqsave(&waveartist_lock, flags);
717
718         waveartist_cmd1(devc, WACMD_OUTPUTSTOP);
719
720         devc->audio_mode &= ~PCM_ENABLE_OUTPUT;
721
722         /*
723          * Clear interrupt by toggling
724          * the IRQ_ACK bit in CTRL
725          */
726         if (inb(devc->hw.io_base + STATR) & IRQ_REQ)
727                 waveartist_iack(devc);
728
729 //      devc->audio_mode &= ~PCM_ENABLE_OUTPUT;
730
731         spin_unlock_irqrestore(&waveartist_lock, flags);
732 }
733
734 static void
735 waveartist_trigger(int dev, int state)
736 {
737         wavnc_info      *devc = (wavnc_info *) audio_devs[dev]->devc;
738         wavnc_port_info *portc = (wavnc_port_info *) audio_devs[dev]->portc;
739         unsigned long   flags;
740
741         if (debug_flg & DEBUG_TRIGGER) {
742                 printk("wavnc: audio trigger ");
743                 if (state & PCM_ENABLE_INPUT)
744                         printk("in ");
745                 if (state & PCM_ENABLE_OUTPUT)
746                         printk("out");
747                 printk("\n");
748         }
749
750         spin_lock_irqsave(&waveartist_lock, flags);
751
752         state &= devc->audio_mode;
753
754         if (portc->open_mode & OPEN_READ &&
755             state & PCM_ENABLE_INPUT)
756                 /*
757                  * enable ADC Data Transfer to PC
758                  */
759                 waveartist_cmd1(devc, WACMD_INPUTSTART);
760
761         if (portc->open_mode & OPEN_WRITE &&
762             state & PCM_ENABLE_OUTPUT)
763                 /*
764                  * enable DAC data transfer from PC
765                  */
766                 waveartist_cmd1(devc, WACMD_OUTPUTSTART);
767
768         spin_unlock_irqrestore(&waveartist_lock, flags);
769 }
770
771 static int
772 waveartist_set_speed(int dev, int arg)
773 {
774         wavnc_port_info *portc = (wavnc_port_info *) audio_devs[dev]->portc;
775
776         if (arg <= 0)
777                 return portc->speed;
778
779         if (arg < 5000)
780                 arg = 5000;
781         if (arg > 44100)
782                 arg = 44100;
783
784         portc->speed = arg;
785         return portc->speed;
786
787 }
788
789 static short
790 waveartist_set_channels(int dev, short arg)
791 {
792         wavnc_port_info *portc = (wavnc_port_info *) audio_devs[dev]->portc;
793
794         if (arg != 1 && arg != 2)
795                 return portc->channels;
796
797         portc->channels = arg;
798         return arg;
799 }
800
801 static unsigned int
802 waveartist_set_bits(int dev, unsigned int arg)
803 {
804         wavnc_port_info *portc = (wavnc_port_info *) audio_devs[dev]->portc;
805
806         if (arg == 0)
807                 return portc->audio_format;
808
809         if ((arg != AFMT_U8) && (arg != AFMT_S16_LE) && (arg != AFMT_S8))
810                 arg = AFMT_U8;
811
812         portc->audio_format = arg;
813
814         return arg;
815 }
816
817 static struct audio_driver waveartist_audio_driver = {
818         .owner                  = THIS_MODULE,
819         .open                   = waveartist_open,
820         .close                  = waveartist_close,
821         .output_block           = waveartist_output_block,
822         .start_input            = waveartist_start_input,
823         .ioctl                  = waveartist_ioctl,
824         .prepare_for_input      = waveartist_prepare_for_input,
825         .prepare_for_output     = waveartist_prepare_for_output,
826         .halt_io                = waveartist_halt,
827         .halt_input             = waveartist_halt_input,
828         .halt_output            = waveartist_halt_output,
829         .trigger                = waveartist_trigger,
830         .set_speed              = waveartist_set_speed,
831         .set_bits               = waveartist_set_bits,
832         .set_channels           = waveartist_set_channels
833 };
834
835
836 static irqreturn_t
837 waveartist_intr(int irq, void *dev_id, struct pt_regs *regs)
838 {
839         wavnc_info *devc = (wavnc_info *)dev_id;
840         int        irqstatus, status;
841
842         spin_lock(&waveartist_lock);
843         irqstatus = inb(devc->hw.io_base + IRQSTAT);
844         status    = inb(devc->hw.io_base + STATR);
845
846         if (debug_flg & DEBUG_INTR)
847                 printk("waveartist_intr: stat=%02x, irqstat=%02x\n",
848                        status, irqstatus);
849
850         if (status & IRQ_REQ)   /* Clear interrupt */
851                 waveartist_iack(devc);
852         else
853                 printk(KERN_WARNING "waveartist: unexpected interrupt\n");
854
855         if (irqstatus & 0x01) {
856                 int temp = 1;
857
858                 /* PCM buffer done
859                  */
860                 if ((status & DMA0) && (devc->audio_mode & PCM_ENABLE_OUTPUT)) {
861                         DMAbuf_outputintr(devc->playback_dev, 1);
862                         temp = 0;
863                 }
864                 if ((status & DMA1) && (devc->audio_mode & PCM_ENABLE_INPUT)) {
865                         DMAbuf_inputintr(devc->record_dev);
866                         temp = 0;
867                 }
868                 if (temp)       //default:
869                         printk(KERN_WARNING "waveartist: Unknown interrupt\n");
870         }
871         if (irqstatus & 0x2)
872                 // We do not use SB mode natively...
873                 printk(KERN_WARNING "waveartist: Unexpected SB interrupt...\n");
874         spin_unlock(&waveartist_lock);
875         return IRQ_HANDLED;
876 }
877
878 /* -------------------------------------------------------------------------
879  * Mixer stuff
880  */
881 struct mix_ent {
882         unsigned char   reg_l;
883         unsigned char   reg_r;
884         unsigned char   shift;
885         unsigned char   max;
886 };
887
888 static const struct mix_ent mix_devs[SOUND_MIXER_NRDEVICES] = {
889         { 2, 6, 1,  7 }, /* SOUND_MIXER_VOLUME   */
890         { 0, 0, 0,  0 }, /* SOUND_MIXER_BASS     */
891         { 0, 0, 0,  0 }, /* SOUND_MIXER_TREBLE   */
892         { 0, 0, 0,  0 }, /* SOUND_MIXER_SYNTH    */
893         { 0, 0, 0,  0 }, /* SOUND_MIXER_PCM      */
894         { 0, 0, 0,  0 }, /* SOUND_MIXER_SPEAKER  */
895         { 0, 4, 6, 31 }, /* SOUND_MIXER_LINE     */
896         { 2, 6, 4,  3 }, /* SOUND_MIXER_MIC      */
897         { 0, 0, 0,  0 }, /* SOUND_MIXER_CD       */
898         { 0, 0, 0,  0 }, /* SOUND_MIXER_IMIX     */
899         { 0, 0, 0,  0 }, /* SOUND_MIXER_ALTPCM   */
900 #if 0
901         { 3, 7, 0, 10 }, /* SOUND_MIXER_RECLEV   */
902         { 0, 0, 0,  0 }, /* SOUND_MIXER_IGAIN    */
903 #else
904         { 0, 0, 0,  0 }, /* SOUND_MIXER_RECLEV   */
905         { 3, 7, 0,  7 }, /* SOUND_MIXER_IGAIN    */
906 #endif
907         { 0, 0, 0,  0 }, /* SOUND_MIXER_OGAIN    */
908         { 0, 4, 1, 31 }, /* SOUND_MIXER_LINE1    */
909         { 1, 5, 6, 31 }, /* SOUND_MIXER_LINE2    */
910         { 0, 0, 0,  0 }, /* SOUND_MIXER_LINE3    */
911         { 0, 0, 0,  0 }, /* SOUND_MIXER_DIGITAL1 */
912         { 0, 0, 0,  0 }, /* SOUND_MIXER_DIGITAL2 */
913         { 0, 0, 0,  0 }, /* SOUND_MIXER_DIGITAL3 */
914         { 0, 0, 0,  0 }, /* SOUND_MIXER_PHONEIN  */
915         { 0, 0, 0,  0 }, /* SOUND_MIXER_PHONEOUT */
916         { 0, 0, 0,  0 }, /* SOUND_MIXER_VIDEO    */
917         { 0, 0, 0,  0 }, /* SOUND_MIXER_RADIO    */
918         { 0, 0, 0,  0 }  /* SOUND_MIXER_MONITOR  */
919 };
920
921 static void
922 waveartist_mixer_update(wavnc_info *devc, int whichDev)
923 {
924         unsigned int lev_left, lev_right;
925
926         lev_left  = devc->levels[whichDev] & 0xff;
927         lev_right = devc->levels[whichDev] >> 8;
928
929         if (lev_left > 100)
930                 lev_left = 100;
931         if (lev_right > 100)
932                 lev_right = 100;
933
934 #define SCALE(lev,max)  ((lev) * (max) / 100)
935
936         if (machine_is_netwinder() && whichDev == SOUND_MIXER_PHONEOUT)
937                 whichDev = SOUND_MIXER_VOLUME;
938
939         if (mix_devs[whichDev].reg_l || mix_devs[whichDev].reg_r) {
940                 const struct mix_ent *mix = mix_devs + whichDev;
941                 unsigned int mask, left, right;
942
943                 mask = mix->max << mix->shift;
944                 lev_left  = SCALE(lev_left,  mix->max) << mix->shift;
945                 lev_right = SCALE(lev_right, mix->max) << mix->shift;
946
947                 /* read left setting */
948                 left  = waveartist_cmd1_r(devc, WACMD_GET_LEVEL |
949                                                mix->reg_l << 8);
950
951                 /* read right setting */
952                 right = waveartist_cmd1_r(devc, WACMD_GET_LEVEL |
953                                                 mix->reg_r << 8);
954
955                 left  = (left  & ~mask) | (lev_left  & mask);
956                 right = (right & ~mask) | (lev_right & mask);
957
958                 /* write left,right back */
959                 waveartist_cmd3(devc, WACMD_SET_MIXER, left, right);
960         } else {
961                 switch(whichDev) {
962                 case SOUND_MIXER_PCM:
963                         waveartist_cmd3(devc, WACMD_SET_LEVEL,
964                                         SCALE(lev_left,  32767),
965                                         SCALE(lev_right, 32767));
966                         break;
967
968                 case SOUND_MIXER_SYNTH:
969                         waveartist_cmd3(devc, 0x0100 | WACMD_SET_LEVEL,
970                                         SCALE(lev_left,  32767),
971                                         SCALE(lev_right, 32767));
972                         break;
973                 }
974         }
975 }
976
977 /*
978  * Set the ADC MUX to the specified values.  We do NOT do any
979  * checking of the values passed, since we assume that the
980  * relevant *_select_input function has done that for us.
981  */
982 static void
983 waveartist_set_adc_mux(wavnc_info *devc, char left_dev, char right_dev)
984 {
985         unsigned int reg_08, reg_09;
986
987         reg_08 = waveartist_cmd1_r(devc, WACMD_GET_LEVEL | 0x0800);
988         reg_09 = waveartist_cmd1_r(devc, WACMD_GET_LEVEL | 0x0900);
989
990         reg_08 = (reg_08 & ~0x3f) | right_dev << 3 | left_dev;
991
992         waveartist_cmd3(devc, WACMD_SET_MIXER, reg_08, reg_09);
993 }
994
995 /*
996  * Decode a recording mask into a mixer selection as follows:
997  *
998  *     OSS Source       WA Source       Actual source
999  *  SOUND_MASK_IMIX     Mixer           Mixer output (same as AD1848)
1000  *  SOUND_MASK_LINE     Line            Line in
1001  *  SOUND_MASK_LINE1    Aux 1           Aux 1 in
1002  *  SOUND_MASK_LINE2    Aux 2           Aux 2 in
1003  *  SOUND_MASK_MIC      Mic             Microphone
1004  */
1005 static unsigned int
1006 waveartist_select_input(wavnc_info *devc, unsigned int recmask,
1007                         unsigned char *dev_l, unsigned char *dev_r)
1008 {
1009         unsigned int recdev = ADC_MUX_NONE;
1010
1011         if (recmask & SOUND_MASK_IMIX) {
1012                 recmask = SOUND_MASK_IMIX;
1013                 recdev = ADC_MUX_MIXER;
1014         } else if (recmask & SOUND_MASK_LINE2) {
1015                 recmask = SOUND_MASK_LINE2;
1016                 recdev = ADC_MUX_AUX2;
1017         } else if (recmask & SOUND_MASK_LINE1) {
1018                 recmask = SOUND_MASK_LINE1;
1019                 recdev = ADC_MUX_AUX1;
1020         } else if (recmask & SOUND_MASK_LINE) {
1021                 recmask = SOUND_MASK_LINE;
1022                 recdev = ADC_MUX_LINE;
1023         } else if (recmask & SOUND_MASK_MIC) {
1024                 recmask = SOUND_MASK_MIC;
1025                 recdev = ADC_MUX_MIC;
1026         }
1027
1028         *dev_l = *dev_r = recdev;
1029
1030         return recmask;
1031 }
1032
1033 static int
1034 waveartist_decode_mixer(wavnc_info *devc, int dev, unsigned char lev_l,
1035                         unsigned char lev_r)
1036 {
1037         switch (dev) {
1038         case SOUND_MIXER_VOLUME:
1039         case SOUND_MIXER_SYNTH:
1040         case SOUND_MIXER_PCM:
1041         case SOUND_MIXER_LINE:
1042         case SOUND_MIXER_MIC:
1043         case SOUND_MIXER_IGAIN:
1044         case SOUND_MIXER_LINE1:
1045         case SOUND_MIXER_LINE2:
1046                 devc->levels[dev] = lev_l | lev_r << 8;
1047                 break;
1048
1049         case SOUND_MIXER_IMIX:
1050                 break;
1051
1052         default:
1053                 dev = -EINVAL;
1054                 break;
1055         }
1056
1057         return dev;
1058 }
1059
1060 static int waveartist_get_mixer(wavnc_info *devc, int dev)
1061 {
1062         return devc->levels[dev];
1063 }
1064
1065 static const struct waveartist_mixer_info waveartist_mixer = {
1066         .supported_devs = SUPPORTED_MIXER_DEVICES | SOUND_MASK_IGAIN,
1067         .recording_devs = SOUND_MASK_LINE  | SOUND_MASK_MIC   |
1068                         SOUND_MASK_LINE1 | SOUND_MASK_LINE2 |
1069                         SOUND_MASK_IMIX,
1070         .stereo_devs    = (SUPPORTED_MIXER_DEVICES | SOUND_MASK_IGAIN) & ~
1071                         (SOUND_MASK_SPEAKER | SOUND_MASK_IMIX),
1072         .select_input   = waveartist_select_input,
1073         .decode_mixer   = waveartist_decode_mixer,
1074         .get_mixer      = waveartist_get_mixer,
1075 };
1076
1077 static void
1078 waveartist_set_recmask(wavnc_info *devc, unsigned int recmask)
1079 {
1080         unsigned char dev_l, dev_r;
1081
1082         recmask &= devc->mix->recording_devs;
1083
1084         /*
1085          * If more than one recording device selected,
1086          * disable the device that is currently in use.
1087          */
1088         if (hweight32(recmask) > 1)
1089                 recmask &= ~devc->recmask;
1090
1091         /*
1092          * Translate the recording device mask into
1093          * the ADC multiplexer settings.
1094          */
1095         devc->recmask = devc->mix->select_input(devc, recmask,
1096                                                 &dev_l, &dev_r);
1097
1098         waveartist_set_adc_mux(devc, dev_l, dev_r);
1099 }
1100
1101 static int
1102 waveartist_set_mixer(wavnc_info *devc, int dev, unsigned int level)
1103 {
1104         unsigned int lev_left  = level & 0x00ff;
1105         unsigned int lev_right = (level & 0xff00) >> 8;
1106
1107         if (lev_left > 100)
1108                 lev_left = 100;
1109         if (lev_right > 100)
1110                 lev_right = 100;
1111
1112         /*
1113          * Mono devices have their right volume forced to their
1114          * left volume.  (from ALSA driver OSS emulation).
1115          */
1116         if (!(devc->mix->stereo_devs & (1 << dev)))
1117                 lev_right = lev_left;
1118
1119         dev = devc->mix->decode_mixer(devc, dev, lev_left, lev_right);
1120
1121         if (dev >= 0)
1122                 waveartist_mixer_update(devc, dev);
1123
1124         return dev < 0 ? dev : 0;
1125 }
1126
1127 static int
1128 waveartist_mixer_ioctl(int dev, unsigned int cmd, void __user * arg)
1129 {
1130         wavnc_info *devc = (wavnc_info *)audio_devs[dev]->devc;
1131         int ret = 0, val, nr;
1132
1133         /*
1134          * All SOUND_MIXER_* ioctls use type 'M'
1135          */
1136         if (((cmd >> 8) & 255) != 'M')
1137                 return -ENOIOCTLCMD;
1138
1139 #ifdef CONFIG_ARCH_NETWINDER
1140         if (machine_is_netwinder()) {
1141                 ret = vnc_private_ioctl(dev, cmd, arg);
1142                 if (ret != -ENOIOCTLCMD)
1143                         return ret;
1144                 else
1145                         ret = 0;
1146         }
1147 #endif
1148
1149         nr = cmd & 0xff;
1150
1151         if (_SIOC_DIR(cmd) & _SIOC_WRITE) {
1152                 if (get_user(val, (int __user *)arg))
1153                         return -EFAULT;
1154
1155                 switch (nr) {
1156                 case SOUND_MIXER_RECSRC:
1157                         waveartist_set_recmask(devc, val);
1158                         break;
1159
1160                 default:
1161                         ret = -EINVAL;
1162                         if (nr < SOUND_MIXER_NRDEVICES &&
1163                             devc->mix->supported_devs & (1 << nr))
1164                                 ret = waveartist_set_mixer(devc, nr, val);
1165                 }
1166         }
1167
1168         if (ret == 0 && _SIOC_DIR(cmd) & _SIOC_READ) {
1169                 ret = -EINVAL;
1170
1171                 switch (nr) {
1172                 case SOUND_MIXER_RECSRC:
1173                         ret = devc->recmask;
1174                         break;
1175
1176                 case SOUND_MIXER_DEVMASK:
1177                         ret = devc->mix->supported_devs;
1178                         break;
1179
1180                 case SOUND_MIXER_STEREODEVS:
1181                         ret = devc->mix->stereo_devs;
1182                         break;
1183
1184                 case SOUND_MIXER_RECMASK:
1185                         ret = devc->mix->recording_devs;
1186                         break;
1187
1188                 case SOUND_MIXER_CAPS:
1189                         ret = SOUND_CAP_EXCL_INPUT;
1190                         break;
1191
1192                 default:
1193                         if (nr < SOUND_MIXER_NRDEVICES)
1194                                 ret = devc->mix->get_mixer(devc, nr);
1195                         break;
1196                 }
1197
1198                 if (ret >= 0)
1199                         ret = put_user(ret, (int __user *)arg) ? -EFAULT : 0;
1200         }
1201
1202         return ret;
1203 }
1204
1205 static struct mixer_operations waveartist_mixer_operations =
1206 {
1207         .owner  = THIS_MODULE,
1208         .id     = "WaveArtist",
1209         .name   = "WaveArtist",
1210         .ioctl  = waveartist_mixer_ioctl
1211 };
1212
1213 static void
1214 waveartist_mixer_reset(wavnc_info *devc)
1215 {
1216         int i;
1217
1218         if (debug_flg & DEBUG_MIXER)
1219                 printk("%s: mixer_reset\n", devc->hw.name);
1220
1221         /*
1222          * reset mixer cmd
1223          */
1224         waveartist_cmd1(devc, WACMD_RST_MIXER);
1225
1226         /*
1227          * set input for ADC to come from 'quiet'
1228          * turn on default modes
1229          */
1230         waveartist_cmd3(devc, WACMD_SET_MIXER, 0x9800, 0xa836);
1231
1232         /*
1233          * set mixer input select to none, RX filter gains 0 dB
1234          */
1235         waveartist_cmd3(devc, WACMD_SET_MIXER, 0x4c00, 0x8c00);
1236
1237         /*
1238          * set bit 0 reg 2 to 1 - unmute MonoOut
1239          */
1240         waveartist_cmd3(devc, WACMD_SET_MIXER, 0x2801, 0x6800);
1241
1242         /* set default input device = internal mic
1243          * current recording device = none
1244          */
1245         waveartist_set_recmask(devc, 0);
1246
1247         for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
1248                 waveartist_mixer_update(devc, i);
1249 }
1250
1251 static int __init waveartist_init(wavnc_info *devc)
1252 {
1253         wavnc_port_info *portc;
1254         char rev[3], dev_name[64];
1255         int my_dev;
1256
1257         if (waveartist_reset(devc))
1258                 return -ENODEV;
1259
1260         sprintf(dev_name, "%s (%s", devc->hw.name, devc->chip_name);
1261
1262         if (waveartist_getrev(devc, rev)) {
1263                 strcat(dev_name, " rev. ");
1264                 strcat(dev_name, rev);
1265         }
1266         strcat(dev_name, ")");
1267
1268         conf_printf2(dev_name, devc->hw.io_base, devc->hw.irq,
1269                      devc->hw.dma, devc->hw.dma2);
1270
1271         portc = (wavnc_port_info *)kmalloc(sizeof(wavnc_port_info), GFP_KERNEL);
1272         if (portc == NULL)
1273                 goto nomem;
1274
1275         memset(portc, 0, sizeof(wavnc_port_info));
1276
1277         my_dev = sound_install_audiodrv(AUDIO_DRIVER_VERSION, dev_name,
1278                         &waveartist_audio_driver, sizeof(struct audio_driver),
1279                         devc->audio_flags, AFMT_U8 | AFMT_S16_LE | AFMT_S8,
1280                         devc, devc->hw.dma, devc->hw.dma2);
1281
1282         if (my_dev < 0)
1283                 goto free;
1284
1285         audio_devs[my_dev]->portc = portc;
1286
1287         waveartist_mixer_reset(devc);
1288
1289         /*
1290          * clear any pending interrupt
1291          */
1292         waveartist_iack(devc);
1293
1294         if (request_irq(devc->hw.irq, waveartist_intr, 0, devc->hw.name, devc) < 0) {
1295                 printk(KERN_ERR "%s: IRQ %d in use\n",
1296                         devc->hw.name, devc->hw.irq);
1297                 goto uninstall;
1298         }
1299
1300         if (sound_alloc_dma(devc->hw.dma, devc->hw.name)) {
1301                 printk(KERN_ERR "%s: Can't allocate DMA%d\n",
1302                         devc->hw.name, devc->hw.dma);
1303                 goto uninstall_irq;
1304         }
1305
1306         if (devc->hw.dma != devc->hw.dma2 && devc->hw.dma2 != NO_DMA)
1307                 if (sound_alloc_dma(devc->hw.dma2, devc->hw.name)) {
1308                         printk(KERN_ERR "%s: can't allocate DMA%d\n",
1309                                 devc->hw.name, devc->hw.dma2);
1310                         goto uninstall_dma;
1311                 }
1312
1313         waveartist_set_ctlr(&devc->hw, 0, DMA1_IE | DMA0_IE);
1314
1315         audio_devs[my_dev]->mixer_dev =
1316                 sound_install_mixer(MIXER_DRIVER_VERSION,
1317                                 dev_name,
1318                                 &waveartist_mixer_operations,
1319                                 sizeof(struct mixer_operations),
1320                                 devc);
1321
1322         return my_dev;
1323
1324 uninstall_dma:
1325         sound_free_dma(devc->hw.dma);
1326
1327 uninstall_irq:
1328         free_irq(devc->hw.irq, devc);
1329
1330 uninstall:
1331         sound_unload_audiodev(my_dev);
1332
1333 free:
1334         kfree(portc);
1335
1336 nomem:
1337         return -1;
1338 }
1339
1340 static int __init probe_waveartist(struct address_info *hw_config)
1341 {
1342         wavnc_info *devc = &adev_info[nr_waveartist_devs];
1343
1344         if (nr_waveartist_devs >= MAX_AUDIO_DEV) {
1345                 printk(KERN_WARNING "waveartist: too many audio devices\n");
1346                 return 0;
1347         }
1348
1349         if (!request_region(hw_config->io_base, 15, hw_config->name))  {
1350                 printk(KERN_WARNING "WaveArtist: I/O port conflict\n");
1351                 return 0;
1352         }
1353
1354         if (hw_config->irq > 15 || hw_config->irq < 0) {
1355                 release_region(hw_config->io_base, 15);
1356                 printk(KERN_WARNING "WaveArtist: Bad IRQ %d\n",
1357                        hw_config->irq);
1358                 return 0;
1359         }
1360
1361         if (hw_config->dma != 3) {
1362                 release_region(hw_config->io_base, 15);
1363                 printk(KERN_WARNING "WaveArtist: Bad DMA %d\n",
1364                        hw_config->dma);
1365                 return 0;
1366         }
1367
1368         hw_config->name = "WaveArtist";
1369         devc->hw = *hw_config;
1370         devc->open_mode = 0;
1371         devc->chip_name = "RWA-010";
1372
1373         return 1;
1374 }
1375
1376 static void __init
1377 attach_waveartist(struct address_info *hw, const struct waveartist_mixer_info *mix)
1378 {
1379         wavnc_info *devc = &adev_info[nr_waveartist_devs];
1380
1381         /*
1382          * NOTE! If irq < 0, there is another driver which has allocated the
1383          *   IRQ so that this driver doesn't need to allocate/deallocate it.
1384          *   The actually used IRQ is ABS(irq).
1385          */
1386         devc->hw = *hw;
1387         devc->hw.irq = (hw->irq > 0) ? hw->irq : 0;
1388         devc->open_mode = 0;
1389         devc->playback_dev = 0;
1390         devc->record_dev = 0;
1391         devc->audio_flags = DMA_AUTOMODE;
1392         devc->levels = levels;
1393
1394         if (hw->dma != hw->dma2 && hw->dma2 != NO_DMA)
1395                 devc->audio_flags |= DMA_DUPLEX;
1396
1397         devc->mix = mix;
1398         devc->dev_no = waveartist_init(devc);
1399
1400         if (devc->dev_no < 0)
1401                 release_region(hw->io_base, 15);
1402         else {
1403 #ifdef CONFIG_ARCH_NETWINDER
1404                 if (machine_is_netwinder()) {
1405                         init_timer(&vnc_timer);
1406                         vnc_timer.function = vnc_slider_tick;
1407                         vnc_timer.expires  = jiffies;
1408                         vnc_timer.data     = nr_waveartist_devs;
1409                         add_timer(&vnc_timer);
1410
1411                         vnc_configure_mixer(devc, 0);
1412
1413                         devc->no_autoselect = 1;
1414                 }
1415 #endif
1416                 nr_waveartist_devs += 1;
1417         }
1418 }
1419
1420 static void __exit unload_waveartist(struct address_info *hw)
1421 {
1422         wavnc_info *devc = NULL;
1423         int i;
1424
1425         for (i = 0; i < nr_waveartist_devs; i++)
1426                 if (hw->io_base == adev_info[i].hw.io_base) {
1427                         devc = adev_info + i;
1428                         break;
1429                 }
1430
1431         if (devc != NULL) {
1432                 int mixer;
1433
1434 #ifdef CONFIG_ARCH_NETWINDER
1435                 if (machine_is_netwinder())
1436                         del_timer(&vnc_timer);
1437 #endif
1438
1439                 release_region(devc->hw.io_base, 15);
1440
1441                 waveartist_set_ctlr(&devc->hw, DMA1_IE|DMA0_IE, 0);
1442
1443                 if (devc->hw.irq >= 0)
1444                         free_irq(devc->hw.irq, devc);
1445
1446                 sound_free_dma(devc->hw.dma);
1447
1448                 if (devc->hw.dma != devc->hw.dma2 &&
1449                     devc->hw.dma2 != NO_DMA)
1450                         sound_free_dma(devc->hw.dma2);
1451
1452                 mixer = audio_devs[devc->dev_no]->mixer_dev;
1453
1454                 if (mixer >= 0)
1455                         sound_unload_mixerdev(mixer);
1456
1457                 if (devc->dev_no >= 0)
1458                         sound_unload_audiodev(devc->dev_no);
1459
1460                 nr_waveartist_devs -= 1;
1461
1462                 for (; i < nr_waveartist_devs; i++)
1463                         adev_info[i] = adev_info[i + 1];
1464         } else
1465                 printk(KERN_WARNING "waveartist: can't find device "
1466                        "to unload\n");
1467 }
1468
1469 #ifdef CONFIG_ARCH_NETWINDER
1470
1471 /*
1472  * Rebel.com Netwinder specifics...
1473  */
1474
1475 #include <asm/hardware/dec21285.h>
1476  
1477 #define VNC_TIMER_PERIOD (HZ/4) //check slider 4 times/sec
1478
1479 #define MIXER_PRIVATE3_RESET    0x53570000
1480 #define MIXER_PRIVATE3_READ     0x53570001
1481 #define MIXER_PRIVATE3_WRITE    0x53570002
1482
1483 #define VNC_MUTE_INTERNAL_SPKR  0x01    //the sw mute on/off control bit
1484 #define VNC_MUTE_LINE_OUT       0x10
1485 #define VNC_PHONE_DETECT        0x20
1486 #define VNC_HANDSET_DETECT      0x40
1487 #define VNC_DISABLE_AUTOSWITCH  0x80
1488
1489 extern spinlock_t gpio_lock;
1490
1491 static inline void
1492 vnc_mute_spkr(wavnc_info *devc)
1493 {
1494         unsigned long flags;
1495
1496         spin_lock_irqsave(&gpio_lock, flags);
1497         cpld_modify(CPLD_UNMUTE, devc->spkr_mute_state ? 0 : CPLD_UNMUTE);
1498         spin_unlock_irqrestore(&gpio_lock, flags);
1499 }
1500
1501 static void
1502 vnc_mute_lout(wavnc_info *devc)
1503 {
1504         unsigned int left, right;
1505
1506         left  = waveartist_cmd1_r(devc, WACMD_GET_LEVEL);
1507         right = waveartist_cmd1_r(devc, WACMD_GET_LEVEL | 0x400);
1508
1509         if (devc->line_mute_state) {
1510                 left &= ~1;
1511                 right &= ~1;
1512         } else {
1513                 left |= 1;
1514                 right |= 1;
1515         }
1516         waveartist_cmd3(devc, WACMD_SET_MIXER, left, right);
1517                 
1518 }
1519
1520 static int
1521 vnc_volume_slider(wavnc_info *devc)
1522 {
1523         static signed int old_slider_volume;
1524         unsigned long flags;
1525         signed int volume = 255;
1526
1527         *CSR_TIMER1_LOAD = 0x00ffffff;
1528
1529         spin_lock_irqsave(&waveartist_lock, flags);
1530
1531         outb(0xFF, 0x201);
1532         *CSR_TIMER1_CNTL = TIMER_CNTL_ENABLE | TIMER_CNTL_DIV1;
1533
1534         while (volume && (inb(0x201) & 0x01))
1535                 volume--;
1536
1537         *CSR_TIMER1_CNTL = 0;
1538
1539         spin_unlock_irqrestore(&waveartist_lock,flags);
1540         
1541         volume = 0x00ffffff - *CSR_TIMER1_VALUE;
1542
1543
1544 #ifndef REVERSE
1545         volume = 150 - (volume >> 5);
1546 #else
1547         volume = (volume >> 6) - 25;
1548 #endif
1549
1550         if (volume < 0)
1551                 volume = 0;
1552
1553         if (volume > 100)
1554                 volume = 100;
1555
1556         /*
1557          * slider quite often reads +-8, so debounce this random noise
1558          */
1559         if (abs(volume - old_slider_volume) > 7) {
1560                 old_slider_volume = volume;
1561
1562                 if (debug_flg & DEBUG_MIXER)
1563                         printk(KERN_DEBUG "Slider volume: %d.\n", volume);
1564         }
1565
1566         return old_slider_volume;
1567 }
1568
1569 /*
1570  * Decode a recording mask into a mixer selection on the NetWinder
1571  * as follows:
1572  *
1573  *     OSS Source       WA Source       Actual source
1574  *  SOUND_MASK_IMIX     Mixer           Mixer output (same as AD1848)
1575  *  SOUND_MASK_LINE     Line            Line in
1576  *  SOUND_MASK_LINE1    Left Mic        Handset
1577  *  SOUND_MASK_PHONEIN  Left Aux        Telephone microphone
1578  *  SOUND_MASK_MIC      Right Mic       Builtin microphone
1579  */
1580 static unsigned int
1581 netwinder_select_input(wavnc_info *devc, unsigned int recmask,
1582                        unsigned char *dev_l, unsigned char *dev_r)
1583 {
1584         unsigned int recdev_l = ADC_MUX_NONE, recdev_r = ADC_MUX_NONE;
1585
1586         if (recmask & SOUND_MASK_IMIX) {
1587                 recmask = SOUND_MASK_IMIX;
1588                 recdev_l = ADC_MUX_MIXER;
1589                 recdev_r = ADC_MUX_MIXER;
1590         } else if (recmask & SOUND_MASK_LINE) {
1591                 recmask = SOUND_MASK_LINE;
1592                 recdev_l = ADC_MUX_LINE;
1593                 recdev_r = ADC_MUX_LINE;
1594         } else if (recmask & SOUND_MASK_LINE1) {
1595                 recmask = SOUND_MASK_LINE1;
1596                 waveartist_cmd1(devc, WACMD_SET_MONO); /* left */
1597                 recdev_l = ADC_MUX_MIC;
1598                 recdev_r = ADC_MUX_NONE;
1599         } else if (recmask & SOUND_MASK_PHONEIN) {
1600                 recmask = SOUND_MASK_PHONEIN;
1601                 waveartist_cmd1(devc, WACMD_SET_MONO); /* left */
1602                 recdev_l = ADC_MUX_AUX1;
1603                 recdev_r = ADC_MUX_NONE;
1604         } else if (recmask & SOUND_MASK_MIC) {
1605                 recmask = SOUND_MASK_MIC;
1606                 waveartist_cmd1(devc, WACMD_SET_MONO | 0x100);  /* right */
1607                 recdev_l = ADC_MUX_NONE;
1608                 recdev_r = ADC_MUX_MIC;
1609         }
1610
1611         *dev_l = recdev_l;
1612         *dev_r = recdev_r;
1613
1614         return recmask;
1615 }
1616
1617 static int
1618 netwinder_decode_mixer(wavnc_info *devc, int dev, unsigned char lev_l,
1619                        unsigned char lev_r)
1620 {
1621         switch (dev) {
1622         case SOUND_MIXER_VOLUME:
1623         case SOUND_MIXER_SYNTH:
1624         case SOUND_MIXER_PCM:
1625         case SOUND_MIXER_LINE:
1626         case SOUND_MIXER_IGAIN:
1627                 devc->levels[dev] = lev_l | lev_r << 8;
1628                 break;
1629
1630         case SOUND_MIXER_MIC:           /* right mic only */
1631                 devc->levels[SOUND_MIXER_MIC] &= 0xff;
1632                 devc->levels[SOUND_MIXER_MIC] |= lev_l << 8;
1633                 break;
1634
1635         case SOUND_MIXER_LINE1:         /* left mic only  */
1636                 devc->levels[SOUND_MIXER_MIC] &= 0xff00;
1637                 devc->levels[SOUND_MIXER_MIC] |= lev_l;
1638                 dev = SOUND_MIXER_MIC;
1639                 break;
1640
1641         case SOUND_MIXER_PHONEIN:       /* left aux only  */
1642                 devc->levels[SOUND_MIXER_LINE1] = lev_l;
1643                 dev = SOUND_MIXER_LINE1;
1644                 break;
1645
1646         case SOUND_MIXER_IMIX:
1647         case SOUND_MIXER_PHONEOUT:
1648                 break;
1649
1650         default:
1651                 dev = -EINVAL;
1652                 break;
1653         }
1654         return dev;
1655 }
1656
1657 static int netwinder_get_mixer(wavnc_info *devc, int dev)
1658 {
1659         int levels;
1660
1661         switch (dev) {
1662         case SOUND_MIXER_VOLUME:
1663         case SOUND_MIXER_SYNTH:
1664         case SOUND_MIXER_PCM:
1665         case SOUND_MIXER_LINE:
1666         case SOUND_MIXER_IGAIN:
1667                 levels = devc->levels[dev];
1668                 break;
1669
1670         case SOUND_MIXER_MIC:           /* builtin mic: right mic only */
1671                 levels = devc->levels[SOUND_MIXER_MIC] >> 8;
1672                 levels |= levels << 8;
1673                 break;
1674
1675         case SOUND_MIXER_LINE1:         /* handset mic: left mic only */
1676                 levels = devc->levels[SOUND_MIXER_MIC] & 0xff;
1677                 levels |= levels << 8;
1678                 break;
1679
1680         case SOUND_MIXER_PHONEIN:       /* phone mic: left aux1 only */
1681                 levels = devc->levels[SOUND_MIXER_LINE1] & 0xff;
1682                 levels |= levels << 8;
1683                 break;
1684
1685         default:
1686                 levels = 0;
1687         }
1688
1689         return levels;
1690 }
1691
1692 /*
1693  * Waveartist specific mixer information.
1694  */
1695 static const struct waveartist_mixer_info netwinder_mixer = {
1696         .supported_devs = SOUND_MASK_VOLUME  | SOUND_MASK_SYNTH   |
1697                         SOUND_MASK_PCM     | SOUND_MASK_SPEAKER |
1698                         SOUND_MASK_LINE    | SOUND_MASK_MIC     |
1699                         SOUND_MASK_IMIX    | SOUND_MASK_LINE1   |
1700                         SOUND_MASK_PHONEIN | SOUND_MASK_PHONEOUT|
1701                         SOUND_MASK_IGAIN,
1702
1703         .recording_devs = SOUND_MASK_LINE    | SOUND_MASK_MIC     |
1704                         SOUND_MASK_IMIX    | SOUND_MASK_LINE1   |
1705                         SOUND_MASK_PHONEIN,
1706
1707         .stereo_devs    = SOUND_MASK_VOLUME  | SOUND_MASK_SYNTH   |
1708                         SOUND_MASK_PCM     | SOUND_MASK_LINE    |
1709                         SOUND_MASK_IMIX    | SOUND_MASK_IGAIN,
1710
1711         .select_input   = netwinder_select_input,
1712         .decode_mixer   = netwinder_decode_mixer,
1713         .get_mixer      = netwinder_get_mixer,
1714 };
1715
1716 static void
1717 vnc_configure_mixer(wavnc_info *devc, unsigned int recmask)
1718 {
1719         if (!devc->no_autoselect) {
1720                 if (devc->handset_detect) {
1721                         recmask = SOUND_MASK_LINE1;
1722                         devc->spkr_mute_state = devc->line_mute_state = 1;
1723                 } else if (devc->telephone_detect) {
1724                         recmask = SOUND_MASK_PHONEIN;
1725                         devc->spkr_mute_state = devc->line_mute_state = 1;
1726                 } else {
1727                         /* unless someone has asked for LINE-IN,
1728                          * we default to MIC
1729                          */
1730                         if ((devc->recmask & SOUND_MASK_LINE) == 0)
1731                                 devc->recmask = SOUND_MASK_MIC;
1732                         devc->spkr_mute_state = devc->line_mute_state = 0;
1733                 }
1734                 vnc_mute_spkr(devc);
1735                 vnc_mute_lout(devc);
1736
1737                 if (recmask != devc->recmask)
1738                         waveartist_set_recmask(devc, recmask);
1739         }
1740 }
1741
1742 static int
1743 vnc_slider(wavnc_info *devc)
1744 {
1745         signed int slider_volume;
1746         unsigned int temp, old_hs, old_td;
1747
1748         /*
1749          * read the "buttons" state.
1750          *  Bit 4 = 0 means handset present
1751          *  Bit 5 = 1 means phone offhook
1752          */
1753         temp = inb(0x201);
1754
1755         old_hs = devc->handset_detect;
1756         old_td = devc->telephone_detect;
1757
1758         devc->handset_detect = !(temp & 0x10);
1759         devc->telephone_detect = !!(temp & 0x20);
1760
1761         if (!devc->no_autoselect &&
1762             (old_hs != devc->handset_detect ||
1763              old_td != devc->telephone_detect))
1764                 vnc_configure_mixer(devc, devc->recmask);
1765
1766         slider_volume = vnc_volume_slider(devc);
1767
1768         /*
1769          * If we're using software controlled volume, and
1770          * the slider moves by more than 20%, then we
1771          * switch back to slider controlled volume.
1772          */
1773         if (abs(devc->slider_vol - slider_volume) > 20)
1774                 devc->use_slider = 1;
1775
1776         /*
1777          * use only left channel
1778          */
1779         temp = levels[SOUND_MIXER_VOLUME] & 0xFF;
1780
1781         if (slider_volume != temp && devc->use_slider) {
1782                 devc->slider_vol = slider_volume;
1783
1784                 waveartist_set_mixer(devc, SOUND_MIXER_VOLUME,
1785                         slider_volume | slider_volume << 8);
1786
1787                 return 1;
1788         }
1789
1790         return 0;
1791 }
1792
1793 static void
1794 vnc_slider_tick(unsigned long data)
1795 {
1796         int next_timeout;
1797
1798         if (vnc_slider(adev_info + data))
1799                 next_timeout = 5;       // mixer reported change
1800         else
1801                 next_timeout = VNC_TIMER_PERIOD;
1802
1803         mod_timer(&vnc_timer, jiffies + next_timeout);
1804 }
1805
1806 static int
1807 vnc_private_ioctl(int dev, unsigned int cmd, int __user * arg)
1808 {
1809         wavnc_info *devc = (wavnc_info *)audio_devs[dev]->devc;
1810         int val;
1811
1812         switch (cmd) {
1813         case SOUND_MIXER_PRIVATE1:
1814         {
1815                 u_int prev_spkr_mute, prev_line_mute, prev_auto_state;
1816                 int val;
1817
1818                 if (get_user(val, arg))
1819                         return -EFAULT;
1820
1821                 /* check if parameter is logical */
1822                 if (val & ~(VNC_MUTE_INTERNAL_SPKR |
1823                             VNC_MUTE_LINE_OUT |
1824                             VNC_DISABLE_AUTOSWITCH))
1825                         return -EINVAL;
1826
1827                 prev_auto_state = devc->no_autoselect;
1828                 prev_spkr_mute  = devc->spkr_mute_state;
1829                 prev_line_mute  = devc->line_mute_state;
1830
1831                 devc->no_autoselect   = (val & VNC_DISABLE_AUTOSWITCH) ? 1 : 0;
1832                 devc->spkr_mute_state = (val & VNC_MUTE_INTERNAL_SPKR) ? 1 : 0;
1833                 devc->line_mute_state = (val & VNC_MUTE_LINE_OUT) ? 1 : 0;
1834
1835                 if (prev_spkr_mute != devc->spkr_mute_state)
1836                         vnc_mute_spkr(devc);
1837
1838                 if (prev_line_mute != devc->line_mute_state)
1839                         vnc_mute_lout(devc);
1840
1841                 if (prev_auto_state != devc->no_autoselect)
1842                         vnc_configure_mixer(devc, devc->recmask);
1843
1844                 return 0;
1845         }
1846
1847         case SOUND_MIXER_PRIVATE2:
1848                 if (get_user(val, arg))
1849                         return -EFAULT;
1850
1851                 switch (val) {
1852 #define VNC_SOUND_PAUSE         0x53    //to pause the DSP
1853 #define VNC_SOUND_RESUME        0x57    //to unpause the DSP
1854                 case VNC_SOUND_PAUSE:
1855                         waveartist_cmd1(devc, 0x16);
1856                         break;
1857
1858                 case VNC_SOUND_RESUME:
1859                         waveartist_cmd1(devc, 0x18);
1860                         break;
1861
1862                 default:
1863                         return -EINVAL;
1864                 }
1865                 return 0;
1866
1867         /* private ioctl to allow bulk access to waveartist */
1868         case SOUND_MIXER_PRIVATE3:
1869         {
1870                 unsigned long   flags;
1871                 int             mixer_reg[15], i, val;
1872
1873                 if (get_user(val, arg))
1874                         return -EFAULT;
1875                 if (copy_from_user(mixer_reg, (void *)val, sizeof(mixer_reg)))
1876                         return -EFAULT;
1877
1878                 switch (mixer_reg[14]) {
1879                 case MIXER_PRIVATE3_RESET:
1880                         waveartist_mixer_reset(devc);
1881                         break;
1882
1883                 case MIXER_PRIVATE3_WRITE:
1884                         waveartist_cmd3(devc, WACMD_SET_MIXER, mixer_reg[0], mixer_reg[4]);
1885                         waveartist_cmd3(devc, WACMD_SET_MIXER, mixer_reg[1], mixer_reg[5]);
1886                         waveartist_cmd3(devc, WACMD_SET_MIXER, mixer_reg[2], mixer_reg[6]);
1887                         waveartist_cmd3(devc, WACMD_SET_MIXER, mixer_reg[3], mixer_reg[7]);
1888                         waveartist_cmd3(devc, WACMD_SET_MIXER, mixer_reg[8], mixer_reg[9]);
1889
1890                         waveartist_cmd3(devc, WACMD_SET_LEVEL, mixer_reg[10], mixer_reg[11]);
1891                         waveartist_cmd3(devc, WACMD_SET_LEVEL, mixer_reg[12], mixer_reg[13]);
1892                         break;
1893
1894                 case MIXER_PRIVATE3_READ:
1895                         spin_lock_irqsave(&waveartist_lock, flags);
1896
1897                         for (i = 0x30; i < 14 << 8; i += 1 << 8)
1898                                 waveartist_cmd(devc, 1, &i, 1, mixer_reg + (i >> 8));
1899
1900                         spin_unlock_irqrestore(&waveartist_lock, flags);
1901
1902                         if (copy_to_user((void *)val, mixer_reg, sizeof(mixer_reg)))
1903                                 return -EFAULT;
1904                         break;
1905
1906                 default:
1907                         return -EINVAL;
1908                 }
1909                 return 0;
1910         }
1911
1912         /* read back the state from PRIVATE1 */
1913         case SOUND_MIXER_PRIVATE4:
1914                 val = (devc->spkr_mute_state  ? VNC_MUTE_INTERNAL_SPKR : 0) |
1915                       (devc->line_mute_state  ? VNC_MUTE_LINE_OUT      : 0) |
1916                       (devc->handset_detect   ? VNC_HANDSET_DETECT     : 0) |
1917                       (devc->telephone_detect ? VNC_PHONE_DETECT       : 0) |
1918                       (devc->no_autoselect    ? VNC_DISABLE_AUTOSWITCH : 0);
1919
1920                 return put_user(val, arg) ? -EFAULT : 0;
1921         }
1922
1923         if (_SIOC_DIR(cmd) & _SIOC_WRITE) {
1924                 /*
1925                  * special case for master volume: if we
1926                  * received this call - switch from hw
1927                  * volume control to a software volume
1928                  * control, till the hw volume is modified
1929                  * to signal that user wants to be back in
1930                  * hardware...
1931                  */
1932                 if ((cmd & 0xff) == SOUND_MIXER_VOLUME)
1933                         devc->use_slider = 0;
1934
1935                 /* speaker output            */
1936                 if ((cmd & 0xff) == SOUND_MIXER_SPEAKER) {
1937                         unsigned int val, l, r;
1938
1939                         if (get_user(val, arg))
1940                                 return -EFAULT;
1941
1942                         l = val & 0x7f;
1943                         r = (val & 0x7f00) >> 8;
1944                         val = (l + r) / 2;
1945                         devc->levels[SOUND_MIXER_SPEAKER] = val | (val << 8);
1946                         devc->spkr_mute_state = (val <= 50);
1947                         vnc_mute_spkr(devc);
1948                         return 0;
1949                 }
1950         }
1951
1952         return -ENOIOCTLCMD;
1953 }
1954
1955 #endif
1956
1957 static struct address_info cfg;
1958
1959 static int attached;
1960
1961 static int __initdata io = 0;
1962 static int __initdata irq = 0;
1963 static int __initdata dma = 0;
1964 static int __initdata dma2 = 0;
1965
1966
1967 static int __init init_waveartist(void)
1968 {
1969         const struct waveartist_mixer_info *mix;
1970
1971         if (!io && machine_is_netwinder()) {
1972                 /*
1973                  * The NetWinder WaveArtist is at a fixed address.
1974                  * If the user does not supply an address, use the
1975                  * well-known parameters.
1976                  */
1977                 io   = 0x250;
1978                 irq  = 12;
1979                 dma  = 3;
1980                 dma2 = 7;
1981         }
1982
1983         mix = &waveartist_mixer;
1984 #ifdef CONFIG_ARCH_NETWINDER
1985         if (machine_is_netwinder())
1986                 mix = &netwinder_mixer;
1987 #endif
1988
1989         cfg.io_base = io;
1990         cfg.irq = irq;
1991         cfg.dma = dma;
1992         cfg.dma2 = dma2;
1993
1994         if (!probe_waveartist(&cfg))
1995                 return -ENODEV;
1996
1997         attach_waveartist(&cfg, mix);
1998         attached = 1;
1999
2000         return 0;
2001 }
2002
2003 static void __exit cleanup_waveartist(void)
2004 {
2005         if (attached)
2006                 unload_waveartist(&cfg);
2007 }
2008
2009 module_init(init_waveartist);
2010 module_exit(cleanup_waveartist);
2011
2012 #ifndef MODULE
2013 static int __init setup_waveartist(char *str)
2014 {
2015         /* io, irq, dma, dma2 */
2016         int ints[5];
2017         
2018         str = get_options(str, ARRAY_SIZE(ints), ints);
2019         
2020         io      = ints[1];
2021         irq     = ints[2];
2022         dma     = ints[3];
2023         dma2    = ints[4];
2024
2025         return 1;
2026 }
2027 __setup("waveartist=", setup_waveartist);
2028 #endif
2029
2030 MODULE_DESCRIPTION("Rockwell WaveArtist RWA-010 sound driver");
2031 module_param(io, int, 0);               /* IO base */
2032 module_param(irq, int, 0);              /* IRQ */
2033 module_param(dma, int, 0);              /* DMA */
2034 module_param(dma2, int, 0);             /* DMA2 */
2035 MODULE_LICENSE("GPL");