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