Ok. I didn't make 2.4.0 in 2000. Tough. I tried, but we had some
[davej-history.git] / drivers / sound / cs46xx.c
blobd3ff7d71fe48887acac60199d107253884578827
1 /*
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>
29 * Nils Faerber (nf)
30 * <nils@kernelconcepts.de>
31 * Thanks to David Pollard for testing.
33 * Changes:
34 * 20000909-nf Changed cs_read, cs_write and drain_dac
35 * 20001025-tw Separate Playback/Capture structs and buffers.
36 * Added Scatter/Gather support for Playback.
37 * Added Capture.
38 * 20001027-nf Port to kernel 2.4.0-test9, some clean-ups
39 * Start of powermanagement support (CS46XX_PM).
40 * 20001128-tw Add module parm for default buffer order.
41 * added DMA_GFP flag to kmalloc dma buffer allocs.
42 * backfill silence to eliminate stuttering on
43 * underruns.
44 * 20001201-tw add resyncing of swptr on underruns.
45 * 20001205-tw-nf fixed GETOSPACE ioctl() after open()
48 * Status:
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>
64 #ifdef CS46XX_PM
65 #include <linux/pm.h>
66 #endif
67 #include <asm/io.h>
68 #include <asm/dma.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>
77 #include "cs461x.h"
80 /* MIDI buffer sizes */
81 #define CS_MIDIINBUF 500
82 #define CS_MIDIOUTBUF 500
84 #define ADC_RUNNING 1
85 #define DAC_RUNNING 2
87 #define CS_FMT_16BIT 1 /* These are fixed in fact */
88 #define CS_FMT_STEREO 2
89 #define CS_FMT_MASK 3
91 #define CS_TYPE_ADC 1
92 #define CS_TYPE_DAC 2
94 * CS461x definitions
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
106 #define CSDEBUG 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.
116 #if CSDEBUG
117 #define CS_DBGOUT(mask,level,x) if((cs_debuglevel >= (level)) && ((mask) & cs_debugmask)) {x;}
118 #else
119 #define CS_DBGOUT(mask,level,x)
120 #endif
122 * cs_debugmask areas
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 */
140 #if CSDEBUG
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 */
143 #endif
144 #define DMABUF_DEFAULTORDER 3
145 static unsigned long defaultorder=DMABUF_DEFAULTORDER;
146 #if MODULE
147 MODULE_PARM(defaultorder, "i");
148 MODULE_PARM(cs_debuglevel, "i");
149 MODULE_PARM(cs_debugmask, "i");
150 #endif
152 static int external_amp = 0;
153 static int thinkpad = 0;
156 /* An instance of the 4610 channel */
158 struct cs_channel
160 int used;
161 int num;
162 void *state;
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" */
170 #define NR_HW_CH 3
172 /* maxinum number of AC97 codecs connected, AC97 2.0 defined 4 */
173 #define NR_AC97 2
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 */
179 struct cs_state {
180 unsigned int magic;
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;
187 /* file mode */
188 mode_t open_mode;
190 /* virtual channel number */
191 int virt;
193 struct dmabuf {
194 /* wave sample stuff */
195 unsigned int rate;
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 */
204 void *rawbuf;
205 dma_addr_t dma_handle;
206 unsigned buforder;
207 unsigned numfrag;
208 unsigned fragshift;
209 unsigned divisor;
210 unsigned type;
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 */
227 unsigned fragsize;
228 unsigned dmasize;
229 unsigned fragsamples;
231 /* OSS stuff */
232 unsigned mapped:1;
233 unsigned ready:1;
234 unsigned endcleared:1;
235 unsigned SGok:1;
236 unsigned update_flag;
237 unsigned ossfragshift;
238 int ossmaxfrags;
239 unsigned subdivision;
240 } dmabuf;
244 struct cs_card {
245 struct cs_channel channel[2];
246 unsigned int magic;
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 */
253 spinlock_t lock;
255 /* PCI device stuff */
256 struct pci_dev * pci_dev;
258 unsigned int pctl, cctl; /* Hardware DMA flag sets */
260 /* soundcore stuff */
261 int dev_audio;
262 int dev_midi;
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];
268 u16 ac97_features;
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;
279 u32 irq;
281 /* mappings */
282 void *ba0;
283 union
285 struct
287 u8 *data0;
288 u8 *data1;
289 u8 *pmem;
290 u8 *reg;
291 } name;
292 u8 *idx[4];
293 } ba1;
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 */
301 struct {
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;
307 spinlock_t lock;
308 unsigned char ibuf[CS_MIDIINBUF];
309 unsigned char obuf[CS_MIDIOUTBUF];
310 mode_t open_mode;
311 struct semaphore open_sem;
312 } midi;
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,
320 unsigned long arg);
321 static loff_t cs_llseek(struct file *file, loff_t offset, int origin);
323 extern __inline__ unsigned ld2(unsigned int x)
325 unsigned r = 0;
327 if (x >= 0x10000) {
328 x >>= 16;
329 r += 16;
331 if (x >= 0x100) {
332 x >>= 8;
333 r += 8;
335 if (x >= 0x10) {
336 x >>= 4;
337 r += 4;
339 if (x >= 4) {
340 x >>= 2;
341 r += 2;
343 if (x >= 2)
344 r++;
345 return r;
348 #if CSDEBUG
350 /* DEBUG ROUTINES */
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)
364 unsigned int i;
365 unsigned char vidx;
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 */
383 switch(x)
385 case SOUND_MIXER_CS_GETDBGMASK:
386 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_CS_GETDBGMASK: ") );
387 break;
388 case SOUND_MIXER_CS_GETDBGLEVEL:
389 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_CS_GETDBGLEVEL: ") );
390 break;
391 case SOUND_MIXER_CS_SETDBGMASK:
392 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_CS_SETDBGMASK: ") );
393 break;
394 case SOUND_MIXER_CS_SETDBGLEVEL:
395 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_CS_SETDBGLEVEL: ") );
396 break;
397 case OSS_GETVERSION:
398 CS_DBGOUT(CS_IOCTL, 4, printk("OSS_GETVERSION: ") );
399 break;
400 case SNDCTL_DSP_SYNC:
401 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_SYNC: ") );
402 break;
403 case SNDCTL_DSP_SETDUPLEX:
404 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_SETDUPLEX: ") );
405 break;
406 case SNDCTL_DSP_GETCAPS:
407 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_GETCAPS: ") );
408 break;
409 case SNDCTL_DSP_RESET:
410 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_RESET: ") );
411 break;
412 case SNDCTL_DSP_SPEED:
413 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_SPEED: ") );
414 break;
415 case SNDCTL_DSP_STEREO:
416 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_STEREO: ") );
417 break;
418 case SNDCTL_DSP_CHANNELS:
419 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_CHANNELS: ") );
420 break;
421 case SNDCTL_DSP_GETFMTS:
422 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_GETFMTS: ") );
423 break;
424 case SNDCTL_DSP_SETFMT:
425 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_SETFMT: ") );
426 break;
427 case SNDCTL_DSP_POST:
428 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_POST: ") );
429 break;
430 case SNDCTL_DSP_GETTRIGGER:
431 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_GETTRIGGER: ") );
432 break;
433 case SNDCTL_DSP_SETTRIGGER:
434 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_SETTRIGGER: ") );
435 break;
436 case SNDCTL_DSP_GETOSPACE:
437 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_GETOSPACE: ") );
438 break;
439 case SNDCTL_DSP_GETISPACE:
440 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_GETISPACE: ") );
441 break;
442 case SNDCTL_DSP_NONBLOCK:
443 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_NONBLOCK: ") );
444 break;
445 case SNDCTL_DSP_GETODELAY:
446 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_GETODELAY: ") );
447 break;
448 case SNDCTL_DSP_GETIPTR:
449 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_GETIPTR: ") );
450 break;
451 case SNDCTL_DSP_GETOPTR:
452 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_GETOPTR: ") );
453 break;
454 case SNDCTL_DSP_GETBLKSIZE:
455 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_GETBLKSIZE: ") );
456 break;
457 case SNDCTL_DSP_SETFRAGMENT:
458 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_SETFRAGMENT: ") );
459 break;
460 case SNDCTL_DSP_SUBDIVIDE:
461 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_SUBDIVIDE: ") );
462 break;
463 case SOUND_PCM_READ_RATE:
464 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_PCM_READ_RATE: ") );
465 break;
466 case SOUND_PCM_READ_CHANNELS:
467 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_PCM_READ_CHANNELS: ") );
468 break;
469 case SOUND_PCM_READ_BITS:
470 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_PCM_READ_BITS: ") );
471 break;
472 case SOUND_PCM_WRITE_FILTER:
473 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_PCM_WRITE_FILTER: ") );
474 break;
475 case SNDCTL_DSP_SETSYNCRO:
476 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_SETSYNCRO: ") );
477 break;
478 case SOUND_PCM_READ_FILTER:
479 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_PCM_READ_FILTER: ") );
480 break;
481 case SNDCTL_DSP_CS_GETDBGMASK:
482 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_CS_GETDBGMASK: ") );
483 break;
484 case SNDCTL_DSP_CS_GETDBGLEVEL:
485 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_CS_GETDBGLEVEL: ") );
486 break;
487 case SNDCTL_DSP_CS_SETDBGMASK:
488 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_CS_SETDBGMASK: ") );
489 break;
490 case SNDCTL_DSP_CS_SETDBGLEVEL:
491 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_CS_SETDBGLEVEL: ") );
492 break;
494 case SOUND_MIXER_PRIVATE1:
495 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_PRIVATE1: ") );
496 break;
497 case SOUND_MIXER_PRIVATE2:
498 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_PRIVATE2: ") );
499 break;
500 case SOUND_MIXER_PRIVATE3:
501 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_PRIVATE3: ") );
502 break;
503 case SOUND_MIXER_PRIVATE4:
504 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_PRIVATE4: ") );
505 break;
506 case SOUND_MIXER_PRIVATE5:
507 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_PRIVATE5: ") );
508 break;
509 case SOUND_MIXER_INFO:
510 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_INFO: ") );
511 break;
512 case SOUND_OLD_MIXER_INFO:
513 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_OLD_MIXER_INFO: ") );
514 break;
516 default:
517 switch (_IOC_NR(x))
519 case SOUND_MIXER_VOLUME:
520 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_VOLUME: ") );
521 break;
522 case SOUND_MIXER_SPEAKER:
523 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_SPEAKER: ") );
524 break;
525 case SOUND_MIXER_RECLEV:
526 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_RECLEV: ") );
527 break;
528 case SOUND_MIXER_MIC:
529 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_MIC: ") );
530 break;
531 case SOUND_MIXER_SYNTH:
532 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_SYNTH: ") );
533 break;
534 case SOUND_MIXER_RECSRC:
535 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_RECSRC: ") );
536 break;
537 case SOUND_MIXER_DEVMASK:
538 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_DEVMASK: ") );
539 break;
540 case SOUND_MIXER_RECMASK:
541 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_RECMASK: ") );
542 break;
543 case SOUND_MIXER_STEREODEVS:
544 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_STEREODEVS: ") );
545 break;
546 case SOUND_MIXER_CAPS:
547 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_CAPS:") );
548 break;
549 default:
550 i = _IOC_NR(x);
551 if (i >= SOUND_MIXER_NRDEVICES || !(vidx = mixtable1[i]))
553 CS_DBGOUT(CS_IOCTL, 4, printk("UNKNOWN IOCTL: 0x%.8x NR=%d ",x,i) );
555 else
557 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_IOCTL AC9x: 0x%.8x NR=%d ",
558 x,i) );
560 break;
563 CS_DBGOUT(CS_IOCTL, 4, printk("command = 0x%x IOC_NR=%d\n",x, _IOC_NR(x)) );
565 #endif
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)
598 return NULL;
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)
607 return NULL;
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)
628 dmabuf->divisor = 1;
629 else if( !(dmabuf->fmt & CS_FMT_STEREO) &&
630 (dmabuf->fmt & CS_FMT_16BIT))
631 dmabuf->divisor = 2;
632 else if( (dmabuf->fmt & CS_FMT_STEREO) &&
633 !(dmabuf->fmt & CS_FMT_16BIT))
634 dmabuf->divisor = 2;
635 else if( !(dmabuf->fmt & CS_FMT_STEREO) &&
636 !(dmabuf->fmt & CS_FMT_16BIT))
637 dmabuf->divisor = 4;
638 else
639 dmabuf->divisor = 1;
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",
644 dmabuf->divisor) );
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) /
663 * GOF_PER_SEC)
664 * ulCorrectionPerSec = Fs,in * 2^26 - Fs,out * phiIncr -M
665 * GOF_PER_SEC * correctionPerGOF
667 * i.e.
669 * phiIncr:other = dividend:remainder((Fs,in * 2^26) / Fs,out)
670 * correctionPerGOF:correctionPerSec =
671 * dividend:remainder(ulOther / GOF_PER_SEC)
673 tmp1 = rate << 16;
674 phiIncr = tmp1 / 48000;
675 tmp1 -= phiIncr * 48000;
676 tmp1 <<= 10;
677 phiIncr <<= 10;
678 tmp2 = tmp1 / 48000;
679 phiIncr += tmp2;
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);
694 dmabuf->rate = rate;
696 CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: cs_set_dac_rate()- %d\n",rate) );
697 return 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)
715 rate = 48000 / 9;
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.
721 if (rate > 48000)
722 rate = 48000;
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) /
732 * GOF_PER_SEC)
733 * correctionPerSec = Fs,in * 2^26 - Fs,out * phiIncr -
734 * GOF_PER_SEC * correctionPerGOF
735 * initialDelay = ceil((24 * Fs,in) / Fs,out)
737 * i.e.
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)
746 tmp1 = rate << 16;
747 coeffIncr = tmp1 / 48000;
748 tmp1 -= coeffIncr * 48000;
749 tmp1 <<= 7;
750 coeffIncr <<= 7;
751 coeffIncr += tmp1 / 48000;
752 coeffIncr ^= 0xFFFFFFFF;
753 coeffIncr++;
754 tmp1 = 48000 << 16;
755 phiIncr = tmp1 / rate;
756 tmp1 -= phiIncr * rate;
757 tmp1 <<= 10;
758 phiIncr <<= 10;
759 tmp2 = tmp1 / rate;
760 phiIncr += tmp2;
761 tmp1 -= tmp2 * 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);
807 dmabuf->rate = rate;
808 CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: cs_set_adc_rate()- %d\n",rate) );
809 return 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);
821 if(!dmabuf->SGok)
822 cs461x_poke(card, BA1_PBA, virt_to_bus(dmabuf->pbuf));
824 Count = 4;
825 playFormat=cs461x_peek(card, BA1_PFIE);
826 if ((dmabuf->fmt & CS_FMT_STEREO)) {
827 playFormat &= ~DMA_RQ_C2_AC_MONO_TO_STEREO;
828 Count *= 2;
830 else
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);
836 Count *= 2;
838 else
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);
845 tmp &= 0xfffffe00;
846 cs461x_poke(card, BA1_PDTC, tmp | --Count);
848 CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: cs_play_setup()-\n") );
852 struct InitStruct
854 u32 long off;
855 u32 long val;
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
868 * capture startup.
870 void SetCaptureSPValues(struct cs_card *card)
872 unsigned i, offset;
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;
911 u32 offset;
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") );
918 return 0;
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) );
937 return offset;
940 static void resync_dma_ptrs(struct cs_state *state)
942 struct dmabuf *dmabuf = &state->dmabuf;
943 int offset;
945 CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: resync_dma_ptrs()+ \n") );
946 offset = 0;
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;
957 unsigned int tmp;
959 dmabuf->enable &= ~ADC_RUNNING;
961 tmp = cs461x_peek(card, BA1_CCTL);
962 tmp &= 0xFFFF0000;
963 cs461x_poke(card, BA1_CCTL, tmp );
966 static void stop_adc(struct cs_state *state)
968 unsigned long flags;
970 CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: stop_adc()+ \n") );
971 spin_lock_irqsave(&state->card->lock, flags);
972 __stop_adc(state);
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;
981 unsigned long flags;
982 unsigned int tmp;
984 spin_lock_irqsave(&card->lock, flags);
985 if (!(dmabuf->enable & ADC_RUNNING) &&
986 ((dmabuf->mapped || dmabuf->count < (signed)dmabuf->dmasize)
987 && dmabuf->ready))
989 dmabuf->enable |= ADC_RUNNING;
990 cs_set_divisor(dmabuf);
991 tmp = cs461x_peek(card, BA1_CCTL);
992 tmp &= 0xFFFF0000;
993 tmp |= card->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;
1006 unsigned int tmp;
1008 dmabuf->enable &= ~DAC_RUNNING;
1010 tmp=cs461x_peek(card, BA1_PCTL);
1011 tmp&=0xFFFF;
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);
1021 __stop_dac(state);
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;
1031 int tmp;
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);
1039 tmp &= 0xFFFF;
1040 tmp |= card->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;
1061 int order;
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)))
1067 break;
1069 if (!rawbuf)
1070 return -ENOMEM;
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)))
1087 break;
1088 if (!tmpbuff)
1089 return -ENOMEM;
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;
1105 dmabuf->SGok = 0;
1106 return 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;
1136 dmabuf->SGok = 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;
1147 int ret;
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;
1165 dmabuf->SGok = 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)))
1172 return ret;
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,
1185 dmabuf->dmasize);
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 */
1198 dmabuf->ready = 1;
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"));
1207 return 0;
1209 else if (dmabuf->type == CS_TYPE_DAC)
1212 * Must be 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;
1220 dmabuf->SGok = 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)))
1227 return ret;
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));
1237 return -ENOMEM;
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)
1252 tmp2 = 0xbfff0000;
1253 else
1254 tmp2 = 0x80000000+8*(tmp1+1);
1255 *(ptmp+2*tmp1+1) = tmp2;
1257 SGarray[0] = 0x82c0200d;
1258 SGarray[1] = 0xffff0000;
1259 SGarray[2] = *ptmp;
1260 SGarray[3] = 0x00010600;
1261 SGarray[4] = *(ptmp+2);
1262 SGarray[5] = 0x80000010;
1263 SGarray[6] = *ptmp;
1264 SGarray[7] = *(ptmp+2);
1265 SGarray[8] = (virt_to_bus(dmabuf->pbuf) & 0xffff000) | 0x10;
1267 if (dmabuf->SGok) {
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;
1274 else {
1275 SGarray[0] = 0xf2c0000f;
1276 SGarray[1] = 0x00000200;
1277 SGarray[2] = 0;
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,
1290 dmabuf->dmasize);
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 */
1301 dmabuf->ready = 1;
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"));
1310 return 0;
1312 else
1314 CS_DBGOUT(CS_FUNCTION, 4, printk("cs46xx: prog_dmabuf()- Invalid Type %d\n",
1315 dmabuf->type));
1317 return 1;
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;
1329 unsigned long tmo;
1330 int count;
1332 if (dmabuf->mapped || !dmabuf->ready)
1333 return 0;
1335 add_wait_queue(&dmabuf->wait, &wait);
1336 for (;;) {
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);
1345 if (count <= 0)
1346 break;
1348 if (signal_pending(current))
1349 break;
1351 if (nonblock) {
1352 remove_wait_queue(&dmabuf->wait, &wait);
1353 current->state = TASK_RUNNING;
1354 return -EBUSY;
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);
1363 break;
1366 remove_wait_queue(&dmabuf->wait, &wait);
1367 current->state = TASK_RUNNING;
1368 if (signal_pending(current))
1369 return -ERESTARTSYS;
1371 return 0;
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;
1381 unsigned hwptr;
1382 int diff;
1384 /* error handling and process wake up for ADC */
1385 state = card->states[0];
1386 if(state)
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",
1396 hwptr,diff) );
1397 dmabuf->hwptr = hwptr;
1398 dmabuf->total_bytes += diff;
1399 dmabuf->count += diff;
1400 if (dmabuf->count > dmabuf->dmasize)
1401 dmabuf->count = dmabuf->dmasize;
1403 if(dmabuf->mapped)
1405 if (dmabuf->count >= (signed)dmabuf->fragsize)
1406 wake_up(&dmabuf->wait);
1407 } else
1409 if (dmabuf->count > 0)
1410 wake_up(&dmabuf->wait);
1416 * Now the DAC
1418 state = card->states[1];
1419 if(state)
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",
1430 hwptr,diff) );
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
1440 * via mmap.
1442 if( dmabuf->count > dmabuf->dmasize)
1443 dmabuf->count &= dmabuf->dmasize-1;
1444 } else {
1445 dmabuf->count -= diff;
1447 * backfill with silence and clear out the last
1448 * "diff" number of bytes.
1450 if(hwptr >= diff)
1452 memset(dmabuf->rawbuf + hwptr - diff,
1453 (dmabuf->fmt & CS_FMT_16BIT) ? 0 : 0x80, diff);
1455 else
1457 memset(dmabuf->rawbuf,
1458 (dmabuf->fmt & CS_FMT_16BIT) ? 0 : 0x80,
1459 (unsigned)hwptr);
1460 memset((void *)((unsigned)dmabuf->rawbuf +
1461 dmabuf->dmasize + hwptr - diff),
1462 (dmabuf->fmt & CS_FMT_16BIT) ? 0 : 0x80,
1463 diff - hwptr);
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",
1469 dmabuf->count));
1471 * buffer underrun or buffer overrun, reset the
1472 * count of bytes written back to 0.
1474 if(dmabuf->count < 0)
1475 dmabuf->underrun=1;
1476 dmabuf->count = 0;
1477 dmabuf->error++;
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)
1490 unsigned char ch;
1491 int wake;
1492 unsigned temp1;
1494 wake = 0;
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;
1500 card->midi.icnt++;
1502 wake = 1;
1504 if (wake)
1505 wake_up(&card->midi.iwait);
1506 wake = 0;
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;
1511 card->midi.ocnt--;
1512 if (card->midi.ocnt < CS_MIDIOUTBUF-16)
1513 wake = 1;
1515 if (wake)
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;
1525 u32 status;
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);
1537 return;
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));
1548 cs_update_ptr();
1551 if( status & HISR_MIDI )
1552 cs_handle_midi(card);
1554 /* clear 'em */
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;
1566 ssize_t ret;
1567 unsigned long flags;
1568 unsigned ptr;
1569 int cnt;
1571 if (ppos != &file->f_pos)
1572 return -ESPIPE;
1573 if (!access_ok(VERIFY_WRITE, buffer, count))
1574 return -EFAULT;
1575 ret = 0;
1576 while (count > 0) {
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);
1583 if (cnt > count)
1584 cnt = count;
1585 if (cnt <= 0) {
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;
1591 continue;
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);
1600 count -= cnt;
1601 buffer += cnt;
1602 ret += cnt;
1604 return ret;
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;
1611 ssize_t ret;
1612 unsigned long flags;
1613 unsigned ptr;
1614 int cnt;
1616 if (ppos != &file->f_pos)
1617 return -ESPIPE;
1618 if (!access_ok(VERIFY_READ, buffer, count))
1619 return -EFAULT;
1620 ret = 0;
1621 while (count > 0) {
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;
1627 if (cnt <= 0)
1628 cs_handle_midi(card);
1629 spin_unlock_irqrestore(&card->lock, flags);
1630 if (cnt > count)
1631 cnt = count;
1632 if (cnt <= 0) {
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;
1638 continue;
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);
1647 count -= cnt;
1648 buffer += cnt;
1649 ret += cnt;
1650 spin_lock_irqsave(&card->lock, flags);
1651 cs_handle_midi(card);
1652 spin_unlock_irqrestore(&card->lock, flags);
1654 return ret;
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);
1678 return mask;
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)
1688 card = card->next;
1689 if (!card)
1690 return -ENODEV;
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);
1697 return -EBUSY;
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);
1722 MOD_INC_USE_COUNT;
1723 return 0;
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);
1737 for (;;) {
1738 spin_lock_irqsave(&card->midi.lock, flags);
1739 count = card->midi.ocnt;
1740 spin_unlock_irqrestore(&card->midi.lock, flags);
1741 if (count <= 0)
1742 break;
1743 if (signal_pending(current))
1744 break;
1745 if (file->f_flags & O_NONBLOCK) {
1746 remove_wait_queue(&card->midi.owait, &wait);
1747 current->state = TASK_RUNNING;
1748 return -EBUSY;
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);
1761 MOD_DEC_USE_COUNT;
1762 return 0;
1766 * Midi file operations struct.
1768 static /*const*/ struct file_operations cs_midi_fops = {
1769 llseek: cs_llseek,
1770 read: cs_midi_read,
1771 write: cs_midi_write,
1772 poll: cs_midi_poll,
1773 open: cs_midi_open,
1774 release: cs_midi_release,
1777 static loff_t cs_llseek(struct file *file, loff_t offset, int origin)
1779 return -ESPIPE;
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.
1788 * Arguments:
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)
1804 s32 s32AudioSample;
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.
1823 psSrc = (s16 *)src;
1824 count = count/2;
1825 while(count--)
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.
1839 psSrc = (s16 *)src;
1840 count = count/2;
1841 while(count--)
1843 s32AudioSample = ((*psSrc)+(*(psSrc + 1)))/2 + (s32)0x80;
1844 if(s32AudioSample > 0x7fff)
1845 s32AudioSample = 0x7fff;
1846 *(pucDst++) = (u8)(((s16)s32AudioSample + (s16)0x8000) >> 8);
1847 psSrc += 2;
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.
1859 psSrc = (s16 *)src;
1860 count = count/2;
1861 while(count--)
1863 *(psDst++) = (s16)((*psSrc)+(*(psSrc + 1)))/2;
1864 psSrc += 2;
1870 * cs_copy_to_user()
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(
1877 struct cs_state *s,
1878 void *dest,
1879 void *hwsrc,
1880 unsigned cnt,
1881 unsigned *copied)
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;
1894 if(!cnt)
1896 *copied = 0;
1897 return 0;
1899 if(dmabuf->divisor != 1)
1901 if(!dmabuf->tmpbuff)
1903 *copied = cnt/dmabuf->divisor;
1904 return 0;
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) );
1917 *copied = 0;
1918 return -EFAULT;
1920 *copied = cnt;
1921 CS_DBGOUT(CS_FUNCTION, 2, printk(KERN_INFO
1922 "cs4281: cs_copy_to_user()- copied bytes is %d \n",cnt) );
1923 return 0;
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;
1934 ssize_t ret = 0;
1935 unsigned long flags;
1936 unsigned swptr;
1937 int cnt;
1938 unsigned copied=0;
1940 CS_DBGOUT(CS_WAVE_READ, 4, printk("cs461x: cs_read()+ %d\n",count) );
1941 state = (struct cs_state *)card->states[0];
1942 if(!state)
1943 return -ENODEV;
1944 dmabuf = &state->dmabuf;
1946 if (ppos != &file->f_pos)
1947 return -ESPIPE;
1948 if (dmabuf->mapped)
1949 return -ENXIO;
1950 if (!dmabuf->ready && (ret = prog_dmabuf(state)))
1951 return ret;
1952 if (!access_ok(VERIFY_WRITE, buffer, count))
1953 return -EFAULT;
1955 add_wait_queue(&state->dmabuf.wait, &wait);
1956 while (count > 0) {
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;
1962 if (cnt <= 0)
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;
1968 if (cnt <= 0) {
1969 /* buffer is empty, start the dma machine and wait for data to be
1970 recorded */
1971 start_adc(state);
1972 if (file->f_flags & O_NONBLOCK) {
1973 if (!ret) ret = -EAGAIN;
1974 remove_wait_queue(&state->dmabuf.wait, &wait);
1975 break;
1977 schedule();
1978 if (signal_pending(current)) {
1979 ret = ret ? ret : -ERESTARTSYS;
1980 break;
1982 continue;
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;
1995 break;
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);
2002 count -= copied;
2003 buffer += copied;
2004 ret += copied;
2005 start_adc(state);
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) );
2010 return 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;
2021 ssize_t ret = 0;
2022 unsigned long flags;
2023 unsigned swptr;
2024 int cnt;
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];
2029 if(!state)
2030 return -ENODEV;
2031 dmabuf = &state->dmabuf;
2033 if (ppos != &file->f_pos)
2034 return -ESPIPE;
2035 if (dmabuf->mapped)
2036 return -ENXIO;
2037 if (!dmabuf->ready && (ret = prog_dmabuf(state)))
2038 return ret;
2039 if (!access_ok(VERIFY_READ, buffer, count))
2040 return -EFAULT;
2041 add_wait_queue(&state->dmabuf.wait, &wait);
2042 while (count > 0) {
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 */
2047 dmabuf->count = 0;
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;
2060 if (cnt <= 0)
2061 __set_current_state(TASK_INTERRUPTIBLE);
2062 spin_unlock_irqrestore(&state->card->lock, flags);
2064 if (cnt > count)
2065 cnt = count;
2066 if (cnt <= 0) {
2067 /* buffer is full, start the dma machine and wait for data to be
2068 played */
2069 start_dac(state);
2070 if (file->f_flags & O_NONBLOCK) {
2071 if (!ret) ret = -EAGAIN;
2072 break;
2074 schedule();
2075 if (signal_pending(current)) {
2076 ret = ret ? ret : -ERESTARTSYS;
2077 break;
2079 continue;
2081 if (copy_from_user(dmabuf->rawbuf + swptr, buffer, cnt)) {
2082 if (!ret) ret = -EFAULT;
2083 return ret;
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);
2100 count -= cnt;
2101 buffer += cnt;
2102 ret += cnt;
2103 start_dac(state);
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) );
2110 return 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)))
2125 return -EINVAL;
2127 if (file->f_mode & FMODE_WRITE)
2129 state = card->states[1];
2130 if(state)
2132 dmabuf = &state->dmabuf;
2133 poll_wait(file, &dmabuf->wait, wait);
2136 if (file->f_mode & FMODE_READ)
2138 state = card->states[0];
2139 if(state)
2141 dmabuf = &state->dmabuf;
2142 poll_wait(file, &dmabuf->wait, wait);
2146 spin_lock_irqsave(&card->lock, flags);
2147 cs_update_ptr();
2148 if (file->f_mode & FMODE_READ) {
2149 state = card->states[0];
2150 if(state)
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];
2159 if(state)
2161 dmabuf = &state->dmabuf;
2162 if (dmabuf->mapped) {
2163 if (dmabuf->count >= (signed)dmabuf->fragsize)
2164 mask |= POLLOUT | POLLWRNORM;
2165 } else {
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"));
2175 return mask;
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
2183 * toys ;)).
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;
2191 int ret;
2192 unsigned long size;
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];
2200 if(state)
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)
2205 return ret;
2207 } else if (vma->vm_flags & VM_READ) {
2208 state = card->states[0];
2209 if(state)
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)
2214 return ret;
2216 } else {
2217 CS_DBGOUT(CS_ERROR, 2, printk(
2218 "cs46xx: cs_mmap() return -EINVAL\n") );
2219 return -EINVAL;
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
2228 * use the DAC only.
2230 state = card->states[1];
2231 if(!(unsigned)state)
2232 return -EINVAL;
2234 dmabuf = &state->dmabuf;
2235 if (vma->vm_pgoff != 0)
2236 return -EINVAL;
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))
2242 return -EINVAL;
2243 if (remap_page_range(vma->vm_start, virt_to_phys(dmabuf->rawbuf),
2244 size, vma->vm_page_prot))
2245 return -EAGAIN;
2246 dmabuf->mapped = 1;
2248 CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: cs_mmap()-\n") );
2249 return 0;
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;
2259 count_info cinfo;
2260 int val, valsave, mapped, ret;
2262 state = (struct cs_state *)card->states[0];
2263 if(state)
2265 dmabuf = &state->dmabuf;
2266 mapped = (file->f_mode & FMODE_READ) && dmabuf->mapped;
2268 state = (struct cs_state *)card->states[1];
2269 if(state)
2271 dmabuf = &state->dmabuf;
2272 mapped |= (file->f_mode & FMODE_WRITE) && dmabuf->mapped;
2275 #if CSDEBUG
2276 printioctl(cmd);
2277 #endif
2279 switch (cmd)
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];
2288 if(state)
2290 dmabuf = &state->dmabuf;
2291 stop_dac(state);
2292 synchronize_irq();
2293 dmabuf->ready = 0;
2294 resync_dma_ptrs(state);
2295 dmabuf->swptr = dmabuf->hwptr = 0;
2296 dmabuf->count = dmabuf->total_bytes = 0;
2297 dmabuf->blocks = 0;
2298 dmabuf->SGok = 0;
2301 if (file->f_mode & FMODE_READ) {
2302 state = (struct cs_state *)card->states[0];
2303 if(state)
2305 dmabuf = &state->dmabuf;
2306 stop_adc(state);
2307 synchronize_irq();
2308 resync_dma_ptrs(state);
2309 dmabuf->ready = 0;
2310 dmabuf->swptr = dmabuf->hwptr = 0;
2311 dmabuf->count = dmabuf->total_bytes = 0;
2312 dmabuf->blocks = 0;
2313 dmabuf->SGok = 0;
2316 CS_DBGOUT(CS_IOCTL, 2, printk("cs46xx: DSP_RESET()-\n") );
2317 return 0;
2319 case SNDCTL_DSP_SYNC:
2320 if (file->f_mode & FMODE_WRITE)
2321 return drain_dac(state, file->f_flags & O_NONBLOCK);
2322 return 0;
2324 case SNDCTL_DSP_SPEED: /* set sample rate */
2325 if (get_user(val, (int *)arg))
2326 return -EFAULT;
2327 if (val >= 0) {
2328 if (file->f_mode & FMODE_READ) {
2329 state = (struct cs_state *)card->states[0];
2330 if(state)
2332 dmabuf = &state->dmabuf;
2333 stop_adc(state);
2334 dmabuf->ready = 0;
2335 dmabuf->SGok = 0;
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];
2342 if(state)
2344 dmabuf = &state->dmabuf;
2345 stop_dac(state);
2346 dmabuf->ready = 0;
2347 dmabuf->SGok = 0;
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" : "",
2356 dmabuf->rate ) );
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))
2363 return -EFAULT;
2364 if (file->f_mode & FMODE_WRITE) {
2365 state = (struct cs_state *)card->states[1];
2366 if(state)
2368 dmabuf = &state->dmabuf;
2369 stop_dac(state);
2370 dmabuf->ready = 0;
2371 dmabuf->SGok = 0;
2372 if(val)
2373 dmabuf->fmt |= CS_FMT_STEREO;
2374 else
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) ?
2380 "STEREO":"MONO") );
2383 if (file->f_mode & FMODE_READ) {
2384 state = (struct cs_state *)card->states[0];
2385 if(state)
2387 dmabuf = &state->dmabuf;
2388 stop_adc(state);
2389 dmabuf->ready = 0;
2390 dmabuf->SGok = 0;
2391 if(val)
2392 dmabuf->fmt |= CS_FMT_STEREO;
2393 else
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) ?
2399 "STEREO":"MONO") );
2402 return 0;
2404 case SNDCTL_DSP_GETBLKSIZE:
2405 if (file->f_mode & FMODE_WRITE) {
2406 state = (struct cs_state *)card->states[1];
2407 if(state)
2409 dmabuf = &state->dmabuf;
2410 if ((val = prog_dmabuf(state)))
2411 return val;
2412 return put_user(dmabuf->fragsize, (int *)arg);
2415 if (file->f_mode & FMODE_READ) {
2416 state = (struct cs_state *)card->states[0];
2417 if(state)
2419 dmabuf = &state->dmabuf;
2420 if ((val = prog_dmabuf(state)))
2421 return val;
2422 return put_user(dmabuf->fragsize/dmabuf->divisor,
2423 (int *)arg);
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))
2433 return -EFAULT;
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" : "") );
2440 valsave = val;
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];
2446 if(state)
2448 dmabuf = &state->dmabuf;
2449 stop_dac(state);
2450 dmabuf->ready = 0;
2451 dmabuf->SGok = 0;
2452 if(val==AFMT_S16_LE)
2453 dmabuf->fmt |= CS_FMT_16BIT;
2454 else
2455 dmabuf->fmt &= ~CS_FMT_16BIT;
2456 cs_set_divisor(dmabuf);
2457 if((ret = prog_dmabuf(state)))
2458 return ret;
2461 if (file->f_mode & FMODE_READ) {
2462 val = valsave;
2463 state = (struct cs_state *)card->states[0];
2464 if(state)
2466 dmabuf = &state->dmabuf;
2467 stop_adc(state);
2468 dmabuf->ready = 0;
2469 dmabuf->SGok = 0;
2470 if(val==AFMT_S16_LE)
2471 dmabuf->fmt |= CS_FMT_16BIT;
2472 else
2473 dmabuf->fmt &= ~CS_FMT_16BIT;
2474 cs_set_divisor(dmabuf);
2475 if((ret = prog_dmabuf(state)))
2476 return ret;
2480 else
2482 CS_DBGOUT(CS_IOCTL | CS_ERROR, 2, printk(
2483 "cs46xx: DSP_SETFMT() Unsupported format (0x%x)\n",
2484 valsave) );
2487 else
2489 if(file->f_mode & FMODE_WRITE)
2491 state = (struct cs_state *)card->states[1];
2492 if(state)
2493 dmabuf = &state->dmabuf;
2495 else if(file->f_mode & FMODE_READ)
2497 state = (struct cs_state *)card->states[0];
2498 if(state)
2499 dmabuf = &state->dmabuf;
2502 if(dmabuf)
2504 if(dmabuf->fmt & CS_FMT_16BIT)
2505 return put_user(AFMT_S16_LE, (int *)arg);
2506 else
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))
2513 return -EFAULT;
2514 if (val != 0) {
2515 if (file->f_mode & FMODE_WRITE) {
2516 state = (struct cs_state *)card->states[1];
2517 if(state)
2519 dmabuf = &state->dmabuf;
2520 stop_dac(state);
2521 dmabuf->ready = 0;
2522 dmabuf->SGok = 0;
2523 if(val>1)
2524 dmabuf->fmt |= CS_FMT_STEREO;
2525 else
2526 dmabuf->fmt &= ~CS_FMT_STEREO;
2527 cs_set_divisor(dmabuf);
2528 if (prog_dmabuf(state))
2529 return 0;
2532 if (file->f_mode & FMODE_READ) {
2533 state = (struct cs_state *)card->states[0];
2534 if(state)
2536 dmabuf = &state->dmabuf;
2537 stop_adc(state);
2538 dmabuf->ready = 0;
2539 dmabuf->SGok = 0;
2540 if(val>1)
2541 dmabuf->fmt |= CS_FMT_STEREO;
2542 else
2543 dmabuf->fmt &= ~CS_FMT_STEREO;
2544 cs_set_divisor(dmabuf);
2545 if (prog_dmabuf(state))
2546 return 0;
2550 return put_user((dmabuf->fmt & CS_FMT_STEREO) ? 2 : 1,
2551 (int *)arg);
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.
2558 return 0;
2560 case SNDCTL_DSP_SUBDIVIDE:
2561 if (file->f_mode & FMODE_WRITE) {
2562 state = (struct cs_state *)card->states[1];
2563 if(state)
2565 dmabuf = &state->dmabuf;
2566 if (dmabuf->subdivision)
2567 return -EINVAL;
2568 if (get_user(val, (int *)arg))
2569 return -EFAULT;
2570 if (val != 1 && val != 2)
2571 return -EINVAL;
2572 dmabuf->subdivision = val;
2575 if (file->f_mode & FMODE_READ) {
2576 state = (struct cs_state *)card->states[0];
2577 if(state)
2579 dmabuf = &state->dmabuf;
2580 if (dmabuf->subdivision)
2581 return -EINVAL;
2582 if (get_user(val, (int *)arg))
2583 return -EFAULT;
2584 if (val != 1 && val != 2)
2585 return -EINVAL;
2586 dmabuf->subdivision = val;
2589 return 0;
2591 case SNDCTL_DSP_SETFRAGMENT:
2592 if (get_user(val, (int *)arg))
2593 return -EFAULT;
2595 if (file->f_mode & FMODE_WRITE) {
2596 state = (struct cs_state *)card->states[1];
2597 if(state)
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];
2606 if(state)
2608 dmabuf = &state->dmabuf;
2609 dmabuf->ossfragshift = val & 0xffff;
2610 dmabuf->ossmaxfrags = (val >> 16) & 0xffff;
2613 return 0;
2615 case SNDCTL_DSP_GETOSPACE:
2616 state = (struct cs_state *)card->states[1];
2617 if(state)
2619 dmabuf = &state->dmabuf;
2620 spin_lock_irqsave(&state->card->lock, flags);
2621 cs_update_ptr();
2622 abinfo.fragsize = dmabuf->fragsize;
2623 abinfo.fragstotal = dmabuf->numfrag;
2625 * for mmap we always have total space available
2627 if (dmabuf->mapped)
2628 abinfo.bytes = dmabuf->dmasize;
2629 else
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;
2636 return -ENODEV;
2638 case SNDCTL_DSP_GETISPACE:
2639 state = (struct cs_state *)card->states[0];
2640 if(state)
2642 dmabuf = &state->dmabuf;
2643 spin_lock_irqsave(&state->card->lock, flags);
2644 cs_update_ptr();
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;
2652 return -ENODEV;
2654 case SNDCTL_DSP_NONBLOCK:
2655 file->f_flags |= O_NONBLOCK;
2656 return 0;
2658 case SNDCTL_DSP_GETCAPS:
2659 return put_user(DSP_CAP_REALTIME|DSP_CAP_TRIGGER|DSP_CAP_MMAP,
2660 (int *)arg);
2662 case SNDCTL_DSP_GETTRIGGER:
2663 val = 0;
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];
2668 if(state)
2670 dmabuf = &state->dmabuf;
2671 if(dmabuf->enable & DAC_RUNNING)
2672 val |= PCM_ENABLE_INPUT;
2675 if (file->f_mode & FMODE_READ)
2677 if(state)
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))
2690 return -EFAULT;
2691 if (file->f_mode & FMODE_READ) {
2692 state = (struct cs_state *)card->states[0];
2693 if(state)
2695 dmabuf = &state->dmabuf;
2696 if (val & PCM_ENABLE_INPUT) {
2697 if (!dmabuf->ready && (ret = prog_dmabuf(state)))
2698 return ret;
2699 start_adc(state);
2700 } else
2701 stop_adc(state);
2704 if (file->f_mode & FMODE_WRITE) {
2705 state = (struct cs_state *)card->states[1];
2706 if(state)
2708 dmabuf = &state->dmabuf;
2709 if (val & PCM_ENABLE_OUTPUT) {
2710 if (!dmabuf->ready && (ret = prog_dmabuf(state)))
2711 return ret;
2712 start_dac(state);
2713 } else
2714 stop_dac(state);
2717 return 0;
2719 case SNDCTL_DSP_GETIPTR:
2720 state = (struct cs_state *)card->states[0];
2721 if(state)
2723 dmabuf = &state->dmabuf;
2724 spin_lock_irqsave(&state->card->lock, flags);
2725 cs_update_ptr();
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));
2732 return -ENODEV;
2734 case SNDCTL_DSP_GETOPTR:
2735 state = (struct cs_state *)card->states[1];
2736 if(state)
2738 dmabuf = &state->dmabuf;
2739 spin_lock_irqsave(&state->card->lock, flags);
2740 cs_update_ptr();
2741 cinfo.bytes = dmabuf->total_bytes;
2742 if (dmabuf->mapped)
2744 cinfo.blocks = (cinfo.bytes >> dmabuf->fragshift)
2745 - dmabuf->blocks;
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;
2751 else
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));
2763 return -ENODEV;
2765 case SNDCTL_DSP_SETDUPLEX:
2766 return -EINVAL;
2768 case SNDCTL_DSP_GETODELAY:
2769 if (!(file->f_mode & FMODE_WRITE))
2770 return -EINVAL;
2771 state = (struct cs_state *)card->states[1];
2772 if(state)
2774 dmabuf = &state->dmabuf;
2775 spin_lock_irqsave(&state->card->lock, flags);
2776 cs_update_ptr();
2777 val = dmabuf->count;
2778 spin_unlock_irqrestore(&state->card->lock, flags);
2780 else
2781 val = 0;
2782 return put_user(val, (int *)arg);
2784 case SOUND_PCM_READ_RATE:
2785 state = (struct cs_state *)card->states[0];
2786 if(state)
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];
2796 if(state)
2798 dmabuf = &state->dmabuf;
2799 return put_user((dmabuf->fmt & CS_FMT_STEREO) ? 2 : 1,
2800 (int *)arg);
2802 return put_user(0, (int *)arg);
2804 case SOUND_PCM_READ_BITS:
2805 state = (struct cs_state *)card->states[0];
2806 if(state)
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:
2818 return -EINVAL;
2820 return -EINVAL;
2825 * AMP control - null AMP
2828 static void amp_none(struct cs_card *card, int change)
2833 * Crystal EAPD mode
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) |
2849 0x8000);
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) &
2856 ~0x8000);
2863 * Untested
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;
2872 if(card->amplifier)
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);
2880 else
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;
2898 u16 control;
2899 u8 pp;
2900 unsigned long port;
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);
2911 port=pp<<8;
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;
2929 int ret=0;
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);
2944 if (state == NULL)
2945 return -ENOMEM;
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)
2951 kfree(state);
2952 card->states[0]=NULL;
2953 return -ENOMEM;
2956 else
2958 state = card->states[0];
2959 if(state->open_mode & FMODE_READ)
2960 return -EBUSY;
2962 dmabuf->channel = card->alloc_rec_pcm_channel(card);
2964 if (dmabuf->channel == NULL) {
2965 kfree (card->states[0]);
2966 card->states[0] = NULL;;
2967 return -ENODEV;
2970 /* Now turn on external AMP if needed */
2971 state->card = card;
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 */
2977 state->virt = 0;
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);
3000 MOD_INC_USE_COUNT;
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);
3008 if (state == NULL)
3009 return -ENOMEM;
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)
3015 kfree(state);
3016 card->states[1]=NULL;
3017 return -ENOMEM;
3020 else
3022 state = card->states[1];
3023 if(state->open_mode & FMODE_WRITE)
3024 return -EBUSY;
3026 dmabuf->channel = card->alloc_pcm_channel(card);
3028 if (dmabuf->channel == NULL) {
3029 kfree (card->states[1]);
3030 card->states[1] = NULL;;
3031 return -ENODEV;
3034 /* Now turn on external AMP if needed */
3035 state->card = card;
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 */
3041 state->virt = 1;
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);
3064 MOD_INC_USE_COUNT;
3065 if((ret = prog_dmabuf(state)))
3066 return ret;
3069 CS_DBGOUT(CS_OPEN | CS_FUNCTION, 2, printk("cs46xx: cs_open()- 0\n") );
3070 return 0;
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)))
3084 return -EINVAL;
3086 state = card->states[1];
3087 if(state)
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);
3097 stop_dac(state);
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);
3111 kfree(state);
3113 MOD_DEC_USE_COUNT;
3116 state = card->states[0];
3117 if(state)
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);
3124 stop_adc(state);
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);
3138 kfree(state);
3140 MOD_DEC_USE_COUNT;
3143 CS_DBGOUT(CS_FUNCTION | CS_RELEASE, 2, printk("cs46xx: cs_release()- 0\n") );
3144 return 0;
3147 static /*const*/ struct file_operations cs461x_fops = {
3148 llseek: cs_llseek,
3149 read: cs_read,
3150 write: cs_write,
3151 poll: cs_poll,
3152 ioctl: cs_ioctl,
3153 mmap: cs_mmap,
3154 open: cs_open,
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;
3164 int count;
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 |
3195 ACCTL_RSTN);
3199 * Wait for the read to occur.
3201 for (count = 0; count < 500; count++) {
3203 * First, we want to wait for a short time.
3205 udelay(10);
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))
3211 break;
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);
3219 return 0xffff;
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)
3232 break;
3233 udelay(10);
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);
3241 return 0xffff;
3245 * Read the data returned from the AC97 register.
3246 * ACSDA = Status Data Register = 474h
3248 #if 0
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));
3252 #endif
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;
3259 int count;
3260 int val2 = 0;
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.
3295 udelay(10);
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))
3301 break;
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
3311 * to the CD.
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
3318 * unmuted.
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;
3342 /* Mute on */
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)
3356 int i;
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)
3364 goto match;
3366 if (!card)
3367 return -ENODEV;
3369 match:
3370 file->private_data = card->ac97_codec[i];
3372 card->active_ctrl(card,1);
3373 MOD_INC_USE_COUNT;
3374 return 0;
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;
3381 int i;
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)
3387 goto match;
3389 if (!card)
3390 return -ENODEV;
3391 match:
3392 card->active_ctrl(card, -1);
3393 MOD_DEC_USE_COUNT;
3394 return 0;
3397 static int cs_ioctl_mixdev(struct inode *inode, struct file *file, unsigned int cmd,
3398 unsigned long arg)
3400 struct ac97_codec *codec = (struct ac97_codec *)file->private_data;
3402 #if CSDEBUG_INTERFACE
3403 int val;
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) )
3410 switch(cmd)
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))
3421 return -EFAULT;
3422 cs_debugmask = val;
3423 return 0;
3425 case SOUND_MIXER_CS_SETDBGLEVEL:
3426 if (get_user(val, (unsigned long *)arg))
3427 return -EFAULT;
3428 cs_debuglevel = val;
3429 return 0;
3430 default:
3431 CS_DBGOUT(CS_ERROR, 1, printk(KERN_INFO
3432 "cs4281: mixer_ioctl(): ERROR unknown debug cmd\n") );
3433 return 0;
3436 #endif
3437 return codec->mixer_ioctl(codec, cmd, arg);
3440 static /*const*/ struct file_operations cs_mixer_fops = {
3441 llseek: cs_llseek,
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)
3450 int num_ac97 = 0;
3451 int ready_2nd = 0;
3452 struct ac97_codec *codec;
3453 u16 eid;
3455 for (num_ac97 = 0; num_ac97 < NR_AC97; num_ac97++) {
3456 if ((codec = kmalloc(sizeof(struct ac97_codec), GFP_KERNEL)) == NULL)
3457 return -ENOMEM;
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)
3469 break;
3471 eid = cs_ac97_get(codec, AC97_EXTENDED_ID);
3473 if(eid==0xFFFFFF)
3475 printk(KERN_WARNING "cs461x: no codec attached ?\n");
3476 kfree(codec);
3477 break;
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");
3484 kfree(codec);
3485 break;
3488 card->ac97_codec[num_ac97] = codec;
3490 /* if there is no secondary codec at all, don't probe any more */
3491 if (!ready_2nd)
3492 return num_ac97+1;
3494 return num_ac97;
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);
3522 iounmap(pBA1);
3527 * Chip reset
3530 static void cs461x_reset(struct cs_card *card)
3532 int idx;
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;
3562 unsigned int tmp;
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);
3570 powerdown = 1;
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++) {
3588 udelay(50);
3589 if (!(cs461x_peekBA0(card, BA0_SERBST) & SERBST_WBSY))
3590 break;
3592 if (cs461x_peekBA0(card, BA0_SERBST) & SERBST_WBSY) {
3593 if (powerdown)
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.
3609 if (powerdown)
3610 cs461x_pokeBA0(card, BA0_CLKCR1, tmp);
3613 static void cs461x_powerup_dac(struct cs_card *card)
3615 int count;
3616 unsigned int tmp;
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
3621 * register.
3623 tmp = cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL);
3624 if (tmp & 2) /* already */
3625 return;
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.
3636 udelay(50);
3639 * Read the current state of the power control register.
3641 if (cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) & 2)
3642 break;
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)
3654 int count;
3655 unsigned int tmp;
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
3660 * register.
3662 tmp = cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL);
3663 if (tmp & 1) /* already */
3664 return;
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.
3675 udelay(50);
3678 * Read the current state of the power control register.
3680 if (cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) & 1)
3681 break;
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)
3693 int cnt;
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
3706 * register.
3708 for (cnt = 0; cnt < 25; cnt++) {
3709 udelay(50);
3710 if (!(cs461x_peek(card, BA1_SPCR) & SPCR_RUNFR))
3711 break;
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;
3732 unsigned int tmp;
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
3753 * for a reset.
3755 cs461x_pokeBA0(card, BA0_ACCTL, 0);
3756 udelay(50);
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
3762 * signal.
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
3769 * input clock).
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);
3789 * Power up the PLL.
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);
3829 do {
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)
3835 break;
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");
3846 return -EIO;
3850 * Assert the vaid frame signal so that we can start sending commands
3851 * to the AC97 card.
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);
3860 do {
3862 * Read the input slot valid register and see if input slots 3 and
3863 * 4 are valid yet.
3865 if ((cs461x_peekBA0(card, BA0_ACISV) & (ACISV_ISV3 | ACISV_ISV4)) == (ACISV_ISV3 | ACISV_ISV4))
3866 break;
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
3873 * an error.
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");
3877 return -EIO;
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
3888 * them.
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.
3902 cs461x_reset(card);
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);
3918 * Stop capture DMA.
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)
3926 return -EIO;
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);
3941 tmp &= ~0x0000f03f;
3942 cs461x_poke(card, BA1_PFIE, tmp); /* playback interrupt enable */
3944 tmp = cs461x_peek(card, BA1_CIE);
3945 tmp &= ~0x0000003f;
3946 tmp |= 0x00000001;
3947 cs461x_poke(card, BA1_CIE, tmp); /* capture interrupt enable */
3948 return 0;
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) */
3956 * Card subid table
3959 struct cs_card_type
3961 u16 vendor;
3962 u16 id;
3963 char *name;
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}
3980 #ifdef CS46XX_PM
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;
3985 if (state) {
3986 switch(rqst) {
3987 case PM_RESUME:
3988 printk( KERN_DEBUG "cs46xx: PM resume request\n");
3989 cs_hardware_init(state->card);
3990 break;
3991 case PM_SUSPEND:
3992 printk( KERN_DEBUG "cs46xx: PM suspend request\n");
3993 stop_dac(state);
3994 resync_dma_ptrs(state);
3995 break;
3999 return 0;
4001 #endif
4003 static int __init cs_install(struct pci_dev *pci_dev)
4005 struct cs_card *card;
4006 struct cs_card_type *cp = &cards[0];
4007 #ifdef CS46XX_PM
4008 struct pm_dev *pmdev;
4009 #endif
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");
4018 return -ENOMEM;
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;
4040 while (cp->name)
4042 if(cp->vendor == ss_vendor && cp->id == ss_card)
4044 card->amplifier_ctrl = cp->amp;
4045 if(cp->active)
4046 card->active_ctrl = cp->active;
4047 break;
4049 cp++;
4051 if (cp->name==NULL)
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);
4056 else
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;
4074 if (thinkpad == 1)
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)
4100 goto fail2;
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);
4104 goto fail2;
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");
4109 goto fail;
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);
4119 if(card->dev_midi)
4120 unregister_sound_midi(card->dev_midi);
4121 goto fail;
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);
4127 card->next = devs;
4128 devs = card;
4129 cs461x_pokeBA0(card, BA0_MIDCR, MIDCR_MRST);
4130 cs461x_pokeBA0(card, BA0_MIDCR, 0);
4132 card->active_ctrl(card, -1);
4133 #ifdef CS46XX_PM
4134 pmdev = pm_register(PM_PCI_DEV, PM_PCI_ID(pci_dev), cs46xx_pm_callback);
4135 if (pmdev)
4136 pmdev->data = card;
4137 #endif
4138 return 0;
4140 fail:
4141 free_irq(card->irq, card);
4142 fail2:
4143 if(card->ba0)
4144 iounmap(card->ba0);
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);
4153 kfree(card);
4154 return -ENODEV;
4158 static void cs_remove(struct cs_card *card)
4160 int i;
4161 unsigned int tmp;
4163 card->active_ctrl(card,1);
4165 tmp = cs461x_peek(card, BA1_PFIE);
4166 tmp &= ~0x0000f03f;
4167 tmp |= 0x00000010;
4168 cs461x_poke(card, BA1_PFIE, tmp); /* playback interrupt disable */
4170 tmp = cs461x_peek(card, BA1_CIE);
4171 tmp &= ~0x0000003f;
4172 tmp |= 0x00000011;
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);
4182 * Stop capture DMA.
4184 tmp = cs461x_peek(card, BA1_CCTL);
4185 cs461x_poke(card, BA1_CCTL, tmp & 0xffff0000);
4188 * Reset the processor.
4190 cs461x_reset(card);
4192 cs461x_proc_stop(card);
4195 * Power down the DAC and ADC. We will power them up (if) when we need
4196 * them.
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);
4216 iounmap(card->ba0);
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);
4229 if(card->dev_midi)
4230 unregister_sound_midi(card->dev_midi);
4231 kfree(card);
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;
4240 int foundone=0;
4242 if (!pci_present()) /* No PCI bus in this machine! */
4243 return -ENODEV;
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)
4250 foundone++;
4252 while( (pcidev = pci_find_device(PCI_VENDOR_ID_CIRRUS, 0x6003 , pcidev))!=NULL ) {
4253 if (cs_install(pcidev)==0)
4254 foundone++;
4256 while( (pcidev = pci_find_device(PCI_VENDOR_ID_CIRRUS, 0x6004 , pcidev))!=NULL ) {
4257 if (cs_install(pcidev)==0)
4258 foundone++;
4261 printk(KERN_INFO "cs461x: Found %d audio device(s).\n",
4262 foundone);
4263 return foundone;
4266 int __init init_module(void)
4268 if(cs_probe()==0)
4269 printk(KERN_ERR "cs461x: No devices found.\n");
4270 return 0;
4273 void __exit cleanup_module (void)
4275 struct cs_card *next;
4276 #ifdef CS46XX_PM
4277 pm_unregister_all(cs46xx_pm_callback);
4278 #endif
4279 while(devs)
4281 next=devs->next;
4282 cs_remove(devs);
4283 devs=next;
4287 MODULE_PARM(external_amp, "i");
4288 MODULE_PARM(thinkpad, "i");