[PATCH] fix semaphore handling in __unregister_chrdev_region
[linux/fpc-iii.git] / sound / oss / waveartist.c
blob99d04ad3ca13158f4281e0c449c3d6a39e12be9d
1 /*
2 * linux/drivers/sound/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/config.h>
39 #include <linux/sched.h>
40 #include <linux/interrupt.h>
41 #include <linux/delay.h>
42 #include <linux/spinlock.h>
43 #include <linux/bitops.h>
45 #include <asm/system.h>
47 #include "sound_config.h"
48 #include "waveartist.h"
50 #ifdef CONFIG_ARM
51 #include <asm/hardware.h>
52 #include <asm/mach-types.h>
53 #endif
55 #ifndef NO_DMA
56 #define NO_DMA 255
57 #endif
59 #define SUPPORTED_MIXER_DEVICES (SOUND_MASK_SYNTH |\
60 SOUND_MASK_PCM |\
61 SOUND_MASK_LINE |\
62 SOUND_MASK_MIC |\
63 SOUND_MASK_LINE1 |\
64 SOUND_MASK_RECLEV |\
65 SOUND_MASK_VOLUME |\
66 SOUND_MASK_IMIX)
68 static unsigned short levels[SOUND_MIXER_NRDEVICES] = {
69 0x5555, /* Master Volume */
70 0x0000, /* Bass */
71 0x0000, /* Treble */
72 0x2323, /* Synth (FM) */
73 0x4b4b, /* PCM */
74 0x6464, /* PC Speaker */
75 0x0000, /* Ext Line */
76 0x0000, /* Mic */
77 0x0000, /* CD */
78 0x6464, /* Recording monitor */
79 0x0000, /* SB PCM (ALT PCM) */
80 0x0000, /* Recording level */
81 0x6464, /* Input gain */
82 0x6464, /* Output gain */
83 0x0000, /* Line1 (Aux1) */
84 0x0000, /* Line2 (Aux2) */
85 0x0000, /* Line3 (Aux3) */
86 0x0000, /* Digital1 */
87 0x0000, /* Digital2 */
88 0x0000, /* Digital3 */
89 0x0000, /* Phone In */
90 0x6464, /* Phone Out */
91 0x0000, /* Video */
92 0x0000, /* Radio */
93 0x0000 /* Monitor */
96 typedef struct {
97 struct address_info hw; /* hardware */
98 char *chip_name;
100 int xfer_count;
101 int audio_mode;
102 int open_mode;
103 int audio_flags;
104 int record_dev;
105 int playback_dev;
106 int dev_no;
108 /* Mixer parameters */
109 const struct waveartist_mixer_info *mix;
111 unsigned short *levels; /* cache of volume settings */
112 int recmask; /* currently enabled recording device! */
114 #ifdef CONFIG_ARCH_NETWINDER
115 signed int slider_vol; /* hardware slider volume */
116 unsigned int handset_detect :1;
117 unsigned int telephone_detect:1;
118 unsigned int no_autoselect :1;/* handset/telephone autoselects a path */
119 unsigned int spkr_mute_state :1;/* set by ioctl or autoselect */
120 unsigned int line_mute_state :1;/* set by ioctl or autoselect */
121 unsigned int use_slider :1;/* use slider setting for o/p vol */
122 #endif
123 } wavnc_info;
126 * This is the implementation specific mixer information.
128 struct waveartist_mixer_info {
129 unsigned int supported_devs; /* Supported devices */
130 unsigned int recording_devs; /* Recordable devies */
131 unsigned int stereo_devs; /* Stereo devices */
133 unsigned int (*select_input)(wavnc_info *, unsigned int,
134 unsigned char *, unsigned char *);
135 int (*decode_mixer)(wavnc_info *, int,
136 unsigned char, unsigned char);
137 int (*get_mixer)(wavnc_info *, int);
140 typedef struct wavnc_port_info {
141 int open_mode;
142 int speed;
143 int channels;
144 int audio_format;
145 } wavnc_port_info;
147 static int nr_waveartist_devs;
148 static wavnc_info adev_info[MAX_AUDIO_DEV];
149 static DEFINE_SPINLOCK(waveartist_lock);
151 #ifndef CONFIG_ARCH_NETWINDER
152 #define machine_is_netwinder() 0
153 #else
154 static struct timer_list vnc_timer;
155 static void vnc_configure_mixer(wavnc_info *devc, unsigned int input_mask);
156 static int vnc_private_ioctl(int dev, unsigned int cmd, int __user *arg);
157 static void vnc_slider_tick(unsigned long data);
158 #endif
160 static inline void
161 waveartist_set_ctlr(struct address_info *hw, unsigned char clear, unsigned char set)
163 unsigned int ctlr_port = hw->io_base + CTLR;
165 clear = ~clear & inb(ctlr_port);
167 outb(clear | set, ctlr_port);
170 /* Toggle IRQ acknowledge line
172 static inline void
173 waveartist_iack(wavnc_info *devc)
175 unsigned int ctlr_port = devc->hw.io_base + CTLR;
176 int old_ctlr;
178 old_ctlr = inb(ctlr_port) & ~IRQ_ACK;
180 outb(old_ctlr | IRQ_ACK, ctlr_port);
181 outb(old_ctlr, ctlr_port);
184 static inline int
185 waveartist_sleep(int timeout_ms)
187 unsigned int timeout = timeout_ms * 10 * HZ / 100;
189 do {
190 set_current_state(TASK_INTERRUPTIBLE);
191 timeout = schedule_timeout(timeout);
192 } while (timeout);
194 return 0;
197 static int
198 waveartist_reset(wavnc_info *devc)
200 struct address_info *hw = &devc->hw;
201 unsigned int timeout, res = -1;
203 waveartist_set_ctlr(hw, -1, RESET);
204 waveartist_sleep(2);
205 waveartist_set_ctlr(hw, RESET, 0);
207 timeout = 500;
208 do {
209 mdelay(2);
211 if (inb(hw->io_base + STATR) & CMD_RF) {
212 res = inw(hw->io_base + CMDR);
213 if (res == 0x55aa)
214 break;
216 } while (--timeout);
218 if (timeout == 0) {
219 printk(KERN_WARNING "WaveArtist: reset timeout ");
220 if (res != (unsigned int)-1)
221 printk("(res=%04X)", res);
222 printk("\n");
223 return 1;
225 return 0;
228 /* Helper function to send and receive words
229 * from WaveArtist. It handles all the handshaking
230 * and can send or receive multiple words.
232 static int
233 waveartist_cmd(wavnc_info *devc,
234 int nr_cmd, unsigned int *cmd,
235 int nr_resp, unsigned int *resp)
237 unsigned int io_base = devc->hw.io_base;
238 unsigned int timed_out = 0;
239 unsigned int i;
241 if (debug_flg & DEBUG_CMD) {
242 printk("waveartist_cmd: cmd=");
244 for (i = 0; i < nr_cmd; i++)
245 printk("%04X ", cmd[i]);
247 printk("\n");
250 if (inb(io_base + STATR) & CMD_RF) {
251 int old_data;
253 /* flush the port
256 old_data = inw(io_base + CMDR);
258 if (debug_flg & DEBUG_CMD)
259 printk("flushed %04X...", old_data);
261 udelay(10);
264 for (i = 0; !timed_out && i < nr_cmd; i++) {
265 int count;
267 for (count = 5000; count; count--)
268 if (inb(io_base + STATR) & CMD_WE)
269 break;
271 if (!count)
272 timed_out = 1;
273 else
274 outw(cmd[i], io_base + CMDR);
277 for (i = 0; !timed_out && i < nr_resp; i++) {
278 int count;
280 for (count = 5000; count; count--)
281 if (inb(io_base + STATR) & CMD_RF)
282 break;
284 if (!count)
285 timed_out = 1;
286 else
287 resp[i] = inw(io_base + CMDR);
290 if (debug_flg & DEBUG_CMD) {
291 if (!timed_out) {
292 printk("waveartist_cmd: resp=");
294 for (i = 0; i < nr_resp; i++)
295 printk("%04X ", resp[i]);
297 printk("\n");
298 } else
299 printk("waveartist_cmd: timed out\n");
302 return timed_out ? 1 : 0;
306 * Send one command word
308 static inline int
309 waveartist_cmd1(wavnc_info *devc, unsigned int cmd)
311 return waveartist_cmd(devc, 1, &cmd, 0, NULL);
315 * Send one command, receive one word
317 static inline unsigned int
318 waveartist_cmd1_r(wavnc_info *devc, unsigned int cmd)
320 unsigned int ret;
322 waveartist_cmd(devc, 1, &cmd, 1, &ret);
324 return ret;
328 * Send a double command, receive one
329 * word (and throw it away)
331 static inline int
332 waveartist_cmd2(wavnc_info *devc, unsigned int cmd, unsigned int arg)
334 unsigned int vals[2];
336 vals[0] = cmd;
337 vals[1] = arg;
339 return waveartist_cmd(devc, 2, vals, 1, vals);
343 * Send a triple command
345 static inline int
346 waveartist_cmd3(wavnc_info *devc, unsigned int cmd,
347 unsigned int arg1, unsigned int arg2)
349 unsigned int vals[3];
351 vals[0] = cmd;
352 vals[1] = arg1;
353 vals[2] = arg2;
355 return waveartist_cmd(devc, 3, vals, 0, NULL);
358 static int
359 waveartist_getrev(wavnc_info *devc, char *rev)
361 unsigned int temp[2];
362 unsigned int cmd = WACMD_GETREV;
364 waveartist_cmd(devc, 1, &cmd, 2, temp);
366 rev[0] = temp[0] >> 8;
367 rev[1] = temp[0] & 255;
368 rev[2] = '\0';
370 return temp[0];
373 static void waveartist_halt_output(int dev);
374 static void waveartist_halt_input(int dev);
375 static void waveartist_halt(int dev);
376 static void waveartist_trigger(int dev, int state);
378 static int
379 waveartist_open(int dev, int mode)
381 wavnc_info *devc;
382 wavnc_port_info *portc;
383 unsigned long flags;
385 if (dev < 0 || dev >= num_audiodevs)
386 return -ENXIO;
388 devc = (wavnc_info *) audio_devs[dev]->devc;
389 portc = (wavnc_port_info *) audio_devs[dev]->portc;
391 spin_lock_irqsave(&waveartist_lock, flags);
392 if (portc->open_mode || (devc->open_mode & mode)) {
393 spin_unlock_irqrestore(&waveartist_lock, flags);
394 return -EBUSY;
397 devc->audio_mode = 0;
398 devc->open_mode |= mode;
399 portc->open_mode = mode;
400 waveartist_trigger(dev, 0);
402 if (mode & OPEN_READ)
403 devc->record_dev = dev;
404 if (mode & OPEN_WRITE)
405 devc->playback_dev = dev;
406 spin_unlock_irqrestore(&waveartist_lock, flags);
408 return 0;
411 static void
412 waveartist_close(int dev)
414 wavnc_info *devc = (wavnc_info *) audio_devs[dev]->devc;
415 wavnc_port_info *portc = (wavnc_port_info *) audio_devs[dev]->portc;
416 unsigned long flags;
418 spin_lock_irqsave(&waveartist_lock, flags);
420 waveartist_halt(dev);
422 devc->audio_mode = 0;
423 devc->open_mode &= ~portc->open_mode;
424 portc->open_mode = 0;
426 spin_unlock_irqrestore(&waveartist_lock, flags);
429 static void
430 waveartist_output_block(int dev, unsigned long buf, int __count, int intrflag)
432 wavnc_port_info *portc = (wavnc_port_info *) audio_devs[dev]->portc;
433 wavnc_info *devc = (wavnc_info *) audio_devs[dev]->devc;
434 unsigned long flags;
435 unsigned int count = __count;
437 if (debug_flg & DEBUG_OUT)
438 printk("waveartist: output block, buf=0x%lx, count=0x%x...\n",
439 buf, count);
441 * 16 bit data
443 if (portc->audio_format & (AFMT_S16_LE | AFMT_S16_BE))
444 count >>= 1;
446 if (portc->channels > 1)
447 count >>= 1;
449 count -= 1;
451 if (devc->audio_mode & PCM_ENABLE_OUTPUT &&
452 audio_devs[dev]->flags & DMA_AUTOMODE &&
453 intrflag &&
454 count == devc->xfer_count) {
455 devc->audio_mode |= PCM_ENABLE_OUTPUT;
456 return; /*
457 * Auto DMA mode on. No need to react
461 spin_lock_irqsave(&waveartist_lock, flags);
464 * set sample count
466 waveartist_cmd2(devc, WACMD_OUTPUTSIZE, count);
468 devc->xfer_count = count;
469 devc->audio_mode |= PCM_ENABLE_OUTPUT;
471 spin_unlock_irqrestore(&waveartist_lock, flags);
474 static void
475 waveartist_start_input(int dev, unsigned long buf, int __count, int intrflag)
477 wavnc_port_info *portc = (wavnc_port_info *) audio_devs[dev]->portc;
478 wavnc_info *devc = (wavnc_info *) audio_devs[dev]->devc;
479 unsigned long flags;
480 unsigned int count = __count;
482 if (debug_flg & DEBUG_IN)
483 printk("waveartist: start input, buf=0x%lx, count=0x%x...\n",
484 buf, count);
486 if (portc->audio_format & (AFMT_S16_LE | AFMT_S16_BE)) /* 16 bit data */
487 count >>= 1;
489 if (portc->channels > 1)
490 count >>= 1;
492 count -= 1;
494 if (devc->audio_mode & PCM_ENABLE_INPUT &&
495 audio_devs[dev]->flags & DMA_AUTOMODE &&
496 intrflag &&
497 count == devc->xfer_count) {
498 devc->audio_mode |= PCM_ENABLE_INPUT;
499 return; /*
500 * Auto DMA mode on. No need to react
504 spin_lock_irqsave(&waveartist_lock, flags);
507 * set sample count
509 waveartist_cmd2(devc, WACMD_INPUTSIZE, count);
511 devc->xfer_count = count;
512 devc->audio_mode |= PCM_ENABLE_INPUT;
514 spin_unlock_irqrestore(&waveartist_lock, flags);
517 static int
518 waveartist_ioctl(int dev, unsigned int cmd, void __user * arg)
520 return -EINVAL;
523 static unsigned int
524 waveartist_get_speed(wavnc_port_info *portc)
526 unsigned int speed;
529 * program the speed, channels, bits
531 if (portc->speed == 8000)
532 speed = 0x2E71;
533 else if (portc->speed == 11025)
534 speed = 0x4000;
535 else if (portc->speed == 22050)
536 speed = 0x8000;
537 else if (portc->speed == 44100)
538 speed = 0x0;
539 else {
541 * non-standard - just calculate
543 speed = portc->speed << 16;
545 speed = (speed / 44100) & 65535;
548 return speed;
551 static unsigned int
552 waveartist_get_bits(wavnc_port_info *portc)
554 unsigned int bits;
556 if (portc->audio_format == AFMT_S16_LE)
557 bits = 1;
558 else if (portc->audio_format == AFMT_S8)
559 bits = 0;
560 else
561 bits = 2; //default AFMT_U8
563 return bits;
566 static int
567 waveartist_prepare_for_input(int dev, int bsize, int bcount)
569 unsigned long flags;
570 wavnc_info *devc = (wavnc_info *) audio_devs[dev]->devc;
571 wavnc_port_info *portc = (wavnc_port_info *) audio_devs[dev]->portc;
572 unsigned int speed, bits;
574 if (devc->audio_mode)
575 return 0;
577 speed = waveartist_get_speed(portc);
578 bits = waveartist_get_bits(portc);
580 spin_lock_irqsave(&waveartist_lock, flags);
582 if (waveartist_cmd2(devc, WACMD_INPUTFORMAT, bits))
583 printk(KERN_WARNING "waveartist: error setting the "
584 "record format to %d\n", portc->audio_format);
586 if (waveartist_cmd2(devc, WACMD_INPUTCHANNELS, portc->channels))
587 printk(KERN_WARNING "waveartist: error setting record "
588 "to %d channels\n", portc->channels);
591 * write cmd SetSampleSpeedTimeConstant
593 if (waveartist_cmd2(devc, WACMD_INPUTSPEED, speed))
594 printk(KERN_WARNING "waveartist: error setting the record "
595 "speed to %dHz.\n", portc->speed);
597 if (waveartist_cmd2(devc, WACMD_INPUTDMA, 1))
598 printk(KERN_WARNING "waveartist: error setting the record "
599 "data path to 0x%X\n", 1);
601 if (waveartist_cmd2(devc, WACMD_INPUTFORMAT, bits))
602 printk(KERN_WARNING "waveartist: error setting the record "
603 "format to %d\n", portc->audio_format);
605 devc->xfer_count = 0;
606 spin_unlock_irqrestore(&waveartist_lock, flags);
607 waveartist_halt_input(dev);
609 if (debug_flg & DEBUG_INTR) {
610 printk("WA CTLR reg: 0x%02X.\n",
611 inb(devc->hw.io_base + CTLR));
612 printk("WA STAT reg: 0x%02X.\n",
613 inb(devc->hw.io_base + STATR));
614 printk("WA IRQS reg: 0x%02X.\n",
615 inb(devc->hw.io_base + IRQSTAT));
618 return 0;
621 static int
622 waveartist_prepare_for_output(int dev, int bsize, int bcount)
624 unsigned long flags;
625 wavnc_info *devc = (wavnc_info *) audio_devs[dev]->devc;
626 wavnc_port_info *portc = (wavnc_port_info *) audio_devs[dev]->portc;
627 unsigned int speed, bits;
630 * program the speed, channels, bits
632 speed = waveartist_get_speed(portc);
633 bits = waveartist_get_bits(portc);
635 spin_lock_irqsave(&waveartist_lock, flags);
637 if (waveartist_cmd2(devc, WACMD_OUTPUTSPEED, speed) &&
638 waveartist_cmd2(devc, WACMD_OUTPUTSPEED, speed))
639 printk(KERN_WARNING "waveartist: error setting the playback "
640 "speed to %dHz.\n", portc->speed);
642 if (waveartist_cmd2(devc, WACMD_OUTPUTCHANNELS, portc->channels))
643 printk(KERN_WARNING "waveartist: error setting the playback "
644 "to %d channels\n", portc->channels);
646 if (waveartist_cmd2(devc, WACMD_OUTPUTDMA, 0))
647 printk(KERN_WARNING "waveartist: error setting the playback "
648 "data path to 0x%X\n", 0);
650 if (waveartist_cmd2(devc, WACMD_OUTPUTFORMAT, bits))
651 printk(KERN_WARNING "waveartist: error setting the playback "
652 "format to %d\n", portc->audio_format);
654 devc->xfer_count = 0;
655 spin_unlock_irqrestore(&waveartist_lock, flags);
656 waveartist_halt_output(dev);
658 if (debug_flg & DEBUG_INTR) {
659 printk("WA CTLR reg: 0x%02X.\n",inb(devc->hw.io_base + CTLR));
660 printk("WA STAT reg: 0x%02X.\n",inb(devc->hw.io_base + STATR));
661 printk("WA IRQS reg: 0x%02X.\n",inb(devc->hw.io_base + IRQSTAT));
664 return 0;
667 static void
668 waveartist_halt(int dev)
670 wavnc_port_info *portc = (wavnc_port_info *) audio_devs[dev]->portc;
671 wavnc_info *devc;
673 if (portc->open_mode & OPEN_WRITE)
674 waveartist_halt_output(dev);
676 if (portc->open_mode & OPEN_READ)
677 waveartist_halt_input(dev);
679 devc = (wavnc_info *) audio_devs[dev]->devc;
680 devc->audio_mode = 0;
683 static void
684 waveartist_halt_input(int dev)
686 wavnc_info *devc = (wavnc_info *) audio_devs[dev]->devc;
687 unsigned long flags;
689 spin_lock_irqsave(&waveartist_lock, flags);
692 * Stop capture
694 waveartist_cmd1(devc, WACMD_INPUTSTOP);
696 devc->audio_mode &= ~PCM_ENABLE_INPUT;
699 * Clear interrupt by toggling
700 * the IRQ_ACK bit in CTRL
702 if (inb(devc->hw.io_base + STATR) & IRQ_REQ)
703 waveartist_iack(devc);
705 // devc->audio_mode &= ~PCM_ENABLE_INPUT;
707 spin_unlock_irqrestore(&waveartist_lock, flags);
710 static void
711 waveartist_halt_output(int dev)
713 wavnc_info *devc = (wavnc_info *) audio_devs[dev]->devc;
714 unsigned long flags;
716 spin_lock_irqsave(&waveartist_lock, flags);
718 waveartist_cmd1(devc, WACMD_OUTPUTSTOP);
720 devc->audio_mode &= ~PCM_ENABLE_OUTPUT;
723 * Clear interrupt by toggling
724 * the IRQ_ACK bit in CTRL
726 if (inb(devc->hw.io_base + STATR) & IRQ_REQ)
727 waveartist_iack(devc);
729 // devc->audio_mode &= ~PCM_ENABLE_OUTPUT;
731 spin_unlock_irqrestore(&waveartist_lock, flags);
734 static void
735 waveartist_trigger(int dev, int state)
737 wavnc_info *devc = (wavnc_info *) audio_devs[dev]->devc;
738 wavnc_port_info *portc = (wavnc_port_info *) audio_devs[dev]->portc;
739 unsigned long flags;
741 if (debug_flg & DEBUG_TRIGGER) {
742 printk("wavnc: audio trigger ");
743 if (state & PCM_ENABLE_INPUT)
744 printk("in ");
745 if (state & PCM_ENABLE_OUTPUT)
746 printk("out");
747 printk("\n");
750 spin_lock_irqsave(&waveartist_lock, flags);
752 state &= devc->audio_mode;
754 if (portc->open_mode & OPEN_READ &&
755 state & PCM_ENABLE_INPUT)
757 * enable ADC Data Transfer to PC
759 waveartist_cmd1(devc, WACMD_INPUTSTART);
761 if (portc->open_mode & OPEN_WRITE &&
762 state & PCM_ENABLE_OUTPUT)
764 * enable DAC data transfer from PC
766 waveartist_cmd1(devc, WACMD_OUTPUTSTART);
768 spin_unlock_irqrestore(&waveartist_lock, flags);
771 static int
772 waveartist_set_speed(int dev, int arg)
774 wavnc_port_info *portc = (wavnc_port_info *) audio_devs[dev]->portc;
776 if (arg <= 0)
777 return portc->speed;
779 if (arg < 5000)
780 arg = 5000;
781 if (arg > 44100)
782 arg = 44100;
784 portc->speed = arg;
785 return portc->speed;
789 static short
790 waveartist_set_channels(int dev, short arg)
792 wavnc_port_info *portc = (wavnc_port_info *) audio_devs[dev]->portc;
794 if (arg != 1 && arg != 2)
795 return portc->channels;
797 portc->channels = arg;
798 return arg;
801 static unsigned int
802 waveartist_set_bits(int dev, unsigned int arg)
804 wavnc_port_info *portc = (wavnc_port_info *) audio_devs[dev]->portc;
806 if (arg == 0)
807 return portc->audio_format;
809 if ((arg != AFMT_U8) && (arg != AFMT_S16_LE) && (arg != AFMT_S8))
810 arg = AFMT_U8;
812 portc->audio_format = arg;
814 return arg;
817 static struct audio_driver waveartist_audio_driver = {
818 .owner = THIS_MODULE,
819 .open = waveartist_open,
820 .close = waveartist_close,
821 .output_block = waveartist_output_block,
822 .start_input = waveartist_start_input,
823 .ioctl = waveartist_ioctl,
824 .prepare_for_input = waveartist_prepare_for_input,
825 .prepare_for_output = waveartist_prepare_for_output,
826 .halt_io = waveartist_halt,
827 .halt_input = waveartist_halt_input,
828 .halt_output = waveartist_halt_output,
829 .trigger = waveartist_trigger,
830 .set_speed = waveartist_set_speed,
831 .set_bits = waveartist_set_bits,
832 .set_channels = waveartist_set_channels
836 static irqreturn_t
837 waveartist_intr(int irq, void *dev_id, struct pt_regs *regs)
839 wavnc_info *devc = (wavnc_info *)dev_id;
840 int irqstatus, status;
842 spin_lock(&waveartist_lock);
843 irqstatus = inb(devc->hw.io_base + IRQSTAT);
844 status = inb(devc->hw.io_base + STATR);
846 if (debug_flg & DEBUG_INTR)
847 printk("waveartist_intr: stat=%02x, irqstat=%02x\n",
848 status, irqstatus);
850 if (status & IRQ_REQ) /* Clear interrupt */
851 waveartist_iack(devc);
852 else
853 printk(KERN_WARNING "waveartist: unexpected interrupt\n");
855 if (irqstatus & 0x01) {
856 int temp = 1;
858 /* PCM buffer done
860 if ((status & DMA0) && (devc->audio_mode & PCM_ENABLE_OUTPUT)) {
861 DMAbuf_outputintr(devc->playback_dev, 1);
862 temp = 0;
864 if ((status & DMA1) && (devc->audio_mode & PCM_ENABLE_INPUT)) {
865 DMAbuf_inputintr(devc->record_dev);
866 temp = 0;
868 if (temp) //default:
869 printk(KERN_WARNING "waveartist: Unknown interrupt\n");
871 if (irqstatus & 0x2)
872 // We do not use SB mode natively...
873 printk(KERN_WARNING "waveartist: Unexpected SB interrupt...\n");
874 spin_unlock(&waveartist_lock);
875 return IRQ_HANDLED;
878 /* -------------------------------------------------------------------------
879 * Mixer stuff
881 struct mix_ent {
882 unsigned char reg_l;
883 unsigned char reg_r;
884 unsigned char shift;
885 unsigned char max;
888 static const struct mix_ent mix_devs[SOUND_MIXER_NRDEVICES] = {
889 { 2, 6, 1, 7 }, /* SOUND_MIXER_VOLUME */
890 { 0, 0, 0, 0 }, /* SOUND_MIXER_BASS */
891 { 0, 0, 0, 0 }, /* SOUND_MIXER_TREBLE */
892 { 0, 0, 0, 0 }, /* SOUND_MIXER_SYNTH */
893 { 0, 0, 0, 0 }, /* SOUND_MIXER_PCM */
894 { 0, 0, 0, 0 }, /* SOUND_MIXER_SPEAKER */
895 { 0, 4, 6, 31 }, /* SOUND_MIXER_LINE */
896 { 2, 6, 4, 3 }, /* SOUND_MIXER_MIC */
897 { 0, 0, 0, 0 }, /* SOUND_MIXER_CD */
898 { 0, 0, 0, 0 }, /* SOUND_MIXER_IMIX */
899 { 0, 0, 0, 0 }, /* SOUND_MIXER_ALTPCM */
900 #if 0
901 { 3, 7, 0, 10 }, /* SOUND_MIXER_RECLEV */
902 { 0, 0, 0, 0 }, /* SOUND_MIXER_IGAIN */
903 #else
904 { 0, 0, 0, 0 }, /* SOUND_MIXER_RECLEV */
905 { 3, 7, 0, 7 }, /* SOUND_MIXER_IGAIN */
906 #endif
907 { 0, 0, 0, 0 }, /* SOUND_MIXER_OGAIN */
908 { 0, 4, 1, 31 }, /* SOUND_MIXER_LINE1 */
909 { 1, 5, 6, 31 }, /* SOUND_MIXER_LINE2 */
910 { 0, 0, 0, 0 }, /* SOUND_MIXER_LINE3 */
911 { 0, 0, 0, 0 }, /* SOUND_MIXER_DIGITAL1 */
912 { 0, 0, 0, 0 }, /* SOUND_MIXER_DIGITAL2 */
913 { 0, 0, 0, 0 }, /* SOUND_MIXER_DIGITAL3 */
914 { 0, 0, 0, 0 }, /* SOUND_MIXER_PHONEIN */
915 { 0, 0, 0, 0 }, /* SOUND_MIXER_PHONEOUT */
916 { 0, 0, 0, 0 }, /* SOUND_MIXER_VIDEO */
917 { 0, 0, 0, 0 }, /* SOUND_MIXER_RADIO */
918 { 0, 0, 0, 0 } /* SOUND_MIXER_MONITOR */
921 static void
922 waveartist_mixer_update(wavnc_info *devc, int whichDev)
924 unsigned int lev_left, lev_right;
926 lev_left = devc->levels[whichDev] & 0xff;
927 lev_right = devc->levels[whichDev] >> 8;
929 if (lev_left > 100)
930 lev_left = 100;
931 if (lev_right > 100)
932 lev_right = 100;
934 #define SCALE(lev,max) ((lev) * (max) / 100)
936 if (machine_is_netwinder() && whichDev == SOUND_MIXER_PHONEOUT)
937 whichDev = SOUND_MIXER_VOLUME;
939 if (mix_devs[whichDev].reg_l || mix_devs[whichDev].reg_r) {
940 const struct mix_ent *mix = mix_devs + whichDev;
941 unsigned int mask, left, right;
943 mask = mix->max << mix->shift;
944 lev_left = SCALE(lev_left, mix->max) << mix->shift;
945 lev_right = SCALE(lev_right, mix->max) << mix->shift;
947 /* read left setting */
948 left = waveartist_cmd1_r(devc, WACMD_GET_LEVEL |
949 mix->reg_l << 8);
951 /* read right setting */
952 right = waveartist_cmd1_r(devc, WACMD_GET_LEVEL |
953 mix->reg_r << 8);
955 left = (left & ~mask) | (lev_left & mask);
956 right = (right & ~mask) | (lev_right & mask);
958 /* write left,right back */
959 waveartist_cmd3(devc, WACMD_SET_MIXER, left, right);
960 } else {
961 switch(whichDev) {
962 case SOUND_MIXER_PCM:
963 waveartist_cmd3(devc, WACMD_SET_LEVEL,
964 SCALE(lev_left, 32767),
965 SCALE(lev_right, 32767));
966 break;
968 case SOUND_MIXER_SYNTH:
969 waveartist_cmd3(devc, 0x0100 | WACMD_SET_LEVEL,
970 SCALE(lev_left, 32767),
971 SCALE(lev_right, 32767));
972 break;
978 * Set the ADC MUX to the specified values. We do NOT do any
979 * checking of the values passed, since we assume that the
980 * relevant *_select_input function has done that for us.
982 static void
983 waveartist_set_adc_mux(wavnc_info *devc, char left_dev, char right_dev)
985 unsigned int reg_08, reg_09;
987 reg_08 = waveartist_cmd1_r(devc, WACMD_GET_LEVEL | 0x0800);
988 reg_09 = waveartist_cmd1_r(devc, WACMD_GET_LEVEL | 0x0900);
990 reg_08 = (reg_08 & ~0x3f) | right_dev << 3 | left_dev;
992 waveartist_cmd3(devc, WACMD_SET_MIXER, reg_08, reg_09);
996 * Decode a recording mask into a mixer selection as follows:
998 * OSS Source WA Source Actual source
999 * SOUND_MASK_IMIX Mixer Mixer output (same as AD1848)
1000 * SOUND_MASK_LINE Line Line in
1001 * SOUND_MASK_LINE1 Aux 1 Aux 1 in
1002 * SOUND_MASK_LINE2 Aux 2 Aux 2 in
1003 * SOUND_MASK_MIC Mic Microphone
1005 static unsigned int
1006 waveartist_select_input(wavnc_info *devc, unsigned int recmask,
1007 unsigned char *dev_l, unsigned char *dev_r)
1009 unsigned int recdev = ADC_MUX_NONE;
1011 if (recmask & SOUND_MASK_IMIX) {
1012 recmask = SOUND_MASK_IMIX;
1013 recdev = ADC_MUX_MIXER;
1014 } else if (recmask & SOUND_MASK_LINE2) {
1015 recmask = SOUND_MASK_LINE2;
1016 recdev = ADC_MUX_AUX2;
1017 } else if (recmask & SOUND_MASK_LINE1) {
1018 recmask = SOUND_MASK_LINE1;
1019 recdev = ADC_MUX_AUX1;
1020 } else if (recmask & SOUND_MASK_LINE) {
1021 recmask = SOUND_MASK_LINE;
1022 recdev = ADC_MUX_LINE;
1023 } else if (recmask & SOUND_MASK_MIC) {
1024 recmask = SOUND_MASK_MIC;
1025 recdev = ADC_MUX_MIC;
1028 *dev_l = *dev_r = recdev;
1030 return recmask;
1033 static int
1034 waveartist_decode_mixer(wavnc_info *devc, int dev, unsigned char lev_l,
1035 unsigned char lev_r)
1037 switch (dev) {
1038 case SOUND_MIXER_VOLUME:
1039 case SOUND_MIXER_SYNTH:
1040 case SOUND_MIXER_PCM:
1041 case SOUND_MIXER_LINE:
1042 case SOUND_MIXER_MIC:
1043 case SOUND_MIXER_IGAIN:
1044 case SOUND_MIXER_LINE1:
1045 case SOUND_MIXER_LINE2:
1046 devc->levels[dev] = lev_l | lev_r << 8;
1047 break;
1049 case SOUND_MIXER_IMIX:
1050 break;
1052 default:
1053 dev = -EINVAL;
1054 break;
1057 return dev;
1060 static int waveartist_get_mixer(wavnc_info *devc, int dev)
1062 return devc->levels[dev];
1065 static const struct waveartist_mixer_info waveartist_mixer = {
1066 .supported_devs = SUPPORTED_MIXER_DEVICES | SOUND_MASK_IGAIN,
1067 .recording_devs = SOUND_MASK_LINE | SOUND_MASK_MIC |
1068 SOUND_MASK_LINE1 | SOUND_MASK_LINE2 |
1069 SOUND_MASK_IMIX,
1070 .stereo_devs = (SUPPORTED_MIXER_DEVICES | SOUND_MASK_IGAIN) & ~
1071 (SOUND_MASK_SPEAKER | SOUND_MASK_IMIX),
1072 .select_input = waveartist_select_input,
1073 .decode_mixer = waveartist_decode_mixer,
1074 .get_mixer = waveartist_get_mixer,
1077 static void
1078 waveartist_set_recmask(wavnc_info *devc, unsigned int recmask)
1080 unsigned char dev_l, dev_r;
1082 recmask &= devc->mix->recording_devs;
1085 * If more than one recording device selected,
1086 * disable the device that is currently in use.
1088 if (hweight32(recmask) > 1)
1089 recmask &= ~devc->recmask;
1092 * Translate the recording device mask into
1093 * the ADC multiplexer settings.
1095 devc->recmask = devc->mix->select_input(devc, recmask,
1096 &dev_l, &dev_r);
1098 waveartist_set_adc_mux(devc, dev_l, dev_r);
1101 static int
1102 waveartist_set_mixer(wavnc_info *devc, int dev, unsigned int level)
1104 unsigned int lev_left = level & 0x00ff;
1105 unsigned int lev_right = (level & 0xff00) >> 8;
1107 if (lev_left > 100)
1108 lev_left = 100;
1109 if (lev_right > 100)
1110 lev_right = 100;
1113 * Mono devices have their right volume forced to their
1114 * left volume. (from ALSA driver OSS emulation).
1116 if (!(devc->mix->stereo_devs & (1 << dev)))
1117 lev_right = lev_left;
1119 dev = devc->mix->decode_mixer(devc, dev, lev_left, lev_right);
1121 if (dev >= 0)
1122 waveartist_mixer_update(devc, dev);
1124 return dev < 0 ? dev : 0;
1127 static int
1128 waveartist_mixer_ioctl(int dev, unsigned int cmd, void __user * arg)
1130 wavnc_info *devc = (wavnc_info *)audio_devs[dev]->devc;
1131 int ret = 0, val, nr;
1134 * All SOUND_MIXER_* ioctls use type 'M'
1136 if (((cmd >> 8) & 255) != 'M')
1137 return -ENOIOCTLCMD;
1139 #ifdef CONFIG_ARCH_NETWINDER
1140 if (machine_is_netwinder()) {
1141 ret = vnc_private_ioctl(dev, cmd, arg);
1142 if (ret != -ENOIOCTLCMD)
1143 return ret;
1144 else
1145 ret = 0;
1147 #endif
1149 nr = cmd & 0xff;
1151 if (_SIOC_DIR(cmd) & _SIOC_WRITE) {
1152 if (get_user(val, (int __user *)arg))
1153 return -EFAULT;
1155 switch (nr) {
1156 case SOUND_MIXER_RECSRC:
1157 waveartist_set_recmask(devc, val);
1158 break;
1160 default:
1161 ret = -EINVAL;
1162 if (nr < SOUND_MIXER_NRDEVICES &&
1163 devc->mix->supported_devs & (1 << nr))
1164 ret = waveartist_set_mixer(devc, nr, val);
1168 if (ret == 0 && _SIOC_DIR(cmd) & _SIOC_READ) {
1169 ret = -EINVAL;
1171 switch (nr) {
1172 case SOUND_MIXER_RECSRC:
1173 ret = devc->recmask;
1174 break;
1176 case SOUND_MIXER_DEVMASK:
1177 ret = devc->mix->supported_devs;
1178 break;
1180 case SOUND_MIXER_STEREODEVS:
1181 ret = devc->mix->stereo_devs;
1182 break;
1184 case SOUND_MIXER_RECMASK:
1185 ret = devc->mix->recording_devs;
1186 break;
1188 case SOUND_MIXER_CAPS:
1189 ret = SOUND_CAP_EXCL_INPUT;
1190 break;
1192 default:
1193 if (nr < SOUND_MIXER_NRDEVICES)
1194 ret = devc->mix->get_mixer(devc, nr);
1195 break;
1198 if (ret >= 0)
1199 ret = put_user(ret, (int __user *)arg) ? -EFAULT : 0;
1202 return ret;
1205 static struct mixer_operations waveartist_mixer_operations =
1207 .owner = THIS_MODULE,
1208 .id = "WaveArtist",
1209 .name = "WaveArtist",
1210 .ioctl = waveartist_mixer_ioctl
1213 static void
1214 waveartist_mixer_reset(wavnc_info *devc)
1216 int i;
1218 if (debug_flg & DEBUG_MIXER)
1219 printk("%s: mixer_reset\n", devc->hw.name);
1222 * reset mixer cmd
1224 waveartist_cmd1(devc, WACMD_RST_MIXER);
1227 * set input for ADC to come from 'quiet'
1228 * turn on default modes
1230 waveartist_cmd3(devc, WACMD_SET_MIXER, 0x9800, 0xa836);
1233 * set mixer input select to none, RX filter gains 0 dB
1235 waveartist_cmd3(devc, WACMD_SET_MIXER, 0x4c00, 0x8c00);
1238 * set bit 0 reg 2 to 1 - unmute MonoOut
1240 waveartist_cmd3(devc, WACMD_SET_MIXER, 0x2801, 0x6800);
1242 /* set default input device = internal mic
1243 * current recording device = none
1245 waveartist_set_recmask(devc, 0);
1247 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
1248 waveartist_mixer_update(devc, i);
1251 static int __init waveartist_init(wavnc_info *devc)
1253 wavnc_port_info *portc;
1254 char rev[3], dev_name[64];
1255 int my_dev;
1257 if (waveartist_reset(devc))
1258 return -ENODEV;
1260 sprintf(dev_name, "%s (%s", devc->hw.name, devc->chip_name);
1262 if (waveartist_getrev(devc, rev)) {
1263 strcat(dev_name, " rev. ");
1264 strcat(dev_name, rev);
1266 strcat(dev_name, ")");
1268 conf_printf2(dev_name, devc->hw.io_base, devc->hw.irq,
1269 devc->hw.dma, devc->hw.dma2);
1271 portc = (wavnc_port_info *)kmalloc(sizeof(wavnc_port_info), GFP_KERNEL);
1272 if (portc == NULL)
1273 goto nomem;
1275 memset(portc, 0, sizeof(wavnc_port_info));
1277 my_dev = sound_install_audiodrv(AUDIO_DRIVER_VERSION, dev_name,
1278 &waveartist_audio_driver, sizeof(struct audio_driver),
1279 devc->audio_flags, AFMT_U8 | AFMT_S16_LE | AFMT_S8,
1280 devc, devc->hw.dma, devc->hw.dma2);
1282 if (my_dev < 0)
1283 goto free;
1285 audio_devs[my_dev]->portc = portc;
1287 waveartist_mixer_reset(devc);
1290 * clear any pending interrupt
1292 waveartist_iack(devc);
1294 if (request_irq(devc->hw.irq, waveartist_intr, 0, devc->hw.name, devc) < 0) {
1295 printk(KERN_ERR "%s: IRQ %d in use\n",
1296 devc->hw.name, devc->hw.irq);
1297 goto uninstall;
1300 if (sound_alloc_dma(devc->hw.dma, devc->hw.name)) {
1301 printk(KERN_ERR "%s: Can't allocate DMA%d\n",
1302 devc->hw.name, devc->hw.dma);
1303 goto uninstall_irq;
1306 if (devc->hw.dma != devc->hw.dma2 && devc->hw.dma2 != NO_DMA)
1307 if (sound_alloc_dma(devc->hw.dma2, devc->hw.name)) {
1308 printk(KERN_ERR "%s: can't allocate DMA%d\n",
1309 devc->hw.name, devc->hw.dma2);
1310 goto uninstall_dma;
1313 waveartist_set_ctlr(&devc->hw, 0, DMA1_IE | DMA0_IE);
1315 audio_devs[my_dev]->mixer_dev =
1316 sound_install_mixer(MIXER_DRIVER_VERSION,
1317 dev_name,
1318 &waveartist_mixer_operations,
1319 sizeof(struct mixer_operations),
1320 devc);
1322 return my_dev;
1324 uninstall_dma:
1325 sound_free_dma(devc->hw.dma);
1327 uninstall_irq:
1328 free_irq(devc->hw.irq, devc);
1330 uninstall:
1331 sound_unload_audiodev(my_dev);
1333 free:
1334 kfree(portc);
1336 nomem:
1337 return -1;
1340 static int __init probe_waveartist(struct address_info *hw_config)
1342 wavnc_info *devc = &adev_info[nr_waveartist_devs];
1344 if (nr_waveartist_devs >= MAX_AUDIO_DEV) {
1345 printk(KERN_WARNING "waveartist: too many audio devices\n");
1346 return 0;
1349 if (!request_region(hw_config->io_base, 15, hw_config->name)) {
1350 printk(KERN_WARNING "WaveArtist: I/O port conflict\n");
1351 return 0;
1354 if (hw_config->irq > 15 || hw_config->irq < 0) {
1355 release_region(hw_config->io_base, 15);
1356 printk(KERN_WARNING "WaveArtist: Bad IRQ %d\n",
1357 hw_config->irq);
1358 return 0;
1361 if (hw_config->dma != 3) {
1362 release_region(hw_config->io_base, 15);
1363 printk(KERN_WARNING "WaveArtist: Bad DMA %d\n",
1364 hw_config->dma);
1365 return 0;
1368 hw_config->name = "WaveArtist";
1369 devc->hw = *hw_config;
1370 devc->open_mode = 0;
1371 devc->chip_name = "RWA-010";
1373 return 1;
1376 static void __init
1377 attach_waveartist(struct address_info *hw, const struct waveartist_mixer_info *mix)
1379 wavnc_info *devc = &adev_info[nr_waveartist_devs];
1382 * NOTE! If irq < 0, there is another driver which has allocated the
1383 * IRQ so that this driver doesn't need to allocate/deallocate it.
1384 * The actually used IRQ is ABS(irq).
1386 devc->hw = *hw;
1387 devc->hw.irq = (hw->irq > 0) ? hw->irq : 0;
1388 devc->open_mode = 0;
1389 devc->playback_dev = 0;
1390 devc->record_dev = 0;
1391 devc->audio_flags = DMA_AUTOMODE;
1392 devc->levels = levels;
1394 if (hw->dma != hw->dma2 && hw->dma2 != NO_DMA)
1395 devc->audio_flags |= DMA_DUPLEX;
1397 devc->mix = mix;
1398 devc->dev_no = waveartist_init(devc);
1400 if (devc->dev_no < 0)
1401 release_region(hw->io_base, 15);
1402 else {
1403 #ifdef CONFIG_ARCH_NETWINDER
1404 if (machine_is_netwinder()) {
1405 init_timer(&vnc_timer);
1406 vnc_timer.function = vnc_slider_tick;
1407 vnc_timer.expires = jiffies;
1408 vnc_timer.data = nr_waveartist_devs;
1409 add_timer(&vnc_timer);
1411 vnc_configure_mixer(devc, 0);
1413 devc->no_autoselect = 1;
1415 #endif
1416 nr_waveartist_devs += 1;
1420 static void __exit unload_waveartist(struct address_info *hw)
1422 wavnc_info *devc = NULL;
1423 int i;
1425 for (i = 0; i < nr_waveartist_devs; i++)
1426 if (hw->io_base == adev_info[i].hw.io_base) {
1427 devc = adev_info + i;
1428 break;
1431 if (devc != NULL) {
1432 int mixer;
1434 #ifdef CONFIG_ARCH_NETWINDER
1435 if (machine_is_netwinder())
1436 del_timer(&vnc_timer);
1437 #endif
1439 release_region(devc->hw.io_base, 15);
1441 waveartist_set_ctlr(&devc->hw, DMA1_IE|DMA0_IE, 0);
1443 if (devc->hw.irq >= 0)
1444 free_irq(devc->hw.irq, devc);
1446 sound_free_dma(devc->hw.dma);
1448 if (devc->hw.dma != devc->hw.dma2 &&
1449 devc->hw.dma2 != NO_DMA)
1450 sound_free_dma(devc->hw.dma2);
1452 mixer = audio_devs[devc->dev_no]->mixer_dev;
1454 if (mixer >= 0)
1455 sound_unload_mixerdev(mixer);
1457 if (devc->dev_no >= 0)
1458 sound_unload_audiodev(devc->dev_no);
1460 nr_waveartist_devs -= 1;
1462 for (; i < nr_waveartist_devs; i++)
1463 adev_info[i] = adev_info[i + 1];
1464 } else
1465 printk(KERN_WARNING "waveartist: can't find device "
1466 "to unload\n");
1469 #ifdef CONFIG_ARCH_NETWINDER
1472 * Rebel.com Netwinder specifics...
1475 #include <asm/hardware/dec21285.h>
1477 #define VNC_TIMER_PERIOD (HZ/4) //check slider 4 times/sec
1479 #define MIXER_PRIVATE3_RESET 0x53570000
1480 #define MIXER_PRIVATE3_READ 0x53570001
1481 #define MIXER_PRIVATE3_WRITE 0x53570002
1483 #define VNC_MUTE_INTERNAL_SPKR 0x01 //the sw mute on/off control bit
1484 #define VNC_MUTE_LINE_OUT 0x10
1485 #define VNC_PHONE_DETECT 0x20
1486 #define VNC_HANDSET_DETECT 0x40
1487 #define VNC_DISABLE_AUTOSWITCH 0x80
1489 extern spinlock_t gpio_lock;
1491 static inline void
1492 vnc_mute_spkr(wavnc_info *devc)
1494 unsigned long flags;
1496 spin_lock_irqsave(&gpio_lock, flags);
1497 cpld_modify(CPLD_UNMUTE, devc->spkr_mute_state ? 0 : CPLD_UNMUTE);
1498 spin_unlock_irqrestore(&gpio_lock, flags);
1501 static void
1502 vnc_mute_lout(wavnc_info *devc)
1504 unsigned int left, right;
1506 left = waveartist_cmd1_r(devc, WACMD_GET_LEVEL);
1507 right = waveartist_cmd1_r(devc, WACMD_GET_LEVEL | 0x400);
1509 if (devc->line_mute_state) {
1510 left &= ~1;
1511 right &= ~1;
1512 } else {
1513 left |= 1;
1514 right |= 1;
1516 waveartist_cmd3(devc, WACMD_SET_MIXER, left, right);
1520 static int
1521 vnc_volume_slider(wavnc_info *devc)
1523 static signed int old_slider_volume;
1524 unsigned long flags;
1525 signed int volume = 255;
1527 *CSR_TIMER1_LOAD = 0x00ffffff;
1529 spin_lock_irqsave(&waveartist_lock, flags);
1531 outb(0xFF, 0x201);
1532 *CSR_TIMER1_CNTL = TIMER_CNTL_ENABLE | TIMER_CNTL_DIV1;
1534 while (volume && (inb(0x201) & 0x01))
1535 volume--;
1537 *CSR_TIMER1_CNTL = 0;
1539 spin_unlock_irqrestore(&waveartist_lock,flags);
1541 volume = 0x00ffffff - *CSR_TIMER1_VALUE;
1544 #ifndef REVERSE
1545 volume = 150 - (volume >> 5);
1546 #else
1547 volume = (volume >> 6) - 25;
1548 #endif
1550 if (volume < 0)
1551 volume = 0;
1553 if (volume > 100)
1554 volume = 100;
1557 * slider quite often reads +-8, so debounce this random noise
1559 if (abs(volume - old_slider_volume) > 7) {
1560 old_slider_volume = volume;
1562 if (debug_flg & DEBUG_MIXER)
1563 printk(KERN_DEBUG "Slider volume: %d.\n", volume);
1566 return old_slider_volume;
1570 * Decode a recording mask into a mixer selection on the NetWinder
1571 * as follows:
1573 * OSS Source WA Source Actual source
1574 * SOUND_MASK_IMIX Mixer Mixer output (same as AD1848)
1575 * SOUND_MASK_LINE Line Line in
1576 * SOUND_MASK_LINE1 Left Mic Handset
1577 * SOUND_MASK_PHONEIN Left Aux Telephone microphone
1578 * SOUND_MASK_MIC Right Mic Builtin microphone
1580 static unsigned int
1581 netwinder_select_input(wavnc_info *devc, unsigned int recmask,
1582 unsigned char *dev_l, unsigned char *dev_r)
1584 unsigned int recdev_l = ADC_MUX_NONE, recdev_r = ADC_MUX_NONE;
1586 if (recmask & SOUND_MASK_IMIX) {
1587 recmask = SOUND_MASK_IMIX;
1588 recdev_l = ADC_MUX_MIXER;
1589 recdev_r = ADC_MUX_MIXER;
1590 } else if (recmask & SOUND_MASK_LINE) {
1591 recmask = SOUND_MASK_LINE;
1592 recdev_l = ADC_MUX_LINE;
1593 recdev_r = ADC_MUX_LINE;
1594 } else if (recmask & SOUND_MASK_LINE1) {
1595 recmask = SOUND_MASK_LINE1;
1596 waveartist_cmd1(devc, WACMD_SET_MONO); /* left */
1597 recdev_l = ADC_MUX_MIC;
1598 recdev_r = ADC_MUX_NONE;
1599 } else if (recmask & SOUND_MASK_PHONEIN) {
1600 recmask = SOUND_MASK_PHONEIN;
1601 waveartist_cmd1(devc, WACMD_SET_MONO); /* left */
1602 recdev_l = ADC_MUX_AUX1;
1603 recdev_r = ADC_MUX_NONE;
1604 } else if (recmask & SOUND_MASK_MIC) {
1605 recmask = SOUND_MASK_MIC;
1606 waveartist_cmd1(devc, WACMD_SET_MONO | 0x100); /* right */
1607 recdev_l = ADC_MUX_NONE;
1608 recdev_r = ADC_MUX_MIC;
1611 *dev_l = recdev_l;
1612 *dev_r = recdev_r;
1614 return recmask;
1617 static int
1618 netwinder_decode_mixer(wavnc_info *devc, int dev, unsigned char lev_l,
1619 unsigned char lev_r)
1621 switch (dev) {
1622 case SOUND_MIXER_VOLUME:
1623 case SOUND_MIXER_SYNTH:
1624 case SOUND_MIXER_PCM:
1625 case SOUND_MIXER_LINE:
1626 case SOUND_MIXER_IGAIN:
1627 devc->levels[dev] = lev_l | lev_r << 8;
1628 break;
1630 case SOUND_MIXER_MIC: /* right mic only */
1631 devc->levels[SOUND_MIXER_MIC] &= 0xff;
1632 devc->levels[SOUND_MIXER_MIC] |= lev_l << 8;
1633 break;
1635 case SOUND_MIXER_LINE1: /* left mic only */
1636 devc->levels[SOUND_MIXER_MIC] &= 0xff00;
1637 devc->levels[SOUND_MIXER_MIC] |= lev_l;
1638 dev = SOUND_MIXER_MIC;
1639 break;
1641 case SOUND_MIXER_PHONEIN: /* left aux only */
1642 devc->levels[SOUND_MIXER_LINE1] = lev_l;
1643 dev = SOUND_MIXER_LINE1;
1644 break;
1646 case SOUND_MIXER_IMIX:
1647 case SOUND_MIXER_PHONEOUT:
1648 break;
1650 default:
1651 dev = -EINVAL;
1652 break;
1654 return dev;
1657 static int netwinder_get_mixer(wavnc_info *devc, int dev)
1659 int levels;
1661 switch (dev) {
1662 case SOUND_MIXER_VOLUME:
1663 case SOUND_MIXER_SYNTH:
1664 case SOUND_MIXER_PCM:
1665 case SOUND_MIXER_LINE:
1666 case SOUND_MIXER_IGAIN:
1667 levels = devc->levels[dev];
1668 break;
1670 case SOUND_MIXER_MIC: /* builtin mic: right mic only */
1671 levels = devc->levels[SOUND_MIXER_MIC] >> 8;
1672 levels |= levels << 8;
1673 break;
1675 case SOUND_MIXER_LINE1: /* handset mic: left mic only */
1676 levels = devc->levels[SOUND_MIXER_MIC] & 0xff;
1677 levels |= levels << 8;
1678 break;
1680 case SOUND_MIXER_PHONEIN: /* phone mic: left aux1 only */
1681 levels = devc->levels[SOUND_MIXER_LINE1] & 0xff;
1682 levels |= levels << 8;
1683 break;
1685 default:
1686 levels = 0;
1689 return levels;
1693 * Waveartist specific mixer information.
1695 static const struct waveartist_mixer_info netwinder_mixer = {
1696 .supported_devs = SOUND_MASK_VOLUME | SOUND_MASK_SYNTH |
1697 SOUND_MASK_PCM | SOUND_MASK_SPEAKER |
1698 SOUND_MASK_LINE | SOUND_MASK_MIC |
1699 SOUND_MASK_IMIX | SOUND_MASK_LINE1 |
1700 SOUND_MASK_PHONEIN | SOUND_MASK_PHONEOUT|
1701 SOUND_MASK_IGAIN,
1703 .recording_devs = SOUND_MASK_LINE | SOUND_MASK_MIC |
1704 SOUND_MASK_IMIX | SOUND_MASK_LINE1 |
1705 SOUND_MASK_PHONEIN,
1707 .stereo_devs = SOUND_MASK_VOLUME | SOUND_MASK_SYNTH |
1708 SOUND_MASK_PCM | SOUND_MASK_LINE |
1709 SOUND_MASK_IMIX | SOUND_MASK_IGAIN,
1711 .select_input = netwinder_select_input,
1712 .decode_mixer = netwinder_decode_mixer,
1713 .get_mixer = netwinder_get_mixer,
1716 static void
1717 vnc_configure_mixer(wavnc_info *devc, unsigned int recmask)
1719 if (!devc->no_autoselect) {
1720 if (devc->handset_detect) {
1721 recmask = SOUND_MASK_LINE1;
1722 devc->spkr_mute_state = devc->line_mute_state = 1;
1723 } else if (devc->telephone_detect) {
1724 recmask = SOUND_MASK_PHONEIN;
1725 devc->spkr_mute_state = devc->line_mute_state = 1;
1726 } else {
1727 /* unless someone has asked for LINE-IN,
1728 * we default to MIC
1730 if ((devc->recmask & SOUND_MASK_LINE) == 0)
1731 devc->recmask = SOUND_MASK_MIC;
1732 devc->spkr_mute_state = devc->line_mute_state = 0;
1734 vnc_mute_spkr(devc);
1735 vnc_mute_lout(devc);
1737 if (recmask != devc->recmask)
1738 waveartist_set_recmask(devc, recmask);
1742 static int
1743 vnc_slider(wavnc_info *devc)
1745 signed int slider_volume;
1746 unsigned int temp, old_hs, old_td;
1749 * read the "buttons" state.
1750 * Bit 4 = 0 means handset present
1751 * Bit 5 = 1 means phone offhook
1753 temp = inb(0x201);
1755 old_hs = devc->handset_detect;
1756 old_td = devc->telephone_detect;
1758 devc->handset_detect = !(temp & 0x10);
1759 devc->telephone_detect = !!(temp & 0x20);
1761 if (!devc->no_autoselect &&
1762 (old_hs != devc->handset_detect ||
1763 old_td != devc->telephone_detect))
1764 vnc_configure_mixer(devc, devc->recmask);
1766 slider_volume = vnc_volume_slider(devc);
1769 * If we're using software controlled volume, and
1770 * the slider moves by more than 20%, then we
1771 * switch back to slider controlled volume.
1773 if (abs(devc->slider_vol - slider_volume) > 20)
1774 devc->use_slider = 1;
1777 * use only left channel
1779 temp = levels[SOUND_MIXER_VOLUME] & 0xFF;
1781 if (slider_volume != temp && devc->use_slider) {
1782 devc->slider_vol = slider_volume;
1784 waveartist_set_mixer(devc, SOUND_MIXER_VOLUME,
1785 slider_volume | slider_volume << 8);
1787 return 1;
1790 return 0;
1793 static void
1794 vnc_slider_tick(unsigned long data)
1796 int next_timeout;
1798 if (vnc_slider(adev_info + data))
1799 next_timeout = 5; // mixer reported change
1800 else
1801 next_timeout = VNC_TIMER_PERIOD;
1803 mod_timer(&vnc_timer, jiffies + next_timeout);
1806 static int
1807 vnc_private_ioctl(int dev, unsigned int cmd, int __user * arg)
1809 wavnc_info *devc = (wavnc_info *)audio_devs[dev]->devc;
1810 int val;
1812 switch (cmd) {
1813 case SOUND_MIXER_PRIVATE1:
1815 u_int prev_spkr_mute, prev_line_mute, prev_auto_state;
1816 int val;
1818 if (get_user(val, arg))
1819 return -EFAULT;
1821 /* check if parameter is logical */
1822 if (val & ~(VNC_MUTE_INTERNAL_SPKR |
1823 VNC_MUTE_LINE_OUT |
1824 VNC_DISABLE_AUTOSWITCH))
1825 return -EINVAL;
1827 prev_auto_state = devc->no_autoselect;
1828 prev_spkr_mute = devc->spkr_mute_state;
1829 prev_line_mute = devc->line_mute_state;
1831 devc->no_autoselect = (val & VNC_DISABLE_AUTOSWITCH) ? 1 : 0;
1832 devc->spkr_mute_state = (val & VNC_MUTE_INTERNAL_SPKR) ? 1 : 0;
1833 devc->line_mute_state = (val & VNC_MUTE_LINE_OUT) ? 1 : 0;
1835 if (prev_spkr_mute != devc->spkr_mute_state)
1836 vnc_mute_spkr(devc);
1838 if (prev_line_mute != devc->line_mute_state)
1839 vnc_mute_lout(devc);
1841 if (prev_auto_state != devc->no_autoselect)
1842 vnc_configure_mixer(devc, devc->recmask);
1844 return 0;
1847 case SOUND_MIXER_PRIVATE2:
1848 if (get_user(val, arg))
1849 return -EFAULT;
1851 switch (val) {
1852 #define VNC_SOUND_PAUSE 0x53 //to pause the DSP
1853 #define VNC_SOUND_RESUME 0x57 //to unpause the DSP
1854 case VNC_SOUND_PAUSE:
1855 waveartist_cmd1(devc, 0x16);
1856 break;
1858 case VNC_SOUND_RESUME:
1859 waveartist_cmd1(devc, 0x18);
1860 break;
1862 default:
1863 return -EINVAL;
1865 return 0;
1867 /* private ioctl to allow bulk access to waveartist */
1868 case SOUND_MIXER_PRIVATE3:
1870 unsigned long flags;
1871 int mixer_reg[15], i, val;
1873 if (get_user(val, arg))
1874 return -EFAULT;
1875 if (copy_from_user(mixer_reg, (void *)val, sizeof(mixer_reg)))
1876 return -EFAULT;
1878 switch (mixer_reg[14]) {
1879 case MIXER_PRIVATE3_RESET:
1880 waveartist_mixer_reset(devc);
1881 break;
1883 case MIXER_PRIVATE3_WRITE:
1884 waveartist_cmd3(devc, WACMD_SET_MIXER, mixer_reg[0], mixer_reg[4]);
1885 waveartist_cmd3(devc, WACMD_SET_MIXER, mixer_reg[1], mixer_reg[5]);
1886 waveartist_cmd3(devc, WACMD_SET_MIXER, mixer_reg[2], mixer_reg[6]);
1887 waveartist_cmd3(devc, WACMD_SET_MIXER, mixer_reg[3], mixer_reg[7]);
1888 waveartist_cmd3(devc, WACMD_SET_MIXER, mixer_reg[8], mixer_reg[9]);
1890 waveartist_cmd3(devc, WACMD_SET_LEVEL, mixer_reg[10], mixer_reg[11]);
1891 waveartist_cmd3(devc, WACMD_SET_LEVEL, mixer_reg[12], mixer_reg[13]);
1892 break;
1894 case MIXER_PRIVATE3_READ:
1895 spin_lock_irqsave(&waveartist_lock, flags);
1897 for (i = 0x30; i < 14 << 8; i += 1 << 8)
1898 waveartist_cmd(devc, 1, &i, 1, mixer_reg + (i >> 8));
1900 spin_unlock_irqrestore(&waveartist_lock, flags);
1902 if (copy_to_user((void *)val, mixer_reg, sizeof(mixer_reg)))
1903 return -EFAULT;
1904 break;
1906 default:
1907 return -EINVAL;
1909 return 0;
1912 /* read back the state from PRIVATE1 */
1913 case SOUND_MIXER_PRIVATE4:
1914 val = (devc->spkr_mute_state ? VNC_MUTE_INTERNAL_SPKR : 0) |
1915 (devc->line_mute_state ? VNC_MUTE_LINE_OUT : 0) |
1916 (devc->handset_detect ? VNC_HANDSET_DETECT : 0) |
1917 (devc->telephone_detect ? VNC_PHONE_DETECT : 0) |
1918 (devc->no_autoselect ? VNC_DISABLE_AUTOSWITCH : 0);
1920 return put_user(val, arg) ? -EFAULT : 0;
1923 if (_SIOC_DIR(cmd) & _SIOC_WRITE) {
1925 * special case for master volume: if we
1926 * received this call - switch from hw
1927 * volume control to a software volume
1928 * control, till the hw volume is modified
1929 * to signal that user wants to be back in
1930 * hardware...
1932 if ((cmd & 0xff) == SOUND_MIXER_VOLUME)
1933 devc->use_slider = 0;
1935 /* speaker output */
1936 if ((cmd & 0xff) == SOUND_MIXER_SPEAKER) {
1937 unsigned int val, l, r;
1939 if (get_user(val, arg))
1940 return -EFAULT;
1942 l = val & 0x7f;
1943 r = (val & 0x7f00) >> 8;
1944 val = (l + r) / 2;
1945 devc->levels[SOUND_MIXER_SPEAKER] = val | (val << 8);
1946 devc->spkr_mute_state = (val <= 50);
1947 vnc_mute_spkr(devc);
1948 return 0;
1952 return -ENOIOCTLCMD;
1955 #endif
1957 static struct address_info cfg;
1959 static int attached;
1961 static int __initdata io = 0;
1962 static int __initdata irq = 0;
1963 static int __initdata dma = 0;
1964 static int __initdata dma2 = 0;
1967 static int __init init_waveartist(void)
1969 const struct waveartist_mixer_info *mix;
1971 if (!io && machine_is_netwinder()) {
1973 * The NetWinder WaveArtist is at a fixed address.
1974 * If the user does not supply an address, use the
1975 * well-known parameters.
1977 io = 0x250;
1978 irq = 12;
1979 dma = 3;
1980 dma2 = 7;
1983 mix = &waveartist_mixer;
1984 #ifdef CONFIG_ARCH_NETWINDER
1985 if (machine_is_netwinder())
1986 mix = &netwinder_mixer;
1987 #endif
1989 cfg.io_base = io;
1990 cfg.irq = irq;
1991 cfg.dma = dma;
1992 cfg.dma2 = dma2;
1994 if (!probe_waveartist(&cfg))
1995 return -ENODEV;
1997 attach_waveartist(&cfg, mix);
1998 attached = 1;
2000 return 0;
2003 static void __exit cleanup_waveartist(void)
2005 if (attached)
2006 unload_waveartist(&cfg);
2009 module_init(init_waveartist);
2010 module_exit(cleanup_waveartist);
2012 #ifndef MODULE
2013 static int __init setup_waveartist(char *str)
2015 /* io, irq, dma, dma2 */
2016 int ints[5];
2018 str = get_options(str, ARRAY_SIZE(ints), ints);
2020 io = ints[1];
2021 irq = ints[2];
2022 dma = ints[3];
2023 dma2 = ints[4];
2025 return 1;
2027 __setup("waveartist=", setup_waveartist);
2028 #endif
2030 MODULE_DESCRIPTION("Rockwell WaveArtist RWA-010 sound driver");
2031 MODULE_PARM(io, "i"); /* IO base */
2032 MODULE_PARM(irq, "i"); /* IRQ */
2033 MODULE_PARM(dma, "i"); /* DMA */
2034 MODULE_PARM(dma2, "i"); /* DMA2 */
2035 MODULE_LICENSE("GPL");