Merge master.kernel.org:/pub/scm/linux/kernel/git/davem/net-2.6
[linux-2.6] / sound / oss / cs46xx.c
1 /*
2  *      Crystal SoundFusion CS46xx driver
3  *
4  *      Copyright 1998-2001 Cirrus Logic Corporation <pcaudio@crystal.cirrus.com>
5  *                                              <twoller@crystal.cirrus.com>
6  *      Copyright 1999-2000 Jaroslav Kysela <perex@suse.cz>
7  *      Copyright 2000 Alan Cox <alan@redhat.com>
8  *
9  *      The core of this code is taken from the ALSA project driver by 
10  *      Jaroslav. Please send Jaroslav the credit for the driver and 
11  *      report bugs in this port to <alan@redhat.com>
12  *
13  *      This program is free software; you can redistribute it and/or modify
14  *      it under the terms of the GNU General Public License as published by
15  *      the Free Software Foundation; either version 2 of the License, or
16  *      (at your option) any later version.
17  *
18  *      This program is distributed in the hope that it will be useful,
19  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
20  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  *      GNU General Public License for more details.
22  *
23  *      You should have received a copy of the GNU General Public License
24  *      along with this program; if not, write to the Free Software
25  *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26  *      Current maintainers:
27  *              Cirrus Logic Corporation, Thomas Woller (tw)
28  *                      <twoller@crystal.cirrus.com>
29  *              Nils Faerber (nf)
30  *                      <nils@kernelconcepts.de>
31  *              Thanks to David Pollard for testing.
32  *
33  *      Changes:
34  *      20000909-nf     Changed cs_read, cs_write and drain_dac
35  *      20001025-tw     Separate Playback/Capture structs and buffers.
36  *                      Added Scatter/Gather support for Playback.
37  *                      Added Capture.
38  *      20001027-nf     Port to kernel 2.4.0-test9, some clean-ups
39  *                      Start of powermanagement support (CS46XX_PM).
40  *      20001128-tw     Add module parm for default buffer order.
41  *                      added DMA_GFP flag to kmalloc dma buffer allocs.
42  *                      backfill silence to eliminate stuttering on
43  *                      underruns.
44  *      20001201-tw     add resyncing of swptr on underruns.
45  *      20001205-tw-nf  fixed GETOSPACE ioctl() after open()
46  *      20010113-tw     patch from Hans Grobler general cleanup.
47  *      20010117-tw     2.4.0 pci cleanup, wrapper code for 2.2.16-2.4.0
48  *      20010118-tw     basic PM support for 2.2.16+ and 2.4.0/2.4.2.
49  *      20010228-dh     patch from David Huggins - cs_update_ptr recursion.
50  *      20010409-tw     add hercules game theatre XP amp code.
51  *      20010420-tw     cleanup powerdown/up code.
52  *      20010521-tw     eliminate pops, and fixes for powerdown.
53  *      20010525-tw     added fixes for thinkpads with powerdown logic.
54  *      20010723-sh     patch from Horms (Simon Horman) -
55  *                      SOUND_PCM_READ_BITS returns bits as set in driver
56  *                      rather than a logical or of the possible values.
57  *                      Various ioctls handle the case where the device
58  *                      is open for reading or writing but not both better.
59  *
60  *      Status:
61  *      Playback/Capture supported from 8k-48k.
62  *      16Bit Signed LE & 8Bit Unsigned, with Mono or Stereo supported.
63  *
64  *      APM/PM - 2.2.x APM is enabled and functioning fine. APM can also
65  *      be enabled for 2.4.x by modifying the CS46XX_ACPI_SUPPORT macro
66  *      definition.
67  *
68  *      Hercules Game Theatre XP - the EGPIO2 pin controls the external Amp,
69  *      so, use the drain/polarity to enable.  
70  *      hercules_egpio_disable set to 1, will force a 0 to EGPIODR.
71  *
72  *      VTB Santa Cruz - the GPIO7/GPIO8 on the Secondary Codec control
73  *      the external amplifier for the "back" speakers, since we do not
74  *      support the secondary codec then this external amp is also not
75  *      turned on.
76  */
77  
78 #include <linux/interrupt.h>
79 #include <linux/list.h>
80 #include <linux/module.h>
81 #include <linux/string.h>
82 #include <linux/ioport.h>
83 #include <linux/sched.h>
84 #include <linux/delay.h>
85 #include <linux/sound.h>
86 #include <linux/slab.h>
87 #include <linux/soundcard.h>
88 #include <linux/pci.h>
89 #include <linux/bitops.h>
90 #include <linux/init.h>
91 #include <linux/poll.h>
92 #include <linux/ac97_codec.h>
93 #include <linux/mutex.h>
94
95 #include <asm/io.h>
96 #include <asm/dma.h>
97 #include <asm/uaccess.h>
98
99 #include "cs46xxpm-24.h"
100 #include "cs46xx_wrapper-24.h"
101 #include "cs461x.h"
102
103 /* MIDI buffer sizes */
104 #define CS_MIDIINBUF  500
105 #define CS_MIDIOUTBUF 500
106
107 #define ADC_RUNNING     1
108 #define DAC_RUNNING     2
109
110 #define CS_FMT_16BIT    1               /* These are fixed in fact */
111 #define CS_FMT_STEREO   2
112 #define CS_FMT_MASK     3
113
114 #define CS_TYPE_ADC     1
115 #define CS_TYPE_DAC     2
116
117 #define CS_TRUE         1
118 #define CS_FALSE        0
119
120 #define CS_INC_USE_COUNT(m) (atomic_inc(m))
121 #define CS_DEC_USE_COUNT(m) (atomic_dec(m))
122 #define CS_DEC_AND_TEST(m) (atomic_dec_and_test(m))
123 #define CS_IN_USE(m) (atomic_read(m) != 0)
124
125 #define CS_DBGBREAKPOINT {__asm__("INT $3");}
126 /*
127  *      CS461x definitions
128  */
129  
130 #define CS461X_BA0_SIZE         0x2000
131 #define CS461X_BA1_DATA0_SIZE   0x3000
132 #define CS461X_BA1_DATA1_SIZE   0x3800
133 #define CS461X_BA1_PRG_SIZE     0x7000
134 #define CS461X_BA1_REG_SIZE     0x0100
135
136 #define GOF_PER_SEC     200
137
138 #define CSDEBUG_INTERFACE 1
139 #define CSDEBUG 1
140 /*
141  * Turn on/off debugging compilation by using 1/0 respectively for CSDEBUG
142  *
143  *
144  * CSDEBUG is usual mode is set to 1, then use the
145  * cs_debuglevel and cs_debugmask to turn on or off debugging.
146  * Debug level of 1 has been defined to be kernel errors and info
147  * that should be printed on any released driver.
148  */
149 #if CSDEBUG
150 #define CS_DBGOUT(mask,level,x) if((cs_debuglevel >= (level)) && ((mask) & cs_debugmask)) {x;} 
151 #else
152 #define CS_DBGOUT(mask,level,x) 
153 #endif
154 /*
155  * cs_debugmask areas
156  */
157 #define CS_INIT         0x00000001              /* initialization and probe functions */
158 #define CS_ERROR        0x00000002              /* tmp debugging bit placeholder */
159 #define CS_INTERRUPT    0x00000004              /* interrupt handler (separate from all other) */
160 #define CS_FUNCTION     0x00000008              /* enter/leave functions */
161 #define CS_WAVE_WRITE   0x00000010              /* write information for wave */
162 #define CS_WAVE_READ    0x00000020              /* read information for wave */
163 #define CS_MIDI_WRITE   0x00000040              /* write information for midi */
164 #define CS_MIDI_READ    0x00000080              /* read information for midi */
165 #define CS_MPU401_WRITE 0x00000100              /* write information for mpu401 */
166 #define CS_MPU401_READ  0x00000200              /* read information for mpu401 */
167 #define CS_OPEN         0x00000400              /* all open functions in the driver */
168 #define CS_RELEASE      0x00000800              /* all release functions in the driver */
169 #define CS_PARMS        0x00001000              /* functional and operational parameters */
170 #define CS_IOCTL        0x00002000              /* ioctl (non-mixer) */
171 #define CS_PM           0x00004000              /* PM */
172 #define CS_TMP          0x10000000              /* tmp debug mask bit */
173
174 #define CS_IOCTL_CMD_SUSPEND    0x1     // suspend
175 #define CS_IOCTL_CMD_RESUME     0x2     // resume
176
177 #if CSDEBUG
178 static unsigned long cs_debuglevel=1;                   /* levels range from 1-9 */
179 module_param(cs_debuglevel, ulong, 0644);
180 static unsigned long cs_debugmask=CS_INIT | CS_ERROR;   /* use CS_DBGOUT with various mask values */
181 module_param(cs_debugmask, ulong, 0644);
182 #endif
183 static unsigned long hercules_egpio_disable;  /* if non-zero set all EGPIO to 0 */
184 module_param(hercules_egpio_disable, ulong, 0);
185 static unsigned long initdelay=700;  /* PM delay in millisecs */
186 module_param(initdelay, ulong, 0);
187 static unsigned long powerdown=-1;  /* turn on/off powerdown processing in driver */
188 module_param(powerdown, ulong, 0);
189 #define DMABUF_DEFAULTORDER 3
190 static unsigned long defaultorder=DMABUF_DEFAULTORDER;
191 module_param(defaultorder, ulong, 0);
192
193 static int external_amp;
194 module_param(external_amp, bool, 0);
195 static int thinkpad;
196 module_param(thinkpad, bool, 0);
197
198 /*
199 * set the powerdown module parm to 0 to disable all 
200 * powerdown. also set thinkpad to 1 to disable powerdown, 
201 * but also to enable the clkrun functionality.
202 */
203 static unsigned cs_powerdown=1;
204 static unsigned cs_laptop_wait=1;
205
206 /* An instance of the 4610 channel */
207 struct cs_channel 
208 {
209         int used;
210         int num;
211         void *state;
212 };
213
214 #define CS46XX_MAJOR_VERSION "1"
215 #define CS46XX_MINOR_VERSION "28"
216
217 #ifdef __ia64__
218 #define CS46XX_ARCH             "64"    //architecture key
219 #else
220 #define CS46XX_ARCH             "32"    //architecture key
221 #endif
222
223 static struct list_head cs46xx_devs = { &cs46xx_devs, &cs46xx_devs };
224
225 /* magic numbers to protect our data structures */
226 #define CS_CARD_MAGIC           0x43525553 /* "CRUS" */
227 #define CS_STATE_MAGIC          0x4c4f4749 /* "LOGI" */
228 #define NR_HW_CH                3
229
230 /* maxinum number of AC97 codecs connected, AC97 2.0 defined 4 */
231 #define NR_AC97         2
232
233 static const unsigned sample_size[] = { 1, 2, 2, 4 };
234 static const unsigned sample_shift[] = { 0, 1, 1, 2 };
235
236 /* "software" or virtual channel, an instance of opened /dev/dsp */
237 struct cs_state {
238         unsigned int magic;
239         struct cs_card *card;   /* Card info */
240
241         /* single open lock mechanism, only used for recording */
242         struct mutex open_mutex;
243         wait_queue_head_t open_wait;
244
245         /* file mode */
246         mode_t open_mode;
247
248         /* virtual channel number */
249         int virt;
250         
251         struct dmabuf {
252                 /* wave sample stuff */
253                 unsigned int rate;
254                 unsigned char fmt, enable;
255
256                 /* hardware channel */
257                 struct cs_channel *channel;
258                 int pringbuf;           /* Software ring slot */
259                 void *pbuf;             /* 4K hardware DMA buffer */
260
261                 /* OSS buffer management stuff */
262                 void *rawbuf;
263                 dma_addr_t dma_handle;
264                 unsigned buforder;
265                 unsigned numfrag;
266                 unsigned fragshift;
267                 unsigned divisor;
268                 unsigned type;
269                 void *tmpbuff;                  /* tmp buffer for sample conversions */
270                 dma_addr_t dmaaddr;
271                 dma_addr_t dmaaddr_tmpbuff;
272                 unsigned buforder_tmpbuff;      /* Log base 2 of size in bytes.. */
273
274                 /* our buffer acts like a circular ring */
275                 unsigned hwptr;         /* where dma last started, updated by update_ptr */
276                 unsigned swptr;         /* where driver last clear/filled, updated by read/write */
277                 int count;              /* bytes to be comsumed or been generated by dma machine */
278                 unsigned total_bytes;   /* total bytes dmaed by hardware */
279                 unsigned blocks;        /* total blocks */
280
281                 unsigned error;         /* number of over/underruns */
282                 unsigned underrun;      /* underrun pending before next write has occurred */
283                 wait_queue_head_t wait; /* put process on wait queue when no more space in buffer */
284
285                 /* redundant, but makes calculations easier */
286                 unsigned fragsize;
287                 unsigned dmasize;
288                 unsigned fragsamples;
289
290                 /* OSS stuff */
291                 unsigned mapped:1;
292                 unsigned ready:1;
293                 unsigned endcleared:1;
294                 unsigned SGok:1;
295                 unsigned update_flag;
296                 unsigned ossfragshift;
297                 int ossmaxfrags;
298                 unsigned subdivision;
299         } dmabuf;
300         /* Guard against mmap/write/read races */
301         struct mutex sem;
302 };
303
304 struct cs_card {
305         struct cs_channel channel[2];
306         unsigned int magic;
307
308         /* We keep cs461x cards in a linked list */
309         struct cs_card *next;
310
311         /* The cs461x has a certain amount of cross channel interaction
312            so we use a single per card lock */
313         spinlock_t lock;
314         
315         /* Keep AC97 sane */
316         spinlock_t ac97_lock;
317
318         /* mixer use count */
319         atomic_t mixer_use_cnt;
320
321         /* PCI device stuff */
322         struct pci_dev * pci_dev;
323         struct list_head list;
324
325         unsigned int pctl, cctl;        /* Hardware DMA flag sets */
326
327         /* soundcore stuff */
328         int dev_audio;
329         int dev_midi;
330
331         /* structures for abstraction of hardware facilities, codecs, banks and channels*/
332         struct ac97_codec *ac97_codec[NR_AC97];
333         struct cs_state *states[2];
334
335         u16 ac97_features;
336         
337         int amplifier;                  /* Amplifier control */
338         void (*amplifier_ctrl)(struct cs_card *, int);
339         void (*amp_init)(struct cs_card *);
340         
341         int active;                     /* Active clocking */
342         void (*active_ctrl)(struct cs_card *, int);
343         
344         /* hardware resources */
345         unsigned long ba0_addr;
346         unsigned long ba1_addr;
347         u32 irq;
348         
349         /* mappings */
350         void __iomem *ba0;
351         union
352         {
353                 struct
354                 {
355                         u8 __iomem *data0;
356                         u8 __iomem *data1;
357                         u8 __iomem *pmem;
358                         u8 __iomem *reg;
359                 } name;
360                 u8 __iomem *idx[4];
361         } ba1;
362         
363         /* Function support */
364         struct cs_channel *(*alloc_pcm_channel)(struct cs_card *);
365         struct cs_channel *(*alloc_rec_pcm_channel)(struct cs_card *);
366         void (*free_pcm_channel)(struct cs_card *, int chan);
367
368         /* /dev/midi stuff */
369         struct {
370                 unsigned ird, iwr, icnt;
371                 unsigned ord, owr, ocnt;
372                 wait_queue_head_t open_wait;
373                 wait_queue_head_t iwait;
374                 wait_queue_head_t owait;
375                 spinlock_t lock;
376                 unsigned char ibuf[CS_MIDIINBUF];
377                 unsigned char obuf[CS_MIDIOUTBUF];
378                 mode_t open_mode;
379                 struct mutex open_mutex;
380         } midi;
381         struct cs46xx_pm pm;
382 };
383
384 static int cs_open_mixdev(struct inode *inode, struct file *file);
385 static int cs_release_mixdev(struct inode *inode, struct file *file);
386 static int cs_ioctl_mixdev(struct inode *inode, struct file *file, unsigned int cmd,
387                                 unsigned long arg);
388 static int cs_hardware_init(struct cs_card *card);
389 static int cs46xx_powerup(struct cs_card *card, unsigned int type);
390 static int cs461x_powerdown(struct cs_card *card, unsigned int type, int suspendflag);
391 static void cs461x_clear_serial_FIFOs(struct cs_card *card, int type);
392 static int cs46xx_suspend_tbl(struct pci_dev *pcidev, pm_message_t state);
393 static int cs46xx_resume_tbl(struct pci_dev *pcidev);
394
395 #if CSDEBUG
396
397 /* DEBUG ROUTINES */
398
399 #define SOUND_MIXER_CS_GETDBGLEVEL      _SIOWR('M',120, int)
400 #define SOUND_MIXER_CS_SETDBGLEVEL      _SIOWR('M',121, int)
401 #define SOUND_MIXER_CS_GETDBGMASK       _SIOWR('M',122, int)
402 #define SOUND_MIXER_CS_SETDBGMASK       _SIOWR('M',123, int)
403 #define SOUND_MIXER_CS_APM              _SIOWR('M',124, int)
404
405 static void printioctl(unsigned int x)
406 {
407     unsigned int i;
408     unsigned char vidx;
409         /* these values are incorrect for the ac97 driver, fix.
410          * Index of mixtable1[] member is Device ID 
411          * and must be <= SOUND_MIXER_NRDEVICES.
412          * Value of array member is index into s->mix.vol[]
413          */
414         static const unsigned char mixtable1[SOUND_MIXER_NRDEVICES] = {
415                 [SOUND_MIXER_PCM]     = 1,   /* voice */
416                 [SOUND_MIXER_LINE1]   = 2,   /* AUX */
417                 [SOUND_MIXER_CD]      = 3,   /* CD */
418                 [SOUND_MIXER_LINE]    = 4,   /* Line */
419                 [SOUND_MIXER_SYNTH]   = 5,   /* FM */
420                 [SOUND_MIXER_MIC]     = 6,   /* Mic */
421                 [SOUND_MIXER_SPEAKER] = 7,   /* Speaker */
422                 [SOUND_MIXER_RECLEV]  = 8,   /* Recording level */
423                 [SOUND_MIXER_VOLUME]  = 9    /* Master Volume */
424         };
425         
426     switch(x) 
427     {
428         case SOUND_MIXER_CS_GETDBGMASK:
429                 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_CS_GETDBGMASK: ") );
430                 break;
431         case SOUND_MIXER_CS_GETDBGLEVEL:
432                 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_CS_GETDBGLEVEL: ") );
433                 break;
434         case SOUND_MIXER_CS_SETDBGMASK:
435                 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_CS_SETDBGMASK: ") );
436                 break;
437         case SOUND_MIXER_CS_SETDBGLEVEL:
438                 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_CS_SETDBGLEVEL: ") );
439                 break;
440         case OSS_GETVERSION:
441                 CS_DBGOUT(CS_IOCTL, 4, printk("OSS_GETVERSION: ") );
442                 break;
443         case SNDCTL_DSP_SYNC:
444                 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_SYNC: ") );
445                 break;
446         case SNDCTL_DSP_SETDUPLEX:
447                 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_SETDUPLEX: ") );
448                 break;
449         case SNDCTL_DSP_GETCAPS:
450                 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_GETCAPS: ") );
451                 break;
452         case SNDCTL_DSP_RESET:
453                 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_RESET: ") );
454                 break;
455         case SNDCTL_DSP_SPEED:
456                 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_SPEED: ") );
457                 break;
458         case SNDCTL_DSP_STEREO:
459                 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_STEREO: ") );
460                 break;
461         case SNDCTL_DSP_CHANNELS:
462                 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_CHANNELS: ") );
463                 break;
464         case SNDCTL_DSP_GETFMTS: 
465                 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_GETFMTS: ") );
466                 break;
467         case SNDCTL_DSP_SETFMT: 
468                 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_SETFMT: ") );
469                 break;
470         case SNDCTL_DSP_POST:
471                 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_POST: ") );
472                 break;
473         case SNDCTL_DSP_GETTRIGGER:
474                 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_GETTRIGGER: ") );
475                 break;
476         case SNDCTL_DSP_SETTRIGGER:
477                 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_SETTRIGGER: ") );
478                 break;
479         case SNDCTL_DSP_GETOSPACE:
480                 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_GETOSPACE: ") );
481                 break;
482         case SNDCTL_DSP_GETISPACE:
483                 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_GETISPACE: ") );
484                 break;
485         case SNDCTL_DSP_NONBLOCK:
486                 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_NONBLOCK: ") );
487                 break;
488         case SNDCTL_DSP_GETODELAY:
489                 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_GETODELAY: ") );
490                 break;
491         case SNDCTL_DSP_GETIPTR:
492                 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_GETIPTR: ") );
493                 break;
494         case SNDCTL_DSP_GETOPTR:
495                 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_GETOPTR: ") );
496                 break;
497         case SNDCTL_DSP_GETBLKSIZE:
498                 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_GETBLKSIZE: ") );
499                 break;
500         case SNDCTL_DSP_SETFRAGMENT:
501                 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_SETFRAGMENT: ") );
502                 break;
503         case SNDCTL_DSP_SUBDIVIDE:
504                 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_SUBDIVIDE: ") );
505                 break;
506         case SOUND_PCM_READ_RATE:
507                 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_PCM_READ_RATE: ") );
508                 break;
509         case SOUND_PCM_READ_CHANNELS:
510                 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_PCM_READ_CHANNELS: ") );
511                 break;
512         case SOUND_PCM_READ_BITS:
513                 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_PCM_READ_BITS: ") );
514                 break;
515         case SOUND_PCM_WRITE_FILTER:
516                 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_PCM_WRITE_FILTER: ") );
517                 break;
518         case SNDCTL_DSP_SETSYNCRO:
519                 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_SETSYNCRO: ") );
520                 break;
521         case SOUND_PCM_READ_FILTER:
522                 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_PCM_READ_FILTER: ") );
523                 break;
524
525         case SOUND_MIXER_PRIVATE1:
526                 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_PRIVATE1: ") );
527                 break;
528         case SOUND_MIXER_PRIVATE2:
529                 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_PRIVATE2: ") );
530                 break;
531         case SOUND_MIXER_PRIVATE3:
532                 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_PRIVATE3: ") );
533                 break;
534         case SOUND_MIXER_PRIVATE4:
535                 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_PRIVATE4: ") );
536                 break;
537         case SOUND_MIXER_PRIVATE5:
538                 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_PRIVATE5: ") );
539                 break;
540         case SOUND_MIXER_INFO:
541                 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_INFO: ") );
542                 break;
543         case SOUND_OLD_MIXER_INFO:
544                 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_OLD_MIXER_INFO: ") );
545                 break;
546
547         default:
548                 switch (_IOC_NR(x)) 
549                 {
550                         case SOUND_MIXER_VOLUME:
551                                 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_VOLUME: ") );
552                                 break;
553                         case SOUND_MIXER_SPEAKER:
554                                 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_SPEAKER: ") );
555                                 break;
556                         case SOUND_MIXER_RECLEV:
557                                 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_RECLEV: ") );
558                                 break;
559                         case SOUND_MIXER_MIC:
560                                 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_MIC: ") );
561                                 break;
562                         case SOUND_MIXER_SYNTH:
563                                 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_SYNTH: ") );
564                                 break;
565                         case SOUND_MIXER_RECSRC: 
566                                 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_RECSRC: ") );
567                                 break;
568                         case SOUND_MIXER_DEVMASK:
569                                 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_DEVMASK: ") );
570                                 break;
571                         case SOUND_MIXER_RECMASK:
572                                 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_RECMASK: ") );
573                                 break;
574                         case SOUND_MIXER_STEREODEVS: 
575                                 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_STEREODEVS: ") );
576                                 break;
577                         case SOUND_MIXER_CAPS:
578                                 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_CAPS:") );
579                                 break;
580                         default:
581                                 i = _IOC_NR(x);
582                                 if (i >= SOUND_MIXER_NRDEVICES || !(vidx = mixtable1[i]))
583                                 {
584                                         CS_DBGOUT(CS_IOCTL, 4, printk("UNKNOWN IOCTL: 0x%.8x NR=%d ",x,i) );
585                                 }
586                                 else
587                                 {
588                                         CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_IOCTL AC9x: 0x%.8x NR=%d ",
589                                                         x,i) );
590                                 }
591                                 break;
592                 }
593     }
594     CS_DBGOUT(CS_IOCTL, 4, printk("command = 0x%x IOC_NR=%d\n",x, _IOC_NR(x)) );
595 }
596 #endif
597
598 /*
599  *  common I/O routines
600  */
601
602 static void cs461x_poke(struct cs_card *codec, unsigned long reg, unsigned int val)
603 {
604         writel(val, codec->ba1.idx[(reg >> 16) & 3]+(reg&0xffff));
605 }
606
607 static unsigned int cs461x_peek(struct cs_card *codec, unsigned long reg)
608 {
609         return readl(codec->ba1.idx[(reg >> 16) & 3]+(reg&0xffff));
610 }
611
612 static void cs461x_pokeBA0(struct cs_card *codec, unsigned long reg, unsigned int val)
613 {
614         writel(val, codec->ba0+reg);
615 }
616
617 static unsigned int cs461x_peekBA0(struct cs_card *codec, unsigned long reg)
618 {
619         return readl(codec->ba0+reg);
620 }
621
622
623 static u16 cs_ac97_get(struct ac97_codec *dev, u8 reg);
624 static void cs_ac97_set(struct ac97_codec *dev, u8 reg, u16 data);
625
626 static struct cs_channel *cs_alloc_pcm_channel(struct cs_card *card)
627 {
628         if(card->channel[1].used==1)
629                 return NULL;
630         card->channel[1].used=1;
631         card->channel[1].num=1;
632         return &card->channel[1];
633 }
634
635 static struct cs_channel *cs_alloc_rec_pcm_channel(struct cs_card *card)
636 {
637         if(card->channel[0].used==1)
638                 return NULL;
639         card->channel[0].used=1;
640         card->channel[0].num=0;
641         return &card->channel[0];
642 }
643
644 static void cs_free_pcm_channel(struct cs_card *card, int channel)
645 {
646         card->channel[channel].state = NULL;
647         card->channel[channel].used=0;
648 }
649
650 /*
651  * setup a divisor value to help with conversion from
652  * 16bit Stereo, down to 8bit stereo/mono or 16bit mono.
653  * assign a divisor of 1 if using 16bit Stereo as that is
654  * the only format that the static image will capture.
655  */
656 static void cs_set_divisor(struct dmabuf *dmabuf)
657 {
658         if(dmabuf->type == CS_TYPE_DAC)
659                 dmabuf->divisor = 1;
660         else if( !(dmabuf->fmt & CS_FMT_STEREO) && 
661             (dmabuf->fmt & CS_FMT_16BIT))
662                 dmabuf->divisor = 2;
663         else if( (dmabuf->fmt & CS_FMT_STEREO) && 
664             !(dmabuf->fmt & CS_FMT_16BIT))
665                 dmabuf->divisor = 2;
666         else if( !(dmabuf->fmt & CS_FMT_STEREO) && 
667             !(dmabuf->fmt & CS_FMT_16BIT))
668                 dmabuf->divisor = 4;
669         else
670                 dmabuf->divisor = 1;
671
672         CS_DBGOUT(CS_PARMS | CS_FUNCTION, 8, printk(
673                 "cs46xx: cs_set_divisor()- %s %d\n",
674                         (dmabuf->type == CS_TYPE_ADC) ? "ADC" : "DAC", 
675                         dmabuf->divisor) );
676 }
677
678 /*
679 * mute some of the more prevalent registers to avoid popping.
680 */
681 static void cs_mute(struct cs_card *card, int state) 
682 {
683         struct ac97_codec *dev=card->ac97_codec[0];
684
685         CS_DBGOUT(CS_FUNCTION, 2, printk(KERN_INFO "cs46xx: cs_mute()+ %s\n",
686                 (state == CS_TRUE) ? "Muting" : "UnMuting") );
687
688         if(state == CS_TRUE)
689         {
690         /*
691         * fix pops when powering up on thinkpads
692         */
693                 card->pm.u32AC97_master_volume = (u32)cs_ac97_get( dev, 
694                                 (u8)BA0_AC97_MASTER_VOLUME); 
695                 card->pm.u32AC97_headphone_volume = (u32)cs_ac97_get(dev, 
696                                 (u8)BA0_AC97_HEADPHONE_VOLUME); 
697                 card->pm.u32AC97_master_volume_mono = (u32)cs_ac97_get(dev, 
698                                 (u8)BA0_AC97_MASTER_VOLUME_MONO); 
699                 card->pm.u32AC97_pcm_out_volume = (u32)cs_ac97_get(dev, 
700                                 (u8)BA0_AC97_PCM_OUT_VOLUME);
701                         
702                 cs_ac97_set(dev, (u8)BA0_AC97_MASTER_VOLUME, 0x8000);
703                 cs_ac97_set(dev, (u8)BA0_AC97_HEADPHONE_VOLUME, 0x8000);
704                 cs_ac97_set(dev, (u8)BA0_AC97_MASTER_VOLUME_MONO, 0x8000);
705                 cs_ac97_set(dev, (u8)BA0_AC97_PCM_OUT_VOLUME, 0x8000);
706         }
707         else
708         {
709                 cs_ac97_set(dev, (u8)BA0_AC97_MASTER_VOLUME, card->pm.u32AC97_master_volume);
710                 cs_ac97_set(dev, (u8)BA0_AC97_HEADPHONE_VOLUME, card->pm.u32AC97_headphone_volume);
711                 cs_ac97_set(dev, (u8)BA0_AC97_MASTER_VOLUME_MONO, card->pm.u32AC97_master_volume_mono);
712                 cs_ac97_set(dev, (u8)BA0_AC97_PCM_OUT_VOLUME, card->pm.u32AC97_pcm_out_volume);
713         }
714         CS_DBGOUT(CS_FUNCTION, 2, printk(KERN_INFO "cs46xx: cs_mute()-\n"));
715 }
716
717 /* set playback sample rate */
718 static unsigned int cs_set_dac_rate(struct cs_state * state, unsigned int rate)
719 {       
720         struct dmabuf *dmabuf = &state->dmabuf;
721         unsigned int tmp1, tmp2;
722         unsigned int phiIncr;
723         unsigned int correctionPerGOF, correctionPerSec;
724         unsigned long flags;
725
726         CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: cs_set_dac_rate()+ %d\n",rate) );
727
728         /*
729          *  Compute the values used to drive the actual sample rate conversion.
730          *  The following formulas are being computed, using inline assembly
731          *  since we need to use 64 bit arithmetic to compute the values:
732          *
733          *  phiIncr = floor((Fs,in * 2^26) / Fs,out)
734          *  correctionPerGOF = floor((Fs,in * 2^26 - Fs,out * phiIncr) /
735          *                                   GOF_PER_SEC)
736          *  ulCorrectionPerSec = Fs,in * 2^26 - Fs,out * phiIncr -M
737          *                       GOF_PER_SEC * correctionPerGOF
738          *
739          *  i.e.
740          *
741          *  phiIncr:other = dividend:remainder((Fs,in * 2^26) / Fs,out)
742          *  correctionPerGOF:correctionPerSec =
743          *      dividend:remainder(ulOther / GOF_PER_SEC)
744          */
745         tmp1 = rate << 16;
746         phiIncr = tmp1 / 48000;
747         tmp1 -= phiIncr * 48000;
748         tmp1 <<= 10;
749         phiIncr <<= 10;
750         tmp2 = tmp1 / 48000;
751         phiIncr += tmp2;
752         tmp1 -= tmp2 * 48000;
753         correctionPerGOF = tmp1 / GOF_PER_SEC;
754         tmp1 -= correctionPerGOF * GOF_PER_SEC;
755         correctionPerSec = tmp1;
756
757         /*
758          *  Fill in the SampleRateConverter control block.
759          */
760          
761         spin_lock_irqsave(&state->card->lock, flags);
762         cs461x_poke(state->card, BA1_PSRC,
763           ((correctionPerSec << 16) & 0xFFFF0000) | (correctionPerGOF & 0xFFFF));
764         cs461x_poke(state->card, BA1_PPI, phiIncr);
765         spin_unlock_irqrestore(&state->card->lock, flags);
766         dmabuf->rate = rate;
767         
768         CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: cs_set_dac_rate()- %d\n",rate) );
769         return rate;
770 }
771
772 /* set recording sample rate */
773 static unsigned int cs_set_adc_rate(struct cs_state * state, unsigned int rate)
774 {
775         struct dmabuf *dmabuf = &state->dmabuf;
776         struct cs_card *card = state->card;
777         unsigned int phiIncr, coeffIncr, tmp1, tmp2;
778         unsigned int correctionPerGOF, correctionPerSec, initialDelay;
779         unsigned int frameGroupLength, cnt;
780         unsigned long flags;
781         CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: cs_set_adc_rate()+ %d\n",rate) );
782
783         /*
784          *  We can only decimate by up to a factor of 1/9th the hardware rate.
785          *  Correct the value if an attempt is made to stray outside that limit.
786          */
787         if ((rate * 9) < 48000)
788                 rate = 48000 / 9;
789
790         /*
791          *  We can not capture at at rate greater than the Input Rate (48000).
792          *  Return an error if an attempt is made to stray outside that limit.
793          */
794         if (rate > 48000)
795                 rate = 48000;
796
797         /*
798          *  Compute the values used to drive the actual sample rate conversion.
799          *  The following formulas are being computed, using inline assembly
800          *  since we need to use 64 bit arithmetic to compute the values:
801          *
802          *     coeffIncr = -floor((Fs,out * 2^23) / Fs,in)
803          *     phiIncr = floor((Fs,in * 2^26) / Fs,out)
804          *     correctionPerGOF = floor((Fs,in * 2^26 - Fs,out * phiIncr) /
805          *                                GOF_PER_SEC)
806          *     correctionPerSec = Fs,in * 2^26 - Fs,out * phiIncr -
807          *                          GOF_PER_SEC * correctionPerGOF
808          *     initialDelay = ceil((24 * Fs,in) / Fs,out)
809          *
810          * i.e.
811          *
812          *     coeffIncr = neg(dividend((Fs,out * 2^23) / Fs,in))
813          *     phiIncr:ulOther = dividend:remainder((Fs,in * 2^26) / Fs,out)
814          *     correctionPerGOF:correctionPerSec =
815          *          dividend:remainder(ulOther / GOF_PER_SEC)
816          *     initialDelay = dividend(((24 * Fs,in) + Fs,out - 1) / Fs,out)
817          */
818
819         tmp1 = rate << 16;
820         coeffIncr = tmp1 / 48000;
821         tmp1 -= coeffIncr * 48000;
822         tmp1 <<= 7;
823         coeffIncr <<= 7;
824         coeffIncr += tmp1 / 48000;
825         coeffIncr ^= 0xFFFFFFFF;
826         coeffIncr++;
827         tmp1 = 48000 << 16;
828         phiIncr = tmp1 / rate;
829         tmp1 -= phiIncr * rate;
830         tmp1 <<= 10;
831         phiIncr <<= 10;
832         tmp2 = tmp1 / rate;
833         phiIncr += tmp2;
834         tmp1 -= tmp2 * rate;
835         correctionPerGOF = tmp1 / GOF_PER_SEC;
836         tmp1 -= correctionPerGOF * GOF_PER_SEC;
837         correctionPerSec = tmp1;
838         initialDelay = ((48000 * 24) + rate - 1) / rate;
839
840         /*
841          *  Fill in the VariDecimate control block.
842          */
843         spin_lock_irqsave(&card->lock, flags);
844         cs461x_poke(card, BA1_CSRC,
845                 ((correctionPerSec << 16) & 0xFFFF0000) | (correctionPerGOF & 0xFFFF));
846         cs461x_poke(card, BA1_CCI, coeffIncr);
847         cs461x_poke(card, BA1_CD,
848                 (((BA1_VARIDEC_BUF_1 + (initialDelay << 2)) << 16) & 0xFFFF0000) | 0x80);
849         cs461x_poke(card, BA1_CPI, phiIncr);
850         spin_unlock_irqrestore(&card->lock, flags);
851
852         /*
853          *  Figure out the frame group length for the write back task.  Basically,
854          *  this is just the factors of 24000 (2^6*3*5^3) that are not present in
855          *  the output sample rate.
856          */
857         frameGroupLength = 1;
858         for (cnt = 2; cnt <= 64; cnt *= 2) {
859                 if (((rate / cnt) * cnt) != rate)
860                         frameGroupLength *= 2;
861         }
862         if (((rate / 3) * 3) != rate) {
863                 frameGroupLength *= 3;
864         }
865         for (cnt = 5; cnt <= 125; cnt *= 5) {
866                 if (((rate / cnt) * cnt) != rate) 
867                         frameGroupLength *= 5;
868         }
869
870         /*
871          * Fill in the WriteBack control block.
872          */
873         spin_lock_irqsave(&card->lock, flags);
874         cs461x_poke(card, BA1_CFG1, frameGroupLength);
875         cs461x_poke(card, BA1_CFG2, (0x00800000 | frameGroupLength));
876         cs461x_poke(card, BA1_CCST, 0x0000FFFF);
877         cs461x_poke(card, BA1_CSPB, ((65536 * rate) / 24000));
878         cs461x_poke(card, (BA1_CSPB + 4), 0x0000FFFF);
879         spin_unlock_irqrestore(&card->lock, flags);
880         dmabuf->rate = rate;
881         CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: cs_set_adc_rate()- %d\n",rate) );
882         return rate;
883 }
884
885 /* prepare channel attributes for playback */ 
886 static void cs_play_setup(struct cs_state *state)
887 {
888         struct dmabuf *dmabuf = &state->dmabuf;
889         struct cs_card *card = state->card;
890         unsigned int tmp, Count, playFormat;
891
892         CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: cs_play_setup()+\n") );
893         cs461x_poke(card, BA1_PVOL, 0x80008000);
894         if(!dmabuf->SGok)
895                cs461x_poke(card, BA1_PBA, virt_to_bus(dmabuf->pbuf));
896     
897         Count = 4;                                                          
898         playFormat=cs461x_peek(card, BA1_PFIE);                             
899         if ((dmabuf->fmt & CS_FMT_STEREO)) {                                
900                 playFormat &= ~DMA_RQ_C2_AC_MONO_TO_STEREO;                 
901                 Count *= 2;                                                 
902         }                                                                   
903         else                                                                
904                 playFormat |= DMA_RQ_C2_AC_MONO_TO_STEREO;                  
905                                                                             
906         if ((dmabuf->fmt & CS_FMT_16BIT)) {                                 
907                 playFormat &= ~(DMA_RQ_C2_AC_8_TO_16_BIT                    
908                            | DMA_RQ_C2_AC_SIGNED_CONVERT);                  
909                 Count *= 2;                                                 
910         }                                                                   
911         else                                                                
912                 playFormat |= (DMA_RQ_C2_AC_8_TO_16_BIT                     
913                            | DMA_RQ_C2_AC_SIGNED_CONVERT);                  
914                                                                             
915         cs461x_poke(card, BA1_PFIE, playFormat);                            
916                                                                             
917         tmp = cs461x_peek(card, BA1_PDTC);                                  
918         tmp &= 0xfffffe00;                                                  
919         cs461x_poke(card, BA1_PDTC, tmp | --Count);                         
920
921         CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: cs_play_setup()-\n") );
922
923 }
924
925 static struct InitStruct
926 {
927     u32 off;
928     u32 val;
929 } InitArray[] = { {0x00000040, 0x3fc0000f},
930                   {0x0000004c, 0x04800000},
931
932                   {0x000000b3, 0x00000780},
933                   {0x000000b7, 0x00000000},
934                   {0x000000bc, 0x07800000},
935
936                   {0x000000cd, 0x00800000},
937                 };
938
939 /*
940  * "SetCaptureSPValues()" -- Initialize record task values before each
941  *      capture startup.  
942  */
943 static void SetCaptureSPValues(struct cs_card *card)
944 {
945         unsigned i, offset;
946         CS_DBGOUT(CS_FUNCTION, 8, printk("cs46xx: SetCaptureSPValues()+\n") );
947         for(i=0; i<sizeof(InitArray)/sizeof(struct InitStruct); i++)
948         {
949                 offset = InitArray[i].off*4; /* 8bit to 32bit offset value */
950                 cs461x_poke(card, offset, InitArray[i].val );
951         }
952         CS_DBGOUT(CS_FUNCTION, 8, printk("cs46xx: SetCaptureSPValues()-\n") );
953 }
954
955 /* prepare channel attributes for recording */
956 static void cs_rec_setup(struct cs_state *state)
957 {
958         struct cs_card *card = state->card;
959         struct dmabuf *dmabuf = &state->dmabuf;
960         CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: cs_rec_setup()+\n") );
961
962         SetCaptureSPValues(card);
963
964         /*
965          * set the attenuation to 0dB 
966          */
967         cs461x_poke(card, BA1_CVOL, 0x80008000);
968
969         /*
970          * set the physical address of the capture buffer into the SP
971          */
972         cs461x_poke(card, BA1_CBA, virt_to_bus(dmabuf->rawbuf));
973
974         CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: cs_rec_setup()-\n") );
975 }
976
977
978 /* get current playback/recording dma buffer pointer (byte offset from LBA),
979    called with spinlock held! */
980    
981 static inline unsigned cs_get_dma_addr(struct cs_state *state)
982 {
983         struct dmabuf *dmabuf = &state->dmabuf;
984         u32 offset;
985         
986         if ( (!(dmabuf->enable & DAC_RUNNING)) &&
987              (!(dmabuf->enable & ADC_RUNNING) ) )
988         {
989                 CS_DBGOUT(CS_ERROR, 2, printk(
990                         "cs46xx: ERROR cs_get_dma_addr(): not enabled \n") );
991                 return 0;
992         }
993                 
994         /*
995          * granularity is byte boundary, good part.
996          */
997         if(dmabuf->enable & DAC_RUNNING)
998         {
999                 offset = cs461x_peek(state->card, BA1_PBA);                                  
1000         }
1001         else /* ADC_RUNNING must be set */
1002         {
1003                 offset = cs461x_peek(state->card, BA1_CBA);                                  
1004         }
1005         CS_DBGOUT(CS_PARMS | CS_FUNCTION, 9, 
1006                 printk("cs46xx: cs_get_dma_addr() %d\n",offset) );
1007         offset = (u32)bus_to_virt((unsigned long)offset) - (u32)dmabuf->rawbuf;
1008         CS_DBGOUT(CS_PARMS | CS_FUNCTION, 8, 
1009                 printk("cs46xx: cs_get_dma_addr()- %d\n",offset) );
1010         return offset;
1011 }
1012
1013 static void resync_dma_ptrs(struct cs_state *state)
1014 {
1015         struct dmabuf *dmabuf;
1016         
1017         CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: resync_dma_ptrs()+ \n") );
1018         if(state)
1019         {
1020                 dmabuf = &state->dmabuf;
1021                 dmabuf->hwptr=dmabuf->swptr = 0;
1022                 dmabuf->pringbuf = 0;
1023         }
1024         CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: resync_dma_ptrs()- \n") );
1025 }
1026         
1027 /* Stop recording (lock held) */
1028 static inline void __stop_adc(struct cs_state *state)
1029 {
1030         struct dmabuf *dmabuf = &state->dmabuf;
1031         struct cs_card *card = state->card;
1032         unsigned int tmp;
1033         
1034         dmabuf->enable &= ~ADC_RUNNING;
1035         
1036         tmp = cs461x_peek(card, BA1_CCTL);
1037         tmp &= 0xFFFF0000;
1038         cs461x_poke(card, BA1_CCTL, tmp );
1039 }
1040
1041 static void stop_adc(struct cs_state *state)
1042 {
1043         unsigned long flags;
1044
1045         CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: stop_adc()+ \n") );
1046         spin_lock_irqsave(&state->card->lock, flags);
1047         __stop_adc(state);
1048         spin_unlock_irqrestore(&state->card->lock, flags);
1049         CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: stop_adc()- \n") );
1050 }
1051
1052 static void start_adc(struct cs_state *state)
1053 {
1054         struct dmabuf *dmabuf = &state->dmabuf;
1055         struct cs_card *card = state->card;
1056         unsigned long flags;
1057         unsigned int tmp;
1058
1059         spin_lock_irqsave(&card->lock, flags);
1060         if (!(dmabuf->enable & ADC_RUNNING) && 
1061              ((dmabuf->mapped || dmabuf->count < (signed)dmabuf->dmasize) 
1062                && dmabuf->ready) && 
1063                ((card->pm.flags & CS46XX_PM_IDLE) || 
1064                 (card->pm.flags & CS46XX_PM_RESUMED)) )
1065         {
1066                 dmabuf->enable |= ADC_RUNNING;
1067                 cs_set_divisor(dmabuf);
1068                 tmp = cs461x_peek(card, BA1_CCTL);
1069                 tmp &= 0xFFFF0000;
1070                 tmp |= card->cctl;
1071                 CS_DBGOUT(CS_FUNCTION, 2, printk(
1072                         "cs46xx: start_adc() poke 0x%x \n",tmp) );
1073                 cs461x_poke(card, BA1_CCTL, tmp);
1074         }
1075         spin_unlock_irqrestore(&card->lock, flags);
1076 }
1077
1078 /* stop playback (lock held) */
1079 static inline void __stop_dac(struct cs_state *state)
1080 {
1081         struct dmabuf *dmabuf = &state->dmabuf;
1082         struct cs_card *card = state->card;
1083         unsigned int tmp;
1084
1085         dmabuf->enable &= ~DAC_RUNNING;
1086         
1087         tmp=cs461x_peek(card, BA1_PCTL);
1088         tmp&=0xFFFF;
1089         cs461x_poke(card, BA1_PCTL, tmp);
1090 }
1091
1092 static void stop_dac(struct cs_state *state)
1093 {
1094         unsigned long flags;
1095
1096         CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: stop_dac()+ \n") );
1097         spin_lock_irqsave(&state->card->lock, flags);
1098         __stop_dac(state);
1099         spin_unlock_irqrestore(&state->card->lock, flags);
1100         CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: stop_dac()- \n") );
1101 }       
1102
1103 static void start_dac(struct cs_state *state)
1104 {
1105         struct dmabuf *dmabuf = &state->dmabuf;
1106         struct cs_card *card = state->card;
1107         unsigned long flags;
1108         int tmp;
1109
1110         CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: start_dac()+ \n") );
1111         spin_lock_irqsave(&card->lock, flags);
1112         if (!(dmabuf->enable & DAC_RUNNING) && 
1113             ((dmabuf->mapped || dmabuf->count > 0) && dmabuf->ready) &&
1114                ((card->pm.flags & CS46XX_PM_IDLE) || 
1115                 (card->pm.flags & CS46XX_PM_RESUMED)) )
1116         {
1117                 dmabuf->enable |= DAC_RUNNING;
1118                 tmp = cs461x_peek(card, BA1_PCTL);
1119                 tmp &= 0xFFFF;
1120                 tmp |= card->pctl;
1121                 CS_DBGOUT(CS_PARMS, 6, printk(
1122                     "cs46xx: start_dac() poke card=%p tmp=0x%.08x addr=%p \n",
1123                     card, (unsigned)tmp, 
1124                     card->ba1.idx[(BA1_PCTL >> 16) & 3]+(BA1_PCTL&0xffff) ) );
1125                 cs461x_poke(card, BA1_PCTL, tmp);
1126         }
1127         spin_unlock_irqrestore(&card->lock, flags);
1128         CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: start_dac()- \n") );
1129 }
1130
1131 #define DMABUF_MINORDER 1
1132
1133 /*
1134  * allocate DMA buffer, playback and recording buffers are separate.
1135  */
1136 static int alloc_dmabuf(struct cs_state *state)
1137 {
1138
1139         struct cs_card *card=state->card;
1140         struct dmabuf *dmabuf = &state->dmabuf;
1141         void *rawbuf = NULL;
1142         void *tmpbuff = NULL;
1143         int order;
1144         struct page *map, *mapend;
1145         unsigned long df;
1146         
1147         dmabuf->ready  = dmabuf->mapped = 0;
1148         dmabuf->SGok = 0;
1149 /*
1150 * check for order within limits, but do not overwrite value.
1151 */
1152         if((defaultorder > 1) && (defaultorder < 12))
1153                 df = defaultorder;
1154         else
1155                 df = 2; 
1156
1157         for (order = df; order >= DMABUF_MINORDER; order--)
1158                 if ( (rawbuf = (void *) pci_alloc_consistent(
1159                         card->pci_dev, PAGE_SIZE << order, &dmabuf->dmaaddr)))
1160                             break;
1161         if (!rawbuf) {
1162                 CS_DBGOUT(CS_ERROR, 1, printk(KERN_ERR
1163                         "cs46xx: alloc_dmabuf(): unable to allocate rawbuf\n"));
1164                 return -ENOMEM;
1165         }
1166         dmabuf->buforder = order;
1167         dmabuf->rawbuf = rawbuf;
1168         // Now mark the pages as reserved; otherwise the 
1169         // remap_pfn_range() in cs46xx_mmap doesn't work.
1170         // 1. get index to last page in mem_map array for rawbuf.
1171         mapend = virt_to_page(dmabuf->rawbuf + 
1172                 (PAGE_SIZE << dmabuf->buforder) - 1);
1173
1174         // 2. mark each physical page in range as 'reserved'.
1175         for (map = virt_to_page(dmabuf->rawbuf); map <= mapend; map++)
1176                 cs4x_mem_map_reserve(map);
1177
1178         CS_DBGOUT(CS_PARMS, 9, printk("cs46xx: alloc_dmabuf(): allocated %ld (order = %d) bytes at %p\n",
1179                PAGE_SIZE << order, order, rawbuf) );
1180
1181 /*
1182 *  only allocate the conversion buffer for the ADC
1183 */
1184         if(dmabuf->type == CS_TYPE_DAC)
1185         {
1186                 dmabuf->tmpbuff = NULL;
1187                 dmabuf->buforder_tmpbuff = 0;
1188                 return 0;
1189         }
1190 /*
1191  * now the temp buffer for 16/8 conversions
1192  */
1193
1194         tmpbuff = (void *) pci_alloc_consistent(
1195                 card->pci_dev, PAGE_SIZE << order, &dmabuf->dmaaddr_tmpbuff);
1196
1197         if (!tmpbuff)
1198                 return -ENOMEM;
1199         CS_DBGOUT(CS_PARMS, 9, printk("cs46xx: allocated %ld (order = %d) bytes at %p\n",
1200                PAGE_SIZE << order, order, tmpbuff) );
1201
1202         dmabuf->tmpbuff = tmpbuff;
1203         dmabuf->buforder_tmpbuff = order;
1204         
1205         // Now mark the pages as reserved; otherwise the 
1206         // remap_pfn_range() in cs46xx_mmap doesn't work.
1207         // 1. get index to last page in mem_map array for rawbuf.
1208         mapend = virt_to_page(dmabuf->tmpbuff + 
1209                 (PAGE_SIZE << dmabuf->buforder_tmpbuff) - 1);
1210
1211         // 2. mark each physical page in range as 'reserved'.
1212         for (map = virt_to_page(dmabuf->tmpbuff); map <= mapend; map++)
1213                 cs4x_mem_map_reserve(map);
1214         return 0;
1215 }
1216
1217 /* free DMA buffer */
1218 static void dealloc_dmabuf(struct cs_state *state)
1219 {
1220         struct dmabuf *dmabuf = &state->dmabuf;
1221         struct page *map, *mapend;
1222
1223         if (dmabuf->rawbuf) {
1224                 // Undo prog_dmabuf()'s marking the pages as reserved 
1225                 mapend = virt_to_page(dmabuf->rawbuf + 
1226                                 (PAGE_SIZE << dmabuf->buforder) - 1);
1227                 for (map = virt_to_page(dmabuf->rawbuf); map <= mapend; map++)
1228                         cs4x_mem_map_unreserve(map);
1229                 free_dmabuf(state->card, dmabuf);
1230         }
1231
1232         if (dmabuf->tmpbuff) {
1233                 // Undo prog_dmabuf()'s marking the pages as reserved 
1234                 mapend = virt_to_page(dmabuf->tmpbuff +
1235                                 (PAGE_SIZE << dmabuf->buforder_tmpbuff) - 1);
1236                 for (map = virt_to_page(dmabuf->tmpbuff); map <= mapend; map++)
1237                         cs4x_mem_map_unreserve(map);
1238                 free_dmabuf2(state->card, dmabuf);
1239         }
1240
1241         dmabuf->rawbuf = NULL;
1242         dmabuf->tmpbuff = NULL;
1243         dmabuf->mapped = dmabuf->ready = 0;
1244         dmabuf->SGok = 0;
1245 }
1246
1247 static int __prog_dmabuf(struct cs_state *state)
1248 {
1249         struct dmabuf *dmabuf = &state->dmabuf;
1250         unsigned long flags;
1251         unsigned long allocated_pages, allocated_bytes;                     
1252         unsigned long tmp1, tmp2, fmt=0;                                           
1253         unsigned long *ptmp = (unsigned long *) dmabuf->pbuf;               
1254         unsigned long SGarray[9], nSGpages=0;                               
1255         int ret;
1256
1257         CS_DBGOUT(CS_FUNCTION, 4, printk("cs46xx: prog_dmabuf()+ \n"));
1258 /*
1259  * check for CAPTURE and use only non-sg for initial release
1260  */
1261         if(dmabuf->type == CS_TYPE_ADC)
1262         {
1263                 CS_DBGOUT(CS_FUNCTION, 4, printk("cs46xx: prog_dmabuf() ADC\n"));
1264                 /* 
1265                  * add in non-sg support for capture.
1266                  */
1267                 spin_lock_irqsave(&state->card->lock, flags);
1268         /* add code to reset the rawbuf memory. TRW */
1269                 resync_dma_ptrs(state);
1270                 dmabuf->total_bytes = dmabuf->blocks = 0;
1271                 dmabuf->count = dmabuf->error = dmabuf->underrun = 0;
1272
1273                 dmabuf->SGok = 0;                                                   
1274
1275                 spin_unlock_irqrestore(&state->card->lock, flags);
1276
1277                 /* allocate DMA buffer if not allocated yet */
1278                 if (!dmabuf->rawbuf || !dmabuf->tmpbuff)
1279                         if ((ret = alloc_dmabuf(state)))
1280                                 return ret; 
1281         /*
1282          * static image only supports 16Bit signed, stereo - hard code fmt
1283          */
1284                 fmt = CS_FMT_16BIT | CS_FMT_STEREO;
1285
1286                 dmabuf->numfrag = 2;                                        
1287                 dmabuf->fragsize = 2048;                                    
1288                 dmabuf->fragsamples = 2048 >> sample_shift[fmt];    
1289                 dmabuf->dmasize = 4096;                                     
1290                 dmabuf->fragshift = 11;                                     
1291
1292                 memset(dmabuf->rawbuf, (fmt & CS_FMT_16BIT) ? 0 : 0x80,
1293                        dmabuf->dmasize);
1294                 memset(dmabuf->tmpbuff, (fmt & CS_FMT_16BIT) ? 0 : 0x80, 
1295                         PAGE_SIZE<<dmabuf->buforder_tmpbuff);      
1296
1297                 /*
1298                  *      Now set up the ring
1299                  */
1300
1301                 spin_lock_irqsave(&state->card->lock, flags);
1302                 cs_rec_setup(state);
1303                 spin_unlock_irqrestore(&state->card->lock, flags);
1304
1305                 /* set the ready flag for the dma buffer */
1306                 dmabuf->ready = 1;
1307
1308                 CS_DBGOUT(CS_PARMS, 4, printk(
1309                         "cs46xx: prog_dmabuf(): CAPTURE rate=%d fmt=0x%x numfrag=%d "
1310                         "fragsize=%d dmasize=%d\n",
1311                             dmabuf->rate, dmabuf->fmt, dmabuf->numfrag,
1312                             dmabuf->fragsize, dmabuf->dmasize) );
1313
1314                 CS_DBGOUT(CS_FUNCTION, 4, printk("cs46xx: prog_dmabuf()- 0 \n"));
1315                 return 0;
1316         }
1317         else if (dmabuf->type == CS_TYPE_DAC)
1318         {
1319         /*
1320          * Must be DAC
1321          */
1322                 CS_DBGOUT(CS_FUNCTION, 4, printk("cs46xx: prog_dmabuf() DAC\n"));
1323                 spin_lock_irqsave(&state->card->lock, flags);
1324                 resync_dma_ptrs(state);
1325                 dmabuf->total_bytes = dmabuf->blocks = 0;
1326                 dmabuf->count = dmabuf->error = dmabuf->underrun = 0;
1327
1328                 dmabuf->SGok = 0;                                                   
1329
1330                 spin_unlock_irqrestore(&state->card->lock, flags);
1331
1332                 /* allocate DMA buffer if not allocated yet */
1333                 if (!dmabuf->rawbuf)
1334                         if ((ret = alloc_dmabuf(state)))
1335                                 return ret;
1336
1337                 allocated_pages = 1 << dmabuf->buforder;                            
1338                 allocated_bytes = allocated_pages*PAGE_SIZE;                        
1339                                                                                     
1340                 if(allocated_pages < 2)                                             
1341                 {
1342                         CS_DBGOUT(CS_FUNCTION, 4, printk(
1343                             "cs46xx: prog_dmabuf() Error: allocated_pages too small (%d)\n",
1344                                 (unsigned)allocated_pages));
1345                         return -ENOMEM;
1346                 }
1347                                                                                     
1348                 /* Use all the pages allocated, fragsize 4k. */
1349                 /* Use 'pbuf' for S/G page map table. */
1350                 dmabuf->SGok = 1;           /* Use S/G. */
1351
1352                 nSGpages = allocated_bytes/4096;    /* S/G pages always 4k. */
1353                                                                                     
1354                      /* Set up S/G variables. */
1355                 *ptmp = virt_to_bus(dmabuf->rawbuf);                                
1356                 *(ptmp+1) = 0x00000008;                                             
1357                 for(tmp1= 1; tmp1 < nSGpages; tmp1++) {                             
1358                         *(ptmp+2*tmp1) = virt_to_bus( (dmabuf->rawbuf)+4096*tmp1);  
1359                         if( tmp1 == nSGpages-1)                                     
1360                                 tmp2 = 0xbfff0000;
1361                         else                                                        
1362                                 tmp2 = 0x80000000+8*(tmp1+1);                       
1363                         *(ptmp+2*tmp1+1) = tmp2;                                    
1364                 }                                                                   
1365                 SGarray[0] = 0x82c0200d;                                            
1366                 SGarray[1] = 0xffff0000;                                            
1367                 SGarray[2] = *ptmp;                                                 
1368                 SGarray[3] = 0x00010600;                                            
1369                 SGarray[4] = *(ptmp+2);                                             
1370                 SGarray[5] = 0x80000010;                                            
1371                 SGarray[6] = *ptmp;                                                 
1372                 SGarray[7] = *(ptmp+2);                                             
1373                 SGarray[8] = (virt_to_bus(dmabuf->pbuf) & 0xffff000) | 0x10;        
1374
1375                 if (dmabuf->SGok) {                                                 
1376                         dmabuf->numfrag = nSGpages;                                 
1377                         dmabuf->fragsize = 4096;                                    
1378                         dmabuf->fragsamples = 4096 >> sample_shift[dmabuf->fmt];    
1379                         dmabuf->fragshift = 12;                                     
1380                         dmabuf->dmasize = dmabuf->numfrag*4096;                     
1381                 }                                                                   
1382                 else {                                                              
1383                         SGarray[0] = 0xf2c0000f;                                    
1384                         SGarray[1] = 0x00000200;                                    
1385                         SGarray[2] = 0;                                             
1386                         SGarray[3] = 0x00010600;                                    
1387                         SGarray[4]=SGarray[5]=SGarray[6]=SGarray[7]=SGarray[8] = 0; 
1388                         dmabuf->numfrag = 2;                                        
1389                         dmabuf->fragsize = 2048;                                    
1390                         dmabuf->fragsamples = 2048 >> sample_shift[dmabuf->fmt];    
1391                         dmabuf->dmasize = 4096;                                     
1392                         dmabuf->fragshift = 11;                                     
1393                 }
1394                 for(tmp1 = 0; tmp1 < sizeof(SGarray)/4; tmp1++)                     
1395                         cs461x_poke( state->card, BA1_PDTC+tmp1*4, SGarray[tmp1]);  
1396
1397                 memset(dmabuf->rawbuf, (dmabuf->fmt & CS_FMT_16BIT) ? 0 : 0x80,
1398                        dmabuf->dmasize);
1399
1400                 /*
1401                  *      Now set up the ring
1402                  */
1403
1404                 spin_lock_irqsave(&state->card->lock, flags);
1405                 cs_play_setup(state);
1406                 spin_unlock_irqrestore(&state->card->lock, flags);
1407
1408                 /* set the ready flag for the dma buffer */
1409                 dmabuf->ready = 1;
1410
1411                 CS_DBGOUT(CS_PARMS, 4, printk(
1412                         "cs46xx: prog_dmabuf(): PLAYBACK rate=%d fmt=0x%x numfrag=%d "
1413                         "fragsize=%d dmasize=%d\n",
1414                             dmabuf->rate, dmabuf->fmt, dmabuf->numfrag,
1415                             dmabuf->fragsize, dmabuf->dmasize) );
1416
1417                 CS_DBGOUT(CS_FUNCTION, 4, printk("cs46xx: prog_dmabuf()- \n"));
1418                 return 0;
1419         }
1420         else
1421         {
1422                 CS_DBGOUT(CS_FUNCTION, 4, printk("cs46xx: prog_dmabuf()- Invalid Type %d\n",
1423                         dmabuf->type));
1424         }
1425         return 1;
1426 }
1427
1428 static int prog_dmabuf(struct cs_state *state)
1429 {
1430         int ret;
1431         
1432         mutex_lock(&state->sem);
1433         ret = __prog_dmabuf(state);
1434         mutex_unlock(&state->sem);
1435         
1436         return ret;
1437 }
1438
1439 static void cs_clear_tail(struct cs_state *state)
1440 {
1441 }
1442
1443 static int drain_dac(struct cs_state *state, int nonblock)
1444 {
1445         DECLARE_WAITQUEUE(wait, current);
1446         struct dmabuf *dmabuf = &state->dmabuf;
1447         struct cs_card *card=state->card;
1448         unsigned long flags;
1449         unsigned long tmo;
1450         int count;
1451
1452         CS_DBGOUT(CS_FUNCTION, 4, printk("cs46xx: drain_dac()+ \n"));
1453         if (dmabuf->mapped || !dmabuf->ready)
1454         {
1455                 CS_DBGOUT(CS_FUNCTION, 4, printk("cs46xx: drain_dac()- 0, not ready\n"));
1456                 return 0;
1457         }
1458
1459         add_wait_queue(&dmabuf->wait, &wait);
1460         for (;;) {
1461                 /* It seems that we have to set the current state to TASK_INTERRUPTIBLE
1462                    every time to make the process really go to sleep */
1463                 current->state = TASK_INTERRUPTIBLE;
1464
1465                 spin_lock_irqsave(&state->card->lock, flags);
1466                 count = dmabuf->count;
1467                 spin_unlock_irqrestore(&state->card->lock, flags);
1468
1469                 if (count <= 0)
1470                         break;
1471
1472                 if (signal_pending(current))
1473                         break;
1474
1475                 if (nonblock) {
1476                         remove_wait_queue(&dmabuf->wait, &wait);
1477                         current->state = TASK_RUNNING;
1478                         return -EBUSY;
1479                 }
1480
1481                 tmo = (dmabuf->dmasize * HZ) / dmabuf->rate;
1482                 tmo >>= sample_shift[dmabuf->fmt];
1483                 tmo += (2048*HZ)/dmabuf->rate;
1484                 
1485                 if (!schedule_timeout(tmo ? tmo : 1) && tmo){
1486                         printk(KERN_ERR "cs46xx: drain_dac, dma timeout? %d\n", count);
1487                         break;
1488                 }
1489         }
1490         remove_wait_queue(&dmabuf->wait, &wait);
1491         current->state = TASK_RUNNING;
1492         if (signal_pending(current))
1493         {
1494                 CS_DBGOUT(CS_FUNCTION, 4, printk("cs46xx: drain_dac()- -ERESTARTSYS\n"));
1495                 /*
1496                 * set to silence and let that clear the fifos.
1497                 */
1498                 cs461x_clear_serial_FIFOs(card, CS_TYPE_DAC);
1499                 return -ERESTARTSYS;
1500         }
1501
1502         CS_DBGOUT(CS_FUNCTION, 4, printk("cs46xx: drain_dac()- 0\n"));
1503         return 0;
1504 }
1505
1506
1507 /* update buffer manangement pointers, especially, dmabuf->count and dmabuf->hwptr */
1508 static void cs_update_ptr(struct cs_card *card, int wake)
1509 {
1510         struct cs_state *state;
1511         struct dmabuf *dmabuf;
1512         unsigned hwptr;
1513         int diff;
1514
1515         /* error handling and process wake up for ADC */
1516         state = card->states[0];
1517         if(state)
1518         {
1519                 dmabuf = &state->dmabuf;
1520                 if (dmabuf->enable & ADC_RUNNING) {
1521                         /* update hardware pointer */
1522                         hwptr = cs_get_dma_addr(state);
1523
1524                         diff = (dmabuf->dmasize + hwptr - dmabuf->hwptr) % dmabuf->dmasize;
1525                         CS_DBGOUT(CS_PARMS, 9, printk(
1526                                 "cs46xx: cs_update_ptr()+ ADC hwptr=%d diff=%d\n", 
1527                                 hwptr,diff) );
1528                         dmabuf->hwptr = hwptr;
1529                         dmabuf->total_bytes += diff;
1530                         dmabuf->count += diff;
1531                         if (dmabuf->count > dmabuf->dmasize)
1532                                 dmabuf->count = dmabuf->dmasize;
1533
1534                         if(dmabuf->mapped)
1535                         {
1536                                 if (wake && dmabuf->count >= (signed)dmabuf->fragsize)
1537                                         wake_up(&dmabuf->wait);
1538                         } else 
1539                         {
1540                                 if (wake && dmabuf->count > 0)
1541                                         wake_up(&dmabuf->wait);
1542                         }
1543                 }
1544         }
1545
1546 /*
1547  * Now the DAC
1548  */
1549         state = card->states[1];
1550         if(state)
1551         {
1552                 dmabuf = &state->dmabuf;
1553                 /* error handling and process wake up for DAC */
1554                 if (dmabuf->enable & DAC_RUNNING) {
1555                         /* update hardware pointer */
1556                         hwptr = cs_get_dma_addr(state);
1557
1558                         diff = (dmabuf->dmasize + hwptr - dmabuf->hwptr) % dmabuf->dmasize;
1559                         CS_DBGOUT(CS_PARMS, 9, printk(
1560                                 "cs46xx: cs_update_ptr()+ DAC hwptr=%d diff=%d\n", 
1561                                 hwptr,diff) );
1562                         dmabuf->hwptr = hwptr;
1563                         dmabuf->total_bytes += diff;
1564                         if (dmabuf->mapped) {
1565                                 dmabuf->count += diff;
1566                                 if (wake && dmabuf->count >= (signed)dmabuf->fragsize)
1567                                         wake_up(&dmabuf->wait);
1568                                 /*
1569                                  * other drivers use fragsize, but don't see any sense
1570                                  * in that, since dmasize is the buffer asked for
1571                                  * via mmap.
1572                                  */
1573                                 if( dmabuf->count > dmabuf->dmasize)
1574                                         dmabuf->count &= dmabuf->dmasize-1;
1575                         } else {
1576                                 dmabuf->count -= diff;
1577                                 /*
1578                                  * backfill with silence and clear out the last 
1579                                  * "diff" number of bytes.
1580                                  */
1581                                 if(hwptr >= diff)
1582                                 {
1583                                         memset(dmabuf->rawbuf + hwptr - diff, 
1584                                                 (dmabuf->fmt & CS_FMT_16BIT) ? 0 : 0x80, diff);
1585                                 }
1586                                 else
1587                                 {
1588                                         memset(dmabuf->rawbuf, 
1589                                                 (dmabuf->fmt & CS_FMT_16BIT) ? 0 : 0x80,
1590                                                 (unsigned)hwptr);
1591                                         memset((char *)dmabuf->rawbuf + 
1592                                                         dmabuf->dmasize + hwptr - diff,
1593                                                 (dmabuf->fmt & CS_FMT_16BIT) ? 0 : 0x80, 
1594                                                 diff - hwptr); 
1595                                 }
1596
1597                                 if (dmabuf->count < 0 || dmabuf->count > dmabuf->dmasize) {
1598                                         CS_DBGOUT(CS_ERROR, 2, printk(KERN_INFO
1599                                           "cs46xx: ERROR DAC count<0 or count > dmasize (%d)\n",
1600                                                 dmabuf->count));
1601                                         /* 
1602                                         * buffer underrun or buffer overrun, reset the
1603                                         * count of bytes written back to 0.
1604                                         */
1605                                         if(dmabuf->count < 0)
1606                                                 dmabuf->underrun=1;
1607                                         dmabuf->count = 0;
1608                                         dmabuf->error++;
1609                                 }
1610                                 if (wake && dmabuf->count < (signed)dmabuf->dmasize/2)
1611                                         wake_up(&dmabuf->wait);
1612                         }
1613                 }
1614         }
1615 }
1616
1617
1618 /* hold spinlock for the following! */
1619 static void cs_handle_midi(struct cs_card *card)
1620 {
1621         unsigned char ch;
1622         int wake;
1623         unsigned temp1;
1624
1625         wake = 0;
1626         while (!(cs461x_peekBA0(card,  BA0_MIDSR) & MIDSR_RBE)) {
1627                 ch = cs461x_peekBA0(card, BA0_MIDRP);
1628                 if (card->midi.icnt < CS_MIDIINBUF) {
1629                         card->midi.ibuf[card->midi.iwr] = ch;
1630                         card->midi.iwr = (card->midi.iwr + 1) % CS_MIDIINBUF;
1631                         card->midi.icnt++;
1632                 }
1633                 wake = 1;
1634         }
1635         if (wake)
1636                 wake_up(&card->midi.iwait);
1637         wake = 0;
1638         while (!(cs461x_peekBA0(card,  BA0_MIDSR) & MIDSR_TBF) && card->midi.ocnt > 0) {
1639                 temp1 = ( card->midi.obuf[card->midi.ord] ) & 0x000000ff;
1640                 cs461x_pokeBA0(card, BA0_MIDWP,temp1);
1641                 card->midi.ord = (card->midi.ord + 1) % CS_MIDIOUTBUF;
1642                 card->midi.ocnt--;
1643                 if (card->midi.ocnt < CS_MIDIOUTBUF-16)
1644                         wake = 1;
1645         }
1646         if (wake)
1647                 wake_up(&card->midi.owait);
1648 }
1649
1650 static irqreturn_t cs_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1651 {
1652         struct cs_card *card = (struct cs_card *)dev_id;
1653         /* Single channel card */
1654         struct cs_state *recstate = card->channel[0].state;
1655         struct cs_state *playstate = card->channel[1].state;
1656         u32 status;
1657
1658         CS_DBGOUT(CS_INTERRUPT, 9, printk("cs46xx: cs_interrupt()+ \n"));
1659
1660         spin_lock(&card->lock);
1661
1662         status = cs461x_peekBA0(card, BA0_HISR);
1663         
1664         if ((status & 0x7fffffff) == 0)
1665         {
1666                 cs461x_pokeBA0(card, BA0_HICR, HICR_CHGM|HICR_IEV);
1667                 spin_unlock(&card->lock);
1668                 return IRQ_HANDLED;     /* Might be IRQ_NONE.. */
1669         }
1670         
1671         /*
1672          * check for playback or capture interrupt only
1673          */
1674         if( ((status & HISR_VC0) && playstate && playstate->dmabuf.ready) || 
1675             (((status & HISR_VC1) && recstate && recstate->dmabuf.ready)) )
1676         {
1677                 CS_DBGOUT(CS_INTERRUPT, 8, printk(
1678                         "cs46xx: cs_interrupt() interrupt bit(s) set (0x%x)\n",status));
1679                 cs_update_ptr(card, CS_TRUE);
1680         }
1681
1682         if( status & HISR_MIDI )
1683                 cs_handle_midi(card);
1684         
1685         /* clear 'em */
1686         cs461x_pokeBA0(card, BA0_HICR, HICR_CHGM|HICR_IEV);
1687         spin_unlock(&card->lock);
1688         CS_DBGOUT(CS_INTERRUPT, 9, printk("cs46xx: cs_interrupt()- \n"));
1689         return IRQ_HANDLED;
1690 }
1691
1692
1693 /**********************************************************************/
1694
1695 static ssize_t cs_midi_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
1696 {
1697         struct cs_card *card = (struct cs_card *)file->private_data;
1698         ssize_t ret;
1699         unsigned long flags;
1700         unsigned ptr;
1701         int cnt;
1702
1703         if (!access_ok(VERIFY_WRITE, buffer, count))
1704                 return -EFAULT;
1705         ret = 0;
1706         while (count > 0) {
1707                 spin_lock_irqsave(&card->lock, flags);
1708                 ptr = card->midi.ird;
1709                 cnt = CS_MIDIINBUF - ptr;
1710                 if (card->midi.icnt < cnt)
1711                         cnt = card->midi.icnt;
1712                 spin_unlock_irqrestore(&card->lock, flags);
1713                 if (cnt > count)
1714                         cnt = count;
1715                 if (cnt <= 0) {
1716                         if (file->f_flags & O_NONBLOCK)
1717                                 return ret ? ret : -EAGAIN;
1718                         interruptible_sleep_on(&card->midi.iwait);
1719                         if (signal_pending(current))
1720                                 return ret ? ret : -ERESTARTSYS;
1721                         continue;
1722                 }
1723                 if (copy_to_user(buffer, card->midi.ibuf + ptr, cnt))
1724                         return ret ? ret : -EFAULT;
1725                 ptr = (ptr + cnt) % CS_MIDIINBUF;
1726                 spin_lock_irqsave(&card->lock, flags);
1727                 card->midi.ird = ptr;
1728                 card->midi.icnt -= cnt;
1729                 spin_unlock_irqrestore(&card->lock, flags);
1730                 count -= cnt;
1731                 buffer += cnt;
1732                 ret += cnt;
1733         }
1734         return ret;
1735 }
1736
1737
1738 static ssize_t cs_midi_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
1739 {
1740         struct cs_card *card = (struct cs_card *)file->private_data;
1741         ssize_t ret;
1742         unsigned long flags;
1743         unsigned ptr;
1744         int cnt;
1745
1746         if (!access_ok(VERIFY_READ, buffer, count))
1747                 return -EFAULT;
1748         ret = 0;
1749         while (count > 0) {
1750                 spin_lock_irqsave(&card->lock, flags);
1751                 ptr = card->midi.owr;
1752                 cnt = CS_MIDIOUTBUF - ptr;
1753                 if (card->midi.ocnt + cnt > CS_MIDIOUTBUF)
1754                         cnt = CS_MIDIOUTBUF - card->midi.ocnt;
1755                 if (cnt <= 0)
1756                         cs_handle_midi(card);
1757                 spin_unlock_irqrestore(&card->lock, flags);
1758                 if (cnt > count)
1759                         cnt = count;
1760                 if (cnt <= 0) {
1761                         if (file->f_flags & O_NONBLOCK)
1762                                 return ret ? ret : -EAGAIN;
1763                         interruptible_sleep_on(&card->midi.owait);
1764                         if (signal_pending(current))
1765                                 return ret ? ret : -ERESTARTSYS;
1766                         continue;
1767                 }
1768                 if (copy_from_user(card->midi.obuf + ptr, buffer, cnt))
1769                         return ret ? ret : -EFAULT;
1770                 ptr = (ptr + cnt) % CS_MIDIOUTBUF;
1771                 spin_lock_irqsave(&card->lock, flags);
1772                 card->midi.owr = ptr;
1773                 card->midi.ocnt += cnt;
1774                 spin_unlock_irqrestore(&card->lock, flags);
1775                 count -= cnt;
1776                 buffer += cnt;
1777                 ret += cnt;
1778                 spin_lock_irqsave(&card->lock, flags);
1779                 cs_handle_midi(card);
1780                 spin_unlock_irqrestore(&card->lock, flags);
1781         }
1782         return ret;
1783 }
1784
1785
1786 static unsigned int cs_midi_poll(struct file *file, struct poll_table_struct *wait)
1787 {
1788         struct cs_card *card = (struct cs_card *)file->private_data;
1789         unsigned long flags;
1790         unsigned int mask = 0;
1791
1792         if (file->f_flags & FMODE_WRITE)
1793                 poll_wait(file, &card->midi.owait, wait);
1794         if (file->f_flags & FMODE_READ)
1795                 poll_wait(file, &card->midi.iwait, wait);
1796         spin_lock_irqsave(&card->lock, flags);
1797         if (file->f_flags & FMODE_READ) {
1798                 if (card->midi.icnt > 0)
1799                         mask |= POLLIN | POLLRDNORM;
1800         }
1801         if (file->f_flags & FMODE_WRITE) {
1802                 if (card->midi.ocnt < CS_MIDIOUTBUF)
1803                         mask |= POLLOUT | POLLWRNORM;
1804         }
1805         spin_unlock_irqrestore(&card->lock, flags);
1806         return mask;
1807 }
1808
1809
1810 static int cs_midi_open(struct inode *inode, struct file *file)
1811 {
1812         unsigned int minor = iminor(inode);
1813         struct cs_card *card=NULL;
1814         unsigned long flags;
1815         struct list_head *entry;
1816
1817         list_for_each(entry, &cs46xx_devs)
1818         {
1819                 card = list_entry(entry, struct cs_card, list);
1820                 if (card->dev_midi == minor)
1821                         break;
1822         }
1823
1824         if (entry == &cs46xx_devs)
1825                 return -ENODEV;
1826         if (!card)
1827         {
1828                 CS_DBGOUT(CS_FUNCTION | CS_OPEN, 2, printk(KERN_INFO
1829                         "cs46xx: cs46xx_midi_open(): Error - unable to find card struct\n"));
1830                 return -ENODEV;
1831         }
1832
1833         file->private_data = card;
1834         /* wait for device to become free */
1835         mutex_lock(&card->midi.open_mutex);
1836         while (card->midi.open_mode & file->f_mode) {
1837                 if (file->f_flags & O_NONBLOCK) {
1838                         mutex_unlock(&card->midi.open_mutex);
1839                         return -EBUSY;
1840                 }
1841                 mutex_unlock(&card->midi.open_mutex);
1842                 interruptible_sleep_on(&card->midi.open_wait);
1843                 if (signal_pending(current))
1844                         return -ERESTARTSYS;
1845                 mutex_lock(&card->midi.open_mutex);
1846         }
1847         spin_lock_irqsave(&card->midi.lock, flags);
1848         if (!(card->midi.open_mode & (FMODE_READ | FMODE_WRITE))) {
1849                 card->midi.ird = card->midi.iwr = card->midi.icnt = 0;
1850                 card->midi.ord = card->midi.owr = card->midi.ocnt = 0;
1851                 card->midi.ird = card->midi.iwr = card->midi.icnt = 0;
1852                 cs461x_pokeBA0(card, BA0_MIDCR, 0x0000000f);            /* Enable xmit, rcv. */
1853                 cs461x_pokeBA0(card, BA0_HICR, HICR_IEV | HICR_CHGM);   /* Enable interrupts */
1854         }
1855         if (file->f_mode & FMODE_READ) {
1856                 card->midi.ird = card->midi.iwr = card->midi.icnt = 0;
1857         }
1858         if (file->f_mode & FMODE_WRITE) {
1859                 card->midi.ord = card->midi.owr = card->midi.ocnt = 0;
1860         }
1861         spin_unlock_irqrestore(&card->midi.lock, flags);
1862         card->midi.open_mode |= (file->f_mode & (FMODE_READ | FMODE_WRITE));
1863         mutex_unlock(&card->midi.open_mutex);
1864         return 0;
1865 }
1866
1867
1868 static int cs_midi_release(struct inode *inode, struct file *file)
1869 {
1870         struct cs_card *card = (struct cs_card *)file->private_data;
1871         DECLARE_WAITQUEUE(wait, current);
1872         unsigned long flags;
1873         unsigned count, tmo;
1874
1875         if (file->f_mode & FMODE_WRITE) {
1876                 current->state = TASK_INTERRUPTIBLE;
1877                 add_wait_queue(&card->midi.owait, &wait);
1878                 for (;;) {
1879                         spin_lock_irqsave(&card->midi.lock, flags);
1880                         count = card->midi.ocnt;
1881                         spin_unlock_irqrestore(&card->midi.lock, flags);
1882                         if (count <= 0)
1883                                 break;
1884                         if (signal_pending(current))
1885                                 break;
1886                         if (file->f_flags & O_NONBLOCK)
1887                                 break;
1888                         tmo = (count * HZ) / 3100;
1889                         if (!schedule_timeout(tmo ? : 1) && tmo)
1890                                 printk(KERN_DEBUG "cs46xx: midi timed out??\n");
1891                 }
1892                 remove_wait_queue(&card->midi.owait, &wait);
1893                 current->state = TASK_RUNNING;
1894         }
1895         mutex_lock(&card->midi.open_mutex);
1896         card->midi.open_mode &= (~(file->f_mode & (FMODE_READ | FMODE_WRITE)));
1897         mutex_unlock(&card->midi.open_mutex);
1898         wake_up(&card->midi.open_wait);
1899         return 0;
1900 }
1901
1902 /*
1903  *   Midi file operations struct.
1904  */
1905 static /*const*/ struct file_operations cs_midi_fops = {
1906         CS_OWNER        CS_THIS_MODULE
1907         .llseek         = no_llseek,
1908         .read           = cs_midi_read,
1909         .write          = cs_midi_write,
1910         .poll           = cs_midi_poll,
1911         .open           = cs_midi_open,
1912         .release        = cs_midi_release,
1913 };
1914
1915 /*
1916  *
1917  * CopySamples copies 16-bit stereo signed samples from the source to the
1918  * destination, possibly converting down to unsigned 8-bit and/or mono.
1919  * count specifies the number of output bytes to write.
1920  *
1921  *  Arguments:
1922  *
1923  *  dst             - Pointer to a destination buffer.
1924  *  src             - Pointer to a source buffer
1925  *  count           - The number of bytes to copy into the destination buffer.
1926  *  fmt             - CS_FMT_16BIT and/or CS_FMT_STEREO bits
1927  *  dmabuf          - pointer to the dma buffer structure
1928  *
1929  * NOTES: only call this routine if the output desired is not 16 Signed Stereo
1930  *      
1931  *
1932  */
1933 static void CopySamples(char *dst, char *src, int count, unsigned fmt, 
1934                 struct dmabuf *dmabuf)
1935 {
1936
1937     s32 s32AudioSample;
1938     s16 *psSrc=(s16 *)src;
1939     s16 *psDst=(s16 *)dst;
1940     u8 *pucDst=(u8 *)dst;
1941
1942     CS_DBGOUT(CS_FUNCTION, 2, printk(KERN_INFO "cs46xx: CopySamples()+ ") );
1943     CS_DBGOUT(CS_WAVE_READ, 8, printk(KERN_INFO
1944         " dst=%p src=%p count=%d fmt=0x%x\n",
1945         dst,src,count,fmt) );
1946
1947     /*
1948      * See if the data should be output as 8-bit unsigned stereo.
1949      */
1950     if((fmt & CS_FMT_STEREO) && !(fmt & CS_FMT_16BIT))
1951     {
1952         /*
1953          * Convert each 16-bit signed stereo sample to 8-bit unsigned 
1954          * stereo using rounding.
1955          */
1956         psSrc = (s16 *)src;
1957         count = count/2;
1958         while(count--)
1959         {
1960             *(pucDst++) = (u8)(((s16)(*psSrc++) + (s16)0x8000) >> 8);
1961         }
1962     }
1963     /*
1964      * See if the data should be output at 8-bit unsigned mono.
1965      */
1966     else if(!(fmt & CS_FMT_STEREO) && !(fmt & CS_FMT_16BIT))
1967     {
1968         /*
1969          * Convert each 16-bit signed stereo sample to 8-bit unsigned 
1970          * mono using averaging and rounding.
1971          */
1972         psSrc = (s16 *)src;
1973         count = count/2;
1974         while(count--)
1975         {
1976             s32AudioSample = ((*psSrc)+(*(psSrc + 1)))/2 + (s32)0x80;
1977             if(s32AudioSample > 0x7fff)
1978                 s32AudioSample = 0x7fff;
1979             *(pucDst++) = (u8)(((s16)s32AudioSample + (s16)0x8000) >> 8);
1980             psSrc += 2;
1981         }
1982     }
1983     /*
1984      * See if the data should be output at 16-bit signed mono.
1985      */
1986     else if(!(fmt & CS_FMT_STEREO) && (fmt & CS_FMT_16BIT))
1987     {
1988         /*
1989          * Convert each 16-bit signed stereo sample to 16-bit signed 
1990          * mono using averaging.
1991          */
1992         psSrc = (s16 *)src;
1993         count = count/2;
1994         while(count--)
1995         {
1996             *(psDst++) = (s16)((*psSrc)+(*(psSrc + 1)))/2;
1997             psSrc += 2;
1998         }
1999     }
2000 }
2001
2002 /*
2003  * cs_copy_to_user()
2004  * replacement for the standard copy_to_user, to allow for a conversion from
2005  * 16 bit to 8 bit and from stereo to mono, if the record conversion is active.  
2006  * The current CS46xx/CS4280 static image only records in 16bit unsigned Stereo, 
2007  * so we convert from any of the other format combinations.
2008  */
2009 static unsigned cs_copy_to_user(
2010         struct cs_state *s, 
2011         void __user *dest, 
2012         void *hwsrc, 
2013         unsigned cnt, 
2014         unsigned *copied)
2015 {
2016         struct dmabuf *dmabuf = &s->dmabuf;
2017         void *src = hwsrc;  /* default to the standard destination buffer addr */
2018
2019         CS_DBGOUT(CS_FUNCTION, 6, printk(KERN_INFO 
2020                 "cs_copy_to_user()+ fmt=0x%x cnt=%d dest=%p\n",
2021                 dmabuf->fmt,(unsigned)cnt,dest) );
2022
2023         if(cnt > dmabuf->dmasize)
2024         {
2025                 cnt = dmabuf->dmasize;
2026         }
2027         if(!cnt)
2028         {
2029                 *copied = 0;
2030                 return 0;
2031         }
2032         if(dmabuf->divisor != 1)
2033         {
2034                 if(!dmabuf->tmpbuff)
2035                 {
2036                         *copied = cnt/dmabuf->divisor;
2037                         return 0;
2038                 }
2039
2040                 CopySamples((char *)dmabuf->tmpbuff, (char *)hwsrc, cnt, 
2041                         dmabuf->fmt, dmabuf);
2042                 src = dmabuf->tmpbuff;
2043                 cnt = cnt/dmabuf->divisor;
2044         }
2045         if (copy_to_user(dest, src, cnt))
2046         {
2047                 CS_DBGOUT(CS_FUNCTION, 2, printk(KERN_ERR 
2048                         "cs46xx: cs_copy_to_user()- fault dest=%p src=%p cnt=%d\n",
2049                                 dest,src,cnt) );
2050                 *copied = 0;
2051                 return -EFAULT;
2052         }
2053         *copied = cnt;
2054         CS_DBGOUT(CS_FUNCTION, 2, printk(KERN_INFO 
2055                 "cs46xx: cs_copy_to_user()- copied bytes is %d \n",cnt) );
2056         return 0;
2057 }
2058
2059 /* in this loop, dmabuf.count signifies the amount of data that is waiting to be copied to
2060    the user's buffer.  it is filled by the dma machine and drained by this loop. */
2061 static ssize_t cs_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
2062 {
2063         struct cs_card *card = (struct cs_card *) file->private_data;
2064         struct cs_state *state;
2065         DECLARE_WAITQUEUE(wait, current);
2066         struct dmabuf *dmabuf;
2067         ssize_t ret = 0;
2068         unsigned long flags;
2069         unsigned swptr;
2070         int cnt;
2071         unsigned copied=0;
2072
2073         CS_DBGOUT(CS_WAVE_READ | CS_FUNCTION, 4, 
2074                 printk("cs46xx: cs_read()+ %zd\n",count) );
2075         state = (struct cs_state *)card->states[0];
2076         if(!state)
2077                 return -ENODEV;
2078         dmabuf = &state->dmabuf;
2079
2080         if (dmabuf->mapped)
2081                 return -ENXIO;
2082         if (!access_ok(VERIFY_WRITE, buffer, count))
2083                 return -EFAULT;
2084         
2085         mutex_lock(&state->sem);
2086         if (!dmabuf->ready && (ret = __prog_dmabuf(state)))
2087                 goto out2;
2088
2089         add_wait_queue(&state->dmabuf.wait, &wait);
2090         while (count > 0) {
2091                 while(!(card->pm.flags & CS46XX_PM_IDLE))
2092                 {
2093                         schedule();
2094                         if (signal_pending(current)) {
2095                                 if(!ret) ret = -ERESTARTSYS;
2096                                 goto out;
2097                         }
2098                 }
2099                 spin_lock_irqsave(&state->card->lock, flags);
2100                 swptr = dmabuf->swptr;
2101                 cnt = dmabuf->dmasize - swptr;
2102                 if (dmabuf->count < cnt)
2103                         cnt = dmabuf->count;
2104                 if (cnt <= 0)
2105                         __set_current_state(TASK_INTERRUPTIBLE);
2106                 spin_unlock_irqrestore(&state->card->lock, flags);
2107
2108                 if (cnt > (count * dmabuf->divisor))
2109                         cnt = count * dmabuf->divisor;
2110                 if (cnt <= 0) {
2111                         /* buffer is empty, start the dma machine and wait for data to be
2112                            recorded */
2113                         start_adc(state);
2114                         if (file->f_flags & O_NONBLOCK) {
2115                                 if (!ret) ret = -EAGAIN;
2116                                 goto out;
2117                         }
2118                         mutex_unlock(&state->sem);
2119                         schedule();
2120                         if (signal_pending(current)) {
2121                                 if(!ret) ret = -ERESTARTSYS;
2122                                 goto out;
2123                         }
2124                         mutex_lock(&state->sem);
2125                         if (dmabuf->mapped) 
2126                         {
2127                                 if(!ret)
2128                                         ret = -ENXIO;
2129                                 goto out;
2130                         }
2131                         continue;
2132                 }
2133
2134                 CS_DBGOUT(CS_WAVE_READ, 2, printk(KERN_INFO 
2135                         "_read() copy_to cnt=%d count=%zd ", cnt,count) );
2136                 CS_DBGOUT(CS_WAVE_READ, 8, printk(KERN_INFO 
2137                         " .dmasize=%d .count=%d buffer=%p ret=%zd\n",
2138                         dmabuf->dmasize,dmabuf->count,buffer,ret) );
2139
2140                 if (cs_copy_to_user(state, buffer, 
2141                         (char *)dmabuf->rawbuf + swptr, cnt, &copied))
2142                 {
2143                         if (!ret) ret = -EFAULT;
2144                         goto out;
2145                 }
2146                 swptr = (swptr + cnt) % dmabuf->dmasize;
2147                 spin_lock_irqsave(&card->lock, flags);
2148                 dmabuf->swptr = swptr;
2149                 dmabuf->count -= cnt;
2150                 spin_unlock_irqrestore(&card->lock, flags);
2151                 count -= copied;
2152                 buffer += copied;
2153                 ret += copied;
2154                 start_adc(state);
2155         }
2156 out:
2157         remove_wait_queue(&state->dmabuf.wait, &wait);
2158 out2:
2159         mutex_unlock(&state->sem);
2160         set_current_state(TASK_RUNNING);
2161         CS_DBGOUT(CS_WAVE_READ | CS_FUNCTION, 4, 
2162                 printk("cs46xx: cs_read()- %zd\n",ret) );
2163         return ret;
2164 }
2165
2166 /* in this loop, dmabuf.count signifies the amount of data that is waiting to be dma to
2167    the soundcard.  it is drained by the dma machine and filled by this loop. */
2168 static ssize_t cs_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
2169 {
2170         struct cs_card *card = (struct cs_card *) file->private_data;
2171         struct cs_state *state;
2172         DECLARE_WAITQUEUE(wait, current);
2173         struct dmabuf *dmabuf;
2174         ssize_t ret;
2175         unsigned long flags;
2176         unsigned swptr;
2177         int cnt;
2178
2179         CS_DBGOUT(CS_WAVE_WRITE | CS_FUNCTION, 4,
2180                 printk("cs46xx: cs_write called, count = %zd\n", count) );
2181         state = (struct cs_state *)card->states[1];
2182         if(!state)
2183                 return -ENODEV;
2184         if (!access_ok(VERIFY_READ, buffer, count))
2185                 return -EFAULT;
2186         dmabuf = &state->dmabuf;
2187
2188         mutex_lock(&state->sem);
2189         if (dmabuf->mapped)
2190         {
2191                 ret = -ENXIO;
2192                 goto out;
2193         }
2194
2195         if (!dmabuf->ready && (ret = __prog_dmabuf(state)))
2196                 goto out;
2197         add_wait_queue(&state->dmabuf.wait, &wait);
2198         ret = 0;
2199 /*
2200 * Start the loop to read from the user's buffer and write to the dma buffer.
2201 * check for PM events and underrun/overrun in the loop.
2202 */
2203         while (count > 0) {
2204                 while(!(card->pm.flags & CS46XX_PM_IDLE))
2205                 {
2206                         schedule();
2207                         if (signal_pending(current)) {
2208                                 if(!ret) ret = -ERESTARTSYS;
2209                                 goto out;
2210                         }
2211                 }
2212                 spin_lock_irqsave(&state->card->lock, flags);
2213                 if (dmabuf->count < 0) {
2214                         /* buffer underrun, we are recovering from sleep_on_timeout,
2215                            resync hwptr and swptr */
2216                         dmabuf->count = 0;
2217                         dmabuf->swptr = dmabuf->hwptr;
2218                 }
2219                 if (dmabuf->underrun)
2220                 {
2221                         dmabuf->underrun = 0;
2222                         dmabuf->hwptr = cs_get_dma_addr(state);
2223                         dmabuf->swptr = dmabuf->hwptr;
2224                 }
2225
2226                 swptr = dmabuf->swptr;
2227                 cnt = dmabuf->dmasize - swptr;
2228                 if (dmabuf->count + cnt > dmabuf->dmasize)
2229                         cnt = dmabuf->dmasize - dmabuf->count;
2230                 if (cnt <= 0)
2231                         __set_current_state(TASK_INTERRUPTIBLE);
2232                 spin_unlock_irqrestore(&state->card->lock, flags);
2233
2234                 if (cnt > count)
2235                         cnt = count;
2236                 if (cnt <= 0) {
2237                         /* buffer is full, start the dma machine and wait for data to be
2238                            played */
2239                         start_dac(state);
2240                         if (file->f_flags & O_NONBLOCK) {
2241                                 if (!ret) ret = -EAGAIN;
2242                                 goto out;
2243                         }
2244                         mutex_unlock(&state->sem);
2245                         schedule();
2246                         if (signal_pending(current)) {
2247                                 if(!ret) ret = -ERESTARTSYS;
2248                                 goto out;
2249                         }
2250                         mutex_lock(&state->sem);
2251                         if (dmabuf->mapped)
2252                         {
2253                                 if(!ret)
2254                                         ret = -ENXIO;
2255                                 goto out;
2256                         }
2257                         continue;
2258                 }
2259                 if (copy_from_user(dmabuf->rawbuf + swptr, buffer, cnt)) {
2260                         if (!ret) ret = -EFAULT;
2261                         goto out;
2262                 }
2263                 spin_lock_irqsave(&state->card->lock, flags);
2264                 swptr = (swptr + cnt) % dmabuf->dmasize;
2265                 dmabuf->swptr = swptr;
2266                 dmabuf->count += cnt;
2267                 if(dmabuf->count > dmabuf->dmasize)
2268                 {
2269                         CS_DBGOUT(CS_WAVE_WRITE | CS_ERROR, 2, printk(
2270                             "cs46xx: cs_write() d->count > dmasize - resetting\n"));
2271                         dmabuf->count = dmabuf->dmasize;
2272                 }
2273                 dmabuf->endcleared = 0;
2274                 spin_unlock_irqrestore(&state->card->lock, flags);
2275
2276                 count -= cnt;
2277                 buffer += cnt;
2278                 ret += cnt;
2279                 start_dac(state);
2280         }
2281 out:
2282         mutex_unlock(&state->sem);
2283         remove_wait_queue(&state->dmabuf.wait, &wait);
2284         set_current_state(TASK_RUNNING);
2285
2286         CS_DBGOUT(CS_WAVE_WRITE | CS_FUNCTION, 2, 
2287                 printk("cs46xx: cs_write()- ret=%zd\n", ret) );
2288         return ret;
2289 }
2290
2291 static unsigned int cs_poll(struct file *file, struct poll_table_struct *wait)
2292 {
2293         struct cs_card *card = (struct cs_card *)file->private_data;
2294         struct dmabuf *dmabuf;
2295         struct cs_state *state;
2296
2297         unsigned long flags;
2298         unsigned int mask = 0;
2299
2300         CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: cs_poll()+ \n"));
2301         if (!(file->f_mode & (FMODE_WRITE | FMODE_READ)))
2302         {
2303                 return -EINVAL;
2304         }
2305         if (file->f_mode & FMODE_WRITE)
2306         {
2307                 state = card->states[1];
2308                 if(state)
2309                 {
2310                         dmabuf = &state->dmabuf;
2311                         poll_wait(file, &dmabuf->wait, wait);
2312                 }
2313         }
2314         if (file->f_mode & FMODE_READ)
2315         {
2316                 state = card->states[0];
2317                 if(state)
2318                 {
2319                         dmabuf = &state->dmabuf;
2320                         poll_wait(file, &dmabuf->wait, wait);
2321                 }
2322         }
2323
2324         spin_lock_irqsave(&card->lock, flags);
2325         cs_update_ptr(card, CS_FALSE);
2326         if (file->f_mode & FMODE_READ) {
2327                 state = card->states[0];
2328                 if(state)
2329                 {
2330                         dmabuf = &state->dmabuf;
2331                         if (dmabuf->count >= (signed)dmabuf->fragsize)
2332                                 mask |= POLLIN | POLLRDNORM;
2333                 }
2334         }
2335         if (file->f_mode & FMODE_WRITE) {
2336                 state = card->states[1];
2337                 if(state)
2338                 {
2339                         dmabuf = &state->dmabuf;
2340                         if (dmabuf->mapped) {
2341                                 if (dmabuf->count >= (signed)dmabuf->fragsize)
2342                                     mask |= POLLOUT | POLLWRNORM;
2343                         } else {
2344                                 if ((signed)dmabuf->dmasize >= dmabuf->count 
2345                                         + (signed)dmabuf->fragsize)
2346                                     mask |= POLLOUT | POLLWRNORM;
2347                         }
2348                 }
2349         }
2350         spin_unlock_irqrestore(&card->lock, flags);
2351
2352         CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: cs_poll()- (0x%x) \n",
2353                 mask));
2354         return mask;
2355 }
2356
2357 /*
2358  *      We let users mmap the ring buffer. Its not the real DMA buffer but
2359  *      that side of the code is hidden in the IRQ handling. We do a software
2360  *      emulation of DMA from a 64K or so buffer into a 2K FIFO. 
2361  *      (the hardware probably deserves a moan here but Crystal send me nice
2362  *      toys ;)).
2363  */
2364  
2365 static int cs_mmap(struct file *file, struct vm_area_struct *vma)
2366 {
2367         struct cs_card *card = (struct cs_card *)file->private_data;
2368         struct cs_state *state;
2369         struct dmabuf *dmabuf;
2370         int ret = 0;
2371         unsigned long size;
2372
2373         CS_DBGOUT(CS_FUNCTION | CS_PARMS, 2, printk("cs46xx: cs_mmap()+ file=%p %s %s\n", 
2374                 file, vma->vm_flags & VM_WRITE ? "VM_WRITE" : "",
2375                 vma->vm_flags & VM_READ ? "VM_READ" : "") );
2376
2377         if (vma->vm_flags & VM_WRITE) {
2378                 state = card->states[1];
2379                 if(state)
2380                 {
2381                         CS_DBGOUT(CS_OPEN, 2, printk(
2382                           "cs46xx: cs_mmap() VM_WRITE - state TRUE prog_dmabuf DAC\n") );
2383                         if ((ret = prog_dmabuf(state)) != 0)
2384                                 return ret;
2385                 }
2386         } else if (vma->vm_flags & VM_READ) {
2387                 state = card->states[0];
2388                 if(state)
2389                 {
2390                         CS_DBGOUT(CS_OPEN, 2, printk(
2391                           "cs46xx: cs_mmap() VM_READ - state TRUE prog_dmabuf ADC\n") );
2392                         if ((ret = prog_dmabuf(state)) != 0)
2393                                 return ret;
2394                 }
2395         } else {
2396                 CS_DBGOUT(CS_ERROR, 2, printk(
2397                   "cs46xx: cs_mmap() return -EINVAL\n") );
2398                 return -EINVAL;
2399         }
2400
2401 /*
2402  * For now ONLY support playback, but seems like the only way to use
2403  * mmap() is to open an FD with RDWR, just read or just write access
2404  * does not function, get an error back from the kernel.
2405  * Also, QuakeIII opens with RDWR!  So, there must be something
2406  * to needing read/write access mapping.  So, allow read/write but 
2407  * use the DAC only.
2408  */
2409         state = card->states[1];  
2410         if (!state) {
2411                 ret = -EINVAL;
2412                 goto out;
2413         }
2414
2415         mutex_lock(&state->sem);
2416         dmabuf = &state->dmabuf;
2417         if (cs4x_pgoff(vma) != 0)
2418         {
2419                 ret = -EINVAL;
2420                 goto out;
2421         }
2422         size = vma->vm_end - vma->vm_start;
2423
2424         CS_DBGOUT(CS_PARMS, 2, printk("cs46xx: cs_mmap(): size=%d\n",(unsigned)size) );
2425
2426         if (size > (PAGE_SIZE << dmabuf->buforder))
2427         {
2428                 ret = -EINVAL;
2429                 goto out;
2430         }
2431         if (remap_pfn_range(vma, vma->vm_start,
2432                              virt_to_phys(dmabuf->rawbuf) >> PAGE_SHIFT,
2433                              size, vma->vm_page_prot))
2434         {
2435                 ret = -EAGAIN;
2436                 goto out;
2437         }
2438         dmabuf->mapped = 1;
2439
2440         CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: cs_mmap()-\n") );
2441 out:
2442         mutex_unlock(&state->sem);
2443         return ret;     
2444 }
2445
2446 static int cs_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
2447 {
2448         struct cs_card *card = (struct cs_card *)file->private_data;
2449         struct cs_state *state;
2450         struct dmabuf *dmabuf=NULL;
2451         unsigned long flags;
2452         audio_buf_info abinfo;
2453         count_info cinfo;
2454         int val, valsave, mapped, ret;
2455         void __user *argp = (void __user *)arg;
2456         int __user *p = argp;
2457
2458         state = (struct cs_state *)card->states[0];
2459         if(state)
2460         {
2461                 dmabuf = &state->dmabuf;
2462                 mapped = (file->f_mode & FMODE_READ) && dmabuf->mapped;
2463         }
2464         state = (struct cs_state *)card->states[1];
2465         if(state)
2466         {
2467                 dmabuf = &state->dmabuf;
2468                 mapped |= (file->f_mode & FMODE_WRITE) && dmabuf->mapped;
2469         }
2470                 
2471 #if CSDEBUG
2472         printioctl(cmd);
2473 #endif
2474
2475         switch (cmd) 
2476         {
2477         case OSS_GETVERSION:
2478                 return put_user(SOUND_VERSION, p);
2479
2480         case SNDCTL_DSP_RESET:
2481                 /* FIXME: spin_lock ? */
2482                 if (file->f_mode & FMODE_WRITE) {
2483                         state = (struct cs_state *)card->states[1];
2484                         if(state)
2485                         {
2486                                 dmabuf = &state->dmabuf;
2487                                 stop_dac(state);
2488                                 synchronize_irq(card->irq);
2489                                 dmabuf->ready = 0;
2490                                 resync_dma_ptrs(state);
2491                                 dmabuf->swptr = dmabuf->hwptr = 0;
2492                                 dmabuf->count = dmabuf->total_bytes = 0;
2493                                 dmabuf->blocks = 0;
2494                                 dmabuf->SGok = 0;
2495                         }
2496                 }
2497                 if (file->f_mode & FMODE_READ) {
2498                         state = (struct cs_state *)card->states[0];
2499                         if(state)
2500                         {
2501                                 dmabuf = &state->dmabuf;
2502                                 stop_adc(state);
2503                                 synchronize_irq(card->irq);
2504                                 resync_dma_ptrs(state);
2505                                 dmabuf->ready = 0;
2506                                 dmabuf->swptr = dmabuf->hwptr = 0;
2507                                 dmabuf->count = dmabuf->total_bytes = 0;
2508                                 dmabuf->blocks = 0;
2509                                 dmabuf->SGok = 0;
2510                         }
2511                 }
2512                 CS_DBGOUT(CS_IOCTL, 2, printk("cs46xx: DSP_RESET()-\n") );
2513                 return 0;
2514
2515         case SNDCTL_DSP_SYNC:
2516                 if (file->f_mode & FMODE_WRITE)
2517                         return drain_dac(state, file->f_flags & O_NONBLOCK);
2518                 return 0;
2519
2520         case SNDCTL_DSP_SPEED: /* set sample rate */
2521                 if (get_user(val, p))
2522                         return -EFAULT;
2523                 if (val >= 0) {
2524                         if (file->f_mode & FMODE_READ) {
2525                                 state = (struct cs_state *)card->states[0];
2526                                 if(state)
2527                                 {
2528                                         dmabuf = &state->dmabuf;
2529                                         stop_adc(state);
2530                                         dmabuf->ready = 0;
2531                                         dmabuf->SGok = 0;
2532                                         cs_set_adc_rate(state, val);
2533                                         cs_set_divisor(dmabuf);
2534                                 }
2535                         }
2536                         if (file->f_mode & FMODE_WRITE) {
2537                                 state = (struct cs_state *)card->states[1];
2538                                 if(state)
2539                                 {
2540                                         dmabuf = &state->dmabuf;
2541                                         stop_dac(state);
2542                                         dmabuf->ready = 0;
2543                                         dmabuf->SGok = 0;
2544                                         cs_set_dac_rate(state, val);
2545                                         cs_set_divisor(dmabuf);
2546                                 }
2547                         }
2548                         CS_DBGOUT(CS_IOCTL | CS_PARMS, 4, printk(
2549                             "cs46xx: cs_ioctl() DSP_SPEED %s %s %d\n",
2550                                 file->f_mode & FMODE_WRITE ? "DAC" : "",
2551                                 file->f_mode & FMODE_READ ? "ADC" : "",
2552                                 dmabuf->rate ) );
2553                         return put_user(dmabuf->rate, p);
2554                 }
2555                 return put_user(0, p);
2556
2557         case SNDCTL_DSP_STEREO: /* set stereo or mono channel */
2558                 if (get_user(val, p))
2559                         return -EFAULT;
2560                 if (file->f_mode & FMODE_WRITE) {
2561                         state = (struct cs_state *)card->states[1];
2562                         if(state)
2563                         {
2564                                 dmabuf = &state->dmabuf;
2565                                 stop_dac(state);
2566                                 dmabuf->ready = 0;
2567                                 dmabuf->SGok = 0;
2568                                 if(val)
2569                                         dmabuf->fmt |= CS_FMT_STEREO;
2570                                 else
2571                                         dmabuf->fmt &= ~CS_FMT_STEREO;
2572                                 cs_set_divisor(dmabuf);
2573                                 CS_DBGOUT(CS_IOCTL | CS_PARMS, 4, printk(
2574                                     "cs46xx: DSP_STEREO() DAC %s\n",
2575                                     (dmabuf->fmt & CS_FMT_STEREO) ?
2576                                         "STEREO":"MONO") );
2577                         }
2578                 }
2579                 if (file->f_mode & FMODE_READ) {
2580                         state = (struct cs_state *)card->states[0];
2581                         if(state)
2582                         {
2583                                 dmabuf = &state->dmabuf;
2584                                 stop_adc(state);
2585                                 dmabuf->ready = 0;
2586                                 dmabuf->SGok = 0;
2587                                 if(val)
2588                                         dmabuf->fmt |= CS_FMT_STEREO;
2589                                 else
2590                                         dmabuf->fmt &= ~CS_FMT_STEREO;
2591                                 cs_set_divisor(dmabuf);
2592                                 CS_DBGOUT(CS_IOCTL | CS_PARMS, 4, printk(
2593                                     "cs46xx: DSP_STEREO() ADC %s\n",
2594                                     (dmabuf->fmt & CS_FMT_STEREO) ?
2595                                         "STEREO":"MONO") );
2596                         }
2597                 }
2598                 return 0;
2599
2600         case SNDCTL_DSP_GETBLKSIZE:
2601                 if (file->f_mode & FMODE_WRITE) {
2602                         state = (struct cs_state *)card->states[1];
2603                         if(state)
2604                         {
2605                                 dmabuf = &state->dmabuf;
2606                                 if ((val = prog_dmabuf(state)))
2607                                         return val;
2608                                 return put_user(dmabuf->fragsize, p);
2609                         }
2610                 }
2611                 if (file->f_mode & FMODE_READ) {
2612                         state = (struct cs_state *)card->states[0];
2613                         if(state)
2614                         {
2615                                 dmabuf = &state->dmabuf;
2616                                 if ((val = prog_dmabuf(state)))
2617                                         return val;
2618                                 return put_user(dmabuf->fragsize/dmabuf->divisor, 
2619                                                 p);
2620                         }
2621                 }
2622                 return put_user(0, p);
2623
2624         case SNDCTL_DSP_GETFMTS: /* Returns a mask of supported sample format*/
2625                 return put_user(AFMT_S16_LE | AFMT_U8, p);
2626
2627         case SNDCTL_DSP_SETFMT: /* Select sample format */
2628                 if (get_user(val, p))
2629                         return -EFAULT;
2630                 CS_DBGOUT(CS_IOCTL | CS_PARMS, 4, printk(
2631                     "cs46xx: cs_ioctl() DSP_SETFMT %s %s %s %s\n",
2632                         file->f_mode & FMODE_WRITE ? "DAC" : "",
2633                         file->f_mode & FMODE_READ ? "ADC" : "",
2634                         val == AFMT_S16_LE ? "16Bit Signed" : "",
2635                         val == AFMT_U8 ? "8Bit Unsigned" : "") );
2636                 valsave = val;
2637                 if (val != AFMT_QUERY) {
2638                         if(val==AFMT_S16_LE || val==AFMT_U8)
2639                         {
2640                                 if (file->f_mode & FMODE_WRITE) {
2641                                         state = (struct cs_state *)card->states[1];
2642                                         if(state)
2643                                         {
2644                                                 dmabuf = &state->dmabuf;
2645                                                 stop_dac(state);
2646                                                 dmabuf->ready = 0;
2647                                                 dmabuf->SGok = 0;
2648                                                 if(val==AFMT_S16_LE)
2649                                                         dmabuf->fmt |= CS_FMT_16BIT;
2650                                                 else
2651                                                         dmabuf->fmt &= ~CS_FMT_16BIT;
2652                                                 cs_set_divisor(dmabuf);
2653                                                 if((ret = prog_dmabuf(state)))
2654                                                         return ret;
2655                                         }
2656                                 }
2657                                 if (file->f_mode & FMODE_READ) {
2658                                         val = valsave;
2659                                         state = (struct cs_state *)card->states[0];
2660                                         if(state)
2661                                         {
2662                                                 dmabuf = &state->dmabuf;
2663                                                 stop_adc(state);
2664                                                 dmabuf->ready = 0;
2665                                                 dmabuf->SGok = 0;
2666                                                 if(val==AFMT_S16_LE)
2667                                                         dmabuf->fmt |= CS_FMT_16BIT;
2668                                                 else
2669                                                         dmabuf->fmt &= ~CS_FMT_16BIT;
2670                                                 cs_set_divisor(dmabuf);
2671                                                 if((ret = prog_dmabuf(state)))
2672                                                         return ret;
2673                                         }
2674                                 }
2675                         }
2676                         else
2677                         {
2678                                 CS_DBGOUT(CS_IOCTL | CS_ERROR, 2, printk(
2679                                     "cs46xx: DSP_SETFMT() Unsupported format (0x%x)\n",
2680                                         valsave) );
2681                         }
2682                 }
2683                 else
2684                 {
2685                         if(file->f_mode & FMODE_WRITE)
2686                         {
2687                                 state = (struct cs_state *)card->states[1];
2688                                 if(state)
2689                                         dmabuf = &state->dmabuf;
2690                         }
2691                         else if(file->f_mode & FMODE_READ)
2692                         {
2693                                 state = (struct cs_state *)card->states[0];
2694                                 if(state)
2695                                         dmabuf = &state->dmabuf;
2696                         }
2697                 }
2698                 if(dmabuf)
2699                 {
2700                         if(dmabuf->fmt & CS_FMT_16BIT)
2701                                 return put_user(AFMT_S16_LE, p);
2702                         else
2703                                 return put_user(AFMT_U8, p);
2704                 }
2705                 return put_user(0, p);
2706
2707         case SNDCTL_DSP_CHANNELS:
2708                 if (get_user(val, p))
2709                         return -EFAULT;
2710                 if (val != 0) {
2711                         if (file->f_mode & FMODE_WRITE) {
2712                                 state = (struct cs_state *)card->states[1];
2713                                 if(state)
2714                                 {
2715                                         dmabuf = &state->dmabuf;
2716                                         stop_dac(state);
2717                                         dmabuf->ready = 0;
2718                                         dmabuf->SGok = 0;
2719                                         if(val>1)
2720                                                 dmabuf->fmt |= CS_FMT_STEREO;
2721                                         else
2722                                                 dmabuf->fmt &= ~CS_FMT_STEREO;
2723                                         cs_set_divisor(dmabuf);
2724                                         if (prog_dmabuf(state))
2725                                                 return 0;
2726                                 }
2727                         }
2728                         if (file->f_mode & FMODE_READ) {
2729                                 state = (struct cs_state *)card->states[0];
2730                                 if(state)
2731                                 {
2732                                         dmabuf = &state->dmabuf;
2733                                         stop_adc(state);
2734                                         dmabuf->ready = 0;
2735                                         dmabuf->SGok = 0;
2736                                         if(val>1)
2737                                                 dmabuf->fmt |= CS_FMT_STEREO;
2738                                         else
2739                                                 dmabuf->fmt &= ~CS_FMT_STEREO;
2740                                         cs_set_divisor(dmabuf);
2741                                         if (prog_dmabuf(state))
2742                                                 return 0;
2743                                 }
2744                         }
2745                 }
2746                 return put_user((dmabuf->fmt & CS_FMT_STEREO) ? 2 : 1,
2747                                 p);
2748
2749         case SNDCTL_DSP_POST:
2750                 /*
2751                  * There will be a longer than normal pause in the data.
2752                  * so... do nothing, because there is nothing that we can do.
2753                  */
2754                 return 0;
2755
2756         case SNDCTL_DSP_SUBDIVIDE:
2757                 if (file->f_mode & FMODE_WRITE) {
2758                         state = (struct cs_state *)card->states[1];
2759                         if(state)
2760                         {
2761                                 dmabuf = &state->dmabuf;
2762                                 if (dmabuf->subdivision)
2763                                         return -EINVAL;
2764                                 if (get_user(val, p))
2765                                         return -EFAULT;
2766                                 if (val != 1 && val != 2)
2767                                         return -EINVAL;
2768                                 dmabuf->subdivision = val;
2769                         }
2770                 }
2771                 if (file->f_mode & FMODE_READ) {
2772                         state = (struct cs_state *)card->states[0];
2773                         if(state)
2774                         {
2775                                 dmabuf = &state->dmabuf;
2776                                 if (dmabuf->subdivision)
2777                                         return -EINVAL;
2778                                 if (get_user(val, p))
2779                                         return -EFAULT;
2780                                 if (val != 1 && val != 2)
2781                                         return -EINVAL;
2782                                 dmabuf->subdivision = val;
2783                         }
2784                 }
2785                 return 0;
2786
2787         case SNDCTL_DSP_SETFRAGMENT:
2788                 if (get_user(val, p))
2789                         return -EFAULT;
2790
2791                 if (file->f_mode & FMODE_WRITE) {
2792                         state = (struct cs_state *)card->states[1];
2793                         if(state)
2794                         {
2795                                 dmabuf = &state->dmabuf;
2796                                 dmabuf->ossfragshift = val & 0xffff;
2797                                 dmabuf->ossmaxfrags = (val >> 16) & 0xffff;
2798                         }
2799                 }
2800                 if (file->f_mode & FMODE_READ) {
2801                         state = (struct cs_state *)card->states[0];
2802                         if(state)
2803                         {
2804                                 dmabuf = &state->dmabuf;
2805                                 dmabuf->ossfragshift = val & 0xffff;
2806                                 dmabuf->ossmaxfrags = (val >> 16) & 0xffff;
2807                         }
2808                 }
2809                 return 0;
2810
2811         case SNDCTL_DSP_GETOSPACE:
2812                 if (!(file->f_mode & FMODE_WRITE))
2813                         return -EINVAL;
2814                 state = (struct cs_state *)card->states[1];
2815                 if(state)
2816                 {
2817                         dmabuf = &state->dmabuf;
2818                         spin_lock_irqsave(&state->card->lock, flags);
2819                         cs_update_ptr(card, CS_TRUE);
2820                         abinfo.fragsize = dmabuf->fragsize;
2821                         abinfo.fragstotal = dmabuf->numfrag;
2822                 /*
2823                  * for mmap we always have total space available
2824                  */
2825                         if (dmabuf->mapped)
2826                                 abinfo.bytes = dmabuf->dmasize;
2827                         else
2828                                 abinfo.bytes = dmabuf->dmasize - dmabuf->count;
2829
2830                         abinfo.fragments = abinfo.bytes >> dmabuf->fragshift;
2831                         spin_unlock_irqrestore(&state->card->lock, flags);
2832                         return copy_to_user(argp, &abinfo, sizeof(abinfo)) ? -EFAULT : 0;
2833                 }
2834                 return -ENODEV;
2835
2836         case SNDCTL_DSP_GETISPACE:
2837                 if (!(file->f_mode & FMODE_READ))
2838                         return -EINVAL;
2839                 state = (struct cs_state *)card->states[0];
2840                 if(state)
2841                 {
2842                         dmabuf = &state->dmabuf;
2843                         spin_lock_irqsave(&state->card->lock, flags);
2844                         cs_update_ptr(card, CS_TRUE);
2845                         abinfo.fragsize = dmabuf->fragsize/dmabuf->divisor;
2846                         abinfo.bytes = dmabuf->count/dmabuf->divisor;
2847                         abinfo.fragstotal = dmabuf->numfrag;
2848                         abinfo.fragments = abinfo.bytes >> dmabuf->fragshift;
2849                         spin_unlock_irqrestore(&state->card->lock, flags);
2850                         return copy_to_user(argp, &abinfo, sizeof(abinfo)) ? -EFAULT : 0;
2851                 }
2852                 return -ENODEV;
2853
2854         case SNDCTL_DSP_NONBLOCK:
2855                 file->f_flags |= O_NONBLOCK;
2856                 return 0;
2857
2858         case SNDCTL_DSP_GETCAPS:
2859                 return put_user(DSP_CAP_REALTIME|DSP_CAP_TRIGGER|DSP_CAP_MMAP,
2860                             p);
2861
2862         case SNDCTL_DSP_GETTRIGGER:
2863                 val = 0;
2864                 CS_DBGOUT(CS_IOCTL, 2, printk("cs46xx: DSP_GETTRIGGER()+\n") );
2865                 if (file->f_mode & FMODE_WRITE)
2866                 {
2867                         state = (struct cs_state *)card->states[1];
2868                         if(state)
2869                         {
2870                                 dmabuf = &state->dmabuf;
2871                                 if(dmabuf->enable & DAC_RUNNING)
2872                                         val |= PCM_ENABLE_INPUT;
2873                         }
2874                 }
2875                 if (file->f_mode & FMODE_READ)
2876                 {
2877                         if(state)
2878                         {
2879                                 state = (struct cs_state *)card->states[0];
2880                                 dmabuf = &state->dmabuf;
2881                                 if(dmabuf->enable & ADC_RUNNING)
2882                                         val |= PCM_ENABLE_OUTPUT;
2883                         }
2884                 }
2885                 CS_DBGOUT(CS_IOCTL, 2, printk("cs46xx: DSP_GETTRIGGER()- val=0x%x\n",val) );
2886                 return put_user(val, p);
2887
2888         case SNDCTL_DSP_SETTRIGGER:
2889                 if (get_user(val, p))
2890                         return -EFAULT;
2891                 if (file->f_mode & FMODE_READ) {
2892                         state = (struct cs_state *)card->states[0];
2893                         if(state)
2894                         {
2895                                 dmabuf = &state->dmabuf;
2896                                 if (val & PCM_ENABLE_INPUT) {
2897                                         if (!dmabuf->ready && (ret = prog_dmabuf(state)))
2898                                                 return ret;
2899                                         start_adc(state);
2900                                 } else
2901                                         stop_adc(state);
2902                         }
2903                 }
2904                 if (file->f_mode & FMODE_WRITE) {
2905                         state = (struct cs_state *)card->states[1];
2906                         if(state)
2907                         {
2908                                 dmabuf = &state->dmabuf;
2909                                 if (val & PCM_ENABLE_OUTPUT) {
2910                                         if (!dmabuf->ready && (ret = prog_dmabuf(state)))
2911                                                 return ret;
2912                                         start_dac(state);
2913                                 } else
2914                                         stop_dac(state);
2915                         }
2916                 }
2917                 return 0;
2918
2919         case SNDCTL_DSP_GETIPTR:
2920                 if (!(file->f_mode & FMODE_READ))
2921                         return -EINVAL;
2922                 state = (struct cs_state *)card->states[0];
2923                 if(state)
2924                 {
2925                         dmabuf = &state->dmabuf;
2926                         spin_lock_irqsave(&state->card->lock, flags);
2927                         cs_update_ptr(card, CS_TRUE);
2928                         cinfo.bytes = dmabuf->total_bytes/dmabuf->divisor;
2929                         cinfo.blocks = dmabuf->count/dmabuf->divisor >> dmabuf->fragshift;
2930                         cinfo.ptr = dmabuf->hwptr/dmabuf->divisor;
2931                         spin_unlock_irqrestore(&state->card->lock, flags);
2932                         if (copy_to_user(argp, &cinfo, sizeof(cinfo)))
2933                                 return -EFAULT;
2934                         return 0;
2935                 }
2936                 return -ENODEV;
2937
2938         case SNDCTL_DSP_GETOPTR:
2939                 if (!(file->f_mode & FMODE_WRITE))
2940                         return -EINVAL;
2941                 state = (struct cs_state *)card->states[1];
2942                 if(state)
2943                 {
2944                         dmabuf = &state->dmabuf;
2945                         spin_lock_irqsave(&state->card->lock, flags);
2946                         cs_update_ptr(card, CS_TRUE);
2947                         cinfo.bytes = dmabuf->total_bytes;
2948                         if (dmabuf->mapped)
2949                         {
2950                                 cinfo.blocks = (cinfo.bytes >> dmabuf->fragshift) 
2951                                                         - dmabuf->blocks;
2952                                 CS_DBGOUT(CS_PARMS, 8, 
2953                                         printk("total_bytes=%d blocks=%d dmabuf->blocks=%d\n", 
2954                                         cinfo.bytes,cinfo.blocks,dmabuf->blocks) );
2955                                 dmabuf->blocks = cinfo.bytes >> dmabuf->fragshift;
2956                         }
2957                         else
2958                         {
2959                                 cinfo.blocks = dmabuf->count >> dmabuf->fragshift;
2960                         }
2961                         cinfo.ptr = dmabuf->hwptr;
2962
2963                         CS_DBGOUT(CS_PARMS, 4, printk(
2964                             "cs46xx: GETOPTR bytes=%d blocks=%d ptr=%d\n",
2965                                 cinfo.bytes,cinfo.blocks,cinfo.ptr) );
2966                         spin_unlock_irqrestore(&state->card->lock, flags);
2967                         if (copy_to_user(argp, &cinfo, sizeof(cinfo)))
2968                                 return -EFAULT;
2969                         return 0;
2970                 }
2971                 return -ENODEV;
2972
2973         case SNDCTL_DSP_SETDUPLEX:
2974                 return 0;
2975
2976         case SNDCTL_DSP_GETODELAY:
2977                 if (!(file->f_mode & FMODE_WRITE))
2978                         return -EINVAL;
2979                 state = (struct cs_state *)card->states[1];
2980                 if(state)
2981                 {
2982                         dmabuf = &state->dmabuf;
2983                         spin_lock_irqsave(&state->card->lock, flags);
2984                         cs_update_ptr(card, CS_TRUE);
2985                         val = dmabuf->count;
2986                         spin_unlock_irqrestore(&state->card->lock, flags);
2987                 }
2988                 else
2989                         val = 0;
2990                 return put_user(val, p);
2991
2992         case SOUND_PCM_READ_RATE:
2993                 if(file->f_mode & FMODE_READ)
2994                         state = (struct cs_state *)card->states[0];
2995                 else 
2996                         state = (struct cs_state *)card->states[1];
2997                 if(state)
2998                 {
2999                         dmabuf = &state->dmabuf;
3000                         return put_user(dmabuf->rate, p);
3001                 }
3002                 return put_user(0, p);
3003                 
3004
3005         case SOUND_PCM_READ_CHANNELS:
3006                 if(file->f_mode & FMODE_READ)
3007                         state = (struct cs_state *)card->states[0];
3008                 else 
3009                         state = (struct cs_state *)card->states[1];
3010                 if(state)
3011                 {
3012                         dmabuf = &state->dmabuf;
3013                         return put_user((dmabuf->fmt & CS_FMT_STEREO) ? 2 : 1,
3014                                 p);
3015                 }
3016                 return put_user(0, p);
3017
3018         case SOUND_PCM_READ_BITS:
3019                 if(file->f_mode & FMODE_READ)
3020                         state = (struct cs_state *)card->states[0];
3021                 else 
3022                         state = (struct cs_state *)card->states[1];
3023                 if(state)
3024                 {
3025                         dmabuf = &state->dmabuf;
3026                         return put_user((dmabuf->fmt & CS_FMT_16BIT) ? 
3027                                 AFMT_S16_LE : AFMT_U8, p);
3028
3029                 }
3030                 return put_user(0, p);
3031
3032         case SNDCTL_DSP_MAPINBUF:
3033         case SNDCTL_DSP_MAPOUTBUF:
3034         case SNDCTL_DSP_SETSYNCRO:
3035         case SOUND_PCM_WRITE_FILTER:
3036         case SOUND_PCM_READ_FILTER:
3037                 return -EINVAL;
3038         }
3039         return -EINVAL;
3040 }
3041
3042
3043 /*
3044  *      AMP control - null AMP
3045  */
3046  
3047 static void amp_none(struct cs_card *card, int change)
3048 {       
3049 }
3050
3051 /*
3052  *      Crystal EAPD mode
3053  */
3054  
3055 static void amp_voyetra(struct cs_card *card, int change)
3056 {
3057         /* Manage the EAPD bit on the Crystal 4297 
3058            and the Analog AD1885 */
3059            
3060         int old=card->amplifier;
3061         
3062         card->amplifier+=change;
3063         if(card->amplifier && !old)
3064         {
3065                 /* Turn the EAPD amp on */
3066                 cs_ac97_set(card->ac97_codec[0],  AC97_POWER_CONTROL, 
3067                         cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) |
3068                                 0x8000);
3069         }
3070         else if(old && !card->amplifier)
3071         {
3072                 /* Turn the EAPD amp off */
3073                 cs_ac97_set(card->ac97_codec[0],  AC97_POWER_CONTROL, 
3074                         cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) &
3075                                 ~0x8000);
3076         }
3077 }
3078
3079                        
3080 /*
3081  *      Game Theatre XP card - EGPIO[2] is used to enable the external amp.
3082  */
3083  
3084 static void amp_hercules(struct cs_card *card, int change)
3085 {
3086         int old=card->amplifier;
3087         if(!card)
3088         {
3089                 CS_DBGOUT(CS_ERROR, 2, printk(KERN_INFO 
3090                         "cs46xx: amp_hercules() called before initialized.\n"));
3091                 return;
3092         }
3093         card->amplifier+=change;
3094         if( (card->amplifier && !old) && !(hercules_egpio_disable))
3095         {
3096                 CS_DBGOUT(CS_PARMS, 4, printk(KERN_INFO 
3097                         "cs46xx: amp_hercules() external amp enabled\n"));
3098                 cs461x_pokeBA0(card, BA0_EGPIODR, 
3099                         EGPIODR_GPOE2);     /* enable EGPIO2 output */
3100                 cs461x_pokeBA0(card, BA0_EGPIOPTR, 
3101                         EGPIOPTR_GPPT2);   /* open-drain on output */
3102         }
3103         else if(old && !card->amplifier)
3104         {
3105                 CS_DBGOUT(CS_PARMS, 4, printk(KERN_INFO 
3106                         "cs46xx: amp_hercules() external amp disabled\n"));
3107                 cs461x_pokeBA0(card, BA0_EGPIODR, 0); /* disable */
3108                 cs461x_pokeBA0(card, BA0_EGPIOPTR, 0); /* disable */
3109         }
3110 }
3111
3112 /*
3113  *      Handle the CLKRUN on a thinkpad. We must disable CLKRUN support
3114  *      whenever we need to beat on the chip.
3115  *
3116  *      The original idea and code for this hack comes from David Kaiser at
3117  *      Linuxcare. Perhaps one day Crystal will document their chips well
3118  *      enough to make them useful.
3119  */
3120  
3121 static void clkrun_hack(struct cs_card *card, int change)
3122 {
3123         struct pci_dev *acpi_dev;
3124         u16 control;
3125         u8 pp;
3126         unsigned long port;
3127         int old=card->active;
3128         
3129         card->active+=change;
3130         
3131         acpi_dev = pci_find_device(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371AB_3, NULL);
3132         if(acpi_dev == NULL)
3133                 return;         /* Not a thinkpad thats for sure */
3134
3135         /* Find the control port */             
3136         pci_read_config_byte(acpi_dev, 0x41, &pp);
3137         port=pp<<8;
3138
3139         /* Read ACPI port */    
3140         control=inw(port+0x10);
3141
3142         /* Flip CLKRUN off while running */
3143         if(!card->active && old)
3144         {
3145                 CS_DBGOUT(CS_PARMS , 9, printk( KERN_INFO
3146                         "cs46xx: clkrun() enable clkrun - change=%d active=%d\n",
3147                                 change,card->active));
3148                 outw(control|0x2000, port+0x10);
3149         }
3150         else 
3151         {
3152         /*
3153         * sometimes on a resume the bit is set, so always reset the bit.
3154         */
3155                 CS_DBGOUT(CS_PARMS , 9, printk( KERN_INFO
3156                         "cs46xx: clkrun() disable clkrun - change=%d active=%d\n",
3157                                 change,card->active));
3158                 outw(control&~0x2000, port+0x10);
3159         }
3160 }
3161
3162         
3163 static int cs_open(struct inode *inode, struct file *file)
3164 {
3165         struct cs_card *card = (struct cs_card *)file->private_data;
3166         struct cs_state *state = NULL;
3167         struct dmabuf *dmabuf = NULL;
3168         struct list_head *entry;
3169         unsigned int minor = iminor(inode);
3170         int ret=0;
3171         unsigned int tmp;
3172
3173         CS_DBGOUT(CS_OPEN | CS_FUNCTION, 2, printk("cs46xx: cs_open()+ file=%p %s %s\n",
3174                 file, file->f_mode & FMODE_WRITE ? "FMODE_WRITE" : "",
3175                 file->f_mode & FMODE_READ ? "FMODE_READ" : "") );
3176
3177         list_for_each(entry, &cs46xx_devs)
3178         {
3179                 card = list_entry(entry, struct cs_card, list);
3180
3181                 if (!((card->dev_audio ^ minor) & ~0xf))
3182                         break;
3183         }
3184         if (entry == &cs46xx_devs)
3185                 return -ENODEV;
3186         if (!card) {
3187                 CS_DBGOUT(CS_FUNCTION | CS_OPEN, 2, printk(KERN_INFO
3188                         "cs46xx: cs_open(): Error - unable to find audio card struct\n"));
3189                 return -ENODEV;
3190         }
3191
3192         /*
3193          * hardcode state[0] for capture, [1] for playback
3194          */
3195         if(file->f_mode & FMODE_READ)
3196         {
3197                 CS_DBGOUT(CS_WAVE_READ, 2, printk("cs46xx: cs_open() FMODE_READ\n") );
3198                 if (card->states[0] == NULL) {
3199                         state = card->states[0] = (struct cs_state *)
3200                                 kmalloc(sizeof(struct cs_state), GFP_KERNEL);
3201                         if (state == NULL)
3202                                 return -ENOMEM;
3203                         memset(state, 0, sizeof(struct cs_state));
3204                         mutex_init(&state->sem);
3205                         dmabuf = &state->dmabuf;
3206                         dmabuf->pbuf = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
3207                         if(dmabuf->pbuf==NULL)
3208                         {
3209                                 kfree(state);
3210                                 card->states[0]=NULL;
3211                                 return -ENOMEM;
3212                         }
3213                 }
3214                 else
3215                 {
3216                         state = card->states[0];
3217                         if(state->open_mode & FMODE_READ)
3218                                 return -EBUSY;
3219                 }
3220                 dmabuf->channel = card->alloc_rec_pcm_channel(card);
3221                         
3222                 if (dmabuf->channel == NULL) {
3223                         kfree (card->states[0]);
3224                         card->states[0] = NULL;
3225                         return -ENODEV;
3226                 }
3227
3228                 /* Now turn on external AMP if needed */
3229                 state->card = card;
3230                 state->card->active_ctrl(state->card,1);
3231                 state->card->amplifier_ctrl(state->card,1);
3232                 
3233                 if( (tmp = cs46xx_powerup(card, CS_POWER_ADC)) )
3234                 {
3235                         CS_DBGOUT(CS_ERROR | CS_INIT, 1, printk(KERN_INFO 
3236                                 "cs46xx: cs46xx_powerup of ADC failed (0x%x)\n",tmp) );
3237                         return -EIO;
3238                 }
3239
3240                 dmabuf->channel->state = state;
3241                 /* initialize the virtual channel */
3242                 state->virt = 0;
3243                 state->magic = CS_STATE_MAGIC;
3244                 init_waitqueue_head(&dmabuf->wait);
3245                 mutex_init(&state->open_mutex);
3246                 file->private_data = card;
3247
3248                 mutex_lock(&state->open_mutex);
3249
3250                 /* set default sample format. According to OSS Programmer's Guide  /dev/dsp
3251                    should be default to unsigned 8-bits, mono, with sample rate 8kHz and
3252                    /dev/dspW will accept 16-bits sample */
3253
3254                 /* Default input is 8bit mono */
3255                 dmabuf->fmt &= ~CS_FMT_MASK;
3256                 dmabuf->type = CS_TYPE_ADC;
3257                 dmabuf->ossfragshift = 0;
3258                 dmabuf->ossmaxfrags  = 0;
3259                 dmabuf->subdivision  = 0;
3260                 cs_set_adc_rate(state, 8000);
3261                 cs_set_divisor(dmabuf);
3262
3263                 state->open_mode |= FMODE_READ;
3264                 mutex_unlock(&state->open_mutex);
3265         }
3266         if(file->f_mode & FMODE_WRITE)
3267         {
3268                 CS_DBGOUT(CS_OPEN, 2, printk("cs46xx: cs_open() FMODE_WRITE\n") );
3269                 if (card->states[1] == NULL) {
3270                         state = card->states[1] = (struct cs_state *)
3271                                 kmalloc(sizeof(struct cs_state), GFP_KERNEL);
3272                         if (state == NULL)
3273                                 return -ENOMEM;
3274                         memset(state, 0, sizeof(struct cs_state));
3275                         mutex_init(&state->sem);
3276                         dmabuf = &state->dmabuf;
3277                         dmabuf->pbuf = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
3278                         if(dmabuf->pbuf==NULL)
3279                         {
3280                                 kfree(state);
3281                                 card->states[1]=NULL;
3282                                 return -ENOMEM;
3283                         }
3284                 }
3285                 else
3286                 {
3287                         state = card->states[1];
3288                         if(state->open_mode & FMODE_WRITE)
3289                                 return -EBUSY;
3290                 }
3291                 dmabuf->channel = card->alloc_pcm_channel(card);
3292                         
3293                 if (dmabuf->channel == NULL) {
3294                         kfree (card->states[1]);
3295                         card->states[1] = NULL;
3296                         return -ENODEV;
3297                 }
3298
3299                 /* Now turn on external AMP if needed */
3300                 state->card = card;
3301                 state->card->active_ctrl(state->card,1);
3302                 state->card->amplifier_ctrl(state->card,1);
3303
3304                 if( (tmp = cs46xx_powerup(card, CS_POWER_DAC)) )
3305                 {
3306                         CS_DBGOUT(CS_ERROR | CS_INIT, 1, printk(KERN_INFO 
3307                                 "cs46xx: cs46xx_powerup of DAC failed (0x%x)\n",tmp) );
3308                         return -EIO;
3309                 }
3310                 
3311                 dmabuf->channel->state = state;
3312                 /* initialize the virtual channel */
3313                 state->virt = 1;
3314                 state->magic = CS_STATE_MAGIC;
3315                 init_waitqueue_head(&dmabuf->wait);
3316                 mutex_init(&state->open_mutex);
3317                 file->private_data = card;
3318
3319                 mutex_lock(&state->open_mutex);
3320
3321                 /* set default sample format. According to OSS Programmer's Guide  /dev/dsp
3322                    should be default to unsigned 8-bits, mono, with sample rate 8kHz and
3323                    /dev/dspW will accept 16-bits sample */
3324
3325                 /* Default output is 8bit mono. */
3326                 dmabuf->fmt &= ~CS_FMT_MASK;
3327                 dmabuf->type = CS_TYPE_DAC;
3328                 dmabuf->ossfragshift = 0;
3329                 dmabuf->ossmaxfrags  = 0;
3330                 dmabuf->subdivision  = 0;
3331                 cs_set_dac_rate(state, 8000);
3332                 cs_set_divisor(dmabuf);
3333
3334                 state->open_mode |= FMODE_WRITE;
3335                 mutex_unlock(&state->open_mutex);
3336                 if((ret = prog_dmabuf(state)))
3337                         return ret;
3338         }
3339         CS_DBGOUT(CS_OPEN | CS_FUNCTION, 2, printk("cs46xx: cs_open()- 0\n") );
3340         return nonseekable_open(inode, file);
3341 }
3342
3343 static int cs_release(struct inode *inode, struct file *file)
3344 {
3345         struct cs_card *card = (struct cs_card *)file->private_data;
3346         struct dmabuf *dmabuf;
3347         struct cs_state *state;
3348         unsigned int tmp;
3349         CS_DBGOUT(CS_RELEASE | CS_FUNCTION, 2, printk("cs46xx: cs_release()+ file=%p %s %s\n",
3350                 file, file->f_mode & FMODE_WRITE ? "FMODE_WRITE" : "",
3351                 file->f_mode & FMODE_READ ? "FMODE_READ" : "") );
3352
3353         if (!(file->f_mode & (FMODE_WRITE | FMODE_READ)))
3354         {
3355                 return -EINVAL;
3356         }
3357         state = card->states[1];
3358         if(state)
3359         {
3360                 if ( (state->open_mode & FMODE_WRITE) & (file->f_mode & FMODE_WRITE) )
3361                 {
3362                         CS_DBGOUT(CS_RELEASE, 2, printk("cs46xx: cs_release() FMODE_WRITE\n") );
3363                         dmabuf = &state->dmabuf;
3364                         cs_clear_tail(state);
3365                         drain_dac(state, file->f_flags & O_NONBLOCK);
3366                         /* stop DMA state machine and free DMA buffers/channels */
3367                         mutex_lock(&state->open_mutex);
3368                         stop_dac(state);
3369                         dealloc_dmabuf(state);
3370                         state->card->free_pcm_channel(state->card, dmabuf->channel->num);
3371                         free_page((unsigned long)state->dmabuf.pbuf);
3372
3373                         /* we're covered by the open_mutex */
3374                         mutex_unlock(&state->open_mutex);
3375                         state->card->states[state->virt] = NULL;
3376                         state->open_mode &= (~file->f_mode) & (FMODE_READ|FMODE_WRITE);
3377
3378                         if( (tmp = cs461x_powerdown(card, CS_POWER_DAC, CS_FALSE )) )
3379                         {
3380                                 CS_DBGOUT(CS_ERROR, 1, printk(KERN_INFO 
3381                                         "cs46xx: cs_release_mixdev() powerdown DAC failure (0x%x)\n",tmp) );
3382                         }
3383
3384                         /* Now turn off external AMP if needed */
3385                         state->card->amplifier_ctrl(state->card, -1);
3386                         state->card->active_ctrl(state->card, -1);
3387
3388                         kfree(state);
3389                 }
3390         }
3391
3392         state = card->states[0];
3393         if(state)
3394         {
3395                 if ( (state->open_mode & FMODE_READ) & (file->f_mode & FMODE_READ) )
3396                 {
3397                         CS_DBGOUT(CS_RELEASE, 2, printk("cs46xx: cs_release() FMODE_READ\n") );
3398                         dmabuf = &state->dmabuf;
3399                         mutex_lock(&state->open_mutex);
3400                         stop_adc(state);
3401                         dealloc_dmabuf(state);
3402                         state->card->free_pcm_channel(state->card, dmabuf->channel->num);
3403                         free_page((unsigned long)state->dmabuf.pbuf);
3404
3405                         /* we're covered by the open_mutex */
3406                         mutex_unlock(&state->open_mutex);
3407                         state->card->states[state->virt] = NULL;
3408                         state->open_mode &= (~file->f_mode) & (FMODE_READ|FMODE_WRITE);
3409
3410                         if( (tmp = cs461x_powerdown(card, CS_POWER_ADC, CS_FALSE )) )
3411                         {
3412                                 CS_DBGOUT(CS_ERROR, 1, printk(KERN_INFO 
3413                                         "cs46xx: cs_release_mixdev() powerdown ADC failure (0x%x)\n",tmp) );
3414                         }
3415
3416                         /* Now turn off external AMP if needed */
3417                         state->card->amplifier_ctrl(state->card, -1);
3418                         state->card->active_ctrl(state->card, -1);
3419
3420                         kfree(state);
3421                 }
3422         }
3423
3424         CS_DBGOUT(CS_FUNCTION | CS_RELEASE, 2, printk("cs46xx: cs_release()- 0\n") );
3425         return 0;
3426 }
3427
3428 static void printpm(struct cs_card *s)
3429 {
3430         CS_DBGOUT(CS_PM, 9, printk("pm struct:\n"));
3431         CS_DBGOUT(CS_PM, 9, printk("flags:0x%x u32CLKCR1_SAVE: 0%x u32SSPMValue: 0x%x\n",
3432                 (unsigned)s->pm.flags,s->pm.u32CLKCR1_SAVE,s->pm.u32SSPMValue));
3433         CS_DBGOUT(CS_PM, 9, printk("u32PPLVCvalue: 0x%x u32PPRVCvalue: 0x%x\n",
3434                 s->pm.u32PPLVCvalue,s->pm.u32PPRVCvalue));
3435         CS_DBGOUT(CS_PM, 9, printk("u32FMLVCvalue: 0x%x u32FMRVCvalue: 0x%x\n",
3436                 s->pm.u32FMLVCvalue,s->pm.u32FMRVCvalue));
3437         CS_DBGOUT(CS_PM, 9, printk("u32GPIORvalue: 0x%x u32JSCTLvalue: 0x%x\n",
3438                 s->pm.u32GPIORvalue,s->pm.u32JSCTLvalue));
3439         CS_DBGOUT(CS_PM, 9, printk("u32SSCR: 0x%x u32SRCSA: 0x%x\n",
3440                 s->pm.u32SSCR,s->pm.u32SRCSA));
3441         CS_DBGOUT(CS_PM, 9, printk("u32DacASR: 0x%x u32AdcASR: 0x%x\n",
3442                 s->pm.u32DacASR,s->pm.u32AdcASR));
3443         CS_DBGOUT(CS_PM, 9, printk("u32DacSR: 0x%x u32AdcSR: 0x%x\n",
3444                 s->pm.u32DacSR,s->pm.u32AdcSR));
3445         CS_DBGOUT(CS_PM, 9, printk("u32MIDCR_Save: 0x%x\n",
3446                 s->pm.u32MIDCR_Save));
3447         CS_DBGOUT(CS_PM, 9, printk("u32AC97_powerdown: 0x%x _general_purpose 0x%x\n",
3448                 s->pm.u32AC97_powerdown,s->pm.u32AC97_general_purpose));
3449         CS_DBGOUT(CS_PM, 9, printk("u32AC97_master_volume: 0x%x\n",
3450                 s->pm.u32AC97_master_volume));
3451         CS_DBGOUT(CS_PM, 9, printk("u32AC97_headphone_volume: 0x%x\n",
3452                 s->pm.u32AC97_headphone_volume));
3453         CS_DBGOUT(CS_PM, 9, printk("u32AC97_master_volume_mono: 0x%x\n",
3454                 s->pm.u32AC97_master_volume_mono));
3455         CS_DBGOUT(CS_PM, 9, printk("u32AC97_pcm_out_volume: 0x%x\n",
3456                 s->pm.u32AC97_pcm_out_volume));
3457         CS_DBGOUT(CS_PM, 9, printk("dmabuf_swptr_play: 0x%x dmabuf_count_play: %d\n",
3458                 s->pm.dmabuf_swptr_play,s->pm.dmabuf_count_play));
3459         CS_DBGOUT(CS_PM, 9, printk("dmabuf_swptr_capture: 0x%x dmabuf_count_capture: %d\n",
3460                 s->pm.dmabuf_swptr_capture,s->pm.dmabuf_count_capture));
3461
3462 }
3463
3464 /****************************************************************************
3465 *
3466 *  Suspend - save the ac97 regs, mute the outputs and power down the part.  
3467 *
3468 ****************************************************************************/
3469 static void cs46xx_ac97_suspend(struct cs_card *card)
3470 {
3471         int Count,i;
3472         struct ac97_codec *dev=card->ac97_codec[0];
3473         unsigned int tmp;
3474
3475         CS_DBGOUT(CS_PM, 9, printk("cs46xx: cs46xx_ac97_suspend()+\n"));
3476
3477         if(card->states[1])
3478         {
3479                 stop_dac(card->states[1]);
3480                 resync_dma_ptrs(card->states[1]);
3481         }
3482         if(card->states[0])
3483         {
3484                 stop_adc(card->states[0]);
3485                 resync_dma_ptrs(card->states[0]);
3486         }
3487
3488         for(Count = 0x2, i=0; (Count <= CS46XX_AC97_HIGHESTREGTORESTORE)
3489                         && (i < CS46XX_AC97_NUMBER_RESTORE_REGS); 
3490                 Count += 2, i++)
3491         {
3492                 card->pm.ac97[i] = cs_ac97_get(dev, BA0_AC97_RESET + Count);
3493         }
3494 /*
3495 * Save the ac97 volume registers as well as the current powerdown state.
3496 * Now, mute the all the outputs (master, headphone, and mono), as well
3497 * as the PCM volume, in preparation for powering down the entire part.
3498         card->pm.u32AC97_master_volume = (u32)cs_ac97_get( dev, 
3499                         (u8)BA0_AC97_MASTER_VOLUME); 
3500         card->pm.u32AC97_headphone_volume = (u32)cs_ac97_get(dev, 
3501                         (u8)BA0_AC97_HEADPHONE_VOLUME); 
3502         card->pm.u32AC97_master_volume_mono = (u32)cs_ac97_get(dev, 
3503                         (u8)BA0_AC97_MASTER_VOLUME_MONO); 
3504         card->pm.u32AC97_pcm_out_volume = (u32)cs_ac97_get(dev, 
3505                         (u8)BA0_AC97_PCM_OUT_VOLUME);
3506 */ 
3507 /*
3508 * mute the outputs
3509 */
3510         cs_ac97_set(dev, (u8)BA0_AC97_MASTER_VOLUME, 0x8000);
3511         cs_ac97_set(dev, (u8)BA0_AC97_HEADPHONE_VOLUME, 0x8000);
3512         cs_ac97_set(dev, (u8)BA0_AC97_MASTER_VOLUME_MONO, 0x8000);
3513         cs_ac97_set(dev, (u8)BA0_AC97_PCM_OUT_VOLUME, 0x8000);
3514
3515 /*
3516 * save the registers that cause pops
3517 */
3518         card->pm.u32AC97_powerdown = (u32)cs_ac97_get(dev, (u8)AC97_POWER_CONTROL); 
3519         card->pm.u32AC97_general_purpose = (u32)cs_ac97_get(dev, (u8)BA0_AC97_GENERAL_PURPOSE); 
3520 /*
3521 * And power down everything on the AC97 codec.
3522 * well, for now, only power down the DAC/ADC and MIXER VREFON components. 
3523 * trouble with removing VREF.
3524 */
3525         if( (tmp = cs461x_powerdown(card, CS_POWER_DAC | CS_POWER_ADC |
3526                         CS_POWER_MIXVON, CS_TRUE )) )
3527         {
3528                 CS_DBGOUT(CS_ERROR | CS_INIT, 1, printk(KERN_INFO 
3529                         "cs46xx: cs46xx_ac97_suspend() failure (0x%x)\n",tmp) );
3530         }
3531
3532         CS_DBGOUT(CS_PM, 9, printk("cs46xx: cs46xx_ac97_suspend()-\n"));
3533 }
3534
3535 /****************************************************************************
3536 *
3537 *  Resume - power up the part and restore its registers..  
3538 *
3539 ****************************************************************************/
3540 static void cs46xx_ac97_resume(struct cs_card *card)
3541 {
3542         int Count,i;
3543         struct ac97_codec *dev=card->ac97_codec[0];
3544
3545         CS_DBGOUT(CS_PM, 9, printk("cs46xx: cs46xx_ac97_resume()+\n"));
3546
3547 /*
3548 * First, we restore the state of the general purpose register.  This
3549 * contains the mic select (mic1 or mic2) and if we restore this after
3550 * we restore the mic volume/boost state and mic2 was selected at
3551 * suspend time, we will end up with a brief period of time where mic1
3552 * is selected with the volume/boost settings for mic2, causing
3553 * acoustic feedback.  So we restore the general purpose register
3554 * first, thereby getting the correct mic selected before we restore
3555 * the mic volume/boost.
3556 */
3557         cs_ac97_set(dev, (u8)BA0_AC97_GENERAL_PURPOSE, 
3558                 (u16)card->pm.u32AC97_general_purpose);
3559 /*
3560 * Now, while the outputs are still muted, restore the state of power
3561 * on the AC97 part.
3562 */
3563         cs_ac97_set(dev, (u8)BA0_AC97_POWERDOWN, (u16)card->pm.u32AC97_powerdown);
3564         mdelay(5 * cs_laptop_wait);
3565 /*
3566 * Restore just the first set of registers, from register number
3567 * 0x02 to the register number that ulHighestRegToRestore specifies.
3568 */
3569         for(    Count = 0x2, i=0; 
3570                 (Count <= CS46XX_AC97_HIGHESTREGTORESTORE)
3571                         && (i < CS46XX_AC97_NUMBER_RESTORE_REGS); 
3572                 Count += 2, i++)
3573         {
3574                 cs_ac97_set(dev, (u8)(BA0_AC97_RESET + Count), (u16)card->pm.ac97[i]);
3575         }
3576
3577         /* Check if we have to init the amplifier */
3578         if(card->amp_init)
3579                 card->amp_init(card);
3580         
3581         CS_DBGOUT(CS_PM, 9, printk("cs46xx: cs46xx_ac97_resume()-\n"));
3582 }
3583
3584
3585 static int cs46xx_restart_part(struct cs_card *card)
3586 {
3587         struct dmabuf *dmabuf;
3588         CS_DBGOUT(CS_PM | CS_FUNCTION, 4, 
3589                 printk( "cs46xx: cs46xx_restart_part()+\n"));
3590         if(card->states[1])
3591         {
3592                 dmabuf = &card->states[1]->dmabuf;
3593                 dmabuf->ready = 0;
3594                 resync_dma_ptrs(card->states[1]);
3595                 cs_set_divisor(dmabuf);
3596                 if(__prog_dmabuf(card->states[1]))
3597                 {
3598                         CS_DBGOUT(CS_PM | CS_ERROR, 1, 
3599                                 printk("cs46xx: cs46xx_restart_part()- (-1) prog_dmabuf() dac error\n"));
3600                         return -1;
3601                 }
3602                 cs_set_dac_rate(card->states[1], dmabuf->rate);
3603         }
3604         if(card->states[0])
3605         {
3606                 dmabuf = &card->states[0]->dmabuf;
3607                 dmabuf->ready = 0;
3608                 resync_dma_ptrs(card->states[0]);
3609                 cs_set_divisor(dmabuf);
3610                 if(__prog_dmabuf(card->states[0]))
3611                 {
3612                         CS_DBGOUT(CS_PM | CS_ERROR, 1, 
3613                                 printk("cs46xx: cs46xx_restart_part()- (-1) prog_dmabuf() adc error\n"));
3614                         return -1;
3615                 }
3616                 cs_set_adc_rate(card->states[0], dmabuf->rate);
3617         }
3618         card->pm.flags |= CS46XX_PM_RESUMED;
3619         if(card->states[0])
3620                 start_adc(card->states[0]);
3621         if(card->states[1])
3622                 start_dac(card->states[1]);
3623
3624         card->pm.flags |= CS46XX_PM_IDLE;
3625         card->pm.flags &= ~(CS46XX_PM_SUSPENDING | CS46XX_PM_SUSPENDED 
3626                         | CS46XX_PM_RESUMING | CS46XX_PM_RESUMED);
3627         if(card->states[0])
3628                 wake_up(&card->states[0]->dmabuf.wait);
3629         if(card->states[1])
3630                 wake_up(&card->states[1]->dmabuf.wait);
3631
3632         CS_DBGOUT(CS_PM | CS_FUNCTION, 4, 
3633                 printk( "cs46xx: cs46xx_restart_part()-\n"));
3634         return 0;
3635 }
3636
3637
3638 static void cs461x_reset(struct cs_card *card);
3639 static void cs461x_proc_stop(struct cs_card *card);
3640 static int cs46xx_suspend(struct cs_card *card, pm_message_t state)
3641 {
3642         unsigned int tmp;
3643         CS_DBGOUT(CS_PM | CS_FUNCTION, 4, 
3644                 printk("cs46xx: cs46xx_suspend()+ flags=0x%x s=%p\n",
3645                         (unsigned)card->pm.flags,card));
3646 /*
3647 * check the current state, only suspend if IDLE
3648 */
3649         if(!(card->pm.flags & CS46XX_PM_IDLE))
3650         {
3651                 CS_DBGOUT(CS_PM | CS_ERROR, 2, 
3652                         printk("cs46xx: cs46xx_suspend() unable to suspend, not IDLE\n"));
3653                 return 1;
3654         }
3655         card->pm.flags &= ~CS46XX_PM_IDLE;
3656         card->pm.flags |= CS46XX_PM_SUSPENDING;
3657
3658         card->active_ctrl(card,1);
3659         
3660         tmp = cs461x_peek(card, BA1_PFIE);
3661         tmp &= ~0x0000f03f;
3662         tmp |=  0x00000010;
3663         cs461x_poke(card, BA1_PFIE, tmp);       /* playback interrupt disable */
3664
3665         tmp = cs461x_peek(card, BA1_CIE);
3666         tmp &= ~0x0000003f;
3667         tmp |=  0x00000011;
3668         cs461x_poke(card, BA1_CIE, tmp);        /* capture interrupt disable */
3669
3670         /*
3671          *  Stop playback DMA.
3672          */
3673         tmp = cs461x_peek(card, BA1_PCTL);
3674         cs461x_poke(card, BA1_PCTL, tmp & 0x0000ffff);
3675
3676         /*
3677          *  Stop capture DMA.
3678          */
3679         tmp = cs461x_peek(card, BA1_CCTL);
3680         cs461x_poke(card, BA1_CCTL, tmp & 0xffff0000);
3681
3682         if(card->states[1])
3683         {
3684                 card->pm.dmabuf_swptr_play = card->states[1]->dmabuf.swptr;
3685                 card->pm.dmabuf_count_play = card->states[1]->dmabuf.count;
3686         }
3687         if(card->states[0])
3688         {
3689                 card->pm.dmabuf_swptr_capture = card->states[0]->dmabuf.swptr;
3690                 card->pm.dmabuf_count_capture = card->states[0]->dmabuf.count;
3691         }
3692
3693         cs46xx_ac97_suspend(card);
3694
3695         /*
3696          *  Reset the processor.
3697          */
3698         cs461x_reset(card);
3699
3700         cs461x_proc_stop(card);
3701
3702         /*
3703          *  Power down the DAC and ADC.  For now leave the other areas on.
3704          */
3705         cs_ac97_set(card->ac97_codec[0], AC97_POWER_CONTROL, 0x0300);
3706
3707         /*
3708          *  Power down the PLL.
3709          */
3710         cs461x_pokeBA0(card, BA0_CLKCR1, 0);
3711
3712         /*
3713          *  Turn off the Processor by turning off the software clock enable flag in 
3714          *  the clock control register.
3715          */
3716         tmp = cs461x_peekBA0(card, BA0_CLKCR1) & ~CLKCR1_SWCE;
3717         cs461x_pokeBA0(card, BA0_CLKCR1, tmp);
3718
3719         card->active_ctrl(card,-1);
3720
3721         card->pm.flags &= ~CS46XX_PM_SUSPENDING;
3722         card->pm.flags |= CS46XX_PM_SUSPENDED;
3723
3724         printpm(card);
3725
3726         CS_DBGOUT(CS_PM | CS_FUNCTION, 4, 
3727                 printk("cs46xx: cs46xx_suspend()- flags=0x%x\n",
3728                         (unsigned)card->pm.flags));
3729         return 0;
3730 }
3731
3732 static int cs46xx_resume(struct cs_card *card)
3733 {
3734         int i;
3735
3736         CS_DBGOUT(CS_PM | CS_FUNCTION, 4, 
3737                 printk( "cs46xx: cs46xx_resume()+ flags=0x%x\n",
3738                         (unsigned)card->pm.flags));
3739         if(!(card->pm.flags & CS46XX_PM_SUSPENDED))
3740         {
3741                 CS_DBGOUT(CS_PM | CS_ERROR, 2, 
3742                         printk("cs46xx: cs46xx_resume() unable to resume, not SUSPENDED\n"));
3743                 return 1;
3744         }
3745         card->pm.flags |= CS46XX_PM_RESUMING;
3746         card->pm.flags &= ~CS46XX_PM_SUSPENDED;
3747         printpm(card);
3748         card->active_ctrl(card, 1);
3749
3750         for(i=0;i<5;i++)
3751         {
3752                 if (cs_hardware_init(card) != 0)
3753                 {
3754                         CS_DBGOUT(CS_PM | CS_ERROR, 4, printk(
3755                                 "cs46xx: cs46xx_resume()- ERROR in cs_hardware_init()\n"));
3756                         mdelay(10 * cs_laptop_wait);
3757                         cs461x_reset(card);
3758                         continue;
3759                 }
3760                 break;
3761         }
3762         if(i>=4)
3763         {
3764                 CS_DBGOUT(CS_PM | CS_ERROR, 1, printk(
3765                         "cs46xx: cs46xx_resume()- cs_hardware_init() failed, retried %d times.\n",i));
3766                 return 0;
3767         }
3768
3769         if(cs46xx_restart_part(card))
3770         {
3771                 CS_DBGOUT(CS_PM | CS_ERROR, 4, printk(
3772                         "cs46xx: cs46xx_resume(): cs46xx_restart_part() returned error\n"));
3773         }
3774
3775         card->active_ctrl(card, -1);
3776
3777         CS_DBGOUT(CS_PM | CS_FUNCTION, 4, printk("cs46xx: cs46xx_resume()- flags=0x%x\n",
3778                 (unsigned)card->pm.flags));
3779         return 0;
3780 }
3781
3782 static /*const*/ struct file_operations cs461x_fops = {
3783         CS_OWNER        CS_THIS_MODULE
3784         .llseek         = no_llseek,
3785         .read           = cs_read,
3786         .write          = cs_write,
3787         .poll           = cs_poll,
3788         .ioctl          = cs_ioctl,
3789         .mmap           = cs_mmap,
3790         .open           = cs_open,
3791         .release        = cs_release,
3792 };
3793
3794 /* Write AC97 codec registers */
3795
3796
3797 static u16 _cs_ac97_get(struct ac97_codec *dev, u8 reg)
3798 {
3799         struct cs_card *card = dev->private_data;
3800         int count,loopcnt;
3801         unsigned int tmp;
3802         u16 ret;
3803         
3804         /*
3805          *  1. Write ACCAD = Command Address Register = 46Ch for AC97 register address
3806          *  2. Write ACCDA = Command Data Register = 470h    for data to write to AC97 
3807          *  3. Write ACCTL = Control Register = 460h for initiating the write
3808          *  4. Read ACCTL = 460h, DCV should be reset by now and 460h = 17h
3809          *  5. if DCV not cleared, break and return error
3810          *  6. Read ACSTS = Status Register = 464h, check VSTS bit
3811          */
3812
3813         cs461x_peekBA0(card, BA0_ACSDA);
3814
3815         /*
3816          *  Setup the AC97 control registers on the CS461x to send the
3817          *  appropriate command to the AC97 to perform the read.
3818          *  ACCAD = Command Address Register = 46Ch
3819          *  ACCDA = Command Data Register = 470h
3820          *  ACCTL = Control Register = 460h
3821          *  set DCV - will clear when process completed
3822          *  set CRW - Read command
3823          *  set VFRM - valid frame enabled
3824          *  set ESYN - ASYNC generation enabled
3825          *  set RSTN - ARST# inactive, AC97 codec not reset
3826          */
3827
3828         cs461x_pokeBA0(card, BA0_ACCAD, reg);
3829         cs461x_pokeBA0(card, BA0_ACCDA, 0);
3830         cs461x_pokeBA0(card, BA0_ACCTL, ACCTL_DCV | ACCTL_CRW |
3831                                              ACCTL_VFRM | ACCTL_ESYN |
3832                                              ACCTL_RSTN);
3833
3834
3835         /*
3836          *  Wait for the read to occur.
3837          */
3838         if(!(card->pm.flags & CS46XX_PM_IDLE))
3839                 loopcnt = 2000;
3840         else
3841                 loopcnt = 500 * cs_laptop_wait;
3842         loopcnt *= cs_laptop_wait;
3843         for (count = 0; count < loopcnt; count++) {
3844                 /*
3845                  *  First, we want to wait for a short time.
3846                  */
3847                 udelay(10 * cs_laptop_wait);
3848                 /*
3849                  *  Now, check to see if the read has completed.
3850                  *  ACCTL = 460h, DCV should be reset by now and 460h = 17h
3851                  */
3852                 if (!(cs461x_peekBA0(card, BA0_ACCTL) & ACCTL_DCV))
3853                         break;
3854         }
3855
3856         /*
3857          *  Make sure the read completed.
3858          */
3859         if (cs461x_peekBA0(card, BA0_ACCTL) & ACCTL_DCV) {
3860                 CS_DBGOUT(CS_ERROR, 1, printk(KERN_WARNING 
3861                         "cs46xx: AC'97 read problem (ACCTL_DCV), reg = 0x%x returning 0xffff\n", reg));
3862                 return 0xffff;
3863         }
3864
3865         /*
3866          *  Wait for the valid status bit to go active.
3867          */
3868
3869         if(!(card->pm.flags & CS46XX_PM_IDLE))
3870                 loopcnt = 2000;
3871         else
3872                 loopcnt = 1000;
3873         loopcnt *= cs_laptop_wait;
3874         for (count = 0; count < loopcnt; count++) {
3875                 /*
3876                  *  Read the AC97 status register.
3877                  *  ACSTS = Status Register = 464h
3878                  *  VSTS - Valid Status
3879                  */
3880                 if (cs461x_peekBA0(card, BA0_ACSTS) & ACSTS_VSTS)
3881                         break;
3882                 udelay(10 * cs_laptop_wait);
3883         }
3884         
3885         /*
3886          *  Make sure we got valid status.
3887          */
3888         if (!( (tmp=cs461x_peekBA0(card, BA0_ACSTS)) & ACSTS_VSTS)) {
3889                 CS_DBGOUT(CS_ERROR, 2, printk(KERN_WARNING 
3890                         "cs46xx: AC'97 read problem (ACSTS_VSTS), reg = 0x%x val=0x%x 0xffff \n", 
3891                                 reg, tmp));
3892                 return 0xffff;
3893         }
3894
3895         /*
3896          *  Read the data returned from the AC97 register.
3897          *  ACSDA = Status Data Register = 474h
3898          */
3899         CS_DBGOUT(CS_FUNCTION, 9, printk(KERN_INFO
3900                 "cs46xx: cs_ac97_get() reg = 0x%x, val = 0x%x, BA0_ACCAD = 0x%x\n", 
3901                         reg, cs461x_peekBA0(card, BA0_ACSDA),
3902                         cs461x_peekBA0(card, BA0_ACCAD)));
3903         ret = cs461x_peekBA0(card, BA0_ACSDA);
3904         return ret;
3905 }
3906
3907 static u16 cs_ac97_get(struct ac97_codec *dev, u8 reg)
3908 {
3909         u16 ret;
3910         struct cs_card *card = dev->private_data;
3911         
3912         spin_lock(&card->ac97_lock);
3913         ret = _cs_ac97_get(dev, reg);
3914         spin_unlock(&card->ac97_lock);
3915         return ret;
3916 }
3917
3918 static void cs_ac97_set(struct ac97_codec *dev, u8 reg, u16 val)
3919 {
3920         struct cs_card *card = dev->private_data;
3921         int count;
3922         int val2 = 0;
3923         
3924         spin_lock(&card->ac97_lock);
3925         
3926         if(reg == AC97_CD_VOL)
3927         {
3928                 val2 = _cs_ac97_get(dev, AC97_CD_VOL);
3929         }
3930         
3931         
3932         /*
3933          *  1. Write ACCAD = Command Address Register = 46Ch for AC97 register address
3934          *  2. Write ACCDA = Command Data Register = 470h    for data to write to AC97
3935          *  3. Write ACCTL = Control Register = 460h for initiating the write
3936          *  4. Read ACCTL = 460h, DCV should be reset by now and 460h = 07h
3937          *  5. if DCV not cleared, break and return error
3938          */
3939
3940         /*
3941          *  Setup the AC97 control registers on the CS461x to send the
3942          *  appropriate command to the AC97 to perform the read.
3943          *  ACCAD = Command Address Register = 46Ch
3944          *  ACCDA = Command Data Register = 470h
3945          *  ACCTL = Control Register = 460h
3946          *  set DCV - will clear when process completed
3947          *  reset CRW - Write command
3948          *  set VFRM - valid frame enabled
3949          *  set ESYN - ASYNC generation enabled
3950          *  set RSTN - ARST# inactive, AC97 codec not reset
3951          */
3952         cs461x_pokeBA0(card, BA0_ACCAD, reg);
3953         cs461x_pokeBA0(card, BA0_ACCDA, val);
3954         cs461x_peekBA0(card, BA0_ACCTL);
3955         cs461x_pokeBA0(card, BA0_ACCTL, 0 | ACCTL_VFRM | ACCTL_ESYN | ACCTL_RSTN);
3956         cs461x_pokeBA0(card, BA0_ACCTL, ACCTL_DCV | ACCTL_VFRM |
3957                                              ACCTL_ESYN | ACCTL_RSTN);
3958         for (count = 0; count < 1000; count++) {
3959                 /*
3960                  *  First, we want to wait for a short time.
3961                  */
3962                 udelay(10 * cs_laptop_wait);
3963                 /*
3964                  *  Now, check to see if the write has completed.
3965                  *  ACCTL = 460h, DCV should be reset by now and 460h = 07h
3966                  */
3967                 if (!(cs461x_peekBA0(card, BA0_ACCTL) & ACCTL_DCV))
3968                         break;
3969         }
3970         /*
3971          *  Make sure the write completed.
3972          */
3973         if (cs461x_peekBA0(card, BA0_ACCTL) & ACCTL_DCV)
3974         {
3975                 CS_DBGOUT(CS_ERROR, 1, printk(KERN_WARNING 
3976                         "cs46xx: AC'97 write problem, reg = 0x%x, val = 0x%x\n", reg, val));
3977         }
3978
3979         spin_unlock(&card->ac97_lock);
3980
3981         /*
3982          *      Adjust power if the mixer is selected/deselected according
3983          *      to the CD.
3984          *
3985          *      IF the CD is a valid input source (mixer or direct) AND
3986          *              the CD is not muted THEN power is needed
3987          *
3988          *      We do two things. When record select changes the input to
3989          *      add/remove the CD we adjust the power count if the CD is
3990          *      unmuted.
3991          *
3992          *      When the CD mute changes we adjust the power level if the
3993          *      CD was a valid input.
3994          *
3995          *      We also check for CD volume != 0, as the CD mute isn't
3996          *      normally tweaked from userspace.
3997          */
3998          
3999         /* CD mute change ? */
4000         
4001         if(reg==AC97_CD_VOL)
4002         {
4003                 /* Mute bit change ? */
4004                 if((val2^val)&0x8000 || ((val2 == 0x1f1f || val == 0x1f1f) && val2 != val))
4005                 {
4006                         /* This is a hack but its cleaner than the alternatives.
4007                            Right now card->ac97_codec[0] might be NULL as we are
4008                            still doing codec setup. This does an early assignment
4009                            to avoid the problem if it occurs */
4010                            
4011                         if(card->ac97_codec[0]==NULL)
4012                                 card->ac97_codec[0]=dev;
4013                                 
4014                         /* Mute on */
4015                         if(val&0x8000 || val == 0x1f1f)
4016                                 card->amplifier_ctrl(card, -1);
4017                         else /* Mute off power on */
4018                         {
4019                                 if(card->amp_init)
4020                                         card->amp_init(card);
4021                                 card->amplifier_ctrl(card, 1);
4022                         }
4023                 }
4024         }
4025 }
4026
4027
4028 /* OSS /dev/mixer file operation methods */
4029
4030 static int cs_open_mixdev(struct inode *inode, struct file *file)
4031 {
4032         int i=0;
4033         unsigned int minor = iminor(inode);
4034         struct cs_card *card=NULL;
4035         struct list_head *entry;
4036         unsigned int tmp;
4037
4038         CS_DBGOUT(CS_FUNCTION | CS_OPEN, 4,
4039                   printk(KERN_INFO "cs46xx: cs_open_mixdev()+\n"));
4040
4041         list_for_each(entry, &cs46xx_devs)
4042         {
4043                 card = list_entry(entry, struct cs_card, list);
4044                 for (i = 0; i < NR_AC97; i++)
4045                         if (card->ac97_codec[i] != NULL &&
4046                             card->ac97_codec[i]->dev_mixer == minor)
4047                                 goto match;
4048         }
4049         if (!card)
4050         {
4051                 CS_DBGOUT(CS_FUNCTION | CS_OPEN | CS_ERROR, 2,
4052                         printk(KERN_INFO "cs46xx: cs46xx_open_mixdev()- -ENODEV\n"));
4053                 return -ENODEV;
4054         }
4055  match:
4056         if(!card->ac97_codec[i])
4057                 return -ENODEV;
4058         file->private_data = card->ac97_codec[i];
4059
4060         card->active_ctrl(card,1);
4061         if(!CS_IN_USE(&card->mixer_use_cnt))
4062         {
4063                 if( (tmp = cs46xx_powerup(card, CS_POWER_MIXVON )) )
4064                 {
4065                         CS_DBGOUT(CS_ERROR | CS_INIT, 1, printk(KERN_INFO 
4066                                 "cs46xx: cs_open_mixdev() powerup failure (0x%x)\n",tmp) );
4067                         return -EIO;
4068                 }
4069         }
4070         card->amplifier_ctrl(card, 1);
4071         CS_INC_USE_COUNT(&card->mixer_use_cnt);
4072         CS_DBGOUT(CS_FUNCTION | CS_OPEN, 4,
4073                   printk(KERN_INFO "cs46xx: cs_open_mixdev()- 0\n"));
4074         return nonseekable_open(inode, file);
4075 }
4076
4077 static int cs_release_mixdev(struct inode *inode, struct file *file)
4078 {
4079         unsigned int minor = iminor(inode);
4080         struct cs_card *card=NULL;
4081         struct list_head *entry;
4082         int i;
4083         unsigned int tmp;
4084
4085         CS_DBGOUT(CS_FUNCTION | CS_RELEASE, 4,
4086                   printk(KERN_INFO "cs46xx: cs_release_mixdev()+\n"));
4087         list_for_each(entry, &cs46xx_devs)
4088         {
4089                 card = list_entry(entry, struct cs_card, list);
4090                 for (i = 0; i < NR_AC97; i++)
4091                         if (card->ac97_codec[i] != NULL &&
4092                             card->ac97_codec[i]->dev_mixer == minor)
4093                                 goto match;
4094         }
4095         if (!card)
4096         {
4097                 CS_DBGOUT(CS_FUNCTION | CS_OPEN | CS_ERROR, 2,
4098                         printk(KERN_INFO "cs46xx: cs46xx_open_mixdev()- -ENODEV\n"));
4099                 return -ENODEV;
4100         }
4101 match:
4102         if(!CS_DEC_AND_TEST(&card->mixer_use_cnt))
4103         {
4104                 CS_DBGOUT(CS_FUNCTION | CS_RELEASE, 4,
4105                           printk(KERN_INFO "cs46xx: cs_release_mixdev()- no powerdown, usecnt>0\n"));
4106                 card->active_ctrl(card, -1);
4107                 card->amplifier_ctrl(card, -1);
4108                 return 0;
4109         }
4110 /*
4111 * ok, no outstanding mixer opens, so powerdown.
4112 */
4113         if( (tmp = cs461x_powerdown(card, CS_POWER_MIXVON, CS_FALSE )) )
4114         {
4115                 CS_DBGOUT(CS_ERROR | CS_INIT, 1, printk(KERN_INFO 
4116                         "cs46xx: cs_release_mixdev() powerdown MIXVON failure (0x%x)\n",tmp) );
4117                 card->active_ctrl(card, -1);
4118                 card->amplifier_ctrl(card, -1);
4119                 return -EIO;
4120         }
4121         card->active_ctrl(card, -1);
4122         card->amplifier_ctrl(card, -1);
4123         CS_DBGOUT(CS_FUNCTION | CS_RELEASE, 4,
4124                   printk(KERN_INFO "cs46xx: cs_release_mixdev()- 0\n"));
4125         return 0;
4126 }
4127
4128 static int cs_ioctl_mixdev(struct inode *inode, struct file *file, unsigned int cmd,
4129                                 unsigned long arg)
4130 {
4131         struct ac97_codec *codec = (struct ac97_codec *)file->private_data;
4132         struct cs_card *card=NULL;
4133         struct list_head *entry;
4134         unsigned long __user *p = (long __user *)arg;
4135
4136 #if CSDEBUG_INTERFACE
4137         int val;
4138
4139         if(     (cmd == SOUND_MIXER_CS_GETDBGMASK) || 
4140                 (cmd == SOUND_MIXER_CS_SETDBGMASK) ||
4141                 (cmd == SOUND_MIXER_CS_GETDBGLEVEL) ||
4142                 (cmd == SOUND_MIXER_CS_SETDBGLEVEL) ||
4143                 (cmd == SOUND_MIXER_CS_APM))
4144         {
4145             switch(cmd)
4146             {
4147
4148                 case SOUND_MIXER_CS_GETDBGMASK:
4149                         return put_user(cs_debugmask, p);
4150                 
4151                 case SOUND_MIXER_CS_GETDBGLEVEL:
4152                         return put_user(cs_debuglevel, p);
4153
4154                 case SOUND_MIXER_CS_SETDBGMASK:
4155                         if (get_user(val, p))
4156                                 return -EFAULT;
4157                         cs_debugmask = val;
4158                         return 0;
4159
4160                 case SOUND_MIXER_CS_SETDBGLEVEL:
4161                         if (get_user(val, p))
4162                                 return -EFAULT;
4163                         cs_debuglevel = val;
4164                         return 0;
4165
4166                 case SOUND_MIXER_CS_APM:
4167                         if (get_user(val, p))
4168                                 return -EFAULT;
4169                         if(val == CS_IOCTL_CMD_SUSPEND) 
4170                         {
4171                                 list_for_each(entry, &cs46xx_devs)
4172                                 {
4173                                         card = list_entry(entry, struct cs_card, list);
4174                                         cs46xx_suspend(card, PMSG_ON);
4175                                 }
4176
4177                         }
4178                         else if(val == CS_IOCTL_CMD_RESUME)
4179                         {
4180                                 list_for_each(entry, &cs46xx_devs)
4181                                 {
4182                                         card = list_entry(entry, struct cs_card, list);
4183                                         cs46xx_resume(card);
4184                                 }
4185                         }
4186                         else
4187                         {
4188                                 CS_DBGOUT(CS_ERROR, 1, printk(KERN_INFO
4189                                     "cs46xx: mixer_ioctl(): invalid APM cmd (%d)\n",
4190                                         val));
4191                         }
4192                         return 0;
4193
4194                 default:
4195                         CS_DBGOUT(CS_ERROR, 1, printk(KERN_INFO 
4196                                 "cs46xx: mixer_ioctl(): ERROR unknown debug cmd\n") );
4197                         return 0;
4198             }
4199         }
4200 #endif
4201         return codec->mixer_ioctl(codec, cmd, arg);
4202 }
4203
4204 static /*const*/ struct file_operations cs_mixer_fops = {
4205         CS_OWNER        CS_THIS_MODULE
4206         .llseek         = no_llseek,
4207         .ioctl          = cs_ioctl_mixdev,
4208         .open           = cs_open_mixdev,
4209         .release        = cs_release_mixdev,
4210 };
4211
4212 /* AC97 codec initialisation. */
4213 static int __init cs_ac97_init(struct cs_card *card)
4214 {
4215         int num_ac97 = 0;
4216         int ready_2nd = 0;
4217         struct ac97_codec *codec;
4218         u16 eid;
4219
4220         CS_DBGOUT(CS_FUNCTION | CS_INIT, 2, printk(KERN_INFO 
4221                 "cs46xx: cs_ac97_init()+\n") );
4222
4223         for (num_ac97 = 0; num_ac97 < NR_AC97; num_ac97++) {
4224                 if ((codec = ac97_alloc_codec()) == NULL)
4225                         return -ENOMEM;
4226
4227                 /* initialize some basic codec information, other fields will be filled
4228                    in ac97_probe_codec */
4229                 codec->private_data = card;
4230                 codec->id = num_ac97;
4231
4232                 codec->codec_read = cs_ac97_get;
4233                 codec->codec_write = cs_ac97_set;
4234         
4235                 if (ac97_probe_codec(codec) == 0)
4236                 {
4237                         CS_DBGOUT(CS_FUNCTION | CS_INIT, 2, printk(KERN_INFO 
4238                                 "cs46xx: cs_ac97_init()- codec number %d not found\n",
4239                                         num_ac97) );
4240                         card->ac97_codec[num_ac97] = NULL;
4241                         break;
4242                 }
4243                 CS_DBGOUT(CS_FUNCTION | CS_INIT, 2, printk(KERN_INFO 
4244                         "cs46xx: cs_ac97_init() found codec %d\n",num_ac97) );
4245
4246                 eid = cs_ac97_get(codec, AC97_EXTENDED_ID);
4247                 
4248                 if(eid==0xFFFF)
4249                 {
4250                         printk(KERN_WARNING "cs46xx: codec %d not present\n",num_ac97);
4251                         ac97_release_codec(codec);
4252                         break;
4253                 }
4254                 
4255                 card->ac97_features = eid;
4256                         
4257                 if ((codec->dev_mixer = register_sound_mixer(&cs_mixer_fops, -1)) < 0) {
4258                         printk(KERN_ERR "cs46xx: couldn't register mixer!\n");
4259                         ac97_release_codec(codec);
4260                         break;
4261                 }
4262                 card->ac97_codec[num_ac97] = codec;
4263
4264                 CS_DBGOUT(CS_FUNCTION | CS_INIT, 2, printk(KERN_INFO 
4265                         "cs46xx: cs_ac97_init() ac97_codec[%d] set to %p\n",
4266                                 (unsigned int)num_ac97,
4267                                 codec));
4268                 /* if there is no secondary codec at all, don't probe any more */
4269                 if (!ready_2nd)
4270                 {
4271                         num_ac97 += 1;
4272                         break;
4273                 }
4274         }
4275         CS_DBGOUT(CS_FUNCTION | CS_INIT, 2, printk(KERN_INFO 
4276                 "cs46xx: cs_ac97_init()- %d\n", (unsigned int)num_ac97));
4277         return num_ac97;
4278 }
4279
4280 /*
4281  * load the static image into the DSP
4282  */
4283 #include "cs461x_image.h"
4284 static void cs461x_download_image(struct cs_card *card)
4285 {
4286     unsigned i, j, temp1, temp2, offset, count;
4287     unsigned char __iomem *pBA1 = ioremap(card->ba1_addr, 0x40000);
4288     for( i=0; i < CLEAR__COUNT; i++)
4289     {
4290         offset = ClrStat[i].BA1__DestByteOffset;
4291         count  = ClrStat[i].BA1__SourceSize;
4292         for(  temp1 = offset; temp1<(offset+count); temp1+=4 )
4293               writel(0, pBA1+temp1);
4294     }
4295
4296     for(i=0; i<FILL__COUNT; i++)
4297     {
4298         temp2 = FillStat[i].Offset;
4299         for(j=0; j<(FillStat[i].Size)/4; j++)
4300         {
4301             temp1 = (FillStat[i]).pFill[j];
4302             writel(temp1, pBA1+temp2+j*4);
4303         }
4304     }
4305     iounmap(pBA1);
4306 }
4307
4308
4309 /*
4310  *  Chip reset
4311  */
4312
4313 static void cs461x_reset(struct cs_card *card)
4314 {
4315         int idx;
4316
4317         /*
4318          *  Write the reset bit of the SP control register.
4319          */
4320         cs461x_poke(card, BA1_SPCR, SPCR_RSTSP);
4321
4322         /*
4323          *  Write the control register.
4324          */
4325         cs461x_poke(card, BA1_SPCR, SPCR_DRQEN);
4326
4327         /*
4328          *  Clear the trap registers.
4329          */
4330         for (idx = 0; idx < 8; idx++) {
4331                 cs461x_poke(card, BA1_DREG, DREG_REGID_TRAP_SELECT + idx);
4332                 cs461x_poke(card, BA1_TWPR, 0xFFFF);
4333         }
4334         cs461x_poke(card, BA1_DREG, 0);
4335
4336         /*
4337          *  Set the frame timer to reflect the number of cycles per frame.
4338          */
4339         cs461x_poke(card, BA1_FRMT, 0xadf);
4340 }
4341
4342 static void cs461x_clear_serial_FIFOs(struct cs_card *card, int type)
4343 {
4344         int idx, loop, startfifo=0, endfifo=0, powerdown1 = 0;
4345         unsigned int tmp;
4346
4347         /*
4348          *  See if the devices are powered down.  If so, we must power them up first
4349          *  or they will not respond.
4350          */
4351         if (!((tmp = cs461x_peekBA0(card, BA0_CLKCR1)) & CLKCR1_SWCE)) {
4352                 cs461x_pokeBA0(card, BA0_CLKCR1, tmp | CLKCR1_SWCE);
4353                 powerdown1 = 1;
4354         }
4355
4356         /*
4357          *  We want to clear out the serial port FIFOs so we don't end up playing
4358          *  whatever random garbage happens to be in them.  We fill the sample FIFOS
4359          *  with zero (silence).
4360          */
4361         cs461x_pokeBA0(card, BA0_SERBWP, 0);
4362
4363         /*
4364         * Check for which FIFO locations to clear, if we are currently
4365         * playing or capturing then we don't want to put in 128 bytes of
4366         * "noise".
4367          */
4368         if(type & CS_TYPE_DAC)
4369         {
4370                 startfifo = 128;
4371                 endfifo = 256;
4372         }
4373         if(type & CS_TYPE_ADC)
4374         {
4375                 startfifo = 0;
4376                 if(!endfifo)
4377                         endfifo = 128;
4378         }
4379         /*
4380          *  Fill sample FIFO locations (256 locations total).
4381          */
4382         for (idx = startfifo; idx < endfifo; idx++) {
4383                 /*
4384                  *  Make sure the previous FIFO write operation has completed.
4385                  */
4386                 for (loop = 0; loop < 5; loop++) {
4387                         udelay(50);
4388                         if (!(cs461x_peekBA0(card, BA0_SERBST) & SERBST_WBSY))
4389                                 break;
4390                 }
4391                 if (cs461x_peekBA0(card, BA0_SERBST) & SERBST_WBSY) {
4392                         if (powerdown1)
4393                                 cs461x_pokeBA0(card, BA0_CLKCR1, tmp);
4394                 }
4395                 /*
4396                  *  Write the serial port FIFO index.
4397                  */
4398                 cs461x_pokeBA0(card, BA0_SERBAD, idx);
4399                 /*
4400                  *  Tell the serial port to load the new value into the FIFO location.
4401                  */
4402                 cs461x_pokeBA0(card, BA0_SERBCM, SERBCM_WRC);
4403         }
4404         /*
4405          *  Now, if we powered up the devices, then power them back down again.
4406          *  This is kinda ugly, but should never happen.
4407          */
4408         if (powerdown1)
4409                 cs461x_pokeBA0(card, BA0_CLKCR1, tmp);
4410 }
4411
4412
4413 static int cs461x_powerdown(struct cs_card *card, unsigned int type, int suspendflag)
4414 {
4415         int count;
4416         unsigned int tmp=0,muted=0;
4417
4418         CS_DBGOUT(CS_FUNCTION, 4, printk(KERN_INFO 
4419                 "cs46xx: cs461x_powerdown()+ type=0x%x\n",type));
4420         if(!cs_powerdown && !suspendflag)
4421         {
4422                 CS_DBGOUT(CS_FUNCTION, 8, printk(KERN_INFO 
4423                         "cs46xx: cs461x_powerdown() DISABLED exiting\n"));
4424                 return 0;
4425         }
4426         tmp = cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL);
4427         CS_DBGOUT(CS_FUNCTION, 8, printk(KERN_INFO 
4428                 "cs46xx: cs461x_powerdown() powerdown reg=0x%x\n",tmp));
4429 /*
4430 * if powering down only the VREF, and not powering down the DAC/ADC,
4431 * then do not power down the VREF, UNLESS both the DAC and ADC are not
4432 * currently powered down.  If powering down DAC and ADC, then
4433 * it is possible to power down the VREF (ON).
4434 */
4435         if (    ((type & CS_POWER_MIXVON) && 
4436                  (!(type & CS_POWER_ADC) || (!(type & CS_POWER_DAC))) )
4437               && 
4438                 ((tmp & CS_AC97_POWER_CONTROL_ADC_ON) ||
4439                  (tmp & CS_AC97_POWER_CONTROL_DAC_ON) ) )
4440         {
4441                 CS_DBGOUT(CS_FUNCTION, 8, printk(KERN_INFO 
4442                         "cs46xx: cs461x_powerdown()- 0  unable to powerdown. tmp=0x%x\n",tmp));
4443                 return 0;
4444         }
4445 /*
4446 * for now, always keep power to the mixer block.
4447 * not sure why it's a problem but it seems to be if we power off.
4448 */
4449         type &= ~CS_POWER_MIXVON;
4450         type &= ~CS_POWER_MIXVOFF;
4451
4452         /*
4453          *  Power down indicated areas.
4454          */
4455         if(type & CS_POWER_MIXVOFF)
4456         {
4457
4458                 CS_DBGOUT(CS_FUNCTION, 4, 
4459                         printk(KERN_INFO "cs46xx: cs461x_powerdown()+ MIXVOFF\n"));
4460                 /*
4461                  *  Power down the MIXER (VREF ON) on the AC97 card.  
4462                  */
4463                 tmp = cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL);
4464                 if (tmp & CS_AC97_POWER_CONTROL_MIXVOFF_ON)
4465                 {
4466                         if(!muted)
4467                         {
4468                                 cs_mute(card, CS_TRUE);
4469                                 muted=1;
4470                         }
4471                         tmp |= CS_AC97_POWER_CONTROL_MIXVOFF;
4472                         cs_ac97_set(card->ac97_codec[0], AC97_POWER_CONTROL, tmp );
4473                         /*
4474                          *  Now, we wait until we sample a ready state.
4475                          */
4476                         for (count = 0; count < 32; count++) {
4477                                 /*
4478                                  *  First, lets wait a short while to let things settle out a
4479                                  *  bit, and to prevent retrying the read too quickly.
4480                                  */
4481                                 udelay(500);
4482
4483                                 /*
4484                                  *  Read the current state of the power control register.
4485                                  */
4486                                 if (!(cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) & 
4487                                         CS_AC97_POWER_CONTROL_MIXVOFF_ON))
4488                                         break;
4489                         }
4490                         
4491                         /*
4492                          *  Check the status..
4493                          */
4494                         if (cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) & 
4495                                 CS_AC97_POWER_CONTROL_MIXVOFF_ON)
4496                         {
4497                                 CS_DBGOUT(CS_ERROR, 1, printk(KERN_WARNING 
4498                                         "cs46xx: powerdown MIXVOFF failed\n"));
4499                                 return 1;
4500                         }
4501                 }
4502         }
4503         if(type & CS_POWER_MIXVON)
4504         {
4505
4506                 CS_DBGOUT(CS_FUNCTION, 4, 
4507                         printk(KERN_INFO "cs46xx: cs461x_powerdown()+ MIXVON\n"));
4508                 /*
4509                  *  Power down the MIXER (VREF ON) on the AC97 card.  
4510                  */
4511                 tmp = cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL);
4512                 if (tmp & CS_AC97_POWER_CONTROL_MIXVON_ON)
4513                 {
4514                         if(!muted)
4515                         {
4516                                 cs_mute(card, CS_TRUE);
4517                                 muted=1;
4518                         }
4519                         tmp |= CS_AC97_POWER_CONTROL_MIXVON;
4520                         cs_ac97_set(card->ac97_codec[0], AC97_POWER_CONTROL, tmp );
4521                         /*
4522                          *  Now, we wait until we sample a ready state.
4523                          */
4524                         for (count = 0; count < 32; count++) {
4525                                 /*
4526                                  *  First, lets wait a short while to let things settle out a
4527                                  *  bit, and to prevent retrying the read too quickly.
4528                                  */
4529                                 udelay(500);
4530
4531                                 /*
4532                                  *  Read the current state of the power control register.
4533                                  */
4534                                 if (!(cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) & 
4535                                         CS_AC97_POWER_CONTROL_MIXVON_ON))
4536                                         break;
4537                         }
4538                         
4539                         /*
4540                          *  Check the status..
4541                          */
4542                         if (cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) & 
4543                                 CS_AC97_POWER_CONTROL_MIXVON_ON)
4544                         {
4545                                 CS_DBGOUT(CS_ERROR, 1, printk(KERN_WARNING 
4546                                         "cs46xx: powerdown MIXVON failed\n"));
4547                                 return 1;
4548                         }
4549                 }
4550         }
4551         if(type & CS_POWER_ADC)
4552         {
4553                 /*
4554                  *  Power down the ADC on the AC97 card.  
4555                  */
4556                 CS_DBGOUT(CS_FUNCTION, 4, printk(KERN_INFO "cs46xx: cs461x_powerdown()+ ADC\n"));
4557                 tmp = cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL);
4558                 if (tmp & CS_AC97_POWER_CONTROL_ADC_ON)
4559                 {
4560                         if(!muted)
4561                         {
4562                                 cs_mute(card, CS_TRUE);
4563                                 muted=1;
4564                         }
4565                         tmp |= CS_AC97_POWER_CONTROL_ADC;
4566                         cs_ac97_set(card->ac97_codec[0], AC97_POWER_CONTROL, tmp );
4567
4568                         /*
4569                          *  Now, we wait until we sample a ready state.
4570                          */
4571                         for (count = 0; count < 32; count++) {
4572                                 /*
4573                                  *  First, lets wait a short while to let things settle out a
4574                                  *  bit, and to prevent retrying the read too quickly.
4575                                  */
4576                                 udelay(500);
4577
4578                                 /*
4579                                  *  Read the current state of the power control register.
4580                                  */
4581                                 if (!(cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) & 
4582                                         CS_AC97_POWER_CONTROL_ADC_ON))
4583                                         break;
4584                         }
4585
4586                         /*
4587                          *  Check the status..
4588                          */
4589                         if (cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) & 
4590                                 CS_AC97_POWER_CONTROL_ADC_ON)
4591                         {
4592                                 CS_DBGOUT(CS_ERROR, 1, printk(KERN_WARNING 
4593                                         "cs46xx: powerdown ADC failed\n"));
4594                                 return 1;
4595                         }
4596                 }
4597         }
4598         if(type & CS_POWER_DAC)
4599         {
4600                 /*
4601                  *  Power down the DAC on the AC97 card.  
4602                  */
4603
4604                 CS_DBGOUT(CS_FUNCTION, 4, 
4605                         printk(KERN_INFO "cs46xx: cs461x_powerdown()+ DAC\n"));
4606                 tmp = cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL);
4607                 if (tmp & CS_AC97_POWER_CONTROL_DAC_ON)
4608                 {
4609                         if(!muted)
4610                         {
4611                                 cs_mute(card, CS_TRUE);
4612                                 muted=1;
4613                         }
4614                         tmp |= CS_AC97_POWER_CONTROL_DAC;
4615                         cs_ac97_set(card->ac97_codec[0], AC97_POWER_CONTROL, tmp );
4616                         /*
4617                          *  Now, we wait until we sample a ready state.
4618                          */
4619                         for (count = 0; count < 32; count++) {
4620                                 /*
4621                                  *  First, lets wait a short while to let things settle out a
4622                                  *  bit, and to prevent retrying the read too quickly.
4623                                  */
4624                                 udelay(500);
4625
4626                                 /*
4627                                  *  Read the current state of the power control register.
4628                                  */
4629                                 if (!(cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) & 
4630                                         CS_AC97_POWER_CONTROL_DAC_ON))
4631                                         break;
4632                         }
4633                         
4634                         /*
4635                          *  Check the status..
4636                          */
4637                         if (cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) & 
4638                                 CS_AC97_POWER_CONTROL_DAC_ON)
4639                         {
4640                                 CS_DBGOUT(CS_ERROR, 1, printk(KERN_WARNING 
4641                                         "cs46xx: powerdown DAC failed\n"));
4642                                 return 1;
4643                         }
4644                 }
4645         }
4646         tmp = cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL);
4647         if(muted)
4648                 cs_mute(card, CS_FALSE);
4649         CS_DBGOUT(CS_FUNCTION, 4, printk(KERN_INFO 
4650                 "cs46xx: cs461x_powerdown()- 0 tmp=0x%x\n",tmp));
4651         return 0;
4652 }
4653
4654 static int cs46xx_powerup(struct cs_card *card, unsigned int type)
4655 {
4656         int count;
4657         unsigned int tmp=0,muted=0;
4658
4659         CS_DBGOUT(CS_FUNCTION, 8, printk(KERN_INFO 
4660                 "cs46xx: cs46xx_powerup()+ type=0x%x\n",type));
4661         /*
4662         * check for VREF and powerup if need to.
4663         */
4664         if(type & CS_POWER_MIXVON)
4665                 type |= CS_POWER_MIXVOFF;
4666         if(type & (CS_POWER_DAC | CS_POWER_ADC))
4667                 type |= CS_POWER_MIXVON | CS_POWER_MIXVOFF;
4668
4669         /*
4670          *  Power up indicated areas.
4671          */
4672         if(type & CS_POWER_MIXVOFF)
4673         {
4674
4675                 CS_DBGOUT(CS_FUNCTION, 4, 
4676                         printk(KERN_INFO "cs46xx: cs46xx_powerup()+ MIXVOFF\n"));
4677                 /*
4678                  *  Power up the MIXER (VREF ON) on the AC97 card.  
4679                  */
4680                 tmp = cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL);
4681                 if (!(tmp & CS_AC97_POWER_CONTROL_MIXVOFF_ON))
4682                 {
4683                         if(!muted)
4684                         {
4685                                 cs_mute(card, CS_TRUE);
4686                                 muted=1;
4687                         }
4688                         tmp &= ~CS_AC97_POWER_CONTROL_MIXVOFF;
4689                         cs_ac97_set(card->ac97_codec[0], AC97_POWER_CONTROL, tmp );
4690                         /*
4691                          *  Now, we wait until we sample a ready state.
4692                          */
4693                         for (count = 0; count < 32; count++) {
4694                                 /*
4695                                  *  First, lets wait a short while to let things settle out a
4696                                  *  bit, and to prevent retrying the read too quickly.
4697                                  */
4698                                 udelay(500);
4699
4700                                 /*
4701                                  *  Read the current state of the power control register.
4702                                  */
4703                                 if (cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) & 
4704                                         CS_AC97_POWER_CONTROL_MIXVOFF_ON)
4705                                         break;
4706                         }
4707                         
4708                         /*
4709                          *  Check the status..
4710                          */
4711                         if (!(cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) & 
4712                                 CS_AC97_POWER_CONTROL_MIXVOFF_ON))
4713                         {
4714                                 CS_DBGOUT(CS_ERROR, 1, printk(KERN_WARNING 
4715                                         "cs46xx: powerup MIXVOFF failed\n"));
4716                                 return 1;
4717                         }
4718                 }
4719         }
4720         if(type & CS_POWER_MIXVON)
4721         {
4722
4723                 CS_DBGOUT(CS_FUNCTION, 4, 
4724                         printk(KERN_INFO "cs46xx: cs46xx_powerup()+ MIXVON\n"));
4725                 /*
4726                  *  Power up the MIXER (VREF ON) on the AC97 card.  
4727                  */
4728                 tmp = cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL);
4729                 if (!(tmp & CS_AC97_POWER_CONTROL_MIXVON_ON))
4730                 {
4731                         if(!muted)
4732                         {
4733                                 cs_mute(card, CS_TRUE);
4734                                 muted=1;
4735                         }
4736                         tmp &= ~CS_AC97_POWER_CONTROL_MIXVON;
4737                         cs_ac97_set(card->ac97_codec[0], AC97_POWER_CONTROL, tmp );
4738                         /*
4739                          *  Now, we wait until we sample a ready state.
4740                          */
4741                         for (count = 0; count < 32; count++) {
4742                                 /*
4743                                  *  First, lets wait a short while to let things settle out a
4744                                  *  bit, and to prevent retrying the read too quickly.
4745                                  */
4746                                 udelay(500);
4747
4748                                 /*
4749                                  *  Read the current state of the power control register.
4750                                  */
4751                                 if (cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) & 
4752                                         CS_AC97_POWER_CONTROL_MIXVON_ON)
4753                                         break;
4754                         }
4755                         
4756                         /*
4757                          *  Check the status..
4758                          */
4759                         if (!(cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) & 
4760                                 CS_AC97_POWER_CONTROL_MIXVON_ON))
4761                         {
4762                                 CS_DBGOUT(CS_ERROR, 1, printk(KERN_WARNING 
4763                                         "cs46xx: powerup MIXVON failed\n"));
4764                                 return 1;
4765                         }
4766                 }
4767         }
4768         if(type & CS_POWER_ADC)
4769         {
4770                 /*
4771                  *  Power up the ADC on the AC97 card.  
4772                  */
4773                 CS_DBGOUT(CS_FUNCTION, 4, printk(KERN_INFO "cs46xx: cs46xx_powerup()+ ADC\n"));
4774                 tmp = cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL);
4775                 if (!(tmp & CS_AC97_POWER_CONTROL_ADC_ON))
4776                 {
4777                         if(!muted)
4778                         {
4779                                 cs_mute(card, CS_TRUE);
4780                                 muted=1;
4781                         }
4782                         tmp &= ~CS_AC97_POWER_CONTROL_ADC;
4783                         cs_ac97_set(card->ac97_codec[0], AC97_POWER_CONTROL, tmp );
4784
4785                         /*
4786                          *  Now, we wait until we sample a ready state.
4787                          */
4788                         for (count = 0; count < 32; count++) {
4789                                 /*
4790                                  *  First, lets wait a short while to let things settle out a
4791                                  *  bit, and to prevent retrying the read too quickly.
4792                                  */
4793                                 udelay(500);
4794
4795                                 /*
4796                                  *  Read the current state of the power control register.
4797                                  */
4798                                 if (cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) & 
4799                                         CS_AC97_POWER_CONTROL_ADC_ON)
4800                                         break;
4801                         }
4802
4803                         /*
4804                          *  Check the status..
4805                          */
4806                         if (!(cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) & 
4807                                 CS_AC97_POWER_CONTROL_ADC_ON))
4808                         {
4809                                 CS_DBGOUT(CS_ERROR, 1, printk(KERN_WARNING 
4810                                         "cs46xx: powerup ADC failed\n"));
4811                                 return 1;
4812                         }
4813                 }
4814         }
4815         if(type & CS_POWER_DAC)
4816         {
4817                 /*
4818                  *  Power up the DAC on the AC97 card.  
4819                  */
4820
4821                 CS_DBGOUT(CS_FUNCTION, 4, 
4822                         printk(KERN_INFO "cs46xx: cs46xx_powerup()+ DAC\n"));
4823                 tmp = cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL);
4824                 if (!(tmp & CS_AC97_POWER_CONTROL_DAC_ON))
4825                 {
4826                         if(!muted)
4827                         {
4828                                 cs_mute(card, CS_TRUE);
4829                                 muted=1;
4830                         }
4831                         tmp &= ~CS_AC97_POWER_CONTROL_DAC;
4832                         cs_ac97_set(card->ac97_codec[0], AC97_POWER_CONTROL, tmp );
4833                         /*
4834                          *  Now, we wait until we sample a ready state.
4835                          */
4836                         for (count = 0; count < 32; count++) {
4837                                 /*
4838                                  *  First, lets wait a short while to let things settle out a
4839                                  *  bit, and to prevent retrying the read too quickly.
4840                                  */
4841                                 udelay(500);
4842
4843                                 /*
4844                                  *  Read the current state of the power control register.
4845                                  */
4846                                 if (cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) & 
4847                                         CS_AC97_POWER_CONTROL_DAC_ON)
4848                                         break;
4849                         }
4850                         
4851                         /*
4852                          *  Check the status..
4853                          */
4854                         if (!(cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) & 
4855                                 CS_AC97_POWER_CONTROL_DAC_ON))
4856                         {
4857                                 CS_DBGOUT(CS_ERROR, 1, printk(KERN_WARNING 
4858                                         "cs46xx: powerup DAC failed\n"));
4859                                 return 1;
4860                         }
4861                 }
4862         }
4863         tmp = cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL);
4864         if(muted)
4865                 cs_mute(card, CS_FALSE);
4866         CS_DBGOUT(CS_FUNCTION, 4, printk(KERN_INFO 
4867                 "cs46xx: cs46xx_powerup()- 0 tmp=0x%x\n",tmp));
4868         return 0;
4869 }
4870
4871
4872 static void cs461x_proc_start(struct cs_card *card)
4873 {
4874         int cnt;
4875
4876         /*
4877          *  Set the frame timer to reflect the number of cycles per frame.
4878          */
4879         cs461x_poke(card, BA1_FRMT, 0xadf);
4880         /*
4881          *  Turn on the run, run at frame, and DMA enable bits in the local copy of
4882          *  the SP control register.
4883          */
4884         cs461x_poke(card, BA1_SPCR, SPCR_RUN | SPCR_RUNFR | SPCR_DRQEN);
4885         /*
4886          *  Wait until the run at frame bit resets itself in the SP control
4887          *  register.
4888          */
4889         for (cnt = 0; cnt < 25; cnt++) {
4890                 udelay(50);
4891                 if (!(cs461x_peek(card, BA1_SPCR) & SPCR_RUNFR))
4892                         break;
4893         }
4894
4895         if (cs461x_peek(card, BA1_SPCR) & SPCR_RUNFR)
4896                 printk(KERN_WARNING "cs46xx: SPCR_RUNFR never reset\n");
4897 }
4898
4899 static void cs461x_proc_stop(struct cs_card *card)
4900 {
4901         /*
4902          *  Turn off the run, run at frame, and DMA enable bits in the local copy of
4903          *  the SP control register.
4904          */
4905         cs461x_poke(card, BA1_SPCR, 0);
4906 }
4907
4908 static int cs_hardware_init(struct cs_card *card)
4909 {
4910         unsigned long end_time;
4911         unsigned int tmp,count;
4912         
4913         CS_DBGOUT(CS_FUNCTION | CS_INIT, 2, printk(KERN_INFO 
4914                 "cs46xx: cs_hardware_init()+\n") );
4915         /* 
4916          *  First, blast the clock control register to zero so that the PLL starts
4917          *  out in a known state, and blast the master serial port control register
4918          *  to zero so that the serial ports also start out in a known state.
4919          */
4920         cs461x_pokeBA0(card, BA0_CLKCR1, 0);
4921         cs461x_pokeBA0(card, BA0_SERMC1, 0);
4922
4923         /*
4924          *  If we are in AC97 mode, then we must set the part to a host controlled
4925          *  AC-link.  Otherwise, we won't be able to bring up the link.
4926          */        
4927         cs461x_pokeBA0(card, BA0_SERACC, SERACC_HSP | SERACC_CODEC_TYPE_1_03);  /* 1.03 card */
4928         /* cs461x_pokeBA0(card, BA0_SERACC, SERACC_HSP | SERACC_CODEC_TYPE_2_0); */ /* 2.00 card */
4929
4930         /*
4931          *  Drive the ARST# pin low for a minimum of 1uS (as defined in the AC97
4932          *  spec) and then drive it high.  This is done for non AC97 modes since
4933          *  there might be logic external to the CS461x that uses the ARST# line
4934          *  for a reset.
4935          */
4936         cs461x_pokeBA0(card, BA0_ACCTL, 1);
4937         udelay(50);
4938         cs461x_pokeBA0(card, BA0_ACCTL, 0);
4939         udelay(50);
4940         cs461x_pokeBA0(card, BA0_ACCTL, ACCTL_RSTN);
4941
4942         /*
4943          *  The first thing we do here is to enable sync generation.  As soon
4944          *  as we start receiving bit clock, we'll start producing the SYNC
4945          *  signal.
4946          */
4947         cs461x_pokeBA0(card, BA0_ACCTL, ACCTL_ESYN | ACCTL_RSTN);
4948
4949         /*
4950          *  Now wait for a short while to allow the AC97 part to start
4951          *  generating bit clock (so we don't try to start the PLL without an
4952          *  input clock).
4953          */
4954         mdelay(5 * cs_laptop_wait);             /* 1 should be enough ?? (and pigs might fly) */
4955
4956         /*
4957          *  Set the serial port timing configuration, so that
4958          *  the clock control circuit gets its clock from the correct place.
4959          */
4960         cs461x_pokeBA0(card, BA0_SERMC1, SERMC1_PTC_AC97);
4961
4962         /*
4963         * The part seems to not be ready for a while after a resume.
4964         * so, if we are resuming, then wait for 700 mils.  Note that 600 mils
4965         * is not enough for some platforms! tested on an IBM Thinkpads and 
4966         * reference cards.
4967         */
4968         if(!(card->pm.flags & CS46XX_PM_IDLE))
4969                 mdelay(initdelay);
4970         /*
4971          *  Write the selected clock control setup to the hardware.  Do not turn on
4972          *  SWCE yet (if requested), so that the devices clocked by the output of
4973          *  PLL are not clocked until the PLL is stable.
4974          */
4975         cs461x_pokeBA0(card, BA0_PLLCC, PLLCC_LPF_1050_2780_KHZ | PLLCC_CDR_73_104_MHZ);
4976         cs461x_pokeBA0(card, BA0_PLLM, 0x3a);
4977         cs461x_pokeBA0(card, BA0_CLKCR2, CLKCR2_PDIVS_8);
4978
4979         /*
4980          *  Power up the PLL.
4981          */
4982         cs461x_pokeBA0(card, BA0_CLKCR1, CLKCR1_PLLP);
4983
4984         /*
4985          *  Wait until the PLL has stabilized.
4986          */
4987         mdelay(5 * cs_laptop_wait);             /* Again 1 should be enough ?? */
4988
4989         /*
4990          *  Turn on clocking of the core so that we can setup the serial ports.
4991          */
4992         tmp = cs461x_peekBA0(card, BA0_CLKCR1) | CLKCR1_SWCE;
4993         cs461x_pokeBA0(card, BA0_CLKCR1, tmp);
4994
4995         /*
4996          *  Fill the serial port FIFOs with silence.
4997          */
4998         cs461x_clear_serial_FIFOs(card,CS_TYPE_DAC | CS_TYPE_ADC);
4999
5000         /*
5001          *  Set the serial port FIFO pointer to the first sample in the FIFO.
5002          */
5003         /* cs461x_pokeBA0(card, BA0_SERBSP, 0); */
5004
5005         /*
5006          *  Write the serial port configuration to the part.  The master
5007          *  enable bit is not set until all other values have been written.
5008          */
5009         cs461x_pokeBA0(card, BA0_SERC1, SERC1_SO1F_AC97 | SERC1_SO1EN);
5010         cs461x_pokeBA0(card, BA0_SERC2, SERC2_SI1F_AC97 | SERC1_SO1EN);
5011         cs461x_pokeBA0(card, BA0_SERMC1, SERMC1_PTC_AC97 | SERMC1_MSPE);
5012
5013
5014         mdelay(5 * cs_laptop_wait);             /* Shouldnt be needed ?? */
5015         
5016 /*
5017 * If we are resuming under 2.2.x then we can not schedule a timeout.
5018 * so, just spin the CPU.
5019 */
5020         if(card->pm.flags & CS46XX_PM_IDLE)
5021         {
5022         /*
5023          * Wait for the card ready signal from the AC97 card.
5024          */
5025                 end_time = jiffies + 3 * (HZ >> 2);
5026                 do {
5027                 /*
5028                  *  Read the AC97 status register to see if we've seen a CODEC READY
5029                  *  signal from the AC97 card.
5030                  */
5031                         if (cs461x_peekBA0(card, BA0_ACSTS) & ACSTS_CRDY)
5032                                 break;
5033                         current->state = TASK_UNINTERRUPTIBLE;
5034                         schedule_timeout(1);
5035                 } while (time_before(jiffies, end_time));
5036         }
5037         else
5038         {
5039                 for (count = 0; count < 100; count++) {
5040                 // First, we want to wait for a short time.
5041                         udelay(25 * cs_laptop_wait);
5042
5043                         if (cs461x_peekBA0(card, BA0_ACSTS) & ACSTS_CRDY)
5044                                 break;
5045                 }
5046         }
5047
5048         /*
5049          *  Make sure CODEC is READY.
5050          */
5051         if (!(cs461x_peekBA0(card, BA0_ACSTS) & ACSTS_CRDY)) {
5052                 CS_DBGOUT(CS_ERROR | CS_INIT, 1, printk(KERN_WARNING  
5053                         "cs46xx: create - never read card ready from AC'97\n"));
5054                 CS_DBGOUT(CS_ERROR | CS_INIT, 1, printk(KERN_WARNING  
5055                         "cs46xx: probably not a bug, try using the CS4232 driver,\n"));
5056                 CS_DBGOUT(CS_ERROR | CS_INIT, 1, printk(KERN_WARNING  
5057                         "cs46xx: or turn off any automatic Power Management support in the BIOS.\n"));
5058                 return -EIO;
5059         }
5060
5061         /*
5062          *  Assert the vaid frame signal so that we can start sending commands
5063          *  to the AC97 card.
5064          */
5065         cs461x_pokeBA0(card, BA0_ACCTL, ACCTL_VFRM | ACCTL_ESYN | ACCTL_RSTN);
5066
5067         if(card->pm.flags & CS46XX_PM_IDLE)
5068         {
5069         /*
5070          *  Wait until we've sampled input slots 3 and 4 as valid, meaning that
5071          *  the card is pumping ADC data across the AC-link.
5072          */
5073                 end_time = jiffies + 3 * (HZ >> 2);
5074                 do {
5075                         /*
5076                          *  Read the input slot valid register and see if input slots 3 and
5077                          *  4 are valid yet.
5078                          */
5079                         if ((cs461x_peekBA0(card, BA0_ACISV) & (ACISV_ISV3 | ACISV_ISV4)) == (ACISV_ISV3 | ACISV_ISV4))
5080                                 break;
5081                         current->state = TASK_UNINTERRUPTIBLE;
5082                         schedule_timeout(1);
5083                 } while (time_before(jiffies, end_time));
5084         }
5085         else
5086         {
5087                 for (count = 0; count < 100; count++) {
5088                 // First, we want to wait for a short time.
5089                         udelay(25 * cs_laptop_wait);
5090
5091                         if ((cs461x_peekBA0(card, BA0_ACISV) & (ACISV_ISV3 | ACISV_ISV4)) == (ACISV_ISV3 | ACISV_ISV4))
5092                                 break;
5093                 }
5094         }
5095         /*
5096          *  Make sure input slots 3 and 4 are valid.  If not, then return
5097          *  an error.
5098          */
5099         if ((cs461x_peekBA0(card, BA0_ACISV) & (ACISV_ISV3 | ACISV_ISV4)) != (ACISV_ISV3 | ACISV_ISV4)) {
5100                 printk(KERN_WARNING "cs46xx: create - never read ISV3 & ISV4 from AC'97\n");
5101                 return -EIO;
5102         }
5103
5104         /*
5105          *  Now, assert valid frame and the slot 3 and 4 valid bits.  This will
5106          *  commense the transfer of digital audio data to the AC97 card.
5107          */
5108         cs461x_pokeBA0(card, BA0_ACOSV, ACOSV_SLV3 | ACOSV_SLV4);
5109
5110         /*
5111          *  Turn off the Processor by turning off the software clock enable flag in 
5112          *  the clock control register.
5113          */
5114         /* tmp = cs461x_peekBA0(card, BA0_CLKCR1) & ~CLKCR1_SWCE; */
5115         /* cs461x_pokeBA0(card, BA0_CLKCR1, tmp); */
5116
5117         /*
5118          *  Reset the processor.
5119          */
5120         cs461x_reset(card);
5121
5122         /*
5123          *  Download the image to the processor.
5124          */
5125         
5126         cs461x_download_image(card);
5127
5128         /*
5129          *  Stop playback DMA.
5130          */
5131         tmp = cs461x_peek(card, BA1_PCTL);
5132         card->pctl = tmp & 0xffff0000;
5133         cs461x_poke(card, BA1_PCTL, tmp & 0x0000ffff);
5134
5135         /*
5136          *  Stop capture DMA.
5137          */
5138         tmp = cs461x_peek(card, BA1_CCTL);
5139         card->cctl = tmp & 0x0000ffff;
5140         cs461x_poke(card, BA1_CCTL, tmp & 0xffff0000);
5141
5142         /* initialize AC97 codec and register /dev/mixer */
5143         if(card->pm.flags & CS46XX_PM_IDLE)
5144         {
5145                 if (cs_ac97_init(card) <= 0)
5146                 {
5147                         CS_DBGOUT(CS_ERROR | CS_INIT, 1, printk(KERN_INFO 
5148                                 "cs46xx: cs_ac97_init() failure\n") );
5149                         return -EIO;
5150                 }
5151         }
5152         else
5153         {
5154                 cs46xx_ac97_resume(card);
5155         }
5156         
5157         cs461x_proc_start(card);
5158
5159         /*
5160          *  Enable interrupts on the part.
5161          */
5162         cs461x_pokeBA0(card, BA0_HICR, HICR_IEV | HICR_CHGM);
5163
5164         tmp = cs461x_peek(card, BA1_PFIE);
5165         tmp &= ~0x0000f03f;
5166         cs461x_poke(card, BA1_PFIE, tmp);       /* playback interrupt enable */
5167
5168         tmp = cs461x_peek(card, BA1_CIE);
5169         tmp &= ~0x0000003f;
5170         tmp |=  0x00000001;
5171         cs461x_poke(card, BA1_CIE, tmp);        /* capture interrupt enable */  
5172
5173         /*
5174          *  If IDLE then Power down the part.  We will power components up 
5175          *  when we need them.  
5176          */
5177         if(card->pm.flags & CS46XX_PM_IDLE)
5178         {
5179                 if(!cs_powerdown)
5180                 {
5181                         if( (tmp = cs46xx_powerup(card, CS_POWER_DAC | CS_POWER_ADC |
5182                                         CS_POWER_MIXVON )) )
5183                         {
5184                                 CS_DBGOUT(CS_ERROR | CS_INIT, 1, printk(KERN_INFO 
5185                                         "cs46xx: cs461x_powerup() failure (0x%x)\n",tmp) );
5186                                 return -EIO;
5187                         }
5188                 }
5189                 else
5190                 {
5191                         if( (tmp = cs461x_powerdown(card, CS_POWER_DAC | CS_POWER_ADC |
5192                                         CS_POWER_MIXVON, CS_FALSE )) )
5193                         {
5194                                 CS_DBGOUT(CS_ERROR | CS_INIT, 1, printk(KERN_INFO 
5195                                         "cs46xx: cs461x_powerdown() failure (0x%x)\n",tmp) );
5196                                 return -EIO;
5197                         }
5198                 }
5199         }
5200         CS_DBGOUT(CS_FUNCTION | CS_INIT, 2, printk(KERN_INFO 
5201                 "cs46xx: cs_hardware_init()- 0\n"));
5202         return 0;
5203 }
5204
5205 /* install the driver, we do not allocate hardware channel nor DMA buffer now, they are defered 
5206    until "ACCESS" time (in prog_dmabuf called by open/read/write/ioctl/mmap) */
5207    
5208 /*
5209  *      Card subid table
5210  */
5211  
5212 struct cs_card_type
5213 {
5214         u16 vendor;
5215         u16 id;
5216         char *name;
5217         void (*amp)(struct cs_card *, int);
5218         void (*amp_init)(struct cs_card *);
5219         void (*active)(struct cs_card *, int);
5220 };
5221
5222 static struct cs_card_type cards[] = {
5223         {
5224                 .vendor = 0x1489,
5225                 .id     = 0x7001,
5226                 .name   = "Genius Soundmaker 128 value",
5227                 .amp    = amp_none,
5228         },
5229         {
5230                 .vendor = 0x5053,
5231                 .id     = 0x3357,
5232                 .name   = "Voyetra",
5233                 .amp    = amp_voyetra,
5234         },
5235         {
5236                 .vendor = 0x1071,
5237                 .id     = 0x6003,
5238                 .name   = "Mitac MI6020/21",
5239                 .amp    = amp_voyetra,
5240         },
5241         {
5242                 .vendor = 0x14AF,
5243                 .id     = 0x0050,
5244                 .name   = "Hercules Game Theatre XP",
5245                 .amp    = amp_hercules,
5246         },
5247         {
5248                 .vendor = 0x1681,
5249                 .id     = 0x0050,
5250                 .name   = "Hercules Game Theatre XP",
5251                 .amp    = amp_hercules,
5252         },
5253         {
5254                 .vendor = 0x1681,
5255                 .id     = 0x0051,
5256                 .name   = "Hercules Game Theatre XP",
5257                 .amp    = amp_hercules,
5258         },
5259         {
5260                 .vendor = 0x1681,
5261                 .id     = 0x0052,
5262                 .name   = "Hercules Game Theatre XP",
5263                 .amp    = amp_hercules,
5264         },
5265         {
5266                 .vendor = 0x1681,
5267                 .id     = 0x0053,
5268                 .name   = "Hercules Game Theatre XP",
5269                 .amp    = amp_hercules,
5270         },
5271         {
5272                 .vendor = 0x1681,
5273                 .id     = 0x0054,
5274                 .name   = "Hercules Game Theatre XP",
5275                 .amp    = amp_hercules,
5276         },
5277         {
5278                 .vendor = 0x1681,
5279                 .id     = 0xa010,
5280                 .name   = "Hercules Fortissimo II",
5281                 .amp    = amp_none,
5282         },
5283         /* Not sure if the 570 needs the clkrun hack */
5284         {
5285                 .vendor = PCI_VENDOR_ID_IBM,
5286                 .id     = 0x0132,
5287                 .name   = "Thinkpad 570",
5288                 .amp    = amp_none,
5289                 .active = clkrun_hack,
5290         },
5291         {
5292                 .vendor = PCI_VENDOR_ID_IBM,
5293                 .id     = 0x0153,
5294                 .name   = "Thinkpad 600X/A20/T20",
5295                 .amp    = amp_none,
5296                 .active = clkrun_hack,
5297         },
5298         {
5299                 .vendor = PCI_VENDOR_ID_IBM,
5300                 .id     = 0x1010,
5301                 .name   = "Thinkpad 600E (unsupported)",
5302         },
5303         {
5304                 .name   = "Card without SSID set",
5305         },
5306         { 0, },
5307 };
5308
5309 MODULE_AUTHOR("Alan Cox <alan@redhat.com>, Jaroslav Kysela, <pcaudio@crystal.cirrus.com>");
5310 MODULE_DESCRIPTION("Crystal SoundFusion Audio Support");
5311 MODULE_LICENSE("GPL");
5312
5313
5314 static const char cs46xx_banner[] = KERN_INFO "Crystal 4280/46xx + AC97 Audio, version " CS46XX_MAJOR_VERSION "." CS46XX_MINOR_VERSION "." CS46XX_ARCH ", " __TIME__ " " __DATE__ "\n";
5315 static const char fndmsg[] = KERN_INFO "cs46xx: Found %d audio device(s).\n";
5316
5317 static int __devinit cs46xx_probe(struct pci_dev *pci_dev,
5318                                   const struct pci_device_id *pciid)
5319 {
5320         int i,j;
5321         u16 ss_card, ss_vendor;
5322         struct cs_card *card;
5323         dma_addr_t dma_mask;
5324         struct cs_card_type *cp = &cards[0];
5325
5326         CS_DBGOUT(CS_FUNCTION | CS_INIT, 2,
5327                   printk(KERN_INFO "cs46xx: probe()+\n"));
5328
5329         dma_mask = 0xffffffff;  /* this enables playback and recording */
5330         if (pci_enable_device(pci_dev)) {
5331                 CS_DBGOUT(CS_INIT | CS_ERROR, 1, printk(KERN_ERR
5332                          "cs46xx: pci_enable_device() failed\n"));
5333                 return -1;
5334         }
5335         if (!RSRCISMEMORYREGION(pci_dev, 0) ||
5336             !RSRCISMEMORYREGION(pci_dev, 1)) {
5337                 CS_DBGOUT(CS_ERROR, 1, printk(KERN_ERR
5338                          "cs46xx: probe()- Memory region not assigned\n"));
5339                 return -1;
5340         }
5341         if (pci_dev->irq == 0) {
5342                 CS_DBGOUT(CS_ERROR, 1, printk(KERN_ERR
5343                          "cs46xx: probe() IRQ not assigned\n"));
5344                 return -1;
5345         }
5346         if (!pci_dma_supported(pci_dev, 0xffffffff)) {
5347                 CS_DBGOUT(CS_ERROR, 1, printk(KERN_ERR
5348                       "cs46xx: probe() architecture does not support 32bit PCI busmaster DMA\n"));
5349                 return -1;
5350         }
5351         pci_read_config_word(pci_dev, PCI_SUBSYSTEM_VENDOR_ID, &ss_vendor);
5352         pci_read_config_word(pci_dev, PCI_SUBSYSTEM_ID, &ss_card);
5353
5354         if ((card = kmalloc(sizeof(struct cs_card), GFP_KERNEL)) == NULL) {
5355                 printk(KERN_ERR "cs46xx: out of memory\n");
5356                 return -ENOMEM;
5357         }
5358         memset(card, 0, sizeof(*card));
5359         card->ba0_addr = RSRCADDRESS(pci_dev, 0);
5360         card->ba1_addr = RSRCADDRESS(pci_dev, 1);
5361         card->pci_dev = pci_dev;
5362         card->irq = pci_dev->irq;
5363         card->magic = CS_CARD_MAGIC;
5364         spin_lock_init(&card->lock);
5365         spin_lock_init(&card->ac97_lock);
5366
5367         pci_set_master(pci_dev);
5368
5369         printk(cs46xx_banner);
5370         printk(KERN_INFO "cs46xx: Card found at 0x%08lx and 0x%08lx, IRQ %d\n",
5371                card->ba0_addr, card->ba1_addr, card->irq);
5372
5373         card->alloc_pcm_channel = cs_alloc_pcm_channel;
5374         card->alloc_rec_pcm_channel = cs_alloc_rec_pcm_channel;
5375         card->free_pcm_channel = cs_free_pcm_channel;
5376         card->amplifier_ctrl = amp_none;
5377         card->active_ctrl = amp_none;
5378
5379         while (cp->name)
5380         {
5381                 if(cp->vendor == ss_vendor && cp->id == ss_card)
5382                 {
5383                         card->amplifier_ctrl = cp->amp;
5384                         if(cp->active)
5385                                 card->active_ctrl = cp->active;
5386                         if(cp->amp_init)
5387                                 card->amp_init = cp->amp_init;
5388                         break;
5389                 }
5390                 cp++;
5391         }
5392         if (cp->name==NULL)
5393         {
5394                 printk(KERN_INFO "cs46xx: Unknown card (%04X:%04X) at 0x%08lx/0x%08lx, IRQ %d\n",
5395                         ss_vendor, ss_card, card->ba0_addr, card->ba1_addr,  card->irq);
5396         }
5397         else
5398         {
5399                 printk(KERN_INFO "cs46xx: %s (%04X:%04X) at 0x%08lx/0x%08lx, IRQ %d\n",
5400                         cp->name, ss_vendor, ss_card, card->ba0_addr, card->ba1_addr, card->irq);
5401         }
5402         
5403         if (card->amplifier_ctrl==NULL)
5404         {
5405                 card->amplifier_ctrl = amp_none;
5406                 card->active_ctrl = clkrun_hack;
5407         }               
5408
5409         if (external_amp == 1)
5410         {
5411                 printk(KERN_INFO "cs46xx: Crystal EAPD support forced on.\n");
5412                 card->amplifier_ctrl = amp_voyetra;
5413         }
5414
5415         if (thinkpad == 1)
5416         {
5417                 printk(KERN_INFO "cs46xx: Activating CLKRUN hack for Thinkpad.\n");
5418                 card->active_ctrl = clkrun_hack;
5419         }
5420 /*
5421 * The thinkpads don't work well without runtime updating on their kernel 
5422 * delay values (or any laptop with variable CPU speeds really).
5423 * so, just to be safe set the init delay to 2100.  Eliminates
5424 * failures on T21 Thinkpads.  remove this code when the udelay
5425 * and mdelay kernel code is replaced by a pm timer, or the delays
5426 * work well for battery and/or AC power both.
5427 */
5428         if(card->active_ctrl == clkrun_hack)
5429         {
5430                 initdelay = 2100;
5431                 cs_laptop_wait = 5;
5432         }
5433         if((card->active_ctrl == clkrun_hack) && !(powerdown == 1))
5434         {
5435 /*
5436 * for some currently unknown reason, powering down the DAC and ADC component
5437 * blocks on thinkpads causes some funky behavior... distoorrrtion and ac97 
5438 * codec access problems.  probably the serial clock becomes unsynced. 
5439 * added code to sync the chips back up, but only helped about 70% the time.
5440 */
5441                 cs_powerdown = 0;
5442         }
5443         if(powerdown == 0)
5444                 cs_powerdown = 0;
5445         card->active_ctrl(card, 1);
5446
5447         /* claim our iospace and irq */
5448         
5449         card->ba0 = ioremap_nocache(card->ba0_addr, CS461X_BA0_SIZE);
5450         card->ba1.name.data0 = ioremap_nocache(card->ba1_addr + BA1_SP_DMEM0, CS461X_BA1_DATA0_SIZE);
5451         card->ba1.name.data1 = ioremap_nocache(card->ba1_addr + BA1_SP_DMEM1, CS461X_BA1_DATA1_SIZE);
5452         card->ba1.name.pmem = ioremap_nocache(card->ba1_addr + BA1_SP_PMEM, CS461X_BA1_PRG_SIZE);
5453         card->ba1.name.reg = ioremap_nocache(card->ba1_addr + BA1_SP_REG, CS461X_BA1_REG_SIZE);
5454         
5455         CS_DBGOUT(CS_INIT, 4, printk(KERN_INFO 
5456                 "cs46xx: card=%p card->ba0=%p\n",card,card->ba0) );
5457         CS_DBGOUT(CS_INIT, 4, printk(KERN_INFO 
5458                 "cs46xx: card->ba1=%p %p %p %p\n",
5459                         card->ba1.name.data0,
5460                         card->ba1.name.data1,
5461                         card->ba1.name.pmem,
5462                         card->ba1.name.reg) );
5463
5464         if(card->ba0 == 0 || card->ba1.name.data0 == 0 ||
5465                 card->ba1.name.data1 == 0 || card->ba1.name.pmem == 0 ||
5466                 card->ba1.name.reg == 0)
5467                 goto fail2;
5468                 
5469         if (request_irq(card->irq, &cs_interrupt, SA_SHIRQ, "cs46xx", card)) {
5470                 printk(KERN_ERR "cs46xx: unable to allocate irq %d\n", card->irq);
5471                 goto fail2;
5472         }
5473         /* register /dev/dsp */
5474         if ((card->dev_audio = register_sound_dsp(&cs461x_fops, -1)) < 0) {
5475                 printk(KERN_ERR "cs46xx: unable to register dsp\n");
5476                 goto fail;
5477         }
5478
5479         /* register /dev/midi */
5480         if((card->dev_midi = register_sound_midi(&cs_midi_fops, -1)) < 0)
5481                 printk(KERN_ERR "cs46xx: unable to register midi\n");
5482                 
5483         card->pm.flags |= CS46XX_PM_IDLE;
5484         for(i=0;i<5;i++)
5485         {
5486                 if (cs_hardware_init(card) != 0)
5487                 {
5488                         CS_DBGOUT(CS_ERROR, 4, printk(
5489                                 "cs46xx: ERROR in cs_hardware_init()... retrying\n"));
5490                         for (j = 0; j < NR_AC97; j++)
5491                                 if (card->ac97_codec[j] != NULL) {
5492                                         unregister_sound_mixer(card->ac97_codec[j]->dev_mixer);
5493                                         ac97_release_codec(card->ac97_codec[j]);
5494                                 }
5495                         mdelay(10 * cs_laptop_wait);
5496                         continue;
5497                 }
5498                 break;
5499         }
5500         if(i>=4)
5501         {
5502                 CS_DBGOUT(CS_PM | CS_ERROR, 1, printk(
5503                         "cs46xx: cs46xx_probe()- cs_hardware_init() failed, retried %d times.\n",i));
5504                 unregister_sound_dsp(card->dev_audio);
5505                 if(card->dev_midi)
5506                         unregister_sound_midi(card->dev_midi);
5507                 goto fail;
5508         }
5509
5510         init_waitqueue_head(&card->midi.open_wait);
5511         mutex_init(&card->midi.open_mutex);
5512         init_waitqueue_head(&card->midi.iwait);
5513         init_waitqueue_head(&card->midi.owait);
5514         cs461x_pokeBA0(card, BA0_MIDCR, MIDCR_MRST);   
5515         cs461x_pokeBA0(card, BA0_MIDCR, 0);   
5516
5517         /* 
5518         * Check if we have to init the amplifier, but probably already done
5519         * since the CD logic in the ac97 init code will turn on the ext amp.
5520         */
5521         if(cp->amp_init)
5522                 cp->amp_init(card);
5523         card->active_ctrl(card, -1);
5524
5525         PCI_SET_DRIVER_DATA(pci_dev, card);
5526         PCI_SET_DMA_MASK(pci_dev, dma_mask);
5527         list_add(&card->list, &cs46xx_devs);
5528
5529         CS_DBGOUT(CS_PM, 9, printk(KERN_INFO "cs46xx: pm.flags=0x%x card=%p\n",
5530                 (unsigned)card->pm.flags,card));
5531
5532         CS_DBGOUT(CS_INIT | CS_FUNCTION, 2, printk(KERN_INFO
5533                 "cs46xx: probe()- device allocated successfully\n"));
5534         return 0;
5535
5536 fail:
5537         free_irq(card->irq, card);
5538 fail2:
5539         if(card->ba0)
5540                 iounmap(card->ba0);
5541         if(card->ba1.name.data0)
5542                 iounmap(card->ba1.name.data0);
5543         if(card->ba1.name.data1)
5544                 iounmap(card->ba1.name.data1);
5545         if(card->ba1.name.pmem)
5546                 iounmap(card->ba1.name.pmem);
5547         if(card->ba1.name.reg)
5548                 iounmap(card->ba1.name.reg);
5549         kfree(card);
5550         CS_DBGOUT(CS_INIT | CS_ERROR, 1, printk(KERN_INFO
5551                 "cs46xx: probe()- no device allocated\n"));
5552         return -ENODEV;
5553 } // probe_cs46xx
5554
5555 // --------------------------------------------------------------------- 
5556
5557 static void __devexit cs46xx_remove(struct pci_dev *pci_dev)
5558 {
5559         struct cs_card *card = PCI_GET_DRIVER_DATA(pci_dev);
5560         int i;
5561         unsigned int tmp;
5562         
5563         CS_DBGOUT(CS_INIT | CS_FUNCTION, 2, printk(KERN_INFO
5564                  "cs46xx: cs46xx_remove()+\n"));
5565
5566         card->active_ctrl(card,1);
5567         
5568         tmp = cs461x_peek(card, BA1_PFIE);
5569         tmp &= ~0x0000f03f;
5570         tmp |=  0x00000010;
5571         cs461x_poke(card, BA1_PFIE, tmp);       /* playback interrupt disable */
5572
5573         tmp = cs461x_peek(card, BA1_CIE);
5574         tmp &= ~0x0000003f;
5575         tmp |=  0x00000011;
5576         cs461x_poke(card, BA1_CIE, tmp);        /* capture interrupt disable */
5577
5578         /*
5579          *  Stop playback DMA.
5580          */
5581         tmp = cs461x_peek(card, BA1_PCTL);
5582         cs461x_poke(card, BA1_PCTL, tmp & 0x0000ffff);
5583
5584         /*
5585          *  Stop capture DMA.
5586          */
5587         tmp = cs461x_peek(card, BA1_CCTL);
5588         cs461x_poke(card, BA1_CCTL, tmp & 0xffff0000);
5589
5590         /*
5591          *  Reset the processor.
5592          */
5593         cs461x_reset(card);
5594
5595         cs461x_proc_stop(card);
5596
5597         /*
5598          *  Power down the DAC and ADC.  We will power them up (if) when we need
5599          *  them.
5600          */
5601         if( (tmp = cs461x_powerdown(card, CS_POWER_DAC | CS_POWER_ADC |
5602                         CS_POWER_MIXVON, CS_TRUE )) )
5603         {
5604                 CS_DBGOUT(CS_ERROR | CS_INIT, 1, printk(KERN_INFO 
5605                         "cs46xx: cs461x_powerdown() failure (0x%x)\n",tmp) );
5606         }
5607
5608         /*
5609          *  Power down the PLL.
5610          */
5611         cs461x_pokeBA0(card, BA0_CLKCR1, 0);
5612
5613         /*
5614          *  Turn off the Processor by turning off the software clock enable flag in 
5615          *  the clock control register.
5616          */
5617         tmp = cs461x_peekBA0(card, BA0_CLKCR1) & ~CLKCR1_SWCE;
5618         cs461x_pokeBA0(card, BA0_CLKCR1, tmp);
5619
5620         card->active_ctrl(card,-1);
5621
5622         /* free hardware resources */
5623         free_irq(card->irq, card);
5624         iounmap(card->ba0);
5625         iounmap(card->ba1.name.data0);
5626         iounmap(card->ba1.name.data1);
5627         iounmap(card->ba1.name.pmem);
5628         iounmap(card->ba1.name.reg);
5629         
5630         /* unregister audio devices */
5631         for (i = 0; i < NR_AC97; i++)
5632                 if (card->ac97_codec[i] != NULL) {
5633                         unregister_sound_mixer(card->ac97_codec[i]->dev_mixer);
5634                         ac97_release_codec(card->ac97_codec[i]);
5635                 }
5636         unregister_sound_dsp(card->dev_audio);
5637         if(card->dev_midi)
5638                 unregister_sound_midi(card->dev_midi);
5639         list_del(&card->list);
5640         kfree(card);
5641         PCI_SET_DRIVER_DATA(pci_dev,NULL);
5642
5643         CS_DBGOUT(CS_INIT | CS_FUNCTION, 2, printk(KERN_INFO
5644                  "cs46xx: cs46xx_remove()-: remove successful\n"));
5645 }
5646
5647 enum {
5648         CS46XX_4610 = 0,
5649         CS46XX_4612,    /* same as 4630 */
5650         CS46XX_4615,    /* same as 4624 */
5651 };
5652
5653 static struct pci_device_id cs46xx_pci_tbl[] = {
5654         {
5655                 .vendor      = PCI_VENDOR_ID_CIRRUS,
5656                 .device      = PCI_DEVICE_ID_CIRRUS_4610,
5657                 .subvendor   = PCI_ANY_ID,
5658                 .subdevice   = PCI_ANY_ID,
5659                 .driver_data = CS46XX_4610,
5660         },
5661         {
5662                 .vendor      = PCI_VENDOR_ID_CIRRUS,
5663                 .device      = PCI_DEVICE_ID_CIRRUS_4612,
5664                 .subvendor   = PCI_ANY_ID,
5665                 .subdevice   = PCI_ANY_ID,
5666                 .driver_data = CS46XX_4612,
5667         },
5668         {
5669                 .vendor      = PCI_VENDOR_ID_CIRRUS,
5670                 .device      = PCI_DEVICE_ID_CIRRUS_4615,
5671                 .subvendor   = PCI_ANY_ID,
5672                 .subdevice   = PCI_ANY_ID,
5673                 .driver_data = CS46XX_4615,
5674         },
5675         { 0, },
5676 };
5677
5678 MODULE_DEVICE_TABLE(pci, cs46xx_pci_tbl);
5679
5680 static struct pci_driver cs46xx_pci_driver = {
5681         .name     = "cs46xx",
5682         .id_table = cs46xx_pci_tbl,
5683         .probe    = cs46xx_probe,
5684         .remove   = __devexit_p(cs46xx_remove),
5685         .suspend  = CS46XX_SUSPEND_TBL,
5686         .resume   = CS46XX_RESUME_TBL,
5687 };
5688
5689 static int __init cs46xx_init_module(void)
5690 {
5691         int rtn = 0;
5692         CS_DBGOUT(CS_INIT | CS_FUNCTION, 2, printk(KERN_INFO 
5693                 "cs46xx: cs46xx_init_module()+ \n"));
5694         rtn = pci_register_driver(&cs46xx_pci_driver);
5695
5696         if(rtn == -ENODEV)
5697         {
5698                 CS_DBGOUT(CS_ERROR | CS_INIT, 1, printk( 
5699                         "cs46xx: Unable to detect valid cs46xx device\n"));
5700         }
5701
5702         CS_DBGOUT(CS_INIT | CS_FUNCTION, 2,
5703                   printk(KERN_INFO "cs46xx: cs46xx_init_module()- (%d)\n",rtn));
5704         return rtn;
5705 }
5706
5707 static void __exit cs46xx_cleanup_module(void)
5708 {
5709         pci_unregister_driver(&cs46xx_pci_driver);
5710         CS_DBGOUT(CS_INIT | CS_FUNCTION, 2,
5711                   printk(KERN_INFO "cs46xx: cleanup_cs46xx() finished\n"));
5712 }
5713
5714 module_init(cs46xx_init_module);
5715 module_exit(cs46xx_cleanup_module);
5716
5717 #if CS46XX_ACPI_SUPPORT
5718 static int cs46xx_suspend_tbl(struct pci_dev *pcidev, pm_message_t state)
5719 {
5720         struct cs_card *s = PCI_GET_DRIVER_DATA(pcidev);
5721         CS_DBGOUT(CS_PM | CS_FUNCTION, 2, 
5722                 printk(KERN_INFO "cs46xx: cs46xx_suspend_tbl request\n"));
5723         cs46xx_suspend(s, state);
5724         return 0;
5725 }
5726
5727 static int cs46xx_resume_tbl(struct pci_dev *pcidev)
5728 {
5729         struct cs_card *s = PCI_GET_DRIVER_DATA(pcidev);
5730         CS_DBGOUT(CS_PM | CS_FUNCTION, 2, 
5731                 printk(KERN_INFO "cs46xx: cs46xx_resume_tbl request\n"));
5732         cs46xx_resume(s);
5733         return 0;
5734 }
5735 #endif