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>
93 #include <linux/mutex.h>
97 #include <asm/uaccess.h>
99 #include "cs46xxpm-24.h"
100 #include "cs46xx_wrapper-24.h"
103 /* MIDI buffer sizes */
104 #define CS_MIDIINBUF 500
105 #define CS_MIDIOUTBUF 500
107 #define ADC_RUNNING 1
108 #define DAC_RUNNING 2
110 #define CS_FMT_16BIT 1 /* These are fixed in fact */
111 #define CS_FMT_STEREO 2
112 #define CS_FMT_MASK 3
114 #define CS_TYPE_ADC 1
115 #define CS_TYPE_DAC 2
120 #define CS_INC_USE_COUNT(m) (atomic_inc(m))
121 #define CS_DEC_USE_COUNT(m) (atomic_dec(m))
122 #define CS_DEC_AND_TEST(m) (atomic_dec_and_test(m))
123 #define CS_IN_USE(m) (atomic_read(m) != 0)
125 #define CS_DBGBREAKPOINT {__asm__("INT $3");}
130 #define CS461X_BA0_SIZE 0x2000
131 #define CS461X_BA1_DATA0_SIZE 0x3000
132 #define CS461X_BA1_DATA1_SIZE 0x3800
133 #define CS461X_BA1_PRG_SIZE 0x7000
134 #define CS461X_BA1_REG_SIZE 0x0100
136 #define GOF_PER_SEC 200
138 #define CSDEBUG_INTERFACE 1
141 * Turn on/off debugging compilation by using 1/0 respectively for CSDEBUG
144 * CSDEBUG is usual mode is set to 1, then use the
145 * cs_debuglevel and cs_debugmask to turn on or off debugging.
146 * Debug level of 1 has been defined to be kernel errors and info
147 * that should be printed on any released driver.
150 #define CS_DBGOUT(mask,level,x) if((cs_debuglevel >= (level)) && ((mask) & cs_debugmask)) {x;}
152 #define CS_DBGOUT(mask,level,x)
157 #define CS_INIT 0x00000001 /* initialization and probe functions */
158 #define CS_ERROR 0x00000002 /* tmp debugging bit placeholder */
159 #define CS_INTERRUPT 0x00000004 /* interrupt handler (separate from all other) */
160 #define CS_FUNCTION 0x00000008 /* enter/leave functions */
161 #define CS_WAVE_WRITE 0x00000010 /* write information for wave */
162 #define CS_WAVE_READ 0x00000020 /* read information for wave */
163 #define CS_MIDI_WRITE 0x00000040 /* write information for midi */
164 #define CS_MIDI_READ 0x00000080 /* read information for midi */
165 #define CS_MPU401_WRITE 0x00000100 /* write information for mpu401 */
166 #define CS_MPU401_READ 0x00000200 /* read information for mpu401 */
167 #define CS_OPEN 0x00000400 /* all open functions in the driver */
168 #define CS_RELEASE 0x00000800 /* all release functions in the driver */
169 #define CS_PARMS 0x00001000 /* functional and operational parameters */
170 #define CS_IOCTL 0x00002000 /* ioctl (non-mixer) */
171 #define CS_PM 0x00004000 /* PM */
172 #define CS_TMP 0x10000000 /* tmp debug mask bit */
174 #define CS_IOCTL_CMD_SUSPEND 0x1 // suspend
175 #define CS_IOCTL_CMD_RESUME 0x2 // resume
178 static unsigned long cs_debuglevel=1; /* levels range from 1-9 */
179 module_param(cs_debuglevel, ulong, 0644);
180 static unsigned long cs_debugmask=CS_INIT | CS_ERROR; /* use CS_DBGOUT with various mask values */
181 module_param(cs_debugmask, ulong, 0644);
183 static unsigned long hercules_egpio_disable; /* if non-zero set all EGPIO to 0 */
184 module_param(hercules_egpio_disable, ulong, 0);
185 static unsigned long initdelay=700; /* PM delay in millisecs */
186 module_param(initdelay, ulong, 0);
187 static unsigned long powerdown=-1; /* turn on/off powerdown processing in driver */
188 module_param(powerdown, ulong, 0);
189 #define DMABUF_DEFAULTORDER 3
190 static unsigned long defaultorder=DMABUF_DEFAULTORDER;
191 module_param(defaultorder, ulong, 0);
193 static int external_amp;
194 module_param(external_amp, bool, 0);
196 module_param(thinkpad, bool, 0);
199 * set the powerdown module parm to 0 to disable all
200 * powerdown. also set thinkpad to 1 to disable powerdown,
201 * but also to enable the clkrun functionality.
203 static unsigned cs_powerdown=1;
204 static unsigned cs_laptop_wait=1;
206 /* An instance of the 4610 channel */
214 #define CS46XX_MAJOR_VERSION "1"
215 #define CS46XX_MINOR_VERSION "28"
218 #define CS46XX_ARCH "64" //architecture key
220 #define CS46XX_ARCH "32" //architecture key
223 static struct list_head cs46xx_devs = { &cs46xx_devs, &cs46xx_devs };
225 /* magic numbers to protect our data structures */
226 #define CS_CARD_MAGIC 0x43525553 /* "CRUS" */
227 #define CS_STATE_MAGIC 0x4c4f4749 /* "LOGI" */
230 /* maxinum number of AC97 codecs connected, AC97 2.0 defined 4 */
233 static const unsigned sample_size[] = { 1, 2, 2, 4 };
234 static const unsigned sample_shift[] = { 0, 1, 1, 2 };
236 /* "software" or virtual channel, an instance of opened /dev/dsp */
239 struct cs_card *card; /* Card info */
241 /* single open lock mechanism, only used for recording */
242 struct mutex open_mutex;
243 wait_queue_head_t open_wait;
248 /* virtual channel number */
252 /* wave sample stuff */
254 unsigned char fmt, enable;
256 /* hardware channel */
257 struct cs_channel *channel;
258 int pringbuf; /* Software ring slot */
259 void *pbuf; /* 4K hardware DMA buffer */
261 /* OSS buffer management stuff */
263 dma_addr_t dma_handle;
269 void *tmpbuff; /* tmp buffer for sample conversions */
271 dma_addr_t dmaaddr_tmpbuff;
272 unsigned buforder_tmpbuff; /* Log base 2 of size in bytes.. */
274 /* our buffer acts like a circular ring */
275 unsigned hwptr; /* where dma last started, updated by update_ptr */
276 unsigned swptr; /* where driver last clear/filled, updated by read/write */
277 int count; /* bytes to be comsumed or been generated by dma machine */
278 unsigned total_bytes; /* total bytes dmaed by hardware */
279 unsigned blocks; /* total blocks */
281 unsigned error; /* number of over/underruns */
282 unsigned underrun; /* underrun pending before next write has occurred */
283 wait_queue_head_t wait; /* put process on wait queue when no more space in buffer */
285 /* redundant, but makes calculations easier */
288 unsigned fragsamples;
293 unsigned endcleared:1;
295 unsigned update_flag;
296 unsigned ossfragshift;
298 unsigned subdivision;
300 /* Guard against mmap/write/read races */
305 struct cs_channel channel[2];
308 /* We keep cs461x cards in a linked list */
309 struct cs_card *next;
311 /* The cs461x has a certain amount of cross channel interaction
312 so we use a single per card lock */
316 spinlock_t ac97_lock;
318 /* mixer use count */
319 atomic_t mixer_use_cnt;
321 /* PCI device stuff */
322 struct pci_dev * pci_dev;
323 struct list_head list;
325 unsigned int pctl, cctl; /* Hardware DMA flag sets */
327 /* soundcore stuff */
331 /* structures for abstraction of hardware facilities, codecs, banks and channels*/
332 struct ac97_codec *ac97_codec[NR_AC97];
333 struct cs_state *states[2];
337 int amplifier; /* Amplifier control */
338 void (*amplifier_ctrl)(struct cs_card *, int);
339 void (*amp_init)(struct cs_card *);
341 int active; /* Active clocking */
342 void (*active_ctrl)(struct cs_card *, int);
344 /* hardware resources */
345 unsigned long ba0_addr;
346 unsigned long ba1_addr;
363 /* Function support */
364 struct cs_channel *(*alloc_pcm_channel)(struct cs_card *);
365 struct cs_channel *(*alloc_rec_pcm_channel)(struct cs_card *);
366 void (*free_pcm_channel)(struct cs_card *, int chan);
368 /* /dev/midi stuff */
370 unsigned ird, iwr, icnt;
371 unsigned ord, owr, ocnt;
372 wait_queue_head_t open_wait;
373 wait_queue_head_t iwait;
374 wait_queue_head_t owait;
376 unsigned char ibuf[CS_MIDIINBUF];
377 unsigned char obuf[CS_MIDIOUTBUF];
379 struct mutex open_mutex;
384 static int cs_open_mixdev(struct inode *inode, struct file *file);
385 static int cs_release_mixdev(struct inode *inode, struct file *file);
386 static int cs_ioctl_mixdev(struct inode *inode, struct file *file, unsigned int cmd,
388 static int cs_hardware_init(struct cs_card *card);
389 static int cs46xx_powerup(struct cs_card *card, unsigned int type);
390 static int cs461x_powerdown(struct cs_card *card, unsigned int type, int suspendflag);
391 static void cs461x_clear_serial_FIFOs(struct cs_card *card, int type);
392 static int cs46xx_suspend_tbl(struct pci_dev *pcidev, pm_message_t state);
393 static int cs46xx_resume_tbl(struct pci_dev *pcidev);
399 #define SOUND_MIXER_CS_GETDBGLEVEL _SIOWR('M',120, int)
400 #define SOUND_MIXER_CS_SETDBGLEVEL _SIOWR('M',121, int)
401 #define SOUND_MIXER_CS_GETDBGMASK _SIOWR('M',122, int)
402 #define SOUND_MIXER_CS_SETDBGMASK _SIOWR('M',123, int)
403 #define SOUND_MIXER_CS_APM _SIOWR('M',124, int)
405 static void printioctl(unsigned int x)
409 /* these values are incorrect for the ac97 driver, fix.
410 * Index of mixtable1[] member is Device ID
411 * and must be <= SOUND_MIXER_NRDEVICES.
412 * Value of array member is index into s->mix.vol[]
414 static const unsigned char mixtable1[SOUND_MIXER_NRDEVICES] = {
415 [SOUND_MIXER_PCM] = 1, /* voice */
416 [SOUND_MIXER_LINE1] = 2, /* AUX */
417 [SOUND_MIXER_CD] = 3, /* CD */
418 [SOUND_MIXER_LINE] = 4, /* Line */
419 [SOUND_MIXER_SYNTH] = 5, /* FM */
420 [SOUND_MIXER_MIC] = 6, /* Mic */
421 [SOUND_MIXER_SPEAKER] = 7, /* Speaker */
422 [SOUND_MIXER_RECLEV] = 8, /* Recording level */
423 [SOUND_MIXER_VOLUME] = 9 /* Master Volume */
428 case SOUND_MIXER_CS_GETDBGMASK:
429 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_CS_GETDBGMASK: ") );
431 case SOUND_MIXER_CS_GETDBGLEVEL:
432 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_CS_GETDBGLEVEL: ") );
434 case SOUND_MIXER_CS_SETDBGMASK:
435 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_CS_SETDBGMASK: ") );
437 case SOUND_MIXER_CS_SETDBGLEVEL:
438 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_CS_SETDBGLEVEL: ") );
441 CS_DBGOUT(CS_IOCTL, 4, printk("OSS_GETVERSION: ") );
443 case SNDCTL_DSP_SYNC:
444 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_SYNC: ") );
446 case SNDCTL_DSP_SETDUPLEX:
447 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_SETDUPLEX: ") );
449 case SNDCTL_DSP_GETCAPS:
450 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_GETCAPS: ") );
452 case SNDCTL_DSP_RESET:
453 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_RESET: ") );
455 case SNDCTL_DSP_SPEED:
456 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_SPEED: ") );
458 case SNDCTL_DSP_STEREO:
459 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_STEREO: ") );
461 case SNDCTL_DSP_CHANNELS:
462 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_CHANNELS: ") );
464 case SNDCTL_DSP_GETFMTS:
465 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_GETFMTS: ") );
467 case SNDCTL_DSP_SETFMT:
468 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_SETFMT: ") );
470 case SNDCTL_DSP_POST:
471 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_POST: ") );
473 case SNDCTL_DSP_GETTRIGGER:
474 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_GETTRIGGER: ") );
476 case SNDCTL_DSP_SETTRIGGER:
477 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_SETTRIGGER: ") );
479 case SNDCTL_DSP_GETOSPACE:
480 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_GETOSPACE: ") );
482 case SNDCTL_DSP_GETISPACE:
483 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_GETISPACE: ") );
485 case SNDCTL_DSP_NONBLOCK:
486 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_NONBLOCK: ") );
488 case SNDCTL_DSP_GETODELAY:
489 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_GETODELAY: ") );
491 case SNDCTL_DSP_GETIPTR:
492 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_GETIPTR: ") );
494 case SNDCTL_DSP_GETOPTR:
495 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_GETOPTR: ") );
497 case SNDCTL_DSP_GETBLKSIZE:
498 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_GETBLKSIZE: ") );
500 case SNDCTL_DSP_SETFRAGMENT:
501 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_SETFRAGMENT: ") );
503 case SNDCTL_DSP_SUBDIVIDE:
504 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_SUBDIVIDE: ") );
506 case SOUND_PCM_READ_RATE:
507 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_PCM_READ_RATE: ") );
509 case SOUND_PCM_READ_CHANNELS:
510 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_PCM_READ_CHANNELS: ") );
512 case SOUND_PCM_READ_BITS:
513 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_PCM_READ_BITS: ") );
515 case SOUND_PCM_WRITE_FILTER:
516 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_PCM_WRITE_FILTER: ") );
518 case SNDCTL_DSP_SETSYNCRO:
519 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_SETSYNCRO: ") );
521 case SOUND_PCM_READ_FILTER:
522 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_PCM_READ_FILTER: ") );
525 case SOUND_MIXER_PRIVATE1:
526 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_PRIVATE1: ") );
528 case SOUND_MIXER_PRIVATE2:
529 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_PRIVATE2: ") );
531 case SOUND_MIXER_PRIVATE3:
532 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_PRIVATE3: ") );
534 case SOUND_MIXER_PRIVATE4:
535 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_PRIVATE4: ") );
537 case SOUND_MIXER_PRIVATE5:
538 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_PRIVATE5: ") );
540 case SOUND_MIXER_INFO:
541 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_INFO: ") );
543 case SOUND_OLD_MIXER_INFO:
544 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_OLD_MIXER_INFO: ") );
550 case SOUND_MIXER_VOLUME:
551 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_VOLUME: ") );
553 case SOUND_MIXER_SPEAKER:
554 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_SPEAKER: ") );
556 case SOUND_MIXER_RECLEV:
557 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_RECLEV: ") );
559 case SOUND_MIXER_MIC:
560 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_MIC: ") );
562 case SOUND_MIXER_SYNTH:
563 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_SYNTH: ") );
565 case SOUND_MIXER_RECSRC:
566 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_RECSRC: ") );
568 case SOUND_MIXER_DEVMASK:
569 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_DEVMASK: ") );
571 case SOUND_MIXER_RECMASK:
572 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_RECMASK: ") );
574 case SOUND_MIXER_STEREODEVS:
575 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_STEREODEVS: ") );
577 case SOUND_MIXER_CAPS:
578 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_CAPS:") );
582 if (i >= SOUND_MIXER_NRDEVICES || !(vidx = mixtable1[i]))
584 CS_DBGOUT(CS_IOCTL, 4, printk("UNKNOWN IOCTL: 0x%.8x NR=%d ",x,i) );
588 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_IOCTL AC9x: 0x%.8x NR=%d ",
594 CS_DBGOUT(CS_IOCTL, 4, printk("command = 0x%x IOC_NR=%d\n",x, _IOC_NR(x)) );
599 * common I/O routines
602 static void cs461x_poke(struct cs_card *codec, unsigned long reg, unsigned int val)
604 writel(val, codec->ba1.idx[(reg >> 16) & 3]+(reg&0xffff));
607 static unsigned int cs461x_peek(struct cs_card *codec, unsigned long reg)
609 return readl(codec->ba1.idx[(reg >> 16) & 3]+(reg&0xffff));
612 static void cs461x_pokeBA0(struct cs_card *codec, unsigned long reg, unsigned int val)
614 writel(val, codec->ba0+reg);
617 static unsigned int cs461x_peekBA0(struct cs_card *codec, unsigned long reg)
619 return readl(codec->ba0+reg);
623 static u16 cs_ac97_get(struct ac97_codec *dev, u8 reg);
624 static void cs_ac97_set(struct ac97_codec *dev, u8 reg, u16 data);
626 static struct cs_channel *cs_alloc_pcm_channel(struct cs_card *card)
628 if(card->channel[1].used==1)
630 card->channel[1].used=1;
631 card->channel[1].num=1;
632 return &card->channel[1];
635 static struct cs_channel *cs_alloc_rec_pcm_channel(struct cs_card *card)
637 if(card->channel[0].used==1)
639 card->channel[0].used=1;
640 card->channel[0].num=0;
641 return &card->channel[0];
644 static void cs_free_pcm_channel(struct cs_card *card, int channel)
646 card->channel[channel].state = NULL;
647 card->channel[channel].used=0;
651 * setup a divisor value to help with conversion from
652 * 16bit Stereo, down to 8bit stereo/mono or 16bit mono.
653 * assign a divisor of 1 if using 16bit Stereo as that is
654 * the only format that the static image will capture.
656 static void cs_set_divisor(struct dmabuf *dmabuf)
658 if(dmabuf->type == CS_TYPE_DAC)
660 else if( !(dmabuf->fmt & CS_FMT_STEREO) &&
661 (dmabuf->fmt & CS_FMT_16BIT))
663 else if( (dmabuf->fmt & CS_FMT_STEREO) &&
664 !(dmabuf->fmt & CS_FMT_16BIT))
666 else if( !(dmabuf->fmt & CS_FMT_STEREO) &&
667 !(dmabuf->fmt & CS_FMT_16BIT))
672 CS_DBGOUT(CS_PARMS | CS_FUNCTION, 8, printk(
673 "cs46xx: cs_set_divisor()- %s %d\n",
674 (dmabuf->type == CS_TYPE_ADC) ? "ADC" : "DAC",
679 * mute some of the more prevalent registers to avoid popping.
681 static void cs_mute(struct cs_card *card, int state)
683 struct ac97_codec *dev=card->ac97_codec[0];
685 CS_DBGOUT(CS_FUNCTION, 2, printk(KERN_INFO "cs46xx: cs_mute()+ %s\n",
686 (state == CS_TRUE) ? "Muting" : "UnMuting") );
691 * fix pops when powering up on thinkpads
693 card->pm.u32AC97_master_volume = (u32)cs_ac97_get( dev,
694 (u8)BA0_AC97_MASTER_VOLUME);
695 card->pm.u32AC97_headphone_volume = (u32)cs_ac97_get(dev,
696 (u8)BA0_AC97_HEADPHONE_VOLUME);
697 card->pm.u32AC97_master_volume_mono = (u32)cs_ac97_get(dev,
698 (u8)BA0_AC97_MASTER_VOLUME_MONO);
699 card->pm.u32AC97_pcm_out_volume = (u32)cs_ac97_get(dev,
700 (u8)BA0_AC97_PCM_OUT_VOLUME);
702 cs_ac97_set(dev, (u8)BA0_AC97_MASTER_VOLUME, 0x8000);
703 cs_ac97_set(dev, (u8)BA0_AC97_HEADPHONE_VOLUME, 0x8000);
704 cs_ac97_set(dev, (u8)BA0_AC97_MASTER_VOLUME_MONO, 0x8000);
705 cs_ac97_set(dev, (u8)BA0_AC97_PCM_OUT_VOLUME, 0x8000);
709 cs_ac97_set(dev, (u8)BA0_AC97_MASTER_VOLUME, card->pm.u32AC97_master_volume);
710 cs_ac97_set(dev, (u8)BA0_AC97_HEADPHONE_VOLUME, card->pm.u32AC97_headphone_volume);
711 cs_ac97_set(dev, (u8)BA0_AC97_MASTER_VOLUME_MONO, card->pm.u32AC97_master_volume_mono);
712 cs_ac97_set(dev, (u8)BA0_AC97_PCM_OUT_VOLUME, card->pm.u32AC97_pcm_out_volume);
714 CS_DBGOUT(CS_FUNCTION, 2, printk(KERN_INFO "cs46xx: cs_mute()-\n"));
717 /* set playback sample rate */
718 static unsigned int cs_set_dac_rate(struct cs_state * state, unsigned int rate)
720 struct dmabuf *dmabuf = &state->dmabuf;
721 unsigned int tmp1, tmp2;
722 unsigned int phiIncr;
723 unsigned int correctionPerGOF, correctionPerSec;
726 CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: cs_set_dac_rate()+ %d\n",rate) );
729 * Compute the values used to drive the actual sample rate conversion.
730 * The following formulas are being computed, using inline assembly
731 * since we need to use 64 bit arithmetic to compute the values:
733 * phiIncr = floor((Fs,in * 2^26) / Fs,out)
734 * correctionPerGOF = floor((Fs,in * 2^26 - Fs,out * phiIncr) /
736 * ulCorrectionPerSec = Fs,in * 2^26 - Fs,out * phiIncr -M
737 * GOF_PER_SEC * correctionPerGOF
741 * phiIncr:other = dividend:remainder((Fs,in * 2^26) / Fs,out)
742 * correctionPerGOF:correctionPerSec =
743 * dividend:remainder(ulOther / GOF_PER_SEC)
746 phiIncr = tmp1 / 48000;
747 tmp1 -= phiIncr * 48000;
752 tmp1 -= tmp2 * 48000;
753 correctionPerGOF = tmp1 / GOF_PER_SEC;
754 tmp1 -= correctionPerGOF * GOF_PER_SEC;
755 correctionPerSec = tmp1;
758 * Fill in the SampleRateConverter control block.
761 spin_lock_irqsave(&state->card->lock, flags);
762 cs461x_poke(state->card, BA1_PSRC,
763 ((correctionPerSec << 16) & 0xFFFF0000) | (correctionPerGOF & 0xFFFF));
764 cs461x_poke(state->card, BA1_PPI, phiIncr);
765 spin_unlock_irqrestore(&state->card->lock, flags);
768 CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: cs_set_dac_rate()- %d\n",rate) );
772 /* set recording sample rate */
773 static unsigned int cs_set_adc_rate(struct cs_state * state, unsigned int rate)
775 struct dmabuf *dmabuf = &state->dmabuf;
776 struct cs_card *card = state->card;
777 unsigned int phiIncr, coeffIncr, tmp1, tmp2;
778 unsigned int correctionPerGOF, correctionPerSec, initialDelay;
779 unsigned int frameGroupLength, cnt;
781 CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: cs_set_adc_rate()+ %d\n",rate) );
784 * We can only decimate by up to a factor of 1/9th the hardware rate.
785 * Correct the value if an attempt is made to stray outside that limit.
787 if ((rate * 9) < 48000)
791 * We can not capture at at rate greater than the Input Rate (48000).
792 * Return an error if an attempt is made to stray outside that limit.
798 * Compute the values used to drive the actual sample rate conversion.
799 * The following formulas are being computed, using inline assembly
800 * since we need to use 64 bit arithmetic to compute the values:
802 * coeffIncr = -floor((Fs,out * 2^23) / Fs,in)
803 * phiIncr = floor((Fs,in * 2^26) / Fs,out)
804 * correctionPerGOF = floor((Fs,in * 2^26 - Fs,out * phiIncr) /
806 * correctionPerSec = Fs,in * 2^26 - Fs,out * phiIncr -
807 * GOF_PER_SEC * correctionPerGOF
808 * initialDelay = ceil((24 * Fs,in) / Fs,out)
812 * coeffIncr = neg(dividend((Fs,out * 2^23) / Fs,in))
813 * phiIncr:ulOther = dividend:remainder((Fs,in * 2^26) / Fs,out)
814 * correctionPerGOF:correctionPerSec =
815 * dividend:remainder(ulOther / GOF_PER_SEC)
816 * initialDelay = dividend(((24 * Fs,in) + Fs,out - 1) / Fs,out)
820 coeffIncr = tmp1 / 48000;
821 tmp1 -= coeffIncr * 48000;
824 coeffIncr += tmp1 / 48000;
825 coeffIncr ^= 0xFFFFFFFF;
828 phiIncr = tmp1 / rate;
829 tmp1 -= phiIncr * rate;
835 correctionPerGOF = tmp1 / GOF_PER_SEC;
836 tmp1 -= correctionPerGOF * GOF_PER_SEC;
837 correctionPerSec = tmp1;
838 initialDelay = ((48000 * 24) + rate - 1) / rate;
841 * Fill in the VariDecimate control block.
843 spin_lock_irqsave(&card->lock, flags);
844 cs461x_poke(card, BA1_CSRC,
845 ((correctionPerSec << 16) & 0xFFFF0000) | (correctionPerGOF & 0xFFFF));
846 cs461x_poke(card, BA1_CCI, coeffIncr);
847 cs461x_poke(card, BA1_CD,
848 (((BA1_VARIDEC_BUF_1 + (initialDelay << 2)) << 16) & 0xFFFF0000) | 0x80);
849 cs461x_poke(card, BA1_CPI, phiIncr);
850 spin_unlock_irqrestore(&card->lock, flags);
853 * Figure out the frame group length for the write back task. Basically,
854 * this is just the factors of 24000 (2^6*3*5^3) that are not present in
855 * the output sample rate.
857 frameGroupLength = 1;
858 for (cnt = 2; cnt <= 64; cnt *= 2) {
859 if (((rate / cnt) * cnt) != rate)
860 frameGroupLength *= 2;
862 if (((rate / 3) * 3) != rate) {
863 frameGroupLength *= 3;
865 for (cnt = 5; cnt <= 125; cnt *= 5) {
866 if (((rate / cnt) * cnt) != rate)
867 frameGroupLength *= 5;
871 * Fill in the WriteBack control block.
873 spin_lock_irqsave(&card->lock, flags);
874 cs461x_poke(card, BA1_CFG1, frameGroupLength);
875 cs461x_poke(card, BA1_CFG2, (0x00800000 | frameGroupLength));
876 cs461x_poke(card, BA1_CCST, 0x0000FFFF);
877 cs461x_poke(card, BA1_CSPB, ((65536 * rate) / 24000));
878 cs461x_poke(card, (BA1_CSPB + 4), 0x0000FFFF);
879 spin_unlock_irqrestore(&card->lock, flags);
881 CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: cs_set_adc_rate()- %d\n",rate) );
885 /* prepare channel attributes for playback */
886 static void cs_play_setup(struct cs_state *state)
888 struct dmabuf *dmabuf = &state->dmabuf;
889 struct cs_card *card = state->card;
890 unsigned int tmp, Count, playFormat;
892 CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: cs_play_setup()+\n") );
893 cs461x_poke(card, BA1_PVOL, 0x80008000);
895 cs461x_poke(card, BA1_PBA, virt_to_bus(dmabuf->pbuf));
898 playFormat=cs461x_peek(card, BA1_PFIE);
899 if ((dmabuf->fmt & CS_FMT_STEREO)) {
900 playFormat &= ~DMA_RQ_C2_AC_MONO_TO_STEREO;
904 playFormat |= DMA_RQ_C2_AC_MONO_TO_STEREO;
906 if ((dmabuf->fmt & CS_FMT_16BIT)) {
907 playFormat &= ~(DMA_RQ_C2_AC_8_TO_16_BIT
908 | DMA_RQ_C2_AC_SIGNED_CONVERT);
912 playFormat |= (DMA_RQ_C2_AC_8_TO_16_BIT
913 | DMA_RQ_C2_AC_SIGNED_CONVERT);
915 cs461x_poke(card, BA1_PFIE, playFormat);
917 tmp = cs461x_peek(card, BA1_PDTC);
919 cs461x_poke(card, BA1_PDTC, tmp | --Count);
921 CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: cs_play_setup()-\n") );
925 static struct InitStruct
929 } InitArray[] = { {0x00000040, 0x3fc0000f},
930 {0x0000004c, 0x04800000},
932 {0x000000b3, 0x00000780},
933 {0x000000b7, 0x00000000},
934 {0x000000bc, 0x07800000},
936 {0x000000cd, 0x00800000},
940 * "SetCaptureSPValues()" -- Initialize record task values before each
943 static void SetCaptureSPValues(struct cs_card *card)
946 CS_DBGOUT(CS_FUNCTION, 8, printk("cs46xx: SetCaptureSPValues()+\n") );
947 for(i=0; i<sizeof(InitArray)/sizeof(struct InitStruct); i++)
949 offset = InitArray[i].off*4; /* 8bit to 32bit offset value */
950 cs461x_poke(card, offset, InitArray[i].val );
952 CS_DBGOUT(CS_FUNCTION, 8, printk("cs46xx: SetCaptureSPValues()-\n") );
955 /* prepare channel attributes for recording */
956 static void cs_rec_setup(struct cs_state *state)
958 struct cs_card *card = state->card;
959 struct dmabuf *dmabuf = &state->dmabuf;
960 CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: cs_rec_setup()+\n") );
962 SetCaptureSPValues(card);
965 * set the attenuation to 0dB
967 cs461x_poke(card, BA1_CVOL, 0x80008000);
970 * set the physical address of the capture buffer into the SP
972 cs461x_poke(card, BA1_CBA, virt_to_bus(dmabuf->rawbuf));
974 CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: cs_rec_setup()-\n") );
978 /* get current playback/recording dma buffer pointer (byte offset from LBA),
979 called with spinlock held! */
981 static inline unsigned cs_get_dma_addr(struct cs_state *state)
983 struct dmabuf *dmabuf = &state->dmabuf;
986 if ( (!(dmabuf->enable & DAC_RUNNING)) &&
987 (!(dmabuf->enable & ADC_RUNNING) ) )
989 CS_DBGOUT(CS_ERROR, 2, printk(
990 "cs46xx: ERROR cs_get_dma_addr(): not enabled \n") );
995 * granularity is byte boundary, good part.
997 if(dmabuf->enable & DAC_RUNNING)
999 offset = cs461x_peek(state->card, BA1_PBA);
1001 else /* ADC_RUNNING must be set */
1003 offset = cs461x_peek(state->card, BA1_CBA);
1005 CS_DBGOUT(CS_PARMS | CS_FUNCTION, 9,
1006 printk("cs46xx: cs_get_dma_addr() %d\n",offset) );
1007 offset = (u32)bus_to_virt((unsigned long)offset) - (u32)dmabuf->rawbuf;
1008 CS_DBGOUT(CS_PARMS | CS_FUNCTION, 8,
1009 printk("cs46xx: cs_get_dma_addr()- %d\n",offset) );
1013 static void resync_dma_ptrs(struct cs_state *state)
1015 struct dmabuf *dmabuf;
1017 CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: resync_dma_ptrs()+ \n") );
1020 dmabuf = &state->dmabuf;
1021 dmabuf->hwptr=dmabuf->swptr = 0;
1022 dmabuf->pringbuf = 0;
1024 CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: resync_dma_ptrs()- \n") );
1027 /* Stop recording (lock held) */
1028 static inline void __stop_adc(struct cs_state *state)
1030 struct dmabuf *dmabuf = &state->dmabuf;
1031 struct cs_card *card = state->card;
1034 dmabuf->enable &= ~ADC_RUNNING;
1036 tmp = cs461x_peek(card, BA1_CCTL);
1038 cs461x_poke(card, BA1_CCTL, tmp );
1041 static void stop_adc(struct cs_state *state)
1043 unsigned long flags;
1045 CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: stop_adc()+ \n") );
1046 spin_lock_irqsave(&state->card->lock, flags);
1048 spin_unlock_irqrestore(&state->card->lock, flags);
1049 CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: stop_adc()- \n") );
1052 static void start_adc(struct cs_state *state)
1054 struct dmabuf *dmabuf = &state->dmabuf;
1055 struct cs_card *card = state->card;
1056 unsigned long flags;
1059 spin_lock_irqsave(&card->lock, flags);
1060 if (!(dmabuf->enable & ADC_RUNNING) &&
1061 ((dmabuf->mapped || dmabuf->count < (signed)dmabuf->dmasize)
1062 && dmabuf->ready) &&
1063 ((card->pm.flags & CS46XX_PM_IDLE) ||
1064 (card->pm.flags & CS46XX_PM_RESUMED)) )
1066 dmabuf->enable |= ADC_RUNNING;
1067 cs_set_divisor(dmabuf);
1068 tmp = cs461x_peek(card, BA1_CCTL);
1071 CS_DBGOUT(CS_FUNCTION, 2, printk(
1072 "cs46xx: start_adc() poke 0x%x \n",tmp) );
1073 cs461x_poke(card, BA1_CCTL, tmp);
1075 spin_unlock_irqrestore(&card->lock, flags);
1078 /* stop playback (lock held) */
1079 static inline void __stop_dac(struct cs_state *state)
1081 struct dmabuf *dmabuf = &state->dmabuf;
1082 struct cs_card *card = state->card;
1085 dmabuf->enable &= ~DAC_RUNNING;
1087 tmp=cs461x_peek(card, BA1_PCTL);
1089 cs461x_poke(card, BA1_PCTL, tmp);
1092 static void stop_dac(struct cs_state *state)
1094 unsigned long flags;
1096 CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: stop_dac()+ \n") );
1097 spin_lock_irqsave(&state->card->lock, flags);
1099 spin_unlock_irqrestore(&state->card->lock, flags);
1100 CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: stop_dac()- \n") );
1103 static void start_dac(struct cs_state *state)
1105 struct dmabuf *dmabuf = &state->dmabuf;
1106 struct cs_card *card = state->card;
1107 unsigned long flags;
1110 CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: start_dac()+ \n") );
1111 spin_lock_irqsave(&card->lock, flags);
1112 if (!(dmabuf->enable & DAC_RUNNING) &&
1113 ((dmabuf->mapped || dmabuf->count > 0) && dmabuf->ready) &&
1114 ((card->pm.flags & CS46XX_PM_IDLE) ||
1115 (card->pm.flags & CS46XX_PM_RESUMED)) )
1117 dmabuf->enable |= DAC_RUNNING;
1118 tmp = cs461x_peek(card, BA1_PCTL);
1121 CS_DBGOUT(CS_PARMS, 6, printk(
1122 "cs46xx: start_dac() poke card=%p tmp=0x%.08x addr=%p \n",
1123 card, (unsigned)tmp,
1124 card->ba1.idx[(BA1_PCTL >> 16) & 3]+(BA1_PCTL&0xffff) ) );
1125 cs461x_poke(card, BA1_PCTL, tmp);
1127 spin_unlock_irqrestore(&card->lock, flags);
1128 CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: start_dac()- \n") );
1131 #define DMABUF_MINORDER 1
1134 * allocate DMA buffer, playback and recording buffers are separate.
1136 static int alloc_dmabuf(struct cs_state *state)
1139 struct cs_card *card=state->card;
1140 struct dmabuf *dmabuf = &state->dmabuf;
1141 void *rawbuf = NULL;
1142 void *tmpbuff = NULL;
1144 struct page *map, *mapend;
1147 dmabuf->ready = dmabuf->mapped = 0;
1150 * check for order within limits, but do not overwrite value.
1152 if((defaultorder > 1) && (defaultorder < 12))
1157 for (order = df; order >= DMABUF_MINORDER; order--)
1158 if ( (rawbuf = (void *) pci_alloc_consistent(
1159 card->pci_dev, PAGE_SIZE << order, &dmabuf->dmaaddr)))
1162 CS_DBGOUT(CS_ERROR, 1, printk(KERN_ERR
1163 "cs46xx: alloc_dmabuf(): unable to allocate rawbuf\n"));
1166 dmabuf->buforder = order;
1167 dmabuf->rawbuf = rawbuf;
1168 // Now mark the pages as reserved; otherwise the
1169 // remap_pfn_range() in cs46xx_mmap doesn't work.
1170 // 1. get index to last page in mem_map array for rawbuf.
1171 mapend = virt_to_page(dmabuf->rawbuf +
1172 (PAGE_SIZE << dmabuf->buforder) - 1);
1174 // 2. mark each physical page in range as 'reserved'.
1175 for (map = virt_to_page(dmabuf->rawbuf); map <= mapend; map++)
1176 cs4x_mem_map_reserve(map);
1178 CS_DBGOUT(CS_PARMS, 9, printk("cs46xx: alloc_dmabuf(): allocated %ld (order = %d) bytes at %p\n",
1179 PAGE_SIZE << order, order, rawbuf) );
1182 * only allocate the conversion buffer for the ADC
1184 if(dmabuf->type == CS_TYPE_DAC)
1186 dmabuf->tmpbuff = NULL;
1187 dmabuf->buforder_tmpbuff = 0;
1191 * now the temp buffer for 16/8 conversions
1194 tmpbuff = (void *) pci_alloc_consistent(
1195 card->pci_dev, PAGE_SIZE << order, &dmabuf->dmaaddr_tmpbuff);
1199 CS_DBGOUT(CS_PARMS, 9, printk("cs46xx: allocated %ld (order = %d) bytes at %p\n",
1200 PAGE_SIZE << order, order, tmpbuff) );
1202 dmabuf->tmpbuff = tmpbuff;
1203 dmabuf->buforder_tmpbuff = order;
1205 // Now mark the pages as reserved; otherwise the
1206 // remap_pfn_range() in cs46xx_mmap doesn't work.
1207 // 1. get index to last page in mem_map array for rawbuf.
1208 mapend = virt_to_page(dmabuf->tmpbuff +
1209 (PAGE_SIZE << dmabuf->buforder_tmpbuff) - 1);
1211 // 2. mark each physical page in range as 'reserved'.
1212 for (map = virt_to_page(dmabuf->tmpbuff); map <= mapend; map++)
1213 cs4x_mem_map_reserve(map);
1217 /* free DMA buffer */
1218 static void dealloc_dmabuf(struct cs_state *state)
1220 struct dmabuf *dmabuf = &state->dmabuf;
1221 struct page *map, *mapend;
1223 if (dmabuf->rawbuf) {
1224 // Undo prog_dmabuf()'s marking the pages as reserved
1225 mapend = virt_to_page(dmabuf->rawbuf +
1226 (PAGE_SIZE << dmabuf->buforder) - 1);
1227 for (map = virt_to_page(dmabuf->rawbuf); map <= mapend; map++)
1228 cs4x_mem_map_unreserve(map);
1229 free_dmabuf(state->card, dmabuf);
1232 if (dmabuf->tmpbuff) {
1233 // Undo prog_dmabuf()'s marking the pages as reserved
1234 mapend = virt_to_page(dmabuf->tmpbuff +
1235 (PAGE_SIZE << dmabuf->buforder_tmpbuff) - 1);
1236 for (map = virt_to_page(dmabuf->tmpbuff); map <= mapend; map++)
1237 cs4x_mem_map_unreserve(map);
1238 free_dmabuf2(state->card, dmabuf);
1241 dmabuf->rawbuf = NULL;
1242 dmabuf->tmpbuff = NULL;
1243 dmabuf->mapped = dmabuf->ready = 0;
1247 static int __prog_dmabuf(struct cs_state *state)
1249 struct dmabuf *dmabuf = &state->dmabuf;
1250 unsigned long flags;
1251 unsigned long allocated_pages, allocated_bytes;
1252 unsigned long tmp1, tmp2, fmt=0;
1253 unsigned long *ptmp = (unsigned long *) dmabuf->pbuf;
1254 unsigned long SGarray[9], nSGpages=0;
1257 CS_DBGOUT(CS_FUNCTION, 4, printk("cs46xx: prog_dmabuf()+ \n"));
1259 * check for CAPTURE and use only non-sg for initial release
1261 if(dmabuf->type == CS_TYPE_ADC)
1263 CS_DBGOUT(CS_FUNCTION, 4, printk("cs46xx: prog_dmabuf() ADC\n"));
1265 * add in non-sg support for capture.
1267 spin_lock_irqsave(&state->card->lock, flags);
1268 /* add code to reset the rawbuf memory. TRW */
1269 resync_dma_ptrs(state);
1270 dmabuf->total_bytes = dmabuf->blocks = 0;
1271 dmabuf->count = dmabuf->error = dmabuf->underrun = 0;
1275 spin_unlock_irqrestore(&state->card->lock, flags);
1277 /* allocate DMA buffer if not allocated yet */
1278 if (!dmabuf->rawbuf || !dmabuf->tmpbuff)
1279 if ((ret = alloc_dmabuf(state)))
1282 * static image only supports 16Bit signed, stereo - hard code fmt
1284 fmt = CS_FMT_16BIT | CS_FMT_STEREO;
1286 dmabuf->numfrag = 2;
1287 dmabuf->fragsize = 2048;
1288 dmabuf->fragsamples = 2048 >> sample_shift[fmt];
1289 dmabuf->dmasize = 4096;
1290 dmabuf->fragshift = 11;
1292 memset(dmabuf->rawbuf, (fmt & CS_FMT_16BIT) ? 0 : 0x80,
1294 memset(dmabuf->tmpbuff, (fmt & CS_FMT_16BIT) ? 0 : 0x80,
1295 PAGE_SIZE<<dmabuf->buforder_tmpbuff);
1298 * Now set up the ring
1301 spin_lock_irqsave(&state->card->lock, flags);
1302 cs_rec_setup(state);
1303 spin_unlock_irqrestore(&state->card->lock, flags);
1305 /* set the ready flag for the dma buffer */
1308 CS_DBGOUT(CS_PARMS, 4, printk(
1309 "cs46xx: prog_dmabuf(): CAPTURE rate=%d fmt=0x%x numfrag=%d "
1310 "fragsize=%d dmasize=%d\n",
1311 dmabuf->rate, dmabuf->fmt, dmabuf->numfrag,
1312 dmabuf->fragsize, dmabuf->dmasize) );
1314 CS_DBGOUT(CS_FUNCTION, 4, printk("cs46xx: prog_dmabuf()- 0 \n"));
1317 else if (dmabuf->type == CS_TYPE_DAC)
1322 CS_DBGOUT(CS_FUNCTION, 4, printk("cs46xx: prog_dmabuf() DAC\n"));
1323 spin_lock_irqsave(&state->card->lock, flags);
1324 resync_dma_ptrs(state);
1325 dmabuf->total_bytes = dmabuf->blocks = 0;
1326 dmabuf->count = dmabuf->error = dmabuf->underrun = 0;
1330 spin_unlock_irqrestore(&state->card->lock, flags);
1332 /* allocate DMA buffer if not allocated yet */
1333 if (!dmabuf->rawbuf)
1334 if ((ret = alloc_dmabuf(state)))
1337 allocated_pages = 1 << dmabuf->buforder;
1338 allocated_bytes = allocated_pages*PAGE_SIZE;
1340 if(allocated_pages < 2)
1342 CS_DBGOUT(CS_FUNCTION, 4, printk(
1343 "cs46xx: prog_dmabuf() Error: allocated_pages too small (%d)\n",
1344 (unsigned)allocated_pages));
1348 /* Use all the pages allocated, fragsize 4k. */
1349 /* Use 'pbuf' for S/G page map table. */
1350 dmabuf->SGok = 1; /* Use S/G. */
1352 nSGpages = allocated_bytes/4096; /* S/G pages always 4k. */
1354 /* Set up S/G variables. */
1355 *ptmp = virt_to_bus(dmabuf->rawbuf);
1356 *(ptmp+1) = 0x00000008;
1357 for(tmp1= 1; tmp1 < nSGpages; tmp1++) {
1358 *(ptmp+2*tmp1) = virt_to_bus( (dmabuf->rawbuf)+4096*tmp1);
1359 if( tmp1 == nSGpages-1)
1362 tmp2 = 0x80000000+8*(tmp1+1);
1363 *(ptmp+2*tmp1+1) = tmp2;
1365 SGarray[0] = 0x82c0200d;
1366 SGarray[1] = 0xffff0000;
1368 SGarray[3] = 0x00010600;
1369 SGarray[4] = *(ptmp+2);
1370 SGarray[5] = 0x80000010;
1372 SGarray[7] = *(ptmp+2);
1373 SGarray[8] = (virt_to_bus(dmabuf->pbuf) & 0xffff000) | 0x10;
1376 dmabuf->numfrag = nSGpages;
1377 dmabuf->fragsize = 4096;
1378 dmabuf->fragsamples = 4096 >> sample_shift[dmabuf->fmt];
1379 dmabuf->fragshift = 12;
1380 dmabuf->dmasize = dmabuf->numfrag*4096;
1383 SGarray[0] = 0xf2c0000f;
1384 SGarray[1] = 0x00000200;
1386 SGarray[3] = 0x00010600;
1387 SGarray[4]=SGarray[5]=SGarray[6]=SGarray[7]=SGarray[8] = 0;
1388 dmabuf->numfrag = 2;
1389 dmabuf->fragsize = 2048;
1390 dmabuf->fragsamples = 2048 >> sample_shift[dmabuf->fmt];
1391 dmabuf->dmasize = 4096;
1392 dmabuf->fragshift = 11;
1394 for(tmp1 = 0; tmp1 < sizeof(SGarray)/4; tmp1++)
1395 cs461x_poke( state->card, BA1_PDTC+tmp1*4, SGarray[tmp1]);
1397 memset(dmabuf->rawbuf, (dmabuf->fmt & CS_FMT_16BIT) ? 0 : 0x80,
1401 * Now set up the ring
1404 spin_lock_irqsave(&state->card->lock, flags);
1405 cs_play_setup(state);
1406 spin_unlock_irqrestore(&state->card->lock, flags);
1408 /* set the ready flag for the dma buffer */
1411 CS_DBGOUT(CS_PARMS, 4, printk(
1412 "cs46xx: prog_dmabuf(): PLAYBACK rate=%d fmt=0x%x numfrag=%d "
1413 "fragsize=%d dmasize=%d\n",
1414 dmabuf->rate, dmabuf->fmt, dmabuf->numfrag,
1415 dmabuf->fragsize, dmabuf->dmasize) );
1417 CS_DBGOUT(CS_FUNCTION, 4, printk("cs46xx: prog_dmabuf()- \n"));
1422 CS_DBGOUT(CS_FUNCTION, 4, printk("cs46xx: prog_dmabuf()- Invalid Type %d\n",
1428 static int prog_dmabuf(struct cs_state *state)
1432 mutex_lock(&state->sem);
1433 ret = __prog_dmabuf(state);
1434 mutex_unlock(&state->sem);
1439 static void cs_clear_tail(struct cs_state *state)
1443 static int drain_dac(struct cs_state *state, int nonblock)
1445 DECLARE_WAITQUEUE(wait, current);
1446 struct dmabuf *dmabuf = &state->dmabuf;
1447 struct cs_card *card=state->card;
1448 unsigned long flags;
1452 CS_DBGOUT(CS_FUNCTION, 4, printk("cs46xx: drain_dac()+ \n"));
1453 if (dmabuf->mapped || !dmabuf->ready)
1455 CS_DBGOUT(CS_FUNCTION, 4, printk("cs46xx: drain_dac()- 0, not ready\n"));
1459 add_wait_queue(&dmabuf->wait, &wait);
1461 /* It seems that we have to set the current state to TASK_INTERRUPTIBLE
1462 every time to make the process really go to sleep */
1463 current->state = TASK_INTERRUPTIBLE;
1465 spin_lock_irqsave(&state->card->lock, flags);
1466 count = dmabuf->count;
1467 spin_unlock_irqrestore(&state->card->lock, flags);
1472 if (signal_pending(current))
1476 remove_wait_queue(&dmabuf->wait, &wait);
1477 current->state = TASK_RUNNING;
1481 tmo = (dmabuf->dmasize * HZ) / dmabuf->rate;
1482 tmo >>= sample_shift[dmabuf->fmt];
1483 tmo += (2048*HZ)/dmabuf->rate;
1485 if (!schedule_timeout(tmo ? tmo : 1) && tmo){
1486 printk(KERN_ERR "cs46xx: drain_dac, dma timeout? %d\n", count);
1490 remove_wait_queue(&dmabuf->wait, &wait);
1491 current->state = TASK_RUNNING;
1492 if (signal_pending(current))
1494 CS_DBGOUT(CS_FUNCTION, 4, printk("cs46xx: drain_dac()- -ERESTARTSYS\n"));
1496 * set to silence and let that clear the fifos.
1498 cs461x_clear_serial_FIFOs(card, CS_TYPE_DAC);
1499 return -ERESTARTSYS;
1502 CS_DBGOUT(CS_FUNCTION, 4, printk("cs46xx: drain_dac()- 0\n"));
1507 /* update buffer manangement pointers, especially, dmabuf->count and dmabuf->hwptr */
1508 static void cs_update_ptr(struct cs_card *card, int wake)
1510 struct cs_state *state;
1511 struct dmabuf *dmabuf;
1515 /* error handling and process wake up for ADC */
1516 state = card->states[0];
1519 dmabuf = &state->dmabuf;
1520 if (dmabuf->enable & ADC_RUNNING) {
1521 /* update hardware pointer */
1522 hwptr = cs_get_dma_addr(state);
1524 diff = (dmabuf->dmasize + hwptr - dmabuf->hwptr) % dmabuf->dmasize;
1525 CS_DBGOUT(CS_PARMS, 9, printk(
1526 "cs46xx: cs_update_ptr()+ ADC hwptr=%d diff=%d\n",
1528 dmabuf->hwptr = hwptr;
1529 dmabuf->total_bytes += diff;
1530 dmabuf->count += diff;
1531 if (dmabuf->count > dmabuf->dmasize)
1532 dmabuf->count = dmabuf->dmasize;
1536 if (wake && dmabuf->count >= (signed)dmabuf->fragsize)
1537 wake_up(&dmabuf->wait);
1540 if (wake && dmabuf->count > 0)
1541 wake_up(&dmabuf->wait);
1549 state = card->states[1];
1552 dmabuf = &state->dmabuf;
1553 /* error handling and process wake up for DAC */
1554 if (dmabuf->enable & DAC_RUNNING) {
1555 /* update hardware pointer */
1556 hwptr = cs_get_dma_addr(state);
1558 diff = (dmabuf->dmasize + hwptr - dmabuf->hwptr) % dmabuf->dmasize;
1559 CS_DBGOUT(CS_PARMS, 9, printk(
1560 "cs46xx: cs_update_ptr()+ DAC hwptr=%d diff=%d\n",
1562 dmabuf->hwptr = hwptr;
1563 dmabuf->total_bytes += diff;
1564 if (dmabuf->mapped) {
1565 dmabuf->count += diff;
1566 if (wake && dmabuf->count >= (signed)dmabuf->fragsize)
1567 wake_up(&dmabuf->wait);
1569 * other drivers use fragsize, but don't see any sense
1570 * in that, since dmasize is the buffer asked for
1573 if( dmabuf->count > dmabuf->dmasize)
1574 dmabuf->count &= dmabuf->dmasize-1;
1576 dmabuf->count -= diff;
1578 * backfill with silence and clear out the last
1579 * "diff" number of bytes.
1583 memset(dmabuf->rawbuf + hwptr - diff,
1584 (dmabuf->fmt & CS_FMT_16BIT) ? 0 : 0x80, diff);
1588 memset(dmabuf->rawbuf,
1589 (dmabuf->fmt & CS_FMT_16BIT) ? 0 : 0x80,
1591 memset((char *)dmabuf->rawbuf +
1592 dmabuf->dmasize + hwptr - diff,
1593 (dmabuf->fmt & CS_FMT_16BIT) ? 0 : 0x80,
1597 if (dmabuf->count < 0 || dmabuf->count > dmabuf->dmasize) {
1598 CS_DBGOUT(CS_ERROR, 2, printk(KERN_INFO
1599 "cs46xx: ERROR DAC count<0 or count > dmasize (%d)\n",
1602 * buffer underrun or buffer overrun, reset the
1603 * count of bytes written back to 0.
1605 if(dmabuf->count < 0)
1610 if (wake && dmabuf->count < (signed)dmabuf->dmasize/2)
1611 wake_up(&dmabuf->wait);
1618 /* hold spinlock for the following! */
1619 static void cs_handle_midi(struct cs_card *card)
1626 while (!(cs461x_peekBA0(card, BA0_MIDSR) & MIDSR_RBE)) {
1627 ch = cs461x_peekBA0(card, BA0_MIDRP);
1628 if (card->midi.icnt < CS_MIDIINBUF) {
1629 card->midi.ibuf[card->midi.iwr] = ch;
1630 card->midi.iwr = (card->midi.iwr + 1) % CS_MIDIINBUF;
1636 wake_up(&card->midi.iwait);
1638 while (!(cs461x_peekBA0(card, BA0_MIDSR) & MIDSR_TBF) && card->midi.ocnt > 0) {
1639 temp1 = ( card->midi.obuf[card->midi.ord] ) & 0x000000ff;
1640 cs461x_pokeBA0(card, BA0_MIDWP,temp1);
1641 card->midi.ord = (card->midi.ord + 1) % CS_MIDIOUTBUF;
1643 if (card->midi.ocnt < CS_MIDIOUTBUF-16)
1647 wake_up(&card->midi.owait);
1650 static irqreturn_t cs_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1652 struct cs_card *card = (struct cs_card *)dev_id;
1653 /* Single channel card */
1654 struct cs_state *recstate = card->channel[0].state;
1655 struct cs_state *playstate = card->channel[1].state;
1658 CS_DBGOUT(CS_INTERRUPT, 9, printk("cs46xx: cs_interrupt()+ \n"));
1660 spin_lock(&card->lock);
1662 status = cs461x_peekBA0(card, BA0_HISR);
1664 if ((status & 0x7fffffff) == 0)
1666 cs461x_pokeBA0(card, BA0_HICR, HICR_CHGM|HICR_IEV);
1667 spin_unlock(&card->lock);
1668 return IRQ_HANDLED; /* Might be IRQ_NONE.. */
1672 * check for playback or capture interrupt only
1674 if( ((status & HISR_VC0) && playstate && playstate->dmabuf.ready) ||
1675 (((status & HISR_VC1) && recstate && recstate->dmabuf.ready)) )
1677 CS_DBGOUT(CS_INTERRUPT, 8, printk(
1678 "cs46xx: cs_interrupt() interrupt bit(s) set (0x%x)\n",status));
1679 cs_update_ptr(card, CS_TRUE);
1682 if( status & HISR_MIDI )
1683 cs_handle_midi(card);
1686 cs461x_pokeBA0(card, BA0_HICR, HICR_CHGM|HICR_IEV);
1687 spin_unlock(&card->lock);
1688 CS_DBGOUT(CS_INTERRUPT, 9, printk("cs46xx: cs_interrupt()- \n"));
1693 /**********************************************************************/
1695 static ssize_t cs_midi_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
1697 struct cs_card *card = (struct cs_card *)file->private_data;
1699 unsigned long flags;
1703 if (!access_ok(VERIFY_WRITE, buffer, count))
1707 spin_lock_irqsave(&card->lock, flags);
1708 ptr = card->midi.ird;
1709 cnt = CS_MIDIINBUF - ptr;
1710 if (card->midi.icnt < cnt)
1711 cnt = card->midi.icnt;
1712 spin_unlock_irqrestore(&card->lock, flags);
1716 if (file->f_flags & O_NONBLOCK)
1717 return ret ? ret : -EAGAIN;
1718 interruptible_sleep_on(&card->midi.iwait);
1719 if (signal_pending(current))
1720 return ret ? ret : -ERESTARTSYS;
1723 if (copy_to_user(buffer, card->midi.ibuf + ptr, cnt))
1724 return ret ? ret : -EFAULT;
1725 ptr = (ptr + cnt) % CS_MIDIINBUF;
1726 spin_lock_irqsave(&card->lock, flags);
1727 card->midi.ird = ptr;
1728 card->midi.icnt -= cnt;
1729 spin_unlock_irqrestore(&card->lock, flags);
1738 static ssize_t cs_midi_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
1740 struct cs_card *card = (struct cs_card *)file->private_data;
1742 unsigned long flags;
1746 if (!access_ok(VERIFY_READ, buffer, count))
1750 spin_lock_irqsave(&card->lock, flags);
1751 ptr = card->midi.owr;
1752 cnt = CS_MIDIOUTBUF - ptr;
1753 if (card->midi.ocnt + cnt > CS_MIDIOUTBUF)
1754 cnt = CS_MIDIOUTBUF - card->midi.ocnt;
1756 cs_handle_midi(card);
1757 spin_unlock_irqrestore(&card->lock, flags);
1761 if (file->f_flags & O_NONBLOCK)
1762 return ret ? ret : -EAGAIN;
1763 interruptible_sleep_on(&card->midi.owait);
1764 if (signal_pending(current))
1765 return ret ? ret : -ERESTARTSYS;
1768 if (copy_from_user(card->midi.obuf + ptr, buffer, cnt))
1769 return ret ? ret : -EFAULT;
1770 ptr = (ptr + cnt) % CS_MIDIOUTBUF;
1771 spin_lock_irqsave(&card->lock, flags);
1772 card->midi.owr = ptr;
1773 card->midi.ocnt += cnt;
1774 spin_unlock_irqrestore(&card->lock, flags);
1778 spin_lock_irqsave(&card->lock, flags);
1779 cs_handle_midi(card);
1780 spin_unlock_irqrestore(&card->lock, flags);
1786 static unsigned int cs_midi_poll(struct file *file, struct poll_table_struct *wait)
1788 struct cs_card *card = (struct cs_card *)file->private_data;
1789 unsigned long flags;
1790 unsigned int mask = 0;
1792 if (file->f_flags & FMODE_WRITE)
1793 poll_wait(file, &card->midi.owait, wait);
1794 if (file->f_flags & FMODE_READ)
1795 poll_wait(file, &card->midi.iwait, wait);
1796 spin_lock_irqsave(&card->lock, flags);
1797 if (file->f_flags & FMODE_READ) {
1798 if (card->midi.icnt > 0)
1799 mask |= POLLIN | POLLRDNORM;
1801 if (file->f_flags & FMODE_WRITE) {
1802 if (card->midi.ocnt < CS_MIDIOUTBUF)
1803 mask |= POLLOUT | POLLWRNORM;
1805 spin_unlock_irqrestore(&card->lock, flags);
1810 static int cs_midi_open(struct inode *inode, struct file *file)
1812 unsigned int minor = iminor(inode);
1813 struct cs_card *card=NULL;
1814 unsigned long flags;
1815 struct list_head *entry;
1817 list_for_each(entry, &cs46xx_devs)
1819 card = list_entry(entry, struct cs_card, list);
1820 if (card->dev_midi == minor)
1824 if (entry == &cs46xx_devs)
1828 CS_DBGOUT(CS_FUNCTION | CS_OPEN, 2, printk(KERN_INFO
1829 "cs46xx: cs46xx_midi_open(): Error - unable to find card struct\n"));
1833 file->private_data = card;
1834 /* wait for device to become free */
1835 mutex_lock(&card->midi.open_mutex);
1836 while (card->midi.open_mode & file->f_mode) {
1837 if (file->f_flags & O_NONBLOCK) {
1838 mutex_unlock(&card->midi.open_mutex);
1841 mutex_unlock(&card->midi.open_mutex);
1842 interruptible_sleep_on(&card->midi.open_wait);
1843 if (signal_pending(current))
1844 return -ERESTARTSYS;
1845 mutex_lock(&card->midi.open_mutex);
1847 spin_lock_irqsave(&card->midi.lock, flags);
1848 if (!(card->midi.open_mode & (FMODE_READ | FMODE_WRITE))) {
1849 card->midi.ird = card->midi.iwr = card->midi.icnt = 0;
1850 card->midi.ord = card->midi.owr = card->midi.ocnt = 0;
1851 card->midi.ird = card->midi.iwr = card->midi.icnt = 0;
1852 cs461x_pokeBA0(card, BA0_MIDCR, 0x0000000f); /* Enable xmit, rcv. */
1853 cs461x_pokeBA0(card, BA0_HICR, HICR_IEV | HICR_CHGM); /* Enable interrupts */
1855 if (file->f_mode & FMODE_READ) {
1856 card->midi.ird = card->midi.iwr = card->midi.icnt = 0;
1858 if (file->f_mode & FMODE_WRITE) {
1859 card->midi.ord = card->midi.owr = card->midi.ocnt = 0;
1861 spin_unlock_irqrestore(&card->midi.lock, flags);
1862 card->midi.open_mode |= (file->f_mode & (FMODE_READ | FMODE_WRITE));
1863 mutex_unlock(&card->midi.open_mutex);
1868 static int cs_midi_release(struct inode *inode, struct file *file)
1870 struct cs_card *card = (struct cs_card *)file->private_data;
1871 DECLARE_WAITQUEUE(wait, current);
1872 unsigned long flags;
1873 unsigned count, tmo;
1875 if (file->f_mode & FMODE_WRITE) {
1876 current->state = TASK_INTERRUPTIBLE;
1877 add_wait_queue(&card->midi.owait, &wait);
1879 spin_lock_irqsave(&card->midi.lock, flags);
1880 count = card->midi.ocnt;
1881 spin_unlock_irqrestore(&card->midi.lock, flags);
1884 if (signal_pending(current))
1886 if (file->f_flags & O_NONBLOCK)
1888 tmo = (count * HZ) / 3100;
1889 if (!schedule_timeout(tmo ? : 1) && tmo)
1890 printk(KERN_DEBUG "cs46xx: midi timed out??\n");
1892 remove_wait_queue(&card->midi.owait, &wait);
1893 current->state = TASK_RUNNING;
1895 mutex_lock(&card->midi.open_mutex);
1896 card->midi.open_mode &= (~(file->f_mode & (FMODE_READ | FMODE_WRITE)));
1897 mutex_unlock(&card->midi.open_mutex);
1898 wake_up(&card->midi.open_wait);
1903 * Midi file operations struct.
1905 static /*const*/ struct file_operations cs_midi_fops = {
1906 CS_OWNER CS_THIS_MODULE
1907 .llseek = no_llseek,
1908 .read = cs_midi_read,
1909 .write = cs_midi_write,
1910 .poll = cs_midi_poll,
1911 .open = cs_midi_open,
1912 .release = cs_midi_release,
1917 * CopySamples copies 16-bit stereo signed samples from the source to the
1918 * destination, possibly converting down to unsigned 8-bit and/or mono.
1919 * count specifies the number of output bytes to write.
1923 * dst - Pointer to a destination buffer.
1924 * src - Pointer to a source buffer
1925 * count - The number of bytes to copy into the destination buffer.
1926 * fmt - CS_FMT_16BIT and/or CS_FMT_STEREO bits
1927 * dmabuf - pointer to the dma buffer structure
1929 * NOTES: only call this routine if the output desired is not 16 Signed Stereo
1933 static void CopySamples(char *dst, char *src, int count, unsigned fmt,
1934 struct dmabuf *dmabuf)
1938 s16 *psSrc=(s16 *)src;
1939 s16 *psDst=(s16 *)dst;
1940 u8 *pucDst=(u8 *)dst;
1942 CS_DBGOUT(CS_FUNCTION, 2, printk(KERN_INFO "cs46xx: CopySamples()+ ") );
1943 CS_DBGOUT(CS_WAVE_READ, 8, printk(KERN_INFO
1944 " dst=%p src=%p count=%d fmt=0x%x\n",
1945 dst,src,count,fmt) );
1948 * See if the data should be output as 8-bit unsigned stereo.
1950 if((fmt & CS_FMT_STEREO) && !(fmt & CS_FMT_16BIT))
1953 * Convert each 16-bit signed stereo sample to 8-bit unsigned
1954 * stereo using rounding.
1960 *(pucDst++) = (u8)(((s16)(*psSrc++) + (s16)0x8000) >> 8);
1964 * See if the data should be output at 8-bit unsigned mono.
1966 else if(!(fmt & CS_FMT_STEREO) && !(fmt & CS_FMT_16BIT))
1969 * Convert each 16-bit signed stereo sample to 8-bit unsigned
1970 * mono using averaging and rounding.
1976 s32AudioSample = ((*psSrc)+(*(psSrc + 1)))/2 + (s32)0x80;
1977 if(s32AudioSample > 0x7fff)
1978 s32AudioSample = 0x7fff;
1979 *(pucDst++) = (u8)(((s16)s32AudioSample + (s16)0x8000) >> 8);
1984 * See if the data should be output at 16-bit signed mono.
1986 else if(!(fmt & CS_FMT_STEREO) && (fmt & CS_FMT_16BIT))
1989 * Convert each 16-bit signed stereo sample to 16-bit signed
1990 * mono using averaging.
1996 *(psDst++) = (s16)((*psSrc)+(*(psSrc + 1)))/2;
2004 * replacement for the standard copy_to_user, to allow for a conversion from
2005 * 16 bit to 8 bit and from stereo to mono, if the record conversion is active.
2006 * The current CS46xx/CS4280 static image only records in 16bit unsigned Stereo,
2007 * so we convert from any of the other format combinations.
2009 static unsigned cs_copy_to_user(
2016 struct dmabuf *dmabuf = &s->dmabuf;
2017 void *src = hwsrc; /* default to the standard destination buffer addr */
2019 CS_DBGOUT(CS_FUNCTION, 6, printk(KERN_INFO
2020 "cs_copy_to_user()+ fmt=0x%x cnt=%d dest=%p\n",
2021 dmabuf->fmt,(unsigned)cnt,dest) );
2023 if(cnt > dmabuf->dmasize)
2025 cnt = dmabuf->dmasize;
2032 if(dmabuf->divisor != 1)
2034 if(!dmabuf->tmpbuff)
2036 *copied = cnt/dmabuf->divisor;
2040 CopySamples((char *)dmabuf->tmpbuff, (char *)hwsrc, cnt,
2041 dmabuf->fmt, dmabuf);
2042 src = dmabuf->tmpbuff;
2043 cnt = cnt/dmabuf->divisor;
2045 if (copy_to_user(dest, src, cnt))
2047 CS_DBGOUT(CS_FUNCTION, 2, printk(KERN_ERR
2048 "cs46xx: cs_copy_to_user()- fault dest=%p src=%p cnt=%d\n",
2054 CS_DBGOUT(CS_FUNCTION, 2, printk(KERN_INFO
2055 "cs46xx: cs_copy_to_user()- copied bytes is %d \n",cnt) );
2059 /* in this loop, dmabuf.count signifies the amount of data that is waiting to be copied to
2060 the user's buffer. it is filled by the dma machine and drained by this loop. */
2061 static ssize_t cs_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
2063 struct cs_card *card = (struct cs_card *) file->private_data;
2064 struct cs_state *state;
2065 DECLARE_WAITQUEUE(wait, current);
2066 struct dmabuf *dmabuf;
2068 unsigned long flags;
2073 CS_DBGOUT(CS_WAVE_READ | CS_FUNCTION, 4,
2074 printk("cs46xx: cs_read()+ %zd\n",count) );
2075 state = (struct cs_state *)card->states[0];
2078 dmabuf = &state->dmabuf;
2082 if (!access_ok(VERIFY_WRITE, buffer, count))
2085 mutex_lock(&state->sem);
2086 if (!dmabuf->ready && (ret = __prog_dmabuf(state)))
2089 add_wait_queue(&state->dmabuf.wait, &wait);
2091 while(!(card->pm.flags & CS46XX_PM_IDLE))
2094 if (signal_pending(current)) {
2095 if(!ret) ret = -ERESTARTSYS;
2099 spin_lock_irqsave(&state->card->lock, flags);
2100 swptr = dmabuf->swptr;
2101 cnt = dmabuf->dmasize - swptr;
2102 if (dmabuf->count < cnt)
2103 cnt = dmabuf->count;
2105 __set_current_state(TASK_INTERRUPTIBLE);
2106 spin_unlock_irqrestore(&state->card->lock, flags);
2108 if (cnt > (count * dmabuf->divisor))
2109 cnt = count * dmabuf->divisor;
2111 /* buffer is empty, start the dma machine and wait for data to be
2114 if (file->f_flags & O_NONBLOCK) {
2115 if (!ret) ret = -EAGAIN;
2118 mutex_unlock(&state->sem);
2120 if (signal_pending(current)) {
2121 if(!ret) ret = -ERESTARTSYS;
2124 mutex_lock(&state->sem);
2134 CS_DBGOUT(CS_WAVE_READ, 2, printk(KERN_INFO
2135 "_read() copy_to cnt=%d count=%zd ", cnt,count) );
2136 CS_DBGOUT(CS_WAVE_READ, 8, printk(KERN_INFO
2137 " .dmasize=%d .count=%d buffer=%p ret=%zd\n",
2138 dmabuf->dmasize,dmabuf->count,buffer,ret) );
2140 if (cs_copy_to_user(state, buffer,
2141 (char *)dmabuf->rawbuf + swptr, cnt, &copied))
2143 if (!ret) ret = -EFAULT;
2146 swptr = (swptr + cnt) % dmabuf->dmasize;
2147 spin_lock_irqsave(&card->lock, flags);
2148 dmabuf->swptr = swptr;
2149 dmabuf->count -= cnt;
2150 spin_unlock_irqrestore(&card->lock, flags);
2157 remove_wait_queue(&state->dmabuf.wait, &wait);
2159 mutex_unlock(&state->sem);
2160 set_current_state(TASK_RUNNING);
2161 CS_DBGOUT(CS_WAVE_READ | CS_FUNCTION, 4,
2162 printk("cs46xx: cs_read()- %zd\n",ret) );
2166 /* in this loop, dmabuf.count signifies the amount of data that is waiting to be dma to
2167 the soundcard. it is drained by the dma machine and filled by this loop. */
2168 static ssize_t cs_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
2170 struct cs_card *card = (struct cs_card *) file->private_data;
2171 struct cs_state *state;
2172 DECLARE_WAITQUEUE(wait, current);
2173 struct dmabuf *dmabuf;
2175 unsigned long flags;
2179 CS_DBGOUT(CS_WAVE_WRITE | CS_FUNCTION, 4,
2180 printk("cs46xx: cs_write called, count = %zd\n", count) );
2181 state = (struct cs_state *)card->states[1];
2184 if (!access_ok(VERIFY_READ, buffer, count))
2186 dmabuf = &state->dmabuf;
2188 mutex_lock(&state->sem);
2195 if (!dmabuf->ready && (ret = __prog_dmabuf(state)))
2197 add_wait_queue(&state->dmabuf.wait, &wait);
2200 * Start the loop to read from the user's buffer and write to the dma buffer.
2201 * check for PM events and underrun/overrun in the loop.
2204 while(!(card->pm.flags & CS46XX_PM_IDLE))
2207 if (signal_pending(current)) {
2208 if(!ret) ret = -ERESTARTSYS;
2212 spin_lock_irqsave(&state->card->lock, flags);
2213 if (dmabuf->count < 0) {
2214 /* buffer underrun, we are recovering from sleep_on_timeout,
2215 resync hwptr and swptr */
2217 dmabuf->swptr = dmabuf->hwptr;
2219 if (dmabuf->underrun)
2221 dmabuf->underrun = 0;
2222 dmabuf->hwptr = cs_get_dma_addr(state);
2223 dmabuf->swptr = dmabuf->hwptr;
2226 swptr = dmabuf->swptr;
2227 cnt = dmabuf->dmasize - swptr;
2228 if (dmabuf->count + cnt > dmabuf->dmasize)
2229 cnt = dmabuf->dmasize - dmabuf->count;
2231 __set_current_state(TASK_INTERRUPTIBLE);
2232 spin_unlock_irqrestore(&state->card->lock, flags);
2237 /* buffer is full, start the dma machine and wait for data to be
2240 if (file->f_flags & O_NONBLOCK) {
2241 if (!ret) ret = -EAGAIN;
2244 mutex_unlock(&state->sem);
2246 if (signal_pending(current)) {
2247 if(!ret) ret = -ERESTARTSYS;
2250 mutex_lock(&state->sem);
2259 if (copy_from_user(dmabuf->rawbuf + swptr, buffer, cnt)) {
2260 if (!ret) ret = -EFAULT;
2263 spin_lock_irqsave(&state->card->lock, flags);
2264 swptr = (swptr + cnt) % dmabuf->dmasize;
2265 dmabuf->swptr = swptr;
2266 dmabuf->count += cnt;
2267 if(dmabuf->count > dmabuf->dmasize)
2269 CS_DBGOUT(CS_WAVE_WRITE | CS_ERROR, 2, printk(
2270 "cs46xx: cs_write() d->count > dmasize - resetting\n"));
2271 dmabuf->count = dmabuf->dmasize;
2273 dmabuf->endcleared = 0;
2274 spin_unlock_irqrestore(&state->card->lock, flags);
2282 mutex_unlock(&state->sem);
2283 remove_wait_queue(&state->dmabuf.wait, &wait);
2284 set_current_state(TASK_RUNNING);
2286 CS_DBGOUT(CS_WAVE_WRITE | CS_FUNCTION, 2,
2287 printk("cs46xx: cs_write()- ret=%zd\n", ret) );
2291 static unsigned int cs_poll(struct file *file, struct poll_table_struct *wait)
2293 struct cs_card *card = (struct cs_card *)file->private_data;
2294 struct dmabuf *dmabuf;
2295 struct cs_state *state;
2297 unsigned long flags;
2298 unsigned int mask = 0;
2300 CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: cs_poll()+ \n"));
2301 if (!(file->f_mode & (FMODE_WRITE | FMODE_READ)))
2305 if (file->f_mode & FMODE_WRITE)
2307 state = card->states[1];
2310 dmabuf = &state->dmabuf;
2311 poll_wait(file, &dmabuf->wait, wait);
2314 if (file->f_mode & FMODE_READ)
2316 state = card->states[0];
2319 dmabuf = &state->dmabuf;
2320 poll_wait(file, &dmabuf->wait, wait);
2324 spin_lock_irqsave(&card->lock, flags);
2325 cs_update_ptr(card, CS_FALSE);
2326 if (file->f_mode & FMODE_READ) {
2327 state = card->states[0];
2330 dmabuf = &state->dmabuf;
2331 if (dmabuf->count >= (signed)dmabuf->fragsize)
2332 mask |= POLLIN | POLLRDNORM;
2335 if (file->f_mode & FMODE_WRITE) {
2336 state = card->states[1];
2339 dmabuf = &state->dmabuf;
2340 if (dmabuf->mapped) {
2341 if (dmabuf->count >= (signed)dmabuf->fragsize)
2342 mask |= POLLOUT | POLLWRNORM;
2344 if ((signed)dmabuf->dmasize >= dmabuf->count
2345 + (signed)dmabuf->fragsize)
2346 mask |= POLLOUT | POLLWRNORM;
2350 spin_unlock_irqrestore(&card->lock, flags);
2352 CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: cs_poll()- (0x%x) \n",
2358 * We let users mmap the ring buffer. Its not the real DMA buffer but
2359 * that side of the code is hidden in the IRQ handling. We do a software
2360 * emulation of DMA from a 64K or so buffer into a 2K FIFO.
2361 * (the hardware probably deserves a moan here but Crystal send me nice
2365 static int cs_mmap(struct file *file, struct vm_area_struct *vma)
2367 struct cs_card *card = (struct cs_card *)file->private_data;
2368 struct cs_state *state;
2369 struct dmabuf *dmabuf;
2373 CS_DBGOUT(CS_FUNCTION | CS_PARMS, 2, printk("cs46xx: cs_mmap()+ file=%p %s %s\n",
2374 file, vma->vm_flags & VM_WRITE ? "VM_WRITE" : "",
2375 vma->vm_flags & VM_READ ? "VM_READ" : "") );
2377 if (vma->vm_flags & VM_WRITE) {
2378 state = card->states[1];
2381 CS_DBGOUT(CS_OPEN, 2, printk(
2382 "cs46xx: cs_mmap() VM_WRITE - state TRUE prog_dmabuf DAC\n") );
2383 if ((ret = prog_dmabuf(state)) != 0)
2386 } else if (vma->vm_flags & VM_READ) {
2387 state = card->states[0];
2390 CS_DBGOUT(CS_OPEN, 2, printk(
2391 "cs46xx: cs_mmap() VM_READ - state TRUE prog_dmabuf ADC\n") );
2392 if ((ret = prog_dmabuf(state)) != 0)
2396 CS_DBGOUT(CS_ERROR, 2, printk(
2397 "cs46xx: cs_mmap() return -EINVAL\n") );
2402 * For now ONLY support playback, but seems like the only way to use
2403 * mmap() is to open an FD with RDWR, just read or just write access
2404 * does not function, get an error back from the kernel.
2405 * Also, QuakeIII opens with RDWR! So, there must be something
2406 * to needing read/write access mapping. So, allow read/write but
2409 state = card->states[1];
2415 mutex_lock(&state->sem);
2416 dmabuf = &state->dmabuf;
2417 if (cs4x_pgoff(vma) != 0)
2422 size = vma->vm_end - vma->vm_start;
2424 CS_DBGOUT(CS_PARMS, 2, printk("cs46xx: cs_mmap(): size=%d\n",(unsigned)size) );
2426 if (size > (PAGE_SIZE << dmabuf->buforder))
2431 if (remap_pfn_range(vma, vma->vm_start,
2432 virt_to_phys(dmabuf->rawbuf) >> PAGE_SHIFT,
2433 size, vma->vm_page_prot))
2440 CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: cs_mmap()-\n") );
2442 mutex_unlock(&state->sem);
2446 static int cs_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
2448 struct cs_card *card = (struct cs_card *)file->private_data;
2449 struct cs_state *state;
2450 struct dmabuf *dmabuf=NULL;
2451 unsigned long flags;
2452 audio_buf_info abinfo;
2454 int val, valsave, mapped, ret;
2455 void __user *argp = (void __user *)arg;
2456 int __user *p = argp;
2458 state = (struct cs_state *)card->states[0];
2461 dmabuf = &state->dmabuf;
2462 mapped = (file->f_mode & FMODE_READ) && dmabuf->mapped;
2464 state = (struct cs_state *)card->states[1];
2467 dmabuf = &state->dmabuf;
2468 mapped |= (file->f_mode & FMODE_WRITE) && dmabuf->mapped;
2477 case OSS_GETVERSION:
2478 return put_user(SOUND_VERSION, p);
2480 case SNDCTL_DSP_RESET:
2481 /* FIXME: spin_lock ? */
2482 if (file->f_mode & FMODE_WRITE) {
2483 state = (struct cs_state *)card->states[1];
2486 dmabuf = &state->dmabuf;
2488 synchronize_irq(card->irq);
2490 resync_dma_ptrs(state);
2491 dmabuf->swptr = dmabuf->hwptr = 0;
2492 dmabuf->count = dmabuf->total_bytes = 0;
2497 if (file->f_mode & FMODE_READ) {
2498 state = (struct cs_state *)card->states[0];
2501 dmabuf = &state->dmabuf;
2503 synchronize_irq(card->irq);
2504 resync_dma_ptrs(state);
2506 dmabuf->swptr = dmabuf->hwptr = 0;
2507 dmabuf->count = dmabuf->total_bytes = 0;
2512 CS_DBGOUT(CS_IOCTL, 2, printk("cs46xx: DSP_RESET()-\n") );
2515 case SNDCTL_DSP_SYNC:
2516 if (file->f_mode & FMODE_WRITE)
2517 return drain_dac(state, file->f_flags & O_NONBLOCK);
2520 case SNDCTL_DSP_SPEED: /* set sample rate */
2521 if (get_user(val, p))
2524 if (file->f_mode & FMODE_READ) {
2525 state = (struct cs_state *)card->states[0];
2528 dmabuf = &state->dmabuf;
2532 cs_set_adc_rate(state, val);
2533 cs_set_divisor(dmabuf);
2536 if (file->f_mode & FMODE_WRITE) {
2537 state = (struct cs_state *)card->states[1];
2540 dmabuf = &state->dmabuf;
2544 cs_set_dac_rate(state, val);
2545 cs_set_divisor(dmabuf);
2548 CS_DBGOUT(CS_IOCTL | CS_PARMS, 4, printk(
2549 "cs46xx: cs_ioctl() DSP_SPEED %s %s %d\n",
2550 file->f_mode & FMODE_WRITE ? "DAC" : "",
2551 file->f_mode & FMODE_READ ? "ADC" : "",
2553 return put_user(dmabuf->rate, p);
2555 return put_user(0, p);
2557 case SNDCTL_DSP_STEREO: /* set stereo or mono channel */
2558 if (get_user(val, p))
2560 if (file->f_mode & FMODE_WRITE) {
2561 state = (struct cs_state *)card->states[1];
2564 dmabuf = &state->dmabuf;
2569 dmabuf->fmt |= CS_FMT_STEREO;
2571 dmabuf->fmt &= ~CS_FMT_STEREO;
2572 cs_set_divisor(dmabuf);
2573 CS_DBGOUT(CS_IOCTL | CS_PARMS, 4, printk(
2574 "cs46xx: DSP_STEREO() DAC %s\n",
2575 (dmabuf->fmt & CS_FMT_STEREO) ?
2579 if (file->f_mode & FMODE_READ) {
2580 state = (struct cs_state *)card->states[0];
2583 dmabuf = &state->dmabuf;
2588 dmabuf->fmt |= CS_FMT_STEREO;
2590 dmabuf->fmt &= ~CS_FMT_STEREO;
2591 cs_set_divisor(dmabuf);
2592 CS_DBGOUT(CS_IOCTL | CS_PARMS, 4, printk(
2593 "cs46xx: DSP_STEREO() ADC %s\n",
2594 (dmabuf->fmt & CS_FMT_STEREO) ?
2600 case SNDCTL_DSP_GETBLKSIZE:
2601 if (file->f_mode & FMODE_WRITE) {
2602 state = (struct cs_state *)card->states[1];
2605 dmabuf = &state->dmabuf;
2606 if ((val = prog_dmabuf(state)))
2608 return put_user(dmabuf->fragsize, p);
2611 if (file->f_mode & FMODE_READ) {
2612 state = (struct cs_state *)card->states[0];
2615 dmabuf = &state->dmabuf;
2616 if ((val = prog_dmabuf(state)))
2618 return put_user(dmabuf->fragsize/dmabuf->divisor,
2622 return put_user(0, p);
2624 case SNDCTL_DSP_GETFMTS: /* Returns a mask of supported sample format*/
2625 return put_user(AFMT_S16_LE | AFMT_U8, p);
2627 case SNDCTL_DSP_SETFMT: /* Select sample format */
2628 if (get_user(val, p))
2630 CS_DBGOUT(CS_IOCTL | CS_PARMS, 4, printk(
2631 "cs46xx: cs_ioctl() DSP_SETFMT %s %s %s %s\n",
2632 file->f_mode & FMODE_WRITE ? "DAC" : "",
2633 file->f_mode & FMODE_READ ? "ADC" : "",
2634 val == AFMT_S16_LE ? "16Bit Signed" : "",
2635 val == AFMT_U8 ? "8Bit Unsigned" : "") );
2637 if (val != AFMT_QUERY) {
2638 if(val==AFMT_S16_LE || val==AFMT_U8)
2640 if (file->f_mode & FMODE_WRITE) {
2641 state = (struct cs_state *)card->states[1];
2644 dmabuf = &state->dmabuf;
2648 if(val==AFMT_S16_LE)
2649 dmabuf->fmt |= CS_FMT_16BIT;
2651 dmabuf->fmt &= ~CS_FMT_16BIT;
2652 cs_set_divisor(dmabuf);
2653 if((ret = prog_dmabuf(state)))
2657 if (file->f_mode & FMODE_READ) {
2659 state = (struct cs_state *)card->states[0];
2662 dmabuf = &state->dmabuf;
2666 if(val==AFMT_S16_LE)
2667 dmabuf->fmt |= CS_FMT_16BIT;
2669 dmabuf->fmt &= ~CS_FMT_16BIT;
2670 cs_set_divisor(dmabuf);
2671 if((ret = prog_dmabuf(state)))
2678 CS_DBGOUT(CS_IOCTL | CS_ERROR, 2, printk(
2679 "cs46xx: DSP_SETFMT() Unsupported format (0x%x)\n",
2685 if(file->f_mode & FMODE_WRITE)
2687 state = (struct cs_state *)card->states[1];
2689 dmabuf = &state->dmabuf;
2691 else if(file->f_mode & FMODE_READ)
2693 state = (struct cs_state *)card->states[0];
2695 dmabuf = &state->dmabuf;
2700 if(dmabuf->fmt & CS_FMT_16BIT)
2701 return put_user(AFMT_S16_LE, p);
2703 return put_user(AFMT_U8, p);
2705 return put_user(0, p);
2707 case SNDCTL_DSP_CHANNELS:
2708 if (get_user(val, p))
2711 if (file->f_mode & FMODE_WRITE) {
2712 state = (struct cs_state *)card->states[1];
2715 dmabuf = &state->dmabuf;
2720 dmabuf->fmt |= CS_FMT_STEREO;
2722 dmabuf->fmt &= ~CS_FMT_STEREO;
2723 cs_set_divisor(dmabuf);
2724 if (prog_dmabuf(state))
2728 if (file->f_mode & FMODE_READ) {
2729 state = (struct cs_state *)card->states[0];
2732 dmabuf = &state->dmabuf;
2737 dmabuf->fmt |= CS_FMT_STEREO;
2739 dmabuf->fmt &= ~CS_FMT_STEREO;
2740 cs_set_divisor(dmabuf);
2741 if (prog_dmabuf(state))
2746 return put_user((dmabuf->fmt & CS_FMT_STEREO) ? 2 : 1,
2749 case SNDCTL_DSP_POST:
2751 * There will be a longer than normal pause in the data.
2752 * so... do nothing, because there is nothing that we can do.
2756 case SNDCTL_DSP_SUBDIVIDE:
2757 if (file->f_mode & FMODE_WRITE) {
2758 state = (struct cs_state *)card->states[1];
2761 dmabuf = &state->dmabuf;
2762 if (dmabuf->subdivision)
2764 if (get_user(val, p))
2766 if (val != 1 && val != 2)
2768 dmabuf->subdivision = val;
2771 if (file->f_mode & FMODE_READ) {
2772 state = (struct cs_state *)card->states[0];
2775 dmabuf = &state->dmabuf;
2776 if (dmabuf->subdivision)
2778 if (get_user(val, p))
2780 if (val != 1 && val != 2)
2782 dmabuf->subdivision = val;
2787 case SNDCTL_DSP_SETFRAGMENT:
2788 if (get_user(val, p))
2791 if (file->f_mode & FMODE_WRITE) {
2792 state = (struct cs_state *)card->states[1];
2795 dmabuf = &state->dmabuf;
2796 dmabuf->ossfragshift = val & 0xffff;
2797 dmabuf->ossmaxfrags = (val >> 16) & 0xffff;
2800 if (file->f_mode & FMODE_READ) {
2801 state = (struct cs_state *)card->states[0];
2804 dmabuf = &state->dmabuf;
2805 dmabuf->ossfragshift = val & 0xffff;
2806 dmabuf->ossmaxfrags = (val >> 16) & 0xffff;
2811 case SNDCTL_DSP_GETOSPACE:
2812 if (!(file->f_mode & FMODE_WRITE))
2814 state = (struct cs_state *)card->states[1];
2817 dmabuf = &state->dmabuf;
2818 spin_lock_irqsave(&state->card->lock, flags);
2819 cs_update_ptr(card, CS_TRUE);
2820 abinfo.fragsize = dmabuf->fragsize;
2821 abinfo.fragstotal = dmabuf->numfrag;
2823 * for mmap we always have total space available
2826 abinfo.bytes = dmabuf->dmasize;
2828 abinfo.bytes = dmabuf->dmasize - dmabuf->count;
2830 abinfo.fragments = abinfo.bytes >> dmabuf->fragshift;
2831 spin_unlock_irqrestore(&state->card->lock, flags);
2832 return copy_to_user(argp, &abinfo, sizeof(abinfo)) ? -EFAULT : 0;
2836 case SNDCTL_DSP_GETISPACE:
2837 if (!(file->f_mode & FMODE_READ))
2839 state = (struct cs_state *)card->states[0];
2842 dmabuf = &state->dmabuf;
2843 spin_lock_irqsave(&state->card->lock, flags);
2844 cs_update_ptr(card, CS_TRUE);
2845 abinfo.fragsize = dmabuf->fragsize/dmabuf->divisor;
2846 abinfo.bytes = dmabuf->count/dmabuf->divisor;
2847 abinfo.fragstotal = dmabuf->numfrag;
2848 abinfo.fragments = abinfo.bytes >> dmabuf->fragshift;
2849 spin_unlock_irqrestore(&state->card->lock, flags);
2850 return copy_to_user(argp, &abinfo, sizeof(abinfo)) ? -EFAULT : 0;
2854 case SNDCTL_DSP_NONBLOCK:
2855 file->f_flags |= O_NONBLOCK;
2858 case SNDCTL_DSP_GETCAPS:
2859 return put_user(DSP_CAP_REALTIME|DSP_CAP_TRIGGER|DSP_CAP_MMAP,
2862 case SNDCTL_DSP_GETTRIGGER:
2864 CS_DBGOUT(CS_IOCTL, 2, printk("cs46xx: DSP_GETTRIGGER()+\n") );
2865 if (file->f_mode & FMODE_WRITE)
2867 state = (struct cs_state *)card->states[1];
2870 dmabuf = &state->dmabuf;
2871 if(dmabuf->enable & DAC_RUNNING)
2872 val |= PCM_ENABLE_INPUT;
2875 if (file->f_mode & FMODE_READ)
2879 state = (struct cs_state *)card->states[0];
2880 dmabuf = &state->dmabuf;
2881 if(dmabuf->enable & ADC_RUNNING)
2882 val |= PCM_ENABLE_OUTPUT;
2885 CS_DBGOUT(CS_IOCTL, 2, printk("cs46xx: DSP_GETTRIGGER()- val=0x%x\n",val) );
2886 return put_user(val, p);
2888 case SNDCTL_DSP_SETTRIGGER:
2889 if (get_user(val, p))
2891 if (file->f_mode & FMODE_READ) {
2892 state = (struct cs_state *)card->states[0];
2895 dmabuf = &state->dmabuf;
2896 if (val & PCM_ENABLE_INPUT) {
2897 if (!dmabuf->ready && (ret = prog_dmabuf(state)))
2904 if (file->f_mode & FMODE_WRITE) {
2905 state = (struct cs_state *)card->states[1];
2908 dmabuf = &state->dmabuf;
2909 if (val & PCM_ENABLE_OUTPUT) {
2910 if (!dmabuf->ready && (ret = prog_dmabuf(state)))
2919 case SNDCTL_DSP_GETIPTR:
2920 if (!(file->f_mode & FMODE_READ))
2922 state = (struct cs_state *)card->states[0];
2925 dmabuf = &state->dmabuf;
2926 spin_lock_irqsave(&state->card->lock, flags);
2927 cs_update_ptr(card, CS_TRUE);
2928 cinfo.bytes = dmabuf->total_bytes/dmabuf->divisor;
2929 cinfo.blocks = dmabuf->count/dmabuf->divisor >> dmabuf->fragshift;
2930 cinfo.ptr = dmabuf->hwptr/dmabuf->divisor;
2931 spin_unlock_irqrestore(&state->card->lock, flags);
2932 if (copy_to_user(argp, &cinfo, sizeof(cinfo)))
2938 case SNDCTL_DSP_GETOPTR:
2939 if (!(file->f_mode & FMODE_WRITE))
2941 state = (struct cs_state *)card->states[1];
2944 dmabuf = &state->dmabuf;
2945 spin_lock_irqsave(&state->card->lock, flags);
2946 cs_update_ptr(card, CS_TRUE);
2947 cinfo.bytes = dmabuf->total_bytes;
2950 cinfo.blocks = (cinfo.bytes >> dmabuf->fragshift)
2952 CS_DBGOUT(CS_PARMS, 8,
2953 printk("total_bytes=%d blocks=%d dmabuf->blocks=%d\n",
2954 cinfo.bytes,cinfo.blocks,dmabuf->blocks) );
2955 dmabuf->blocks = cinfo.bytes >> dmabuf->fragshift;
2959 cinfo.blocks = dmabuf->count >> dmabuf->fragshift;
2961 cinfo.ptr = dmabuf->hwptr;
2963 CS_DBGOUT(CS_PARMS, 4, printk(
2964 "cs46xx: GETOPTR bytes=%d blocks=%d ptr=%d\n",
2965 cinfo.bytes,cinfo.blocks,cinfo.ptr) );
2966 spin_unlock_irqrestore(&state->card->lock, flags);
2967 if (copy_to_user(argp, &cinfo, sizeof(cinfo)))
2973 case SNDCTL_DSP_SETDUPLEX:
2976 case SNDCTL_DSP_GETODELAY:
2977 if (!(file->f_mode & FMODE_WRITE))
2979 state = (struct cs_state *)card->states[1];
2982 dmabuf = &state->dmabuf;
2983 spin_lock_irqsave(&state->card->lock, flags);
2984 cs_update_ptr(card, CS_TRUE);
2985 val = dmabuf->count;
2986 spin_unlock_irqrestore(&state->card->lock, flags);
2990 return put_user(val, p);
2992 case SOUND_PCM_READ_RATE:
2993 if(file->f_mode & FMODE_READ)
2994 state = (struct cs_state *)card->states[0];
2996 state = (struct cs_state *)card->states[1];
2999 dmabuf = &state->dmabuf;
3000 return put_user(dmabuf->rate, p);
3002 return put_user(0, p);
3005 case SOUND_PCM_READ_CHANNELS:
3006 if(file->f_mode & FMODE_READ)
3007 state = (struct cs_state *)card->states[0];
3009 state = (struct cs_state *)card->states[1];
3012 dmabuf = &state->dmabuf;
3013 return put_user((dmabuf->fmt & CS_FMT_STEREO) ? 2 : 1,
3016 return put_user(0, p);
3018 case SOUND_PCM_READ_BITS:
3019 if(file->f_mode & FMODE_READ)
3020 state = (struct cs_state *)card->states[0];
3022 state = (struct cs_state *)card->states[1];
3025 dmabuf = &state->dmabuf;
3026 return put_user((dmabuf->fmt & CS_FMT_16BIT) ?
3027 AFMT_S16_LE : AFMT_U8, p);
3030 return put_user(0, p);
3032 case SNDCTL_DSP_MAPINBUF:
3033 case SNDCTL_DSP_MAPOUTBUF:
3034 case SNDCTL_DSP_SETSYNCRO:
3035 case SOUND_PCM_WRITE_FILTER:
3036 case SOUND_PCM_READ_FILTER:
3044 * AMP control - null AMP
3047 static void amp_none(struct cs_card *card, int change)
3055 static void amp_voyetra(struct cs_card *card, int change)
3057 /* Manage the EAPD bit on the Crystal 4297
3058 and the Analog AD1885 */
3060 int old=card->amplifier;
3062 card->amplifier+=change;
3063 if(card->amplifier && !old)
3065 /* Turn the EAPD amp on */
3066 cs_ac97_set(card->ac97_codec[0], AC97_POWER_CONTROL,
3067 cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) |
3070 else if(old && !card->amplifier)
3072 /* Turn the EAPD amp off */
3073 cs_ac97_set(card->ac97_codec[0], AC97_POWER_CONTROL,
3074 cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) &
3081 * Game Theatre XP card - EGPIO[2] is used to enable the external amp.
3084 static void amp_hercules(struct cs_card *card, int change)
3086 int old=card->amplifier;
3089 CS_DBGOUT(CS_ERROR, 2, printk(KERN_INFO
3090 "cs46xx: amp_hercules() called before initialized.\n"));
3093 card->amplifier+=change;
3094 if( (card->amplifier && !old) && !(hercules_egpio_disable))
3096 CS_DBGOUT(CS_PARMS, 4, printk(KERN_INFO
3097 "cs46xx: amp_hercules() external amp enabled\n"));
3098 cs461x_pokeBA0(card, BA0_EGPIODR,
3099 EGPIODR_GPOE2); /* enable EGPIO2 output */
3100 cs461x_pokeBA0(card, BA0_EGPIOPTR,
3101 EGPIOPTR_GPPT2); /* open-drain on output */
3103 else if(old && !card->amplifier)
3105 CS_DBGOUT(CS_PARMS, 4, printk(KERN_INFO
3106 "cs46xx: amp_hercules() external amp disabled\n"));
3107 cs461x_pokeBA0(card, BA0_EGPIODR, 0); /* disable */
3108 cs461x_pokeBA0(card, BA0_EGPIOPTR, 0); /* disable */
3113 * Handle the CLKRUN on a thinkpad. We must disable CLKRUN support
3114 * whenever we need to beat on the chip.
3116 * The original idea and code for this hack comes from David Kaiser at
3117 * Linuxcare. Perhaps one day Crystal will document their chips well
3118 * enough to make them useful.
3121 static void clkrun_hack(struct cs_card *card, int change)
3123 struct pci_dev *acpi_dev;
3127 int old=card->active;
3129 card->active+=change;
3131 acpi_dev = pci_find_device(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371AB_3, NULL);
3132 if(acpi_dev == NULL)
3133 return; /* Not a thinkpad thats for sure */
3135 /* Find the control port */
3136 pci_read_config_byte(acpi_dev, 0x41, &pp);
3139 /* Read ACPI port */
3140 control=inw(port+0x10);
3142 /* Flip CLKRUN off while running */
3143 if(!card->active && old)
3145 CS_DBGOUT(CS_PARMS , 9, printk( KERN_INFO
3146 "cs46xx: clkrun() enable clkrun - change=%d active=%d\n",
3147 change,card->active));
3148 outw(control|0x2000, port+0x10);
3153 * sometimes on a resume the bit is set, so always reset the bit.
3155 CS_DBGOUT(CS_PARMS , 9, printk( KERN_INFO
3156 "cs46xx: clkrun() disable clkrun - change=%d active=%d\n",
3157 change,card->active));
3158 outw(control&~0x2000, port+0x10);
3163 static int cs_open(struct inode *inode, struct file *file)
3165 struct cs_card *card = (struct cs_card *)file->private_data;
3166 struct cs_state *state = NULL;
3167 struct dmabuf *dmabuf = NULL;
3168 struct list_head *entry;
3169 unsigned int minor = iminor(inode);
3173 CS_DBGOUT(CS_OPEN | CS_FUNCTION, 2, printk("cs46xx: cs_open()+ file=%p %s %s\n",
3174 file, file->f_mode & FMODE_WRITE ? "FMODE_WRITE" : "",
3175 file->f_mode & FMODE_READ ? "FMODE_READ" : "") );
3177 list_for_each(entry, &cs46xx_devs)
3179 card = list_entry(entry, struct cs_card, list);
3181 if (!((card->dev_audio ^ minor) & ~0xf))
3184 if (entry == &cs46xx_devs)
3187 CS_DBGOUT(CS_FUNCTION | CS_OPEN, 2, printk(KERN_INFO
3188 "cs46xx: cs_open(): Error - unable to find audio card struct\n"));
3193 * hardcode state[0] for capture, [1] for playback
3195 if(file->f_mode & FMODE_READ)
3197 CS_DBGOUT(CS_WAVE_READ, 2, printk("cs46xx: cs_open() FMODE_READ\n") );
3198 if (card->states[0] == NULL) {
3199 state = card->states[0] = (struct cs_state *)
3200 kmalloc(sizeof(struct cs_state), GFP_KERNEL);
3203 memset(state, 0, sizeof(struct cs_state));
3204 mutex_init(&state->sem);
3205 dmabuf = &state->dmabuf;
3206 dmabuf->pbuf = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
3207 if(dmabuf->pbuf==NULL)
3210 card->states[0]=NULL;
3216 state = card->states[0];
3217 if(state->open_mode & FMODE_READ)
3220 dmabuf->channel = card->alloc_rec_pcm_channel(card);
3222 if (dmabuf->channel == NULL) {
3223 kfree (card->states[0]);
3224 card->states[0] = NULL;
3228 /* Now turn on external AMP if needed */
3230 state->card->active_ctrl(state->card,1);
3231 state->card->amplifier_ctrl(state->card,1);
3233 if( (tmp = cs46xx_powerup(card, CS_POWER_ADC)) )
3235 CS_DBGOUT(CS_ERROR | CS_INIT, 1, printk(KERN_INFO
3236 "cs46xx: cs46xx_powerup of ADC failed (0x%x)\n",tmp) );
3240 dmabuf->channel->state = state;
3241 /* initialize the virtual channel */
3243 state->magic = CS_STATE_MAGIC;
3244 init_waitqueue_head(&dmabuf->wait);
3245 mutex_init(&state->open_mutex);
3246 file->private_data = card;
3248 mutex_lock(&state->open_mutex);
3250 /* set default sample format. According to OSS Programmer's Guide /dev/dsp
3251 should be default to unsigned 8-bits, mono, with sample rate 8kHz and
3252 /dev/dspW will accept 16-bits sample */
3254 /* Default input is 8bit mono */
3255 dmabuf->fmt &= ~CS_FMT_MASK;
3256 dmabuf->type = CS_TYPE_ADC;
3257 dmabuf->ossfragshift = 0;
3258 dmabuf->ossmaxfrags = 0;
3259 dmabuf->subdivision = 0;
3260 cs_set_adc_rate(state, 8000);
3261 cs_set_divisor(dmabuf);
3263 state->open_mode |= FMODE_READ;
3264 mutex_unlock(&state->open_mutex);
3266 if(file->f_mode & FMODE_WRITE)
3268 CS_DBGOUT(CS_OPEN, 2, printk("cs46xx: cs_open() FMODE_WRITE\n") );
3269 if (card->states[1] == NULL) {
3270 state = card->states[1] = (struct cs_state *)
3271 kmalloc(sizeof(struct cs_state), GFP_KERNEL);
3274 memset(state, 0, sizeof(struct cs_state));
3275 mutex_init(&state->sem);
3276 dmabuf = &state->dmabuf;
3277 dmabuf->pbuf = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
3278 if(dmabuf->pbuf==NULL)
3281 card->states[1]=NULL;
3287 state = card->states[1];
3288 if(state->open_mode & FMODE_WRITE)
3291 dmabuf->channel = card->alloc_pcm_channel(card);
3293 if (dmabuf->channel == NULL) {
3294 kfree (card->states[1]);
3295 card->states[1] = NULL;
3299 /* Now turn on external AMP if needed */
3301 state->card->active_ctrl(state->card,1);
3302 state->card->amplifier_ctrl(state->card,1);
3304 if( (tmp = cs46xx_powerup(card, CS_POWER_DAC)) )
3306 CS_DBGOUT(CS_ERROR | CS_INIT, 1, printk(KERN_INFO
3307 "cs46xx: cs46xx_powerup of DAC failed (0x%x)\n",tmp) );
3311 dmabuf->channel->state = state;
3312 /* initialize the virtual channel */
3314 state->magic = CS_STATE_MAGIC;
3315 init_waitqueue_head(&dmabuf->wait);
3316 mutex_init(&state->open_mutex);
3317 file->private_data = card;
3319 mutex_lock(&state->open_mutex);
3321 /* set default sample format. According to OSS Programmer's Guide /dev/dsp
3322 should be default to unsigned 8-bits, mono, with sample rate 8kHz and
3323 /dev/dspW will accept 16-bits sample */
3325 /* Default output is 8bit mono. */
3326 dmabuf->fmt &= ~CS_FMT_MASK;
3327 dmabuf->type = CS_TYPE_DAC;
3328 dmabuf->ossfragshift = 0;
3329 dmabuf->ossmaxfrags = 0;
3330 dmabuf->subdivision = 0;
3331 cs_set_dac_rate(state, 8000);
3332 cs_set_divisor(dmabuf);
3334 state->open_mode |= FMODE_WRITE;
3335 mutex_unlock(&state->open_mutex);
3336 if((ret = prog_dmabuf(state)))
3339 CS_DBGOUT(CS_OPEN | CS_FUNCTION, 2, printk("cs46xx: cs_open()- 0\n") );
3340 return nonseekable_open(inode, file);
3343 static int cs_release(struct inode *inode, struct file *file)
3345 struct cs_card *card = (struct cs_card *)file->private_data;
3346 struct dmabuf *dmabuf;
3347 struct cs_state *state;
3349 CS_DBGOUT(CS_RELEASE | CS_FUNCTION, 2, printk("cs46xx: cs_release()+ file=%p %s %s\n",
3350 file, file->f_mode & FMODE_WRITE ? "FMODE_WRITE" : "",
3351 file->f_mode & FMODE_READ ? "FMODE_READ" : "") );
3353 if (!(file->f_mode & (FMODE_WRITE | FMODE_READ)))
3357 state = card->states[1];
3360 if ( (state->open_mode & FMODE_WRITE) & (file->f_mode & FMODE_WRITE) )
3362 CS_DBGOUT(CS_RELEASE, 2, printk("cs46xx: cs_release() FMODE_WRITE\n") );
3363 dmabuf = &state->dmabuf;
3364 cs_clear_tail(state);
3365 drain_dac(state, file->f_flags & O_NONBLOCK);
3366 /* stop DMA state machine and free DMA buffers/channels */
3367 mutex_lock(&state->open_mutex);
3369 dealloc_dmabuf(state);
3370 state->card->free_pcm_channel(state->card, dmabuf->channel->num);
3371 free_page((unsigned long)state->dmabuf.pbuf);
3373 /* we're covered by the open_mutex */
3374 mutex_unlock(&state->open_mutex);
3375 state->card->states[state->virt] = NULL;
3376 state->open_mode &= (~file->f_mode) & (FMODE_READ|FMODE_WRITE);
3378 if( (tmp = cs461x_powerdown(card, CS_POWER_DAC, CS_FALSE )) )
3380 CS_DBGOUT(CS_ERROR, 1, printk(KERN_INFO
3381 "cs46xx: cs_release_mixdev() powerdown DAC failure (0x%x)\n",tmp) );
3384 /* Now turn off external AMP if needed */
3385 state->card->amplifier_ctrl(state->card, -1);
3386 state->card->active_ctrl(state->card, -1);
3392 state = card->states[0];
3395 if ( (state->open_mode & FMODE_READ) & (file->f_mode & FMODE_READ) )
3397 CS_DBGOUT(CS_RELEASE, 2, printk("cs46xx: cs_release() FMODE_READ\n") );
3398 dmabuf = &state->dmabuf;
3399 mutex_lock(&state->open_mutex);
3401 dealloc_dmabuf(state);
3402 state->card->free_pcm_channel(state->card, dmabuf->channel->num);
3403 free_page((unsigned long)state->dmabuf.pbuf);
3405 /* we're covered by the open_mutex */
3406 mutex_unlock(&state->open_mutex);
3407 state->card->states[state->virt] = NULL;
3408 state->open_mode &= (~file->f_mode) & (FMODE_READ|FMODE_WRITE);
3410 if( (tmp = cs461x_powerdown(card, CS_POWER_ADC, CS_FALSE )) )
3412 CS_DBGOUT(CS_ERROR, 1, printk(KERN_INFO
3413 "cs46xx: cs_release_mixdev() powerdown ADC failure (0x%x)\n",tmp) );
3416 /* Now turn off external AMP if needed */
3417 state->card->amplifier_ctrl(state->card, -1);
3418 state->card->active_ctrl(state->card, -1);
3424 CS_DBGOUT(CS_FUNCTION | CS_RELEASE, 2, printk("cs46xx: cs_release()- 0\n") );
3428 static void printpm(struct cs_card *s)
3430 CS_DBGOUT(CS_PM, 9, printk("pm struct:\n"));
3431 CS_DBGOUT(CS_PM, 9, printk("flags:0x%x u32CLKCR1_SAVE: 0%x u32SSPMValue: 0x%x\n",
3432 (unsigned)s->pm.flags,s->pm.u32CLKCR1_SAVE,s->pm.u32SSPMValue));
3433 CS_DBGOUT(CS_PM, 9, printk("u32PPLVCvalue: 0x%x u32PPRVCvalue: 0x%x\n",
3434 s->pm.u32PPLVCvalue,s->pm.u32PPRVCvalue));
3435 CS_DBGOUT(CS_PM, 9, printk("u32FMLVCvalue: 0x%x u32FMRVCvalue: 0x%x\n",
3436 s->pm.u32FMLVCvalue,s->pm.u32FMRVCvalue));
3437 CS_DBGOUT(CS_PM, 9, printk("u32GPIORvalue: 0x%x u32JSCTLvalue: 0x%x\n",
3438 s->pm.u32GPIORvalue,s->pm.u32JSCTLvalue));
3439 CS_DBGOUT(CS_PM, 9, printk("u32SSCR: 0x%x u32SRCSA: 0x%x\n",
3440 s->pm.u32SSCR,s->pm.u32SRCSA));
3441 CS_DBGOUT(CS_PM, 9, printk("u32DacASR: 0x%x u32AdcASR: 0x%x\n",
3442 s->pm.u32DacASR,s->pm.u32AdcASR));
3443 CS_DBGOUT(CS_PM, 9, printk("u32DacSR: 0x%x u32AdcSR: 0x%x\n",
3444 s->pm.u32DacSR,s->pm.u32AdcSR));
3445 CS_DBGOUT(CS_PM, 9, printk("u32MIDCR_Save: 0x%x\n",
3446 s->pm.u32MIDCR_Save));
3447 CS_DBGOUT(CS_PM, 9, printk("u32AC97_powerdown: 0x%x _general_purpose 0x%x\n",
3448 s->pm.u32AC97_powerdown,s->pm.u32AC97_general_purpose));
3449 CS_DBGOUT(CS_PM, 9, printk("u32AC97_master_volume: 0x%x\n",
3450 s->pm.u32AC97_master_volume));
3451 CS_DBGOUT(CS_PM, 9, printk("u32AC97_headphone_volume: 0x%x\n",
3452 s->pm.u32AC97_headphone_volume));
3453 CS_DBGOUT(CS_PM, 9, printk("u32AC97_master_volume_mono: 0x%x\n",
3454 s->pm.u32AC97_master_volume_mono));
3455 CS_DBGOUT(CS_PM, 9, printk("u32AC97_pcm_out_volume: 0x%x\n",
3456 s->pm.u32AC97_pcm_out_volume));
3457 CS_DBGOUT(CS_PM, 9, printk("dmabuf_swptr_play: 0x%x dmabuf_count_play: %d\n",
3458 s->pm.dmabuf_swptr_play,s->pm.dmabuf_count_play));
3459 CS_DBGOUT(CS_PM, 9, printk("dmabuf_swptr_capture: 0x%x dmabuf_count_capture: %d\n",
3460 s->pm.dmabuf_swptr_capture,s->pm.dmabuf_count_capture));
3464 /****************************************************************************
3466 * Suspend - save the ac97 regs, mute the outputs and power down the part.
3468 ****************************************************************************/
3469 static void cs46xx_ac97_suspend(struct cs_card *card)
3472 struct ac97_codec *dev=card->ac97_codec[0];
3475 CS_DBGOUT(CS_PM, 9, printk("cs46xx: cs46xx_ac97_suspend()+\n"));
3479 stop_dac(card->states[1]);
3480 resync_dma_ptrs(card->states[1]);
3484 stop_adc(card->states[0]);
3485 resync_dma_ptrs(card->states[0]);
3488 for(Count = 0x2, i=0; (Count <= CS46XX_AC97_HIGHESTREGTORESTORE)
3489 && (i < CS46XX_AC97_NUMBER_RESTORE_REGS);
3492 card->pm.ac97[i] = cs_ac97_get(dev, BA0_AC97_RESET + Count);
3495 * Save the ac97 volume registers as well as the current powerdown state.
3496 * Now, mute the all the outputs (master, headphone, and mono), as well
3497 * as the PCM volume, in preparation for powering down the entire part.
3498 card->pm.u32AC97_master_volume = (u32)cs_ac97_get( dev,
3499 (u8)BA0_AC97_MASTER_VOLUME);
3500 card->pm.u32AC97_headphone_volume = (u32)cs_ac97_get(dev,
3501 (u8)BA0_AC97_HEADPHONE_VOLUME);
3502 card->pm.u32AC97_master_volume_mono = (u32)cs_ac97_get(dev,
3503 (u8)BA0_AC97_MASTER_VOLUME_MONO);
3504 card->pm.u32AC97_pcm_out_volume = (u32)cs_ac97_get(dev,
3505 (u8)BA0_AC97_PCM_OUT_VOLUME);
3510 cs_ac97_set(dev, (u8)BA0_AC97_MASTER_VOLUME, 0x8000);
3511 cs_ac97_set(dev, (u8)BA0_AC97_HEADPHONE_VOLUME, 0x8000);
3512 cs_ac97_set(dev, (u8)BA0_AC97_MASTER_VOLUME_MONO, 0x8000);
3513 cs_ac97_set(dev, (u8)BA0_AC97_PCM_OUT_VOLUME, 0x8000);
3516 * save the registers that cause pops
3518 card->pm.u32AC97_powerdown = (u32)cs_ac97_get(dev, (u8)AC97_POWER_CONTROL);
3519 card->pm.u32AC97_general_purpose = (u32)cs_ac97_get(dev, (u8)BA0_AC97_GENERAL_PURPOSE);
3521 * And power down everything on the AC97 codec.
3522 * well, for now, only power down the DAC/ADC and MIXER VREFON components.
3523 * trouble with removing VREF.
3525 if( (tmp = cs461x_powerdown(card, CS_POWER_DAC | CS_POWER_ADC |
3526 CS_POWER_MIXVON, CS_TRUE )) )
3528 CS_DBGOUT(CS_ERROR | CS_INIT, 1, printk(KERN_INFO
3529 "cs46xx: cs46xx_ac97_suspend() failure (0x%x)\n",tmp) );
3532 CS_DBGOUT(CS_PM, 9, printk("cs46xx: cs46xx_ac97_suspend()-\n"));
3535 /****************************************************************************
3537 * Resume - power up the part and restore its registers..
3539 ****************************************************************************/
3540 static void cs46xx_ac97_resume(struct cs_card *card)
3543 struct ac97_codec *dev=card->ac97_codec[0];
3545 CS_DBGOUT(CS_PM, 9, printk("cs46xx: cs46xx_ac97_resume()+\n"));
3548 * First, we restore the state of the general purpose register. This
3549 * contains the mic select (mic1 or mic2) and if we restore this after
3550 * we restore the mic volume/boost state and mic2 was selected at
3551 * suspend time, we will end up with a brief period of time where mic1
3552 * is selected with the volume/boost settings for mic2, causing
3553 * acoustic feedback. So we restore the general purpose register
3554 * first, thereby getting the correct mic selected before we restore
3555 * the mic volume/boost.
3557 cs_ac97_set(dev, (u8)BA0_AC97_GENERAL_PURPOSE,
3558 (u16)card->pm.u32AC97_general_purpose);
3560 * Now, while the outputs are still muted, restore the state of power
3563 cs_ac97_set(dev, (u8)BA0_AC97_POWERDOWN, (u16)card->pm.u32AC97_powerdown);
3564 mdelay(5 * cs_laptop_wait);
3566 * Restore just the first set of registers, from register number
3567 * 0x02 to the register number that ulHighestRegToRestore specifies.
3569 for( Count = 0x2, i=0;
3570 (Count <= CS46XX_AC97_HIGHESTREGTORESTORE)
3571 && (i < CS46XX_AC97_NUMBER_RESTORE_REGS);
3574 cs_ac97_set(dev, (u8)(BA0_AC97_RESET + Count), (u16)card->pm.ac97[i]);
3577 /* Check if we have to init the amplifier */
3579 card->amp_init(card);
3581 CS_DBGOUT(CS_PM, 9, printk("cs46xx: cs46xx_ac97_resume()-\n"));
3585 static int cs46xx_restart_part(struct cs_card *card)
3587 struct dmabuf *dmabuf;
3588 CS_DBGOUT(CS_PM | CS_FUNCTION, 4,
3589 printk( "cs46xx: cs46xx_restart_part()+\n"));
3592 dmabuf = &card->states[1]->dmabuf;
3594 resync_dma_ptrs(card->states[1]);
3595 cs_set_divisor(dmabuf);
3596 if(__prog_dmabuf(card->states[1]))
3598 CS_DBGOUT(CS_PM | CS_ERROR, 1,
3599 printk("cs46xx: cs46xx_restart_part()- (-1) prog_dmabuf() dac error\n"));
3602 cs_set_dac_rate(card->states[1], dmabuf->rate);
3606 dmabuf = &card->states[0]->dmabuf;
3608 resync_dma_ptrs(card->states[0]);
3609 cs_set_divisor(dmabuf);
3610 if(__prog_dmabuf(card->states[0]))
3612 CS_DBGOUT(CS_PM | CS_ERROR, 1,
3613 printk("cs46xx: cs46xx_restart_part()- (-1) prog_dmabuf() adc error\n"));
3616 cs_set_adc_rate(card->states[0], dmabuf->rate);
3618 card->pm.flags |= CS46XX_PM_RESUMED;
3620 start_adc(card->states[0]);
3622 start_dac(card->states[1]);
3624 card->pm.flags |= CS46XX_PM_IDLE;
3625 card->pm.flags &= ~(CS46XX_PM_SUSPENDING | CS46XX_PM_SUSPENDED
3626 | CS46XX_PM_RESUMING | CS46XX_PM_RESUMED);
3628 wake_up(&card->states[0]->dmabuf.wait);
3630 wake_up(&card->states[1]->dmabuf.wait);
3632 CS_DBGOUT(CS_PM | CS_FUNCTION, 4,
3633 printk( "cs46xx: cs46xx_restart_part()-\n"));
3638 static void cs461x_reset(struct cs_card *card);
3639 static void cs461x_proc_stop(struct cs_card *card);
3640 static int cs46xx_suspend(struct cs_card *card, pm_message_t state)
3643 CS_DBGOUT(CS_PM | CS_FUNCTION, 4,
3644 printk("cs46xx: cs46xx_suspend()+ flags=0x%x s=%p\n",
3645 (unsigned)card->pm.flags,card));
3647 * check the current state, only suspend if IDLE
3649 if(!(card->pm.flags & CS46XX_PM_IDLE))
3651 CS_DBGOUT(CS_PM | CS_ERROR, 2,
3652 printk("cs46xx: cs46xx_suspend() unable to suspend, not IDLE\n"));
3655 card->pm.flags &= ~CS46XX_PM_IDLE;
3656 card->pm.flags |= CS46XX_PM_SUSPENDING;
3658 card->active_ctrl(card,1);
3660 tmp = cs461x_peek(card, BA1_PFIE);
3663 cs461x_poke(card, BA1_PFIE, tmp); /* playback interrupt disable */
3665 tmp = cs461x_peek(card, BA1_CIE);
3668 cs461x_poke(card, BA1_CIE, tmp); /* capture interrupt disable */
3671 * Stop playback DMA.
3673 tmp = cs461x_peek(card, BA1_PCTL);
3674 cs461x_poke(card, BA1_PCTL, tmp & 0x0000ffff);
3679 tmp = cs461x_peek(card, BA1_CCTL);
3680 cs461x_poke(card, BA1_CCTL, tmp & 0xffff0000);
3684 card->pm.dmabuf_swptr_play = card->states[1]->dmabuf.swptr;
3685 card->pm.dmabuf_count_play = card->states[1]->dmabuf.count;
3689 card->pm.dmabuf_swptr_capture = card->states[0]->dmabuf.swptr;
3690 card->pm.dmabuf_count_capture = card->states[0]->dmabuf.count;
3693 cs46xx_ac97_suspend(card);
3696 * Reset the processor.
3700 cs461x_proc_stop(card);
3703 * Power down the DAC and ADC. For now leave the other areas on.
3705 cs_ac97_set(card->ac97_codec[0], AC97_POWER_CONTROL, 0x0300);
3708 * Power down the PLL.
3710 cs461x_pokeBA0(card, BA0_CLKCR1, 0);
3713 * Turn off the Processor by turning off the software clock enable flag in
3714 * the clock control register.
3716 tmp = cs461x_peekBA0(card, BA0_CLKCR1) & ~CLKCR1_SWCE;
3717 cs461x_pokeBA0(card, BA0_CLKCR1, tmp);
3719 card->active_ctrl(card,-1);
3721 card->pm.flags &= ~CS46XX_PM_SUSPENDING;
3722 card->pm.flags |= CS46XX_PM_SUSPENDED;
3726 CS_DBGOUT(CS_PM | CS_FUNCTION, 4,
3727 printk("cs46xx: cs46xx_suspend()- flags=0x%x\n",
3728 (unsigned)card->pm.flags));
3732 static int cs46xx_resume(struct cs_card *card)
3736 CS_DBGOUT(CS_PM | CS_FUNCTION, 4,
3737 printk( "cs46xx: cs46xx_resume()+ flags=0x%x\n",
3738 (unsigned)card->pm.flags));
3739 if(!(card->pm.flags & CS46XX_PM_SUSPENDED))
3741 CS_DBGOUT(CS_PM | CS_ERROR, 2,
3742 printk("cs46xx: cs46xx_resume() unable to resume, not SUSPENDED\n"));
3745 card->pm.flags |= CS46XX_PM_RESUMING;
3746 card->pm.flags &= ~CS46XX_PM_SUSPENDED;
3748 card->active_ctrl(card, 1);
3752 if (cs_hardware_init(card) != 0)
3754 CS_DBGOUT(CS_PM | CS_ERROR, 4, printk(
3755 "cs46xx: cs46xx_resume()- ERROR in cs_hardware_init()\n"));
3756 mdelay(10 * cs_laptop_wait);
3764 CS_DBGOUT(CS_PM | CS_ERROR, 1, printk(
3765 "cs46xx: cs46xx_resume()- cs_hardware_init() failed, retried %d times.\n",i));
3769 if(cs46xx_restart_part(card))
3771 CS_DBGOUT(CS_PM | CS_ERROR, 4, printk(
3772 "cs46xx: cs46xx_resume(): cs46xx_restart_part() returned error\n"));
3775 card->active_ctrl(card, -1);
3777 CS_DBGOUT(CS_PM | CS_FUNCTION, 4, printk("cs46xx: cs46xx_resume()- flags=0x%x\n",
3778 (unsigned)card->pm.flags));
3782 static /*const*/ struct file_operations cs461x_fops = {
3783 CS_OWNER CS_THIS_MODULE
3784 .llseek = no_llseek,
3791 .release = cs_release,
3794 /* Write AC97 codec registers */
3797 static u16 _cs_ac97_get(struct ac97_codec *dev, u8 reg)
3799 struct cs_card *card = dev->private_data;
3805 * 1. Write ACCAD = Command Address Register = 46Ch for AC97 register address
3806 * 2. Write ACCDA = Command Data Register = 470h for data to write to AC97
3807 * 3. Write ACCTL = Control Register = 460h for initiating the write
3808 * 4. Read ACCTL = 460h, DCV should be reset by now and 460h = 17h
3809 * 5. if DCV not cleared, break and return error
3810 * 6. Read ACSTS = Status Register = 464h, check VSTS bit
3813 cs461x_peekBA0(card, BA0_ACSDA);
3816 * Setup the AC97 control registers on the CS461x to send the
3817 * appropriate command to the AC97 to perform the read.
3818 * ACCAD = Command Address Register = 46Ch
3819 * ACCDA = Command Data Register = 470h
3820 * ACCTL = Control Register = 460h
3821 * set DCV - will clear when process completed
3822 * set CRW - Read command
3823 * set VFRM - valid frame enabled
3824 * set ESYN - ASYNC generation enabled
3825 * set RSTN - ARST# inactive, AC97 codec not reset
3828 cs461x_pokeBA0(card, BA0_ACCAD, reg);
3829 cs461x_pokeBA0(card, BA0_ACCDA, 0);
3830 cs461x_pokeBA0(card, BA0_ACCTL, ACCTL_DCV | ACCTL_CRW |
3831 ACCTL_VFRM | ACCTL_ESYN |
3836 * Wait for the read to occur.
3838 if(!(card->pm.flags & CS46XX_PM_IDLE))
3841 loopcnt = 500 * cs_laptop_wait;
3842 loopcnt *= cs_laptop_wait;
3843 for (count = 0; count < loopcnt; count++) {
3845 * First, we want to wait for a short time.
3847 udelay(10 * cs_laptop_wait);
3849 * Now, check to see if the read has completed.
3850 * ACCTL = 460h, DCV should be reset by now and 460h = 17h
3852 if (!(cs461x_peekBA0(card, BA0_ACCTL) & ACCTL_DCV))
3857 * Make sure the read completed.
3859 if (cs461x_peekBA0(card, BA0_ACCTL) & ACCTL_DCV) {
3860 CS_DBGOUT(CS_ERROR, 1, printk(KERN_WARNING
3861 "cs46xx: AC'97 read problem (ACCTL_DCV), reg = 0x%x returning 0xffff\n", reg));
3866 * Wait for the valid status bit to go active.
3869 if(!(card->pm.flags & CS46XX_PM_IDLE))
3873 loopcnt *= cs_laptop_wait;
3874 for (count = 0; count < loopcnt; count++) {
3876 * Read the AC97 status register.
3877 * ACSTS = Status Register = 464h
3878 * VSTS - Valid Status
3880 if (cs461x_peekBA0(card, BA0_ACSTS) & ACSTS_VSTS)
3882 udelay(10 * cs_laptop_wait);
3886 * Make sure we got valid status.
3888 if (!( (tmp=cs461x_peekBA0(card, BA0_ACSTS)) & ACSTS_VSTS)) {
3889 CS_DBGOUT(CS_ERROR, 2, printk(KERN_WARNING
3890 "cs46xx: AC'97 read problem (ACSTS_VSTS), reg = 0x%x val=0x%x 0xffff \n",
3896 * Read the data returned from the AC97 register.
3897 * ACSDA = Status Data Register = 474h
3899 CS_DBGOUT(CS_FUNCTION, 9, printk(KERN_INFO
3900 "cs46xx: cs_ac97_get() reg = 0x%x, val = 0x%x, BA0_ACCAD = 0x%x\n",
3901 reg, cs461x_peekBA0(card, BA0_ACSDA),
3902 cs461x_peekBA0(card, BA0_ACCAD)));
3903 ret = cs461x_peekBA0(card, BA0_ACSDA);
3907 static u16 cs_ac97_get(struct ac97_codec *dev, u8 reg)
3910 struct cs_card *card = dev->private_data;
3912 spin_lock(&card->ac97_lock);
3913 ret = _cs_ac97_get(dev, reg);
3914 spin_unlock(&card->ac97_lock);
3918 static void cs_ac97_set(struct ac97_codec *dev, u8 reg, u16 val)
3920 struct cs_card *card = dev->private_data;
3924 spin_lock(&card->ac97_lock);
3926 if(reg == AC97_CD_VOL)
3928 val2 = _cs_ac97_get(dev, AC97_CD_VOL);
3933 * 1. Write ACCAD = Command Address Register = 46Ch for AC97 register address
3934 * 2. Write ACCDA = Command Data Register = 470h for data to write to AC97
3935 * 3. Write ACCTL = Control Register = 460h for initiating the write
3936 * 4. Read ACCTL = 460h, DCV should be reset by now and 460h = 07h
3937 * 5. if DCV not cleared, break and return error
3941 * Setup the AC97 control registers on the CS461x to send the
3942 * appropriate command to the AC97 to perform the read.
3943 * ACCAD = Command Address Register = 46Ch
3944 * ACCDA = Command Data Register = 470h
3945 * ACCTL = Control Register = 460h
3946 * set DCV - will clear when process completed
3947 * reset CRW - Write command
3948 * set VFRM - valid frame enabled
3949 * set ESYN - ASYNC generation enabled
3950 * set RSTN - ARST# inactive, AC97 codec not reset
3952 cs461x_pokeBA0(card, BA0_ACCAD, reg);
3953 cs461x_pokeBA0(card, BA0_ACCDA, val);
3954 cs461x_peekBA0(card, BA0_ACCTL);
3955 cs461x_pokeBA0(card, BA0_ACCTL, 0 | ACCTL_VFRM | ACCTL_ESYN | ACCTL_RSTN);
3956 cs461x_pokeBA0(card, BA0_ACCTL, ACCTL_DCV | ACCTL_VFRM |
3957 ACCTL_ESYN | ACCTL_RSTN);
3958 for (count = 0; count < 1000; count++) {
3960 * First, we want to wait for a short time.
3962 udelay(10 * cs_laptop_wait);
3964 * Now, check to see if the write has completed.
3965 * ACCTL = 460h, DCV should be reset by now and 460h = 07h
3967 if (!(cs461x_peekBA0(card, BA0_ACCTL) & ACCTL_DCV))
3971 * Make sure the write completed.
3973 if (cs461x_peekBA0(card, BA0_ACCTL) & ACCTL_DCV)
3975 CS_DBGOUT(CS_ERROR, 1, printk(KERN_WARNING
3976 "cs46xx: AC'97 write problem, reg = 0x%x, val = 0x%x\n", reg, val));
3979 spin_unlock(&card->ac97_lock);
3982 * Adjust power if the mixer is selected/deselected according
3985 * IF the CD is a valid input source (mixer or direct) AND
3986 * the CD is not muted THEN power is needed
3988 * We do two things. When record select changes the input to
3989 * add/remove the CD we adjust the power count if the CD is
3992 * When the CD mute changes we adjust the power level if the
3993 * CD was a valid input.
3995 * We also check for CD volume != 0, as the CD mute isn't
3996 * normally tweaked from userspace.
3999 /* CD mute change ? */
4001 if(reg==AC97_CD_VOL)
4003 /* Mute bit change ? */
4004 if((val2^val)&0x8000 || ((val2 == 0x1f1f || val == 0x1f1f) && val2 != val))
4006 /* This is a hack but its cleaner than the alternatives.
4007 Right now card->ac97_codec[0] might be NULL as we are
4008 still doing codec setup. This does an early assignment
4009 to avoid the problem if it occurs */
4011 if(card->ac97_codec[0]==NULL)
4012 card->ac97_codec[0]=dev;
4015 if(val&0x8000 || val == 0x1f1f)
4016 card->amplifier_ctrl(card, -1);
4017 else /* Mute off power on */
4020 card->amp_init(card);
4021 card->amplifier_ctrl(card, 1);
4028 /* OSS /dev/mixer file operation methods */
4030 static int cs_open_mixdev(struct inode *inode, struct file *file)
4033 unsigned int minor = iminor(inode);
4034 struct cs_card *card=NULL;
4035 struct list_head *entry;
4038 CS_DBGOUT(CS_FUNCTION | CS_OPEN, 4,
4039 printk(KERN_INFO "cs46xx: cs_open_mixdev()+\n"));
4041 list_for_each(entry, &cs46xx_devs)
4043 card = list_entry(entry, struct cs_card, list);
4044 for (i = 0; i < NR_AC97; i++)
4045 if (card->ac97_codec[i] != NULL &&
4046 card->ac97_codec[i]->dev_mixer == minor)
4051 CS_DBGOUT(CS_FUNCTION | CS_OPEN | CS_ERROR, 2,
4052 printk(KERN_INFO "cs46xx: cs46xx_open_mixdev()- -ENODEV\n"));
4056 if(!card->ac97_codec[i])
4058 file->private_data = card->ac97_codec[i];
4060 card->active_ctrl(card,1);
4061 if(!CS_IN_USE(&card->mixer_use_cnt))
4063 if( (tmp = cs46xx_powerup(card, CS_POWER_MIXVON )) )
4065 CS_DBGOUT(CS_ERROR | CS_INIT, 1, printk(KERN_INFO
4066 "cs46xx: cs_open_mixdev() powerup failure (0x%x)\n",tmp) );
4070 card->amplifier_ctrl(card, 1);
4071 CS_INC_USE_COUNT(&card->mixer_use_cnt);
4072 CS_DBGOUT(CS_FUNCTION | CS_OPEN, 4,
4073 printk(KERN_INFO "cs46xx: cs_open_mixdev()- 0\n"));
4074 return nonseekable_open(inode, file);
4077 static int cs_release_mixdev(struct inode *inode, struct file *file)
4079 unsigned int minor = iminor(inode);
4080 struct cs_card *card=NULL;
4081 struct list_head *entry;
4085 CS_DBGOUT(CS_FUNCTION | CS_RELEASE, 4,
4086 printk(KERN_INFO "cs46xx: cs_release_mixdev()+\n"));
4087 list_for_each(entry, &cs46xx_devs)
4089 card = list_entry(entry, struct cs_card, list);
4090 for (i = 0; i < NR_AC97; i++)
4091 if (card->ac97_codec[i] != NULL &&
4092 card->ac97_codec[i]->dev_mixer == minor)
4097 CS_DBGOUT(CS_FUNCTION | CS_OPEN | CS_ERROR, 2,
4098 printk(KERN_INFO "cs46xx: cs46xx_open_mixdev()- -ENODEV\n"));
4102 if(!CS_DEC_AND_TEST(&card->mixer_use_cnt))
4104 CS_DBGOUT(CS_FUNCTION | CS_RELEASE, 4,
4105 printk(KERN_INFO "cs46xx: cs_release_mixdev()- no powerdown, usecnt>0\n"));
4106 card->active_ctrl(card, -1);
4107 card->amplifier_ctrl(card, -1);
4111 * ok, no outstanding mixer opens, so powerdown.
4113 if( (tmp = cs461x_powerdown(card, CS_POWER_MIXVON, CS_FALSE )) )
4115 CS_DBGOUT(CS_ERROR | CS_INIT, 1, printk(KERN_INFO
4116 "cs46xx: cs_release_mixdev() powerdown MIXVON failure (0x%x)\n",tmp) );
4117 card->active_ctrl(card, -1);
4118 card->amplifier_ctrl(card, -1);
4121 card->active_ctrl(card, -1);
4122 card->amplifier_ctrl(card, -1);
4123 CS_DBGOUT(CS_FUNCTION | CS_RELEASE, 4,
4124 printk(KERN_INFO "cs46xx: cs_release_mixdev()- 0\n"));
4128 static int cs_ioctl_mixdev(struct inode *inode, struct file *file, unsigned int cmd,
4131 struct ac97_codec *codec = (struct ac97_codec *)file->private_data;
4132 struct cs_card *card=NULL;
4133 struct list_head *entry;
4134 unsigned long __user *p = (long __user *)arg;
4136 #if CSDEBUG_INTERFACE
4139 if( (cmd == SOUND_MIXER_CS_GETDBGMASK) ||
4140 (cmd == SOUND_MIXER_CS_SETDBGMASK) ||
4141 (cmd == SOUND_MIXER_CS_GETDBGLEVEL) ||
4142 (cmd == SOUND_MIXER_CS_SETDBGLEVEL) ||
4143 (cmd == SOUND_MIXER_CS_APM))
4148 case SOUND_MIXER_CS_GETDBGMASK:
4149 return put_user(cs_debugmask, p);
4151 case SOUND_MIXER_CS_GETDBGLEVEL:
4152 return put_user(cs_debuglevel, p);
4154 case SOUND_MIXER_CS_SETDBGMASK:
4155 if (get_user(val, p))
4160 case SOUND_MIXER_CS_SETDBGLEVEL:
4161 if (get_user(val, p))
4163 cs_debuglevel = val;
4166 case SOUND_MIXER_CS_APM:
4167 if (get_user(val, p))
4169 if(val == CS_IOCTL_CMD_SUSPEND)
4171 list_for_each(entry, &cs46xx_devs)
4173 card = list_entry(entry, struct cs_card, list);
4174 cs46xx_suspend(card, PMSG_ON);
4178 else if(val == CS_IOCTL_CMD_RESUME)
4180 list_for_each(entry, &cs46xx_devs)
4182 card = list_entry(entry, struct cs_card, list);
4183 cs46xx_resume(card);
4188 CS_DBGOUT(CS_ERROR, 1, printk(KERN_INFO
4189 "cs46xx: mixer_ioctl(): invalid APM cmd (%d)\n",
4195 CS_DBGOUT(CS_ERROR, 1, printk(KERN_INFO
4196 "cs46xx: mixer_ioctl(): ERROR unknown debug cmd\n") );
4201 return codec->mixer_ioctl(codec, cmd, arg);
4204 static /*const*/ struct file_operations cs_mixer_fops = {
4205 CS_OWNER CS_THIS_MODULE
4206 .llseek = no_llseek,
4207 .ioctl = cs_ioctl_mixdev,
4208 .open = cs_open_mixdev,
4209 .release = cs_release_mixdev,
4212 /* AC97 codec initialisation. */
4213 static int __init cs_ac97_init(struct cs_card *card)
4217 struct ac97_codec *codec;
4220 CS_DBGOUT(CS_FUNCTION | CS_INIT, 2, printk(KERN_INFO
4221 "cs46xx: cs_ac97_init()+\n") );
4223 for (num_ac97 = 0; num_ac97 < NR_AC97; num_ac97++) {
4224 if ((codec = ac97_alloc_codec()) == NULL)
4227 /* initialize some basic codec information, other fields will be filled
4228 in ac97_probe_codec */
4229 codec->private_data = card;
4230 codec->id = num_ac97;
4232 codec->codec_read = cs_ac97_get;
4233 codec->codec_write = cs_ac97_set;
4235 if (ac97_probe_codec(codec) == 0)
4237 CS_DBGOUT(CS_FUNCTION | CS_INIT, 2, printk(KERN_INFO
4238 "cs46xx: cs_ac97_init()- codec number %d not found\n",
4240 card->ac97_codec[num_ac97] = NULL;
4243 CS_DBGOUT(CS_FUNCTION | CS_INIT, 2, printk(KERN_INFO
4244 "cs46xx: cs_ac97_init() found codec %d\n",num_ac97) );
4246 eid = cs_ac97_get(codec, AC97_EXTENDED_ID);
4250 printk(KERN_WARNING "cs46xx: codec %d not present\n",num_ac97);
4251 ac97_release_codec(codec);
4255 card->ac97_features = eid;
4257 if ((codec->dev_mixer = register_sound_mixer(&cs_mixer_fops, -1)) < 0) {
4258 printk(KERN_ERR "cs46xx: couldn't register mixer!\n");
4259 ac97_release_codec(codec);
4262 card->ac97_codec[num_ac97] = codec;
4264 CS_DBGOUT(CS_FUNCTION | CS_INIT, 2, printk(KERN_INFO
4265 "cs46xx: cs_ac97_init() ac97_codec[%d] set to %p\n",
4266 (unsigned int)num_ac97,
4268 /* if there is no secondary codec at all, don't probe any more */
4275 CS_DBGOUT(CS_FUNCTION | CS_INIT, 2, printk(KERN_INFO
4276 "cs46xx: cs_ac97_init()- %d\n", (unsigned int)num_ac97));
4281 * load the static image into the DSP
4283 #include "cs461x_image.h"
4284 static void cs461x_download_image(struct cs_card *card)
4286 unsigned i, j, temp1, temp2, offset, count;
4287 unsigned char __iomem *pBA1 = ioremap(card->ba1_addr, 0x40000);
4288 for( i=0; i < CLEAR__COUNT; i++)
4290 offset = ClrStat[i].BA1__DestByteOffset;
4291 count = ClrStat[i].BA1__SourceSize;
4292 for( temp1 = offset; temp1<(offset+count); temp1+=4 )
4293 writel(0, pBA1+temp1);
4296 for(i=0; i<FILL__COUNT; i++)
4298 temp2 = FillStat[i].Offset;
4299 for(j=0; j<(FillStat[i].Size)/4; j++)
4301 temp1 = (FillStat[i]).pFill[j];
4302 writel(temp1, pBA1+temp2+j*4);
4313 static void cs461x_reset(struct cs_card *card)
4318 * Write the reset bit of the SP control register.
4320 cs461x_poke(card, BA1_SPCR, SPCR_RSTSP);
4323 * Write the control register.
4325 cs461x_poke(card, BA1_SPCR, SPCR_DRQEN);
4328 * Clear the trap registers.
4330 for (idx = 0; idx < 8; idx++) {
4331 cs461x_poke(card, BA1_DREG, DREG_REGID_TRAP_SELECT + idx);
4332 cs461x_poke(card, BA1_TWPR, 0xFFFF);
4334 cs461x_poke(card, BA1_DREG, 0);
4337 * Set the frame timer to reflect the number of cycles per frame.
4339 cs461x_poke(card, BA1_FRMT, 0xadf);
4342 static void cs461x_clear_serial_FIFOs(struct cs_card *card, int type)
4344 int idx, loop, startfifo=0, endfifo=0, powerdown1 = 0;
4348 * See if the devices are powered down. If so, we must power them up first
4349 * or they will not respond.
4351 if (!((tmp = cs461x_peekBA0(card, BA0_CLKCR1)) & CLKCR1_SWCE)) {
4352 cs461x_pokeBA0(card, BA0_CLKCR1, tmp | CLKCR1_SWCE);
4357 * We want to clear out the serial port FIFOs so we don't end up playing
4358 * whatever random garbage happens to be in them. We fill the sample FIFOS
4359 * with zero (silence).
4361 cs461x_pokeBA0(card, BA0_SERBWP, 0);
4364 * Check for which FIFO locations to clear, if we are currently
4365 * playing or capturing then we don't want to put in 128 bytes of
4368 if(type & CS_TYPE_DAC)
4373 if(type & CS_TYPE_ADC)
4380 * Fill sample FIFO locations (256 locations total).
4382 for (idx = startfifo; idx < endfifo; idx++) {
4384 * Make sure the previous FIFO write operation has completed.
4386 for (loop = 0; loop < 5; loop++) {
4388 if (!(cs461x_peekBA0(card, BA0_SERBST) & SERBST_WBSY))
4391 if (cs461x_peekBA0(card, BA0_SERBST) & SERBST_WBSY) {
4393 cs461x_pokeBA0(card, BA0_CLKCR1, tmp);
4396 * Write the serial port FIFO index.
4398 cs461x_pokeBA0(card, BA0_SERBAD, idx);
4400 * Tell the serial port to load the new value into the FIFO location.
4402 cs461x_pokeBA0(card, BA0_SERBCM, SERBCM_WRC);
4405 * Now, if we powered up the devices, then power them back down again.
4406 * This is kinda ugly, but should never happen.
4409 cs461x_pokeBA0(card, BA0_CLKCR1, tmp);
4413 static int cs461x_powerdown(struct cs_card *card, unsigned int type, int suspendflag)
4416 unsigned int tmp=0,muted=0;
4418 CS_DBGOUT(CS_FUNCTION, 4, printk(KERN_INFO
4419 "cs46xx: cs461x_powerdown()+ type=0x%x\n",type));
4420 if(!cs_powerdown && !suspendflag)
4422 CS_DBGOUT(CS_FUNCTION, 8, printk(KERN_INFO
4423 "cs46xx: cs461x_powerdown() DISABLED exiting\n"));
4426 tmp = cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL);
4427 CS_DBGOUT(CS_FUNCTION, 8, printk(KERN_INFO
4428 "cs46xx: cs461x_powerdown() powerdown reg=0x%x\n",tmp));
4430 * if powering down only the VREF, and not powering down the DAC/ADC,
4431 * then do not power down the VREF, UNLESS both the DAC and ADC are not
4432 * currently powered down. If powering down DAC and ADC, then
4433 * it is possible to power down the VREF (ON).
4435 if ( ((type & CS_POWER_MIXVON) &&
4436 (!(type & CS_POWER_ADC) || (!(type & CS_POWER_DAC))) )
4438 ((tmp & CS_AC97_POWER_CONTROL_ADC_ON) ||
4439 (tmp & CS_AC97_POWER_CONTROL_DAC_ON) ) )
4441 CS_DBGOUT(CS_FUNCTION, 8, printk(KERN_INFO
4442 "cs46xx: cs461x_powerdown()- 0 unable to powerdown. tmp=0x%x\n",tmp));
4446 * for now, always keep power to the mixer block.
4447 * not sure why it's a problem but it seems to be if we power off.
4449 type &= ~CS_POWER_MIXVON;
4450 type &= ~CS_POWER_MIXVOFF;
4453 * Power down indicated areas.
4455 if(type & CS_POWER_MIXVOFF)
4458 CS_DBGOUT(CS_FUNCTION, 4,
4459 printk(KERN_INFO "cs46xx: cs461x_powerdown()+ MIXVOFF\n"));
4461 * Power down the MIXER (VREF ON) on the AC97 card.
4463 tmp = cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL);
4464 if (tmp & CS_AC97_POWER_CONTROL_MIXVOFF_ON)
4468 cs_mute(card, CS_TRUE);
4471 tmp |= CS_AC97_POWER_CONTROL_MIXVOFF;
4472 cs_ac97_set(card->ac97_codec[0], AC97_POWER_CONTROL, tmp );
4474 * Now, we wait until we sample a ready state.
4476 for (count = 0; count < 32; count++) {
4478 * First, lets wait a short while to let things settle out a
4479 * bit, and to prevent retrying the read too quickly.
4484 * Read the current state of the power control register.
4486 if (!(cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) &
4487 CS_AC97_POWER_CONTROL_MIXVOFF_ON))
4492 * Check the status..
4494 if (cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) &
4495 CS_AC97_POWER_CONTROL_MIXVOFF_ON)
4497 CS_DBGOUT(CS_ERROR, 1, printk(KERN_WARNING
4498 "cs46xx: powerdown MIXVOFF failed\n"));
4503 if(type & CS_POWER_MIXVON)
4506 CS_DBGOUT(CS_FUNCTION, 4,
4507 printk(KERN_INFO "cs46xx: cs461x_powerdown()+ MIXVON\n"));
4509 * Power down the MIXER (VREF ON) on the AC97 card.
4511 tmp = cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL);
4512 if (tmp & CS_AC97_POWER_CONTROL_MIXVON_ON)
4516 cs_mute(card, CS_TRUE);
4519 tmp |= CS_AC97_POWER_CONTROL_MIXVON;
4520 cs_ac97_set(card->ac97_codec[0], AC97_POWER_CONTROL, tmp );
4522 * Now, we wait until we sample a ready state.
4524 for (count = 0; count < 32; count++) {
4526 * First, lets wait a short while to let things settle out a
4527 * bit, and to prevent retrying the read too quickly.
4532 * Read the current state of the power control register.
4534 if (!(cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) &
4535 CS_AC97_POWER_CONTROL_MIXVON_ON))
4540 * Check the status..
4542 if (cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) &
4543 CS_AC97_POWER_CONTROL_MIXVON_ON)
4545 CS_DBGOUT(CS_ERROR, 1, printk(KERN_WARNING
4546 "cs46xx: powerdown MIXVON failed\n"));
4551 if(type & CS_POWER_ADC)
4554 * Power down the ADC on the AC97 card.
4556 CS_DBGOUT(CS_FUNCTION, 4, printk(KERN_INFO "cs46xx: cs461x_powerdown()+ ADC\n"));
4557 tmp = cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL);
4558 if (tmp & CS_AC97_POWER_CONTROL_ADC_ON)
4562 cs_mute(card, CS_TRUE);
4565 tmp |= CS_AC97_POWER_CONTROL_ADC;
4566 cs_ac97_set(card->ac97_codec[0], AC97_POWER_CONTROL, tmp );
4569 * Now, we wait until we sample a ready state.
4571 for (count = 0; count < 32; count++) {
4573 * First, lets wait a short while to let things settle out a
4574 * bit, and to prevent retrying the read too quickly.
4579 * Read the current state of the power control register.
4581 if (!(cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) &
4582 CS_AC97_POWER_CONTROL_ADC_ON))
4587 * Check the status..
4589 if (cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) &
4590 CS_AC97_POWER_CONTROL_ADC_ON)
4592 CS_DBGOUT(CS_ERROR, 1, printk(KERN_WARNING
4593 "cs46xx: powerdown ADC failed\n"));
4598 if(type & CS_POWER_DAC)
4601 * Power down the DAC on the AC97 card.
4604 CS_DBGOUT(CS_FUNCTION, 4,
4605 printk(KERN_INFO "cs46xx: cs461x_powerdown()+ DAC\n"));
4606 tmp = cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL);
4607 if (tmp & CS_AC97_POWER_CONTROL_DAC_ON)
4611 cs_mute(card, CS_TRUE);
4614 tmp |= CS_AC97_POWER_CONTROL_DAC;
4615 cs_ac97_set(card->ac97_codec[0], AC97_POWER_CONTROL, tmp );
4617 * Now, we wait until we sample a ready state.
4619 for (count = 0; count < 32; count++) {
4621 * First, lets wait a short while to let things settle out a
4622 * bit, and to prevent retrying the read too quickly.
4627 * Read the current state of the power control register.
4629 if (!(cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) &
4630 CS_AC97_POWER_CONTROL_DAC_ON))
4635 * Check the status..
4637 if (cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) &
4638 CS_AC97_POWER_CONTROL_DAC_ON)
4640 CS_DBGOUT(CS_ERROR, 1, printk(KERN_WARNING
4641 "cs46xx: powerdown DAC failed\n"));
4646 tmp = cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL);
4648 cs_mute(card, CS_FALSE);
4649 CS_DBGOUT(CS_FUNCTION, 4, printk(KERN_INFO
4650 "cs46xx: cs461x_powerdown()- 0 tmp=0x%x\n",tmp));
4654 static int cs46xx_powerup(struct cs_card *card, unsigned int type)
4657 unsigned int tmp=0,muted=0;
4659 CS_DBGOUT(CS_FUNCTION, 8, printk(KERN_INFO
4660 "cs46xx: cs46xx_powerup()+ type=0x%x\n",type));
4662 * check for VREF and powerup if need to.
4664 if(type & CS_POWER_MIXVON)
4665 type |= CS_POWER_MIXVOFF;
4666 if(type & (CS_POWER_DAC | CS_POWER_ADC))
4667 type |= CS_POWER_MIXVON | CS_POWER_MIXVOFF;
4670 * Power up indicated areas.
4672 if(type & CS_POWER_MIXVOFF)
4675 CS_DBGOUT(CS_FUNCTION, 4,
4676 printk(KERN_INFO "cs46xx: cs46xx_powerup()+ MIXVOFF\n"));
4678 * Power up the MIXER (VREF ON) on the AC97 card.
4680 tmp = cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL);
4681 if (!(tmp & CS_AC97_POWER_CONTROL_MIXVOFF_ON))
4685 cs_mute(card, CS_TRUE);
4688 tmp &= ~CS_AC97_POWER_CONTROL_MIXVOFF;
4689 cs_ac97_set(card->ac97_codec[0], AC97_POWER_CONTROL, tmp );
4691 * Now, we wait until we sample a ready state.
4693 for (count = 0; count < 32; count++) {
4695 * First, lets wait a short while to let things settle out a
4696 * bit, and to prevent retrying the read too quickly.
4701 * Read the current state of the power control register.
4703 if (cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) &
4704 CS_AC97_POWER_CONTROL_MIXVOFF_ON)
4709 * Check the status..
4711 if (!(cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) &
4712 CS_AC97_POWER_CONTROL_MIXVOFF_ON))
4714 CS_DBGOUT(CS_ERROR, 1, printk(KERN_WARNING
4715 "cs46xx: powerup MIXVOFF failed\n"));
4720 if(type & CS_POWER_MIXVON)
4723 CS_DBGOUT(CS_FUNCTION, 4,
4724 printk(KERN_INFO "cs46xx: cs46xx_powerup()+ MIXVON\n"));
4726 * Power up the MIXER (VREF ON) on the AC97 card.
4728 tmp = cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL);
4729 if (!(tmp & CS_AC97_POWER_CONTROL_MIXVON_ON))
4733 cs_mute(card, CS_TRUE);
4736 tmp &= ~CS_AC97_POWER_CONTROL_MIXVON;
4737 cs_ac97_set(card->ac97_codec[0], AC97_POWER_CONTROL, tmp );
4739 * Now, we wait until we sample a ready state.
4741 for (count = 0; count < 32; count++) {
4743 * First, lets wait a short while to let things settle out a
4744 * bit, and to prevent retrying the read too quickly.
4749 * Read the current state of the power control register.
4751 if (cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) &
4752 CS_AC97_POWER_CONTROL_MIXVON_ON)
4757 * Check the status..
4759 if (!(cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) &
4760 CS_AC97_POWER_CONTROL_MIXVON_ON))
4762 CS_DBGOUT(CS_ERROR, 1, printk(KERN_WARNING
4763 "cs46xx: powerup MIXVON failed\n"));
4768 if(type & CS_POWER_ADC)
4771 * Power up the ADC on the AC97 card.
4773 CS_DBGOUT(CS_FUNCTION, 4, printk(KERN_INFO "cs46xx: cs46xx_powerup()+ ADC\n"));
4774 tmp = cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL);
4775 if (!(tmp & CS_AC97_POWER_CONTROL_ADC_ON))
4779 cs_mute(card, CS_TRUE);
4782 tmp &= ~CS_AC97_POWER_CONTROL_ADC;
4783 cs_ac97_set(card->ac97_codec[0], AC97_POWER_CONTROL, tmp );
4786 * Now, we wait until we sample a ready state.
4788 for (count = 0; count < 32; count++) {
4790 * First, lets wait a short while to let things settle out a
4791 * bit, and to prevent retrying the read too quickly.
4796 * Read the current state of the power control register.
4798 if (cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) &
4799 CS_AC97_POWER_CONTROL_ADC_ON)
4804 * Check the status..
4806 if (!(cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) &
4807 CS_AC97_POWER_CONTROL_ADC_ON))
4809 CS_DBGOUT(CS_ERROR, 1, printk(KERN_WARNING
4810 "cs46xx: powerup ADC failed\n"));
4815 if(type & CS_POWER_DAC)
4818 * Power up the DAC on the AC97 card.
4821 CS_DBGOUT(CS_FUNCTION, 4,
4822 printk(KERN_INFO "cs46xx: cs46xx_powerup()+ DAC\n"));
4823 tmp = cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL);
4824 if (!(tmp & CS_AC97_POWER_CONTROL_DAC_ON))
4828 cs_mute(card, CS_TRUE);
4831 tmp &= ~CS_AC97_POWER_CONTROL_DAC;
4832 cs_ac97_set(card->ac97_codec[0], AC97_POWER_CONTROL, tmp );
4834 * Now, we wait until we sample a ready state.
4836 for (count = 0; count < 32; count++) {
4838 * First, lets wait a short while to let things settle out a
4839 * bit, and to prevent retrying the read too quickly.
4844 * Read the current state of the power control register.
4846 if (cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) &
4847 CS_AC97_POWER_CONTROL_DAC_ON)
4852 * Check the status..
4854 if (!(cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) &
4855 CS_AC97_POWER_CONTROL_DAC_ON))
4857 CS_DBGOUT(CS_ERROR, 1, printk(KERN_WARNING
4858 "cs46xx: powerup DAC failed\n"));
4863 tmp = cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL);
4865 cs_mute(card, CS_FALSE);
4866 CS_DBGOUT(CS_FUNCTION, 4, printk(KERN_INFO
4867 "cs46xx: cs46xx_powerup()- 0 tmp=0x%x\n",tmp));
4872 static void cs461x_proc_start(struct cs_card *card)
4877 * Set the frame timer to reflect the number of cycles per frame.
4879 cs461x_poke(card, BA1_FRMT, 0xadf);
4881 * Turn on the run, run at frame, and DMA enable bits in the local copy of
4882 * the SP control register.
4884 cs461x_poke(card, BA1_SPCR, SPCR_RUN | SPCR_RUNFR | SPCR_DRQEN);
4886 * Wait until the run at frame bit resets itself in the SP control
4889 for (cnt = 0; cnt < 25; cnt++) {
4891 if (!(cs461x_peek(card, BA1_SPCR) & SPCR_RUNFR))
4895 if (cs461x_peek(card, BA1_SPCR) & SPCR_RUNFR)
4896 printk(KERN_WARNING "cs46xx: SPCR_RUNFR never reset\n");
4899 static void cs461x_proc_stop(struct cs_card *card)
4902 * Turn off the run, run at frame, and DMA enable bits in the local copy of
4903 * the SP control register.
4905 cs461x_poke(card, BA1_SPCR, 0);
4908 static int cs_hardware_init(struct cs_card *card)
4910 unsigned long end_time;
4911 unsigned int tmp,count;
4913 CS_DBGOUT(CS_FUNCTION | CS_INIT, 2, printk(KERN_INFO
4914 "cs46xx: cs_hardware_init()+\n") );
4916 * First, blast the clock control register to zero so that the PLL starts
4917 * out in a known state, and blast the master serial port control register
4918 * to zero so that the serial ports also start out in a known state.
4920 cs461x_pokeBA0(card, BA0_CLKCR1, 0);
4921 cs461x_pokeBA0(card, BA0_SERMC1, 0);
4924 * If we are in AC97 mode, then we must set the part to a host controlled
4925 * AC-link. Otherwise, we won't be able to bring up the link.
4927 cs461x_pokeBA0(card, BA0_SERACC, SERACC_HSP | SERACC_CODEC_TYPE_1_03); /* 1.03 card */
4928 /* cs461x_pokeBA0(card, BA0_SERACC, SERACC_HSP | SERACC_CODEC_TYPE_2_0); */ /* 2.00 card */
4931 * Drive the ARST# pin low for a minimum of 1uS (as defined in the AC97
4932 * spec) and then drive it high. This is done for non AC97 modes since
4933 * there might be logic external to the CS461x that uses the ARST# line
4936 cs461x_pokeBA0(card, BA0_ACCTL, 1);
4938 cs461x_pokeBA0(card, BA0_ACCTL, 0);
4940 cs461x_pokeBA0(card, BA0_ACCTL, ACCTL_RSTN);
4943 * The first thing we do here is to enable sync generation. As soon
4944 * as we start receiving bit clock, we'll start producing the SYNC
4947 cs461x_pokeBA0(card, BA0_ACCTL, ACCTL_ESYN | ACCTL_RSTN);
4950 * Now wait for a short while to allow the AC97 part to start
4951 * generating bit clock (so we don't try to start the PLL without an
4954 mdelay(5 * cs_laptop_wait); /* 1 should be enough ?? (and pigs might fly) */
4957 * Set the serial port timing configuration, so that
4958 * the clock control circuit gets its clock from the correct place.
4960 cs461x_pokeBA0(card, BA0_SERMC1, SERMC1_PTC_AC97);
4963 * The part seems to not be ready for a while after a resume.
4964 * so, if we are resuming, then wait for 700 mils. Note that 600 mils
4965 * is not enough for some platforms! tested on an IBM Thinkpads and
4968 if(!(card->pm.flags & CS46XX_PM_IDLE))
4971 * Write the selected clock control setup to the hardware. Do not turn on
4972 * SWCE yet (if requested), so that the devices clocked by the output of
4973 * PLL are not clocked until the PLL is stable.
4975 cs461x_pokeBA0(card, BA0_PLLCC, PLLCC_LPF_1050_2780_KHZ | PLLCC_CDR_73_104_MHZ);
4976 cs461x_pokeBA0(card, BA0_PLLM, 0x3a);
4977 cs461x_pokeBA0(card, BA0_CLKCR2, CLKCR2_PDIVS_8);
4982 cs461x_pokeBA0(card, BA0_CLKCR1, CLKCR1_PLLP);
4985 * Wait until the PLL has stabilized.
4987 mdelay(5 * cs_laptop_wait); /* Again 1 should be enough ?? */
4990 * Turn on clocking of the core so that we can setup the serial ports.
4992 tmp = cs461x_peekBA0(card, BA0_CLKCR1) | CLKCR1_SWCE;
4993 cs461x_pokeBA0(card, BA0_CLKCR1, tmp);
4996 * Fill the serial port FIFOs with silence.
4998 cs461x_clear_serial_FIFOs(card,CS_TYPE_DAC | CS_TYPE_ADC);
5001 * Set the serial port FIFO pointer to the first sample in the FIFO.
5003 /* cs461x_pokeBA0(card, BA0_SERBSP, 0); */
5006 * Write the serial port configuration to the part. The master
5007 * enable bit is not set until all other values have been written.
5009 cs461x_pokeBA0(card, BA0_SERC1, SERC1_SO1F_AC97 | SERC1_SO1EN);
5010 cs461x_pokeBA0(card, BA0_SERC2, SERC2_SI1F_AC97 | SERC1_SO1EN);
5011 cs461x_pokeBA0(card, BA0_SERMC1, SERMC1_PTC_AC97 | SERMC1_MSPE);
5014 mdelay(5 * cs_laptop_wait); /* Shouldnt be needed ?? */
5017 * If we are resuming under 2.2.x then we can not schedule a timeout.
5018 * so, just spin the CPU.
5020 if(card->pm.flags & CS46XX_PM_IDLE)
5023 * Wait for the card ready signal from the AC97 card.
5025 end_time = jiffies + 3 * (HZ >> 2);
5028 * Read the AC97 status register to see if we've seen a CODEC READY
5029 * signal from the AC97 card.
5031 if (cs461x_peekBA0(card, BA0_ACSTS) & ACSTS_CRDY)
5033 current->state = TASK_UNINTERRUPTIBLE;
5034 schedule_timeout(1);
5035 } while (time_before(jiffies, end_time));
5039 for (count = 0; count < 100; count++) {
5040 // First, we want to wait for a short time.
5041 udelay(25 * cs_laptop_wait);
5043 if (cs461x_peekBA0(card, BA0_ACSTS) & ACSTS_CRDY)
5049 * Make sure CODEC is READY.
5051 if (!(cs461x_peekBA0(card, BA0_ACSTS) & ACSTS_CRDY)) {
5052 CS_DBGOUT(CS_ERROR | CS_INIT, 1, printk(KERN_WARNING
5053 "cs46xx: create - never read card ready from AC'97\n"));
5054 CS_DBGOUT(CS_ERROR | CS_INIT, 1, printk(KERN_WARNING
5055 "cs46xx: probably not a bug, try using the CS4232 driver,\n"));
5056 CS_DBGOUT(CS_ERROR | CS_INIT, 1, printk(KERN_WARNING
5057 "cs46xx: or turn off any automatic Power Management support in the BIOS.\n"));
5062 * Assert the vaid frame signal so that we can start sending commands
5065 cs461x_pokeBA0(card, BA0_ACCTL, ACCTL_VFRM | ACCTL_ESYN | ACCTL_RSTN);
5067 if(card->pm.flags & CS46XX_PM_IDLE)
5070 * Wait until we've sampled input slots 3 and 4 as valid, meaning that
5071 * the card is pumping ADC data across the AC-link.
5073 end_time = jiffies + 3 * (HZ >> 2);
5076 * Read the input slot valid register and see if input slots 3 and
5079 if ((cs461x_peekBA0(card, BA0_ACISV) & (ACISV_ISV3 | ACISV_ISV4)) == (ACISV_ISV3 | ACISV_ISV4))
5081 current->state = TASK_UNINTERRUPTIBLE;
5082 schedule_timeout(1);
5083 } while (time_before(jiffies, end_time));
5087 for (count = 0; count < 100; count++) {
5088 // First, we want to wait for a short time.
5089 udelay(25 * cs_laptop_wait);
5091 if ((cs461x_peekBA0(card, BA0_ACISV) & (ACISV_ISV3 | ACISV_ISV4)) == (ACISV_ISV3 | ACISV_ISV4))
5096 * Make sure input slots 3 and 4 are valid. If not, then return
5099 if ((cs461x_peekBA0(card, BA0_ACISV) & (ACISV_ISV3 | ACISV_ISV4)) != (ACISV_ISV3 | ACISV_ISV4)) {
5100 printk(KERN_WARNING "cs46xx: create - never read ISV3 & ISV4 from AC'97\n");
5105 * Now, assert valid frame and the slot 3 and 4 valid bits. This will
5106 * commense the transfer of digital audio data to the AC97 card.
5108 cs461x_pokeBA0(card, BA0_ACOSV, ACOSV_SLV3 | ACOSV_SLV4);
5111 * Turn off the Processor by turning off the software clock enable flag in
5112 * the clock control register.
5114 /* tmp = cs461x_peekBA0(card, BA0_CLKCR1) & ~CLKCR1_SWCE; */
5115 /* cs461x_pokeBA0(card, BA0_CLKCR1, tmp); */
5118 * Reset the processor.
5123 * Download the image to the processor.
5126 cs461x_download_image(card);
5129 * Stop playback DMA.
5131 tmp = cs461x_peek(card, BA1_PCTL);
5132 card->pctl = tmp & 0xffff0000;
5133 cs461x_poke(card, BA1_PCTL, tmp & 0x0000ffff);
5138 tmp = cs461x_peek(card, BA1_CCTL);
5139 card->cctl = tmp & 0x0000ffff;
5140 cs461x_poke(card, BA1_CCTL, tmp & 0xffff0000);
5142 /* initialize AC97 codec and register /dev/mixer */
5143 if(card->pm.flags & CS46XX_PM_IDLE)
5145 if (cs_ac97_init(card) <= 0)
5147 CS_DBGOUT(CS_ERROR | CS_INIT, 1, printk(KERN_INFO
5148 "cs46xx: cs_ac97_init() failure\n") );
5154 cs46xx_ac97_resume(card);
5157 cs461x_proc_start(card);
5160 * Enable interrupts on the part.
5162 cs461x_pokeBA0(card, BA0_HICR, HICR_IEV | HICR_CHGM);
5164 tmp = cs461x_peek(card, BA1_PFIE);
5166 cs461x_poke(card, BA1_PFIE, tmp); /* playback interrupt enable */
5168 tmp = cs461x_peek(card, BA1_CIE);
5171 cs461x_poke(card, BA1_CIE, tmp); /* capture interrupt enable */
5174 * If IDLE then Power down the part. We will power components up
5175 * when we need them.
5177 if(card->pm.flags & CS46XX_PM_IDLE)
5181 if( (tmp = cs46xx_powerup(card, CS_POWER_DAC | CS_POWER_ADC |
5182 CS_POWER_MIXVON )) )
5184 CS_DBGOUT(CS_ERROR | CS_INIT, 1, printk(KERN_INFO
5185 "cs46xx: cs461x_powerup() failure (0x%x)\n",tmp) );
5191 if( (tmp = cs461x_powerdown(card, CS_POWER_DAC | CS_POWER_ADC |
5192 CS_POWER_MIXVON, CS_FALSE )) )
5194 CS_DBGOUT(CS_ERROR | CS_INIT, 1, printk(KERN_INFO
5195 "cs46xx: cs461x_powerdown() failure (0x%x)\n",tmp) );
5200 CS_DBGOUT(CS_FUNCTION | CS_INIT, 2, printk(KERN_INFO
5201 "cs46xx: cs_hardware_init()- 0\n"));
5205 /* install the driver, we do not allocate hardware channel nor DMA buffer now, they are defered
5206 until "ACCESS" time (in prog_dmabuf called by open/read/write/ioctl/mmap) */
5217 void (*amp)(struct cs_card *, int);
5218 void (*amp_init)(struct cs_card *);
5219 void (*active)(struct cs_card *, int);
5222 static struct cs_card_type cards[] = {
5226 .name = "Genius Soundmaker 128 value",
5238 .name = "Mitac MI6020/21",
5244 .name = "Hercules Game Theatre XP",
5245 .amp = amp_hercules,
5250 .name = "Hercules Game Theatre XP",
5251 .amp = amp_hercules,
5256 .name = "Hercules Game Theatre XP",
5257 .amp = amp_hercules,
5262 .name = "Hercules Game Theatre XP",
5263 .amp = amp_hercules,
5268 .name = "Hercules Game Theatre XP",
5269 .amp = amp_hercules,
5274 .name = "Hercules Game Theatre XP",
5275 .amp = amp_hercules,
5280 .name = "Hercules Fortissimo II",
5283 /* Not sure if the 570 needs the clkrun hack */
5285 .vendor = PCI_VENDOR_ID_IBM,
5287 .name = "Thinkpad 570",
5289 .active = clkrun_hack,
5292 .vendor = PCI_VENDOR_ID_IBM,
5294 .name = "Thinkpad 600X/A20/T20",
5296 .active = clkrun_hack,
5299 .vendor = PCI_VENDOR_ID_IBM,
5301 .name = "Thinkpad 600E (unsupported)",
5304 .name = "Card without SSID set",
5309 MODULE_AUTHOR("Alan Cox <alan@redhat.com>, Jaroslav Kysela, <pcaudio@crystal.cirrus.com>");
5310 MODULE_DESCRIPTION("Crystal SoundFusion Audio Support");
5311 MODULE_LICENSE("GPL");
5314 static const char cs46xx_banner[] = KERN_INFO "Crystal 4280/46xx + AC97 Audio, version " CS46XX_MAJOR_VERSION "." CS46XX_MINOR_VERSION "." CS46XX_ARCH ", " __TIME__ " " __DATE__ "\n";
5315 static const char fndmsg[] = KERN_INFO "cs46xx: Found %d audio device(s).\n";
5317 static int __devinit cs46xx_probe(struct pci_dev *pci_dev,
5318 const struct pci_device_id *pciid)
5321 u16 ss_card, ss_vendor;
5322 struct cs_card *card;
5323 dma_addr_t dma_mask;
5324 struct cs_card_type *cp = &cards[0];
5326 CS_DBGOUT(CS_FUNCTION | CS_INIT, 2,
5327 printk(KERN_INFO "cs46xx: probe()+\n"));
5329 dma_mask = 0xffffffff; /* this enables playback and recording */
5330 if (pci_enable_device(pci_dev)) {
5331 CS_DBGOUT(CS_INIT | CS_ERROR, 1, printk(KERN_ERR
5332 "cs46xx: pci_enable_device() failed\n"));
5335 if (!RSRCISMEMORYREGION(pci_dev, 0) ||
5336 !RSRCISMEMORYREGION(pci_dev, 1)) {
5337 CS_DBGOUT(CS_ERROR, 1, printk(KERN_ERR
5338 "cs46xx: probe()- Memory region not assigned\n"));
5341 if (pci_dev->irq == 0) {
5342 CS_DBGOUT(CS_ERROR, 1, printk(KERN_ERR
5343 "cs46xx: probe() IRQ not assigned\n"));
5346 if (!pci_dma_supported(pci_dev, 0xffffffff)) {
5347 CS_DBGOUT(CS_ERROR, 1, printk(KERN_ERR
5348 "cs46xx: probe() architecture does not support 32bit PCI busmaster DMA\n"));
5351 pci_read_config_word(pci_dev, PCI_SUBSYSTEM_VENDOR_ID, &ss_vendor);
5352 pci_read_config_word(pci_dev, PCI_SUBSYSTEM_ID, &ss_card);
5354 if ((card = kmalloc(sizeof(struct cs_card), GFP_KERNEL)) == NULL) {
5355 printk(KERN_ERR "cs46xx: out of memory\n");
5358 memset(card, 0, sizeof(*card));
5359 card->ba0_addr = RSRCADDRESS(pci_dev, 0);
5360 card->ba1_addr = RSRCADDRESS(pci_dev, 1);
5361 card->pci_dev = pci_dev;
5362 card->irq = pci_dev->irq;
5363 card->magic = CS_CARD_MAGIC;
5364 spin_lock_init(&card->lock);
5365 spin_lock_init(&card->ac97_lock);
5367 pci_set_master(pci_dev);
5369 printk(cs46xx_banner);
5370 printk(KERN_INFO "cs46xx: Card found at 0x%08lx and 0x%08lx, IRQ %d\n",
5371 card->ba0_addr, card->ba1_addr, card->irq);
5373 card->alloc_pcm_channel = cs_alloc_pcm_channel;
5374 card->alloc_rec_pcm_channel = cs_alloc_rec_pcm_channel;
5375 card->free_pcm_channel = cs_free_pcm_channel;
5376 card->amplifier_ctrl = amp_none;
5377 card->active_ctrl = amp_none;
5381 if(cp->vendor == ss_vendor && cp->id == ss_card)
5383 card->amplifier_ctrl = cp->amp;
5385 card->active_ctrl = cp->active;
5387 card->amp_init = cp->amp_init;
5394 printk(KERN_INFO "cs46xx: Unknown card (%04X:%04X) at 0x%08lx/0x%08lx, IRQ %d\n",
5395 ss_vendor, ss_card, card->ba0_addr, card->ba1_addr, card->irq);
5399 printk(KERN_INFO "cs46xx: %s (%04X:%04X) at 0x%08lx/0x%08lx, IRQ %d\n",
5400 cp->name, ss_vendor, ss_card, card->ba0_addr, card->ba1_addr, card->irq);
5403 if (card->amplifier_ctrl==NULL)
5405 card->amplifier_ctrl = amp_none;
5406 card->active_ctrl = clkrun_hack;
5409 if (external_amp == 1)
5411 printk(KERN_INFO "cs46xx: Crystal EAPD support forced on.\n");
5412 card->amplifier_ctrl = amp_voyetra;
5417 printk(KERN_INFO "cs46xx: Activating CLKRUN hack for Thinkpad.\n");
5418 card->active_ctrl = clkrun_hack;
5421 * The thinkpads don't work well without runtime updating on their kernel
5422 * delay values (or any laptop with variable CPU speeds really).
5423 * so, just to be safe set the init delay to 2100. Eliminates
5424 * failures on T21 Thinkpads. remove this code when the udelay
5425 * and mdelay kernel code is replaced by a pm timer, or the delays
5426 * work well for battery and/or AC power both.
5428 if(card->active_ctrl == clkrun_hack)
5433 if((card->active_ctrl == clkrun_hack) && !(powerdown == 1))
5436 * for some currently unknown reason, powering down the DAC and ADC component
5437 * blocks on thinkpads causes some funky behavior... distoorrrtion and ac97
5438 * codec access problems. probably the serial clock becomes unsynced.
5439 * added code to sync the chips back up, but only helped about 70% the time.
5445 card->active_ctrl(card, 1);
5447 /* claim our iospace and irq */
5449 card->ba0 = ioremap_nocache(card->ba0_addr, CS461X_BA0_SIZE);
5450 card->ba1.name.data0 = ioremap_nocache(card->ba1_addr + BA1_SP_DMEM0, CS461X_BA1_DATA0_SIZE);
5451 card->ba1.name.data1 = ioremap_nocache(card->ba1_addr + BA1_SP_DMEM1, CS461X_BA1_DATA1_SIZE);
5452 card->ba1.name.pmem = ioremap_nocache(card->ba1_addr + BA1_SP_PMEM, CS461X_BA1_PRG_SIZE);
5453 card->ba1.name.reg = ioremap_nocache(card->ba1_addr + BA1_SP_REG, CS461X_BA1_REG_SIZE);
5455 CS_DBGOUT(CS_INIT, 4, printk(KERN_INFO
5456 "cs46xx: card=%p card->ba0=%p\n",card,card->ba0) );
5457 CS_DBGOUT(CS_INIT, 4, printk(KERN_INFO
5458 "cs46xx: card->ba1=%p %p %p %p\n",
5459 card->ba1.name.data0,
5460 card->ba1.name.data1,
5461 card->ba1.name.pmem,
5462 card->ba1.name.reg) );
5464 if(card->ba0 == 0 || card->ba1.name.data0 == 0 ||
5465 card->ba1.name.data1 == 0 || card->ba1.name.pmem == 0 ||
5466 card->ba1.name.reg == 0)
5469 if (request_irq(card->irq, &cs_interrupt, SA_SHIRQ, "cs46xx", card)) {
5470 printk(KERN_ERR "cs46xx: unable to allocate irq %d\n", card->irq);
5473 /* register /dev/dsp */
5474 if ((card->dev_audio = register_sound_dsp(&cs461x_fops, -1)) < 0) {
5475 printk(KERN_ERR "cs46xx: unable to register dsp\n");
5479 /* register /dev/midi */
5480 if((card->dev_midi = register_sound_midi(&cs_midi_fops, -1)) < 0)
5481 printk(KERN_ERR "cs46xx: unable to register midi\n");
5483 card->pm.flags |= CS46XX_PM_IDLE;
5486 if (cs_hardware_init(card) != 0)
5488 CS_DBGOUT(CS_ERROR, 4, printk(
5489 "cs46xx: ERROR in cs_hardware_init()... retrying\n"));
5490 for (j = 0; j < NR_AC97; j++)
5491 if (card->ac97_codec[j] != NULL) {
5492 unregister_sound_mixer(card->ac97_codec[j]->dev_mixer);
5493 ac97_release_codec(card->ac97_codec[j]);
5495 mdelay(10 * cs_laptop_wait);
5502 CS_DBGOUT(CS_PM | CS_ERROR, 1, printk(
5503 "cs46xx: cs46xx_probe()- cs_hardware_init() failed, retried %d times.\n",i));
5504 unregister_sound_dsp(card->dev_audio);
5506 unregister_sound_midi(card->dev_midi);
5510 init_waitqueue_head(&card->midi.open_wait);
5511 mutex_init(&card->midi.open_mutex);
5512 init_waitqueue_head(&card->midi.iwait);
5513 init_waitqueue_head(&card->midi.owait);
5514 cs461x_pokeBA0(card, BA0_MIDCR, MIDCR_MRST);
5515 cs461x_pokeBA0(card, BA0_MIDCR, 0);
5518 * Check if we have to init the amplifier, but probably already done
5519 * since the CD logic in the ac97 init code will turn on the ext amp.
5523 card->active_ctrl(card, -1);
5525 PCI_SET_DRIVER_DATA(pci_dev, card);
5526 PCI_SET_DMA_MASK(pci_dev, dma_mask);
5527 list_add(&card->list, &cs46xx_devs);
5529 CS_DBGOUT(CS_PM, 9, printk(KERN_INFO "cs46xx: pm.flags=0x%x card=%p\n",
5530 (unsigned)card->pm.flags,card));
5532 CS_DBGOUT(CS_INIT | CS_FUNCTION, 2, printk(KERN_INFO
5533 "cs46xx: probe()- device allocated successfully\n"));
5537 free_irq(card->irq, card);
5541 if(card->ba1.name.data0)
5542 iounmap(card->ba1.name.data0);
5543 if(card->ba1.name.data1)
5544 iounmap(card->ba1.name.data1);
5545 if(card->ba1.name.pmem)
5546 iounmap(card->ba1.name.pmem);
5547 if(card->ba1.name.reg)
5548 iounmap(card->ba1.name.reg);
5550 CS_DBGOUT(CS_INIT | CS_ERROR, 1, printk(KERN_INFO
5551 "cs46xx: probe()- no device allocated\n"));
5555 // ---------------------------------------------------------------------
5557 static void __devexit cs46xx_remove(struct pci_dev *pci_dev)
5559 struct cs_card *card = PCI_GET_DRIVER_DATA(pci_dev);
5563 CS_DBGOUT(CS_INIT | CS_FUNCTION, 2, printk(KERN_INFO
5564 "cs46xx: cs46xx_remove()+\n"));
5566 card->active_ctrl(card,1);
5568 tmp = cs461x_peek(card, BA1_PFIE);
5571 cs461x_poke(card, BA1_PFIE, tmp); /* playback interrupt disable */
5573 tmp = cs461x_peek(card, BA1_CIE);
5576 cs461x_poke(card, BA1_CIE, tmp); /* capture interrupt disable */
5579 * Stop playback DMA.
5581 tmp = cs461x_peek(card, BA1_PCTL);
5582 cs461x_poke(card, BA1_PCTL, tmp & 0x0000ffff);
5587 tmp = cs461x_peek(card, BA1_CCTL);
5588 cs461x_poke(card, BA1_CCTL, tmp & 0xffff0000);
5591 * Reset the processor.
5595 cs461x_proc_stop(card);
5598 * Power down the DAC and ADC. We will power them up (if) when we need
5601 if( (tmp = cs461x_powerdown(card, CS_POWER_DAC | CS_POWER_ADC |
5602 CS_POWER_MIXVON, CS_TRUE )) )
5604 CS_DBGOUT(CS_ERROR | CS_INIT, 1, printk(KERN_INFO
5605 "cs46xx: cs461x_powerdown() failure (0x%x)\n",tmp) );
5609 * Power down the PLL.
5611 cs461x_pokeBA0(card, BA0_CLKCR1, 0);
5614 * Turn off the Processor by turning off the software clock enable flag in
5615 * the clock control register.
5617 tmp = cs461x_peekBA0(card, BA0_CLKCR1) & ~CLKCR1_SWCE;
5618 cs461x_pokeBA0(card, BA0_CLKCR1, tmp);
5620 card->active_ctrl(card,-1);
5622 /* free hardware resources */
5623 free_irq(card->irq, card);
5625 iounmap(card->ba1.name.data0);
5626 iounmap(card->ba1.name.data1);
5627 iounmap(card->ba1.name.pmem);
5628 iounmap(card->ba1.name.reg);
5630 /* unregister audio devices */
5631 for (i = 0; i < NR_AC97; i++)
5632 if (card->ac97_codec[i] != NULL) {
5633 unregister_sound_mixer(card->ac97_codec[i]->dev_mixer);
5634 ac97_release_codec(card->ac97_codec[i]);
5636 unregister_sound_dsp(card->dev_audio);
5638 unregister_sound_midi(card->dev_midi);
5639 list_del(&card->list);
5641 PCI_SET_DRIVER_DATA(pci_dev,NULL);
5643 CS_DBGOUT(CS_INIT | CS_FUNCTION, 2, printk(KERN_INFO
5644 "cs46xx: cs46xx_remove()-: remove successful\n"));
5649 CS46XX_4612, /* same as 4630 */
5650 CS46XX_4615, /* same as 4624 */
5653 static struct pci_device_id cs46xx_pci_tbl[] = {
5655 .vendor = PCI_VENDOR_ID_CIRRUS,
5656 .device = PCI_DEVICE_ID_CIRRUS_4610,
5657 .subvendor = PCI_ANY_ID,
5658 .subdevice = PCI_ANY_ID,
5659 .driver_data = CS46XX_4610,
5662 .vendor = PCI_VENDOR_ID_CIRRUS,
5663 .device = PCI_DEVICE_ID_CIRRUS_4612,
5664 .subvendor = PCI_ANY_ID,
5665 .subdevice = PCI_ANY_ID,
5666 .driver_data = CS46XX_4612,
5669 .vendor = PCI_VENDOR_ID_CIRRUS,
5670 .device = PCI_DEVICE_ID_CIRRUS_4615,
5671 .subvendor = PCI_ANY_ID,
5672 .subdevice = PCI_ANY_ID,
5673 .driver_data = CS46XX_4615,
5678 MODULE_DEVICE_TABLE(pci, cs46xx_pci_tbl);
5680 static struct pci_driver cs46xx_pci_driver = {
5682 .id_table = cs46xx_pci_tbl,
5683 .probe = cs46xx_probe,
5684 .remove = __devexit_p(cs46xx_remove),
5685 .suspend = CS46XX_SUSPEND_TBL,
5686 .resume = CS46XX_RESUME_TBL,
5689 static int __init cs46xx_init_module(void)
5692 CS_DBGOUT(CS_INIT | CS_FUNCTION, 2, printk(KERN_INFO
5693 "cs46xx: cs46xx_init_module()+ \n"));
5694 rtn = pci_register_driver(&cs46xx_pci_driver);
5698 CS_DBGOUT(CS_ERROR | CS_INIT, 1, printk(
5699 "cs46xx: Unable to detect valid cs46xx device\n"));
5702 CS_DBGOUT(CS_INIT | CS_FUNCTION, 2,
5703 printk(KERN_INFO "cs46xx: cs46xx_init_module()- (%d)\n",rtn));
5707 static void __exit cs46xx_cleanup_module(void)
5709 pci_unregister_driver(&cs46xx_pci_driver);
5710 CS_DBGOUT(CS_INIT | CS_FUNCTION, 2,
5711 printk(KERN_INFO "cs46xx: cleanup_cs46xx() finished\n"));
5714 module_init(cs46xx_init_module);
5715 module_exit(cs46xx_cleanup_module);
5717 #if CS46XX_ACPI_SUPPORT
5718 static int cs46xx_suspend_tbl(struct pci_dev *pcidev, pm_message_t state)
5720 struct cs_card *s = PCI_GET_DRIVER_DATA(pcidev);
5721 CS_DBGOUT(CS_PM | CS_FUNCTION, 2,
5722 printk(KERN_INFO "cs46xx: cs46xx_suspend_tbl request\n"));
5723 cs46xx_suspend(s, state);
5727 static int cs46xx_resume_tbl(struct pci_dev *pcidev)
5729 struct cs_card *s = PCI_GET_DRIVER_DATA(pcidev);
5730 CS_DBGOUT(CS_PM | CS_FUNCTION, 2,
5731 printk(KERN_INFO "cs46xx: cs46xx_resume_tbl request\n"));