1 /*
2  * Support for VIA 82Cxxx Audio Codecs
3  * Copyright 1999,2000 Jeff Garzik
4  *
5  * Updated to support the VIA 8233/8235 audio subsystem
6  * Alan Cox <alan@redhat.com> (C) Copyright 2002, 2003 Red Hat Inc
7  *
8  * Distributed under the GNU GENERAL PUBLIC LICENSE (GPL) Version 2.
9  * See the "COPYING" file distributed with this software for more info.
10  * NO WARRANTY
11  *
12  * For a list of known bugs (errata) and documentation,
13  * see via-audio.pdf in linux/Documentation/DocBook.
14  * If this documentation does not exist, run "make pdfdocs".
15  */
16 
17 
18 #define VIA_VERSION	"1.9.1-ac3"
19 
20 
21 #include <linux/config.h>
22 #include <linux/module.h>
23 #include <linux/kernel.h>
24 #include <linux/fs.h>
25 #include <linux/mm.h>
26 #include <linux/pci.h>
27 #include <linux/init.h>
28 #include <linux/proc_fs.h>
29 #include <linux/spinlock.h>
30 #include <linux/sound.h>
31 #include <linux/poll.h>
32 #include <linux/soundcard.h>
33 #include <linux/ac97_codec.h>
34 #include <linux/smp_lock.h>
35 #include <linux/ioport.h>
36 #include <linux/wrapper.h>
37 #include <linux/delay.h>
38 #include <asm/io.h>
39 #include <asm/uaccess.h>
40 #include <asm/hardirq.h>
41 #include <asm/semaphore.h>
42 #include "sound_config.h"
43 #include "dev_table.h"
44 #include "mpu401.h"
45 
46 
47 #undef VIA_DEBUG	/* define to enable debugging output and checks */
48 #ifdef VIA_DEBUG
49 /* note: prints function name for you */
50 #define DPRINTK(fmt, args...) printk(KERN_DEBUG "%s: " fmt, __FUNCTION__ , ## args)
51 #else
52 #define DPRINTK(fmt, args...)
53 #endif
54 
55 #undef VIA_NDEBUG	/* define to disable lightweight runtime checks */
56 #ifdef VIA_NDEBUG
57 #define assert(expr)
58 #else
59 #define assert(expr) \
60         if(!(expr)) {					\
61         printk( "Assertion failed! %s,%s,%s,line=%d\n",	\
62         #expr,__FILE__,__FUNCTION__,__LINE__);		\
63         }
64 #endif
65 
66 #define VIA_SUPPORT_MMAP 1 /* buggy, for now... */
67 
68 #define MAX_CARDS	1
69 
70 #define VIA_CARD_NAME	"VIA 82Cxxx Audio driver " VIA_VERSION
71 #define VIA_MODULE_NAME "via82cxxx"
72 #define PFX		VIA_MODULE_NAME ": "
73 
74 #define VIA_COUNTER_LIMIT	100000
75 
76 /* size of DMA buffers */
77 #define VIA_MAX_BUFFER_DMA_PAGES	32
78 
79 /* buffering default values in ms */
80 #define VIA_DEFAULT_FRAG_TIME		20
81 #define VIA_DEFAULT_BUFFER_TIME		500
82 
83 /* the hardware has a 256 fragment limit */
84 #define VIA_MIN_FRAG_NUMBER		2
85 #define VIA_MAX_FRAG_NUMBER		128
86 
87 #define VIA_MAX_FRAG_SIZE		PAGE_SIZE
88 #define VIA_MIN_FRAG_SIZE		(VIA_MAX_BUFFER_DMA_PAGES * PAGE_SIZE / VIA_MAX_FRAG_NUMBER)
89 
90 
91 /* 82C686 function 5 (audio codec) PCI configuration registers */
92 #define VIA_ACLINK_STATUS	0x40
93 #define VIA_ACLINK_CTRL		0x41
94 #define VIA_FUNC_ENABLE		0x42
95 #define VIA_PNP_CONTROL		0x43
96 #define VIA_FM_NMI_CTRL		0x48
97 
98 /*
99  * controller base 0 (scatter-gather) registers
100  *
101  * NOTE: Via datasheet lists first channel as "read"
102  * channel and second channel as "write" channel.
103  * I changed the naming of the constants to be more
104  * clear than I felt the datasheet to be.
105  */
106 
107 #define VIA_BASE0_PCM_OUT_CHAN	0x00 /* output PCM to user */
108 #define VIA_BASE0_PCM_OUT_CHAN_STATUS 0x00
109 #define VIA_BASE0_PCM_OUT_CHAN_CTRL	0x01
110 #define VIA_BASE0_PCM_OUT_CHAN_TYPE	0x02
111 
112 #define VIA_BASE0_PCM_IN_CHAN		0x10 /* input PCM from user */
113 #define VIA_BASE0_PCM_IN_CHAN_STATUS	0x10
114 #define VIA_BASE0_PCM_IN_CHAN_CTRL	0x11
115 #define VIA_BASE0_PCM_IN_CHAN_TYPE	0x12
116 
117 /* offsets from base */
118 #define VIA_PCM_STATUS			0x00
119 #define VIA_PCM_CONTROL			0x01
120 #define VIA_PCM_TYPE			0x02
121 #define VIA_PCM_LEFTVOL			0x02
122 #define VIA_PCM_RIGHTVOL		0x03
123 #define VIA_PCM_TABLE_ADDR		0x04
124 #define VIA_PCM_STOPRATE		0x08	/* 8233+ */
125 #define VIA_PCM_BLOCK_COUNT		0x0C
126 
127 /* XXX unused DMA channel for FM PCM data */
128 #define VIA_BASE0_FM_OUT_CHAN		0x20
129 #define VIA_BASE0_FM_OUT_CHAN_STATUS	0x20
130 #define VIA_BASE0_FM_OUT_CHAN_CTRL	0x21
131 #define VIA_BASE0_FM_OUT_CHAN_TYPE	0x22
132 
133 /* Six channel audio output on 8233 */
134 #define VIA_BASE0_MULTI_OUT_CHAN		0x40
135 #define VIA_BASE0_MULTI_OUT_CHAN_STATUS		0x40
136 #define VIA_BASE0_MULTI_OUT_CHAN_CTRL		0x41
137 #define VIA_BASE0_MULTI_OUT_CHAN_TYPE		0x42
138 
139 #define VIA_BASE0_AC97_CTRL		0x80
140 #define VIA_BASE0_SGD_STATUS_SHADOW	0x84
141 #define VIA_BASE0_GPI_INT_ENABLE	0x8C
142 #define VIA_INTR_OUT			((1<<0) |  (1<<4) |  (1<<8))
143 #define VIA_INTR_IN			((1<<1) |  (1<<5) |  (1<<9))
144 #define VIA_INTR_FM			((1<<2) |  (1<<6) | (1<<10))
145 #define VIA_INTR_MASK		(VIA_INTR_OUT | VIA_INTR_IN | VIA_INTR_FM)
146 
147 /* Newer VIA we need to monitor the low 3 bits of each channel. This
148    mask covers the channels we don't yet use as well
149  */
150 
151 #define VIA_NEW_INTR_MASK		0x77077777UL
152 
153 /* VIA_BASE0_AUDIO_xxx_CHAN_TYPE bits */
154 #define VIA_IRQ_ON_FLAG			(1<<0)	/* int on each flagged scatter block */
155 #define VIA_IRQ_ON_EOL			(1<<1)	/* int at end of scatter list */
156 #define VIA_INT_SEL_PCI_LAST_LINE_READ	(0)	/* int at PCI read of last line */
157 #define VIA_INT_SEL_LAST_SAMPLE_SENT	(1<<2)	/* int at last sample sent */
158 #define VIA_INT_SEL_ONE_LINE_LEFT	(1<<3)	/* int at less than one line to send */
159 #define VIA_PCM_FMT_STEREO		(1<<4)	/* PCM stereo format (bit clear == mono) */
160 #define VIA_PCM_FMT_16BIT		(1<<5)	/* PCM 16-bit format (bit clear == 8-bit) */
161 #define VIA_PCM_REC_FIFO		(1<<6)	/* PCM Recording FIFO */
162 #define VIA_RESTART_SGD_ON_EOL		(1<<7)	/* restart scatter-gather at EOL */
163 #define VIA_PCM_FMT_MASK		(VIA_PCM_FMT_STEREO|VIA_PCM_FMT_16BIT)
164 #define VIA_CHAN_TYPE_MASK		(VIA_RESTART_SGD_ON_EOL | \
165 					 VIA_IRQ_ON_FLAG | \
166 					 VIA_IRQ_ON_EOL)
167 #define VIA_CHAN_TYPE_INT_SELECT	(VIA_INT_SEL_LAST_SAMPLE_SENT)
168 
169 /* PCI configuration register bits and masks */
170 #define VIA_CR40_AC97_READY	0x01
171 #define VIA_CR40_AC97_LOW_POWER	0x02
172 #define VIA_CR40_SECONDARY_READY 0x04
173 
174 #define VIA_CR41_AC97_ENABLE	0x80 /* enable AC97 codec */
175 #define VIA_CR41_AC97_RESET	0x40 /* clear bit to reset AC97 */
176 #define VIA_CR41_AC97_WAKEUP	0x20 /* wake up from power-down mode */
177 #define VIA_CR41_AC97_SDO	0x10 /* force Serial Data Out (SDO) high */
178 #define VIA_CR41_VRA		0x08 /* enable variable sample rate */
179 #define VIA_CR41_PCM_ENABLE	0x04 /* AC Link SGD Read Channel PCM Data Output */
180 #define VIA_CR41_FM_PCM_ENABLE	0x02 /* AC Link FM Channel PCM Data Out */
181 #define VIA_CR41_SB_PCM_ENABLE	0x01 /* AC Link SB PCM Data Output */
182 #define VIA_CR41_BOOT_MASK	(VIA_CR41_AC97_ENABLE | \
183 				 VIA_CR41_AC97_WAKEUP | \
184 				 VIA_CR41_AC97_SDO)
185 #define VIA_CR41_RUN_MASK	(VIA_CR41_AC97_ENABLE | \
186 				 VIA_CR41_AC97_RESET | \
187 				 VIA_CR41_VRA | \
188 				 VIA_CR41_PCM_ENABLE)
189 
190 #define VIA_CR42_SB_ENABLE	0x01
191 #define VIA_CR42_MIDI_ENABLE	0x02
192 #define VIA_CR42_FM_ENABLE	0x04
193 #define VIA_CR42_GAME_ENABLE	0x08
194 #define VIA_CR42_MIDI_IRQMASK   0x40
195 #define VIA_CR42_MIDI_PNP	0x80
196 
197 #define VIA_CR44_SECOND_CODEC_SUPPORT	(1 << 6)
198 #define VIA_CR44_AC_LINK_ACCESS		(1 << 7)
199 
200 #define VIA_CR48_FM_TRAP_TO_NMI		(1 << 2)
201 
202 /* controller base 0 register bitmasks */
203 #define VIA_INT_DISABLE_MASK		(~(0x01|0x02))
204 #define VIA_SGD_STOPPED			(1 << 2)
205 #define VIA_SGD_PAUSED			(1 << 6)
206 #define VIA_SGD_ACTIVE			(1 << 7)
207 #define VIA_SGD_TERMINATE		(1 << 6)
208 #define VIA_SGD_FLAG			(1 << 0)
209 #define VIA_SGD_EOL			(1 << 1)
210 #define VIA_SGD_START			(1 << 7)
211 
212 #define VIA_CR80_FIRST_CODEC		0
213 #define VIA_CR80_SECOND_CODEC		(1 << 30)
214 #define VIA_CR80_FIRST_CODEC_VALID	(1 << 25)
215 #define VIA_CR80_VALID			(1 << 25)
216 #define VIA_CR80_SECOND_CODEC_VALID	(1 << 27)
217 #define VIA_CR80_BUSY			(1 << 24)
218 #define VIA_CR83_BUSY			(1)
219 #define VIA_CR83_FIRST_CODEC_VALID	(1 << 1)
220 #define VIA_CR80_READ			(1 << 23)
221 #define VIA_CR80_WRITE_MODE		0
222 #define VIA_CR80_REG_IDX(idx)		((((idx) & 0xFF) >> 1) << 16)
223 
224 /* capabilities we announce */
225 #ifdef VIA_SUPPORT_MMAP
226 #define VIA_DSP_CAP (DSP_CAP_REVISION | DSP_CAP_DUPLEX | DSP_CAP_MMAP | \
227 		     DSP_CAP_TRIGGER | DSP_CAP_REALTIME)
228 #else
229 #define VIA_DSP_CAP (DSP_CAP_REVISION | DSP_CAP_DUPLEX | \
230 		     DSP_CAP_TRIGGER | DSP_CAP_REALTIME)
231 #endif
232 
233 /* scatter-gather DMA table entry, exactly as passed to hardware */
234 struct via_sgd_table {
235 	u32 addr;
236 	u32 count;	/* includes additional VIA_xxx bits also */
237 };
238 
239 #define VIA_EOL (1 << 31)
240 #define VIA_FLAG (1 << 30)
241 #define VIA_STOP (1 << 29)
242 
243 
244 enum via_channel_states {
245 	sgd_stopped = 0,
246 	sgd_in_progress = 1,
247 };
248 
249 
250 struct via_buffer_pgtbl {
251 	dma_addr_t handle;
252 	void *cpuaddr;
253 };
254 
255 
256 struct via_channel {
257 	atomic_t n_frags;
258 	atomic_t hw_ptr;
259 	wait_queue_head_t wait;
260 
261 	unsigned int sw_ptr;
262 	unsigned int slop_len;
263 	unsigned int n_irqs;
264 	int bytes;
265 
266 	unsigned is_active : 1;
267 	unsigned is_record : 1;
268 	unsigned is_mapped : 1;
269 	unsigned is_enabled : 1;
270 	unsigned is_multi: 1;	/* 8233 6 channel */
271 	u8 pcm_fmt;		/* VIA_PCM_FMT_xxx */
272 	u8 channels;		/* Channel count */
273 
274 	unsigned rate;		/* sample rate */
275 	unsigned int frag_size;
276 	unsigned int frag_number;
277 
278 	unsigned char intmask;
279 
280 	volatile struct via_sgd_table *sgtable;
281 	dma_addr_t sgt_handle;
282 
283 	unsigned int page_number;
284 	struct via_buffer_pgtbl pgtbl[VIA_MAX_BUFFER_DMA_PAGES];
285 
286 	long iobase;
287 
288 	const char *name;
289 };
290 
291 
292 /* data stored for each chip */
293 struct via_info {
294 	struct pci_dev *pdev;
295 	long baseaddr;
296 
297 	struct ac97_codec *ac97;
298 	spinlock_t ac97_lock;
299 	spinlock_t lock;
300 	int card_num;		/* unique card number, from 0 */
301 
302 	int dev_dsp;		/* /dev/dsp index from register_sound_dsp() */
303 
304 	unsigned rev_h : 1;
305 	unsigned legacy: 1;	/* Has legacy ports */
306 	unsigned intmask: 1;	/* Needs int bits */
307 	unsigned sixchannel: 1;	/* 8233/35 with 6 channel support */
308 	unsigned volume: 1;
309 
310 	int locked_rate : 1;
311 
312 	int mixer_vol;		/* 8233/35 volume  - not yet implemented */
313 
314 	struct semaphore syscall_sem;
315 	struct semaphore open_sem;
316 
317 	/* The 8233/8235 have 4 DX audio channels, two record and
318 	   one six channel out. We bind ch_in to DX 1, ch_out to multichannel
319 	   and ch_fm to DX 2. DX 3 and REC0/REC1 are unused at the
320 	   moment */
321 
322 	struct via_channel ch_in;
323 	struct via_channel ch_out;
324 	struct via_channel ch_fm;
325 
326 #ifdef CONFIG_MIDI_VIA82CXXX
327         void *midi_devc;
328         struct address_info midi_info;
329 #endif
330 };
331 
332 
333 /* number of cards, used for assigning unique numbers to cards */
334 static unsigned via_num_cards = 0;
335 
336 
337 
338 /****************************************************************
339  *
340  * prototypes
341  *
342  *
343  */
344 
345 static int via_init_one (struct pci_dev *dev, const struct pci_device_id *id);
346 static void __devexit via_remove_one (struct pci_dev *pdev);
347 
348 static ssize_t via_dsp_read(struct file *file, char *buffer, size_t count, loff_t *ppos);
349 static ssize_t via_dsp_write(struct file *file, const char *buffer, size_t count, loff_t *ppos);
350 static unsigned int via_dsp_poll(struct file *file, struct poll_table_struct *wait);
351 static int via_dsp_ioctl (struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg);
352 static int via_dsp_open (struct inode *inode, struct file *file);
353 static int via_dsp_release(struct inode *inode, struct file *file);
354 static int via_dsp_mmap(struct file *file, struct vm_area_struct *vma);
355 
356 static u16 via_ac97_read_reg (struct ac97_codec *codec, u8 reg);
357 static void via_ac97_write_reg (struct ac97_codec *codec, u8 reg, u16 value);
358 static u8 via_ac97_wait_idle (struct via_info *card);
359 
360 static void via_chan_free (struct via_info *card, struct via_channel *chan);
361 static void via_chan_clear (struct via_info *card, struct via_channel *chan);
362 static void via_chan_pcm_fmt (struct via_channel *chan, int reset);
363 static void via_chan_buffer_free (struct via_info *card, struct via_channel *chan);
364 
365 #ifdef CONFIG_PROC_FS
366 static int via_init_proc (void);
367 static void via_cleanup_proc (void);
368 static int via_card_init_proc (struct via_info *card);
369 static void via_card_cleanup_proc (struct via_info *card);
370 #else
via_init_proc(void)371 static inline int via_init_proc (void) { return 0; }
via_cleanup_proc(void)372 static inline void via_cleanup_proc (void) {}
via_card_init_proc(struct via_info * card)373 static inline int via_card_init_proc (struct via_info *card) { return 0; }
via_card_cleanup_proc(struct via_info * card)374 static inline void via_card_cleanup_proc (struct via_info *card) {}
375 #endif
376 
377 
378 /****************************************************************
379  *
380  * Various data the driver needs
381  *
382  *
383  */
384 
385 
386 static struct pci_device_id via_pci_tbl[] __initdata = {
387 	{ PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_82C686_5,
388 	  PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
389 	{ PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8233_5,
390 	  PCI_ANY_ID, PCI_ANY_ID, 0, 0, 1},
391 	{ 0, }
392 };
393 MODULE_DEVICE_TABLE(pci,via_pci_tbl);
394 
395 
396 static struct pci_driver via_driver = {
397 	name:		VIA_MODULE_NAME,
398 	id_table:	via_pci_tbl,
399 	probe:		via_init_one,
400 	remove:		__devexit_p(via_remove_one),
401 };
402 
403 
404 /****************************************************************
405  *
406  * Low-level base 0 register read/write helpers
407  *
408  *
409  */
410 
411 /**
412  *	via_chan_stop - Terminate DMA on specified PCM channel
413  *	@iobase: PCI base address for SGD channel registers
414  *
415  *	Terminate scatter-gather DMA operation for given
416  *	channel (derived from @iobase), if DMA is active.
417  *
418  *	Note that @iobase is not the PCI base address,
419  *	but the PCI base address plus an offset to
420  *	one of three PCM channels supported by the chip.
421  *
422  */
423 
via_chan_stop(long iobase)424 static inline void via_chan_stop (long iobase)
425 {
426 	if (inb (iobase + VIA_PCM_STATUS) & VIA_SGD_ACTIVE)
427 		outb (VIA_SGD_TERMINATE, iobase + VIA_PCM_CONTROL);
428 }
429 
430 
431 /**
432  *	via_chan_status_clear - Clear status flags on specified DMA channel
433  *	@iobase: PCI base address for SGD channel registers
434  *
435  *	Clear any pending status flags for the given
436  *	DMA channel (derived from @iobase), if any
437  *	flags are asserted.
438  *
439  *	Note that @iobase is not the PCI base address,
440  *	but the PCI base address plus an offset to
441  *	one of three PCM channels supported by the chip.
442  *
443  */
444 
via_chan_status_clear(long iobase)445 static inline void via_chan_status_clear (long iobase)
446 {
447 	u8 tmp = inb (iobase + VIA_PCM_STATUS);
448 
449 	if (tmp != 0)
450 		outb (tmp, iobase + VIA_PCM_STATUS);
451 }
452 
453 
454 /**
455  *	sg_begin - Begin recording or playback on a PCM channel
456  *	@chan: Channel for which DMA operation shall begin
457  *
458  *	Start scatter-gather DMA for the given channel.
459  *
460  */
461 
sg_begin(struct via_channel * chan)462 static inline void sg_begin (struct via_channel *chan)
463 {
464 	DPRINTK("Start with intmask %d\n", chan->intmask);
465 	DPRINTK("About to start from %d to %d\n",
466 		inl(chan->iobase + VIA_PCM_BLOCK_COUNT),
467 		inb(chan->iobase + VIA_PCM_STOPRATE + 3));
468 	outb (VIA_SGD_START|chan->intmask, chan->iobase + VIA_PCM_CONTROL);
469 	DPRINTK("Status is now %02X\n", inb(chan->iobase + VIA_PCM_STATUS));
470 	DPRINTK("Control is now %02X\n", inb(chan->iobase + VIA_PCM_CONTROL));
471 }
472 
473 
sg_active(long iobase)474 static int sg_active (long iobase)
475 {
476 	u8 tmp = inb (iobase + VIA_PCM_STATUS);
477 	if ((tmp & VIA_SGD_STOPPED) || (tmp & VIA_SGD_PAUSED)) {
478 		printk(KERN_WARNING "via82cxxx warning: SG stopped or paused\n");
479 		return 0;
480 	}
481 	if (tmp & VIA_SGD_ACTIVE)
482 		return 1;
483 	return 0;
484 }
485 
via_sg_offset(struct via_channel * chan)486 static int via_sg_offset(struct via_channel *chan)
487 {
488 	return inl (chan->iobase + VIA_PCM_BLOCK_COUNT) & 0x00FFFFFF;
489 }
490 
491 /****************************************************************
492  *
493  * Miscellaneous debris
494  *
495  *
496  */
497 
498 
499 /**
500  *	via_syscall_down - down the card-specific syscell semaphore
501  *	@card: Private info for specified board
502  *	@nonblock: boolean, non-zero if O_NONBLOCK is set
503  *
504  *	Encapsulates standard method of acquiring the syscall sem.
505  *
506  *	Returns negative errno on error, or zero for success.
507  */
508 
via_syscall_down(struct via_info * card,int nonblock)509 static inline int via_syscall_down (struct via_info *card, int nonblock)
510 {
511 	/* Thomas Sailer:
512 	 * EAGAIN is supposed to be used if IO is pending,
513 	 * not if there is contention on some internal
514 	 * synchronization primitive which should be
515 	 * held only for a short time anyway
516 	 */
517 	nonblock = 0;
518 
519 	if (nonblock) {
520 		if (down_trylock (&card->syscall_sem))
521 			return -EAGAIN;
522 	} else {
523 		if (down_interruptible (&card->syscall_sem))
524 			return -ERESTARTSYS;
525 	}
526 
527 	return 0;
528 }
529 
530 
531 /**
532  *	via_stop_everything - Stop all audio operations
533  *	@card: Private info for specified board
534  *
535  *	Stops all DMA operations and interrupts, and clear
536  *	any pending status bits resulting from those operations.
537  */
538 
via_stop_everything(struct via_info * card)539 static void via_stop_everything (struct via_info *card)
540 {
541 	u8 tmp, new_tmp;
542 
543 	DPRINTK ("ENTER\n");
544 
545 	assert (card != NULL);
546 
547 	/*
548 	 * terminate any existing operations on audio read/write channels
549 	 */
550 	via_chan_stop (card->baseaddr + VIA_BASE0_PCM_OUT_CHAN);
551 	via_chan_stop (card->baseaddr + VIA_BASE0_PCM_IN_CHAN);
552 	via_chan_stop (card->baseaddr + VIA_BASE0_FM_OUT_CHAN);
553 	if(card->sixchannel)
554 		via_chan_stop (card->baseaddr + VIA_BASE0_MULTI_OUT_CHAN);
555 
556 	/*
557 	 * clear any existing stops / flags (sanity check mainly)
558 	 */
559 	via_chan_status_clear (card->baseaddr + VIA_BASE0_PCM_OUT_CHAN);
560 	via_chan_status_clear (card->baseaddr + VIA_BASE0_PCM_IN_CHAN);
561 	via_chan_status_clear (card->baseaddr + VIA_BASE0_FM_OUT_CHAN);
562 	if(card->sixchannel)
563 		via_chan_status_clear (card->baseaddr + VIA_BASE0_MULTI_OUT_CHAN);
564 
565 	/*
566 	 * clear any enabled interrupt bits
567 	 */
568 	tmp = inb (card->baseaddr + VIA_BASE0_PCM_OUT_CHAN_TYPE);
569 	new_tmp = tmp & ~(VIA_IRQ_ON_FLAG|VIA_IRQ_ON_EOL|VIA_RESTART_SGD_ON_EOL);
570 	if (tmp != new_tmp)
571 		outb (0, card->baseaddr + VIA_BASE0_PCM_OUT_CHAN_TYPE);
572 
573 	tmp = inb (card->baseaddr + VIA_BASE0_PCM_IN_CHAN_TYPE);
574 	new_tmp = tmp & ~(VIA_IRQ_ON_FLAG|VIA_IRQ_ON_EOL|VIA_RESTART_SGD_ON_EOL);
575 	if (tmp != new_tmp)
576 		outb (0, card->baseaddr + VIA_BASE0_PCM_IN_CHAN_TYPE);
577 
578 	tmp = inb (card->baseaddr + VIA_BASE0_FM_OUT_CHAN_TYPE);
579 	new_tmp = tmp & ~(VIA_IRQ_ON_FLAG|VIA_IRQ_ON_EOL|VIA_RESTART_SGD_ON_EOL);
580 	if (tmp != new_tmp)
581 		outb (0, card->baseaddr + VIA_BASE0_FM_OUT_CHAN_TYPE);
582 
583 	if(card->sixchannel)
584 	{
585 		tmp = inb (card->baseaddr + VIA_BASE0_MULTI_OUT_CHAN_TYPE);
586 		new_tmp = tmp & ~(VIA_IRQ_ON_FLAG|VIA_IRQ_ON_EOL|VIA_RESTART_SGD_ON_EOL);
587 		if (tmp != new_tmp)
588 			outb (0, card->baseaddr + VIA_BASE0_MULTI_OUT_CHAN_TYPE);
589 	}
590 
591 	udelay(10);
592 
593 	/*
594 	 * clear any existing flags
595 	 */
596 	via_chan_status_clear (card->baseaddr + VIA_BASE0_PCM_OUT_CHAN);
597 	via_chan_status_clear (card->baseaddr + VIA_BASE0_PCM_IN_CHAN);
598 	via_chan_status_clear (card->baseaddr + VIA_BASE0_FM_OUT_CHAN);
599 
600 	DPRINTK ("EXIT\n");
601 }
602 
603 
604 /**
605  *	via_set_rate - Set PCM rate for given channel
606  *	@ac97: Pointer to generic codec info struct
607  *	@chan: Private info for specified channel
608  *	@rate: Desired PCM sample rate, in Khz
609  *
610  *	Sets the PCM sample rate for a channel.
611  *
612  *	Values for @rate are clamped to a range of 4000 Khz through 48000 Khz,
613  *	due to hardware constraints.
614  */
615 
via_set_rate(struct ac97_codec * ac97,struct via_channel * chan,unsigned rate)616 static int via_set_rate (struct ac97_codec *ac97,
617 			 struct via_channel *chan, unsigned rate)
618 {
619 	struct via_info *card = ac97->private_data;
620 	int rate_reg;
621 	u32 dacp;
622 	u32 mast_vol, phone_vol, mono_vol, pcm_vol;
623 	u32 mute_vol = 0x8000;	/* The mute volume? -- Seems to work! */
624 
625 	DPRINTK ("ENTER, rate = %d\n", rate);
626 
627 	if (chan->rate == rate)
628 		goto out;
629 	if (card->locked_rate) {
630 		chan->rate = 48000;
631 		goto out;
632 	}
633 
634 	if (rate > 48000)		rate = 48000;
635 	if (rate < 4000) 		rate = 4000;
636 
637 	rate_reg = chan->is_record ? AC97_PCM_LR_ADC_RATE :
638 			    AC97_PCM_FRONT_DAC_RATE;
639 
640 	/* Save current state */
641 	dacp=via_ac97_read_reg(ac97, AC97_POWER_CONTROL);
642 	mast_vol = via_ac97_read_reg(ac97, AC97_MASTER_VOL_STEREO);
643 	mono_vol = via_ac97_read_reg(ac97, AC97_MASTER_VOL_MONO);
644 	phone_vol = via_ac97_read_reg(ac97, AC97_HEADPHONE_VOL);
645 	pcm_vol = via_ac97_read_reg(ac97, AC97_PCMOUT_VOL);
646 	/* Mute - largely reduces popping */
647 	via_ac97_write_reg(ac97, AC97_MASTER_VOL_STEREO, mute_vol);
648 	via_ac97_write_reg(ac97, AC97_MASTER_VOL_MONO, mute_vol);
649 	via_ac97_write_reg(ac97, AC97_HEADPHONE_VOL, mute_vol);
650        	via_ac97_write_reg(ac97, AC97_PCMOUT_VOL, mute_vol);
651 	/* Power down the DAC */
652 	via_ac97_write_reg(ac97, AC97_POWER_CONTROL, dacp|0x0200);
653 
654         /* Set new rate */
655 	via_ac97_write_reg (ac97, rate_reg, rate);
656 
657 	/* Power DAC back up */
658 	via_ac97_write_reg(ac97, AC97_POWER_CONTROL, dacp);
659 	udelay (200); /* reduces popping */
660 
661 	/* Restore volumes */
662 	via_ac97_write_reg(ac97, AC97_MASTER_VOL_STEREO, mast_vol);
663 	via_ac97_write_reg(ac97, AC97_MASTER_VOL_MONO, mono_vol);
664 	via_ac97_write_reg(ac97, AC97_HEADPHONE_VOL, phone_vol);
665 	via_ac97_write_reg(ac97, AC97_PCMOUT_VOL, pcm_vol);
666 
667 	/* the hardware might return a value different than what we
668 	 * passed to it, so read the rate value back from hardware
669 	 * to see what we came up with
670 	 */
671 	chan->rate = via_ac97_read_reg (ac97, rate_reg);
672 
673 	if (chan->rate == 0) {
674 		card->locked_rate = 1;
675 		chan->rate = 48000;
676 		printk (KERN_WARNING PFX "Codec rate locked at 48Khz\n");
677 	}
678 
679 out:
680 	DPRINTK ("EXIT, returning rate %d Hz\n", chan->rate);
681 	return chan->rate;
682 }
683 
684 
685 /****************************************************************
686  *
687  * Channel-specific operations
688  *
689  *
690  */
691 
692 
693 /**
694  *	via_chan_init_defaults - Initialize a struct via_channel
695  *	@card: Private audio chip info
696  *	@chan: Channel to be initialized
697  *
698  *	Zero @chan, and then set all static defaults for the structure.
699  */
700 
via_chan_init_defaults(struct via_info * card,struct via_channel * chan)701 static void via_chan_init_defaults (struct via_info *card, struct via_channel *chan)
702 {
703 	memset (chan, 0, sizeof (*chan));
704 
705 	if(card->intmask)
706 		chan->intmask = 0x23;	/* Turn on the IRQ bits */
707 
708 	if (chan == &card->ch_out) {
709 		chan->name = "PCM-OUT";
710 		if(card->sixchannel)
711 		{
712 			chan->iobase = card->baseaddr + VIA_BASE0_MULTI_OUT_CHAN;
713 			chan->is_multi = 1;
714 			DPRINTK("Using multichannel for pcm out\n");
715 		}
716 		else
717 			chan->iobase = card->baseaddr + VIA_BASE0_PCM_OUT_CHAN;
718 	} else if (chan == &card->ch_in) {
719 		chan->name = "PCM-IN";
720 		chan->iobase = card->baseaddr + VIA_BASE0_PCM_IN_CHAN;
721 		chan->is_record = 1;
722 	} else if (chan == &card->ch_fm) {
723 		chan->name = "PCM-OUT-FM";
724 		chan->iobase = card->baseaddr + VIA_BASE0_FM_OUT_CHAN;
725 	} else {
726 		BUG();
727 	}
728 
729 	init_waitqueue_head (&chan->wait);
730 
731 	chan->pcm_fmt = VIA_PCM_FMT_MASK;
732 	chan->is_enabled = 1;
733 
734 	chan->frag_number = 0;
735         chan->frag_size = 0;
736 	atomic_set(&chan->n_frags, 0);
737 	atomic_set (&chan->hw_ptr, 0);
738 }
739 
740 /**
741  *      via_chan_init - Initialize PCM channel
742  *      @card: Private audio chip info
743  *      @chan: Channel to be initialized
744  *
745  *      Performs some of the preparations necessary to begin
746  *      using a PCM channel.
747  *
748  *      Currently the preparations consist of
749  *      setting the PCM channel to a known state.
750  */
751 
752 
via_chan_init(struct via_info * card,struct via_channel * chan)753 static void via_chan_init (struct via_info *card, struct via_channel *chan)
754 {
755 
756         DPRINTK ("ENTER\n");
757 
758 	/* bzero channel structure, and init members to defaults */
759         via_chan_init_defaults (card, chan);
760 
761         /* stop any existing channel output */
762         via_chan_clear (card, chan);
763         via_chan_status_clear (chan->iobase);
764         via_chan_pcm_fmt (chan, 1);
765 
766 	DPRINTK ("EXIT\n");
767 }
768 
769 /**
770  *	via_chan_buffer_init - Initialize PCM channel buffer
771  *	@card: Private audio chip info
772  *	@chan: Channel to be initialized
773  *
774  *	Performs some of the preparations necessary to begin
775  *	using a PCM channel.
776  *
777  *	Currently the preparations include allocating the
778  *	scatter-gather DMA table and buffers,
779  *	and passing the
780  *	address of the DMA table to the hardware.
781  *
782  *	Note that special care is taken when passing the
783  *	DMA table address to hardware, because it was found
784  *	during driver development that the hardware did not
785  *	always "take" the address.
786  */
787 
via_chan_buffer_init(struct via_info * card,struct via_channel * chan)788 static int via_chan_buffer_init (struct via_info *card, struct via_channel *chan)
789 {
790 	int page, offset;
791 	int i;
792 
793 	DPRINTK ("ENTER\n");
794 
795 
796 	chan->intmask = 0;
797 	if(card->intmask)
798 		chan->intmask = 0x23;	/* Turn on the IRQ bits */
799 
800 	if (chan->sgtable != NULL) {
801 		DPRINTK ("EXIT\n");
802 		return 0;
803 	}
804 
805 	/* alloc DMA-able memory for scatter-gather table */
806 	chan->sgtable = pci_alloc_consistent (card->pdev,
807 		(sizeof (struct via_sgd_table) * chan->frag_number),
808 		&chan->sgt_handle);
809 	if (!chan->sgtable) {
810 		printk (KERN_ERR PFX "DMA table alloc fail, aborting\n");
811 		DPRINTK ("EXIT\n");
812 		return -ENOMEM;
813 	}
814 
815 	memset ((void*)chan->sgtable, 0,
816 		(sizeof (struct via_sgd_table) * chan->frag_number));
817 
818 	/* alloc DMA-able memory for scatter-gather buffers */
819 
820 	chan->page_number = (chan->frag_number * chan->frag_size) / PAGE_SIZE +
821 			    (((chan->frag_number * chan->frag_size) % PAGE_SIZE) ? 1 : 0);
822 
823 	for (i = 0; i < chan->page_number; i++) {
824 		chan->pgtbl[i].cpuaddr = pci_alloc_consistent (card->pdev, PAGE_SIZE,
825 					      &chan->pgtbl[i].handle);
826 
827 		if (!chan->pgtbl[i].cpuaddr) {
828 			chan->page_number = i;
829 			goto err_out_nomem;
830 		}
831 
832 #ifndef VIA_NDEBUG
833                 memset (chan->pgtbl[i].cpuaddr, 0xBC, chan->frag_size);
834 #endif
835 
836 #if 1
837                 DPRINTK ("dmabuf_pg #%d (h=%lx, v2p=%lx, a=%p)\n",
838 			i, (long)chan->pgtbl[i].handle,
839 			virt_to_phys(chan->pgtbl[i].cpuaddr),
840 			chan->pgtbl[i].cpuaddr);
841 #endif
842 	}
843 
844 	for (i = 0; i < chan->frag_number; i++) {
845 
846 		page = i / (PAGE_SIZE / chan->frag_size);
847 		offset = (i % (PAGE_SIZE / chan->frag_size)) * chan->frag_size;
848 
849 		chan->sgtable[i].count = cpu_to_le32 (chan->frag_size | VIA_FLAG);
850 		chan->sgtable[i].addr = cpu_to_le32 (chan->pgtbl[page].handle + offset);
851 
852 #if 1
853 		DPRINTK ("dmabuf #%d (32(h)=%lx)\n",
854 			 i,
855 			 (long)chan->sgtable[i].addr);
856 #endif
857 	}
858 
859 	/* overwrite the last buffer information */
860 	chan->sgtable[chan->frag_number - 1].count = cpu_to_le32 (chan->frag_size | VIA_EOL);
861 
862 	/* set location of DMA-able scatter-gather info table */
863 	DPRINTK ("outl (0x%X, 0x%04lX)\n",
864 		chan->sgt_handle, chan->iobase + VIA_PCM_TABLE_ADDR);
865 
866 	via_ac97_wait_idle (card);
867 	outl (chan->sgt_handle, chan->iobase + VIA_PCM_TABLE_ADDR);
868 	udelay (20);
869 	via_ac97_wait_idle (card);
870 	/* load no rate adaption, stereo 16bit, set up ring slots */
871 	if(card->sixchannel)
872 	{
873 		if(!chan->is_multi)
874 		{
875 			outl (0xFFFFF | (0x3 << 20) | (chan->frag_number << 24), chan->iobase + VIA_PCM_STOPRATE);
876 			udelay (20);
877 			via_ac97_wait_idle (card);
878 		}
879 	}
880 
881 	DPRINTK ("inl (0x%lX) = %x\n",
882 		chan->iobase + VIA_PCM_TABLE_ADDR,
883 		inl(chan->iobase + VIA_PCM_TABLE_ADDR));
884 
885 	DPRINTK ("EXIT\n");
886 	return 0;
887 
888 err_out_nomem:
889 	printk (KERN_ERR PFX "DMA buffer alloc fail, aborting\n");
890 	via_chan_buffer_free (card, chan);
891 	DPRINTK ("EXIT\n");
892 	return -ENOMEM;
893 }
894 
895 
896 /**
897  *	via_chan_free - Release a PCM channel
898  *	@card: Private audio chip info
899  *	@chan: Channel to be released
900  *
901  *	Performs all the functions necessary to clean up
902  *	an initialized channel.
903  *
904  *	Currently these functions include disabled any
905  *	active DMA operations, setting the PCM channel
906  *	back to a known state, and releasing any allocated
907  *	sound buffers.
908  */
909 
via_chan_free(struct via_info * card,struct via_channel * chan)910 static void via_chan_free (struct via_info *card, struct via_channel *chan)
911 {
912 	DPRINTK ("ENTER\n");
913 
914 	spin_lock_irq (&card->lock);
915 
916 	/* stop any existing channel output */
917 	via_chan_status_clear (chan->iobase);
918 	via_chan_stop (chan->iobase);
919 	via_chan_status_clear (chan->iobase);
920 
921 	spin_unlock_irq (&card->lock);
922 
923 	synchronize_irq();
924 
925 	DPRINTK ("EXIT\n");
926 }
927 
via_chan_buffer_free(struct via_info * card,struct via_channel * chan)928 static void via_chan_buffer_free (struct via_info *card, struct via_channel *chan)
929 {
930 	int i;
931 
932         DPRINTK ("ENTER\n");
933 
934 	/* zero location of DMA-able scatter-gather info table */
935 	via_ac97_wait_idle(card);
936 	outl (0, chan->iobase + VIA_PCM_TABLE_ADDR);
937 
938 	for (i = 0; i < chan->page_number; i++)
939 		if (chan->pgtbl[i].cpuaddr) {
940 			pci_free_consistent (card->pdev, PAGE_SIZE,
941 					     chan->pgtbl[i].cpuaddr,
942 					     chan->pgtbl[i].handle);
943 			chan->pgtbl[i].cpuaddr = NULL;
944 			chan->pgtbl[i].handle = 0;
945 		}
946 
947 	chan->page_number = 0;
948 
949 	if (chan->sgtable) {
950 		pci_free_consistent (card->pdev,
951 			(sizeof (struct via_sgd_table) * chan->frag_number),
952 			(void*)chan->sgtable, chan->sgt_handle);
953 		chan->sgtable = NULL;
954 	}
955 
956 	DPRINTK ("EXIT\n");
957 }
958 
959 
960 /**
961  *	via_chan_pcm_fmt - Update PCM channel settings
962  *	@chan: Channel to be updated
963  *	@reset: Boolean.  If non-zero, channel will be reset
964  *		to 8-bit mono mode.
965  *
966  *	Stores the settings of the current PCM format,
967  *	8-bit or 16-bit, and mono/stereo, into the
968  *	hardware settings for the specified channel.
969  *	If @reset is non-zero, the channel is reset
970  *	to 8-bit mono mode.  Otherwise, the channel
971  *	is set to the values stored in the channel
972  *	information struct @chan.
973  */
974 
via_chan_pcm_fmt(struct via_channel * chan,int reset)975 static void via_chan_pcm_fmt (struct via_channel *chan, int reset)
976 {
977 	DPRINTK ("ENTER, pcm_fmt=0x%02X, reset=%s\n",
978 		 chan->pcm_fmt, reset ? "yes" : "no");
979 
980 	assert (chan != NULL);
981 
982 	if (reset)
983 	{
984 		/* reset to 8-bit mono mode */
985 		chan->pcm_fmt = 0;
986 		chan->channels = 1;
987 	}
988 
989 	/* enable interrupts on FLAG and EOL */
990 	chan->pcm_fmt |= VIA_CHAN_TYPE_MASK;
991 
992 	/* if we are recording, enable recording fifo bit */
993 	if (chan->is_record)
994 		chan->pcm_fmt |= VIA_PCM_REC_FIFO;
995 	/* set interrupt select bits where applicable (PCM in & out channels) */
996 	if (!chan->is_record)
997 		chan->pcm_fmt |= VIA_CHAN_TYPE_INT_SELECT;
998 
999 	DPRINTK("SET FMT - %02x %02x\n", chan->intmask , chan->is_multi);
1000 
1001 	if(chan->intmask)
1002 	{
1003 		u32 m;
1004 
1005 		/*
1006 		 *	Channel 0x4 is up to 6 x 16bit and has to be
1007 		 *	programmed differently
1008 		 */
1009 
1010 		if(chan->is_multi)
1011 		{
1012 			u8 c = 0;
1013 
1014 			/*
1015 			 *	Load the type bit for num channels
1016 			 *	and 8/16bit
1017 			 */
1018 
1019 			if(chan->pcm_fmt & VIA_PCM_FMT_16BIT)
1020 				c = 1 << 7;
1021 			if(chan->pcm_fmt & VIA_PCM_FMT_STEREO)
1022 				c |= (2<<4);
1023 			else
1024 				c |= (1<<4);
1025 
1026 			outb(c, chan->iobase + VIA_PCM_TYPE);
1027 
1028 			/*
1029 			 *	Set the channel steering
1030 			 *	Mono
1031 			 *		Channel 0 to slot 3
1032 			 *		Channel 0 to slot 4
1033 			 *	Stereo
1034 			 *		Channel 0 to slot 3
1035 			 *		Channel 1 to slot 4
1036 			 */
1037 
1038 			switch(chan->channels)
1039 			{
1040 				case 1:
1041 					outl(0xFF000000 | (1<<0) | (1<<4) , chan->iobase + VIA_PCM_STOPRATE);
1042 					break;
1043 				case 2:
1044 					outl(0xFF000000 | (1<<0) | (2<<4) , chan->iobase + VIA_PCM_STOPRATE);
1045 					break;
1046 				case 4:
1047 					outl(0xFF000000 | (1<<0) | (2<<4) | (3<<8) | (4<<12), chan->iobase + VIA_PCM_STOPRATE);
1048 					break;
1049 				case 6:
1050 					outl(0xFF000000 | (1<<0) | (2<<4) | (5<<8) | (6<<12) | (3<<16) | (4<<20), chan->iobase + VIA_PCM_STOPRATE);
1051 					break;
1052 			}
1053 		}
1054 		else
1055 		{
1056 			/*
1057 			 *	New style, turn off channel volume
1058 			 *	control, set bits in the right register
1059 			 */
1060 			outb(0x0, chan->iobase + VIA_PCM_LEFTVOL);
1061 			outb(0x0, chan->iobase + VIA_PCM_RIGHTVOL);
1062 
1063 			m = inl(chan->iobase + VIA_PCM_STOPRATE);
1064 			m &= ~(3<<20);
1065 			if(chan->pcm_fmt & VIA_PCM_FMT_STEREO)
1066 				m |= (1 << 20);
1067 			if(chan->pcm_fmt & VIA_PCM_FMT_16BIT)
1068 				m |= (1 << 21);
1069 			outl(m, chan->iobase + VIA_PCM_STOPRATE);
1070 		}
1071 	}
1072 	else
1073 		outb (chan->pcm_fmt, chan->iobase + VIA_PCM_TYPE);
1074 
1075 
1076 	DPRINTK ("EXIT, pcm_fmt = 0x%02X, reg = 0x%02X\n",
1077 		 chan->pcm_fmt,
1078 		 inb (chan->iobase + VIA_PCM_TYPE));
1079 }
1080 
1081 
1082 /**
1083  *	via_chan_clear - Stop DMA channel operation, and reset pointers
1084  *	@card: the chip to accessed
1085  *	@chan: Channel to be cleared
1086  *
1087  *	Call via_chan_stop to halt DMA operations, and then resets
1088  *	all software pointers which track DMA operation.
1089  */
1090 
via_chan_clear(struct via_info * card,struct via_channel * chan)1091 static void via_chan_clear (struct via_info *card, struct via_channel *chan)
1092 {
1093 	DPRINTK ("ENTER\n");
1094 	via_chan_stop (chan->iobase);
1095 	via_chan_buffer_free(card, chan);
1096 	chan->is_active = 0;
1097 	chan->is_mapped = 0;
1098 	chan->is_enabled = 1;
1099 	chan->slop_len = 0;
1100 	chan->sw_ptr = 0;
1101 	chan->n_irqs = 0;
1102 	atomic_set (&chan->hw_ptr, 0);
1103 	DPRINTK ("EXIT\n");
1104 }
1105 
1106 
1107 /**
1108  *	via_chan_set_speed - Set PCM sample rate for given channel
1109  *	@card: Private info for specified board
1110  *	@chan: Channel whose sample rate will be adjusted
1111  *	@val: New sample rate, in Khz
1112  *
1113  *	Helper function for the %SNDCTL_DSP_SPEED ioctl.  OSS semantics
1114  *	demand that all audio operations halt (if they are not already
1115  *	halted) when the %SNDCTL_DSP_SPEED is given.
1116  *
1117  *	This function halts all audio operations for the given channel
1118  *	@chan, and then calls via_set_rate to set the audio hardware
1119  *	to the new rate.
1120  */
1121 
via_chan_set_speed(struct via_info * card,struct via_channel * chan,int val)1122 static int via_chan_set_speed (struct via_info *card,
1123 			       struct via_channel *chan, int val)
1124 {
1125 	DPRINTK ("ENTER, requested rate = %d\n", val);
1126 
1127 	via_chan_clear (card, chan);
1128 
1129 	val = via_set_rate (card->ac97, chan, val);
1130 
1131 	DPRINTK ("EXIT, returning %d\n", val);
1132 	return val;
1133 }
1134 
1135 
1136 /**
1137  *	via_chan_set_fmt - Set PCM sample size for given channel
1138  *	@card: Private info for specified board
1139  *	@chan: Channel whose sample size will be adjusted
1140  *	@val: New sample size, use the %AFMT_xxx constants
1141  *
1142  *	Helper function for the %SNDCTL_DSP_SETFMT ioctl.  OSS semantics
1143  *	demand that all audio operations halt (if they are not already
1144  *	halted) when the %SNDCTL_DSP_SETFMT is given.
1145  *
1146  *	This function halts all audio operations for the given channel
1147  *	@chan, and then calls via_chan_pcm_fmt to set the audio hardware
1148  *	to the new sample size, either 8-bit or 16-bit.
1149  */
1150 
via_chan_set_fmt(struct via_info * card,struct via_channel * chan,int val)1151 static int via_chan_set_fmt (struct via_info *card,
1152 			     struct via_channel *chan, int val)
1153 {
1154 	DPRINTK ("ENTER, val=%s\n",
1155 		 val == AFMT_U8 ? "AFMT_U8" :
1156 	 	 val == AFMT_S16_LE ? "AFMT_S16_LE" :
1157 		 "unknown");
1158 
1159 	via_chan_clear (card, chan);
1160 
1161 	assert (val != AFMT_QUERY); /* this case is handled elsewhere */
1162 
1163 	switch (val) {
1164 	case AFMT_S16_LE:
1165 		if ((chan->pcm_fmt & VIA_PCM_FMT_16BIT) == 0) {
1166 			chan->pcm_fmt |= VIA_PCM_FMT_16BIT;
1167 			via_chan_pcm_fmt (chan, 0);
1168 		}
1169 		break;
1170 
1171 	case AFMT_U8:
1172 		if (chan->pcm_fmt & VIA_PCM_FMT_16BIT) {
1173 			chan->pcm_fmt &= ~VIA_PCM_FMT_16BIT;
1174 			via_chan_pcm_fmt (chan, 0);
1175 		}
1176 		break;
1177 
1178 	default:
1179 		DPRINTK ("unknown AFMT: 0x%X\n", val);
1180 		val = AFMT_S16_LE;
1181 	}
1182 
1183 	DPRINTK ("EXIT\n");
1184 	return val;
1185 }
1186 
1187 
1188 /**
1189  *	via_chan_set_stereo - Enable or disable stereo for a DMA channel
1190  *	@card: Private info for specified board
1191  *	@chan: Channel whose stereo setting will be adjusted
1192  *	@val: New sample size, use the %AFMT_xxx constants
1193  *
1194  *	Helper function for the %SNDCTL_DSP_CHANNELS and %SNDCTL_DSP_STEREO ioctls.  OSS semantics
1195  *	demand that all audio operations halt (if they are not already
1196  *	halted) when %SNDCTL_DSP_CHANNELS or SNDCTL_DSP_STEREO is given.
1197  *
1198  *	This function halts all audio operations for the given channel
1199  *	@chan, and then calls via_chan_pcm_fmt to set the audio hardware
1200  *	to enable or disable stereo.
1201  */
1202 
via_chan_set_stereo(struct via_info * card,struct via_channel * chan,int val)1203 static int via_chan_set_stereo (struct via_info *card,
1204 			        struct via_channel *chan, int val)
1205 {
1206 	DPRINTK ("ENTER, channels = %d\n", val);
1207 
1208 	via_chan_clear (card, chan);
1209 
1210 	switch (val) {
1211 
1212 	/* mono */
1213 	case 1:
1214 		chan->pcm_fmt &= ~VIA_PCM_FMT_STEREO;
1215 		chan->channels = 1;
1216 		via_chan_pcm_fmt (chan, 0);
1217 		break;
1218 
1219 	/* stereo */
1220 	case 2:
1221 		chan->pcm_fmt |= VIA_PCM_FMT_STEREO;
1222 		chan->channels = 2;
1223 		via_chan_pcm_fmt (chan, 0);
1224 		break;
1225 
1226 	case 4:
1227 	case 6:
1228 		if(chan->is_multi)
1229 		{
1230 			chan->pcm_fmt |= VIA_PCM_FMT_STEREO;
1231 			chan->channels = val;
1232 			break;
1233 		}
1234 	/* unknown */
1235 	default:
1236 		val = -EINVAL;
1237 		break;
1238 	}
1239 
1240 	DPRINTK ("EXIT, returning %d\n", val);
1241 	return val;
1242 }
1243 
via_chan_set_buffering(struct via_info * card,struct via_channel * chan,int val)1244 static int via_chan_set_buffering (struct via_info *card,
1245                                 struct via_channel *chan, int val)
1246 {
1247 	int shift;
1248 
1249         DPRINTK ("ENTER\n");
1250 
1251 	/* in both cases the buffer cannot be changed */
1252 	if (chan->is_active || chan->is_mapped) {
1253 		DPRINTK ("EXIT\n");
1254 		return -EINVAL;
1255 	}
1256 
1257 	/* called outside SETFRAGMENT */
1258 	/* set defaults or do nothing */
1259 	if (val < 0) {
1260 
1261 		if (chan->frag_size && chan->frag_number)
1262 			goto out;
1263 
1264 		DPRINTK ("\n");
1265 
1266 		chan->frag_size = (VIA_DEFAULT_FRAG_TIME * chan->rate * chan->channels
1267 				   * ((chan->pcm_fmt & VIA_PCM_FMT_16BIT) ? 2 : 1)) / 1000 - 1;
1268 
1269 		shift = 0;
1270 		while (chan->frag_size) {
1271 			chan->frag_size >>= 1;
1272 			shift++;
1273 		}
1274 		chan->frag_size = 1 << shift;
1275 
1276 		chan->frag_number = (VIA_DEFAULT_BUFFER_TIME / VIA_DEFAULT_FRAG_TIME);
1277 
1278 		DPRINTK ("setting default values %d %d\n", chan->frag_size, chan->frag_number);
1279 	} else {
1280 		chan->frag_size = 1 << (val & 0xFFFF);
1281 		chan->frag_number = (val >> 16) & 0xFFFF;
1282 
1283 		DPRINTK ("using user values %d %d\n", chan->frag_size, chan->frag_number);
1284 	}
1285 
1286 	/* quake3 wants frag_number to be a power of two */
1287 	shift = 0;
1288 	while (chan->frag_number) {
1289 		chan->frag_number >>= 1;
1290 		shift++;
1291 	}
1292 	chan->frag_number = 1 << shift;
1293 
1294 	if (chan->frag_size > VIA_MAX_FRAG_SIZE)
1295 		chan->frag_size = VIA_MAX_FRAG_SIZE;
1296 	else if (chan->frag_size < VIA_MIN_FRAG_SIZE)
1297 		chan->frag_size = VIA_MIN_FRAG_SIZE;
1298 
1299 	if (chan->frag_number < VIA_MIN_FRAG_NUMBER)
1300                 chan->frag_number = VIA_MIN_FRAG_NUMBER;
1301         if (chan->frag_number > VIA_MAX_FRAG_NUMBER)
1302         	chan->frag_number = VIA_MAX_FRAG_NUMBER;
1303 
1304 	if ((chan->frag_number * chan->frag_size) / PAGE_SIZE > VIA_MAX_BUFFER_DMA_PAGES)
1305 		chan->frag_number = (VIA_MAX_BUFFER_DMA_PAGES * PAGE_SIZE) / chan->frag_size;
1306 
1307 out:
1308 	if (chan->is_record)
1309 		atomic_set (&chan->n_frags, 0);
1310 	else
1311 		atomic_set (&chan->n_frags, chan->frag_number);
1312 
1313 	DPRINTK ("EXIT\n");
1314 
1315 	return 0;
1316 }
1317 
1318 #ifdef VIA_CHAN_DUMP_BUFS
1319 /**
1320  *	via_chan_dump_bufs - Display DMA table contents
1321  *	@chan: Channel whose DMA table will be displayed
1322  *
1323  *	Debugging function which displays the contents of the
1324  *	scatter-gather DMA table for the given channel @chan.
1325  */
1326 
via_chan_dump_bufs(struct via_channel * chan)1327 static void via_chan_dump_bufs (struct via_channel *chan)
1328 {
1329 	int i;
1330 
1331 	for (i = 0; i < chan->frag_number; i++) {
1332 		DPRINTK ("#%02d: addr=%x, count=%u, flag=%d, eol=%d\n",
1333 			 i, chan->sgtable[i].addr,
1334 			 chan->sgtable[i].count & 0x00FFFFFF,
1335 			 chan->sgtable[i].count & VIA_FLAG ? 1 : 0,
1336 			 chan->sgtable[i].count & VIA_EOL ? 1 : 0);
1337 	}
1338 	DPRINTK ("buf_in_use = %d, nextbuf = %d\n",
1339 		 atomic_read (&chan->buf_in_use),
1340 		 atomic_read (&chan->sw_ptr));
1341 }
1342 #endif /* VIA_CHAN_DUMP_BUFS */
1343 
1344 
1345 /**
1346  *	via_chan_flush_frag - Flush partially-full playback buffer to hardware
1347  *	@chan: Channel whose DMA table will be flushed
1348  *
1349  *	Flushes partially-full playback buffer to hardware.
1350  */
1351 
via_chan_flush_frag(struct via_channel * chan)1352 static void via_chan_flush_frag (struct via_channel *chan)
1353 {
1354 	DPRINTK ("ENTER\n");
1355 
1356 	assert (chan->slop_len > 0);
1357 
1358 	if (chan->sw_ptr == (chan->frag_number - 1))
1359 		chan->sw_ptr = 0;
1360 	else
1361 		chan->sw_ptr++;
1362 
1363 	chan->slop_len = 0;
1364 
1365 	assert (atomic_read (&chan->n_frags) > 0);
1366 	atomic_dec (&chan->n_frags);
1367 
1368 	DPRINTK ("EXIT\n");
1369 }
1370 
1371 
1372 
1373 /**
1374  *	via_chan_maybe_start - Initiate audio hardware DMA operation
1375  *	@chan: Channel whose DMA is to be started
1376  *
1377  *	Initiate DMA operation, if the DMA engine for the given
1378  *	channel @chan is not already active.
1379  */
1380 
via_chan_maybe_start(struct via_channel * chan)1381 static inline void via_chan_maybe_start (struct via_channel *chan)
1382 {
1383 	assert (chan->is_active == sg_active(chan->iobase));
1384 
1385 	DPRINTK ("MAYBE START %s\n", chan->name);
1386 	if (!chan->is_active && chan->is_enabled) {
1387 		chan->is_active = 1;
1388 		sg_begin (chan);
1389 		DPRINTK ("starting channel %s\n", chan->name);
1390 	}
1391 }
1392 
1393 
1394 /****************************************************************
1395  *
1396  * Interface to ac97-codec module
1397  *
1398  *
1399  */
1400 
1401 /**
1402  *	via_ac97_wait_idle - Wait until AC97 codec is not busy
1403  *	@card: Private info for specified board
1404  *
1405  *	Sleep until the AC97 codec is no longer busy.
1406  *	Returns the final value read from the SGD
1407  *	register being polled.
1408  */
1409 
via_ac97_wait_idle(struct via_info * card)1410 static u8 via_ac97_wait_idle (struct via_info *card)
1411 {
1412 	u8 tmp8;
1413 	int counter = VIA_COUNTER_LIMIT;
1414 
1415 	DPRINTK ("ENTER/EXIT\n");
1416 
1417 	assert (card != NULL);
1418 	assert (card->pdev != NULL);
1419 
1420 	do {
1421 		udelay (15);
1422 
1423 		tmp8 = inb (card->baseaddr + 0x83);
1424 	} while ((tmp8 & VIA_CR83_BUSY) && (counter-- > 0));
1425 
1426 	if (tmp8 & VIA_CR83_BUSY)
1427 		printk (KERN_WARNING PFX "timeout waiting on AC97 codec\n");
1428 	return tmp8;
1429 }
1430 
1431 
1432 /**
1433  *	via_ac97_read_reg - Read AC97 standard register
1434  *	@codec: Pointer to generic AC97 codec info
1435  *	@reg: Index of AC97 register to be read
1436  *
1437  *	Read the value of a single AC97 codec register,
1438  *	as defined by the Intel AC97 specification.
1439  *
1440  *	Defines the standard AC97 read-register operation
1441  *	required by the kernel's ac97_codec interface.
1442  *
1443  *	Returns the 16-bit value stored in the specified
1444  *	register.
1445  */
1446 
via_ac97_read_reg(struct ac97_codec * codec,u8 reg)1447 static u16 via_ac97_read_reg (struct ac97_codec *codec, u8 reg)
1448 {
1449 	unsigned long data;
1450 	struct via_info *card;
1451 	int counter;
1452 
1453 	DPRINTK ("ENTER\n");
1454 
1455 	assert (codec != NULL);
1456 	assert (codec->private_data != NULL);
1457 
1458 	card = codec->private_data;
1459 
1460 	spin_lock(&card->ac97_lock);
1461 
1462 	/* Every time we write to register 80 we cause a transaction.
1463 	   The only safe way to clear the valid bit is to write it at
1464 	   the same time as the command */
1465 	data = (reg << 16) | VIA_CR80_READ | VIA_CR80_VALID;
1466 
1467 	outl (data, card->baseaddr + VIA_BASE0_AC97_CTRL);
1468 	udelay (20);
1469 
1470 	for (counter = VIA_COUNTER_LIMIT; counter > 0; counter--) {
1471 		udelay (1);
1472 		if ((((data = inl(card->baseaddr + VIA_BASE0_AC97_CTRL)) &
1473 		      (VIA_CR80_VALID|VIA_CR80_BUSY)) == VIA_CR80_VALID))
1474 			goto out;
1475 	}
1476 
1477 	printk (KERN_WARNING PFX "timeout while reading AC97 codec (0x%lX)\n", data);
1478 	goto err_out;
1479 
1480 out:
1481 	/* Once the valid bit has become set, we must wait a complete AC97
1482 	   frame before the data has settled. */
1483 	udelay(25);
1484 	data = (unsigned long) inl (card->baseaddr + VIA_BASE0_AC97_CTRL);
1485 
1486 	outb (0x02, card->baseaddr + 0x83);
1487 
1488 	if (((data & 0x007F0000) >> 16) == reg) {
1489 		DPRINTK ("EXIT, success, data=0x%lx, retval=0x%lx\n",
1490 			 data, data & 0x0000FFFF);
1491 		spin_unlock(&card->ac97_lock);
1492 		return data & 0x0000FFFF;
1493 	}
1494 
1495 	printk (KERN_WARNING "via82cxxx_audio: not our index: reg=0x%x, newreg=0x%lx\n",
1496 		reg, ((data & 0x007F0000) >> 16));
1497 
1498 err_out:
1499 	spin_unlock(&card->ac97_lock);
1500 	DPRINTK ("EXIT, returning 0\n");
1501 	return 0;
1502 }
1503 
1504 
1505 /**
1506  *	via_ac97_write_reg - Write AC97 standard register
1507  *	@codec: Pointer to generic AC97 codec info
1508  *	@reg: Index of AC97 register to be written
1509  *	@value: Value to be written to AC97 register
1510  *
1511  *	Write the value of a single AC97 codec register,
1512  *	as defined by the Intel AC97 specification.
1513  *
1514  *	Defines the standard AC97 write-register operation
1515  *	required by the kernel's ac97_codec interface.
1516  */
1517 
via_ac97_write_reg(struct ac97_codec * codec,u8 reg,u16 value)1518 static void via_ac97_write_reg (struct ac97_codec *codec, u8 reg, u16 value)
1519 {
1520 	u32 data;
1521 	struct via_info *card;
1522 	int counter;
1523 
1524 	DPRINTK ("ENTER\n");
1525 
1526 	assert (codec != NULL);
1527 	assert (codec->private_data != NULL);
1528 
1529 	card = codec->private_data;
1530 
1531 	spin_lock(&card->ac97_lock);
1532 
1533 	data = (reg << 16) + value;
1534 	outl (data, card->baseaddr + VIA_BASE0_AC97_CTRL);
1535 	udelay (10);
1536 
1537 	for (counter = VIA_COUNTER_LIMIT; counter > 0; counter--) {
1538 		if ((inb (card->baseaddr + 0x83) & VIA_CR83_BUSY) == 0)
1539 			goto out;
1540 
1541 		udelay (15);
1542 	}
1543 
1544 	printk (KERN_WARNING PFX "timeout after AC97 codec write (0x%X, 0x%X)\n", reg, value);
1545 
1546 out:
1547 	spin_unlock(&card->ac97_lock);
1548 	DPRINTK ("EXIT\n");
1549 }
1550 
1551 
via_mixer_open(struct inode * inode,struct file * file)1552 static int via_mixer_open (struct inode *inode, struct file *file)
1553 {
1554 	int minor = MINOR(inode->i_rdev);
1555 	struct via_info *card;
1556 	struct pci_dev *pdev;
1557 	struct pci_driver *drvr;
1558 
1559 	DPRINTK ("ENTER\n");
1560 
1561 	pci_for_each_dev(pdev) {
1562 		drvr = pci_dev_driver (pdev);
1563 		if (drvr == &via_driver) {
1564 			assert (pci_get_drvdata (pdev) != NULL);
1565 
1566 			card = pci_get_drvdata (pdev);
1567 			if (card->ac97->dev_mixer == minor)
1568 				goto match;
1569 		}
1570 	}
1571 
1572 	DPRINTK ("EXIT, returning -ENODEV\n");
1573 	return -ENODEV;
1574 
1575 match:
1576 	file->private_data = card->ac97;
1577 
1578 	DPRINTK ("EXIT, returning 0\n");
1579 	return 0;
1580 }
1581 
via_mixer_ioctl(struct inode * inode,struct file * file,unsigned int cmd,unsigned long arg)1582 static int via_mixer_ioctl (struct inode *inode, struct file *file, unsigned int cmd,
1583 			    unsigned long arg)
1584 {
1585 	struct ac97_codec *codec = file->private_data;
1586 	struct via_info *card;
1587 	int nonblock = (file->f_flags & O_NONBLOCK);
1588 	int rc;
1589 
1590 	DPRINTK ("ENTER\n");
1591 
1592 	assert (codec != NULL);
1593 	card = codec->private_data;
1594 	assert (card != NULL);
1595 
1596 	rc = via_syscall_down (card, nonblock);
1597 	if (rc) goto out;
1598 
1599 #if 0
1600 	/*
1601 	 *	Intercept volume control on 8233 and 8235
1602 	 */
1603 	if(card->volume)
1604 	{
1605 		switch(cmd)
1606 		{
1607 			case SOUND_MIXER_READ_VOLUME:
1608 				return card->mixer_vol;
1609 			case SOUND_MIXER_WRITE_VOLUME:
1610 			{
1611 				int v;
1612 				if(get_user(v, (int *)arg))
1613 				{
1614 					rc = -EFAULT;
1615 					goto out;
1616 				}
1617 				card->mixer_vol = v;
1618 			}
1619 		}
1620 	}
1621 #endif
1622 	rc = codec->mixer_ioctl(codec, cmd, arg);
1623 
1624 	up (&card->syscall_sem);
1625 
1626 out:
1627 	DPRINTK ("EXIT, returning %d\n", rc);
1628 	return rc;
1629 }
1630 
1631 
1632 static struct file_operations via_mixer_fops = {
1633 	owner:		THIS_MODULE,
1634 	open:		via_mixer_open,
1635 	llseek:		no_llseek,
1636 	ioctl:		via_mixer_ioctl,
1637 };
1638 
1639 
via_ac97_reset(struct via_info * card)1640 static int __init via_ac97_reset (struct via_info *card)
1641 {
1642 	struct pci_dev *pdev = card->pdev;
1643 	u8 tmp8;
1644 	u16 tmp16;
1645 
1646 	DPRINTK ("ENTER\n");
1647 
1648 	assert (pdev != NULL);
1649 
1650 #ifndef NDEBUG
1651 	{
1652 		u8 r40,r41,r42,r43,r44,r48;
1653 		pci_read_config_byte (card->pdev, 0x40, &r40);
1654 		pci_read_config_byte (card->pdev, 0x41, &r41);
1655 		pci_read_config_byte (card->pdev, 0x42, &r42);
1656 		pci_read_config_byte (card->pdev, 0x43, &r43);
1657 		pci_read_config_byte (card->pdev, 0x44, &r44);
1658 		pci_read_config_byte (card->pdev, 0x48, &r48);
1659 		DPRINTK ("PCI config: %02X %02X %02X %02X %02X %02X\n",
1660 			r40,r41,r42,r43,r44,r48);
1661 
1662 		spin_lock_irq (&card->lock);
1663 		DPRINTK ("regs==%02X %02X %02X %08X %08X %08X %08X\n",
1664 			 inb (card->baseaddr + 0x00),
1665 			 inb (card->baseaddr + 0x01),
1666 			 inb (card->baseaddr + 0x02),
1667 			 inl (card->baseaddr + 0x04),
1668 			 inl (card->baseaddr + 0x0C),
1669 			 inl (card->baseaddr + 0x80),
1670 			 inl (card->baseaddr + 0x84));
1671 		spin_unlock_irq (&card->lock);
1672 
1673 	}
1674 #endif
1675 
1676         /*
1677          * Reset AC97 controller: enable, disable, enable,
1678          * pausing after each command for good luck.  Only
1679 	 * do this if the codec is not ready, because it causes
1680 	 * loud pops and such due to such a hard codec reset.
1681          */
1682 	pci_read_config_byte (pdev, VIA_ACLINK_STATUS, &tmp8);
1683 	if ((tmp8 & VIA_CR40_AC97_READY) == 0) {
1684         	pci_write_config_byte (pdev, VIA_ACLINK_CTRL,
1685 				       VIA_CR41_AC97_ENABLE |
1686                 		       VIA_CR41_AC97_RESET |
1687 				       VIA_CR41_AC97_WAKEUP);
1688         	udelay (100);
1689 
1690         	pci_write_config_byte (pdev, VIA_ACLINK_CTRL, 0);
1691         	udelay (100);
1692 
1693         	pci_write_config_byte (pdev, VIA_ACLINK_CTRL,
1694 				       VIA_CR41_AC97_ENABLE |
1695 				       VIA_CR41_PCM_ENABLE |
1696                 		       VIA_CR41_VRA | VIA_CR41_AC97_RESET);
1697         	udelay (100);
1698 	}
1699 
1700 	/* Make sure VRA is enabled, in case we didn't do a
1701 	 * complete codec reset, above
1702 	 */
1703 	pci_read_config_byte (pdev, VIA_ACLINK_CTRL, &tmp8);
1704 	if (((tmp8 & VIA_CR41_VRA) == 0) ||
1705 	    ((tmp8 & VIA_CR41_AC97_ENABLE) == 0) ||
1706 	    ((tmp8 & VIA_CR41_PCM_ENABLE) == 0) ||
1707 	    ((tmp8 & VIA_CR41_AC97_RESET) == 0)) {
1708         	pci_write_config_byte (pdev, VIA_ACLINK_CTRL,
1709 				       VIA_CR41_AC97_ENABLE |
1710 				       VIA_CR41_PCM_ENABLE |
1711                 		       VIA_CR41_VRA | VIA_CR41_AC97_RESET);
1712         	udelay (100);
1713 	}
1714 
1715 	if(card->legacy)
1716 	{
1717 #if 0 /* this breaks on K7M */
1718 		/* disable legacy stuff */
1719 		pci_write_config_byte (pdev, 0x42, 0x00);
1720 		udelay(10);
1721 #endif
1722 
1723 		/* route FM trap to IRQ, disable FM trap */
1724 		pci_write_config_byte (pdev, 0x48, 0x05);
1725 		udelay(10);
1726 	}
1727 
1728 	/* disable all codec GPI interrupts */
1729 	outl (0, pci_resource_start (pdev, 0) + 0x8C);
1730 
1731 	/* WARNING: this line is magic.  Remove this
1732 	 * and things break. */
1733 	/* enable variable rate */
1734  	tmp16 = via_ac97_read_reg (card->ac97, AC97_EXTENDED_STATUS);
1735  	if ((tmp16 & 1) == 0)
1736  		via_ac97_write_reg (card->ac97, AC97_EXTENDED_STATUS, tmp16 | 1);
1737 
1738 	DPRINTK ("EXIT, returning 0\n");
1739 	return 0;
1740 }
1741 
1742 
via_ac97_codec_wait(struct ac97_codec * codec)1743 static void via_ac97_codec_wait (struct ac97_codec *codec)
1744 {
1745 	assert (codec->private_data != NULL);
1746 	via_ac97_wait_idle (codec->private_data);
1747 }
1748 
1749 
via_ac97_init(struct via_info * card)1750 static int __init via_ac97_init (struct via_info *card)
1751 {
1752 	int rc;
1753 	u16 tmp16;
1754 
1755 	DPRINTK ("ENTER\n");
1756 
1757 	assert (card != NULL);
1758 
1759 	card->ac97 = ac97_alloc_codec();
1760 	if(card->ac97 == NULL)
1761 		return -ENOMEM;
1762 
1763 	card->ac97->private_data = card;
1764 	card->ac97->codec_read = via_ac97_read_reg;
1765 	card->ac97->codec_write = via_ac97_write_reg;
1766 	card->ac97->codec_wait = via_ac97_codec_wait;
1767 
1768 	card->ac97->dev_mixer = register_sound_mixer (&via_mixer_fops, -1);
1769 	if (card->ac97->dev_mixer < 0) {
1770 		printk (KERN_ERR PFX "unable to register AC97 mixer, aborting\n");
1771 		DPRINTK ("EXIT, returning -EIO\n");
1772 		ac97_release_codec(card->ac97);
1773 		return -EIO;
1774 	}
1775 
1776 	rc = via_ac97_reset (card);
1777 	if (rc) {
1778 		printk (KERN_ERR PFX "unable to reset AC97 codec, aborting\n");
1779 		goto err_out;
1780 	}
1781 
1782 	mdelay(10);
1783 
1784 	if (ac97_probe_codec (card->ac97) == 0) {
1785 		printk (KERN_ERR PFX "unable to probe AC97 codec, aborting\n");
1786 		rc = -EIO;
1787 		goto err_out;
1788 	}
1789 
1790 	/* enable variable rate */
1791 	tmp16 = via_ac97_read_reg (card->ac97, AC97_EXTENDED_STATUS);
1792 	via_ac97_write_reg (card->ac97, AC97_EXTENDED_STATUS, tmp16 | 1);
1793 
1794  	/*
1795  	 * If we cannot enable VRA, we have a locked-rate codec.
1796  	 * We try again to enable VRA before assuming so, however.
1797  	 */
1798  	tmp16 = via_ac97_read_reg (card->ac97, AC97_EXTENDED_STATUS);
1799  	if ((tmp16 & 1) == 0) {
1800  		via_ac97_write_reg (card->ac97, AC97_EXTENDED_STATUS, tmp16 | 1);
1801  		tmp16 = via_ac97_read_reg (card->ac97, AC97_EXTENDED_STATUS);
1802  		if ((tmp16 & 1) == 0) {
1803  			card->locked_rate = 1;
1804  			printk (KERN_WARNING PFX "Codec rate locked at 48Khz\n");
1805  		}
1806  	}
1807 
1808 	DPRINTK ("EXIT, returning 0\n");
1809 	return 0;
1810 
1811 err_out:
1812 	unregister_sound_mixer (card->ac97->dev_mixer);
1813 	DPRINTK ("EXIT, returning %d\n", rc);
1814 	ac97_release_codec(card->ac97);
1815 	return rc;
1816 }
1817 
1818 
via_ac97_cleanup(struct via_info * card)1819 static void via_ac97_cleanup (struct via_info *card)
1820 {
1821 	DPRINTK ("ENTER\n");
1822 
1823 	assert (card != NULL);
1824 	assert (card->ac97->dev_mixer >= 0);
1825 
1826 	unregister_sound_mixer (card->ac97->dev_mixer);
1827 	ac97_release_codec(card->ac97);
1828 
1829 	DPRINTK ("EXIT\n");
1830 }
1831 
1832 
1833 
1834 /****************************************************************
1835  *
1836  * Interrupt-related code
1837  *
1838  */
1839 
1840 /**
1841  *	via_intr_channel - handle an interrupt for a single channel
1842  *	@chan: handle interrupt for this channel
1843  *
1844  *	This is the "meat" of the interrupt handler,
1845  *	containing the actions taken each time an interrupt
1846  *	occurs.  All communication and coordination with
1847  *	userspace takes place here.
1848  *
1849  *	Locking: inside card->lock
1850  */
1851 
via_intr_channel(struct via_info * card,struct via_channel * chan)1852 static void via_intr_channel (struct via_info *card, struct via_channel *chan)
1853 {
1854 	u8 status;
1855 	int n;
1856 
1857 	/* check pertinent bits of status register for action bits */
1858 	status = inb (chan->iobase) & (VIA_SGD_FLAG | VIA_SGD_EOL | VIA_SGD_STOPPED);
1859 	if (!status)
1860 		return;
1861 
1862 	/* acknowledge any flagged bits ASAP */
1863 	outb (status, chan->iobase);
1864 
1865 	if (!chan->sgtable) /* XXX: temporary solution */
1866 		return;
1867 
1868 	/* grab current h/w ptr value */
1869 	n = atomic_read (&chan->hw_ptr);
1870 
1871 	/* sanity check: make sure our h/w ptr doesn't have a weird value */
1872 	assert (n >= 0);
1873 	assert (n < chan->frag_number);
1874 
1875 
1876 	/* reset SGD data structure in memory to reflect a full buffer,
1877 	 * and advance the h/w ptr, wrapping around to zero if needed
1878 	 */
1879 	if (n == (chan->frag_number - 1)) {
1880 		chan->sgtable[n].count = cpu_to_le32(chan->frag_size | VIA_EOL);
1881 		atomic_set (&chan->hw_ptr, 0);
1882 	} else {
1883 		chan->sgtable[n].count = cpu_to_le32(chan->frag_size | VIA_FLAG);
1884 		atomic_inc (&chan->hw_ptr);
1885 	}
1886 
1887 	/* accounting crap for SNDCTL_DSP_GETxPTR */
1888 	chan->n_irqs++;
1889 	chan->bytes += chan->frag_size;
1890 	/* FIXME - signed overflow is undefined */
1891 	if (chan->bytes < 0) /* handle overflow of 31-bit value */
1892 		chan->bytes = chan->frag_size;
1893 	/* all following checks only occur when not in mmap(2) mode */
1894 	if (!chan->is_mapped)
1895 	{
1896 		/* If we are recording, then n_frags represents the number
1897 		 * of fragments waiting to be handled by userspace.
1898 		 * If we are playback, then n_frags represents the number
1899 		 * of fragments remaining to be filled by userspace.
1900 		 * We increment here.  If we reach max number of fragments,
1901 		 * this indicates an underrun/overrun.  For this case under OSS,
1902 		 * we stop the record/playback process.
1903 		 */
1904 		if (atomic_read (&chan->n_frags) < chan->frag_number)
1905 			atomic_inc (&chan->n_frags);
1906 		assert (atomic_read (&chan->n_frags) <= chan->frag_number);
1907 		if (atomic_read (&chan->n_frags) == chan->frag_number) {
1908 			chan->is_active = 0;
1909 			via_chan_stop (chan->iobase);
1910 		}
1911 	}
1912 	/* wake up anyone listening to see when interrupts occur */
1913 	wake_up_all (&chan->wait);
1914 
1915 	DPRINTK ("%s intr, status=0x%02X, hwptr=0x%lX, chan->hw_ptr=%d\n",
1916 		 chan->name, status, (long) inl (chan->iobase + 0x04),
1917 		 atomic_read (&chan->hw_ptr));
1918 
1919 	DPRINTK ("%s intr, channel n_frags == %d, missed %d\n", chan->name,
1920 		 atomic_read (&chan->n_frags), missed);
1921 }
1922 
1923 
via_interrupt(int irq,void * dev_id,struct pt_regs * regs)1924 static void via_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1925 {
1926 	struct via_info *card = dev_id;
1927 	u32 status32;
1928 
1929 	/* to minimize interrupt sharing costs, we use the SGD status
1930 	 * shadow register to check the status of all inputs and
1931 	 * outputs with a single 32-bit bus read.  If no interrupt
1932 	 * conditions are flagged, we exit immediately
1933 	 */
1934 	status32 = inl (card->baseaddr + VIA_BASE0_SGD_STATUS_SHADOW);
1935 	if (!(status32 & VIA_INTR_MASK))
1936         {
1937 #ifdef CONFIG_MIDI_VIA82CXXX
1938 	    	 if (card->midi_devc)
1939                     	uart401intr(irq, card->midi_devc, regs);
1940 #endif
1941 		return;
1942     	}
1943 	DPRINTK ("intr, status32 == 0x%08X\n", status32);
1944 
1945 	/* synchronize interrupt handling under SMP.  this spinlock
1946 	 * goes away completely on UP
1947 	 */
1948 	spin_lock (&card->lock);
1949 
1950 	if (status32 & VIA_INTR_OUT)
1951 		via_intr_channel (card, &card->ch_out);
1952 	if (status32 & VIA_INTR_IN)
1953 		via_intr_channel (card, &card->ch_in);
1954 	if (status32 & VIA_INTR_FM)
1955 		via_intr_channel (card, &card->ch_fm);
1956 
1957 	spin_unlock (&card->lock);
1958 }
1959 
via_new_interrupt(int irq,void * dev_id,struct pt_regs * regs)1960 static void via_new_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1961 {
1962 	struct via_info *card = dev_id;
1963 	u32 status32;
1964 
1965 	/* to minimize interrupt sharing costs, we use the SGD status
1966 	 * shadow register to check the status of all inputs and
1967 	 * outputs with a single 32-bit bus read.  If no interrupt
1968 	 * conditions are flagged, we exit immediately
1969 	 */
1970 	status32 = inl (card->baseaddr + VIA_BASE0_SGD_STATUS_SHADOW);
1971 	if (!(status32 & VIA_NEW_INTR_MASK))
1972 		return;
1973 	/*
1974 	 * goes away completely on UP
1975 	 */
1976 	spin_lock (&card->lock);
1977 
1978 	via_intr_channel (card, &card->ch_out);
1979 	via_intr_channel (card, &card->ch_in);
1980 	via_intr_channel (card, &card->ch_fm);
1981 
1982 	spin_unlock (&card->lock);
1983 }
1984 
1985 
1986 /**
1987  *	via_interrupt_init - Initialize interrupt handling
1988  *	@card: Private info for specified board
1989  *
1990  *	Obtain and reserve IRQ for using in handling audio events.
1991  *	Also, disable any IRQ-generating resources, to make sure
1992  *	we don't get interrupts before we want them.
1993  */
1994 
via_interrupt_init(struct via_info * card)1995 static int via_interrupt_init (struct via_info *card)
1996 {
1997 	u8 tmp8;
1998 
1999 	DPRINTK ("ENTER\n");
2000 
2001 	assert (card != NULL);
2002 	assert (card->pdev != NULL);
2003 
2004 	/* check for sane IRQ number. can this ever happen? */
2005 	if (card->pdev->irq < 2) {
2006 		printk (KERN_ERR PFX "insane IRQ %d, aborting\n",
2007 			card->pdev->irq);
2008 		DPRINTK ("EXIT, returning -EIO\n");
2009 		return -EIO;
2010 	}
2011 
2012 	/* VIA requires this is done */
2013 	pci_write_config_byte(card->pdev, PCI_INTERRUPT_LINE, card->pdev->irq);
2014 
2015 	if(card->legacy)
2016 	{
2017 		/* make sure FM irq is not routed to us */
2018 		pci_read_config_byte (card->pdev, VIA_FM_NMI_CTRL, &tmp8);
2019 		if ((tmp8 & VIA_CR48_FM_TRAP_TO_NMI) == 0) {
2020 			tmp8 |= VIA_CR48_FM_TRAP_TO_NMI;
2021 			pci_write_config_byte (card->pdev, VIA_FM_NMI_CTRL, tmp8);
2022 		}
2023 		if (request_irq (card->pdev->irq, via_interrupt, SA_SHIRQ, VIA_MODULE_NAME, card)) {
2024 			printk (KERN_ERR PFX "unable to obtain IRQ %d, aborting\n",
2025 				card->pdev->irq);
2026 			DPRINTK ("EXIT, returning -EBUSY\n");
2027 			return -EBUSY;
2028 		}
2029 	}
2030 	else
2031 	{
2032 		if (request_irq (card->pdev->irq, via_new_interrupt, SA_SHIRQ, VIA_MODULE_NAME, card)) {
2033 			printk (KERN_ERR PFX "unable to obtain IRQ %d, aborting\n",
2034 				card->pdev->irq);
2035 			DPRINTK ("EXIT, returning -EBUSY\n");
2036 			return -EBUSY;
2037 		}
2038 	}
2039 
2040 	DPRINTK ("EXIT, returning 0\n");
2041 	return 0;
2042 }
2043 
2044 
2045 /****************************************************************
2046  *
2047  * OSS DSP device
2048  *
2049  */
2050 
2051 static struct file_operations via_dsp_fops = {
2052 	owner:		THIS_MODULE,
2053 	open:		via_dsp_open,
2054 	release:	via_dsp_release,
2055 	read:		via_dsp_read,
2056 	write:		via_dsp_write,
2057 	poll:		via_dsp_poll,
2058 	llseek: 	no_llseek,
2059 	ioctl:		via_dsp_ioctl,
2060 	mmap:		via_dsp_mmap,
2061 };
2062 
2063 
via_dsp_init(struct via_info * card)2064 static int __init via_dsp_init (struct via_info *card)
2065 {
2066 	u8 tmp8;
2067 
2068 	DPRINTK ("ENTER\n");
2069 
2070 	assert (card != NULL);
2071 
2072 	if(card->legacy)
2073 	{
2074 		/* turn off legacy features, if not already */
2075 		pci_read_config_byte (card->pdev, VIA_FUNC_ENABLE, &tmp8);
2076 		if (tmp8 & (VIA_CR42_SB_ENABLE |  VIA_CR42_FM_ENABLE)) {
2077 			tmp8 &= ~(VIA_CR42_SB_ENABLE | VIA_CR42_FM_ENABLE);
2078 			pci_write_config_byte (card->pdev, VIA_FUNC_ENABLE, tmp8);
2079 		}
2080 	}
2081 
2082 	via_stop_everything (card);
2083 
2084 	card->dev_dsp = register_sound_dsp (&via_dsp_fops, -1);
2085 	if (card->dev_dsp < 0) {
2086 		DPRINTK ("EXIT, returning -ENODEV\n");
2087 		return -ENODEV;
2088 	}
2089 	DPRINTK ("EXIT, returning 0\n");
2090 	return 0;
2091 }
2092 
2093 
via_dsp_cleanup(struct via_info * card)2094 static void via_dsp_cleanup (struct via_info *card)
2095 {
2096 	DPRINTK ("ENTER\n");
2097 
2098 	assert (card != NULL);
2099 	assert (card->dev_dsp >= 0);
2100 
2101 	via_stop_everything (card);
2102 
2103 	unregister_sound_dsp (card->dev_dsp);
2104 
2105 	DPRINTK ("EXIT\n");
2106 }
2107 
2108 
via_mm_nopage(struct vm_area_struct * vma,unsigned long address,int write_access)2109 static struct page * via_mm_nopage (struct vm_area_struct * vma,
2110 				    unsigned long address, int write_access)
2111 {
2112 	struct via_info *card = vma->vm_private_data;
2113 	struct via_channel *chan = &card->ch_out;
2114 	unsigned long max_bufs;
2115 	struct page *dmapage;
2116 	unsigned long pgoff;
2117 	int rd, wr;
2118 
2119 	DPRINTK ("ENTER, start %lXh, ofs %lXh, pgoff %ld, addr %lXh, wr %d\n",
2120 		 vma->vm_start,
2121 		 address - vma->vm_start,
2122 		 (address - vma->vm_start) >> PAGE_SHIFT,
2123 		 address,
2124 		 write_access);
2125 
2126         if (address > vma->vm_end) {
2127 		DPRINTK ("EXIT, returning NOPAGE_SIGBUS\n");
2128 		return NOPAGE_SIGBUS; /* Disallow mremap */
2129 	}
2130         if (!card) {
2131 		DPRINTK ("EXIT, returning NOPAGE_OOM\n");
2132 		return NOPAGE_OOM;	/* Nothing allocated */
2133 	}
2134 
2135 	pgoff = vma->vm_pgoff + ((address - vma->vm_start) >> PAGE_SHIFT);
2136 	rd = card->ch_in.is_mapped;
2137 	wr = card->ch_out.is_mapped;
2138 
2139 	max_bufs = chan->frag_number;
2140 	if (rd && wr)
2141 		max_bufs *= 2;
2142 	if (pgoff >= max_bufs)
2143 		return NOPAGE_SIGBUS;
2144 
2145 	/* if full-duplex (read+write) and we have two sets of bufs,
2146 	 * then the playback buffers come first, sez soundcard.c */
2147 	if (pgoff >= chan->page_number) {
2148 		pgoff -= chan->page_number;
2149 		chan = &card->ch_in;
2150 	} else if (!wr)
2151 		chan = &card->ch_in;
2152 
2153 	assert ((((unsigned long)chan->pgtbl[pgoff].cpuaddr) % PAGE_SIZE) == 0);
2154 
2155 	dmapage = virt_to_page (chan->pgtbl[pgoff].cpuaddr);
2156 	DPRINTK ("EXIT, returning page %p for cpuaddr %lXh\n",
2157 		 dmapage, (unsigned long) chan->pgtbl[pgoff].cpuaddr);
2158 	get_page (dmapage);
2159 	return dmapage;
2160 }
2161 
2162 
2163 #ifndef VM_RESERVED
via_mm_swapout(struct page * page,struct file * filp)2164 static int via_mm_swapout (struct page *page, struct file *filp)
2165 {
2166 	return 0;
2167 }
2168 #endif /* VM_RESERVED */
2169 
2170 
2171 struct vm_operations_struct via_mm_ops = {
2172 	nopage:		via_mm_nopage,
2173 
2174 #ifndef VM_RESERVED
2175 	swapout:	via_mm_swapout,
2176 #endif
2177 };
2178 
2179 
via_dsp_mmap(struct file * file,struct vm_area_struct * vma)2180 static int via_dsp_mmap(struct file *file, struct vm_area_struct *vma)
2181 {
2182 	struct via_info *card;
2183 	int nonblock = (file->f_flags & O_NONBLOCK);
2184 	int rc = -EINVAL, rd=0, wr=0;
2185 	unsigned long max_size, size, start, offset;
2186 
2187 	assert (file != NULL);
2188 	assert (vma != NULL);
2189 	card = file->private_data;
2190 	assert (card != NULL);
2191 
2192 	DPRINTK ("ENTER, start %lXh, size %ld, pgoff %ld\n",
2193 		 vma->vm_start,
2194 		 vma->vm_end - vma->vm_start,
2195 		 vma->vm_pgoff);
2196 
2197 	max_size = 0;
2198 	if (vma->vm_flags & VM_READ) {
2199 		rd = 1;
2200 		via_chan_set_buffering(card, &card->ch_in, -1);
2201 		via_chan_buffer_init (card, &card->ch_in);
2202 		max_size += card->ch_in.page_number << PAGE_SHIFT;
2203 	}
2204 	if (vma->vm_flags & VM_WRITE) {
2205 		wr = 1;
2206 		via_chan_set_buffering(card, &card->ch_out, -1);
2207 		via_chan_buffer_init (card, &card->ch_out);
2208 		max_size += card->ch_out.page_number << PAGE_SHIFT;
2209 	}
2210 
2211 	start = vma->vm_start;
2212 	offset = (vma->vm_pgoff << PAGE_SHIFT);
2213 	size = vma->vm_end - vma->vm_start;
2214 
2215 	/* some basic size/offset sanity checks */
2216 	if (size > max_size)
2217 		goto out;
2218 	if (offset > max_size - size)
2219 		goto out;
2220 
2221 	rc = via_syscall_down (card, nonblock);
2222 	if (rc) goto out;
2223 
2224 	vma->vm_ops = &via_mm_ops;
2225 	vma->vm_private_data = card;
2226 
2227 #ifdef VM_RESERVED
2228 	vma->vm_flags |= VM_RESERVED;
2229 #endif
2230 
2231 	if (rd)
2232 		card->ch_in.is_mapped = 1;
2233 	if (wr)
2234 		card->ch_out.is_mapped = 1;
2235 
2236 	up (&card->syscall_sem);
2237 	rc = 0;
2238 
2239 out:
2240 	DPRINTK ("EXIT, returning %d\n", rc);
2241 	return rc;
2242 }
2243 
2244 
via_dsp_do_read(struct via_info * card,char * userbuf,size_t count,int nonblock)2245 static ssize_t via_dsp_do_read (struct via_info *card,
2246 				char *userbuf, size_t count,
2247 				int nonblock)
2248 {
2249         DECLARE_WAITQUEUE(wait, current);
2250 	const char *orig_userbuf = userbuf;
2251 	struct via_channel *chan = &card->ch_in;
2252 	size_t size;
2253 	int n, tmp;
2254 	ssize_t ret = 0;
2255 
2256 	/* if SGD has not yet been started, start it */
2257 	via_chan_maybe_start (chan);
2258 
2259 handle_one_block:
2260 	/* just to be a nice neighbor */
2261 	/* Thomas Sailer:
2262 	 * But also to ourselves, release semaphore if we do so */
2263 	if (current->need_resched) {
2264 		up(&card->syscall_sem);
2265 		schedule ();
2266 		ret = via_syscall_down (card, nonblock);
2267 		if (ret)
2268 			goto out;
2269 	}
2270 
2271 	/* grab current channel software pointer.  In the case of
2272 	 * recording, this is pointing to the next buffer that
2273 	 * will receive data from the audio hardware.
2274 	 */
2275 	n = chan->sw_ptr;
2276 
2277 	/* n_frags represents the number of fragments waiting
2278 	 * to be copied to userland.  sleep until at least
2279 	 * one buffer has been read from the audio hardware.
2280 	 */
2281 	add_wait_queue(&chan->wait, &wait);
2282 	for (;;) {
2283 		__set_current_state(TASK_INTERRUPTIBLE);
2284 		tmp = atomic_read (&chan->n_frags);
2285 		assert (tmp >= 0);
2286 		assert (tmp <= chan->frag_number);
2287 		if (tmp)
2288 			break;
2289 		if (nonblock || !chan->is_active) {
2290 			ret = -EAGAIN;
2291 			break;
2292 		}
2293 
2294 		up(&card->syscall_sem);
2295 
2296 		DPRINTK ("Sleeping on block %d\n", n);
2297 		schedule();
2298 
2299 		ret = via_syscall_down (card, nonblock);
2300 		if (ret)
2301 			break;
2302 
2303 		if (signal_pending (current)) {
2304 			ret = -ERESTARTSYS;
2305 			break;
2306 		}
2307 	}
2308 	set_current_state(TASK_RUNNING);
2309 	remove_wait_queue(&chan->wait, &wait);
2310 	if (ret)
2311 		goto out;
2312 
2313 	/* Now that we have a buffer we can read from, send
2314 	 * as much as sample data possible to userspace.
2315 	 */
2316 	while ((count > 0) && (chan->slop_len < chan->frag_size)) {
2317 		size_t slop_left = chan->frag_size - chan->slop_len;
2318 		void *base = chan->pgtbl[n / (PAGE_SIZE / chan->frag_size)].cpuaddr;
2319 		unsigned ofs = (n % (PAGE_SIZE / chan->frag_size)) * chan->frag_size;
2320 
2321 		size = (count < slop_left) ? count : slop_left;
2322 		if (copy_to_user (userbuf,
2323 				  base + ofs + chan->slop_len,
2324 				  size)) {
2325 			ret = -EFAULT;
2326 			goto out;
2327 		}
2328 
2329 		count -= size;
2330 		chan->slop_len += size;
2331 		userbuf += size;
2332 	}
2333 
2334 	/* If we didn't copy the buffer completely to userspace,
2335 	 * stop now.
2336 	 */
2337 	if (chan->slop_len < chan->frag_size)
2338 		goto out;
2339 
2340 	/*
2341 	 * If we get to this point, we copied one buffer completely
2342 	 * to userspace, give the buffer back to the hardware.
2343 	 */
2344 
2345 	/* advance channel software pointer to point to
2346 	 * the next buffer from which we will copy
2347 	 */
2348 	if (chan->sw_ptr == (chan->frag_number - 1))
2349 		chan->sw_ptr = 0;
2350 	else
2351 		chan->sw_ptr++;
2352 
2353 	/* mark one less buffer waiting to be processed */
2354 	assert (atomic_read (&chan->n_frags) > 0);
2355 	atomic_dec (&chan->n_frags);
2356 
2357 	/* we are at a block boundary, there is no fragment data */
2358 	chan->slop_len = 0;
2359 
2360 	DPRINTK ("Flushed block %u, sw_ptr now %u, n_frags now %d\n",
2361 		n, chan->sw_ptr, atomic_read (&chan->n_frags));
2362 
2363 	DPRINTK ("regs==%02X %02X %02X %08X %08X %08X %08X\n",
2364 		 inb (card->baseaddr + 0x00),
2365 		 inb (card->baseaddr + 0x01),
2366 		 inb (card->baseaddr + 0x02),
2367 		 inl (card->baseaddr + 0x04),
2368 		 inl (card->baseaddr + 0x0C),
2369 		 inl (card->baseaddr + 0x80),
2370 		 inl (card->baseaddr + 0x84));
2371 
2372 	if (count > 0)
2373 		goto handle_one_block;
2374 
2375 out:
2376 	return (userbuf != orig_userbuf) ? (userbuf - orig_userbuf) : ret;
2377 }
2378 
2379 
via_dsp_read(struct file * file,char * buffer,size_t count,loff_t * ppos)2380 static ssize_t via_dsp_read(struct file *file, char *buffer, size_t count, loff_t *ppos)
2381 {
2382 	struct via_info *card;
2383 	int nonblock = (file->f_flags & O_NONBLOCK);
2384 	int rc;
2385 
2386 	DPRINTK ("ENTER, file=%p, buffer=%p, count=%u, ppos=%lu\n",
2387 		 file, buffer, count, ppos ? ((unsigned long)*ppos) : 0);
2388 
2389 	assert (file != NULL);
2390 	card = file->private_data;
2391 	assert (card != NULL);
2392 
2393 	if (ppos != &file->f_pos) {
2394 		DPRINTK ("EXIT, returning -ESPIPE\n");
2395 		return -ESPIPE;
2396 	}
2397 
2398 	rc = via_syscall_down (card, nonblock);
2399 	if (rc) goto out;
2400 
2401 	if (card->ch_in.is_mapped) {
2402 		rc = -ENXIO;
2403 		goto out_up;
2404 	}
2405 
2406 	via_chan_set_buffering(card, &card->ch_in, -1);
2407         rc = via_chan_buffer_init (card, &card->ch_in);
2408 
2409 	if (rc)
2410 		goto out_up;
2411 
2412 	rc = via_dsp_do_read (card, buffer, count, nonblock);
2413 
2414 out_up:
2415 	up (&card->syscall_sem);
2416 out:
2417 	DPRINTK ("EXIT, returning %ld\n",(long) rc);
2418 	return rc;
2419 }
2420 
2421 
via_dsp_do_write(struct via_info * card,const char * userbuf,size_t count,int nonblock)2422 static ssize_t via_dsp_do_write (struct via_info *card,
2423 				 const char *userbuf, size_t count,
2424 				 int nonblock)
2425 {
2426         DECLARE_WAITQUEUE(wait, current);
2427 	const char *orig_userbuf = userbuf;
2428 	struct via_channel *chan = &card->ch_out;
2429 	volatile struct via_sgd_table *sgtable = chan->sgtable;
2430 	size_t size;
2431 	int n, tmp;
2432 	ssize_t ret = 0;
2433 
2434 handle_one_block:
2435 	/* just to be a nice neighbor */
2436 	/* Thomas Sailer:
2437 	 * But also to ourselves, release semaphore if we do so */
2438 	if (current->need_resched) {
2439 		up(&card->syscall_sem);
2440 		schedule ();
2441 		ret = via_syscall_down (card, nonblock);
2442 		if (ret)
2443 			goto out;
2444 	}
2445 
2446 	/* grab current channel fragment pointer.  In the case of
2447 	 * playback, this is pointing to the next fragment that
2448 	 * should receive data from userland.
2449 	 */
2450 	n = chan->sw_ptr;
2451 
2452 	/* n_frags represents the number of fragments remaining
2453 	 * to be filled by userspace.  Sleep until
2454 	 * at least one fragment is available for our use.
2455 	 */
2456 	add_wait_queue(&chan->wait, &wait);
2457 	for (;;) {
2458 		__set_current_state(TASK_INTERRUPTIBLE);
2459 		tmp = atomic_read (&chan->n_frags);
2460 		assert (tmp >= 0);
2461 		assert (tmp <= chan->frag_number);
2462 		if (tmp)
2463 			break;
2464 		if (nonblock || !chan->is_active) {
2465 			ret = -EAGAIN;
2466 			break;
2467 		}
2468 
2469 		up(&card->syscall_sem);
2470 
2471 		DPRINTK ("Sleeping on page %d, tmp==%d, ir==%d\n", n, tmp, chan->is_record);
2472 		schedule();
2473 
2474 		ret = via_syscall_down (card, nonblock);
2475 		if (ret)
2476 			break;
2477 
2478 		if (signal_pending (current)) {
2479 			ret = -ERESTARTSYS;
2480 			break;
2481 		}
2482 	}
2483 	set_current_state(TASK_RUNNING);
2484 	remove_wait_queue(&chan->wait, &wait);
2485 	if (ret)
2486 		goto out;
2487 
2488 	/* Now that we have at least one fragment we can write to, fill the buffer
2489 	 * as much as possible with data from userspace.
2490 	 */
2491 	while ((count > 0) && (chan->slop_len < chan->frag_size)) {
2492 		size_t slop_left = chan->frag_size - chan->slop_len;
2493 
2494 		size = (count < slop_left) ? count : slop_left;
2495 		if (copy_from_user (chan->pgtbl[n / (PAGE_SIZE / chan->frag_size)].cpuaddr + (n % (PAGE_SIZE / chan->frag_size)) * chan->frag_size + chan->slop_len,
2496 				    userbuf, size)) {
2497 			ret = -EFAULT;
2498 			goto out;
2499 		}
2500 
2501 		count -= size;
2502 		chan->slop_len += size;
2503 		userbuf += size;
2504 	}
2505 
2506 	/* If we didn't fill up the buffer with data, stop now.
2507          * Put a 'stop' marker in the DMA table too, to tell the
2508          * audio hardware to stop if it gets here.
2509          */
2510 	if (chan->slop_len < chan->frag_size) {
2511 		sgtable[n].count = cpu_to_le32 (chan->slop_len | VIA_EOL | VIA_STOP);
2512 		goto out;
2513 	}
2514 
2515 	/*
2516          * If we get to this point, we have filled a buffer with
2517          * audio data, flush the buffer to audio hardware.
2518          */
2519 
2520 	/* Record the true size for the audio hardware to notice */
2521         if (n == (chan->frag_number - 1))
2522                 sgtable[n].count = cpu_to_le32 (chan->frag_size | VIA_EOL);
2523         else
2524                 sgtable[n].count = cpu_to_le32 (chan->frag_size | VIA_FLAG);
2525 
2526 	/* advance channel software pointer to point to
2527 	 * the next buffer we will fill with data
2528 	 */
2529 	if (chan->sw_ptr == (chan->frag_number - 1))
2530 		chan->sw_ptr = 0;
2531 	else
2532 		chan->sw_ptr++;
2533 
2534 	/* mark one less buffer as being available for userspace consumption */
2535 	assert (atomic_read (&chan->n_frags) > 0);
2536 	atomic_dec (&chan->n_frags);
2537 
2538 	/* we are at a block boundary, there is no fragment data */
2539 	chan->slop_len = 0;
2540 
2541 	/* if SGD has not yet been started, start it */
2542 	via_chan_maybe_start (chan);
2543 
2544 	DPRINTK ("Flushed block %u, sw_ptr now %u, n_frags now %d\n",
2545 		n, chan->sw_ptr, atomic_read (&chan->n_frags));
2546 
2547 	DPRINTK ("regs==S=%02X C=%02X TP=%02X BP=%08X RT=%08X SG=%08X CC=%08X SS=%08X\n",
2548 		 inb (card->baseaddr + 0x00),
2549 		 inb (card->baseaddr + 0x01),
2550 		 inb (card->baseaddr + 0x02),
2551 		 inl (card->baseaddr + 0x04),
2552 		 inl (card->baseaddr + 0x08),
2553 		 inl (card->baseaddr + 0x0C),
2554 		 inl (card->baseaddr + 0x80),
2555 		 inl (card->baseaddr + 0x84));
2556 
2557 	if (count > 0)
2558 		goto handle_one_block;
2559 
2560 out:
2561 	if (userbuf - orig_userbuf)
2562 		return userbuf - orig_userbuf;
2563 	else
2564 		return ret;
2565 }
2566 
2567 
via_dsp_write(struct file * file,const char * buffer,size_t count,loff_t * ppos)2568 static ssize_t via_dsp_write(struct file *file, const char *buffer, size_t count, loff_t *ppos)
2569 {
2570 	struct via_info *card;
2571 	ssize_t rc;
2572 	int nonblock = (file->f_flags & O_NONBLOCK);
2573 
2574 	DPRINTK ("ENTER, file=%p, buffer=%p, count=%u, ppos=%lu\n",
2575 		 file, buffer, count, ppos ? ((unsigned long)*ppos) : 0);
2576 
2577 	assert (file != NULL);
2578 	card = file->private_data;
2579 	assert (card != NULL);
2580 
2581 	if (ppos != &file->f_pos) {
2582 		DPRINTK ("EXIT, returning -ESPIPE\n");
2583 		return -ESPIPE;
2584 	}
2585 
2586 	rc = via_syscall_down (card, nonblock);
2587 	if (rc) goto out;
2588 
2589 	if (card->ch_out.is_mapped) {
2590 		rc = -ENXIO;
2591 		goto out_up;
2592 	}
2593 
2594 	via_chan_set_buffering(card, &card->ch_out, -1);
2595 	rc = via_chan_buffer_init (card, &card->ch_out);
2596 
2597 	if (rc)
2598 		goto out_up;
2599 
2600 	rc = via_dsp_do_write (card, buffer, count, nonblock);
2601 
2602 out_up:
2603 	up (&card->syscall_sem);
2604 out:
2605 	DPRINTK ("EXIT, returning %ld\n",(long) rc);
2606 	return rc;
2607 }
2608 
2609 
via_dsp_poll(struct file * file,struct poll_table_struct * wait)2610 static unsigned int via_dsp_poll(struct file *file, struct poll_table_struct *wait)
2611 {
2612 	struct via_info *card;
2613 	struct via_channel *chan;
2614 	unsigned int mask = 0;
2615 
2616 	DPRINTK ("ENTER\n");
2617 
2618 	assert (file != NULL);
2619 	card = file->private_data;
2620 	assert (card != NULL);
2621 
2622 	if (file->f_mode & FMODE_READ) {
2623 		chan = &card->ch_in;
2624 		if (sg_active (chan->iobase))
2625 	                poll_wait(file, &chan->wait, wait);
2626 		if (atomic_read (&chan->n_frags) > 0)
2627 			mask |= POLLIN | POLLRDNORM;
2628 	}
2629 
2630 	if (file->f_mode & FMODE_WRITE) {
2631 		chan = &card->ch_out;
2632 		if (sg_active (chan->iobase))
2633 	                poll_wait(file, &chan->wait, wait);
2634 		if (atomic_read (&chan->n_frags) > 0)
2635 			mask |= POLLOUT | POLLWRNORM;
2636 	}
2637 
2638 	DPRINTK ("EXIT, returning %u\n", mask);
2639 	return mask;
2640 }
2641 
2642 
2643 /**
2644  *	via_dsp_drain_playback - sleep until all playback samples are flushed
2645  *	@card: Private info for specified board
2646  *	@chan: Channel to drain
2647  *	@nonblock: boolean, non-zero if O_NONBLOCK is set
2648  *
2649  *	Sleeps until all playback has been flushed to the audio
2650  *	hardware.
2651  *
2652  *	Locking: inside card->syscall_sem
2653  */
2654 
via_dsp_drain_playback(struct via_info * card,struct via_channel * chan,int nonblock)2655 static int via_dsp_drain_playback (struct via_info *card,
2656 				   struct via_channel *chan, int nonblock)
2657 {
2658         DECLARE_WAITQUEUE(wait, current);
2659 	int ret = 0;
2660 
2661 	DPRINTK ("ENTER, nonblock = %d\n", nonblock);
2662 
2663 	if (chan->slop_len > 0)
2664 		via_chan_flush_frag (chan);
2665 
2666 	if (atomic_read (&chan->n_frags) == chan->frag_number)
2667 		goto out;
2668 
2669 	via_chan_maybe_start (chan);
2670 
2671 	add_wait_queue(&chan->wait, &wait);
2672 	for (;;) {
2673 		DPRINTK ("FRAGS %d FRAGNUM %d\n", atomic_read(&chan->n_frags), chan->frag_number);
2674 		__set_current_state(TASK_INTERRUPTIBLE);
2675 		if (atomic_read (&chan->n_frags) >= chan->frag_number)
2676 			break;
2677 
2678 		if (nonblock) {
2679 			DPRINTK ("EXIT, returning -EAGAIN\n");
2680 			ret = -EAGAIN;
2681 			break;
2682 		}
2683 
2684 #ifdef VIA_DEBUG
2685 		{
2686 		u8 r40,r41,r42,r43,r44,r48;
2687 		pci_read_config_byte (card->pdev, 0x40, &r40);
2688 		pci_read_config_byte (card->pdev, 0x41, &r41);
2689 		pci_read_config_byte (card->pdev, 0x42, &r42);
2690 		pci_read_config_byte (card->pdev, 0x43, &r43);
2691 		pci_read_config_byte (card->pdev, 0x44, &r44);
2692 		pci_read_config_byte (card->pdev, 0x48, &r48);
2693 		DPRINTK ("PCI config: %02X %02X %02X %02X %02X %02X\n",
2694 			r40,r41,r42,r43,r44,r48);
2695 
2696 		DPRINTK ("regs==%02X %02X %02X %08X %08X %08X %08X\n",
2697 			 inb (card->baseaddr + 0x00),
2698 			 inb (card->baseaddr + 0x01),
2699 			 inb (card->baseaddr + 0x02),
2700 			 inl (card->baseaddr + 0x04),
2701 			 inl (card->baseaddr + 0x0C),
2702 			 inl (card->baseaddr + 0x80),
2703 			 inl (card->baseaddr + 0x84));
2704 		}
2705 
2706 		if (!chan->is_active)
2707 			printk (KERN_ERR "sleeping but not active\n");
2708 #endif
2709 
2710 		up(&card->syscall_sem);
2711 
2712 		DPRINTK ("sleeping, nbufs=%d\n", atomic_read (&chan->n_frags));
2713 		schedule();
2714 
2715 		if ((ret = via_syscall_down (card, nonblock)))
2716 			break;
2717 
2718 		if (signal_pending (current)) {
2719 			DPRINTK ("EXIT, returning -ERESTARTSYS\n");
2720 			ret = -ERESTARTSYS;
2721 			break;
2722 		}
2723 	}
2724 	set_current_state(TASK_RUNNING);
2725 	remove_wait_queue(&chan->wait, &wait);
2726 
2727 #ifdef VIA_DEBUG
2728 	{
2729 		u8 r40,r41,r42,r43,r44,r48;
2730 		pci_read_config_byte (card->pdev, 0x40, &r40);
2731 		pci_read_config_byte (card->pdev, 0x41, &r41);
2732 		pci_read_config_byte (card->pdev, 0x42, &r42);
2733 		pci_read_config_byte (card->pdev, 0x43, &r43);
2734 		pci_read_config_byte (card->pdev, 0x44, &r44);
2735 		pci_read_config_byte (card->pdev, 0x48, &r48);
2736 		DPRINTK ("PCI config: %02X %02X %02X %02X %02X %02X\n",
2737 			r40,r41,r42,r43,r44,r48);
2738 
2739 		DPRINTK ("regs==%02X %02X %02X %08X %08X %08X %08X\n",
2740 			 inb (card->baseaddr + 0x00),
2741 			 inb (card->baseaddr + 0x01),
2742 			 inb (card->baseaddr + 0x02),
2743 			 inl (card->baseaddr + 0x04),
2744 			 inl (card->baseaddr + 0x0C),
2745 			 inl (card->baseaddr + 0x80),
2746 			 inl (card->baseaddr + 0x84));
2747 
2748 		DPRINTK ("final nbufs=%d\n", atomic_read (&chan->n_frags));
2749 	}
2750 #endif
2751 
2752 out:
2753 	DPRINTK ("EXIT, returning %d\n", ret);
2754 	return ret;
2755 }
2756 
2757 
2758 /**
2759  *	via_dsp_ioctl_space - get information about channel buffering
2760  *	@card: Private info for specified board
2761  *	@chan: pointer to channel-specific info
2762  *	@arg: user buffer for returned information
2763  *
2764  *	Handles SNDCTL_DSP_GETISPACE and SNDCTL_DSP_GETOSPACE.
2765  *
2766  *	Locking: inside card->syscall_sem
2767  */
2768 
via_dsp_ioctl_space(struct via_info * card,struct via_channel * chan,void * arg)2769 static int via_dsp_ioctl_space (struct via_info *card,
2770 				struct via_channel *chan,
2771 				void *arg)
2772 {
2773 	audio_buf_info info;
2774 
2775 	via_chan_set_buffering(card, chan, -1);
2776 
2777 	info.fragstotal = chan->frag_number;
2778 	info.fragsize = chan->frag_size;
2779 
2780 	/* number of full fragments we can read/write without blocking */
2781 	info.fragments = atomic_read (&chan->n_frags);
2782 
2783 	if ((chan->slop_len % chan->frag_size > 0) && (info.fragments > 0))
2784 		info.fragments--;
2785 
2786 	/* number of bytes that can be read or written immediately
2787 	 * without blocking.
2788 	 */
2789 	info.bytes = (info.fragments * chan->frag_size);
2790 	if (chan->slop_len % chan->frag_size > 0)
2791 		info.bytes += chan->frag_size - (chan->slop_len % chan->frag_size);
2792 
2793 	DPRINTK ("EXIT, returning fragstotal=%d, fragsize=%d, fragments=%d, bytes=%d\n",
2794 		info.fragstotal,
2795 		info.fragsize,
2796 		info.fragments,
2797 		info.bytes);
2798 
2799 	return copy_to_user (arg, &info, sizeof (info))?-EFAULT:0;
2800 }
2801 
2802 
2803 /**
2804  *	via_dsp_ioctl_ptr - get information about hardware buffer ptr
2805  *	@card: Private info for specified board
2806  *	@chan: pointer to channel-specific info
2807  *	@arg: user buffer for returned information
2808  *
2809  *	Handles SNDCTL_DSP_GETIPTR and SNDCTL_DSP_GETOPTR.
2810  *
2811  *	Locking: inside card->syscall_sem
2812  */
2813 
via_dsp_ioctl_ptr(struct via_info * card,struct via_channel * chan,void * arg)2814 static int via_dsp_ioctl_ptr (struct via_info *card,
2815 				struct via_channel *chan,
2816 				void *arg)
2817 {
2818 	count_info info;
2819 
2820 	spin_lock_irq (&card->lock);
2821 
2822 	info.bytes = chan->bytes;
2823 	info.blocks = chan->n_irqs;
2824 	chan->n_irqs = 0;
2825 
2826 	spin_unlock_irq (&card->lock);
2827 
2828 	if (chan->is_active) {
2829 		unsigned long extra;
2830 		info.ptr = atomic_read (&chan->hw_ptr) * chan->frag_size;
2831 		extra = chan->frag_size - via_sg_offset(chan);
2832 		info.ptr += extra;
2833 		info.bytes += extra;
2834 	} else {
2835 		info.ptr = 0;
2836 	}
2837 
2838 	DPRINTK ("EXIT, returning bytes=%d, blocks=%d, ptr=%d\n",
2839 		info.bytes,
2840 		info.blocks,
2841 		info.ptr);
2842 
2843 	return copy_to_user (arg, &info, sizeof (info))?-EFAULT:0;
2844 }
2845 
2846 
via_dsp_ioctl_trigger(struct via_channel * chan,int val)2847 static int via_dsp_ioctl_trigger (struct via_channel *chan, int val)
2848 {
2849 	int enable, do_something;
2850 
2851 	if (chan->is_record)
2852 		enable = (val & PCM_ENABLE_INPUT);
2853 	else
2854 		enable = (val & PCM_ENABLE_OUTPUT);
2855 
2856 	if (!chan->is_enabled && enable) {
2857 		do_something = 1;
2858 	} else if (chan->is_enabled && !enable) {
2859 		do_something = -1;
2860 	} else {
2861 		do_something = 0;
2862 	}
2863 
2864 	DPRINTK ("enable=%d, do_something=%d\n",
2865 		 enable, do_something);
2866 
2867 	if (chan->is_active && do_something)
2868 		return -EINVAL;
2869 
2870 	if (do_something == 1) {
2871 		chan->is_enabled = 1;
2872 		via_chan_maybe_start (chan);
2873 		DPRINTK ("Triggering input\n");
2874 	}
2875 
2876 	else if (do_something == -1) {
2877 		chan->is_enabled = 0;
2878 		DPRINTK ("Setup input trigger\n");
2879 	}
2880 
2881 	return 0;
2882 }
2883 
2884 
via_dsp_ioctl(struct inode * inode,struct file * file,unsigned int cmd,unsigned long arg)2885 static int via_dsp_ioctl (struct inode *inode, struct file *file,
2886 			  unsigned int cmd, unsigned long arg)
2887 {
2888 	int rc, rd=0, wr=0, val=0;
2889 	struct via_info *card;
2890 	struct via_channel *chan;
2891 	int nonblock = (file->f_flags & O_NONBLOCK);
2892 
2893 	assert (file != NULL);
2894 	card = file->private_data;
2895 	assert (card != NULL);
2896 
2897 	if (file->f_mode & FMODE_WRITE)
2898 		wr = 1;
2899 	if (file->f_mode & FMODE_READ)
2900 		rd = 1;
2901 
2902 	rc = via_syscall_down (card, nonblock);
2903 	if (rc)
2904 		return rc;
2905 	rc = -EINVAL;
2906 
2907 	switch (cmd) {
2908 
2909 	/* OSS API version.  XXX unverified */
2910 	case OSS_GETVERSION:
2911 		DPRINTK ("ioctl OSS_GETVERSION, EXIT, returning SOUND_VERSION\n");
2912 		rc = put_user (SOUND_VERSION, (int *)arg);
2913 		break;
2914 
2915 	/* list of supported PCM data formats */
2916 	case SNDCTL_DSP_GETFMTS:
2917 		DPRINTK ("DSP_GETFMTS, EXIT, returning AFMT U8|S16_LE\n");
2918                 rc = put_user (AFMT_U8 | AFMT_S16_LE, (int *)arg);
2919 		break;
2920 
2921 	/* query or set current channel's PCM data format */
2922 	case SNDCTL_DSP_SETFMT:
2923 		if (get_user(val, (int *)arg)) {
2924 			rc = -EFAULT;
2925 			break;
2926 		}
2927 		DPRINTK ("DSP_SETFMT, val==%d\n", val);
2928 		if (val != AFMT_QUERY) {
2929 			rc = 0;
2930 
2931 			if (rd)
2932 				rc = via_chan_set_fmt (card, &card->ch_in, val);
2933 
2934 			if (rc >= 0 && wr)
2935 				rc = via_chan_set_fmt (card, &card->ch_out, val);
2936 
2937 			if (rc < 0)
2938 				break;
2939 
2940 			val = rc;
2941 		} else {
2942 			if ((rd && (card->ch_in.pcm_fmt & VIA_PCM_FMT_16BIT)) ||
2943 			    (wr && (card->ch_out.pcm_fmt & VIA_PCM_FMT_16BIT)))
2944 				val = AFMT_S16_LE;
2945 			else
2946 				val = AFMT_U8;
2947 		}
2948 		DPRINTK ("SETFMT EXIT, returning %d\n", val);
2949                 rc = put_user (val, (int *)arg);
2950 		break;
2951 
2952 	/* query or set number of channels (1=mono, 2=stereo, 4/6 for multichannel) */
2953         case SNDCTL_DSP_CHANNELS:
2954 		if (get_user(val, (int *)arg)) {
2955 			rc = -EFAULT;
2956 			break;
2957 		}
2958 		DPRINTK ("DSP_CHANNELS, val==%d\n", val);
2959 		if (val != 0) {
2960 			rc = 0;
2961 
2962 			if (rd)
2963 				rc = via_chan_set_stereo (card, &card->ch_in, val);
2964 
2965 			if (rc >= 0 && wr)
2966 				rc = via_chan_set_stereo (card, &card->ch_out, val);
2967 
2968 			if (rc < 0)
2969 				break;
2970 
2971 			val = rc;
2972 		} else {
2973 			if (rd)
2974 				val = card->ch_in.channels;
2975 			else
2976 				val = card->ch_out.channels;
2977 		}
2978 		DPRINTK ("CHANNELS EXIT, returning %d\n", val);
2979                 rc = put_user (val, (int *)arg);
2980 		break;
2981 
2982 	/* enable (val is not zero) or disable (val == 0) stereo */
2983         case SNDCTL_DSP_STEREO:
2984 		if (get_user(val, (int *)arg)) {
2985 			rc = -EFAULT;
2986 			break;
2987 		}
2988 		DPRINTK ("DSP_STEREO, val==%d\n", val);
2989 		rc = 0;
2990 
2991 		if (rd)
2992 			rc = via_chan_set_stereo (card, &card->ch_in, val ? 2 : 1);
2993 		if (rc >= 0 && wr)
2994 			rc = via_chan_set_stereo (card, &card->ch_out, val ? 2 : 1);
2995 
2996 		if (rc < 0)
2997 			break;
2998 
2999 		val = rc - 1;
3000 
3001 		DPRINTK ("STEREO EXIT, returning %d\n", val);
3002 		rc = put_user(val, (int *) arg);
3003 		break;
3004 
3005 	/* query or set sampling rate */
3006         case SNDCTL_DSP_SPEED:
3007 		if (get_user(val, (int *)arg)) {
3008 			rc = -EFAULT;
3009 			break;
3010 		}
3011 		DPRINTK ("DSP_SPEED, val==%d\n", val);
3012 		if (val < 0) {
3013 			rc = -EINVAL;
3014 			break;
3015 		}
3016 		if (val > 0) {
3017 			rc = 0;
3018 
3019 			if (rd)
3020 				rc = via_chan_set_speed (card, &card->ch_in, val);
3021 			if (rc >= 0 && wr)
3022 				rc = via_chan_set_speed (card, &card->ch_out, val);
3023 
3024 			if (rc < 0)
3025 				break;
3026 
3027 			val = rc;
3028 		} else {
3029 			if (rd)
3030 				val = card->ch_in.rate;
3031 			else if (wr)
3032 				val = card->ch_out.rate;
3033 			else
3034 				val = 0;
3035 		}
3036 		DPRINTK ("SPEED EXIT, returning %d\n", val);
3037                 rc = put_user (val, (int *)arg);
3038 		break;
3039 
3040 	/* wait until all buffers have been played, and then stop device */
3041 	case SNDCTL_DSP_SYNC:
3042 		DPRINTK ("DSP_SYNC\n");
3043 		rc = 0;
3044 		if (wr) {
3045 			DPRINTK ("SYNC EXIT (after calling via_dsp_drain_playback)\n");
3046 			rc = via_dsp_drain_playback (card, &card->ch_out, nonblock);
3047 		}
3048 		break;
3049 
3050 	/* stop recording/playback immediately */
3051         case SNDCTL_DSP_RESET:
3052 		DPRINTK ("DSP_RESET\n");
3053 		if (rd) {
3054 			via_chan_clear (card, &card->ch_in);
3055 			card->ch_in.frag_number = 0;
3056 			card->ch_in.frag_size = 0;
3057 			atomic_set(&card->ch_in.n_frags, 0);
3058 		}
3059 
3060 		if (wr) {
3061 			via_chan_clear (card, &card->ch_out);
3062 			card->ch_out.frag_number = 0;
3063 			card->ch_out.frag_size = 0;
3064 			atomic_set(&card->ch_out.n_frags, 0);
3065 		}
3066 
3067 		rc = 0;
3068 		break;
3069 
3070 	case SNDCTL_DSP_NONBLOCK:
3071 		file->f_flags |= O_NONBLOCK;
3072 		rc = 0;
3073 		break;
3074 
3075 	/* obtain bitmask of device capabilities, such as mmap, full duplex, etc. */
3076 	case SNDCTL_DSP_GETCAPS:
3077 		DPRINTK ("DSP_GETCAPS\n");
3078 		rc = put_user(VIA_DSP_CAP, (int *)arg);
3079 		break;
3080 
3081 	/* obtain buffer fragment size */
3082 	case SNDCTL_DSP_GETBLKSIZE:
3083 		DPRINTK ("DSP_GETBLKSIZE\n");
3084 
3085 		if (rd) {
3086 			via_chan_set_buffering(card, &card->ch_in, -1);
3087 			rc = put_user(card->ch_in.frag_size, (int *)arg);
3088 		} else if (wr) {
3089 			via_chan_set_buffering(card, &card->ch_out, -1);
3090 			rc = put_user(card->ch_out.frag_size, (int *)arg);
3091 		}
3092 		break;
3093 
3094 	/* obtain information about input buffering */
3095 	case SNDCTL_DSP_GETISPACE:
3096 		DPRINTK ("DSP_GETISPACE\n");
3097 		if (rd)
3098 			rc = via_dsp_ioctl_space (card, &card->ch_in, (void*) arg);
3099 		break;
3100 
3101 	/* obtain information about output buffering */
3102 	case SNDCTL_DSP_GETOSPACE:
3103 		DPRINTK ("DSP_GETOSPACE\n");
3104 		if (wr)
3105 			rc = via_dsp_ioctl_space (card, &card->ch_out, (void*) arg);
3106 		break;
3107 
3108 	/* obtain information about input hardware pointer */
3109 	case SNDCTL_DSP_GETIPTR:
3110 		DPRINTK ("DSP_GETIPTR\n");
3111 		if (rd)
3112 			rc = via_dsp_ioctl_ptr (card, &card->ch_in, (void*) arg);
3113 		break;
3114 
3115 	/* obtain information about output hardware pointer */
3116 	case SNDCTL_DSP_GETOPTR:
3117 		DPRINTK ("DSP_GETOPTR\n");
3118 		if (wr)
3119 			rc = via_dsp_ioctl_ptr (card, &card->ch_out, (void*) arg);
3120 		break;
3121 
3122 	/* return number of bytes remaining to be played by DMA engine */
3123 	case SNDCTL_DSP_GETODELAY:
3124 		{
3125 		DPRINTK ("DSP_GETODELAY\n");
3126 
3127 		chan = &card->ch_out;
3128 
3129 		if (!wr)
3130 			break;
3131 
3132 		if (chan->is_active) {
3133 
3134 			val = chan->frag_number - atomic_read (&chan->n_frags);
3135 
3136 			assert(val >= 0);
3137 
3138 			if (val > 0) {
3139 				val *= chan->frag_size;
3140 				val -= chan->frag_size - via_sg_offset(chan);
3141 			}
3142 			val += chan->slop_len % chan->frag_size;
3143 		} else
3144 			val = 0;
3145 
3146 		assert (val <= (chan->frag_size * chan->frag_number));
3147 
3148 		DPRINTK ("GETODELAY EXIT, val = %d bytes\n", val);
3149                 rc = put_user (val, (int *)arg);
3150 		break;
3151 		}
3152 
3153 	/* handle the quick-start of a channel,
3154 	 * or the notification that a quick-start will
3155 	 * occur in the future
3156 	 */
3157 	case SNDCTL_DSP_SETTRIGGER:
3158 		if (get_user(val, (int *)arg)) {
3159 			rc = -EFAULT;
3160 			break;
3161 		}
3162 		DPRINTK ("DSP_SETTRIGGER, rd=%d, wr=%d, act=%d/%d, en=%d/%d\n",
3163 			rd, wr, card->ch_in.is_active, card->ch_out.is_active,
3164 			card->ch_in.is_enabled, card->ch_out.is_enabled);
3165 
3166 		rc = 0;
3167 
3168 		if (rd)
3169 			rc = via_dsp_ioctl_trigger (&card->ch_in, val);
3170 
3171 		if (!rc && wr)
3172 			rc = via_dsp_ioctl_trigger (&card->ch_out, val);
3173 
3174 		break;
3175 
3176 	case SNDCTL_DSP_GETTRIGGER:
3177 		val = 0;
3178 		if ((file->f_mode & FMODE_READ) && card->ch_in.is_enabled)
3179 			val |= PCM_ENABLE_INPUT;
3180 		if ((file->f_mode & FMODE_WRITE) && card->ch_out.is_enabled)
3181 			val |= PCM_ENABLE_OUTPUT;
3182 		rc = put_user(val, (int *)arg);
3183 		break;
3184 
3185 	/* Enable full duplex.  Since we do this as soon as we are opened
3186 	 * with O_RDWR, this is mainly a no-op that always returns success.
3187 	 */
3188 	case SNDCTL_DSP_SETDUPLEX:
3189 		DPRINTK ("DSP_SETDUPLEX\n");
3190 		if (!rd || !wr)
3191 			break;
3192 		rc = 0;
3193 		break;
3194 
3195 	/* set fragment size.  implemented as a successful no-op for now */
3196 	case SNDCTL_DSP_SETFRAGMENT:
3197 		if (get_user(val, (int *)arg)) {
3198 			rc = -EFAULT;
3199 			break;
3200 		}
3201 		DPRINTK ("DSP_SETFRAGMENT, val==%d\n", val);
3202 
3203 		if (rd)
3204 			rc = via_chan_set_buffering(card, &card->ch_in, val);
3205 
3206 		if (wr)
3207 			rc = via_chan_set_buffering(card, &card->ch_out, val);
3208 
3209 		DPRINTK ("SNDCTL_DSP_SETFRAGMENT (fragshift==0x%04X (%d), maxfrags==0x%04X (%d))\n",
3210 			 val & 0xFFFF,
3211 			 val & 0xFFFF,
3212 			 (val >> 16) & 0xFFFF,
3213 			 (val >> 16) & 0xFFFF);
3214 
3215 		rc = 0;
3216 		break;
3217 
3218 	/* inform device of an upcoming pause in input (or output). */
3219 	case SNDCTL_DSP_POST:
3220 		DPRINTK ("DSP_POST\n");
3221 		if (wr) {
3222 			if (card->ch_out.slop_len > 0)
3223 				via_chan_flush_frag (&card->ch_out);
3224 			via_chan_maybe_start (&card->ch_out);
3225 		}
3226 
3227 		rc = 0;
3228 		break;
3229 
3230 	/* not implemented */
3231 	default:
3232 		DPRINTK ("unhandled ioctl, cmd==%u, arg==%p\n",
3233 			 cmd, (void*) arg);
3234 		break;
3235 	}
3236 
3237 	up (&card->syscall_sem);
3238 	DPRINTK ("EXIT, returning %d\n", rc);
3239 	return rc;
3240 }
3241 
3242 
via_dsp_open(struct inode * inode,struct file * file)3243 static int via_dsp_open (struct inode *inode, struct file *file)
3244 {
3245 	int minor = MINOR(inode->i_rdev);
3246 	struct via_info *card;
3247 	struct pci_dev *pdev;
3248 	struct via_channel *chan;
3249 	struct pci_driver *drvr;
3250 	int nonblock = (file->f_flags & O_NONBLOCK);
3251 
3252 	DPRINTK ("ENTER, minor=%d, file->f_mode=0x%x\n", minor, file->f_mode);
3253 
3254 	if (!(file->f_mode & (FMODE_READ | FMODE_WRITE))) {
3255 		DPRINTK ("EXIT, returning -EINVAL\n");
3256 		return -EINVAL;
3257 	}
3258 
3259 	card = NULL;
3260 	pci_for_each_dev(pdev) {
3261 		drvr = pci_dev_driver (pdev);
3262 		if (drvr == &via_driver) {
3263 			assert (pci_get_drvdata (pdev) != NULL);
3264 
3265 			card = pci_get_drvdata (pdev);
3266 			DPRINTK ("dev_dsp = %d, minor = %d, assn = %d\n",
3267 				 card->dev_dsp, minor,
3268 				 (card->dev_dsp ^ minor) & ~0xf);
3269 
3270 			if (((card->dev_dsp ^ minor) & ~0xf) == 0)
3271 				goto match;
3272 		}
3273 	}
3274 
3275 	DPRINTK ("no matching %s found\n", card ? "minor" : "driver");
3276 	return -ENODEV;
3277 
3278 match:
3279 	if (nonblock) {
3280 		if (down_trylock (&card->open_sem)) {
3281 			DPRINTK ("EXIT, returning -EAGAIN\n");
3282 			return -EAGAIN;
3283 		}
3284 	} else {
3285 		if (down_interruptible (&card->open_sem)) {
3286 			DPRINTK ("EXIT, returning -ERESTARTSYS\n");
3287 			return -ERESTARTSYS;
3288 		}
3289 	}
3290 
3291 	file->private_data = card;
3292 	DPRINTK ("file->f_mode == 0x%x\n", file->f_mode);
3293 
3294 	/* handle input from analog source */
3295 	if (file->f_mode & FMODE_READ) {
3296 		chan = &card->ch_in;
3297 
3298 		via_chan_init (card, chan);
3299 
3300 		/* why is this forced to 16-bit stereo in all drivers? */
3301 		chan->pcm_fmt = VIA_PCM_FMT_16BIT | VIA_PCM_FMT_STEREO;
3302 		chan->channels = 2;
3303 
3304 		// TO DO - use FIFO: via_capture_fifo(card, 1);
3305 		via_chan_pcm_fmt (chan, 0);
3306 		via_set_rate (card->ac97, chan, 44100);
3307 	}
3308 
3309 	/* handle output to analog source */
3310 	if (file->f_mode & FMODE_WRITE) {
3311 		chan = &card->ch_out;
3312 
3313 		via_chan_init (card, chan);
3314 
3315 		if (file->f_mode & FMODE_READ) {
3316 			/* if in duplex mode make the recording and playback channels
3317 			   have the same settings */
3318 			chan->pcm_fmt = VIA_PCM_FMT_16BIT | VIA_PCM_FMT_STEREO;
3319 			chan->channels = 2;
3320 			via_chan_pcm_fmt (chan, 0);
3321                         via_set_rate (card->ac97, chan, 44100);
3322 		} else {
3323 			 if ((minor & 0xf) == SND_DEV_DSP16) {
3324 				chan->pcm_fmt = VIA_PCM_FMT_16BIT;
3325 				via_chan_pcm_fmt (chan, 0);
3326 				via_set_rate (card->ac97, chan, 44100);
3327 			} else {
3328 				via_chan_pcm_fmt (chan, 1);
3329 				via_set_rate (card->ac97, chan, 8000);
3330 			}
3331 		}
3332 	}
3333 
3334 	DPRINTK ("EXIT, returning 0\n");
3335 	return 0;
3336 }
3337 
3338 
via_dsp_release(struct inode * inode,struct file * file)3339 static int via_dsp_release(struct inode *inode, struct file *file)
3340 {
3341 	struct via_info *card;
3342 	int nonblock = (file->f_flags & O_NONBLOCK);
3343 	int rc;
3344 
3345 	DPRINTK ("ENTER\n");
3346 
3347 	assert (file != NULL);
3348 	card = file->private_data;
3349 	assert (card != NULL);
3350 
3351 	rc = via_syscall_down (card, nonblock);
3352 	if (rc) {
3353 		DPRINTK ("EXIT (syscall_down error), rc=%d\n", rc);
3354 		return rc;
3355 	}
3356 
3357 	if (file->f_mode & FMODE_WRITE) {
3358 		rc = via_dsp_drain_playback (card, &card->ch_out, nonblock);
3359 		if (rc && rc != ERESTARTSYS)	/* Nobody needs to know about ^C */
3360 			printk (KERN_DEBUG "via_audio: ignoring drain playback error %d\n", rc);
3361 
3362 		via_chan_free (card, &card->ch_out);
3363 		via_chan_buffer_free(card, &card->ch_out);
3364 	}
3365 
3366 	if (file->f_mode & FMODE_READ) {
3367 		via_chan_free (card, &card->ch_in);
3368 		via_chan_buffer_free (card, &card->ch_in);
3369 	}
3370 
3371 	up (&card->syscall_sem);
3372 	up (&card->open_sem);
3373 
3374 	DPRINTK ("EXIT, returning 0\n");
3375 	return 0;
3376 }
3377 
3378 
3379 /****************************************************************
3380  *
3381  * Chip setup and kernel registration
3382  *
3383  *
3384  */
3385 
via_init_one(struct pci_dev * pdev,const struct pci_device_id * id)3386 static int __init via_init_one (struct pci_dev *pdev, const struct pci_device_id *id)
3387 {
3388 #ifdef CONFIG_MIDI_VIA82CXXX
3389 	u8 r42;
3390 #endif
3391 	int rc;
3392 	struct via_info *card;
3393 	static int printed_version = 0;
3394 
3395 	DPRINTK ("ENTER\n");
3396 
3397 	if (printed_version++ == 0)
3398 		printk (KERN_INFO "Via 686a/8233/8235 audio driver " VIA_VERSION "\n");
3399 
3400 	rc = pci_enable_device (pdev);
3401 	if (rc)
3402 		goto err_out;
3403 
3404 
3405 	rc = pci_request_regions (pdev, "via82cxxx_audio");
3406 	if (rc)
3407 		goto err_out_disable;
3408 
3409 	card = kmalloc (sizeof (*card), GFP_KERNEL);
3410 	if (!card) {
3411 		printk (KERN_ERR PFX "out of memory, aborting\n");
3412 		rc = -ENOMEM;
3413 		goto err_out_res;
3414 	}
3415 
3416 	pci_set_drvdata (pdev, card);
3417 
3418 	memset (card, 0, sizeof (*card));
3419 	card->pdev = pdev;
3420 	card->baseaddr = pci_resource_start (pdev, 0);
3421 	card->card_num = via_num_cards++;
3422 	spin_lock_init (&card->lock);
3423 	spin_lock_init (&card->ac97_lock);
3424 	init_MUTEX (&card->syscall_sem);
3425 	init_MUTEX (&card->open_sem);
3426 
3427 	/* we must init these now, in case the intr handler needs them */
3428 	via_chan_init_defaults (card, &card->ch_out);
3429 	via_chan_init_defaults (card, &card->ch_in);
3430 	via_chan_init_defaults (card, &card->ch_fm);
3431 
3432 	/* if BAR 2 is present, chip is Rev H or later,
3433 	 * which means it has a few extra features */
3434 	if (pci_resource_start (pdev, 2) > 0)
3435 		card->rev_h = 1;
3436 
3437 	/* Overkill for now, but more flexible done right */
3438 
3439 	card->intmask = id->driver_data;
3440 	card->legacy = !card->intmask;
3441 	card->sixchannel = id->driver_data;
3442 
3443 	if(card->sixchannel)
3444 		printk(KERN_INFO PFX "Six channel audio available\n");
3445 	if (pdev->irq < 1) {
3446 		printk (KERN_ERR PFX "invalid PCI IRQ %d, aborting\n", pdev->irq);
3447 		rc = -ENODEV;
3448 		goto err_out_kfree;
3449 	}
3450 
3451 	if (!(pci_resource_flags (pdev, 0) & IORESOURCE_IO)) {
3452 		printk (KERN_ERR PFX "unable to locate I/O resources, aborting\n");
3453 		rc = -ENODEV;
3454 		goto err_out_kfree;
3455 	}
3456 
3457 	pci_set_master(pdev);
3458 
3459 	/*
3460 	 * init AC97 mixer and codec
3461 	 */
3462 	rc = via_ac97_init (card);
3463 	if (rc) {
3464 		printk (KERN_ERR PFX "AC97 init failed, aborting\n");
3465 		goto err_out_kfree;
3466 	}
3467 
3468 	/*
3469 	 * init DSP device
3470 	 */
3471 	rc = via_dsp_init (card);
3472 	if (rc) {
3473 		printk (KERN_ERR PFX "DSP device init failed, aborting\n");
3474 		goto err_out_have_mixer;
3475 	}
3476 
3477 	/*
3478 	 * per-card /proc info
3479 	 */
3480 	rc = via_card_init_proc (card);
3481 	if (rc) {
3482 		printk (KERN_ERR PFX "card-specific /proc init failed, aborting\n");
3483 		goto err_out_have_dsp;
3484 	}
3485 
3486 	/*
3487 	 * init and turn on interrupts, as the last thing we do
3488 	 */
3489 	rc = via_interrupt_init (card);
3490 	if (rc) {
3491 		printk (KERN_ERR PFX "interrupt init failed, aborting\n");
3492 		goto err_out_have_proc;
3493 	}
3494 
3495 	printk (KERN_INFO PFX "board #%d at 0x%04lX, IRQ %d\n",
3496 		card->card_num + 1, card->baseaddr, pdev->irq);
3497 
3498 #ifdef CONFIG_MIDI_VIA82CXXX
3499 	/* Disable by default */
3500 	card->midi_info.io_base = 0;
3501 
3502 	if(card->legacy)
3503 	{
3504 		pci_read_config_byte (pdev, 0x42, &r42);
3505 		/* Disable MIDI interrupt */
3506 		pci_write_config_byte (pdev, 0x42, r42 | VIA_CR42_MIDI_IRQMASK);
3507 		if (r42 & VIA_CR42_MIDI_ENABLE)
3508 		{
3509 			if (r42 & VIA_CR42_MIDI_PNP) /* Address selected by iobase 2 - not tested */
3510 				card->midi_info.io_base = pci_resource_start (pdev, 2);
3511 			else /* Address selected by byte 0x43 */
3512 			{
3513 				u8 r43;
3514 				pci_read_config_byte (pdev, 0x43, &r43);
3515 				card->midi_info.io_base = 0x300 + ((r43 & 0x0c) << 2);
3516 			}
3517 
3518 			card->midi_info.irq = -pdev->irq;
3519 			if (probe_uart401(& card->midi_info, THIS_MODULE))
3520 			{
3521 				card->midi_devc=midi_devs[card->midi_info.slots[4]]->devc;
3522 				pci_write_config_byte(pdev, 0x42, r42 & ~VIA_CR42_MIDI_IRQMASK);
3523 				printk("Enabled Via MIDI\n");
3524 			}
3525 		}
3526 	}
3527 #endif
3528 
3529 	DPRINTK ("EXIT, returning 0\n");
3530 	return 0;
3531 
3532 err_out_have_proc:
3533 	via_card_cleanup_proc (card);
3534 
3535 err_out_have_dsp:
3536 	via_dsp_cleanup (card);
3537 
3538 err_out_have_mixer:
3539 	via_ac97_cleanup (card);
3540 
3541 err_out_kfree:
3542 #ifndef VIA_NDEBUG
3543 	memset (card, 0xAB, sizeof (*card)); /* poison memory */
3544 #endif
3545 	kfree (card);
3546 
3547 err_out_res:
3548 	pci_release_regions (pdev);
3549 
3550 err_out_disable:
3551 	pci_disable_device (pdev);
3552 
3553 err_out:
3554 	pci_set_drvdata (pdev, NULL);
3555 	DPRINTK ("EXIT - returning %d\n", rc);
3556 	return rc;
3557 }
3558 
3559 
via_remove_one(struct pci_dev * pdev)3560 static void __devexit via_remove_one (struct pci_dev *pdev)
3561 {
3562 	struct via_info *card;
3563 
3564 	DPRINTK ("ENTER\n");
3565 
3566 	assert (pdev != NULL);
3567 	card = pci_get_drvdata (pdev);
3568 	assert (card != NULL);
3569 
3570 #ifdef CONFIG_MIDI_VIA82CXXX
3571 	if (card->midi_info.io_base)
3572 		unload_uart401(&card->midi_info);
3573 #endif
3574 
3575 	free_irq (card->pdev->irq, card);
3576 	via_card_cleanup_proc (card);
3577 	via_dsp_cleanup (card);
3578 	via_ac97_cleanup (card);
3579 
3580 #ifndef VIA_NDEBUG
3581 	memset (card, 0xAB, sizeof (*card)); /* poison memory */
3582 #endif
3583 	kfree (card);
3584 
3585 	pci_set_drvdata (pdev, NULL);
3586 
3587 	pci_release_regions (pdev);
3588 	pci_disable_device (pdev);
3589 	pci_set_power_state (pdev, 3); /* ...zzzzzz */
3590 
3591 	DPRINTK ("EXIT\n");
3592 	return;
3593 }
3594 
3595 
3596 /****************************************************************
3597  *
3598  * Driver initialization and cleanup
3599  *
3600  *
3601  */
3602 
init_via82cxxx_audio(void)3603 static int __init init_via82cxxx_audio(void)
3604 {
3605 	int rc;
3606 
3607 	DPRINTK ("ENTER\n");
3608 
3609 	rc = via_init_proc ();
3610 	if (rc) {
3611 		DPRINTK ("EXIT, returning %d\n", rc);
3612 		return rc;
3613 	}
3614 
3615 	rc = pci_register_driver (&via_driver);
3616 	if (rc < 1) {
3617 		if (rc == 0)
3618 			pci_unregister_driver (&via_driver);
3619 		via_cleanup_proc ();
3620 		DPRINTK ("EXIT, returning -ENODEV\n");
3621 		return -ENODEV;
3622 	}
3623 
3624 	DPRINTK ("EXIT, returning 0\n");
3625 	return 0;
3626 }
3627 
3628 
cleanup_via82cxxx_audio(void)3629 static void __exit cleanup_via82cxxx_audio(void)
3630 {
3631 	DPRINTK ("ENTER\n");
3632 
3633 	pci_unregister_driver (&via_driver);
3634 	via_cleanup_proc ();
3635 
3636 	DPRINTK ("EXIT\n");
3637 }
3638 
3639 
3640 module_init(init_via82cxxx_audio);
3641 module_exit(cleanup_via82cxxx_audio);
3642 
3643 MODULE_AUTHOR("Jeff Garzik");
3644 MODULE_DESCRIPTION("DSP audio and mixer driver for Via 82Cxxx audio devices");
3645 MODULE_LICENSE("GPL");
3646 
3647 EXPORT_NO_SYMBOLS;
3648 
3649 
3650 
3651 #ifdef CONFIG_PROC_FS
3652 
3653 /****************************************************************
3654  *
3655  * /proc/driver/via/info
3656  *
3657  *
3658  */
3659 
via_info_read_proc(char * page,char ** start,off_t off,int count,int * eof,void * data)3660 static int via_info_read_proc (char *page, char **start, off_t off,
3661 			       int count, int *eof, void *data)
3662 {
3663 #define YN(val,bit) (((val) & (bit)) ? "yes" : "no")
3664 #define ED(val,bit) (((val) & (bit)) ? "enable" : "disable")
3665 
3666 	int len = 0;
3667 	u8 r40, r41, r42, r44;
3668 	struct via_info *card = data;
3669 
3670 	DPRINTK ("ENTER\n");
3671 
3672 	assert (card != NULL);
3673 
3674 	len += sprintf (page+len, VIA_CARD_NAME "\n\n");
3675 
3676 	pci_read_config_byte (card->pdev, 0x40, &r40);
3677 	pci_read_config_byte (card->pdev, 0x41, &r41);
3678 	pci_read_config_byte (card->pdev, 0x42, &r42);
3679 	pci_read_config_byte (card->pdev, 0x44, &r44);
3680 
3681 	len += sprintf (page+len,
3682 		"Via 82Cxxx PCI registers:\n"
3683 		"\n"
3684 		"40  Codec Ready: %s\n"
3685 		"    Codec Low-power: %s\n"
3686 		"    Secondary Codec Ready: %s\n"
3687 		"\n"
3688 		"41  Interface Enable: %s\n"
3689 		"    De-Assert Reset: %s\n"
3690 		"    Force SYNC high: %s\n"
3691 		"    Force SDO high: %s\n"
3692 		"    Variable Sample Rate On-Demand Mode: %s\n"
3693 		"    SGD Read Channel PCM Data Out: %s\n"
3694 		"    FM Channel PCM Data Out: %s\n"
3695 		"    SB PCM Data Out: %s\n"
3696 		"\n"
3697 		"42  Game port enabled: %s\n"
3698 		"    SoundBlaster enabled: %s\n"
3699 		"    FM enabled: %s\n"
3700 		"    MIDI enabled: %s\n"
3701 		"\n"
3702 		"44  AC-Link Interface Access: %s\n"
3703 		"    Secondary Codec Support: %s\n"
3704 
3705 		"\n",
3706 
3707 		YN (r40, VIA_CR40_AC97_READY),
3708 		YN (r40, VIA_CR40_AC97_LOW_POWER),
3709 		YN (r40, VIA_CR40_SECONDARY_READY),
3710 
3711 		ED (r41, VIA_CR41_AC97_ENABLE),
3712 		YN (r41, (1 << 6)),
3713 		YN (r41, (1 << 5)),
3714 		YN (r41, (1 << 4)),
3715 		ED (r41, (1 << 3)),
3716 		ED (r41, (1 << 2)),
3717 		ED (r41, (1 << 1)),
3718 		ED (r41, (1 << 0)),
3719 
3720 		YN (r42, VIA_CR42_GAME_ENABLE),
3721 		YN (r42, VIA_CR42_SB_ENABLE),
3722 		YN (r42, VIA_CR42_FM_ENABLE),
3723 		YN (r42, VIA_CR42_MIDI_ENABLE),
3724 
3725 		YN (r44, VIA_CR44_AC_LINK_ACCESS),
3726 		YN (r44, VIA_CR44_SECOND_CODEC_SUPPORT)
3727 
3728 		);
3729 
3730 	DPRINTK ("EXIT, returning %d\n", len);
3731 	return len;
3732 
3733 #undef YN
3734 #undef ED
3735 }
3736 
3737 
3738 /****************************************************************
3739  *
3740  * /proc/driver/via/... setup and cleanup
3741  *
3742  *
3743  */
3744 
via_init_proc(void)3745 static int __init via_init_proc (void)
3746 {
3747 	DPRINTK ("ENTER\n");
3748 
3749 	if (!proc_mkdir ("driver/via", 0))
3750 		return -EIO;
3751 
3752 	DPRINTK ("EXIT, returning 0\n");
3753 	return 0;
3754 }
3755 
3756 
via_cleanup_proc(void)3757 static void via_cleanup_proc (void)
3758 {
3759 	DPRINTK ("ENTER\n");
3760 
3761 	remove_proc_entry ("driver/via", NULL);
3762 
3763 	DPRINTK ("EXIT\n");
3764 }
3765 
3766 
via_card_init_proc(struct via_info * card)3767 static int __init via_card_init_proc (struct via_info *card)
3768 {
3769 	char s[32];
3770 	int rc;
3771 
3772 	DPRINTK ("ENTER\n");
3773 
3774 	sprintf (s, "driver/via/%d", card->card_num);
3775 	if (!proc_mkdir (s, 0)) {
3776 		rc = -EIO;
3777 		goto err_out_none;
3778 	}
3779 
3780 	sprintf (s, "driver/via/%d/info", card->card_num);
3781 	if (!create_proc_read_entry (s, 0, 0, via_info_read_proc, card)) {
3782 		rc = -EIO;
3783 		goto err_out_dir;
3784 	}
3785 
3786 	sprintf (s, "driver/via/%d/ac97", card->card_num);
3787 	if (!create_proc_read_entry (s, 0, 0, ac97_read_proc, card->ac97)) {
3788 		rc = -EIO;
3789 		goto err_out_info;
3790 	}
3791 
3792 	DPRINTK ("EXIT, returning 0\n");
3793 	return 0;
3794 
3795 err_out_info:
3796 	sprintf (s, "driver/via/%d/info", card->card_num);
3797 	remove_proc_entry (s, NULL);
3798 
3799 err_out_dir:
3800 	sprintf (s, "driver/via/%d", card->card_num);
3801 	remove_proc_entry (s, NULL);
3802 
3803 err_out_none:
3804 	DPRINTK ("EXIT, returning %d\n", rc);
3805 	return rc;
3806 }
3807 
3808 
via_card_cleanup_proc(struct via_info * card)3809 static void via_card_cleanup_proc (struct via_info *card)
3810 {
3811 	char s[32];
3812 
3813 	DPRINTK ("ENTER\n");
3814 
3815 	sprintf (s, "driver/via/%d/ac97", card->card_num);
3816 	remove_proc_entry (s, NULL);
3817 
3818 	sprintf (s, "driver/via/%d/info", card->card_num);
3819 	remove_proc_entry (s, NULL);
3820 
3821 	sprintf (s, "driver/via/%d", card->card_num);
3822 	remove_proc_entry (s, NULL);
3823 
3824 	DPRINTK ("EXIT\n");
3825 }
3826 
3827 #endif /* CONFIG_PROC_FS */
3828