2 * Crystal SoundFusion CS46xx driver
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>
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>
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.
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.
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>
30 * <nils@kernelconcepts.de>
31 * Thanks to David Pollard for testing.
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.
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
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.
61 * Playback/Capture supported from 8k-48k.
62 * 16Bit Signed LE & 8Bit Unsigned, with Mono or Stereo supported.
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
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.
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
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>
96 #include <asm/uaccess.h>
98 #include "cs46xxpm-24.h"
99 #include "cs46xx_wrapper-24.h"
102 /* MIDI buffer sizes */
103 #define CS_MIDIINBUF 500
104 #define CS_MIDIOUTBUF 500
106 #define ADC_RUNNING 1
107 #define DAC_RUNNING 2
109 #define CS_FMT_16BIT 1 /* These are fixed in fact */
110 #define CS_FMT_STEREO 2
111 #define CS_FMT_MASK 3
113 #define CS_TYPE_ADC 1
114 #define CS_TYPE_DAC 2
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)
124 #define CS_DBGBREAKPOINT {__asm__("INT $3");}
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
135 #define GOF_PER_SEC 200
137 #define CSDEBUG_INTERFACE 1
140 * Turn on/off debugging compilation by using 1/0 respectively for CSDEBUG
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.
149 #define CS_DBGOUT(mask,level,x) if((cs_debuglevel >= (level)) && ((mask) & cs_debugmask)) {x;}
151 #define CS_DBGOUT(mask,level,x)
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 */
173 #define CS_IOCTL_CMD_SUSPEND 0x1 // suspend
174 #define CS_IOCTL_CMD_RESUME 0x2 // resume
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);
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);
192 static int external_amp;
193 module_param(external_amp, bool, 0);
195 module_param(thinkpad, bool, 0);
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.
202 static unsigned cs_powerdown=1;
203 static unsigned cs_laptop_wait=1;
205 /* An instance of the 4610 channel */
213 #define CS46XX_MAJOR_VERSION "1"
214 #define CS46XX_MINOR_VERSION "28"
217 #define CS46XX_ARCH "64" //architecture key
219 #define CS46XX_ARCH "32" //architecture key
222 static struct list_head cs46xx_devs = { &cs46xx_devs, &cs46xx_devs };
224 /* magic numbers to protect our data structures */
225 #define CS_CARD_MAGIC 0x43525553 /* "CRUS" */
226 #define CS_STATE_MAGIC 0x4c4f4749 /* "LOGI" */
229 /* maxinum number of AC97 codecs connected, AC97 2.0 defined 4 */
232 static const unsigned sample_size[] = { 1, 2, 2, 4 };
233 static const unsigned sample_shift[] = { 0, 1, 1, 2 };
235 /* "software" or virtual channel, an instance of opened /dev/dsp */
238 struct cs_card *card; /* Card info */
240 /* single open lock mechanism, only used for recording */
241 struct semaphore open_sem;
242 wait_queue_head_t open_wait;
247 /* virtual channel number */
251 /* wave sample stuff */
253 unsigned char fmt, enable;
255 /* hardware channel */
256 struct cs_channel *channel;
257 int pringbuf; /* Software ring slot */
258 void *pbuf; /* 4K hardware DMA buffer */
260 /* OSS buffer management stuff */
262 dma_addr_t dma_handle;
268 void *tmpbuff; /* tmp buffer for sample conversions */
270 dma_addr_t dmaaddr_tmpbuff;
271 unsigned buforder_tmpbuff; /* Log base 2 of size in bytes.. */
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 */
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 */
284 /* redundant, but makes calculations easier */
287 unsigned fragsamples;
292 unsigned endcleared:1;
294 unsigned update_flag;
295 unsigned ossfragshift;
297 unsigned subdivision;
299 /* Guard against mmap/write/read races */
300 struct semaphore sem;
304 struct cs_channel channel[2];
307 /* We keep cs461x cards in a linked list */
308 struct cs_card *next;
310 /* The cs461x has a certain amount of cross channel interaction
311 so we use a single per card lock */
315 spinlock_t ac97_lock;
317 /* mixer use count */
318 atomic_t mixer_use_cnt;
320 /* PCI device stuff */
321 struct pci_dev * pci_dev;
322 struct list_head list;
324 unsigned int pctl, cctl; /* Hardware DMA flag sets */
326 /* soundcore stuff */
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];
336 int amplifier; /* Amplifier control */
337 void (*amplifier_ctrl)(struct cs_card *, int);
338 void (*amp_init)(struct cs_card *);
340 int active; /* Active clocking */
341 void (*active_ctrl)(struct cs_card *, int);
343 /* hardware resources */
344 unsigned long ba0_addr;
345 unsigned long ba1_addr;
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);
367 /* /dev/midi stuff */
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;
375 unsigned char ibuf[CS_MIDIINBUF];
376 unsigned char obuf[CS_MIDIOUTBUF];
378 struct semaphore open_sem;
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,
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);
398 #define SOUND_MIXER_CS_GETDBGLEVEL _SIOWR('M',120, int)
399 #define SOUND_MIXER_CS_SETDBGLEVEL _SIOWR('M',121, int)
400 #define SOUND_MIXER_CS_GETDBGMASK _SIOWR('M',122, int)
401 #define SOUND_MIXER_CS_SETDBGMASK _SIOWR('M',123, int)
402 #define SOUND_MIXER_CS_APM _SIOWR('M',124, int)
404 static void printioctl(unsigned int x)
408 /* these values are incorrect for the ac97 driver, fix.
409 * Index of mixtable1[] member is Device ID
410 * and must be <= SOUND_MIXER_NRDEVICES.
411 * Value of array member is index into s->mix.vol[]
413 static const unsigned char mixtable1[SOUND_MIXER_NRDEVICES] = {
414 [SOUND_MIXER_PCM] = 1, /* voice */
415 [SOUND_MIXER_LINE1] = 2, /* AUX */
416 [SOUND_MIXER_CD] = 3, /* CD */
417 [SOUND_MIXER_LINE] = 4, /* Line */
418 [SOUND_MIXER_SYNTH] = 5, /* FM */
419 [SOUND_MIXER_MIC] = 6, /* Mic */
420 [SOUND_MIXER_SPEAKER] = 7, /* Speaker */
421 [SOUND_MIXER_RECLEV] = 8, /* Recording level */
422 [SOUND_MIXER_VOLUME] = 9 /* Master Volume */
427 case SOUND_MIXER_CS_GETDBGMASK:
428 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_CS_GETDBGMASK: ") );
430 case SOUND_MIXER_CS_GETDBGLEVEL:
431 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_CS_GETDBGLEVEL: ") );
433 case SOUND_MIXER_CS_SETDBGMASK:
434 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_CS_SETDBGMASK: ") );
436 case SOUND_MIXER_CS_SETDBGLEVEL:
437 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_CS_SETDBGLEVEL: ") );
440 CS_DBGOUT(CS_IOCTL, 4, printk("OSS_GETVERSION: ") );
442 case SNDCTL_DSP_SYNC:
443 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_SYNC: ") );
445 case SNDCTL_DSP_SETDUPLEX:
446 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_SETDUPLEX: ") );
448 case SNDCTL_DSP_GETCAPS:
449 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_GETCAPS: ") );
451 case SNDCTL_DSP_RESET:
452 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_RESET: ") );
454 case SNDCTL_DSP_SPEED:
455 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_SPEED: ") );
457 case SNDCTL_DSP_STEREO:
458 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_STEREO: ") );
460 case SNDCTL_DSP_CHANNELS:
461 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_CHANNELS: ") );
463 case SNDCTL_DSP_GETFMTS:
464 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_GETFMTS: ") );
466 case SNDCTL_DSP_SETFMT:
467 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_SETFMT: ") );
469 case SNDCTL_DSP_POST:
470 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_POST: ") );
472 case SNDCTL_DSP_GETTRIGGER:
473 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_GETTRIGGER: ") );
475 case SNDCTL_DSP_SETTRIGGER:
476 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_SETTRIGGER: ") );
478 case SNDCTL_DSP_GETOSPACE:
479 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_GETOSPACE: ") );
481 case SNDCTL_DSP_GETISPACE:
482 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_GETISPACE: ") );
484 case SNDCTL_DSP_NONBLOCK:
485 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_NONBLOCK: ") );
487 case SNDCTL_DSP_GETODELAY:
488 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_GETODELAY: ") );
490 case SNDCTL_DSP_GETIPTR:
491 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_GETIPTR: ") );
493 case SNDCTL_DSP_GETOPTR:
494 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_GETOPTR: ") );
496 case SNDCTL_DSP_GETBLKSIZE:
497 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_GETBLKSIZE: ") );
499 case SNDCTL_DSP_SETFRAGMENT:
500 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_SETFRAGMENT: ") );
502 case SNDCTL_DSP_SUBDIVIDE:
503 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_SUBDIVIDE: ") );
505 case SOUND_PCM_READ_RATE:
506 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_PCM_READ_RATE: ") );
508 case SOUND_PCM_READ_CHANNELS:
509 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_PCM_READ_CHANNELS: ") );
511 case SOUND_PCM_READ_BITS:
512 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_PCM_READ_BITS: ") );
514 case SOUND_PCM_WRITE_FILTER:
515 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_PCM_WRITE_FILTER: ") );
517 case SNDCTL_DSP_SETSYNCRO:
518 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_SETSYNCRO: ") );
520 case SOUND_PCM_READ_FILTER:
521 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_PCM_READ_FILTER: ") );
524 case SOUND_MIXER_PRIVATE1:
525 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_PRIVATE1: ") );
527 case SOUND_MIXER_PRIVATE2:
528 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_PRIVATE2: ") );
530 case SOUND_MIXER_PRIVATE3:
531 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_PRIVATE3: ") );
533 case SOUND_MIXER_PRIVATE4:
534 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_PRIVATE4: ") );
536 case SOUND_MIXER_PRIVATE5:
537 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_PRIVATE5: ") );
539 case SOUND_MIXER_INFO:
540 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_INFO: ") );
542 case SOUND_OLD_MIXER_INFO:
543 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_OLD_MIXER_INFO: ") );
549 case SOUND_MIXER_VOLUME:
550 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_VOLUME: ") );
552 case SOUND_MIXER_SPEAKER:
553 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_SPEAKER: ") );
555 case SOUND_MIXER_RECLEV:
556 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_RECLEV: ") );
558 case SOUND_MIXER_MIC:
559 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_MIC: ") );
561 case SOUND_MIXER_SYNTH:
562 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_SYNTH: ") );
564 case SOUND_MIXER_RECSRC:
565 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_RECSRC: ") );
567 case SOUND_MIXER_DEVMASK:
568 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_DEVMASK: ") );
570 case SOUND_MIXER_RECMASK:
571 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_RECMASK: ") );
573 case SOUND_MIXER_STEREODEVS:
574 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_STEREODEVS: ") );
576 case SOUND_MIXER_CAPS:
577 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_CAPS:") );
581 if (i >= SOUND_MIXER_NRDEVICES || !(vidx = mixtable1[i]))
583 CS_DBGOUT(CS_IOCTL, 4, printk("UNKNOWN IOCTL: 0x%.8x NR=%d ",x,i) );
587 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_IOCTL AC9x: 0x%.8x NR=%d ",
593 CS_DBGOUT(CS_IOCTL, 4, printk("command = 0x%x IOC_NR=%d\n",x, _IOC_NR(x)) );
598 * common I/O routines
601 static void cs461x_poke(struct cs_card *codec, unsigned long reg, unsigned int val)
603 writel(val, codec->ba1.idx[(reg >> 16) & 3]+(reg&0xffff));
606 static unsigned int cs461x_peek(struct cs_card *codec, unsigned long reg)
608 return readl(codec->ba1.idx[(reg >> 16) & 3]+(reg&0xffff));
611 static void cs461x_pokeBA0(struct cs_card *codec, unsigned long reg, unsigned int val)
613 writel(val, codec->ba0+reg);
616 static unsigned int cs461x_peekBA0(struct cs_card *codec, unsigned long reg)
618 return readl(codec->ba0+reg);
622 static u16 cs_ac97_get(struct ac97_codec *dev, u8 reg);
623 static void cs_ac97_set(struct ac97_codec *dev, u8 reg, u16 data);
625 static struct cs_channel *cs_alloc_pcm_channel(struct cs_card *card)
627 if(card->channel[1].used==1)
629 card->channel[1].used=1;
630 card->channel[1].num=1;
631 return &card->channel[1];
634 static struct cs_channel *cs_alloc_rec_pcm_channel(struct cs_card *card)
636 if(card->channel[0].used==1)
638 card->channel[0].used=1;
639 card->channel[0].num=0;
640 return &card->channel[0];
643 static void cs_free_pcm_channel(struct cs_card *card, int channel)
645 card->channel[channel].state = NULL;
646 card->channel[channel].used=0;
650 * setup a divisor value to help with conversion from
651 * 16bit Stereo, down to 8bit stereo/mono or 16bit mono.
652 * assign a divisor of 1 if using 16bit Stereo as that is
653 * the only format that the static image will capture.
655 static void cs_set_divisor(struct dmabuf *dmabuf)
657 if(dmabuf->type == CS_TYPE_DAC)
659 else if( !(dmabuf->fmt & CS_FMT_STEREO) &&
660 (dmabuf->fmt & CS_FMT_16BIT))
662 else if( (dmabuf->fmt & CS_FMT_STEREO) &&
663 !(dmabuf->fmt & CS_FMT_16BIT))
665 else if( !(dmabuf->fmt & CS_FMT_STEREO) &&
666 !(dmabuf->fmt & CS_FMT_16BIT))
671 CS_DBGOUT(CS_PARMS | CS_FUNCTION, 8, printk(
672 "cs46xx: cs_set_divisor()- %s %d\n",
673 (dmabuf->type == CS_TYPE_ADC) ? "ADC" : "DAC",
678 * mute some of the more prevalent registers to avoid popping.
680 static void cs_mute(struct cs_card *card, int state)
682 struct ac97_codec *dev=card->ac97_codec[0];
684 CS_DBGOUT(CS_FUNCTION, 2, printk(KERN_INFO "cs46xx: cs_mute()+ %s\n",
685 (state == CS_TRUE) ? "Muting" : "UnMuting") );
690 * fix pops when powering up on thinkpads
692 card->pm.u32AC97_master_volume = (u32)cs_ac97_get( dev,
693 (u8)BA0_AC97_MASTER_VOLUME);
694 card->pm.u32AC97_headphone_volume = (u32)cs_ac97_get(dev,
695 (u8)BA0_AC97_HEADPHONE_VOLUME);
696 card->pm.u32AC97_master_volume_mono = (u32)cs_ac97_get(dev,
697 (u8)BA0_AC97_MASTER_VOLUME_MONO);
698 card->pm.u32AC97_pcm_out_volume = (u32)cs_ac97_get(dev,
699 (u8)BA0_AC97_PCM_OUT_VOLUME);
701 cs_ac97_set(dev, (u8)BA0_AC97_MASTER_VOLUME, 0x8000);
702 cs_ac97_set(dev, (u8)BA0_AC97_HEADPHONE_VOLUME, 0x8000);
703 cs_ac97_set(dev, (u8)BA0_AC97_MASTER_VOLUME_MONO, 0x8000);
704 cs_ac97_set(dev, (u8)BA0_AC97_PCM_OUT_VOLUME, 0x8000);
708 cs_ac97_set(dev, (u8)BA0_AC97_MASTER_VOLUME, card->pm.u32AC97_master_volume);
709 cs_ac97_set(dev, (u8)BA0_AC97_HEADPHONE_VOLUME, card->pm.u32AC97_headphone_volume);
710 cs_ac97_set(dev, (u8)BA0_AC97_MASTER_VOLUME_MONO, card->pm.u32AC97_master_volume_mono);
711 cs_ac97_set(dev, (u8)BA0_AC97_PCM_OUT_VOLUME, card->pm.u32AC97_pcm_out_volume);
713 CS_DBGOUT(CS_FUNCTION, 2, printk(KERN_INFO "cs46xx: cs_mute()-\n"));
716 /* set playback sample rate */
717 static unsigned int cs_set_dac_rate(struct cs_state * state, unsigned int rate)
719 struct dmabuf *dmabuf = &state->dmabuf;
720 unsigned int tmp1, tmp2;
721 unsigned int phiIncr;
722 unsigned int correctionPerGOF, correctionPerSec;
725 CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: cs_set_dac_rate()+ %d\n",rate) );
728 * Compute the values used to drive the actual sample rate conversion.
729 * The following formulas are being computed, using inline assembly
730 * since we need to use 64 bit arithmetic to compute the values:
732 * phiIncr = floor((Fs,in * 2^26) / Fs,out)
733 * correctionPerGOF = floor((Fs,in * 2^26 - Fs,out * phiIncr) /
735 * ulCorrectionPerSec = Fs,in * 2^26 - Fs,out * phiIncr -M
736 * GOF_PER_SEC * correctionPerGOF
740 * phiIncr:other = dividend:remainder((Fs,in * 2^26) / Fs,out)
741 * correctionPerGOF:correctionPerSec =
742 * dividend:remainder(ulOther / GOF_PER_SEC)
745 phiIncr = tmp1 / 48000;
746 tmp1 -= phiIncr * 48000;
751 tmp1 -= tmp2 * 48000;
752 correctionPerGOF = tmp1 / GOF_PER_SEC;
753 tmp1 -= correctionPerGOF * GOF_PER_SEC;
754 correctionPerSec = tmp1;
757 * Fill in the SampleRateConverter control block.
760 spin_lock_irqsave(&state->card->lock, flags);
761 cs461x_poke(state->card, BA1_PSRC,
762 ((correctionPerSec << 16) & 0xFFFF0000) | (correctionPerGOF & 0xFFFF));
763 cs461x_poke(state->card, BA1_PPI, phiIncr);
764 spin_unlock_irqrestore(&state->card->lock, flags);
767 CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: cs_set_dac_rate()- %d\n",rate) );
771 /* set recording sample rate */
772 static unsigned int cs_set_adc_rate(struct cs_state * state, unsigned int rate)
774 struct dmabuf *dmabuf = &state->dmabuf;
775 struct cs_card *card = state->card;
776 unsigned int phiIncr, coeffIncr, tmp1, tmp2;
777 unsigned int correctionPerGOF, correctionPerSec, initialDelay;
778 unsigned int frameGroupLength, cnt;
780 CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: cs_set_adc_rate()+ %d\n",rate) );
783 * We can only decimate by up to a factor of 1/9th the hardware rate.
784 * Correct the value if an attempt is made to stray outside that limit.
786 if ((rate * 9) < 48000)
790 * We can not capture at at rate greater than the Input Rate (48000).
791 * Return an error if an attempt is made to stray outside that limit.
797 * Compute the values used to drive the actual sample rate conversion.
798 * The following formulas are being computed, using inline assembly
799 * since we need to use 64 bit arithmetic to compute the values:
801 * coeffIncr = -floor((Fs,out * 2^23) / Fs,in)
802 * phiIncr = floor((Fs,in * 2^26) / Fs,out)
803 * correctionPerGOF = floor((Fs,in * 2^26 - Fs,out * phiIncr) /
805 * correctionPerSec = Fs,in * 2^26 - Fs,out * phiIncr -
806 * GOF_PER_SEC * correctionPerGOF
807 * initialDelay = ceil((24 * Fs,in) / Fs,out)
811 * coeffIncr = neg(dividend((Fs,out * 2^23) / Fs,in))
812 * phiIncr:ulOther = dividend:remainder((Fs,in * 2^26) / Fs,out)
813 * correctionPerGOF:correctionPerSec =
814 * dividend:remainder(ulOther / GOF_PER_SEC)
815 * initialDelay = dividend(((24 * Fs,in) + Fs,out - 1) / Fs,out)
819 coeffIncr = tmp1 / 48000;
820 tmp1 -= coeffIncr * 48000;
823 coeffIncr += tmp1 / 48000;
824 coeffIncr ^= 0xFFFFFFFF;
827 phiIncr = tmp1 / rate;
828 tmp1 -= phiIncr * rate;
834 correctionPerGOF = tmp1 / GOF_PER_SEC;
835 tmp1 -= correctionPerGOF * GOF_PER_SEC;
836 correctionPerSec = tmp1;
837 initialDelay = ((48000 * 24) + rate - 1) / rate;
840 * Fill in the VariDecimate control block.
842 spin_lock_irqsave(&card->lock, flags);
843 cs461x_poke(card, BA1_CSRC,
844 ((correctionPerSec << 16) & 0xFFFF0000) | (correctionPerGOF & 0xFFFF));
845 cs461x_poke(card, BA1_CCI, coeffIncr);
846 cs461x_poke(card, BA1_CD,
847 (((BA1_VARIDEC_BUF_1 + (initialDelay << 2)) << 16) & 0xFFFF0000) | 0x80);
848 cs461x_poke(card, BA1_CPI, phiIncr);
849 spin_unlock_irqrestore(&card->lock, flags);
852 * Figure out the frame group length for the write back task. Basically,
853 * this is just the factors of 24000 (2^6*3*5^3) that are not present in
854 * the output sample rate.
856 frameGroupLength = 1;
857 for (cnt = 2; cnt <= 64; cnt *= 2) {
858 if (((rate / cnt) * cnt) != rate)
859 frameGroupLength *= 2;
861 if (((rate / 3) * 3) != rate) {
862 frameGroupLength *= 3;
864 for (cnt = 5; cnt <= 125; cnt *= 5) {
865 if (((rate / cnt) * cnt) != rate)
866 frameGroupLength *= 5;
870 * Fill in the WriteBack control block.
872 spin_lock_irqsave(&card->lock, flags);
873 cs461x_poke(card, BA1_CFG1, frameGroupLength);
874 cs461x_poke(card, BA1_CFG2, (0x00800000 | frameGroupLength));
875 cs461x_poke(card, BA1_CCST, 0x0000FFFF);
876 cs461x_poke(card, BA1_CSPB, ((65536 * rate) / 24000));
877 cs461x_poke(card, (BA1_CSPB + 4), 0x0000FFFF);
878 spin_unlock_irqrestore(&card->lock, flags);
880 CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: cs_set_adc_rate()- %d\n",rate) );
884 /* prepare channel attributes for playback */
885 static void cs_play_setup(struct cs_state *state)
887 struct dmabuf *dmabuf = &state->dmabuf;
888 struct cs_card *card = state->card;
889 unsigned int tmp, Count, playFormat;
891 CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: cs_play_setup()+\n") );
892 cs461x_poke(card, BA1_PVOL, 0x80008000);
894 cs461x_poke(card, BA1_PBA, virt_to_bus(dmabuf->pbuf));
897 playFormat=cs461x_peek(card, BA1_PFIE);
898 if ((dmabuf->fmt & CS_FMT_STEREO)) {
899 playFormat &= ~DMA_RQ_C2_AC_MONO_TO_STEREO;
903 playFormat |= DMA_RQ_C2_AC_MONO_TO_STEREO;
905 if ((dmabuf->fmt & CS_FMT_16BIT)) {
906 playFormat &= ~(DMA_RQ_C2_AC_8_TO_16_BIT
907 | DMA_RQ_C2_AC_SIGNED_CONVERT);
911 playFormat |= (DMA_RQ_C2_AC_8_TO_16_BIT
912 | DMA_RQ_C2_AC_SIGNED_CONVERT);
914 cs461x_poke(card, BA1_PFIE, playFormat);
916 tmp = cs461x_peek(card, BA1_PDTC);
918 cs461x_poke(card, BA1_PDTC, tmp | --Count);
920 CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: cs_play_setup()-\n") );
924 static struct InitStruct
928 } InitArray[] = { {0x00000040, 0x3fc0000f},
929 {0x0000004c, 0x04800000},
931 {0x000000b3, 0x00000780},
932 {0x000000b7, 0x00000000},
933 {0x000000bc, 0x07800000},
935 {0x000000cd, 0x00800000},
939 * "SetCaptureSPValues()" -- Initialize record task values before each
942 static void SetCaptureSPValues(struct cs_card *card)
945 CS_DBGOUT(CS_FUNCTION, 8, printk("cs46xx: SetCaptureSPValues()+\n") );
946 for(i=0; i<sizeof(InitArray)/sizeof(struct InitStruct); i++)
948 offset = InitArray[i].off*4; /* 8bit to 32bit offset value */
949 cs461x_poke(card, offset, InitArray[i].val );
951 CS_DBGOUT(CS_FUNCTION, 8, printk("cs46xx: SetCaptureSPValues()-\n") );
954 /* prepare channel attributes for recording */
955 static void cs_rec_setup(struct cs_state *state)
957 struct cs_card *card = state->card;
958 struct dmabuf *dmabuf = &state->dmabuf;
959 CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: cs_rec_setup()+\n") );
961 SetCaptureSPValues(card);
964 * set the attenuation to 0dB
966 cs461x_poke(card, BA1_CVOL, 0x80008000);
969 * set the physical address of the capture buffer into the SP
971 cs461x_poke(card, BA1_CBA, virt_to_bus(dmabuf->rawbuf));
973 CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: cs_rec_setup()-\n") );
977 /* get current playback/recording dma buffer pointer (byte offset from LBA),
978 called with spinlock held! */
980 static inline unsigned cs_get_dma_addr(struct cs_state *state)
982 struct dmabuf *dmabuf = &state->dmabuf;
985 if ( (!(dmabuf->enable & DAC_RUNNING)) &&
986 (!(dmabuf->enable & ADC_RUNNING) ) )
988 CS_DBGOUT(CS_ERROR, 2, printk(
989 "cs46xx: ERROR cs_get_dma_addr(): not enabled \n") );
994 * granularity is byte boundary, good part.
996 if(dmabuf->enable & DAC_RUNNING)
998 offset = cs461x_peek(state->card, BA1_PBA);
1000 else /* ADC_RUNNING must be set */
1002 offset = cs461x_peek(state->card, BA1_CBA);
1004 CS_DBGOUT(CS_PARMS | CS_FUNCTION, 9,
1005 printk("cs46xx: cs_get_dma_addr() %d\n",offset) );
1006 offset = (u32)bus_to_virt((unsigned long)offset) - (u32)dmabuf->rawbuf;
1007 CS_DBGOUT(CS_PARMS | CS_FUNCTION, 8,
1008 printk("cs46xx: cs_get_dma_addr()- %d\n",offset) );
1012 static void resync_dma_ptrs(struct cs_state *state)
1014 struct dmabuf *dmabuf;
1016 CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: resync_dma_ptrs()+ \n") );
1019 dmabuf = &state->dmabuf;
1020 dmabuf->hwptr=dmabuf->swptr = 0;
1021 dmabuf->pringbuf = 0;
1023 CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: resync_dma_ptrs()- \n") );
1026 /* Stop recording (lock held) */
1027 static inline void __stop_adc(struct cs_state *state)
1029 struct dmabuf *dmabuf = &state->dmabuf;
1030 struct cs_card *card = state->card;
1033 dmabuf->enable &= ~ADC_RUNNING;
1035 tmp = cs461x_peek(card, BA1_CCTL);
1037 cs461x_poke(card, BA1_CCTL, tmp );
1040 static void stop_adc(struct cs_state *state)
1042 unsigned long flags;
1044 CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: stop_adc()+ \n") );
1045 spin_lock_irqsave(&state->card->lock, flags);
1047 spin_unlock_irqrestore(&state->card->lock, flags);
1048 CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: stop_adc()- \n") );
1051 static void start_adc(struct cs_state *state)
1053 struct dmabuf *dmabuf = &state->dmabuf;
1054 struct cs_card *card = state->card;
1055 unsigned long flags;
1058 spin_lock_irqsave(&card->lock, flags);
1059 if (!(dmabuf->enable & ADC_RUNNING) &&
1060 ((dmabuf->mapped || dmabuf->count < (signed)dmabuf->dmasize)
1061 && dmabuf->ready) &&
1062 ((card->pm.flags & CS46XX_PM_IDLE) ||
1063 (card->pm.flags & CS46XX_PM_RESUMED)) )
1065 dmabuf->enable |= ADC_RUNNING;
1066 cs_set_divisor(dmabuf);
1067 tmp = cs461x_peek(card, BA1_CCTL);
1070 CS_DBGOUT(CS_FUNCTION, 2, printk(
1071 "cs46xx: start_adc() poke 0x%x \n",tmp) );
1072 cs461x_poke(card, BA1_CCTL, tmp);
1074 spin_unlock_irqrestore(&card->lock, flags);
1077 /* stop playback (lock held) */
1078 static inline void __stop_dac(struct cs_state *state)
1080 struct dmabuf *dmabuf = &state->dmabuf;
1081 struct cs_card *card = state->card;
1084 dmabuf->enable &= ~DAC_RUNNING;
1086 tmp=cs461x_peek(card, BA1_PCTL);
1088 cs461x_poke(card, BA1_PCTL, tmp);
1091 static void stop_dac(struct cs_state *state)
1093 unsigned long flags;
1095 CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: stop_dac()+ \n") );
1096 spin_lock_irqsave(&state->card->lock, flags);
1098 spin_unlock_irqrestore(&state->card->lock, flags);
1099 CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: stop_dac()- \n") );
1102 static void start_dac(struct cs_state *state)
1104 struct dmabuf *dmabuf = &state->dmabuf;
1105 struct cs_card *card = state->card;
1106 unsigned long flags;
1109 CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: start_dac()+ \n") );
1110 spin_lock_irqsave(&card->lock, flags);
1111 if (!(dmabuf->enable & DAC_RUNNING) &&
1112 ((dmabuf->mapped || dmabuf->count > 0) && dmabuf->ready) &&
1113 ((card->pm.flags & CS46XX_PM_IDLE) ||
1114 (card->pm.flags & CS46XX_PM_RESUMED)) )
1116 dmabuf->enable |= DAC_RUNNING;
1117 tmp = cs461x_peek(card, BA1_PCTL);
1120 CS_DBGOUT(CS_PARMS, 6, printk(
1121 "cs46xx: start_dac() poke card=%p tmp=0x%.08x addr=%p \n",
1122 card, (unsigned)tmp,
1123 card->ba1.idx[(BA1_PCTL >> 16) & 3]+(BA1_PCTL&0xffff) ) );
1124 cs461x_poke(card, BA1_PCTL, tmp);
1126 spin_unlock_irqrestore(&card->lock, flags);
1127 CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: start_dac()- \n") );
1130 #define DMABUF_MINORDER 1
1133 * allocate DMA buffer, playback and recording buffers are separate.
1135 static int alloc_dmabuf(struct cs_state *state)
1138 struct cs_card *card=state->card;
1139 struct dmabuf *dmabuf = &state->dmabuf;
1140 void *rawbuf = NULL;
1141 void *tmpbuff = NULL;
1143 struct page *map, *mapend;
1146 dmabuf->ready = dmabuf->mapped = 0;
1149 * check for order within limits, but do not overwrite value.
1151 if((defaultorder > 1) && (defaultorder < 12))
1156 for (order = df; order >= DMABUF_MINORDER; order--)
1157 if ( (rawbuf = (void *) pci_alloc_consistent(
1158 card->pci_dev, PAGE_SIZE << order, &dmabuf->dmaaddr)))
1161 CS_DBGOUT(CS_ERROR, 1, printk(KERN_ERR
1162 "cs46xx: alloc_dmabuf(): unable to allocate rawbuf\n"));
1165 dmabuf->buforder = order;
1166 dmabuf->rawbuf = rawbuf;
1167 // Now mark the pages as reserved; otherwise the
1168 // remap_pfn_range() in cs46xx_mmap doesn't work.
1169 // 1. get index to last page in mem_map array for rawbuf.
1170 mapend = virt_to_page(dmabuf->rawbuf +
1171 (PAGE_SIZE << dmabuf->buforder) - 1);
1173 // 2. mark each physical page in range as 'reserved'.
1174 for (map = virt_to_page(dmabuf->rawbuf); map <= mapend; map++)
1175 cs4x_mem_map_reserve(map);
1177 CS_DBGOUT(CS_PARMS, 9, printk("cs46xx: alloc_dmabuf(): allocated %ld (order = %d) bytes at %p\n",
1178 PAGE_SIZE << order, order, rawbuf) );
1181 * only allocate the conversion buffer for the ADC
1183 if(dmabuf->type == CS_TYPE_DAC)
1185 dmabuf->tmpbuff = NULL;
1186 dmabuf->buforder_tmpbuff = 0;
1190 * now the temp buffer for 16/8 conversions
1193 tmpbuff = (void *) pci_alloc_consistent(
1194 card->pci_dev, PAGE_SIZE << order, &dmabuf->dmaaddr_tmpbuff);
1198 CS_DBGOUT(CS_PARMS, 9, printk("cs46xx: allocated %ld (order = %d) bytes at %p\n",
1199 PAGE_SIZE << order, order, tmpbuff) );
1201 dmabuf->tmpbuff = tmpbuff;
1202 dmabuf->buforder_tmpbuff = order;
1204 // Now mark the pages as reserved; otherwise the
1205 // remap_pfn_range() in cs46xx_mmap doesn't work.
1206 // 1. get index to last page in mem_map array for rawbuf.
1207 mapend = virt_to_page(dmabuf->tmpbuff +
1208 (PAGE_SIZE << dmabuf->buforder_tmpbuff) - 1);
1210 // 2. mark each physical page in range as 'reserved'.
1211 for (map = virt_to_page(dmabuf->tmpbuff); map <= mapend; map++)
1212 cs4x_mem_map_reserve(map);
1216 /* free DMA buffer */
1217 static void dealloc_dmabuf(struct cs_state *state)
1219 struct dmabuf *dmabuf = &state->dmabuf;
1220 struct page *map, *mapend;
1222 if (dmabuf->rawbuf) {
1223 // Undo prog_dmabuf()'s marking the pages as reserved
1224 mapend = virt_to_page(dmabuf->rawbuf +
1225 (PAGE_SIZE << dmabuf->buforder) - 1);
1226 for (map = virt_to_page(dmabuf->rawbuf); map <= mapend; map++)
1227 cs4x_mem_map_unreserve(map);
1228 free_dmabuf(state->card, dmabuf);
1231 if (dmabuf->tmpbuff) {
1232 // Undo prog_dmabuf()'s marking the pages as reserved
1233 mapend = virt_to_page(dmabuf->tmpbuff +
1234 (PAGE_SIZE << dmabuf->buforder_tmpbuff) - 1);
1235 for (map = virt_to_page(dmabuf->tmpbuff); map <= mapend; map++)
1236 cs4x_mem_map_unreserve(map);
1237 free_dmabuf2(state->card, dmabuf);
1240 dmabuf->rawbuf = NULL;
1241 dmabuf->tmpbuff = NULL;
1242 dmabuf->mapped = dmabuf->ready = 0;
1246 static int __prog_dmabuf(struct cs_state *state)
1248 struct dmabuf *dmabuf = &state->dmabuf;
1249 unsigned long flags;
1250 unsigned long allocated_pages, allocated_bytes;
1251 unsigned long tmp1, tmp2, fmt=0;
1252 unsigned long *ptmp = (unsigned long *) dmabuf->pbuf;
1253 unsigned long SGarray[9], nSGpages=0;
1256 CS_DBGOUT(CS_FUNCTION, 4, printk("cs46xx: prog_dmabuf()+ \n"));
1258 * check for CAPTURE and use only non-sg for initial release
1260 if(dmabuf->type == CS_TYPE_ADC)
1262 CS_DBGOUT(CS_FUNCTION, 4, printk("cs46xx: prog_dmabuf() ADC\n"));
1264 * add in non-sg support for capture.
1266 spin_lock_irqsave(&state->card->lock, flags);
1267 /* add code to reset the rawbuf memory. TRW */
1268 resync_dma_ptrs(state);
1269 dmabuf->total_bytes = dmabuf->blocks = 0;
1270 dmabuf->count = dmabuf->error = dmabuf->underrun = 0;
1274 spin_unlock_irqrestore(&state->card->lock, flags);
1276 /* allocate DMA buffer if not allocated yet */
1277 if (!dmabuf->rawbuf || !dmabuf->tmpbuff)
1278 if ((ret = alloc_dmabuf(state)))
1281 * static image only supports 16Bit signed, stereo - hard code fmt
1283 fmt = CS_FMT_16BIT | CS_FMT_STEREO;
1285 dmabuf->numfrag = 2;
1286 dmabuf->fragsize = 2048;
1287 dmabuf->fragsamples = 2048 >> sample_shift[fmt];
1288 dmabuf->dmasize = 4096;
1289 dmabuf->fragshift = 11;
1291 memset(dmabuf->rawbuf, (fmt & CS_FMT_16BIT) ? 0 : 0x80,
1293 memset(dmabuf->tmpbuff, (fmt & CS_FMT_16BIT) ? 0 : 0x80,
1294 PAGE_SIZE<<dmabuf->buforder_tmpbuff);
1297 * Now set up the ring
1300 spin_lock_irqsave(&state->card->lock, flags);
1301 cs_rec_setup(state);
1302 spin_unlock_irqrestore(&state->card->lock, flags);
1304 /* set the ready flag for the dma buffer */
1307 CS_DBGOUT(CS_PARMS, 4, printk(
1308 "cs46xx: prog_dmabuf(): CAPTURE rate=%d fmt=0x%x numfrag=%d "
1309 "fragsize=%d dmasize=%d\n",
1310 dmabuf->rate, dmabuf->fmt, dmabuf->numfrag,
1311 dmabuf->fragsize, dmabuf->dmasize) );
1313 CS_DBGOUT(CS_FUNCTION, 4, printk("cs46xx: prog_dmabuf()- 0 \n"));
1316 else if (dmabuf->type == CS_TYPE_DAC)
1321 CS_DBGOUT(CS_FUNCTION, 4, printk("cs46xx: prog_dmabuf() DAC\n"));
1322 spin_lock_irqsave(&state->card->lock, flags);
1323 resync_dma_ptrs(state);
1324 dmabuf->total_bytes = dmabuf->blocks = 0;
1325 dmabuf->count = dmabuf->error = dmabuf->underrun = 0;
1329 spin_unlock_irqrestore(&state->card->lock, flags);
1331 /* allocate DMA buffer if not allocated yet */
1332 if (!dmabuf->rawbuf)
1333 if ((ret = alloc_dmabuf(state)))
1336 allocated_pages = 1 << dmabuf->buforder;
1337 allocated_bytes = allocated_pages*PAGE_SIZE;
1339 if(allocated_pages < 2)
1341 CS_DBGOUT(CS_FUNCTION, 4, printk(
1342 "cs46xx: prog_dmabuf() Error: allocated_pages too small (%d)\n",
1343 (unsigned)allocated_pages));
1347 /* Use all the pages allocated, fragsize 4k. */
1348 /* Use 'pbuf' for S/G page map table. */
1349 dmabuf->SGok = 1; /* Use S/G. */
1351 nSGpages = allocated_bytes/4096; /* S/G pages always 4k. */
1353 /* Set up S/G variables. */
1354 *ptmp = virt_to_bus(dmabuf->rawbuf);
1355 *(ptmp+1) = 0x00000008;
1356 for(tmp1= 1; tmp1 < nSGpages; tmp1++) {
1357 *(ptmp+2*tmp1) = virt_to_bus( (dmabuf->rawbuf)+4096*tmp1);
1358 if( tmp1 == nSGpages-1)
1361 tmp2 = 0x80000000+8*(tmp1+1);
1362 *(ptmp+2*tmp1+1) = tmp2;
1364 SGarray[0] = 0x82c0200d;
1365 SGarray[1] = 0xffff0000;
1367 SGarray[3] = 0x00010600;
1368 SGarray[4] = *(ptmp+2);
1369 SGarray[5] = 0x80000010;
1371 SGarray[7] = *(ptmp+2);
1372 SGarray[8] = (virt_to_bus(dmabuf->pbuf) & 0xffff000) | 0x10;
1375 dmabuf->numfrag = nSGpages;
1376 dmabuf->fragsize = 4096;
1377 dmabuf->fragsamples = 4096 >> sample_shift[dmabuf->fmt];
1378 dmabuf->fragshift = 12;
1379 dmabuf->dmasize = dmabuf->numfrag*4096;
1382 SGarray[0] = 0xf2c0000f;
1383 SGarray[1] = 0x00000200;
1385 SGarray[3] = 0x00010600;
1386 SGarray[4]=SGarray[5]=SGarray[6]=SGarray[7]=SGarray[8] = 0;
1387 dmabuf->numfrag = 2;
1388 dmabuf->fragsize = 2048;
1389 dmabuf->fragsamples = 2048 >> sample_shift[dmabuf->fmt];
1390 dmabuf->dmasize = 4096;
1391 dmabuf->fragshift = 11;
1393 for(tmp1 = 0; tmp1 < sizeof(SGarray)/4; tmp1++)
1394 cs461x_poke( state->card, BA1_PDTC+tmp1*4, SGarray[tmp1]);
1396 memset(dmabuf->rawbuf, (dmabuf->fmt & CS_FMT_16BIT) ? 0 : 0x80,
1400 * Now set up the ring
1403 spin_lock_irqsave(&state->card->lock, flags);
1404 cs_play_setup(state);
1405 spin_unlock_irqrestore(&state->card->lock, flags);
1407 /* set the ready flag for the dma buffer */
1410 CS_DBGOUT(CS_PARMS, 4, printk(
1411 "cs46xx: prog_dmabuf(): PLAYBACK rate=%d fmt=0x%x numfrag=%d "
1412 "fragsize=%d dmasize=%d\n",
1413 dmabuf->rate, dmabuf->fmt, dmabuf->numfrag,
1414 dmabuf->fragsize, dmabuf->dmasize) );
1416 CS_DBGOUT(CS_FUNCTION, 4, printk("cs46xx: prog_dmabuf()- \n"));
1421 CS_DBGOUT(CS_FUNCTION, 4, printk("cs46xx: prog_dmabuf()- Invalid Type %d\n",
1427 static int prog_dmabuf(struct cs_state *state)
1432 ret = __prog_dmabuf(state);
1438 static void cs_clear_tail(struct cs_state *state)
1442 static int drain_dac(struct cs_state *state, int nonblock)
1444 DECLARE_WAITQUEUE(wait, current);
1445 struct dmabuf *dmabuf = &state->dmabuf;
1446 struct cs_card *card=state->card;
1447 unsigned long flags;
1451 CS_DBGOUT(CS_FUNCTION, 4, printk("cs46xx: drain_dac()+ \n"));
1452 if (dmabuf->mapped || !dmabuf->ready)
1454 CS_DBGOUT(CS_FUNCTION, 4, printk("cs46xx: drain_dac()- 0, not ready\n"));
1458 add_wait_queue(&dmabuf->wait, &wait);
1460 /* It seems that we have to set the current state to TASK_INTERRUPTIBLE
1461 every time to make the process really go to sleep */
1462 current->state = TASK_INTERRUPTIBLE;
1464 spin_lock_irqsave(&state->card->lock, flags);
1465 count = dmabuf->count;
1466 spin_unlock_irqrestore(&state->card->lock, flags);
1471 if (signal_pending(current))
1475 remove_wait_queue(&dmabuf->wait, &wait);
1476 current->state = TASK_RUNNING;
1480 tmo = (dmabuf->dmasize * HZ) / dmabuf->rate;
1481 tmo >>= sample_shift[dmabuf->fmt];
1482 tmo += (2048*HZ)/dmabuf->rate;
1484 if (!schedule_timeout(tmo ? tmo : 1) && tmo){
1485 printk(KERN_ERR "cs46xx: drain_dac, dma timeout? %d\n", count);
1489 remove_wait_queue(&dmabuf->wait, &wait);
1490 current->state = TASK_RUNNING;
1491 if (signal_pending(current))
1493 CS_DBGOUT(CS_FUNCTION, 4, printk("cs46xx: drain_dac()- -ERESTARTSYS\n"));
1495 * set to silence and let that clear the fifos.
1497 cs461x_clear_serial_FIFOs(card, CS_TYPE_DAC);
1498 return -ERESTARTSYS;
1501 CS_DBGOUT(CS_FUNCTION, 4, printk("cs46xx: drain_dac()- 0\n"));
1506 /* update buffer manangement pointers, especially, dmabuf->count and dmabuf->hwptr */
1507 static void cs_update_ptr(struct cs_card *card, int wake)
1509 struct cs_state *state;
1510 struct dmabuf *dmabuf;
1514 /* error handling and process wake up for ADC */
1515 state = card->states[0];
1518 dmabuf = &state->dmabuf;
1519 if (dmabuf->enable & ADC_RUNNING) {
1520 /* update hardware pointer */
1521 hwptr = cs_get_dma_addr(state);
1523 diff = (dmabuf->dmasize + hwptr - dmabuf->hwptr) % dmabuf->dmasize;
1524 CS_DBGOUT(CS_PARMS, 9, printk(
1525 "cs46xx: cs_update_ptr()+ ADC hwptr=%d diff=%d\n",
1527 dmabuf->hwptr = hwptr;
1528 dmabuf->total_bytes += diff;
1529 dmabuf->count += diff;
1530 if (dmabuf->count > dmabuf->dmasize)
1531 dmabuf->count = dmabuf->dmasize;
1535 if (wake && dmabuf->count >= (signed)dmabuf->fragsize)
1536 wake_up(&dmabuf->wait);
1539 if (wake && dmabuf->count > 0)
1540 wake_up(&dmabuf->wait);
1548 state = card->states[1];
1551 dmabuf = &state->dmabuf;
1552 /* error handling and process wake up for DAC */
1553 if (dmabuf->enable & DAC_RUNNING) {
1554 /* update hardware pointer */
1555 hwptr = cs_get_dma_addr(state);
1557 diff = (dmabuf->dmasize + hwptr - dmabuf->hwptr) % dmabuf->dmasize;
1558 CS_DBGOUT(CS_PARMS, 9, printk(
1559 "cs46xx: cs_update_ptr()+ DAC hwptr=%d diff=%d\n",
1561 dmabuf->hwptr = hwptr;
1562 dmabuf->total_bytes += diff;
1563 if (dmabuf->mapped) {
1564 dmabuf->count += diff;
1565 if (wake && dmabuf->count >= (signed)dmabuf->fragsize)
1566 wake_up(&dmabuf->wait);
1568 * other drivers use fragsize, but don't see any sense
1569 * in that, since dmasize is the buffer asked for
1572 if( dmabuf->count > dmabuf->dmasize)
1573 dmabuf->count &= dmabuf->dmasize-1;
1575 dmabuf->count -= diff;
1577 * backfill with silence and clear out the last
1578 * "diff" number of bytes.
1582 memset(dmabuf->rawbuf + hwptr - diff,
1583 (dmabuf->fmt & CS_FMT_16BIT) ? 0 : 0x80, diff);
1587 memset(dmabuf->rawbuf,
1588 (dmabuf->fmt & CS_FMT_16BIT) ? 0 : 0x80,
1590 memset((char *)dmabuf->rawbuf +
1591 dmabuf->dmasize + hwptr - diff,
1592 (dmabuf->fmt & CS_FMT_16BIT) ? 0 : 0x80,
1596 if (dmabuf->count < 0 || dmabuf->count > dmabuf->dmasize) {
1597 CS_DBGOUT(CS_ERROR, 2, printk(KERN_INFO
1598 "cs46xx: ERROR DAC count<0 or count > dmasize (%d)\n",
1601 * buffer underrun or buffer overrun, reset the
1602 * count of bytes written back to 0.
1604 if(dmabuf->count < 0)
1609 if (wake && dmabuf->count < (signed)dmabuf->dmasize/2)
1610 wake_up(&dmabuf->wait);
1617 /* hold spinlock for the following! */
1618 static void cs_handle_midi(struct cs_card *card)
1625 while (!(cs461x_peekBA0(card, BA0_MIDSR) & MIDSR_RBE)) {
1626 ch = cs461x_peekBA0(card, BA0_MIDRP);
1627 if (card->midi.icnt < CS_MIDIINBUF) {
1628 card->midi.ibuf[card->midi.iwr] = ch;
1629 card->midi.iwr = (card->midi.iwr + 1) % CS_MIDIINBUF;
1635 wake_up(&card->midi.iwait);
1637 while (!(cs461x_peekBA0(card, BA0_MIDSR) & MIDSR_TBF) && card->midi.ocnt > 0) {
1638 temp1 = ( card->midi.obuf[card->midi.ord] ) & 0x000000ff;
1639 cs461x_pokeBA0(card, BA0_MIDWP,temp1);
1640 card->midi.ord = (card->midi.ord + 1) % CS_MIDIOUTBUF;
1642 if (card->midi.ocnt < CS_MIDIOUTBUF-16)
1646 wake_up(&card->midi.owait);
1649 static irqreturn_t cs_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1651 struct cs_card *card = (struct cs_card *)dev_id;
1652 /* Single channel card */
1653 struct cs_state *recstate = card->channel[0].state;
1654 struct cs_state *playstate = card->channel[1].state;
1657 CS_DBGOUT(CS_INTERRUPT, 9, printk("cs46xx: cs_interrupt()+ \n"));
1659 spin_lock(&card->lock);
1661 status = cs461x_peekBA0(card, BA0_HISR);
1663 if ((status & 0x7fffffff) == 0)
1665 cs461x_pokeBA0(card, BA0_HICR, HICR_CHGM|HICR_IEV);
1666 spin_unlock(&card->lock);
1667 return IRQ_HANDLED; /* Might be IRQ_NONE.. */
1671 * check for playback or capture interrupt only
1673 if( ((status & HISR_VC0) && playstate && playstate->dmabuf.ready) ||
1674 (((status & HISR_VC1) && recstate && recstate->dmabuf.ready)) )
1676 CS_DBGOUT(CS_INTERRUPT, 8, printk(
1677 "cs46xx: cs_interrupt() interrupt bit(s) set (0x%x)\n",status));
1678 cs_update_ptr(card, CS_TRUE);
1681 if( status & HISR_MIDI )
1682 cs_handle_midi(card);
1685 cs461x_pokeBA0(card, BA0_HICR, HICR_CHGM|HICR_IEV);
1686 spin_unlock(&card->lock);
1687 CS_DBGOUT(CS_INTERRUPT, 9, printk("cs46xx: cs_interrupt()- \n"));
1692 /**********************************************************************/
1694 static ssize_t cs_midi_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
1696 struct cs_card *card = (struct cs_card *)file->private_data;
1698 unsigned long flags;
1702 if (!access_ok(VERIFY_WRITE, buffer, count))
1706 spin_lock_irqsave(&card->lock, flags);
1707 ptr = card->midi.ird;
1708 cnt = CS_MIDIINBUF - ptr;
1709 if (card->midi.icnt < cnt)
1710 cnt = card->midi.icnt;
1711 spin_unlock_irqrestore(&card->lock, flags);
1715 if (file->f_flags & O_NONBLOCK)
1716 return ret ? ret : -EAGAIN;
1717 interruptible_sleep_on(&card->midi.iwait);
1718 if (signal_pending(current))
1719 return ret ? ret : -ERESTARTSYS;
1722 if (copy_to_user(buffer, card->midi.ibuf + ptr, cnt))
1723 return ret ? ret : -EFAULT;
1724 ptr = (ptr + cnt) % CS_MIDIINBUF;
1725 spin_lock_irqsave(&card->lock, flags);
1726 card->midi.ird = ptr;
1727 card->midi.icnt -= cnt;
1728 spin_unlock_irqrestore(&card->lock, flags);
1737 static ssize_t cs_midi_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
1739 struct cs_card *card = (struct cs_card *)file->private_data;
1741 unsigned long flags;
1745 if (!access_ok(VERIFY_READ, buffer, count))
1749 spin_lock_irqsave(&card->lock, flags);
1750 ptr = card->midi.owr;
1751 cnt = CS_MIDIOUTBUF - ptr;
1752 if (card->midi.ocnt + cnt > CS_MIDIOUTBUF)
1753 cnt = CS_MIDIOUTBUF - card->midi.ocnt;
1755 cs_handle_midi(card);
1756 spin_unlock_irqrestore(&card->lock, flags);
1760 if (file->f_flags & O_NONBLOCK)
1761 return ret ? ret : -EAGAIN;
1762 interruptible_sleep_on(&card->midi.owait);
1763 if (signal_pending(current))
1764 return ret ? ret : -ERESTARTSYS;
1767 if (copy_from_user(card->midi.obuf + ptr, buffer, cnt))
1768 return ret ? ret : -EFAULT;
1769 ptr = (ptr + cnt) % CS_MIDIOUTBUF;
1770 spin_lock_irqsave(&card->lock, flags);
1771 card->midi.owr = ptr;
1772 card->midi.ocnt += cnt;
1773 spin_unlock_irqrestore(&card->lock, flags);
1777 spin_lock_irqsave(&card->lock, flags);
1778 cs_handle_midi(card);
1779 spin_unlock_irqrestore(&card->lock, flags);
1785 static unsigned int cs_midi_poll(struct file *file, struct poll_table_struct *wait)
1787 struct cs_card *card = (struct cs_card *)file->private_data;
1788 unsigned long flags;
1789 unsigned int mask = 0;
1791 if (file->f_flags & FMODE_WRITE)
1792 poll_wait(file, &card->midi.owait, wait);
1793 if (file->f_flags & FMODE_READ)
1794 poll_wait(file, &card->midi.iwait, wait);
1795 spin_lock_irqsave(&card->lock, flags);
1796 if (file->f_flags & FMODE_READ) {
1797 if (card->midi.icnt > 0)
1798 mask |= POLLIN | POLLRDNORM;
1800 if (file->f_flags & FMODE_WRITE) {
1801 if (card->midi.ocnt < CS_MIDIOUTBUF)
1802 mask |= POLLOUT | POLLWRNORM;
1804 spin_unlock_irqrestore(&card->lock, flags);
1809 static int cs_midi_open(struct inode *inode, struct file *file)
1811 unsigned int minor = iminor(inode);
1812 struct cs_card *card=NULL;
1813 unsigned long flags;
1814 struct list_head *entry;
1816 list_for_each(entry, &cs46xx_devs)
1818 card = list_entry(entry, struct cs_card, list);
1819 if (card->dev_midi == minor)
1823 if (entry == &cs46xx_devs)
1827 CS_DBGOUT(CS_FUNCTION | CS_OPEN, 2, printk(KERN_INFO
1828 "cs46xx: cs46xx_midi_open(): Error - unable to find card struct\n"));
1832 file->private_data = card;
1833 /* wait for device to become free */
1834 down(&card->midi.open_sem);
1835 while (card->midi.open_mode & file->f_mode) {
1836 if (file->f_flags & O_NONBLOCK) {
1837 up(&card->midi.open_sem);
1840 up(&card->midi.open_sem);
1841 interruptible_sleep_on(&card->midi.open_wait);
1842 if (signal_pending(current))
1843 return -ERESTARTSYS;
1844 down(&card->midi.open_sem);
1846 spin_lock_irqsave(&card->midi.lock, flags);
1847 if (!(card->midi.open_mode & (FMODE_READ | FMODE_WRITE))) {
1848 card->midi.ird = card->midi.iwr = card->midi.icnt = 0;
1849 card->midi.ord = card->midi.owr = card->midi.ocnt = 0;
1850 card->midi.ird = card->midi.iwr = card->midi.icnt = 0;
1851 cs461x_pokeBA0(card, BA0_MIDCR, 0x0000000f); /* Enable xmit, rcv. */
1852 cs461x_pokeBA0(card, BA0_HICR, HICR_IEV | HICR_CHGM); /* Enable interrupts */
1854 if (file->f_mode & FMODE_READ) {
1855 card->midi.ird = card->midi.iwr = card->midi.icnt = 0;
1857 if (file->f_mode & FMODE_WRITE) {
1858 card->midi.ord = card->midi.owr = card->midi.ocnt = 0;
1860 spin_unlock_irqrestore(&card->midi.lock, flags);
1861 card->midi.open_mode |= (file->f_mode & (FMODE_READ | FMODE_WRITE));
1862 up(&card->midi.open_sem);
1867 static int cs_midi_release(struct inode *inode, struct file *file)
1869 struct cs_card *card = (struct cs_card *)file->private_data;
1870 DECLARE_WAITQUEUE(wait, current);
1871 unsigned long flags;
1872 unsigned count, tmo;
1874 if (file->f_mode & FMODE_WRITE) {
1875 current->state = TASK_INTERRUPTIBLE;
1876 add_wait_queue(&card->midi.owait, &wait);
1878 spin_lock_irqsave(&card->midi.lock, flags);
1879 count = card->midi.ocnt;
1880 spin_unlock_irqrestore(&card->midi.lock, flags);
1883 if (signal_pending(current))
1885 if (file->f_flags & O_NONBLOCK)
1887 tmo = (count * HZ) / 3100;
1888 if (!schedule_timeout(tmo ? : 1) && tmo)
1889 printk(KERN_DEBUG "cs46xx: midi timed out??\n");
1891 remove_wait_queue(&card->midi.owait, &wait);
1892 current->state = TASK_RUNNING;
1894 down(&card->midi.open_sem);
1895 card->midi.open_mode &= (~(file->f_mode & (FMODE_READ | FMODE_WRITE)));
1896 up(&card->midi.open_sem);
1897 wake_up(&card->midi.open_wait);
1902 * Midi file operations struct.
1904 static /*const*/ struct file_operations cs_midi_fops = {
1905 CS_OWNER CS_THIS_MODULE
1906 .llseek = no_llseek,
1907 .read = cs_midi_read,
1908 .write = cs_midi_write,
1909 .poll = cs_midi_poll,
1910 .open = cs_midi_open,
1911 .release = cs_midi_release,
1916 * CopySamples copies 16-bit stereo signed samples from the source to the
1917 * destination, possibly converting down to unsigned 8-bit and/or mono.
1918 * count specifies the number of output bytes to write.
1922 * dst - Pointer to a destination buffer.
1923 * src - Pointer to a source buffer
1924 * count - The number of bytes to copy into the destination buffer.
1925 * fmt - CS_FMT_16BIT and/or CS_FMT_STEREO bits
1926 * dmabuf - pointer to the dma buffer structure
1928 * NOTES: only call this routine if the output desired is not 16 Signed Stereo
1932 static void CopySamples(char *dst, char *src, int count, unsigned fmt,
1933 struct dmabuf *dmabuf)
1937 s16 *psSrc=(s16 *)src;
1938 s16 *psDst=(s16 *)dst;
1939 u8 *pucDst=(u8 *)dst;
1941 CS_DBGOUT(CS_FUNCTION, 2, printk(KERN_INFO "cs46xx: CopySamples()+ ") );
1942 CS_DBGOUT(CS_WAVE_READ, 8, printk(KERN_INFO
1943 " dst=%p src=%p count=%d fmt=0x%x\n",
1944 dst,src,count,fmt) );
1947 * See if the data should be output as 8-bit unsigned stereo.
1949 if((fmt & CS_FMT_STEREO) && !(fmt & CS_FMT_16BIT))
1952 * Convert each 16-bit signed stereo sample to 8-bit unsigned
1953 * stereo using rounding.
1959 *(pucDst++) = (u8)(((s16)(*psSrc++) + (s16)0x8000) >> 8);
1963 * See if the data should be output at 8-bit unsigned mono.
1965 else if(!(fmt & CS_FMT_STEREO) && !(fmt & CS_FMT_16BIT))
1968 * Convert each 16-bit signed stereo sample to 8-bit unsigned
1969 * mono using averaging and rounding.
1975 s32AudioSample = ((*psSrc)+(*(psSrc + 1)))/2 + (s32)0x80;
1976 if(s32AudioSample > 0x7fff)
1977 s32AudioSample = 0x7fff;
1978 *(pucDst++) = (u8)(((s16)s32AudioSample + (s16)0x8000) >> 8);
1983 * See if the data should be output at 16-bit signed mono.
1985 else if(!(fmt & CS_FMT_STEREO) && (fmt & CS_FMT_16BIT))
1988 * Convert each 16-bit signed stereo sample to 16-bit signed
1989 * mono using averaging.
1995 *(psDst++) = (s16)((*psSrc)+(*(psSrc + 1)))/2;
2003 * replacement for the standard copy_to_user, to allow for a conversion from
2004 * 16 bit to 8 bit and from stereo to mono, if the record conversion is active.
2005 * The current CS46xx/CS4280 static image only records in 16bit unsigned Stereo,
2006 * so we convert from any of the other format combinations.
2008 static unsigned cs_copy_to_user(
2015 struct dmabuf *dmabuf = &s->dmabuf;
2016 void *src = hwsrc; /* default to the standard destination buffer addr */
2018 CS_DBGOUT(CS_FUNCTION, 6, printk(KERN_INFO
2019 "cs_copy_to_user()+ fmt=0x%x cnt=%d dest=%p\n",
2020 dmabuf->fmt,(unsigned)cnt,dest) );
2022 if(cnt > dmabuf->dmasize)
2024 cnt = dmabuf->dmasize;
2031 if(dmabuf->divisor != 1)
2033 if(!dmabuf->tmpbuff)
2035 *copied = cnt/dmabuf->divisor;
2039 CopySamples((char *)dmabuf->tmpbuff, (char *)hwsrc, cnt,
2040 dmabuf->fmt, dmabuf);
2041 src = dmabuf->tmpbuff;
2042 cnt = cnt/dmabuf->divisor;
2044 if (copy_to_user(dest, src, cnt))
2046 CS_DBGOUT(CS_FUNCTION, 2, printk(KERN_ERR
2047 "cs46xx: cs_copy_to_user()- fault dest=%p src=%p cnt=%d\n",
2053 CS_DBGOUT(CS_FUNCTION, 2, printk(KERN_INFO
2054 "cs46xx: cs_copy_to_user()- copied bytes is %d \n",cnt) );
2058 /* in this loop, dmabuf.count signifies the amount of data that is waiting to be copied to
2059 the user's buffer. it is filled by the dma machine and drained by this loop. */
2060 static ssize_t cs_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
2062 struct cs_card *card = (struct cs_card *) file->private_data;
2063 struct cs_state *state;
2064 DECLARE_WAITQUEUE(wait, current);
2065 struct dmabuf *dmabuf;
2067 unsigned long flags;
2072 CS_DBGOUT(CS_WAVE_READ | CS_FUNCTION, 4,
2073 printk("cs46xx: cs_read()+ %zd\n",count) );
2074 state = (struct cs_state *)card->states[0];
2077 dmabuf = &state->dmabuf;
2081 if (!access_ok(VERIFY_WRITE, buffer, count))
2085 if (!dmabuf->ready && (ret = __prog_dmabuf(state)))
2088 add_wait_queue(&state->dmabuf.wait, &wait);
2090 while(!(card->pm.flags & CS46XX_PM_IDLE))
2093 if (signal_pending(current)) {
2094 if(!ret) ret = -ERESTARTSYS;
2098 spin_lock_irqsave(&state->card->lock, flags);
2099 swptr = dmabuf->swptr;
2100 cnt = dmabuf->dmasize - swptr;
2101 if (dmabuf->count < cnt)
2102 cnt = dmabuf->count;
2104 __set_current_state(TASK_INTERRUPTIBLE);
2105 spin_unlock_irqrestore(&state->card->lock, flags);
2107 if (cnt > (count * dmabuf->divisor))
2108 cnt = count * dmabuf->divisor;
2110 /* buffer is empty, start the dma machine and wait for data to be
2113 if (file->f_flags & O_NONBLOCK) {
2114 if (!ret) ret = -EAGAIN;
2119 if (signal_pending(current)) {
2120 if(!ret) ret = -ERESTARTSYS;
2133 CS_DBGOUT(CS_WAVE_READ, 2, printk(KERN_INFO
2134 "_read() copy_to cnt=%d count=%zd ", cnt,count) );
2135 CS_DBGOUT(CS_WAVE_READ, 8, printk(KERN_INFO
2136 " .dmasize=%d .count=%d buffer=%p ret=%zd\n",
2137 dmabuf->dmasize,dmabuf->count,buffer,ret) );
2139 if (cs_copy_to_user(state, buffer,
2140 (char *)dmabuf->rawbuf + swptr, cnt, &copied))
2142 if (!ret) ret = -EFAULT;
2145 swptr = (swptr + cnt) % dmabuf->dmasize;
2146 spin_lock_irqsave(&card->lock, flags);
2147 dmabuf->swptr = swptr;
2148 dmabuf->count -= cnt;
2149 spin_unlock_irqrestore(&card->lock, flags);
2156 remove_wait_queue(&state->dmabuf.wait, &wait);
2159 set_current_state(TASK_RUNNING);
2160 CS_DBGOUT(CS_WAVE_READ | CS_FUNCTION, 4,
2161 printk("cs46xx: cs_read()- %zd\n",ret) );
2165 /* in this loop, dmabuf.count signifies the amount of data that is waiting to be dma to
2166 the soundcard. it is drained by the dma machine and filled by this loop. */
2167 static ssize_t cs_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
2169 struct cs_card *card = (struct cs_card *) file->private_data;
2170 struct cs_state *state;
2171 DECLARE_WAITQUEUE(wait, current);
2172 struct dmabuf *dmabuf;
2174 unsigned long flags;
2178 CS_DBGOUT(CS_WAVE_WRITE | CS_FUNCTION, 4,
2179 printk("cs46xx: cs_write called, count = %zd\n", count) );
2180 state = (struct cs_state *)card->states[1];
2183 if (!access_ok(VERIFY_READ, buffer, count))
2185 dmabuf = &state->dmabuf;
2194 if (!dmabuf->ready && (ret = __prog_dmabuf(state)))
2196 add_wait_queue(&state->dmabuf.wait, &wait);
2199 * Start the loop to read from the user's buffer and write to the dma buffer.
2200 * check for PM events and underrun/overrun in the loop.
2203 while(!(card->pm.flags & CS46XX_PM_IDLE))
2206 if (signal_pending(current)) {
2207 if(!ret) ret = -ERESTARTSYS;
2211 spin_lock_irqsave(&state->card->lock, flags);
2212 if (dmabuf->count < 0) {
2213 /* buffer underrun, we are recovering from sleep_on_timeout,
2214 resync hwptr and swptr */
2216 dmabuf->swptr = dmabuf->hwptr;
2218 if (dmabuf->underrun)
2220 dmabuf->underrun = 0;
2221 dmabuf->hwptr = cs_get_dma_addr(state);
2222 dmabuf->swptr = dmabuf->hwptr;
2225 swptr = dmabuf->swptr;
2226 cnt = dmabuf->dmasize - swptr;
2227 if (dmabuf->count + cnt > dmabuf->dmasize)
2228 cnt = dmabuf->dmasize - dmabuf->count;
2230 __set_current_state(TASK_INTERRUPTIBLE);
2231 spin_unlock_irqrestore(&state->card->lock, flags);
2236 /* buffer is full, start the dma machine and wait for data to be
2239 if (file->f_flags & O_NONBLOCK) {
2240 if (!ret) ret = -EAGAIN;
2245 if (signal_pending(current)) {
2246 if(!ret) ret = -ERESTARTSYS;
2258 if (copy_from_user(dmabuf->rawbuf + swptr, buffer, cnt)) {
2259 if (!ret) ret = -EFAULT;
2262 spin_lock_irqsave(&state->card->lock, flags);
2263 swptr = (swptr + cnt) % dmabuf->dmasize;
2264 dmabuf->swptr = swptr;
2265 dmabuf->count += cnt;
2266 if(dmabuf->count > dmabuf->dmasize)
2268 CS_DBGOUT(CS_WAVE_WRITE | CS_ERROR, 2, printk(
2269 "cs46xx: cs_write() d->count > dmasize - resetting\n"));
2270 dmabuf->count = dmabuf->dmasize;
2272 dmabuf->endcleared = 0;
2273 spin_unlock_irqrestore(&state->card->lock, flags);
2282 remove_wait_queue(&state->dmabuf.wait, &wait);
2283 set_current_state(TASK_RUNNING);
2285 CS_DBGOUT(CS_WAVE_WRITE | CS_FUNCTION, 2,
2286 printk("cs46xx: cs_write()- ret=%zd\n", ret) );
2290 static unsigned int cs_poll(struct file *file, struct poll_table_struct *wait)
2292 struct cs_card *card = (struct cs_card *)file->private_data;
2293 struct dmabuf *dmabuf;
2294 struct cs_state *state;
2296 unsigned long flags;
2297 unsigned int mask = 0;
2299 CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: cs_poll()+ \n"));
2300 if (!(file->f_mode & (FMODE_WRITE | FMODE_READ)))
2304 if (file->f_mode & FMODE_WRITE)
2306 state = card->states[1];
2309 dmabuf = &state->dmabuf;
2310 poll_wait(file, &dmabuf->wait, wait);
2313 if (file->f_mode & FMODE_READ)
2315 state = card->states[0];
2318 dmabuf = &state->dmabuf;
2319 poll_wait(file, &dmabuf->wait, wait);
2323 spin_lock_irqsave(&card->lock, flags);
2324 cs_update_ptr(card, CS_FALSE);
2325 if (file->f_mode & FMODE_READ) {
2326 state = card->states[0];
2329 dmabuf = &state->dmabuf;
2330 if (dmabuf->count >= (signed)dmabuf->fragsize)
2331 mask |= POLLIN | POLLRDNORM;
2334 if (file->f_mode & FMODE_WRITE) {
2335 state = card->states[1];
2338 dmabuf = &state->dmabuf;
2339 if (dmabuf->mapped) {
2340 if (dmabuf->count >= (signed)dmabuf->fragsize)
2341 mask |= POLLOUT | POLLWRNORM;
2343 if ((signed)dmabuf->dmasize >= dmabuf->count
2344 + (signed)dmabuf->fragsize)
2345 mask |= POLLOUT | POLLWRNORM;
2349 spin_unlock_irqrestore(&card->lock, flags);
2351 CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: cs_poll()- (0x%x) \n",
2357 * We let users mmap the ring buffer. Its not the real DMA buffer but
2358 * that side of the code is hidden in the IRQ handling. We do a software
2359 * emulation of DMA from a 64K or so buffer into a 2K FIFO.
2360 * (the hardware probably deserves a moan here but Crystal send me nice
2364 static int cs_mmap(struct file *file, struct vm_area_struct *vma)
2366 struct cs_card *card = (struct cs_card *)file->private_data;
2367 struct cs_state *state;
2368 struct dmabuf *dmabuf;
2372 CS_DBGOUT(CS_FUNCTION | CS_PARMS, 2, printk("cs46xx: cs_mmap()+ file=%p %s %s\n",
2373 file, vma->vm_flags & VM_WRITE ? "VM_WRITE" : "",
2374 vma->vm_flags & VM_READ ? "VM_READ" : "") );
2376 if (vma->vm_flags & VM_WRITE) {
2377 state = card->states[1];
2380 CS_DBGOUT(CS_OPEN, 2, printk(
2381 "cs46xx: cs_mmap() VM_WRITE - state TRUE prog_dmabuf DAC\n") );
2382 if ((ret = prog_dmabuf(state)) != 0)
2385 } else if (vma->vm_flags & VM_READ) {
2386 state = card->states[0];
2389 CS_DBGOUT(CS_OPEN, 2, printk(
2390 "cs46xx: cs_mmap() VM_READ - state TRUE prog_dmabuf ADC\n") );
2391 if ((ret = prog_dmabuf(state)) != 0)
2395 CS_DBGOUT(CS_ERROR, 2, printk(
2396 "cs46xx: cs_mmap() return -EINVAL\n") );
2401 * For now ONLY support playback, but seems like the only way to use
2402 * mmap() is to open an FD with RDWR, just read or just write access
2403 * does not function, get an error back from the kernel.
2404 * Also, QuakeIII opens with RDWR! So, there must be something
2405 * to needing read/write access mapping. So, allow read/write but
2408 state = card->states[1];
2415 dmabuf = &state->dmabuf;
2416 if (cs4x_pgoff(vma) != 0)
2421 size = vma->vm_end - vma->vm_start;
2423 CS_DBGOUT(CS_PARMS, 2, printk("cs46xx: cs_mmap(): size=%d\n",(unsigned)size) );
2425 if (size > (PAGE_SIZE << dmabuf->buforder))
2430 if (remap_pfn_range(vma, vma->vm_start,
2431 virt_to_phys(dmabuf->rawbuf) >> PAGE_SHIFT,
2432 size, vma->vm_page_prot))
2439 CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: cs_mmap()-\n") );
2445 static int cs_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
2447 struct cs_card *card = (struct cs_card *)file->private_data;
2448 struct cs_state *state;
2449 struct dmabuf *dmabuf=NULL;
2450 unsigned long flags;
2451 audio_buf_info abinfo;
2453 int val, valsave, mapped, ret;
2454 void __user *argp = (void __user *)arg;
2455 int __user *p = argp;
2457 state = (struct cs_state *)card->states[0];
2460 dmabuf = &state->dmabuf;
2461 mapped = (file->f_mode & FMODE_READ) && dmabuf->mapped;
2463 state = (struct cs_state *)card->states[1];
2466 dmabuf = &state->dmabuf;
2467 mapped |= (file->f_mode & FMODE_WRITE) && dmabuf->mapped;
2476 case OSS_GETVERSION:
2477 return put_user(SOUND_VERSION, p);
2479 case SNDCTL_DSP_RESET:
2480 /* FIXME: spin_lock ? */
2481 if (file->f_mode & FMODE_WRITE) {
2482 state = (struct cs_state *)card->states[1];
2485 dmabuf = &state->dmabuf;
2487 synchronize_irq(card->irq);
2489 resync_dma_ptrs(state);
2490 dmabuf->swptr = dmabuf->hwptr = 0;
2491 dmabuf->count = dmabuf->total_bytes = 0;
2496 if (file->f_mode & FMODE_READ) {
2497 state = (struct cs_state *)card->states[0];
2500 dmabuf = &state->dmabuf;
2502 synchronize_irq(card->irq);
2503 resync_dma_ptrs(state);
2505 dmabuf->swptr = dmabuf->hwptr = 0;
2506 dmabuf->count = dmabuf->total_bytes = 0;
2511 CS_DBGOUT(CS_IOCTL, 2, printk("cs46xx: DSP_RESET()-\n") );
2514 case SNDCTL_DSP_SYNC:
2515 if (file->f_mode & FMODE_WRITE)
2516 return drain_dac(state, file->f_flags & O_NONBLOCK);
2519 case SNDCTL_DSP_SPEED: /* set sample rate */
2520 if (get_user(val, p))
2523 if (file->f_mode & FMODE_READ) {
2524 state = (struct cs_state *)card->states[0];
2527 dmabuf = &state->dmabuf;
2531 cs_set_adc_rate(state, val);
2532 cs_set_divisor(dmabuf);
2535 if (file->f_mode & FMODE_WRITE) {
2536 state = (struct cs_state *)card->states[1];
2539 dmabuf = &state->dmabuf;
2543 cs_set_dac_rate(state, val);
2544 cs_set_divisor(dmabuf);
2547 CS_DBGOUT(CS_IOCTL | CS_PARMS, 4, printk(
2548 "cs46xx: cs_ioctl() DSP_SPEED %s %s %d\n",
2549 file->f_mode & FMODE_WRITE ? "DAC" : "",
2550 file->f_mode & FMODE_READ ? "ADC" : "",
2552 return put_user(dmabuf->rate, p);
2554 return put_user(0, p);
2556 case SNDCTL_DSP_STEREO: /* set stereo or mono channel */
2557 if (get_user(val, p))
2559 if (file->f_mode & FMODE_WRITE) {
2560 state = (struct cs_state *)card->states[1];
2563 dmabuf = &state->dmabuf;
2568 dmabuf->fmt |= CS_FMT_STEREO;
2570 dmabuf->fmt &= ~CS_FMT_STEREO;
2571 cs_set_divisor(dmabuf);
2572 CS_DBGOUT(CS_IOCTL | CS_PARMS, 4, printk(
2573 "cs46xx: DSP_STEREO() DAC %s\n",
2574 (dmabuf->fmt & CS_FMT_STEREO) ?
2578 if (file->f_mode & FMODE_READ) {
2579 state = (struct cs_state *)card->states[0];
2582 dmabuf = &state->dmabuf;
2587 dmabuf->fmt |= CS_FMT_STEREO;
2589 dmabuf->fmt &= ~CS_FMT_STEREO;
2590 cs_set_divisor(dmabuf);
2591 CS_DBGOUT(CS_IOCTL | CS_PARMS, 4, printk(
2592 "cs46xx: DSP_STEREO() ADC %s\n",
2593 (dmabuf->fmt & CS_FMT_STEREO) ?
2599 case SNDCTL_DSP_GETBLKSIZE:
2600 if (file->f_mode & FMODE_WRITE) {
2601 state = (struct cs_state *)card->states[1];
2604 dmabuf = &state->dmabuf;
2605 if ((val = prog_dmabuf(state)))
2607 return put_user(dmabuf->fragsize, p);
2610 if (file->f_mode & FMODE_READ) {
2611 state = (struct cs_state *)card->states[0];
2614 dmabuf = &state->dmabuf;
2615 if ((val = prog_dmabuf(state)))
2617 return put_user(dmabuf->fragsize/dmabuf->divisor,
2621 return put_user(0, p);
2623 case SNDCTL_DSP_GETFMTS: /* Returns a mask of supported sample format*/
2624 return put_user(AFMT_S16_LE | AFMT_U8, p);
2626 case SNDCTL_DSP_SETFMT: /* Select sample format */
2627 if (get_user(val, p))
2629 CS_DBGOUT(CS_IOCTL | CS_PARMS, 4, printk(
2630 "cs46xx: cs_ioctl() DSP_SETFMT %s %s %s %s\n",
2631 file->f_mode & FMODE_WRITE ? "DAC" : "",
2632 file->f_mode & FMODE_READ ? "ADC" : "",
2633 val == AFMT_S16_LE ? "16Bit Signed" : "",
2634 val == AFMT_U8 ? "8Bit Unsigned" : "") );
2636 if (val != AFMT_QUERY) {
2637 if(val==AFMT_S16_LE || val==AFMT_U8)
2639 if (file->f_mode & FMODE_WRITE) {
2640 state = (struct cs_state *)card->states[1];
2643 dmabuf = &state->dmabuf;
2647 if(val==AFMT_S16_LE)
2648 dmabuf->fmt |= CS_FMT_16BIT;
2650 dmabuf->fmt &= ~CS_FMT_16BIT;
2651 cs_set_divisor(dmabuf);
2652 if((ret = prog_dmabuf(state)))
2656 if (file->f_mode & FMODE_READ) {
2658 state = (struct cs_state *)card->states[0];
2661 dmabuf = &state->dmabuf;
2665 if(val==AFMT_S16_LE)
2666 dmabuf->fmt |= CS_FMT_16BIT;
2668 dmabuf->fmt &= ~CS_FMT_16BIT;
2669 cs_set_divisor(dmabuf);
2670 if((ret = prog_dmabuf(state)))
2677 CS_DBGOUT(CS_IOCTL | CS_ERROR, 2, printk(
2678 "cs46xx: DSP_SETFMT() Unsupported format (0x%x)\n",
2684 if(file->f_mode & FMODE_WRITE)
2686 state = (struct cs_state *)card->states[1];
2688 dmabuf = &state->dmabuf;
2690 else if(file->f_mode & FMODE_READ)
2692 state = (struct cs_state *)card->states[0];
2694 dmabuf = &state->dmabuf;
2699 if(dmabuf->fmt & CS_FMT_16BIT)
2700 return put_user(AFMT_S16_LE, p);
2702 return put_user(AFMT_U8, p);
2704 return put_user(0, p);
2706 case SNDCTL_DSP_CHANNELS:
2707 if (get_user(val, p))
2710 if (file->f_mode & FMODE_WRITE) {
2711 state = (struct cs_state *)card->states[1];
2714 dmabuf = &state->dmabuf;
2719 dmabuf->fmt |= CS_FMT_STEREO;
2721 dmabuf->fmt &= ~CS_FMT_STEREO;
2722 cs_set_divisor(dmabuf);
2723 if (prog_dmabuf(state))
2727 if (file->f_mode & FMODE_READ) {
2728 state = (struct cs_state *)card->states[0];
2731 dmabuf = &state->dmabuf;
2736 dmabuf->fmt |= CS_FMT_STEREO;
2738 dmabuf->fmt &= ~CS_FMT_STEREO;
2739 cs_set_divisor(dmabuf);
2740 if (prog_dmabuf(state))
2745 return put_user((dmabuf->fmt & CS_FMT_STEREO) ? 2 : 1,
2748 case SNDCTL_DSP_POST:
2750 * There will be a longer than normal pause in the data.
2751 * so... do nothing, because there is nothing that we can do.
2755 case SNDCTL_DSP_SUBDIVIDE:
2756 if (file->f_mode & FMODE_WRITE) {
2757 state = (struct cs_state *)card->states[1];
2760 dmabuf = &state->dmabuf;
2761 if (dmabuf->subdivision)
2763 if (get_user(val, p))
2765 if (val != 1 && val != 2)
2767 dmabuf->subdivision = val;
2770 if (file->f_mode & FMODE_READ) {
2771 state = (struct cs_state *)card->states[0];
2774 dmabuf = &state->dmabuf;
2775 if (dmabuf->subdivision)
2777 if (get_user(val, p))
2779 if (val != 1 && val != 2)
2781 dmabuf->subdivision = val;
2786 case SNDCTL_DSP_SETFRAGMENT:
2787 if (get_user(val, p))
2790 if (file->f_mode & FMODE_WRITE) {
2791 state = (struct cs_state *)card->states[1];
2794 dmabuf = &state->dmabuf;
2795 dmabuf->ossfragshift = val & 0xffff;
2796 dmabuf->ossmaxfrags = (val >> 16) & 0xffff;
2799 if (file->f_mode & FMODE_READ) {
2800 state = (struct cs_state *)card->states[0];
2803 dmabuf = &state->dmabuf;
2804 dmabuf->ossfragshift = val & 0xffff;
2805 dmabuf->ossmaxfrags = (val >> 16) & 0xffff;
2810 case SNDCTL_DSP_GETOSPACE:
2811 if (!(file->f_mode & FMODE_WRITE))
2813 state = (struct cs_state *)card->states[1];
2816 dmabuf = &state->dmabuf;
2817 spin_lock_irqsave(&state->card->lock, flags);
2818 cs_update_ptr(card, CS_TRUE);
2819 abinfo.fragsize = dmabuf->fragsize;
2820 abinfo.fragstotal = dmabuf->numfrag;
2822 * for mmap we always have total space available
2825 abinfo.bytes = dmabuf->dmasize;
2827 abinfo.bytes = dmabuf->dmasize - dmabuf->count;
2829 abinfo.fragments = abinfo.bytes >> dmabuf->fragshift;
2830 spin_unlock_irqrestore(&state->card->lock, flags);
2831 return copy_to_user(argp, &abinfo, sizeof(abinfo)) ? -EFAULT : 0;
2835 case SNDCTL_DSP_GETISPACE:
2836 if (!(file->f_mode & FMODE_READ))
2838 state = (struct cs_state *)card->states[0];
2841 dmabuf = &state->dmabuf;
2842 spin_lock_irqsave(&state->card->lock, flags);
2843 cs_update_ptr(card, CS_TRUE);
2844 abinfo.fragsize = dmabuf->fragsize/dmabuf->divisor;
2845 abinfo.bytes = dmabuf->count/dmabuf->divisor;
2846 abinfo.fragstotal = dmabuf->numfrag;
2847 abinfo.fragments = abinfo.bytes >> dmabuf->fragshift;
2848 spin_unlock_irqrestore(&state->card->lock, flags);
2849 return copy_to_user(argp, &abinfo, sizeof(abinfo)) ? -EFAULT : 0;
2853 case SNDCTL_DSP_NONBLOCK:
2854 file->f_flags |= O_NONBLOCK;
2857 case SNDCTL_DSP_GETCAPS:
2858 return put_user(DSP_CAP_REALTIME|DSP_CAP_TRIGGER|DSP_CAP_MMAP,
2861 case SNDCTL_DSP_GETTRIGGER:
2863 CS_DBGOUT(CS_IOCTL, 2, printk("cs46xx: DSP_GETTRIGGER()+\n") );
2864 if (file->f_mode & FMODE_WRITE)
2866 state = (struct cs_state *)card->states[1];
2869 dmabuf = &state->dmabuf;
2870 if(dmabuf->enable & DAC_RUNNING)
2871 val |= PCM_ENABLE_INPUT;
2874 if (file->f_mode & FMODE_READ)
2878 state = (struct cs_state *)card->states[0];
2879 dmabuf = &state->dmabuf;
2880 if(dmabuf->enable & ADC_RUNNING)
2881 val |= PCM_ENABLE_OUTPUT;
2884 CS_DBGOUT(CS_IOCTL, 2, printk("cs46xx: DSP_GETTRIGGER()- val=0x%x\n",val) );
2885 return put_user(val, p);
2887 case SNDCTL_DSP_SETTRIGGER:
2888 if (get_user(val, p))
2890 if (file->f_mode & FMODE_READ) {
2891 state = (struct cs_state *)card->states[0];
2894 dmabuf = &state->dmabuf;
2895 if (val & PCM_ENABLE_INPUT) {
2896 if (!dmabuf->ready && (ret = prog_dmabuf(state)))
2903 if (file->f_mode & FMODE_WRITE) {
2904 state = (struct cs_state *)card->states[1];
2907 dmabuf = &state->dmabuf;
2908 if (val & PCM_ENABLE_OUTPUT) {
2909 if (!dmabuf->ready && (ret = prog_dmabuf(state)))
2918 case SNDCTL_DSP_GETIPTR:
2919 if (!(file->f_mode & FMODE_READ))
2921 state = (struct cs_state *)card->states[0];
2924 dmabuf = &state->dmabuf;
2925 spin_lock_irqsave(&state->card->lock, flags);
2926 cs_update_ptr(card, CS_TRUE);
2927 cinfo.bytes = dmabuf->total_bytes/dmabuf->divisor;
2928 cinfo.blocks = dmabuf->count/dmabuf->divisor >> dmabuf->fragshift;
2929 cinfo.ptr = dmabuf->hwptr/dmabuf->divisor;
2930 spin_unlock_irqrestore(&state->card->lock, flags);
2931 if (copy_to_user(argp, &cinfo, sizeof(cinfo)))
2937 case SNDCTL_DSP_GETOPTR:
2938 if (!(file->f_mode & FMODE_WRITE))
2940 state = (struct cs_state *)card->states[1];
2943 dmabuf = &state->dmabuf;
2944 spin_lock_irqsave(&state->card->lock, flags);
2945 cs_update_ptr(card, CS_TRUE);
2946 cinfo.bytes = dmabuf->total_bytes;
2949 cinfo.blocks = (cinfo.bytes >> dmabuf->fragshift)
2951 CS_DBGOUT(CS_PARMS, 8,
2952 printk("total_bytes=%d blocks=%d dmabuf->blocks=%d\n",
2953 cinfo.bytes,cinfo.blocks,dmabuf->blocks) );
2954 dmabuf->blocks = cinfo.bytes >> dmabuf->fragshift;
2958 cinfo.blocks = dmabuf->count >> dmabuf->fragshift;
2960 cinfo.ptr = dmabuf->hwptr;
2962 CS_DBGOUT(CS_PARMS, 4, printk(
2963 "cs46xx: GETOPTR bytes=%d blocks=%d ptr=%d\n",
2964 cinfo.bytes,cinfo.blocks,cinfo.ptr) );
2965 spin_unlock_irqrestore(&state->card->lock, flags);
2966 if (copy_to_user(argp, &cinfo, sizeof(cinfo)))
2972 case SNDCTL_DSP_SETDUPLEX:
2975 case SNDCTL_DSP_GETODELAY:
2976 if (!(file->f_mode & FMODE_WRITE))
2978 state = (struct cs_state *)card->states[1];
2981 dmabuf = &state->dmabuf;
2982 spin_lock_irqsave(&state->card->lock, flags);
2983 cs_update_ptr(card, CS_TRUE);
2984 val = dmabuf->count;
2985 spin_unlock_irqrestore(&state->card->lock, flags);
2989 return put_user(val, p);
2991 case SOUND_PCM_READ_RATE:
2992 if(file->f_mode & FMODE_READ)
2993 state = (struct cs_state *)card->states[0];
2995 state = (struct cs_state *)card->states[1];
2998 dmabuf = &state->dmabuf;
2999 return put_user(dmabuf->rate, p);
3001 return put_user(0, p);
3004 case SOUND_PCM_READ_CHANNELS:
3005 if(file->f_mode & FMODE_READ)
3006 state = (struct cs_state *)card->states[0];
3008 state = (struct cs_state *)card->states[1];
3011 dmabuf = &state->dmabuf;
3012 return put_user((dmabuf->fmt & CS_FMT_STEREO) ? 2 : 1,
3015 return put_user(0, p);
3017 case SOUND_PCM_READ_BITS:
3018 if(file->f_mode & FMODE_READ)
3019 state = (struct cs_state *)card->states[0];
3021 state = (struct cs_state *)card->states[1];
3024 dmabuf = &state->dmabuf;
3025 return put_user((dmabuf->fmt & CS_FMT_16BIT) ?
3026 AFMT_S16_LE : AFMT_U8, p);
3029 return put_user(0, p);
3031 case SNDCTL_DSP_MAPINBUF:
3032 case SNDCTL_DSP_MAPOUTBUF:
3033 case SNDCTL_DSP_SETSYNCRO:
3034 case SOUND_PCM_WRITE_FILTER:
3035 case SOUND_PCM_READ_FILTER:
3043 * AMP control - null AMP
3046 static void amp_none(struct cs_card *card, int change)
3054 static void amp_voyetra(struct cs_card *card, int change)
3056 /* Manage the EAPD bit on the Crystal 4297
3057 and the Analog AD1885 */
3059 int old=card->amplifier;
3061 card->amplifier+=change;
3062 if(card->amplifier && !old)
3064 /* Turn the EAPD amp on */
3065 cs_ac97_set(card->ac97_codec[0], AC97_POWER_CONTROL,
3066 cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) |
3069 else if(old && !card->amplifier)
3071 /* Turn the EAPD amp off */
3072 cs_ac97_set(card->ac97_codec[0], AC97_POWER_CONTROL,
3073 cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) &
3080 * Game Theatre XP card - EGPIO[2] is used to enable the external amp.
3083 static void amp_hercules(struct cs_card *card, int change)
3085 int old=card->amplifier;
3088 CS_DBGOUT(CS_ERROR, 2, printk(KERN_INFO
3089 "cs46xx: amp_hercules() called before initialized.\n"));
3092 card->amplifier+=change;
3093 if( (card->amplifier && !old) && !(hercules_egpio_disable))
3095 CS_DBGOUT(CS_PARMS, 4, printk(KERN_INFO
3096 "cs46xx: amp_hercules() external amp enabled\n"));
3097 cs461x_pokeBA0(card, BA0_EGPIODR,
3098 EGPIODR_GPOE2); /* enable EGPIO2 output */
3099 cs461x_pokeBA0(card, BA0_EGPIOPTR,
3100 EGPIOPTR_GPPT2); /* open-drain on output */
3102 else if(old && !card->amplifier)
3104 CS_DBGOUT(CS_PARMS, 4, printk(KERN_INFO
3105 "cs46xx: amp_hercules() external amp disabled\n"));
3106 cs461x_pokeBA0(card, BA0_EGPIODR, 0); /* disable */
3107 cs461x_pokeBA0(card, BA0_EGPIOPTR, 0); /* disable */
3112 * Handle the CLKRUN on a thinkpad. We must disable CLKRUN support
3113 * whenever we need to beat on the chip.
3115 * The original idea and code for this hack comes from David Kaiser at
3116 * Linuxcare. Perhaps one day Crystal will document their chips well
3117 * enough to make them useful.
3120 static void clkrun_hack(struct cs_card *card, int change)
3122 struct pci_dev *acpi_dev;
3126 int old=card->active;
3128 card->active+=change;
3130 acpi_dev = pci_find_device(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371AB_3, NULL);
3131 if(acpi_dev == NULL)
3132 return; /* Not a thinkpad thats for sure */
3134 /* Find the control port */
3135 pci_read_config_byte(acpi_dev, 0x41, &pp);
3138 /* Read ACPI port */
3139 control=inw(port+0x10);
3141 /* Flip CLKRUN off while running */
3142 if(!card->active && old)
3144 CS_DBGOUT(CS_PARMS , 9, printk( KERN_INFO
3145 "cs46xx: clkrun() enable clkrun - change=%d active=%d\n",
3146 change,card->active));
3147 outw(control|0x2000, port+0x10);
3152 * sometimes on a resume the bit is set, so always reset the bit.
3154 CS_DBGOUT(CS_PARMS , 9, printk( KERN_INFO
3155 "cs46xx: clkrun() disable clkrun - change=%d active=%d\n",
3156 change,card->active));
3157 outw(control&~0x2000, port+0x10);
3162 static int cs_open(struct inode *inode, struct file *file)
3164 struct cs_card *card = (struct cs_card *)file->private_data;
3165 struct cs_state *state = NULL;
3166 struct dmabuf *dmabuf = NULL;
3167 struct list_head *entry;
3168 unsigned int minor = iminor(inode);
3172 CS_DBGOUT(CS_OPEN | CS_FUNCTION, 2, printk("cs46xx: cs_open()+ file=%p %s %s\n",
3173 file, file->f_mode & FMODE_WRITE ? "FMODE_WRITE" : "",
3174 file->f_mode & FMODE_READ ? "FMODE_READ" : "") );
3176 list_for_each(entry, &cs46xx_devs)
3178 card = list_entry(entry, struct cs_card, list);
3180 if (!((card->dev_audio ^ minor) & ~0xf))
3183 if (entry == &cs46xx_devs)
3186 CS_DBGOUT(CS_FUNCTION | CS_OPEN, 2, printk(KERN_INFO
3187 "cs46xx: cs_open(): Error - unable to find audio card struct\n"));
3192 * hardcode state[0] for capture, [1] for playback
3194 if(file->f_mode & FMODE_READ)
3196 CS_DBGOUT(CS_WAVE_READ, 2, printk("cs46xx: cs_open() FMODE_READ\n") );
3197 if (card->states[0] == NULL) {
3198 state = card->states[0] = (struct cs_state *)
3199 kmalloc(sizeof(struct cs_state), GFP_KERNEL);
3202 memset(state, 0, sizeof(struct cs_state));
3203 init_MUTEX(&state->sem);
3204 dmabuf = &state->dmabuf;
3205 dmabuf->pbuf = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
3206 if(dmabuf->pbuf==NULL)
3209 card->states[0]=NULL;
3215 state = card->states[0];
3216 if(state->open_mode & FMODE_READ)
3219 dmabuf->channel = card->alloc_rec_pcm_channel(card);
3221 if (dmabuf->channel == NULL) {
3222 kfree (card->states[0]);
3223 card->states[0] = NULL;
3227 /* Now turn on external AMP if needed */
3229 state->card->active_ctrl(state->card,1);
3230 state->card->amplifier_ctrl(state->card,1);
3232 if( (tmp = cs46xx_powerup(card, CS_POWER_ADC)) )
3234 CS_DBGOUT(CS_ERROR | CS_INIT, 1, printk(KERN_INFO
3235 "cs46xx: cs46xx_powerup of ADC failed (0x%x)\n",tmp) );
3239 dmabuf->channel->state = state;
3240 /* initialize the virtual channel */
3242 state->magic = CS_STATE_MAGIC;
3243 init_waitqueue_head(&dmabuf->wait);
3244 init_MUTEX(&state->open_sem);
3245 file->private_data = card;
3247 down(&state->open_sem);
3249 /* set default sample format. According to OSS Programmer's Guide /dev/dsp
3250 should be default to unsigned 8-bits, mono, with sample rate 8kHz and
3251 /dev/dspW will accept 16-bits sample */
3253 /* Default input is 8bit mono */
3254 dmabuf->fmt &= ~CS_FMT_MASK;
3255 dmabuf->type = CS_TYPE_ADC;
3256 dmabuf->ossfragshift = 0;
3257 dmabuf->ossmaxfrags = 0;
3258 dmabuf->subdivision = 0;
3259 cs_set_adc_rate(state, 8000);
3260 cs_set_divisor(dmabuf);
3262 state->open_mode |= FMODE_READ;
3263 up(&state->open_sem);
3265 if(file->f_mode & FMODE_WRITE)
3267 CS_DBGOUT(CS_OPEN, 2, printk("cs46xx: cs_open() FMODE_WRITE\n") );
3268 if (card->states[1] == NULL) {
3269 state = card->states[1] = (struct cs_state *)
3270 kmalloc(sizeof(struct cs_state), GFP_KERNEL);
3273 memset(state, 0, sizeof(struct cs_state));
3274 init_MUTEX(&state->sem);
3275 dmabuf = &state->dmabuf;
3276 dmabuf->pbuf = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
3277 if(dmabuf->pbuf==NULL)
3280 card->states[1]=NULL;
3286 state = card->states[1];
3287 if(state->open_mode & FMODE_WRITE)
3290 dmabuf->channel = card->alloc_pcm_channel(card);
3292 if (dmabuf->channel == NULL) {
3293 kfree (card->states[1]);
3294 card->states[1] = NULL;
3298 /* Now turn on external AMP if needed */
3300 state->card->active_ctrl(state->card,1);
3301 state->card->amplifier_ctrl(state->card,1);
3303 if( (tmp = cs46xx_powerup(card, CS_POWER_DAC)) )
3305 CS_DBGOUT(CS_ERROR | CS_INIT, 1, printk(KERN_INFO
3306 "cs46xx: cs46xx_powerup of DAC failed (0x%x)\n",tmp) );
3310 dmabuf->channel->state = state;
3311 /* initialize the virtual channel */
3313 state->magic = CS_STATE_MAGIC;
3314 init_waitqueue_head(&dmabuf->wait);
3315 init_MUTEX(&state->open_sem);
3316 file->private_data = card;
3318 down(&state->open_sem);
3320 /* set default sample format. According to OSS Programmer's Guide /dev/dsp
3321 should be default to unsigned 8-bits, mono, with sample rate 8kHz and
3322 /dev/dspW will accept 16-bits sample */
3324 /* Default output is 8bit mono. */
3325 dmabuf->fmt &= ~CS_FMT_MASK;
3326 dmabuf->type = CS_TYPE_DAC;
3327 dmabuf->ossfragshift = 0;
3328 dmabuf->ossmaxfrags = 0;
3329 dmabuf->subdivision = 0;
3330 cs_set_dac_rate(state, 8000);
3331 cs_set_divisor(dmabuf);
3333 state->open_mode |= FMODE_WRITE;
3334 up(&state->open_sem);
3335 if((ret = prog_dmabuf(state)))
3338 CS_DBGOUT(CS_OPEN | CS_FUNCTION, 2, printk("cs46xx: cs_open()- 0\n") );
3339 return nonseekable_open(inode, file);
3342 static int cs_release(struct inode *inode, struct file *file)
3344 struct cs_card *card = (struct cs_card *)file->private_data;
3345 struct dmabuf *dmabuf;
3346 struct cs_state *state;
3348 CS_DBGOUT(CS_RELEASE | CS_FUNCTION, 2, printk("cs46xx: cs_release()+ file=%p %s %s\n",
3349 file, file->f_mode & FMODE_WRITE ? "FMODE_WRITE" : "",
3350 file->f_mode & FMODE_READ ? "FMODE_READ" : "") );
3352 if (!(file->f_mode & (FMODE_WRITE | FMODE_READ)))
3356 state = card->states[1];
3359 if ( (state->open_mode & FMODE_WRITE) & (file->f_mode & FMODE_WRITE) )
3361 CS_DBGOUT(CS_RELEASE, 2, printk("cs46xx: cs_release() FMODE_WRITE\n") );
3362 dmabuf = &state->dmabuf;
3363 cs_clear_tail(state);
3364 drain_dac(state, file->f_flags & O_NONBLOCK);
3365 /* stop DMA state machine and free DMA buffers/channels */
3366 down(&state->open_sem);
3368 dealloc_dmabuf(state);
3369 state->card->free_pcm_channel(state->card, dmabuf->channel->num);
3370 free_page((unsigned long)state->dmabuf.pbuf);
3372 /* we're covered by the open_sem */
3373 up(&state->open_sem);
3374 state->card->states[state->virt] = NULL;
3375 state->open_mode &= (~file->f_mode) & (FMODE_READ|FMODE_WRITE);
3377 if( (tmp = cs461x_powerdown(card, CS_POWER_DAC, CS_FALSE )) )
3379 CS_DBGOUT(CS_ERROR, 1, printk(KERN_INFO
3380 "cs46xx: cs_release_mixdev() powerdown DAC failure (0x%x)\n",tmp) );
3383 /* Now turn off external AMP if needed */
3384 state->card->amplifier_ctrl(state->card, -1);
3385 state->card->active_ctrl(state->card, -1);
3391 state = card->states[0];
3394 if ( (state->open_mode & FMODE_READ) & (file->f_mode & FMODE_READ) )
3396 CS_DBGOUT(CS_RELEASE, 2, printk("cs46xx: cs_release() FMODE_READ\n") );
3397 dmabuf = &state->dmabuf;
3398 down(&state->open_sem);
3400 dealloc_dmabuf(state);
3401 state->card->free_pcm_channel(state->card, dmabuf->channel->num);
3402 free_page((unsigned long)state->dmabuf.pbuf);
3404 /* we're covered by the open_sem */
3405 up(&state->open_sem);
3406 state->card->states[state->virt] = NULL;
3407 state->open_mode &= (~file->f_mode) & (FMODE_READ|FMODE_WRITE);
3409 if( (tmp = cs461x_powerdown(card, CS_POWER_ADC, CS_FALSE )) )
3411 CS_DBGOUT(CS_ERROR, 1, printk(KERN_INFO
3412 "cs46xx: cs_release_mixdev() powerdown ADC failure (0x%x)\n",tmp) );
3415 /* Now turn off external AMP if needed */
3416 state->card->amplifier_ctrl(state->card, -1);
3417 state->card->active_ctrl(state->card, -1);
3423 CS_DBGOUT(CS_FUNCTION | CS_RELEASE, 2, printk("cs46xx: cs_release()- 0\n") );
3427 static void printpm(struct cs_card *s)
3429 CS_DBGOUT(CS_PM, 9, printk("pm struct:\n"));
3430 CS_DBGOUT(CS_PM, 9, printk("flags:0x%x u32CLKCR1_SAVE: 0%x u32SSPMValue: 0x%x\n",
3431 (unsigned)s->pm.flags,s->pm.u32CLKCR1_SAVE,s->pm.u32SSPMValue));
3432 CS_DBGOUT(CS_PM, 9, printk("u32PPLVCvalue: 0x%x u32PPRVCvalue: 0x%x\n",
3433 s->pm.u32PPLVCvalue,s->pm.u32PPRVCvalue));
3434 CS_DBGOUT(CS_PM, 9, printk("u32FMLVCvalue: 0x%x u32FMRVCvalue: 0x%x\n",
3435 s->pm.u32FMLVCvalue,s->pm.u32FMRVCvalue));
3436 CS_DBGOUT(CS_PM, 9, printk("u32GPIORvalue: 0x%x u32JSCTLvalue: 0x%x\n",
3437 s->pm.u32GPIORvalue,s->pm.u32JSCTLvalue));
3438 CS_DBGOUT(CS_PM, 9, printk("u32SSCR: 0x%x u32SRCSA: 0x%x\n",
3439 s->pm.u32SSCR,s->pm.u32SRCSA));
3440 CS_DBGOUT(CS_PM, 9, printk("u32DacASR: 0x%x u32AdcASR: 0x%x\n",
3441 s->pm.u32DacASR,s->pm.u32AdcASR));
3442 CS_DBGOUT(CS_PM, 9, printk("u32DacSR: 0x%x u32AdcSR: 0x%x\n",
3443 s->pm.u32DacSR,s->pm.u32AdcSR));
3444 CS_DBGOUT(CS_PM, 9, printk("u32MIDCR_Save: 0x%x\n",
3445 s->pm.u32MIDCR_Save));
3446 CS_DBGOUT(CS_PM, 9, printk("u32AC97_powerdown: 0x%x _general_purpose 0x%x\n",
3447 s->pm.u32AC97_powerdown,s->pm.u32AC97_general_purpose));
3448 CS_DBGOUT(CS_PM, 9, printk("u32AC97_master_volume: 0x%x\n",
3449 s->pm.u32AC97_master_volume));
3450 CS_DBGOUT(CS_PM, 9, printk("u32AC97_headphone_volume: 0x%x\n",
3451 s->pm.u32AC97_headphone_volume));
3452 CS_DBGOUT(CS_PM, 9, printk("u32AC97_master_volume_mono: 0x%x\n",
3453 s->pm.u32AC97_master_volume_mono));
3454 CS_DBGOUT(CS_PM, 9, printk("u32AC97_pcm_out_volume: 0x%x\n",
3455 s->pm.u32AC97_pcm_out_volume));
3456 CS_DBGOUT(CS_PM, 9, printk("dmabuf_swptr_play: 0x%x dmabuf_count_play: %d\n",
3457 s->pm.dmabuf_swptr_play,s->pm.dmabuf_count_play));
3458 CS_DBGOUT(CS_PM, 9, printk("dmabuf_swptr_capture: 0x%x dmabuf_count_capture: %d\n",
3459 s->pm.dmabuf_swptr_capture,s->pm.dmabuf_count_capture));
3463 /****************************************************************************
3465 * Suspend - save the ac97 regs, mute the outputs and power down the part.
3467 ****************************************************************************/
3468 static void cs46xx_ac97_suspend(struct cs_card *card)
3471 struct ac97_codec *dev=card->ac97_codec[0];
3474 CS_DBGOUT(CS_PM, 9, printk("cs46xx: cs46xx_ac97_suspend()+\n"));
3478 stop_dac(card->states[1]);
3479 resync_dma_ptrs(card->states[1]);
3483 stop_adc(card->states[0]);
3484 resync_dma_ptrs(card->states[0]);
3487 for(Count = 0x2, i=0; (Count <= CS46XX_AC97_HIGHESTREGTORESTORE)
3488 && (i < CS46XX_AC97_NUMBER_RESTORE_REGS);
3491 card->pm.ac97[i] = cs_ac97_get(dev, BA0_AC97_RESET + Count);
3494 * Save the ac97 volume registers as well as the current powerdown state.
3495 * Now, mute the all the outputs (master, headphone, and mono), as well
3496 * as the PCM volume, in preparation for powering down the entire part.
3497 card->pm.u32AC97_master_volume = (u32)cs_ac97_get( dev,
3498 (u8)BA0_AC97_MASTER_VOLUME);
3499 card->pm.u32AC97_headphone_volume = (u32)cs_ac97_get(dev,
3500 (u8)BA0_AC97_HEADPHONE_VOLUME);
3501 card->pm.u32AC97_master_volume_mono = (u32)cs_ac97_get(dev,
3502 (u8)BA0_AC97_MASTER_VOLUME_MONO);
3503 card->pm.u32AC97_pcm_out_volume = (u32)cs_ac97_get(dev,
3504 (u8)BA0_AC97_PCM_OUT_VOLUME);
3509 cs_ac97_set(dev, (u8)BA0_AC97_MASTER_VOLUME, 0x8000);
3510 cs_ac97_set(dev, (u8)BA0_AC97_HEADPHONE_VOLUME, 0x8000);
3511 cs_ac97_set(dev, (u8)BA0_AC97_MASTER_VOLUME_MONO, 0x8000);
3512 cs_ac97_set(dev, (u8)BA0_AC97_PCM_OUT_VOLUME, 0x8000);
3515 * save the registers that cause pops
3517 card->pm.u32AC97_powerdown = (u32)cs_ac97_get(dev, (u8)AC97_POWER_CONTROL);
3518 card->pm.u32AC97_general_purpose = (u32)cs_ac97_get(dev, (u8)BA0_AC97_GENERAL_PURPOSE);
3520 * And power down everything on the AC97 codec.
3521 * well, for now, only power down the DAC/ADC and MIXER VREFON components.
3522 * trouble with removing VREF.
3524 if( (tmp = cs461x_powerdown(card, CS_POWER_DAC | CS_POWER_ADC |
3525 CS_POWER_MIXVON, CS_TRUE )) )
3527 CS_DBGOUT(CS_ERROR | CS_INIT, 1, printk(KERN_INFO
3528 "cs46xx: cs46xx_ac97_suspend() failure (0x%x)\n",tmp) );
3531 CS_DBGOUT(CS_PM, 9, printk("cs46xx: cs46xx_ac97_suspend()-\n"));
3534 /****************************************************************************
3536 * Resume - power up the part and restore its registers..
3538 ****************************************************************************/
3539 static void cs46xx_ac97_resume(struct cs_card *card)
3542 struct ac97_codec *dev=card->ac97_codec[0];
3544 CS_DBGOUT(CS_PM, 9, printk("cs46xx: cs46xx_ac97_resume()+\n"));
3547 * First, we restore the state of the general purpose register. This
3548 * contains the mic select (mic1 or mic2) and if we restore this after
3549 * we restore the mic volume/boost state and mic2 was selected at
3550 * suspend time, we will end up with a brief period of time where mic1
3551 * is selected with the volume/boost settings for mic2, causing
3552 * acoustic feedback. So we restore the general purpose register
3553 * first, thereby getting the correct mic selected before we restore
3554 * the mic volume/boost.
3556 cs_ac97_set(dev, (u8)BA0_AC97_GENERAL_PURPOSE,
3557 (u16)card->pm.u32AC97_general_purpose);
3559 * Now, while the outputs are still muted, restore the state of power
3562 cs_ac97_set(dev, (u8)BA0_AC97_POWERDOWN, (u16)card->pm.u32AC97_powerdown);
3563 mdelay(5 * cs_laptop_wait);
3565 * Restore just the first set of registers, from register number
3566 * 0x02 to the register number that ulHighestRegToRestore specifies.
3568 for( Count = 0x2, i=0;
3569 (Count <= CS46XX_AC97_HIGHESTREGTORESTORE)
3570 && (i < CS46XX_AC97_NUMBER_RESTORE_REGS);
3573 cs_ac97_set(dev, (u8)(BA0_AC97_RESET + Count), (u16)card->pm.ac97[i]);
3576 /* Check if we have to init the amplifier */
3578 card->amp_init(card);
3580 CS_DBGOUT(CS_PM, 9, printk("cs46xx: cs46xx_ac97_resume()-\n"));
3584 static int cs46xx_restart_part(struct cs_card *card)
3586 struct dmabuf *dmabuf;
3587 CS_DBGOUT(CS_PM | CS_FUNCTION, 4,
3588 printk( "cs46xx: cs46xx_restart_part()+\n"));
3591 dmabuf = &card->states[1]->dmabuf;
3593 resync_dma_ptrs(card->states[1]);
3594 cs_set_divisor(dmabuf);
3595 if(__prog_dmabuf(card->states[1]))
3597 CS_DBGOUT(CS_PM | CS_ERROR, 1,
3598 printk("cs46xx: cs46xx_restart_part()- (-1) prog_dmabuf() dac error\n"));
3601 cs_set_dac_rate(card->states[1], dmabuf->rate);
3605 dmabuf = &card->states[0]->dmabuf;
3607 resync_dma_ptrs(card->states[0]);
3608 cs_set_divisor(dmabuf);
3609 if(__prog_dmabuf(card->states[0]))
3611 CS_DBGOUT(CS_PM | CS_ERROR, 1,
3612 printk("cs46xx: cs46xx_restart_part()- (-1) prog_dmabuf() adc error\n"));
3615 cs_set_adc_rate(card->states[0], dmabuf->rate);
3617 card->pm.flags |= CS46XX_PM_RESUMED;
3619 start_adc(card->states[0]);
3621 start_dac(card->states[1]);
3623 card->pm.flags |= CS46XX_PM_IDLE;
3624 card->pm.flags &= ~(CS46XX_PM_SUSPENDING | CS46XX_PM_SUSPENDED
3625 | CS46XX_PM_RESUMING | CS46XX_PM_RESUMED);
3627 wake_up(&card->states[0]->dmabuf.wait);
3629 wake_up(&card->states[1]->dmabuf.wait);
3631 CS_DBGOUT(CS_PM | CS_FUNCTION, 4,
3632 printk( "cs46xx: cs46xx_restart_part()-\n"));
3637 static void cs461x_reset(struct cs_card *card);
3638 static void cs461x_proc_stop(struct cs_card *card);
3639 static int cs46xx_suspend(struct cs_card *card, pm_message_t state)
3642 CS_DBGOUT(CS_PM | CS_FUNCTION, 4,
3643 printk("cs46xx: cs46xx_suspend()+ flags=0x%x s=%p\n",
3644 (unsigned)card->pm.flags,card));
3646 * check the current state, only suspend if IDLE
3648 if(!(card->pm.flags & CS46XX_PM_IDLE))
3650 CS_DBGOUT(CS_PM | CS_ERROR, 2,
3651 printk("cs46xx: cs46xx_suspend() unable to suspend, not IDLE\n"));
3654 card->pm.flags &= ~CS46XX_PM_IDLE;
3655 card->pm.flags |= CS46XX_PM_SUSPENDING;
3657 card->active_ctrl(card,1);
3659 tmp = cs461x_peek(card, BA1_PFIE);
3662 cs461x_poke(card, BA1_PFIE, tmp); /* playback interrupt disable */
3664 tmp = cs461x_peek(card, BA1_CIE);
3667 cs461x_poke(card, BA1_CIE, tmp); /* capture interrupt disable */
3670 * Stop playback DMA.
3672 tmp = cs461x_peek(card, BA1_PCTL);
3673 cs461x_poke(card, BA1_PCTL, tmp & 0x0000ffff);
3678 tmp = cs461x_peek(card, BA1_CCTL);
3679 cs461x_poke(card, BA1_CCTL, tmp & 0xffff0000);
3683 card->pm.dmabuf_swptr_play = card->states[1]->dmabuf.swptr;
3684 card->pm.dmabuf_count_play = card->states[1]->dmabuf.count;
3688 card->pm.dmabuf_swptr_capture = card->states[0]->dmabuf.swptr;
3689 card->pm.dmabuf_count_capture = card->states[0]->dmabuf.count;
3692 cs46xx_ac97_suspend(card);
3695 * Reset the processor.
3699 cs461x_proc_stop(card);
3702 * Power down the DAC and ADC. For now leave the other areas on.
3704 cs_ac97_set(card->ac97_codec[0], AC97_POWER_CONTROL, 0x0300);
3707 * Power down the PLL.
3709 cs461x_pokeBA0(card, BA0_CLKCR1, 0);
3712 * Turn off the Processor by turning off the software clock enable flag in
3713 * the clock control register.
3715 tmp = cs461x_peekBA0(card, BA0_CLKCR1) & ~CLKCR1_SWCE;
3716 cs461x_pokeBA0(card, BA0_CLKCR1, tmp);
3718 card->active_ctrl(card,-1);
3720 card->pm.flags &= ~CS46XX_PM_SUSPENDING;
3721 card->pm.flags |= CS46XX_PM_SUSPENDED;
3725 CS_DBGOUT(CS_PM | CS_FUNCTION, 4,
3726 printk("cs46xx: cs46xx_suspend()- flags=0x%x\n",
3727 (unsigned)card->pm.flags));
3731 static int cs46xx_resume(struct cs_card *card)
3735 CS_DBGOUT(CS_PM | CS_FUNCTION, 4,
3736 printk( "cs46xx: cs46xx_resume()+ flags=0x%x\n",
3737 (unsigned)card->pm.flags));
3738 if(!(card->pm.flags & CS46XX_PM_SUSPENDED))
3740 CS_DBGOUT(CS_PM | CS_ERROR, 2,
3741 printk("cs46xx: cs46xx_resume() unable to resume, not SUSPENDED\n"));
3744 card->pm.flags |= CS46XX_PM_RESUMING;
3745 card->pm.flags &= ~CS46XX_PM_SUSPENDED;
3747 card->active_ctrl(card, 1);
3751 if (cs_hardware_init(card) != 0)
3753 CS_DBGOUT(CS_PM | CS_ERROR, 4, printk(
3754 "cs46xx: cs46xx_resume()- ERROR in cs_hardware_init()\n"));
3755 mdelay(10 * cs_laptop_wait);
3763 CS_DBGOUT(CS_PM | CS_ERROR, 1, printk(
3764 "cs46xx: cs46xx_resume()- cs_hardware_init() failed, retried %d times.\n",i));
3768 if(cs46xx_restart_part(card))
3770 CS_DBGOUT(CS_PM | CS_ERROR, 4, printk(
3771 "cs46xx: cs46xx_resume(): cs46xx_restart_part() returned error\n"));
3774 card->active_ctrl(card, -1);
3776 CS_DBGOUT(CS_PM | CS_FUNCTION, 4, printk("cs46xx: cs46xx_resume()- flags=0x%x\n",
3777 (unsigned)card->pm.flags));
3781 static /*const*/ struct file_operations cs461x_fops = {
3782 CS_OWNER CS_THIS_MODULE
3783 .llseek = no_llseek,
3790 .release = cs_release,
3793 /* Write AC97 codec registers */
3796 static u16 _cs_ac97_get(struct ac97_codec *dev, u8 reg)
3798 struct cs_card *card = dev->private_data;
3804 * 1. Write ACCAD = Command Address Register = 46Ch for AC97 register address
3805 * 2. Write ACCDA = Command Data Register = 470h for data to write to AC97
3806 * 3. Write ACCTL = Control Register = 460h for initiating the write
3807 * 4. Read ACCTL = 460h, DCV should be reset by now and 460h = 17h
3808 * 5. if DCV not cleared, break and return error
3809 * 6. Read ACSTS = Status Register = 464h, check VSTS bit
3812 cs461x_peekBA0(card, BA0_ACSDA);
3815 * Setup the AC97 control registers on the CS461x to send the
3816 * appropriate command to the AC97 to perform the read.
3817 * ACCAD = Command Address Register = 46Ch
3818 * ACCDA = Command Data Register = 470h
3819 * ACCTL = Control Register = 460h
3820 * set DCV - will clear when process completed
3821 * set CRW - Read command
3822 * set VFRM - valid frame enabled
3823 * set ESYN - ASYNC generation enabled
3824 * set RSTN - ARST# inactive, AC97 codec not reset
3827 cs461x_pokeBA0(card, BA0_ACCAD, reg);
3828 cs461x_pokeBA0(card, BA0_ACCDA, 0);
3829 cs461x_pokeBA0(card, BA0_ACCTL, ACCTL_DCV | ACCTL_CRW |
3830 ACCTL_VFRM | ACCTL_ESYN |
3835 * Wait for the read to occur.
3837 if(!(card->pm.flags & CS46XX_PM_IDLE))
3840 loopcnt = 500 * cs_laptop_wait;
3841 loopcnt *= cs_laptop_wait;
3842 for (count = 0; count < loopcnt; count++) {
3844 * First, we want to wait for a short time.
3846 udelay(10 * cs_laptop_wait);
3848 * Now, check to see if the read has completed.
3849 * ACCTL = 460h, DCV should be reset by now and 460h = 17h
3851 if (!(cs461x_peekBA0(card, BA0_ACCTL) & ACCTL_DCV))
3856 * Make sure the read completed.
3858 if (cs461x_peekBA0(card, BA0_ACCTL) & ACCTL_DCV) {
3859 CS_DBGOUT(CS_ERROR, 1, printk(KERN_WARNING
3860 "cs46xx: AC'97 read problem (ACCTL_DCV), reg = 0x%x returning 0xffff\n", reg));
3865 * Wait for the valid status bit to go active.
3868 if(!(card->pm.flags & CS46XX_PM_IDLE))
3872 loopcnt *= cs_laptop_wait;
3873 for (count = 0; count < loopcnt; count++) {
3875 * Read the AC97 status register.
3876 * ACSTS = Status Register = 464h
3877 * VSTS - Valid Status
3879 if (cs461x_peekBA0(card, BA0_ACSTS) & ACSTS_VSTS)
3881 udelay(10 * cs_laptop_wait);
3885 * Make sure we got valid status.
3887 if (!( (tmp=cs461x_peekBA0(card, BA0_ACSTS)) & ACSTS_VSTS)) {
3888 CS_DBGOUT(CS_ERROR, 2, printk(KERN_WARNING
3889 "cs46xx: AC'97 read problem (ACSTS_VSTS), reg = 0x%x val=0x%x 0xffff \n",
3895 * Read the data returned from the AC97 register.
3896 * ACSDA = Status Data Register = 474h
3898 CS_DBGOUT(CS_FUNCTION, 9, printk(KERN_INFO
3899 "cs46xx: cs_ac97_get() reg = 0x%x, val = 0x%x, BA0_ACCAD = 0x%x\n",
3900 reg, cs461x_peekBA0(card, BA0_ACSDA),
3901 cs461x_peekBA0(card, BA0_ACCAD)));
3902 ret = cs461x_peekBA0(card, BA0_ACSDA);
3906 static u16 cs_ac97_get(struct ac97_codec *dev, u8 reg)
3909 struct cs_card *card = dev->private_data;
3911 spin_lock(&card->ac97_lock);
3912 ret = _cs_ac97_get(dev, reg);
3913 spin_unlock(&card->ac97_lock);
3917 static void cs_ac97_set(struct ac97_codec *dev, u8 reg, u16 val)
3919 struct cs_card *card = dev->private_data;
3923 spin_lock(&card->ac97_lock);
3925 if(reg == AC97_CD_VOL)
3927 val2 = _cs_ac97_get(dev, AC97_CD_VOL);
3932 * 1. Write ACCAD = Command Address Register = 46Ch for AC97 register address
3933 * 2. Write ACCDA = Command Data Register = 470h for data to write to AC97
3934 * 3. Write ACCTL = Control Register = 460h for initiating the write
3935 * 4. Read ACCTL = 460h, DCV should be reset by now and 460h = 07h
3936 * 5. if DCV not cleared, break and return error
3940 * Setup the AC97 control registers on the CS461x to send the
3941 * appropriate command to the AC97 to perform the read.
3942 * ACCAD = Command Address Register = 46Ch
3943 * ACCDA = Command Data Register = 470h
3944 * ACCTL = Control Register = 460h
3945 * set DCV - will clear when process completed
3946 * reset CRW - Write command
3947 * set VFRM - valid frame enabled
3948 * set ESYN - ASYNC generation enabled
3949 * set RSTN - ARST# inactive, AC97 codec not reset
3951 cs461x_pokeBA0(card, BA0_ACCAD, reg);
3952 cs461x_pokeBA0(card, BA0_ACCDA, val);
3953 cs461x_peekBA0(card, BA0_ACCTL);
3954 cs461x_pokeBA0(card, BA0_ACCTL, 0 | ACCTL_VFRM | ACCTL_ESYN | ACCTL_RSTN);
3955 cs461x_pokeBA0(card, BA0_ACCTL, ACCTL_DCV | ACCTL_VFRM |
3956 ACCTL_ESYN | ACCTL_RSTN);
3957 for (count = 0; count < 1000; count++) {
3959 * First, we want to wait for a short time.
3961 udelay(10 * cs_laptop_wait);
3963 * Now, check to see if the write has completed.
3964 * ACCTL = 460h, DCV should be reset by now and 460h = 07h
3966 if (!(cs461x_peekBA0(card, BA0_ACCTL) & ACCTL_DCV))
3970 * Make sure the write completed.
3972 if (cs461x_peekBA0(card, BA0_ACCTL) & ACCTL_DCV)
3974 CS_DBGOUT(CS_ERROR, 1, printk(KERN_WARNING
3975 "cs46xx: AC'97 write problem, reg = 0x%x, val = 0x%x\n", reg, val));
3978 spin_unlock(&card->ac97_lock);
3981 * Adjust power if the mixer is selected/deselected according
3984 * IF the CD is a valid input source (mixer or direct) AND
3985 * the CD is not muted THEN power is needed
3987 * We do two things. When record select changes the input to
3988 * add/remove the CD we adjust the power count if the CD is
3991 * When the CD mute changes we adjust the power level if the
3992 * CD was a valid input.
3994 * We also check for CD volume != 0, as the CD mute isn't
3995 * normally tweaked from userspace.
3998 /* CD mute change ? */
4000 if(reg==AC97_CD_VOL)
4002 /* Mute bit change ? */
4003 if((val2^val)&0x8000 || ((val2 == 0x1f1f || val == 0x1f1f) && val2 != val))
4005 /* This is a hack but its cleaner than the alternatives.
4006 Right now card->ac97_codec[0] might be NULL as we are
4007 still doing codec setup. This does an early assignment
4008 to avoid the problem if it occurs */
4010 if(card->ac97_codec[0]==NULL)
4011 card->ac97_codec[0]=dev;
4014 if(val&0x8000 || val == 0x1f1f)
4015 card->amplifier_ctrl(card, -1);
4016 else /* Mute off power on */
4019 card->amp_init(card);
4020 card->amplifier_ctrl(card, 1);
4027 /* OSS /dev/mixer file operation methods */
4029 static int cs_open_mixdev(struct inode *inode, struct file *file)
4032 unsigned int minor = iminor(inode);
4033 struct cs_card *card=NULL;
4034 struct list_head *entry;
4037 CS_DBGOUT(CS_FUNCTION | CS_OPEN, 4,
4038 printk(KERN_INFO "cs46xx: cs_open_mixdev()+\n"));
4040 list_for_each(entry, &cs46xx_devs)
4042 card = list_entry(entry, struct cs_card, list);
4043 for (i = 0; i < NR_AC97; i++)
4044 if (card->ac97_codec[i] != NULL &&
4045 card->ac97_codec[i]->dev_mixer == minor)
4050 CS_DBGOUT(CS_FUNCTION | CS_OPEN | CS_ERROR, 2,
4051 printk(KERN_INFO "cs46xx: cs46xx_open_mixdev()- -ENODEV\n"));
4055 if(!card->ac97_codec[i])
4057 file->private_data = card->ac97_codec[i];
4059 card->active_ctrl(card,1);
4060 if(!CS_IN_USE(&card->mixer_use_cnt))
4062 if( (tmp = cs46xx_powerup(card, CS_POWER_MIXVON )) )
4064 CS_DBGOUT(CS_ERROR | CS_INIT, 1, printk(KERN_INFO
4065 "cs46xx: cs_open_mixdev() powerup failure (0x%x)\n",tmp) );
4069 card->amplifier_ctrl(card, 1);
4070 CS_INC_USE_COUNT(&card->mixer_use_cnt);
4071 CS_DBGOUT(CS_FUNCTION | CS_OPEN, 4,
4072 printk(KERN_INFO "cs46xx: cs_open_mixdev()- 0\n"));
4073 return nonseekable_open(inode, file);
4076 static int cs_release_mixdev(struct inode *inode, struct file *file)
4078 unsigned int minor = iminor(inode);
4079 struct cs_card *card=NULL;
4080 struct list_head *entry;
4084 CS_DBGOUT(CS_FUNCTION | CS_RELEASE, 4,
4085 printk(KERN_INFO "cs46xx: cs_release_mixdev()+\n"));
4086 list_for_each(entry, &cs46xx_devs)
4088 card = list_entry(entry, struct cs_card, list);
4089 for (i = 0; i < NR_AC97; i++)
4090 if (card->ac97_codec[i] != NULL &&
4091 card->ac97_codec[i]->dev_mixer == minor)
4096 CS_DBGOUT(CS_FUNCTION | CS_OPEN | CS_ERROR, 2,
4097 printk(KERN_INFO "cs46xx: cs46xx_open_mixdev()- -ENODEV\n"));
4101 if(!CS_DEC_AND_TEST(&card->mixer_use_cnt))
4103 CS_DBGOUT(CS_FUNCTION | CS_RELEASE, 4,
4104 printk(KERN_INFO "cs46xx: cs_release_mixdev()- no powerdown, usecnt>0\n"));
4105 card->active_ctrl(card, -1);
4106 card->amplifier_ctrl(card, -1);
4110 * ok, no outstanding mixer opens, so powerdown.
4112 if( (tmp = cs461x_powerdown(card, CS_POWER_MIXVON, CS_FALSE )) )
4114 CS_DBGOUT(CS_ERROR | CS_INIT, 1, printk(KERN_INFO
4115 "cs46xx: cs_release_mixdev() powerdown MIXVON failure (0x%x)\n",tmp) );
4116 card->active_ctrl(card, -1);
4117 card->amplifier_ctrl(card, -1);
4120 card->active_ctrl(card, -1);
4121 card->amplifier_ctrl(card, -1);
4122 CS_DBGOUT(CS_FUNCTION | CS_RELEASE, 4,
4123 printk(KERN_INFO "cs46xx: cs_release_mixdev()- 0\n"));
4127 static int cs_ioctl_mixdev(struct inode *inode, struct file *file, unsigned int cmd,
4130 struct ac97_codec *codec = (struct ac97_codec *)file->private_data;
4131 struct cs_card *card=NULL;
4132 struct list_head *entry;
4133 unsigned long __user *p = (long __user *)arg;
4135 #if CSDEBUG_INTERFACE
4138 if( (cmd == SOUND_MIXER_CS_GETDBGMASK) ||
4139 (cmd == SOUND_MIXER_CS_SETDBGMASK) ||
4140 (cmd == SOUND_MIXER_CS_GETDBGLEVEL) ||
4141 (cmd == SOUND_MIXER_CS_SETDBGLEVEL) ||
4142 (cmd == SOUND_MIXER_CS_APM))
4147 case SOUND_MIXER_CS_GETDBGMASK:
4148 return put_user(cs_debugmask, p);
4150 case SOUND_MIXER_CS_GETDBGLEVEL:
4151 return put_user(cs_debuglevel, p);
4153 case SOUND_MIXER_CS_SETDBGMASK:
4154 if (get_user(val, p))
4159 case SOUND_MIXER_CS_SETDBGLEVEL:
4160 if (get_user(val, p))
4162 cs_debuglevel = val;
4165 case SOUND_MIXER_CS_APM:
4166 if (get_user(val, p))
4168 if(val == CS_IOCTL_CMD_SUSPEND)
4170 list_for_each(entry, &cs46xx_devs)
4172 card = list_entry(entry, struct cs_card, list);
4173 cs46xx_suspend(card, PMSG_ON);
4177 else if(val == CS_IOCTL_CMD_RESUME)
4179 list_for_each(entry, &cs46xx_devs)
4181 card = list_entry(entry, struct cs_card, list);
4182 cs46xx_resume(card);
4187 CS_DBGOUT(CS_ERROR, 1, printk(KERN_INFO
4188 "cs46xx: mixer_ioctl(): invalid APM cmd (%d)\n",
4194 CS_DBGOUT(CS_ERROR, 1, printk(KERN_INFO
4195 "cs46xx: mixer_ioctl(): ERROR unknown debug cmd\n") );
4200 return codec->mixer_ioctl(codec, cmd, arg);
4203 static /*const*/ struct file_operations cs_mixer_fops = {
4204 CS_OWNER CS_THIS_MODULE
4205 .llseek = no_llseek,
4206 .ioctl = cs_ioctl_mixdev,
4207 .open = cs_open_mixdev,
4208 .release = cs_release_mixdev,
4211 /* AC97 codec initialisation. */
4212 static int __init cs_ac97_init(struct cs_card *card)
4216 struct ac97_codec *codec;
4219 CS_DBGOUT(CS_FUNCTION | CS_INIT, 2, printk(KERN_INFO
4220 "cs46xx: cs_ac97_init()+\n") );
4222 for (num_ac97 = 0; num_ac97 < NR_AC97; num_ac97++) {
4223 if ((codec = ac97_alloc_codec()) == NULL)
4226 /* initialize some basic codec information, other fields will be filled
4227 in ac97_probe_codec */
4228 codec->private_data = card;
4229 codec->id = num_ac97;
4231 codec->codec_read = cs_ac97_get;
4232 codec->codec_write = cs_ac97_set;
4234 if (ac97_probe_codec(codec) == 0)
4236 CS_DBGOUT(CS_FUNCTION | CS_INIT, 2, printk(KERN_INFO
4237 "cs46xx: cs_ac97_init()- codec number %d not found\n",
4239 card->ac97_codec[num_ac97] = NULL;
4242 CS_DBGOUT(CS_FUNCTION | CS_INIT, 2, printk(KERN_INFO
4243 "cs46xx: cs_ac97_init() found codec %d\n",num_ac97) );
4245 eid = cs_ac97_get(codec, AC97_EXTENDED_ID);
4249 printk(KERN_WARNING "cs46xx: codec %d not present\n",num_ac97);
4250 ac97_release_codec(codec);
4254 card->ac97_features = eid;
4256 if ((codec->dev_mixer = register_sound_mixer(&cs_mixer_fops, -1)) < 0) {
4257 printk(KERN_ERR "cs46xx: couldn't register mixer!\n");
4258 ac97_release_codec(codec);
4261 card->ac97_codec[num_ac97] = codec;
4263 CS_DBGOUT(CS_FUNCTION | CS_INIT, 2, printk(KERN_INFO
4264 "cs46xx: cs_ac97_init() ac97_codec[%d] set to %p\n",
4265 (unsigned int)num_ac97,
4267 /* if there is no secondary codec at all, don't probe any more */
4274 CS_DBGOUT(CS_FUNCTION | CS_INIT, 2, printk(KERN_INFO
4275 "cs46xx: cs_ac97_init()- %d\n", (unsigned int)num_ac97));
4280 * load the static image into the DSP
4282 #include "cs461x_image.h"
4283 static void cs461x_download_image(struct cs_card *card)
4285 unsigned i, j, temp1, temp2, offset, count;
4286 unsigned char __iomem *pBA1 = ioremap(card->ba1_addr, 0x40000);
4287 for( i=0; i < CLEAR__COUNT; i++)
4289 offset = ClrStat[i].BA1__DestByteOffset;
4290 count = ClrStat[i].BA1__SourceSize;
4291 for( temp1 = offset; temp1<(offset+count); temp1+=4 )
4292 writel(0, pBA1+temp1);
4295 for(i=0; i<FILL__COUNT; i++)
4297 temp2 = FillStat[i].Offset;
4298 for(j=0; j<(FillStat[i].Size)/4; j++)
4300 temp1 = (FillStat[i]).pFill[j];
4301 writel(temp1, pBA1+temp2+j*4);
4312 static void cs461x_reset(struct cs_card *card)
4317 * Write the reset bit of the SP control register.
4319 cs461x_poke(card, BA1_SPCR, SPCR_RSTSP);
4322 * Write the control register.
4324 cs461x_poke(card, BA1_SPCR, SPCR_DRQEN);
4327 * Clear the trap registers.
4329 for (idx = 0; idx < 8; idx++) {
4330 cs461x_poke(card, BA1_DREG, DREG_REGID_TRAP_SELECT + idx);
4331 cs461x_poke(card, BA1_TWPR, 0xFFFF);
4333 cs461x_poke(card, BA1_DREG, 0);
4336 * Set the frame timer to reflect the number of cycles per frame.
4338 cs461x_poke(card, BA1_FRMT, 0xadf);
4341 static void cs461x_clear_serial_FIFOs(struct cs_card *card, int type)
4343 int idx, loop, startfifo=0, endfifo=0, powerdown1 = 0;
4347 * See if the devices are powered down. If so, we must power them up first
4348 * or they will not respond.
4350 if (!((tmp = cs461x_peekBA0(card, BA0_CLKCR1)) & CLKCR1_SWCE)) {
4351 cs461x_pokeBA0(card, BA0_CLKCR1, tmp | CLKCR1_SWCE);
4356 * We want to clear out the serial port FIFOs so we don't end up playing
4357 * whatever random garbage happens to be in them. We fill the sample FIFOS
4358 * with zero (silence).
4360 cs461x_pokeBA0(card, BA0_SERBWP, 0);
4363 * Check for which FIFO locations to clear, if we are currently
4364 * playing or capturing then we don't want to put in 128 bytes of
4367 if(type & CS_TYPE_DAC)
4372 if(type & CS_TYPE_ADC)
4379 * Fill sample FIFO locations (256 locations total).
4381 for (idx = startfifo; idx < endfifo; idx++) {
4383 * Make sure the previous FIFO write operation has completed.
4385 for (loop = 0; loop < 5; loop++) {
4387 if (!(cs461x_peekBA0(card, BA0_SERBST) & SERBST_WBSY))
4390 if (cs461x_peekBA0(card, BA0_SERBST) & SERBST_WBSY) {
4392 cs461x_pokeBA0(card, BA0_CLKCR1, tmp);
4395 * Write the serial port FIFO index.
4397 cs461x_pokeBA0(card, BA0_SERBAD, idx);
4399 * Tell the serial port to load the new value into the FIFO location.
4401 cs461x_pokeBA0(card, BA0_SERBCM, SERBCM_WRC);
4404 * Now, if we powered up the devices, then power them back down again.
4405 * This is kinda ugly, but should never happen.
4408 cs461x_pokeBA0(card, BA0_CLKCR1, tmp);
4412 static int cs461x_powerdown(struct cs_card *card, unsigned int type, int suspendflag)
4415 unsigned int tmp=0,muted=0;
4417 CS_DBGOUT(CS_FUNCTION, 4, printk(KERN_INFO
4418 "cs46xx: cs461x_powerdown()+ type=0x%x\n",type));
4419 if(!cs_powerdown && !suspendflag)
4421 CS_DBGOUT(CS_FUNCTION, 8, printk(KERN_INFO
4422 "cs46xx: cs461x_powerdown() DISABLED exiting\n"));
4425 tmp = cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL);
4426 CS_DBGOUT(CS_FUNCTION, 8, printk(KERN_INFO
4427 "cs46xx: cs461x_powerdown() powerdown reg=0x%x\n",tmp));
4429 * if powering down only the VREF, and not powering down the DAC/ADC,
4430 * then do not power down the VREF, UNLESS both the DAC and ADC are not
4431 * currently powered down. If powering down DAC and ADC, then
4432 * it is possible to power down the VREF (ON).
4434 if ( ((type & CS_POWER_MIXVON) &&
4435 (!(type & CS_POWER_ADC) || (!(type & CS_POWER_DAC))) )
4437 ((tmp & CS_AC97_POWER_CONTROL_ADC_ON) ||
4438 (tmp & CS_AC97_POWER_CONTROL_DAC_ON) ) )
4440 CS_DBGOUT(CS_FUNCTION, 8, printk(KERN_INFO
4441 "cs46xx: cs461x_powerdown()- 0 unable to powerdown. tmp=0x%x\n",tmp));
4445 * for now, always keep power to the mixer block.
4446 * not sure why it's a problem but it seems to be if we power off.
4448 type &= ~CS_POWER_MIXVON;
4449 type &= ~CS_POWER_MIXVOFF;
4452 * Power down indicated areas.
4454 if(type & CS_POWER_MIXVOFF)
4457 CS_DBGOUT(CS_FUNCTION, 4,
4458 printk(KERN_INFO "cs46xx: cs461x_powerdown()+ MIXVOFF\n"));
4460 * Power down the MIXER (VREF ON) on the AC97 card.
4462 tmp = cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL);
4463 if (tmp & CS_AC97_POWER_CONTROL_MIXVOFF_ON)
4467 cs_mute(card, CS_TRUE);
4470 tmp |= CS_AC97_POWER_CONTROL_MIXVOFF;
4471 cs_ac97_set(card->ac97_codec[0], AC97_POWER_CONTROL, tmp );
4473 * Now, we wait until we sample a ready state.
4475 for (count = 0; count < 32; count++) {
4477 * First, lets wait a short while to let things settle out a
4478 * bit, and to prevent retrying the read too quickly.
4483 * Read the current state of the power control register.
4485 if (!(cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) &
4486 CS_AC97_POWER_CONTROL_MIXVOFF_ON))
4491 * Check the status..
4493 if (cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) &
4494 CS_AC97_POWER_CONTROL_MIXVOFF_ON)
4496 CS_DBGOUT(CS_ERROR, 1, printk(KERN_WARNING
4497 "cs46xx: powerdown MIXVOFF failed\n"));
4502 if(type & CS_POWER_MIXVON)
4505 CS_DBGOUT(CS_FUNCTION, 4,
4506 printk(KERN_INFO "cs46xx: cs461x_powerdown()+ MIXVON\n"));
4508 * Power down the MIXER (VREF ON) on the AC97 card.
4510 tmp = cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL);
4511 if (tmp & CS_AC97_POWER_CONTROL_MIXVON_ON)
4515 cs_mute(card, CS_TRUE);
4518 tmp |= CS_AC97_POWER_CONTROL_MIXVON;
4519 cs_ac97_set(card->ac97_codec[0], AC97_POWER_CONTROL, tmp );
4521 * Now, we wait until we sample a ready state.
4523 for (count = 0; count < 32; count++) {
4525 * First, lets wait a short while to let things settle out a
4526 * bit, and to prevent retrying the read too quickly.
4531 * Read the current state of the power control register.
4533 if (!(cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) &
4534 CS_AC97_POWER_CONTROL_MIXVON_ON))
4539 * Check the status..
4541 if (cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) &
4542 CS_AC97_POWER_CONTROL_MIXVON_ON)
4544 CS_DBGOUT(CS_ERROR, 1, printk(KERN_WARNING
4545 "cs46xx: powerdown MIXVON failed\n"));
4550 if(type & CS_POWER_ADC)
4553 * Power down the ADC on the AC97 card.
4555 CS_DBGOUT(CS_FUNCTION, 4, printk(KERN_INFO "cs46xx: cs461x_powerdown()+ ADC\n"));
4556 tmp = cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL);
4557 if (tmp & CS_AC97_POWER_CONTROL_ADC_ON)
4561 cs_mute(card, CS_TRUE);
4564 tmp |= CS_AC97_POWER_CONTROL_ADC;
4565 cs_ac97_set(card->ac97_codec[0], AC97_POWER_CONTROL, tmp );
4568 * Now, we wait until we sample a ready state.
4570 for (count = 0; count < 32; count++) {
4572 * First, lets wait a short while to let things settle out a
4573 * bit, and to prevent retrying the read too quickly.
4578 * Read the current state of the power control register.
4580 if (!(cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) &
4581 CS_AC97_POWER_CONTROL_ADC_ON))
4586 * Check the status..
4588 if (cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) &
4589 CS_AC97_POWER_CONTROL_ADC_ON)
4591 CS_DBGOUT(CS_ERROR, 1, printk(KERN_WARNING
4592 "cs46xx: powerdown ADC failed\n"));
4597 if(type & CS_POWER_DAC)
4600 * Power down the DAC on the AC97 card.
4603 CS_DBGOUT(CS_FUNCTION, 4,
4604 printk(KERN_INFO "cs46xx: cs461x_powerdown()+ DAC\n"));
4605 tmp = cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL);
4606 if (tmp & CS_AC97_POWER_CONTROL_DAC_ON)
4610 cs_mute(card, CS_TRUE);
4613 tmp |= CS_AC97_POWER_CONTROL_DAC;
4614 cs_ac97_set(card->ac97_codec[0], AC97_POWER_CONTROL, tmp );
4616 * Now, we wait until we sample a ready state.
4618 for (count = 0; count < 32; count++) {
4620 * First, lets wait a short while to let things settle out a
4621 * bit, and to prevent retrying the read too quickly.
4626 * Read the current state of the power control register.
4628 if (!(cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) &
4629 CS_AC97_POWER_CONTROL_DAC_ON))
4634 * Check the status..
4636 if (cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) &
4637 CS_AC97_POWER_CONTROL_DAC_ON)
4639 CS_DBGOUT(CS_ERROR, 1, printk(KERN_WARNING
4640 "cs46xx: powerdown DAC failed\n"));
4645 tmp = cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL);
4647 cs_mute(card, CS_FALSE);
4648 CS_DBGOUT(CS_FUNCTION, 4, printk(KERN_INFO
4649 "cs46xx: cs461x_powerdown()- 0 tmp=0x%x\n",tmp));
4653 static int cs46xx_powerup(struct cs_card *card, unsigned int type)
4656 unsigned int tmp=0,muted=0;
4658 CS_DBGOUT(CS_FUNCTION, 8, printk(KERN_INFO
4659 "cs46xx: cs46xx_powerup()+ type=0x%x\n",type));
4661 * check for VREF and powerup if need to.
4663 if(type & CS_POWER_MIXVON)
4664 type |= CS_POWER_MIXVOFF;
4665 if(type & (CS_POWER_DAC | CS_POWER_ADC))
4666 type |= CS_POWER_MIXVON | CS_POWER_MIXVOFF;
4669 * Power up indicated areas.
4671 if(type & CS_POWER_MIXVOFF)
4674 CS_DBGOUT(CS_FUNCTION, 4,
4675 printk(KERN_INFO "cs46xx: cs46xx_powerup()+ MIXVOFF\n"));
4677 * Power up the MIXER (VREF ON) on the AC97 card.
4679 tmp = cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL);
4680 if (!(tmp & CS_AC97_POWER_CONTROL_MIXVOFF_ON))
4684 cs_mute(card, CS_TRUE);
4687 tmp &= ~CS_AC97_POWER_CONTROL_MIXVOFF;
4688 cs_ac97_set(card->ac97_codec[0], AC97_POWER_CONTROL, tmp );
4690 * Now, we wait until we sample a ready state.
4692 for (count = 0; count < 32; count++) {
4694 * First, lets wait a short while to let things settle out a
4695 * bit, and to prevent retrying the read too quickly.
4700 * Read the current state of the power control register.
4702 if (cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) &
4703 CS_AC97_POWER_CONTROL_MIXVOFF_ON)
4708 * Check the status..
4710 if (!(cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) &
4711 CS_AC97_POWER_CONTROL_MIXVOFF_ON))
4713 CS_DBGOUT(CS_ERROR, 1, printk(KERN_WARNING
4714 "cs46xx: powerup MIXVOFF failed\n"));
4719 if(type & CS_POWER_MIXVON)
4722 CS_DBGOUT(CS_FUNCTION, 4,
4723 printk(KERN_INFO "cs46xx: cs46xx_powerup()+ MIXVON\n"));
4725 * Power up the MIXER (VREF ON) on the AC97 card.
4727 tmp = cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL);
4728 if (!(tmp & CS_AC97_POWER_CONTROL_MIXVON_ON))
4732 cs_mute(card, CS_TRUE);
4735 tmp &= ~CS_AC97_POWER_CONTROL_MIXVON;
4736 cs_ac97_set(card->ac97_codec[0], AC97_POWER_CONTROL, tmp );
4738 * Now, we wait until we sample a ready state.
4740 for (count = 0; count < 32; count++) {
4742 * First, lets wait a short while to let things settle out a
4743 * bit, and to prevent retrying the read too quickly.
4748 * Read the current state of the power control register.
4750 if (cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) &
4751 CS_AC97_POWER_CONTROL_MIXVON_ON)
4756 * Check the status..
4758 if (!(cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) &
4759 CS_AC97_POWER_CONTROL_MIXVON_ON))
4761 CS_DBGOUT(CS_ERROR, 1, printk(KERN_WARNING
4762 "cs46xx: powerup MIXVON failed\n"));
4767 if(type & CS_POWER_ADC)
4770 * Power up the ADC on the AC97 card.
4772 CS_DBGOUT(CS_FUNCTION, 4, printk(KERN_INFO "cs46xx: cs46xx_powerup()+ ADC\n"));
4773 tmp = cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL);
4774 if (!(tmp & CS_AC97_POWER_CONTROL_ADC_ON))
4778 cs_mute(card, CS_TRUE);
4781 tmp &= ~CS_AC97_POWER_CONTROL_ADC;
4782 cs_ac97_set(card->ac97_codec[0], AC97_POWER_CONTROL, tmp );
4785 * Now, we wait until we sample a ready state.
4787 for (count = 0; count < 32; count++) {
4789 * First, lets wait a short while to let things settle out a
4790 * bit, and to prevent retrying the read too quickly.
4795 * Read the current state of the power control register.
4797 if (cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) &
4798 CS_AC97_POWER_CONTROL_ADC_ON)
4803 * Check the status..
4805 if (!(cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) &
4806 CS_AC97_POWER_CONTROL_ADC_ON))
4808 CS_DBGOUT(CS_ERROR, 1, printk(KERN_WARNING
4809 "cs46xx: powerup ADC failed\n"));
4814 if(type & CS_POWER_DAC)
4817 * Power up the DAC on the AC97 card.
4820 CS_DBGOUT(CS_FUNCTION, 4,
4821 printk(KERN_INFO "cs46xx: cs46xx_powerup()+ DAC\n"));
4822 tmp = cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL);
4823 if (!(tmp & CS_AC97_POWER_CONTROL_DAC_ON))
4827 cs_mute(card, CS_TRUE);
4830 tmp &= ~CS_AC97_POWER_CONTROL_DAC;
4831 cs_ac97_set(card->ac97_codec[0], AC97_POWER_CONTROL, tmp );
4833 * Now, we wait until we sample a ready state.
4835 for (count = 0; count < 32; count++) {
4837 * First, lets wait a short while to let things settle out a
4838 * bit, and to prevent retrying the read too quickly.
4843 * Read the current state of the power control register.
4845 if (cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) &
4846 CS_AC97_POWER_CONTROL_DAC_ON)
4851 * Check the status..
4853 if (!(cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) &
4854 CS_AC97_POWER_CONTROL_DAC_ON))
4856 CS_DBGOUT(CS_ERROR, 1, printk(KERN_WARNING
4857 "cs46xx: powerup DAC failed\n"));
4862 tmp = cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL);
4864 cs_mute(card, CS_FALSE);
4865 CS_DBGOUT(CS_FUNCTION, 4, printk(KERN_INFO
4866 "cs46xx: cs46xx_powerup()- 0 tmp=0x%x\n",tmp));
4871 static void cs461x_proc_start(struct cs_card *card)
4876 * Set the frame timer to reflect the number of cycles per frame.
4878 cs461x_poke(card, BA1_FRMT, 0xadf);
4880 * Turn on the run, run at frame, and DMA enable bits in the local copy of
4881 * the SP control register.
4883 cs461x_poke(card, BA1_SPCR, SPCR_RUN | SPCR_RUNFR | SPCR_DRQEN);
4885 * Wait until the run at frame bit resets itself in the SP control
4888 for (cnt = 0; cnt < 25; cnt++) {
4890 if (!(cs461x_peek(card, BA1_SPCR) & SPCR_RUNFR))
4894 if (cs461x_peek(card, BA1_SPCR) & SPCR_RUNFR)
4895 printk(KERN_WARNING "cs46xx: SPCR_RUNFR never reset\n");
4898 static void cs461x_proc_stop(struct cs_card *card)
4901 * Turn off the run, run at frame, and DMA enable bits in the local copy of
4902 * the SP control register.
4904 cs461x_poke(card, BA1_SPCR, 0);
4907 static int cs_hardware_init(struct cs_card *card)
4909 unsigned long end_time;
4910 unsigned int tmp,count;
4912 CS_DBGOUT(CS_FUNCTION | CS_INIT, 2, printk(KERN_INFO
4913 "cs46xx: cs_hardware_init()+\n") );
4915 * First, blast the clock control register to zero so that the PLL starts
4916 * out in a known state, and blast the master serial port control register
4917 * to zero so that the serial ports also start out in a known state.
4919 cs461x_pokeBA0(card, BA0_CLKCR1, 0);
4920 cs461x_pokeBA0(card, BA0_SERMC1, 0);
4923 * If we are in AC97 mode, then we must set the part to a host controlled
4924 * AC-link. Otherwise, we won't be able to bring up the link.
4926 cs461x_pokeBA0(card, BA0_SERACC, SERACC_HSP | SERACC_CODEC_TYPE_1_03); /* 1.03 card */
4927 /* cs461x_pokeBA0(card, BA0_SERACC, SERACC_HSP | SERACC_CODEC_TYPE_2_0); */ /* 2.00 card */
4930 * Drive the ARST# pin low for a minimum of 1uS (as defined in the AC97
4931 * spec) and then drive it high. This is done for non AC97 modes since
4932 * there might be logic external to the CS461x that uses the ARST# line
4935 cs461x_pokeBA0(card, BA0_ACCTL, 1);
4937 cs461x_pokeBA0(card, BA0_ACCTL, 0);
4939 cs461x_pokeBA0(card, BA0_ACCTL, ACCTL_RSTN);
4942 * The first thing we do here is to enable sync generation. As soon
4943 * as we start receiving bit clock, we'll start producing the SYNC
4946 cs461x_pokeBA0(card, BA0_ACCTL, ACCTL_ESYN | ACCTL_RSTN);
4949 * Now wait for a short while to allow the AC97 part to start
4950 * generating bit clock (so we don't try to start the PLL without an
4953 mdelay(5 * cs_laptop_wait); /* 1 should be enough ?? (and pigs might fly) */
4956 * Set the serial port timing configuration, so that
4957 * the clock control circuit gets its clock from the correct place.
4959 cs461x_pokeBA0(card, BA0_SERMC1, SERMC1_PTC_AC97);
4962 * The part seems to not be ready for a while after a resume.
4963 * so, if we are resuming, then wait for 700 mils. Note that 600 mils
4964 * is not enough for some platforms! tested on an IBM Thinkpads and
4967 if(!(card->pm.flags & CS46XX_PM_IDLE))
4970 * Write the selected clock control setup to the hardware. Do not turn on
4971 * SWCE yet (if requested), so that the devices clocked by the output of
4972 * PLL are not clocked until the PLL is stable.
4974 cs461x_pokeBA0(card, BA0_PLLCC, PLLCC_LPF_1050_2780_KHZ | PLLCC_CDR_73_104_MHZ);
4975 cs461x_pokeBA0(card, BA0_PLLM, 0x3a);
4976 cs461x_pokeBA0(card, BA0_CLKCR2, CLKCR2_PDIVS_8);
4981 cs461x_pokeBA0(card, BA0_CLKCR1, CLKCR1_PLLP);
4984 * Wait until the PLL has stabilized.
4986 mdelay(5 * cs_laptop_wait); /* Again 1 should be enough ?? */
4989 * Turn on clocking of the core so that we can setup the serial ports.
4991 tmp = cs461x_peekBA0(card, BA0_CLKCR1) | CLKCR1_SWCE;
4992 cs461x_pokeBA0(card, BA0_CLKCR1, tmp);
4995 * Fill the serial port FIFOs with silence.
4997 cs461x_clear_serial_FIFOs(card,CS_TYPE_DAC | CS_TYPE_ADC);
5000 * Set the serial port FIFO pointer to the first sample in the FIFO.
5002 /* cs461x_pokeBA0(card, BA0_SERBSP, 0); */
5005 * Write the serial port configuration to the part. The master
5006 * enable bit is not set until all other values have been written.
5008 cs461x_pokeBA0(card, BA0_SERC1, SERC1_SO1F_AC97 | SERC1_SO1EN);
5009 cs461x_pokeBA0(card, BA0_SERC2, SERC2_SI1F_AC97 | SERC1_SO1EN);
5010 cs461x_pokeBA0(card, BA0_SERMC1, SERMC1_PTC_AC97 | SERMC1_MSPE);
5013 mdelay(5 * cs_laptop_wait); /* Shouldnt be needed ?? */
5016 * If we are resuming under 2.2.x then we can not schedule a timeout.
5017 * so, just spin the CPU.
5019 if(card->pm.flags & CS46XX_PM_IDLE)
5022 * Wait for the card ready signal from the AC97 card.
5024 end_time = jiffies + 3 * (HZ >> 2);
5027 * Read the AC97 status register to see if we've seen a CODEC READY
5028 * signal from the AC97 card.
5030 if (cs461x_peekBA0(card, BA0_ACSTS) & ACSTS_CRDY)
5032 current->state = TASK_UNINTERRUPTIBLE;
5033 schedule_timeout(1);
5034 } while (time_before(jiffies, end_time));
5038 for (count = 0; count < 100; count++) {
5039 // First, we want to wait for a short time.
5040 udelay(25 * cs_laptop_wait);
5042 if (cs461x_peekBA0(card, BA0_ACSTS) & ACSTS_CRDY)
5048 * Make sure CODEC is READY.
5050 if (!(cs461x_peekBA0(card, BA0_ACSTS) & ACSTS_CRDY)) {
5051 CS_DBGOUT(CS_ERROR | CS_INIT, 1, printk(KERN_WARNING
5052 "cs46xx: create - never read card ready from AC'97\n"));
5053 CS_DBGOUT(CS_ERROR | CS_INIT, 1, printk(KERN_WARNING
5054 "cs46xx: probably not a bug, try using the CS4232 driver,\n"));
5055 CS_DBGOUT(CS_ERROR | CS_INIT, 1, printk(KERN_WARNING
5056 "cs46xx: or turn off any automatic Power Management support in the BIOS.\n"));
5061 * Assert the vaid frame signal so that we can start sending commands
5064 cs461x_pokeBA0(card, BA0_ACCTL, ACCTL_VFRM | ACCTL_ESYN | ACCTL_RSTN);
5066 if(card->pm.flags & CS46XX_PM_IDLE)
5069 * Wait until we've sampled input slots 3 and 4 as valid, meaning that
5070 * the card is pumping ADC data across the AC-link.
5072 end_time = jiffies + 3 * (HZ >> 2);
5075 * Read the input slot valid register and see if input slots 3 and
5078 if ((cs461x_peekBA0(card, BA0_ACISV) & (ACISV_ISV3 | ACISV_ISV4)) == (ACISV_ISV3 | ACISV_ISV4))
5080 current->state = TASK_UNINTERRUPTIBLE;
5081 schedule_timeout(1);
5082 } while (time_before(jiffies, end_time));
5086 for (count = 0; count < 100; count++) {
5087 // First, we want to wait for a short time.
5088 udelay(25 * cs_laptop_wait);
5090 if ((cs461x_peekBA0(card, BA0_ACISV) & (ACISV_ISV3 | ACISV_ISV4)) == (ACISV_ISV3 | ACISV_ISV4))
5095 * Make sure input slots 3 and 4 are valid. If not, then return
5098 if ((cs461x_peekBA0(card, BA0_ACISV) & (ACISV_ISV3 | ACISV_ISV4)) != (ACISV_ISV3 | ACISV_ISV4)) {
5099 printk(KERN_WARNING "cs46xx: create - never read ISV3 & ISV4 from AC'97\n");
5104 * Now, assert valid frame and the slot 3 and 4 valid bits. This will
5105 * commense the transfer of digital audio data to the AC97 card.
5107 cs461x_pokeBA0(card, BA0_ACOSV, ACOSV_SLV3 | ACOSV_SLV4);
5110 * Turn off the Processor by turning off the software clock enable flag in
5111 * the clock control register.
5113 /* tmp = cs461x_peekBA0(card, BA0_CLKCR1) & ~CLKCR1_SWCE; */
5114 /* cs461x_pokeBA0(card, BA0_CLKCR1, tmp); */
5117 * Reset the processor.
5122 * Download the image to the processor.
5125 cs461x_download_image(card);
5128 * Stop playback DMA.
5130 tmp = cs461x_peek(card, BA1_PCTL);
5131 card->pctl = tmp & 0xffff0000;
5132 cs461x_poke(card, BA1_PCTL, tmp & 0x0000ffff);
5137 tmp = cs461x_peek(card, BA1_CCTL);
5138 card->cctl = tmp & 0x0000ffff;
5139 cs461x_poke(card, BA1_CCTL, tmp & 0xffff0000);
5141 /* initialize AC97 codec and register /dev/mixer */
5142 if(card->pm.flags & CS46XX_PM_IDLE)
5144 if (cs_ac97_init(card) <= 0)
5146 CS_DBGOUT(CS_ERROR | CS_INIT, 1, printk(KERN_INFO
5147 "cs46xx: cs_ac97_init() failure\n") );
5153 cs46xx_ac97_resume(card);
5156 cs461x_proc_start(card);
5159 * Enable interrupts on the part.
5161 cs461x_pokeBA0(card, BA0_HICR, HICR_IEV | HICR_CHGM);
5163 tmp = cs461x_peek(card, BA1_PFIE);
5165 cs461x_poke(card, BA1_PFIE, tmp); /* playback interrupt enable */
5167 tmp = cs461x_peek(card, BA1_CIE);
5170 cs461x_poke(card, BA1_CIE, tmp); /* capture interrupt enable */
5173 * If IDLE then Power down the part. We will power components up
5174 * when we need them.
5176 if(card->pm.flags & CS46XX_PM_IDLE)
5180 if( (tmp = cs46xx_powerup(card, CS_POWER_DAC | CS_POWER_ADC |
5181 CS_POWER_MIXVON )) )
5183 CS_DBGOUT(CS_ERROR | CS_INIT, 1, printk(KERN_INFO
5184 "cs46xx: cs461x_powerup() failure (0x%x)\n",tmp) );
5190 if( (tmp = cs461x_powerdown(card, CS_POWER_DAC | CS_POWER_ADC |
5191 CS_POWER_MIXVON, CS_FALSE )) )
5193 CS_DBGOUT(CS_ERROR | CS_INIT, 1, printk(KERN_INFO
5194 "cs46xx: cs461x_powerdown() failure (0x%x)\n",tmp) );
5199 CS_DBGOUT(CS_FUNCTION | CS_INIT, 2, printk(KERN_INFO
5200 "cs46xx: cs_hardware_init()- 0\n"));
5204 /* install the driver, we do not allocate hardware channel nor DMA buffer now, they are defered
5205 until "ACCESS" time (in prog_dmabuf called by open/read/write/ioctl/mmap) */
5216 void (*amp)(struct cs_card *, int);
5217 void (*amp_init)(struct cs_card *);
5218 void (*active)(struct cs_card *, int);
5221 static struct cs_card_type cards[] = {
5225 .name = "Genius Soundmaker 128 value",
5237 .name = "Mitac MI6020/21",
5243 .name = "Hercules Game Theatre XP",
5244 .amp = amp_hercules,
5249 .name = "Hercules Game Theatre XP",
5250 .amp = amp_hercules,
5255 .name = "Hercules Game Theatre XP",
5256 .amp = amp_hercules,
5261 .name = "Hercules Game Theatre XP",
5262 .amp = amp_hercules,
5267 .name = "Hercules Game Theatre XP",
5268 .amp = amp_hercules,
5273 .name = "Hercules Game Theatre XP",
5274 .amp = amp_hercules,
5279 .name = "Hercules Fortissimo II",
5282 /* Not sure if the 570 needs the clkrun hack */
5284 .vendor = PCI_VENDOR_ID_IBM,
5286 .name = "Thinkpad 570",
5288 .active = clkrun_hack,
5291 .vendor = PCI_VENDOR_ID_IBM,
5293 .name = "Thinkpad 600X/A20/T20",
5295 .active = clkrun_hack,
5298 .vendor = PCI_VENDOR_ID_IBM,
5300 .name = "Thinkpad 600E (unsupported)",
5303 .name = "Card without SSID set",
5308 MODULE_AUTHOR("Alan Cox <alan@redhat.com>, Jaroslav Kysela, <pcaudio@crystal.cirrus.com>");
5309 MODULE_DESCRIPTION("Crystal SoundFusion Audio Support");
5310 MODULE_LICENSE("GPL");
5313 static const char cs46xx_banner[] = KERN_INFO "Crystal 4280/46xx + AC97 Audio, version " CS46XX_MAJOR_VERSION "." CS46XX_MINOR_VERSION "." CS46XX_ARCH ", " __TIME__ " " __DATE__ "\n";
5314 static const char fndmsg[] = KERN_INFO "cs46xx: Found %d audio device(s).\n";
5316 static int __devinit cs46xx_probe(struct pci_dev *pci_dev,
5317 const struct pci_device_id *pciid)
5320 u16 ss_card, ss_vendor;
5321 struct cs_card *card;
5322 dma_addr_t dma_mask;
5323 struct cs_card_type *cp = &cards[0];
5325 CS_DBGOUT(CS_FUNCTION | CS_INIT, 2,
5326 printk(KERN_INFO "cs46xx: probe()+\n"));
5328 dma_mask = 0xffffffff; /* this enables playback and recording */
5329 if (pci_enable_device(pci_dev)) {
5330 CS_DBGOUT(CS_INIT | CS_ERROR, 1, printk(KERN_ERR
5331 "cs46xx: pci_enable_device() failed\n"));
5334 if (!RSRCISMEMORYREGION(pci_dev, 0) ||
5335 !RSRCISMEMORYREGION(pci_dev, 1)) {
5336 CS_DBGOUT(CS_ERROR, 1, printk(KERN_ERR
5337 "cs46xx: probe()- Memory region not assigned\n"));
5340 if (pci_dev->irq == 0) {
5341 CS_DBGOUT(CS_ERROR, 1, printk(KERN_ERR
5342 "cs46xx: probe() IRQ not assigned\n"));
5345 if (!pci_dma_supported(pci_dev, 0xffffffff)) {
5346 CS_DBGOUT(CS_ERROR, 1, printk(KERN_ERR
5347 "cs46xx: probe() architecture does not support 32bit PCI busmaster DMA\n"));
5350 pci_read_config_word(pci_dev, PCI_SUBSYSTEM_VENDOR_ID, &ss_vendor);
5351 pci_read_config_word(pci_dev, PCI_SUBSYSTEM_ID, &ss_card);
5353 if ((card = kmalloc(sizeof(struct cs_card), GFP_KERNEL)) == NULL) {
5354 printk(KERN_ERR "cs46xx: out of memory\n");
5357 memset(card, 0, sizeof(*card));
5358 card->ba0_addr = RSRCADDRESS(pci_dev, 0);
5359 card->ba1_addr = RSRCADDRESS(pci_dev, 1);
5360 card->pci_dev = pci_dev;
5361 card->irq = pci_dev->irq;
5362 card->magic = CS_CARD_MAGIC;
5363 spin_lock_init(&card->lock);
5364 spin_lock_init(&card->ac97_lock);
5366 pci_set_master(pci_dev);
5368 printk(cs46xx_banner);
5369 printk(KERN_INFO "cs46xx: Card found at 0x%08lx and 0x%08lx, IRQ %d\n",
5370 card->ba0_addr, card->ba1_addr, card->irq);
5372 card->alloc_pcm_channel = cs_alloc_pcm_channel;
5373 card->alloc_rec_pcm_channel = cs_alloc_rec_pcm_channel;
5374 card->free_pcm_channel = cs_free_pcm_channel;
5375 card->amplifier_ctrl = amp_none;
5376 card->active_ctrl = amp_none;
5380 if(cp->vendor == ss_vendor && cp->id == ss_card)
5382 card->amplifier_ctrl = cp->amp;
5384 card->active_ctrl = cp->active;
5386 card->amp_init = cp->amp_init;
5393 printk(KERN_INFO "cs46xx: Unknown card (%04X:%04X) at 0x%08lx/0x%08lx, IRQ %d\n",
5394 ss_vendor, ss_card, card->ba0_addr, card->ba1_addr, card->irq);
5398 printk(KERN_INFO "cs46xx: %s (%04X:%04X) at 0x%08lx/0x%08lx, IRQ %d\n",
5399 cp->name, ss_vendor, ss_card, card->ba0_addr, card->ba1_addr, card->irq);
5402 if (card->amplifier_ctrl==NULL)
5404 card->amplifier_ctrl = amp_none;
5405 card->active_ctrl = clkrun_hack;
5408 if (external_amp == 1)
5410 printk(KERN_INFO "cs46xx: Crystal EAPD support forced on.\n");
5411 card->amplifier_ctrl = amp_voyetra;
5416 printk(KERN_INFO "cs46xx: Activating CLKRUN hack for Thinkpad.\n");
5417 card->active_ctrl = clkrun_hack;
5420 * The thinkpads don't work well without runtime updating on their kernel
5421 * delay values (or any laptop with variable CPU speeds really).
5422 * so, just to be safe set the init delay to 2100. Eliminates
5423 * failures on T21 Thinkpads. remove this code when the udelay
5424 * and mdelay kernel code is replaced by a pm timer, or the delays
5425 * work well for battery and/or AC power both.
5427 if(card->active_ctrl == clkrun_hack)
5432 if((card->active_ctrl == clkrun_hack) && !(powerdown == 1))
5435 * for some currently unknown reason, powering down the DAC and ADC component
5436 * blocks on thinkpads causes some funky behavior... distoorrrtion and ac97
5437 * codec access problems. probably the serial clock becomes unsynced.
5438 * added code to sync the chips back up, but only helped about 70% the time.
5444 card->active_ctrl(card, 1);
5446 /* claim our iospace and irq */
5448 card->ba0 = ioremap_nocache(card->ba0_addr, CS461X_BA0_SIZE);
5449 card->ba1.name.data0 = ioremap_nocache(card->ba1_addr + BA1_SP_DMEM0, CS461X_BA1_DATA0_SIZE);
5450 card->ba1.name.data1 = ioremap_nocache(card->ba1_addr + BA1_SP_DMEM1, CS461X_BA1_DATA1_SIZE);
5451 card->ba1.name.pmem = ioremap_nocache(card->ba1_addr + BA1_SP_PMEM, CS461X_BA1_PRG_SIZE);
5452 card->ba1.name.reg = ioremap_nocache(card->ba1_addr + BA1_SP_REG, CS461X_BA1_REG_SIZE);
5454 CS_DBGOUT(CS_INIT, 4, printk(KERN_INFO
5455 "cs46xx: card=%p card->ba0=%p\n",card,card->ba0) );
5456 CS_DBGOUT(CS_INIT, 4, printk(KERN_INFO
5457 "cs46xx: card->ba1=%p %p %p %p\n",
5458 card->ba1.name.data0,
5459 card->ba1.name.data1,
5460 card->ba1.name.pmem,
5461 card->ba1.name.reg) );
5463 if(card->ba0 == 0 || card->ba1.name.data0 == 0 ||
5464 card->ba1.name.data1 == 0 || card->ba1.name.pmem == 0 ||
5465 card->ba1.name.reg == 0)
5468 if (request_irq(card->irq, &cs_interrupt, SA_SHIRQ, "cs46xx", card)) {
5469 printk(KERN_ERR "cs46xx: unable to allocate irq %d\n", card->irq);
5472 /* register /dev/dsp */
5473 if ((card->dev_audio = register_sound_dsp(&cs461x_fops, -1)) < 0) {
5474 printk(KERN_ERR "cs46xx: unable to register dsp\n");
5478 /* register /dev/midi */
5479 if((card->dev_midi = register_sound_midi(&cs_midi_fops, -1)) < 0)
5480 printk(KERN_ERR "cs46xx: unable to register midi\n");
5482 card->pm.flags |= CS46XX_PM_IDLE;
5485 if (cs_hardware_init(card) != 0)
5487 CS_DBGOUT(CS_ERROR, 4, printk(
5488 "cs46xx: ERROR in cs_hardware_init()... retrying\n"));
5489 for (j = 0; j < NR_AC97; j++)
5490 if (card->ac97_codec[j] != NULL) {
5491 unregister_sound_mixer(card->ac97_codec[j]->dev_mixer);
5492 ac97_release_codec(card->ac97_codec[j]);
5494 mdelay(10 * cs_laptop_wait);
5501 CS_DBGOUT(CS_PM | CS_ERROR, 1, printk(
5502 "cs46xx: cs46xx_probe()- cs_hardware_init() failed, retried %d times.\n",i));
5503 unregister_sound_dsp(card->dev_audio);
5505 unregister_sound_midi(card->dev_midi);
5509 init_waitqueue_head(&card->midi.open_wait);
5510 init_MUTEX(&card->midi.open_sem);
5511 init_waitqueue_head(&card->midi.iwait);
5512 init_waitqueue_head(&card->midi.owait);
5513 cs461x_pokeBA0(card, BA0_MIDCR, MIDCR_MRST);
5514 cs461x_pokeBA0(card, BA0_MIDCR, 0);
5517 * Check if we have to init the amplifier, but probably already done
5518 * since the CD logic in the ac97 init code will turn on the ext amp.
5522 card->active_ctrl(card, -1);
5524 PCI_SET_DRIVER_DATA(pci_dev, card);
5525 PCI_SET_DMA_MASK(pci_dev, dma_mask);
5526 list_add(&card->list, &cs46xx_devs);
5528 CS_DBGOUT(CS_PM, 9, printk(KERN_INFO "cs46xx: pm.flags=0x%x card=%p\n",
5529 (unsigned)card->pm.flags,card));
5531 CS_DBGOUT(CS_INIT | CS_FUNCTION, 2, printk(KERN_INFO
5532 "cs46xx: probe()- device allocated successfully\n"));
5536 free_irq(card->irq, card);
5540 if(card->ba1.name.data0)
5541 iounmap(card->ba1.name.data0);
5542 if(card->ba1.name.data1)
5543 iounmap(card->ba1.name.data1);
5544 if(card->ba1.name.pmem)
5545 iounmap(card->ba1.name.pmem);
5546 if(card->ba1.name.reg)
5547 iounmap(card->ba1.name.reg);
5549 CS_DBGOUT(CS_INIT | CS_ERROR, 1, printk(KERN_INFO
5550 "cs46xx: probe()- no device allocated\n"));
5554 // ---------------------------------------------------------------------
5556 static void __devexit cs46xx_remove(struct pci_dev *pci_dev)
5558 struct cs_card *card = PCI_GET_DRIVER_DATA(pci_dev);
5562 CS_DBGOUT(CS_INIT | CS_FUNCTION, 2, printk(KERN_INFO
5563 "cs46xx: cs46xx_remove()+\n"));
5565 card->active_ctrl(card,1);
5567 tmp = cs461x_peek(card, BA1_PFIE);
5570 cs461x_poke(card, BA1_PFIE, tmp); /* playback interrupt disable */
5572 tmp = cs461x_peek(card, BA1_CIE);
5575 cs461x_poke(card, BA1_CIE, tmp); /* capture interrupt disable */
5578 * Stop playback DMA.
5580 tmp = cs461x_peek(card, BA1_PCTL);
5581 cs461x_poke(card, BA1_PCTL, tmp & 0x0000ffff);
5586 tmp = cs461x_peek(card, BA1_CCTL);
5587 cs461x_poke(card, BA1_CCTL, tmp & 0xffff0000);
5590 * Reset the processor.
5594 cs461x_proc_stop(card);
5597 * Power down the DAC and ADC. We will power them up (if) when we need
5600 if( (tmp = cs461x_powerdown(card, CS_POWER_DAC | CS_POWER_ADC |
5601 CS_POWER_MIXVON, CS_TRUE )) )
5603 CS_DBGOUT(CS_ERROR | CS_INIT, 1, printk(KERN_INFO
5604 "cs46xx: cs461x_powerdown() failure (0x%x)\n",tmp) );
5608 * Power down the PLL.
5610 cs461x_pokeBA0(card, BA0_CLKCR1, 0);
5613 * Turn off the Processor by turning off the software clock enable flag in
5614 * the clock control register.
5616 tmp = cs461x_peekBA0(card, BA0_CLKCR1) & ~CLKCR1_SWCE;
5617 cs461x_pokeBA0(card, BA0_CLKCR1, tmp);
5619 card->active_ctrl(card,-1);
5621 /* free hardware resources */
5622 free_irq(card->irq, card);
5624 iounmap(card->ba1.name.data0);
5625 iounmap(card->ba1.name.data1);
5626 iounmap(card->ba1.name.pmem);
5627 iounmap(card->ba1.name.reg);
5629 /* unregister audio devices */
5630 for (i = 0; i < NR_AC97; i++)
5631 if (card->ac97_codec[i] != NULL) {
5632 unregister_sound_mixer(card->ac97_codec[i]->dev_mixer);
5633 ac97_release_codec(card->ac97_codec[i]);
5635 unregister_sound_dsp(card->dev_audio);
5637 unregister_sound_midi(card->dev_midi);
5638 list_del(&card->list);
5640 PCI_SET_DRIVER_DATA(pci_dev,NULL);
5642 CS_DBGOUT(CS_INIT | CS_FUNCTION, 2, printk(KERN_INFO
5643 "cs46xx: cs46xx_remove()-: remove successful\n"));
5648 CS46XX_4612, /* same as 4630 */
5649 CS46XX_4615, /* same as 4624 */
5652 static struct pci_device_id cs46xx_pci_tbl[] = {
5654 .vendor = PCI_VENDOR_ID_CIRRUS,
5655 .device = PCI_DEVICE_ID_CIRRUS_4610,
5656 .subvendor = PCI_ANY_ID,
5657 .subdevice = PCI_ANY_ID,
5658 .driver_data = CS46XX_4610,
5661 .vendor = PCI_VENDOR_ID_CIRRUS,
5662 .device = PCI_DEVICE_ID_CIRRUS_4612,
5663 .subvendor = PCI_ANY_ID,
5664 .subdevice = PCI_ANY_ID,
5665 .driver_data = CS46XX_4612,
5668 .vendor = PCI_VENDOR_ID_CIRRUS,
5669 .device = PCI_DEVICE_ID_CIRRUS_4615,
5670 .subvendor = PCI_ANY_ID,
5671 .subdevice = PCI_ANY_ID,
5672 .driver_data = CS46XX_4615,
5677 MODULE_DEVICE_TABLE(pci, cs46xx_pci_tbl);
5679 static struct pci_driver cs46xx_pci_driver = {
5681 .id_table = cs46xx_pci_tbl,
5682 .probe = cs46xx_probe,
5683 .remove = __devexit_p(cs46xx_remove),
5684 .suspend = CS46XX_SUSPEND_TBL,
5685 .resume = CS46XX_RESUME_TBL,
5688 static int __init cs46xx_init_module(void)
5691 CS_DBGOUT(CS_INIT | CS_FUNCTION, 2, printk(KERN_INFO
5692 "cs46xx: cs46xx_init_module()+ \n"));
5693 rtn = pci_module_init(&cs46xx_pci_driver);
5697 CS_DBGOUT(CS_ERROR | CS_INIT, 1, printk(
5698 "cs46xx: Unable to detect valid cs46xx device\n"));
5701 CS_DBGOUT(CS_INIT | CS_FUNCTION, 2,
5702 printk(KERN_INFO "cs46xx: cs46xx_init_module()- (%d)\n",rtn));
5706 static void __exit cs46xx_cleanup_module(void)
5708 pci_unregister_driver(&cs46xx_pci_driver);
5709 CS_DBGOUT(CS_INIT | CS_FUNCTION, 2,
5710 printk(KERN_INFO "cs46xx: cleanup_cs46xx() finished\n"));
5713 module_init(cs46xx_init_module);
5714 module_exit(cs46xx_cleanup_module);
5716 #if CS46XX_ACPI_SUPPORT
5717 static int cs46xx_suspend_tbl(struct pci_dev *pcidev, pm_message_t state)
5719 struct cs_card *s = PCI_GET_DRIVER_DATA(pcidev);
5720 CS_DBGOUT(CS_PM | CS_FUNCTION, 2,
5721 printk(KERN_INFO "cs46xx: cs46xx_suspend_tbl request\n"));
5722 cs46xx_suspend(s, state);
5726 static int cs46xx_resume_tbl(struct pci_dev *pcidev)
5728 struct cs_card *s = PCI_GET_DRIVER_DATA(pcidev);
5729 CS_DBGOUT(CS_PM | CS_FUNCTION, 2,
5730 printk(KERN_INFO "cs46xx: cs46xx_resume_tbl request\n"));