2 * Crystal SoundFusion CS46xx driver
4 * Copyright 1998-2000 Cirrus Logic Corporation <audio@crystal.cirrus.com>
5 * Copyright 1999-2000 Jaroslav Kysela <perex@suse.cz>
6 * Copyright 2000 Alan Cox <alan@redhat.com>
8 * The core of this code is taken from the ALSA project driver by
9 * Jaroslav. Please send Jaroslav the credit for the driver and
10 * report bugs in this port to <alan@redhat.com>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * 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()
49 * Playback/Capture supported from 8k-48k.
50 * 16Bit Signed LE & 8Bit Unsigned, with Mono or Stereo supported.
53 #include <linux/module.h>
54 #include <linux/version.h>
55 #include <linux/string.h>
56 #include <linux/ctype.h>
57 #include <linux/ioport.h>
58 #include <linux/sched.h>
59 #include <linux/delay.h>
60 #include <linux/sound.h>
61 #include <linux/malloc.h>
62 #include <linux/soundcard.h>
63 #include <linux/pci.h>
69 #include <linux/init.h>
70 #include <linux/poll.h>
71 #include <linux/spinlock.h>
72 #include <linux/ac97_codec.h>
73 #include <linux/wrapper.h>
74 #include <asm/uaccess.h>
75 #include <asm/hardirq.h>
80 /* MIDI buffer sizes */
81 #define CS_MIDIINBUF 500
82 #define CS_MIDIOUTBUF 500
87 #define CS_FMT_16BIT 1 /* These are fixed in fact */
88 #define CS_FMT_STEREO 2
97 #define CS461X_BA0_SIZE 0x2000
98 #define CS461X_BA1_DATA0_SIZE 0x3000
99 #define CS461X_BA1_DATA1_SIZE 0x3800
100 #define CS461X_BA1_PRG_SIZE 0x7000
101 #define CS461X_BA1_REG_SIZE 0x0100
103 #define GOF_PER_SEC 200
105 #define CSDEBUG_INTERFACE 1
108 * Turn on/off debugging compilation by using 1/0 respectively for CSDEBUG
111 * CSDEBUG is usual mode is set to 1, then use the
112 * cs_debuglevel and cs_debugmask to turn on or off debugging.
113 * Debug level of 1 has been defined to be kernel errors and info
114 * that should be printed on any released driver.
117 #define CS_DBGOUT(mask,level,x) if((cs_debuglevel >= (level)) && ((mask) & cs_debugmask)) {x;}
119 #define CS_DBGOUT(mask,level,x)
124 #define CS_INIT 0x00000001 /* initialization and probe functions */
125 #define CS_ERROR 0x00000002 /* tmp debugging bit placeholder */
126 #define CS_INTERRUPT 0x00000004 /* interrupt handler (separate from all other) */
127 #define CS_FUNCTION 0x00000008 /* enter/leave functions */
128 #define CS_WAVE_WRITE 0x00000010 /* write information for wave */
129 #define CS_WAVE_READ 0x00000020 /* read information for wave */
130 #define CS_MIDI_WRITE 0x00000040 /* write information for midi */
131 #define CS_MIDI_READ 0x00000080 /* read information for midi */
132 #define CS_MPU401_WRITE 0x00000100 /* write information for mpu401 */
133 #define CS_MPU401_READ 0x00000200 /* read information for mpu401 */
134 #define CS_OPEN 0x00000400 /* all open functions in the driver */
135 #define CS_RELEASE 0x00000800 /* all release functions in the driver */
136 #define CS_PARMS 0x00001000 /* functional and operational parameters */
137 #define CS_IOCTL 0x00002000 /* ioctl (non-mixer) */
138 #define CS_TMP 0x10000000 /* tmp debug mask bit */
141 static unsigned long cs_debuglevel
=1; /* levels range from 1-9 */
142 static unsigned long cs_debugmask
=CS_INIT
| CS_ERROR
; /* use CS_DBGOUT with various mask values */
144 #define DMABUF_DEFAULTORDER 3
145 static unsigned long defaultorder
=DMABUF_DEFAULTORDER
;
147 MODULE_PARM(defaultorder
, "i");
148 MODULE_PARM(cs_debuglevel
, "i");
149 MODULE_PARM(cs_debugmask
, "i");
152 static int external_amp
= 0;
153 static int thinkpad
= 0;
156 /* An instance of the 4610 channel */
165 #define DRIVER_VERSION "1.10"
167 /* magic numbers to protect our data structures */
168 #define CS_CARD_MAGIC 0x43525553 /* "CRUS" */
169 #define CS_STATE_MAGIC 0x4c4f4749 /* "LOGI" */
172 /* maxinum number of AC97 codecs connected, AC97 2.0 defined 4 */
175 static const unsigned sample_size
[] = { 1, 2, 2, 4 };
176 static const unsigned sample_shift
[] = { 0, 1, 1, 2 };
178 /* "software" or virtual channel, an instance of opened /dev/dsp */
181 struct cs_card
*card
; /* Card info */
183 /* single open lock mechanism, only used for recording */
184 struct semaphore open_sem
;
185 wait_queue_head_t open_wait
;
190 /* virtual channel number */
194 /* wave sample stuff */
196 unsigned char fmt
, enable
;
198 /* hardware channel */
199 struct cs_channel
*channel
;
200 int pringbuf
; /* Software ring slot */
201 void *pbuf
; /* 4K hardware DMA buffer */
203 /* OSS buffer management stuff */
205 dma_addr_t dma_handle
;
211 void *tmpbuff
; /* tmp buffer for sample conversions */
212 dma_addr_t dma_handle_tmpbuff
;
213 unsigned buforder_tmpbuff
; /* Log base 2 of size in bytes.. */
215 /* our buffer acts like a circular ring */
216 unsigned hwptr
; /* where dma last started, updated by update_ptr */
217 unsigned swptr
; /* where driver last clear/filled, updated by read/write */
218 int count
; /* bytes to be comsumed or been generated by dma machine */
219 unsigned total_bytes
; /* total bytes dmaed by hardware */
220 unsigned blocks
; /* total blocks */
222 unsigned error
; /* number of over/underruns */
223 unsigned underrun
; /* underrun pending before next write has occurred */
224 wait_queue_head_t wait
; /* put process on wait queue when no more space in buffer */
226 /* redundant, but makes calculations easier */
229 unsigned fragsamples
;
234 unsigned endcleared
:1;
236 unsigned update_flag
;
237 unsigned ossfragshift
;
239 unsigned subdivision
;
245 struct cs_channel channel
[2];
248 /* We keep cs461x cards in a linked list */
249 struct cs_card
*next
;
251 /* The cs461x has a certain amount of cross channel interaction
252 so we use a single per card lock */
255 /* PCI device stuff */
256 struct pci_dev
* pci_dev
;
258 unsigned int pctl
, cctl
; /* Hardware DMA flag sets */
260 /* soundcore stuff */
264 /* structures for abstraction of hardware facilities, codecs, banks and channels*/
265 struct ac97_codec
*ac97_codec
[NR_AC97
];
266 struct cs_state
*states
[2];
270 int amplifier
; /* Amplifier control */
271 void (*amplifier_ctrl
)(struct cs_card
*, int);
273 int active
; /* Active clocking */
274 void (*active_ctrl
)(struct cs_card
*, int);
276 /* hardware resources */
277 unsigned long ba0_addr
;
278 unsigned long ba1_addr
;
295 /* Function support */
296 struct cs_channel
*(*alloc_pcm_channel
)(struct cs_card
*);
297 struct cs_channel
*(*alloc_rec_pcm_channel
)(struct cs_card
*);
298 void (*free_pcm_channel
)(struct cs_card
*, int chan
);
300 /* /dev/midi stuff */
302 unsigned ird
, iwr
, icnt
;
303 unsigned ord
, owr
, ocnt
;
304 wait_queue_head_t open_wait
;
305 wait_queue_head_t iwait
;
306 wait_queue_head_t owait
;
308 unsigned char ibuf
[CS_MIDIINBUF
];
309 unsigned char obuf
[CS_MIDIOUTBUF
];
311 struct semaphore open_sem
;
315 static struct cs_card
*devs
= NULL
;
317 static int cs_open_mixdev(struct inode
*inode
, struct file
*file
);
318 static int cs_release_mixdev(struct inode
*inode
, struct file
*file
);
319 static int cs_ioctl_mixdev(struct inode
*inode
, struct file
*file
, unsigned int cmd
,
321 static loff_t
cs_llseek(struct file
*file
, loff_t offset
, int origin
);
323 extern __inline__
unsigned ld2(unsigned int x
)
352 #define SOUND_MIXER_CS_GETDBGLEVEL _SIOWR('M',120, int)
353 #define SOUND_MIXER_CS_SETDBGLEVEL _SIOWR('M',121, int)
354 #define SOUND_MIXER_CS_GETDBGMASK _SIOWR('M',122, int)
355 #define SOUND_MIXER_CS_SETDBGMASK _SIOWR('M',123, int)
357 #define SNDCTL_DSP_CS_GETDBGLEVEL _SIOWR('P', 50, int)
358 #define SNDCTL_DSP_CS_SETDBGLEVEL _SIOWR('P', 51, int)
359 #define SNDCTL_DSP_CS_GETDBGMASK _SIOWR('P', 52, int)
360 #define SNDCTL_DSP_CS_SETDBGMASK _SIOWR('P', 53, int)
362 static void printioctl(unsigned int x
)
366 /* these values are incorrect for the ac97 driver, fix.
367 * Index of mixtable1[] member is Device ID
368 * and must be <= SOUND_MIXER_NRDEVICES.
369 * Value of array member is index into s->mix.vol[]
371 static const unsigned char mixtable1
[SOUND_MIXER_NRDEVICES
] = {
372 [SOUND_MIXER_PCM
] = 1, /* voice */
373 [SOUND_MIXER_LINE1
] = 2, /* AUX */
374 [SOUND_MIXER_CD
] = 3, /* CD */
375 [SOUND_MIXER_LINE
] = 4, /* Line */
376 [SOUND_MIXER_SYNTH
] = 5, /* FM */
377 [SOUND_MIXER_MIC
] = 6, /* Mic */
378 [SOUND_MIXER_SPEAKER
] = 7, /* Speaker */
379 [SOUND_MIXER_RECLEV
] = 8, /* Recording level */
380 [SOUND_MIXER_VOLUME
] = 9 /* Master Volume */
385 case SOUND_MIXER_CS_GETDBGMASK
:
386 CS_DBGOUT(CS_IOCTL
, 4, printk("SOUND_MIXER_CS_GETDBGMASK: ") );
388 case SOUND_MIXER_CS_GETDBGLEVEL
:
389 CS_DBGOUT(CS_IOCTL
, 4, printk("SOUND_MIXER_CS_GETDBGLEVEL: ") );
391 case SOUND_MIXER_CS_SETDBGMASK
:
392 CS_DBGOUT(CS_IOCTL
, 4, printk("SOUND_MIXER_CS_SETDBGMASK: ") );
394 case SOUND_MIXER_CS_SETDBGLEVEL
:
395 CS_DBGOUT(CS_IOCTL
, 4, printk("SOUND_MIXER_CS_SETDBGLEVEL: ") );
398 CS_DBGOUT(CS_IOCTL
, 4, printk("OSS_GETVERSION: ") );
400 case SNDCTL_DSP_SYNC
:
401 CS_DBGOUT(CS_IOCTL
, 4, printk("SNDCTL_DSP_SYNC: ") );
403 case SNDCTL_DSP_SETDUPLEX
:
404 CS_DBGOUT(CS_IOCTL
, 4, printk("SNDCTL_DSP_SETDUPLEX: ") );
406 case SNDCTL_DSP_GETCAPS
:
407 CS_DBGOUT(CS_IOCTL
, 4, printk("SNDCTL_DSP_GETCAPS: ") );
409 case SNDCTL_DSP_RESET
:
410 CS_DBGOUT(CS_IOCTL
, 4, printk("SNDCTL_DSP_RESET: ") );
412 case SNDCTL_DSP_SPEED
:
413 CS_DBGOUT(CS_IOCTL
, 4, printk("SNDCTL_DSP_SPEED: ") );
415 case SNDCTL_DSP_STEREO
:
416 CS_DBGOUT(CS_IOCTL
, 4, printk("SNDCTL_DSP_STEREO: ") );
418 case SNDCTL_DSP_CHANNELS
:
419 CS_DBGOUT(CS_IOCTL
, 4, printk("SNDCTL_DSP_CHANNELS: ") );
421 case SNDCTL_DSP_GETFMTS
:
422 CS_DBGOUT(CS_IOCTL
, 4, printk("SNDCTL_DSP_GETFMTS: ") );
424 case SNDCTL_DSP_SETFMT
:
425 CS_DBGOUT(CS_IOCTL
, 4, printk("SNDCTL_DSP_SETFMT: ") );
427 case SNDCTL_DSP_POST
:
428 CS_DBGOUT(CS_IOCTL
, 4, printk("SNDCTL_DSP_POST: ") );
430 case SNDCTL_DSP_GETTRIGGER
:
431 CS_DBGOUT(CS_IOCTL
, 4, printk("SNDCTL_DSP_GETTRIGGER: ") );
433 case SNDCTL_DSP_SETTRIGGER
:
434 CS_DBGOUT(CS_IOCTL
, 4, printk("SNDCTL_DSP_SETTRIGGER: ") );
436 case SNDCTL_DSP_GETOSPACE
:
437 CS_DBGOUT(CS_IOCTL
, 4, printk("SNDCTL_DSP_GETOSPACE: ") );
439 case SNDCTL_DSP_GETISPACE
:
440 CS_DBGOUT(CS_IOCTL
, 4, printk("SNDCTL_DSP_GETISPACE: ") );
442 case SNDCTL_DSP_NONBLOCK
:
443 CS_DBGOUT(CS_IOCTL
, 4, printk("SNDCTL_DSP_NONBLOCK: ") );
445 case SNDCTL_DSP_GETODELAY
:
446 CS_DBGOUT(CS_IOCTL
, 4, printk("SNDCTL_DSP_GETODELAY: ") );
448 case SNDCTL_DSP_GETIPTR
:
449 CS_DBGOUT(CS_IOCTL
, 4, printk("SNDCTL_DSP_GETIPTR: ") );
451 case SNDCTL_DSP_GETOPTR
:
452 CS_DBGOUT(CS_IOCTL
, 4, printk("SNDCTL_DSP_GETOPTR: ") );
454 case SNDCTL_DSP_GETBLKSIZE
:
455 CS_DBGOUT(CS_IOCTL
, 4, printk("SNDCTL_DSP_GETBLKSIZE: ") );
457 case SNDCTL_DSP_SETFRAGMENT
:
458 CS_DBGOUT(CS_IOCTL
, 4, printk("SNDCTL_DSP_SETFRAGMENT: ") );
460 case SNDCTL_DSP_SUBDIVIDE
:
461 CS_DBGOUT(CS_IOCTL
, 4, printk("SNDCTL_DSP_SUBDIVIDE: ") );
463 case SOUND_PCM_READ_RATE
:
464 CS_DBGOUT(CS_IOCTL
, 4, printk("SOUND_PCM_READ_RATE: ") );
466 case SOUND_PCM_READ_CHANNELS
:
467 CS_DBGOUT(CS_IOCTL
, 4, printk("SOUND_PCM_READ_CHANNELS: ") );
469 case SOUND_PCM_READ_BITS
:
470 CS_DBGOUT(CS_IOCTL
, 4, printk("SOUND_PCM_READ_BITS: ") );
472 case SOUND_PCM_WRITE_FILTER
:
473 CS_DBGOUT(CS_IOCTL
, 4, printk("SOUND_PCM_WRITE_FILTER: ") );
475 case SNDCTL_DSP_SETSYNCRO
:
476 CS_DBGOUT(CS_IOCTL
, 4, printk("SNDCTL_DSP_SETSYNCRO: ") );
478 case SOUND_PCM_READ_FILTER
:
479 CS_DBGOUT(CS_IOCTL
, 4, printk("SOUND_PCM_READ_FILTER: ") );
481 case SNDCTL_DSP_CS_GETDBGMASK
:
482 CS_DBGOUT(CS_IOCTL
, 4, printk("SNDCTL_DSP_CS_GETDBGMASK: ") );
484 case SNDCTL_DSP_CS_GETDBGLEVEL
:
485 CS_DBGOUT(CS_IOCTL
, 4, printk("SNDCTL_DSP_CS_GETDBGLEVEL: ") );
487 case SNDCTL_DSP_CS_SETDBGMASK
:
488 CS_DBGOUT(CS_IOCTL
, 4, printk("SNDCTL_DSP_CS_SETDBGMASK: ") );
490 case SNDCTL_DSP_CS_SETDBGLEVEL
:
491 CS_DBGOUT(CS_IOCTL
, 4, printk("SNDCTL_DSP_CS_SETDBGLEVEL: ") );
494 case SOUND_MIXER_PRIVATE1
:
495 CS_DBGOUT(CS_IOCTL
, 4, printk("SOUND_MIXER_PRIVATE1: ") );
497 case SOUND_MIXER_PRIVATE2
:
498 CS_DBGOUT(CS_IOCTL
, 4, printk("SOUND_MIXER_PRIVATE2: ") );
500 case SOUND_MIXER_PRIVATE3
:
501 CS_DBGOUT(CS_IOCTL
, 4, printk("SOUND_MIXER_PRIVATE3: ") );
503 case SOUND_MIXER_PRIVATE4
:
504 CS_DBGOUT(CS_IOCTL
, 4, printk("SOUND_MIXER_PRIVATE4: ") );
506 case SOUND_MIXER_PRIVATE5
:
507 CS_DBGOUT(CS_IOCTL
, 4, printk("SOUND_MIXER_PRIVATE5: ") );
509 case SOUND_MIXER_INFO
:
510 CS_DBGOUT(CS_IOCTL
, 4, printk("SOUND_MIXER_INFO: ") );
512 case SOUND_OLD_MIXER_INFO
:
513 CS_DBGOUT(CS_IOCTL
, 4, printk("SOUND_OLD_MIXER_INFO: ") );
519 case SOUND_MIXER_VOLUME
:
520 CS_DBGOUT(CS_IOCTL
, 4, printk("SOUND_MIXER_VOLUME: ") );
522 case SOUND_MIXER_SPEAKER
:
523 CS_DBGOUT(CS_IOCTL
, 4, printk("SOUND_MIXER_SPEAKER: ") );
525 case SOUND_MIXER_RECLEV
:
526 CS_DBGOUT(CS_IOCTL
, 4, printk("SOUND_MIXER_RECLEV: ") );
528 case SOUND_MIXER_MIC
:
529 CS_DBGOUT(CS_IOCTL
, 4, printk("SOUND_MIXER_MIC: ") );
531 case SOUND_MIXER_SYNTH
:
532 CS_DBGOUT(CS_IOCTL
, 4, printk("SOUND_MIXER_SYNTH: ") );
534 case SOUND_MIXER_RECSRC
:
535 CS_DBGOUT(CS_IOCTL
, 4, printk("SOUND_MIXER_RECSRC: ") );
537 case SOUND_MIXER_DEVMASK
:
538 CS_DBGOUT(CS_IOCTL
, 4, printk("SOUND_MIXER_DEVMASK: ") );
540 case SOUND_MIXER_RECMASK
:
541 CS_DBGOUT(CS_IOCTL
, 4, printk("SOUND_MIXER_RECMASK: ") );
543 case SOUND_MIXER_STEREODEVS
:
544 CS_DBGOUT(CS_IOCTL
, 4, printk("SOUND_MIXER_STEREODEVS: ") );
546 case SOUND_MIXER_CAPS
:
547 CS_DBGOUT(CS_IOCTL
, 4, printk("SOUND_MIXER_CAPS:") );
551 if (i
>= SOUND_MIXER_NRDEVICES
|| !(vidx
= mixtable1
[i
]))
553 CS_DBGOUT(CS_IOCTL
, 4, printk("UNKNOWN IOCTL: 0x%.8x NR=%d ",x
,i
) );
557 CS_DBGOUT(CS_IOCTL
, 4, printk("SOUND_MIXER_IOCTL AC9x: 0x%.8x NR=%d ",
563 CS_DBGOUT(CS_IOCTL
, 4, printk("command = 0x%x IOC_NR=%d\n",x
, _IOC_NR(x
)) );
568 * common I/O routines
571 static void cs461x_poke(struct cs_card
*codec
, unsigned long reg
, unsigned int val
)
573 writel(val
, codec
->ba1
.idx
[(reg
>> 16) & 3]+(reg
&0xffff));
576 static unsigned int cs461x_peek(struct cs_card
*codec
, unsigned long reg
)
578 return readl(codec
->ba1
.idx
[(reg
>> 16) & 3]+(reg
&0xffff));
581 static void cs461x_pokeBA0(struct cs_card
*codec
, unsigned long reg
, unsigned int val
)
583 writel(val
, codec
->ba0
+reg
);
586 static unsigned int cs461x_peekBA0(struct cs_card
*codec
, unsigned long reg
)
588 return readl(codec
->ba0
+reg
);
592 static u16
cs_ac97_get(struct ac97_codec
*dev
, u8 reg
);
593 static void cs_ac97_set(struct ac97_codec
*dev
, u8 reg
, u16 data
);
595 static struct cs_channel
*cs_alloc_pcm_channel(struct cs_card
*card
)
597 if(card
->channel
[1].used
==1)
599 card
->channel
[1].used
=1;
600 card
->channel
[1].num
=1;
601 return &card
->channel
[1];
604 static struct cs_channel
*cs_alloc_rec_pcm_channel(struct cs_card
*card
)
606 if(card
->channel
[0].used
==1)
608 card
->channel
[0].used
=1;
609 card
->channel
[0].num
=0;
610 return &card
->channel
[0];
613 static void cs_free_pcm_channel(struct cs_card
*card
, int channel
)
615 card
->channel
[channel
].state
= NULL
;
616 card
->channel
[channel
].used
=0;
620 * setup a divisor value to help with conversion from
621 * 16bit Stereo, down to 8bit stereo/mono or 16bit mono.
622 * assign a divisor of 1 if using 16bit Stereo as that is
623 * the only format that the static image will capture.
625 static void cs_set_divisor(struct dmabuf
*dmabuf
)
627 if(dmabuf
->type
== CS_TYPE_DAC
)
629 else if( !(dmabuf
->fmt
& CS_FMT_STEREO
) &&
630 (dmabuf
->fmt
& CS_FMT_16BIT
))
632 else if( (dmabuf
->fmt
& CS_FMT_STEREO
) &&
633 !(dmabuf
->fmt
& CS_FMT_16BIT
))
635 else if( !(dmabuf
->fmt
& CS_FMT_STEREO
) &&
636 !(dmabuf
->fmt
& CS_FMT_16BIT
))
641 CS_DBGOUT(CS_PARMS
| CS_FUNCTION
, 8, printk(
642 "cs46xx: cs_set_divisor()- %s %d\n",
643 (dmabuf
->type
== CS_TYPE_ADC
) ? "ADC" : "DAC",
647 /* set playback sample rate */
648 static unsigned int cs_set_dac_rate(struct cs_state
* state
, unsigned int rate
)
650 struct dmabuf
*dmabuf
= &state
->dmabuf
;
651 unsigned int tmp1
, tmp2
;
652 unsigned int phiIncr
;
653 unsigned int correctionPerGOF
, correctionPerSec
;
654 CS_DBGOUT(CS_FUNCTION
, 2, printk("cs46xx: cs_set_dac_rate()+ %d\n",rate
) );
657 * Compute the values used to drive the actual sample rate conversion.
658 * The following formulas are being computed, using inline assembly
659 * since we need to use 64 bit arithmetic to compute the values:
661 * phiIncr = floor((Fs,in * 2^26) / Fs,out)
662 * correctionPerGOF = floor((Fs,in * 2^26 - Fs,out * phiIncr) /
664 * ulCorrectionPerSec = Fs,in * 2^26 - Fs,out * phiIncr -M
665 * GOF_PER_SEC * correctionPerGOF
669 * phiIncr:other = dividend:remainder((Fs,in * 2^26) / Fs,out)
670 * correctionPerGOF:correctionPerSec =
671 * dividend:remainder(ulOther / GOF_PER_SEC)
674 phiIncr
= tmp1
/ 48000;
675 tmp1
-= phiIncr
* 48000;
680 tmp1
-= tmp2
* 48000;
681 correctionPerGOF
= tmp1
/ GOF_PER_SEC
;
682 tmp1
-= correctionPerGOF
* GOF_PER_SEC
;
683 correctionPerSec
= tmp1
;
686 * Fill in the SampleRateConverter control block.
689 spin_lock_irq(&state
->card
->lock
);
690 cs461x_poke(state
->card
, BA1_PSRC
,
691 ((correctionPerSec
<< 16) & 0xFFFF0000) | (correctionPerGOF
& 0xFFFF));
692 cs461x_poke(state
->card
, BA1_PPI
, phiIncr
);
693 spin_unlock_irq(&state
->card
->lock
);
696 CS_DBGOUT(CS_FUNCTION
, 2, printk("cs46xx: cs_set_dac_rate()- %d\n",rate
) );
700 /* set recording sample rate */
701 static unsigned int cs_set_adc_rate(struct cs_state
* state
, unsigned int rate
)
703 struct dmabuf
*dmabuf
= &state
->dmabuf
;
704 struct cs_card
*card
= state
->card
;
705 unsigned int phiIncr
, coeffIncr
, tmp1
, tmp2
;
706 unsigned int correctionPerGOF
, correctionPerSec
, initialDelay
;
707 unsigned int frameGroupLength
, cnt
;
708 CS_DBGOUT(CS_FUNCTION
, 2, printk("cs46xx: cs_set_adc_rate()+ %d\n",rate
) );
711 * We can only decimate by up to a factor of 1/9th the hardware rate.
712 * Correct the value if an attempt is made to stray outside that limit.
714 if ((rate
* 9) < 48000)
718 * We can not capture at at rate greater than the Input Rate (48000).
719 * Return an error if an attempt is made to stray outside that limit.
725 * Compute the values used to drive the actual sample rate conversion.
726 * The following formulas are being computed, using inline assembly
727 * since we need to use 64 bit arithmetic to compute the values:
729 * coeffIncr = -floor((Fs,out * 2^23) / Fs,in)
730 * phiIncr = floor((Fs,in * 2^26) / Fs,out)
731 * correctionPerGOF = floor((Fs,in * 2^26 - Fs,out * phiIncr) /
733 * correctionPerSec = Fs,in * 2^26 - Fs,out * phiIncr -
734 * GOF_PER_SEC * correctionPerGOF
735 * initialDelay = ceil((24 * Fs,in) / Fs,out)
739 * coeffIncr = neg(dividend((Fs,out * 2^23) / Fs,in))
740 * phiIncr:ulOther = dividend:remainder((Fs,in * 2^26) / Fs,out)
741 * correctionPerGOF:correctionPerSec =
742 * dividend:remainder(ulOther / GOF_PER_SEC)
743 * initialDelay = dividend(((24 * Fs,in) + Fs,out - 1) / Fs,out)
747 coeffIncr
= tmp1
/ 48000;
748 tmp1
-= coeffIncr
* 48000;
751 coeffIncr
+= tmp1
/ 48000;
752 coeffIncr
^= 0xFFFFFFFF;
755 phiIncr
= tmp1
/ rate
;
756 tmp1
-= phiIncr
* rate
;
762 correctionPerGOF
= tmp1
/ GOF_PER_SEC
;
763 tmp1
-= correctionPerGOF
* GOF_PER_SEC
;
764 correctionPerSec
= tmp1
;
765 initialDelay
= ((48000 * 24) + rate
- 1) / rate
;
768 * Fill in the VariDecimate control block.
770 spin_lock_irq(&card
->lock
);
771 cs461x_poke(card
, BA1_CSRC
,
772 ((correctionPerSec
<< 16) & 0xFFFF0000) | (correctionPerGOF
& 0xFFFF));
773 cs461x_poke(card
, BA1_CCI
, coeffIncr
);
774 cs461x_poke(card
, BA1_CD
,
775 (((BA1_VARIDEC_BUF_1
+ (initialDelay
<< 2)) << 16) & 0xFFFF0000) | 0x80);
776 cs461x_poke(card
, BA1_CPI
, phiIncr
);
777 spin_unlock_irq(&card
->lock
);
780 * Figure out the frame group length for the write back task. Basically,
781 * this is just the factors of 24000 (2^6*3*5^3) that are not present in
782 * the output sample rate.
784 frameGroupLength
= 1;
785 for (cnt
= 2; cnt
<= 64; cnt
*= 2) {
786 if (((rate
/ cnt
) * cnt
) != rate
)
787 frameGroupLength
*= 2;
789 if (((rate
/ 3) * 3) != rate
) {
790 frameGroupLength
*= 3;
792 for (cnt
= 5; cnt
<= 125; cnt
*= 5) {
793 if (((rate
/ cnt
) * cnt
) != rate
)
794 frameGroupLength
*= 5;
798 * Fill in the WriteBack control block.
800 spin_lock_irq(&card
->lock
);
801 cs461x_poke(card
, BA1_CFG1
, frameGroupLength
);
802 cs461x_poke(card
, BA1_CFG2
, (0x00800000 | frameGroupLength
));
803 cs461x_poke(card
, BA1_CCST
, 0x0000FFFF);
804 cs461x_poke(card
, BA1_CSPB
, ((65536 * rate
) / 24000));
805 cs461x_poke(card
, (BA1_CSPB
+ 4), 0x0000FFFF);
806 spin_unlock_irq(&card
->lock
);
808 CS_DBGOUT(CS_FUNCTION
, 2, printk("cs46xx: cs_set_adc_rate()- %d\n",rate
) );
812 /* prepare channel attributes for playback */
813 static void cs_play_setup(struct cs_state
*state
)
815 struct dmabuf
*dmabuf
= &state
->dmabuf
;
816 struct cs_card
*card
= state
->card
;
817 unsigned int tmp
, Count
, playFormat
;
819 CS_DBGOUT(CS_FUNCTION
, 2, printk("cs46xx: cs_play_setup()+\n") );
820 cs461x_poke(card
, BA1_PVOL
, 0x80008000);
822 cs461x_poke(card
, BA1_PBA
, virt_to_bus(dmabuf
->pbuf
));
825 playFormat
=cs461x_peek(card
, BA1_PFIE
);
826 if ((dmabuf
->fmt
& CS_FMT_STEREO
)) {
827 playFormat
&= ~DMA_RQ_C2_AC_MONO_TO_STEREO
;
831 playFormat
|= DMA_RQ_C2_AC_MONO_TO_STEREO
;
833 if ((dmabuf
->fmt
& CS_FMT_16BIT
)) {
834 playFormat
&= ~(DMA_RQ_C2_AC_8_TO_16_BIT
835 | DMA_RQ_C2_AC_SIGNED_CONVERT
);
839 playFormat
|= (DMA_RQ_C2_AC_8_TO_16_BIT
840 | DMA_RQ_C2_AC_SIGNED_CONVERT
);
842 cs461x_poke(card
, BA1_PFIE
, playFormat
);
844 tmp
= cs461x_peek(card
, BA1_PDTC
);
846 cs461x_poke(card
, BA1_PDTC
, tmp
| --Count
);
848 CS_DBGOUT(CS_FUNCTION
, 2, printk("cs46xx: cs_play_setup()-\n") );
856 } InitArray
[] = { {0x00000040, 0x3fc0000f},
857 {0x0000004c, 0x04800000},
859 {0x000000b3, 0x00000780},
860 {0x000000b7, 0x00000000},
861 {0x000000bc, 0x07800000},
863 {0x000000cd, 0x00800000},
867 * "SetCaptureSPValues()" -- Initialize record task values before each
870 void SetCaptureSPValues(struct cs_card
*card
)
873 CS_DBGOUT(CS_FUNCTION
, 8, printk("cs46xx: SetCaptureSPValues()+\n") );
874 for(i
=0; i
<sizeof(InitArray
)/sizeof(struct InitStruct
); i
++)
876 offset
= InitArray
[i
].off
*4; /* 8bit to 32bit offset value */
877 cs461x_poke(card
, offset
, InitArray
[i
].val
);
879 CS_DBGOUT(CS_FUNCTION
, 8, printk("cs46xx: SetCaptureSPValues()-\n") );
882 /* prepare channel attributes for recording */
883 static void cs_rec_setup(struct cs_state
*state
)
885 struct cs_card
*card
= state
->card
;
886 struct dmabuf
*dmabuf
= &state
->dmabuf
;
887 CS_DBGOUT(CS_FUNCTION
, 2, printk("cs46xx: cs_rec_setup()+\n") );
889 SetCaptureSPValues(card
);
892 * set the attenuation to 0dB
894 cs461x_poke(card
, BA1_CVOL
, 0x80008000);
897 * set the physical address of the capture buffer into the SP
899 cs461x_poke(card
, BA1_CBA
, virt_to_bus(dmabuf
->rawbuf
));
901 CS_DBGOUT(CS_FUNCTION
, 2, printk("cs46xx: cs_rec_setup()-\n") );
905 /* get current playback/recording dma buffer pointer (byte offset from LBA),
906 called with spinlock held! */
908 extern __inline__
unsigned cs_get_dma_addr(struct cs_state
*state
)
910 struct dmabuf
*dmabuf
= &state
->dmabuf
;
913 if ( (!(dmabuf
->enable
& DAC_RUNNING
)) &&
914 (!(dmabuf
->enable
& ADC_RUNNING
) ) )
916 CS_DBGOUT(CS_ERROR
, 2, printk(
917 "cs46xx: ERROR cs_get_dma_addr(): not enabled \n") );
922 * ganularity is byte boundry, good part.
924 if(dmabuf
->enable
& DAC_RUNNING
)
926 offset
= cs461x_peek(state
->card
, BA1_PBA
);
928 else /* ADC_RUNNING must be set */
930 offset
= cs461x_peek(state
->card
, BA1_CBA
);
932 CS_DBGOUT(CS_PARMS
| CS_FUNCTION
, 9,
933 printk("cs46xx: cs_get_dma_addr() %d\n",offset
) );
934 offset
= (u32
)bus_to_virt((unsigned long)offset
) - (u32
)dmabuf
->rawbuf
;
935 CS_DBGOUT(CS_PARMS
| CS_FUNCTION
, 8,
936 printk("cs46xx: cs_get_dma_addr()- %d\n",offset
) );
940 static void resync_dma_ptrs(struct cs_state
*state
)
942 struct dmabuf
*dmabuf
= &state
->dmabuf
;
945 CS_DBGOUT(CS_FUNCTION
, 2, printk("cs46xx: resync_dma_ptrs()+ \n") );
947 dmabuf
->hwptr
=dmabuf
->swptr
= 0;
948 dmabuf
->pringbuf
= 0;
949 CS_DBGOUT(CS_FUNCTION
, 2, printk("cs46xx: resync_dma_ptrs()- \n") );
952 /* Stop recording (lock held) */
953 extern __inline__
void __stop_adc(struct cs_state
*state
)
955 struct dmabuf
*dmabuf
= &state
->dmabuf
;
956 struct cs_card
*card
= state
->card
;
959 dmabuf
->enable
&= ~ADC_RUNNING
;
961 tmp
= cs461x_peek(card
, BA1_CCTL
);
963 cs461x_poke(card
, BA1_CCTL
, tmp
);
966 static void stop_adc(struct cs_state
*state
)
970 CS_DBGOUT(CS_FUNCTION
, 2, printk("cs46xx: stop_adc()+ \n") );
971 spin_lock_irqsave(&state
->card
->lock
, flags
);
973 spin_unlock_irqrestore(&state
->card
->lock
, flags
);
974 CS_DBGOUT(CS_FUNCTION
, 2, printk("cs46xx: stop_adc()- \n") );
977 static void start_adc(struct cs_state
*state
)
979 struct dmabuf
*dmabuf
= &state
->dmabuf
;
980 struct cs_card
*card
= state
->card
;
984 spin_lock_irqsave(&card
->lock
, flags
);
985 if (!(dmabuf
->enable
& ADC_RUNNING
) &&
986 ((dmabuf
->mapped
|| dmabuf
->count
< (signed)dmabuf
->dmasize
)
989 dmabuf
->enable
|= ADC_RUNNING
;
990 cs_set_divisor(dmabuf
);
991 tmp
= cs461x_peek(card
, BA1_CCTL
);
994 CS_DBGOUT(CS_FUNCTION
, 2, printk(
995 "cs46xx: start_adc() poke 0x%x \n",tmp
) );
996 cs461x_poke(card
, BA1_CCTL
, tmp
);
998 spin_unlock_irqrestore(&card
->lock
, flags
);
1001 /* stop playback (lock held) */
1002 extern __inline__
void __stop_dac(struct cs_state
*state
)
1004 struct dmabuf
*dmabuf
= &state
->dmabuf
;
1005 struct cs_card
*card
= state
->card
;
1008 dmabuf
->enable
&= ~DAC_RUNNING
;
1010 tmp
=cs461x_peek(card
, BA1_PCTL
);
1012 cs461x_poke(card
, BA1_PCTL
, tmp
);
1015 static void stop_dac(struct cs_state
*state
)
1017 unsigned long flags
;
1019 CS_DBGOUT(CS_FUNCTION
, 2, printk("cs46xx: stop_dac()+ \n") );
1020 spin_lock_irqsave(&state
->card
->lock
, flags
);
1022 spin_unlock_irqrestore(&state
->card
->lock
, flags
);
1023 CS_DBGOUT(CS_FUNCTION
, 2, printk("cs46xx: stop_dac()- \n") );
1026 static void start_dac(struct cs_state
*state
)
1028 struct dmabuf
*dmabuf
= &state
->dmabuf
;
1029 struct cs_card
*card
= state
->card
;
1030 unsigned long flags
;
1033 CS_DBGOUT(CS_FUNCTION
, 2, printk("cs46xx: start_dac()+ \n") );
1034 spin_lock_irqsave(&card
->lock
, flags
);
1035 if (!(dmabuf
->enable
& DAC_RUNNING
) &&
1036 ((dmabuf
->mapped
|| dmabuf
->count
> 0) && dmabuf
->ready
)) {
1037 dmabuf
->enable
|= DAC_RUNNING
;
1038 tmp
= cs461x_peek(card
, BA1_PCTL
);
1041 CS_DBGOUT(CS_PARMS
, 6, printk(
1042 "cs46xx: start_dac() poke card=0x%.08x tmp=0x%.08x addr=0x%.08x \n",
1043 (unsigned)card
, (unsigned)tmp
,
1044 (unsigned)card
->ba1
.idx
[(BA1_PCTL
>> 16) & 3]+(BA1_PCTL
&0xffff) ) );
1045 cs461x_poke(card
, BA1_PCTL
, tmp
);
1047 spin_unlock_irqrestore(&card
->lock
, flags
);
1048 CS_DBGOUT(CS_FUNCTION
, 2, printk("cs46xx: start_dac()- \n") );
1051 #define DMABUF_MINORDER 1
1054 * allocate DMA buffer, playback and recording buffers are separate.
1056 static int alloc_dmabuf(struct cs_state
*state
)
1058 struct dmabuf
*dmabuf
= &state
->dmabuf
;
1059 void *rawbuf
= NULL
;
1060 void *tmpbuff
= NULL
;
1062 struct page
*page
, *pend
;
1064 /* alloc as big a chunk as we can */
1065 for (order
= defaultorder
; order
>= DMABUF_MINORDER
; order
--)
1066 if((rawbuf
= (void *)__get_free_pages(GFP_KERNEL
| GFP_DMA
, order
)))
1072 dmabuf
->buforder
= order
;
1073 dmabuf
->rawbuf
= rawbuf
;
1075 /* now mark the pages as reserved; otherwise remap_page_range doesn't do what we want */
1076 pend
= virt_to_page(rawbuf
+ (PAGE_SIZE
<< order
) - 1);
1077 for (page
= virt_to_page(rawbuf
); page
<= pend
; page
++)
1078 mem_map_reserve(page
);
1080 CS_DBGOUT(CS_PARMS
, 9, printk("cs461x: allocated %ld (order = %d) bytes at %p\n",
1081 PAGE_SIZE
<< order
, order
, rawbuf
) );
1083 * now the temp buffer for 16/8 conversions
1085 for (order
= defaultorder
; order
>= DMABUF_MINORDER
; order
--)
1086 if((tmpbuff
= (void *)__get_free_pages(GFP_KERNEL
| GFP_DMA
, order
)))
1090 CS_DBGOUT(CS_PARMS
, 9, printk("cs461x: allocated %ld (order = %d) bytes at %p\n",
1091 PAGE_SIZE
<< order
, order
, tmpbuff
) );
1093 dmabuf
->tmpbuff
= tmpbuff
;
1094 dmabuf
->buforder_tmpbuff
= order
;
1096 /* now mark the pages as reserved; otherwise remap_page_range doesn't do what we want */
1097 pend
= virt_to_page(tmpbuff
+ (PAGE_SIZE
<< order
) - 1);
1098 for (page
= virt_to_page(tmpbuff
); page
<= pend
; page
++)
1099 mem_map_reserve(page
);
1101 CS_DBGOUT(CS_PARMS
, 9, printk("cs461x: allocated %ld (order = %d) bytes at %p\n",
1102 PAGE_SIZE
<< order
, order
, tmpbuff
) );
1104 dmabuf
->ready
= dmabuf
->mapped
= 0;
1109 /* free DMA buffer */
1110 static void dealloc_dmabuf(struct cs_state
*state
)
1112 struct dmabuf
*dmabuf
= &state
->dmabuf
;
1113 struct page
*page
, *pend
;
1115 if (dmabuf
->rawbuf
) {
1116 pend
= virt_to_page(dmabuf
->rawbuf
+ (PAGE_SIZE
<< dmabuf
->buforder
) - 1);
1117 for (page
= virt_to_page(dmabuf
->rawbuf
); page
<= pend
; page
++)
1118 mem_map_unreserve(page
);
1119 pci_free_consistent(state
->card
->pci_dev
, PAGE_SIZE
<< dmabuf
->buforder
,
1120 dmabuf
->rawbuf
, dmabuf
->dma_handle
);
1122 dmabuf
->rawbuf
= NULL
;
1124 if (dmabuf
->tmpbuff
) {
1125 /* undo marking the pages as reserved */
1126 pend
= virt_to_page(dmabuf
->tmpbuff
+ (PAGE_SIZE
<< dmabuf
->buforder_tmpbuff
) - 1);
1127 for (page
= virt_to_page(dmabuf
->tmpbuff
); page
<= pend
; page
++)
1128 mem_map_unreserve(page
);
1129 pci_free_consistent(state
->card
->pci_dev
, PAGE_SIZE
<< dmabuf
->buforder_tmpbuff
,
1130 dmabuf
->tmpbuff
, dmabuf
->dma_handle_tmpbuff
);
1133 dmabuf
->rawbuf
= NULL
;
1134 dmabuf
->tmpbuff
= NULL
;
1135 dmabuf
->mapped
= dmabuf
->ready
= 0;
1139 static int prog_dmabuf(struct cs_state
*state
)
1141 struct dmabuf
*dmabuf
= &state
->dmabuf
;
1142 unsigned long flags
;
1143 unsigned long allocated_pages
, allocated_bytes
;
1144 unsigned long tmp1
, tmp2
, fmt
=0;
1145 unsigned long *ptmp
= (unsigned long *) dmabuf
->pbuf
;
1146 unsigned long SGarray
[9], nSGpages
=0;
1149 CS_DBGOUT(CS_FUNCTION
, 4, printk("cs46xx: prog_dmabuf()+ \n"));
1151 * check for CAPTURE and use only non-sg for initial release
1153 if(dmabuf
->type
== CS_TYPE_ADC
)
1155 CS_DBGOUT(CS_FUNCTION
, 4, printk("cs46xx: prog_dmabuf() ADC\n"));
1157 * add in non-sg support for capture.
1159 spin_lock_irqsave(&state
->card
->lock
, flags
);
1160 /* add code to reset the rawbuf memory. TRW */
1161 resync_dma_ptrs(state
);
1162 dmabuf
->total_bytes
= dmabuf
->blocks
= 0;
1163 dmabuf
->count
= dmabuf
->error
= dmabuf
->underrun
= 0;
1167 spin_unlock_irqrestore(&state
->card
->lock
, flags
);
1169 /* allocate DMA buffer if not allocated yet */
1170 if (!dmabuf
->rawbuf
|| !dmabuf
->tmpbuff
)
1171 if ((ret
= alloc_dmabuf(state
)))
1174 * static image only supports 16Bit signed, stereo - hard code fmt
1176 fmt
= CS_FMT_16BIT
| CS_FMT_STEREO
;
1178 dmabuf
->numfrag
= 2;
1179 dmabuf
->fragsize
= 2048;
1180 dmabuf
->fragsamples
= 2048 >> sample_shift
[fmt
];
1181 dmabuf
->dmasize
= 4096;
1182 dmabuf
->fragshift
= 11;
1184 memset(dmabuf
->rawbuf
, (fmt
& CS_FMT_16BIT
) ? 0 : 0x80,
1186 memset(dmabuf
->tmpbuff
, (fmt
& CS_FMT_16BIT
) ? 0 : 0x80,
1187 PAGE_SIZE
<<dmabuf
->buforder_tmpbuff
);
1190 * Now set up the ring
1193 spin_lock_irqsave(&state
->card
->lock
, flags
);
1194 cs_rec_setup(state
);
1195 spin_unlock_irqrestore(&state
->card
->lock
, flags
);
1197 /* set the ready flag for the dma buffer */
1200 CS_DBGOUT(CS_PARMS
, 4, printk(
1201 "cs461x: prog_dmabuf(): CAPTURE rate=%d fmt=0x%x numfrag=%d "
1202 "fragsize=%d dmasize=%d\n",
1203 dmabuf
->rate
, dmabuf
->fmt
, dmabuf
->numfrag
,
1204 dmabuf
->fragsize
, dmabuf
->dmasize
) );
1206 CS_DBGOUT(CS_FUNCTION
, 4, printk("cs46xx: prog_dmabuf()- 0 \n"));
1209 else if (dmabuf
->type
== CS_TYPE_DAC
)
1214 CS_DBGOUT(CS_FUNCTION
, 4, printk("cs46xx: prog_dmabuf() DAC\n"));
1215 spin_lock_irqsave(&state
->card
->lock
, flags
);
1216 resync_dma_ptrs(state
);
1217 dmabuf
->total_bytes
= dmabuf
->blocks
= 0;
1218 dmabuf
->count
= dmabuf
->error
= dmabuf
->underrun
= 0;
1222 spin_unlock_irqrestore(&state
->card
->lock
, flags
);
1224 /* allocate DMA buffer if not allocated yet */
1225 if (!dmabuf
->rawbuf
)
1226 if ((ret
= alloc_dmabuf(state
)))
1229 allocated_pages
= 1 << dmabuf
->buforder
;
1230 allocated_bytes
= allocated_pages
*PAGE_SIZE
;
1232 if(allocated_pages
< 2)
1234 CS_DBGOUT(CS_FUNCTION
, 4, printk(
1235 "cs46xx: prog_dmabuf() Error: allocated_pages too small (%d)\n",
1236 (unsigned)allocated_pages
));
1240 /* Use all the pages allocated, fragsize 4k. */
1241 /* Use 'pbuf' for S/G page map table. */
1242 dmabuf
->SGok
= 1; /* Use S/G. */
1244 nSGpages
= allocated_bytes
/4096; /* S/G pages always 4k. */
1246 /* Set up S/G variables. */
1247 *ptmp
= virt_to_bus(dmabuf
->rawbuf
);
1248 *(ptmp
+1) = 0x00000008;
1249 for(tmp1
= 1; tmp1
< nSGpages
; tmp1
++) {
1250 *(ptmp
+2*tmp1
) = virt_to_bus( (dmabuf
->rawbuf
)+4096*tmp1
);
1251 if( tmp1
== nSGpages
-1)
1254 tmp2
= 0x80000000+8*(tmp1
+1);
1255 *(ptmp
+2*tmp1
+1) = tmp2
;
1257 SGarray
[0] = 0x82c0200d;
1258 SGarray
[1] = 0xffff0000;
1260 SGarray
[3] = 0x00010600;
1261 SGarray
[4] = *(ptmp
+2);
1262 SGarray
[5] = 0x80000010;
1264 SGarray
[7] = *(ptmp
+2);
1265 SGarray
[8] = (virt_to_bus(dmabuf
->pbuf
) & 0xffff000) | 0x10;
1268 dmabuf
->numfrag
= nSGpages
;
1269 dmabuf
->fragsize
= 4096;
1270 dmabuf
->fragsamples
= 4096 >> sample_shift
[dmabuf
->fmt
];
1271 dmabuf
->fragshift
= 12;
1272 dmabuf
->dmasize
= dmabuf
->numfrag
*4096;
1275 SGarray
[0] = 0xf2c0000f;
1276 SGarray
[1] = 0x00000200;
1278 SGarray
[3] = 0x00010600;
1279 SGarray
[4]=SGarray
[5]=SGarray
[6]=SGarray
[7]=SGarray
[8] = 0;
1280 dmabuf
->numfrag
= 2;
1281 dmabuf
->fragsize
= 2048;
1282 dmabuf
->fragsamples
= 2048 >> sample_shift
[dmabuf
->fmt
];
1283 dmabuf
->dmasize
= 4096;
1284 dmabuf
->fragshift
= 11;
1286 for(tmp1
= 0; tmp1
< sizeof(SGarray
)/4; tmp1
++)
1287 cs461x_poke( state
->card
, BA1_PDTC
+tmp1
*4, SGarray
[tmp1
]);
1289 memset(dmabuf
->rawbuf
, (dmabuf
->fmt
& CS_FMT_16BIT
) ? 0 : 0x80,
1293 * Now set up the ring
1296 spin_lock_irqsave(&state
->card
->lock
, flags
);
1297 cs_play_setup(state
);
1298 spin_unlock_irqrestore(&state
->card
->lock
, flags
);
1300 /* set the ready flag for the dma buffer */
1303 CS_DBGOUT(CS_PARMS
, 4, printk(
1304 "cs461x: prog_dmabuf(): PLAYBACK rate=%d fmt=0x%x numfrag=%d "
1305 "fragsize=%d dmasize=%d\n",
1306 dmabuf
->rate
, dmabuf
->fmt
, dmabuf
->numfrag
,
1307 dmabuf
->fragsize
, dmabuf
->dmasize
) );
1309 CS_DBGOUT(CS_FUNCTION
, 4, printk("cs46xx: prog_dmabuf()- \n"));
1314 CS_DBGOUT(CS_FUNCTION
, 4, printk("cs46xx: prog_dmabuf()- Invalid Type %d\n",
1320 static void cs_clear_tail(struct cs_state
*state
)
1324 static int drain_dac(struct cs_state
*state
, int nonblock
)
1326 DECLARE_WAITQUEUE(wait
, current
);
1327 struct dmabuf
*dmabuf
= &state
->dmabuf
;
1328 unsigned long flags
;
1332 if (dmabuf
->mapped
|| !dmabuf
->ready
)
1335 add_wait_queue(&dmabuf
->wait
, &wait
);
1337 /* It seems that we have to set the current state to TASK_INTERRUPTIBLE
1338 every time to make the process really go to sleep */
1339 current
->state
= TASK_INTERRUPTIBLE
;
1341 spin_lock_irqsave(&state
->card
->lock
, flags
);
1342 count
= dmabuf
->count
;
1343 spin_unlock_irqrestore(&state
->card
->lock
, flags
);
1348 if (signal_pending(current
))
1352 remove_wait_queue(&dmabuf
->wait
, &wait
);
1353 current
->state
= TASK_RUNNING
;
1357 tmo
= (dmabuf
->dmasize
* HZ
) / dmabuf
->rate
;
1358 tmo
>>= sample_shift
[dmabuf
->fmt
];
1359 tmo
+= (2048*HZ
)/dmabuf
->rate
;
1361 if (!schedule_timeout(tmo
? tmo
: 1) && tmo
){
1362 printk(KERN_ERR
"cs461x: drain_dac, dma timeout? %d\n", count
);
1366 remove_wait_queue(&dmabuf
->wait
, &wait
);
1367 current
->state
= TASK_RUNNING
;
1368 if (signal_pending(current
))
1369 return -ERESTARTSYS
;
1375 /* update buffer manangement pointers, especially, dmabuf->count and dmabuf->hwptr */
1376 static void cs_update_ptr(void)
1378 struct cs_card
*card
=devs
;
1379 struct cs_state
*state
;
1380 struct dmabuf
*dmabuf
;
1384 /* error handling and process wake up for ADC */
1385 state
= card
->states
[0];
1388 dmabuf
= &state
->dmabuf
;
1389 if (dmabuf
->enable
& ADC_RUNNING
) {
1390 /* update hardware pointer */
1391 hwptr
= cs_get_dma_addr(state
);
1393 diff
= (dmabuf
->dmasize
+ hwptr
- dmabuf
->hwptr
) % dmabuf
->dmasize
;
1394 CS_DBGOUT(CS_PARMS
, 9, printk(
1395 "cs46xx: cs_update_ptr()+ ADC hwptr=%d diff=%d\n",
1397 dmabuf
->hwptr
= hwptr
;
1398 dmabuf
->total_bytes
+= diff
;
1399 dmabuf
->count
+= diff
;
1400 if (dmabuf
->count
> dmabuf
->dmasize
)
1401 dmabuf
->count
= dmabuf
->dmasize
;
1405 if (dmabuf
->count
>= (signed)dmabuf
->fragsize
)
1406 wake_up(&dmabuf
->wait
);
1409 if (dmabuf
->count
> 0)
1410 wake_up(&dmabuf
->wait
);
1418 state
= card
->states
[1];
1421 dmabuf
= &state
->dmabuf
;
1422 /* error handling and process wake up for DAC */
1423 if (dmabuf
->enable
& DAC_RUNNING
) {
1424 /* update hardware pointer */
1425 hwptr
= cs_get_dma_addr(state
);
1427 diff
= (dmabuf
->dmasize
+ hwptr
- dmabuf
->hwptr
) % dmabuf
->dmasize
;
1428 CS_DBGOUT(CS_PARMS
, 9, printk(
1429 "cs46xx: cs_update_ptr()+ DAC hwptr=%d diff=%d\n",
1431 dmabuf
->hwptr
= hwptr
;
1432 dmabuf
->total_bytes
+= diff
;
1433 if (dmabuf
->mapped
) {
1434 dmabuf
->count
+= diff
;
1435 if (dmabuf
->count
>= (signed)dmabuf
->fragsize
)
1436 wake_up(&dmabuf
->wait
);
1438 * other drivers use fragsize, but don't see any sense
1439 * in that, since dmasize is the buffer asked for
1442 if( dmabuf
->count
> dmabuf
->dmasize
)
1443 dmabuf
->count
&= dmabuf
->dmasize
-1;
1445 dmabuf
->count
-= diff
;
1447 * backfill with silence and clear out the last
1448 * "diff" number of bytes.
1452 memset(dmabuf
->rawbuf
+ hwptr
- diff
,
1453 (dmabuf
->fmt
& CS_FMT_16BIT
) ? 0 : 0x80, diff
);
1457 memset(dmabuf
->rawbuf
,
1458 (dmabuf
->fmt
& CS_FMT_16BIT
) ? 0 : 0x80,
1460 memset((void *)((unsigned)dmabuf
->rawbuf
+
1461 dmabuf
->dmasize
+ hwptr
- diff
),
1462 (dmabuf
->fmt
& CS_FMT_16BIT
) ? 0 : 0x80,
1466 if (dmabuf
->count
< 0 || dmabuf
->count
> dmabuf
->dmasize
) {
1467 CS_DBGOUT(CS_ERROR
, 2, printk(
1468 "cs46xx: ERROR DAC count<0 or count > dmasize (%d)\n",
1471 * buffer underrun or buffer overrun, reset the
1472 * count of bytes written back to 0.
1474 if(dmabuf
->count
< 0)
1479 if (dmabuf
->count
< (signed)dmabuf
->dmasize
/2)
1480 wake_up(&dmabuf
->wait
);
1487 /* hold spinlock for the following! */
1488 static void cs_handle_midi(struct cs_card
*card
)
1495 while (!(cs461x_peekBA0(card
, BA0_MIDSR
) & MIDSR_RBE
)) {
1496 ch
= cs461x_peekBA0(card
, BA0_MIDRP
);
1497 if (card
->midi
.icnt
< CS_MIDIINBUF
) {
1498 card
->midi
.ibuf
[card
->midi
.iwr
] = ch
;
1499 card
->midi
.iwr
= (card
->midi
.iwr
+ 1) % CS_MIDIINBUF
;
1505 wake_up(&card
->midi
.iwait
);
1507 while (!(cs461x_peekBA0(card
, BA0_MIDSR
) & MIDSR_TBF
) && card
->midi
.ocnt
> 0) {
1508 temp1
= ( card
->midi
.obuf
[card
->midi
.ord
] ) & 0x000000ff;
1509 cs461x_pokeBA0(card
, BA0_MIDWP
,temp1
);
1510 card
->midi
.ord
= (card
->midi
.ord
+ 1) % CS_MIDIOUTBUF
;
1512 if (card
->midi
.ocnt
< CS_MIDIOUTBUF
-16)
1516 wake_up(&card
->midi
.owait
);
1519 static void cs_interrupt(int irq
, void *dev_id
, struct pt_regs
*regs
)
1521 struct cs_card
*card
= (struct cs_card
*)dev_id
;
1522 /* Single channel card */
1523 struct cs_state
*recstate
= card
->channel
[0].state
;
1524 struct cs_state
*playstate
= card
->channel
[1].state
;
1527 CS_DBGOUT(CS_INTERRUPT
, 4, printk("cs46xx: cs_interrupt()+ \n"));
1529 spin_lock(&card
->lock
);
1531 status
= cs461x_peekBA0(card
, BA0_HISR
);
1533 if ((status
& 0x7fffffff) == 0)
1535 cs461x_pokeBA0(card
, BA0_HICR
, HICR_CHGM
|HICR_IEV
);
1536 spin_unlock(&card
->lock
);
1541 * check for playback or capture interrupt only
1543 if( ((status
& HISR_VC0
) && playstate
&& playstate
->dmabuf
.ready
) ||
1544 (((status
& HISR_VC1
) && recstate
&& recstate
->dmabuf
.ready
)) )
1546 CS_DBGOUT(CS_INTERRUPT
, 8, printk(
1547 "cs46xx: cs_interrupt() interrupt bit(s) set (0x%x)\n",status
));
1551 if( status
& HISR_MIDI
)
1552 cs_handle_midi(card
);
1555 cs461x_pokeBA0(card
, BA0_HICR
, HICR_CHGM
|HICR_IEV
);
1556 spin_unlock(&card
->lock
);
1557 CS_DBGOUT(CS_INTERRUPT
, 4, printk("cs46xx: cs_interrupt()- \n"));
1561 /**********************************************************************/
1563 static ssize_t
cs_midi_read(struct file
*file
, char *buffer
, size_t count
, loff_t
*ppos
)
1565 struct cs_card
*card
= (struct cs_card
*)file
->private_data
;
1567 unsigned long flags
;
1571 if (ppos
!= &file
->f_pos
)
1573 if (!access_ok(VERIFY_WRITE
, buffer
, count
))
1577 spin_lock_irqsave(&card
->lock
, flags
);
1578 ptr
= card
->midi
.ird
;
1579 cnt
= CS_MIDIINBUF
- ptr
;
1580 if (card
->midi
.icnt
< cnt
)
1581 cnt
= card
->midi
.icnt
;
1582 spin_unlock_irqrestore(&card
->lock
, flags
);
1586 if (file
->f_flags
& O_NONBLOCK
)
1587 return ret
? ret
: -EAGAIN
;
1588 interruptible_sleep_on(&card
->midi
.iwait
);
1589 if (signal_pending(current
))
1590 return ret
? ret
: -ERESTARTSYS
;
1593 if (copy_to_user(buffer
, card
->midi
.ibuf
+ ptr
, cnt
))
1594 return ret
? ret
: -EFAULT
;
1595 ptr
= (ptr
+ cnt
) % CS_MIDIINBUF
;
1596 spin_lock_irqsave(&card
->lock
, flags
);
1597 card
->midi
.ird
= ptr
;
1598 card
->midi
.icnt
-= cnt
;
1599 spin_unlock_irqrestore(&card
->lock
, flags
);
1608 static ssize_t
cs_midi_write(struct file
*file
, const char *buffer
, size_t count
, loff_t
*ppos
)
1610 struct cs_card
*card
= (struct cs_card
*)file
->private_data
;
1612 unsigned long flags
;
1616 if (ppos
!= &file
->f_pos
)
1618 if (!access_ok(VERIFY_READ
, buffer
, count
))
1622 spin_lock_irqsave(&card
->lock
, flags
);
1623 ptr
= card
->midi
.owr
;
1624 cnt
= CS_MIDIOUTBUF
- ptr
;
1625 if (card
->midi
.ocnt
+ cnt
> CS_MIDIOUTBUF
)
1626 cnt
= CS_MIDIOUTBUF
- card
->midi
.ocnt
;
1628 cs_handle_midi(card
);
1629 spin_unlock_irqrestore(&card
->lock
, flags
);
1633 if (file
->f_flags
& O_NONBLOCK
)
1634 return ret
? ret
: -EAGAIN
;
1635 interruptible_sleep_on(&card
->midi
.owait
);
1636 if (signal_pending(current
))
1637 return ret
? ret
: -ERESTARTSYS
;
1640 if (copy_from_user(card
->midi
.obuf
+ ptr
, buffer
, cnt
))
1641 return ret
? ret
: -EFAULT
;
1642 ptr
= (ptr
+ cnt
) % CS_MIDIOUTBUF
;
1643 spin_lock_irqsave(&card
->lock
, flags
);
1644 card
->midi
.owr
= ptr
;
1645 card
->midi
.ocnt
+= cnt
;
1646 spin_unlock_irqrestore(&card
->lock
, flags
);
1650 spin_lock_irqsave(&card
->lock
, flags
);
1651 cs_handle_midi(card
);
1652 spin_unlock_irqrestore(&card
->lock
, flags
);
1658 static unsigned int cs_midi_poll(struct file
*file
, struct poll_table_struct
*wait
)
1660 struct cs_card
*card
= (struct cs_card
*)file
->private_data
;
1661 unsigned long flags
;
1662 unsigned int mask
= 0;
1664 if (file
->f_flags
& FMODE_WRITE
)
1665 poll_wait(file
, &card
->midi
.owait
, wait
);
1666 if (file
->f_flags
& FMODE_READ
)
1667 poll_wait(file
, &card
->midi
.iwait
, wait
);
1668 spin_lock_irqsave(&card
->lock
, flags
);
1669 if (file
->f_flags
& FMODE_READ
) {
1670 if (card
->midi
.icnt
> 0)
1671 mask
|= POLLIN
| POLLRDNORM
;
1673 if (file
->f_flags
& FMODE_WRITE
) {
1674 if (card
->midi
.ocnt
< CS_MIDIOUTBUF
)
1675 mask
|= POLLOUT
| POLLWRNORM
;
1677 spin_unlock_irqrestore(&card
->lock
, flags
);
1682 static int cs_midi_open(struct inode
*inode
, struct file
*file
)
1684 int minor
= MINOR(inode
->i_rdev
);
1685 struct cs_card
*card
= devs
;
1686 unsigned long flags
;
1687 while (card
&& card
->dev_midi
!= minor
)
1691 file
->private_data
= card
;
1692 /* wait for device to become free */
1693 down(&card
->midi
.open_sem
);
1694 while (card
->midi
.open_mode
& file
->f_mode
) {
1695 if (file
->f_flags
& O_NONBLOCK
) {
1696 up(&card
->midi
.open_sem
);
1699 up(&card
->midi
.open_sem
);
1700 interruptible_sleep_on(&card
->midi
.open_wait
);
1701 if (signal_pending(current
))
1702 return -ERESTARTSYS
;
1703 down(&card
->midi
.open_sem
);
1705 spin_lock_irqsave(&card
->midi
.lock
, flags
);
1706 if (!(card
->midi
.open_mode
& (FMODE_READ
| FMODE_WRITE
))) {
1707 card
->midi
.ird
= card
->midi
.iwr
= card
->midi
.icnt
= 0;
1708 card
->midi
.ord
= card
->midi
.owr
= card
->midi
.ocnt
= 0;
1709 card
->midi
.ird
= card
->midi
.iwr
= card
->midi
.icnt
= 0;
1710 cs461x_pokeBA0(card
, BA0_MIDCR
, 0x0000000f); /* Enable xmit, rcv. */
1711 cs461x_pokeBA0(card
, BA0_HICR
, HICR_IEV
| HICR_CHGM
); /* Enable interrupts */
1713 if (file
->f_mode
& FMODE_READ
) {
1714 card
->midi
.ird
= card
->midi
.iwr
= card
->midi
.icnt
= 0;
1716 if (file
->f_mode
& FMODE_WRITE
) {
1717 card
->midi
.ord
= card
->midi
.owr
= card
->midi
.ocnt
= 0;
1719 spin_unlock_irqrestore(&card
->midi
.lock
, flags
);
1720 card
->midi
.open_mode
|= (file
->f_mode
& (FMODE_READ
| FMODE_WRITE
));
1721 up(&card
->midi
.open_sem
);
1727 static int cs_midi_release(struct inode
*inode
, struct file
*file
)
1729 struct cs_card
*card
= (struct cs_card
*)file
->private_data
;
1730 DECLARE_WAITQUEUE(wait
, current
);
1731 unsigned long flags
;
1732 unsigned count
, tmo
;
1734 if (file
->f_mode
& FMODE_WRITE
) {
1735 current
->state
= TASK_INTERRUPTIBLE
;
1736 add_wait_queue(&card
->midi
.owait
, &wait
);
1738 spin_lock_irqsave(&card
->midi
.lock
, flags
);
1739 count
= card
->midi
.ocnt
;
1740 spin_unlock_irqrestore(&card
->midi
.lock
, flags
);
1743 if (signal_pending(current
))
1745 if (file
->f_flags
& O_NONBLOCK
) {
1746 remove_wait_queue(&card
->midi
.owait
, &wait
);
1747 current
->state
= TASK_RUNNING
;
1750 tmo
= (count
* HZ
) / 3100;
1751 if (!schedule_timeout(tmo
? : 1) && tmo
)
1752 printk(KERN_DEBUG
"cs46xx: midi timed out??\n");
1754 remove_wait_queue(&card
->midi
.owait
, &wait
);
1755 current
->state
= TASK_RUNNING
;
1757 down(&card
->midi
.open_sem
);
1758 card
->midi
.open_mode
&= (~(file
->f_mode
& (FMODE_READ
| FMODE_WRITE
)));
1759 up(&card
->midi
.open_sem
);
1760 wake_up(&card
->midi
.open_wait
);
1766 * Midi file operations struct.
1768 static /*const*/ struct file_operations cs_midi_fops
= {
1771 write
: cs_midi_write
,
1774 release
: cs_midi_release
,
1777 static loff_t
cs_llseek(struct file
*file
, loff_t offset
, int origin
)
1784 * CopySamples copies 16-bit stereo signed samples from the source to the
1785 * destination, possibly converting down to unsigned 8-bit and/or mono.
1786 * count specifies the number of output bytes to write.
1790 * dst - Pointer to a destination buffer.
1791 * src - Pointer to a source buffer
1792 * count - The number of bytes to copy into the destination buffer.
1793 * fmt - CS_FMT_16BIT and/or CS_FMT_STEREO bits
1794 * dmabuf - pointer to the dma buffer structure
1796 * NOTES: only call this routine if the output desired is not 16 Signed Stereo
1800 static void CopySamples(char *dst
, char *src
, int count
, unsigned fmt
,
1801 struct dmabuf
*dmabuf
)
1805 s16
*psSrc
=(s16
*)src
;
1806 s16
*psDst
=(s16
*)dst
;
1807 u8
*pucDst
=(u8
*)dst
;
1809 CS_DBGOUT(CS_FUNCTION
, 2, printk(KERN_INFO
"cs4281: CopySamples()+ ") );
1810 CS_DBGOUT(CS_WAVE_READ
, 8, printk(KERN_INFO
1811 " dst=0x%x src=0x%x count=%d fmt=0x%x\n",
1812 (unsigned)dst
,(unsigned)src
,(unsigned)count
,(unsigned)fmt
) );
1815 * See if the data should be output as 8-bit unsigned stereo.
1817 if((fmt
& CS_FMT_STEREO
) && !(fmt
& CS_FMT_16BIT
))
1820 * Convert each 16-bit signed stereo sample to 8-bit unsigned
1821 * stereo using rounding.
1827 *(pucDst
++) = (u8
)(((s16
)(*psSrc
++) + (s16
)0x8000) >> 8);
1831 * See if the data should be output at 8-bit unsigned mono.
1833 else if(!(fmt
& CS_FMT_STEREO
) && !(fmt
& CS_FMT_16BIT
))
1836 * Convert each 16-bit signed stereo sample to 8-bit unsigned
1837 * mono using averaging and rounding.
1843 s32AudioSample
= ((*psSrc
)+(*(psSrc
+ 1)))/2 + (s32
)0x80;
1844 if(s32AudioSample
> 0x7fff)
1845 s32AudioSample
= 0x7fff;
1846 *(pucDst
++) = (u8
)(((s16
)s32AudioSample
+ (s16
)0x8000) >> 8);
1851 * See if the data should be output at 16-bit signed mono.
1853 else if(!(fmt
& CS_FMT_STEREO
) && (fmt
& CS_FMT_16BIT
))
1856 * Convert each 16-bit signed stereo sample to 16-bit signed
1857 * mono using averaging.
1863 *(psDst
++) = (s16
)((*psSrc
)+(*(psSrc
+ 1)))/2;
1871 * replacement for the standard copy_to_user, to allow for a conversion from
1872 * 16 bit to 8 bit and from stereo to mono, if the record conversion is active.
1873 * The current CS46xx/CS4280 static image only records in 16bit unsigned Stereo,
1874 * so we convert from any of the other format combinations.
1876 static unsigned cs_copy_to_user(
1883 struct dmabuf
*dmabuf
= &s
->dmabuf
;
1884 void *src
= hwsrc
; /* default to the standard destination buffer addr */
1886 CS_DBGOUT(CS_FUNCTION
, 6, printk(KERN_INFO
1887 "cs_copy_to_user()+ fmt=0x%x cnt=%d dest=0x%.8x\n",
1888 dmabuf
->fmt
,(unsigned)cnt
,(unsigned)dest
) );
1890 if(cnt
> dmabuf
->dmasize
)
1892 cnt
= dmabuf
->dmasize
;
1899 if(dmabuf
->divisor
!= 1)
1901 if(!dmabuf
->tmpbuff
)
1903 *copied
= cnt
/dmabuf
->divisor
;
1907 CopySamples((char *)dmabuf
->tmpbuff
, (char *)hwsrc
, cnt
,
1908 dmabuf
->fmt
, dmabuf
);
1909 src
= dmabuf
->tmpbuff
;
1910 cnt
= cnt
/dmabuf
->divisor
;
1912 if (copy_to_user(dest
, src
, cnt
))
1914 CS_DBGOUT(CS_FUNCTION
, 2, printk(KERN_ERR
1915 "cs4281: cs_copy_to_user()- fault dest=0x%x src=0x%x cnt=%d\n",
1916 (unsigned)dest
,(unsigned)src
,cnt
) );
1921 CS_DBGOUT(CS_FUNCTION
, 2, printk(KERN_INFO
1922 "cs4281: cs_copy_to_user()- copied bytes is %d \n",cnt
) );
1926 /* in this loop, dmabuf.count signifies the amount of data that is waiting to be copied to
1927 the user's buffer. it is filled by the dma machine and drained by this loop. */
1928 static ssize_t
cs_read(struct file
*file
, char *buffer
, size_t count
, loff_t
*ppos
)
1930 struct cs_card
*card
=devs
;
1931 struct cs_state
*state
;
1932 DECLARE_WAITQUEUE(wait
, current
);
1933 struct dmabuf
*dmabuf
;
1935 unsigned long flags
;
1940 CS_DBGOUT(CS_WAVE_READ
, 4, printk("cs461x: cs_read()+ %d\n",count
) );
1941 state
= (struct cs_state
*)card
->states
[0];
1944 dmabuf
= &state
->dmabuf
;
1946 if (ppos
!= &file
->f_pos
)
1950 if (!dmabuf
->ready
&& (ret
= prog_dmabuf(state
)))
1952 if (!access_ok(VERIFY_WRITE
, buffer
, count
))
1955 add_wait_queue(&state
->dmabuf
.wait
, &wait
);
1957 spin_lock_irqsave(&state
->card
->lock
, flags
);
1958 swptr
= dmabuf
->swptr
;
1959 cnt
= dmabuf
->dmasize
- swptr
;
1960 if (dmabuf
->count
< cnt
)
1961 cnt
= dmabuf
->count
;
1963 __set_current_state(TASK_INTERRUPTIBLE
);
1964 spin_unlock_irqrestore(&state
->card
->lock
, flags
);
1966 if (cnt
> (count
* dmabuf
->divisor
))
1967 cnt
= count
* dmabuf
->divisor
;
1969 /* buffer is empty, start the dma machine and wait for data to be
1972 if (file
->f_flags
& O_NONBLOCK
) {
1973 if (!ret
) ret
= -EAGAIN
;
1974 remove_wait_queue(&state
->dmabuf
.wait
, &wait
);
1978 if (signal_pending(current
)) {
1979 ret
= ret
? ret
: -ERESTARTSYS
;
1985 CS_DBGOUT(CS_WAVE_READ
, 2, printk(KERN_INFO
1986 "_read() copy_to cnt=%d count=%d ", cnt
,count
) );
1987 CS_DBGOUT(CS_WAVE_READ
, 8, printk(KERN_INFO
1988 " .dmasize=%d .count=%d buffer=0x%.8x ret=%d\n",
1989 dmabuf
->dmasize
,dmabuf
->count
,(unsigned)buffer
,ret
) );
1991 if (cs_copy_to_user(state
, buffer
,
1992 (void *)((unsigned)dmabuf
->rawbuf
+ swptr
), cnt
, &copied
))
1994 if (!ret
) ret
= -EFAULT
;
1997 swptr
= (swptr
+ cnt
) % dmabuf
->dmasize
;
1998 spin_lock_irqsave(&card
->lock
, flags
);
1999 dmabuf
->swptr
= swptr
;
2000 dmabuf
->count
-= cnt
;
2001 spin_unlock_irqrestore(&card
->lock
, flags
);
2007 remove_wait_queue(&state
->dmabuf
.wait
, &wait
);
2008 set_current_state(TASK_RUNNING
);
2009 CS_DBGOUT(CS_WAVE_READ
, 4, printk("cs461x: cs_read()- %d\n",ret
) );
2013 /* in this loop, dmabuf.count signifies the amount of data that is waiting to be dma to
2014 the soundcard. it is drained by the dma machine and filled by this loop. */
2015 static ssize_t
cs_write(struct file
*file
, const char *buffer
, size_t count
, loff_t
*ppos
)
2017 struct cs_card
*card
=devs
;
2018 struct cs_state
*state
;
2019 DECLARE_WAITQUEUE(wait
, current
);
2020 struct dmabuf
*dmabuf
;
2022 unsigned long flags
;
2026 CS_DBGOUT(CS_WAVE_WRITE
| CS_FUNCTION
, 4,
2027 printk("cs461x: cs_write called, count = %d\n", count
) );
2028 state
= (struct cs_state
*)card
->states
[1];
2031 dmabuf
= &state
->dmabuf
;
2033 if (ppos
!= &file
->f_pos
)
2037 if (!dmabuf
->ready
&& (ret
= prog_dmabuf(state
)))
2039 if (!access_ok(VERIFY_READ
, buffer
, count
))
2041 add_wait_queue(&state
->dmabuf
.wait
, &wait
);
2043 spin_lock_irqsave(&state
->card
->lock
, flags
);
2044 if (dmabuf
->count
< 0) {
2045 /* buffer underrun, we are recovering from sleep_on_timeout,
2046 resync hwptr and swptr */
2048 dmabuf
->swptr
= dmabuf
->hwptr
;
2050 if (dmabuf
->underrun
)
2052 dmabuf
->underrun
= 0;
2053 dmabuf
->hwptr
= cs_get_dma_addr(state
);
2054 dmabuf
->swptr
= dmabuf
->hwptr
;
2056 swptr
= dmabuf
->swptr
;
2057 cnt
= dmabuf
->dmasize
- swptr
;
2058 if (dmabuf
->count
+ cnt
> dmabuf
->dmasize
)
2059 cnt
= dmabuf
->dmasize
- dmabuf
->count
;
2061 __set_current_state(TASK_INTERRUPTIBLE
);
2062 spin_unlock_irqrestore(&state
->card
->lock
, flags
);
2067 /* buffer is full, start the dma machine and wait for data to be
2070 if (file
->f_flags
& O_NONBLOCK
) {
2071 if (!ret
) ret
= -EAGAIN
;
2075 if (signal_pending(current
)) {
2076 ret
= ret
? ret
: -ERESTARTSYS
;
2081 if (copy_from_user(dmabuf
->rawbuf
+ swptr
, buffer
, cnt
)) {
2082 if (!ret
) ret
= -EFAULT
;
2086 swptr
= (swptr
+ cnt
) % dmabuf
->dmasize
;
2088 spin_lock_irqsave(&state
->card
->lock
, flags
);
2089 dmabuf
->swptr
= swptr
;
2090 dmabuf
->count
+= cnt
;
2091 if(dmabuf
->count
> dmabuf
->dmasize
)
2093 CS_DBGOUT(CS_WAVE_WRITE
| CS_ERROR
, 2, printk(
2094 "cs46xx: cs_write() d->count > dmasize - resetting\n"));
2095 dmabuf
->count
= dmabuf
->dmasize
;
2097 dmabuf
->endcleared
= 0;
2098 spin_unlock_irqrestore(&state
->card
->lock
, flags
);
2105 remove_wait_queue(&state
->dmabuf
.wait
, &wait
);
2106 set_current_state(TASK_RUNNING
);
2108 CS_DBGOUT(CS_WAVE_WRITE
| CS_FUNCTION
, 2,
2109 printk("cs46xx: cs_write()- ret=0x%x\n", ret
) );
2113 static unsigned int cs_poll(struct file
*file
, struct poll_table_struct
*wait
)
2115 struct cs_card
*card
= (struct cs_card
*)file
->private_data
;
2116 struct dmabuf
*dmabuf
;
2117 struct cs_state
*state
;
2119 unsigned long flags
;
2120 unsigned int mask
= 0;
2122 CS_DBGOUT(CS_FUNCTION
, 2, printk("cs46xx: cs_poll()+ \n"));
2123 if (!(file
->f_mode
& (FMODE_WRITE
| FMODE_READ
)))
2127 if (file
->f_mode
& FMODE_WRITE
)
2129 state
= card
->states
[1];
2132 dmabuf
= &state
->dmabuf
;
2133 poll_wait(file
, &dmabuf
->wait
, wait
);
2136 if (file
->f_mode
& FMODE_READ
)
2138 state
= card
->states
[0];
2141 dmabuf
= &state
->dmabuf
;
2142 poll_wait(file
, &dmabuf
->wait
, wait
);
2146 spin_lock_irqsave(&card
->lock
, flags
);
2148 if (file
->f_mode
& FMODE_READ
) {
2149 state
= card
->states
[0];
2152 dmabuf
= &state
->dmabuf
;
2153 if (dmabuf
->count
>= (signed)dmabuf
->fragsize
)
2154 mask
|= POLLIN
| POLLRDNORM
;
2157 if (file
->f_mode
& FMODE_WRITE
) {
2158 state
= card
->states
[1];
2161 dmabuf
= &state
->dmabuf
;
2162 if (dmabuf
->mapped
) {
2163 if (dmabuf
->count
>= (signed)dmabuf
->fragsize
)
2164 mask
|= POLLOUT
| POLLWRNORM
;
2166 if ((signed)dmabuf
->dmasize
>= dmabuf
->count
2167 + (signed)dmabuf
->fragsize
)
2168 mask
|= POLLOUT
| POLLWRNORM
;
2172 spin_unlock_irqrestore(&card
->lock
, flags
);
2174 CS_DBGOUT(CS_FUNCTION
, 2, printk("cs46xx: cs_poll()- \n"));
2179 * We let users mmap the ring buffer. Its not the real DMA buffer but
2180 * that side of the code is hidden in the IRQ handling. We do a software
2181 * emulation of DMA from a 64K or so buffer into a 2K FIFO.
2182 * (the hardware probably deserves a moan here but Crystal send me nice
2186 static int cs_mmap(struct file
*file
, struct vm_area_struct
*vma
)
2188 struct cs_card
*card
=devs
;
2189 struct cs_state
*state
;
2190 struct dmabuf
*dmabuf
;
2194 CS_DBGOUT(CS_FUNCTION
| CS_PARMS
, 2, printk("cs46xx: cs_mmap()+ file=0x%x %s %s\n",
2195 (unsigned)file
, vma
->vm_flags
& VM_WRITE
? "VM_WRITE" : "",
2196 vma
->vm_flags
& VM_READ
? "VM_READ" : "") );
2198 if (vma
->vm_flags
& VM_WRITE
) {
2199 state
= card
->states
[1];
2202 CS_DBGOUT(CS_OPEN
, 2, printk(
2203 "cs46xx: cs_mmap() VM_WRITE - state TRUE prog_dmabuf DAC\n") );
2204 if ((ret
= prog_dmabuf(state
)) != 0)
2207 } else if (vma
->vm_flags
& VM_READ
) {
2208 state
= card
->states
[0];
2211 CS_DBGOUT(CS_OPEN
, 2, printk(
2212 "cs46xx: cs_mmap() VM_READ - state TRUE prog_dmabuf ADC\n") );
2213 if ((ret
= prog_dmabuf(state
)) != 0)
2217 CS_DBGOUT(CS_ERROR
, 2, printk(
2218 "cs46xx: cs_mmap() return -EINVAL\n") );
2223 * For now ONLY support playback, but seems like the only way to use
2224 * mmap() is to open an FD with RDWR, just read or just write access
2225 * does not function, get an error back from the kernel.
2226 * Also, QuakeIII opens with RDWR! So, there must be something
2227 * to needing read/write access mapping. So, allow read/write but
2230 state
= card
->states
[1];
2231 if(!(unsigned)state
)
2234 dmabuf
= &state
->dmabuf
;
2235 if (vma
->vm_pgoff
!= 0)
2237 size
= vma
->vm_end
- vma
->vm_start
;
2239 CS_DBGOUT(CS_PARMS
, 2, printk("cs46xx: cs_mmap(): size=%d\n",(unsigned)size
) );
2241 if (size
> (PAGE_SIZE
<< dmabuf
->buforder
))
2243 if (remap_page_range(vma
->vm_start
, virt_to_phys(dmabuf
->rawbuf
),
2244 size
, vma
->vm_page_prot
))
2248 CS_DBGOUT(CS_FUNCTION
, 2, printk("cs46xx: cs_mmap()-\n") );
2252 static int cs_ioctl(struct inode
*inode
, struct file
*file
, unsigned int cmd
, unsigned long arg
)
2254 struct cs_card
*card
= (struct cs_card
*)file
->private_data
;
2255 struct cs_state
*state
;
2256 struct dmabuf
*dmabuf
=0;
2257 unsigned long flags
;
2258 audio_buf_info abinfo
;
2260 int val
, valsave
, mapped
, ret
;
2262 state
= (struct cs_state
*)card
->states
[0];
2265 dmabuf
= &state
->dmabuf
;
2266 mapped
= (file
->f_mode
& FMODE_READ
) && dmabuf
->mapped
;
2268 state
= (struct cs_state
*)card
->states
[1];
2271 dmabuf
= &state
->dmabuf
;
2272 mapped
|= (file
->f_mode
& FMODE_WRITE
) && dmabuf
->mapped
;
2281 case OSS_GETVERSION
:
2282 return put_user(SOUND_VERSION
, (int *)arg
);
2284 case SNDCTL_DSP_RESET
:
2285 /* FIXME: spin_lock ? */
2286 if (file
->f_mode
& FMODE_WRITE
) {
2287 state
= (struct cs_state
*)card
->states
[1];
2290 dmabuf
= &state
->dmabuf
;
2294 resync_dma_ptrs(state
);
2295 dmabuf
->swptr
= dmabuf
->hwptr
= 0;
2296 dmabuf
->count
= dmabuf
->total_bytes
= 0;
2301 if (file
->f_mode
& FMODE_READ
) {
2302 state
= (struct cs_state
*)card
->states
[0];
2305 dmabuf
= &state
->dmabuf
;
2308 resync_dma_ptrs(state
);
2310 dmabuf
->swptr
= dmabuf
->hwptr
= 0;
2311 dmabuf
->count
= dmabuf
->total_bytes
= 0;
2316 CS_DBGOUT(CS_IOCTL
, 2, printk("cs46xx: DSP_RESET()-\n") );
2319 case SNDCTL_DSP_SYNC
:
2320 if (file
->f_mode
& FMODE_WRITE
)
2321 return drain_dac(state
, file
->f_flags
& O_NONBLOCK
);
2324 case SNDCTL_DSP_SPEED
: /* set sample rate */
2325 if (get_user(val
, (int *)arg
))
2328 if (file
->f_mode
& FMODE_READ
) {
2329 state
= (struct cs_state
*)card
->states
[0];
2332 dmabuf
= &state
->dmabuf
;
2336 cs_set_adc_rate(state
, val
);
2337 cs_set_divisor(dmabuf
);
2340 if (file
->f_mode
& FMODE_WRITE
) {
2341 state
= (struct cs_state
*)card
->states
[1];
2344 dmabuf
= &state
->dmabuf
;
2348 cs_set_dac_rate(state
, val
);
2349 cs_set_divisor(dmabuf
);
2352 CS_DBGOUT(CS_IOCTL
| CS_PARMS
, 4, printk(
2353 "cs46xx: cs_ioctl() DSP_SPEED %s %s %d\n",
2354 file
->f_mode
& FMODE_WRITE
? "DAC" : "",
2355 file
->f_mode
& FMODE_READ
? "ADC" : "",
2357 return put_user(dmabuf
->rate
, (int *)arg
);
2359 return put_user(0, (int *)arg
);
2361 case SNDCTL_DSP_STEREO
: /* set stereo or mono channel */
2362 if (get_user(val
, (int *)arg
))
2364 if (file
->f_mode
& FMODE_WRITE
) {
2365 state
= (struct cs_state
*)card
->states
[1];
2368 dmabuf
= &state
->dmabuf
;
2373 dmabuf
->fmt
|= CS_FMT_STEREO
;
2375 dmabuf
->fmt
&= ~CS_FMT_STEREO
;
2376 cs_set_divisor(dmabuf
);
2377 CS_DBGOUT(CS_IOCTL
| CS_PARMS
, 4, printk(
2378 "cs46xx: DSP_STEREO() DAC %s\n",
2379 (dmabuf
->fmt
& CS_FMT_STEREO
) ?
2383 if (file
->f_mode
& FMODE_READ
) {
2384 state
= (struct cs_state
*)card
->states
[0];
2387 dmabuf
= &state
->dmabuf
;
2392 dmabuf
->fmt
|= CS_FMT_STEREO
;
2394 dmabuf
->fmt
&= ~CS_FMT_STEREO
;
2395 cs_set_divisor(dmabuf
);
2396 CS_DBGOUT(CS_IOCTL
| CS_PARMS
, 4, printk(
2397 "cs46xx: DSP_STEREO() ADC %s\n",
2398 (dmabuf
->fmt
& CS_FMT_STEREO
) ?
2404 case SNDCTL_DSP_GETBLKSIZE
:
2405 if (file
->f_mode
& FMODE_WRITE
) {
2406 state
= (struct cs_state
*)card
->states
[1];
2409 dmabuf
= &state
->dmabuf
;
2410 if ((val
= prog_dmabuf(state
)))
2412 return put_user(dmabuf
->fragsize
, (int *)arg
);
2415 if (file
->f_mode
& FMODE_READ
) {
2416 state
= (struct cs_state
*)card
->states
[0];
2419 dmabuf
= &state
->dmabuf
;
2420 if ((val
= prog_dmabuf(state
)))
2422 return put_user(dmabuf
->fragsize
/dmabuf
->divisor
,
2426 return put_user(0, (int *)arg
);
2428 case SNDCTL_DSP_GETFMTS
: /* Returns a mask of supported sample format*/
2429 return put_user(AFMT_S16_LE
| AFMT_U8
, (int *)arg
);
2431 case SNDCTL_DSP_SETFMT
: /* Select sample format */
2432 if (get_user(val
, (int *)arg
))
2434 CS_DBGOUT(CS_IOCTL
| CS_PARMS
, 4, printk(
2435 "cs46xx: cs_ioctl() DSP_SETFMT %s %s %s %s\n",
2436 file
->f_mode
& FMODE_WRITE
? "DAC" : "",
2437 file
->f_mode
& FMODE_READ
? "ADC" : "",
2438 val
== AFMT_S16_LE
? "16Bit Signed" : "",
2439 val
== AFMT_U8
? "8Bit Unsigned" : "") );
2441 if (val
!= AFMT_QUERY
) {
2442 if(val
==AFMT_S16_LE
|| val
==AFMT_U8
)
2444 if (file
->f_mode
& FMODE_WRITE
) {
2445 state
= (struct cs_state
*)card
->states
[1];
2448 dmabuf
= &state
->dmabuf
;
2452 if(val
==AFMT_S16_LE
)
2453 dmabuf
->fmt
|= CS_FMT_16BIT
;
2455 dmabuf
->fmt
&= ~CS_FMT_16BIT
;
2456 cs_set_divisor(dmabuf
);
2457 if((ret
= prog_dmabuf(state
)))
2461 if (file
->f_mode
& FMODE_READ
) {
2463 state
= (struct cs_state
*)card
->states
[0];
2466 dmabuf
= &state
->dmabuf
;
2470 if(val
==AFMT_S16_LE
)
2471 dmabuf
->fmt
|= CS_FMT_16BIT
;
2473 dmabuf
->fmt
&= ~CS_FMT_16BIT
;
2474 cs_set_divisor(dmabuf
);
2475 if((ret
= prog_dmabuf(state
)))
2482 CS_DBGOUT(CS_IOCTL
| CS_ERROR
, 2, printk(
2483 "cs46xx: DSP_SETFMT() Unsupported format (0x%x)\n",
2489 if(file
->f_mode
& FMODE_WRITE
)
2491 state
= (struct cs_state
*)card
->states
[1];
2493 dmabuf
= &state
->dmabuf
;
2495 else if(file
->f_mode
& FMODE_READ
)
2497 state
= (struct cs_state
*)card
->states
[0];
2499 dmabuf
= &state
->dmabuf
;
2504 if(dmabuf
->fmt
& CS_FMT_16BIT
)
2505 return put_user(AFMT_S16_LE
, (int *)arg
);
2507 return put_user(AFMT_U8
, (int *)arg
);
2509 return put_user(0, (int *)arg
);
2511 case SNDCTL_DSP_CHANNELS
:
2512 if (get_user(val
, (int *)arg
))
2515 if (file
->f_mode
& FMODE_WRITE
) {
2516 state
= (struct cs_state
*)card
->states
[1];
2519 dmabuf
= &state
->dmabuf
;
2524 dmabuf
->fmt
|= CS_FMT_STEREO
;
2526 dmabuf
->fmt
&= ~CS_FMT_STEREO
;
2527 cs_set_divisor(dmabuf
);
2528 if (prog_dmabuf(state
))
2532 if (file
->f_mode
& FMODE_READ
) {
2533 state
= (struct cs_state
*)card
->states
[0];
2536 dmabuf
= &state
->dmabuf
;
2541 dmabuf
->fmt
|= CS_FMT_STEREO
;
2543 dmabuf
->fmt
&= ~CS_FMT_STEREO
;
2544 cs_set_divisor(dmabuf
);
2545 if (prog_dmabuf(state
))
2550 return put_user((dmabuf
->fmt
& CS_FMT_STEREO
) ? 2 : 1,
2553 case SNDCTL_DSP_POST
:
2555 * There will be a longer than normal pause in the data.
2556 * so... do nothing, because there is nothing that we can do.
2560 case SNDCTL_DSP_SUBDIVIDE
:
2561 if (file
->f_mode
& FMODE_WRITE
) {
2562 state
= (struct cs_state
*)card
->states
[1];
2565 dmabuf
= &state
->dmabuf
;
2566 if (dmabuf
->subdivision
)
2568 if (get_user(val
, (int *)arg
))
2570 if (val
!= 1 && val
!= 2)
2572 dmabuf
->subdivision
= val
;
2575 if (file
->f_mode
& FMODE_READ
) {
2576 state
= (struct cs_state
*)card
->states
[0];
2579 dmabuf
= &state
->dmabuf
;
2580 if (dmabuf
->subdivision
)
2582 if (get_user(val
, (int *)arg
))
2584 if (val
!= 1 && val
!= 2)
2586 dmabuf
->subdivision
= val
;
2591 case SNDCTL_DSP_SETFRAGMENT
:
2592 if (get_user(val
, (int *)arg
))
2595 if (file
->f_mode
& FMODE_WRITE
) {
2596 state
= (struct cs_state
*)card
->states
[1];
2599 dmabuf
= &state
->dmabuf
;
2600 dmabuf
->ossfragshift
= val
& 0xffff;
2601 dmabuf
->ossmaxfrags
= (val
>> 16) & 0xffff;
2604 if (file
->f_mode
& FMODE_READ
) {
2605 state
= (struct cs_state
*)card
->states
[0];
2608 dmabuf
= &state
->dmabuf
;
2609 dmabuf
->ossfragshift
= val
& 0xffff;
2610 dmabuf
->ossmaxfrags
= (val
>> 16) & 0xffff;
2615 case SNDCTL_DSP_GETOSPACE
:
2616 state
= (struct cs_state
*)card
->states
[1];
2619 dmabuf
= &state
->dmabuf
;
2620 spin_lock_irqsave(&state
->card
->lock
, flags
);
2622 abinfo
.fragsize
= dmabuf
->fragsize
;
2623 abinfo
.fragstotal
= dmabuf
->numfrag
;
2625 * for mmap we always have total space available
2628 abinfo
.bytes
= dmabuf
->dmasize
;
2630 abinfo
.bytes
= dmabuf
->dmasize
- dmabuf
->count
;
2632 abinfo
.fragments
= abinfo
.bytes
>> dmabuf
->fragshift
;
2633 spin_unlock_irqrestore(&state
->card
->lock
, flags
);
2634 return copy_to_user((void *)arg
, &abinfo
, sizeof(abinfo
)) ? -EFAULT
: 0;
2638 case SNDCTL_DSP_GETISPACE
:
2639 state
= (struct cs_state
*)card
->states
[0];
2642 dmabuf
= &state
->dmabuf
;
2643 spin_lock_irqsave(&state
->card
->lock
, flags
);
2645 abinfo
.fragsize
= dmabuf
->fragsize
/dmabuf
->divisor
;
2646 abinfo
.bytes
= dmabuf
->count
/dmabuf
->divisor
;
2647 abinfo
.fragstotal
= dmabuf
->numfrag
;
2648 abinfo
.fragments
= abinfo
.bytes
>> dmabuf
->fragshift
;
2649 spin_unlock_irqrestore(&state
->card
->lock
, flags
);
2650 return copy_to_user((void *)arg
, &abinfo
, sizeof(abinfo
)) ? -EFAULT
: 0;
2654 case SNDCTL_DSP_NONBLOCK
:
2655 file
->f_flags
|= O_NONBLOCK
;
2658 case SNDCTL_DSP_GETCAPS
:
2659 return put_user(DSP_CAP_REALTIME
|DSP_CAP_TRIGGER
|DSP_CAP_MMAP
,
2662 case SNDCTL_DSP_GETTRIGGER
:
2664 CS_DBGOUT(CS_IOCTL
, 2, printk("cs46xx: DSP_GETTRIGGER()+\n") );
2665 if (file
->f_mode
& FMODE_WRITE
)
2667 state
= (struct cs_state
*)card
->states
[1];
2670 dmabuf
= &state
->dmabuf
;
2671 if(dmabuf
->enable
& DAC_RUNNING
)
2672 val
|= PCM_ENABLE_INPUT
;
2675 if (file
->f_mode
& FMODE_READ
)
2679 state
= (struct cs_state
*)card
->states
[0];
2680 dmabuf
= &state
->dmabuf
;
2681 if(dmabuf
->enable
& ADC_RUNNING
)
2682 val
|= PCM_ENABLE_OUTPUT
;
2685 CS_DBGOUT(CS_IOCTL
, 2, printk("cs46xx: DSP_GETTRIGGER()- val=0x%x\n",val
) );
2686 return put_user(val
, (int *)arg
);
2688 case SNDCTL_DSP_SETTRIGGER
:
2689 if (get_user(val
, (int *)arg
))
2691 if (file
->f_mode
& FMODE_READ
) {
2692 state
= (struct cs_state
*)card
->states
[0];
2695 dmabuf
= &state
->dmabuf
;
2696 if (val
& PCM_ENABLE_INPUT
) {
2697 if (!dmabuf
->ready
&& (ret
= prog_dmabuf(state
)))
2704 if (file
->f_mode
& FMODE_WRITE
) {
2705 state
= (struct cs_state
*)card
->states
[1];
2708 dmabuf
= &state
->dmabuf
;
2709 if (val
& PCM_ENABLE_OUTPUT
) {
2710 if (!dmabuf
->ready
&& (ret
= prog_dmabuf(state
)))
2719 case SNDCTL_DSP_GETIPTR
:
2720 state
= (struct cs_state
*)card
->states
[0];
2723 dmabuf
= &state
->dmabuf
;
2724 spin_lock_irqsave(&state
->card
->lock
, flags
);
2726 cinfo
.bytes
= dmabuf
->total_bytes
/dmabuf
->divisor
;
2727 cinfo
.blocks
= dmabuf
->count
/dmabuf
->divisor
>> dmabuf
->fragshift
;
2728 cinfo
.ptr
= dmabuf
->hwptr
/dmabuf
->divisor
;
2729 spin_unlock_irqrestore(&state
->card
->lock
, flags
);
2730 return copy_to_user((void *)arg
, &cinfo
, sizeof(cinfo
));
2734 case SNDCTL_DSP_GETOPTR
:
2735 state
= (struct cs_state
*)card
->states
[1];
2738 dmabuf
= &state
->dmabuf
;
2739 spin_lock_irqsave(&state
->card
->lock
, flags
);
2741 cinfo
.bytes
= dmabuf
->total_bytes
;
2744 cinfo
.blocks
= (cinfo
.bytes
>> dmabuf
->fragshift
)
2746 CS_DBGOUT(CS_PARMS
, 8,
2747 printk("total_bytes=%d blocks=%d dmabuf->blocks=%d\n",
2748 cinfo
.bytes
,cinfo
.blocks
,dmabuf
->blocks
) );
2749 dmabuf
->blocks
= cinfo
.bytes
>> dmabuf
->fragshift
;
2753 cinfo
.blocks
= dmabuf
->count
>> dmabuf
->fragshift
;
2755 cinfo
.ptr
= dmabuf
->hwptr
;
2757 CS_DBGOUT(CS_PARMS
, 4, printk(
2758 "cs46xx: GETOPTR bytes=%d blocks=%d ptr=%d\n",
2759 cinfo
.bytes
,cinfo
.blocks
,cinfo
.ptr
) );
2760 spin_unlock_irqrestore(&state
->card
->lock
, flags
);
2761 return copy_to_user((void *)arg
, &cinfo
, sizeof(cinfo
));
2765 case SNDCTL_DSP_SETDUPLEX
:
2768 case SNDCTL_DSP_GETODELAY
:
2769 if (!(file
->f_mode
& FMODE_WRITE
))
2771 state
= (struct cs_state
*)card
->states
[1];
2774 dmabuf
= &state
->dmabuf
;
2775 spin_lock_irqsave(&state
->card
->lock
, flags
);
2777 val
= dmabuf
->count
;
2778 spin_unlock_irqrestore(&state
->card
->lock
, flags
);
2782 return put_user(val
, (int *)arg
);
2784 case SOUND_PCM_READ_RATE
:
2785 state
= (struct cs_state
*)card
->states
[0];
2788 dmabuf
= &state
->dmabuf
;
2789 return put_user(dmabuf
->rate
, (int *)arg
);
2791 return put_user(0, (int *)arg
);
2794 case SOUND_PCM_READ_CHANNELS
:
2795 state
= (struct cs_state
*)card
->states
[0];
2798 dmabuf
= &state
->dmabuf
;
2799 return put_user((dmabuf
->fmt
& CS_FMT_STEREO
) ? 2 : 1,
2802 return put_user(0, (int *)arg
);
2804 case SOUND_PCM_READ_BITS
:
2805 state
= (struct cs_state
*)card
->states
[0];
2808 dmabuf
= &state
->dmabuf
;
2809 return put_user(AFMT_S16_LE
| AFMT_U8
, (int *)arg
);
2811 return put_user(0, (int *)arg
);
2813 case SNDCTL_DSP_MAPINBUF
:
2814 case SNDCTL_DSP_MAPOUTBUF
:
2815 case SNDCTL_DSP_SETSYNCRO
:
2816 case SOUND_PCM_WRITE_FILTER
:
2817 case SOUND_PCM_READ_FILTER
:
2825 * AMP control - null AMP
2828 static void amp_none(struct cs_card
*card
, int change
)
2836 static void amp_voyetra(struct cs_card
*card
, int change
)
2838 /* Manage the EAPD bit on the Crystal 4297
2839 and the Analog AD1885 */
2841 int old
=card
->amplifier
;
2843 card
->amplifier
+=change
;
2844 if(card
->amplifier
&& !old
)
2846 /* Turn the EAPD amp on */
2847 cs_ac97_set(card
->ac97_codec
[0], AC97_POWER_CONTROL
,
2848 cs_ac97_get(card
->ac97_codec
[0], AC97_POWER_CONTROL
) |
2851 else if(old
&& !card
->amplifier
)
2853 /* Turn the EAPD amp off */
2854 cs_ac97_set(card
->ac97_codec
[0], AC97_POWER_CONTROL
,
2855 cs_ac97_get(card
->ac97_codec
[0], AC97_POWER_CONTROL
) &
2866 static void amp_voyetra_4294(struct cs_card
*card
, int change
)
2868 struct ac97_codec
*c
=card
->ac97_codec
[0];
2870 card
->amplifier
+=change
;
2874 /* Switch the GPIO pins 7 and 8 to open drain */
2875 cs_ac97_set(c
, 0x4C, cs_ac97_get(c
, 0x4C) & 0xFE7F);
2876 cs_ac97_set(c
, 0x4E, cs_ac97_get(c
, 0x4E) | 0x0180);
2877 /* Now wake the AMP (this might be backwards) */
2878 cs_ac97_set(c
, 0x54, cs_ac97_get(c
, 0x54) & ~0x0180);
2882 cs_ac97_set(c
, 0x54, cs_ac97_get(c
, 0x54) | 0x0180);
2887 * Handle the CLKRUN on a thinkpad. We must disable CLKRUN support
2888 * whenever we need to beat on the chip.
2890 * The original idea and code for this hack comes from David Kaiser at
2891 * Linuxcare. Perhaps one day Crystal will document their chips well
2892 * enough to make them useful.
2895 static void clkrun_hack(struct cs_card
*card
, int change
)
2897 struct pci_dev
*acpi_dev
;
2901 int old
=card
->amplifier
;
2903 card
->amplifier
+=change
;
2905 acpi_dev
= pci_find_device(PCI_VENDOR_ID_INTEL
, PCI_DEVICE_ID_INTEL_82371AB_3
, NULL
);
2906 if(acpi_dev
== NULL
)
2907 return; /* Not a thinkpad thats for sure */
2909 /* Find the control port */
2910 pci_read_config_byte(acpi_dev
, 0x41, &pp
);
2913 /* Read ACPI port */
2914 control
=inw(port
+0x10);
2916 /* Flip CLKRUN off while running */
2917 if(!card
->amplifier
&& old
)
2918 outw(control
|0x2000, port
+0x10);
2919 else if(card
->amplifier
&& !old
)
2920 outw(control
&~0x2000, port
+0x10);
2924 static int cs_open(struct inode
*inode
, struct file
*file
)
2926 struct cs_card
*card
= devs
;
2927 struct cs_state
*state
= NULL
;
2928 struct dmabuf
*dmabuf
= NULL
;
2931 CS_DBGOUT(CS_OPEN
| CS_FUNCTION
, 2, printk("cs46xx: cs_open()+ file=0x%x %s %s\n",
2932 (unsigned)file
, file
->f_mode
& FMODE_WRITE
? "FMODE_WRITE" : "",
2933 file
->f_mode
& FMODE_READ
? "FMODE_READ" : "") );
2936 * hardcode state[0] for capture, [1] for playback
2938 if(file
->f_mode
& FMODE_READ
)
2940 CS_DBGOUT(CS_WAVE_READ
, 2, printk("cs46xx: cs_open() FMODE_READ\n") );
2941 if (card
->states
[0] == NULL
) {
2942 state
= card
->states
[0] = (struct cs_state
*)
2943 kmalloc(sizeof(struct cs_state
), GFP_KERNEL
);
2946 memset(state
, 0, sizeof(struct cs_state
));
2947 dmabuf
= &state
->dmabuf
;
2948 dmabuf
->pbuf
= (void *)get_free_page(GFP_KERNEL
| GFP_DMA
);
2949 if(dmabuf
->pbuf
==NULL
)
2952 card
->states
[0]=NULL
;
2958 state
= card
->states
[0];
2959 if(state
->open_mode
& FMODE_READ
)
2962 dmabuf
->channel
= card
->alloc_rec_pcm_channel(card
);
2964 if (dmabuf
->channel
== NULL
) {
2965 kfree (card
->states
[0]);
2966 card
->states
[0] = NULL
;;
2970 /* Now turn on external AMP if needed */
2972 state
->card
->active_ctrl(state
->card
,1);
2973 state
->card
->amplifier_ctrl(state
->card
,1);
2975 dmabuf
->channel
->state
= state
;
2976 /* initialize the virtual channel */
2978 state
->magic
= CS_STATE_MAGIC
;
2979 init_waitqueue_head(&dmabuf
->wait
);
2980 init_MUTEX(&state
->open_sem
);
2981 file
->private_data
= card
;
2983 down(&state
->open_sem
);
2985 /* set default sample format. According to OSS Programmer's Guide /dev/dsp
2986 should be default to unsigned 8-bits, mono, with sample rate 8kHz and
2987 /dev/dspW will accept 16-bits sample */
2989 /* Default input is 8bit mono */
2990 dmabuf
->fmt
&= ~CS_FMT_MASK
;
2991 dmabuf
->type
= CS_TYPE_ADC
;
2992 dmabuf
->ossfragshift
= 0;
2993 dmabuf
->ossmaxfrags
= 0;
2994 dmabuf
->subdivision
= 0;
2995 cs_set_adc_rate(state
, 8000);
2996 cs_set_divisor(dmabuf
);
2998 state
->open_mode
|= FMODE_READ
;
2999 up(&state
->open_sem
);
3002 if(file
->f_mode
& FMODE_WRITE
)
3004 CS_DBGOUT(CS_OPEN
, 2, printk("cs46xx: cs_open() FMODE_WRITE\n") );
3005 if (card
->states
[1] == NULL
) {
3006 state
= card
->states
[1] = (struct cs_state
*)
3007 kmalloc(sizeof(struct cs_state
), GFP_KERNEL
);
3010 memset(state
, 0, sizeof(struct cs_state
));
3011 dmabuf
= &state
->dmabuf
;
3012 dmabuf
->pbuf
= (void *)get_free_page(GFP_KERNEL
| GFP_DMA
);
3013 if(dmabuf
->pbuf
==NULL
)
3016 card
->states
[1]=NULL
;
3022 state
= card
->states
[1];
3023 if(state
->open_mode
& FMODE_WRITE
)
3026 dmabuf
->channel
= card
->alloc_pcm_channel(card
);
3028 if (dmabuf
->channel
== NULL
) {
3029 kfree (card
->states
[1]);
3030 card
->states
[1] = NULL
;;
3034 /* Now turn on external AMP if needed */
3036 state
->card
->active_ctrl(state
->card
,1);
3037 state
->card
->amplifier_ctrl(state
->card
,1);
3039 dmabuf
->channel
->state
= state
;
3040 /* initialize the virtual channel */
3042 state
->magic
= CS_STATE_MAGIC
;
3043 init_waitqueue_head(&dmabuf
->wait
);
3044 init_MUTEX(&state
->open_sem
);
3045 file
->private_data
= card
;
3047 down(&state
->open_sem
);
3049 /* set default sample format. According to OSS Programmer's Guide /dev/dsp
3050 should be default to unsigned 8-bits, mono, with sample rate 8kHz and
3051 /dev/dspW will accept 16-bits sample */
3053 /* Default output is 8bit mono. */
3054 dmabuf
->fmt
&= ~CS_FMT_MASK
;
3055 dmabuf
->type
= CS_TYPE_DAC
;
3056 dmabuf
->ossfragshift
= 0;
3057 dmabuf
->ossmaxfrags
= 0;
3058 dmabuf
->subdivision
= 0;
3059 cs_set_dac_rate(state
, 8000);
3060 cs_set_divisor(dmabuf
);
3062 state
->open_mode
|= FMODE_WRITE
;
3063 up(&state
->open_sem
);
3065 if((ret
= prog_dmabuf(state
)))
3069 CS_DBGOUT(CS_OPEN
| CS_FUNCTION
, 2, printk("cs46xx: cs_open()- 0\n") );
3073 static int cs_release(struct inode
*inode
, struct file
*file
)
3075 struct cs_card
*card
= (struct cs_card
*)file
->private_data
;
3076 struct dmabuf
*dmabuf
;
3077 struct cs_state
*state
;
3078 CS_DBGOUT(CS_RELEASE
| CS_FUNCTION
, 2, printk("cs46xx: cs_release()+ file=0x%x %s %s\n",
3079 (unsigned)file
, file
->f_mode
& FMODE_WRITE
? "FMODE_WRITE" : "",
3080 file
->f_mode
& FMODE_READ
? "FMODE_READ" : "") );
3082 if (!(file
->f_mode
& (FMODE_WRITE
| FMODE_READ
)))
3086 state
= card
->states
[1];
3089 if ( (state
->open_mode
& FMODE_WRITE
) & (file
->f_mode
& FMODE_WRITE
) )
3091 CS_DBGOUT(CS_RELEASE
, 2, printk("cs46xx: cs_release() FMODE_WRITE\n") );
3092 dmabuf
= &state
->dmabuf
;
3093 cs_clear_tail(state
);
3094 drain_dac(state
, file
->f_flags
& O_NONBLOCK
);
3095 /* stop DMA state machine and free DMA buffers/channels */
3096 down(&state
->open_sem
);
3098 dealloc_dmabuf(state
);
3099 state
->card
->free_pcm_channel(state
->card
, dmabuf
->channel
->num
);
3100 free_page((unsigned long)state
->dmabuf
.pbuf
);
3102 /* we're covered by the open_sem */
3103 up(&state
->open_sem
);
3104 state
->card
->states
[state
->virt
] = NULL
;
3105 state
->open_mode
&= (~file
->f_mode
) & (FMODE_READ
|FMODE_WRITE
);
3107 /* Now turn off external AMP if needed */
3108 state
->card
->amplifier_ctrl(state
->card
, -1);
3109 state
->card
->active_ctrl(state
->card
, -1);
3116 state
= card
->states
[0];
3119 if ( (state
->open_mode
& FMODE_READ
) & (file
->f_mode
& FMODE_READ
) )
3121 CS_DBGOUT(CS_RELEASE
, 2, printk("cs46xx: cs_release() FMODE_READ\n") );
3122 dmabuf
= &state
->dmabuf
;
3123 down(&state
->open_sem
);
3125 dealloc_dmabuf(state
);
3126 state
->card
->free_pcm_channel(state
->card
, dmabuf
->channel
->num
);
3127 free_page((unsigned long)state
->dmabuf
.pbuf
);
3129 /* we're covered by the open_sem */
3130 up(&state
->open_sem
);
3131 state
->card
->states
[state
->virt
] = NULL
;
3132 state
->open_mode
&= (~file
->f_mode
) & (FMODE_READ
|FMODE_WRITE
);
3134 /* Now turn off external AMP if needed */
3135 state
->card
->amplifier_ctrl(state
->card
, -1);
3136 state
->card
->active_ctrl(state
->card
, -1);
3143 CS_DBGOUT(CS_FUNCTION
| CS_RELEASE
, 2, printk("cs46xx: cs_release()- 0\n") );
3147 static /*const*/ struct file_operations cs461x_fops
= {
3155 release
: cs_release
,
3158 /* Write AC97 codec registers */
3161 static u16
cs_ac97_get(struct ac97_codec
*dev
, u8 reg
)
3163 struct cs_card
*card
= dev
->private_data
;
3167 * 1. Write ACCAD = Command Address Register = 46Ch for AC97 register address
3168 * 2. Write ACCDA = Command Data Register = 470h for data to write to AC97
3169 * 3. Write ACCTL = Control Register = 460h for initiating the write
3170 * 4. Read ACCTL = 460h, DCV should be reset by now and 460h = 17h
3171 * 5. if DCV not cleared, break and return error
3172 * 6. Read ACSTS = Status Register = 464h, check VSTS bit
3176 cs461x_peekBA0(card
, BA0_ACSDA
);
3179 * Setup the AC97 control registers on the CS461x to send the
3180 * appropriate command to the AC97 to perform the read.
3181 * ACCAD = Command Address Register = 46Ch
3182 * ACCDA = Command Data Register = 470h
3183 * ACCTL = Control Register = 460h
3184 * set DCV - will clear when process completed
3185 * set CRW - Read command
3186 * set VFRM - valid frame enabled
3187 * set ESYN - ASYNC generation enabled
3188 * set RSTN - ARST# inactive, AC97 codec not reset
3191 cs461x_pokeBA0(card
, BA0_ACCAD
, reg
);
3192 cs461x_pokeBA0(card
, BA0_ACCDA
, 0);
3193 cs461x_pokeBA0(card
, BA0_ACCTL
, ACCTL_DCV
| ACCTL_CRW
|
3194 ACCTL_VFRM
| ACCTL_ESYN
|
3199 * Wait for the read to occur.
3201 for (count
= 0; count
< 500; count
++) {
3203 * First, we want to wait for a short time.
3207 * Now, check to see if the read has completed.
3208 * ACCTL = 460h, DCV should be reset by now and 460h = 17h
3210 if (!(cs461x_peekBA0(card
, BA0_ACCTL
) & ACCTL_DCV
))
3215 * Make sure the read completed.
3217 if (cs461x_peekBA0(card
, BA0_ACCTL
) & ACCTL_DCV
) {
3218 printk(KERN_WARNING
"cs461x: AC'97 read problem (ACCTL_DCV), reg = 0x%x\n", reg
);
3223 * Wait for the valid status bit to go active.
3225 for (count
= 0; count
< 100; count
++) {
3227 * Read the AC97 status register.
3228 * ACSTS = Status Register = 464h
3229 * VSTS - Valid Status
3231 if (cs461x_peekBA0(card
, BA0_ACSTS
) & ACSTS_VSTS
)
3237 * Make sure we got valid status.
3239 if (!(cs461x_peekBA0(card
, BA0_ACSTS
) & ACSTS_VSTS
)) {
3240 printk(KERN_WARNING
"cs461x: AC'97 read problem (ACSTS_VSTS), reg = 0x%x\n", reg
);
3245 * Read the data returned from the AC97 register.
3246 * ACSDA = Status Data Register = 474h
3249 printk("e) reg = 0x%x, val = 0x%x, BA0_ACCAD = 0x%x\n", reg
,
3250 cs461x_peekBA0(card
, BA0_ACSDA
),
3251 cs461x_peekBA0(card
, BA0_ACCAD
));
3253 return cs461x_peekBA0(card
, BA0_ACSDA
);
3256 static void cs_ac97_set(struct ac97_codec
*dev
, u8 reg
, u16 val
)
3258 struct cs_card
*card
= dev
->private_data
;
3262 if(reg
== AC97_CD_VOL
)
3264 val2
= cs_ac97_get(dev
, AC97_CD_VOL
);
3268 * 1. Write ACCAD = Command Address Register = 46Ch for AC97 register address
3269 * 2. Write ACCDA = Command Data Register = 470h for data to write to AC97
3270 * 3. Write ACCTL = Control Register = 460h for initiating the write
3271 * 4. Read ACCTL = 460h, DCV should be reset by now and 460h = 07h
3272 * 5. if DCV not cleared, break and return error
3276 * Setup the AC97 control registers on the CS461x to send the
3277 * appropriate command to the AC97 to perform the read.
3278 * ACCAD = Command Address Register = 46Ch
3279 * ACCDA = Command Data Register = 470h
3280 * ACCTL = Control Register = 460h
3281 * set DCV - will clear when process completed
3282 * reset CRW - Write command
3283 * set VFRM - valid frame enabled
3284 * set ESYN - ASYNC generation enabled
3285 * set RSTN - ARST# inactive, AC97 codec not reset
3287 cs461x_pokeBA0(card
, BA0_ACCAD
, reg
);
3288 cs461x_pokeBA0(card
, BA0_ACCDA
, val
);
3289 cs461x_pokeBA0(card
, BA0_ACCTL
, ACCTL_DCV
| ACCTL_VFRM
|
3290 ACCTL_ESYN
| ACCTL_RSTN
);
3291 for (count
= 0; count
< 1000; count
++) {
3293 * First, we want to wait for a short time.
3297 * Now, check to see if the write has completed.
3298 * ACCTL = 460h, DCV should be reset by now and 460h = 07h
3300 if (!(cs461x_peekBA0(card
, BA0_ACCTL
) & ACCTL_DCV
))
3304 * Make sure the write completed.
3306 if (cs461x_peekBA0(card
, BA0_ACCTL
) & ACCTL_DCV
)
3307 printk(KERN_WARNING
"cs461x: AC'97 write problem, reg = 0x%x, val = 0x%x\n", reg
, val
);
3310 * Adjust power if the mixer is selected/deselected according
3313 * IF the CD is a valid input source (mixer or direct) AND
3314 * the CD is not muted THEN power is needed
3316 * We do two things. When record select changes the input to
3317 * add/remove the CD we adjust the power count if the CD is
3320 * When the CD mute changes we adjust the power level if the
3321 * CD was a valid input.
3323 * We also check for CD volume != 0, as the CD mute isn't
3324 * normally tweaked from userspace.
3327 /* CD mute change ? */
3329 if(reg
==AC97_CD_VOL
)
3331 /* Mute bit change ? */
3332 if((val2
^val
)&0x8000 || ((val2
== 0x1f1f || val
== 0x1f1f) && val2
!= val
))
3334 /* This is a hack but its cleaner than the alternatives.
3335 Right now card->ac97_codec[0] might be NULL as we are
3336 still doing codec setup. This does an early assignment
3337 to avoid the problem if it occurs */
3339 if(card
->ac97_codec
[0]==NULL
)
3340 card
->ac97_codec
[0]=dev
;
3343 if(val
&0x8000 || val
== 0x1f1f)
3344 card
->amplifier_ctrl(card
, -1);
3345 else /* Mute off power on */
3346 card
->amplifier_ctrl(card
, 1);
3352 /* OSS /dev/mixer file operation methods */
3354 static int cs_open_mixdev(struct inode
*inode
, struct file
*file
)
3357 int minor
= MINOR(inode
->i_rdev
);
3358 struct cs_card
*card
= devs
;
3360 for (card
= devs
; card
!= NULL
; card
= card
->next
)
3361 for (i
= 0; i
< NR_AC97
; i
++)
3362 if (card
->ac97_codec
[i
] != NULL
&&
3363 card
->ac97_codec
[i
]->dev_mixer
== minor
)
3370 file
->private_data
= card
->ac97_codec
[i
];
3372 card
->active_ctrl(card
,1);
3377 static int cs_release_mixdev(struct inode
*inode
, struct file
*file
)
3379 int minor
= MINOR(inode
->i_rdev
);
3380 struct cs_card
*card
= devs
;
3383 for (card
= devs
; card
!= NULL
; card
= card
->next
)
3384 for (i
= 0; i
< NR_AC97
; i
++)
3385 if (card
->ac97_codec
[i
] != NULL
&&
3386 card
->ac97_codec
[i
]->dev_mixer
== minor
)
3392 card
->active_ctrl(card
, -1);
3397 static int cs_ioctl_mixdev(struct inode
*inode
, struct file
*file
, unsigned int cmd
,
3400 struct ac97_codec
*codec
= (struct ac97_codec
*)file
->private_data
;
3402 #if CSDEBUG_INTERFACE
3405 if( (cmd
== SOUND_MIXER_CS_GETDBGMASK
) ||
3406 (cmd
== SOUND_MIXER_CS_SETDBGMASK
) ||
3407 (cmd
== SOUND_MIXER_CS_GETDBGLEVEL
) ||
3408 (cmd
== SOUND_MIXER_CS_SETDBGLEVEL
) )
3413 case SOUND_MIXER_CS_GETDBGMASK
:
3414 return put_user(cs_debugmask
, (unsigned long *)arg
);
3416 case SOUND_MIXER_CS_GETDBGLEVEL
:
3417 return put_user(cs_debuglevel
, (unsigned long *)arg
);
3419 case SOUND_MIXER_CS_SETDBGMASK
:
3420 if (get_user(val
, (unsigned long *)arg
))
3425 case SOUND_MIXER_CS_SETDBGLEVEL
:
3426 if (get_user(val
, (unsigned long *)arg
))
3428 cs_debuglevel
= val
;
3431 CS_DBGOUT(CS_ERROR
, 1, printk(KERN_INFO
3432 "cs4281: mixer_ioctl(): ERROR unknown debug cmd\n") );
3437 return codec
->mixer_ioctl(codec
, cmd
, arg
);
3440 static /*const*/ struct file_operations cs_mixer_fops
= {
3442 ioctl
: cs_ioctl_mixdev
,
3443 open
: cs_open_mixdev
,
3444 release
: cs_release_mixdev
,
3447 /* AC97 codec initialisation. */
3448 static int __init
cs_ac97_init(struct cs_card
*card
)
3452 struct ac97_codec
*codec
;
3455 for (num_ac97
= 0; num_ac97
< NR_AC97
; num_ac97
++) {
3456 if ((codec
= kmalloc(sizeof(struct ac97_codec
), GFP_KERNEL
)) == NULL
)
3458 memset(codec
, 0, sizeof(struct ac97_codec
));
3460 /* initialize some basic codec information, other fields will be filled
3461 in ac97_probe_codec */
3462 codec
->private_data
= card
;
3463 codec
->id
= num_ac97
;
3465 codec
->codec_read
= cs_ac97_get
;
3466 codec
->codec_write
= cs_ac97_set
;
3468 if (ac97_probe_codec(codec
) == 0)
3471 eid
= cs_ac97_get(codec
, AC97_EXTENDED_ID
);
3475 printk(KERN_WARNING
"cs461x: no codec attached ?\n");
3480 card
->ac97_features
= eid
;
3482 if ((codec
->dev_mixer
= register_sound_mixer(&cs_mixer_fops
, -1)) < 0) {
3483 printk(KERN_ERR
"cs461x: couldn't register mixer!\n");
3488 card
->ac97_codec
[num_ac97
] = codec
;
3490 /* if there is no secondary codec at all, don't probe any more */
3498 * load the static image into the DSP
3500 #include "cs461x_image.h"
3501 static void cs461x_download_image(struct cs_card
*card
)
3503 unsigned i
, j
, temp1
, temp2
, offset
, count
;
3504 unsigned char *pBA1
= ioremap(card
->ba1_addr
, 0x40000);
3505 for( i
=0; i
< CLEAR__COUNT
; i
++)
3507 offset
= ClrStat
[i
].BA1__DestByteOffset
;
3508 count
= ClrStat
[i
].BA1__SourceSize
;
3509 for( temp1
= offset
; temp1
<(offset
+count
); temp1
+=4 );
3510 writel(0, pBA1
+temp1
);
3513 for(i
=0; i
<FILL__COUNT
; i
++)
3515 temp2
= FillStat
[i
].Offset
;
3516 for(j
=0; j
<(FillStat
[i
].Size
)/4; j
++)
3518 temp1
= (FillStat
[i
]).pFill
[j
];
3519 writel(temp1
, pBA1
+temp2
+j
*4);
3530 static void cs461x_reset(struct cs_card
*card
)
3535 * Write the reset bit of the SP control register.
3537 cs461x_poke(card
, BA1_SPCR
, SPCR_RSTSP
);
3540 * Write the control register.
3542 cs461x_poke(card
, BA1_SPCR
, SPCR_DRQEN
);
3545 * Clear the trap registers.
3547 for (idx
= 0; idx
< 8; idx
++) {
3548 cs461x_poke(card
, BA1_DREG
, DREG_REGID_TRAP_SELECT
+ idx
);
3549 cs461x_poke(card
, BA1_TWPR
, 0xFFFF);
3551 cs461x_poke(card
, BA1_DREG
, 0);
3554 * Set the frame timer to reflect the number of cycles per frame.
3556 cs461x_poke(card
, BA1_FRMT
, 0xadf);
3559 static void cs461x_clear_serial_FIFOs(struct cs_card
*card
)
3561 int idx
, loop
, powerdown
= 0;
3565 * See if the devices are powered down. If so, we must power them up first
3566 * or they will not respond.
3568 if (!((tmp
= cs461x_peekBA0(card
, BA0_CLKCR1
)) & CLKCR1_SWCE
)) {
3569 cs461x_pokeBA0(card
, BA0_CLKCR1
, tmp
| CLKCR1_SWCE
);
3574 * We want to clear out the serial port FIFOs so we don't end up playing
3575 * whatever random garbage happens to be in them. We fill the sample FIFOS
3576 * with zero (silence).
3578 cs461x_pokeBA0(card
, BA0_SERBWP
, 0);
3581 * Fill all 256 sample FIFO locations.
3583 for (idx
= 0; idx
< 256; idx
++) {
3585 * Make sure the previous FIFO write operation has completed.
3587 for (loop
= 0; loop
< 5; loop
++) {
3589 if (!(cs461x_peekBA0(card
, BA0_SERBST
) & SERBST_WBSY
))
3592 if (cs461x_peekBA0(card
, BA0_SERBST
) & SERBST_WBSY
) {
3594 cs461x_pokeBA0(card
, BA0_CLKCR1
, tmp
);
3597 * Write the serial port FIFO index.
3599 cs461x_pokeBA0(card
, BA0_SERBAD
, idx
);
3601 * Tell the serial port to load the new value into the FIFO location.
3603 cs461x_pokeBA0(card
, BA0_SERBCM
, SERBCM_WRC
);
3606 * Now, if we powered up the devices, then power them back down again.
3607 * This is kinda ugly, but should never happen.
3610 cs461x_pokeBA0(card
, BA0_CLKCR1
, tmp
);
3613 static void cs461x_powerup_dac(struct cs_card
*card
)
3619 * Power on the DACs on the AC97 card. We turn off the DAC
3620 * powerdown bit and write the new value of the power control
3623 tmp
= cs_ac97_get(card
->ac97_codec
[0], AC97_POWER_CONTROL
);
3624 if (tmp
& 2) /* already */
3626 cs_ac97_set(card
->ac97_codec
[0], AC97_POWER_CONTROL
, tmp
& 0xfdff);
3629 * Now, we wait until we sample a DAC ready state.
3631 for (count
= 0; count
< 32; count
++) {
3633 * First, lets wait a short while to let things settle out a
3634 * bit, and to prevent retrying the read too quickly.
3639 * Read the current state of the power control register.
3641 if (cs_ac97_get(card
->ac97_codec
[0], AC97_POWER_CONTROL
) & 2)
3646 * Check the status..
3648 if (!(cs_ac97_get(card
->ac97_codec
[0], AC97_POWER_CONTROL
) & 2))
3649 printk(KERN_WARNING
"cs461x: powerup DAC failed\n");
3652 static void cs461x_powerup_adc(struct cs_card
*card
)
3658 * Power on the ADCs on the AC97 card. We turn off the DAC
3659 * powerdown bit and write the new value of the power control
3662 tmp
= cs_ac97_get(card
->ac97_codec
[0], AC97_POWER_CONTROL
);
3663 if (tmp
& 1) /* already */
3665 cs_ac97_set(card
->ac97_codec
[0], AC97_POWER_CONTROL
, tmp
& 0xfeff);
3668 * Now, we wait until we sample a ADC ready state.
3670 for (count
= 0; count
< 32; count
++) {
3672 * First, lets wait a short while to let things settle out a
3673 * bit, and to prevent retrying the read too quickly.
3678 * Read the current state of the power control register.
3680 if (cs_ac97_get(card
->ac97_codec
[0], AC97_POWER_CONTROL
) & 1)
3685 * Check the status..
3687 if (!(cs_ac97_get(card
->ac97_codec
[0], AC97_POWER_CONTROL
) & 1))
3688 printk(KERN_WARNING
"cs461x: powerup ADC failed\n");
3691 static void cs461x_proc_start(struct cs_card
*card
)
3696 * Set the frame timer to reflect the number of cycles per frame.
3698 cs461x_poke(card
, BA1_FRMT
, 0xadf);
3700 * Turn on the run, run at frame, and DMA enable bits in the local copy of
3701 * the SP control register.
3703 cs461x_poke(card
, BA1_SPCR
, SPCR_RUN
| SPCR_RUNFR
| SPCR_DRQEN
);
3705 * Wait until the run at frame bit resets itself in the SP control
3708 for (cnt
= 0; cnt
< 25; cnt
++) {
3710 if (!(cs461x_peek(card
, BA1_SPCR
) & SPCR_RUNFR
))
3714 if (cs461x_peek(card
, BA1_SPCR
) & SPCR_RUNFR
)
3715 printk(KERN_WARNING
"cs461x: SPCR_RUNFR never reset\n");
3718 static void cs461x_proc_stop(struct cs_card
*card
)
3721 * Turn off the run, run at frame, and DMA enable bits in the local copy of
3722 * the SP control register.
3724 cs461x_poke(card
, BA1_SPCR
, 0);
3729 static int cs_hardware_init(struct cs_card
*card
)
3731 unsigned long end_time
;
3735 * First, blast the clock control register to zero so that the PLL starts
3736 * out in a known state, and blast the master serial port control register
3737 * to zero so that the serial ports also start out in a known state.
3739 cs461x_pokeBA0(card
, BA0_CLKCR1
, 0);
3740 cs461x_pokeBA0(card
, BA0_SERMC1
, 0);
3743 * If we are in AC97 mode, then we must set the part to a host controlled
3744 * AC-link. Otherwise, we won't be able to bring up the link.
3746 cs461x_pokeBA0(card
, BA0_SERACC
, SERACC_HSP
| SERACC_CODEC_TYPE_1_03
); /* 1.03 card */
3747 /* cs461x_pokeBA0(card, BA0_SERACC, SERACC_HSP | SERACC_CODEC_TYPE_2_0); */ /* 2.00 card */
3750 * Drive the ARST# pin low for a minimum of 1uS (as defined in the AC97
3751 * spec) and then drive it high. This is done for non AC97 modes since
3752 * there might be logic external to the CS461x that uses the ARST# line
3755 cs461x_pokeBA0(card
, BA0_ACCTL
, 0);
3757 cs461x_pokeBA0(card
, BA0_ACCTL
, ACCTL_RSTN
);
3760 * The first thing we do here is to enable sync generation. As soon
3761 * as we start receiving bit clock, we'll start producing the SYNC
3764 cs461x_pokeBA0(card
, BA0_ACCTL
, ACCTL_ESYN
| ACCTL_RSTN
);
3767 * Now wait for a short while to allow the AC97 part to start
3768 * generating bit clock (so we don't try to start the PLL without an
3771 mdelay(5); /* 1 should be enough ?? (and pigs might fly) */
3774 * Set the serial port timing configuration, so that
3775 * the clock control circuit gets its clock from the correct place.
3777 cs461x_pokeBA0(card
, BA0_SERMC1
, SERMC1_PTC_AC97
);
3780 * Write the selected clock control setup to the hardware. Do not turn on
3781 * SWCE yet (if requested), so that the devices clocked by the output of
3782 * PLL are not clocked until the PLL is stable.
3784 cs461x_pokeBA0(card
, BA0_PLLCC
, PLLCC_LPF_1050_2780_KHZ
| PLLCC_CDR_73_104_MHZ
);
3785 cs461x_pokeBA0(card
, BA0_PLLM
, 0x3a);
3786 cs461x_pokeBA0(card
, BA0_CLKCR2
, CLKCR2_PDIVS_8
);
3791 cs461x_pokeBA0(card
, BA0_CLKCR1
, CLKCR1_PLLP
);
3794 * Wait until the PLL has stabilized.
3796 mdelay(5); /* Again 1 should be enough ?? */
3799 * Turn on clocking of the core so that we can setup the serial ports.
3801 tmp
= cs461x_peekBA0(card
, BA0_CLKCR1
) | CLKCR1_SWCE
;
3802 cs461x_pokeBA0(card
, BA0_CLKCR1
, tmp
);
3805 * Fill the serial port FIFOs with silence.
3807 cs461x_clear_serial_FIFOs(card
);
3810 * Set the serial port FIFO pointer to the first sample in the FIFO.
3812 /* cs461x_pokeBA0(card, BA0_SERBSP, 0); */
3815 * Write the serial port configuration to the part. The master
3816 * enable bit is not set until all other values have been written.
3818 cs461x_pokeBA0(card
, BA0_SERC1
, SERC1_SO1F_AC97
| SERC1_SO1EN
);
3819 cs461x_pokeBA0(card
, BA0_SERC2
, SERC2_SI1F_AC97
| SERC1_SO1EN
);
3820 cs461x_pokeBA0(card
, BA0_SERMC1
, SERMC1_PTC_AC97
| SERMC1_MSPE
);
3823 mdelay(5); /* Shouldnt be needed ?? */
3826 * Wait for the card ready signal from the AC97 card.
3828 end_time
= jiffies
+ 3 * (HZ
>> 2);
3831 * Read the AC97 status register to see if we've seen a CODEC READY
3832 * signal from the AC97 card.
3834 if (cs461x_peekBA0(card
, BA0_ACSTS
) & ACSTS_CRDY
)
3836 current
->state
= TASK_UNINTERRUPTIBLE
;
3837 schedule_timeout(1);
3838 } while (time_before(jiffies
, end_time
));
3841 * Make sure CODEC is READY.
3843 if (!(cs461x_peekBA0(card
, BA0_ACSTS
) & ACSTS_CRDY
)) {
3844 printk(KERN_WARNING
"cs461x: create - never read card ready from AC'97\n");
3845 printk(KERN_WARNING
"cs461x: it is probably not a bug, try using the CS4232 driver\n");
3850 * Assert the vaid frame signal so that we can start sending commands
3853 cs461x_pokeBA0(card
, BA0_ACCTL
, ACCTL_VFRM
| ACCTL_ESYN
| ACCTL_RSTN
);
3856 * Wait until we've sampled input slots 3 and 4 as valid, meaning that
3857 * the card is pumping ADC data across the AC-link.
3859 end_time
= jiffies
+ 3 * (HZ
>> 2);
3862 * Read the input slot valid register and see if input slots 3 and
3865 if ((cs461x_peekBA0(card
, BA0_ACISV
) & (ACISV_ISV3
| ACISV_ISV4
)) == (ACISV_ISV3
| ACISV_ISV4
))
3867 current
->state
= TASK_UNINTERRUPTIBLE
;
3868 schedule_timeout(1);
3869 } while (time_before(jiffies
, end_time
));
3872 * Make sure input slots 3 and 4 are valid. If not, then return
3875 if ((cs461x_peekBA0(card
, BA0_ACISV
) & (ACISV_ISV3
| ACISV_ISV4
)) != (ACISV_ISV3
| ACISV_ISV4
)) {
3876 printk(KERN_WARNING
"cs461x: create - never read ISV3 & ISV4 from AC'97\n");
3881 * Now, assert valid frame and the slot 3 and 4 valid bits. This will
3882 * commense the transfer of digital audio data to the AC97 card.
3884 cs461x_pokeBA0(card
, BA0_ACOSV
, ACOSV_SLV3
| ACOSV_SLV4
);
3887 * Power down the DAC and ADC. We will power them up (if) when we need
3890 /* cs461x_pokeBA0(card, BA0_AC97_POWERDOWN, 0x300); */
3893 * Turn off the Processor by turning off the software clock enable flag in
3894 * the clock control register.
3896 /* tmp = cs461x_peekBA0(card, BA0_CLKCR1) & ~CLKCR1_SWCE; */
3897 /* cs461x_pokeBA0(card, BA0_CLKCR1, tmp); */
3900 * Reset the processor.
3905 * Download the image to the processor.
3908 cs461x_download_image(card
);
3911 * Stop playback DMA.
3913 tmp
= cs461x_peek(card
, BA1_PCTL
);
3914 card
->pctl
= tmp
& 0xffff0000;
3915 cs461x_poke(card
, BA1_PCTL
, tmp
& 0x0000ffff);
3920 tmp
= cs461x_peek(card
, BA1_CCTL
);
3921 card
->cctl
= tmp
& 0x0000ffff;
3922 cs461x_poke(card
, BA1_CCTL
, tmp
& 0xffff0000);
3924 /* initialize AC97 codec and register /dev/mixer */
3925 if (cs_ac97_init(card
) <= 0)
3928 mdelay(5); /* Do we need this ?? */
3930 cs461x_powerup_adc(card
);
3931 cs461x_powerup_dac(card
);
3933 cs461x_proc_start(card
);
3936 * Enable interrupts on the part.
3938 cs461x_pokeBA0(card
, BA0_HICR
, HICR_IEV
| HICR_CHGM
);
3940 tmp
= cs461x_peek(card
, BA1_PFIE
);
3942 cs461x_poke(card
, BA1_PFIE
, tmp
); /* playback interrupt enable */
3944 tmp
= cs461x_peek(card
, BA1_CIE
);
3947 cs461x_poke(card
, BA1_CIE
, tmp
); /* capture interrupt enable */
3951 /* install the driver, we do not allocate hardware channel nor DMA buffer now, they are defered
3952 untill "ACCESS" time (in prog_dmabuf called by open/read/write/ioctl/mmap) */
3964 void (*amp
)(struct cs_card
*, int);
3965 void (*active
)(struct cs_card
*, int);
3968 static struct cs_card_type __initdata cards
[]={
3969 {0x1489, 0x7001, "Genius Soundmaker 128 value", amp_none
, NULL
},
3970 {0x5053, 0x3357, "Voyetra", amp_voyetra
, NULL
},
3971 {0x1071, 0x6003, "Mitac MI6020/21", amp_voyetra
, NULL
},
3972 /* Not sure if the 570 needs the clkrun hack */
3973 {PCI_VENDOR_ID_IBM
, 0x0132, "Thinkpad 570", amp_none
, clkrun_hack
},
3974 {PCI_VENDOR_ID_IBM
, 0x0153, "Thinkpad 600X/A20/T20", amp_none
, clkrun_hack
},
3975 {PCI_VENDOR_ID_IBM
, 0x1010, "Thinkpad 600E (unsupported)", NULL
, NULL
},
3976 {0, 0, "Card without SSID set", NULL
, NULL
},
3977 {0, 0, NULL
, NULL
, NULL
}
3981 static int cs46xx_pm_callback(struct pm_dev
*dev
, pm_request_t rqst
, void *data
)
3983 struct cs_state
*state
= (struct cs_state
*) dev
->data
;
3988 printk( KERN_DEBUG
"cs46xx: PM resume request\n");
3989 cs_hardware_init(state
->card
);
3992 printk( KERN_DEBUG
"cs46xx: PM suspend request\n");
3994 resync_dma_ptrs(state
);
4003 static int __init
cs_install(struct pci_dev
*pci_dev
)
4005 struct cs_card
*card
;
4006 struct cs_card_type
*cp
= &cards
[0];
4008 struct pm_dev
*pmdev
;
4010 u16 ss_card
, ss_vendor
;
4013 pci_read_config_word(pci_dev
, PCI_SUBSYSTEM_VENDOR_ID
, &ss_vendor
);
4014 pci_read_config_word(pci_dev
, PCI_SUBSYSTEM_ID
, &ss_card
);
4016 if ((card
= kmalloc(sizeof(struct cs_card
), GFP_KERNEL
)) == NULL
) {
4017 printk(KERN_ERR
"cs461x: out of memory\n");
4020 memset(card
, 0, sizeof(*card
));
4022 card
->ba0_addr
= pci_dev
->resource
[0].start
&PCI_BASE_ADDRESS_MEM_MASK
;
4023 card
->ba1_addr
= pci_dev
->resource
[1].start
&PCI_BASE_ADDRESS_MEM_MASK
;
4024 card
->pci_dev
= pci_dev
;
4025 card
->irq
= pci_dev
->irq
;
4026 card
->magic
= CS_CARD_MAGIC
;
4027 spin_lock_init(&card
->lock
);
4029 pci_set_master(pci_dev
);
4031 printk(KERN_INFO
"cs461x: Card found at 0x%08lx and 0x%08lx, IRQ %d\n",
4032 card
->ba0_addr
, card
->ba1_addr
, card
->irq
);
4034 card
->alloc_pcm_channel
= cs_alloc_pcm_channel
;
4035 card
->alloc_rec_pcm_channel
= cs_alloc_rec_pcm_channel
;
4036 card
->free_pcm_channel
= cs_free_pcm_channel
;
4037 card
->amplifier_ctrl
= amp_none
;
4038 card
->active_ctrl
= amp_none
;
4042 if(cp
->vendor
== ss_vendor
&& cp
->id
== ss_card
)
4044 card
->amplifier_ctrl
= cp
->amp
;
4046 card
->active_ctrl
= cp
->active
;
4053 printk(KERN_INFO
"cs461x: Unknown card (%04X:%04X) at 0x%08lx/0x%08lx, IRQ %d\n",
4054 ss_vendor
, ss_card
, card
->ba0_addr
, card
->ba1_addr
, card
->irq
);
4058 printk(KERN_INFO
"cs461x: %s at 0x%08lx/0x%08lx, IRQ %d\n",
4059 cp
->name
, card
->ba0_addr
, card
->ba1_addr
, card
->irq
);
4062 if (card
->amplifier_ctrl
==NULL
)
4064 card
->amplifier_ctrl
= amp_none
;
4065 card
->active_ctrl
= clkrun_hack
;
4068 if (external_amp
== 1)
4070 printk(KERN_INFO
"cs461x: Crystal EAPD support forced on.\n");
4071 card
->amplifier_ctrl
= amp_voyetra
;
4076 card
->active_ctrl
= clkrun_hack
;
4077 printk(KERN_INFO
"cs461x: Activating CLKRUN hack for Thinkpad.\n");
4080 card
->active_ctrl(card
, 1);
4082 /* claim our iospace and irq */
4084 card
->ba0
= ioremap(card
->ba0_addr
, CS461X_BA0_SIZE
);
4085 card
->ba1
.name
.data0
= ioremap(card
->ba1_addr
+ BA1_SP_DMEM0
, CS461X_BA1_DATA0_SIZE
);
4086 card
->ba1
.name
.data1
= ioremap(card
->ba1_addr
+ BA1_SP_DMEM1
, CS461X_BA1_DATA1_SIZE
);
4087 card
->ba1
.name
.pmem
= ioremap(card
->ba1_addr
+ BA1_SP_PMEM
, CS461X_BA1_PRG_SIZE
);
4088 card
->ba1
.name
.reg
= ioremap(card
->ba1_addr
+ BA1_SP_REG
, CS461X_BA1_REG_SIZE
);
4090 CS_DBGOUT(CS_INIT
, 4, printk("card->ba0=0x%.08x\n",(unsigned)card
->ba0
) );
4091 CS_DBGOUT(CS_INIT
, 4, printk("card->ba1=0x%.08x 0x%.08x 0x%.08x 0x%.08x\n",
4092 (unsigned)card
->ba1
.name
.data0
,
4093 (unsigned)card
->ba1
.name
.data1
,
4094 (unsigned)card
->ba1
.name
.pmem
,
4095 (unsigned)card
->ba1
.name
.reg
) );
4097 if(card
->ba0
== 0 || card
->ba1
.name
.data0
== 0 ||
4098 card
->ba1
.name
.data1
== 0 || card
->ba1
.name
.pmem
== 0 ||
4099 card
->ba1
.name
.reg
== 0)
4102 if (request_irq(card
->irq
, &cs_interrupt
, SA_SHIRQ
, "cs461x", card
)) {
4103 printk(KERN_ERR
"cs461x: unable to allocate irq %d\n", card
->irq
);
4106 /* register /dev/dsp */
4107 if ((card
->dev_audio
= register_sound_dsp(&cs461x_fops
, -1)) < 0) {
4108 printk(KERN_ERR
"cs461x: unable to register dsp\n");
4112 /* register /dev/midi */
4113 if((card
->dev_midi
= register_sound_midi(&cs_midi_fops
, -1)) < 0)
4114 printk(KERN_ERR
"cs461x: unable to register midi\n");
4116 if (cs_hardware_init(card
)<0)
4118 unregister_sound_dsp(card
->dev_audio
);
4120 unregister_sound_midi(card
->dev_midi
);
4123 init_waitqueue_head(&card
->midi
.open_wait
);
4124 init_MUTEX(&card
->midi
.open_sem
);
4125 init_waitqueue_head(&card
->midi
.iwait
);
4126 init_waitqueue_head(&card
->midi
.owait
);
4129 cs461x_pokeBA0(card
, BA0_MIDCR
, MIDCR_MRST
);
4130 cs461x_pokeBA0(card
, BA0_MIDCR
, 0);
4132 card
->active_ctrl(card
, -1);
4134 pmdev
= pm_register(PM_PCI_DEV
, PM_PCI_ID(pci_dev
), cs46xx_pm_callback
);
4141 free_irq(card
->irq
, card
);
4145 if(card
->ba1
.name
.data0
)
4146 iounmap(card
->ba1
.name
.data0
);
4147 if(card
->ba1
.name
.data1
)
4148 iounmap(card
->ba1
.name
.data1
);
4149 if(card
->ba1
.name
.pmem
)
4150 iounmap(card
->ba1
.name
.pmem
);
4151 if(card
->ba1
.name
.reg
)
4152 iounmap(card
->ba1
.name
.reg
);
4158 static void cs_remove(struct cs_card
*card
)
4163 card
->active_ctrl(card
,1);
4165 tmp
= cs461x_peek(card
, BA1_PFIE
);
4168 cs461x_poke(card
, BA1_PFIE
, tmp
); /* playback interrupt disable */
4170 tmp
= cs461x_peek(card
, BA1_CIE
);
4173 cs461x_poke(card
, BA1_CIE
, tmp
); /* capture interrupt disable */
4176 * Stop playback DMA.
4178 tmp
= cs461x_peek(card
, BA1_PCTL
);
4179 cs461x_poke(card
, BA1_PCTL
, tmp
& 0x0000ffff);
4184 tmp
= cs461x_peek(card
, BA1_CCTL
);
4185 cs461x_poke(card
, BA1_CCTL
, tmp
& 0xffff0000);
4188 * Reset the processor.
4192 cs461x_proc_stop(card
);
4195 * Power down the DAC and ADC. We will power them up (if) when we need
4198 cs_ac97_set(card
->ac97_codec
[0], AC97_POWER_CONTROL
, 0x300);
4201 * Power down the PLL.
4203 cs461x_pokeBA0(card
, BA0_CLKCR1
, 0);
4206 * Turn off the Processor by turning off the software clock enable flag in
4207 * the clock control register.
4209 tmp
= cs461x_peekBA0(card
, BA0_CLKCR1
) & ~CLKCR1_SWCE
;
4210 cs461x_pokeBA0(card
, BA0_CLKCR1
, tmp
);
4212 card
->active_ctrl(card
,-1);
4214 /* free hardware resources */
4215 free_irq(card
->irq
, card
);
4217 iounmap(card
->ba1
.name
.data0
);
4218 iounmap(card
->ba1
.name
.data1
);
4219 iounmap(card
->ba1
.name
.pmem
);
4220 iounmap(card
->ba1
.name
.reg
);
4222 /* unregister audio devices */
4223 for (i
= 0; i
< NR_AC97
; i
++)
4224 if (card
->ac97_codec
[i
] != NULL
) {
4225 unregister_sound_mixer(card
->ac97_codec
[i
]->dev_mixer
);
4226 kfree (card
->ac97_codec
[i
]);
4228 unregister_sound_dsp(card
->dev_audio
);
4230 unregister_sound_midi(card
->dev_midi
);
4234 MODULE_AUTHOR("Alan Cox <alan@redhat.com>, Jaroslav Kysela, <audio@crystal.cirrus.com>");
4235 MODULE_DESCRIPTION("Crystal SoundFusion Audio Support");
4237 int __init
cs_probe(void)
4239 struct pci_dev
*pcidev
= NULL
;
4242 if (!pci_present()) /* No PCI bus in this machine! */
4245 printk(KERN_INFO
"Crystal 4280/461x + AC97 Audio, version "
4246 DRIVER_VERSION
", " __TIME__
" " __DATE__
"\n");
4248 while( (pcidev
= pci_find_device(PCI_VENDOR_ID_CIRRUS
, 0x6001 , pcidev
))!=NULL
) {
4249 if (cs_install(pcidev
)==0)
4252 while( (pcidev
= pci_find_device(PCI_VENDOR_ID_CIRRUS
, 0x6003 , pcidev
))!=NULL
) {
4253 if (cs_install(pcidev
)==0)
4256 while( (pcidev
= pci_find_device(PCI_VENDOR_ID_CIRRUS
, 0x6004 , pcidev
))!=NULL
) {
4257 if (cs_install(pcidev
)==0)
4261 printk(KERN_INFO
"cs461x: Found %d audio device(s).\n",
4266 int __init
init_module(void)
4269 printk(KERN_ERR
"cs461x: No devices found.\n");
4273 void __exit
cleanup_module (void)
4275 struct cs_card
*next
;
4277 pm_unregister_all(cs46xx_pm_callback
);
4287 MODULE_PARM(external_amp
, "i");
4288 MODULE_PARM(thinkpad
, "i");