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