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