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