~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/sound/oss/via82cxxx_audio.c

Version: ~ [ linux-5.2-rc1 ] ~ [ linux-5.1.2 ] ~ [ linux-5.0.16 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.43 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.119 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.176 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.179 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.139 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.67 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.39.4 ] ~ [ linux-2.6.38.8 ] ~ [ linux-2.6.37.6 ] ~ [ linux-2.6.36.4 ] ~ [ linux-2.6.35.14 ] ~ [ linux-2.6.34.15 ] ~ [ linux-2.6.33.20 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp