[TCP]: Catch skb with S+L bugs earlier
[linux-2.6/mini2440.git] / sound / oss / waveartist.c
blobb48c72923a13dfb30c35669213112a31baa4ffc2
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/sched.h>
39 #include <linux/interrupt.h>
40 #include <linux/delay.h>
41 #include <linux/spinlock.h>
42 #include <linux/bitops.h>
44 #include <asm/system.h>
46 #include "sound_config.h"
47 #include "waveartist.h"
49 #ifdef CONFIG_ARM
50 #include <asm/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 = timeout_ms * 10 * HZ / 100;
188 do {
189 set_current_state(TASK_INTERRUPTIBLE);
190 timeout = schedule_timeout(timeout);
191 } while (timeout);
193 return 0;
196 static int
197 waveartist_reset(wavnc_info *devc)
199 struct address_info *hw = &devc->hw;
200 unsigned int timeout, res = -1;
202 waveartist_set_ctlr(hw, -1, RESET);
203 waveartist_sleep(2);
204 waveartist_set_ctlr(hw, RESET, 0);
206 timeout = 500;
207 do {
208 mdelay(2);
210 if (inb(hw->io_base + STATR) & CMD_RF) {
211 res = inw(hw->io_base + CMDR);
212 if (res == 0x55aa)
213 break;
215 } while (--timeout);
217 if (timeout == 0) {
218 printk(KERN_WARNING "WaveArtist: reset timeout ");
219 if (res != (unsigned int)-1)
220 printk("(res=%04X)", res);
221 printk("\n");
222 return 1;
224 return 0;
227 /* Helper function to send and receive words
228 * from WaveArtist. It handles all the handshaking
229 * and can send or receive multiple words.
231 static int
232 waveartist_cmd(wavnc_info *devc,
233 int nr_cmd, unsigned int *cmd,
234 int nr_resp, unsigned int *resp)
236 unsigned int io_base = devc->hw.io_base;
237 unsigned int timed_out = 0;
238 unsigned int i;
240 if (debug_flg & DEBUG_CMD) {
241 printk("waveartist_cmd: cmd=");
243 for (i = 0; i < nr_cmd; i++)
244 printk("%04X ", cmd[i]);
246 printk("\n");
249 if (inb(io_base + STATR) & CMD_RF) {
250 int old_data;
252 /* flush the port
255 old_data = inw(io_base + CMDR);
257 if (debug_flg & DEBUG_CMD)
258 printk("flushed %04X...", old_data);
260 udelay(10);
263 for (i = 0; !timed_out && i < nr_cmd; i++) {
264 int count;
266 for (count = 5000; count; count--)
267 if (inb(io_base + STATR) & CMD_WE)
268 break;
270 if (!count)
271 timed_out = 1;
272 else
273 outw(cmd[i], io_base + CMDR);
276 for (i = 0; !timed_out && i < nr_resp; i++) {
277 int count;
279 for (count = 5000; count; count--)
280 if (inb(io_base + STATR) & CMD_RF)
281 break;
283 if (!count)
284 timed_out = 1;
285 else
286 resp[i] = inw(io_base + CMDR);
289 if (debug_flg & DEBUG_CMD) {
290 if (!timed_out) {
291 printk("waveartist_cmd: resp=");
293 for (i = 0; i < nr_resp; i++)
294 printk("%04X ", resp[i]);
296 printk("\n");
297 } else
298 printk("waveartist_cmd: timed out\n");
301 return timed_out ? 1 : 0;
305 * Send one command word
307 static inline int
308 waveartist_cmd1(wavnc_info *devc, unsigned int cmd)
310 return waveartist_cmd(devc, 1, &cmd, 0, NULL);
314 * Send one command, receive one word
316 static inline unsigned int
317 waveartist_cmd1_r(wavnc_info *devc, unsigned int cmd)
319 unsigned int ret;
321 waveartist_cmd(devc, 1, &cmd, 1, &ret);
323 return ret;
327 * Send a double command, receive one
328 * word (and throw it away)
330 static inline int
331 waveartist_cmd2(wavnc_info *devc, unsigned int cmd, unsigned int arg)
333 unsigned int vals[2];
335 vals[0] = cmd;
336 vals[1] = arg;
338 return waveartist_cmd(devc, 2, vals, 1, vals);
342 * Send a triple command
344 static inline int
345 waveartist_cmd3(wavnc_info *devc, unsigned int cmd,
346 unsigned int arg1, unsigned int arg2)
348 unsigned int vals[3];
350 vals[0] = cmd;
351 vals[1] = arg1;
352 vals[2] = arg2;
354 return waveartist_cmd(devc, 3, vals, 0, NULL);
357 static int
358 waveartist_getrev(wavnc_info *devc, char *rev)
360 unsigned int temp[2];
361 unsigned int cmd = WACMD_GETREV;
363 waveartist_cmd(devc, 1, &cmd, 2, temp);
365 rev[0] = temp[0] >> 8;
366 rev[1] = temp[0] & 255;
367 rev[2] = '\0';
369 return temp[0];
372 static void waveartist_halt_output(int dev);
373 static void waveartist_halt_input(int dev);
374 static void waveartist_halt(int dev);
375 static void waveartist_trigger(int dev, int state);
377 static int
378 waveartist_open(int dev, int mode)
380 wavnc_info *devc;
381 wavnc_port_info *portc;
382 unsigned long flags;
384 if (dev < 0 || dev >= num_audiodevs)
385 return -ENXIO;
387 devc = (wavnc_info *) audio_devs[dev]->devc;
388 portc = (wavnc_port_info *) audio_devs[dev]->portc;
390 spin_lock_irqsave(&waveartist_lock, flags);
391 if (portc->open_mode || (devc->open_mode & mode)) {
392 spin_unlock_irqrestore(&waveartist_lock, flags);
393 return -EBUSY;
396 devc->audio_mode = 0;
397 devc->open_mode |= mode;
398 portc->open_mode = mode;
399 waveartist_trigger(dev, 0);
401 if (mode & OPEN_READ)
402 devc->record_dev = dev;
403 if (mode & OPEN_WRITE)
404 devc->playback_dev = dev;
405 spin_unlock_irqrestore(&waveartist_lock, flags);
407 return 0;
410 static void
411 waveartist_close(int dev)
413 wavnc_info *devc = (wavnc_info *) audio_devs[dev]->devc;
414 wavnc_port_info *portc = (wavnc_port_info *) audio_devs[dev]->portc;
415 unsigned long flags;
417 spin_lock_irqsave(&waveartist_lock, flags);
419 waveartist_halt(dev);
421 devc->audio_mode = 0;
422 devc->open_mode &= ~portc->open_mode;
423 portc->open_mode = 0;
425 spin_unlock_irqrestore(&waveartist_lock, flags);
428 static void
429 waveartist_output_block(int dev, unsigned long buf, int __count, int intrflag)
431 wavnc_port_info *portc = (wavnc_port_info *) audio_devs[dev]->portc;
432 wavnc_info *devc = (wavnc_info *) audio_devs[dev]->devc;
433 unsigned long flags;
434 unsigned int count = __count;
436 if (debug_flg & DEBUG_OUT)
437 printk("waveartist: output block, buf=0x%lx, count=0x%x...\n",
438 buf, count);
440 * 16 bit data
442 if (portc->audio_format & (AFMT_S16_LE | AFMT_S16_BE))
443 count >>= 1;
445 if (portc->channels > 1)
446 count >>= 1;
448 count -= 1;
450 if (devc->audio_mode & PCM_ENABLE_OUTPUT &&
451 audio_devs[dev]->flags & DMA_AUTOMODE &&
452 intrflag &&
453 count == devc->xfer_count) {
454 devc->audio_mode |= PCM_ENABLE_OUTPUT;
455 return; /*
456 * Auto DMA mode on. No need to react
460 spin_lock_irqsave(&waveartist_lock, flags);
463 * set sample count
465 waveartist_cmd2(devc, WACMD_OUTPUTSIZE, count);
467 devc->xfer_count = count;
468 devc->audio_mode |= PCM_ENABLE_OUTPUT;
470 spin_unlock_irqrestore(&waveartist_lock, flags);
473 static void
474 waveartist_start_input(int dev, unsigned long buf, int __count, int intrflag)
476 wavnc_port_info *portc = (wavnc_port_info *) audio_devs[dev]->portc;
477 wavnc_info *devc = (wavnc_info *) audio_devs[dev]->devc;
478 unsigned long flags;
479 unsigned int count = __count;
481 if (debug_flg & DEBUG_IN)
482 printk("waveartist: start input, buf=0x%lx, count=0x%x...\n",
483 buf, count);
485 if (portc->audio_format & (AFMT_S16_LE | AFMT_S16_BE)) /* 16 bit data */
486 count >>= 1;
488 if (portc->channels > 1)
489 count >>= 1;
491 count -= 1;
493 if (devc->audio_mode & PCM_ENABLE_INPUT &&
494 audio_devs[dev]->flags & DMA_AUTOMODE &&
495 intrflag &&
496 count == devc->xfer_count) {
497 devc->audio_mode |= PCM_ENABLE_INPUT;
498 return; /*
499 * Auto DMA mode on. No need to react
503 spin_lock_irqsave(&waveartist_lock, flags);
506 * set sample count
508 waveartist_cmd2(devc, WACMD_INPUTSIZE, count);
510 devc->xfer_count = count;
511 devc->audio_mode |= PCM_ENABLE_INPUT;
513 spin_unlock_irqrestore(&waveartist_lock, flags);
516 static int
517 waveartist_ioctl(int dev, unsigned int cmd, void __user * arg)
519 return -EINVAL;
522 static unsigned int
523 waveartist_get_speed(wavnc_port_info *portc)
525 unsigned int speed;
528 * program the speed, channels, bits
530 if (portc->speed == 8000)
531 speed = 0x2E71;
532 else if (portc->speed == 11025)
533 speed = 0x4000;
534 else if (portc->speed == 22050)
535 speed = 0x8000;
536 else if (portc->speed == 44100)
537 speed = 0x0;
538 else {
540 * non-standard - just calculate
542 speed = portc->speed << 16;
544 speed = (speed / 44100) & 65535;
547 return speed;
550 static unsigned int
551 waveartist_get_bits(wavnc_port_info *portc)
553 unsigned int bits;
555 if (portc->audio_format == AFMT_S16_LE)
556 bits = 1;
557 else if (portc->audio_format == AFMT_S8)
558 bits = 0;
559 else
560 bits = 2; //default AFMT_U8
562 return bits;
565 static int
566 waveartist_prepare_for_input(int dev, int bsize, int bcount)
568 unsigned long flags;
569 wavnc_info *devc = (wavnc_info *) audio_devs[dev]->devc;
570 wavnc_port_info *portc = (wavnc_port_info *) audio_devs[dev]->portc;
571 unsigned int speed, bits;
573 if (devc->audio_mode)
574 return 0;
576 speed = waveartist_get_speed(portc);
577 bits = waveartist_get_bits(portc);
579 spin_lock_irqsave(&waveartist_lock, flags);
581 if (waveartist_cmd2(devc, WACMD_INPUTFORMAT, bits))
582 printk(KERN_WARNING "waveartist: error setting the "
583 "record format to %d\n", portc->audio_format);
585 if (waveartist_cmd2(devc, WACMD_INPUTCHANNELS, portc->channels))
586 printk(KERN_WARNING "waveartist: error setting record "
587 "to %d channels\n", portc->channels);
590 * write cmd SetSampleSpeedTimeConstant
592 if (waveartist_cmd2(devc, WACMD_INPUTSPEED, speed))
593 printk(KERN_WARNING "waveartist: error setting the record "
594 "speed to %dHz.\n", portc->speed);
596 if (waveartist_cmd2(devc, WACMD_INPUTDMA, 1))
597 printk(KERN_WARNING "waveartist: error setting the record "
598 "data path to 0x%X\n", 1);
600 if (waveartist_cmd2(devc, WACMD_INPUTFORMAT, bits))
601 printk(KERN_WARNING "waveartist: error setting the record "
602 "format to %d\n", portc->audio_format);
604 devc->xfer_count = 0;
605 spin_unlock_irqrestore(&waveartist_lock, flags);
606 waveartist_halt_input(dev);
608 if (debug_flg & DEBUG_INTR) {
609 printk("WA CTLR reg: 0x%02X.\n",
610 inb(devc->hw.io_base + CTLR));
611 printk("WA STAT reg: 0x%02X.\n",
612 inb(devc->hw.io_base + STATR));
613 printk("WA IRQS reg: 0x%02X.\n",
614 inb(devc->hw.io_base + IRQSTAT));
617 return 0;
620 static int
621 waveartist_prepare_for_output(int dev, int bsize, int bcount)
623 unsigned long flags;
624 wavnc_info *devc = (wavnc_info *) audio_devs[dev]->devc;
625 wavnc_port_info *portc = (wavnc_port_info *) audio_devs[dev]->portc;
626 unsigned int speed, bits;
629 * program the speed, channels, bits
631 speed = waveartist_get_speed(portc);
632 bits = waveartist_get_bits(portc);
634 spin_lock_irqsave(&waveartist_lock, flags);
636 if (waveartist_cmd2(devc, WACMD_OUTPUTSPEED, speed) &&
637 waveartist_cmd2(devc, WACMD_OUTPUTSPEED, speed))
638 printk(KERN_WARNING "waveartist: error setting the playback "
639 "speed to %dHz.\n", portc->speed);
641 if (waveartist_cmd2(devc, WACMD_OUTPUTCHANNELS, portc->channels))
642 printk(KERN_WARNING "waveartist: error setting the playback "
643 "to %d channels\n", portc->channels);
645 if (waveartist_cmd2(devc, WACMD_OUTPUTDMA, 0))
646 printk(KERN_WARNING "waveartist: error setting the playback "
647 "data path to 0x%X\n", 0);
649 if (waveartist_cmd2(devc, WACMD_OUTPUTFORMAT, bits))
650 printk(KERN_WARNING "waveartist: error setting the playback "
651 "format to %d\n", portc->audio_format);
653 devc->xfer_count = 0;
654 spin_unlock_irqrestore(&waveartist_lock, flags);
655 waveartist_halt_output(dev);
657 if (debug_flg & DEBUG_INTR) {
658 printk("WA CTLR reg: 0x%02X.\n",inb(devc->hw.io_base + CTLR));
659 printk("WA STAT reg: 0x%02X.\n",inb(devc->hw.io_base + STATR));
660 printk("WA IRQS reg: 0x%02X.\n",inb(devc->hw.io_base + IRQSTAT));
663 return 0;
666 static void
667 waveartist_halt(int dev)
669 wavnc_port_info *portc = (wavnc_port_info *) audio_devs[dev]->portc;
670 wavnc_info *devc;
672 if (portc->open_mode & OPEN_WRITE)
673 waveartist_halt_output(dev);
675 if (portc->open_mode & OPEN_READ)
676 waveartist_halt_input(dev);
678 devc = (wavnc_info *) audio_devs[dev]->devc;
679 devc->audio_mode = 0;
682 static void
683 waveartist_halt_input(int dev)
685 wavnc_info *devc = (wavnc_info *) audio_devs[dev]->devc;
686 unsigned long flags;
688 spin_lock_irqsave(&waveartist_lock, flags);
691 * Stop capture
693 waveartist_cmd1(devc, WACMD_INPUTSTOP);
695 devc->audio_mode &= ~PCM_ENABLE_INPUT;
698 * Clear interrupt by toggling
699 * the IRQ_ACK bit in CTRL
701 if (inb(devc->hw.io_base + STATR) & IRQ_REQ)
702 waveartist_iack(devc);
704 // devc->audio_mode &= ~PCM_ENABLE_INPUT;
706 spin_unlock_irqrestore(&waveartist_lock, flags);
709 static void
710 waveartist_halt_output(int dev)
712 wavnc_info *devc = (wavnc_info *) audio_devs[dev]->devc;
713 unsigned long flags;
715 spin_lock_irqsave(&waveartist_lock, flags);
717 waveartist_cmd1(devc, WACMD_OUTPUTSTOP);
719 devc->audio_mode &= ~PCM_ENABLE_OUTPUT;
722 * Clear interrupt by toggling
723 * the IRQ_ACK bit in CTRL
725 if (inb(devc->hw.io_base + STATR) & IRQ_REQ)
726 waveartist_iack(devc);
728 // devc->audio_mode &= ~PCM_ENABLE_OUTPUT;
730 spin_unlock_irqrestore(&waveartist_lock, flags);
733 static void
734 waveartist_trigger(int dev, int state)
736 wavnc_info *devc = (wavnc_info *) audio_devs[dev]->devc;
737 wavnc_port_info *portc = (wavnc_port_info *) audio_devs[dev]->portc;
738 unsigned long flags;
740 if (debug_flg & DEBUG_TRIGGER) {
741 printk("wavnc: audio trigger ");
742 if (state & PCM_ENABLE_INPUT)
743 printk("in ");
744 if (state & PCM_ENABLE_OUTPUT)
745 printk("out");
746 printk("\n");
749 spin_lock_irqsave(&waveartist_lock, flags);
751 state &= devc->audio_mode;
753 if (portc->open_mode & OPEN_READ &&
754 state & PCM_ENABLE_INPUT)
756 * enable ADC Data Transfer to PC
758 waveartist_cmd1(devc, WACMD_INPUTSTART);
760 if (portc->open_mode & OPEN_WRITE &&
761 state & PCM_ENABLE_OUTPUT)
763 * enable DAC data transfer from PC
765 waveartist_cmd1(devc, WACMD_OUTPUTSTART);
767 spin_unlock_irqrestore(&waveartist_lock, flags);
770 static int
771 waveartist_set_speed(int dev, int arg)
773 wavnc_port_info *portc = (wavnc_port_info *) audio_devs[dev]->portc;
775 if (arg <= 0)
776 return portc->speed;
778 if (arg < 5000)
779 arg = 5000;
780 if (arg > 44100)
781 arg = 44100;
783 portc->speed = arg;
784 return portc->speed;
788 static short
789 waveartist_set_channels(int dev, short arg)
791 wavnc_port_info *portc = (wavnc_port_info *) audio_devs[dev]->portc;
793 if (arg != 1 && arg != 2)
794 return portc->channels;
796 portc->channels = arg;
797 return arg;
800 static unsigned int
801 waveartist_set_bits(int dev, unsigned int arg)
803 wavnc_port_info *portc = (wavnc_port_info *) audio_devs[dev]->portc;
805 if (arg == 0)
806 return portc->audio_format;
808 if ((arg != AFMT_U8) && (arg != AFMT_S16_LE) && (arg != AFMT_S8))
809 arg = AFMT_U8;
811 portc->audio_format = arg;
813 return arg;
816 static struct audio_driver waveartist_audio_driver = {
817 .owner = THIS_MODULE,
818 .open = waveartist_open,
819 .close = waveartist_close,
820 .output_block = waveartist_output_block,
821 .start_input = waveartist_start_input,
822 .ioctl = waveartist_ioctl,
823 .prepare_for_input = waveartist_prepare_for_input,
824 .prepare_for_output = waveartist_prepare_for_output,
825 .halt_io = waveartist_halt,
826 .halt_input = waveartist_halt_input,
827 .halt_output = waveartist_halt_output,
828 .trigger = waveartist_trigger,
829 .set_speed = waveartist_set_speed,
830 .set_bits = waveartist_set_bits,
831 .set_channels = waveartist_set_channels
835 static irqreturn_t
836 waveartist_intr(int irq, void *dev_id)
838 wavnc_info *devc = (wavnc_info *)dev_id;
839 int irqstatus, status;
841 spin_lock(&waveartist_lock);
842 irqstatus = inb(devc->hw.io_base + IRQSTAT);
843 status = inb(devc->hw.io_base + STATR);
845 if (debug_flg & DEBUG_INTR)
846 printk("waveartist_intr: stat=%02x, irqstat=%02x\n",
847 status, irqstatus);
849 if (status & IRQ_REQ) /* Clear interrupt */
850 waveartist_iack(devc);
851 else
852 printk(KERN_WARNING "waveartist: unexpected interrupt\n");
854 if (irqstatus & 0x01) {
855 int temp = 1;
857 /* PCM buffer done
859 if ((status & DMA0) && (devc->audio_mode & PCM_ENABLE_OUTPUT)) {
860 DMAbuf_outputintr(devc->playback_dev, 1);
861 temp = 0;
863 if ((status & DMA1) && (devc->audio_mode & PCM_ENABLE_INPUT)) {
864 DMAbuf_inputintr(devc->record_dev);
865 temp = 0;
867 if (temp) //default:
868 printk(KERN_WARNING "waveartist: Unknown interrupt\n");
870 if (irqstatus & 0x2)
871 // We do not use SB mode natively...
872 printk(KERN_WARNING "waveartist: Unexpected SB interrupt...\n");
873 spin_unlock(&waveartist_lock);
874 return IRQ_HANDLED;
877 /* -------------------------------------------------------------------------
878 * Mixer stuff
880 struct mix_ent {
881 unsigned char reg_l;
882 unsigned char reg_r;
883 unsigned char shift;
884 unsigned char max;
887 static const struct mix_ent mix_devs[SOUND_MIXER_NRDEVICES] = {
888 { 2, 6, 1, 7 }, /* SOUND_MIXER_VOLUME */
889 { 0, 0, 0, 0 }, /* SOUND_MIXER_BASS */
890 { 0, 0, 0, 0 }, /* SOUND_MIXER_TREBLE */
891 { 0, 0, 0, 0 }, /* SOUND_MIXER_SYNTH */
892 { 0, 0, 0, 0 }, /* SOUND_MIXER_PCM */
893 { 0, 0, 0, 0 }, /* SOUND_MIXER_SPEAKER */
894 { 0, 4, 6, 31 }, /* SOUND_MIXER_LINE */
895 { 2, 6, 4, 3 }, /* SOUND_MIXER_MIC */
896 { 0, 0, 0, 0 }, /* SOUND_MIXER_CD */
897 { 0, 0, 0, 0 }, /* SOUND_MIXER_IMIX */
898 { 0, 0, 0, 0 }, /* SOUND_MIXER_ALTPCM */
899 #if 0
900 { 3, 7, 0, 10 }, /* SOUND_MIXER_RECLEV */
901 { 0, 0, 0, 0 }, /* SOUND_MIXER_IGAIN */
902 #else
903 { 0, 0, 0, 0 }, /* SOUND_MIXER_RECLEV */
904 { 3, 7, 0, 7 }, /* SOUND_MIXER_IGAIN */
905 #endif
906 { 0, 0, 0, 0 }, /* SOUND_MIXER_OGAIN */
907 { 0, 4, 1, 31 }, /* SOUND_MIXER_LINE1 */
908 { 1, 5, 6, 31 }, /* SOUND_MIXER_LINE2 */
909 { 0, 0, 0, 0 }, /* SOUND_MIXER_LINE3 */
910 { 0, 0, 0, 0 }, /* SOUND_MIXER_DIGITAL1 */
911 { 0, 0, 0, 0 }, /* SOUND_MIXER_DIGITAL2 */
912 { 0, 0, 0, 0 }, /* SOUND_MIXER_DIGITAL3 */
913 { 0, 0, 0, 0 }, /* SOUND_MIXER_PHONEIN */
914 { 0, 0, 0, 0 }, /* SOUND_MIXER_PHONEOUT */
915 { 0, 0, 0, 0 }, /* SOUND_MIXER_VIDEO */
916 { 0, 0, 0, 0 }, /* SOUND_MIXER_RADIO */
917 { 0, 0, 0, 0 } /* SOUND_MIXER_MONITOR */
920 static void
921 waveartist_mixer_update(wavnc_info *devc, int whichDev)
923 unsigned int lev_left, lev_right;
925 lev_left = devc->levels[whichDev] & 0xff;
926 lev_right = devc->levels[whichDev] >> 8;
928 if (lev_left > 100)
929 lev_left = 100;
930 if (lev_right > 100)
931 lev_right = 100;
933 #define SCALE(lev,max) ((lev) * (max) / 100)
935 if (machine_is_netwinder() && whichDev == SOUND_MIXER_PHONEOUT)
936 whichDev = SOUND_MIXER_VOLUME;
938 if (mix_devs[whichDev].reg_l || mix_devs[whichDev].reg_r) {
939 const struct mix_ent *mix = mix_devs + whichDev;
940 unsigned int mask, left, right;
942 mask = mix->max << mix->shift;
943 lev_left = SCALE(lev_left, mix->max) << mix->shift;
944 lev_right = SCALE(lev_right, mix->max) << mix->shift;
946 /* read left setting */
947 left = waveartist_cmd1_r(devc, WACMD_GET_LEVEL |
948 mix->reg_l << 8);
950 /* read right setting */
951 right = waveartist_cmd1_r(devc, WACMD_GET_LEVEL |
952 mix->reg_r << 8);
954 left = (left & ~mask) | (lev_left & mask);
955 right = (right & ~mask) | (lev_right & mask);
957 /* write left,right back */
958 waveartist_cmd3(devc, WACMD_SET_MIXER, left, right);
959 } else {
960 switch(whichDev) {
961 case SOUND_MIXER_PCM:
962 waveartist_cmd3(devc, WACMD_SET_LEVEL,
963 SCALE(lev_left, 32767),
964 SCALE(lev_right, 32767));
965 break;
967 case SOUND_MIXER_SYNTH:
968 waveartist_cmd3(devc, 0x0100 | WACMD_SET_LEVEL,
969 SCALE(lev_left, 32767),
970 SCALE(lev_right, 32767));
971 break;
977 * Set the ADC MUX to the specified values. We do NOT do any
978 * checking of the values passed, since we assume that the
979 * relevant *_select_input function has done that for us.
981 static void
982 waveartist_set_adc_mux(wavnc_info *devc, char left_dev, char right_dev)
984 unsigned int reg_08, reg_09;
986 reg_08 = waveartist_cmd1_r(devc, WACMD_GET_LEVEL | 0x0800);
987 reg_09 = waveartist_cmd1_r(devc, WACMD_GET_LEVEL | 0x0900);
989 reg_08 = (reg_08 & ~0x3f) | right_dev << 3 | left_dev;
991 waveartist_cmd3(devc, WACMD_SET_MIXER, reg_08, reg_09);
995 * Decode a recording mask into a mixer selection as follows:
997 * OSS Source WA Source Actual source
998 * SOUND_MASK_IMIX Mixer Mixer output (same as AD1848)
999 * SOUND_MASK_LINE Line Line in
1000 * SOUND_MASK_LINE1 Aux 1 Aux 1 in
1001 * SOUND_MASK_LINE2 Aux 2 Aux 2 in
1002 * SOUND_MASK_MIC Mic Microphone
1004 static unsigned int
1005 waveartist_select_input(wavnc_info *devc, unsigned int recmask,
1006 unsigned char *dev_l, unsigned char *dev_r)
1008 unsigned int recdev = ADC_MUX_NONE;
1010 if (recmask & SOUND_MASK_IMIX) {
1011 recmask = SOUND_MASK_IMIX;
1012 recdev = ADC_MUX_MIXER;
1013 } else if (recmask & SOUND_MASK_LINE2) {
1014 recmask = SOUND_MASK_LINE2;
1015 recdev = ADC_MUX_AUX2;
1016 } else if (recmask & SOUND_MASK_LINE1) {
1017 recmask = SOUND_MASK_LINE1;
1018 recdev = ADC_MUX_AUX1;
1019 } else if (recmask & SOUND_MASK_LINE) {
1020 recmask = SOUND_MASK_LINE;
1021 recdev = ADC_MUX_LINE;
1022 } else if (recmask & SOUND_MASK_MIC) {
1023 recmask = SOUND_MASK_MIC;
1024 recdev = ADC_MUX_MIC;
1027 *dev_l = *dev_r = recdev;
1029 return recmask;
1032 static int
1033 waveartist_decode_mixer(wavnc_info *devc, int dev, unsigned char lev_l,
1034 unsigned char lev_r)
1036 switch (dev) {
1037 case SOUND_MIXER_VOLUME:
1038 case SOUND_MIXER_SYNTH:
1039 case SOUND_MIXER_PCM:
1040 case SOUND_MIXER_LINE:
1041 case SOUND_MIXER_MIC:
1042 case SOUND_MIXER_IGAIN:
1043 case SOUND_MIXER_LINE1:
1044 case SOUND_MIXER_LINE2:
1045 devc->levels[dev] = lev_l | lev_r << 8;
1046 break;
1048 case SOUND_MIXER_IMIX:
1049 break;
1051 default:
1052 dev = -EINVAL;
1053 break;
1056 return dev;
1059 static int waveartist_get_mixer(wavnc_info *devc, int dev)
1061 return devc->levels[dev];
1064 static const struct waveartist_mixer_info waveartist_mixer = {
1065 .supported_devs = SUPPORTED_MIXER_DEVICES | SOUND_MASK_IGAIN,
1066 .recording_devs = SOUND_MASK_LINE | SOUND_MASK_MIC |
1067 SOUND_MASK_LINE1 | SOUND_MASK_LINE2 |
1068 SOUND_MASK_IMIX,
1069 .stereo_devs = (SUPPORTED_MIXER_DEVICES | SOUND_MASK_IGAIN) & ~
1070 (SOUND_MASK_SPEAKER | SOUND_MASK_IMIX),
1071 .select_input = waveartist_select_input,
1072 .decode_mixer = waveartist_decode_mixer,
1073 .get_mixer = waveartist_get_mixer,
1076 static void
1077 waveartist_set_recmask(wavnc_info *devc, unsigned int recmask)
1079 unsigned char dev_l, dev_r;
1081 recmask &= devc->mix->recording_devs;
1084 * If more than one recording device selected,
1085 * disable the device that is currently in use.
1087 if (hweight32(recmask) > 1)
1088 recmask &= ~devc->recmask;
1091 * Translate the recording device mask into
1092 * the ADC multiplexer settings.
1094 devc->recmask = devc->mix->select_input(devc, recmask,
1095 &dev_l, &dev_r);
1097 waveartist_set_adc_mux(devc, dev_l, dev_r);
1100 static int
1101 waveartist_set_mixer(wavnc_info *devc, int dev, unsigned int level)
1103 unsigned int lev_left = level & 0x00ff;
1104 unsigned int lev_right = (level & 0xff00) >> 8;
1106 if (lev_left > 100)
1107 lev_left = 100;
1108 if (lev_right > 100)
1109 lev_right = 100;
1112 * Mono devices have their right volume forced to their
1113 * left volume. (from ALSA driver OSS emulation).
1115 if (!(devc->mix->stereo_devs & (1 << dev)))
1116 lev_right = lev_left;
1118 dev = devc->mix->decode_mixer(devc, dev, lev_left, lev_right);
1120 if (dev >= 0)
1121 waveartist_mixer_update(devc, dev);
1123 return dev < 0 ? dev : 0;
1126 static int
1127 waveartist_mixer_ioctl(int dev, unsigned int cmd, void __user * arg)
1129 wavnc_info *devc = (wavnc_info *)audio_devs[dev]->devc;
1130 int ret = 0, val, nr;
1133 * All SOUND_MIXER_* ioctls use type 'M'
1135 if (((cmd >> 8) & 255) != 'M')
1136 return -ENOIOCTLCMD;
1138 #ifdef CONFIG_ARCH_NETWINDER
1139 if (machine_is_netwinder()) {
1140 ret = vnc_private_ioctl(dev, cmd, arg);
1141 if (ret != -ENOIOCTLCMD)
1142 return ret;
1143 else
1144 ret = 0;
1146 #endif
1148 nr = cmd & 0xff;
1150 if (_SIOC_DIR(cmd) & _SIOC_WRITE) {
1151 if (get_user(val, (int __user *)arg))
1152 return -EFAULT;
1154 switch (nr) {
1155 case SOUND_MIXER_RECSRC:
1156 waveartist_set_recmask(devc, val);
1157 break;
1159 default:
1160 ret = -EINVAL;
1161 if (nr < SOUND_MIXER_NRDEVICES &&
1162 devc->mix->supported_devs & (1 << nr))
1163 ret = waveartist_set_mixer(devc, nr, val);
1167 if (ret == 0 && _SIOC_DIR(cmd) & _SIOC_READ) {
1168 ret = -EINVAL;
1170 switch (nr) {
1171 case SOUND_MIXER_RECSRC:
1172 ret = devc->recmask;
1173 break;
1175 case SOUND_MIXER_DEVMASK:
1176 ret = devc->mix->supported_devs;
1177 break;
1179 case SOUND_MIXER_STEREODEVS:
1180 ret = devc->mix->stereo_devs;
1181 break;
1183 case SOUND_MIXER_RECMASK:
1184 ret = devc->mix->recording_devs;
1185 break;
1187 case SOUND_MIXER_CAPS:
1188 ret = SOUND_CAP_EXCL_INPUT;
1189 break;
1191 default:
1192 if (nr < SOUND_MIXER_NRDEVICES)
1193 ret = devc->mix->get_mixer(devc, nr);
1194 break;
1197 if (ret >= 0)
1198 ret = put_user(ret, (int __user *)arg) ? -EFAULT : 0;
1201 return ret;
1204 static struct mixer_operations waveartist_mixer_operations =
1206 .owner = THIS_MODULE,
1207 .id = "WaveArtist",
1208 .name = "WaveArtist",
1209 .ioctl = waveartist_mixer_ioctl
1212 static void
1213 waveartist_mixer_reset(wavnc_info *devc)
1215 int i;
1217 if (debug_flg & DEBUG_MIXER)
1218 printk("%s: mixer_reset\n", devc->hw.name);
1221 * reset mixer cmd
1223 waveartist_cmd1(devc, WACMD_RST_MIXER);
1226 * set input for ADC to come from 'quiet'
1227 * turn on default modes
1229 waveartist_cmd3(devc, WACMD_SET_MIXER, 0x9800, 0xa836);
1232 * set mixer input select to none, RX filter gains 0 dB
1234 waveartist_cmd3(devc, WACMD_SET_MIXER, 0x4c00, 0x8c00);
1237 * set bit 0 reg 2 to 1 - unmute MonoOut
1239 waveartist_cmd3(devc, WACMD_SET_MIXER, 0x2801, 0x6800);
1241 /* set default input device = internal mic
1242 * current recording device = none
1244 waveartist_set_recmask(devc, 0);
1246 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
1247 waveartist_mixer_update(devc, i);
1250 static int __init waveartist_init(wavnc_info *devc)
1252 wavnc_port_info *portc;
1253 char rev[3], dev_name[64];
1254 int my_dev;
1256 if (waveartist_reset(devc))
1257 return -ENODEV;
1259 sprintf(dev_name, "%s (%s", devc->hw.name, devc->chip_name);
1261 if (waveartist_getrev(devc, rev)) {
1262 strcat(dev_name, " rev. ");
1263 strcat(dev_name, rev);
1265 strcat(dev_name, ")");
1267 conf_printf2(dev_name, devc->hw.io_base, devc->hw.irq,
1268 devc->hw.dma, devc->hw.dma2);
1270 portc = kzalloc(sizeof(wavnc_port_info), GFP_KERNEL);
1271 if (portc == NULL)
1272 goto nomem;
1274 my_dev = sound_install_audiodrv(AUDIO_DRIVER_VERSION, dev_name,
1275 &waveartist_audio_driver, sizeof(struct audio_driver),
1276 devc->audio_flags, AFMT_U8 | AFMT_S16_LE | AFMT_S8,
1277 devc, devc->hw.dma, devc->hw.dma2);
1279 if (my_dev < 0)
1280 goto free;
1282 audio_devs[my_dev]->portc = portc;
1284 waveartist_mixer_reset(devc);
1287 * clear any pending interrupt
1289 waveartist_iack(devc);
1291 if (request_irq(devc->hw.irq, waveartist_intr, 0, devc->hw.name, devc) < 0) {
1292 printk(KERN_ERR "%s: IRQ %d in use\n",
1293 devc->hw.name, devc->hw.irq);
1294 goto uninstall;
1297 if (sound_alloc_dma(devc->hw.dma, devc->hw.name)) {
1298 printk(KERN_ERR "%s: Can't allocate DMA%d\n",
1299 devc->hw.name, devc->hw.dma);
1300 goto uninstall_irq;
1303 if (devc->hw.dma != devc->hw.dma2 && devc->hw.dma2 != NO_DMA)
1304 if (sound_alloc_dma(devc->hw.dma2, devc->hw.name)) {
1305 printk(KERN_ERR "%s: can't allocate DMA%d\n",
1306 devc->hw.name, devc->hw.dma2);
1307 goto uninstall_dma;
1310 waveartist_set_ctlr(&devc->hw, 0, DMA1_IE | DMA0_IE);
1312 audio_devs[my_dev]->mixer_dev =
1313 sound_install_mixer(MIXER_DRIVER_VERSION,
1314 dev_name,
1315 &waveartist_mixer_operations,
1316 sizeof(struct mixer_operations),
1317 devc);
1319 return my_dev;
1321 uninstall_dma:
1322 sound_free_dma(devc->hw.dma);
1324 uninstall_irq:
1325 free_irq(devc->hw.irq, devc);
1327 uninstall:
1328 sound_unload_audiodev(my_dev);
1330 free:
1331 kfree(portc);
1333 nomem:
1334 return -1;
1337 static int __init probe_waveartist(struct address_info *hw_config)
1339 wavnc_info *devc = &adev_info[nr_waveartist_devs];
1341 if (nr_waveartist_devs >= MAX_AUDIO_DEV) {
1342 printk(KERN_WARNING "waveartist: too many audio devices\n");
1343 return 0;
1346 if (!request_region(hw_config->io_base, 15, hw_config->name)) {
1347 printk(KERN_WARNING "WaveArtist: I/O port conflict\n");
1348 return 0;
1351 if (hw_config->irq > 15 || hw_config->irq < 0) {
1352 release_region(hw_config->io_base, 15);
1353 printk(KERN_WARNING "WaveArtist: Bad IRQ %d\n",
1354 hw_config->irq);
1355 return 0;
1358 if (hw_config->dma != 3) {
1359 release_region(hw_config->io_base, 15);
1360 printk(KERN_WARNING "WaveArtist: Bad DMA %d\n",
1361 hw_config->dma);
1362 return 0;
1365 hw_config->name = "WaveArtist";
1366 devc->hw = *hw_config;
1367 devc->open_mode = 0;
1368 devc->chip_name = "RWA-010";
1370 return 1;
1373 static void __init
1374 attach_waveartist(struct address_info *hw, const struct waveartist_mixer_info *mix)
1376 wavnc_info *devc = &adev_info[nr_waveartist_devs];
1379 * NOTE! If irq < 0, there is another driver which has allocated the
1380 * IRQ so that this driver doesn't need to allocate/deallocate it.
1381 * The actually used IRQ is ABS(irq).
1383 devc->hw = *hw;
1384 devc->hw.irq = (hw->irq > 0) ? hw->irq : 0;
1385 devc->open_mode = 0;
1386 devc->playback_dev = 0;
1387 devc->record_dev = 0;
1388 devc->audio_flags = DMA_AUTOMODE;
1389 devc->levels = levels;
1391 if (hw->dma != hw->dma2 && hw->dma2 != NO_DMA)
1392 devc->audio_flags |= DMA_DUPLEX;
1394 devc->mix = mix;
1395 devc->dev_no = waveartist_init(devc);
1397 if (devc->dev_no < 0)
1398 release_region(hw->io_base, 15);
1399 else {
1400 #ifdef CONFIG_ARCH_NETWINDER
1401 if (machine_is_netwinder()) {
1402 init_timer(&vnc_timer);
1403 vnc_timer.function = vnc_slider_tick;
1404 vnc_timer.expires = jiffies;
1405 vnc_timer.data = nr_waveartist_devs;
1406 add_timer(&vnc_timer);
1408 vnc_configure_mixer(devc, 0);
1410 devc->no_autoselect = 1;
1412 #endif
1413 nr_waveartist_devs += 1;
1417 static void __exit unload_waveartist(struct address_info *hw)
1419 wavnc_info *devc = NULL;
1420 int i;
1422 for (i = 0; i < nr_waveartist_devs; i++)
1423 if (hw->io_base == adev_info[i].hw.io_base) {
1424 devc = adev_info + i;
1425 break;
1428 if (devc != NULL) {
1429 int mixer;
1431 #ifdef CONFIG_ARCH_NETWINDER
1432 if (machine_is_netwinder())
1433 del_timer(&vnc_timer);
1434 #endif
1436 release_region(devc->hw.io_base, 15);
1438 waveartist_set_ctlr(&devc->hw, DMA1_IE|DMA0_IE, 0);
1440 if (devc->hw.irq >= 0)
1441 free_irq(devc->hw.irq, devc);
1443 sound_free_dma(devc->hw.dma);
1445 if (devc->hw.dma != devc->hw.dma2 &&
1446 devc->hw.dma2 != NO_DMA)
1447 sound_free_dma(devc->hw.dma2);
1449 mixer = audio_devs[devc->dev_no]->mixer_dev;
1451 if (mixer >= 0)
1452 sound_unload_mixerdev(mixer);
1454 if (devc->dev_no >= 0)
1455 sound_unload_audiodev(devc->dev_no);
1457 nr_waveartist_devs -= 1;
1459 for (; i < nr_waveartist_devs; i++)
1460 adev_info[i] = adev_info[i + 1];
1461 } else
1462 printk(KERN_WARNING "waveartist: can't find device "
1463 "to unload\n");
1466 #ifdef CONFIG_ARCH_NETWINDER
1469 * Rebel.com Netwinder specifics...
1472 #include <asm/hardware/dec21285.h>
1474 #define VNC_TIMER_PERIOD (HZ/4) //check slider 4 times/sec
1476 #define MIXER_PRIVATE3_RESET 0x53570000
1477 #define MIXER_PRIVATE3_READ 0x53570001
1478 #define MIXER_PRIVATE3_WRITE 0x53570002
1480 #define VNC_MUTE_INTERNAL_SPKR 0x01 //the sw mute on/off control bit
1481 #define VNC_MUTE_LINE_OUT 0x10
1482 #define VNC_PHONE_DETECT 0x20
1483 #define VNC_HANDSET_DETECT 0x40
1484 #define VNC_DISABLE_AUTOSWITCH 0x80
1486 extern spinlock_t gpio_lock;
1488 static inline void
1489 vnc_mute_spkr(wavnc_info *devc)
1491 unsigned long flags;
1493 spin_lock_irqsave(&gpio_lock, flags);
1494 cpld_modify(CPLD_UNMUTE, devc->spkr_mute_state ? 0 : CPLD_UNMUTE);
1495 spin_unlock_irqrestore(&gpio_lock, flags);
1498 static void
1499 vnc_mute_lout(wavnc_info *devc)
1501 unsigned int left, right;
1503 left = waveartist_cmd1_r(devc, WACMD_GET_LEVEL);
1504 right = waveartist_cmd1_r(devc, WACMD_GET_LEVEL | 0x400);
1506 if (devc->line_mute_state) {
1507 left &= ~1;
1508 right &= ~1;
1509 } else {
1510 left |= 1;
1511 right |= 1;
1513 waveartist_cmd3(devc, WACMD_SET_MIXER, left, right);
1517 static int
1518 vnc_volume_slider(wavnc_info *devc)
1520 static signed int old_slider_volume;
1521 unsigned long flags;
1522 signed int volume = 255;
1524 *CSR_TIMER1_LOAD = 0x00ffffff;
1526 spin_lock_irqsave(&waveartist_lock, flags);
1528 outb(0xFF, 0x201);
1529 *CSR_TIMER1_CNTL = TIMER_CNTL_ENABLE | TIMER_CNTL_DIV1;
1531 while (volume && (inb(0x201) & 0x01))
1532 volume--;
1534 *CSR_TIMER1_CNTL = 0;
1536 spin_unlock_irqrestore(&waveartist_lock,flags);
1538 volume = 0x00ffffff - *CSR_TIMER1_VALUE;
1541 #ifndef REVERSE
1542 volume = 150 - (volume >> 5);
1543 #else
1544 volume = (volume >> 6) - 25;
1545 #endif
1547 if (volume < 0)
1548 volume = 0;
1550 if (volume > 100)
1551 volume = 100;
1554 * slider quite often reads +-8, so debounce this random noise
1556 if (abs(volume - old_slider_volume) > 7) {
1557 old_slider_volume = volume;
1559 if (debug_flg & DEBUG_MIXER)
1560 printk(KERN_DEBUG "Slider volume: %d.\n", volume);
1563 return old_slider_volume;
1567 * Decode a recording mask into a mixer selection on the NetWinder
1568 * as follows:
1570 * OSS Source WA Source Actual source
1571 * SOUND_MASK_IMIX Mixer Mixer output (same as AD1848)
1572 * SOUND_MASK_LINE Line Line in
1573 * SOUND_MASK_LINE1 Left Mic Handset
1574 * SOUND_MASK_PHONEIN Left Aux Telephone microphone
1575 * SOUND_MASK_MIC Right Mic Builtin microphone
1577 static unsigned int
1578 netwinder_select_input(wavnc_info *devc, unsigned int recmask,
1579 unsigned char *dev_l, unsigned char *dev_r)
1581 unsigned int recdev_l = ADC_MUX_NONE, recdev_r = ADC_MUX_NONE;
1583 if (recmask & SOUND_MASK_IMIX) {
1584 recmask = SOUND_MASK_IMIX;
1585 recdev_l = ADC_MUX_MIXER;
1586 recdev_r = ADC_MUX_MIXER;
1587 } else if (recmask & SOUND_MASK_LINE) {
1588 recmask = SOUND_MASK_LINE;
1589 recdev_l = ADC_MUX_LINE;
1590 recdev_r = ADC_MUX_LINE;
1591 } else if (recmask & SOUND_MASK_LINE1) {
1592 recmask = SOUND_MASK_LINE1;
1593 waveartist_cmd1(devc, WACMD_SET_MONO); /* left */
1594 recdev_l = ADC_MUX_MIC;
1595 recdev_r = ADC_MUX_NONE;
1596 } else if (recmask & SOUND_MASK_PHONEIN) {
1597 recmask = SOUND_MASK_PHONEIN;
1598 waveartist_cmd1(devc, WACMD_SET_MONO); /* left */
1599 recdev_l = ADC_MUX_AUX1;
1600 recdev_r = ADC_MUX_NONE;
1601 } else if (recmask & SOUND_MASK_MIC) {
1602 recmask = SOUND_MASK_MIC;
1603 waveartist_cmd1(devc, WACMD_SET_MONO | 0x100); /* right */
1604 recdev_l = ADC_MUX_NONE;
1605 recdev_r = ADC_MUX_MIC;
1608 *dev_l = recdev_l;
1609 *dev_r = recdev_r;
1611 return recmask;
1614 static int
1615 netwinder_decode_mixer(wavnc_info *devc, int dev, unsigned char lev_l,
1616 unsigned char lev_r)
1618 switch (dev) {
1619 case SOUND_MIXER_VOLUME:
1620 case SOUND_MIXER_SYNTH:
1621 case SOUND_MIXER_PCM:
1622 case SOUND_MIXER_LINE:
1623 case SOUND_MIXER_IGAIN:
1624 devc->levels[dev] = lev_l | lev_r << 8;
1625 break;
1627 case SOUND_MIXER_MIC: /* right mic only */
1628 devc->levels[SOUND_MIXER_MIC] &= 0xff;
1629 devc->levels[SOUND_MIXER_MIC] |= lev_l << 8;
1630 break;
1632 case SOUND_MIXER_LINE1: /* left mic only */
1633 devc->levels[SOUND_MIXER_MIC] &= 0xff00;
1634 devc->levels[SOUND_MIXER_MIC] |= lev_l;
1635 dev = SOUND_MIXER_MIC;
1636 break;
1638 case SOUND_MIXER_PHONEIN: /* left aux only */
1639 devc->levels[SOUND_MIXER_LINE1] = lev_l;
1640 dev = SOUND_MIXER_LINE1;
1641 break;
1643 case SOUND_MIXER_IMIX:
1644 case SOUND_MIXER_PHONEOUT:
1645 break;
1647 default:
1648 dev = -EINVAL;
1649 break;
1651 return dev;
1654 static int netwinder_get_mixer(wavnc_info *devc, int dev)
1656 int levels;
1658 switch (dev) {
1659 case SOUND_MIXER_VOLUME:
1660 case SOUND_MIXER_SYNTH:
1661 case SOUND_MIXER_PCM:
1662 case SOUND_MIXER_LINE:
1663 case SOUND_MIXER_IGAIN:
1664 levels = devc->levels[dev];
1665 break;
1667 case SOUND_MIXER_MIC: /* builtin mic: right mic only */
1668 levels = devc->levels[SOUND_MIXER_MIC] >> 8;
1669 levels |= levels << 8;
1670 break;
1672 case SOUND_MIXER_LINE1: /* handset mic: left mic only */
1673 levels = devc->levels[SOUND_MIXER_MIC] & 0xff;
1674 levels |= levels << 8;
1675 break;
1677 case SOUND_MIXER_PHONEIN: /* phone mic: left aux1 only */
1678 levels = devc->levels[SOUND_MIXER_LINE1] & 0xff;
1679 levels |= levels << 8;
1680 break;
1682 default:
1683 levels = 0;
1686 return levels;
1690 * Waveartist specific mixer information.
1692 static const struct waveartist_mixer_info netwinder_mixer = {
1693 .supported_devs = SOUND_MASK_VOLUME | SOUND_MASK_SYNTH |
1694 SOUND_MASK_PCM | SOUND_MASK_SPEAKER |
1695 SOUND_MASK_LINE | SOUND_MASK_MIC |
1696 SOUND_MASK_IMIX | SOUND_MASK_LINE1 |
1697 SOUND_MASK_PHONEIN | SOUND_MASK_PHONEOUT|
1698 SOUND_MASK_IGAIN,
1700 .recording_devs = SOUND_MASK_LINE | SOUND_MASK_MIC |
1701 SOUND_MASK_IMIX | SOUND_MASK_LINE1 |
1702 SOUND_MASK_PHONEIN,
1704 .stereo_devs = SOUND_MASK_VOLUME | SOUND_MASK_SYNTH |
1705 SOUND_MASK_PCM | SOUND_MASK_LINE |
1706 SOUND_MASK_IMIX | SOUND_MASK_IGAIN,
1708 .select_input = netwinder_select_input,
1709 .decode_mixer = netwinder_decode_mixer,
1710 .get_mixer = netwinder_get_mixer,
1713 static void
1714 vnc_configure_mixer(wavnc_info *devc, unsigned int recmask)
1716 if (!devc->no_autoselect) {
1717 if (devc->handset_detect) {
1718 recmask = SOUND_MASK_LINE1;
1719 devc->spkr_mute_state = devc->line_mute_state = 1;
1720 } else if (devc->telephone_detect) {
1721 recmask = SOUND_MASK_PHONEIN;
1722 devc->spkr_mute_state = devc->line_mute_state = 1;
1723 } else {
1724 /* unless someone has asked for LINE-IN,
1725 * we default to MIC
1727 if ((devc->recmask & SOUND_MASK_LINE) == 0)
1728 devc->recmask = SOUND_MASK_MIC;
1729 devc->spkr_mute_state = devc->line_mute_state = 0;
1731 vnc_mute_spkr(devc);
1732 vnc_mute_lout(devc);
1734 if (recmask != devc->recmask)
1735 waveartist_set_recmask(devc, recmask);
1739 static int
1740 vnc_slider(wavnc_info *devc)
1742 signed int slider_volume;
1743 unsigned int temp, old_hs, old_td;
1746 * read the "buttons" state.
1747 * Bit 4 = 0 means handset present
1748 * Bit 5 = 1 means phone offhook
1750 temp = inb(0x201);
1752 old_hs = devc->handset_detect;
1753 old_td = devc->telephone_detect;
1755 devc->handset_detect = !(temp & 0x10);
1756 devc->telephone_detect = !!(temp & 0x20);
1758 if (!devc->no_autoselect &&
1759 (old_hs != devc->handset_detect ||
1760 old_td != devc->telephone_detect))
1761 vnc_configure_mixer(devc, devc->recmask);
1763 slider_volume = vnc_volume_slider(devc);
1766 * If we're using software controlled volume, and
1767 * the slider moves by more than 20%, then we
1768 * switch back to slider controlled volume.
1770 if (abs(devc->slider_vol - slider_volume) > 20)
1771 devc->use_slider = 1;
1774 * use only left channel
1776 temp = levels[SOUND_MIXER_VOLUME] & 0xFF;
1778 if (slider_volume != temp && devc->use_slider) {
1779 devc->slider_vol = slider_volume;
1781 waveartist_set_mixer(devc, SOUND_MIXER_VOLUME,
1782 slider_volume | slider_volume << 8);
1784 return 1;
1787 return 0;
1790 static void
1791 vnc_slider_tick(unsigned long data)
1793 int next_timeout;
1795 if (vnc_slider(adev_info + data))
1796 next_timeout = 5; // mixer reported change
1797 else
1798 next_timeout = VNC_TIMER_PERIOD;
1800 mod_timer(&vnc_timer, jiffies + next_timeout);
1803 static int
1804 vnc_private_ioctl(int dev, unsigned int cmd, int __user * arg)
1806 wavnc_info *devc = (wavnc_info *)audio_devs[dev]->devc;
1807 int val;
1809 switch (cmd) {
1810 case SOUND_MIXER_PRIVATE1:
1812 u_int prev_spkr_mute, prev_line_mute, prev_auto_state;
1813 int val;
1815 if (get_user(val, arg))
1816 return -EFAULT;
1818 /* check if parameter is logical */
1819 if (val & ~(VNC_MUTE_INTERNAL_SPKR |
1820 VNC_MUTE_LINE_OUT |
1821 VNC_DISABLE_AUTOSWITCH))
1822 return -EINVAL;
1824 prev_auto_state = devc->no_autoselect;
1825 prev_spkr_mute = devc->spkr_mute_state;
1826 prev_line_mute = devc->line_mute_state;
1828 devc->no_autoselect = (val & VNC_DISABLE_AUTOSWITCH) ? 1 : 0;
1829 devc->spkr_mute_state = (val & VNC_MUTE_INTERNAL_SPKR) ? 1 : 0;
1830 devc->line_mute_state = (val & VNC_MUTE_LINE_OUT) ? 1 : 0;
1832 if (prev_spkr_mute != devc->spkr_mute_state)
1833 vnc_mute_spkr(devc);
1835 if (prev_line_mute != devc->line_mute_state)
1836 vnc_mute_lout(devc);
1838 if (prev_auto_state != devc->no_autoselect)
1839 vnc_configure_mixer(devc, devc->recmask);
1841 return 0;
1844 case SOUND_MIXER_PRIVATE2:
1845 if (get_user(val, arg))
1846 return -EFAULT;
1848 switch (val) {
1849 #define VNC_SOUND_PAUSE 0x53 //to pause the DSP
1850 #define VNC_SOUND_RESUME 0x57 //to unpause the DSP
1851 case VNC_SOUND_PAUSE:
1852 waveartist_cmd1(devc, 0x16);
1853 break;
1855 case VNC_SOUND_RESUME:
1856 waveartist_cmd1(devc, 0x18);
1857 break;
1859 default:
1860 return -EINVAL;
1862 return 0;
1864 /* private ioctl to allow bulk access to waveartist */
1865 case SOUND_MIXER_PRIVATE3:
1867 unsigned long flags;
1868 int mixer_reg[15], i, val;
1870 if (get_user(val, arg))
1871 return -EFAULT;
1872 if (copy_from_user(mixer_reg, (void *)val, sizeof(mixer_reg)))
1873 return -EFAULT;
1875 switch (mixer_reg[14]) {
1876 case MIXER_PRIVATE3_RESET:
1877 waveartist_mixer_reset(devc);
1878 break;
1880 case MIXER_PRIVATE3_WRITE:
1881 waveartist_cmd3(devc, WACMD_SET_MIXER, mixer_reg[0], mixer_reg[4]);
1882 waveartist_cmd3(devc, WACMD_SET_MIXER, mixer_reg[1], mixer_reg[5]);
1883 waveartist_cmd3(devc, WACMD_SET_MIXER, mixer_reg[2], mixer_reg[6]);
1884 waveartist_cmd3(devc, WACMD_SET_MIXER, mixer_reg[3], mixer_reg[7]);
1885 waveartist_cmd3(devc, WACMD_SET_MIXER, mixer_reg[8], mixer_reg[9]);
1887 waveartist_cmd3(devc, WACMD_SET_LEVEL, mixer_reg[10], mixer_reg[11]);
1888 waveartist_cmd3(devc, WACMD_SET_LEVEL, mixer_reg[12], mixer_reg[13]);
1889 break;
1891 case MIXER_PRIVATE3_READ:
1892 spin_lock_irqsave(&waveartist_lock, flags);
1894 for (i = 0x30; i < 14 << 8; i += 1 << 8)
1895 waveartist_cmd(devc, 1, &i, 1, mixer_reg + (i >> 8));
1897 spin_unlock_irqrestore(&waveartist_lock, flags);
1899 if (copy_to_user((void *)val, mixer_reg, sizeof(mixer_reg)))
1900 return -EFAULT;
1901 break;
1903 default:
1904 return -EINVAL;
1906 return 0;
1909 /* read back the state from PRIVATE1 */
1910 case SOUND_MIXER_PRIVATE4:
1911 val = (devc->spkr_mute_state ? VNC_MUTE_INTERNAL_SPKR : 0) |
1912 (devc->line_mute_state ? VNC_MUTE_LINE_OUT : 0) |
1913 (devc->handset_detect ? VNC_HANDSET_DETECT : 0) |
1914 (devc->telephone_detect ? VNC_PHONE_DETECT : 0) |
1915 (devc->no_autoselect ? VNC_DISABLE_AUTOSWITCH : 0);
1917 return put_user(val, arg) ? -EFAULT : 0;
1920 if (_SIOC_DIR(cmd) & _SIOC_WRITE) {
1922 * special case for master volume: if we
1923 * received this call - switch from hw
1924 * volume control to a software volume
1925 * control, till the hw volume is modified
1926 * to signal that user wants to be back in
1927 * hardware...
1929 if ((cmd & 0xff) == SOUND_MIXER_VOLUME)
1930 devc->use_slider = 0;
1932 /* speaker output */
1933 if ((cmd & 0xff) == SOUND_MIXER_SPEAKER) {
1934 unsigned int val, l, r;
1936 if (get_user(val, arg))
1937 return -EFAULT;
1939 l = val & 0x7f;
1940 r = (val & 0x7f00) >> 8;
1941 val = (l + r) / 2;
1942 devc->levels[SOUND_MIXER_SPEAKER] = val | (val << 8);
1943 devc->spkr_mute_state = (val <= 50);
1944 vnc_mute_spkr(devc);
1945 return 0;
1949 return -ENOIOCTLCMD;
1952 #endif
1954 static struct address_info cfg;
1956 static int attached;
1958 static int __initdata io = 0;
1959 static int __initdata irq = 0;
1960 static int __initdata dma = 0;
1961 static int __initdata dma2 = 0;
1964 static int __init init_waveartist(void)
1966 const struct waveartist_mixer_info *mix;
1968 if (!io && machine_is_netwinder()) {
1970 * The NetWinder WaveArtist is at a fixed address.
1971 * If the user does not supply an address, use the
1972 * well-known parameters.
1974 io = 0x250;
1975 irq = 12;
1976 dma = 3;
1977 dma2 = 7;
1980 mix = &waveartist_mixer;
1981 #ifdef CONFIG_ARCH_NETWINDER
1982 if (machine_is_netwinder())
1983 mix = &netwinder_mixer;
1984 #endif
1986 cfg.io_base = io;
1987 cfg.irq = irq;
1988 cfg.dma = dma;
1989 cfg.dma2 = dma2;
1991 if (!probe_waveartist(&cfg))
1992 return -ENODEV;
1994 attach_waveartist(&cfg, mix);
1995 attached = 1;
1997 return 0;
2000 static void __exit cleanup_waveartist(void)
2002 if (attached)
2003 unload_waveartist(&cfg);
2006 module_init(init_waveartist);
2007 module_exit(cleanup_waveartist);
2009 #ifndef MODULE
2010 static int __init setup_waveartist(char *str)
2012 /* io, irq, dma, dma2 */
2013 int ints[5];
2015 str = get_options(str, ARRAY_SIZE(ints), ints);
2017 io = ints[1];
2018 irq = ints[2];
2019 dma = ints[3];
2020 dma2 = ints[4];
2022 return 1;
2024 __setup("waveartist=", setup_waveartist);
2025 #endif
2027 MODULE_DESCRIPTION("Rockwell WaveArtist RWA-010 sound driver");
2028 module_param(io, int, 0); /* IO base */
2029 module_param(irq, int, 0); /* IRQ */
2030 module_param(dma, int, 0); /* DMA */
2031 module_param(dma2, int, 0); /* DMA2 */
2032 MODULE_LICENSE("GPL");