x86_64: Quieten Atari keyboard warnings in Kconfig
[linux-2.6] / 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/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)
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 = kzalloc(sizeof(wavnc_port_info), GFP_KERNEL);
1271         if (portc == NULL)
1272                 goto nomem;
1273
1274         my_dev = sound_install_audiodrv(AUDIO_DRIVER_VERSION, dev_name,
1275                         &waveartist_audio_driver, sizeof(struct audio_driver),
1276                         devc->audio_flags, AFMT_U8 | AFMT_S16_LE | AFMT_S8,
1277                         devc, devc->hw.dma, devc->hw.dma2);
1278
1279         if (my_dev < 0)
1280                 goto free;
1281
1282         audio_devs[my_dev]->portc = portc;
1283
1284         waveartist_mixer_reset(devc);
1285
1286         /*
1287          * clear any pending interrupt
1288          */
1289         waveartist_iack(devc);
1290
1291         if (request_irq(devc->hw.irq, waveartist_intr, 0, devc->hw.name, devc) < 0) {
1292                 printk(KERN_ERR "%s: IRQ %d in use\n",
1293                         devc->hw.name, devc->hw.irq);
1294                 goto uninstall;
1295         }
1296
1297         if (sound_alloc_dma(devc->hw.dma, devc->hw.name)) {
1298                 printk(KERN_ERR "%s: Can't allocate DMA%d\n",
1299                         devc->hw.name, devc->hw.dma);
1300                 goto uninstall_irq;
1301         }
1302
1303         if (devc->hw.dma != devc->hw.dma2 && devc->hw.dma2 != NO_DMA)
1304                 if (sound_alloc_dma(devc->hw.dma2, devc->hw.name)) {
1305                         printk(KERN_ERR "%s: can't allocate DMA%d\n",
1306                                 devc->hw.name, devc->hw.dma2);
1307                         goto uninstall_dma;
1308                 }
1309
1310         waveartist_set_ctlr(&devc->hw, 0, DMA1_IE | DMA0_IE);
1311
1312         audio_devs[my_dev]->mixer_dev =
1313                 sound_install_mixer(MIXER_DRIVER_VERSION,
1314                                 dev_name,
1315                                 &waveartist_mixer_operations,
1316                                 sizeof(struct mixer_operations),
1317                                 devc);
1318
1319         return my_dev;
1320
1321 uninstall_dma:
1322         sound_free_dma(devc->hw.dma);
1323
1324 uninstall_irq:
1325         free_irq(devc->hw.irq, devc);
1326
1327 uninstall:
1328         sound_unload_audiodev(my_dev);
1329
1330 free:
1331         kfree(portc);
1332
1333 nomem:
1334         return -1;
1335 }
1336
1337 static int __init probe_waveartist(struct address_info *hw_config)
1338 {
1339         wavnc_info *devc = &adev_info[nr_waveartist_devs];
1340
1341         if (nr_waveartist_devs >= MAX_AUDIO_DEV) {
1342                 printk(KERN_WARNING "waveartist: too many audio devices\n");
1343                 return 0;
1344         }
1345
1346         if (!request_region(hw_config->io_base, 15, hw_config->name))  {
1347                 printk(KERN_WARNING "WaveArtist: I/O port conflict\n");
1348                 return 0;
1349         }
1350
1351         if (hw_config->irq > 15 || hw_config->irq < 0) {
1352                 release_region(hw_config->io_base, 15);
1353                 printk(KERN_WARNING "WaveArtist: Bad IRQ %d\n",
1354                        hw_config->irq);
1355                 return 0;
1356         }
1357
1358         if (hw_config->dma != 3) {
1359                 release_region(hw_config->io_base, 15);
1360                 printk(KERN_WARNING "WaveArtist: Bad DMA %d\n",
1361                        hw_config->dma);
1362                 return 0;
1363         }
1364
1365         hw_config->name = "WaveArtist";
1366         devc->hw = *hw_config;
1367         devc->open_mode = 0;
1368         devc->chip_name = "RWA-010";
1369
1370         return 1;
1371 }
1372
1373 static void __init
1374 attach_waveartist(struct address_info *hw, const struct waveartist_mixer_info *mix)
1375 {
1376         wavnc_info *devc = &adev_info[nr_waveartist_devs];
1377
1378         /*
1379          * NOTE! If irq < 0, there is another driver which has allocated the
1380          *   IRQ so that this driver doesn't need to allocate/deallocate it.
1381          *   The actually used IRQ is ABS(irq).
1382          */
1383         devc->hw = *hw;
1384         devc->hw.irq = (hw->irq > 0) ? hw->irq : 0;
1385         devc->open_mode = 0;
1386         devc->playback_dev = 0;
1387         devc->record_dev = 0;
1388         devc->audio_flags = DMA_AUTOMODE;
1389         devc->levels = levels;
1390
1391         if (hw->dma != hw->dma2 && hw->dma2 != NO_DMA)
1392                 devc->audio_flags |= DMA_DUPLEX;
1393
1394         devc->mix = mix;
1395         devc->dev_no = waveartist_init(devc);
1396
1397         if (devc->dev_no < 0)
1398                 release_region(hw->io_base, 15);
1399         else {
1400 #ifdef CONFIG_ARCH_NETWINDER
1401                 if (machine_is_netwinder()) {
1402                         init_timer(&vnc_timer);
1403                         vnc_timer.function = vnc_slider_tick;
1404                         vnc_timer.expires  = jiffies;
1405                         vnc_timer.data     = nr_waveartist_devs;
1406                         add_timer(&vnc_timer);
1407
1408                         vnc_configure_mixer(devc, 0);
1409
1410                         devc->no_autoselect = 1;
1411                 }
1412 #endif
1413                 nr_waveartist_devs += 1;
1414         }
1415 }
1416
1417 static void __exit unload_waveartist(struct address_info *hw)
1418 {
1419         wavnc_info *devc = NULL;
1420         int i;
1421
1422         for (i = 0; i < nr_waveartist_devs; i++)
1423                 if (hw->io_base == adev_info[i].hw.io_base) {
1424                         devc = adev_info + i;
1425                         break;
1426                 }
1427
1428         if (devc != NULL) {
1429                 int mixer;
1430
1431 #ifdef CONFIG_ARCH_NETWINDER
1432                 if (machine_is_netwinder())
1433                         del_timer(&vnc_timer);
1434 #endif
1435
1436                 release_region(devc->hw.io_base, 15);
1437
1438                 waveartist_set_ctlr(&devc->hw, DMA1_IE|DMA0_IE, 0);
1439
1440                 if (devc->hw.irq >= 0)
1441                         free_irq(devc->hw.irq, devc);
1442
1443                 sound_free_dma(devc->hw.dma);
1444
1445                 if (devc->hw.dma != devc->hw.dma2 &&
1446                     devc->hw.dma2 != NO_DMA)
1447                         sound_free_dma(devc->hw.dma2);
1448
1449                 mixer = audio_devs[devc->dev_no]->mixer_dev;
1450
1451                 if (mixer >= 0)
1452                         sound_unload_mixerdev(mixer);
1453
1454                 if (devc->dev_no >= 0)
1455                         sound_unload_audiodev(devc->dev_no);
1456
1457                 nr_waveartist_devs -= 1;
1458
1459                 for (; i < nr_waveartist_devs; i++)
1460                         adev_info[i] = adev_info[i + 1];
1461         } else
1462                 printk(KERN_WARNING "waveartist: can't find device "
1463                        "to unload\n");
1464 }
1465
1466 #ifdef CONFIG_ARCH_NETWINDER
1467
1468 /*
1469  * Rebel.com Netwinder specifics...
1470  */
1471
1472 #include <asm/hardware/dec21285.h>
1473  
1474 #define VNC_TIMER_PERIOD (HZ/4) //check slider 4 times/sec
1475
1476 #define MIXER_PRIVATE3_RESET    0x53570000
1477 #define MIXER_PRIVATE3_READ     0x53570001
1478 #define MIXER_PRIVATE3_WRITE    0x53570002
1479
1480 #define VNC_MUTE_INTERNAL_SPKR  0x01    //the sw mute on/off control bit
1481 #define VNC_MUTE_LINE_OUT       0x10
1482 #define VNC_PHONE_DETECT        0x20
1483 #define VNC_HANDSET_DETECT      0x40
1484 #define VNC_DISABLE_AUTOSWITCH  0x80
1485
1486 extern spinlock_t gpio_lock;
1487
1488 static inline void
1489 vnc_mute_spkr(wavnc_info *devc)
1490 {
1491         unsigned long flags;
1492
1493         spin_lock_irqsave(&gpio_lock, flags);
1494         cpld_modify(CPLD_UNMUTE, devc->spkr_mute_state ? 0 : CPLD_UNMUTE);
1495         spin_unlock_irqrestore(&gpio_lock, flags);
1496 }
1497
1498 static void
1499 vnc_mute_lout(wavnc_info *devc)
1500 {
1501         unsigned int left, right;
1502
1503         left  = waveartist_cmd1_r(devc, WACMD_GET_LEVEL);
1504         right = waveartist_cmd1_r(devc, WACMD_GET_LEVEL | 0x400);
1505
1506         if (devc->line_mute_state) {
1507                 left &= ~1;
1508                 right &= ~1;
1509         } else {
1510                 left |= 1;
1511                 right |= 1;
1512         }
1513         waveartist_cmd3(devc, WACMD_SET_MIXER, left, right);
1514                 
1515 }
1516
1517 static int
1518 vnc_volume_slider(wavnc_info *devc)
1519 {
1520         static signed int old_slider_volume;
1521         unsigned long flags;
1522         signed int volume = 255;
1523
1524         *CSR_TIMER1_LOAD = 0x00ffffff;
1525
1526         spin_lock_irqsave(&waveartist_lock, flags);
1527
1528         outb(0xFF, 0x201);
1529         *CSR_TIMER1_CNTL = TIMER_CNTL_ENABLE | TIMER_CNTL_DIV1;
1530
1531         while (volume && (inb(0x201) & 0x01))
1532                 volume--;
1533
1534         *CSR_TIMER1_CNTL = 0;
1535
1536         spin_unlock_irqrestore(&waveartist_lock,flags);
1537         
1538         volume = 0x00ffffff - *CSR_TIMER1_VALUE;
1539
1540
1541 #ifndef REVERSE
1542         volume = 150 - (volume >> 5);
1543 #else
1544         volume = (volume >> 6) - 25;
1545 #endif
1546
1547         if (volume < 0)
1548                 volume = 0;
1549
1550         if (volume > 100)
1551                 volume = 100;
1552
1553         /*
1554          * slider quite often reads +-8, so debounce this random noise
1555          */
1556         if (abs(volume - old_slider_volume) > 7) {
1557                 old_slider_volume = volume;
1558
1559                 if (debug_flg & DEBUG_MIXER)
1560                         printk(KERN_DEBUG "Slider volume: %d.\n", volume);
1561         }
1562
1563         return old_slider_volume;
1564 }
1565
1566 /*
1567  * Decode a recording mask into a mixer selection on the NetWinder
1568  * as follows:
1569  *
1570  *     OSS Source       WA Source       Actual source
1571  *  SOUND_MASK_IMIX     Mixer           Mixer output (same as AD1848)
1572  *  SOUND_MASK_LINE     Line            Line in
1573  *  SOUND_MASK_LINE1    Left Mic        Handset
1574  *  SOUND_MASK_PHONEIN  Left Aux        Telephone microphone
1575  *  SOUND_MASK_MIC      Right Mic       Builtin microphone
1576  */
1577 static unsigned int
1578 netwinder_select_input(wavnc_info *devc, unsigned int recmask,
1579                        unsigned char *dev_l, unsigned char *dev_r)
1580 {
1581         unsigned int recdev_l = ADC_MUX_NONE, recdev_r = ADC_MUX_NONE;
1582
1583         if (recmask & SOUND_MASK_IMIX) {
1584                 recmask = SOUND_MASK_IMIX;
1585                 recdev_l = ADC_MUX_MIXER;
1586                 recdev_r = ADC_MUX_MIXER;
1587         } else if (recmask & SOUND_MASK_LINE) {
1588                 recmask = SOUND_MASK_LINE;
1589                 recdev_l = ADC_MUX_LINE;
1590                 recdev_r = ADC_MUX_LINE;
1591         } else if (recmask & SOUND_MASK_LINE1) {
1592                 recmask = SOUND_MASK_LINE1;
1593                 waveartist_cmd1(devc, WACMD_SET_MONO); /* left */
1594                 recdev_l = ADC_MUX_MIC;
1595                 recdev_r = ADC_MUX_NONE;
1596         } else if (recmask & SOUND_MASK_PHONEIN) {
1597                 recmask = SOUND_MASK_PHONEIN;
1598                 waveartist_cmd1(devc, WACMD_SET_MONO); /* left */
1599                 recdev_l = ADC_MUX_AUX1;
1600                 recdev_r = ADC_MUX_NONE;
1601         } else if (recmask & SOUND_MASK_MIC) {
1602                 recmask = SOUND_MASK_MIC;
1603                 waveartist_cmd1(devc, WACMD_SET_MONO | 0x100);  /* right */
1604                 recdev_l = ADC_MUX_NONE;
1605                 recdev_r = ADC_MUX_MIC;
1606         }
1607
1608         *dev_l = recdev_l;
1609         *dev_r = recdev_r;
1610
1611         return recmask;
1612 }
1613
1614 static int
1615 netwinder_decode_mixer(wavnc_info *devc, int dev, unsigned char lev_l,
1616                        unsigned char lev_r)
1617 {
1618         switch (dev) {
1619         case SOUND_MIXER_VOLUME:
1620         case SOUND_MIXER_SYNTH:
1621         case SOUND_MIXER_PCM:
1622         case SOUND_MIXER_LINE:
1623         case SOUND_MIXER_IGAIN:
1624                 devc->levels[dev] = lev_l | lev_r << 8;
1625                 break;
1626
1627         case SOUND_MIXER_MIC:           /* right mic only */
1628                 devc->levels[SOUND_MIXER_MIC] &= 0xff;
1629                 devc->levels[SOUND_MIXER_MIC] |= lev_l << 8;
1630                 break;
1631
1632         case SOUND_MIXER_LINE1:         /* left mic only  */
1633                 devc->levels[SOUND_MIXER_MIC] &= 0xff00;
1634                 devc->levels[SOUND_MIXER_MIC] |= lev_l;
1635                 dev = SOUND_MIXER_MIC;
1636                 break;
1637
1638         case SOUND_MIXER_PHONEIN:       /* left aux only  */
1639                 devc->levels[SOUND_MIXER_LINE1] = lev_l;
1640                 dev = SOUND_MIXER_LINE1;
1641                 break;
1642
1643         case SOUND_MIXER_IMIX:
1644         case SOUND_MIXER_PHONEOUT:
1645                 break;
1646
1647         default:
1648                 dev = -EINVAL;
1649                 break;
1650         }
1651         return dev;
1652 }
1653
1654 static int netwinder_get_mixer(wavnc_info *devc, int dev)
1655 {
1656         int levels;
1657
1658         switch (dev) {
1659         case SOUND_MIXER_VOLUME:
1660         case SOUND_MIXER_SYNTH:
1661         case SOUND_MIXER_PCM:
1662         case SOUND_MIXER_LINE:
1663         case SOUND_MIXER_IGAIN:
1664                 levels = devc->levels[dev];
1665                 break;
1666
1667         case SOUND_MIXER_MIC:           /* builtin mic: right mic only */
1668                 levels = devc->levels[SOUND_MIXER_MIC] >> 8;
1669                 levels |= levels << 8;
1670                 break;
1671
1672         case SOUND_MIXER_LINE1:         /* handset mic: left mic only */
1673                 levels = devc->levels[SOUND_MIXER_MIC] & 0xff;
1674                 levels |= levels << 8;
1675                 break;
1676
1677         case SOUND_MIXER_PHONEIN:       /* phone mic: left aux1 only */
1678                 levels = devc->levels[SOUND_MIXER_LINE1] & 0xff;
1679                 levels |= levels << 8;
1680                 break;
1681
1682         default:
1683                 levels = 0;
1684         }
1685
1686         return levels;
1687 }
1688
1689 /*
1690  * Waveartist specific mixer information.
1691  */
1692 static const struct waveartist_mixer_info netwinder_mixer = {
1693         .supported_devs = SOUND_MASK_VOLUME  | SOUND_MASK_SYNTH   |
1694                         SOUND_MASK_PCM     | SOUND_MASK_SPEAKER |
1695                         SOUND_MASK_LINE    | SOUND_MASK_MIC     |
1696                         SOUND_MASK_IMIX    | SOUND_MASK_LINE1   |
1697                         SOUND_MASK_PHONEIN | SOUND_MASK_PHONEOUT|
1698                         SOUND_MASK_IGAIN,
1699
1700         .recording_devs = SOUND_MASK_LINE    | SOUND_MASK_MIC     |
1701                         SOUND_MASK_IMIX    | SOUND_MASK_LINE1   |
1702                         SOUND_MASK_PHONEIN,
1703
1704         .stereo_devs    = SOUND_MASK_VOLUME  | SOUND_MASK_SYNTH   |
1705                         SOUND_MASK_PCM     | SOUND_MASK_LINE    |
1706                         SOUND_MASK_IMIX    | SOUND_MASK_IGAIN,
1707
1708         .select_input   = netwinder_select_input,
1709         .decode_mixer   = netwinder_decode_mixer,
1710         .get_mixer      = netwinder_get_mixer,
1711 };
1712
1713 static void
1714 vnc_configure_mixer(wavnc_info *devc, unsigned int recmask)
1715 {
1716         if (!devc->no_autoselect) {
1717                 if (devc->handset_detect) {
1718                         recmask = SOUND_MASK_LINE1;
1719                         devc->spkr_mute_state = devc->line_mute_state = 1;
1720                 } else if (devc->telephone_detect) {
1721                         recmask = SOUND_MASK_PHONEIN;
1722                         devc->spkr_mute_state = devc->line_mute_state = 1;
1723                 } else {
1724                         /* unless someone has asked for LINE-IN,
1725                          * we default to MIC
1726                          */
1727                         if ((devc->recmask & SOUND_MASK_LINE) == 0)
1728                                 devc->recmask = SOUND_MASK_MIC;
1729                         devc->spkr_mute_state = devc->line_mute_state = 0;
1730                 }
1731                 vnc_mute_spkr(devc);
1732                 vnc_mute_lout(devc);
1733
1734                 if (recmask != devc->recmask)
1735                         waveartist_set_recmask(devc, recmask);
1736         }
1737 }
1738
1739 static int
1740 vnc_slider(wavnc_info *devc)
1741 {
1742         signed int slider_volume;
1743         unsigned int temp, old_hs, old_td;
1744
1745         /*
1746          * read the "buttons" state.
1747          *  Bit 4 = 0 means handset present
1748          *  Bit 5 = 1 means phone offhook
1749          */
1750         temp = inb(0x201);
1751
1752         old_hs = devc->handset_detect;
1753         old_td = devc->telephone_detect;
1754
1755         devc->handset_detect = !(temp & 0x10);
1756         devc->telephone_detect = !!(temp & 0x20);
1757
1758         if (!devc->no_autoselect &&
1759             (old_hs != devc->handset_detect ||
1760              old_td != devc->telephone_detect))
1761                 vnc_configure_mixer(devc, devc->recmask);
1762
1763         slider_volume = vnc_volume_slider(devc);
1764
1765         /*
1766          * If we're using software controlled volume, and
1767          * the slider moves by more than 20%, then we
1768          * switch back to slider controlled volume.
1769          */
1770         if (abs(devc->slider_vol - slider_volume) > 20)
1771                 devc->use_slider = 1;
1772
1773         /*
1774          * use only left channel
1775          */
1776         temp = levels[SOUND_MIXER_VOLUME] & 0xFF;
1777
1778         if (slider_volume != temp && devc->use_slider) {
1779                 devc->slider_vol = slider_volume;
1780
1781                 waveartist_set_mixer(devc, SOUND_MIXER_VOLUME,
1782                         slider_volume | slider_volume << 8);
1783
1784                 return 1;
1785         }
1786
1787         return 0;
1788 }
1789
1790 static void
1791 vnc_slider_tick(unsigned long data)
1792 {
1793         int next_timeout;
1794
1795         if (vnc_slider(adev_info + data))
1796                 next_timeout = 5;       // mixer reported change
1797         else
1798                 next_timeout = VNC_TIMER_PERIOD;
1799
1800         mod_timer(&vnc_timer, jiffies + next_timeout);
1801 }
1802
1803 static int
1804 vnc_private_ioctl(int dev, unsigned int cmd, int __user * arg)
1805 {
1806         wavnc_info *devc = (wavnc_info *)audio_devs[dev]->devc;
1807         int val;
1808
1809         switch (cmd) {
1810         case SOUND_MIXER_PRIVATE1:
1811         {
1812                 u_int prev_spkr_mute, prev_line_mute, prev_auto_state;
1813                 int val;
1814
1815                 if (get_user(val, arg))
1816                         return -EFAULT;
1817
1818                 /* check if parameter is logical */
1819                 if (val & ~(VNC_MUTE_INTERNAL_SPKR |
1820                             VNC_MUTE_LINE_OUT |
1821                             VNC_DISABLE_AUTOSWITCH))
1822                         return -EINVAL;
1823
1824                 prev_auto_state = devc->no_autoselect;
1825                 prev_spkr_mute  = devc->spkr_mute_state;
1826                 prev_line_mute  = devc->line_mute_state;
1827
1828                 devc->no_autoselect   = (val & VNC_DISABLE_AUTOSWITCH) ? 1 : 0;
1829                 devc->spkr_mute_state = (val & VNC_MUTE_INTERNAL_SPKR) ? 1 : 0;
1830                 devc->line_mute_state = (val & VNC_MUTE_LINE_OUT) ? 1 : 0;
1831
1832                 if (prev_spkr_mute != devc->spkr_mute_state)
1833                         vnc_mute_spkr(devc);
1834
1835                 if (prev_line_mute != devc->line_mute_state)
1836                         vnc_mute_lout(devc);
1837
1838                 if (prev_auto_state != devc->no_autoselect)
1839                         vnc_configure_mixer(devc, devc->recmask);
1840
1841                 return 0;
1842         }
1843
1844         case SOUND_MIXER_PRIVATE2:
1845                 if (get_user(val, arg))
1846                         return -EFAULT;
1847
1848                 switch (val) {
1849 #define VNC_SOUND_PAUSE         0x53    //to pause the DSP
1850 #define VNC_SOUND_RESUME        0x57    //to unpause the DSP
1851                 case VNC_SOUND_PAUSE:
1852                         waveartist_cmd1(devc, 0x16);
1853                         break;
1854
1855                 case VNC_SOUND_RESUME:
1856                         waveartist_cmd1(devc, 0x18);
1857                         break;
1858
1859                 default:
1860                         return -EINVAL;
1861                 }
1862                 return 0;
1863
1864         /* private ioctl to allow bulk access to waveartist */
1865         case SOUND_MIXER_PRIVATE3:
1866         {
1867                 unsigned long   flags;
1868                 int             mixer_reg[15], i, val;
1869
1870                 if (get_user(val, arg))
1871                         return -EFAULT;
1872                 if (copy_from_user(mixer_reg, (void *)val, sizeof(mixer_reg)))
1873                         return -EFAULT;
1874
1875                 switch (mixer_reg[14]) {
1876                 case MIXER_PRIVATE3_RESET:
1877                         waveartist_mixer_reset(devc);
1878                         break;
1879
1880                 case MIXER_PRIVATE3_WRITE:
1881                         waveartist_cmd3(devc, WACMD_SET_MIXER, mixer_reg[0], mixer_reg[4]);
1882                         waveartist_cmd3(devc, WACMD_SET_MIXER, mixer_reg[1], mixer_reg[5]);
1883                         waveartist_cmd3(devc, WACMD_SET_MIXER, mixer_reg[2], mixer_reg[6]);
1884                         waveartist_cmd3(devc, WACMD_SET_MIXER, mixer_reg[3], mixer_reg[7]);
1885                         waveartist_cmd3(devc, WACMD_SET_MIXER, mixer_reg[8], mixer_reg[9]);
1886
1887                         waveartist_cmd3(devc, WACMD_SET_LEVEL, mixer_reg[10], mixer_reg[11]);
1888                         waveartist_cmd3(devc, WACMD_SET_LEVEL, mixer_reg[12], mixer_reg[13]);
1889                         break;
1890
1891                 case MIXER_PRIVATE3_READ:
1892                         spin_lock_irqsave(&waveartist_lock, flags);
1893
1894                         for (i = 0x30; i < 14 << 8; i += 1 << 8)
1895                                 waveartist_cmd(devc, 1, &i, 1, mixer_reg + (i >> 8));
1896
1897                         spin_unlock_irqrestore(&waveartist_lock, flags);
1898
1899                         if (copy_to_user((void *)val, mixer_reg, sizeof(mixer_reg)))
1900                                 return -EFAULT;
1901                         break;
1902
1903                 default:
1904                         return -EINVAL;
1905                 }
1906                 return 0;
1907         }
1908
1909         /* read back the state from PRIVATE1 */
1910         case SOUND_MIXER_PRIVATE4:
1911                 val = (devc->spkr_mute_state  ? VNC_MUTE_INTERNAL_SPKR : 0) |
1912                       (devc->line_mute_state  ? VNC_MUTE_LINE_OUT      : 0) |
1913                       (devc->handset_detect   ? VNC_HANDSET_DETECT     : 0) |
1914                       (devc->telephone_detect ? VNC_PHONE_DETECT       : 0) |
1915                       (devc->no_autoselect    ? VNC_DISABLE_AUTOSWITCH : 0);
1916
1917                 return put_user(val, arg) ? -EFAULT : 0;
1918         }
1919
1920         if (_SIOC_DIR(cmd) & _SIOC_WRITE) {
1921                 /*
1922                  * special case for master volume: if we
1923                  * received this call - switch from hw
1924                  * volume control to a software volume
1925                  * control, till the hw volume is modified
1926                  * to signal that user wants to be back in
1927                  * hardware...
1928                  */
1929                 if ((cmd & 0xff) == SOUND_MIXER_VOLUME)
1930                         devc->use_slider = 0;
1931
1932                 /* speaker output            */
1933                 if ((cmd & 0xff) == SOUND_MIXER_SPEAKER) {
1934                         unsigned int val, l, r;
1935
1936                         if (get_user(val, arg))
1937                                 return -EFAULT;
1938
1939                         l = val & 0x7f;
1940                         r = (val & 0x7f00) >> 8;
1941                         val = (l + r) / 2;
1942                         devc->levels[SOUND_MIXER_SPEAKER] = val | (val << 8);
1943                         devc->spkr_mute_state = (val <= 50);
1944                         vnc_mute_spkr(devc);
1945                         return 0;
1946                 }
1947         }
1948
1949         return -ENOIOCTLCMD;
1950 }
1951
1952 #endif
1953
1954 static struct address_info cfg;
1955
1956 static int attached;
1957
1958 static int __initdata io = 0;
1959 static int __initdata irq = 0;
1960 static int __initdata dma = 0;
1961 static int __initdata dma2 = 0;
1962
1963
1964 static int __init init_waveartist(void)
1965 {
1966         const struct waveartist_mixer_info *mix;
1967
1968         if (!io && machine_is_netwinder()) {
1969                 /*
1970                  * The NetWinder WaveArtist is at a fixed address.
1971                  * If the user does not supply an address, use the
1972                  * well-known parameters.
1973                  */
1974                 io   = 0x250;
1975                 irq  = 12;
1976                 dma  = 3;
1977                 dma2 = 7;
1978         }
1979
1980         mix = &waveartist_mixer;
1981 #ifdef CONFIG_ARCH_NETWINDER
1982         if (machine_is_netwinder())
1983                 mix = &netwinder_mixer;
1984 #endif
1985
1986         cfg.io_base = io;
1987         cfg.irq = irq;
1988         cfg.dma = dma;
1989         cfg.dma2 = dma2;
1990
1991         if (!probe_waveartist(&cfg))
1992                 return -ENODEV;
1993
1994         attach_waveartist(&cfg, mix);
1995         attached = 1;
1996
1997         return 0;
1998 }
1999
2000 static void __exit cleanup_waveartist(void)
2001 {
2002         if (attached)
2003                 unload_waveartist(&cfg);
2004 }
2005
2006 module_init(init_waveartist);
2007 module_exit(cleanup_waveartist);
2008
2009 #ifndef MODULE
2010 static int __init setup_waveartist(char *str)
2011 {
2012         /* io, irq, dma, dma2 */
2013         int ints[5];
2014         
2015         str = get_options(str, ARRAY_SIZE(ints), ints);
2016         
2017         io      = ints[1];
2018         irq     = ints[2];
2019         dma     = ints[3];
2020         dma2    = ints[4];
2021
2022         return 1;
2023 }
2024 __setup("waveartist=", setup_waveartist);
2025 #endif
2026
2027 MODULE_DESCRIPTION("Rockwell WaveArtist RWA-010 sound driver");
2028 module_param(io, int, 0);               /* IO base */
2029 module_param(irq, int, 0);              /* IRQ */
2030 module_param(dma, int, 0);              /* DMA */
2031 module_param(dma2, int, 0);             /* DMA2 */
2032 MODULE_LICENSE("GPL");