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