ext4: fix xfstest generic/299 block validity failures
[linux/fpc-iii.git] / sound / oss / waveartist.c
blob672af8b56542ddf8e04639dca07959dcb1d1ac79
1 /*
2 * linux/sound/oss/waveartist.c
4 * The low level driver for the RWA010 Rockwell Wave Artist
5 * codec chip used in the Rebel.com NetWinder.
7 * Cleaned up and integrated into 2.1 by Russell King (rmk@arm.linux.org.uk)
8 * and Pat Beirne (patb@corel.ca)
11 * Copyright (C) by Rebel.com 1998-1999
13 * RWA010 specs received under NDA from Rockwell
15 * Copyright (C) by Hannu Savolainen 1993-1997
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.
21 * Changes:
22 * 11-10-2000 Bartlomiej Zolnierkiewicz <bkz@linux-ide.org>
23 * Added __init to waveartist_init()
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
34 #define debug_flg (0)
36 #include <linux/module.h>
37 #include <linux/init.h>
38 #include <linux/slab.h>
39 #include <linux/sched.h>
40 #include <linux/interrupt.h>
41 #include <linux/delay.h>
42 #include <linux/spinlock.h>
43 #include <linux/bitops.h>
46 #include "sound_config.h"
47 #include "waveartist.h"
49 #ifdef CONFIG_ARM
50 #include <mach/hardware.h>
51 #include <asm/mach-types.h>
52 #endif
54 #ifndef NO_DMA
55 #define NO_DMA 255
56 #endif
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)
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 */
95 typedef struct {
96 struct address_info hw; /* hardware */
97 char *chip_name;
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;
107 /* Mixer parameters */
108 const struct waveartist_mixer_info *mix;
110 unsigned short *levels; /* cache of volume settings */
111 int recmask; /* currently enabled recording device! */
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;
125 * This is the implementation specific mixer information.
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 */
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);
139 typedef struct wavnc_port_info {
140 int open_mode;
141 int speed;
142 int channels;
143 int audio_format;
144 } wavnc_port_info;
146 static int nr_waveartist_devs;
147 static wavnc_info adev_info[MAX_AUDIO_DEV];
148 static DEFINE_SPINLOCK(waveartist_lock);
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
159 static inline void
160 waveartist_set_ctlr(struct address_info *hw, unsigned char clear, unsigned char set)
162 unsigned int ctlr_port = hw->io_base + CTLR;
164 clear = ~clear & inb(ctlr_port);
166 outb(clear | set, ctlr_port);
169 /* Toggle IRQ acknowledge line
171 static inline void
172 waveartist_iack(wavnc_info *devc)
174 unsigned int ctlr_port = devc->hw.io_base + CTLR;
175 int old_ctlr;
177 old_ctlr = inb(ctlr_port) & ~IRQ_ACK;
179 outb(old_ctlr | IRQ_ACK, ctlr_port);
180 outb(old_ctlr, ctlr_port);
183 static inline int
184 waveartist_sleep(int timeout_ms)
186 unsigned int timeout = msecs_to_jiffies(timeout_ms*100);
187 return schedule_timeout_interruptible(timeout);
190 static int
191 waveartist_reset(wavnc_info *devc)
193 struct address_info *hw = &devc->hw;
194 unsigned int timeout, res = -1;
196 waveartist_set_ctlr(hw, -1, RESET);
197 waveartist_sleep(2);
198 waveartist_set_ctlr(hw, RESET, 0);
200 timeout = 500;
201 do {
202 mdelay(2);
204 if (inb(hw->io_base + STATR) & CMD_RF) {
205 res = inw(hw->io_base + CMDR);
206 if (res == 0x55aa)
207 break;
209 } while (--timeout);
211 if (timeout == 0) {
212 printk(KERN_WARNING "WaveArtist: reset timeout ");
213 if (res != (unsigned int)-1)
214 printk("(res=%04X)", res);
215 printk("\n");
216 return 1;
218 return 0;
221 /* Helper function to send and receive words
222 * from WaveArtist. It handles all the handshaking
223 * and can send or receive multiple words.
225 static int
226 waveartist_cmd(wavnc_info *devc,
227 int nr_cmd, unsigned int *cmd,
228 int nr_resp, unsigned int *resp)
230 unsigned int io_base = devc->hw.io_base;
231 unsigned int timed_out = 0;
232 unsigned int i;
234 if (debug_flg & DEBUG_CMD) {
235 printk("waveartist_cmd: cmd=");
237 for (i = 0; i < nr_cmd; i++)
238 printk("%04X ", cmd[i]);
240 printk("\n");
243 if (inb(io_base + STATR) & CMD_RF) {
244 int old_data;
246 /* flush the port
249 old_data = inw(io_base + CMDR);
251 if (debug_flg & DEBUG_CMD)
252 printk("flushed %04X...", old_data);
254 udelay(10);
257 for (i = 0; !timed_out && i < nr_cmd; i++) {
258 int count;
260 for (count = 5000; count; count--)
261 if (inb(io_base + STATR) & CMD_WE)
262 break;
264 if (!count)
265 timed_out = 1;
266 else
267 outw(cmd[i], io_base + CMDR);
270 for (i = 0; !timed_out && i < nr_resp; i++) {
271 int count;
273 for (count = 5000; count; count--)
274 if (inb(io_base + STATR) & CMD_RF)
275 break;
277 if (!count)
278 timed_out = 1;
279 else
280 resp[i] = inw(io_base + CMDR);
283 if (debug_flg & DEBUG_CMD) {
284 if (!timed_out) {
285 printk("waveartist_cmd: resp=");
287 for (i = 0; i < nr_resp; i++)
288 printk("%04X ", resp[i]);
290 printk("\n");
291 } else
292 printk("waveartist_cmd: timed out\n");
295 return timed_out ? 1 : 0;
299 * Send one command word
301 static inline int
302 waveartist_cmd1(wavnc_info *devc, unsigned int cmd)
304 return waveartist_cmd(devc, 1, &cmd, 0, NULL);
308 * Send one command, receive one word
310 static inline unsigned int
311 waveartist_cmd1_r(wavnc_info *devc, unsigned int cmd)
313 unsigned int ret;
315 waveartist_cmd(devc, 1, &cmd, 1, &ret);
317 return ret;
321 * Send a double command, receive one
322 * word (and throw it away)
324 static inline int
325 waveartist_cmd2(wavnc_info *devc, unsigned int cmd, unsigned int arg)
327 unsigned int vals[2];
329 vals[0] = cmd;
330 vals[1] = arg;
332 return waveartist_cmd(devc, 2, vals, 1, vals);
336 * Send a triple command
338 static inline int
339 waveartist_cmd3(wavnc_info *devc, unsigned int cmd,
340 unsigned int arg1, unsigned int arg2)
342 unsigned int vals[3];
344 vals[0] = cmd;
345 vals[1] = arg1;
346 vals[2] = arg2;
348 return waveartist_cmd(devc, 3, vals, 0, NULL);
351 static int
352 waveartist_getrev(wavnc_info *devc, char *rev)
354 unsigned int temp[2];
355 unsigned int cmd = WACMD_GETREV;
357 waveartist_cmd(devc, 1, &cmd, 2, temp);
359 rev[0] = temp[0] >> 8;
360 rev[1] = temp[0] & 255;
361 rev[2] = '\0';
363 return temp[0];
366 static void waveartist_halt_output(int dev);
367 static void waveartist_halt_input(int dev);
368 static void waveartist_halt(int dev);
369 static void waveartist_trigger(int dev, int state);
371 static int
372 waveartist_open(int dev, int mode)
374 wavnc_info *devc;
375 wavnc_port_info *portc;
376 unsigned long flags;
378 if (dev < 0 || dev >= num_audiodevs)
379 return -ENXIO;
381 devc = (wavnc_info *) audio_devs[dev]->devc;
382 portc = (wavnc_port_info *) audio_devs[dev]->portc;
384 spin_lock_irqsave(&waveartist_lock, flags);
385 if (portc->open_mode || (devc->open_mode & mode)) {
386 spin_unlock_irqrestore(&waveartist_lock, flags);
387 return -EBUSY;
390 devc->audio_mode = 0;
391 devc->open_mode |= mode;
392 portc->open_mode = mode;
393 waveartist_trigger(dev, 0);
395 if (mode & OPEN_READ)
396 devc->record_dev = dev;
397 if (mode & OPEN_WRITE)
398 devc->playback_dev = dev;
399 spin_unlock_irqrestore(&waveartist_lock, flags);
401 return 0;
404 static void
405 waveartist_close(int dev)
407 wavnc_info *devc = (wavnc_info *) audio_devs[dev]->devc;
408 wavnc_port_info *portc = (wavnc_port_info *) audio_devs[dev]->portc;
409 unsigned long flags;
411 spin_lock_irqsave(&waveartist_lock, flags);
413 waveartist_halt(dev);
415 devc->audio_mode = 0;
416 devc->open_mode &= ~portc->open_mode;
417 portc->open_mode = 0;
419 spin_unlock_irqrestore(&waveartist_lock, flags);
422 static void
423 waveartist_output_block(int dev, unsigned long buf, int __count, int intrflag)
425 wavnc_port_info *portc = (wavnc_port_info *) audio_devs[dev]->portc;
426 wavnc_info *devc = (wavnc_info *) audio_devs[dev]->devc;
427 unsigned long flags;
428 unsigned int count = __count;
430 if (debug_flg & DEBUG_OUT)
431 printk("waveartist: output block, buf=0x%lx, count=0x%x...\n",
432 buf, count);
434 * 16 bit data
436 if (portc->audio_format & (AFMT_S16_LE | AFMT_S16_BE))
437 count >>= 1;
439 if (portc->channels > 1)
440 count >>= 1;
442 count -= 1;
444 if (devc->audio_mode & PCM_ENABLE_OUTPUT &&
445 audio_devs[dev]->flags & DMA_AUTOMODE &&
446 intrflag &&
447 count == devc->xfer_count) {
448 devc->audio_mode |= PCM_ENABLE_OUTPUT;
449 return; /*
450 * Auto DMA mode on. No need to react
454 spin_lock_irqsave(&waveartist_lock, flags);
457 * set sample count
459 waveartist_cmd2(devc, WACMD_OUTPUTSIZE, count);
461 devc->xfer_count = count;
462 devc->audio_mode |= PCM_ENABLE_OUTPUT;
464 spin_unlock_irqrestore(&waveartist_lock, flags);
467 static void
468 waveartist_start_input(int dev, unsigned long buf, int __count, int intrflag)
470 wavnc_port_info *portc = (wavnc_port_info *) audio_devs[dev]->portc;
471 wavnc_info *devc = (wavnc_info *) audio_devs[dev]->devc;
472 unsigned long flags;
473 unsigned int count = __count;
475 if (debug_flg & DEBUG_IN)
476 printk("waveartist: start input, buf=0x%lx, count=0x%x...\n",
477 buf, count);
479 if (portc->audio_format & (AFMT_S16_LE | AFMT_S16_BE)) /* 16 bit data */
480 count >>= 1;
482 if (portc->channels > 1)
483 count >>= 1;
485 count -= 1;
487 if (devc->audio_mode & PCM_ENABLE_INPUT &&
488 audio_devs[dev]->flags & DMA_AUTOMODE &&
489 intrflag &&
490 count == devc->xfer_count) {
491 devc->audio_mode |= PCM_ENABLE_INPUT;
492 return; /*
493 * Auto DMA mode on. No need to react
497 spin_lock_irqsave(&waveartist_lock, flags);
500 * set sample count
502 waveartist_cmd2(devc, WACMD_INPUTSIZE, count);
504 devc->xfer_count = count;
505 devc->audio_mode |= PCM_ENABLE_INPUT;
507 spin_unlock_irqrestore(&waveartist_lock, flags);
510 static int
511 waveartist_ioctl(int dev, unsigned int cmd, void __user * arg)
513 return -EINVAL;
516 static unsigned int
517 waveartist_get_speed(wavnc_port_info *portc)
519 unsigned int speed;
522 * program the speed, channels, bits
524 if (portc->speed == 8000)
525 speed = 0x2E71;
526 else if (portc->speed == 11025)
527 speed = 0x4000;
528 else if (portc->speed == 22050)
529 speed = 0x8000;
530 else if (portc->speed == 44100)
531 speed = 0x0;
532 else {
534 * non-standard - just calculate
536 speed = portc->speed << 16;
538 speed = (speed / 44100) & 65535;
541 return speed;
544 static unsigned int
545 waveartist_get_bits(wavnc_port_info *portc)
547 unsigned int bits;
549 if (portc->audio_format == AFMT_S16_LE)
550 bits = 1;
551 else if (portc->audio_format == AFMT_S8)
552 bits = 0;
553 else
554 bits = 2; //default AFMT_U8
556 return bits;
559 static int
560 waveartist_prepare_for_input(int dev, int bsize, int bcount)
562 unsigned long flags;
563 wavnc_info *devc = (wavnc_info *) audio_devs[dev]->devc;
564 wavnc_port_info *portc = (wavnc_port_info *) audio_devs[dev]->portc;
565 unsigned int speed, bits;
567 if (devc->audio_mode)
568 return 0;
570 speed = waveartist_get_speed(portc);
571 bits = waveartist_get_bits(portc);
573 spin_lock_irqsave(&waveartist_lock, flags);
575 if (waveartist_cmd2(devc, WACMD_INPUTFORMAT, bits))
576 printk(KERN_WARNING "waveartist: error setting the "
577 "record format to %d\n", portc->audio_format);
579 if (waveartist_cmd2(devc, WACMD_INPUTCHANNELS, portc->channels))
580 printk(KERN_WARNING "waveartist: error setting record "
581 "to %d channels\n", portc->channels);
584 * write cmd SetSampleSpeedTimeConstant
586 if (waveartist_cmd2(devc, WACMD_INPUTSPEED, speed))
587 printk(KERN_WARNING "waveartist: error setting the record "
588 "speed to %dHz.\n", portc->speed);
590 if (waveartist_cmd2(devc, WACMD_INPUTDMA, 1))
591 printk(KERN_WARNING "waveartist: error setting the record "
592 "data path to 0x%X\n", 1);
594 if (waveartist_cmd2(devc, WACMD_INPUTFORMAT, bits))
595 printk(KERN_WARNING "waveartist: error setting the record "
596 "format to %d\n", portc->audio_format);
598 devc->xfer_count = 0;
599 spin_unlock_irqrestore(&waveartist_lock, flags);
600 waveartist_halt_input(dev);
602 if (debug_flg & DEBUG_INTR) {
603 printk("WA CTLR reg: 0x%02X.\n",
604 inb(devc->hw.io_base + CTLR));
605 printk("WA STAT reg: 0x%02X.\n",
606 inb(devc->hw.io_base + STATR));
607 printk("WA IRQS reg: 0x%02X.\n",
608 inb(devc->hw.io_base + IRQSTAT));
611 return 0;
614 static int
615 waveartist_prepare_for_output(int dev, int bsize, int bcount)
617 unsigned long flags;
618 wavnc_info *devc = (wavnc_info *) audio_devs[dev]->devc;
619 wavnc_port_info *portc = (wavnc_port_info *) audio_devs[dev]->portc;
620 unsigned int speed, bits;
623 * program the speed, channels, bits
625 speed = waveartist_get_speed(portc);
626 bits = waveartist_get_bits(portc);
628 spin_lock_irqsave(&waveartist_lock, flags);
630 if (waveartist_cmd2(devc, WACMD_OUTPUTSPEED, speed) &&
631 waveartist_cmd2(devc, WACMD_OUTPUTSPEED, speed))
632 printk(KERN_WARNING "waveartist: error setting the playback "
633 "speed to %dHz.\n", portc->speed);
635 if (waveartist_cmd2(devc, WACMD_OUTPUTCHANNELS, portc->channels))
636 printk(KERN_WARNING "waveartist: error setting the playback "
637 "to %d channels\n", portc->channels);
639 if (waveartist_cmd2(devc, WACMD_OUTPUTDMA, 0))
640 printk(KERN_WARNING "waveartist: error setting the playback "
641 "data path to 0x%X\n", 0);
643 if (waveartist_cmd2(devc, WACMD_OUTPUTFORMAT, bits))
644 printk(KERN_WARNING "waveartist: error setting the playback "
645 "format to %d\n", portc->audio_format);
647 devc->xfer_count = 0;
648 spin_unlock_irqrestore(&waveartist_lock, flags);
649 waveartist_halt_output(dev);
651 if (debug_flg & DEBUG_INTR) {
652 printk("WA CTLR reg: 0x%02X.\n",inb(devc->hw.io_base + CTLR));
653 printk("WA STAT reg: 0x%02X.\n",inb(devc->hw.io_base + STATR));
654 printk("WA IRQS reg: 0x%02X.\n",inb(devc->hw.io_base + IRQSTAT));
657 return 0;
660 static void
661 waveartist_halt(int dev)
663 wavnc_port_info *portc = (wavnc_port_info *) audio_devs[dev]->portc;
664 wavnc_info *devc;
666 if (portc->open_mode & OPEN_WRITE)
667 waveartist_halt_output(dev);
669 if (portc->open_mode & OPEN_READ)
670 waveartist_halt_input(dev);
672 devc = (wavnc_info *) audio_devs[dev]->devc;
673 devc->audio_mode = 0;
676 static void
677 waveartist_halt_input(int dev)
679 wavnc_info *devc = (wavnc_info *) audio_devs[dev]->devc;
680 unsigned long flags;
682 spin_lock_irqsave(&waveartist_lock, flags);
685 * Stop capture
687 waveartist_cmd1(devc, WACMD_INPUTSTOP);
689 devc->audio_mode &= ~PCM_ENABLE_INPUT;
692 * Clear interrupt by toggling
693 * the IRQ_ACK bit in CTRL
695 if (inb(devc->hw.io_base + STATR) & IRQ_REQ)
696 waveartist_iack(devc);
698 // devc->audio_mode &= ~PCM_ENABLE_INPUT;
700 spin_unlock_irqrestore(&waveartist_lock, flags);
703 static void
704 waveartist_halt_output(int dev)
706 wavnc_info *devc = (wavnc_info *) audio_devs[dev]->devc;
707 unsigned long flags;
709 spin_lock_irqsave(&waveartist_lock, flags);
711 waveartist_cmd1(devc, WACMD_OUTPUTSTOP);
713 devc->audio_mode &= ~PCM_ENABLE_OUTPUT;
716 * Clear interrupt by toggling
717 * the IRQ_ACK bit in CTRL
719 if (inb(devc->hw.io_base + STATR) & IRQ_REQ)
720 waveartist_iack(devc);
722 // devc->audio_mode &= ~PCM_ENABLE_OUTPUT;
724 spin_unlock_irqrestore(&waveartist_lock, flags);
727 static void
728 waveartist_trigger(int dev, int state)
730 wavnc_info *devc = (wavnc_info *) audio_devs[dev]->devc;
731 wavnc_port_info *portc = (wavnc_port_info *) audio_devs[dev]->portc;
732 unsigned long flags;
734 if (debug_flg & DEBUG_TRIGGER) {
735 printk("wavnc: audio trigger ");
736 if (state & PCM_ENABLE_INPUT)
737 printk("in ");
738 if (state & PCM_ENABLE_OUTPUT)
739 printk("out");
740 printk("\n");
743 spin_lock_irqsave(&waveartist_lock, flags);
745 state &= devc->audio_mode;
747 if (portc->open_mode & OPEN_READ &&
748 state & PCM_ENABLE_INPUT)
750 * enable ADC Data Transfer to PC
752 waveartist_cmd1(devc, WACMD_INPUTSTART);
754 if (portc->open_mode & OPEN_WRITE &&
755 state & PCM_ENABLE_OUTPUT)
757 * enable DAC data transfer from PC
759 waveartist_cmd1(devc, WACMD_OUTPUTSTART);
761 spin_unlock_irqrestore(&waveartist_lock, flags);
764 static int
765 waveartist_set_speed(int dev, int arg)
767 wavnc_port_info *portc = (wavnc_port_info *) audio_devs[dev]->portc;
769 if (arg <= 0)
770 return portc->speed;
772 if (arg < 5000)
773 arg = 5000;
774 if (arg > 44100)
775 arg = 44100;
777 portc->speed = arg;
778 return portc->speed;
782 static short
783 waveartist_set_channels(int dev, short arg)
785 wavnc_port_info *portc = (wavnc_port_info *) audio_devs[dev]->portc;
787 if (arg != 1 && arg != 2)
788 return portc->channels;
790 portc->channels = arg;
791 return arg;
794 static unsigned int
795 waveartist_set_bits(int dev, unsigned int arg)
797 wavnc_port_info *portc = (wavnc_port_info *) audio_devs[dev]->portc;
799 if (arg == 0)
800 return portc->audio_format;
802 if ((arg != AFMT_U8) && (arg != AFMT_S16_LE) && (arg != AFMT_S8))
803 arg = AFMT_U8;
805 portc->audio_format = arg;
807 return arg;
810 static struct audio_driver waveartist_audio_driver = {
811 .owner = THIS_MODULE,
812 .open = waveartist_open,
813 .close = waveartist_close,
814 .output_block = waveartist_output_block,
815 .start_input = waveartist_start_input,
816 .ioctl = waveartist_ioctl,
817 .prepare_for_input = waveartist_prepare_for_input,
818 .prepare_for_output = waveartist_prepare_for_output,
819 .halt_io = waveartist_halt,
820 .halt_input = waveartist_halt_input,
821 .halt_output = waveartist_halt_output,
822 .trigger = waveartist_trigger,
823 .set_speed = waveartist_set_speed,
824 .set_bits = waveartist_set_bits,
825 .set_channels = waveartist_set_channels
829 static irqreturn_t
830 waveartist_intr(int irq, void *dev_id)
832 wavnc_info *devc = dev_id;
833 int irqstatus, status;
835 spin_lock(&waveartist_lock);
836 irqstatus = inb(devc->hw.io_base + IRQSTAT);
837 status = inb(devc->hw.io_base + STATR);
839 if (debug_flg & DEBUG_INTR)
840 printk("waveartist_intr: stat=%02x, irqstat=%02x\n",
841 status, irqstatus);
843 if (status & IRQ_REQ) /* Clear interrupt */
844 waveartist_iack(devc);
845 else
846 printk(KERN_WARNING "waveartist: unexpected interrupt\n");
848 if (irqstatus & 0x01) {
849 int temp = 1;
851 /* PCM buffer done
853 if ((status & DMA0) && (devc->audio_mode & PCM_ENABLE_OUTPUT)) {
854 DMAbuf_outputintr(devc->playback_dev, 1);
855 temp = 0;
857 if ((status & DMA1) && (devc->audio_mode & PCM_ENABLE_INPUT)) {
858 DMAbuf_inputintr(devc->record_dev);
859 temp = 0;
861 if (temp) //default:
862 printk(KERN_WARNING "waveartist: Unknown interrupt\n");
864 if (irqstatus & 0x2)
865 // We do not use SB mode natively...
866 printk(KERN_WARNING "waveartist: Unexpected SB interrupt...\n");
867 spin_unlock(&waveartist_lock);
868 return IRQ_HANDLED;
871 /* -------------------------------------------------------------------------
872 * Mixer stuff
874 struct mix_ent {
875 unsigned char reg_l;
876 unsigned char reg_r;
877 unsigned char shift;
878 unsigned char max;
881 static const struct mix_ent mix_devs[SOUND_MIXER_NRDEVICES] = {
882 { 2, 6, 1, 7 }, /* SOUND_MIXER_VOLUME */
883 { 0, 0, 0, 0 }, /* SOUND_MIXER_BASS */
884 { 0, 0, 0, 0 }, /* SOUND_MIXER_TREBLE */
885 { 0, 0, 0, 0 }, /* SOUND_MIXER_SYNTH */
886 { 0, 0, 0, 0 }, /* SOUND_MIXER_PCM */
887 { 0, 0, 0, 0 }, /* SOUND_MIXER_SPEAKER */
888 { 0, 4, 6, 31 }, /* SOUND_MIXER_LINE */
889 { 2, 6, 4, 3 }, /* SOUND_MIXER_MIC */
890 { 0, 0, 0, 0 }, /* SOUND_MIXER_CD */
891 { 0, 0, 0, 0 }, /* SOUND_MIXER_IMIX */
892 { 0, 0, 0, 0 }, /* SOUND_MIXER_ALTPCM */
893 #if 0
894 { 3, 7, 0, 10 }, /* SOUND_MIXER_RECLEV */
895 { 0, 0, 0, 0 }, /* SOUND_MIXER_IGAIN */
896 #else
897 { 0, 0, 0, 0 }, /* SOUND_MIXER_RECLEV */
898 { 3, 7, 0, 7 }, /* SOUND_MIXER_IGAIN */
899 #endif
900 { 0, 0, 0, 0 }, /* SOUND_MIXER_OGAIN */
901 { 0, 4, 1, 31 }, /* SOUND_MIXER_LINE1 */
902 { 1, 5, 6, 31 }, /* SOUND_MIXER_LINE2 */
903 { 0, 0, 0, 0 }, /* SOUND_MIXER_LINE3 */
904 { 0, 0, 0, 0 }, /* SOUND_MIXER_DIGITAL1 */
905 { 0, 0, 0, 0 }, /* SOUND_MIXER_DIGITAL2 */
906 { 0, 0, 0, 0 }, /* SOUND_MIXER_DIGITAL3 */
907 { 0, 0, 0, 0 }, /* SOUND_MIXER_PHONEIN */
908 { 0, 0, 0, 0 }, /* SOUND_MIXER_PHONEOUT */
909 { 0, 0, 0, 0 }, /* SOUND_MIXER_VIDEO */
910 { 0, 0, 0, 0 }, /* SOUND_MIXER_RADIO */
911 { 0, 0, 0, 0 } /* SOUND_MIXER_MONITOR */
914 static void
915 waveartist_mixer_update(wavnc_info *devc, int whichDev)
917 unsigned int lev_left, lev_right;
919 lev_left = devc->levels[whichDev] & 0xff;
920 lev_right = devc->levels[whichDev] >> 8;
922 if (lev_left > 100)
923 lev_left = 100;
924 if (lev_right > 100)
925 lev_right = 100;
927 #define SCALE(lev,max) ((lev) * (max) / 100)
929 if (machine_is_netwinder() && whichDev == SOUND_MIXER_PHONEOUT)
930 whichDev = SOUND_MIXER_VOLUME;
932 if (mix_devs[whichDev].reg_l || mix_devs[whichDev].reg_r) {
933 const struct mix_ent *mix = mix_devs + whichDev;
934 unsigned int mask, left, right;
936 mask = mix->max << mix->shift;
937 lev_left = SCALE(lev_left, mix->max) << mix->shift;
938 lev_right = SCALE(lev_right, mix->max) << mix->shift;
940 /* read left setting */
941 left = waveartist_cmd1_r(devc, WACMD_GET_LEVEL |
942 mix->reg_l << 8);
944 /* read right setting */
945 right = waveartist_cmd1_r(devc, WACMD_GET_LEVEL |
946 mix->reg_r << 8);
948 left = (left & ~mask) | (lev_left & mask);
949 right = (right & ~mask) | (lev_right & mask);
951 /* write left,right back */
952 waveartist_cmd3(devc, WACMD_SET_MIXER, left, right);
953 } else {
954 switch(whichDev) {
955 case SOUND_MIXER_PCM:
956 waveartist_cmd3(devc, WACMD_SET_LEVEL,
957 SCALE(lev_left, 32767),
958 SCALE(lev_right, 32767));
959 break;
961 case SOUND_MIXER_SYNTH:
962 waveartist_cmd3(devc, 0x0100 | WACMD_SET_LEVEL,
963 SCALE(lev_left, 32767),
964 SCALE(lev_right, 32767));
965 break;
971 * Set the ADC MUX to the specified values. We do NOT do any
972 * checking of the values passed, since we assume that the
973 * relevant *_select_input function has done that for us.
975 static void
976 waveartist_set_adc_mux(wavnc_info *devc, char left_dev, char right_dev)
978 unsigned int reg_08, reg_09;
980 reg_08 = waveartist_cmd1_r(devc, WACMD_GET_LEVEL | 0x0800);
981 reg_09 = waveartist_cmd1_r(devc, WACMD_GET_LEVEL | 0x0900);
983 reg_08 = (reg_08 & ~0x3f) | right_dev << 3 | left_dev;
985 waveartist_cmd3(devc, WACMD_SET_MIXER, reg_08, reg_09);
989 * Decode a recording mask into a mixer selection as follows:
991 * OSS Source WA Source Actual source
992 * SOUND_MASK_IMIX Mixer Mixer output (same as AD1848)
993 * SOUND_MASK_LINE Line Line in
994 * SOUND_MASK_LINE1 Aux 1 Aux 1 in
995 * SOUND_MASK_LINE2 Aux 2 Aux 2 in
996 * SOUND_MASK_MIC Mic Microphone
998 static unsigned int
999 waveartist_select_input(wavnc_info *devc, unsigned int recmask,
1000 unsigned char *dev_l, unsigned char *dev_r)
1002 unsigned int recdev = ADC_MUX_NONE;
1004 if (recmask & SOUND_MASK_IMIX) {
1005 recmask = SOUND_MASK_IMIX;
1006 recdev = ADC_MUX_MIXER;
1007 } else if (recmask & SOUND_MASK_LINE2) {
1008 recmask = SOUND_MASK_LINE2;
1009 recdev = ADC_MUX_AUX2;
1010 } else if (recmask & SOUND_MASK_LINE1) {
1011 recmask = SOUND_MASK_LINE1;
1012 recdev = ADC_MUX_AUX1;
1013 } else if (recmask & SOUND_MASK_LINE) {
1014 recmask = SOUND_MASK_LINE;
1015 recdev = ADC_MUX_LINE;
1016 } else if (recmask & SOUND_MASK_MIC) {
1017 recmask = SOUND_MASK_MIC;
1018 recdev = ADC_MUX_MIC;
1021 *dev_l = *dev_r = recdev;
1023 return recmask;
1026 static int
1027 waveartist_decode_mixer(wavnc_info *devc, int dev, unsigned char lev_l,
1028 unsigned char lev_r)
1030 switch (dev) {
1031 case SOUND_MIXER_VOLUME:
1032 case SOUND_MIXER_SYNTH:
1033 case SOUND_MIXER_PCM:
1034 case SOUND_MIXER_LINE:
1035 case SOUND_MIXER_MIC:
1036 case SOUND_MIXER_IGAIN:
1037 case SOUND_MIXER_LINE1:
1038 case SOUND_MIXER_LINE2:
1039 devc->levels[dev] = lev_l | lev_r << 8;
1040 break;
1042 case SOUND_MIXER_IMIX:
1043 break;
1045 default:
1046 dev = -EINVAL;
1047 break;
1050 return dev;
1053 static int waveartist_get_mixer(wavnc_info *devc, int dev)
1055 return devc->levels[dev];
1058 static const struct waveartist_mixer_info waveartist_mixer = {
1059 .supported_devs = SUPPORTED_MIXER_DEVICES | SOUND_MASK_IGAIN,
1060 .recording_devs = SOUND_MASK_LINE | SOUND_MASK_MIC |
1061 SOUND_MASK_LINE1 | SOUND_MASK_LINE2 |
1062 SOUND_MASK_IMIX,
1063 .stereo_devs = (SUPPORTED_MIXER_DEVICES | SOUND_MASK_IGAIN) & ~
1064 (SOUND_MASK_SPEAKER | SOUND_MASK_IMIX),
1065 .select_input = waveartist_select_input,
1066 .decode_mixer = waveartist_decode_mixer,
1067 .get_mixer = waveartist_get_mixer,
1070 static void
1071 waveartist_set_recmask(wavnc_info *devc, unsigned int recmask)
1073 unsigned char dev_l, dev_r;
1075 recmask &= devc->mix->recording_devs;
1078 * If more than one recording device selected,
1079 * disable the device that is currently in use.
1081 if (hweight32(recmask) > 1)
1082 recmask &= ~devc->recmask;
1085 * Translate the recording device mask into
1086 * the ADC multiplexer settings.
1088 devc->recmask = devc->mix->select_input(devc, recmask,
1089 &dev_l, &dev_r);
1091 waveartist_set_adc_mux(devc, dev_l, dev_r);
1094 static int
1095 waveartist_set_mixer(wavnc_info *devc, int dev, unsigned int level)
1097 unsigned int lev_left = level & 0x00ff;
1098 unsigned int lev_right = (level & 0xff00) >> 8;
1100 if (lev_left > 100)
1101 lev_left = 100;
1102 if (lev_right > 100)
1103 lev_right = 100;
1106 * Mono devices have their right volume forced to their
1107 * left volume. (from ALSA driver OSS emulation).
1109 if (!(devc->mix->stereo_devs & (1 << dev)))
1110 lev_right = lev_left;
1112 dev = devc->mix->decode_mixer(devc, dev, lev_left, lev_right);
1114 if (dev >= 0)
1115 waveartist_mixer_update(devc, dev);
1117 return dev < 0 ? dev : 0;
1120 static int
1121 waveartist_mixer_ioctl(int dev, unsigned int cmd, void __user * arg)
1123 wavnc_info *devc = (wavnc_info *)audio_devs[dev]->devc;
1124 int ret = 0, val, nr;
1127 * All SOUND_MIXER_* ioctls use type 'M'
1129 if (((cmd >> 8) & 255) != 'M')
1130 return -ENOIOCTLCMD;
1132 #ifdef CONFIG_ARCH_NETWINDER
1133 if (machine_is_netwinder()) {
1134 ret = vnc_private_ioctl(dev, cmd, arg);
1135 if (ret != -ENOIOCTLCMD)
1136 return ret;
1137 else
1138 ret = 0;
1140 #endif
1142 nr = cmd & 0xff;
1144 if (_SIOC_DIR(cmd) & _SIOC_WRITE) {
1145 if (get_user(val, (int __user *)arg))
1146 return -EFAULT;
1148 switch (nr) {
1149 case SOUND_MIXER_RECSRC:
1150 waveartist_set_recmask(devc, val);
1151 break;
1153 default:
1154 ret = -EINVAL;
1155 if (nr < SOUND_MIXER_NRDEVICES &&
1156 devc->mix->supported_devs & (1 << nr))
1157 ret = waveartist_set_mixer(devc, nr, val);
1161 if (ret == 0 && _SIOC_DIR(cmd) & _SIOC_READ) {
1162 ret = -EINVAL;
1164 switch (nr) {
1165 case SOUND_MIXER_RECSRC:
1166 ret = devc->recmask;
1167 break;
1169 case SOUND_MIXER_DEVMASK:
1170 ret = devc->mix->supported_devs;
1171 break;
1173 case SOUND_MIXER_STEREODEVS:
1174 ret = devc->mix->stereo_devs;
1175 break;
1177 case SOUND_MIXER_RECMASK:
1178 ret = devc->mix->recording_devs;
1179 break;
1181 case SOUND_MIXER_CAPS:
1182 ret = SOUND_CAP_EXCL_INPUT;
1183 break;
1185 default:
1186 if (nr < SOUND_MIXER_NRDEVICES)
1187 ret = devc->mix->get_mixer(devc, nr);
1188 break;
1191 if (ret >= 0)
1192 ret = put_user(ret, (int __user *)arg) ? -EFAULT : 0;
1195 return ret;
1198 static struct mixer_operations waveartist_mixer_operations =
1200 .owner = THIS_MODULE,
1201 .id = "WaveArtist",
1202 .name = "WaveArtist",
1203 .ioctl = waveartist_mixer_ioctl
1206 static void
1207 waveartist_mixer_reset(wavnc_info *devc)
1209 int i;
1211 if (debug_flg & DEBUG_MIXER)
1212 printk("%s: mixer_reset\n", devc->hw.name);
1215 * reset mixer cmd
1217 waveartist_cmd1(devc, WACMD_RST_MIXER);
1220 * set input for ADC to come from 'quiet'
1221 * turn on default modes
1223 waveartist_cmd3(devc, WACMD_SET_MIXER, 0x9800, 0xa836);
1226 * set mixer input select to none, RX filter gains 0 dB
1228 waveartist_cmd3(devc, WACMD_SET_MIXER, 0x4c00, 0x8c00);
1231 * set bit 0 reg 2 to 1 - unmute MonoOut
1233 waveartist_cmd3(devc, WACMD_SET_MIXER, 0x2801, 0x6800);
1235 /* set default input device = internal mic
1236 * current recording device = none
1238 waveartist_set_recmask(devc, 0);
1240 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
1241 waveartist_mixer_update(devc, i);
1244 static int __init waveartist_init(wavnc_info *devc)
1246 wavnc_port_info *portc;
1247 char rev[3], dev_name[64];
1248 int my_dev;
1250 if (waveartist_reset(devc))
1251 return -ENODEV;
1253 sprintf(dev_name, "%s (%s", devc->hw.name, devc->chip_name);
1255 if (waveartist_getrev(devc, rev)) {
1256 strcat(dev_name, " rev. ");
1257 strcat(dev_name, rev);
1259 strcat(dev_name, ")");
1261 conf_printf2(dev_name, devc->hw.io_base, devc->hw.irq,
1262 devc->hw.dma, devc->hw.dma2);
1264 portc = kzalloc(sizeof(wavnc_port_info), GFP_KERNEL);
1265 if (portc == NULL)
1266 goto nomem;
1268 my_dev = sound_install_audiodrv(AUDIO_DRIVER_VERSION, dev_name,
1269 &waveartist_audio_driver, sizeof(struct audio_driver),
1270 devc->audio_flags, AFMT_U8 | AFMT_S16_LE | AFMT_S8,
1271 devc, devc->hw.dma, devc->hw.dma2);
1273 if (my_dev < 0)
1274 goto free;
1276 audio_devs[my_dev]->portc = portc;
1278 waveartist_mixer_reset(devc);
1281 * clear any pending interrupt
1283 waveartist_iack(devc);
1285 if (request_irq(devc->hw.irq, waveartist_intr, 0, devc->hw.name, devc) < 0) {
1286 printk(KERN_ERR "%s: IRQ %d in use\n",
1287 devc->hw.name, devc->hw.irq);
1288 goto uninstall;
1291 if (sound_alloc_dma(devc->hw.dma, devc->hw.name)) {
1292 printk(KERN_ERR "%s: Can't allocate DMA%d\n",
1293 devc->hw.name, devc->hw.dma);
1294 goto uninstall_irq;
1297 if (devc->hw.dma != devc->hw.dma2 && devc->hw.dma2 != NO_DMA)
1298 if (sound_alloc_dma(devc->hw.dma2, devc->hw.name)) {
1299 printk(KERN_ERR "%s: can't allocate DMA%d\n",
1300 devc->hw.name, devc->hw.dma2);
1301 goto uninstall_dma;
1304 waveartist_set_ctlr(&devc->hw, 0, DMA1_IE | DMA0_IE);
1306 audio_devs[my_dev]->mixer_dev =
1307 sound_install_mixer(MIXER_DRIVER_VERSION,
1308 dev_name,
1309 &waveartist_mixer_operations,
1310 sizeof(struct mixer_operations),
1311 devc);
1313 return my_dev;
1315 uninstall_dma:
1316 sound_free_dma(devc->hw.dma);
1318 uninstall_irq:
1319 free_irq(devc->hw.irq, devc);
1321 uninstall:
1322 sound_unload_audiodev(my_dev);
1324 free:
1325 kfree(portc);
1327 nomem:
1328 return -1;
1331 static int __init probe_waveartist(struct address_info *hw_config)
1333 wavnc_info *devc = &adev_info[nr_waveartist_devs];
1335 if (nr_waveartist_devs >= MAX_AUDIO_DEV) {
1336 printk(KERN_WARNING "waveartist: too many audio devices\n");
1337 return 0;
1340 if (!request_region(hw_config->io_base, 15, hw_config->name)) {
1341 printk(KERN_WARNING "WaveArtist: I/O port conflict\n");
1342 return 0;
1345 if (hw_config->irq > 15 || hw_config->irq < 0) {
1346 release_region(hw_config->io_base, 15);
1347 printk(KERN_WARNING "WaveArtist: Bad IRQ %d\n",
1348 hw_config->irq);
1349 return 0;
1352 if (hw_config->dma != 3) {
1353 release_region(hw_config->io_base, 15);
1354 printk(KERN_WARNING "WaveArtist: Bad DMA %d\n",
1355 hw_config->dma);
1356 return 0;
1359 hw_config->name = "WaveArtist";
1360 devc->hw = *hw_config;
1361 devc->open_mode = 0;
1362 devc->chip_name = "RWA-010";
1364 return 1;
1367 static void __init
1368 attach_waveartist(struct address_info *hw, const struct waveartist_mixer_info *mix)
1370 wavnc_info *devc = &adev_info[nr_waveartist_devs];
1373 * NOTE! If irq < 0, there is another driver which has allocated the
1374 * IRQ so that this driver doesn't need to allocate/deallocate it.
1375 * The actually used IRQ is ABS(irq).
1377 devc->hw = *hw;
1378 devc->hw.irq = (hw->irq > 0) ? hw->irq : 0;
1379 devc->open_mode = 0;
1380 devc->playback_dev = 0;
1381 devc->record_dev = 0;
1382 devc->audio_flags = DMA_AUTOMODE;
1383 devc->levels = levels;
1385 if (hw->dma != hw->dma2 && hw->dma2 != NO_DMA)
1386 devc->audio_flags |= DMA_DUPLEX;
1388 devc->mix = mix;
1389 devc->dev_no = waveartist_init(devc);
1391 if (devc->dev_no < 0)
1392 release_region(hw->io_base, 15);
1393 else {
1394 #ifdef CONFIG_ARCH_NETWINDER
1395 if (machine_is_netwinder()) {
1396 init_timer(&vnc_timer);
1397 vnc_timer.function = vnc_slider_tick;
1398 vnc_timer.expires = jiffies;
1399 vnc_timer.data = nr_waveartist_devs;
1400 add_timer(&vnc_timer);
1402 vnc_configure_mixer(devc, 0);
1404 devc->no_autoselect = 1;
1406 #endif
1407 nr_waveartist_devs += 1;
1411 static void __exit unload_waveartist(struct address_info *hw)
1413 wavnc_info *devc = NULL;
1414 int i;
1416 for (i = 0; i < nr_waveartist_devs; i++)
1417 if (hw->io_base == adev_info[i].hw.io_base) {
1418 devc = adev_info + i;
1419 break;
1422 if (devc != NULL) {
1423 int mixer;
1425 #ifdef CONFIG_ARCH_NETWINDER
1426 if (machine_is_netwinder())
1427 del_timer(&vnc_timer);
1428 #endif
1430 release_region(devc->hw.io_base, 15);
1432 waveartist_set_ctlr(&devc->hw, DMA1_IE|DMA0_IE, 0);
1434 if (devc->hw.irq >= 0)
1435 free_irq(devc->hw.irq, devc);
1437 sound_free_dma(devc->hw.dma);
1439 if (devc->hw.dma != devc->hw.dma2 &&
1440 devc->hw.dma2 != NO_DMA)
1441 sound_free_dma(devc->hw.dma2);
1443 mixer = audio_devs[devc->dev_no]->mixer_dev;
1445 if (mixer >= 0)
1446 sound_unload_mixerdev(mixer);
1448 if (devc->dev_no >= 0)
1449 sound_unload_audiodev(devc->dev_no);
1451 nr_waveartist_devs -= 1;
1453 for (; i < nr_waveartist_devs; i++)
1454 adev_info[i] = adev_info[i + 1];
1455 } else
1456 printk(KERN_WARNING "waveartist: can't find device "
1457 "to unload\n");
1460 #ifdef CONFIG_ARCH_NETWINDER
1463 * Rebel.com Netwinder specifics...
1466 #include <asm/hardware/dec21285.h>
1468 #define VNC_TIMER_PERIOD (HZ/4) //check slider 4 times/sec
1470 #define MIXER_PRIVATE3_RESET 0x53570000
1471 #define MIXER_PRIVATE3_READ 0x53570001
1472 #define MIXER_PRIVATE3_WRITE 0x53570002
1474 #define VNC_MUTE_INTERNAL_SPKR 0x01 //the sw mute on/off control bit
1475 #define VNC_MUTE_LINE_OUT 0x10
1476 #define VNC_PHONE_DETECT 0x20
1477 #define VNC_HANDSET_DETECT 0x40
1478 #define VNC_DISABLE_AUTOSWITCH 0x80
1480 static inline void
1481 vnc_mute_spkr(wavnc_info *devc)
1483 unsigned long flags;
1485 raw_spin_lock_irqsave(&nw_gpio_lock, flags);
1486 nw_cpld_modify(CPLD_UNMUTE, devc->spkr_mute_state ? 0 : CPLD_UNMUTE);
1487 raw_spin_unlock_irqrestore(&nw_gpio_lock, flags);
1490 static void
1491 vnc_mute_lout(wavnc_info *devc)
1493 unsigned int left, right;
1495 left = waveartist_cmd1_r(devc, WACMD_GET_LEVEL);
1496 right = waveartist_cmd1_r(devc, WACMD_GET_LEVEL | 0x400);
1498 if (devc->line_mute_state) {
1499 left &= ~1;
1500 right &= ~1;
1501 } else {
1502 left |= 1;
1503 right |= 1;
1505 waveartist_cmd3(devc, WACMD_SET_MIXER, left, right);
1509 static int
1510 vnc_volume_slider(wavnc_info *devc)
1512 static signed int old_slider_volume;
1513 unsigned long flags;
1514 signed int volume = 255;
1516 *CSR_TIMER1_LOAD = 0x00ffffff;
1518 spin_lock_irqsave(&waveartist_lock, flags);
1520 outb(0xFF, 0x201);
1521 *CSR_TIMER1_CNTL = TIMER_CNTL_ENABLE | TIMER_CNTL_DIV1;
1523 while (volume && (inb(0x201) & 0x01))
1524 volume--;
1526 *CSR_TIMER1_CNTL = 0;
1528 spin_unlock_irqrestore(&waveartist_lock,flags);
1530 volume = 0x00ffffff - *CSR_TIMER1_VALUE;
1533 #ifndef REVERSE
1534 volume = 150 - (volume >> 5);
1535 #else
1536 volume = (volume >> 6) - 25;
1537 #endif
1539 if (volume < 0)
1540 volume = 0;
1542 if (volume > 100)
1543 volume = 100;
1546 * slider quite often reads +-8, so debounce this random noise
1548 if (abs(volume - old_slider_volume) > 7) {
1549 old_slider_volume = volume;
1551 if (debug_flg & DEBUG_MIXER)
1552 printk(KERN_DEBUG "Slider volume: %d.\n", volume);
1555 return old_slider_volume;
1559 * Decode a recording mask into a mixer selection on the NetWinder
1560 * as follows:
1562 * OSS Source WA Source Actual source
1563 * SOUND_MASK_IMIX Mixer Mixer output (same as AD1848)
1564 * SOUND_MASK_LINE Line Line in
1565 * SOUND_MASK_LINE1 Left Mic Handset
1566 * SOUND_MASK_PHONEIN Left Aux Telephone microphone
1567 * SOUND_MASK_MIC Right Mic Builtin microphone
1569 static unsigned int
1570 netwinder_select_input(wavnc_info *devc, unsigned int recmask,
1571 unsigned char *dev_l, unsigned char *dev_r)
1573 unsigned int recdev_l = ADC_MUX_NONE, recdev_r = ADC_MUX_NONE;
1575 if (recmask & SOUND_MASK_IMIX) {
1576 recmask = SOUND_MASK_IMIX;
1577 recdev_l = ADC_MUX_MIXER;
1578 recdev_r = ADC_MUX_MIXER;
1579 } else if (recmask & SOUND_MASK_LINE) {
1580 recmask = SOUND_MASK_LINE;
1581 recdev_l = ADC_MUX_LINE;
1582 recdev_r = ADC_MUX_LINE;
1583 } else if (recmask & SOUND_MASK_LINE1) {
1584 recmask = SOUND_MASK_LINE1;
1585 waveartist_cmd1(devc, WACMD_SET_MONO); /* left */
1586 recdev_l = ADC_MUX_MIC;
1587 recdev_r = ADC_MUX_NONE;
1588 } else if (recmask & SOUND_MASK_PHONEIN) {
1589 recmask = SOUND_MASK_PHONEIN;
1590 waveartist_cmd1(devc, WACMD_SET_MONO); /* left */
1591 recdev_l = ADC_MUX_AUX1;
1592 recdev_r = ADC_MUX_NONE;
1593 } else if (recmask & SOUND_MASK_MIC) {
1594 recmask = SOUND_MASK_MIC;
1595 waveartist_cmd1(devc, WACMD_SET_MONO | 0x100); /* right */
1596 recdev_l = ADC_MUX_NONE;
1597 recdev_r = ADC_MUX_MIC;
1600 *dev_l = recdev_l;
1601 *dev_r = recdev_r;
1603 return recmask;
1606 static int
1607 netwinder_decode_mixer(wavnc_info *devc, int dev, unsigned char lev_l,
1608 unsigned char lev_r)
1610 switch (dev) {
1611 case SOUND_MIXER_VOLUME:
1612 case SOUND_MIXER_SYNTH:
1613 case SOUND_MIXER_PCM:
1614 case SOUND_MIXER_LINE:
1615 case SOUND_MIXER_IGAIN:
1616 devc->levels[dev] = lev_l | lev_r << 8;
1617 break;
1619 case SOUND_MIXER_MIC: /* right mic only */
1620 devc->levels[SOUND_MIXER_MIC] &= 0xff;
1621 devc->levels[SOUND_MIXER_MIC] |= lev_l << 8;
1622 break;
1624 case SOUND_MIXER_LINE1: /* left mic only */
1625 devc->levels[SOUND_MIXER_MIC] &= 0xff00;
1626 devc->levels[SOUND_MIXER_MIC] |= lev_l;
1627 dev = SOUND_MIXER_MIC;
1628 break;
1630 case SOUND_MIXER_PHONEIN: /* left aux only */
1631 devc->levels[SOUND_MIXER_LINE1] = lev_l;
1632 dev = SOUND_MIXER_LINE1;
1633 break;
1635 case SOUND_MIXER_IMIX:
1636 case SOUND_MIXER_PHONEOUT:
1637 break;
1639 default:
1640 dev = -EINVAL;
1641 break;
1643 return dev;
1646 static int netwinder_get_mixer(wavnc_info *devc, int dev)
1648 int levels;
1650 switch (dev) {
1651 case SOUND_MIXER_VOLUME:
1652 case SOUND_MIXER_SYNTH:
1653 case SOUND_MIXER_PCM:
1654 case SOUND_MIXER_LINE:
1655 case SOUND_MIXER_IGAIN:
1656 levels = devc->levels[dev];
1657 break;
1659 case SOUND_MIXER_MIC: /* builtin mic: right mic only */
1660 levels = devc->levels[SOUND_MIXER_MIC] >> 8;
1661 levels |= levels << 8;
1662 break;
1664 case SOUND_MIXER_LINE1: /* handset mic: left mic only */
1665 levels = devc->levels[SOUND_MIXER_MIC] & 0xff;
1666 levels |= levels << 8;
1667 break;
1669 case SOUND_MIXER_PHONEIN: /* phone mic: left aux1 only */
1670 levels = devc->levels[SOUND_MIXER_LINE1] & 0xff;
1671 levels |= levels << 8;
1672 break;
1674 default:
1675 levels = 0;
1678 return levels;
1682 * Waveartist specific mixer information.
1684 static const struct waveartist_mixer_info netwinder_mixer = {
1685 .supported_devs = SOUND_MASK_VOLUME | SOUND_MASK_SYNTH |
1686 SOUND_MASK_PCM | SOUND_MASK_SPEAKER |
1687 SOUND_MASK_LINE | SOUND_MASK_MIC |
1688 SOUND_MASK_IMIX | SOUND_MASK_LINE1 |
1689 SOUND_MASK_PHONEIN | SOUND_MASK_PHONEOUT|
1690 SOUND_MASK_IGAIN,
1692 .recording_devs = SOUND_MASK_LINE | SOUND_MASK_MIC |
1693 SOUND_MASK_IMIX | SOUND_MASK_LINE1 |
1694 SOUND_MASK_PHONEIN,
1696 .stereo_devs = SOUND_MASK_VOLUME | SOUND_MASK_SYNTH |
1697 SOUND_MASK_PCM | SOUND_MASK_LINE |
1698 SOUND_MASK_IMIX | SOUND_MASK_IGAIN,
1700 .select_input = netwinder_select_input,
1701 .decode_mixer = netwinder_decode_mixer,
1702 .get_mixer = netwinder_get_mixer,
1705 static void
1706 vnc_configure_mixer(wavnc_info *devc, unsigned int recmask)
1708 if (!devc->no_autoselect) {
1709 if (devc->handset_detect) {
1710 recmask = SOUND_MASK_LINE1;
1711 devc->spkr_mute_state = devc->line_mute_state = 1;
1712 } else if (devc->telephone_detect) {
1713 recmask = SOUND_MASK_PHONEIN;
1714 devc->spkr_mute_state = devc->line_mute_state = 1;
1715 } else {
1716 /* unless someone has asked for LINE-IN,
1717 * we default to MIC
1719 if ((devc->recmask & SOUND_MASK_LINE) == 0)
1720 devc->recmask = SOUND_MASK_MIC;
1721 devc->spkr_mute_state = devc->line_mute_state = 0;
1723 vnc_mute_spkr(devc);
1724 vnc_mute_lout(devc);
1726 if (recmask != devc->recmask)
1727 waveartist_set_recmask(devc, recmask);
1731 static int
1732 vnc_slider(wavnc_info *devc)
1734 signed int slider_volume;
1735 unsigned int temp, old_hs, old_td;
1738 * read the "buttons" state.
1739 * Bit 4 = 0 means handset present
1740 * Bit 5 = 1 means phone offhook
1742 temp = inb(0x201);
1744 old_hs = devc->handset_detect;
1745 old_td = devc->telephone_detect;
1747 devc->handset_detect = !(temp & 0x10);
1748 devc->telephone_detect = !!(temp & 0x20);
1750 if (!devc->no_autoselect &&
1751 (old_hs != devc->handset_detect ||
1752 old_td != devc->telephone_detect))
1753 vnc_configure_mixer(devc, devc->recmask);
1755 slider_volume = vnc_volume_slider(devc);
1758 * If we're using software controlled volume, and
1759 * the slider moves by more than 20%, then we
1760 * switch back to slider controlled volume.
1762 if (abs(devc->slider_vol - slider_volume) > 20)
1763 devc->use_slider = 1;
1766 * use only left channel
1768 temp = levels[SOUND_MIXER_VOLUME] & 0xFF;
1770 if (slider_volume != temp && devc->use_slider) {
1771 devc->slider_vol = slider_volume;
1773 waveartist_set_mixer(devc, SOUND_MIXER_VOLUME,
1774 slider_volume | slider_volume << 8);
1776 return 1;
1779 return 0;
1782 static void
1783 vnc_slider_tick(unsigned long data)
1785 int next_timeout;
1787 if (vnc_slider(adev_info + data))
1788 next_timeout = 5; // mixer reported change
1789 else
1790 next_timeout = VNC_TIMER_PERIOD;
1792 mod_timer(&vnc_timer, jiffies + next_timeout);
1795 static int
1796 vnc_private_ioctl(int dev, unsigned int cmd, int __user * arg)
1798 wavnc_info *devc = (wavnc_info *)audio_devs[dev]->devc;
1799 int val;
1801 switch (cmd) {
1802 case SOUND_MIXER_PRIVATE1:
1804 u_int prev_spkr_mute, prev_line_mute, prev_auto_state;
1805 int val;
1807 if (get_user(val, arg))
1808 return -EFAULT;
1810 /* check if parameter is logical */
1811 if (val & ~(VNC_MUTE_INTERNAL_SPKR |
1812 VNC_MUTE_LINE_OUT |
1813 VNC_DISABLE_AUTOSWITCH))
1814 return -EINVAL;
1816 prev_auto_state = devc->no_autoselect;
1817 prev_spkr_mute = devc->spkr_mute_state;
1818 prev_line_mute = devc->line_mute_state;
1820 devc->no_autoselect = (val & VNC_DISABLE_AUTOSWITCH) ? 1 : 0;
1821 devc->spkr_mute_state = (val & VNC_MUTE_INTERNAL_SPKR) ? 1 : 0;
1822 devc->line_mute_state = (val & VNC_MUTE_LINE_OUT) ? 1 : 0;
1824 if (prev_spkr_mute != devc->spkr_mute_state)
1825 vnc_mute_spkr(devc);
1827 if (prev_line_mute != devc->line_mute_state)
1828 vnc_mute_lout(devc);
1830 if (prev_auto_state != devc->no_autoselect)
1831 vnc_configure_mixer(devc, devc->recmask);
1833 return 0;
1836 case SOUND_MIXER_PRIVATE2:
1837 if (get_user(val, arg))
1838 return -EFAULT;
1840 switch (val) {
1841 #define VNC_SOUND_PAUSE 0x53 //to pause the DSP
1842 #define VNC_SOUND_RESUME 0x57 //to unpause the DSP
1843 case VNC_SOUND_PAUSE:
1844 waveartist_cmd1(devc, 0x16);
1845 break;
1847 case VNC_SOUND_RESUME:
1848 waveartist_cmd1(devc, 0x18);
1849 break;
1851 default:
1852 return -EINVAL;
1854 return 0;
1856 /* private ioctl to allow bulk access to waveartist */
1857 case SOUND_MIXER_PRIVATE3:
1859 unsigned long flags;
1860 int mixer_reg[15], i, val;
1862 if (get_user(val, arg))
1863 return -EFAULT;
1864 if (copy_from_user(mixer_reg, (void *)val, sizeof(mixer_reg)))
1865 return -EFAULT;
1867 switch (mixer_reg[14]) {
1868 case MIXER_PRIVATE3_RESET:
1869 waveartist_mixer_reset(devc);
1870 break;
1872 case MIXER_PRIVATE3_WRITE:
1873 waveartist_cmd3(devc, WACMD_SET_MIXER, mixer_reg[0], mixer_reg[4]);
1874 waveartist_cmd3(devc, WACMD_SET_MIXER, mixer_reg[1], mixer_reg[5]);
1875 waveartist_cmd3(devc, WACMD_SET_MIXER, mixer_reg[2], mixer_reg[6]);
1876 waveartist_cmd3(devc, WACMD_SET_MIXER, mixer_reg[3], mixer_reg[7]);
1877 waveartist_cmd3(devc, WACMD_SET_MIXER, mixer_reg[8], mixer_reg[9]);
1879 waveartist_cmd3(devc, WACMD_SET_LEVEL, mixer_reg[10], mixer_reg[11]);
1880 waveartist_cmd3(devc, WACMD_SET_LEVEL, mixer_reg[12], mixer_reg[13]);
1881 break;
1883 case MIXER_PRIVATE3_READ:
1884 spin_lock_irqsave(&waveartist_lock, flags);
1886 for (i = 0x30; i < 14 << 8; i += 1 << 8)
1887 waveartist_cmd(devc, 1, &i, 1, mixer_reg + (i >> 8));
1889 spin_unlock_irqrestore(&waveartist_lock, flags);
1891 if (copy_to_user((void *)val, mixer_reg, sizeof(mixer_reg)))
1892 return -EFAULT;
1893 break;
1895 default:
1896 return -EINVAL;
1898 return 0;
1901 /* read back the state from PRIVATE1 */
1902 case SOUND_MIXER_PRIVATE4:
1903 val = (devc->spkr_mute_state ? VNC_MUTE_INTERNAL_SPKR : 0) |
1904 (devc->line_mute_state ? VNC_MUTE_LINE_OUT : 0) |
1905 (devc->handset_detect ? VNC_HANDSET_DETECT : 0) |
1906 (devc->telephone_detect ? VNC_PHONE_DETECT : 0) |
1907 (devc->no_autoselect ? VNC_DISABLE_AUTOSWITCH : 0);
1909 return put_user(val, arg) ? -EFAULT : 0;
1912 if (_SIOC_DIR(cmd) & _SIOC_WRITE) {
1914 * special case for master volume: if we
1915 * received this call - switch from hw
1916 * volume control to a software volume
1917 * control, till the hw volume is modified
1918 * to signal that user wants to be back in
1919 * hardware...
1921 if ((cmd & 0xff) == SOUND_MIXER_VOLUME)
1922 devc->use_slider = 0;
1924 /* speaker output */
1925 if ((cmd & 0xff) == SOUND_MIXER_SPEAKER) {
1926 unsigned int val, l, r;
1928 if (get_user(val, arg))
1929 return -EFAULT;
1931 l = val & 0x7f;
1932 r = (val & 0x7f00) >> 8;
1933 val = (l + r) / 2;
1934 devc->levels[SOUND_MIXER_SPEAKER] = val | (val << 8);
1935 devc->spkr_mute_state = (val <= 50);
1936 vnc_mute_spkr(devc);
1937 return 0;
1941 return -ENOIOCTLCMD;
1944 #endif
1946 static struct address_info cfg;
1948 static int attached;
1950 static int __initdata io = 0;
1951 static int __initdata irq = 0;
1952 static int __initdata dma = 0;
1953 static int __initdata dma2 = 0;
1956 static int __init init_waveartist(void)
1958 const struct waveartist_mixer_info *mix;
1960 if (!io && machine_is_netwinder()) {
1962 * The NetWinder WaveArtist is at a fixed address.
1963 * If the user does not supply an address, use the
1964 * well-known parameters.
1966 io = 0x250;
1967 irq = 12;
1968 dma = 3;
1969 dma2 = 7;
1972 mix = &waveartist_mixer;
1973 #ifdef CONFIG_ARCH_NETWINDER
1974 if (machine_is_netwinder())
1975 mix = &netwinder_mixer;
1976 #endif
1978 cfg.io_base = io;
1979 cfg.irq = irq;
1980 cfg.dma = dma;
1981 cfg.dma2 = dma2;
1983 if (!probe_waveartist(&cfg))
1984 return -ENODEV;
1986 attach_waveartist(&cfg, mix);
1987 attached = 1;
1989 return 0;
1992 static void __exit cleanup_waveartist(void)
1994 if (attached)
1995 unload_waveartist(&cfg);
1998 module_init(init_waveartist);
1999 module_exit(cleanup_waveartist);
2001 #ifndef MODULE
2002 static int __init setup_waveartist(char *str)
2004 /* io, irq, dma, dma2 */
2005 int ints[5];
2007 str = get_options(str, ARRAY_SIZE(ints), ints);
2009 io = ints[1];
2010 irq = ints[2];
2011 dma = ints[3];
2012 dma2 = ints[4];
2014 return 1;
2016 __setup("waveartist=", setup_waveartist);
2017 #endif
2019 MODULE_DESCRIPTION("Rockwell WaveArtist RWA-010 sound driver");
2020 module_param(io, int, 0); /* IO base */
2021 module_param(irq, int, 0); /* IRQ */
2022 module_param(dma, int, 0); /* DMA */
2023 module_param(dma2, int, 0); /* DMA2 */
2024 MODULE_LICENSE("GPL");