2 * Crystal SoundFusion CS46xx driver
4 * Copyright 1998-2001 Cirrus Logic Corporation <pcaudio@crystal.cirrus.com>
5 * <twoller@crystal.cirrus.com>
6 * Copyright 1999-2000 Jaroslav Kysela <perex@suse.cz>
7 * Copyright 2000 Alan Cox <alan@redhat.com>
9 * The core of this code is taken from the ALSA project driver by
10 * Jaroslav. Please send Jaroslav the credit for the driver and
11 * report bugs in this port to <alan@redhat.com>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 * Current maintainers:
27 * Cirrus Logic Corporation, Thomas Woller (tw)
28 * <twoller@crystal.cirrus.com>
30 * <nils@kernelconcepts.de>
31 * Thanks to David Pollard for testing.
34 * 20000909-nf Changed cs_read, cs_write and drain_dac
35 * 20001025-tw Separate Playback/Capture structs and buffers.
36 * Added Scatter/Gather support for Playback.
38 * 20001027-nf Port to kernel 2.4.0-test9, some clean-ups
39 * Start of powermanagement support (CS46XX_PM).
40 * 20001128-tw Add module parm for default buffer order.
41 * added DMA_GFP flag to kmalloc dma buffer allocs.
42 * backfill silence to eliminate stuttering on
44 * 20001201-tw add resyncing of swptr on underruns.
45 * 20001205-tw-nf fixed GETOSPACE ioctl() after open()
46 * 20010113-tw patch from Hans Grobler general cleanup.
47 * 20010117-tw 2.4.0 pci cleanup, wrapper code for 2.2.16-2.4.0
48 * 20010118-tw basic PM support for 2.2.16+ and 2.4.0/2.4.2.
49 * 20010228-dh patch from David Huggins - cs_update_ptr recursion.
50 * 20010409-tw add hercules game theatre XP amp code.
51 * 20010420-tw cleanup powerdown/up code.
52 * 20010521-tw eliminate pops, and fixes for powerdown.
53 * 20010525-tw added fixes for thinkpads with powerdown logic.
54 * 20010723-sh patch from Horms (Simon Horman) -
55 * SOUND_PCM_READ_BITS returns bits as set in driver
56 * rather than a logical or of the possible values.
57 * Various ioctls handle the case where the device
58 * is open for reading or writing but not both better.
61 * Playback/Capture supported from 8k-48k.
62 * 16Bit Signed LE & 8Bit Unsigned, with Mono or Stereo supported.
64 * APM/PM - 2.2.x APM is enabled and functioning fine. APM can also
65 * be enabled for 2.4.x by modifying the CS46XX_ACPI_SUPPORT macro
68 * Hercules Game Theatre XP - the EGPIO2 pin controls the external Amp,
69 * so, use the drain/polarity to enable.
70 * hercules_egpio_disable set to 1, will force a 0 to EGPIODR.
72 * VTB Santa Cruz - the GPIO7/GPIO8 on the Secondary Codec control
73 * the external amplifier for the "back" speakers, since we do not
74 * support the secondary codec then this external amp is also not
78 #include <linux/interrupt.h>
79 #include <linux/list.h>
80 #include <linux/module.h>
81 #include <linux/string.h>
82 #include <linux/ioport.h>
83 #include <linux/sched.h>
84 #include <linux/delay.h>
85 #include <linux/sound.h>
86 #include <linux/slab.h>
87 #include <linux/soundcard.h>
88 #include <linux/pci.h>
89 #include <linux/bitops.h>
90 #include <linux/init.h>
91 #include <linux/poll.h>
92 #include <linux/ac97_codec.h>
93 #include <linux/mutex.h>
97 #include <asm/uaccess.h>
99 #include "cs46xxpm-24.h"
100 #include "cs46xx_wrapper-24.h"
103 /* MIDI buffer sizes */
104 #define CS_MIDIINBUF 500
105 #define CS_MIDIOUTBUF 500
107 #define ADC_RUNNING 1
108 #define DAC_RUNNING 2
110 #define CS_FMT_16BIT 1 /* These are fixed in fact */
111 #define CS_FMT_STEREO 2
112 #define CS_FMT_MASK 3
114 #define CS_TYPE_ADC 1
115 #define CS_TYPE_DAC 2
120 #define CS_INC_USE_COUNT(m) (atomic_inc(m))
121 #define CS_DEC_USE_COUNT(m) (atomic_dec(m))
122 #define CS_DEC_AND_TEST(m) (atomic_dec_and_test(m))
123 #define CS_IN_USE(m) (atomic_read(m) != 0)
125 #define CS_DBGBREAKPOINT {__asm__("INT $3");}
130 #define CS461X_BA0_SIZE 0x2000
131 #define CS461X_BA1_DATA0_SIZE 0x3000
132 #define CS461X_BA1_DATA1_SIZE 0x3800
133 #define CS461X_BA1_PRG_SIZE 0x7000
134 #define CS461X_BA1_REG_SIZE 0x0100
136 #define GOF_PER_SEC 200
138 #define CSDEBUG_INTERFACE 1
141 * Turn on/off debugging compilation by using 1/0 respectively for CSDEBUG
144 * CSDEBUG is usual mode is set to 1, then use the
145 * cs_debuglevel and cs_debugmask to turn on or off debugging.
146 * Debug level of 1 has been defined to be kernel errors and info
147 * that should be printed on any released driver.
150 #define CS_DBGOUT(mask,level,x) if ((cs_debuglevel >= (level)) && ((mask) & cs_debugmask)) {x;}
152 #define CS_DBGOUT(mask,level,x)
157 #define CS_INIT 0x00000001 /* initialization and probe functions */
158 #define CS_ERROR 0x00000002 /* tmp debugging bit placeholder */
159 #define CS_INTERRUPT 0x00000004 /* interrupt handler (separate from all other) */
160 #define CS_FUNCTION 0x00000008 /* enter/leave functions */
161 #define CS_WAVE_WRITE 0x00000010 /* write information for wave */
162 #define CS_WAVE_READ 0x00000020 /* read information for wave */
163 #define CS_MIDI_WRITE 0x00000040 /* write information for midi */
164 #define CS_MIDI_READ 0x00000080 /* read information for midi */
165 #define CS_MPU401_WRITE 0x00000100 /* write information for mpu401 */
166 #define CS_MPU401_READ 0x00000200 /* read information for mpu401 */
167 #define CS_OPEN 0x00000400 /* all open functions in the driver */
168 #define CS_RELEASE 0x00000800 /* all release functions in the driver */
169 #define CS_PARMS 0x00001000 /* functional and operational parameters */
170 #define CS_IOCTL 0x00002000 /* ioctl (non-mixer) */
171 #define CS_PM 0x00004000 /* PM */
172 #define CS_TMP 0x10000000 /* tmp debug mask bit */
174 #define CS_IOCTL_CMD_SUSPEND 0x1 // suspend
175 #define CS_IOCTL_CMD_RESUME 0x2 // resume
178 static unsigned long cs_debuglevel
= 1; /* levels range from 1-9 */
179 module_param(cs_debuglevel
, ulong
, 0644);
180 static unsigned long cs_debugmask
= CS_INIT
| CS_ERROR
; /* use CS_DBGOUT with various mask values */
181 module_param(cs_debugmask
, ulong
, 0644);
183 static unsigned long hercules_egpio_disable
; /* if non-zero set all EGPIO to 0 */
184 module_param(hercules_egpio_disable
, ulong
, 0);
185 static unsigned long initdelay
= 700; /* PM delay in millisecs */
186 module_param(initdelay
, ulong
, 0);
187 static unsigned long powerdown
= -1; /* turn on/off powerdown processing in driver */
188 module_param(powerdown
, ulong
, 0);
189 #define DMABUF_DEFAULTORDER 3
190 static unsigned long defaultorder
= DMABUF_DEFAULTORDER
;
191 module_param(defaultorder
, ulong
, 0);
193 static int external_amp
;
194 module_param(external_amp
, bool, 0);
196 module_param(thinkpad
, bool, 0);
199 * set the powerdown module parm to 0 to disable all
200 * powerdown. also set thinkpad to 1 to disable powerdown,
201 * but also to enable the clkrun functionality.
203 static unsigned cs_powerdown
= 1;
204 static unsigned cs_laptop_wait
= 1;
206 /* An instance of the 4610 channel */
214 #define CS46XX_MAJOR_VERSION "1"
215 #define CS46XX_MINOR_VERSION "28"
218 #define CS46XX_ARCH "64" //architecture key
220 #define CS46XX_ARCH "32" //architecture key
223 static struct list_head cs46xx_devs
= { &cs46xx_devs
, &cs46xx_devs
};
225 /* magic numbers to protect our data structures */
226 #define CS_CARD_MAGIC 0x43525553 /* "CRUS" */
227 #define CS_STATE_MAGIC 0x4c4f4749 /* "LOGI" */
230 /* maxinum number of AC97 codecs connected, AC97 2.0 defined 4 */
233 static const unsigned sample_size
[] = { 1, 2, 2, 4 };
234 static const unsigned sample_shift
[] = { 0, 1, 1, 2 };
236 /* "software" or virtual channel, an instance of opened /dev/dsp */
239 struct cs_card
*card
; /* Card info */
241 /* single open lock mechanism, only used for recording */
242 struct mutex open_mutex
;
243 wait_queue_head_t open_wait
;
248 /* virtual channel number */
252 /* wave sample stuff */
254 unsigned char fmt
, enable
;
256 /* hardware channel */
257 struct cs_channel
*channel
;
258 int pringbuf
; /* Software ring slot */
259 void *pbuf
; /* 4K hardware DMA buffer */
261 /* OSS buffer management stuff */
263 dma_addr_t dma_handle
;
269 void *tmpbuff
; /* tmp buffer for sample conversions */
271 dma_addr_t dmaaddr_tmpbuff
;
272 unsigned buforder_tmpbuff
; /* Log base 2 of size in bytes.. */
274 /* our buffer acts like a circular ring */
275 unsigned hwptr
; /* where dma last started, updated by update_ptr */
276 unsigned swptr
; /* where driver last clear/filled, updated by read/write */
277 int count
; /* bytes to be comsumed or been generated by dma machine */
278 unsigned total_bytes
; /* total bytes dmaed by hardware */
279 unsigned blocks
; /* total blocks */
281 unsigned error
; /* number of over/underruns */
282 unsigned underrun
; /* underrun pending before next write has occurred */
283 wait_queue_head_t wait
; /* put process on wait queue when no more space in buffer */
285 /* redundant, but makes calculations easier */
288 unsigned fragsamples
;
293 unsigned endcleared
:1;
295 unsigned update_flag
;
296 unsigned ossfragshift
;
298 unsigned subdivision
;
300 /* Guard against mmap/write/read races */
305 struct cs_channel channel
[2];
308 /* We keep cs461x cards in a linked list */
309 struct cs_card
*next
;
311 /* The cs461x has a certain amount of cross channel interaction
312 so we use a single per card lock */
316 spinlock_t ac97_lock
;
318 /* mixer use count */
319 atomic_t mixer_use_cnt
;
321 /* PCI device stuff */
322 struct pci_dev
*pci_dev
;
323 struct list_head list
;
325 unsigned int pctl
, cctl
; /* Hardware DMA flag sets */
327 /* soundcore stuff */
331 /* structures for abstraction of hardware facilities, codecs, banks and channels*/
332 struct ac97_codec
*ac97_codec
[NR_AC97
];
333 struct cs_state
*states
[2];
337 int amplifier
; /* Amplifier control */
338 void (*amplifier_ctrl
)(struct cs_card
*, int);
339 void (*amp_init
)(struct cs_card
*);
341 int active
; /* Active clocking */
342 void (*active_ctrl
)(struct cs_card
*, int);
344 /* hardware resources */
345 unsigned long ba0_addr
;
346 unsigned long ba1_addr
;
363 /* Function support */
364 struct cs_channel
*(*alloc_pcm_channel
)(struct cs_card
*);
365 struct cs_channel
*(*alloc_rec_pcm_channel
)(struct cs_card
*);
366 void (*free_pcm_channel
)(struct cs_card
*, int chan
);
368 /* /dev/midi stuff */
370 unsigned ird
, iwr
, icnt
;
371 unsigned ord
, owr
, ocnt
;
372 wait_queue_head_t open_wait
;
373 wait_queue_head_t iwait
;
374 wait_queue_head_t owait
;
376 unsigned char ibuf
[CS_MIDIINBUF
];
377 unsigned char obuf
[CS_MIDIOUTBUF
];
379 struct mutex open_mutex
;
384 static int cs_open_mixdev(struct inode
*inode
, struct file
*file
);
385 static int cs_release_mixdev(struct inode
*inode
, struct file
*file
);
386 static int cs_ioctl_mixdev(struct inode
*inode
, struct file
*file
, unsigned int cmd
,
388 static int cs_hardware_init(struct cs_card
*card
);
389 static int cs46xx_powerup(struct cs_card
*card
, unsigned int type
);
390 static int cs461x_powerdown(struct cs_card
*card
, unsigned int type
, int suspendflag
);
391 static void cs461x_clear_serial_FIFOs(struct cs_card
*card
, int type
);
392 static int cs46xx_suspend_tbl(struct pci_dev
*pcidev
, pm_message_t state
);
393 static int cs46xx_resume_tbl(struct pci_dev
*pcidev
);
399 #define SOUND_MIXER_CS_GETDBGLEVEL _SIOWR('M',120, int)
400 #define SOUND_MIXER_CS_SETDBGLEVEL _SIOWR('M',121, int)
401 #define SOUND_MIXER_CS_GETDBGMASK _SIOWR('M',122, int)
402 #define SOUND_MIXER_CS_SETDBGMASK _SIOWR('M',123, int)
403 #define SOUND_MIXER_CS_APM _SIOWR('M',124, int)
405 static void printioctl(unsigned int x
)
409 /* these values are incorrect for the ac97 driver, fix.
410 * Index of mixtable1[] member is Device ID
411 * and must be <= SOUND_MIXER_NRDEVICES.
412 * Value of array member is index into s->mix.vol[]
414 static const unsigned char mixtable1
[SOUND_MIXER_NRDEVICES
] = {
415 [SOUND_MIXER_PCM
] = 1, /* voice */
416 [SOUND_MIXER_LINE1
] = 2, /* AUX */
417 [SOUND_MIXER_CD
] = 3, /* CD */
418 [SOUND_MIXER_LINE
] = 4, /* Line */
419 [SOUND_MIXER_SYNTH
] = 5, /* FM */
420 [SOUND_MIXER_MIC
] = 6, /* Mic */
421 [SOUND_MIXER_SPEAKER
] = 7, /* Speaker */
422 [SOUND_MIXER_RECLEV
] = 8, /* Recording level */
423 [SOUND_MIXER_VOLUME
] = 9 /* Master Volume */
427 case SOUND_MIXER_CS_GETDBGMASK
:
428 CS_DBGOUT(CS_IOCTL
, 4, printk("SOUND_MIXER_CS_GETDBGMASK: ") );
430 case SOUND_MIXER_CS_GETDBGLEVEL
:
431 CS_DBGOUT(CS_IOCTL
, 4, printk("SOUND_MIXER_CS_GETDBGLEVEL: ") );
433 case SOUND_MIXER_CS_SETDBGMASK
:
434 CS_DBGOUT(CS_IOCTL
, 4, printk("SOUND_MIXER_CS_SETDBGMASK: ") );
436 case SOUND_MIXER_CS_SETDBGLEVEL
:
437 CS_DBGOUT(CS_IOCTL
, 4, printk("SOUND_MIXER_CS_SETDBGLEVEL: ") );
440 CS_DBGOUT(CS_IOCTL
, 4, printk("OSS_GETVERSION: ") );
442 case SNDCTL_DSP_SYNC
:
443 CS_DBGOUT(CS_IOCTL
, 4, printk("SNDCTL_DSP_SYNC: ") );
445 case SNDCTL_DSP_SETDUPLEX
:
446 CS_DBGOUT(CS_IOCTL
, 4, printk("SNDCTL_DSP_SETDUPLEX: ") );
448 case SNDCTL_DSP_GETCAPS
:
449 CS_DBGOUT(CS_IOCTL
, 4, printk("SNDCTL_DSP_GETCAPS: ") );
451 case SNDCTL_DSP_RESET
:
452 CS_DBGOUT(CS_IOCTL
, 4, printk("SNDCTL_DSP_RESET: ") );
454 case SNDCTL_DSP_SPEED
:
455 CS_DBGOUT(CS_IOCTL
, 4, printk("SNDCTL_DSP_SPEED: ") );
457 case SNDCTL_DSP_STEREO
:
458 CS_DBGOUT(CS_IOCTL
, 4, printk("SNDCTL_DSP_STEREO: ") );
460 case SNDCTL_DSP_CHANNELS
:
461 CS_DBGOUT(CS_IOCTL
, 4, printk("SNDCTL_DSP_CHANNELS: ") );
463 case SNDCTL_DSP_GETFMTS
:
464 CS_DBGOUT(CS_IOCTL
, 4, printk("SNDCTL_DSP_GETFMTS: ") );
466 case SNDCTL_DSP_SETFMT
:
467 CS_DBGOUT(CS_IOCTL
, 4, printk("SNDCTL_DSP_SETFMT: ") );
469 case SNDCTL_DSP_POST
:
470 CS_DBGOUT(CS_IOCTL
, 4, printk("SNDCTL_DSP_POST: ") );
472 case SNDCTL_DSP_GETTRIGGER
:
473 CS_DBGOUT(CS_IOCTL
, 4, printk("SNDCTL_DSP_GETTRIGGER: ") );
475 case SNDCTL_DSP_SETTRIGGER
:
476 CS_DBGOUT(CS_IOCTL
, 4, printk("SNDCTL_DSP_SETTRIGGER: ") );
478 case SNDCTL_DSP_GETOSPACE
:
479 CS_DBGOUT(CS_IOCTL
, 4, printk("SNDCTL_DSP_GETOSPACE: ") );
481 case SNDCTL_DSP_GETISPACE
:
482 CS_DBGOUT(CS_IOCTL
, 4, printk("SNDCTL_DSP_GETISPACE: ") );
484 case SNDCTL_DSP_NONBLOCK
:
485 CS_DBGOUT(CS_IOCTL
, 4, printk("SNDCTL_DSP_NONBLOCK: ") );
487 case SNDCTL_DSP_GETODELAY
:
488 CS_DBGOUT(CS_IOCTL
, 4, printk("SNDCTL_DSP_GETODELAY: ") );
490 case SNDCTL_DSP_GETIPTR
:
491 CS_DBGOUT(CS_IOCTL
, 4, printk("SNDCTL_DSP_GETIPTR: ") );
493 case SNDCTL_DSP_GETOPTR
:
494 CS_DBGOUT(CS_IOCTL
, 4, printk("SNDCTL_DSP_GETOPTR: ") );
496 case SNDCTL_DSP_GETBLKSIZE
:
497 CS_DBGOUT(CS_IOCTL
, 4, printk("SNDCTL_DSP_GETBLKSIZE: ") );
499 case SNDCTL_DSP_SETFRAGMENT
:
500 CS_DBGOUT(CS_IOCTL
, 4, printk("SNDCTL_DSP_SETFRAGMENT: ") );
502 case SNDCTL_DSP_SUBDIVIDE
:
503 CS_DBGOUT(CS_IOCTL
, 4, printk("SNDCTL_DSP_SUBDIVIDE: ") );
505 case SOUND_PCM_READ_RATE
:
506 CS_DBGOUT(CS_IOCTL
, 4, printk("SOUND_PCM_READ_RATE: ") );
508 case SOUND_PCM_READ_CHANNELS
:
509 CS_DBGOUT(CS_IOCTL
, 4, printk("SOUND_PCM_READ_CHANNELS: ") );
511 case SOUND_PCM_READ_BITS
:
512 CS_DBGOUT(CS_IOCTL
, 4, printk("SOUND_PCM_READ_BITS: ") );
514 case SOUND_PCM_WRITE_FILTER
:
515 CS_DBGOUT(CS_IOCTL
, 4, printk("SOUND_PCM_WRITE_FILTER: ") );
517 case SNDCTL_DSP_SETSYNCRO
:
518 CS_DBGOUT(CS_IOCTL
, 4, printk("SNDCTL_DSP_SETSYNCRO: ") );
520 case SOUND_PCM_READ_FILTER
:
521 CS_DBGOUT(CS_IOCTL
, 4, printk("SOUND_PCM_READ_FILTER: ") );
523 case SOUND_MIXER_PRIVATE1
:
524 CS_DBGOUT(CS_IOCTL
, 4, printk("SOUND_MIXER_PRIVATE1: ") );
526 case SOUND_MIXER_PRIVATE2
:
527 CS_DBGOUT(CS_IOCTL
, 4, printk("SOUND_MIXER_PRIVATE2: ") );
529 case SOUND_MIXER_PRIVATE3
:
530 CS_DBGOUT(CS_IOCTL
, 4, printk("SOUND_MIXER_PRIVATE3: ") );
532 case SOUND_MIXER_PRIVATE4
:
533 CS_DBGOUT(CS_IOCTL
, 4, printk("SOUND_MIXER_PRIVATE4: ") );
535 case SOUND_MIXER_PRIVATE5
:
536 CS_DBGOUT(CS_IOCTL
, 4, printk("SOUND_MIXER_PRIVATE5: ") );
538 case SOUND_MIXER_INFO
:
539 CS_DBGOUT(CS_IOCTL
, 4, printk("SOUND_MIXER_INFO: ") );
541 case SOUND_OLD_MIXER_INFO
:
542 CS_DBGOUT(CS_IOCTL
, 4, printk("SOUND_OLD_MIXER_INFO: ") );
545 switch (_IOC_NR(x
)) {
546 case SOUND_MIXER_VOLUME
:
547 CS_DBGOUT(CS_IOCTL
, 4, printk("SOUND_MIXER_VOLUME: ") );
549 case SOUND_MIXER_SPEAKER
:
550 CS_DBGOUT(CS_IOCTL
, 4, printk("SOUND_MIXER_SPEAKER: ") );
552 case SOUND_MIXER_RECLEV
:
553 CS_DBGOUT(CS_IOCTL
, 4, printk("SOUND_MIXER_RECLEV: ") );
555 case SOUND_MIXER_MIC
:
556 CS_DBGOUT(CS_IOCTL
, 4, printk("SOUND_MIXER_MIC: ") );
558 case SOUND_MIXER_SYNTH
:
559 CS_DBGOUT(CS_IOCTL
, 4, printk("SOUND_MIXER_SYNTH: ") );
561 case SOUND_MIXER_RECSRC
:
562 CS_DBGOUT(CS_IOCTL
, 4, printk("SOUND_MIXER_RECSRC: ") );
564 case SOUND_MIXER_DEVMASK
:
565 CS_DBGOUT(CS_IOCTL
, 4, printk("SOUND_MIXER_DEVMASK: ") );
567 case SOUND_MIXER_RECMASK
:
568 CS_DBGOUT(CS_IOCTL
, 4, printk("SOUND_MIXER_RECMASK: ") );
570 case SOUND_MIXER_STEREODEVS
:
571 CS_DBGOUT(CS_IOCTL
, 4, printk("SOUND_MIXER_STEREODEVS: ") );
573 case SOUND_MIXER_CAPS
:
574 CS_DBGOUT(CS_IOCTL
, 4, printk("SOUND_MIXER_CAPS:") );
578 if (i
>= SOUND_MIXER_NRDEVICES
|| !(vidx
= mixtable1
[i
])) {
579 CS_DBGOUT(CS_IOCTL
, 4, printk("UNKNOWN IOCTL: 0x%.8x NR=%d ",x
,i
) );
581 CS_DBGOUT(CS_IOCTL
, 4, printk("SOUND_MIXER_IOCTL AC9x: 0x%.8x NR=%d ",
587 CS_DBGOUT(CS_IOCTL
, 4, printk("command = 0x%x IOC_NR=%d\n",x
, _IOC_NR(x
)) );
592 * common I/O routines
595 static void cs461x_poke(struct cs_card
*codec
, unsigned long reg
, unsigned int val
)
597 writel(val
, codec
->ba1
.idx
[(reg
>> 16) & 3] + (reg
& 0xffff));
600 static unsigned int cs461x_peek(struct cs_card
*codec
, unsigned long reg
)
602 return readl(codec
->ba1
.idx
[(reg
>> 16) & 3] + (reg
& 0xffff));
605 static void cs461x_pokeBA0(struct cs_card
*codec
, unsigned long reg
, unsigned int val
)
607 writel(val
, codec
->ba0
+ reg
);
610 static unsigned int cs461x_peekBA0(struct cs_card
*codec
, unsigned long reg
)
612 return readl(codec
->ba0
+ reg
);
616 static u16
cs_ac97_get(struct ac97_codec
*dev
, u8 reg
);
617 static void cs_ac97_set(struct ac97_codec
*dev
, u8 reg
, u16 data
);
619 static struct cs_channel
*cs_alloc_pcm_channel(struct cs_card
*card
)
621 if (card
->channel
[1].used
== 1)
623 card
->channel
[1].used
= 1;
624 card
->channel
[1].num
= 1;
625 return &card
->channel
[1];
628 static struct cs_channel
*cs_alloc_rec_pcm_channel(struct cs_card
*card
)
630 if (card
->channel
[0].used
== 1)
632 card
->channel
[0].used
= 1;
633 card
->channel
[0].num
= 0;
634 return &card
->channel
[0];
637 static void cs_free_pcm_channel(struct cs_card
*card
, int channel
)
639 card
->channel
[channel
].state
= NULL
;
640 card
->channel
[channel
].used
= 0;
644 * setup a divisor value to help with conversion from
645 * 16bit Stereo, down to 8bit stereo/mono or 16bit mono.
646 * assign a divisor of 1 if using 16bit Stereo as that is
647 * the only format that the static image will capture.
649 static void cs_set_divisor(struct dmabuf
*dmabuf
)
651 if (dmabuf
->type
== CS_TYPE_DAC
)
653 else if (!(dmabuf
->fmt
& CS_FMT_STEREO
) &&
654 (dmabuf
->fmt
& CS_FMT_16BIT
))
656 else if ((dmabuf
->fmt
& CS_FMT_STEREO
) &&
657 !(dmabuf
->fmt
& CS_FMT_16BIT
))
659 else if (!(dmabuf
->fmt
& CS_FMT_STEREO
) &&
660 !(dmabuf
->fmt
& CS_FMT_16BIT
))
665 CS_DBGOUT(CS_PARMS
| CS_FUNCTION
, 8, printk(
666 "cs46xx: cs_set_divisor()- %s %d\n",
667 (dmabuf
->type
== CS_TYPE_ADC
) ? "ADC" : "DAC",
672 * mute some of the more prevalent registers to avoid popping.
674 static void cs_mute(struct cs_card
*card
, int state
)
676 struct ac97_codec
*dev
= card
->ac97_codec
[0];
678 CS_DBGOUT(CS_FUNCTION
, 2, printk(KERN_INFO
"cs46xx: cs_mute()+ %s\n",
679 (state
== CS_TRUE
) ? "Muting" : "UnMuting"));
681 if (state
== CS_TRUE
) {
683 * fix pops when powering up on thinkpads
685 card
->pm
.u32AC97_master_volume
= (u32
)cs_ac97_get( dev
,
686 (u8
)BA0_AC97_MASTER_VOLUME
);
687 card
->pm
.u32AC97_headphone_volume
= (u32
)cs_ac97_get(dev
,
688 (u8
)BA0_AC97_HEADPHONE_VOLUME
);
689 card
->pm
.u32AC97_master_volume_mono
= (u32
)cs_ac97_get(dev
,
690 (u8
)BA0_AC97_MASTER_VOLUME_MONO
);
691 card
->pm
.u32AC97_pcm_out_volume
= (u32
)cs_ac97_get(dev
,
692 (u8
)BA0_AC97_PCM_OUT_VOLUME
);
694 cs_ac97_set(dev
, (u8
)BA0_AC97_MASTER_VOLUME
, 0x8000);
695 cs_ac97_set(dev
, (u8
)BA0_AC97_HEADPHONE_VOLUME
, 0x8000);
696 cs_ac97_set(dev
, (u8
)BA0_AC97_MASTER_VOLUME_MONO
, 0x8000);
697 cs_ac97_set(dev
, (u8
)BA0_AC97_PCM_OUT_VOLUME
, 0x8000);
699 cs_ac97_set(dev
, (u8
)BA0_AC97_MASTER_VOLUME
, card
->pm
.u32AC97_master_volume
);
700 cs_ac97_set(dev
, (u8
)BA0_AC97_HEADPHONE_VOLUME
, card
->pm
.u32AC97_headphone_volume
);
701 cs_ac97_set(dev
, (u8
)BA0_AC97_MASTER_VOLUME_MONO
, card
->pm
.u32AC97_master_volume_mono
);
702 cs_ac97_set(dev
, (u8
)BA0_AC97_PCM_OUT_VOLUME
, card
->pm
.u32AC97_pcm_out_volume
);
704 CS_DBGOUT(CS_FUNCTION
, 2, printk(KERN_INFO
"cs46xx: cs_mute()-\n"));
707 /* set playback sample rate */
708 static unsigned int cs_set_dac_rate(struct cs_state
* state
, unsigned int rate
)
710 struct dmabuf
*dmabuf
= &state
->dmabuf
;
711 unsigned int tmp1
, tmp2
;
712 unsigned int phiIncr
;
713 unsigned int correctionPerGOF
, correctionPerSec
;
716 CS_DBGOUT(CS_FUNCTION
, 2, printk("cs46xx: cs_set_dac_rate()+ %d\n",rate
) );
719 * Compute the values used to drive the actual sample rate conversion.
720 * The following formulas are being computed, using inline assembly
721 * since we need to use 64 bit arithmetic to compute the values:
723 * phiIncr = floor((Fs,in * 2^26) / Fs,out)
724 * correctionPerGOF = floor((Fs,in * 2^26 - Fs,out * phiIncr) /
726 * ulCorrectionPerSec = Fs,in * 2^26 - Fs,out * phiIncr -M
727 * GOF_PER_SEC * correctionPerGOF
731 * phiIncr:other = dividend:remainder((Fs,in * 2^26) / Fs,out)
732 * correctionPerGOF:correctionPerSec =
733 * dividend:remainder(ulOther / GOF_PER_SEC)
736 phiIncr
= tmp1
/ 48000;
737 tmp1
-= phiIncr
* 48000;
742 tmp1
-= tmp2
* 48000;
743 correctionPerGOF
= tmp1
/ GOF_PER_SEC
;
744 tmp1
-= correctionPerGOF
* GOF_PER_SEC
;
745 correctionPerSec
= tmp1
;
748 * Fill in the SampleRateConverter control block.
750 spin_lock_irqsave(&state
->card
->lock
, flags
);
751 cs461x_poke(state
->card
, BA1_PSRC
,
752 ((correctionPerSec
<< 16) & 0xFFFF0000) | (correctionPerGOF
& 0xFFFF));
753 cs461x_poke(state
->card
, BA1_PPI
, phiIncr
);
754 spin_unlock_irqrestore(&state
->card
->lock
, flags
);
757 CS_DBGOUT(CS_FUNCTION
, 2, printk("cs46xx: cs_set_dac_rate()- %d\n",rate
) );
761 /* set recording sample rate */
762 static unsigned int cs_set_adc_rate(struct cs_state
*state
, unsigned int rate
)
764 struct dmabuf
*dmabuf
= &state
->dmabuf
;
765 struct cs_card
*card
= state
->card
;
766 unsigned int phiIncr
, coeffIncr
, tmp1
, tmp2
;
767 unsigned int correctionPerGOF
, correctionPerSec
, initialDelay
;
768 unsigned int frameGroupLength
, cnt
;
770 CS_DBGOUT(CS_FUNCTION
, 2, printk("cs46xx: cs_set_adc_rate()+ %d\n",rate
) );
773 * We can only decimate by up to a factor of 1/9th the hardware rate.
774 * Correct the value if an attempt is made to stray outside that limit.
776 if ((rate
* 9) < 48000)
780 * We can not capture at at rate greater than the Input Rate (48000).
781 * Return an error if an attempt is made to stray outside that limit.
787 * Compute the values used to drive the actual sample rate conversion.
788 * The following formulas are being computed, using inline assembly
789 * since we need to use 64 bit arithmetic to compute the values:
791 * coeffIncr = -floor((Fs,out * 2^23) / Fs,in)
792 * phiIncr = floor((Fs,in * 2^26) / Fs,out)
793 * correctionPerGOF = floor((Fs,in * 2^26 - Fs,out * phiIncr) /
795 * correctionPerSec = Fs,in * 2^26 - Fs,out * phiIncr -
796 * GOF_PER_SEC * correctionPerGOF
797 * initialDelay = ceil((24 * Fs,in) / Fs,out)
801 * coeffIncr = neg(dividend((Fs,out * 2^23) / Fs,in))
802 * phiIncr:ulOther = dividend:remainder((Fs,in * 2^26) / Fs,out)
803 * correctionPerGOF:correctionPerSec =
804 * dividend:remainder(ulOther / GOF_PER_SEC)
805 * initialDelay = dividend(((24 * Fs,in) + Fs,out - 1) / Fs,out)
808 coeffIncr
= tmp1
/ 48000;
809 tmp1
-= coeffIncr
* 48000;
812 coeffIncr
+= tmp1
/ 48000;
813 coeffIncr
^= 0xFFFFFFFF;
816 phiIncr
= tmp1
/ rate
;
817 tmp1
-= phiIncr
* rate
;
823 correctionPerGOF
= tmp1
/ GOF_PER_SEC
;
824 tmp1
-= correctionPerGOF
* GOF_PER_SEC
;
825 correctionPerSec
= tmp1
;
826 initialDelay
= ((48000 * 24) + rate
- 1) / rate
;
829 * Fill in the VariDecimate control block.
831 spin_lock_irqsave(&card
->lock
, flags
);
832 cs461x_poke(card
, BA1_CSRC
,
833 ((correctionPerSec
<< 16) & 0xFFFF0000) | (correctionPerGOF
& 0xFFFF));
834 cs461x_poke(card
, BA1_CCI
, coeffIncr
);
835 cs461x_poke(card
, BA1_CD
,
836 (((BA1_VARIDEC_BUF_1
+ (initialDelay
<< 2)) << 16) & 0xFFFF0000) | 0x80);
837 cs461x_poke(card
, BA1_CPI
, phiIncr
);
838 spin_unlock_irqrestore(&card
->lock
, flags
);
841 * Figure out the frame group length for the write back task. Basically,
842 * this is just the factors of 24000 (2^6*3*5^3) that are not present in
843 * the output sample rate.
845 frameGroupLength
= 1;
846 for (cnt
= 2; cnt
<= 64; cnt
*= 2) {
847 if (((rate
/ cnt
) * cnt
) != rate
)
848 frameGroupLength
*= 2;
850 if (((rate
/ 3) * 3) != rate
) {
851 frameGroupLength
*= 3;
853 for (cnt
= 5; cnt
<= 125; cnt
*= 5) {
854 if (((rate
/ cnt
) * cnt
) != rate
)
855 frameGroupLength
*= 5;
859 * Fill in the WriteBack control block.
861 spin_lock_irqsave(&card
->lock
, flags
);
862 cs461x_poke(card
, BA1_CFG1
, frameGroupLength
);
863 cs461x_poke(card
, BA1_CFG2
, (0x00800000 | frameGroupLength
));
864 cs461x_poke(card
, BA1_CCST
, 0x0000FFFF);
865 cs461x_poke(card
, BA1_CSPB
, ((65536 * rate
) / 24000));
866 cs461x_poke(card
, (BA1_CSPB
+ 4), 0x0000FFFF);
867 spin_unlock_irqrestore(&card
->lock
, flags
);
869 CS_DBGOUT(CS_FUNCTION
, 2, printk("cs46xx: cs_set_adc_rate()- %d\n",rate
) );
873 /* prepare channel attributes for playback */
874 static void cs_play_setup(struct cs_state
*state
)
876 struct dmabuf
*dmabuf
= &state
->dmabuf
;
877 struct cs_card
*card
= state
->card
;
878 unsigned int tmp
, Count
, playFormat
;
880 CS_DBGOUT(CS_FUNCTION
, 2, printk("cs46xx: cs_play_setup()+\n") );
881 cs461x_poke(card
, BA1_PVOL
, 0x80008000);
883 cs461x_poke(card
, BA1_PBA
, virt_to_bus(dmabuf
->pbuf
));
886 playFormat
=cs461x_peek(card
, BA1_PFIE
);
887 if ((dmabuf
->fmt
& CS_FMT_STEREO
)) {
888 playFormat
&= ~DMA_RQ_C2_AC_MONO_TO_STEREO
;
891 playFormat
|= DMA_RQ_C2_AC_MONO_TO_STEREO
;
893 if ((dmabuf
->fmt
& CS_FMT_16BIT
)) {
894 playFormat
&= ~(DMA_RQ_C2_AC_8_TO_16_BIT
895 | DMA_RQ_C2_AC_SIGNED_CONVERT
);
898 playFormat
|= (DMA_RQ_C2_AC_8_TO_16_BIT
899 | DMA_RQ_C2_AC_SIGNED_CONVERT
);
901 cs461x_poke(card
, BA1_PFIE
, playFormat
);
903 tmp
= cs461x_peek(card
, BA1_PDTC
);
905 cs461x_poke(card
, BA1_PDTC
, tmp
| --Count
);
907 CS_DBGOUT(CS_FUNCTION
, 2, printk("cs46xx: cs_play_setup()-\n") );
910 static struct InitStruct
914 } InitArray
[] = { {0x00000040, 0x3fc0000f},
915 {0x0000004c, 0x04800000},
917 {0x000000b3, 0x00000780},
918 {0x000000b7, 0x00000000},
919 {0x000000bc, 0x07800000},
921 {0x000000cd, 0x00800000},
925 * "SetCaptureSPValues()" -- Initialize record task values before each
928 static void SetCaptureSPValues(struct cs_card
*card
)
931 CS_DBGOUT(CS_FUNCTION
, 8, printk("cs46xx: SetCaptureSPValues()+\n") );
932 for (i
= 0; i
< sizeof(InitArray
) / sizeof(struct InitStruct
); i
++) {
933 offset
= InitArray
[i
].off
*4; /* 8bit to 32bit offset value */
934 cs461x_poke(card
, offset
, InitArray
[i
].val
);
936 CS_DBGOUT(CS_FUNCTION
, 8, printk("cs46xx: SetCaptureSPValues()-\n") );
939 /* prepare channel attributes for recording */
940 static void cs_rec_setup(struct cs_state
*state
)
942 struct cs_card
*card
= state
->card
;
943 struct dmabuf
*dmabuf
= &state
->dmabuf
;
945 CS_DBGOUT(CS_FUNCTION
, 2, printk("cs46xx: cs_rec_setup()+\n"));
946 SetCaptureSPValues(card
);
949 * set the attenuation to 0dB
951 cs461x_poke(card
, BA1_CVOL
, 0x80008000);
954 * set the physical address of the capture buffer into the SP
956 cs461x_poke(card
, BA1_CBA
, virt_to_bus(dmabuf
->rawbuf
));
958 CS_DBGOUT(CS_FUNCTION
, 2, printk("cs46xx: cs_rec_setup()-\n") );
962 /* get current playback/recording dma buffer pointer (byte offset from LBA),
963 called with spinlock held! */
965 static inline unsigned cs_get_dma_addr(struct cs_state
*state
)
967 struct dmabuf
*dmabuf
= &state
->dmabuf
;
970 if ( (!(dmabuf
->enable
& DAC_RUNNING
)) &&
971 (!(dmabuf
->enable
& ADC_RUNNING
) ) )
973 CS_DBGOUT(CS_ERROR
, 2, printk(
974 "cs46xx: ERROR cs_get_dma_addr(): not enabled \n") );
979 * granularity is byte boundary, good part.
981 if (dmabuf
->enable
& DAC_RUNNING
)
982 offset
= cs461x_peek(state
->card
, BA1_PBA
);
983 else /* ADC_RUNNING must be set */
984 offset
= cs461x_peek(state
->card
, BA1_CBA
);
986 CS_DBGOUT(CS_PARMS
| CS_FUNCTION
, 9,
987 printk("cs46xx: cs_get_dma_addr() %d\n",offset
) );
988 offset
= (u32
)bus_to_virt((unsigned long)offset
) - (u32
)dmabuf
->rawbuf
;
989 CS_DBGOUT(CS_PARMS
| CS_FUNCTION
, 8,
990 printk("cs46xx: cs_get_dma_addr()- %d\n",offset
) );
994 static void resync_dma_ptrs(struct cs_state
*state
)
996 struct dmabuf
*dmabuf
;
998 CS_DBGOUT(CS_FUNCTION
, 2, printk("cs46xx: resync_dma_ptrs()+ \n") );
1000 dmabuf
= &state
->dmabuf
;
1001 dmabuf
->hwptr
=dmabuf
->swptr
= 0;
1002 dmabuf
->pringbuf
= 0;
1004 CS_DBGOUT(CS_FUNCTION
, 2, printk("cs46xx: resync_dma_ptrs()- \n") );
1007 /* Stop recording (lock held) */
1008 static inline void __stop_adc(struct cs_state
*state
)
1010 struct dmabuf
*dmabuf
= &state
->dmabuf
;
1011 struct cs_card
*card
= state
->card
;
1014 dmabuf
->enable
&= ~ADC_RUNNING
;
1016 tmp
= cs461x_peek(card
, BA1_CCTL
);
1018 cs461x_poke(card
, BA1_CCTL
, tmp
);
1021 static void stop_adc(struct cs_state
*state
)
1023 unsigned long flags
;
1025 CS_DBGOUT(CS_FUNCTION
, 2, printk("cs46xx: stop_adc()+ \n") );
1026 spin_lock_irqsave(&state
->card
->lock
, flags
);
1028 spin_unlock_irqrestore(&state
->card
->lock
, flags
);
1029 CS_DBGOUT(CS_FUNCTION
, 2, printk("cs46xx: stop_adc()- \n") );
1032 static void start_adc(struct cs_state
*state
)
1034 struct dmabuf
*dmabuf
= &state
->dmabuf
;
1035 struct cs_card
*card
= state
->card
;
1036 unsigned long flags
;
1039 spin_lock_irqsave(&card
->lock
, flags
);
1040 if (!(dmabuf
->enable
& ADC_RUNNING
) &&
1041 ((dmabuf
->mapped
|| dmabuf
->count
< (signed)dmabuf
->dmasize
)
1042 && dmabuf
->ready
) &&
1043 ((card
->pm
.flags
& CS46XX_PM_IDLE
) ||
1044 (card
->pm
.flags
& CS46XX_PM_RESUMED
)) )
1046 dmabuf
->enable
|= ADC_RUNNING
;
1047 cs_set_divisor(dmabuf
);
1048 tmp
= cs461x_peek(card
, BA1_CCTL
);
1051 CS_DBGOUT(CS_FUNCTION
, 2, printk(
1052 "cs46xx: start_adc() poke 0x%x \n",tmp
) );
1053 cs461x_poke(card
, BA1_CCTL
, tmp
);
1055 spin_unlock_irqrestore(&card
->lock
, flags
);
1058 /* stop playback (lock held) */
1059 static inline void __stop_dac(struct cs_state
*state
)
1061 struct dmabuf
*dmabuf
= &state
->dmabuf
;
1062 struct cs_card
*card
= state
->card
;
1065 dmabuf
->enable
&= ~DAC_RUNNING
;
1067 tmp
=cs461x_peek(card
, BA1_PCTL
);
1069 cs461x_poke(card
, BA1_PCTL
, tmp
);
1072 static void stop_dac(struct cs_state
*state
)
1074 unsigned long flags
;
1076 CS_DBGOUT(CS_FUNCTION
, 2, printk("cs46xx: stop_dac()+ \n") );
1077 spin_lock_irqsave(&state
->card
->lock
, flags
);
1079 spin_unlock_irqrestore(&state
->card
->lock
, flags
);
1080 CS_DBGOUT(CS_FUNCTION
, 2, printk("cs46xx: stop_dac()- \n") );
1083 static void start_dac(struct cs_state
*state
)
1085 struct dmabuf
*dmabuf
= &state
->dmabuf
;
1086 struct cs_card
*card
= state
->card
;
1087 unsigned long flags
;
1090 CS_DBGOUT(CS_FUNCTION
, 2, printk("cs46xx: start_dac()+ \n") );
1091 spin_lock_irqsave(&card
->lock
, flags
);
1092 if (!(dmabuf
->enable
& DAC_RUNNING
) &&
1093 ((dmabuf
->mapped
|| dmabuf
->count
> 0) && dmabuf
->ready
) &&
1094 ((card
->pm
.flags
& CS46XX_PM_IDLE
) ||
1095 (card
->pm
.flags
& CS46XX_PM_RESUMED
)) )
1097 dmabuf
->enable
|= DAC_RUNNING
;
1098 tmp
= cs461x_peek(card
, BA1_PCTL
);
1101 CS_DBGOUT(CS_PARMS
, 6, printk(
1102 "cs46xx: start_dac() poke card=%p tmp=0x%.08x addr=%p \n",
1103 card
, (unsigned)tmp
,
1104 card
->ba1
.idx
[(BA1_PCTL
>> 16) & 3]+(BA1_PCTL
&0xffff) ) );
1105 cs461x_poke(card
, BA1_PCTL
, tmp
);
1107 spin_unlock_irqrestore(&card
->lock
, flags
);
1108 CS_DBGOUT(CS_FUNCTION
, 2, printk("cs46xx: start_dac()- \n") );
1111 #define DMABUF_MINORDER 1
1114 * allocate DMA buffer, playback and recording buffers are separate.
1116 static int alloc_dmabuf(struct cs_state
*state
)
1119 struct cs_card
*card
=state
->card
;
1120 struct dmabuf
*dmabuf
= &state
->dmabuf
;
1121 void *rawbuf
= NULL
;
1122 void *tmpbuff
= NULL
;
1124 struct page
*map
, *mapend
;
1127 dmabuf
->ready
= dmabuf
->mapped
= 0;
1130 * check for order within limits, but do not overwrite value.
1132 if ((defaultorder
> 1) && (defaultorder
< 12))
1137 for (order
= df
; order
>= DMABUF_MINORDER
; order
--)
1138 if ((rawbuf
= (void *)pci_alloc_consistent(
1139 card
->pci_dev
, PAGE_SIZE
<< order
, &dmabuf
->dmaaddr
)))
1142 CS_DBGOUT(CS_ERROR
, 1, printk(KERN_ERR
1143 "cs46xx: alloc_dmabuf(): unable to allocate rawbuf\n"));
1146 dmabuf
->buforder
= order
;
1147 dmabuf
->rawbuf
= rawbuf
;
1148 // Now mark the pages as reserved; otherwise the
1149 // remap_pfn_range() in cs46xx_mmap doesn't work.
1150 // 1. get index to last page in mem_map array for rawbuf.
1151 mapend
= virt_to_page(dmabuf
->rawbuf
+
1152 (PAGE_SIZE
<< dmabuf
->buforder
) - 1);
1154 // 2. mark each physical page in range as 'reserved'.
1155 for (map
= virt_to_page(dmabuf
->rawbuf
); map
<= mapend
; map
++)
1156 cs4x_mem_map_reserve(map
);
1158 CS_DBGOUT(CS_PARMS
, 9, printk("cs46xx: alloc_dmabuf(): allocated %ld (order = %d) bytes at %p\n",
1159 PAGE_SIZE
<< order
, order
, rawbuf
) );
1162 * only allocate the conversion buffer for the ADC
1164 if (dmabuf
->type
== CS_TYPE_DAC
) {
1165 dmabuf
->tmpbuff
= NULL
;
1166 dmabuf
->buforder_tmpbuff
= 0;
1170 * now the temp buffer for 16/8 conversions
1173 tmpbuff
= (void *) pci_alloc_consistent(
1174 card
->pci_dev
, PAGE_SIZE
<< order
, &dmabuf
->dmaaddr_tmpbuff
);
1178 CS_DBGOUT(CS_PARMS
, 9, printk("cs46xx: allocated %ld (order = %d) bytes at %p\n",
1179 PAGE_SIZE
<< order
, order
, tmpbuff
) );
1181 dmabuf
->tmpbuff
= tmpbuff
;
1182 dmabuf
->buforder_tmpbuff
= order
;
1184 // Now mark the pages as reserved; otherwise the
1185 // remap_pfn_range() in cs46xx_mmap doesn't work.
1186 // 1. get index to last page in mem_map array for rawbuf.
1187 mapend
= virt_to_page(dmabuf
->tmpbuff
+
1188 (PAGE_SIZE
<< dmabuf
->buforder_tmpbuff
) - 1);
1190 // 2. mark each physical page in range as 'reserved'.
1191 for (map
= virt_to_page(dmabuf
->tmpbuff
); map
<= mapend
; map
++)
1192 cs4x_mem_map_reserve(map
);
1196 /* free DMA buffer */
1197 static void dealloc_dmabuf(struct cs_state
*state
)
1199 struct dmabuf
*dmabuf
= &state
->dmabuf
;
1200 struct page
*map
, *mapend
;
1202 if (dmabuf
->rawbuf
) {
1203 // Undo prog_dmabuf()'s marking the pages as reserved
1204 mapend
= virt_to_page(dmabuf
->rawbuf
+
1205 (PAGE_SIZE
<< dmabuf
->buforder
) - 1);
1206 for (map
= virt_to_page(dmabuf
->rawbuf
); map
<= mapend
; map
++)
1207 cs4x_mem_map_unreserve(map
);
1208 free_dmabuf(state
->card
, dmabuf
);
1211 if (dmabuf
->tmpbuff
) {
1212 // Undo prog_dmabuf()'s marking the pages as reserved
1213 mapend
= virt_to_page(dmabuf
->tmpbuff
+
1214 (PAGE_SIZE
<< dmabuf
->buforder_tmpbuff
) - 1);
1215 for (map
= virt_to_page(dmabuf
->tmpbuff
); map
<= mapend
; map
++)
1216 cs4x_mem_map_unreserve(map
);
1217 free_dmabuf2(state
->card
, dmabuf
);
1220 dmabuf
->rawbuf
= NULL
;
1221 dmabuf
->tmpbuff
= NULL
;
1222 dmabuf
->mapped
= dmabuf
->ready
= 0;
1226 static int __prog_dmabuf(struct cs_state
*state
)
1228 struct dmabuf
*dmabuf
= &state
->dmabuf
;
1229 unsigned long flags
;
1230 unsigned long allocated_pages
, allocated_bytes
;
1231 unsigned long tmp1
, tmp2
, fmt
=0;
1232 unsigned long *ptmp
= (unsigned long *) dmabuf
->pbuf
;
1233 unsigned long SGarray
[9], nSGpages
=0;
1236 CS_DBGOUT(CS_FUNCTION
, 4, printk("cs46xx: prog_dmabuf()+ \n"));
1238 * check for CAPTURE and use only non-sg for initial release
1240 if (dmabuf
->type
== CS_TYPE_ADC
) {
1241 CS_DBGOUT(CS_FUNCTION
, 4, printk("cs46xx: prog_dmabuf() ADC\n"));
1243 * add in non-sg support for capture.
1245 spin_lock_irqsave(&state
->card
->lock
, flags
);
1246 /* add code to reset the rawbuf memory. TRW */
1247 resync_dma_ptrs(state
);
1248 dmabuf
->total_bytes
= dmabuf
->blocks
= 0;
1249 dmabuf
->count
= dmabuf
->error
= dmabuf
->underrun
= 0;
1253 spin_unlock_irqrestore(&state
->card
->lock
, flags
);
1255 /* allocate DMA buffer if not allocated yet */
1256 if (!dmabuf
->rawbuf
|| !dmabuf
->tmpbuff
)
1257 if ((ret
= alloc_dmabuf(state
)))
1260 * static image only supports 16Bit signed, stereo - hard code fmt
1262 fmt
= CS_FMT_16BIT
| CS_FMT_STEREO
;
1264 dmabuf
->numfrag
= 2;
1265 dmabuf
->fragsize
= 2048;
1266 dmabuf
->fragsamples
= 2048 >> sample_shift
[fmt
];
1267 dmabuf
->dmasize
= 4096;
1268 dmabuf
->fragshift
= 11;
1270 memset(dmabuf
->rawbuf
, (fmt
& CS_FMT_16BIT
) ? 0 : 0x80,
1272 memset(dmabuf
->tmpbuff
, (fmt
& CS_FMT_16BIT
) ? 0 : 0x80,
1273 PAGE_SIZE
<<dmabuf
->buforder_tmpbuff
);
1276 * Now set up the ring
1279 spin_lock_irqsave(&state
->card
->lock
, flags
);
1280 cs_rec_setup(state
);
1281 spin_unlock_irqrestore(&state
->card
->lock
, flags
);
1283 /* set the ready flag for the dma buffer */
1286 CS_DBGOUT(CS_PARMS
, 4, printk(
1287 "cs46xx: prog_dmabuf(): CAPTURE rate=%d fmt=0x%x numfrag=%d "
1288 "fragsize=%d dmasize=%d\n",
1289 dmabuf
->rate
, dmabuf
->fmt
, dmabuf
->numfrag
,
1290 dmabuf
->fragsize
, dmabuf
->dmasize
) );
1292 CS_DBGOUT(CS_FUNCTION
, 4, printk("cs46xx: prog_dmabuf()- 0 \n"));
1294 } else if (dmabuf
->type
== CS_TYPE_DAC
) {
1298 CS_DBGOUT(CS_FUNCTION
, 4, printk("cs46xx: prog_dmabuf() DAC\n"));
1299 spin_lock_irqsave(&state
->card
->lock
, flags
);
1300 resync_dma_ptrs(state
);
1301 dmabuf
->total_bytes
= dmabuf
->blocks
= 0;
1302 dmabuf
->count
= dmabuf
->error
= dmabuf
->underrun
= 0;
1306 spin_unlock_irqrestore(&state
->card
->lock
, flags
);
1308 /* allocate DMA buffer if not allocated yet */
1309 if (!dmabuf
->rawbuf
)
1310 if ((ret
= alloc_dmabuf(state
)))
1313 allocated_pages
= 1 << dmabuf
->buforder
;
1314 allocated_bytes
= allocated_pages
*PAGE_SIZE
;
1316 if (allocated_pages
< 2) {
1317 CS_DBGOUT(CS_FUNCTION
, 4, printk(
1318 "cs46xx: prog_dmabuf() Error: allocated_pages too small (%d)\n",
1319 (unsigned)allocated_pages
));
1323 /* Use all the pages allocated, fragsize 4k. */
1324 /* Use 'pbuf' for S/G page map table. */
1325 dmabuf
->SGok
= 1; /* Use S/G. */
1327 nSGpages
= allocated_bytes
/4096; /* S/G pages always 4k. */
1329 /* Set up S/G variables. */
1330 *ptmp
= virt_to_bus(dmabuf
->rawbuf
);
1331 *(ptmp
+ 1) = 0x00000008;
1332 for (tmp1
= 1; tmp1
< nSGpages
; tmp1
++) {
1333 *(ptmp
+ 2 * tmp1
) = virt_to_bus((dmabuf
->rawbuf
) + 4096 * tmp1
);
1334 if (tmp1
== nSGpages
- 1)
1337 tmp2
= 0x80000000 + 8 * (tmp1
+ 1);
1338 *(ptmp
+ 2 * tmp1
+ 1) = tmp2
;
1340 SGarray
[0] = 0x82c0200d;
1341 SGarray
[1] = 0xffff0000;
1343 SGarray
[3] = 0x00010600;
1344 SGarray
[4] = *(ptmp
+2);
1345 SGarray
[5] = 0x80000010;
1347 SGarray
[7] = *(ptmp
+2);
1348 SGarray
[8] = (virt_to_bus(dmabuf
->pbuf
) & 0xffff000) | 0x10;
1351 dmabuf
->numfrag
= nSGpages
;
1352 dmabuf
->fragsize
= 4096;
1353 dmabuf
->fragsamples
= 4096 >> sample_shift
[dmabuf
->fmt
];
1354 dmabuf
->fragshift
= 12;
1355 dmabuf
->dmasize
= dmabuf
->numfrag
* 4096;
1357 SGarray
[0] = 0xf2c0000f;
1358 SGarray
[1] = 0x00000200;
1360 SGarray
[3] = 0x00010600;
1361 SGarray
[4]=SGarray
[5]=SGarray
[6]=SGarray
[7]=SGarray
[8] = 0;
1362 dmabuf
->numfrag
= 2;
1363 dmabuf
->fragsize
= 2048;
1364 dmabuf
->fragsamples
= 2048 >> sample_shift
[dmabuf
->fmt
];
1365 dmabuf
->dmasize
= 4096;
1366 dmabuf
->fragshift
= 11;
1368 for (tmp1
= 0; tmp1
< sizeof(SGarray
) / 4; tmp1
++)
1369 cs461x_poke(state
->card
, BA1_PDTC
+tmp1
* 4, SGarray
[tmp1
]);
1371 memset(dmabuf
->rawbuf
, (dmabuf
->fmt
& CS_FMT_16BIT
) ? 0 : 0x80,
1375 * Now set up the ring
1378 spin_lock_irqsave(&state
->card
->lock
, flags
);
1379 cs_play_setup(state
);
1380 spin_unlock_irqrestore(&state
->card
->lock
, flags
);
1382 /* set the ready flag for the dma buffer */
1385 CS_DBGOUT(CS_PARMS
, 4, printk(
1386 "cs46xx: prog_dmabuf(): PLAYBACK rate=%d fmt=0x%x numfrag=%d "
1387 "fragsize=%d dmasize=%d\n",
1388 dmabuf
->rate
, dmabuf
->fmt
, dmabuf
->numfrag
,
1389 dmabuf
->fragsize
, dmabuf
->dmasize
) );
1391 CS_DBGOUT(CS_FUNCTION
, 4, printk("cs46xx: prog_dmabuf()- \n"));
1394 CS_DBGOUT(CS_FUNCTION
, 4, printk("cs46xx: prog_dmabuf()- Invalid Type %d\n",
1400 static int prog_dmabuf(struct cs_state
*state
)
1404 mutex_lock(&state
->sem
);
1405 ret
= __prog_dmabuf(state
);
1406 mutex_unlock(&state
->sem
);
1411 static void cs_clear_tail(struct cs_state
*state
)
1415 static int drain_dac(struct cs_state
*state
, int nonblock
)
1417 DECLARE_WAITQUEUE(wait
, current
);
1418 struct dmabuf
*dmabuf
= &state
->dmabuf
;
1419 struct cs_card
*card
=state
->card
;
1420 unsigned long flags
;
1424 CS_DBGOUT(CS_FUNCTION
, 4, printk("cs46xx: drain_dac()+ \n"));
1425 if (dmabuf
->mapped
|| !dmabuf
->ready
)
1427 CS_DBGOUT(CS_FUNCTION
, 4, printk("cs46xx: drain_dac()- 0, not ready\n"));
1431 add_wait_queue(&dmabuf
->wait
, &wait
);
1433 /* It seems that we have to set the current state to TASK_INTERRUPTIBLE
1434 every time to make the process really go to sleep */
1435 current
->state
= TASK_INTERRUPTIBLE
;
1437 spin_lock_irqsave(&state
->card
->lock
, flags
);
1438 count
= dmabuf
->count
;
1439 spin_unlock_irqrestore(&state
->card
->lock
, flags
);
1444 if (signal_pending(current
))
1448 remove_wait_queue(&dmabuf
->wait
, &wait
);
1449 current
->state
= TASK_RUNNING
;
1453 tmo
= (dmabuf
->dmasize
* HZ
) / dmabuf
->rate
;
1454 tmo
>>= sample_shift
[dmabuf
->fmt
];
1455 tmo
+= (2048*HZ
)/dmabuf
->rate
;
1457 if (!schedule_timeout(tmo
? tmo
: 1) && tmo
){
1458 printk(KERN_ERR
"cs46xx: drain_dac, dma timeout? %d\n", count
);
1462 remove_wait_queue(&dmabuf
->wait
, &wait
);
1463 current
->state
= TASK_RUNNING
;
1464 if (signal_pending(current
)) {
1465 CS_DBGOUT(CS_FUNCTION
, 4, printk("cs46xx: drain_dac()- -ERESTARTSYS\n"));
1467 * set to silence and let that clear the fifos.
1469 cs461x_clear_serial_FIFOs(card
, CS_TYPE_DAC
);
1470 return -ERESTARTSYS
;
1473 CS_DBGOUT(CS_FUNCTION
, 4, printk("cs46xx: drain_dac()- 0\n"));
1478 /* update buffer manangement pointers, especially, dmabuf->count and dmabuf->hwptr */
1479 static void cs_update_ptr(struct cs_card
*card
, int wake
)
1481 struct cs_state
*state
;
1482 struct dmabuf
*dmabuf
;
1486 /* error handling and process wake up for ADC */
1487 state
= card
->states
[0];
1489 dmabuf
= &state
->dmabuf
;
1490 if (dmabuf
->enable
& ADC_RUNNING
) {
1491 /* update hardware pointer */
1492 hwptr
= cs_get_dma_addr(state
);
1494 diff
= (dmabuf
->dmasize
+ hwptr
- dmabuf
->hwptr
) % dmabuf
->dmasize
;
1495 CS_DBGOUT(CS_PARMS
, 9, printk(
1496 "cs46xx: cs_update_ptr()+ ADC hwptr=%d diff=%d\n",
1498 dmabuf
->hwptr
= hwptr
;
1499 dmabuf
->total_bytes
+= diff
;
1500 dmabuf
->count
+= diff
;
1501 if (dmabuf
->count
> dmabuf
->dmasize
)
1502 dmabuf
->count
= dmabuf
->dmasize
;
1504 if (dmabuf
->mapped
) {
1505 if (wake
&& dmabuf
->count
>= (signed)dmabuf
->fragsize
)
1506 wake_up(&dmabuf
->wait
);
1508 if (wake
&& dmabuf
->count
> 0)
1509 wake_up(&dmabuf
->wait
);
1517 state
= card
->states
[1];
1519 dmabuf
= &state
->dmabuf
;
1520 /* error handling and process wake up for DAC */
1521 if (dmabuf
->enable
& DAC_RUNNING
) {
1522 /* update hardware pointer */
1523 hwptr
= cs_get_dma_addr(state
);
1525 diff
= (dmabuf
->dmasize
+ hwptr
- dmabuf
->hwptr
) % dmabuf
->dmasize
;
1526 CS_DBGOUT(CS_PARMS
, 9, printk(
1527 "cs46xx: cs_update_ptr()+ DAC hwptr=%d diff=%d\n",
1529 dmabuf
->hwptr
= hwptr
;
1530 dmabuf
->total_bytes
+= diff
;
1531 if (dmabuf
->mapped
) {
1532 dmabuf
->count
+= diff
;
1533 if (wake
&& dmabuf
->count
>= (signed)dmabuf
->fragsize
)
1534 wake_up(&dmabuf
->wait
);
1536 * other drivers use fragsize, but don't see any sense
1537 * in that, since dmasize is the buffer asked for
1540 if (dmabuf
->count
> dmabuf
->dmasize
)
1541 dmabuf
->count
&= dmabuf
->dmasize
-1;
1543 dmabuf
->count
-= diff
;
1545 * backfill with silence and clear out the last
1546 * "diff" number of bytes.
1548 if (hwptr
>= diff
) {
1549 memset(dmabuf
->rawbuf
+ hwptr
- diff
,
1550 (dmabuf
->fmt
& CS_FMT_16BIT
) ? 0 : 0x80, diff
);
1552 memset(dmabuf
->rawbuf
,
1553 (dmabuf
->fmt
& CS_FMT_16BIT
) ? 0 : 0x80,
1555 memset((char *)dmabuf
->rawbuf
+
1556 dmabuf
->dmasize
+ hwptr
- diff
,
1557 (dmabuf
->fmt
& CS_FMT_16BIT
) ? 0 : 0x80,
1561 if (dmabuf
->count
< 0 || dmabuf
->count
> dmabuf
->dmasize
) {
1562 CS_DBGOUT(CS_ERROR
, 2, printk(KERN_INFO
1563 "cs46xx: ERROR DAC count<0 or count > dmasize (%d)\n",
1566 * buffer underrun or buffer overrun, reset the
1567 * count of bytes written back to 0.
1569 if (dmabuf
->count
< 0)
1570 dmabuf
->underrun
= 1;
1574 if (wake
&& dmabuf
->count
< (signed)dmabuf
->dmasize
/ 2)
1575 wake_up(&dmabuf
->wait
);
1582 /* hold spinlock for the following! */
1583 static void cs_handle_midi(struct cs_card
*card
)
1590 while (!(cs461x_peekBA0(card
, BA0_MIDSR
) & MIDSR_RBE
)) {
1591 ch
= cs461x_peekBA0(card
, BA0_MIDRP
);
1592 if (card
->midi
.icnt
< CS_MIDIINBUF
) {
1593 card
->midi
.ibuf
[card
->midi
.iwr
] = ch
;
1594 card
->midi
.iwr
= (card
->midi
.iwr
+ 1) % CS_MIDIINBUF
;
1600 wake_up(&card
->midi
.iwait
);
1602 while (!(cs461x_peekBA0(card
, BA0_MIDSR
) & MIDSR_TBF
) && card
->midi
.ocnt
> 0) {
1603 temp1
= ( card
->midi
.obuf
[card
->midi
.ord
] ) & 0x000000ff;
1604 cs461x_pokeBA0(card
, BA0_MIDWP
,temp1
);
1605 card
->midi
.ord
= (card
->midi
.ord
+ 1) % CS_MIDIOUTBUF
;
1607 if (card
->midi
.ocnt
< CS_MIDIOUTBUF
-16)
1611 wake_up(&card
->midi
.owait
);
1614 static irqreturn_t
cs_interrupt(int irq
, void *dev_id
, struct pt_regs
*regs
)
1616 struct cs_card
*card
= (struct cs_card
*)dev_id
;
1617 /* Single channel card */
1618 struct cs_state
*recstate
= card
->channel
[0].state
;
1619 struct cs_state
*playstate
= card
->channel
[1].state
;
1622 CS_DBGOUT(CS_INTERRUPT
, 9, printk("cs46xx: cs_interrupt()+ \n"));
1624 spin_lock(&card
->lock
);
1626 status
= cs461x_peekBA0(card
, BA0_HISR
);
1628 if ((status
& 0x7fffffff) == 0) {
1629 cs461x_pokeBA0(card
, BA0_HICR
, HICR_CHGM
|HICR_IEV
);
1630 spin_unlock(&card
->lock
);
1631 return IRQ_HANDLED
; /* Might be IRQ_NONE.. */
1635 * check for playback or capture interrupt only
1637 if (((status
& HISR_VC0
) && playstate
&& playstate
->dmabuf
.ready
) ||
1638 (((status
& HISR_VC1
) && recstate
&& recstate
->dmabuf
.ready
))) {
1639 CS_DBGOUT(CS_INTERRUPT
, 8, printk(
1640 "cs46xx: cs_interrupt() interrupt bit(s) set (0x%x)\n",status
));
1641 cs_update_ptr(card
, CS_TRUE
);
1644 if (status
& HISR_MIDI
)
1645 cs_handle_midi(card
);
1648 cs461x_pokeBA0(card
, BA0_HICR
, HICR_CHGM
|HICR_IEV
);
1649 spin_unlock(&card
->lock
);
1650 CS_DBGOUT(CS_INTERRUPT
, 9, printk("cs46xx: cs_interrupt()- \n"));
1655 /**********************************************************************/
1657 static ssize_t
cs_midi_read(struct file
*file
, char __user
*buffer
, size_t count
, loff_t
*ppos
)
1659 struct cs_card
*card
= file
->private_data
;
1661 unsigned long flags
;
1665 if (!access_ok(VERIFY_WRITE
, buffer
, count
))
1669 spin_lock_irqsave(&card
->lock
, flags
);
1670 ptr
= card
->midi
.ird
;
1671 cnt
= CS_MIDIINBUF
- ptr
;
1672 if (card
->midi
.icnt
< cnt
)
1673 cnt
= card
->midi
.icnt
;
1674 spin_unlock_irqrestore(&card
->lock
, flags
);
1678 if (file
->f_flags
& O_NONBLOCK
)
1679 return ret
? ret
: -EAGAIN
;
1680 interruptible_sleep_on(&card
->midi
.iwait
);
1681 if (signal_pending(current
))
1682 return ret
? ret
: -ERESTARTSYS
;
1685 if (copy_to_user(buffer
, card
->midi
.ibuf
+ ptr
, cnt
))
1686 return ret
? ret
: -EFAULT
;
1687 ptr
= (ptr
+ cnt
) % CS_MIDIINBUF
;
1688 spin_lock_irqsave(&card
->lock
, flags
);
1689 card
->midi
.ird
= ptr
;
1690 card
->midi
.icnt
-= cnt
;
1691 spin_unlock_irqrestore(&card
->lock
, flags
);
1700 static ssize_t
cs_midi_write(struct file
*file
, const char __user
*buffer
, size_t count
, loff_t
*ppos
)
1702 struct cs_card
*card
= file
->private_data
;
1704 unsigned long flags
;
1708 if (!access_ok(VERIFY_READ
, buffer
, count
))
1712 spin_lock_irqsave(&card
->lock
, flags
);
1713 ptr
= card
->midi
.owr
;
1714 cnt
= CS_MIDIOUTBUF
- ptr
;
1715 if (card
->midi
.ocnt
+ cnt
> CS_MIDIOUTBUF
)
1716 cnt
= CS_MIDIOUTBUF
- card
->midi
.ocnt
;
1718 cs_handle_midi(card
);
1719 spin_unlock_irqrestore(&card
->lock
, flags
);
1723 if (file
->f_flags
& O_NONBLOCK
)
1724 return ret
? ret
: -EAGAIN
;
1725 interruptible_sleep_on(&card
->midi
.owait
);
1726 if (signal_pending(current
))
1727 return ret
? ret
: -ERESTARTSYS
;
1730 if (copy_from_user(card
->midi
.obuf
+ ptr
, buffer
, cnt
))
1731 return ret
? ret
: -EFAULT
;
1732 ptr
= (ptr
+ cnt
) % CS_MIDIOUTBUF
;
1733 spin_lock_irqsave(&card
->lock
, flags
);
1734 card
->midi
.owr
= ptr
;
1735 card
->midi
.ocnt
+= cnt
;
1736 spin_unlock_irqrestore(&card
->lock
, flags
);
1740 spin_lock_irqsave(&card
->lock
, flags
);
1741 cs_handle_midi(card
);
1742 spin_unlock_irqrestore(&card
->lock
, flags
);
1748 static unsigned int cs_midi_poll(struct file
*file
, struct poll_table_struct
*wait
)
1750 struct cs_card
*card
= file
->private_data
;
1751 unsigned long flags
;
1752 unsigned int mask
= 0;
1754 if (file
->f_flags
& FMODE_WRITE
)
1755 poll_wait(file
, &card
->midi
.owait
, wait
);
1756 if (file
->f_flags
& FMODE_READ
)
1757 poll_wait(file
, &card
->midi
.iwait
, wait
);
1758 spin_lock_irqsave(&card
->lock
, flags
);
1759 if (file
->f_flags
& FMODE_READ
) {
1760 if (card
->midi
.icnt
> 0)
1761 mask
|= POLLIN
| POLLRDNORM
;
1763 if (file
->f_flags
& FMODE_WRITE
) {
1764 if (card
->midi
.ocnt
< CS_MIDIOUTBUF
)
1765 mask
|= POLLOUT
| POLLWRNORM
;
1767 spin_unlock_irqrestore(&card
->lock
, flags
);
1772 static int cs_midi_open(struct inode
*inode
, struct file
*file
)
1774 unsigned int minor
= iminor(inode
);
1775 struct cs_card
*card
= NULL
;
1776 unsigned long flags
;
1777 struct list_head
*entry
;
1779 list_for_each(entry
, &cs46xx_devs
) {
1780 card
= list_entry(entry
, struct cs_card
, list
);
1781 if (card
->dev_midi
== minor
)
1785 if (entry
== &cs46xx_devs
)
1788 CS_DBGOUT(CS_FUNCTION
| CS_OPEN
, 2, printk(KERN_INFO
1789 "cs46xx: cs46xx_midi_open(): Error - unable to find card struct\n"));
1793 file
->private_data
= card
;
1794 /* wait for device to become free */
1795 mutex_lock(&card
->midi
.open_mutex
);
1796 while (card
->midi
.open_mode
& file
->f_mode
) {
1797 if (file
->f_flags
& O_NONBLOCK
) {
1798 mutex_unlock(&card
->midi
.open_mutex
);
1801 mutex_unlock(&card
->midi
.open_mutex
);
1802 interruptible_sleep_on(&card
->midi
.open_wait
);
1803 if (signal_pending(current
))
1804 return -ERESTARTSYS
;
1805 mutex_lock(&card
->midi
.open_mutex
);
1807 spin_lock_irqsave(&card
->midi
.lock
, flags
);
1808 if (!(card
->midi
.open_mode
& (FMODE_READ
| FMODE_WRITE
))) {
1809 card
->midi
.ird
= card
->midi
.iwr
= card
->midi
.icnt
= 0;
1810 card
->midi
.ord
= card
->midi
.owr
= card
->midi
.ocnt
= 0;
1811 card
->midi
.ird
= card
->midi
.iwr
= card
->midi
.icnt
= 0;
1812 cs461x_pokeBA0(card
, BA0_MIDCR
, 0x0000000f); /* Enable xmit, rcv. */
1813 cs461x_pokeBA0(card
, BA0_HICR
, HICR_IEV
| HICR_CHGM
); /* Enable interrupts */
1815 if (file
->f_mode
& FMODE_READ
)
1816 card
->midi
.ird
= card
->midi
.iwr
= card
->midi
.icnt
= 0;
1817 if (file
->f_mode
& FMODE_WRITE
)
1818 card
->midi
.ord
= card
->midi
.owr
= card
->midi
.ocnt
= 0;
1819 spin_unlock_irqrestore(&card
->midi
.lock
, flags
);
1820 card
->midi
.open_mode
|= (file
->f_mode
& (FMODE_READ
| FMODE_WRITE
));
1821 mutex_unlock(&card
->midi
.open_mutex
);
1826 static int cs_midi_release(struct inode
*inode
, struct file
*file
)
1828 struct cs_card
*card
= file
->private_data
;
1829 DECLARE_WAITQUEUE(wait
, current
);
1830 unsigned long flags
;
1831 unsigned count
, tmo
;
1833 if (file
->f_mode
& FMODE_WRITE
) {
1834 current
->state
= TASK_INTERRUPTIBLE
;
1835 add_wait_queue(&card
->midi
.owait
, &wait
);
1837 spin_lock_irqsave(&card
->midi
.lock
, flags
);
1838 count
= card
->midi
.ocnt
;
1839 spin_unlock_irqrestore(&card
->midi
.lock
, flags
);
1842 if (signal_pending(current
))
1844 if (file
->f_flags
& O_NONBLOCK
)
1846 tmo
= (count
* HZ
) / 3100;
1847 if (!schedule_timeout(tmo
? : 1) && tmo
)
1848 printk(KERN_DEBUG
"cs46xx: midi timed out??\n");
1850 remove_wait_queue(&card
->midi
.owait
, &wait
);
1851 current
->state
= TASK_RUNNING
;
1853 mutex_lock(&card
->midi
.open_mutex
);
1854 card
->midi
.open_mode
&= (~(file
->f_mode
& (FMODE_READ
| FMODE_WRITE
)));
1855 mutex_unlock(&card
->midi
.open_mutex
);
1856 wake_up(&card
->midi
.open_wait
);
1861 * Midi file operations struct.
1863 static /*const*/ struct file_operations cs_midi_fops
= {
1864 CS_OWNER CS_THIS_MODULE
1865 .llseek
= no_llseek
,
1866 .read
= cs_midi_read
,
1867 .write
= cs_midi_write
,
1868 .poll
= cs_midi_poll
,
1869 .open
= cs_midi_open
,
1870 .release
= cs_midi_release
,
1875 * CopySamples copies 16-bit stereo signed samples from the source to the
1876 * destination, possibly converting down to unsigned 8-bit and/or mono.
1877 * count specifies the number of output bytes to write.
1881 * dst - Pointer to a destination buffer.
1882 * src - Pointer to a source buffer
1883 * count - The number of bytes to copy into the destination buffer.
1884 * fmt - CS_FMT_16BIT and/or CS_FMT_STEREO bits
1885 * dmabuf - pointer to the dma buffer structure
1887 * NOTES: only call this routine if the output desired is not 16 Signed Stereo
1891 static void CopySamples(char *dst
, char *src
, int count
, unsigned fmt
,
1892 struct dmabuf
*dmabuf
)
1895 s16
*psSrc
= (s16
*)src
;
1896 s16
*psDst
= (s16
*)dst
;
1897 u8
*pucDst
= (u8
*)dst
;
1899 CS_DBGOUT(CS_FUNCTION
, 2, printk(KERN_INFO
"cs46xx: CopySamples()+ ") );
1900 CS_DBGOUT(CS_WAVE_READ
, 8, printk(KERN_INFO
1901 " dst=%p src=%p count=%d fmt=0x%x\n",
1902 dst
,src
,count
,fmt
) );
1905 * See if the data should be output as 8-bit unsigned stereo.
1907 if ((fmt
& CS_FMT_STEREO
) && !(fmt
& CS_FMT_16BIT
)) {
1909 * Convert each 16-bit signed stereo sample to 8-bit unsigned
1910 * stereo using rounding.
1915 *(pucDst
++) = (u8
)(((s16
)(*psSrc
++) + (s16
)0x8000) >> 8);
1918 * See if the data should be output at 8-bit unsigned mono.
1920 else if (!(fmt
& CS_FMT_STEREO
) && !(fmt
& CS_FMT_16BIT
)) {
1922 * Convert each 16-bit signed stereo sample to 8-bit unsigned
1923 * mono using averaging and rounding.
1928 s32AudioSample
= ((*psSrc
) + (*(psSrc
+ 1))) / 2 + (s32
)0x80;
1929 if (s32AudioSample
> 0x7fff)
1930 s32AudioSample
= 0x7fff;
1931 *(pucDst
++) = (u8
)(((s16
)s32AudioSample
+ (s16
)0x8000) >> 8);
1936 * See if the data should be output at 16-bit signed mono.
1938 else if (!(fmt
& CS_FMT_STEREO
) && (fmt
& CS_FMT_16BIT
)) {
1940 * Convert each 16-bit signed stereo sample to 16-bit signed
1941 * mono using averaging.
1946 *(psDst
++) = (s16
)((*psSrc
) + (*(psSrc
+ 1))) / 2;
1954 * replacement for the standard copy_to_user, to allow for a conversion from
1955 * 16 bit to 8 bit and from stereo to mono, if the record conversion is active.
1956 * The current CS46xx/CS4280 static image only records in 16bit unsigned Stereo,
1957 * so we convert from any of the other format combinations.
1959 static unsigned cs_copy_to_user(
1966 struct dmabuf
*dmabuf
= &s
->dmabuf
;
1967 void *src
= hwsrc
; /* default to the standard destination buffer addr */
1969 CS_DBGOUT(CS_FUNCTION
, 6, printk(KERN_INFO
1970 "cs_copy_to_user()+ fmt=0x%x cnt=%d dest=%p\n",
1971 dmabuf
->fmt
,(unsigned)cnt
,dest
) );
1973 if (cnt
> dmabuf
->dmasize
)
1974 cnt
= dmabuf
->dmasize
;
1979 if (dmabuf
->divisor
!= 1) {
1980 if (!dmabuf
->tmpbuff
) {
1981 *copied
= cnt
/ dmabuf
->divisor
;
1985 CopySamples((char *)dmabuf
->tmpbuff
, (char *)hwsrc
, cnt
,
1986 dmabuf
->fmt
, dmabuf
);
1987 src
= dmabuf
->tmpbuff
;
1988 cnt
= cnt
/dmabuf
->divisor
;
1990 if (copy_to_user(dest
, src
, cnt
)) {
1991 CS_DBGOUT(CS_FUNCTION
, 2, printk(KERN_ERR
1992 "cs46xx: cs_copy_to_user()- fault dest=%p src=%p cnt=%d\n",
1998 CS_DBGOUT(CS_FUNCTION
, 2, printk(KERN_INFO
1999 "cs46xx: cs_copy_to_user()- copied bytes is %d \n",cnt
));
2003 /* in this loop, dmabuf.count signifies the amount of data that is waiting to be copied to
2004 the user's buffer. it is filled by the dma machine and drained by this loop. */
2005 static ssize_t
cs_read(struct file
*file
, char __user
*buffer
, size_t count
, loff_t
*ppos
)
2007 struct cs_card
*card
= file
->private_data
;
2008 struct cs_state
*state
;
2009 DECLARE_WAITQUEUE(wait
, current
);
2010 struct dmabuf
*dmabuf
;
2012 unsigned long flags
;
2015 unsigned copied
= 0;
2017 CS_DBGOUT(CS_WAVE_READ
| CS_FUNCTION
, 4,
2018 printk("cs46xx: cs_read()+ %zd\n",count
) );
2019 state
= card
->states
[0];
2022 dmabuf
= &state
->dmabuf
;
2026 if (!access_ok(VERIFY_WRITE
, buffer
, count
))
2029 mutex_lock(&state
->sem
);
2030 if (!dmabuf
->ready
&& (ret
= __prog_dmabuf(state
)))
2033 add_wait_queue(&state
->dmabuf
.wait
, &wait
);
2035 while (!(card
->pm
.flags
& CS46XX_PM_IDLE
)) {
2037 if (signal_pending(current
)) {
2043 spin_lock_irqsave(&state
->card
->lock
, flags
);
2044 swptr
= dmabuf
->swptr
;
2045 cnt
= dmabuf
->dmasize
- swptr
;
2046 if (dmabuf
->count
< cnt
)
2047 cnt
= dmabuf
->count
;
2049 __set_current_state(TASK_INTERRUPTIBLE
);
2050 spin_unlock_irqrestore(&state
->card
->lock
, flags
);
2052 if (cnt
> (count
* dmabuf
->divisor
))
2053 cnt
= count
* dmabuf
->divisor
;
2055 /* buffer is empty, start the dma machine and wait for data to be
2058 if (file
->f_flags
& O_NONBLOCK
) {
2063 mutex_unlock(&state
->sem
);
2065 if (signal_pending(current
)) {
2070 mutex_lock(&state
->sem
);
2071 if (dmabuf
->mapped
) {
2079 CS_DBGOUT(CS_WAVE_READ
, 2, printk(KERN_INFO
2080 "_read() copy_to cnt=%d count=%zd ", cnt
,count
) );
2081 CS_DBGOUT(CS_WAVE_READ
, 8, printk(KERN_INFO
2082 " .dmasize=%d .count=%d buffer=%p ret=%zd\n",
2083 dmabuf
->dmasize
,dmabuf
->count
,buffer
,ret
));
2085 if (cs_copy_to_user(state
, buffer
,
2086 (char *)dmabuf
->rawbuf
+ swptr
, cnt
, &copied
)) {
2091 swptr
= (swptr
+ cnt
) % dmabuf
->dmasize
;
2092 spin_lock_irqsave(&card
->lock
, flags
);
2093 dmabuf
->swptr
= swptr
;
2094 dmabuf
->count
-= cnt
;
2095 spin_unlock_irqrestore(&card
->lock
, flags
);
2102 remove_wait_queue(&state
->dmabuf
.wait
, &wait
);
2104 mutex_unlock(&state
->sem
);
2105 set_current_state(TASK_RUNNING
);
2106 CS_DBGOUT(CS_WAVE_READ
| CS_FUNCTION
, 4,
2107 printk("cs46xx: cs_read()- %zd\n",ret
) );
2111 /* in this loop, dmabuf.count signifies the amount of data that is waiting to be dma to
2112 the soundcard. it is drained by the dma machine and filled by this loop. */
2113 static ssize_t
cs_write(struct file
*file
, const char __user
*buffer
, size_t count
, loff_t
*ppos
)
2115 struct cs_card
*card
= file
->private_data
;
2116 struct cs_state
*state
;
2117 DECLARE_WAITQUEUE(wait
, current
);
2118 struct dmabuf
*dmabuf
;
2120 unsigned long flags
;
2124 CS_DBGOUT(CS_WAVE_WRITE
| CS_FUNCTION
, 4,
2125 printk("cs46xx: cs_write called, count = %zd\n", count
) );
2126 state
= card
->states
[1];
2129 if (!access_ok(VERIFY_READ
, buffer
, count
))
2131 dmabuf
= &state
->dmabuf
;
2133 mutex_lock(&state
->sem
);
2134 if (dmabuf
->mapped
) {
2139 if (!dmabuf
->ready
&& (ret
= __prog_dmabuf(state
)))
2141 add_wait_queue(&state
->dmabuf
.wait
, &wait
);
2144 * Start the loop to read from the user's buffer and write to the dma buffer.
2145 * check for PM events and underrun/overrun in the loop.
2148 while (!(card
->pm
.flags
& CS46XX_PM_IDLE
)) {
2150 if (signal_pending(current
)) {
2156 spin_lock_irqsave(&state
->card
->lock
, flags
);
2157 if (dmabuf
->count
< 0) {
2158 /* buffer underrun, we are recovering from sleep_on_timeout,
2159 resync hwptr and swptr */
2161 dmabuf
->swptr
= dmabuf
->hwptr
;
2163 if (dmabuf
->underrun
) {
2164 dmabuf
->underrun
= 0;
2165 dmabuf
->hwptr
= cs_get_dma_addr(state
);
2166 dmabuf
->swptr
= dmabuf
->hwptr
;
2169 swptr
= dmabuf
->swptr
;
2170 cnt
= dmabuf
->dmasize
- swptr
;
2171 if (dmabuf
->count
+ cnt
> dmabuf
->dmasize
)
2172 cnt
= dmabuf
->dmasize
- dmabuf
->count
;
2174 __set_current_state(TASK_INTERRUPTIBLE
);
2175 spin_unlock_irqrestore(&state
->card
->lock
, flags
);
2180 /* buffer is full, start the dma machine and wait for data to be
2183 if (file
->f_flags
& O_NONBLOCK
) {
2188 mutex_unlock(&state
->sem
);
2190 if (signal_pending(current
)) {
2195 mutex_lock(&state
->sem
);
2196 if (dmabuf
->mapped
) {
2203 if (copy_from_user(dmabuf
->rawbuf
+ swptr
, buffer
, cnt
)) {
2208 spin_lock_irqsave(&state
->card
->lock
, flags
);
2209 swptr
= (swptr
+ cnt
) % dmabuf
->dmasize
;
2210 dmabuf
->swptr
= swptr
;
2211 dmabuf
->count
+= cnt
;
2212 if (dmabuf
->count
> dmabuf
->dmasize
) {
2213 CS_DBGOUT(CS_WAVE_WRITE
| CS_ERROR
, 2, printk(
2214 "cs46xx: cs_write() d->count > dmasize - resetting\n"));
2215 dmabuf
->count
= dmabuf
->dmasize
;
2217 dmabuf
->endcleared
= 0;
2218 spin_unlock_irqrestore(&state
->card
->lock
, flags
);
2226 mutex_unlock(&state
->sem
);
2227 remove_wait_queue(&state
->dmabuf
.wait
, &wait
);
2228 set_current_state(TASK_RUNNING
);
2230 CS_DBGOUT(CS_WAVE_WRITE
| CS_FUNCTION
, 2,
2231 printk("cs46xx: cs_write()- ret=%zd\n", ret
));
2235 static unsigned int cs_poll(struct file
*file
, struct poll_table_struct
*wait
)
2237 struct cs_card
*card
= file
->private_data
;
2238 struct dmabuf
*dmabuf
;
2239 struct cs_state
*state
;
2240 unsigned long flags
;
2241 unsigned int mask
= 0;
2243 CS_DBGOUT(CS_FUNCTION
, 2, printk("cs46xx: cs_poll()+ \n"));
2244 if (!(file
->f_mode
& (FMODE_WRITE
| FMODE_READ
))) {
2247 if (file
->f_mode
& FMODE_WRITE
) {
2248 state
= card
->states
[1];
2250 dmabuf
= &state
->dmabuf
;
2251 poll_wait(file
, &dmabuf
->wait
, wait
);
2254 if (file
->f_mode
& FMODE_READ
) {
2255 state
= card
->states
[0];
2257 dmabuf
= &state
->dmabuf
;
2258 poll_wait(file
, &dmabuf
->wait
, wait
);
2262 spin_lock_irqsave(&card
->lock
, flags
);
2263 cs_update_ptr(card
, CS_FALSE
);
2264 if (file
->f_mode
& FMODE_READ
) {
2265 state
= card
->states
[0];
2267 dmabuf
= &state
->dmabuf
;
2268 if (dmabuf
->count
>= (signed)dmabuf
->fragsize
)
2269 mask
|= POLLIN
| POLLRDNORM
;
2272 if (file
->f_mode
& FMODE_WRITE
) {
2273 state
= card
->states
[1];
2275 dmabuf
= &state
->dmabuf
;
2276 if (dmabuf
->mapped
) {
2277 if (dmabuf
->count
>= (signed)dmabuf
->fragsize
)
2278 mask
|= POLLOUT
| POLLWRNORM
;
2280 if ((signed)dmabuf
->dmasize
>= dmabuf
->count
2281 + (signed)dmabuf
->fragsize
)
2282 mask
|= POLLOUT
| POLLWRNORM
;
2286 spin_unlock_irqrestore(&card
->lock
, flags
);
2288 CS_DBGOUT(CS_FUNCTION
, 2, printk("cs46xx: cs_poll()- (0x%x) \n",
2294 * We let users mmap the ring buffer. Its not the real DMA buffer but
2295 * that side of the code is hidden in the IRQ handling. We do a software
2296 * emulation of DMA from a 64K or so buffer into a 2K FIFO.
2297 * (the hardware probably deserves a moan here but Crystal send me nice
2301 static int cs_mmap(struct file
*file
, struct vm_area_struct
*vma
)
2303 struct cs_card
*card
= file
->private_data
;
2304 struct cs_state
*state
;
2305 struct dmabuf
*dmabuf
;
2309 CS_DBGOUT(CS_FUNCTION
| CS_PARMS
, 2, printk("cs46xx: cs_mmap()+ file=%p %s %s\n",
2310 file
, vma
->vm_flags
& VM_WRITE
? "VM_WRITE" : "",
2311 vma
->vm_flags
& VM_READ
? "VM_READ" : "") );
2313 if (vma
->vm_flags
& VM_WRITE
) {
2314 state
= card
->states
[1];
2316 CS_DBGOUT(CS_OPEN
, 2, printk(
2317 "cs46xx: cs_mmap() VM_WRITE - state TRUE prog_dmabuf DAC\n") );
2318 if ((ret
= prog_dmabuf(state
)) != 0)
2321 } else if (vma
->vm_flags
& VM_READ
) {
2322 state
= card
->states
[0];
2324 CS_DBGOUT(CS_OPEN
, 2, printk(
2325 "cs46xx: cs_mmap() VM_READ - state TRUE prog_dmabuf ADC\n") );
2326 if ((ret
= prog_dmabuf(state
)) != 0)
2330 CS_DBGOUT(CS_ERROR
, 2, printk(
2331 "cs46xx: cs_mmap() return -EINVAL\n") );
2336 * For now ONLY support playback, but seems like the only way to use
2337 * mmap() is to open an FD with RDWR, just read or just write access
2338 * does not function, get an error back from the kernel.
2339 * Also, QuakeIII opens with RDWR! So, there must be something
2340 * to needing read/write access mapping. So, allow read/write but
2343 state
= card
->states
[1];
2349 mutex_lock(&state
->sem
);
2350 dmabuf
= &state
->dmabuf
;
2351 if (cs4x_pgoff(vma
) != 0) {
2355 size
= vma
->vm_end
- vma
->vm_start
;
2357 CS_DBGOUT(CS_PARMS
, 2, printk("cs46xx: cs_mmap(): size=%d\n",(unsigned)size
) );
2359 if (size
> (PAGE_SIZE
<< dmabuf
->buforder
)) {
2363 if (remap_pfn_range(vma
, vma
->vm_start
,
2364 virt_to_phys(dmabuf
->rawbuf
) >> PAGE_SHIFT
,
2365 size
, vma
->vm_page_prot
)) {
2371 CS_DBGOUT(CS_FUNCTION
, 2, printk("cs46xx: cs_mmap()-\n") );
2373 mutex_unlock(&state
->sem
);
2377 static int cs_ioctl(struct inode
*inode
, struct file
*file
, unsigned int cmd
, unsigned long arg
)
2379 struct cs_card
*card
= file
->private_data
;
2380 struct cs_state
*state
;
2381 struct dmabuf
*dmabuf
= NULL
;
2382 unsigned long flags
;
2383 audio_buf_info abinfo
;
2385 int val
, valsave
, ret
;
2387 void __user
*argp
= (void __user
*)arg
;
2388 int __user
*p
= argp
;
2390 state
= card
->states
[0];
2392 dmabuf
= &state
->dmabuf
;
2393 mapped
= (file
->f_mode
& FMODE_READ
) && dmabuf
->mapped
;
2395 state
= card
->states
[1];
2397 dmabuf
= &state
->dmabuf
;
2398 mapped
|= (file
->f_mode
& FMODE_WRITE
) && dmabuf
->mapped
;
2406 case OSS_GETVERSION
:
2407 return put_user(SOUND_VERSION
, p
);
2408 case SNDCTL_DSP_RESET
:
2409 /* FIXME: spin_lock ? */
2410 if (file
->f_mode
& FMODE_WRITE
) {
2411 state
= card
->states
[1];
2413 dmabuf
= &state
->dmabuf
;
2415 synchronize_irq(card
->irq
);
2417 resync_dma_ptrs(state
);
2418 dmabuf
->swptr
= dmabuf
->hwptr
= 0;
2419 dmabuf
->count
= dmabuf
->total_bytes
= 0;
2424 if (file
->f_mode
& FMODE_READ
) {
2425 state
= card
->states
[0];
2427 dmabuf
= &state
->dmabuf
;
2429 synchronize_irq(card
->irq
);
2430 resync_dma_ptrs(state
);
2432 dmabuf
->swptr
= dmabuf
->hwptr
= 0;
2433 dmabuf
->count
= dmabuf
->total_bytes
= 0;
2438 CS_DBGOUT(CS_IOCTL
, 2, printk("cs46xx: DSP_RESET()-\n") );
2440 case SNDCTL_DSP_SYNC
:
2441 if (file
->f_mode
& FMODE_WRITE
)
2442 return drain_dac(state
, file
->f_flags
& O_NONBLOCK
);
2444 case SNDCTL_DSP_SPEED
: /* set sample rate */
2445 if (get_user(val
, p
))
2448 if (file
->f_mode
& FMODE_READ
) {
2449 state
= card
->states
[0];
2451 dmabuf
= &state
->dmabuf
;
2455 cs_set_adc_rate(state
, val
);
2456 cs_set_divisor(dmabuf
);
2459 if (file
->f_mode
& FMODE_WRITE
) {
2460 state
= card
->states
[1];
2462 dmabuf
= &state
->dmabuf
;
2466 cs_set_dac_rate(state
, val
);
2467 cs_set_divisor(dmabuf
);
2470 CS_DBGOUT(CS_IOCTL
| CS_PARMS
, 4, printk(
2471 "cs46xx: cs_ioctl() DSP_SPEED %s %s %d\n",
2472 file
->f_mode
& FMODE_WRITE
? "DAC" : "",
2473 file
->f_mode
& FMODE_READ
? "ADC" : "",
2475 return put_user(dmabuf
->rate
, p
);
2477 return put_user(0, p
);
2478 case SNDCTL_DSP_STEREO
: /* set stereo or mono channel */
2479 if (get_user(val
, p
))
2481 if (file
->f_mode
& FMODE_WRITE
) {
2482 state
= card
->states
[1];
2484 dmabuf
= &state
->dmabuf
;
2489 dmabuf
->fmt
|= CS_FMT_STEREO
;
2491 dmabuf
->fmt
&= ~CS_FMT_STEREO
;
2492 cs_set_divisor(dmabuf
);
2493 CS_DBGOUT(CS_IOCTL
| CS_PARMS
, 4, printk(
2494 "cs46xx: DSP_STEREO() DAC %s\n",
2495 (dmabuf
->fmt
& CS_FMT_STEREO
) ?
2499 if (file
->f_mode
& FMODE_READ
) {
2500 state
= card
->states
[0];
2502 dmabuf
= &state
->dmabuf
;
2507 dmabuf
->fmt
|= CS_FMT_STEREO
;
2509 dmabuf
->fmt
&= ~CS_FMT_STEREO
;
2510 cs_set_divisor(dmabuf
);
2511 CS_DBGOUT(CS_IOCTL
| CS_PARMS
, 4, printk(
2512 "cs46xx: DSP_STEREO() ADC %s\n",
2513 (dmabuf
->fmt
& CS_FMT_STEREO
) ?
2518 case SNDCTL_DSP_GETBLKSIZE
:
2519 if (file
->f_mode
& FMODE_WRITE
) {
2520 state
= card
->states
[1];
2522 dmabuf
= &state
->dmabuf
;
2523 if ((val
= prog_dmabuf(state
)))
2525 return put_user(dmabuf
->fragsize
, p
);
2528 if (file
->f_mode
& FMODE_READ
) {
2529 state
= card
->states
[0];
2531 dmabuf
= &state
->dmabuf
;
2532 if ((val
= prog_dmabuf(state
)))
2534 return put_user(dmabuf
->fragsize
/dmabuf
->divisor
,
2538 return put_user(0, p
);
2539 case SNDCTL_DSP_GETFMTS
: /* Returns a mask of supported sample format*/
2540 return put_user(AFMT_S16_LE
| AFMT_U8
, p
);
2541 case SNDCTL_DSP_SETFMT
: /* Select sample format */
2542 if (get_user(val
, p
))
2544 CS_DBGOUT(CS_IOCTL
| CS_PARMS
, 4, printk(
2545 "cs46xx: cs_ioctl() DSP_SETFMT %s %s %s %s\n",
2546 file
->f_mode
& FMODE_WRITE
? "DAC" : "",
2547 file
->f_mode
& FMODE_READ
? "ADC" : "",
2548 val
== AFMT_S16_LE
? "16Bit Signed" : "",
2549 val
== AFMT_U8
? "8Bit Unsigned" : "") );
2551 if (val
!= AFMT_QUERY
) {
2552 if (val
==AFMT_S16_LE
|| val
==AFMT_U8
) {
2553 if (file
->f_mode
& FMODE_WRITE
) {
2554 state
= card
->states
[1];
2556 dmabuf
= &state
->dmabuf
;
2560 if (val
== AFMT_S16_LE
)
2561 dmabuf
->fmt
|= CS_FMT_16BIT
;
2563 dmabuf
->fmt
&= ~CS_FMT_16BIT
;
2564 cs_set_divisor(dmabuf
);
2565 if ((ret
= prog_dmabuf(state
)))
2569 if (file
->f_mode
& FMODE_READ
) {
2571 state
= card
->states
[0];
2573 dmabuf
= &state
->dmabuf
;
2577 if (val
== AFMT_S16_LE
)
2578 dmabuf
->fmt
|= CS_FMT_16BIT
;
2580 dmabuf
->fmt
&= ~CS_FMT_16BIT
;
2581 cs_set_divisor(dmabuf
);
2582 if ((ret
= prog_dmabuf(state
)))
2587 CS_DBGOUT(CS_IOCTL
| CS_ERROR
, 2, printk(
2588 "cs46xx: DSP_SETFMT() Unsupported format (0x%x)\n",
2592 if (file
->f_mode
& FMODE_WRITE
) {
2593 state
= card
->states
[1];
2595 dmabuf
= &state
->dmabuf
;
2596 } else if (file
->f_mode
& FMODE_READ
) {
2597 state
= card
->states
[0];
2599 dmabuf
= &state
->dmabuf
;
2603 if (dmabuf
->fmt
& CS_FMT_16BIT
)
2604 return put_user(AFMT_S16_LE
, p
);
2606 return put_user(AFMT_U8
, p
);
2608 return put_user(0, p
);
2609 case SNDCTL_DSP_CHANNELS
:
2610 if (get_user(val
, p
))
2613 if (file
->f_mode
& FMODE_WRITE
) {
2614 state
= card
->states
[1];
2616 dmabuf
= &state
->dmabuf
;
2621 dmabuf
->fmt
|= CS_FMT_STEREO
;
2623 dmabuf
->fmt
&= ~CS_FMT_STEREO
;
2624 cs_set_divisor(dmabuf
);
2625 if (prog_dmabuf(state
))
2629 if (file
->f_mode
& FMODE_READ
) {
2630 state
= card
->states
[0];
2632 dmabuf
= &state
->dmabuf
;
2637 dmabuf
->fmt
|= CS_FMT_STEREO
;
2639 dmabuf
->fmt
&= ~CS_FMT_STEREO
;
2640 cs_set_divisor(dmabuf
);
2641 if (prog_dmabuf(state
))
2646 return put_user((dmabuf
->fmt
& CS_FMT_STEREO
) ? 2 : 1,
2648 case SNDCTL_DSP_POST
:
2650 * There will be a longer than normal pause in the data.
2651 * so... do nothing, because there is nothing that we can do.
2654 case SNDCTL_DSP_SUBDIVIDE
:
2655 if (file
->f_mode
& FMODE_WRITE
) {
2656 state
= card
->states
[1];
2658 dmabuf
= &state
->dmabuf
;
2659 if (dmabuf
->subdivision
)
2661 if (get_user(val
, p
))
2663 if (val
!= 1 && val
!= 2)
2665 dmabuf
->subdivision
= val
;
2668 if (file
->f_mode
& FMODE_READ
) {
2669 state
= card
->states
[0];
2671 dmabuf
= &state
->dmabuf
;
2672 if (dmabuf
->subdivision
)
2674 if (get_user(val
, p
))
2676 if (val
!= 1 && val
!= 2)
2678 dmabuf
->subdivision
= val
;
2682 case SNDCTL_DSP_SETFRAGMENT
:
2683 if (get_user(val
, p
))
2685 if (file
->f_mode
& FMODE_WRITE
) {
2686 state
= card
->states
[1];
2688 dmabuf
= &state
->dmabuf
;
2689 dmabuf
->ossfragshift
= val
& 0xffff;
2690 dmabuf
->ossmaxfrags
= (val
>> 16) & 0xffff;
2693 if (file
->f_mode
& FMODE_READ
) {
2694 state
= card
->states
[0];
2696 dmabuf
= &state
->dmabuf
;
2697 dmabuf
->ossfragshift
= val
& 0xffff;
2698 dmabuf
->ossmaxfrags
= (val
>> 16) & 0xffff;
2702 case SNDCTL_DSP_GETOSPACE
:
2703 if (!(file
->f_mode
& FMODE_WRITE
))
2705 state
= card
->states
[1];
2707 dmabuf
= &state
->dmabuf
;
2708 spin_lock_irqsave(&state
->card
->lock
, flags
);
2709 cs_update_ptr(card
, CS_TRUE
);
2710 abinfo
.fragsize
= dmabuf
->fragsize
;
2711 abinfo
.fragstotal
= dmabuf
->numfrag
;
2713 * for mmap we always have total space available
2716 abinfo
.bytes
= dmabuf
->dmasize
;
2718 abinfo
.bytes
= dmabuf
->dmasize
- dmabuf
->count
;
2720 abinfo
.fragments
= abinfo
.bytes
>> dmabuf
->fragshift
;
2721 spin_unlock_irqrestore(&state
->card
->lock
, flags
);
2722 return copy_to_user(argp
, &abinfo
, sizeof(abinfo
)) ? -EFAULT
: 0;
2725 case SNDCTL_DSP_GETISPACE
:
2726 if (!(file
->f_mode
& FMODE_READ
))
2728 state
= card
->states
[0];
2730 dmabuf
= &state
->dmabuf
;
2731 spin_lock_irqsave(&state
->card
->lock
, flags
);
2732 cs_update_ptr(card
, CS_TRUE
);
2733 abinfo
.fragsize
= dmabuf
->fragsize
/dmabuf
->divisor
;
2734 abinfo
.bytes
= dmabuf
->count
/dmabuf
->divisor
;
2735 abinfo
.fragstotal
= dmabuf
->numfrag
;
2736 abinfo
.fragments
= abinfo
.bytes
>> dmabuf
->fragshift
;
2737 spin_unlock_irqrestore(&state
->card
->lock
, flags
);
2738 return copy_to_user(argp
, &abinfo
, sizeof(abinfo
)) ? -EFAULT
: 0;
2741 case SNDCTL_DSP_NONBLOCK
:
2742 file
->f_flags
|= O_NONBLOCK
;
2744 case SNDCTL_DSP_GETCAPS
:
2745 return put_user(DSP_CAP_REALTIME
|DSP_CAP_TRIGGER
|DSP_CAP_MMAP
,
2747 case SNDCTL_DSP_GETTRIGGER
:
2749 CS_DBGOUT(CS_IOCTL
, 2, printk("cs46xx: DSP_GETTRIGGER()+\n") );
2750 if (file
->f_mode
& FMODE_WRITE
) {
2751 state
= card
->states
[1];
2753 dmabuf
= &state
->dmabuf
;
2754 if (dmabuf
->enable
& DAC_RUNNING
)
2755 val
|= PCM_ENABLE_INPUT
;
2758 if (file
->f_mode
& FMODE_READ
) {
2760 state
= card
->states
[0];
2761 dmabuf
= &state
->dmabuf
;
2762 if (dmabuf
->enable
& ADC_RUNNING
)
2763 val
|= PCM_ENABLE_OUTPUT
;
2766 CS_DBGOUT(CS_IOCTL
, 2, printk("cs46xx: DSP_GETTRIGGER()- val=0x%x\n",val
) );
2767 return put_user(val
, p
);
2768 case SNDCTL_DSP_SETTRIGGER
:
2769 if (get_user(val
, p
))
2771 if (file
->f_mode
& FMODE_READ
) {
2772 state
= card
->states
[0];
2774 dmabuf
= &state
->dmabuf
;
2775 if (val
& PCM_ENABLE_INPUT
) {
2776 if (!dmabuf
->ready
&& (ret
= prog_dmabuf(state
)))
2783 if (file
->f_mode
& FMODE_WRITE
) {
2784 state
= card
->states
[1];
2786 dmabuf
= &state
->dmabuf
;
2787 if (val
& PCM_ENABLE_OUTPUT
) {
2788 if (!dmabuf
->ready
&& (ret
= prog_dmabuf(state
)))
2796 case SNDCTL_DSP_GETIPTR
:
2797 if (!(file
->f_mode
& FMODE_READ
))
2799 state
= card
->states
[0];
2801 dmabuf
= &state
->dmabuf
;
2802 spin_lock_irqsave(&state
->card
->lock
, flags
);
2803 cs_update_ptr(card
, CS_TRUE
);
2804 cinfo
.bytes
= dmabuf
->total_bytes
/dmabuf
->divisor
;
2805 cinfo
.blocks
= dmabuf
->count
/dmabuf
->divisor
>> dmabuf
->fragshift
;
2806 cinfo
.ptr
= dmabuf
->hwptr
/dmabuf
->divisor
;
2807 spin_unlock_irqrestore(&state
->card
->lock
, flags
);
2808 if (copy_to_user(argp
, &cinfo
, sizeof(cinfo
)))
2813 case SNDCTL_DSP_GETOPTR
:
2814 if (!(file
->f_mode
& FMODE_WRITE
))
2816 state
= card
->states
[1];
2818 dmabuf
= &state
->dmabuf
;
2819 spin_lock_irqsave(&state
->card
->lock
, flags
);
2820 cs_update_ptr(card
, CS_TRUE
);
2821 cinfo
.bytes
= dmabuf
->total_bytes
;
2822 if (dmabuf
->mapped
) {
2823 cinfo
.blocks
= (cinfo
.bytes
>> dmabuf
->fragshift
)
2825 CS_DBGOUT(CS_PARMS
, 8,
2826 printk("total_bytes=%d blocks=%d dmabuf->blocks=%d\n",
2827 cinfo
.bytes
,cinfo
.blocks
,dmabuf
->blocks
) );
2828 dmabuf
->blocks
= cinfo
.bytes
>> dmabuf
->fragshift
;
2830 cinfo
.blocks
= dmabuf
->count
>> dmabuf
->fragshift
;
2832 cinfo
.ptr
= dmabuf
->hwptr
;
2834 CS_DBGOUT(CS_PARMS
, 4, printk(
2835 "cs46xx: GETOPTR bytes=%d blocks=%d ptr=%d\n",
2836 cinfo
.bytes
,cinfo
.blocks
,cinfo
.ptr
) );
2837 spin_unlock_irqrestore(&state
->card
->lock
, flags
);
2838 if (copy_to_user(argp
, &cinfo
, sizeof(cinfo
)))
2843 case SNDCTL_DSP_SETDUPLEX
:
2845 case SNDCTL_DSP_GETODELAY
:
2846 if (!(file
->f_mode
& FMODE_WRITE
))
2848 state
= card
->states
[1];
2850 dmabuf
= &state
->dmabuf
;
2851 spin_lock_irqsave(&state
->card
->lock
, flags
);
2852 cs_update_ptr(card
, CS_TRUE
);
2853 val
= dmabuf
->count
;
2854 spin_unlock_irqrestore(&state
->card
->lock
, flags
);
2857 return put_user(val
, p
);
2858 case SOUND_PCM_READ_RATE
:
2859 if (file
->f_mode
& FMODE_READ
)
2860 state
= card
->states
[0];
2862 state
= card
->states
[1];
2864 dmabuf
= &state
->dmabuf
;
2865 return put_user(dmabuf
->rate
, p
);
2867 return put_user(0, p
);
2868 case SOUND_PCM_READ_CHANNELS
:
2869 if (file
->f_mode
& FMODE_READ
)
2870 state
= card
->states
[0];
2872 state
= card
->states
[1];
2874 dmabuf
= &state
->dmabuf
;
2875 return put_user((dmabuf
->fmt
& CS_FMT_STEREO
) ? 2 : 1,
2878 return put_user(0, p
);
2879 case SOUND_PCM_READ_BITS
:
2880 if (file
->f_mode
& FMODE_READ
)
2881 state
= card
->states
[0];
2883 state
= card
->states
[1];
2885 dmabuf
= &state
->dmabuf
;
2886 return put_user((dmabuf
->fmt
& CS_FMT_16BIT
) ?
2887 AFMT_S16_LE
: AFMT_U8
, p
);
2890 return put_user(0, p
);
2891 case SNDCTL_DSP_MAPINBUF
:
2892 case SNDCTL_DSP_MAPOUTBUF
:
2893 case SNDCTL_DSP_SETSYNCRO
:
2894 case SOUND_PCM_WRITE_FILTER
:
2895 case SOUND_PCM_READ_FILTER
:
2903 * AMP control - null AMP
2906 static void amp_none(struct cs_card
*card
, int change
)
2914 static void amp_voyetra(struct cs_card
*card
, int change
)
2916 /* Manage the EAPD bit on the Crystal 4297
2917 and the Analog AD1885 */
2919 int old
= card
->amplifier
;
2921 card
->amplifier
+=change
;
2922 if (card
->amplifier
&& !old
) {
2923 /* Turn the EAPD amp on */
2924 cs_ac97_set(card
->ac97_codec
[0], AC97_POWER_CONTROL
,
2925 cs_ac97_get(card
->ac97_codec
[0], AC97_POWER_CONTROL
) |
2927 } else if(old
&& !card
->amplifier
) {
2928 /* Turn the EAPD amp off */
2929 cs_ac97_set(card
->ac97_codec
[0], AC97_POWER_CONTROL
,
2930 cs_ac97_get(card
->ac97_codec
[0], AC97_POWER_CONTROL
) &
2937 * Game Theatre XP card - EGPIO[2] is used to enable the external amp.
2940 static void amp_hercules(struct cs_card
*card
, int change
)
2942 int old
= card
->amplifier
;
2944 CS_DBGOUT(CS_ERROR
, 2, printk(KERN_INFO
2945 "cs46xx: amp_hercules() called before initialized.\n"));
2948 card
->amplifier
+=change
;
2949 if ((card
->amplifier
&& !old
) && !(hercules_egpio_disable
)) {
2950 CS_DBGOUT(CS_PARMS
, 4, printk(KERN_INFO
2951 "cs46xx: amp_hercules() external amp enabled\n"));
2952 cs461x_pokeBA0(card
, BA0_EGPIODR
,
2953 EGPIODR_GPOE2
); /* enable EGPIO2 output */
2954 cs461x_pokeBA0(card
, BA0_EGPIOPTR
,
2955 EGPIOPTR_GPPT2
); /* open-drain on output */
2956 } else if (old
&& !card
->amplifier
) {
2957 CS_DBGOUT(CS_PARMS
, 4, printk(KERN_INFO
2958 "cs46xx: amp_hercules() external amp disabled\n"));
2959 cs461x_pokeBA0(card
, BA0_EGPIODR
, 0); /* disable */
2960 cs461x_pokeBA0(card
, BA0_EGPIOPTR
, 0); /* disable */
2965 * Handle the CLKRUN on a thinkpad. We must disable CLKRUN support
2966 * whenever we need to beat on the chip.
2968 * The original idea and code for this hack comes from David Kaiser at
2969 * Linuxcare. Perhaps one day Crystal will document their chips well
2970 * enough to make them useful.
2973 static void clkrun_hack(struct cs_card
*card
, int change
)
2975 struct pci_dev
*acpi_dev
;
2979 int old
= card
->active
;
2981 card
->active
+=change
;
2983 acpi_dev
= pci_find_device(PCI_VENDOR_ID_INTEL
, PCI_DEVICE_ID_INTEL_82371AB_3
, NULL
);
2984 if (acpi_dev
== NULL
)
2985 return; /* Not a thinkpad thats for sure */
2987 /* Find the control port */
2988 pci_read_config_byte(acpi_dev
, 0x41, &pp
);
2991 /* Read ACPI port */
2992 control
= inw(port
+ 0x10);
2994 /* Flip CLKRUN off while running */
2995 if (!card
->active
&& old
) {
2996 CS_DBGOUT(CS_PARMS
, 9, printk( KERN_INFO
2997 "cs46xx: clkrun() enable clkrun - change=%d active=%d\n",
2998 change
,card
->active
));
2999 outw(control
|0x2000, port
+0x10);
3002 * sometimes on a resume the bit is set, so always reset the bit.
3004 CS_DBGOUT(CS_PARMS
, 9, printk( KERN_INFO
3005 "cs46xx: clkrun() disable clkrun - change=%d active=%d\n",
3006 change
,card
->active
));
3007 outw(control
&~0x2000, port
+0x10);
3012 static int cs_open(struct inode
*inode
, struct file
*file
)
3014 struct cs_card
*card
= file
->private_data
;
3015 struct cs_state
*state
= NULL
;
3016 struct dmabuf
*dmabuf
= NULL
;
3017 struct list_head
*entry
;
3018 unsigned int minor
= iminor(inode
);
3022 CS_DBGOUT(CS_OPEN
| CS_FUNCTION
, 2, printk("cs46xx: cs_open()+ file=%p %s %s\n",
3023 file
, file
->f_mode
& FMODE_WRITE
? "FMODE_WRITE" : "",
3024 file
->f_mode
& FMODE_READ
? "FMODE_READ" : "") );
3026 list_for_each(entry
, &cs46xx_devs
) {
3027 card
= list_entry(entry
, struct cs_card
, list
);
3029 if (!((card
->dev_audio
^ minor
) & ~0xf))
3032 if (entry
== &cs46xx_devs
)
3035 CS_DBGOUT(CS_FUNCTION
| CS_OPEN
, 2, printk(KERN_INFO
3036 "cs46xx: cs_open(): Error - unable to find audio card struct\n"));
3041 * hardcode state[0] for capture, [1] for playback
3043 if (file
->f_mode
& FMODE_READ
) {
3044 CS_DBGOUT(CS_WAVE_READ
, 2, printk("cs46xx: cs_open() FMODE_READ\n") );
3045 if (card
->states
[0] == NULL
) {
3046 state
= card
->states
[0] =
3047 kmalloc(sizeof(struct cs_state
), GFP_KERNEL
);
3050 memset(state
, 0, sizeof(struct cs_state
));
3051 mutex_init(&state
->sem
);
3052 dmabuf
= &state
->dmabuf
;
3053 dmabuf
->pbuf
= (void *)get_zeroed_page(GFP_KERNEL
| GFP_DMA
);
3054 if (dmabuf
->pbuf
== NULL
) {
3056 card
->states
[0] = NULL
;
3060 state
= card
->states
[0];
3061 if (state
->open_mode
& FMODE_READ
)
3064 dmabuf
->channel
= card
->alloc_rec_pcm_channel(card
);
3066 if (dmabuf
->channel
== NULL
) {
3067 kfree(card
->states
[0]);
3068 card
->states
[0] = NULL
;
3072 /* Now turn on external AMP if needed */
3074 state
->card
->active_ctrl(state
->card
, 1);
3075 state
->card
->amplifier_ctrl(state
->card
, 1);
3077 if ((tmp
= cs46xx_powerup(card
, CS_POWER_ADC
))) {
3078 CS_DBGOUT(CS_ERROR
| CS_INIT
, 1, printk(KERN_INFO
3079 "cs46xx: cs46xx_powerup of ADC failed (0x%x)\n", tmp
));
3083 dmabuf
->channel
->state
= state
;
3084 /* initialize the virtual channel */
3086 state
->magic
= CS_STATE_MAGIC
;
3087 init_waitqueue_head(&dmabuf
->wait
);
3088 mutex_init(&state
->open_mutex
);
3089 file
->private_data
= card
;
3091 mutex_lock(&state
->open_mutex
);
3093 /* set default sample format. According to OSS Programmer's Guide /dev/dsp
3094 should be default to unsigned 8-bits, mono, with sample rate 8kHz and
3095 /dev/dspW will accept 16-bits sample */
3097 /* Default input is 8bit mono */
3098 dmabuf
->fmt
&= ~CS_FMT_MASK
;
3099 dmabuf
->type
= CS_TYPE_ADC
;
3100 dmabuf
->ossfragshift
= 0;
3101 dmabuf
->ossmaxfrags
= 0;
3102 dmabuf
->subdivision
= 0;
3103 cs_set_adc_rate(state
, 8000);
3104 cs_set_divisor(dmabuf
);
3106 state
->open_mode
|= FMODE_READ
;
3107 mutex_unlock(&state
->open_mutex
);
3109 if (file
->f_mode
& FMODE_WRITE
) {
3110 CS_DBGOUT(CS_OPEN
, 2, printk("cs46xx: cs_open() FMODE_WRITE\n") );
3111 if (card
->states
[1] == NULL
) {
3112 state
= card
->states
[1] =
3113 kmalloc(sizeof(struct cs_state
), GFP_KERNEL
);
3116 memset(state
, 0, sizeof(struct cs_state
));
3117 mutex_init(&state
->sem
);
3118 dmabuf
= &state
->dmabuf
;
3119 dmabuf
->pbuf
= (void *)get_zeroed_page(GFP_KERNEL
| GFP_DMA
);
3120 if (dmabuf
->pbuf
== NULL
) {
3122 card
->states
[1] = NULL
;
3126 state
= card
->states
[1];
3127 if (state
->open_mode
& FMODE_WRITE
)
3130 dmabuf
->channel
= card
->alloc_pcm_channel(card
);
3132 if (dmabuf
->channel
== NULL
) {
3133 kfree(card
->states
[1]);
3134 card
->states
[1] = NULL
;
3138 /* Now turn on external AMP if needed */
3140 state
->card
->active_ctrl(state
->card
, 1);
3141 state
->card
->amplifier_ctrl(state
->card
, 1);
3143 if ((tmp
= cs46xx_powerup(card
, CS_POWER_DAC
))) {
3144 CS_DBGOUT(CS_ERROR
| CS_INIT
, 1, printk(KERN_INFO
3145 "cs46xx: cs46xx_powerup of DAC failed (0x%x)\n", tmp
));
3149 dmabuf
->channel
->state
= state
;
3150 /* initialize the virtual channel */
3152 state
->magic
= CS_STATE_MAGIC
;
3153 init_waitqueue_head(&dmabuf
->wait
);
3154 mutex_init(&state
->open_mutex
);
3155 file
->private_data
= card
;
3157 mutex_lock(&state
->open_mutex
);
3159 /* set default sample format. According to OSS Programmer's Guide /dev/dsp
3160 should be default to unsigned 8-bits, mono, with sample rate 8kHz and
3161 /dev/dspW will accept 16-bits sample */
3163 /* Default output is 8bit mono. */
3164 dmabuf
->fmt
&= ~CS_FMT_MASK
;
3165 dmabuf
->type
= CS_TYPE_DAC
;
3166 dmabuf
->ossfragshift
= 0;
3167 dmabuf
->ossmaxfrags
= 0;
3168 dmabuf
->subdivision
= 0;
3169 cs_set_dac_rate(state
, 8000);
3170 cs_set_divisor(dmabuf
);
3172 state
->open_mode
|= FMODE_WRITE
;
3173 mutex_unlock(&state
->open_mutex
);
3174 if ((ret
= prog_dmabuf(state
)))
3177 CS_DBGOUT(CS_OPEN
| CS_FUNCTION
, 2, printk("cs46xx: cs_open()- 0\n"));
3178 return nonseekable_open(inode
, file
);
3181 static int cs_release(struct inode
*inode
, struct file
*file
)
3183 struct cs_card
*card
= file
->private_data
;
3184 struct dmabuf
*dmabuf
;
3185 struct cs_state
*state
;
3187 CS_DBGOUT(CS_RELEASE
| CS_FUNCTION
, 2, printk("cs46xx: cs_release()+ file=%p %s %s\n",
3188 file
, file
->f_mode
& FMODE_WRITE
? "FMODE_WRITE" : "",
3189 file
->f_mode
& FMODE_READ
? "FMODE_READ" : ""));
3191 if (!(file
->f_mode
& (FMODE_WRITE
| FMODE_READ
)))
3193 state
= card
->states
[1];
3195 if ((state
->open_mode
& FMODE_WRITE
) & (file
->f_mode
& FMODE_WRITE
)) {
3196 CS_DBGOUT(CS_RELEASE
, 2, printk("cs46xx: cs_release() FMODE_WRITE\n"));
3197 dmabuf
= &state
->dmabuf
;
3198 cs_clear_tail(state
);
3199 drain_dac(state
, file
->f_flags
& O_NONBLOCK
);
3200 /* stop DMA state machine and free DMA buffers/channels */
3201 mutex_lock(&state
->open_mutex
);
3203 dealloc_dmabuf(state
);
3204 state
->card
->free_pcm_channel(state
->card
, dmabuf
->channel
->num
);
3205 free_page((unsigned long)state
->dmabuf
.pbuf
);
3207 /* we're covered by the open_mutex */
3208 mutex_unlock(&state
->open_mutex
);
3209 state
->card
->states
[state
->virt
] = NULL
;
3210 state
->open_mode
&= (~file
->f_mode
) & (FMODE_READ
|FMODE_WRITE
);
3212 if ((tmp
= cs461x_powerdown(card
, CS_POWER_DAC
, CS_FALSE
))) {
3213 CS_DBGOUT(CS_ERROR
, 1, printk(KERN_INFO
3214 "cs46xx: cs_release_mixdev() powerdown DAC failure (0x%x)\n",tmp
) );
3217 /* Now turn off external AMP if needed */
3218 state
->card
->amplifier_ctrl(state
->card
, -1);
3219 state
->card
->active_ctrl(state
->card
, -1);
3224 state
= card
->states
[0];
3226 if ((state
->open_mode
& FMODE_READ
) & (file
->f_mode
& FMODE_READ
)) {
3227 CS_DBGOUT(CS_RELEASE
, 2, printk("cs46xx: cs_release() FMODE_READ\n"));
3228 dmabuf
= &state
->dmabuf
;
3229 mutex_lock(&state
->open_mutex
);
3231 dealloc_dmabuf(state
);
3232 state
->card
->free_pcm_channel(state
->card
, dmabuf
->channel
->num
);
3233 free_page((unsigned long)state
->dmabuf
.pbuf
);
3235 /* we're covered by the open_mutex */
3236 mutex_unlock(&state
->open_mutex
);
3237 state
->card
->states
[state
->virt
] = NULL
;
3238 state
->open_mode
&= (~file
->f_mode
) & (FMODE_READ
|FMODE_WRITE
);
3240 if ((tmp
= cs461x_powerdown(card
, CS_POWER_ADC
, CS_FALSE
))) {
3241 CS_DBGOUT(CS_ERROR
, 1, printk(KERN_INFO
3242 "cs46xx: cs_release_mixdev() powerdown ADC failure (0x%x)\n",tmp
) );
3245 /* Now turn off external AMP if needed */
3246 state
->card
->amplifier_ctrl(state
->card
, -1);
3247 state
->card
->active_ctrl(state
->card
, -1);
3252 CS_DBGOUT(CS_FUNCTION
| CS_RELEASE
, 2, printk("cs46xx: cs_release()- 0\n"));
3256 static void printpm(struct cs_card
*s
)
3258 CS_DBGOUT(CS_PM
, 9, printk("pm struct:\n"));
3259 CS_DBGOUT(CS_PM
, 9, printk("flags:0x%x u32CLKCR1_SAVE: 0%x u32SSPMValue: 0x%x\n",
3260 (unsigned)s
->pm
.flags
,s
->pm
.u32CLKCR1_SAVE
,s
->pm
.u32SSPMValue
));
3261 CS_DBGOUT(CS_PM
, 9, printk("u32PPLVCvalue: 0x%x u32PPRVCvalue: 0x%x\n",
3262 s
->pm
.u32PPLVCvalue
,s
->pm
.u32PPRVCvalue
));
3263 CS_DBGOUT(CS_PM
, 9, printk("u32FMLVCvalue: 0x%x u32FMRVCvalue: 0x%x\n",
3264 s
->pm
.u32FMLVCvalue
,s
->pm
.u32FMRVCvalue
));
3265 CS_DBGOUT(CS_PM
, 9, printk("u32GPIORvalue: 0x%x u32JSCTLvalue: 0x%x\n",
3266 s
->pm
.u32GPIORvalue
,s
->pm
.u32JSCTLvalue
));
3267 CS_DBGOUT(CS_PM
, 9, printk("u32SSCR: 0x%x u32SRCSA: 0x%x\n",
3268 s
->pm
.u32SSCR
,s
->pm
.u32SRCSA
));
3269 CS_DBGOUT(CS_PM
, 9, printk("u32DacASR: 0x%x u32AdcASR: 0x%x\n",
3270 s
->pm
.u32DacASR
,s
->pm
.u32AdcASR
));
3271 CS_DBGOUT(CS_PM
, 9, printk("u32DacSR: 0x%x u32AdcSR: 0x%x\n",
3272 s
->pm
.u32DacSR
,s
->pm
.u32AdcSR
));
3273 CS_DBGOUT(CS_PM
, 9, printk("u32MIDCR_Save: 0x%x\n",
3274 s
->pm
.u32MIDCR_Save
));
3275 CS_DBGOUT(CS_PM
, 9, printk("u32AC97_powerdown: 0x%x _general_purpose 0x%x\n",
3276 s
->pm
.u32AC97_powerdown
,s
->pm
.u32AC97_general_purpose
));
3277 CS_DBGOUT(CS_PM
, 9, printk("u32AC97_master_volume: 0x%x\n",
3278 s
->pm
.u32AC97_master_volume
));
3279 CS_DBGOUT(CS_PM
, 9, printk("u32AC97_headphone_volume: 0x%x\n",
3280 s
->pm
.u32AC97_headphone_volume
));
3281 CS_DBGOUT(CS_PM
, 9, printk("u32AC97_master_volume_mono: 0x%x\n",
3282 s
->pm
.u32AC97_master_volume_mono
));
3283 CS_DBGOUT(CS_PM
, 9, printk("u32AC97_pcm_out_volume: 0x%x\n",
3284 s
->pm
.u32AC97_pcm_out_volume
));
3285 CS_DBGOUT(CS_PM
, 9, printk("dmabuf_swptr_play: 0x%x dmabuf_count_play: %d\n",
3286 s
->pm
.dmabuf_swptr_play
,s
->pm
.dmabuf_count_play
));
3287 CS_DBGOUT(CS_PM
, 9, printk("dmabuf_swptr_capture: 0x%x dmabuf_count_capture: %d\n",
3288 s
->pm
.dmabuf_swptr_capture
,s
->pm
.dmabuf_count_capture
));
3292 /****************************************************************************
3294 * Suspend - save the ac97 regs, mute the outputs and power down the part.
3296 ****************************************************************************/
3297 static void cs46xx_ac97_suspend(struct cs_card
*card
)
3300 struct ac97_codec
*dev
=card
->ac97_codec
[0];
3303 CS_DBGOUT(CS_PM
, 9, printk("cs46xx: cs46xx_ac97_suspend()+\n"));
3305 if (card
->states
[1]) {
3306 stop_dac(card
->states
[1]);
3307 resync_dma_ptrs(card
->states
[1]);
3309 if (card
->states
[0]) {
3310 stop_adc(card
->states
[0]);
3311 resync_dma_ptrs(card
->states
[0]);
3314 for (Count
= 0x2, i
= 0; (Count
<= CS46XX_AC97_HIGHESTREGTORESTORE
)
3315 && (i
< CS46XX_AC97_NUMBER_RESTORE_REGS
);
3317 card
->pm
.ac97
[i
] = cs_ac97_get(dev
, BA0_AC97_RESET
+ Count
);
3320 * Save the ac97 volume registers as well as the current powerdown state.
3321 * Now, mute the all the outputs (master, headphone, and mono), as well
3322 * as the PCM volume, in preparation for powering down the entire part.
3323 card->pm.u32AC97_master_volume = (u32)cs_ac97_get( dev,
3324 (u8)BA0_AC97_MASTER_VOLUME);
3325 card->pm.u32AC97_headphone_volume = (u32)cs_ac97_get(dev,
3326 (u8)BA0_AC97_HEADPHONE_VOLUME);
3327 card->pm.u32AC97_master_volume_mono = (u32)cs_ac97_get(dev,
3328 (u8)BA0_AC97_MASTER_VOLUME_MONO);
3329 card->pm.u32AC97_pcm_out_volume = (u32)cs_ac97_get(dev,
3330 (u8)BA0_AC97_PCM_OUT_VOLUME);
3335 cs_ac97_set(dev
, (u8
)BA0_AC97_MASTER_VOLUME
, 0x8000);
3336 cs_ac97_set(dev
, (u8
)BA0_AC97_HEADPHONE_VOLUME
, 0x8000);
3337 cs_ac97_set(dev
, (u8
)BA0_AC97_MASTER_VOLUME_MONO
, 0x8000);
3338 cs_ac97_set(dev
, (u8
)BA0_AC97_PCM_OUT_VOLUME
, 0x8000);
3341 * save the registers that cause pops
3343 card
->pm
.u32AC97_powerdown
= (u32
)cs_ac97_get(dev
, (u8
)AC97_POWER_CONTROL
);
3344 card
->pm
.u32AC97_general_purpose
= (u32
)cs_ac97_get(dev
, (u8
)BA0_AC97_GENERAL_PURPOSE
);
3346 * And power down everything on the AC97 codec.
3347 * well, for now, only power down the DAC/ADC and MIXER VREFON components.
3348 * trouble with removing VREF.
3350 if ((tmp
= cs461x_powerdown(card
, CS_POWER_DAC
| CS_POWER_ADC
|
3351 CS_POWER_MIXVON
, CS_TRUE
))) {
3352 CS_DBGOUT(CS_ERROR
| CS_INIT
, 1, printk(KERN_INFO
3353 "cs46xx: cs46xx_ac97_suspend() failure (0x%x)\n",tmp
));
3356 CS_DBGOUT(CS_PM
, 9, printk("cs46xx: cs46xx_ac97_suspend()-\n"));
3359 /****************************************************************************
3361 * Resume - power up the part and restore its registers..
3363 ****************************************************************************/
3364 static void cs46xx_ac97_resume(struct cs_card
*card
)
3367 struct ac97_codec
*dev
=card
->ac97_codec
[0];
3369 CS_DBGOUT(CS_PM
, 9, printk("cs46xx: cs46xx_ac97_resume()+\n"));
3372 * First, we restore the state of the general purpose register. This
3373 * contains the mic select (mic1 or mic2) and if we restore this after
3374 * we restore the mic volume/boost state and mic2 was selected at
3375 * suspend time, we will end up with a brief period of time where mic1
3376 * is selected with the volume/boost settings for mic2, causing
3377 * acoustic feedback. So we restore the general purpose register
3378 * first, thereby getting the correct mic selected before we restore
3379 * the mic volume/boost.
3381 cs_ac97_set(dev
, (u8
)BA0_AC97_GENERAL_PURPOSE
,
3382 (u16
)card
->pm
.u32AC97_general_purpose
);
3384 * Now, while the outputs are still muted, restore the state of power
3387 cs_ac97_set(dev
, (u8
)BA0_AC97_POWERDOWN
, (u16
)card
->pm
.u32AC97_powerdown
);
3388 mdelay(5 * cs_laptop_wait
);
3390 * Restore just the first set of registers, from register number
3391 * 0x02 to the register number that ulHighestRegToRestore specifies.
3393 for (Count
= 0x2, i
=0; (Count
<= CS46XX_AC97_HIGHESTREGTORESTORE
) &&
3394 (i
< CS46XX_AC97_NUMBER_RESTORE_REGS
); Count
+= 2, i
++) {
3395 cs_ac97_set(dev
, (u8
)(BA0_AC97_RESET
+ Count
), (u16
)card
->pm
.ac97
[i
]);
3398 /* Check if we have to init the amplifier */
3400 card
->amp_init(card
);
3402 CS_DBGOUT(CS_PM
, 9, printk("cs46xx: cs46xx_ac97_resume()-\n"));
3406 static int cs46xx_restart_part(struct cs_card
*card
)
3408 struct dmabuf
*dmabuf
;
3410 CS_DBGOUT(CS_PM
| CS_FUNCTION
, 4,
3411 printk( "cs46xx: cs46xx_restart_part()+\n"));
3412 if (card
->states
[1]) {
3413 dmabuf
= &card
->states
[1]->dmabuf
;
3415 resync_dma_ptrs(card
->states
[1]);
3416 cs_set_divisor(dmabuf
);
3417 if (__prog_dmabuf(card
->states
[1])) {
3418 CS_DBGOUT(CS_PM
| CS_ERROR
, 1,
3419 printk("cs46xx: cs46xx_restart_part()- (-1) prog_dmabuf() dac error\n"));
3422 cs_set_dac_rate(card
->states
[1], dmabuf
->rate
);
3424 if (card
->states
[0]) {
3425 dmabuf
= &card
->states
[0]->dmabuf
;
3427 resync_dma_ptrs(card
->states
[0]);
3428 cs_set_divisor(dmabuf
);
3429 if (__prog_dmabuf(card
->states
[0])) {
3430 CS_DBGOUT(CS_PM
| CS_ERROR
, 1,
3431 printk("cs46xx: cs46xx_restart_part()- (-1) prog_dmabuf() adc error\n"));
3434 cs_set_adc_rate(card
->states
[0], dmabuf
->rate
);
3436 card
->pm
.flags
|= CS46XX_PM_RESUMED
;
3437 if (card
->states
[0])
3438 start_adc(card
->states
[0]);
3439 if (card
->states
[1])
3440 start_dac(card
->states
[1]);
3442 card
->pm
.flags
|= CS46XX_PM_IDLE
;
3443 card
->pm
.flags
&= ~(CS46XX_PM_SUSPENDING
| CS46XX_PM_SUSPENDED
3444 | CS46XX_PM_RESUMING
| CS46XX_PM_RESUMED
);
3445 if (card
->states
[0])
3446 wake_up(&card
->states
[0]->dmabuf
.wait
);
3447 if (card
->states
[1])
3448 wake_up(&card
->states
[1]->dmabuf
.wait
);
3450 CS_DBGOUT(CS_PM
| CS_FUNCTION
, 4,
3451 printk( "cs46xx: cs46xx_restart_part()-\n"));
3455 static void cs461x_reset(struct cs_card
*card
);
3456 static void cs461x_proc_stop(struct cs_card
*card
);
3457 static int cs46xx_suspend(struct cs_card
*card
, pm_message_t state
)
3461 CS_DBGOUT(CS_PM
| CS_FUNCTION
, 4,
3462 printk("cs46xx: cs46xx_suspend()+ flags=0x%x s=%p\n",
3463 (unsigned)card
->pm
.flags
,card
));
3465 * check the current state, only suspend if IDLE
3467 if (!(card
->pm
.flags
& CS46XX_PM_IDLE
)) {
3468 CS_DBGOUT(CS_PM
| CS_ERROR
, 2,
3469 printk("cs46xx: cs46xx_suspend() unable to suspend, not IDLE\n"));
3472 card
->pm
.flags
&= ~CS46XX_PM_IDLE
;
3473 card
->pm
.flags
|= CS46XX_PM_SUSPENDING
;
3475 card
->active_ctrl(card
,1);
3477 tmp
= cs461x_peek(card
, BA1_PFIE
);
3480 cs461x_poke(card
, BA1_PFIE
, tmp
); /* playback interrupt disable */
3482 tmp
= cs461x_peek(card
, BA1_CIE
);
3485 cs461x_poke(card
, BA1_CIE
, tmp
); /* capture interrupt disable */
3488 * Stop playback DMA.
3490 tmp
= cs461x_peek(card
, BA1_PCTL
);
3491 cs461x_poke(card
, BA1_PCTL
, tmp
& 0x0000ffff);
3496 tmp
= cs461x_peek(card
, BA1_CCTL
);
3497 cs461x_poke(card
, BA1_CCTL
, tmp
& 0xffff0000);
3499 if (card
->states
[1]) {
3500 card
->pm
.dmabuf_swptr_play
= card
->states
[1]->dmabuf
.swptr
;
3501 card
->pm
.dmabuf_count_play
= card
->states
[1]->dmabuf
.count
;
3503 if (card
->states
[0]) {
3504 card
->pm
.dmabuf_swptr_capture
= card
->states
[0]->dmabuf
.swptr
;
3505 card
->pm
.dmabuf_count_capture
= card
->states
[0]->dmabuf
.count
;
3508 cs46xx_ac97_suspend(card
);
3511 * Reset the processor.
3515 cs461x_proc_stop(card
);
3518 * Power down the DAC and ADC. For now leave the other areas on.
3520 cs_ac97_set(card
->ac97_codec
[0], AC97_POWER_CONTROL
, 0x0300);
3523 * Power down the PLL.
3525 cs461x_pokeBA0(card
, BA0_CLKCR1
, 0);
3528 * Turn off the Processor by turning off the software clock enable flag in
3529 * the clock control register.
3531 tmp
= cs461x_peekBA0(card
, BA0_CLKCR1
) & ~CLKCR1_SWCE
;
3532 cs461x_pokeBA0(card
, BA0_CLKCR1
, tmp
);
3534 card
->active_ctrl(card
,-1);
3536 card
->pm
.flags
&= ~CS46XX_PM_SUSPENDING
;
3537 card
->pm
.flags
|= CS46XX_PM_SUSPENDED
;
3541 CS_DBGOUT(CS_PM
| CS_FUNCTION
, 4,
3542 printk("cs46xx: cs46xx_suspend()- flags=0x%x\n",
3543 (unsigned)card
->pm
.flags
));
3547 static int cs46xx_resume(struct cs_card
*card
)
3551 CS_DBGOUT(CS_PM
| CS_FUNCTION
, 4,
3552 printk( "cs46xx: cs46xx_resume()+ flags=0x%x\n",
3553 (unsigned)card
->pm
.flags
));
3554 if (!(card
->pm
.flags
& CS46XX_PM_SUSPENDED
)) {
3555 CS_DBGOUT(CS_PM
| CS_ERROR
, 2,
3556 printk("cs46xx: cs46xx_resume() unable to resume, not SUSPENDED\n"));
3559 card
->pm
.flags
|= CS46XX_PM_RESUMING
;
3560 card
->pm
.flags
&= ~CS46XX_PM_SUSPENDED
;
3562 card
->active_ctrl(card
, 1);
3564 for (i
= 0; i
< 5; i
++) {
3565 if (cs_hardware_init(card
) != 0) {
3566 CS_DBGOUT(CS_PM
| CS_ERROR
, 4, printk(
3567 "cs46xx: cs46xx_resume()- ERROR in cs_hardware_init()\n"));
3568 mdelay(10 * cs_laptop_wait
);
3575 CS_DBGOUT(CS_PM
| CS_ERROR
, 1, printk(
3576 "cs46xx: cs46xx_resume()- cs_hardware_init() failed, retried %d times.\n",i
));
3580 if (cs46xx_restart_part(card
)) {
3581 CS_DBGOUT(CS_PM
| CS_ERROR
, 4, printk(
3582 "cs46xx: cs46xx_resume(): cs46xx_restart_part() returned error\n"));
3585 card
->active_ctrl(card
, -1);
3587 CS_DBGOUT(CS_PM
| CS_FUNCTION
, 4, printk("cs46xx: cs46xx_resume()- flags=0x%x\n",
3588 (unsigned)card
->pm
.flags
));
3592 static /*const*/ struct file_operations cs461x_fops
= {
3593 CS_OWNER CS_THIS_MODULE
3594 .llseek
= no_llseek
,
3601 .release
= cs_release
,
3604 /* Write AC97 codec registers */
3607 static u16
_cs_ac97_get(struct ac97_codec
*dev
, u8 reg
)
3609 struct cs_card
*card
= dev
->private_data
;
3615 * 1. Write ACCAD = Command Address Register = 46Ch for AC97 register address
3616 * 2. Write ACCDA = Command Data Register = 470h for data to write to AC97
3617 * 3. Write ACCTL = Control Register = 460h for initiating the write
3618 * 4. Read ACCTL = 460h, DCV should be reset by now and 460h = 17h
3619 * 5. if DCV not cleared, break and return error
3620 * 6. Read ACSTS = Status Register = 464h, check VSTS bit
3623 cs461x_peekBA0(card
, BA0_ACSDA
);
3626 * Setup the AC97 control registers on the CS461x to send the
3627 * appropriate command to the AC97 to perform the read.
3628 * ACCAD = Command Address Register = 46Ch
3629 * ACCDA = Command Data Register = 470h
3630 * ACCTL = Control Register = 460h
3631 * set DCV - will clear when process completed
3632 * set CRW - Read command
3633 * set VFRM - valid frame enabled
3634 * set ESYN - ASYNC generation enabled
3635 * set RSTN - ARST# inactive, AC97 codec not reset
3638 cs461x_pokeBA0(card
, BA0_ACCAD
, reg
);
3639 cs461x_pokeBA0(card
, BA0_ACCDA
, 0);
3640 cs461x_pokeBA0(card
, BA0_ACCTL
, ACCTL_DCV
| ACCTL_CRW
|
3641 ACCTL_VFRM
| ACCTL_ESYN
|
3646 * Wait for the read to occur.
3648 if (!(card
->pm
.flags
& CS46XX_PM_IDLE
))
3651 loopcnt
= 500 * cs_laptop_wait
;
3652 loopcnt
*= cs_laptop_wait
;
3653 for (count
= 0; count
< loopcnt
; count
++) {
3655 * First, we want to wait for a short time.
3657 udelay(10 * cs_laptop_wait
);
3659 * Now, check to see if the read has completed.
3660 * ACCTL = 460h, DCV should be reset by now and 460h = 17h
3662 if (!(cs461x_peekBA0(card
, BA0_ACCTL
) & ACCTL_DCV
))
3667 * Make sure the read completed.
3669 if (cs461x_peekBA0(card
, BA0_ACCTL
) & ACCTL_DCV
) {
3670 CS_DBGOUT(CS_ERROR
, 1, printk(KERN_WARNING
3671 "cs46xx: AC'97 read problem (ACCTL_DCV), reg = 0x%x returning 0xffff\n", reg
));
3676 * Wait for the valid status bit to go active.
3679 if (!(card
->pm
.flags
& CS46XX_PM_IDLE
))
3683 loopcnt
*= cs_laptop_wait
;
3684 for (count
= 0; count
< loopcnt
; count
++) {
3686 * Read the AC97 status register.
3687 * ACSTS = Status Register = 464h
3688 * VSTS - Valid Status
3690 if (cs461x_peekBA0(card
, BA0_ACSTS
) & ACSTS_VSTS
)
3692 udelay(10 * cs_laptop_wait
);
3696 * Make sure we got valid status.
3698 if (!((tmp
= cs461x_peekBA0(card
, BA0_ACSTS
)) & ACSTS_VSTS
)) {
3699 CS_DBGOUT(CS_ERROR
, 2, printk(KERN_WARNING
3700 "cs46xx: AC'97 read problem (ACSTS_VSTS), reg = 0x%x val=0x%x 0xffff \n",
3706 * Read the data returned from the AC97 register.
3707 * ACSDA = Status Data Register = 474h
3709 CS_DBGOUT(CS_FUNCTION
, 9, printk(KERN_INFO
3710 "cs46xx: cs_ac97_get() reg = 0x%x, val = 0x%x, BA0_ACCAD = 0x%x\n",
3711 reg
, cs461x_peekBA0(card
, BA0_ACSDA
),
3712 cs461x_peekBA0(card
, BA0_ACCAD
)));
3713 ret
= cs461x_peekBA0(card
, BA0_ACSDA
);
3717 static u16
cs_ac97_get(struct ac97_codec
*dev
, u8 reg
)
3720 struct cs_card
*card
= dev
->private_data
;
3722 spin_lock(&card
->ac97_lock
);
3723 ret
= _cs_ac97_get(dev
, reg
);
3724 spin_unlock(&card
->ac97_lock
);
3728 static void cs_ac97_set(struct ac97_codec
*dev
, u8 reg
, u16 val
)
3730 struct cs_card
*card
= dev
->private_data
;
3734 spin_lock(&card
->ac97_lock
);
3736 if (reg
== AC97_CD_VOL
)
3737 val2
= _cs_ac97_get(dev
, AC97_CD_VOL
);
3740 * 1. Write ACCAD = Command Address Register = 46Ch for AC97 register address
3741 * 2. Write ACCDA = Command Data Register = 470h for data to write to AC97
3742 * 3. Write ACCTL = Control Register = 460h for initiating the write
3743 * 4. Read ACCTL = 460h, DCV should be reset by now and 460h = 07h
3744 * 5. if DCV not cleared, break and return error
3748 * Setup the AC97 control registers on the CS461x to send the
3749 * appropriate command to the AC97 to perform the read.
3750 * ACCAD = Command Address Register = 46Ch
3751 * ACCDA = Command Data Register = 470h
3752 * ACCTL = Control Register = 460h
3753 * set DCV - will clear when process completed
3754 * reset CRW - Write command
3755 * set VFRM - valid frame enabled
3756 * set ESYN - ASYNC generation enabled
3757 * set RSTN - ARST# inactive, AC97 codec not reset
3759 cs461x_pokeBA0(card
, BA0_ACCAD
, reg
);
3760 cs461x_pokeBA0(card
, BA0_ACCDA
, val
);
3761 cs461x_peekBA0(card
, BA0_ACCTL
);
3762 cs461x_pokeBA0(card
, BA0_ACCTL
, 0 | ACCTL_VFRM
| ACCTL_ESYN
| ACCTL_RSTN
);
3763 cs461x_pokeBA0(card
, BA0_ACCTL
, ACCTL_DCV
| ACCTL_VFRM
|
3764 ACCTL_ESYN
| ACCTL_RSTN
);
3765 for (count
= 0; count
< 1000; count
++) {
3767 * First, we want to wait for a short time.
3769 udelay(10 * cs_laptop_wait
);
3771 * Now, check to see if the write has completed.
3772 * ACCTL = 460h, DCV should be reset by now and 460h = 07h
3774 if (!(cs461x_peekBA0(card
, BA0_ACCTL
) & ACCTL_DCV
))
3778 * Make sure the write completed.
3780 if (cs461x_peekBA0(card
, BA0_ACCTL
) & ACCTL_DCV
) {
3781 CS_DBGOUT(CS_ERROR
, 1, printk(KERN_WARNING
3782 "cs46xx: AC'97 write problem, reg = 0x%x, val = 0x%x\n", reg
, val
));
3785 spin_unlock(&card
->ac97_lock
);
3788 * Adjust power if the mixer is selected/deselected according
3791 * IF the CD is a valid input source (mixer or direct) AND
3792 * the CD is not muted THEN power is needed
3794 * We do two things. When record select changes the input to
3795 * add/remove the CD we adjust the power count if the CD is
3798 * When the CD mute changes we adjust the power level if the
3799 * CD was a valid input.
3801 * We also check for CD volume != 0, as the CD mute isn't
3802 * normally tweaked from userspace.
3805 /* CD mute change ? */
3807 if (reg
== AC97_CD_VOL
) {
3808 /* Mute bit change ? */
3809 if ((val2
^val
) & 0x8000 ||
3810 ((val2
== 0x1f1f || val
== 0x1f1f) && val2
!= val
)) {
3811 /* This is a hack but its cleaner than the alternatives.
3812 Right now card->ac97_codec[0] might be NULL as we are
3813 still doing codec setup. This does an early assignment
3814 to avoid the problem if it occurs */
3816 if (card
->ac97_codec
[0] == NULL
)
3817 card
->ac97_codec
[0] = dev
;
3820 if (val
& 0x8000 || val
== 0x1f1f)
3821 card
->amplifier_ctrl(card
, -1);
3822 else { /* Mute off power on */
3824 card
->amp_init(card
);
3825 card
->amplifier_ctrl(card
, 1);
3831 /* OSS /dev/mixer file operation methods */
3833 static int cs_open_mixdev(struct inode
*inode
, struct file
*file
)
3836 unsigned int minor
= iminor(inode
);
3837 struct cs_card
*card
= NULL
;
3838 struct list_head
*entry
;
3841 CS_DBGOUT(CS_FUNCTION
| CS_OPEN
, 4,
3842 printk(KERN_INFO
"cs46xx: cs_open_mixdev()+\n"));
3844 list_for_each(entry
, &cs46xx_devs
) {
3845 card
= list_entry(entry
, struct cs_card
, list
);
3846 for (i
= 0; i
< NR_AC97
; i
++)
3847 if (card
->ac97_codec
[i
] != NULL
&&
3848 card
->ac97_codec
[i
]->dev_mixer
== minor
)
3852 CS_DBGOUT(CS_FUNCTION
| CS_OPEN
| CS_ERROR
, 2,
3853 printk(KERN_INFO
"cs46xx: cs46xx_open_mixdev()- -ENODEV\n"));
3857 if (!card
->ac97_codec
[i
])
3859 file
->private_data
= card
->ac97_codec
[i
];
3861 card
->active_ctrl(card
,1);
3862 if (!CS_IN_USE(&card
->mixer_use_cnt
)) {
3863 if ((tmp
= cs46xx_powerup(card
, CS_POWER_MIXVON
))) {
3864 CS_DBGOUT(CS_ERROR
| CS_INIT
, 1, printk(KERN_INFO
3865 "cs46xx: cs_open_mixdev() powerup failure (0x%x)\n", tmp
));
3869 card
->amplifier_ctrl(card
, 1);
3870 CS_INC_USE_COUNT(&card
->mixer_use_cnt
);
3871 CS_DBGOUT(CS_FUNCTION
| CS_OPEN
, 4,
3872 printk(KERN_INFO
"cs46xx: cs_open_mixdev()- 0\n"));
3873 return nonseekable_open(inode
, file
);
3876 static int cs_release_mixdev(struct inode
*inode
, struct file
*file
)
3878 unsigned int minor
= iminor(inode
);
3879 struct cs_card
*card
= NULL
;
3880 struct list_head
*entry
;
3884 CS_DBGOUT(CS_FUNCTION
| CS_RELEASE
, 4,
3885 printk(KERN_INFO
"cs46xx: cs_release_mixdev()+\n"));
3886 list_for_each(entry
, &cs46xx_devs
)
3888 card
= list_entry(entry
, struct cs_card
, list
);
3889 for (i
= 0; i
< NR_AC97
; i
++)
3890 if (card
->ac97_codec
[i
] != NULL
&&
3891 card
->ac97_codec
[i
]->dev_mixer
== minor
)
3895 CS_DBGOUT(CS_FUNCTION
| CS_OPEN
| CS_ERROR
, 2,
3896 printk(KERN_INFO
"cs46xx: cs46xx_open_mixdev()- -ENODEV\n"));
3900 if (!CS_DEC_AND_TEST(&card
->mixer_use_cnt
)) {
3901 CS_DBGOUT(CS_FUNCTION
| CS_RELEASE
, 4,
3902 printk(KERN_INFO
"cs46xx: cs_release_mixdev()- no powerdown, usecnt>0\n"));
3903 card
->active_ctrl(card
, -1);
3904 card
->amplifier_ctrl(card
, -1);
3908 * ok, no outstanding mixer opens, so powerdown.
3910 if ((tmp
= cs461x_powerdown(card
, CS_POWER_MIXVON
, CS_FALSE
))) {
3911 CS_DBGOUT(CS_ERROR
| CS_INIT
, 1, printk(KERN_INFO
3912 "cs46xx: cs_release_mixdev() powerdown MIXVON failure (0x%x)\n", tmp
));
3913 card
->active_ctrl(card
, -1);
3914 card
->amplifier_ctrl(card
, -1);
3917 card
->active_ctrl(card
, -1);
3918 card
->amplifier_ctrl(card
, -1);
3919 CS_DBGOUT(CS_FUNCTION
| CS_RELEASE
, 4,
3920 printk(KERN_INFO
"cs46xx: cs_release_mixdev()- 0\n"));
3924 static int cs_ioctl_mixdev(struct inode
*inode
, struct file
*file
, unsigned int cmd
,
3927 struct ac97_codec
*codec
= file
->private_data
;
3928 struct cs_card
*card
= NULL
;
3929 struct list_head
*entry
;
3930 unsigned long __user
*p
= (long __user
*)arg
;
3931 #if CSDEBUG_INTERFACE
3934 if ( (cmd
== SOUND_MIXER_CS_GETDBGMASK
) ||
3935 (cmd
== SOUND_MIXER_CS_SETDBGMASK
) ||
3936 (cmd
== SOUND_MIXER_CS_GETDBGLEVEL
) ||
3937 (cmd
== SOUND_MIXER_CS_SETDBGLEVEL
) ||
3938 (cmd
== SOUND_MIXER_CS_APM
)) {
3940 case SOUND_MIXER_CS_GETDBGMASK
:
3941 return put_user(cs_debugmask
, p
);
3942 case SOUND_MIXER_CS_GETDBGLEVEL
:
3943 return put_user(cs_debuglevel
, p
);
3944 case SOUND_MIXER_CS_SETDBGMASK
:
3945 if (get_user(val
, p
))
3949 case SOUND_MIXER_CS_SETDBGLEVEL
:
3950 if (get_user(val
, p
))
3952 cs_debuglevel
= val
;
3954 case SOUND_MIXER_CS_APM
:
3955 if (get_user(val
, p
))
3957 if (val
== CS_IOCTL_CMD_SUSPEND
) {
3958 list_for_each(entry
, &cs46xx_devs
) {
3959 card
= list_entry(entry
, struct cs_card
, list
);
3960 cs46xx_suspend(card
, PMSG_ON
);
3963 } else if (val
== CS_IOCTL_CMD_RESUME
) {
3964 list_for_each(entry
, &cs46xx_devs
) {
3965 card
= list_entry(entry
, struct cs_card
, list
);
3966 cs46xx_resume(card
);
3969 CS_DBGOUT(CS_ERROR
, 1, printk(KERN_INFO
3970 "cs46xx: mixer_ioctl(): invalid APM cmd (%d)\n",
3975 CS_DBGOUT(CS_ERROR
, 1, printk(KERN_INFO
3976 "cs46xx: mixer_ioctl(): ERROR unknown debug cmd\n"));
3981 return codec
->mixer_ioctl(codec
, cmd
, arg
);
3984 static /*const*/ struct file_operations cs_mixer_fops
= {
3985 CS_OWNER CS_THIS_MODULE
3986 .llseek
= no_llseek
,
3987 .ioctl
= cs_ioctl_mixdev
,
3988 .open
= cs_open_mixdev
,
3989 .release
= cs_release_mixdev
,
3992 /* AC97 codec initialisation. */
3993 static int __init
cs_ac97_init(struct cs_card
*card
)
3997 struct ac97_codec
*codec
;
4000 CS_DBGOUT(CS_FUNCTION
| CS_INIT
, 2, printk(KERN_INFO
4001 "cs46xx: cs_ac97_init()+\n") );
4003 for (num_ac97
= 0; num_ac97
< NR_AC97
; num_ac97
++) {
4004 if ((codec
= ac97_alloc_codec()) == NULL
)
4007 /* initialize some basic codec information, other fields will be filled
4008 in ac97_probe_codec */
4009 codec
->private_data
= card
;
4010 codec
->id
= num_ac97
;
4012 codec
->codec_read
= cs_ac97_get
;
4013 codec
->codec_write
= cs_ac97_set
;
4015 if (ac97_probe_codec(codec
) == 0) {
4016 CS_DBGOUT(CS_FUNCTION
| CS_INIT
, 2, printk(KERN_INFO
4017 "cs46xx: cs_ac97_init()- codec number %d not found\n",
4019 card
->ac97_codec
[num_ac97
] = NULL
;
4022 CS_DBGOUT(CS_FUNCTION
| CS_INIT
, 2, printk(KERN_INFO
4023 "cs46xx: cs_ac97_init() found codec %d\n",num_ac97
));
4025 eid
= cs_ac97_get(codec
, AC97_EXTENDED_ID
);
4027 if (eid
== 0xFFFF) {
4028 printk(KERN_WARNING
"cs46xx: codec %d not present\n",num_ac97
);
4029 ac97_release_codec(codec
);
4033 card
->ac97_features
= eid
;
4035 if ((codec
->dev_mixer
= register_sound_mixer(&cs_mixer_fops
, -1)) < 0) {
4036 printk(KERN_ERR
"cs46xx: couldn't register mixer!\n");
4037 ac97_release_codec(codec
);
4040 card
->ac97_codec
[num_ac97
] = codec
;
4042 CS_DBGOUT(CS_FUNCTION
| CS_INIT
, 2, printk(KERN_INFO
4043 "cs46xx: cs_ac97_init() ac97_codec[%d] set to %p\n",
4044 (unsigned int)num_ac97
,
4046 /* if there is no secondary codec at all, don't probe any more */
4053 CS_DBGOUT(CS_FUNCTION
| CS_INIT
, 2, printk(KERN_INFO
4054 "cs46xx: cs_ac97_init()- %d\n", (unsigned int)num_ac97
));
4059 * load the static image into the DSP
4061 #include "cs461x_image.h"
4062 static void cs461x_download_image(struct cs_card
*card
)
4064 unsigned i
, j
, temp1
, temp2
, offset
, count
;
4065 unsigned char __iomem
*pBA1
= ioremap(card
->ba1_addr
, 0x40000);
4066 for (i
= 0; i
< CLEAR__COUNT
; i
++) {
4067 offset
= ClrStat
[i
].BA1__DestByteOffset
;
4068 count
= ClrStat
[i
].BA1__SourceSize
;
4069 for (temp1
= offset
; temp1
< (offset
+ count
); temp1
+= 4)
4070 writel(0, pBA1
+temp1
);
4073 for (i
= 0; i
< FILL__COUNT
; i
++) {
4074 temp2
= FillStat
[i
].Offset
;
4075 for (j
= 0; j
< (FillStat
[i
].Size
) / 4; j
++) {
4076 temp1
= (FillStat
[i
]).pFill
[j
];
4077 writel(temp1
, pBA1
+temp2
+ j
* 4);
4087 static void cs461x_reset(struct cs_card
*card
)
4092 * Write the reset bit of the SP control register.
4094 cs461x_poke(card
, BA1_SPCR
, SPCR_RSTSP
);
4097 * Write the control register.
4099 cs461x_poke(card
, BA1_SPCR
, SPCR_DRQEN
);
4102 * Clear the trap registers.
4104 for (idx
= 0; idx
< 8; idx
++) {
4105 cs461x_poke(card
, BA1_DREG
, DREG_REGID_TRAP_SELECT
+ idx
);
4106 cs461x_poke(card
, BA1_TWPR
, 0xFFFF);
4108 cs461x_poke(card
, BA1_DREG
, 0);
4111 * Set the frame timer to reflect the number of cycles per frame.
4113 cs461x_poke(card
, BA1_FRMT
, 0xadf);
4116 static void cs461x_clear_serial_FIFOs(struct cs_card
*card
, int type
)
4118 int idx
, loop
, startfifo
=0, endfifo
=0, powerdown1
= 0;
4122 * See if the devices are powered down. If so, we must power them up first
4123 * or they will not respond.
4125 if (!((tmp
= cs461x_peekBA0(card
, BA0_CLKCR1
)) & CLKCR1_SWCE
)) {
4126 cs461x_pokeBA0(card
, BA0_CLKCR1
, tmp
| CLKCR1_SWCE
);
4131 * We want to clear out the serial port FIFOs so we don't end up playing
4132 * whatever random garbage happens to be in them. We fill the sample FIFOS
4133 * with zero (silence).
4135 cs461x_pokeBA0(card
, BA0_SERBWP
, 0);
4138 * Check for which FIFO locations to clear, if we are currently
4139 * playing or capturing then we don't want to put in 128 bytes of
4142 if (type
& CS_TYPE_DAC
) {
4146 if (type
& CS_TYPE_ADC
) {
4152 * Fill sample FIFO locations (256 locations total).
4154 for (idx
= startfifo
; idx
< endfifo
; idx
++) {
4156 * Make sure the previous FIFO write operation has completed.
4158 for (loop
= 0; loop
< 5; loop
++) {
4160 if (!(cs461x_peekBA0(card
, BA0_SERBST
) & SERBST_WBSY
))
4163 if (cs461x_peekBA0(card
, BA0_SERBST
) & SERBST_WBSY
) {
4165 cs461x_pokeBA0(card
, BA0_CLKCR1
, tmp
);
4168 * Write the serial port FIFO index.
4170 cs461x_pokeBA0(card
, BA0_SERBAD
, idx
);
4172 * Tell the serial port to load the new value into the FIFO location.
4174 cs461x_pokeBA0(card
, BA0_SERBCM
, SERBCM_WRC
);
4177 * Now, if we powered up the devices, then power them back down again.
4178 * This is kinda ugly, but should never happen.
4181 cs461x_pokeBA0(card
, BA0_CLKCR1
, tmp
);
4185 static int cs461x_powerdown(struct cs_card
*card
, unsigned int type
, int suspendflag
)
4188 unsigned int tmp
=0,muted
=0;
4190 CS_DBGOUT(CS_FUNCTION
, 4, printk(KERN_INFO
4191 "cs46xx: cs461x_powerdown()+ type=0x%x\n",type
));
4192 if (!cs_powerdown
&& !suspendflag
) {
4193 CS_DBGOUT(CS_FUNCTION
, 8, printk(KERN_INFO
4194 "cs46xx: cs461x_powerdown() DISABLED exiting\n"));
4197 tmp
= cs_ac97_get(card
->ac97_codec
[0], AC97_POWER_CONTROL
);
4198 CS_DBGOUT(CS_FUNCTION
, 8, printk(KERN_INFO
4199 "cs46xx: cs461x_powerdown() powerdown reg=0x%x\n",tmp
));
4201 * if powering down only the VREF, and not powering down the DAC/ADC,
4202 * then do not power down the VREF, UNLESS both the DAC and ADC are not
4203 * currently powered down. If powering down DAC and ADC, then
4204 * it is possible to power down the VREF (ON).
4206 if (((type
& CS_POWER_MIXVON
) &&
4207 (!(type
& CS_POWER_ADC
) || (!(type
& CS_POWER_DAC
))))
4209 ((tmp
& CS_AC97_POWER_CONTROL_ADC_ON
) ||
4210 (tmp
& CS_AC97_POWER_CONTROL_DAC_ON
))) {
4211 CS_DBGOUT(CS_FUNCTION
, 8, printk(KERN_INFO
4212 "cs46xx: cs461x_powerdown()- 0 unable to powerdown. tmp=0x%x\n",tmp
));
4216 * for now, always keep power to the mixer block.
4217 * not sure why it's a problem but it seems to be if we power off.
4219 type
&= ~CS_POWER_MIXVON
;
4220 type
&= ~CS_POWER_MIXVOFF
;
4223 * Power down indicated areas.
4225 if (type
& CS_POWER_MIXVOFF
) {
4227 CS_DBGOUT(CS_FUNCTION
, 4,
4228 printk(KERN_INFO
"cs46xx: cs461x_powerdown()+ MIXVOFF\n"));
4230 * Power down the MIXER (VREF ON) on the AC97 card.
4232 tmp
= cs_ac97_get(card
->ac97_codec
[0], AC97_POWER_CONTROL
);
4233 if (tmp
& CS_AC97_POWER_CONTROL_MIXVOFF_ON
) {
4235 cs_mute(card
, CS_TRUE
);
4238 tmp
|= CS_AC97_POWER_CONTROL_MIXVOFF
;
4239 cs_ac97_set(card
->ac97_codec
[0], AC97_POWER_CONTROL
, tmp
);
4241 * Now, we wait until we sample a ready state.
4243 for (count
= 0; count
< 32; count
++) {
4245 * First, lets wait a short while to let things settle out a
4246 * bit, and to prevent retrying the read too quickly.
4251 * Read the current state of the power control register.
4253 if (!(cs_ac97_get(card
->ac97_codec
[0], AC97_POWER_CONTROL
) &
4254 CS_AC97_POWER_CONTROL_MIXVOFF_ON
))
4259 * Check the status..
4261 if (cs_ac97_get(card
->ac97_codec
[0], AC97_POWER_CONTROL
) &
4262 CS_AC97_POWER_CONTROL_MIXVOFF_ON
) {
4263 CS_DBGOUT(CS_ERROR
, 1, printk(KERN_WARNING
4264 "cs46xx: powerdown MIXVOFF failed\n"));
4269 if (type
& CS_POWER_MIXVON
) {
4271 CS_DBGOUT(CS_FUNCTION
, 4,
4272 printk(KERN_INFO
"cs46xx: cs461x_powerdown()+ MIXVON\n"));
4274 * Power down the MIXER (VREF ON) on the AC97 card.
4276 tmp
= cs_ac97_get(card
->ac97_codec
[0], AC97_POWER_CONTROL
);
4277 if (tmp
& CS_AC97_POWER_CONTROL_MIXVON_ON
) {
4279 cs_mute(card
, CS_TRUE
);
4282 tmp
|= CS_AC97_POWER_CONTROL_MIXVON
;
4283 cs_ac97_set(card
->ac97_codec
[0], AC97_POWER_CONTROL
, tmp
);
4285 * Now, we wait until we sample a ready state.
4287 for (count
= 0; count
< 32; count
++) {
4289 * First, lets wait a short while to let things settle out a
4290 * bit, and to prevent retrying the read too quickly.
4295 * Read the current state of the power control register.
4297 if (!(cs_ac97_get(card
->ac97_codec
[0], AC97_POWER_CONTROL
) &
4298 CS_AC97_POWER_CONTROL_MIXVON_ON
))
4303 * Check the status..
4305 if (cs_ac97_get(card
->ac97_codec
[0], AC97_POWER_CONTROL
) &
4306 CS_AC97_POWER_CONTROL_MIXVON_ON
) {
4307 CS_DBGOUT(CS_ERROR
, 1, printk(KERN_WARNING
4308 "cs46xx: powerdown MIXVON failed\n"));
4313 if (type
& CS_POWER_ADC
) {
4315 * Power down the ADC on the AC97 card.
4317 CS_DBGOUT(CS_FUNCTION
, 4, printk(KERN_INFO
"cs46xx: cs461x_powerdown()+ ADC\n"));
4318 tmp
= cs_ac97_get(card
->ac97_codec
[0], AC97_POWER_CONTROL
);
4319 if (tmp
& CS_AC97_POWER_CONTROL_ADC_ON
) {
4321 cs_mute(card
, CS_TRUE
);
4324 tmp
|= CS_AC97_POWER_CONTROL_ADC
;
4325 cs_ac97_set(card
->ac97_codec
[0], AC97_POWER_CONTROL
, tmp
);
4328 * Now, we wait until we sample a ready state.
4330 for (count
= 0; count
< 32; count
++) {
4332 * First, lets wait a short while to let things settle out a
4333 * bit, and to prevent retrying the read too quickly.
4338 * Read the current state of the power control register.
4340 if (!(cs_ac97_get(card
->ac97_codec
[0], AC97_POWER_CONTROL
) &
4341 CS_AC97_POWER_CONTROL_ADC_ON
))
4346 * Check the status..
4348 if (cs_ac97_get(card
->ac97_codec
[0], AC97_POWER_CONTROL
) &
4349 CS_AC97_POWER_CONTROL_ADC_ON
) {
4350 CS_DBGOUT(CS_ERROR
, 1, printk(KERN_WARNING
4351 "cs46xx: powerdown ADC failed\n"));
4356 if (type
& CS_POWER_DAC
) {
4358 * Power down the DAC on the AC97 card.
4361 CS_DBGOUT(CS_FUNCTION
, 4,
4362 printk(KERN_INFO
"cs46xx: cs461x_powerdown()+ DAC\n"));
4363 tmp
= cs_ac97_get(card
->ac97_codec
[0], AC97_POWER_CONTROL
);
4364 if (tmp
& CS_AC97_POWER_CONTROL_DAC_ON
) {
4366 cs_mute(card
, CS_TRUE
);
4369 tmp
|= CS_AC97_POWER_CONTROL_DAC
;
4370 cs_ac97_set(card
->ac97_codec
[0], AC97_POWER_CONTROL
, tmp
);
4372 * Now, we wait until we sample a ready state.
4374 for (count
= 0; count
< 32; count
++) {
4376 * First, lets wait a short while to let things settle out a
4377 * bit, and to prevent retrying the read too quickly.
4382 * Read the current state of the power control register.
4384 if (!(cs_ac97_get(card
->ac97_codec
[0], AC97_POWER_CONTROL
) &
4385 CS_AC97_POWER_CONTROL_DAC_ON
))
4390 * Check the status..
4392 if (cs_ac97_get(card
->ac97_codec
[0], AC97_POWER_CONTROL
) &
4393 CS_AC97_POWER_CONTROL_DAC_ON
) {
4394 CS_DBGOUT(CS_ERROR
, 1, printk(KERN_WARNING
4395 "cs46xx: powerdown DAC failed\n"));
4400 tmp
= cs_ac97_get(card
->ac97_codec
[0], AC97_POWER_CONTROL
);
4402 cs_mute(card
, CS_FALSE
);
4403 CS_DBGOUT(CS_FUNCTION
, 4, printk(KERN_INFO
4404 "cs46xx: cs461x_powerdown()- 0 tmp=0x%x\n",tmp
));
4408 static int cs46xx_powerup(struct cs_card
*card
, unsigned int type
)
4411 unsigned int tmp
= 0, muted
= 0;
4413 CS_DBGOUT(CS_FUNCTION
, 8, printk(KERN_INFO
4414 "cs46xx: cs46xx_powerup()+ type=0x%x\n",type
));
4416 * check for VREF and powerup if need to.
4418 if (type
& CS_POWER_MIXVON
)
4419 type
|= CS_POWER_MIXVOFF
;
4420 if (type
& (CS_POWER_DAC
| CS_POWER_ADC
))
4421 type
|= CS_POWER_MIXVON
| CS_POWER_MIXVOFF
;
4424 * Power up indicated areas.
4426 if (type
& CS_POWER_MIXVOFF
) {
4428 CS_DBGOUT(CS_FUNCTION
, 4,
4429 printk(KERN_INFO
"cs46xx: cs46xx_powerup()+ MIXVOFF\n"));
4431 * Power up the MIXER (VREF ON) on the AC97 card.
4433 tmp
= cs_ac97_get(card
->ac97_codec
[0], AC97_POWER_CONTROL
);
4434 if (!(tmp
& CS_AC97_POWER_CONTROL_MIXVOFF_ON
)) {
4436 cs_mute(card
, CS_TRUE
);
4439 tmp
&= ~CS_AC97_POWER_CONTROL_MIXVOFF
;
4440 cs_ac97_set(card
->ac97_codec
[0], AC97_POWER_CONTROL
, tmp
);
4442 * Now, we wait until we sample a ready state.
4444 for (count
= 0; count
< 32; count
++) {
4446 * First, lets wait a short while to let things settle out a
4447 * bit, and to prevent retrying the read too quickly.
4452 * Read the current state of the power control register.
4454 if (cs_ac97_get(card
->ac97_codec
[0], AC97_POWER_CONTROL
) &
4455 CS_AC97_POWER_CONTROL_MIXVOFF_ON
)
4460 * Check the status..
4462 if (!(cs_ac97_get(card
->ac97_codec
[0], AC97_POWER_CONTROL
) &
4463 CS_AC97_POWER_CONTROL_MIXVOFF_ON
)) {
4464 CS_DBGOUT(CS_ERROR
, 1, printk(KERN_WARNING
4465 "cs46xx: powerup MIXVOFF failed\n"));
4470 if(type
& CS_POWER_MIXVON
) {
4472 CS_DBGOUT(CS_FUNCTION
, 4,
4473 printk(KERN_INFO
"cs46xx: cs46xx_powerup()+ MIXVON\n"));
4475 * Power up the MIXER (VREF ON) on the AC97 card.
4477 tmp
= cs_ac97_get(card
->ac97_codec
[0], AC97_POWER_CONTROL
);
4478 if (!(tmp
& CS_AC97_POWER_CONTROL_MIXVON_ON
)) {
4480 cs_mute(card
, CS_TRUE
);
4483 tmp
&= ~CS_AC97_POWER_CONTROL_MIXVON
;
4484 cs_ac97_set(card
->ac97_codec
[0], AC97_POWER_CONTROL
, tmp
);
4486 * Now, we wait until we sample a ready state.
4488 for (count
= 0; count
< 32; count
++) {
4490 * First, lets wait a short while to let things settle out a
4491 * bit, and to prevent retrying the read too quickly.
4496 * Read the current state of the power control register.
4498 if (cs_ac97_get(card
->ac97_codec
[0], AC97_POWER_CONTROL
) &
4499 CS_AC97_POWER_CONTROL_MIXVON_ON
)
4504 * Check the status..
4506 if (!(cs_ac97_get(card
->ac97_codec
[0], AC97_POWER_CONTROL
) &
4507 CS_AC97_POWER_CONTROL_MIXVON_ON
)) {
4508 CS_DBGOUT(CS_ERROR
, 1, printk(KERN_WARNING
4509 "cs46xx: powerup MIXVON failed\n"));
4514 if (type
& CS_POWER_ADC
) {
4516 * Power up the ADC on the AC97 card.
4518 CS_DBGOUT(CS_FUNCTION
, 4, printk(KERN_INFO
"cs46xx: cs46xx_powerup()+ ADC\n"));
4519 tmp
= cs_ac97_get(card
->ac97_codec
[0], AC97_POWER_CONTROL
);
4520 if (!(tmp
& CS_AC97_POWER_CONTROL_ADC_ON
)) {
4522 cs_mute(card
, CS_TRUE
);
4525 tmp
&= ~CS_AC97_POWER_CONTROL_ADC
;
4526 cs_ac97_set(card
->ac97_codec
[0], AC97_POWER_CONTROL
, tmp
);
4529 * Now, we wait until we sample a ready state.
4531 for (count
= 0; count
< 32; count
++) {
4533 * First, lets wait a short while to let things settle out a
4534 * bit, and to prevent retrying the read too quickly.
4539 * Read the current state of the power control register.
4541 if (cs_ac97_get(card
->ac97_codec
[0], AC97_POWER_CONTROL
) &
4542 CS_AC97_POWER_CONTROL_ADC_ON
)
4547 * Check the status..
4549 if (!(cs_ac97_get(card
->ac97_codec
[0], AC97_POWER_CONTROL
) &
4550 CS_AC97_POWER_CONTROL_ADC_ON
)) {
4551 CS_DBGOUT(CS_ERROR
, 1, printk(KERN_WARNING
4552 "cs46xx: powerup ADC failed\n"));
4557 if (type
& CS_POWER_DAC
) {
4559 * Power up the DAC on the AC97 card.
4562 CS_DBGOUT(CS_FUNCTION
, 4,
4563 printk(KERN_INFO
"cs46xx: cs46xx_powerup()+ DAC\n"));
4564 tmp
= cs_ac97_get(card
->ac97_codec
[0], AC97_POWER_CONTROL
);
4565 if (!(tmp
& CS_AC97_POWER_CONTROL_DAC_ON
)) {
4567 cs_mute(card
, CS_TRUE
);
4570 tmp
&= ~CS_AC97_POWER_CONTROL_DAC
;
4571 cs_ac97_set(card
->ac97_codec
[0], AC97_POWER_CONTROL
, tmp
);
4573 * Now, we wait until we sample a ready state.
4575 for (count
= 0; count
< 32; count
++) {
4577 * First, lets wait a short while to let things settle out a
4578 * bit, and to prevent retrying the read too quickly.
4583 * Read the current state of the power control register.
4585 if (cs_ac97_get(card
->ac97_codec
[0], AC97_POWER_CONTROL
) &
4586 CS_AC97_POWER_CONTROL_DAC_ON
)
4591 * Check the status..
4593 if (!(cs_ac97_get(card
->ac97_codec
[0], AC97_POWER_CONTROL
) &
4594 CS_AC97_POWER_CONTROL_DAC_ON
)) {
4595 CS_DBGOUT(CS_ERROR
, 1, printk(KERN_WARNING
4596 "cs46xx: powerup DAC failed\n"));
4601 tmp
= cs_ac97_get(card
->ac97_codec
[0], AC97_POWER_CONTROL
);
4603 cs_mute(card
, CS_FALSE
);
4604 CS_DBGOUT(CS_FUNCTION
, 4, printk(KERN_INFO
4605 "cs46xx: cs46xx_powerup()- 0 tmp=0x%x\n",tmp
));
4609 static void cs461x_proc_start(struct cs_card
*card
)
4614 * Set the frame timer to reflect the number of cycles per frame.
4616 cs461x_poke(card
, BA1_FRMT
, 0xadf);
4618 * Turn on the run, run at frame, and DMA enable bits in the local copy of
4619 * the SP control register.
4621 cs461x_poke(card
, BA1_SPCR
, SPCR_RUN
| SPCR_RUNFR
| SPCR_DRQEN
);
4623 * Wait until the run at frame bit resets itself in the SP control
4626 for (cnt
= 0; cnt
< 25; cnt
++) {
4628 if (!(cs461x_peek(card
, BA1_SPCR
) & SPCR_RUNFR
))
4632 if (cs461x_peek(card
, BA1_SPCR
) & SPCR_RUNFR
)
4633 printk(KERN_WARNING
"cs46xx: SPCR_RUNFR never reset\n");
4636 static void cs461x_proc_stop(struct cs_card
*card
)
4639 * Turn off the run, run at frame, and DMA enable bits in the local copy of
4640 * the SP control register.
4642 cs461x_poke(card
, BA1_SPCR
, 0);
4645 static int cs_hardware_init(struct cs_card
*card
)
4647 unsigned long end_time
;
4648 unsigned int tmp
,count
;
4650 CS_DBGOUT(CS_FUNCTION
| CS_INIT
, 2, printk(KERN_INFO
4651 "cs46xx: cs_hardware_init()+\n") );
4653 * First, blast the clock control register to zero so that the PLL starts
4654 * out in a known state, and blast the master serial port control register
4655 * to zero so that the serial ports also start out in a known state.
4657 cs461x_pokeBA0(card
, BA0_CLKCR1
, 0);
4658 cs461x_pokeBA0(card
, BA0_SERMC1
, 0);
4661 * If we are in AC97 mode, then we must set the part to a host controlled
4662 * AC-link. Otherwise, we won't be able to bring up the link.
4664 cs461x_pokeBA0(card
, BA0_SERACC
, SERACC_HSP
| SERACC_CODEC_TYPE_1_03
); /* 1.03 card */
4665 /* cs461x_pokeBA0(card, BA0_SERACC, SERACC_HSP | SERACC_CODEC_TYPE_2_0); */ /* 2.00 card */
4668 * Drive the ARST# pin low for a minimum of 1uS (as defined in the AC97
4669 * spec) and then drive it high. This is done for non AC97 modes since
4670 * there might be logic external to the CS461x that uses the ARST# line
4673 cs461x_pokeBA0(card
, BA0_ACCTL
, 1);
4675 cs461x_pokeBA0(card
, BA0_ACCTL
, 0);
4677 cs461x_pokeBA0(card
, BA0_ACCTL
, ACCTL_RSTN
);
4680 * The first thing we do here is to enable sync generation. As soon
4681 * as we start receiving bit clock, we'll start producing the SYNC
4684 cs461x_pokeBA0(card
, BA0_ACCTL
, ACCTL_ESYN
| ACCTL_RSTN
);
4687 * Now wait for a short while to allow the AC97 part to start
4688 * generating bit clock (so we don't try to start the PLL without an
4691 mdelay(5 * cs_laptop_wait
); /* 1 should be enough ?? (and pigs might fly) */
4694 * Set the serial port timing configuration, so that
4695 * the clock control circuit gets its clock from the correct place.
4697 cs461x_pokeBA0(card
, BA0_SERMC1
, SERMC1_PTC_AC97
);
4700 * The part seems to not be ready for a while after a resume.
4701 * so, if we are resuming, then wait for 700 mils. Note that 600 mils
4702 * is not enough for some platforms! tested on an IBM Thinkpads and
4705 if (!(card
->pm
.flags
& CS46XX_PM_IDLE
))
4708 * Write the selected clock control setup to the hardware. Do not turn on
4709 * SWCE yet (if requested), so that the devices clocked by the output of
4710 * PLL are not clocked until the PLL is stable.
4712 cs461x_pokeBA0(card
, BA0_PLLCC
, PLLCC_LPF_1050_2780_KHZ
| PLLCC_CDR_73_104_MHZ
);
4713 cs461x_pokeBA0(card
, BA0_PLLM
, 0x3a);
4714 cs461x_pokeBA0(card
, BA0_CLKCR2
, CLKCR2_PDIVS_8
);
4719 cs461x_pokeBA0(card
, BA0_CLKCR1
, CLKCR1_PLLP
);
4722 * Wait until the PLL has stabilized.
4724 mdelay(5 * cs_laptop_wait
); /* Again 1 should be enough ?? */
4727 * Turn on clocking of the core so that we can setup the serial ports.
4729 tmp
= cs461x_peekBA0(card
, BA0_CLKCR1
) | CLKCR1_SWCE
;
4730 cs461x_pokeBA0(card
, BA0_CLKCR1
, tmp
);
4733 * Fill the serial port FIFOs with silence.
4735 cs461x_clear_serial_FIFOs(card
,CS_TYPE_DAC
| CS_TYPE_ADC
);
4738 * Set the serial port FIFO pointer to the first sample in the FIFO.
4740 /* cs461x_pokeBA0(card, BA0_SERBSP, 0); */
4743 * Write the serial port configuration to the part. The master
4744 * enable bit is not set until all other values have been written.
4746 cs461x_pokeBA0(card
, BA0_SERC1
, SERC1_SO1F_AC97
| SERC1_SO1EN
);
4747 cs461x_pokeBA0(card
, BA0_SERC2
, SERC2_SI1F_AC97
| SERC1_SO1EN
);
4748 cs461x_pokeBA0(card
, BA0_SERMC1
, SERMC1_PTC_AC97
| SERMC1_MSPE
);
4751 mdelay(5 * cs_laptop_wait
); /* Shouldnt be needed ?? */
4754 * If we are resuming under 2.2.x then we can not schedule a timeout.
4755 * so, just spin the CPU.
4757 if (card
->pm
.flags
& CS46XX_PM_IDLE
) {
4759 * Wait for the card ready signal from the AC97 card.
4761 end_time
= jiffies
+ 3 * (HZ
>> 2);
4764 * Read the AC97 status register to see if we've seen a CODEC READY
4765 * signal from the AC97 card.
4767 if (cs461x_peekBA0(card
, BA0_ACSTS
) & ACSTS_CRDY
)
4769 current
->state
= TASK_UNINTERRUPTIBLE
;
4770 schedule_timeout(1);
4771 } while (time_before(jiffies
, end_time
));
4773 for (count
= 0; count
< 100; count
++) {
4774 // First, we want to wait for a short time.
4775 udelay(25 * cs_laptop_wait
);
4777 if (cs461x_peekBA0(card
, BA0_ACSTS
) & ACSTS_CRDY
)
4783 * Make sure CODEC is READY.
4785 if (!(cs461x_peekBA0(card
, BA0_ACSTS
) & ACSTS_CRDY
)) {
4786 CS_DBGOUT(CS_ERROR
| CS_INIT
, 1, printk(KERN_WARNING
4787 "cs46xx: create - never read card ready from AC'97\n"));
4788 CS_DBGOUT(CS_ERROR
| CS_INIT
, 1, printk(KERN_WARNING
4789 "cs46xx: probably not a bug, try using the CS4232 driver,\n"));
4790 CS_DBGOUT(CS_ERROR
| CS_INIT
, 1, printk(KERN_WARNING
4791 "cs46xx: or turn off any automatic Power Management support in the BIOS.\n"));
4796 * Assert the vaid frame signal so that we can start sending commands
4799 cs461x_pokeBA0(card
, BA0_ACCTL
, ACCTL_VFRM
| ACCTL_ESYN
| ACCTL_RSTN
);
4801 if (card
->pm
.flags
& CS46XX_PM_IDLE
) {
4803 * Wait until we've sampled input slots 3 and 4 as valid, meaning that
4804 * the card is pumping ADC data across the AC-link.
4806 end_time
= jiffies
+ 3 * (HZ
>> 2);
4809 * Read the input slot valid register and see if input slots 3 and
4812 if ((cs461x_peekBA0(card
, BA0_ACISV
) & (ACISV_ISV3
| ACISV_ISV4
)) == (ACISV_ISV3
| ACISV_ISV4
))
4814 current
->state
= TASK_UNINTERRUPTIBLE
;
4815 schedule_timeout(1);
4816 } while (time_before(jiffies
, end_time
));
4818 for (count
= 0; count
< 100; count
++) {
4819 // First, we want to wait for a short time.
4820 udelay(25 * cs_laptop_wait
);
4822 if ((cs461x_peekBA0(card
, BA0_ACISV
) & (ACISV_ISV3
| ACISV_ISV4
)) == (ACISV_ISV3
| ACISV_ISV4
))
4827 * Make sure input slots 3 and 4 are valid. If not, then return
4830 if ((cs461x_peekBA0(card
, BA0_ACISV
) & (ACISV_ISV3
| ACISV_ISV4
)) != (ACISV_ISV3
| ACISV_ISV4
)) {
4831 printk(KERN_WARNING
"cs46xx: create - never read ISV3 & ISV4 from AC'97\n");
4836 * Now, assert valid frame and the slot 3 and 4 valid bits. This will
4837 * commense the transfer of digital audio data to the AC97 card.
4839 cs461x_pokeBA0(card
, BA0_ACOSV
, ACOSV_SLV3
| ACOSV_SLV4
);
4842 * Turn off the Processor by turning off the software clock enable flag in
4843 * the clock control register.
4845 /* tmp = cs461x_peekBA0(card, BA0_CLKCR1) & ~CLKCR1_SWCE; */
4846 /* cs461x_pokeBA0(card, BA0_CLKCR1, tmp); */
4849 * Reset the processor.
4854 * Download the image to the processor.
4857 cs461x_download_image(card
);
4860 * Stop playback DMA.
4862 tmp
= cs461x_peek(card
, BA1_PCTL
);
4863 card
->pctl
= tmp
& 0xffff0000;
4864 cs461x_poke(card
, BA1_PCTL
, tmp
& 0x0000ffff);
4869 tmp
= cs461x_peek(card
, BA1_CCTL
);
4870 card
->cctl
= tmp
& 0x0000ffff;
4871 cs461x_poke(card
, BA1_CCTL
, tmp
& 0xffff0000);
4873 /* initialize AC97 codec and register /dev/mixer */
4874 if (card
->pm
.flags
& CS46XX_PM_IDLE
) {
4875 if (cs_ac97_init(card
) <= 0) {
4876 CS_DBGOUT(CS_ERROR
| CS_INIT
, 1, printk(KERN_INFO
4877 "cs46xx: cs_ac97_init() failure\n"));
4881 cs46xx_ac97_resume(card
);
4884 cs461x_proc_start(card
);
4887 * Enable interrupts on the part.
4889 cs461x_pokeBA0(card
, BA0_HICR
, HICR_IEV
| HICR_CHGM
);
4891 tmp
= cs461x_peek(card
, BA1_PFIE
);
4893 cs461x_poke(card
, BA1_PFIE
, tmp
); /* playback interrupt enable */
4895 tmp
= cs461x_peek(card
, BA1_CIE
);
4898 cs461x_poke(card
, BA1_CIE
, tmp
); /* capture interrupt enable */
4901 * If IDLE then Power down the part. We will power components up
4902 * when we need them.
4904 if (card
->pm
.flags
& CS46XX_PM_IDLE
) {
4905 if (!cs_powerdown
) {
4906 if ((tmp
= cs46xx_powerup(card
, CS_POWER_DAC
| CS_POWER_ADC
|
4907 CS_POWER_MIXVON
))) {
4908 CS_DBGOUT(CS_ERROR
| CS_INIT
, 1, printk(KERN_INFO
4909 "cs46xx: cs461x_powerup() failure (0x%x)\n",tmp
) );
4913 if ((tmp
= cs461x_powerdown(card
, CS_POWER_DAC
| CS_POWER_ADC
|
4914 CS_POWER_MIXVON
, CS_FALSE
))) {
4915 CS_DBGOUT(CS_ERROR
| CS_INIT
, 1, printk(KERN_INFO
4916 "cs46xx: cs461x_powerdown() failure (0x%x)\n",tmp
) );
4921 CS_DBGOUT(CS_FUNCTION
| CS_INIT
, 2, printk(KERN_INFO
4922 "cs46xx: cs_hardware_init()- 0\n"));
4926 /* install the driver, we do not allocate hardware channel nor DMA buffer now, they are defered
4927 until "ACCESS" time (in prog_dmabuf called by open/read/write/ioctl/mmap) */
4938 void (*amp
)(struct cs_card
*, int);
4939 void (*amp_init
)(struct cs_card
*);
4940 void (*active
)(struct cs_card
*, int);
4943 static struct cs_card_type cards
[] = {
4947 .name
= "Genius Soundmaker 128 value",
4959 .name
= "Mitac MI6020/21",
4965 .name
= "Hercules Game Theatre XP",
4966 .amp
= amp_hercules
,
4971 .name
= "Hercules Game Theatre XP",
4972 .amp
= amp_hercules
,
4977 .name
= "Hercules Game Theatre XP",
4978 .amp
= amp_hercules
,
4983 .name
= "Hercules Game Theatre XP",
4984 .amp
= amp_hercules
,
4989 .name
= "Hercules Game Theatre XP",
4990 .amp
= amp_hercules
,
4995 .name
= "Hercules Game Theatre XP",
4996 .amp
= amp_hercules
,
5001 .name
= "Hercules Fortissimo II",
5004 /* Not sure if the 570 needs the clkrun hack */
5006 .vendor
= PCI_VENDOR_ID_IBM
,
5008 .name
= "Thinkpad 570",
5010 .active
= clkrun_hack
,
5013 .vendor
= PCI_VENDOR_ID_IBM
,
5015 .name
= "Thinkpad 600X/A20/T20",
5017 .active
= clkrun_hack
,
5020 .vendor
= PCI_VENDOR_ID_IBM
,
5022 .name
= "Thinkpad 600E (unsupported)",
5025 .name
= "Card without SSID set",
5030 MODULE_AUTHOR("Alan Cox <alan@redhat.com>, Jaroslav Kysela, <pcaudio@crystal.cirrus.com>");
5031 MODULE_DESCRIPTION("Crystal SoundFusion Audio Support");
5032 MODULE_LICENSE("GPL");
5034 static const char cs46xx_banner
[] = KERN_INFO
"Crystal 4280/46xx + AC97 Audio, version " CS46XX_MAJOR_VERSION
"." CS46XX_MINOR_VERSION
"." CS46XX_ARCH
", " __TIME__
" " __DATE__
"\n";
5035 static const char fndmsg
[] = KERN_INFO
"cs46xx: Found %d audio device(s).\n";
5037 static int __devinit
cs46xx_probe(struct pci_dev
*pci_dev
,
5038 const struct pci_device_id
*pciid
)
5041 u16 ss_card
, ss_vendor
;
5042 struct cs_card
*card
;
5043 dma_addr_t dma_mask
;
5044 struct cs_card_type
*cp
= &cards
[0];
5046 CS_DBGOUT(CS_FUNCTION
| CS_INIT
, 2,
5047 printk(KERN_INFO
"cs46xx: probe()+\n"));
5049 dma_mask
= 0xffffffff; /* this enables playback and recording */
5050 if (pci_enable_device(pci_dev
)) {
5051 CS_DBGOUT(CS_INIT
| CS_ERROR
, 1, printk(KERN_ERR
5052 "cs46xx: pci_enable_device() failed\n"));
5055 if (!RSRCISMEMORYREGION(pci_dev
, 0) ||
5056 !RSRCISMEMORYREGION(pci_dev
, 1)) {
5057 CS_DBGOUT(CS_ERROR
, 1, printk(KERN_ERR
5058 "cs46xx: probe()- Memory region not assigned\n"));
5061 if (pci_dev
->irq
== 0) {
5062 CS_DBGOUT(CS_ERROR
, 1, printk(KERN_ERR
5063 "cs46xx: probe() IRQ not assigned\n"));
5066 if (!pci_dma_supported(pci_dev
, 0xffffffff)) {
5067 CS_DBGOUT(CS_ERROR
, 1, printk(KERN_ERR
5068 "cs46xx: probe() architecture does not support 32bit PCI busmaster DMA\n"));
5071 pci_read_config_word(pci_dev
, PCI_SUBSYSTEM_VENDOR_ID
, &ss_vendor
);
5072 pci_read_config_word(pci_dev
, PCI_SUBSYSTEM_ID
, &ss_card
);
5074 if ((card
= kmalloc(sizeof(struct cs_card
), GFP_KERNEL
)) == NULL
) {
5075 printk(KERN_ERR
"cs46xx: out of memory\n");
5078 memset(card
, 0, sizeof(*card
));
5079 card
->ba0_addr
= RSRCADDRESS(pci_dev
, 0);
5080 card
->ba1_addr
= RSRCADDRESS(pci_dev
, 1);
5081 card
->pci_dev
= pci_dev
;
5082 card
->irq
= pci_dev
->irq
;
5083 card
->magic
= CS_CARD_MAGIC
;
5084 spin_lock_init(&card
->lock
);
5085 spin_lock_init(&card
->ac97_lock
);
5087 pci_set_master(pci_dev
);
5089 printk(cs46xx_banner
);
5090 printk(KERN_INFO
"cs46xx: Card found at 0x%08lx and 0x%08lx, IRQ %d\n",
5091 card
->ba0_addr
, card
->ba1_addr
, card
->irq
);
5093 card
->alloc_pcm_channel
= cs_alloc_pcm_channel
;
5094 card
->alloc_rec_pcm_channel
= cs_alloc_rec_pcm_channel
;
5095 card
->free_pcm_channel
= cs_free_pcm_channel
;
5096 card
->amplifier_ctrl
= amp_none
;
5097 card
->active_ctrl
= amp_none
;
5101 if (cp
->vendor
== ss_vendor
&& cp
->id
== ss_card
) {
5102 card
->amplifier_ctrl
= cp
->amp
;
5104 card
->active_ctrl
= cp
->active
;
5106 card
->amp_init
= cp
->amp_init
;
5111 if (cp
->name
== NULL
) {
5112 printk(KERN_INFO
"cs46xx: Unknown card (%04X:%04X) at 0x%08lx/0x%08lx, IRQ %d\n",
5113 ss_vendor
, ss_card
, card
->ba0_addr
, card
->ba1_addr
, card
->irq
);
5115 printk(KERN_INFO
"cs46xx: %s (%04X:%04X) at 0x%08lx/0x%08lx, IRQ %d\n",
5116 cp
->name
, ss_vendor
, ss_card
, card
->ba0_addr
, card
->ba1_addr
, card
->irq
);
5119 if (card
->amplifier_ctrl
== NULL
) {
5120 card
->amplifier_ctrl
= amp_none
;
5121 card
->active_ctrl
= clkrun_hack
;
5124 if (external_amp
== 1) {
5125 printk(KERN_INFO
"cs46xx: Crystal EAPD support forced on.\n");
5126 card
->amplifier_ctrl
= amp_voyetra
;
5129 if (thinkpad
== 1) {
5130 printk(KERN_INFO
"cs46xx: Activating CLKRUN hack for Thinkpad.\n");
5131 card
->active_ctrl
= clkrun_hack
;
5134 * The thinkpads don't work well without runtime updating on their kernel
5135 * delay values (or any laptop with variable CPU speeds really).
5136 * so, just to be safe set the init delay to 2100. Eliminates
5137 * failures on T21 Thinkpads. remove this code when the udelay
5138 * and mdelay kernel code is replaced by a pm timer, or the delays
5139 * work well for battery and/or AC power both.
5141 if (card
->active_ctrl
== clkrun_hack
) {
5145 if ((card
->active_ctrl
== clkrun_hack
) && !(powerdown
== 1)) {
5147 * for some currently unknown reason, powering down the DAC and ADC component
5148 * blocks on thinkpads causes some funky behavior... distoorrrtion and ac97
5149 * codec access problems. probably the serial clock becomes unsynced.
5150 * added code to sync the chips back up, but only helped about 70% the time.
5156 card
->active_ctrl(card
, 1);
5158 /* claim our iospace and irq */
5160 card
->ba0
= ioremap_nocache(card
->ba0_addr
, CS461X_BA0_SIZE
);
5161 card
->ba1
.name
.data0
= ioremap_nocache(card
->ba1_addr
+ BA1_SP_DMEM0
, CS461X_BA1_DATA0_SIZE
);
5162 card
->ba1
.name
.data1
= ioremap_nocache(card
->ba1_addr
+ BA1_SP_DMEM1
, CS461X_BA1_DATA1_SIZE
);
5163 card
->ba1
.name
.pmem
= ioremap_nocache(card
->ba1_addr
+ BA1_SP_PMEM
, CS461X_BA1_PRG_SIZE
);
5164 card
->ba1
.name
.reg
= ioremap_nocache(card
->ba1_addr
+ BA1_SP_REG
, CS461X_BA1_REG_SIZE
);
5166 CS_DBGOUT(CS_INIT
, 4, printk(KERN_INFO
5167 "cs46xx: card=%p card->ba0=%p\n",card
,card
->ba0
) );
5168 CS_DBGOUT(CS_INIT
, 4, printk(KERN_INFO
5169 "cs46xx: card->ba1=%p %p %p %p\n",
5170 card
->ba1
.name
.data0
,
5171 card
->ba1
.name
.data1
,
5172 card
->ba1
.name
.pmem
,
5173 card
->ba1
.name
.reg
) );
5175 if (card
->ba0
== 0 || card
->ba1
.name
.data0
== 0 ||
5176 card
->ba1
.name
.data1
== 0 || card
->ba1
.name
.pmem
== 0 ||
5177 card
->ba1
.name
.reg
== 0)
5180 if (request_irq(card
->irq
, &cs_interrupt
, SA_SHIRQ
, "cs46xx", card
)) {
5181 printk(KERN_ERR
"cs46xx: unable to allocate irq %d\n", card
->irq
);
5184 /* register /dev/dsp */
5185 if ((card
->dev_audio
= register_sound_dsp(&cs461x_fops
, -1)) < 0) {
5186 printk(KERN_ERR
"cs46xx: unable to register dsp\n");
5190 /* register /dev/midi */
5191 if ((card
->dev_midi
= register_sound_midi(&cs_midi_fops
, -1)) < 0)
5192 printk(KERN_ERR
"cs46xx: unable to register midi\n");
5194 card
->pm
.flags
|= CS46XX_PM_IDLE
;
5195 for (i
= 0; i
< 5; i
++) {
5196 if (cs_hardware_init(card
) != 0) {
5197 CS_DBGOUT(CS_ERROR
, 4, printk(
5198 "cs46xx: ERROR in cs_hardware_init()... retrying\n"));
5199 for (j
= 0; j
< NR_AC97
; j
++)
5200 if (card
->ac97_codec
[j
] != NULL
) {
5201 unregister_sound_mixer(card
->ac97_codec
[j
]->dev_mixer
);
5202 ac97_release_codec(card
->ac97_codec
[j
]);
5204 mdelay(10 * cs_laptop_wait
);
5210 CS_DBGOUT(CS_PM
| CS_ERROR
, 1, printk(
5211 "cs46xx: cs46xx_probe()- cs_hardware_init() failed, retried %d times.\n",i
));
5212 unregister_sound_dsp(card
->dev_audio
);
5214 unregister_sound_midi(card
->dev_midi
);
5218 init_waitqueue_head(&card
->midi
.open_wait
);
5219 mutex_init(&card
->midi
.open_mutex
);
5220 init_waitqueue_head(&card
->midi
.iwait
);
5221 init_waitqueue_head(&card
->midi
.owait
);
5222 cs461x_pokeBA0(card
, BA0_MIDCR
, MIDCR_MRST
);
5223 cs461x_pokeBA0(card
, BA0_MIDCR
, 0);
5226 * Check if we have to init the amplifier, but probably already done
5227 * since the CD logic in the ac97 init code will turn on the ext amp.
5231 card
->active_ctrl(card
, -1);
5233 PCI_SET_DRIVER_DATA(pci_dev
, card
);
5234 PCI_SET_DMA_MASK(pci_dev
, dma_mask
);
5235 list_add(&card
->list
, &cs46xx_devs
);
5237 CS_DBGOUT(CS_PM
, 9, printk(KERN_INFO
"cs46xx: pm.flags=0x%x card=%p\n",
5238 (unsigned)card
->pm
.flags
,card
));
5240 CS_DBGOUT(CS_INIT
| CS_FUNCTION
, 2, printk(KERN_INFO
5241 "cs46xx: probe()- device allocated successfully\n"));
5245 free_irq(card
->irq
, card
);
5249 if (card
->ba1
.name
.data0
)
5250 iounmap(card
->ba1
.name
.data0
);
5251 if (card
->ba1
.name
.data1
)
5252 iounmap(card
->ba1
.name
.data1
);
5253 if (card
->ba1
.name
.pmem
)
5254 iounmap(card
->ba1
.name
.pmem
);
5255 if (card
->ba1
.name
.reg
)
5256 iounmap(card
->ba1
.name
.reg
);
5258 CS_DBGOUT(CS_INIT
| CS_ERROR
, 1, printk(KERN_INFO
5259 "cs46xx: probe()- no device allocated\n"));
5263 // ---------------------------------------------------------------------
5265 static void __devexit
cs46xx_remove(struct pci_dev
*pci_dev
)
5267 struct cs_card
*card
= PCI_GET_DRIVER_DATA(pci_dev
);
5271 CS_DBGOUT(CS_INIT
| CS_FUNCTION
, 2, printk(KERN_INFO
5272 "cs46xx: cs46xx_remove()+\n"));
5274 card
->active_ctrl(card
,1);
5276 tmp
= cs461x_peek(card
, BA1_PFIE
);
5279 cs461x_poke(card
, BA1_PFIE
, tmp
); /* playback interrupt disable */
5281 tmp
= cs461x_peek(card
, BA1_CIE
);
5284 cs461x_poke(card
, BA1_CIE
, tmp
); /* capture interrupt disable */
5287 * Stop playback DMA.
5289 tmp
= cs461x_peek(card
, BA1_PCTL
);
5290 cs461x_poke(card
, BA1_PCTL
, tmp
& 0x0000ffff);
5295 tmp
= cs461x_peek(card
, BA1_CCTL
);
5296 cs461x_poke(card
, BA1_CCTL
, tmp
& 0xffff0000);
5299 * Reset the processor.
5303 cs461x_proc_stop(card
);
5306 * Power down the DAC and ADC. We will power them up (if) when we need
5309 if ((tmp
= cs461x_powerdown(card
, CS_POWER_DAC
| CS_POWER_ADC
|
5310 CS_POWER_MIXVON
, CS_TRUE
))) {
5311 CS_DBGOUT(CS_ERROR
| CS_INIT
, 1, printk(KERN_INFO
5312 "cs46xx: cs461x_powerdown() failure (0x%x)\n",tmp
) );
5316 * Power down the PLL.
5318 cs461x_pokeBA0(card
, BA0_CLKCR1
, 0);
5321 * Turn off the Processor by turning off the software clock enable flag in
5322 * the clock control register.
5324 tmp
= cs461x_peekBA0(card
, BA0_CLKCR1
) & ~CLKCR1_SWCE
;
5325 cs461x_pokeBA0(card
, BA0_CLKCR1
, tmp
);
5327 card
->active_ctrl(card
,-1);
5329 /* free hardware resources */
5330 free_irq(card
->irq
, card
);
5332 iounmap(card
->ba1
.name
.data0
);
5333 iounmap(card
->ba1
.name
.data1
);
5334 iounmap(card
->ba1
.name
.pmem
);
5335 iounmap(card
->ba1
.name
.reg
);
5337 /* unregister audio devices */
5338 for (i
= 0; i
< NR_AC97
; i
++)
5339 if (card
->ac97_codec
[i
] != NULL
) {
5340 unregister_sound_mixer(card
->ac97_codec
[i
]->dev_mixer
);
5341 ac97_release_codec(card
->ac97_codec
[i
]);
5343 unregister_sound_dsp(card
->dev_audio
);
5345 unregister_sound_midi(card
->dev_midi
);
5346 list_del(&card
->list
);
5348 PCI_SET_DRIVER_DATA(pci_dev
,NULL
);
5350 CS_DBGOUT(CS_INIT
| CS_FUNCTION
, 2, printk(KERN_INFO
5351 "cs46xx: cs46xx_remove()-: remove successful\n"));
5356 CS46XX_4612
, /* same as 4630 */
5357 CS46XX_4615
, /* same as 4624 */
5360 static struct pci_device_id cs46xx_pci_tbl
[] = {
5362 .vendor
= PCI_VENDOR_ID_CIRRUS
,
5363 .device
= PCI_DEVICE_ID_CIRRUS_4610
,
5364 .subvendor
= PCI_ANY_ID
,
5365 .subdevice
= PCI_ANY_ID
,
5366 .driver_data
= CS46XX_4610
,
5369 .vendor
= PCI_VENDOR_ID_CIRRUS
,
5370 .device
= PCI_DEVICE_ID_CIRRUS_4612
,
5371 .subvendor
= PCI_ANY_ID
,
5372 .subdevice
= PCI_ANY_ID
,
5373 .driver_data
= CS46XX_4612
,
5376 .vendor
= PCI_VENDOR_ID_CIRRUS
,
5377 .device
= PCI_DEVICE_ID_CIRRUS_4615
,
5378 .subvendor
= PCI_ANY_ID
,
5379 .subdevice
= PCI_ANY_ID
,
5380 .driver_data
= CS46XX_4615
,
5385 MODULE_DEVICE_TABLE(pci
, cs46xx_pci_tbl
);
5387 static struct pci_driver cs46xx_pci_driver
= {
5389 .id_table
= cs46xx_pci_tbl
,
5390 .probe
= cs46xx_probe
,
5391 .remove
= __devexit_p(cs46xx_remove
),
5392 .suspend
= CS46XX_SUSPEND_TBL
,
5393 .resume
= CS46XX_RESUME_TBL
,
5396 static int __init
cs46xx_init_module(void)
5399 CS_DBGOUT(CS_INIT
| CS_FUNCTION
, 2, printk(KERN_INFO
5400 "cs46xx: cs46xx_init_module()+ \n"));
5401 rtn
= pci_register_driver(&cs46xx_pci_driver
);
5403 if (rtn
== -ENODEV
) {
5404 CS_DBGOUT(CS_ERROR
| CS_INIT
, 1, printk(
5405 "cs46xx: Unable to detect valid cs46xx device\n"));
5408 CS_DBGOUT(CS_INIT
| CS_FUNCTION
, 2,
5409 printk(KERN_INFO
"cs46xx: cs46xx_init_module()- (%d)\n",rtn
));
5413 static void __exit
cs46xx_cleanup_module(void)
5415 pci_unregister_driver(&cs46xx_pci_driver
);
5416 CS_DBGOUT(CS_INIT
| CS_FUNCTION
, 2,
5417 printk(KERN_INFO
"cs46xx: cleanup_cs46xx() finished\n"));
5420 module_init(cs46xx_init_module
);
5421 module_exit(cs46xx_cleanup_module
);
5423 #if CS46XX_ACPI_SUPPORT
5424 static int cs46xx_suspend_tbl(struct pci_dev
*pcidev
, pm_message_t state
)
5426 struct cs_card
*s
= PCI_GET_DRIVER_DATA(pcidev
);
5427 CS_DBGOUT(CS_PM
| CS_FUNCTION
, 2,
5428 printk(KERN_INFO
"cs46xx: cs46xx_suspend_tbl request\n"));
5429 cs46xx_suspend(s
, state
);
5433 static int cs46xx_resume_tbl(struct pci_dev
*pcidev
)
5435 struct cs_card
*s
= PCI_GET_DRIVER_DATA(pcidev
);
5436 CS_DBGOUT(CS_PM
| CS_FUNCTION
, 2,
5437 printk(KERN_INFO
"cs46xx: cs46xx_resume_tbl request\n"));