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