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

Linux Cross Reference
Linux/drivers/sound/via82cxxx_audio.c

Version: ~ [ 2.4.0 ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

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

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.