2 * Support for VIA 82Cxxx Audio Codecs
3 * Copyright 1999,2000 Jeff Garzik <jgarzik@mandrakesoft.com>
5 * Distributed under the GNU GENERAL PUBLIC LICENSE (GPL) Version 2.
6 * See the "COPYING" file distributed with this software for more info.
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/
18 #define VIA_VERSION "1.1.8"
21 #include <linux/config.h>
22 #include <linux/module.h>
23 #include <linux/kernel.h>
25 #include <linux/pci.h>
26 #include <linux/init.h>
27 #include <linux/proc_fs.h>
28 #include <linux/spinlock.h>
29 #include <linux/sound.h>
30 #include <linux/poll.h>
31 #include <linux/soundcard.h>
32 #include <linux/ac97_codec.h>
33 #include <linux/smp_lock.h>
35 #include <asm/delay.h>
36 #include <asm/uaccess.h>
37 #include <asm/hardirq.h>
40 #undef VIA_DEBUG /* define to enable debugging output and checks */
42 /* note: prints function name for you */
43 #define DPRINTK(fmt, args...) printk(KERN_DEBUG "%s: " fmt, __FUNCTION__ , ## args)
45 #define DPRINTK(fmt, args...)
48 #define VIA_NDEBUG /* define to disable lightweight runtime checks */
52 #define assert(expr) \
54 printk( "Assertion failed! %s,%s,%s,line=%d\n", \
55 #expr,__FILE__,__FUNCTION__,__LINE__); \
59 /* user switch: undefine to exclude /proc data */
62 /* don't mess with this */
63 #ifndef CONFIG_PROC_FS
67 #define arraysize(x) (sizeof(x)/sizeof(*(x)))
73 #define VIA_CARD_NAME "VIA 82Cxxx Audio driver " VIA_VERSION
74 #define VIA_MODULE_NAME "via82cxxx"
75 #define PFX VIA_MODULE_NAME ": "
77 #define VIA_COUNTER_LIMIT 100000
79 /* size of DMA buffers */
80 #define VIA_DMA_BUFFERS 16
81 #define VIA_DMA_BUF_SIZE PAGE_SIZE
83 /* 82C686 function 5 (audio codec) PCI configuration registers */
84 #define VIA_ACLINK_CTRL 0x41
85 #define VIA_FUNC_ENABLE 0x42
86 #define VIA_PNP_CONTROL 0x43
87 #define VIA_FM_NMI_CTRL 0x48
90 * controller base 0 (scatter-gather) registers
92 * NOTE: Via datasheet lists first channel as "read"
93 * channel and second channel as "write" channel.
94 * I changed the naming of the constants to be more
95 * clear than I felt the datasheet to be.
98 #define VIA_BASE0_PCM_OUT_CHAN 0x00 /* output PCM to user */
99 #define VIA_BASE0_PCM_OUT_CHAN_STATUS 0x00
100 #define VIA_BASE0_PCM_OUT_CHAN_CTRL 0x01
101 #define VIA_BASE0_PCM_OUT_CHAN_TYPE 0x02
102 #define VIA_BASE0_PCM_OUT_BLOCK_COUNT 0x0C
104 #define VIA_BASE0_PCM_IN_CHAN 0x10 /* input PCM from user */
105 #define VIA_BASE0_PCM_IN_CHAN_STATUS 0x10
106 #define VIA_BASE0_PCM_IN_CHAN_CTRL 0x11
107 #define VIA_BASE0_PCM_IN_CHAN_TYPE 0x12
109 /* offsets from base */
110 #define VIA_PCM_STATUS 0x00
111 #define VIA_PCM_CONTROL 0x01
112 #define VIA_PCM_TYPE 0x02
113 #define VIA_PCM_TABLE_ADDR 0x04
115 /* XXX unused DMA channel for FM PCM data */
116 #define VIA_BASE0_FM_OUT_CHAN 0x20
117 #define VIA_BASE0_FM_OUT_CHAN_STATUS 0x20
118 #define VIA_BASE0_FM_OUT_CHAN_CTRL 0x21
119 #define VIA_BASE0_FM_OUT_CHAN_TYPE 0x22
121 #define VIA_BASE0_AC97_CTRL 0x80
122 #define VIA_BASE0_SGD_STATUS_SHADOW 0x84
123 #define VIA_BASE0_GPI_INT_ENABLE 0x8C
125 /* VIA_BASE0_AUDIO_xxx_CHAN_TYPE bits */
126 #define VIA_IRQ_ON_FLAG (1<<0) /* int on each flagged scatter block */
127 #define VIA_IRQ_ON_EOL (1<<1) /* int at end of scatter list */
128 #define VIA_INT_SEL_PCI_LAST_LINE_READ (0) /* int at PCI read of last line */
129 #define VIA_INT_SEL_LAST_SAMPLE_SENT (1<<2) /* int at last sample sent */
130 #define VIA_INT_SEL_ONE_LINE_LEFT (1<<3) /* int at less than one line to send */
131 #define VIA_PCM_FMT_STEREO (1<<4) /* PCM stereo format (bit clear == mono) */
132 #define VIA_PCM_FMT_16BIT (1<<5) /* PCM 16-bit format (bit clear == 8-bit) */
133 #define VIA_PCM_FIFO (1<<6) /* enable FIFO? documented as "reserved" */
134 #define VIA_RESTART_SGD_ON_EOL (1<<7) /* restart scatter-gather at EOL */
135 #define VIA_PCM_FMT_MASK (VIA_PCM_FMT_STEREO|VIA_PCM_FMT_16BIT)
136 #define VIA_CHAN_TYPE_MASK (VIA_RESTART_SGD_ON_EOL | \
140 #define VIA_CHAN_TYPE_INT_SELECT (VIA_INT_SEL_LAST_SAMPLE_SENT)
142 /* PCI configuration register bits and masks */
143 #define VIA_CR40_AC97_READY 0x01
144 #define VIA_CR40_AC97_LOW_POWER 0x02
145 #define VIA_CR40_SECONDARY_READY 0x04
147 #define VIA_CR41_AC97_ENABLE 0x80 /* enable AC97 codec */
148 #define VIA_CR41_AC97_RESET 0x40 /* clear bit to reset AC97 */
149 #define VIA_CR41_AC97_WAKEUP 0x20 /* wake up from power-down mode */
150 #define VIA_CR41_AC97_SDO 0x10 /* force Serial Data Out (SDO) high */
151 #define VIA_CR41_VRA 0x08 /* enable variable sample rate */
152 #define VIA_CR41_PCM_ENABLE 0x04 /* AC Link SGD Read Channel PCM Data Output */
153 #define VIA_CR41_FM_PCM_ENABLE 0x02 /* AC Link FM Channel PCM Data Out */
154 #define VIA_CR41_SB_PCM_ENABLE 0x01 /* AC Link SB PCM Data Output */
155 #define VIA_CR41_BOOT_MASK (VIA_CR41_AC97_ENABLE | \
156 VIA_CR41_AC97_WAKEUP | \
158 #define VIA_CR41_RUN_MASK (VIA_CR41_AC97_ENABLE | \
159 VIA_CR41_AC97_RESET | \
163 #define VIA_CR42_SB_ENABLE 0x01
164 #define VIA_CR42_MIDI_ENABLE 0x02
165 #define VIA_CR42_FM_ENABLE 0x04
166 #define VIA_CR42_GAME_ENABLE 0x08
168 #define VIA_CR44_SECOND_CODEC_SUPPORT (1 << 6)
169 #define VIA_CR44_AC_LINK_ACCESS (1 << 7)
171 #define VIA_CR48_FM_TRAP_TO_NMI (1 << 2)
173 /* controller base 0 register bitmasks */
174 #define VIA_INT_DISABLE_MASK (~(0x01|0x02))
175 #define VIA_SGD_STOPPED (1 << 2)
176 #define VIA_SGD_ACTIVE (1 << 7)
177 #define VIA_SGD_TERMINATE (1 << 6)
178 #define VIA_SGD_FLAG (1 << 0)
179 #define VIA_SGD_EOL (1 << 1)
180 #define VIA_SGD_START (1 << 7)
182 #define VIA_CR80_FIRST_CODEC 0
183 #define VIA_CR80_SECOND_CODEC (1 << 30)
184 #define VIA_CR80_FIRST_CODEC_VALID (1 << 25)
185 #define VIA_CR80_VALID (1 << 25)
186 #define VIA_CR80_SECOND_CODEC_VALID (1 << 27)
187 #define VIA_CR80_BUSY (1 << 24)
188 #define VIA_CR83_BUSY (1)
189 #define VIA_CR83_FIRST_CODEC_VALID (1 << 1)
190 #define VIA_CR80_READ (1 << 23)
191 #define VIA_CR80_WRITE_MODE 0
192 #define VIA_CR80_REG_IDX(idx) ((((idx) & 0xFF) >> 1) << 16)
194 /* h r puff n stuff */
195 #define VIA_FMT_STEREO 0x01
196 #define VIA_FMT_16BIT 0x02
197 #define VIA_FMT_MASK 0x03
198 #define VIA_DAC_SHIFT 0
199 #define VIA_ADC_SHIFT 4
201 /* undocumented(?) values for setting rate, from Via's source */
202 #define VIA_SET_RATE_IN 0x00320000 /* set input rate */
203 #define VIA_SET_RATE_OUT 0x002c0000 /* set output rate */
206 /* scatter-gather DMA table entry, exactly as passed to hardware */
207 struct via_sgd_table
{
209 u32 count
; /* includes additional bits also */
211 #define VIA_EOL (1 << 31)
212 #define VIA_FLAG (1 << 30)
215 enum via_channel_states
{
221 struct via_sgd_data
{
223 volatile void *cpuaddr
;
228 unsigned rate
; /* sample rate */
230 u8 pcm_fmt
; /* VIA_PCM_FMT_xxx */
236 volatile struct via_sgd_table
*sgtable
;
237 dma_addr_t sgt_handle
;
239 struct via_sgd_data sgbuf
[VIA_DMA_BUFFERS
];
241 wait_queue_head_t wait
;
247 /* data stored for each chip */
249 struct pci_dev
*pdev
;
252 struct ac97_codec ac97
;
254 int card_num
; /* unique card number, from 0 */
256 int dev_dsp
; /* /dev/dsp index from register_sound_dsp() */
260 wait_queue_head_t open_wait
;
263 struct via_channel ch_in
;
264 struct via_channel ch_out
;
268 /* number of cards, used for assigning unique numbers to cards */
269 static unsigned via_num_cards
= 0;
272 /****************************************************************
279 static int via_init_one (struct pci_dev
*dev
, const struct pci_device_id
*id
);
280 static void via_remove_one (struct pci_dev
*pdev
);
282 static ssize_t
via_dsp_read(struct file
*file
, char *buffer
, size_t count
, loff_t
*ppos
);
283 static ssize_t
via_dsp_write(struct file
*file
, const char *buffer
, size_t count
, loff_t
*ppos
);
284 static unsigned int via_dsp_poll(struct file
*file
, struct poll_table_struct
*wait
);
285 static int via_dsp_ioctl (struct inode
*inode
, struct file
*file
, unsigned int cmd
, unsigned long arg
);
286 static int via_dsp_open (struct inode
*inode
, struct file
*file
);
287 static int via_dsp_release(struct inode
*inode
, struct file
*file
);
289 static u16
via_ac97_read_reg (struct ac97_codec
*codec
, u8 reg
);
290 static void via_ac97_write_reg (struct ac97_codec
*codec
, u8 reg
, u16 value
);
291 static u8
via_ac97_wait_idle (struct via_info
*card
);
293 static void via_chan_free (struct via_info
*card
, struct via_channel
*chan
);
294 static void via_chan_clear (struct via_channel
*chan
);
295 static void via_chan_pcm_fmt (struct via_info
*card
,
296 struct via_channel
*chan
, int reset
);
299 /****************************************************************
301 * Various data the driver needs
307 static struct pci_device_id via_pci_tbl
[] __initdata
= {
308 { PCI_VENDOR_ID_VIA
, PCI_DEVICE_ID_VIA_82C686_5
, PCI_ANY_ID
, PCI_ANY_ID
, },
311 MODULE_DEVICE_TABLE(pci
,via_pci_tbl
);
314 static struct pci_driver via_driver
= {
315 name
: VIA_MODULE_NAME
,
316 id_table
: via_pci_tbl
,
318 remove
: via_remove_one
,
322 /****************************************************************
324 * Low-level base 0 register read/write helpers
330 * via_chan_stop - Terminate DMA on specified PCM channel
331 * @iobase: PCI base address for SGD channel registers
333 * Terminate scatter-gather DMA operation for given
334 * channel (derived from @iobase), if DMA is active.
336 * Note that @iobase is not the PCI base address,
337 * but the PCI base address plus an offset to
338 * one of three PCM channels supported by the chip.
342 static inline void via_chan_stop (int iobase
)
344 if (inb (iobase
+ VIA_PCM_STATUS
) & VIA_SGD_ACTIVE
)
345 outb (VIA_SGD_TERMINATE
, iobase
+ VIA_PCM_CONTROL
);
350 * via_chan_status_clear - Clear status flags on specified DMA channel
351 * @iobase: PCI base address for SGD channel registers
353 * Clear any pending status flags for the given
354 * DMA channel (derived from @iobase), if any
355 * flags are asserted.
357 * Note that @iobase is not the PCI base address,
358 * but the PCI base address plus an offset to
359 * one of three PCM channels supported by the chip.
363 static inline void via_chan_status_clear (int iobase
)
365 u8 tmp
= inb (iobase
+ VIA_PCM_STATUS
);
368 outb (tmp
, iobase
+ VIA_PCM_STATUS
);
373 * sg_begin - Begin recording or playback on a PCM channel
374 * @chan: Channel for which DMA operation shall begin
376 * Start scatter-gather DMA for the given channel.
380 static inline void sg_begin (struct via_channel
*chan
)
382 outb (VIA_SGD_START
, chan
->iobase
+ VIA_PCM_CONTROL
);
387 * via_chan_bufs_in_use - Number of buffers waiting to be consumed
388 * @chan: Channel for which DMA buffers will be counted
390 * Count the number of buffers waiting to be consumed. For a
391 * playback operation, this is the number of buffers which have
392 * yet to be sent to the DAC. For a recording operation, this
393 * is the number of buffers waiting to be consumed by software
394 * calling read() system call.
398 static inline int via_chan_bufs_in_use (struct via_channel
*chan
)
400 return atomic_read(&chan
->next_buf
) -
401 atomic_read(&chan
->buf_in_use
);
406 * via_chan_full - Check for no-free-buffers condition
407 * @chan: Channel for which DMA full condition will be checked
409 * Count the number of buffers waiting to be consumed, and return
410 * true (non-zero) if no buffers are available to be filled on the
415 static inline int via_chan_full (struct via_channel
*chan
)
417 return (via_chan_bufs_in_use (chan
) == VIA_DMA_BUFFERS
);
422 * via_chan_empty - Check for no-buffers-in-use condition
423 * @chan: Channel for which DMA empty condition will be checked
425 * Count the number of buffers waiting to be consumed, and return
426 * true (non-zero) if no buffers are currently in use.
430 static inline int via_chan_empty (struct via_channel
*chan
)
432 return (atomic_read(&chan
->next_buf
) ==
433 atomic_read(&chan
->buf_in_use
));
437 /****************************************************************
439 * Miscellaneous debris
446 * via_stop_everything - Stop all audio operations
447 * @card: Private info for specified board
449 * Stops all DMA operations and interrupts, and clear
450 * any pending status bits resulting from those operations.
453 static void via_stop_everything (struct via_info
*card
)
457 assert (card
!= NULL
);
460 * terminate any existing operations on audio read/write channels
462 via_chan_stop (card
->baseaddr
+ VIA_BASE0_PCM_OUT_CHAN
);
463 via_chan_stop (card
->baseaddr
+ VIA_BASE0_PCM_IN_CHAN
);
464 via_chan_stop (card
->baseaddr
+ VIA_BASE0_FM_OUT_CHAN
);
467 * clear any existing stops / flags (sanity check mainly)
469 via_chan_status_clear (card
->baseaddr
+ VIA_BASE0_PCM_OUT_CHAN
);
470 via_chan_status_clear (card
->baseaddr
+ VIA_BASE0_PCM_IN_CHAN
);
471 via_chan_status_clear (card
->baseaddr
+ VIA_BASE0_FM_OUT_CHAN
);
474 * clear any enabled interrupt bits, reset to 8-bit mono PCM mode
476 outb (0, card
->baseaddr
+ VIA_BASE0_PCM_OUT_CHAN
);
477 outb (0, card
->baseaddr
+ VIA_BASE0_PCM_IN_CHAN
);
478 outb (0, card
->baseaddr
+ VIA_BASE0_FM_OUT_CHAN
);
484 * via_set_rate - Set PCM rate for given channel
485 * @card: Private info for specified board
486 * @rate: Desired PCM sample rate, in Khz
487 * @inhale_deeply: Boolean. If non-zero (true), the recording sample rate
488 * is set. If zero (false), the playback sample rate
491 * Sets the PCM sample rate for a channel.
493 * Values for @rate are clamped to a range of 4000 Khz through 48000 Khz,
494 * due to hardware constraints.
496 * FIXME: @inhale_deeply argument is ignored, and %AC97_PCM_FRONT_DAC_RATE
497 * is the only rate which is really set. This needs to be fixed when
498 * recording support is added.
501 static int via_set_rate (struct via_info
*card
, unsigned rate
, int inhale_deeply
)
504 DPRINTK ("ENTER, rate = %d, inhale = %s\n",
505 rate
, inhale_deeply
? "yes" : "no");
507 if (rate
> 48000) rate
= 48000;
508 if (rate
< 4000) rate
= 4000;
510 via_ac97_write_reg (&card
->ac97
, AC97_POWER_CONTROL
,
511 (via_ac97_read_reg (&card
->ac97
, AC97_POWER_CONTROL
) & ~0x0200) |
513 via_ac97_write_reg (&card
->ac97
, AC97_PCM_FRONT_DAC_RATE
, rate
);
514 via_ac97_write_reg (&card
->ac97
, AC97_POWER_CONTROL
,
515 via_ac97_read_reg (&card
->ac97
, AC97_POWER_CONTROL
) & ~0x0200);
517 DPRINTK ("EXIT, returning 0\n");
523 * via_set_adc_rate - Set PCM rate for recording channel
524 * @card: Private info for specified board
525 * @rate: Desired PCM sample rate, in Khz
527 * Sets the PCM sample rate for a recording channel.
529 * FIXME: @inhale_deeply argument to via_set_rate is ignored, and %AC97_PCM_FRONT_DAC_RATE
530 * is the only rate which is really set. Thus, this function will
531 * not work until via_set_rate is fixed.
534 static inline int via_set_adc_rate (struct via_info
*card
, int rate
)
536 return via_set_rate (card
, rate
, 1);
541 * via_set_dac_rate - Set PCM rate for playback channel
542 * @card: Private info for specified board
543 * @rate: Desired PCM sample rate, in Khz
545 * Sets the PCM sample rate for a playback channel.
548 static inline int via_set_dac_rate (struct via_info
*card
, int rate
)
550 return via_set_rate (card
, rate
, 0);
554 /****************************************************************
556 * Channel-specific operations
562 * via_chan_init - Initialize PCM channel
563 * @card: Private audio chip info
564 * @chan: Channel to be initialized
565 * @chan_ofs: Offset from PCI address, which determines the
566 * set of SGD registers to use.
568 * Performs all the preparations necessary to begin
569 * using a PCM channel.
571 * Currently the preparations include allocating the
572 * scatter-gather DMA table and buffers, setting the
573 * PCM channel to a known state, and passing the
574 * address of the DMA table to the hardware.
576 * Note that special care is taken when passing the
577 * DMA table address to hardware, because it was found
578 * during driver development that the hardware did not
579 * always "take" the address.
582 static int via_chan_init (struct via_info
*card
,
583 struct via_channel
*chan
, long chan_ofs
)
590 memset (chan
, 0, sizeof (*chan
));
592 /* alloc DMA-able memory for scatter-gather table */
593 chan
->sgtable
= pci_alloc_consistent (card
->pdev
,
594 (sizeof (struct via_sgd_table
) * VIA_DMA_BUFFERS
),
596 if (!chan
->sgtable
) {
597 printk (KERN_ERR PFX
"DMA table alloc fail, aborting\n");
602 memset ((void*)chan
->sgtable
, 0,
603 (sizeof (struct via_sgd_table
) * VIA_DMA_BUFFERS
));
605 /* alloc DMA-able memory for scatter-gather buffers */
606 for (i
= 0; i
< VIA_DMA_BUFFERS
; i
++) {
607 chan
->sgbuf
[i
].cpuaddr
=
608 pci_alloc_consistent (card
->pdev
, VIA_DMA_BUF_SIZE
,
609 &chan
->sgbuf
[i
].handle
);
611 if (!chan
->sgbuf
[i
].cpuaddr
)
614 if (i
< (VIA_DMA_BUFFERS
- 1))
615 chan
->sgtable
[i
].count
= cpu_to_le32 (VIA_DMA_BUF_SIZE
| VIA_FLAG
);
617 chan
->sgtable
[i
].count
= cpu_to_le32 (VIA_DMA_BUF_SIZE
| VIA_EOL
);
618 chan
->sgtable
[i
].addr
= cpu_to_le32 (chan
->sgbuf
[i
].handle
);
621 memset (chan
->sgbuf
[i
].cpuaddr
, 0xBC, VIA_DMA_BUF_SIZE
);
625 DPRINTK ("dmabuf #%d (h=%lx, 32(h)=%lx, v2p=%lx, a=%p)\n",
626 i
, (long)chan
->sgbuf
[i
].handle
,
627 (long)chan
->sgtable
[i
].addr
,
628 virt_to_phys(chan
->sgbuf
[i
].cpuaddr
),
629 chan
->sgbuf
[i
].cpuaddr
);
633 init_waitqueue_head (&chan
->wait
);
635 chan
->pcm_fmt
= VIA_PCM_FMT_MASK
;
636 chan
->iobase
= card
->baseaddr
+ chan_ofs
;
638 spin_lock_irqsave (&card
->lock
, flags
);
640 /* stop any existing channel output */
641 via_chan_clear (chan
);
642 via_chan_status_clear (chan
->iobase
);
643 via_chan_pcm_fmt (card
, chan
, 1);
645 spin_unlock_irqrestore (&card
->lock
, flags
);
647 /* set location of DMA-able scatter-gather info table */
648 DPRINTK("outl (0x%X, 0x%04lX)\n",
649 cpu_to_le32 (chan
->sgt_handle
),
650 card
->baseaddr
+ chan_ofs
+ VIA_PCM_TABLE_ADDR
);
652 via_ac97_wait_idle (card
);
653 outl (cpu_to_le32 (chan
->sgt_handle
),
654 card
->baseaddr
+ chan_ofs
+ VIA_PCM_TABLE_ADDR
);
656 via_ac97_wait_idle (card
);
658 DPRINTK("inl (0x%lX) = %x\n",
659 chan
->iobase
+ VIA_PCM_TABLE_ADDR
,
660 inl(chan
->iobase
+ VIA_PCM_TABLE_ADDR
));
666 printk (KERN_ERR PFX
"DMA buffer alloc fail, aborting\n");
667 via_chan_free (card
, chan
);
674 * via_chan_free - Release a PCM channel
675 * @card: Private audio chip info
676 * @chan: Channel to be released
678 * Performs all the functions necessary to clean up
679 * an initialized channel.
681 * Currently these functions include disabled any
682 * active DMA operations, setting the PCM channel
683 * back to a known state, and releasing any allocated
687 static void via_chan_free (struct via_info
*card
, struct via_channel
*chan
)
696 spin_lock_irqsave (&card
->lock
, flags
);
698 /* stop any existing channel output */
699 via_chan_stop (chan
->iobase
);
700 via_chan_status_clear (chan
->iobase
);
701 via_chan_pcm_fmt (card
, chan
, 1);
703 spin_unlock_irqrestore (&card
->lock
, flags
);
705 /* zero location of DMA-able scatter-gather info table */
706 via_ac97_wait_idle(card
);
707 outl (0, chan
->iobase
+ VIA_PCM_TABLE_ADDR
);
709 for (i
= 0; i
< VIA_DMA_BUFFERS
; i
++)
710 if (chan
->sgbuf
[i
].cpuaddr
) {
711 pci_free_consistent (card
->pdev
, VIA_DMA_BUF_SIZE
,
712 (void*)chan
->sgbuf
[i
].cpuaddr
,
713 chan
->sgbuf
[i
].handle
);
714 chan
->sgbuf
[i
].cpuaddr
= NULL
;
715 chan
->sgbuf
[i
].handle
= 0;
719 pci_free_consistent (card
->pdev
,
720 (sizeof (struct via_sgd_table
) * VIA_DMA_BUFFERS
),
721 (void*)chan
->sgtable
, chan
->sgt_handle
);
722 chan
->sgtable
= NULL
;
730 * via_chan_pcm_fmt - Update PCM channel settings
731 * @card: Private audio chip info
732 * @chan: Channel to be updated
733 * @reset: Boolean. If non-zero, channel will be reset
734 * to 8-bit mono mode.
736 * Stores the settings of the current PCM format,
737 * 8-bit or 16-bit, and mono/stereo, into the
738 * hardware settings for the specified channel.
739 * If @reset is non-zero, the channel is reset
740 * to 8-bit mono mode. Otherwise, the channel
741 * is set to the values stored in the channel
742 * information struct @chan.
745 static void via_chan_pcm_fmt (struct via_info
*card
,
746 struct via_channel
*chan
, int reset
)
748 DPRINTK ("ENTER, pcm_fmt=0x%02X, reset=%s\n",
749 chan
->pcm_fmt
, reset
? "yes" : "no");
751 assert (card
!= NULL
);
752 assert (chan
!= NULL
);
755 /* reset to 8-bit mono mode */
758 /* enable interrupts on FLAG and EOL */
759 chan
->pcm_fmt
|= VIA_CHAN_TYPE_MASK
;
761 /* set interrupt select bits where applicable (PCM & FM out channels) */
762 if (chan
== &card
->ch_out
)
763 chan
->pcm_fmt
|= VIA_CHAN_TYPE_INT_SELECT
;
765 outb (chan
->pcm_fmt
, chan
->iobase
+ 2);
767 DPRINTK ("EXIT, pcm_fmt = 0x%02X, reg = 0x%02X\n",
769 inb (chan
->iobase
+ 2));
774 * via_chan_clear - Stop DMA channel operation, and reset pointers
775 * @chan: Channel to be cleared
777 * Call via_chan_stop to halt DMA operations, and then resets
778 * all software pointers which track DMA operation.
781 static void via_chan_clear (struct via_channel
*chan
)
783 via_chan_stop (chan
->iobase
);
784 atomic_set (&chan
->state
, sgd_stopped
);
785 atomic_set (&chan
->buf_in_use
, 0);
786 atomic_set (&chan
->next_buf
, 0);
791 * via_chan_set_speed - Set PCM sample rate for given channel
792 * @card: Private info for specified board
793 * @chan: Channel whose sample rate will be adjusted
794 * @val: New sample rate, in Khz
796 * Helper function for the %SNDCTL_DSP_SPEED ioctl. OSS semantics
797 * demand that all audio operations halt (if they are not already
798 * halted) when the %SNDCTL_DSP_SPEED is given.
800 * This function halts all audio operations for the given channel
801 * @chan, and then calls via_set_rate to set the audio hardware
805 static int via_chan_set_speed (struct via_info
*card
,
806 struct via_channel
*chan
, int val
)
808 DPRINTK ("ENTER, requested rate = %d\n", val
);
810 via_chan_clear (chan
);
812 val
= via_set_rate (card
, val
, chan
== &card
->ch_in
);
814 DPRINTK ("EXIT, returning %d\n", val
);
820 * via_chan_set_fmt - Set PCM sample size for given channel
821 * @card: Private info for specified board
822 * @chan: Channel whose sample size will be adjusted
823 * @val: New sample size, use the %AFMT_xxx constants
825 * Helper function for the %SNDCTL_DSP_SETFMT ioctl. OSS semantics
826 * demand that all audio operations halt (if they are not already
827 * halted) when the %SNDCTL_DSP_SETFMT is given.
829 * This function halts all audio operations for the given channel
830 * @chan, and then calls via_chan_pcm_fmt to set the audio hardware
831 * to the new sample size, either 8-bit or 16-bit.
834 static int via_chan_set_fmt (struct via_info
*card
,
835 struct via_channel
*chan
, int val
)
837 DPRINTK ("ENTER, val=%s\n",
838 val
== AFMT_U8
? "AFMT_U8" :
839 val
== AFMT_S16_LE
? "AFMT_S16_LE" :
842 via_chan_clear (chan
);
846 chan
->pcm_fmt
&= ~VIA_PCM_FMT_16BIT
;
847 via_chan_pcm_fmt (card
, chan
, 0);
850 chan
->pcm_fmt
|= VIA_PCM_FMT_16BIT
;
851 via_chan_pcm_fmt (card
, chan
, 0);
854 printk (KERN_WARNING PFX
"unknown AFMT\n");
859 DPRINTK ("EXIT, returning %d\n", val
);
865 * via_chan_set_stereo - Enable or disable stereo for a DMA channel
866 * @card: Private info for specified board
867 * @chan: Channel whose stereo setting will be adjusted
868 * @val: New sample size, use the %AFMT_xxx constants
870 * Helper function for the %SNDCTL_DSP_CHANNELS and %SNDCTL_DSP_STEREO ioctls. OSS semantics
871 * demand that all audio operations halt (if they are not already
872 * halted) when %SNDCTL_DSP_CHANNELS or SNDCTL_DSP_STEREO is given.
874 * This function halts all audio operations for the given channel
875 * @chan, and then calls via_chan_pcm_fmt to set the audio hardware
876 * to enable or disable stereo.
879 static int via_chan_set_stereo (struct via_info
*card
,
880 struct via_channel
*chan
, int val
)
882 DPRINTK ("ENTER, channels = %d\n", val
);
884 via_chan_clear (chan
);
890 chan
->pcm_fmt
&= ~VIA_PCM_FMT_STEREO
;
891 via_chan_pcm_fmt (card
, chan
, 0);
896 chan
->pcm_fmt
|= VIA_PCM_FMT_STEREO
;
897 via_chan_pcm_fmt (card
, chan
, 0);
902 printk (KERN_WARNING PFX
"unknown number of channels\n");
907 DPRINTK ("EXIT, returning %d\n", val
);
914 * via_chan_dump_bufs - Display DMA table contents
915 * @chan: Channel whose DMA table will be displayed
917 * Debugging function which displays the contents of the
918 * scatter-gather DMA table for the given channel @chan.
921 static void via_chan_dump_bufs (struct via_channel
*chan
)
925 for (i
= 0; i
< VIA_DMA_BUFFERS
; i
++) {
926 DPRINTK ("#%02d: addr=%x, count=%u, flag=%d, eol=%d\n",
927 i
, chan
->sgtable
[i
].addr
,
928 chan
->sgtable
[i
].count
& 0x00FFFFFF,
929 chan
->sgtable
[i
].count
& VIA_FLAG
? 1 : 0,
930 chan
->sgtable
[i
].count
& VIA_EOL
? 1 : 0);
932 DPRINTK ("buf_in_use = %d, nextbuf = %d\n",
933 atomic_read (&chan
->buf_in_use
),
934 atomic_read (&chan
->next_buf
));
939 /****************************************************************
941 * Interface to ac97-codec module
947 * via_ac97_wait_idle - Wait until AC97 codec is not busy
948 * @card: Private info for specified board
950 * Sleep until the AC97 codec is no longer busy.
951 * Returns the final value read from the SGD
952 * register being polled.
955 static u8
via_ac97_wait_idle (struct via_info
*card
)
958 int counter
= VIA_COUNTER_LIMIT
;
960 DPRINTK ("ENTER/EXIT\n");
962 assert (card
!= NULL
);
963 assert (card
->pdev
!= NULL
);
966 if (current
->need_resched
)
971 spin_lock_irq (&card
->lock
);
972 tmp8
= inb (card
->baseaddr
+ 0x83);
973 spin_unlock_irq (&card
->lock
);
974 } while ((tmp8
& VIA_CR83_BUSY
) && (counter
-- > 0));
981 * via_ac97_read_reg - Read AC97 standard register
982 * @codec: Pointer to generic AC97 codec info
983 * @reg: Index of AC97 register to be read
985 * Read the value of a single AC97 codec register,
986 * as defined by the Intel AC97 specification.
988 * Defines the standard AC97 read-register operation
989 * required by the kernel's ac97_codec interface.
991 * Returns the 16-bit value stored in the specified
995 static u16
via_ac97_read_reg (struct ac97_codec
*codec
, u8 reg
)
998 struct via_info
*card
;
1001 DPRINTK ("ENTER\n");
1003 assert (codec
!= NULL
);
1004 assert (codec
->private_data
!= NULL
);
1006 card
= codec
->private_data
;
1008 data
= (reg
<< 16) | VIA_CR80_READ
;
1010 outl (data
, card
->baseaddr
+ VIA_BASE0_AC97_CTRL
);
1013 for (counter
= VIA_COUNTER_LIMIT
; counter
> 0; counter
--) {
1014 if (inl (card
->baseaddr
+ 0x80) & VIA_CR80_VALID
)
1017 if (current
->need_resched
)
1021 printk (KERN_WARNING PFX
"timeout while reading AC97 codec\n");
1025 data
= inl (card
->baseaddr
+ 0x80);
1026 outb (0x02, card
->baseaddr
+ 0x83);
1028 if (((data
& 0x007F0000) >> 16) == reg
) {
1029 DPRINTK ("EXIT, success, data=0x%x, retval=0x%x\n",
1030 data
, data
& 0x0000FFFF);
1031 return data
& 0x0000FFFF;
1034 DPRINTK ("WARNING: not our index: reg=0x%x, newreg=0x%x\n",
1035 reg
, ((data
& 0x007F0000) >> 16));
1038 DPRINTK ("EXIT, returning 0\n");
1044 * via_ac97_write_reg - Write AC97 standard register
1045 * @codec: Pointer to generic AC97 codec info
1046 * @reg: Index of AC97 register to be written
1047 * @value: Value to be written to AC97 register
1049 * Write the value of a single AC97 codec register,
1050 * as defined by the Intel AC97 specification.
1052 * Defines the standard AC97 write-register operation
1053 * required by the kernel's ac97_codec interface.
1056 static void via_ac97_write_reg (struct ac97_codec
*codec
, u8 reg
, u16 value
)
1059 struct via_info
*card
;
1062 DPRINTK ("ENTER\n");
1064 assert (codec
!= NULL
);
1065 assert (codec
->private_data
!= NULL
);
1067 card
= codec
->private_data
;
1069 data
= (reg
<< 16) + value
;
1070 outl (data
, card
->baseaddr
+ VIA_BASE0_AC97_CTRL
);
1073 for (counter
= VIA_COUNTER_LIMIT
; counter
> 0; counter
--) {
1074 if ((inb (card
->baseaddr
+ 0x83) & VIA_CR83_BUSY
) == 0)
1077 if (current
->need_resched
)
1081 printk (KERN_WARNING PFX
"timeout after AC97 codec write\n");
1088 static int via_mixer_open (struct inode
*inode
, struct file
*file
)
1090 int minor
= MINOR(inode
->i_rdev
);
1091 struct via_info
*card
;
1092 struct pci_dev
*pdev
;
1093 struct pci_driver
*drvr
;
1095 DPRINTK ("ENTER\n");
1097 pci_for_each_dev(pdev
) {
1098 drvr
= pci_dev_driver (pdev
);
1099 if (drvr
== &via_driver
) {
1100 assert (pdev
->driver_data
!= NULL
);
1102 card
= pdev
->driver_data
;
1103 if (card
->ac97
.dev_mixer
== minor
)
1108 DPRINTK ("EXIT, returning -ENODEV\n");
1112 file
->private_data
= &card
->ac97
;
1114 DPRINTK ("EXIT, returning 0\n");
1118 static int via_mixer_ioctl (struct inode
*inode
, struct file
*file
, unsigned int cmd
,
1121 struct ac97_codec
*codec
= file
->private_data
;
1124 assert (codec
!= NULL
);
1126 rc
= codec
->mixer_ioctl(codec
, cmd
, arg
);
1132 static loff_t
via_llseek(struct file
*file
, loff_t offset
, int origin
)
1134 DPRINTK ("ENTER\n");
1136 DPRINTK("EXIT, returning -ESPIPE\n");
1141 static struct file_operations via_mixer_fops
= {
1143 open
: via_mixer_open
,
1145 ioctl
: via_mixer_ioctl
,
1149 #if 0 /* values reasoned from debugging dumps of via's driver */
1153 } mixer_init_vals
[] __devinitdata
= {
1172 static int __init
via_ac97_reset (struct via_info
*card
)
1174 struct pci_dev
*pdev
= card
->pdev
;
1178 DPRINTK ("ENTER\n");
1180 assert (pdev
!= NULL
);
1184 u8 r40
,r41
,r42
,r43
,r44
,r48
;
1185 pci_read_config_byte (card
->pdev
, 0x40, &r40
);
1186 pci_read_config_byte (card
->pdev
, 0x41, &r41
);
1187 pci_read_config_byte (card
->pdev
, 0x42, &r42
);
1188 pci_read_config_byte (card
->pdev
, 0x43, &r43
);
1189 pci_read_config_byte (card
->pdev
, 0x44, &r44
);
1190 pci_read_config_byte (card
->pdev
, 0x48, &r48
);
1191 DPRINTK("PCI config: %02X %02X %02X %02X %02X %02X\n",
1192 r40
,r41
,r42
,r43
,r44
,r48
);
1194 spin_lock_irq (&card
->lock
);
1195 DPRINTK ("regs==%02X %02X %02X %08X %08X %08X %08X\n",
1196 inb (card
->baseaddr
+ 0x00),
1197 inb (card
->baseaddr
+ 0x01),
1198 inb (card
->baseaddr
+ 0x02),
1199 inl (card
->baseaddr
+ 0x04),
1200 inl (card
->baseaddr
+ 0x0C),
1201 inl (card
->baseaddr
+ 0x80),
1202 inl (card
->baseaddr
+ 0x84));
1203 spin_unlock_irq (&card
->lock
);
1209 * reset AC97 controller: enable, disable, enable
1210 * pause after each command for good luck
1212 pci_write_config_byte (pdev
, VIA_ACLINK_CTRL
, VIA_CR41_AC97_ENABLE
|
1213 VIA_CR41_AC97_RESET
| VIA_CR41_AC97_WAKEUP
);
1216 pci_write_config_byte (pdev
, VIA_ACLINK_CTRL
, 0);
1219 pci_write_config_byte (pdev
, VIA_ACLINK_CTRL
,
1220 VIA_CR41_AC97_ENABLE
| VIA_CR41_PCM_ENABLE
|
1221 VIA_CR41_VRA
| VIA_CR41_AC97_RESET
);
1224 #if 0 /* this breaks on K7M */
1225 /* disable legacy stuff */
1226 pci_write_config_byte (pdev
, 0x42, 0x00);
1230 /* route FM trap to IRQ, disable FM trap */
1231 pci_write_config_byte (pdev
, 0x48, 0x05);
1234 /* disable all codec GPI interrupts */
1235 outl (0, pci_resource_start (pdev
, 0) + 0x8C);
1237 /* enable variable rate */
1238 tmp16
= via_ac97_read_reg (&card
->ac97
, 0x2A);
1239 via_ac97_write_reg (&card
->ac97
, 0x2A, tmp16
| 0x01);
1241 /* boost headphone vol if disabled */
1242 tmp16
= via_ac97_read_reg (&card
->ac97
, AC97_HEADPHONE_VOL
);
1244 via_ac97_write_reg (&card
->ac97
, AC97_HEADPHONE_VOL
, 0x1F1F);
1246 pci_read_config_byte (pdev
, VIA_ACLINK_CTRL
, &tmp8
);
1247 if ((tmp8
& (VIA_CR41_AC97_ENABLE
| VIA_CR41_AC97_RESET
)) == 0) {
1248 printk (KERN_ERR PFX
"cannot enable AC97 controller, aborting\n");
1249 DPRINTK ("EXIT, tmp8=%X, returning -ENODEV\n", tmp8
);
1253 DPRINTK ("EXIT, returning 0\n");
1258 static int __init
via_ac97_init (struct via_info
*card
)
1263 DPRINTK ("ENTER\n");
1265 assert (card
!= NULL
);
1267 memset (&card
->ac97
, 0, sizeof (card
->ac97
));
1268 card
->ac97
.private_data
= card
;
1269 card
->ac97
.codec_read
= via_ac97_read_reg
;
1270 card
->ac97
.codec_write
= via_ac97_write_reg
;
1272 card
->ac97
.dev_mixer
= register_sound_mixer (&via_mixer_fops
, -1);
1273 if (card
->ac97
.dev_mixer
< 0) {
1274 printk (KERN_ERR PFX
"unable to register AC97 mixer, aborting\n");
1275 DPRINTK("EXIT, returning -EIO\n");
1279 rc
= via_ac97_reset (card
);
1281 printk (KERN_ERR PFX
"unable to reset AC97 codec, aborting\n");
1285 if (ac97_probe_codec (&card
->ac97
) == 0) {
1286 printk (KERN_ERR PFX
"unable to probe AC97 codec, aborting\n");
1291 tmp16
= via_ac97_read_reg (&card
->ac97
, 0x2A);
1292 via_ac97_write_reg (&card
->ac97
, 0x2A, tmp16
| 0x01);
1294 DPRINTK ("EXIT, returning 0\n");
1298 unregister_sound_mixer (card
->ac97
.dev_mixer
);
1299 DPRINTK("EXIT, returning %d\n", rc
);
1304 static void via_ac97_cleanup (struct via_info
*card
)
1308 assert (card
!= NULL
);
1309 assert (card
->ac97
.dev_mixer
>= 0);
1311 unregister_sound_mixer (card
->ac97
.dev_mixer
);
1318 /****************************************************************
1320 * Interrupt-related code
1324 static inline void via_interrupt_write (struct via_channel
*chan
)
1326 assert (atomic_read(&chan
->buf_in_use
) <
1327 atomic_read(&chan
->next_buf
));
1329 /* XXX sanity check: read h/w counter to
1330 * ensure no lost frames */
1332 atomic_inc (&chan
->buf_in_use
);
1334 /* if SG ptr catches up with userland ptr, stop playback */
1335 if (atomic_read(&chan
->buf_in_use
) == atomic_read(&chan
->next_buf
)) {
1336 atomic_set (&chan
->state
, sgd_stopped
);
1337 via_chan_stop (chan
->iobase
);
1340 /* wake up anybody listening */
1341 if (waitqueue_active (&chan
->wait
))
1342 wake_up (&chan
->wait
);
1347 static void via_interrupt(int irq
, void *dev_id
, struct pt_regs
*regs
)
1349 struct via_info
*card
= dev_id
;
1350 struct via_channel
*chan
;
1353 status
= inb (card
->baseaddr
+ 0x00);
1355 assert (card
->open_mode
& FMODE_WRITE
);
1357 chan
= &card
->ch_out
;
1359 if (status
& VIA_SGD_FLAG
) {
1360 assert ((status
& VIA_SGD_EOL
) == 0);
1361 outb (VIA_SGD_FLAG
, chan
->iobase
+ 0x00);
1362 DPRINTK("FLAG intr, status=0x%02X\n", status
);
1363 via_interrupt_write (chan
);
1366 if (status
& VIA_SGD_EOL
) {
1367 assert ((status
& VIA_SGD_FLAG
) == 0);
1368 outb (VIA_SGD_EOL
, chan
->iobase
+ 0x00);
1369 DPRINTK("EOL intr, status=0x%02X\n", status
);
1370 via_interrupt_write (chan
);
1373 if (status
& VIA_SGD_STOPPED
) {
1374 outb (VIA_SGD_STOPPED
, chan
->iobase
+ 0x00);
1375 DPRINTK("STOPPED intr, status=0x%02X\n", status
);
1379 via_chan_dump_bufs (&card
->ch_out
);
1385 static void via_interrupt_disable (struct via_info
*card
)
1388 unsigned long flags
;
1390 DPRINTK ("ENTER\n");
1392 assert (card
!= NULL
);
1394 spin_lock_irqsave (&card
->lock
, flags
);
1396 pci_read_config_byte (card
->pdev
, VIA_FM_NMI_CTRL
, &tmp8
);
1397 if ((tmp8
& VIA_CR48_FM_TRAP_TO_NMI
) == 0) {
1398 tmp8
|= VIA_CR48_FM_TRAP_TO_NMI
;
1399 pci_write_config_byte (card
->pdev
, VIA_FM_NMI_CTRL
, tmp8
);
1402 outb (inb (card
->baseaddr
+ VIA_BASE0_PCM_OUT_CHAN_TYPE
) &
1403 VIA_INT_DISABLE_MASK
,
1404 card
->baseaddr
+ VIA_BASE0_PCM_OUT_CHAN_TYPE
);
1405 outb (inb (card
->baseaddr
+ VIA_BASE0_PCM_IN_CHAN_TYPE
) &
1406 VIA_INT_DISABLE_MASK
,
1407 card
->baseaddr
+ VIA_BASE0_PCM_IN_CHAN_TYPE
);
1408 outb (inb (card
->baseaddr
+ VIA_BASE0_FM_OUT_CHAN_TYPE
) &
1409 VIA_INT_DISABLE_MASK
,
1410 card
->baseaddr
+ VIA_BASE0_FM_OUT_CHAN_TYPE
);
1412 spin_unlock_irqrestore (&card
->lock
, flags
);
1418 static int via_interrupt_init (struct via_info
*card
)
1420 DPRINTK ("ENTER\n");
1422 assert (card
!= NULL
);
1423 assert (card
->pdev
!= NULL
);
1425 /* check for sane IRQ number. can this ever happen? */
1426 if (card
->pdev
->irq
< 2) {
1427 printk (KERN_ERR PFX
"insane IRQ %d, aborting\n",
1429 DPRINTK ("EXIT, returning -EIO\n");
1433 if (request_irq (card
->pdev
->irq
, via_interrupt
, SA_SHIRQ
, VIA_MODULE_NAME
, card
)) {
1434 printk (KERN_ERR PFX
"unable to obtain IRQ %d, aborting\n",
1436 DPRINTK ("EXIT, returning -EBUSY\n");
1440 /* we don't want interrupts until we're opened */
1441 via_interrupt_disable (card
);
1443 DPRINTK ("EXIT, returning 0\n");
1448 static void via_interrupt_cleanup (struct via_info
*card
)
1450 DPRINTK ("ENTER\n");
1452 assert (card
!= NULL
);
1453 assert (card
->pdev
!= NULL
);
1455 via_interrupt_disable (card
);
1457 free_irq (card
->pdev
->irq
, card
);
1463 /****************************************************************
1469 static struct file_operations via_dsp_fops
= {
1472 release
: via_dsp_release
,
1474 write
: via_dsp_write
,
1477 ioctl
: via_dsp_ioctl
,
1481 static int __init
via_dsp_init (struct via_info
*card
)
1485 DPRINTK ("ENTER\n");
1487 assert (card
!= NULL
);
1489 /* turn off legacy features, if not already */
1490 pci_read_config_byte (card
->pdev
, VIA_FUNC_ENABLE
, &tmp8
);
1491 tmp8
&= ~(VIA_CR42_SB_ENABLE
| VIA_CR42_MIDI_ENABLE
|
1492 VIA_CR42_FM_ENABLE
);
1493 pci_write_config_byte (card
->pdev
, VIA_FUNC_ENABLE
, tmp8
);
1495 via_stop_everything (card
);
1497 card
->dev_dsp
= register_sound_dsp (&via_dsp_fops
, -1);
1498 if (card
->dev_dsp
< 0) {
1499 DPRINTK ("EXIT, returning -ENODEV\n");
1502 DPRINTK ("EXIT, returning 0\n");
1507 static void via_dsp_cleanup (struct via_info
*card
)
1509 DPRINTK ("ENTER\n");
1511 assert (card
!= NULL
);
1512 assert (card
->dev_dsp
>= 0);
1514 via_stop_everything (card
);
1516 unregister_sound_dsp (card
->dev_dsp
);
1522 static ssize_t
via_dsp_read(struct file
*file
, char *buffer
, size_t count
, loff_t
*ppos
)
1524 struct via_info
*card
;
1526 DPRINTK ("ENTER\n");
1528 assert (file
!= NULL
);
1529 assert (buffer
!= NULL
);
1530 card
= file
->private_data
;
1531 assert (card
!= NULL
);
1533 DPRINTK("EXIT, returning -EINVAL\n");
1539 #define sgcount(n) (sgtable[(n)].count & 0x00FFFFFF)
1540 #define NEXTBUF (atomic_read(&chan->next_buf) % VIA_DMA_BUFFERS)
1541 #define BUF_IN_USE (atomic_read(&chan->buf_in_use) % VIA_DMA_BUFFERS)
1542 #define STATE_STOPPED (atomic_read (state) == sgd_stopped)
1543 #define STATE_STARTED (atomic_read (state) == sgd_in_progress)
1544 static ssize_t
via_dsp_do_write (struct via_info
*card
,
1545 const char *userbuf
, size_t count
,
1548 const char *orig_userbuf
= userbuf
;
1549 struct via_channel
*chan
= &card
->ch_out
;
1550 volatile struct via_sgd_table
*sgtable
= chan
->sgtable
;
1551 atomic_t
*state
= &chan
->state
;
1553 int nextbuf
, prevbuf
, n
, realcount
;
1557 if (current
->need_resched
)
1560 spin_lock_irq (&card
->lock
);
1561 DPRINTK ("regs==%02X %02X %02X %08X %08X %08X %08X\n",
1562 inb (card
->baseaddr
+ 0x00),
1563 inb (card
->baseaddr
+ 0x01),
1564 inb (card
->baseaddr
+ 0x02),
1565 inl (card
->baseaddr
+ 0x04),
1566 inl (card
->baseaddr
+ 0x0C),
1567 inl (card
->baseaddr
+ 0x80),
1568 inl (card
->baseaddr
+ 0x84));
1569 spin_unlock_irq (&card
->lock
);
1571 size
= (count
< VIA_DMA_BUF_SIZE
) ? count
: VIA_DMA_BUF_SIZE
;
1573 /* case 1: SGD not active, list is ours for the mangling */
1575 if (STATE_STOPPED
) {
1576 DPRINTK ("case 1\n");
1578 if (copy_from_user ((void*)chan
->sgbuf
[0].cpuaddr
,
1582 assert (sgtable
[0].addr
== cpu_to_le32 (chan
->sgbuf
[0].handle
));
1583 sgtable
[0].count
= size
| VIA_FLAG
;
1585 atomic_set (state
, sgd_in_progress
);
1586 atomic_set (&chan
->buf_in_use
, 0);
1587 atomic_set (&chan
->next_buf
, 1);
1592 spin_lock_irq (&card
->lock
);
1594 spin_unlock_irq (&card
->lock
);
1601 prevbuf
= nextbuf
- 1;
1603 prevbuf
= VIA_DMA_BUFFERS
- 1;
1605 /* case 2: if final buffer is (a) a fragment, and (b) not
1606 * currently being consumed by the SGD engine, then append
1607 * as much data as possible to the fragment. */
1609 realcount
= sgcount(prevbuf
);
1610 if (STATE_STARTED
&& (prevbuf
!= BUF_IN_USE
) &&
1611 (realcount
< VIA_DMA_BUF_SIZE
)) {
1612 DPRINTK ("case 2\n");
1613 DPRINTK ("st=%d, fb=%d -- nb=%d, pb=%d, n=%d, rc=%d\n",
1614 atomic_read (state
),
1623 if ((VIA_DMA_BUF_SIZE
- realcount
) < size
)
1624 size
= VIA_DMA_BUF_SIZE
- realcount
;
1626 if (copy_from_user ((void*)(chan
->sgbuf
[n
].cpuaddr
+
1631 /* slack way to try and prevent races */
1632 if (prevbuf
== BUF_IN_USE
|| !STATE_STARTED
)
1635 assert (sgtable
[n
].addr
== cpu_to_le32 (chan
->sgbuf
[n
].handle
));
1636 if (n
== (VIA_DMA_BUFFERS
- 1))
1637 sgtable
[n
].count
= (realcount
+ size
) | VIA_EOL
;
1639 sgtable
[n
].count
= (realcount
+ size
) | VIA_FLAG
;
1646 /* case 3: if there are buffers left, use one
1647 * XXX needs more review for possible races */
1649 else if (STATE_STARTED
&& !via_chan_full (chan
)) {
1650 DPRINTK ("case 3\n");
1651 DPRINTK ("st=%d, fb=%d -- nb=%d, pb=%d, n=%d\n",
1652 atomic_read (state
),
1660 if (copy_from_user ((void*)chan
->sgbuf
[n
].cpuaddr
,
1664 if (n
== (VIA_DMA_BUFFERS
- 1))
1665 sgtable
[n
].count
= size
| VIA_EOL
;
1667 sgtable
[n
].count
= size
| VIA_FLAG
;
1669 /* if SGD stopped during data copy or SG table update,
1670 * then loop back to the beginning without updating
1672 * ie. slack way to prevent race */
1676 atomic_inc (&chan
->next_buf
);
1683 /* case 4, final SGT active case: no free buffers, wait for one */
1686 DPRINTK ("case 4\n");
1687 DPRINTK ("st=%d, fb=%d -- nb=%d, pb=%d\n",
1688 atomic_read (state
),
1693 /* if playback stopped, no need to sleep */
1697 /* if buffer free, no need to sleep */
1698 if (!via_chan_full (chan
))
1702 rc
= userbuf
- orig_userbuf
;
1708 DPRINTK ("sleeping\n");
1709 interruptible_sleep_on (&chan
->wait
);
1710 if (signal_pending (current
))
1711 return -ERESTARTSYS
;
1717 u8 r40
,r41
,r42
,r43
,r44
,r48
;
1718 pci_read_config_byte (card
->pdev
, 0x40, &r40
);
1719 pci_read_config_byte (card
->pdev
, 0x41, &r41
);
1720 pci_read_config_byte (card
->pdev
, 0x42, &r42
);
1721 pci_read_config_byte (card
->pdev
, 0x43, &r43
);
1722 pci_read_config_byte (card
->pdev
, 0x44, &r44
);
1723 pci_read_config_byte (card
->pdev
, 0x48, &r48
);
1724 DPRINTK("PCI config: %02X %02X %02X %02X %02X %02X\n",
1725 r40
,r41
,r42
,r43
,r44
,r48
);
1729 DPRINTK ("EXIT, returning %d\n",
1730 userbuf
- orig_userbuf
);
1731 return userbuf
- orig_userbuf
;
1736 #undef STATE_STOPPED
1737 #undef STATE_STARTED
1740 static ssize_t
via_dsp_write(struct file
*file
, const char *buffer
, size_t count
, loff_t
*ppos
)
1742 struct via_info
*card
;
1745 DPRINTK ("ENTER, file=%p, buffer=%p, count=%u, ppos=%lu\n",
1746 file
, buffer
, count
, ppos
? ((unsigned long)*ppos
) : 0);
1748 assert (file
!= NULL
);
1749 assert (buffer
!= NULL
);
1750 card
= file
->private_data
;
1751 assert (card
!= NULL
);
1753 if (ppos
!= &file
->f_pos
) {
1754 DPRINTK ("EXIT, returning -ESPIPE\n");
1758 rc
= via_dsp_do_write (card
, buffer
, count
, (file
->f_flags
& O_NONBLOCK
));
1760 DPRINTK("EXIT, returning %ld\n",(long) rc
);
1765 static unsigned int via_dsp_poll(struct file
*file
, struct poll_table_struct
*wait
)
1767 struct via_info
*card
;
1768 unsigned int mask
= 0;
1770 DPRINTK ("ENTER\n");
1772 assert (file
!= NULL
);
1773 assert (wait
!= NULL
);
1774 card
= file
->private_data
;
1775 assert (card
!= NULL
);
1777 if ((file
->f_mode
& FMODE_WRITE
) &&
1778 (atomic_read (&card
->ch_out
.state
) != sgd_stopped
)) {
1779 poll_wait(file
, &card
->ch_out
.wait
, wait
);
1781 /* XXX is this correct */
1782 if (atomic_read (&card
->ch_out
.buf_in_use
) <
1783 atomic_read (&card
->ch_out
.next_buf
))
1784 mask
|= POLLOUT
| POLLWRNORM
;
1787 DPRINTK("EXIT, returning %u\n", mask
);
1792 static int via_dsp_drain_dac (struct via_info
*card
, int non_block
)
1794 DPRINTK ("ENTER, non_block = %d\n", non_block
);
1796 while (!via_chan_empty (&card
->ch_out
)) {
1798 DPRINTK ("EXIT, returning -EBUSY\n");
1801 if (signal_pending (current
)) {
1802 DPRINTK ("EXIT, returning -ERESTARTSYS\n");
1803 return -ERESTARTSYS
;
1808 u8 r40
,r41
,r42
,r43
,r44
,r48
;
1809 pci_read_config_byte (card
->pdev
, 0x40, &r40
);
1810 pci_read_config_byte (card
->pdev
, 0x41, &r41
);
1811 pci_read_config_byte (card
->pdev
, 0x42, &r42
);
1812 pci_read_config_byte (card
->pdev
, 0x43, &r43
);
1813 pci_read_config_byte (card
->pdev
, 0x44, &r44
);
1814 pci_read_config_byte (card
->pdev
, 0x48, &r48
);
1815 DPRINTK("PCI config: %02X %02X %02X %02X %02X %02X\n",
1816 r40
,r41
,r42
,r43
,r44
,r48
);
1818 spin_lock_irq (&card
->lock
);
1819 DPRINTK ("regs==%02X %02X %02X %08X %08X %08X %08X\n",
1820 inb (card
->baseaddr
+ 0x00),
1821 inb (card
->baseaddr
+ 0x01),
1822 inb (card
->baseaddr
+ 0x02),
1823 inl (card
->baseaddr
+ 0x04),
1824 inl (card
->baseaddr
+ 0x0C),
1825 inl (card
->baseaddr
+ 0x80),
1826 inl (card
->baseaddr
+ 0x84));
1827 spin_unlock_irq (&card
->lock
);
1832 DPRINTK ("sleeping\n");
1833 interruptible_sleep_on (&card
->ch_out
.wait
);
1841 static int via_dsp_ioctl_space (struct via_info
*card
,
1842 struct via_channel
*chan
,
1845 audio_buf_info info
;
1848 info
.fragstotal
= VIA_DMA_BUFFERS
;
1849 info
.fragsize
= VIA_DMA_BUF_SIZE
;
1851 /* number of full fragments we can read without blocking */
1852 n
= atomic_read (&chan
->next_buf
) - atomic_read (&chan
->buf_in_use
);
1853 info
.fragments
= VIA_DMA_BUFFERS
- n
;
1855 /* number of bytes that can be read or written immediately
1856 * without blocking. FIXME: we are lazy and ignore partially-full
1859 info
.bytes
= info
.fragments
* VIA_DMA_BUF_SIZE
;
1861 DPRINTK ("EXIT, returning fragstotal=%d, fragsize=%d, fragments=%d, bytes=%d\n",
1867 return copy_to_user (arg
, &info
, sizeof (info
));
1872 static int via_dsp_ioctl (struct inode
*inode
, struct file
*file
,
1873 unsigned int cmd
, unsigned long arg
)
1875 int rc
= -EINVAL
, rd
=0, wr
=0, val
=0;
1876 struct via_info
*card
;
1878 DPRINTK ("ENTER, cmd = 0x%08X\n", cmd
);
1880 assert (file
!= NULL
);
1881 card
= file
->private_data
;
1882 assert (card
!= NULL
);
1884 if (file
->f_mode
& FMODE_WRITE
)
1886 if (file
->f_mode
& FMODE_READ
)
1891 /* OSS API version. XXX unverified */
1892 case OSS_GETVERSION
:
1893 DPRINTK("EXIT, returning SOUND_VERSION\n");
1894 return put_user (SOUND_VERSION
, (int *)arg
);
1896 /* list of supported PCM data formats */
1897 case SNDCTL_DSP_GETFMTS
:
1898 DPRINTK("EXIT, returning AFMT U8|S16_LE\n");
1899 return put_user (AFMT_U8
| AFMT_S16_LE
, (int *)arg
);
1901 /* query or set current channel's PCM data format */
1902 case SNDCTL_DSP_SETFMT
:
1903 if (get_user(val
, (int *)arg
))
1905 if (val
!= AFMT_QUERY
) {
1908 spin_lock_irq (&card
->lock
);
1910 rc
= via_chan_set_fmt (card
, &card
->ch_in
, val
);
1912 rc
= via_chan_set_fmt (card
, &card
->ch_out
, val
);
1913 spin_unlock_irq (&card
->lock
);
1916 return rc
? rc
: -EINVAL
;
1919 spin_lock_irq (&card
->lock
);
1920 if ((rd
&& (card
->ch_in
.pcm_fmt
& VIA_PCM_FMT_16BIT
)) ||
1921 (wr
&& (card
->ch_out
.pcm_fmt
& VIA_PCM_FMT_16BIT
)))
1925 spin_unlock_irq (&card
->lock
);
1927 DPRINTK("SETFMT EXIT, returning %d\n", val
);
1928 return put_user (val
, (int *)arg
);
1930 /* query or set number of channels (1=mono, 2=stereo) */
1931 case SNDCTL_DSP_CHANNELS
:
1932 if (get_user(val
, (int *)arg
))
1936 spin_lock_irq (&card
->lock
);
1938 rc
= via_chan_set_stereo (card
, &card
->ch_in
, val
);
1940 rc
= via_chan_set_stereo (card
, &card
->ch_out
, val
);
1941 spin_unlock_irq (&card
->lock
);
1943 return rc
? rc
: -EINVAL
;
1946 spin_lock_irq (&card
->lock
);
1947 if ((rd
&& (card
->ch_in
.pcm_fmt
& VIA_PCM_FMT_STEREO
)) ||
1948 (wr
&& (card
->ch_out
.pcm_fmt
& VIA_PCM_FMT_STEREO
)))
1952 spin_unlock_irq (&card
->lock
);
1954 DPRINTK("CHANNELS EXIT, returning %d\n", val
);
1955 return put_user (val
, (int *)arg
);
1957 /* enable (val is not zero) or disable (val == 0) stereo */
1958 case SNDCTL_DSP_STEREO
:
1959 if (get_user(val
, (int *)arg
))
1962 spin_lock_irq (&card
->lock
);
1964 rc
= via_chan_set_stereo (card
, &card
->ch_in
, val
? 2 : 1);
1966 rc
= via_chan_set_stereo (card
, &card
->ch_out
, val
? 2 : 1);
1967 spin_unlock_irq (&card
->lock
);
1969 return rc
? rc
: -EINVAL
;
1970 DPRINTK("STEREO EXIT, returning %d\n", val
);
1973 /* query or set sampling rate */
1974 case SNDCTL_DSP_SPEED
:
1975 if (get_user(val
, (int *)arg
))
1981 spin_lock_irq (&card
->lock
);
1983 rc
= via_chan_set_speed (card
, &card
->ch_in
, val
);
1985 rc
= via_chan_set_speed (card
, &card
->ch_out
, val
);
1986 spin_unlock_irq (&card
->lock
);
1988 return rc
? rc
: -EINVAL
;
1991 spin_lock_irq (&card
->lock
);
1993 val
= card
->ch_in
.rate
;
1995 val
= card
->ch_out
.rate
;
1998 spin_unlock_irq (&card
->lock
);
2000 DPRINTK("SPEED EXIT, returning %d\n", val
);
2001 return put_user (val
, (int *)arg
);
2003 /* wait until all buffers have been played, and then stop device */
2004 case SNDCTL_DSP_SYNC
:
2006 DPRINTK("SYNC EXIT (after calling via_dsp_drain_dac)\n");
2007 return via_dsp_drain_dac (card
, file
->f_flags
& O_NONBLOCK
);
2011 /* stop recording/playback immediately */
2012 case SNDCTL_DSP_RESET
:
2013 spin_lock_irq (&card
->lock
);
2015 via_chan_clear (&card
->ch_in
);
2016 via_chan_pcm_fmt (card
, &card
->ch_in
, 1);
2019 via_chan_clear (&card
->ch_out
);
2020 via_chan_pcm_fmt (card
, &card
->ch_out
, 1);
2022 spin_unlock_irq (&card
->lock
);
2023 DPRINTK("RESET EXIT, returning 0\n");
2026 /* obtain bitmask of device capabilities, such as mmap, full duplex, etc. */
2027 case SNDCTL_DSP_GETCAPS
:
2028 DPRINTK("GETCAPS EXIT\n");
2029 return put_user(DSP_CAP_REVISION
, (int *)arg
);
2031 /* obtain bitmask of device capabilities, such as mmap, full duplex, etc. */
2032 case SNDCTL_DSP_GETBLKSIZE
:
2033 DPRINTK("GETBLKSIZE EXIT\n");
2034 return put_user(VIA_DMA_BUF_SIZE
, (int *)arg
);
2036 /* obtain information about input buffering */
2037 case SNDCTL_DSP_GETISPACE
:
2038 DPRINTK("GETISPACE EXIT\n");
2039 return via_dsp_ioctl_space (card
, &card
->ch_in
, (void*) arg
);
2041 /* obtain information about output buffering */
2042 case SNDCTL_DSP_GETOSPACE
:
2043 DPRINTK("GETOSPACE EXIT\n");
2044 return via_dsp_ioctl_space (card
, &card
->ch_out
, (void*) arg
);
2046 /* return number of bytes remaining to be played by DMA engine */
2047 case SNDCTL_DSP_GETODELAY
:
2051 n
= atomic_read (&card
->ch_out
.next_buf
) -
2052 atomic_read (&card
->ch_out
.buf_in_use
);
2058 val
= (n
- 1) * VIA_DMA_BUF_SIZE
;
2059 val
+= inl (card
->ch_out
.iobase
+ VIA_BASE0_PCM_OUT_BLOCK_COUNT
);
2062 DPRINTK("GETODELAY EXIT, val = %d bytes\n", val
);
2063 return put_user (val
, (int *)arg
);
2066 /* set fragment size. implemented as a successful no-op for now */
2067 case SNDCTL_DSP_SETFRAGMENT
:
2068 if (get_user(val
, (int *)arg
))
2071 DPRINTK ("SNDCTL_DSP_SETFRAGMENT (fragshift==0x%04X (%d), maxfrags==0x%04X (%d))\n",
2074 (val
>> 16) & 0xFFFF,
2075 (val
>> 16) & 0xFFFF);
2077 /* just to shut up some programs */
2080 /* inform device of an upcoming pause in input (or output). not implemented */
2081 case SNDCTL_DSP_POST
:
2082 DPRINTK("POST EXIT (null ioctl, returning -EINVAL)\n");
2085 /* not implemented */
2087 DPRINTK ("unhandled ioctl\n");
2091 DPRINTK("EXIT, returning %d\n", rc
);
2096 static int via_dsp_open (struct inode
*inode
, struct file
*file
)
2098 int open_mode
, rc
= -EINVAL
, minor
= MINOR(inode
->i_rdev
);
2099 int got_read_chan
= 0, is_busy
;
2100 struct via_info
*card
;
2101 struct pci_dev
*pdev
;
2102 struct pci_driver
*drvr
;
2103 unsigned long flags
;
2105 DPRINTK ("ENTER, minor=%d, file->f_mode=0x%x\n", minor
, file
->f_mode
);
2107 if (file
->f_mode
& FMODE_READ
) /* no input ATM */
2111 pci_for_each_dev(pdev
) {
2112 drvr
= pci_dev_driver (pdev
);
2113 if (drvr
== &via_driver
) {
2114 assert (pdev
->driver_data
!= NULL
);
2116 card
= pdev
->driver_data
;
2117 DPRINTK ("dev_dsp = %d, minor = %d, assn = %d\n",
2118 card
->dev_dsp
, minor
,
2119 (card
->dev_dsp
^ minor
) & ~0xf);
2121 if (((card
->dev_dsp
^ minor
) & ~0xf) == 0)
2126 DPRINTK ("no matching %s found\n", card
? "minor" : "driver");
2131 file
->private_data
= card
;
2133 /* wait for device to become free */
2134 spin_lock_irqsave (&card
->lock
, flags
);
2135 open_mode
= card
->open_mode
;
2136 if (open_mode
& file
->f_mode
)
2140 card
->open_mode
|= file
->f_mode
;
2141 open_mode
= card
->open_mode
;
2143 spin_unlock_irqrestore (&card
->lock
, flags
);
2149 DPRINTK("open_mode now 0x%x\n", open_mode
);
2151 /* handle input from analog source */
2152 if (file
->f_mode
& FMODE_READ
) {
2153 rc
= via_chan_init (card
, &card
->ch_in
, 0x10);
2155 goto err_out_clear_mode
;
2159 /* why is this forced to 16-bit stereo in all drivers? */
2160 card
->ch_in
.pcm_fmt
=
2161 VIA_PCM_FMT_16BIT
| VIA_PCM_FMT_STEREO
;
2163 spin_lock_irqsave (&card
->lock
, flags
);
2164 via_chan_pcm_fmt (card
, &card
->ch_out
, 0);
2165 spin_unlock_irqrestore (&card
->lock
, flags
);
2167 via_set_adc_rate (card
, 8000);
2170 /* handle output to analog source */
2171 if (file
->f_mode
& FMODE_WRITE
) {
2172 rc
= via_chan_init (card
, &card
->ch_out
, 0x00);
2174 goto err_out_clear_mode
;
2176 if ((minor
& 0xf) == SND_DEV_DSP16
)
2177 card
->ch_out
.pcm_fmt
|= VIA_PCM_FMT_16BIT
;
2179 spin_lock_irqsave (&card
->lock
, flags
);
2180 via_chan_pcm_fmt (card
, &card
->ch_out
, 0);
2181 spin_unlock_irqrestore (&card
->lock
, flags
);
2183 via_set_dac_rate (card
, 8000);
2186 DPRINTK ("EXIT, returning 0\n");
2191 via_chan_free (card
, &card
->ch_in
);
2192 spin_lock_irqsave (&card
->lock
, flags
);
2193 card
->open_mode
&= ~file
->f_mode
;
2194 spin_unlock_irqrestore (&card
->lock
, flags
);
2196 DPRINTK("ERROR EXIT, returning %d\n", rc
);
2201 static int via_dsp_release(struct inode
*inode
, struct file
*file
)
2203 struct via_info
*card
;
2204 unsigned long flags
;
2206 DPRINTK ("ENTER\n");
2208 assert (file
!= NULL
);
2209 card
= file
->private_data
;
2210 assert (card
!= NULL
);
2213 if (file
->f_mode
& FMODE_READ
)
2214 via_chan_free (card
, &card
->ch_in
);
2216 if (file
->f_mode
& FMODE_WRITE
) {
2217 via_dsp_drain_dac (card
, file
->f_flags
& O_NONBLOCK
);
2218 via_chan_free (card
, &card
->ch_out
);
2221 spin_lock_irqsave (&card
->lock
, flags
);
2222 card
->open_mode
&= ~(file
->f_mode
);
2223 spin_unlock_irqrestore (&card
->lock
, flags
);
2225 wake_up (&card
->open_wait
);
2228 DPRINTK("EXIT, returning 0\n");
2235 /****************************************************************
2237 * /proc/driver/via/info
2242 static int via_info_read_proc (char *page
, char **start
, off_t off
,
2243 int count
, int *eof
, void *data
)
2245 #define YN(val,bit) (((val) & (bit)) ? "yes" : "no")
2246 #define ED(val,bit) (((val) & (bit)) ? "enable" : "disable")
2249 u8 r40
, r41
, r42
, r44
;
2250 struct via_info
*card
= data
;
2252 DPRINTK ("ENTER\n");
2254 assert (card
!= NULL
);
2256 len
+= sprintf (page
+len
, VIA_CARD_NAME
"\n\n");
2258 pci_read_config_byte (card
->pdev
, 0x40, &r40
);
2259 pci_read_config_byte (card
->pdev
, 0x41, &r41
);
2260 pci_read_config_byte (card
->pdev
, 0x42, &r42
);
2261 pci_read_config_byte (card
->pdev
, 0x44, &r44
);
2263 len
+= sprintf (page
+len
,
2264 "Via 82Cxxx PCI registers:\n"
2266 "40 Codec Ready: %s\n"
2267 " Codec Low-power: %s\n"
2268 " Secondary Codec Ready: %s\n"
2270 "41 Interface Enable: %s\n"
2271 " De-Assert Reset: %s\n"
2272 " Force SYNC high: %s\n"
2273 " Force SDO high: %s\n"
2274 " Variable Sample Rate On-Demand Mode: %s\n"
2275 " SGD Read Channel PCM Data Out: %s\n"
2276 " FM Channel PCM Data Out: %s\n"
2277 " SB PCM Data Out: %s\n"
2279 "42 Game port enabled: %s\n"
2280 " SoundBlaster enabled: %s\n"
2282 " MIDI enabled: %s\n"
2284 "44 AC-Link Interface Access: %s\n"
2285 " Secondary Codec Support: %s\n"
2289 YN (r40
, VIA_CR40_AC97_READY
),
2290 YN (r40
, VIA_CR40_AC97_LOW_POWER
),
2291 YN (r40
, VIA_CR40_SECONDARY_READY
),
2293 ED (r41
, VIA_CR41_AC97_ENABLE
),
2302 YN (r42
, VIA_CR42_GAME_ENABLE
),
2303 YN (r42
, VIA_CR42_SB_ENABLE
),
2304 YN (r42
, VIA_CR42_FM_ENABLE
),
2305 YN (r42
, VIA_CR42_MIDI_ENABLE
),
2307 YN (r44
, VIA_CR44_AC_LINK_ACCESS
),
2308 YN (r44
, VIA_CR44_SECOND_CODEC_SUPPORT
)
2312 DPRINTK("EXIT, returning %d\n", len
);
2320 /****************************************************************
2322 * /proc/driver/via/... setup and cleanup
2327 static int __init
via_init_proc (void)
2329 DPRINTK ("ENTER\n");
2331 if (!proc_mkdir ("driver/via", 0))
2334 DPRINTK ("EXIT, returning 0\n");
2339 static void via_cleanup_proc (void)
2341 DPRINTK ("ENTER\n");
2343 remove_proc_entry ("driver/via", NULL
);
2349 static int __init
via_card_init_proc (struct via_info
*card
)
2354 DPRINTK ("ENTER\n");
2356 sprintf (s
, "driver/via/%d", card
->card_num
);
2357 if (!proc_mkdir (s
, 0)) {
2362 sprintf (s
, "driver/via/%d/info", card
->card_num
);
2363 if (!create_proc_read_entry (s
, 0, 0, via_info_read_proc
, card
)) {
2368 sprintf (s
, "driver/via/%d/ac97", card
->card_num
);
2369 if (!create_proc_read_entry (s
, 0, 0, ac97_read_proc
, &card
->ac97
)) {
2374 DPRINTK ("EXIT, returning 0\n");
2378 sprintf (s
, "driver/via/%d/info", card
->card_num
);
2379 remove_proc_entry (s
, NULL
);
2382 sprintf (s
, "driver/via/%d", card
->card_num
);
2383 remove_proc_entry (s
, NULL
);
2386 DPRINTK ("EXIT, returning %d\n", rc
);
2391 static void via_card_cleanup_proc (struct via_info
*card
)
2395 DPRINTK ("ENTER\n");
2397 sprintf (s
, "driver/via/%d/ac97", card
->card_num
);
2398 remove_proc_entry (s
, NULL
);
2400 sprintf (s
, "driver/via/%d/info", card
->card_num
);
2401 remove_proc_entry (s
, NULL
);
2403 sprintf (s
, "driver/via/%d", card
->card_num
);
2404 remove_proc_entry (s
, NULL
);
2412 static inline int via_init_proc (void) { return 0; }
2413 static inline void via_cleanup_proc (void) {}
2414 static inline int via_card_init_proc (struct via_info
*card
) { return 0; }
2415 static inline void via_card_cleanup_proc (struct via_info
*card
) {}
2417 #endif /* VIA_PROC_FS */
2420 /****************************************************************
2422 * Chip setup and kernel registration
2427 static int __init
via_init_one (struct pci_dev
*pdev
, const struct pci_device_id
*id
)
2430 struct via_info
*card
;
2432 static int printed_version
= 0;
2434 DPRINTK ("ENTER\n");
2436 if (printed_version
++ == 0)
2437 printk (KERN_INFO
"Via 686a audio driver " VIA_VERSION
"\n");
2439 if (!request_region (pci_resource_start (pdev
, 0),
2440 pci_resource_len (pdev
, 0),
2442 printk (KERN_ERR PFX
"unable to obtain I/O resources, aborting\n");
2447 if (pci_enable_device (pdev
)) {
2452 card
= kmalloc (sizeof (*card
), GFP_KERNEL
);
2454 printk (KERN_ERR PFX
"out of memory, aborting\n");
2459 pdev
->driver_data
= card
;
2461 memset (card
, 0, sizeof (*card
));
2463 card
->baseaddr
= pci_resource_start (pdev
, 0);
2464 card
->card_num
= via_num_cards
++;
2465 spin_lock_init (&card
->lock
);
2466 init_waitqueue_head(&card
->open_wait
);
2468 /* if BAR 2 is present, chip is Rev H or later,
2469 * which means it has a few extra features */
2470 if (pci_resource_start (pdev
, 2) > 0)
2473 if (pdev
->irq
< 1) {
2474 printk (KERN_ERR PFX
"invalid PCI IRQ %d, aborting\n", pdev
->irq
);
2479 if (!(pci_resource_flags (pdev
, 0) & IORESOURCE_IO
)) {
2480 printk (KERN_ERR PFX
"unable to locate I/O resources, aborting\n");
2486 * init AC97 mixer and codec
2488 rc
= via_ac97_init (card
);
2490 printk (KERN_ERR PFX
"AC97 init failed, aborting\n");
2497 rc
= via_dsp_init (card
);
2499 printk (KERN_ERR PFX
"DSP device init failed, aborting\n");
2500 goto err_out_have_mixer
;
2504 * per-card /proc info
2506 rc
= via_card_init_proc (card
);
2508 printk (KERN_ERR PFX
"card-specific /proc init failed, aborting\n");
2509 goto err_out_have_dsp
;
2513 * init and turn on interrupts, as the last thing we do
2515 rc
= via_interrupt_init (card
);
2517 printk (KERN_ERR PFX
"interrupt init failed, aborting\n");
2518 goto err_out_have_proc
;
2521 pci_read_config_byte (pdev
, 0x3C, &tmp
);
2522 if ((tmp
& 0x0F) != pdev
->irq
) {
2523 printk (KERN_WARNING PFX
"IRQ fixup, 0x3C==0x%02X\n", tmp
);
2526 pci_write_config_byte (pdev
, 0x3C, tmp
);
2527 DPRINTK("new 0x3c==0x%02x\n", tmp
);
2529 DPRINTK("IRQ reg 0x3c==0x%02x, irq==%d\n",
2533 printk (KERN_INFO PFX
"board #%d at 0x%04lX, IRQ %d\n",
2534 card
->card_num
+ 1, card
->baseaddr
, pdev
->irq
);
2536 DPRINTK ("EXIT, returning 0\n");
2540 via_card_cleanup_proc (card
);
2543 via_dsp_cleanup (card
);
2546 via_ac97_cleanup (card
);
2550 memset (card
, 0xAB, sizeof (*card
)); /* poison memory */
2555 release_region (pci_resource_start (pdev
, 0), pci_resource_len (pdev
, 0));
2557 pdev
->driver_data
= NULL
;
2558 DPRINTK ("EXIT - returning %d\n", rc
);
2563 static void __exit
via_remove_one (struct pci_dev
*pdev
)
2565 struct via_info
*card
;
2567 DPRINTK ("ENTER\n");
2569 assert (pdev
!= NULL
);
2570 card
= pdev
->driver_data
;
2571 assert (card
!= NULL
);
2573 via_interrupt_cleanup (card
);
2574 via_card_cleanup_proc (card
);
2575 via_dsp_cleanup (card
);
2576 via_ac97_cleanup (card
);
2578 release_region (pci_resource_start (pdev
, 0), pci_resource_len (pdev
, 0));
2581 memset (card
, 0xAB, sizeof (*card
)); /* poison memory */
2585 pdev
->driver_data
= NULL
;
2587 pci_set_power_state (pdev
, 3); /* ...zzzzzz */
2594 /****************************************************************
2596 * Driver initialization and cleanup
2601 static int __init
init_via82cxxx_audio(void)
2605 DPRINTK ("ENTER\n");
2607 rc
= via_init_proc ();
2609 DPRINTK ("EXIT, returning %d\n", rc
);
2613 rc
= pci_register_driver (&via_driver
);
2616 pci_unregister_driver (&via_driver
);
2617 via_cleanup_proc ();
2618 DPRINTK ("EXIT, returning -ENODEV\n");
2622 DPRINTK ("EXIT, returning 0\n");
2627 static void __exit
cleanup_via82cxxx_audio(void)
2631 pci_unregister_driver (&via_driver
);
2632 via_cleanup_proc ();
2638 module_init(init_via82cxxx_audio
);
2639 module_exit(cleanup_via82cxxx_audio
);